a finite volume pde solver using python...

26
A Finite Volume PDE Solver Using Python (FiPy) Jonathan E. Guyer, Daniel Wheeler & James A. Warren [email protected] [email protected] [email protected] Metallurgy Division Materials 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.

Upload: others

Post on 22-Apr-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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.

Page 2: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 3: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

Phase Field

Research BackgroundModeling Phase Transformations

concentration

distance

potential

electrode electrolyte

φ0

φ

concentration

distance

potential

δ

Page 4: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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)

Page 5: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

Phase Field

SolidificationDendritesGrain boundary motion, impingement, rotation

Electrochemistry

Research BackgroundModeling Phase Transformations

Page 6: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 7: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

Research BackgroundModeling Phase Transformations

Phase Field

SolidificationDendritesGrain boundaries

ElectrochemistryStresses and strains

Level Set Method

Electrodeposition

Page 8: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 9: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 10: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 11: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 12: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 13: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

FiPy Design - Objects

Equation Iterator

Solver

Term

Variable Viewer

SparseMatrix

MeshCell Face Vertex

BoundaryCondition

cell

adjacentcell

face

vertex

Page 14: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 15: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 16: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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.)

Page 17: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 18: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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):::

Page 19: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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):::

Page 20: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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))

::

Page 21: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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()

Page 22: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

Example Problem - Grain Impingement

Page 23: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 24: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 25: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

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

Page 26: A Finite Volume PDE Solver Using Python (FiPy)ftp.ntua.gr/mirror/python/pycon/dc2004/papers/28/fipy.pdf · 2005-08-26 · A Finite Volume PDE Solver Using Python (FiPy) Jonathan E

Further WorkMeshing packages

DX viewer

Documentation

Simplify installation

Even more test cases

Level Sets

???