Constructing a Simple Model: Solid Diffusion

Jacob Kelter
Chemistry, Physics
60
Teachers and Professors 
v1

Overview

This lesson introduces the ABM design process as well as a number of important ABM concepts by guiding the learner through constructing a simple ABM of solid diffusion. 

Standards

Computational Thinking in STEM
  • Modeling and Simulation Practices
    • Designing Computational Models
    • Using Computational Models to Understand a Concept
    • Constructing Computational Models
  • Computational Problem Solving Practices
    • Assessing Different Approaches/Solutions to a Problem
    • Creating Computational Abstractions
    • Computer Programming
    • Preparing Problems for Computational Solutions

Activities

  • 1. Choosing agents, environment and setup
  • 2. Implementing the setup procedure
  • 3. Choose agent behaviors, time step and pseudo-code of go procedure
  • 4. Implementing the Go Procedure
  • 5. Choose measures and implement them
  • 6. Optional Extensions and Wrap-Up

Student Directions and Resources


In this lesson we are going to make a simple agent-based model from scratch following a top-down design process. "The top-down design process starts by choosing a phenomenon or situation that you want to model or coming up with a question that you want to answer, and then designing agents and rules of behavior that model the elements of the situation. You then refine that conceptual model and continue to revise it until it is at a fine enough level of detail that you can see how to write the code for the model" (An Introduction to Agent-Based Modeling pg. 160). 

We are going to model diffusion in solids. Usually, the phenomenon we are trying to model has "a characteristic pattern, known as a reference pattern" (ibid. pg 159). Our goal is to create a model that somehow captures the reference pattern. In this case, the reference pattern will be diffusion concentration profiles. There are multiple types of diffusion in solids, but we will start simply in accordance with the ABM design principle: "Start simple and build toward the question you want to answer" (ibid.). So, our goal for now is not to create a model that makes accurate quantitative predictions about real materials. Rather, we are just trying to make a model that gives qualitatively similar results to explain the phenomenon.

The reference pattern we will compare against is the following graph which shows concentrations of nickle and copper in a copper-nickle diffusion couple. In this case the diffusion couple was of thin films, but that doesn't matter for us, because we are only trying to qualitatively reproduce the behavior. 


Abdul-Lettif, A. M. Investigation of interdiffusion in copper–nickel bilayer thin films. Physica B: Condensed Matter 388, 107–111 (2007).

1. Choosing agents, environment and setup


This lesson is structured to go through the steps of designing an ABM. For each step, you will first try it on your own. Then, you'll get to see the way the authors of this lesson would do it. It doesn't mean your way is wrong if it is different than ours, because there are always multiple ways to model something, but it will be helpful to have something to compare against.

In accordance with the ABM design principle to start simple and build toward the question you want to answer, our model will be of a solid diffusion couple of two very similarly sized elements (in fact we will treat them as the same size for now) and we will model the atomic lattice in two dimensions using a square lattice.

The Fire Model you worked with is below as a reference, because some of the steps reference it as an example.

 


Question 1.1

The first step is to choose the agents of the model. For example, in the Fire model, the agents are fires and trees. Turtles are used to represent fires and patches acted as sites on which a tree may or may not exist. What should the agents of the diffusion model be?



Question 1.2

The next step is choosing agent properties. For example, in the Fire model, patches can be green or black to represent a tree or an empty spot in the forest. Turtles are either "fires" or "embers". They were represented as different "breeds" in the model. A breed is a specialized type of turtle to help distinguish between different types of agents in the model. "Fire" and "ember" breeds have different colors. They also have a location (as all turtles do), but it never changes. In the Fire model, all the agents only used default properties (e.g. color), but they were chosen to represent different things. It is possible to give agents additional properties, but in this case it wasn't necessary.

What should the properties of the agents in the diffusion model be? Remember to start simple.



Question 1.3

Before continuing with the design of the model, we will implement what we have so far in a setup procedure. When you are first designing a model, it can be helpful to first write the model setup in "pseudo-code" which just means that you write it in a code-like way without worrying about getting the commands right. For example, the setup procedure for the Fire model in pseudo-code could be:

  • Ask patches to turn green with a probability equal to the "density" parameter
  • Ask patches on the left edge to ignite (which creates turtles of the "fire" breed).

Write a setup procedure in pseudo code for the diffusion model. Remember we are modeling a solid diffusion couple of two similar sized elements, with a two dimensional square lattice.



Question 1.4

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



2. Implementing the setup procedure


Here is one way to choose the agents and agent-properties and write a pseudo-code setup procedure

Agents: a-type atoms  and b-type atoms (turtles), lattice sites (patches)

Agent properties: location (atoms)

Pseudo-code setup procedure: 

  • Ask patches on the left half of the view to sprout type 1 atoms
  • Ask patches on the right half of the view to sprout type 2 atoms.

As you can see, this is quite simple. For now, we won't need any more agent properties besides the default property of location which all turtles already have in the form of an xcor and ycor. We will also use color to tell the two types of atoms apart. 



Question 2.1

