A Finite Volume PDE Solver Using Python...
Transcript of A Finite Volume PDE Solver Using Python...
A Finite Volume PDE Solver Using Python (FiPy)
Jonathan E. Guyer, Daniel Wheeler & James A. [email protected] [email protected] [email protected]
Metallurgy DivisionMaterials Science and Engineering Laboratory
Certain software packages are identified in this document in order to specify the experimental
procedure adequately. Such identification is not intended to imply recommendation or endorsement by the
National Institute of Standards and Technology, nor is it intended to imply that the software packages identified
are necessarily the best available for the purpose.
Attention, users of this electronic file: The information contained herein should not be altered in any way. Attempts to change the file will adversely affect the integrity of the information and its usefulness.
MotivationMany interface tracking codes for solving Materials Science problems
Commercial CFD codes are expensive and written in FORTRAN or C
Interface tracking requires unstructured meshes
Unstructured meshing requires Finite Element / Finite Volume
Difficult for end users to customize existing codes in FORTRAN/C
Python is the code and the user scriptGood Python libraries to exploit
NumPy for array manipulationPySparse for linear algebraGist for viewingSciPy for C inlining within Python (weave)Scientific Python for physical dimensionsProfiler - PyGTK GUI by NIST’s Steve Langer
Phase Field
Research BackgroundModeling Phase Transformations
concentration
distance
potential
electrode electrolyte
φ0
φ
concentration
distance
potential
δ
Research BackgroundModeling Phase Transformations
Phase Field
SolidificationDendritesGrain boundary motion, impingement, rotation
L. Gránásy, T. Börzsönyi, T. Pusztai, Phys. Rev. Lett. 88, 205105 (2002)
Phase Field
SolidificationDendritesGrain boundary motion, impingement, rotation
Electrochemistry
Research BackgroundModeling Phase Transformations
Research BackgroundModeling Phase Transformations
Phase Field
SolidificationDendritesGrain boundary motion, impingement, rotation
ElectrochemistryStresses and strains
σ12J. Slutsker, G. B. McFadden, A. L. Roytburd,
W. J. Boettinger, and J. A. Warren
Research BackgroundModeling Phase Transformations
Phase Field
SolidificationDendritesGrain boundaries
ElectrochemistryStresses and strains
Level Set Method
Electrodeposition
FiPy OverviewFinite Volume code for solving coupled sets of PDEs
Code currently addresses Phase Field models
Compatible with unstructured meshes
Code will be freely available (watch <http://www.ctcms.nist.gov>)
Large archive of test problems
User controls program flow
Finite Volume MethodSolve a general PDE on a given domain for a field
!("#)!t︸ ︷︷ ︸
transient
+$.("u#)︸ ︷︷ ︸convection
= $.(%$#)︸ ︷︷ ︸diffusion
+ S#︸︷︷︸source
a11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
domain
Finite Volume MethodSolve a general PDE on a given domain for a field
Integrate PDE over arbitrary control volumes
!("#)!t︸ ︷︷ ︸
transient
+$.("u#)︸ ︷︷ ︸convection
= $.(%$#)︸ ︷︷ ︸diffusion
+ S#︸︷︷︸source
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
controlvolume
Finite Volume MethodSolve a general PDE on a given domain for a field
Integrate PDE over arbitrary control volumes
Evaluate PDE over polyhedral control volumes !("#)!t︸ ︷︷ ︸
transient
+$.("u#)︸ ︷︷ ︸convection
= $.(%$#)︸ ︷︷ ︸diffusion
+ S#︸︷︷︸source
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
face
cell
vertex
Finite Volume MethodSolve a general PDE on a given domain for a field
Integrate PDE over arbitrary control volumes
Evaluate PDE over polyhedral control volumes
Obtain a large coupled set of linear equations in
!("#)!t︸ ︷︷ ︸
transient
+$.("u#)︸ ︷︷ ︸convection
= $.(%$#)︸ ︷︷ ︸diffusion
+ S#︸︷︷︸source
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
FiPy Design - Objects
Equation Iterator
Solver
Term
Variable Viewer
SparseMatrix
MeshCell Face Vertex
BoundaryCondition
cell
adjacentcell
face
vertex
FiPy Design - Equations
Equation Iterator
Solver
Term
Variable Viewer
SparseMatrix
MeshCell Face Vertex
BoundaryCondition
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
Iterator
Equation
AdvectionEquation DiffusionEquation
Solver Term Variable SparseMatrix
PhaseEquation TemperatureEquation
FiPy Design - Terms
Equation Iterator
Solver
Term
Variable Viewer
SparseMatrix
MeshCell Face Vertex
BoundaryCondition
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
Equation
Term
CellTerm FaceTerm
Mesh
BoundaryCondition
TransientTerm SourceTerm DiffusionTerm ConvectionTerm
cell
adjacentcell
face
vertex
FiPy Design - Variables
Equation Iterator
Solver
Term
Variable Viewer
SparseMatrix
MeshCell Face Vertex
BoundaryCondition
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
1
Equation
Variable
CellVariable FaceVariable GradVariable
Viewer Mesh
CellGradVariable FaceGradVariable
Either:
solution variables (evaluated by Equation)set by intermediate calculation
Lazy evaluation
Physical dimensions
Standard operators apply (__add__, __div__, etc.)
Example Problem - Grain ImpingementGoverning Equations
Phase field variable
Orientation variable
0 ≤ φ ≤ 1
−π ≤ θ ≤ π
2
0 ≤ φ ≤ 1
−π ≤ θ ≤ π
2
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
N FORTRAN (s) Python (s) Penalty100 0.0619 12.8 ×207400 0.188 40.2 ×214
1600 0.573 157 ×2736400 3.06 707 ×231
25600 12.0 4060 ×338102400 37.3 ??? ×???409600 148 ??? ×???
N FORTRAN (s) Python (s) Penalty Inlined (s) Penalty100 0.0619 12.8 ×207 2.45 ×39.6400 0.188 40.2 ×214 2.95 ×15.3
1600 0.573 157 ×273 5.22 × 9.106400 3.06 707 ×231 15.3 × 4.99
25600 12.0 4060 ×338 66.0 × 5.53102400 37.3 ??? ×??? 243 × 6.50409600 148 ??? ×??? 1510 ×10.2
Q(φ,∇θ)∂φ
∂t︸ ︷︷ ︸transient
= α2∇2φ︸ ︷︷ ︸diffusion
− ∂f
∂φ− ∂g
∂φs|∇φ|− ∂h
∂φ
ε2
2|∇φ|2︸ ︷︷ ︸
source
P (φ,∇θ)∂θ
∂t︸ ︷︷ ︸transient
= ∇ ·[hε2∇θ + gs
∇θ
|∇θ|]
︸ ︷︷ ︸diffusion
1
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
N FORTRAN (s) Python (s) Penalty100 0.0619 12.8 ×207400 0.188 40.2 ×214
1600 0.573 157 ×2736400 3.06 707 ×231
25600 12.0 4060 ×338102400 37.3 ??? ×???409600 148 ??? ×???
N FORTRAN (s) Python (s) Penalty Inlined (s) Penalty100 0.0619 12.8 ×207 2.45 ×39.6400 0.188 40.2 ×214 2.95 ×15.3
1600 0.573 157 ×273 5.22 × 9.106400 3.06 707 ×231 15.3 × 4.99
25600 12.0 4060 ×338 66.0 × 5.53102400 37.3 ??? ×??? 243 × 6.50409600 148 ??? ×??? 1510 ×10.2
Q(φ,∇θ)∂φ
∂t︸ ︷︷ ︸transient
= α2∇2φ︸ ︷︷ ︸diffusion
− ∂f
∂φ− ∂g
∂φs|∇φ|− ∂h
∂φ
ε2
2|∇φ|2︸ ︷︷ ︸
source
P (φ,∇θ)∂θ
∂t︸ ︷︷ ︸transient
= ∇ ·[hε2∇θ + gs
∇θ
|∇θ|]
︸ ︷︷ ︸diffusion
1
R. Koybayashi, W. C. Carter, and J. A. Warren
Example Problem - Input Fileclass ImpingementSystem: def __init__(self, n = 100, dx = 0.01, steps = 10, plotsteps = 5, timeStepDuration = 0.02):
::
# create meshmesh = Grid2D(dx, dy, nx, ny)
# create variables::
# create viewersself.viewers = [Grid2DGistViewer(var = field, palette = 'rainbow.gp') for field in (phase,theta)]
# create equations::
# create iteratorself.it = Iterator(equations = (thetaEq, phaseEq))
::
def run(self):::
Example Problem - Input Fileclass ImpingementSystem: def __init__(self, n = 100, dx = 0.01, steps = 10, plotsteps = 5, timeStepDuration = 0.02):
::
# create meshmesh = Grid2D(dx, dy, nx, ny)
# create variablesphase = CellVariable(name = 'PhaseField', mesh = mesh, value = 0.)theta = ModularVariable(name = 'Theta', mesh = mesh, value = 0.)
setCells = mesh.getCells(lambda cell: cell.getCenter()[0] > L/2.)phase.setValue(1.,setCells)
# create viewersself.viewers = [Grid2DGistViewer(var = field, palette = 'rainbow.gp') for field in (phase,theta)]
# create equations::
# create iteratorself.it = Iterator(equations = (thetaEq, phaseEq))
::
def run(self):::
Example Problem - Input Fileclass ImpingementSystem: def __init__(self, n = 100, dx = 0.01, steps = 10, plotsteps = 5, timeStepDuration = 0.02):
:::
# create equationsthetaEq = ThetaEquation(var = theta,
solver = LinearPCGSolver( tolerance = 1.e-15, steps = 2000 ),boundaryConditions = ( FixedFlux(mesh.getExteriorFaces(), 0.), ),
parameters = {'time step duration' : timeStepDuration, 'beta' : 1e5, 'mu' : 1e3
},fields = (phase,))
phaseEq = PhaseEquation(var = phase,mPhi = Type1MPhiVariable,solver = LinearPCGSolver(tolerance = 1.e-15, steps = 1000),boundaryConditions = ( FixedFlux(mesh.getExteriorFaces(), 0.), ),
parameters = { 'tau' : 0.1,
'time step duration' : timeStepDuration },
fields = (theta,))
# create iteratorself.it = Iterator(equations = (thetaEq, phaseEq))
::
Example Problem - Input Fileclass ImpingementSystem: def __init__(self, n = 100, dx = 0.01, steps = 10, plotsteps = 5, timeStepDuration = 0.02):
::::
def run(self):
for plotstep in range(self.plotsteps): for step in range(self.steps):
self.it.timestep() for viewer in self.viewers:
viewer.plot()
Example Problem - Grain Impingement
NumPy is much faster than plain Python, but still orders of magnitude slower than tailored FORTRAN code
NumArray is faster for large arrays?
Significant overhead in transition between Python and C for lots of simple operations
Example Problem - Efficiency
0 ≤ φ ≤ 1
−π ≤ θ ≤ π
$A = $B × $C + tan $D
2
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
N FORTRAN (s) Numeric (s) Penalty100 0.0619 12.8 ×207400 0.188 40.2 ×214
1600 0.573 157 ×2736400 3.06 707 ×231
25600 12.0 4060 ×338102400 37.3 ??? ×???409600 148 ??? ×???
N FORTRAN (s) Numeric (s) Penalty Inline C (s) Penalty100 0.0619 12.8 ×207 2.45 ×39.6400 0.188 40.2 ×214 2.95 ×15.3
1600 0.573 157 ×273 5.22 × 9.106400 3.06 707 ×231 15.3 × 4.99
25600 12.0 4060 ×338 66.0 × 5.53102400 37.3 ??? ×??? 243 × 6.50409600 148 ??? ×??? 1510 ×10.2
Q(φ,∇θ)∂φ
∂t︸ ︷︷ ︸transient
= α2∇2φ︸ ︷︷ ︸diffusion
− ∂f
∂φ− ∂g
∂φs|∇φ|− ∂h
∂φ
ε2
2|∇φ|2︸ ︷︷ ︸
source
P (φ,∇θ)∂θ
∂t︸ ︷︷ ︸transient
= ∇ ·[hε2∇θ + gs
∇θ
|∇θ|]
︸ ︷︷ ︸diffusion
1
Costly operations are not neatly encapsulated in a generalized library
C code is embedded within Python code using SciPy’s weave
Design in Python, optimize in C
Python and C variants are maintained in parallel (--inline at command line)
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
N FORTRAN (s) Numeric (s) Penalty100 0.0619 12.8 ×207400 0.188 40.2 ×214
1600 0.573 157 ×2736400 3.06 707 ×231
25600 12.0 4060 ×338102400 37.3 ??? ×???409600 148 ??? ×???
N FORTRAN (s) Numeric (s) Penalty Inline C (s) Penalty100 0.0619 12.8 ×207 2.45 ×39.6400 0.188 40.2 ×214 2.95 ×15.3
1600 0.573 157 ×273 5.22 × 9.106400 3.06 707 ×231 15.3 × 4.99
25600 12.0 4060 ×338 66.0 × 5.53102400 37.3 ??? ×??? 243 × 6.50409600 148 ??? ×??? 1510 ×10.2
Q(φ,∇θ)∂φ
∂t︸ ︷︷ ︸transient
= α2∇2φ︸ ︷︷ ︸diffusion
− ∂f
∂φ− ∂g
∂φs|∇φ|− ∂h
∂φ
ε2
2|∇φ|2︸ ︷︷ ︸
source
P (φ,∇θ)∂θ
∂t︸ ︷︷ ︸transient
= ∇ ·[hε2∇θ + gs
∇θ
|∇θ|]
︸ ︷︷ ︸diffusion
1
Example Problem - Efficiency
Maintaining clean and fast code is challenging
blitz isn’t fast enoughweave is a bit clumsySwig?Pyrex? 10:30 am Thursday
Example Problem - Efficiency
φa11 a12
a21 a22. . .
. . . . . . . . .. . . ann
φ1
φ2...
φn
=
b1
b2...
bn
∂(ρφ)
∂t︸ ︷︷ ︸transient
+∇ · ($uφ)︸ ︷︷ ︸convection
= ∇ · (Γ∇φ)︸ ︷︷ ︸diffusion
+ Sφ︸︷︷︸source∫
V
∂(ρφ)∂t
dV︸ ︷︷ ︸transient
+∫
S($n · $u)φ dS︸ ︷︷ ︸convection
=∫
SΓ($n ·∇φ) dS︸ ︷︷ ︸
diffusion
+∫
VSφ dV︸ ︷︷ ︸
source
ρφV − (ρφV )old
∆t︸ ︷︷ ︸transient
+∑face
[($n · $u)Aφ]face︸ ︷︷ ︸convection
=∑face
[ΓA$n ·∇φ]face︸ ︷︷ ︸diffusion
+ V Sφ︸︷︷︸source
N FORTRAN (s) Numeric (s) Penalty100 0.0619 12.8 ×207400 0.188 40.2 ×214
1600 0.573 157 ×2736400 3.06 707 ×231
25600 12.0 4060 ×338102400 37.3 ??? ×???409600 148 ??? ×???
N FORTRAN (s) Numeric (s) Penalty Inline C (s) Penalty100 0.0619 12.8 ×207 2.45 ×39.6400 0.188 40.2 ×214 2.95 ×15.3
1600 0.573 157 ×273 5.22 × 9.106400 3.06 707 ×231 15.3 × 4.99
25600 12.0 4060 ×338 66.0 × 5.53102400 37.3 ??? ×??? 243 × 6.50409600 148 ??? ×??? 1510 ×10.2
Q(φ,∇θ)∂φ
∂t︸ ︷︷ ︸transient
= α2∇2φ︸ ︷︷ ︸diffusion
− ∂f
∂φ− ∂g
∂φs|∇φ|− ∂h
∂φ
ε2
2|∇φ|2︸ ︷︷ ︸
source
P (φ,∇θ)∂θ
∂t︸ ︷︷ ︸transient
= ∇ ·[hε2∇θ + gs
∇θ
|∇θ|]
︸ ︷︷ ︸diffusion
1
Further WorkMeshing packages
DX viewer
Documentation
Simplify installation
Even more test cases
Level Sets
???