-
Notifications
You must be signed in to change notification settings - Fork 149
FiPyWorkbook
The study of kinetics often involves solving differential equations. This part of the wiki will lay out a short series of tutorials that will help you learn how to solve differential equations with a program called Fipy. We'll start by looking at how computers solve equations. Next we'll step through a simple Fipy script line by line. After you see how the scripts are constructed you'll be able to write your own solutions. It's not as hard as it sounds, once you understand how.
There are two basic strategies for solving differential equations. An “analytic solution” is an exact solution. To get an analytic solution a human brain uses some analysis (techniques you learned in algebra or trigonometry for example) to get the answer. The equation
has the analytic solution
The other strategy is to solve the differential equation “numerically.” This is how a computer program solves equations. In this section we are going to write a computer program to numerically solve the simple example above and we’ll see some differences. Numerical solutions tend to involve a lot of repetitive calculation, so they are easier to get with a computer program than by hand.
To get a numerical solution, the space we are working in (called the “domain”) is broken up into tiny pieces. This process is called discretizing. To see how this works, let’s first consider the number line as we know it. Since we’re dealing with sin and cos, let’s write the number line from 0 to
When we write the number line, we typically mark some values with the understanding that the intermediate values are there. I wrote
Image(discretizednumberline.jpg)
What’s the difference? Here there are blocks and everywhere in the block has the same value. Take the block labeled
||dx||x|| || ||0|| ||0.523599||0.523599|| ||0.523599||1.047198|| ||0.523599||1.570796|| ||0.523599||2.094395|| ||0.523599||2.617994|| ||0.523599||3.141593|| ||0.523599||3.665191|| ||0.523599||4.18879|| ||0.523599||4.712389|| ||0.523599||5.235988|| ||0.523599||5.759587|| ||0.523599||6.283185||
Now, calculate
We’re now ready to numerically solve the equation. We know what the slope is – it’s dy/dx. So
||dx||x||dy/dx||y| || ||0||1||0|| ||0.523599||0.523599||0.866025||0.45345|| ||0.523599||1.047198||0.5||0.715249|| ||0.523599||1.570796||6.13E-17||0.715249|| ||0.523599||2.094395||-0.5||0.45345|| ||0.523599||2.617994||-0.86603||0|| ||0.523599||3.141593||-1||-0.5236|| ||0.523599||3.665191||-0.86603||-0.97705|| ||0.523599||4.18879||-0.5||-1.23885|| ||0.523599||4.712389||-1.8E-16||-1.23885|| ||0.523599||5.235988||0.5||-0.97705|| ||0.523599||5.759587||0.866025||-0.5236|| ||0.523599||6.283185||1||0||
Excellent. Let’s plot our answer so we can look at it. Also, since we know the answer from the analytic solution, let’s plot that as well.
Image(plot_numeric_analytic_cos.jpg)
Hmm. Our solution doesn’t match the analytic solution so well (and we know the analytic solution is correct). What happened? Let’s examine some of the approximations we made. One of them was which value of dy/dx we used. The dx was constant, but in reality the dy is changing across our cells. Since we have discretized our space we lost this distinction.
Image(slope_varies_between_cells.jpg)
Really we should have used the value of dy at the face between the cells, not the value inside the cell. In this instance, since we know the answer we could cheat and just calculate the correct value. But in general we won’t have an analytic solution so we have to be more clever. We have to interpolate. Mathematicians and computer scientists have spent a lot of time thinking about how to interpolate values. For now, let’s just use a linear average of the values in the two cells while keeping in mind that there are many other ways to do it. For the harder problems, we’ll use software that takes care of this for us.
||dx||x||dy/dx||y|| || ||0||1||0|| ||0.523599||0.523599||0.866025||0.488524|| ||0.523599||1.047198||0.5||0.846149|| ||0.523599||1.570796||6.13E-17||0.977049|| ||0.523599||2.094395||-0.5||0.846149|| ||0.523599||2.617994||-0.86603||0.488524|| ||0.523599||3.141593||-1||0|| ||0.523599||3.665191||-0.86603||-0.48852|| ||0.523599||4.18879||-0.5||-0.84615|| ||0.523599||4.712389||-1.8E-16||-0.97705|| ||0.523599||5.235988||0.5||-0.84615|| ||0.523599||5.759587||0.866025||-0.48852|| ||0.523599||6.283185||1||-4.4E-16||
Image(plot_numeric_analytic_cos_corrected.jpg)
That solution matches the analytic solution much better, doesn’t it?
Now, we’ll do the same exercise in FiPy. Start up a fresh window in your favorite Python editor. If you don't already have FiPy this is a good time to get it.
The easiest way to start using FiPy is to download the Matforge/FiPy live CD. Think of it as a fan mash-up of Ubuntu 9.10 and FiPy. It's designed to help you "jump start" using FiPy without needing to install FiPy and all of its dependencies. To use the live CD, download and burn the ISO image into a CD, and reboot your computer from the CD. The ISO image can also be used in a virtualization environment (e.g. VirtualBox, VMWare, Parallels, KVM, Qemu, etc.) without needing to reboot the computer.
If you are a do-it-yourselfer, or you don't want to have to boot into another operating system to use FiPy you can install it. The developers of FiPy have created an installation guide for Windows, Mac, and Unix-based systems. Quick warning for Windows users: as of FiPy 2.1, you should install Python 2.5 (not 2.6) because one package that FiPy depends on (Pysparse) doesn't play well with Python 2.6.
Ready to roll? Good.
We’ll follow the same steps as when we solved the problem with the spreadsheet. Just like you had to open your spreadsheet program, you also have to “open” FiPy. This is done with the command
Now, all the fipy commands are available to us.
Next, we discretize our domain. Fortunately, FiPy has built in functions that handle the messy stuff for us. We just have to tell it what we want. Let’s define the same dx. This is simple.
We also have to define a number of points, nx.
That’s all the information it takes to create a discretized domain. We create the domain with the command
Our domain information is stored in the variable called “mesh”. You can imagine the mesh like your blank spreadsheet. Since this is a 1-dimensional mesh, we have told the program that we will only be defining variables that use a single column of this spreadsheet at a time. . The mesh defines the domain we are working in. But we can’t store any information in the mesh. In just a bit we will define some variables (dy/dx) and tell them to use the mesh we have created. Defining the variables is just like filling their names in at the top of the spreadsheet and putting in their values. It’s important to check your work often to make sure you’re getting what you expect. A good check to see if your mesh is defined properly is
This assigns the value of every cell center to the variable x, and prints the value of x to the screen so we can see what it is. The output should be
[0.26179939]
These aren’t the same values we had for x in our spreadsheet. What happened? The difference is subtle, but important to keep in mind when doing numerical computing. When we defined x in our spreadsheet we were keeping track of the location of the cell edge. Remember, our domain is defined from 0 to
Image(cell_face_explaination.jpg)
This distinction is not just academic. Fipy differentiates between variables defined at cell centers (called cell variables) from those defined at cell faces (called face variables). You already know why this is an important distinction to make. Remember when you made plot \label{fig:y_rightedgeslope}? The errors in that solution crept in because we weren’t careful which variables should be defined at the center, and which at the faces. Fipy is very particular about these things, and won’t let you add cell and face variables together. But we’ll deal with that when we come to it. Next we’ll define our variable. Just like in our spreadsheet we need to input what we know about dy/dx. We’ll define dy_dx as one of the cell variables we were just talking about. The only thing we need to tell fipy to make a cell variable is the mesh we are using. It’s good practice to give the cell variable a name also.
We don’t strictly need to, but it’s much cleaner if we define dy as a separate variable.
Again, just like our spreadsheet, we’ll define y. But we’ll make y a face variable like it should be.
We included the “value=0” in the definition so that all the values in y start at 0. At the left edge, this is consistent with our boundary condition. But what about all those other values? We need to calculate them. In the future, we’ll let fipy do all the heavy lifting in solving these equations but this one we can do ourselves (especially since we’ve already written the algorithm in the spreadsheet). We’ll need to create an iterative solution since each value of y depends on the one before it. That sounds like a good job for a “for” loop. We also need some way to keep track of how many times we’ve run through the loop. One easy way to do this is to define an “index” counter and just iterate it each time through. With all that bookkeeping in place, all we have to do is apply the same algorithm we’ve already used.
Take a second to convince yourself that this code does the same thing the spreadsheet does. Experienced python users will notice that we can operate on the CellVariable using [index] similar to a list. (Be careful. There are lots of commands that lists have that a CellVariable doesn't, and vice versa).
Now that we’ve seen all the steps to doing a simple numerical solution we’re going to learn to use some of the more powerful features of FiPy. These features will do a lot of what we just did for us, behind the scenes, so we don’t have to worry about it. So that we have something concrete to talk about, let’s solve one particular problem about the carburization of steel. Let’s say we have a sample of pure iron (0% carbon initially) and we flow a carbon containing gas (such as methane or carbon dioxide) over the sample. The flowing gas creates a constant concentration of carbon ( c=1) at the surface of the iron, which will diffuse into the bulk of the sample over time. The question is, what does the concentration profile (that’s a graph of C vs. Distance) look like at a given time t? You can assume a constant diffusivity, that the sample is infinitely thick, and that a one-dimensional answer is alright. You can also assume that the concentration in the gas phase is always c=1 and that we are only interested in the concentration of carbon in the iron.
Before we get to coding, it’s a good idea to ask ourselves a few questions. The answers to these questions will be the outline of the program. That way, in the future, when you have a different more difficult problem to solve you can ask yourself the same questions and come up with your own program.
First question: what do we want to know? This is given in the problem. We want to know the concentration profile at a time t. This concentration profile will be given by an equation. That begs the second question.
Second question: what is the equation? It’s the well known diffusion equation that you learned in your kinetics class:
That’s all the material science questions we need to answer to solve this problem. The only thing left is to figure out what all our variables are. Sixth question: what variables do I need? Try to come up with the list on your own before reading the next paragraph. The problem talks about a diffusivity, so we’ll need that. We’ll also need a total length of the domain. What else are we missing? Think back to the problem we did about numerically integrating cos(x). In that case we needed a dx and also a number of points along the domain, nx. And just like we discretized space in that problem, we need to discretize time in this one (since we’re solving for dc/dt). Recap of the Variables:
- L --> length of domain
- D --> diffusivity
- nx --> number of discretized cells along x-axis
- dx --> how large each discretized cell is
- t --> how long we will run the simulation for. Said another way, the number of discretized time steps.
- timestepDuration --> how much time each time step represents.
The final question we have to ask is “how are we going to do all this?” FiPy has the tools we need, but we need to “open the toolbox.” The whole process is recapped below. We’re going to code up the answers to these questions in reverse order, so that’s how they’re listed below.
Look at all the work we’ve had to do before we even start programming. Start with a new, blank python file. Once we’ve built this program together you’ll see what goes into them and you’ll be able to do it yourself by answering the same series of questions. We’ll go through it together this first time so you can learn the syntax. Python, like many other languages, allows you to reuse code by “importing” it into the current program. This command is done with
just like in the previous example. Now all the FiPy commands are available to us. Next, we need to define all our variables.
It is easier to work in unitless space and time so let’s not worry about seconds or meters at this point. Consider this an abstract problem. An exercise at the end lets you apply units to this example. One thing to notice is that dx is defined in terms of L and nx. We could have just as easily defined dx and nx, in which case L would be nx*dx. Those three terms are all related. Do you see why? Next, we answer what domain we’re working in. The problem told us to work in one-dimension so that’s what we’ll do. Remembering that FiPy variables are built on grids, let’s make one of those and build our concentration variable on it.
Next question: what are the initial conditions? Here, we just assign the initial values to c.
This is a bit of a “gotcha” with the FiPy syntax. What if you had said
instead? The variable “c” would no longer be a cell variable, built on the FiPy mesh! It would instead be a simple integer value of zero. That’s because you assigned c to the value of zero. Remember to use the FiPy command .setValue to fill in the values for the CellVariable. Moving right along to the boundary conditions. Now we can really use some of the power of FiPy. Because boundary conditions are necessary to solve any differential equations, FiPy has a built-in way to handle boundary conditions.
Fittingly, if you wanted fixed flux boundary conditions, you could have said FixedFlux instead of FixedValue.
Next, let’s code the equation. This will be much easier using FiPy’s built-in tools than it was to do earlier. The syntax is a little tricky, but you can do it once you get the hang of it. FiPy has names for different terms. For any variable V if you have a term that looks like
We call that a transient term. If you have a term that looks like
A couple notes about the syntax. We’re making a new variable called eqn1 and assigning it to have the value of everything to the right of the
The loop iterates over all our time t. At each time we solve the equation, which is just as easy as using the “solve” command (if only everything were that easy). Notice that in the solve command, we finally tell FiPy what variable to use. We also have to tell it about the boundary conditions, and how long the timestep is. At the end of the loop we save the output text to a file. We could have also printed a graph to the screen.
Take a little time to go back over the code. Can you follow the logic of each step? Try these additional problems to test your understanding. If you'd like you can also download the example code.
1. Alter the script to show the concentration profiles at many different times.
2. Redo the problem using realistic values with units.
3. FiPy variables can store units. For example, you could declare dx=Variable(“3 cm”). Read the FiPy documentation about using these units and redo part (2) with the built-in unit capability.
4. What if the sample were not infinitely thick but instead only 3 cm thick. How would the problem change?
5. What if D(c) instead of a constant?
6. What if D(x) instead of a constant?