Implement a setup procedure in NetLogo code based on the pseudo-code. You can use the pseudo-code above, or if yours is different and you think it is better, use that. When you have it working, copy your code here. (Make sure to check out sprout command if you haven't already). It is recommended to use NetLogo Desktop for this whole lesson, because there are parts at the end which are not yet supported on NetLogo Web.



Question 2.2

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



3. Choose agent behaviors, time step and pseudo-code of go procedure


One way to implement the pseudo-code for the setup procedure in the previous section is in the model below. Compare it with yours, and make any changes you want. The choice to make the atoms square is purely aesthetic to match the square lattice. If you prefer circles or another shape that's fine. Compare this setup procedure to yours and make any changes you want. Then continue below to choose agent behaviors and write pseudo-code for the go procedure. If you are unfamiliar with any of the commands in the setup procedure, look them up in the Programming Guide.


Question 3.1

The next step is to choose agent behaviors. In the Fire model the agent behaviors are:

  • ignite: fires light neighboring unburnt trees on fire
  • fade-embers: fires burn out

What should the agent behaviors in the diffusion model be? Answer in the box below. 

(the Fire model is here for reference)



Question 3.2

Designing a time step

Now we will design a time step. This just mean writing down everything that happens each tick. This can be written as pseudo-code for a go procedure. For the fire model, this could be written as:

At each tick:

  • If I'm a fire I light neighboring trees on fire and then turn to an ember
  • If I'm an ember, I fade

Writing the behaviors at each tick from the perspective agents can make it easier to reason about, but you can use whatever phrasing makes the most sense to you.

Try writing pseudo-code for what should happen at each tick in the diffusion model.



Question 3.3

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



4. Implementing the Go Procedure


Here is one way to write pseudo-code for a go procedure in the Diffusion model:

At each tick:

  • If I am an empty patch (lattice site), I ask one of the neighboring atoms to move here. 

It may seem strange to have empty lattice sites be the active agents here as opposed to the atoms, but there is at least one good reason to do it this way: it is computationally more efficient to have each patch check if it is empty than to have each atom check if any of its neighboring patches are empty. The latter option would result in many patches getting checked multiple times. Furthermore, it is totally reasonable to think about vacancies moving around the lattice.


Question 4.1

Implement a Go procedure in NetLogo code based on the pseudo-code. You can use the pseudo-code above, or if yours is different and you think it is better, use that. When you have it working, copy your code here.

 

Here are some commands you may find useful:  with, not, any?, turtles-here, one-of, turtles-on, neighbors4, nobody, move-to, myself. Look them up in the Programming Guide if you don't know what they do.



Question 4.2

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



5. Choose measures and implement them


One way to implement the pseudo-code for the Go procedure in the previous section is in the model below. Compare it with yours, and make any changes you want. If you run the model for a few seconds, you will notice vacancies on the edge of the view disappearing and reappearing on the opposite edge of the view. This is because by default, the patches in NetLogo "wrap around." Unless we are modeling the diffusion of two half-toroids, this isn't what we want. To change this setting:

  • If you are using NetLogo Desktop click the Settings button on the top right of the interface and uncheck the boxes for "World wraps horizontally" and "World wraps vertically."
  • If you are using NetLogo Web, go into Authoring mode, then right-click the view (the box of patches) and click "edit." Then uncheck the boxes that say "wraps horizontally" and "wraps vertically."

 

Now it is time to make some plots to check if our model matches the reference pattern. 

 


Question 5.1

First we'll make a plot of the maximum diffusion distance so that you can learn how to make plots. Then you will make a plot on your own to match the reference pattern.

Right now, there is no way to create plots directly in NetLogo Web, so you'll have to use NetLogo Desktop.

For one dimensional diffusion, there is a known linear relationship between the squared diffusion distance and time. We'll make a plot of the squared maximum diffusion distance over time, and it should show an approximately linear relationship. 

To create a plot, right-click on the white area and select plot. You can also click the add button on the top of the interface with the selector next to it set to "plot." Enter this code in the Pen update command box:  plot (max([pxcor] of a-atoms) - min([pxcor] of b-atoms)) ^ 2. And change the title to Max Diffusion Distance. 

Your final plot box should look like this:

Here is what's going on in the plot command:

  • plot: plots the new point (full documentation)
  • [pxcor] of a-atoms: returns a list of the pxcor of the patch that each a-atom is on
  • max(list) and min(list): return the maximum and minimum value of a list respectively 
  • number ^ 2: squares the numberis really "to the power" so if you changed the 2 to a 3, it would cube the number.

Run the model with the new graph. Is the relationship approximately linear? Why isn't it exactly linear?



Question 5.2

Now you will make a graph of reference pattern, shown again below for reference. There are multiple way to do this, but you may want to use a histogram. You use this command inside the Pen update commands box instead of the plot command. You can add a second line to the graph by clicking the add pen button in the plot editing pane. You also may need the set-plot-x-range command if you use a histogram.  Copy the code from you pen update commands below. 

Hint: Since we are just matching the graph qualitatively, you don't need to use concentrations. You can just use a count of the number of atoms. The next page will show one solution.



Question 5.3

If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.



6. Optional Extensions and Wrap-Up


Here is one way to plot the distribution of atoms:

The Solid Diffusion model in the models library uses a different (more complicated) method to get a slightly different looking graph. Both are shown in the model below, and the code for the Models Library version is in the procedure plot-atoms in the code tab. 


Question 6.1

What do you think about the two different ways of plotting the number of atoms per column? What are the advantages and disadvantages of each?



Question 6.2

If you have time and are curious, here are some ideas for extending the model. If you choose to do any of these, describe what you did below:

  • The model uses a very simple initial state in which there is always exactly one column of vacancies and they are all located in the middle. Try adding settings (e.g. a slider) that dictate how many vacancies there are and where they start out.

  • Give the two metals, or the two sides of the world, different characteristics. For example, a temperature difference could be simulated by making atomic movements on one side happen less often than on the other. Or you could make the atoms different sizes and make them move with a probability less than one. 



Question 6.3

How long did it take you to go through the lesson?



Question 6.4

Were there any parts of the lesson that were confusing? Were there parts you think you could have done significantly faster with a little more explanation?



Question 6.5

What was your favorite part(s) of the lesson and what did you learn from it?



Question 6.6

Is there anything you think should be added to the lesson?



Question 6.7

You can enter any other feedback you have below.