Professional Documents
Culture Documents
Partial Differential Equations
Partial Differential Equations
With Python
Many existing partial differential equation solver packages focus on the important, but
arcane, task of numerically solving the linearized set of algebraic equations that result
from discretizing a set of PDEs. Many researchers, however, need something higher level
than that.
P
artial differential equations (PDEs) behaviors that involve complex interactions of
are ubiquitous to the mathematical de- multiple systems. Teasing out the quantity to be
scription of physical phenomena: they determined from an experiment’s “signal” often
describe the relationships between requires the solution of a mathematical mod-
functions of more than one independent variable el in the form of a set of PDEs. In most cases,
and partial derivatives with respect to those vari- those PDEs don’t admit an analytical solution,
ables. Typical examples in the physical sciences so researchers use computers to obtain numeri-
describe the evolution of a field in time as a func- cal solutions to practical problems. The goal of
tion of its value in space, such as in wave propaga- computational materials science is to apply com-
tion, heat flow, or fluid dynamics. putational methods to explain and predict prop-
Although PDEs are relevant throughout the erties such as experimental microstructures and
sciences, we focus our attention here on mate- phenomena (see Figure 1).
rials. Materials science studies the relationships Lots of numerical PDE solvers are available to-
between the properties and microstructures of day, and FiPy (www.ctcms.nist.gov/fipy) won’t be
materials and how both characteristics are af- the last; indeed, it isn’t even the first discussion
fected by (and how they affect) the processing of a Python-based PDE solver in these pages.1,2
of those materials. As the science of materials Many existing PDE solver packages focus on the
measurement advances, it has become increas- important, but relatively arcane, task of numeri-
ingly difficult to determine what a measure- cally solving the linearized set of algebraic equa-
ment “means” or, alternatively, what measurable tions that result from discretizing a set of PDEs.
quantities are appropriate to shed light on the However, many, if not most, researchers would
phenomena of interest. These phenomena are prefer to avoid reckoning with such details and
often at the nanoscale, with highly nonclassical work at a higher level of abstraction. Specifically,
they have the physical knowledge to describe
1521-9615/09/$25.00 © 2009 IEEE
their model and can apply the calculus needed
Copublished by the IEEE CS and the AIP to obtain the appropriate governing conditions,
but when faced with rendering those governing
Jonathan E. Guyer, Daniel Wheeler, and James A. Warren
equations on a computer, their skills (or time)
US National Institute of Standards and Technology
are limited to a straightforward implementation
May/June 2009 7
• an interpreted language that combines remark- (Γi∇)]nφ in the diffusion term represents any of the
able power with very clear syntax; family of terms ∇ ⋅ (Γ1∇φ), ∇ ⋅ (Γ1∇[∇ ⋅ (Γ2∇φ)]), and
• freely usable and distributable, even for com- so on. The convection term describes the “blowing”
mercial use; of φ by a velocity field u. Finally, a source term rep-
• fully object oriented; resents any source or sink that injects or removes φ
• distributed with powerful automated testing from a point in space. Equation 1 doesn’t address all
tools; applications of PDEs, but it so far covers everything
• actively used and extended by other scien- we’ve tried to do in materials science.
tists and mathematicians (such as NumPy, To solve Equation 1, we cast it in integral (weak)
http://numpy.scipy.org; SciPy, www.scipy.org; form as
ScientificPython, http://dirac.cnrs-orleans.
fr/plone/software/scientificpython; and Py ∂( ρφ )
∫V τ dV − ∫ S τΓ n ( n ⋅ ∇) dS
Sparse, http://pysparse.sourceforge.net); and ∂t diffusion
transient
• easily integrated with low-level languages such
as C or Fortran (such as Weave and Blitz, www. − ∫ S τ ( n ⋅ u )φ dS − ∫V τ Sφ dV = 0 , (2)
scipy.org/Weave; PyRex, www.cosc.canterbury. convection source
May/June 2009 9
calculate these values on the fly, saving a con- less” phase separation process, such that under the
siderable amount of memory. In either case, the right thermodynamic conditions, any fluctuation,
user need not be concerned with what amounts to no matter how small, will tend to grow. This is
nothing more than tedious bookkeeping. in contrast to nucleation, in which a fluctuation
By default, DiffusionTerm and Convection- must exceed some critical magnitude before it
Term are implicit, but an ExplicitDiffusion- will survive and grow. We can describe spinodal
Term and ExplicitUpwindConvectionTerm decomposition via the Cahn-Hilliard equation6,7
are available, primarily for didactic purposes. In (also known as “conserved Ginsberg-Landau” or
some cases, semi-explicit approaches can yield “model B” of Hohenberg and Halperin):
larger time steps than a naïve, fully explicit imple-
mentation, but with better accuracy than a fully ∂φ ∂f
= ∇ ⋅ D∇ − ε 2∇2φ ,
implicit treatment. For example, on a uniform, ∂t ∂φ
one-dimensional grid with a constant coefficient
D, we can discretize the expression ∂u/∂t = ∇ ⋅ where φ is a conserved order parameter, possi-
D∇u at each point j as bly representing alloy composition or spin. The
double-well free energy function f = (a2/2)φ2(1 −
u new
j − u old
j u new new
j +1 − 2u j + u new
j −1 φ)2 penalizes states with intermediate values of φ
=α D
∆t ∆x 2 between 0 and 1. The gradient energy term ε2∇2φ,
u old old old on the other hand, penalizes sharp changes of φ.
j +1 − 2u j + u j −1
+ (1 − α ) D . These two competing effects result in the segre-
∆x 2 gation of φ into domains of 0 and 1, separated by
When α = 0, u advances by a fully explicit forward abrupt, but smooth, transitions. The parameters a
Euler time step; when α = 1, it takes a fully implicit and ε determine the relative weighting of the two
backward Euler time step. We obtain the semi- effects, and D is a rate constant.
explicit Crank-Nicolson scheme when α = 1/2. The We can simulate this process in FiPy with a
FiPy implementation of this scheme is as simple as simple script:
PHI = phi.getArithmeticFaceValue()
D = a = eps = 1.
eq = (TransientTerm() 1.0
== DiffusionTerm(coeff= φ
250 0.9
D * a**2 * (1 - 6 * PHI * (1-PHI)))
- DiffusionTerm(coeff=(D, eps**2))) 0.8
200 0.7
Because the evolution of a spinodal micro- 0.6
structure slows with time, we use exponentially 150
increasing time steps to keep the simulation “in- 0.5
teresting.” The FiPy user always has direct con- 100 0.4
trol over the problem’s evolution: 0.3
50
0.2
dexp = -5
elapsed = 0. 0.1
0 50 100 150 200 250
while elapsed < 1000.: (b) 0.0
dt = min(100, exp(dexp))
elapsed += dt
dexp += 0.01
eq.solve(phi, dt=dt) 1.0
viewer.plot() φ
250 0.9
May/June 2009 11
{Duplicata{Surface{1};}};
t4[] = Rotate {{0,1,0},{0,0,0},-Pi/2}
{Duplicata{Surface{1};}};
t5[] = Rotate {{0,0,1},{0,0,0},Pi/2}
{Duplicata{Surface{t4[0]};}};
t6[] = Rotate {{0,0,1},{0,0,0},Pi}
{Duplicata{Surface{t4[0]};}};
t7[] = Rotate {{0,0,1},{0,0,0},Pi*3/2}
{Duplicata{Surface{t4[0]};}};
dt = 5e-4
Figure 4. Spinodal decomposition on a sphere’s
We consider the simultaneous evolution of a surface. Compare this with, for example, www.
phase-field variable φ (taken to be 0 in the liquid youtube.com/watch?v=kDsFP67_ZSE.
phase and 1 in the solid)
1 + c β −c ∂β
phase = CellVariable(name=r’$\phi$’,
mesh=mesh, hasOld=True) ∂ψ
D = α 2 (1 + c β )
∂β ,
and a dimensionless undercooling ΔT (ΔT = 0 at c 1+ cβ
∂ψ
the melting point)
May/June 2009 13
FiPy’s viewers are utilitarian, striving to let users
0.04 see something, regardless of their operating system
φ and ∆Τ
or installed packages. As a result, users won’t be
12
able to simultaneously view two fields “out of the
0.02 box,” but, because all of Python is accessible and
10 FiPy is object oriented, it isn’t hard to adapt one of
0.00
the existing viewers to create a specialized display:
8 import pylab
–0.02 class DendriteViewer(
Matplotlib2DGridViewer):
6
def __init__(self, phase, dT,
–0.04
title=None, limits={}, **kwlimits):
4 self.phase = phase
–0.06 self.contour = None
Matplotlib2DGridViewer.__init__(
2
self, vars=(dT,), title=title,
–0.08 cmap=pylab.cm.hot,
0 2 4 6 8 10 12 limits=limits, **kwlimits)
–0.10
def _plot(self):
Matplotlib2DGridViewer._plot(self)
Figure 5. Dendrite formation due to temperature gradients. Faster if self.contour is not None:
solidification in steeper temperature gradients leads to surface cc = self.contour.collections
instability and progressively finer branching. for c in cc:
c.remove()
mesh = self.phase.getMesh()
tau = 3e-4 shape = mesh.getShape()
kappa1 = 0.9 x, y = mesh.getCellCenters()
kappa2 = 20. z = self.phase.getValue()
phaseEq = (TransientTerm(tau) x, y, z = [a.reshape(
== DiffusionTerm(D) shape, order=”FORTRAN”)
+ ImplicitSourceTerm((phase - 0.5 for a in (x, y, z)]
- kappa1 / pi * arctan(kappa2 * dT)) self.contour = pylab.contour(
* (1 - phase))) x, y, z, (0.5,))
W
dynamics and Kinetics Group in the Metallurgy Divi-
e developed FiPy to address a sion at the US National Institute of Standards and
troublesome repetition of effort Technology. His interests include numerical analysis
in our own research and in that for interpreting experimental results. Wheeler has a
of our colleagues. The result is a PhD in computational mechanics from Greenwich
tool that we enjoy using and that we’ve been able to University. Contact him at daniel.wheeler@nist.gov.
apply to a diverse collection of research problems.
We can get an implementation of a new combina- James A. Warren is leader of the Thermodynamics
tion of physics far faster than we ever could when and Kinetics Group in the Metallurgy Division at the
we were coding in C or Fortran. We’ve been grat- US National Institute of Standards and Technology.
ified that many others have found our tool useful, His interests include developing both models of mate-
often for applications we never dreamed of. We’re rials phenomena and the tools to enable the solution
also pleased that FiPy is already helping in the of these models. Warren has a PhD in physics from
education of the next generation of materials sci- the University of California, Santa Barbara. Contact
entists. Our current development efforts focus on him at james.warren@nist.gov.
improving performance, through parallelism and
more sophisticated matrix preconditioners and
solvers, by exploiting the Trilinos package from
Sandia National Laboratories. We look forward Contact CiSE
to tackling even larger and more complicated
problems, while keeping the ease of use that we’ve Web sites: www.computer.org/cise/
already established. or http://cise.aip.org
Writers: Visit our “Write for Us” section at
References www.computer.org/cise/author.htm.
1. M. Tobis, “PyNSol: Objects as Scaffolding,” Computing in
Science & Eng., vol. 7, no. 4, 2005, pp. 84–91. Letters to the Editors: Email Jennifer
2. K.-A. Mardal et al., “Using Python to Solve Partial Differential Gardelle, lead editor, jgardelle@computer.
Equations,” Computing in Science & Eng., vol. 9, no. 3, 2007, org. Provide an email address or daytime
pp. 48–51.
phone number.
3. D. Josell, D. Wheeler, and T.P. Moffat, "Gold Superfill in
Submicrometer Trenches: Experiment and Prediction," J. Subscribe: Visit https://www.aip.org/forms/
Electrochemical Soc., vol. 153, no. 1, 2006, pp. C11–C18. journal_catalog/order_form_fs.html or www.
4. W.J. Boettinger et al., “Computation of the Kirkendall Veloc- computer.org/subscribe/.
ity and Displacement Fields in a One-Dimensional Binary
Diffusion Couple with a Moving Interface,” Proc. Royal Soc. Subscription Change of Address: For the
A: Mathematical, vol. 463, 2007, pp. 3347–3373. IEEE or IEEE Computer Society, email address.
5. J. Mazur, “Numerical Simulation of Temperature Field in Soil change@ieee.org. Specify CiSE. For the AIP,
Generated by Solar Radiation,” J. Physique IV France, vol. 137,
email subs@aip.org.
Nov. 2006, pp. 317–320.
6. J.W. Cahn and J.E. Hilliard, “Free Energy of a Nonuniform Missing or Damaged Copies: For IEEE
System. I. Interfacial Free Energy,” J. Chemical Physics, vol. Computer Society subscribers, email help@
28, no. 2, 1958, pp. 258–267.
computer.org. For AIP subscribers, email
7. J.W. Cahn, “Free Energy of a Nonuniform System. II. Ther-
claims@aip.org.
modynamic Basis,” J. Chemical Physics, vol. 30, no. 5, 1959,
pp. 1121–1124. Article Reprints: Email cise@computer.org
8. J.A. Warren et al., “Extending Phase Field Models of Solidifi- or fax +1 714 821 4010.
cation to Polycrystalline Materials,” Acta Materialia, vol. 51,
no. 20, 2003, pp. 6035–6058. Reprint Permission: Email William Hagen,
Copyrights & Trademarks Manager, at
Jonathan E. Guyer is a member of the Thermody- copyrights@ieee.org.
namics and Kinetics Group in the Metallurgy Divi-
sion at the US National Institute of Standards and
Technology. His interests include models of phase www.computer.org/cise
May/June 2009 15