sfepy - simple finite elements in python - short · pdf file1/26 introduction our choice...

44
1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements in Python Short Introduction . . . Robert Cimrman 1 Ondˇ rej ˇ Cert´ ık 2 1 Department of Mechanics & New Technologies Research Centre University of West Bohemia in Plzeˇ n, Czech Republic 2 Institute of Physics, Academy of Sciences of the Czech Republic & Charles University in Prague, Czech Republic EuroSciPy 2008, July 27, Leipzig, Germany

Upload: truongcong

Post on 12-Mar-2018

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

1/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

SfePy - Simple Finite Elements in PythonShort Introduction . . .

Robert Cimrman1 Ondrej Certık2

1Department of Mechanics & New Technologies Research CentreUniversity of West Bohemia in Plzen, Czech Republic

2Institute of Physics, Academy of Sciences of the Czech Republic& Charles University in Prague, Czech Republic

EuroSciPy 2008, July 27, Leipzig, Germany

Page 2: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

2/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Outline

1 IntroductionNotes on Programming Languages

2 Our choiceMixing Languages — Best of Both WorldsSoftware Dependencies

3 Complete Example (Simple)IntroductionProblem Description FileRunning SfePyTop-level Scripts

4 TestingVerification of Numerical Results

5 Example ProblemsShape Optimization in Incompressible Flow ProblemsFinite Element Formulation of Schrodinger Equation

6 Conclusion

Page 3: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

3/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Introduction

SfePy = simple finite elements in Python

general finite element analysis softwaresolving systems of PDEs

BSD open-source license

available athttp://sfepy.org (developers)

mailing lists, issue (bug) trackingwe encourage and support everyone who joins!

http://sfepy.kme.zcu.cz (project information)

selected applications:

homogenization of porous media (parallel flows in a deformableporous medium)acoustic band gaps (homogenization of a strongly heterogenouselastic structure: phononic materials)shape optimization in incompressible flow problemsfinite element formulation of Schrodinger equation

Page 4: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

3/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Introduction

SfePy = simple finite elements in Python

general finite element analysis softwaresolving systems of PDEs

BSD open-source license

available athttp://sfepy.org (developers)

mailing lists, issue (bug) trackingwe encourage and support everyone who joins!

http://sfepy.kme.zcu.cz (project information)

selected applications:

homogenization of porous media (parallel flows in a deformableporous medium)acoustic band gaps (homogenization of a strongly heterogenouselastic structure: phononic materials)shape optimization in incompressible flow problemsfinite element formulation of Schrodinger equation

Page 5: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

4/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Notes on Programming LanguagesRough Division

compiled (fortran, C, C++, Java, . . . )

Pros

speed

large code base (legacy codes)

tradition

Cons

(often) complicated build process,recompile after any change

low-level ⇒ lots of lines to getbasic stuff done

code size ⇒ maintenance problems

static!

interpreted or scripting (sh, tcl, matlab, perl, ruby, python, . . . )

Pros

no compiling

(very) high-level ⇒ a few of linesto get (complex) stuff done

code size ⇒ easy maintenance

dynamic!

(often) large code base

Cons

many are relatively new

not known as useful in manyscientific communities

lack of speed

Page 6: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

4/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Notes on Programming LanguagesRough Division

compiled (fortran, C, C++, Java, . . . )

Pros

speed

large code base (legacy codes)

tradition

Cons

(often) complicated build process,recompile after any change

low-level ⇒ lots of lines to getbasic stuff done

code size ⇒ maintenance problems

static!

interpreted or scripting (sh, tcl, matlab, perl, ruby, python, . . . )

Pros

no compiling

(very) high-level ⇒ a few of linesto get (complex) stuff done

code size ⇒ easy maintenance

dynamic!

(often) large code base

Cons

many are relatively new

not known as useful in manyscientific communities

lack of speed

Page 7: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

4/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Notes on Programming LanguagesRough Division

compiled (fortran, C, C++, Java, . . . )

Pros

speed

large code base (legacy codes)

tradition

Cons

(often) complicated build process,recompile after any change

low-level ⇒ lots of lines to getbasic stuff done

code size ⇒ maintenance problems

static!

interpreted or scripting (sh, tcl, matlab, perl, ruby, python, . . . )

Pros

no compiling

(very) high-level ⇒ a few of linesto get (complex) stuff done

code size ⇒ easy maintenance

dynamic!

(often) large code base

Cons

many are relatively new

not known as useful in manyscientific communities

lack of speed

Page 8: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

4/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Notes on Programming LanguagesRough Division

compiled (fortran, C, C++, Java, . . . )

Pros

speed

large code base (legacy codes)

tradition

Cons

(often) complicated build process,recompile after any change

low-level ⇒ lots of lines to getbasic stuff done

code size ⇒ maintenance problems

static!

interpreted or scripting (sh, tcl, matlab, perl, ruby, python, . . . )

Pros

no compiling

(very) high-level ⇒ a few of linesto get (complex) stuff done

code size ⇒ easy maintenance

dynamic!

(often) large code base

Cons

many are relatively new

not known as useful in manyscientific communities

lack of speed

Page 9: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

5/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Mixing Languages — Best of Both Worlds

low level code (C or fortran): element matrix evaluations, costlymesh-related functions, . . .

high level code (Python): logic of the code, particular applications,configuration files, problem description files

www.python.org

SfePy = Python + C (+ fortran)

notable features:

small size (complete sources are just about 1.3 MB, July 2008)fast compilationproblem description files in pure Pythonproblem description form similar to mathematical description “onpaper”

Page 10: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

5/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Mixing Languages — Best of Both Worlds

low level code (C or fortran): element matrix evaluations, costlymesh-related functions, . . .

high level code (Python): logic of the code, particular applications,configuration files, problem description files

www.python.org

SfePy = Python + C (+ fortran)

notable features:

small size (complete sources are just about 1.3 MB, July 2008)fast compilationproblem description files in pure Pythonproblem description form similar to mathematical description “onpaper”

Page 11: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

6/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Software Dependencies

to install and use SfePy, several other packages or libraries areneeded:

NumPy and SciPy: free (BSD license) collection of numericalcomputing libraries for Python

enables Matlab-like array/matrix manipulations and indexing

other: UMFPACK, Pyparsing, Matplotlib, Pytables (+ HDF5), swigvisualization of results: ParaView, MayaVi2, or any otherVTK-capable viewer

missing:

free (BSD license) 3D mesh generation and refinement tool. . . can use netgen, tetgen

Page 12: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

6/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Software Dependencies

to install and use SfePy, several other packages or libraries areneeded:

NumPy and SciPy: free (BSD license) collection of numericalcomputing libraries for Python

enables Matlab-like array/matrix manipulations and indexing

other: UMFPACK, Pyparsing, Matplotlib, Pytables (+ HDF5), swigvisualization of results: ParaView, MayaVi2, or any otherVTK-capable viewer

missing:

free (BSD license) 3D mesh generation and refinement tool. . . can use netgen, tetgen

Page 13: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

7/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Introduction

problem description file is a regular Python module, i.e. all Pythonsyntax and power is accessibleconsists of entities defining:

fields of various FE approximations, variablesequations in the weak form, quadraturesboundary conditions (Dirichlet, periodic, “rigid body”)FE mesh file name, options, solvers, . . .

simple example: the Laplace equation:

c∆u = 0 in Ω, u = u on Γ, weak form:

∫Ω

c ∇u · ∇v = 0, ∀v ∈ V0

Page 14: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

8/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — FE Approximations

mesh → define FE approximation to Ω:

filename mesh = ’simple.mesh’

fields → define space Vh:

field 1 = ’name’ : ’temperature’,’dim’ : (1,1),’domain’ : ’Omega’,’bases’ : ’Omega’ : ’3 4 P1’

’3 4 P1’ means P1 approximation, in 3D, on 4-node FEs (tetrahedra)

variables → define uh, vh:

variables = ’u’ : (’unknown field’, ’temperature’, 0),’v’ : (’test field’, ’temperature’, ’u’),

Page 15: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

8/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — FE Approximations

mesh → define FE approximation to Ω:

filename mesh = ’simple.mesh’

fields → define space Vh:

field 1 = ’name’ : ’temperature’,’dim’ : (1,1),’domain’ : ’Omega’,’bases’ : ’Omega’ : ’3 4 P1’

’3 4 P1’ means P1 approximation, in 3D, on 4-node FEs (tetrahedra)

variables → define uh, vh:

variables = ’u’ : (’unknown field’, ’temperature’, 0),’v’ : (’test field’, ’temperature’, ’u’),

Page 16: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

8/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — FE Approximations

mesh → define FE approximation to Ω:

filename mesh = ’simple.mesh’

fields → define space Vh:

field 1 = ’name’ : ’temperature’,’dim’ : (1,1),’domain’ : ’Omega’,’bases’ : ’Omega’ : ’3 4 P1’

’3 4 P1’ means P1 approximation, in 3D, on 4-node FEs (tetrahedra)

variables → define uh, vh:

variables = ’u’ : (’unknown field’, ’temperature’, 0),’v’ : (’test field’, ’temperature’, ’u’),

Page 17: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

9/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — Boundary Conditions

regions → define domain Ω, regions Γleft, Γright, Γ = Γleft ∪ Γright:

h omitted from now on . . .

regions = ’Omega’ : (’all’, ),’Gamma Left’ : (’nodes in (x < 0.0001)’, ),’Gamma Right’ : (’nodes in (x > 0.0999)’, ),

Dirichlet BC → define u on Γleft, Γright:

ebcs = ’t left’ : (’Gamma Left’, ’u.0’ : 2.0),’t right’ : (’Gamma Right’, ’u.all’ : -2.0),

Page 18: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

9/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — Boundary Conditions

regions → define domain Ω, regions Γleft, Γright, Γ = Γleft ∪ Γright:

h omitted from now on . . .

regions = ’Omega’ : (’all’, ),’Gamma Left’ : (’nodes in (x < 0.0001)’, ),’Gamma Right’ : (’nodes in (x > 0.0999)’, ),

Dirichlet BC → define u on Γleft, Γright:

ebcs = ’t left’ : (’Gamma Left’, ’u.0’ : 2.0),’t right’ : (’Gamma Right’, ’u.all’ : -2.0),

Page 19: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

10/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — Equations

materials → define c:material 1 =

’name’ : ’m’,’mode’ : ’here’,’region’ : ’Omega’,’c’ : 1.0,

integrals → define numerical quadrature:

integral 1 = ’name’ : ’i1’,’kind’ : ’v’,’quadrature’ : ’gauss o1 d3’,

equations → define what and where should be solved:

equations = ’eq’ : ’dw laplace.i1.Omega( m.c, v, u ) = 0’

Page 20: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

10/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — Equations

materials → define c:material 1 =

’name’ : ’m’,’mode’ : ’here’,’region’ : ’Omega’,’c’ : 1.0,

integrals → define numerical quadrature:

integral 1 = ’name’ : ’i1’,’kind’ : ’v’,’quadrature’ : ’gauss o1 d3’,

equations → define what and where should be solved:

equations = ’eq’ : ’dw laplace.i1.Omega( m.c, v, u ) = 0’

Page 21: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

10/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Problem Description FileSolving Laplace Equation — Equations

materials → define c:material 1 =

’name’ : ’m’,’mode’ : ’here’,’region’ : ’Omega’,’c’ : 1.0,

integrals → define numerical quadrature:

integral 1 = ’name’ : ’i1’,’kind’ : ’v’,’quadrature’ : ’gauss o1 d3’,

equations → define what and where should be solved:

equations = ’eq’ : ’dw laplace.i1.Omega( m.c, v, u ) = 0’

Page 22: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

11/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Running SfePy

$ . / s i m p l e . py i n p u t / p o i s s o n . pys f e p y : r e a d i n g mesh . . .s f e p y : . . . done i n 0 . 0 2 ss f e p y : s e t t i n g up domain edges . . .s f e p y : . . . done i n 0 . 0 2 ss f e p y : s e t t i n g up domain f a c e s . . .s f e p y : . . . done i n 0 . 0 2 ss f e p y : c r e a t i n g r e g i o n s . . .s f e p y : l e a f Gamma Right r e g i o n 4s f e p y : l e a f Omega r e g i o n 1 0 0 0s f e p y : l e a f Gamma Left r e g i o n 0 3s f e p y : . . . done i n 0 . 0 7 ss f e p y : e q u a t i o n ” Temperature ” :s f e p y : d w l a p l a c e . i 1 . Omega( c o e f . v a l , s , t ) = 0s f e p y : d e s c r i b i n g g e o m e t r i e s . . .s f e p y : . . . done i n 0 . 0 1 ss f e p y : s e t t i n g up d o f c o n n e c t i v i t i e s . . .s f e p y : . . . done i n 0 . 0 0 ss f e p y : u s i n g s o l v e r s :

n l s : newtonl s : l s

s f e p y : m a t r i x shape : ( 30 0 , 300)s f e p y : a s s e m b l i n g m a t r i x graph . . .s f e p y : . . . done i n 0 . 0 1 ss f e p y : m a t r i x s t r u c t u r a l n o n z e r o s : 3538 ( 3 . 9 3 e−02% f i l l )s f e p y : u p d a t i n g m a t e r i a l s . . .s f e p y : c o e fs f e p y : . . . done i n 0 . 0 0 ss f e p y : n l s : i t e r : 0 , r e s i d u a l : 1 .176265 e−01 ( r e l : 1 .000000 e +00)s f e p y : r e z i d u a l : 0 . 0 0 [ s ]s f e p y : s o l v e : 0 . 0 1 [ s ]s f e p y : m a t r i x : 0 . 0 0 [ s ]s f e p y : n l s : i t e r : 1 , r e s i d u a l : 9 .921082 e−17 ( r e l : 8 .434391 e−16)

top level of SfePy code is acollection of executable scriptstailored for various applications

simple.py is dumb script ofbrute force, attempting to solveany equations it finds by theNewton method

. . . exactly what we need here(solver options were omitted inprevious slides)

Page 23: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

12/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Top-level Scripts

Main scripts / applications:

runTests.py . . . run all/selected unit tests

simple.py . . . generic problem solver, both for stationary andtime-dependent problems

eigen.py . . . application: acoustic band gaps in stronglyheterogenous media

schroedinger.py . . . application: Schrodinger equation solver

Auxiliary:

extractor.py . . . extract results stored in a HDF5 file, dumpresults to VTK

findSurf.py . . . extract a mesh surface, mark its components

gen . . . (re-)generate documentation, found indoc/sfepy manual.pdf, requires additional packages: pexpect, lxml

genPerMesh.py . . . scale and periodically repeat a reference volumemesh

Page 24: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

12/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Top-level Scripts

Main scripts / applications:

runTests.py . . . run all/selected unit tests

simple.py . . . generic problem solver, both for stationary andtime-dependent problems

eigen.py . . . application: acoustic band gaps in stronglyheterogenous media

schroedinger.py . . . application: Schrodinger equation solver

Auxiliary:

extractor.py . . . extract results stored in a HDF5 file, dumpresults to VTK

findSurf.py . . . extract a mesh surface, mark its components

gen . . . (re-)generate documentation, found indoc/sfepy manual.pdf, requires additional packages: pexpect, lxml

genPerMesh.py . . . scale and periodically repeat a reference volumemesh

Page 25: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

13/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Verification of Numerical Results

to verify numerical results we use method of manufactured solutions:for example, for Poisson’s equation div(grad(u)) = f :

1 make up a solution, e.g. u = sin 3x cos 4y2 compute corresponding f , here f = 25 sin 3x cos 4y, and boundary

conditions by substituting u into the equation3 solve numerically and compare the exact solution of the strong

problem with the numerical solution of the weak problem

→ allows to assess both the discretization and numerical errors

manual derivation of f tedious → SymPy

each term class annotated by a corresponding symbolic expressionexample: anisotropic diffusion term

symbolic = ’expression’: ’div( K * grad( u ) )’,

’map’ : ’u’ : ’state’, ’K’ : ’material’f is built by substituting the manufactured solution into theexpressions and subsequent evaluation in FE nodeswork in progress

Page 26: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

13/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Verification of Numerical Results

to verify numerical results we use method of manufactured solutions:for example, for Poisson’s equation div(grad(u)) = f :

1 make up a solution, e.g. u = sin 3x cos 4y2 compute corresponding f , here f = 25 sin 3x cos 4y, and boundary

conditions by substituting u into the equation3 solve numerically and compare the exact solution of the strong

problem with the numerical solution of the weak problem

→ allows to assess both the discretization and numerical errors

manual derivation of f tedious → SymPy

each term class annotated by a corresponding symbolic expressionexample: anisotropic diffusion term

symbolic = ’expression’: ’div( K * grad( u ) )’,

’map’ : ’u’ : ’state’, ’K’ : ’material’f is built by substituting the manufactured solution into theexpressions and subsequent evaluation in FE nodeswork in progress

Page 27: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

14/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Optimal Flow ProblemProblem Setting

Objective Function

Ψ(u) ≡ ν

2

∫Ωc

|∇u|2 −→ min

minimize gradients of solution (e.g. losses) in Ωc ⊂ Ωby moving design boundary Γ ⊂ ∂Ωperturbation of Γ by vector field V

Ω(t) = Ω + tV(x)x∈Ω where V = 0 in Ωc ∪ ∂Ω \ Γ

Γin ΓoutΓD

ΓD

ΓC ΩC

ΩD

Ω

Page 28: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

15/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Optimal Flow ProblemExample Results

flow and domain control boxes, left: initial, right: final

ΩC between two grey planes

work in progress . . .

Page 29: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

16/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Direct Problem. . . paper ↔ input file

weak form of Navier-Stokes equations: ? u ∈ V0(Ω), p ∈ L2(Ω)such that

aΩ (u, v) + cΩ (u, u, v)− bΩ (v, p) = gΓout (v) ∀v ∈ V0 ,

bΩ (u, q) = 0 ∀q ∈ L2(Ω) .(1)

in SfePy syntax:

equations = ’balance’ : """

dw div grad.i2.Omega( fluid.viscosity, v, u )

+ dw convect.i2.Omega( v, u )

- dw grad.i1.Omega( v, p ) = 0""",

’incompressibility’ : """

dw div.i1.Omega( q, u ) = 0""",

Page 30: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

16/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Direct Problem. . . paper ↔ input file

weak form of Navier-Stokes equations: ? u ∈ V0(Ω), p ∈ L2(Ω)such that

aΩ (u, v) + cΩ (u, u, v)− bΩ (v, p) = gΓout (v) ∀v ∈ V0 ,

bΩ (u, q) = 0 ∀q ∈ L2(Ω) .(1)

in SfePy syntax:

equations = ’balance’ : """

dw div grad.i2.Omega( fluid.viscosity, v, u )

+ dw convect.i2.Omega( v, u )

- dw grad.i1.Omega( v, p ) = 0""",

’incompressibility’ : """

dw div.i1.Omega( q, u ) = 0""",

Page 31: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

17/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Adjoint Problem. . . paper ↔ input file

KKT conditions δu,pL = 0 yield adjoint state problem for w, r:

δuL v = 0 = δuΨ(u, p) v

+ aΩ (v, w) + cΩ (v, u, w) + cΩ (u, v, w) + bΩ (v, r) ,

δpL q = 0 = δpΨ(u, p) q − bΩ (w, q) ,∀v ∈ V0, and ∀q ∈ L2(Ω).

in SfePy syntax:

equations = ’balance’ : """

dw div grad.i2.Omega( fluid.viscosity, v, w )

+ dw adj convect1.i2.Omega( v, w, u )

+ dw adj convect2.i2.Omega( v, w, u )

+ dw grad.i1.Omega( v, r )

= - ‘‘δuΨ(u , p) v ’’""",’incompressibility’ : """

dw div.i1.Omega( q, w ) = 0""",

Page 32: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

17/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Adjoint Problem. . . paper ↔ input file

KKT conditions δu,pL = 0 yield adjoint state problem for w, r:

δuL v = 0 = δuΨ(u, p) v

+ aΩ (v, w) + cΩ (v, u, w) + cΩ (u, v, w) + bΩ (v, r) ,

δpL q = 0 = δpΨ(u, p) q − bΩ (w, q) ,∀v ∈ V0, and ∀q ∈ L2(Ω).

in SfePy syntax:

equations = ’balance’ : """

dw div grad.i2.Omega( fluid.viscosity, v, w )

+ dw adj convect1.i2.Omega( v, w, u )

+ dw adj convect2.i2.Omega( v, w, u )

+ dw grad.i1.Omega( v, r )

= - ‘‘δuΨ(u , p) v ’’""",’incompressibility’ : """

dw div.i1.Omega( q, w ) = 0""",

Page 33: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

18/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Finite Element Formulation of Schrodinger Equation

One particle Schrodinger equation:(− ~2

2m∇2 + V

)ψ = Eψ .

FEM:(Kij + Vij) qj = EMijqj + Fi ,

Vij =∫φiV φj dV ,

Mij =∫φiφj dV ,

Kij =~2

2m

∫∇φi · ∇φj dV ,

Fi =~2

2m

∮dψdn

φi dS .

Usually we set Fi = 0.

Page 34: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

19/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Particle in the Box

V (x) =

0, inside the box a× a× a∞, outside

Analytic solution:

En1n2n3 =π2

2a2

(n2

1 + n22 + n2

3

)where ni = 1, 2, 3, . . . are independent quantum numbers. We chosea = 1, i.e.: E111 = 14.804, E211 = E121 = E112 = 29.608,E122 = E212 = E221 = 44.413, E311 = E131 = E113 = 54.282E222 = 59.217, E123 = Eperm. = 69.087.

Numerical solution (a = 1, 24702 nodes):E 1 2-4 5-7 8-10 11 12-

theory 14.804 29.608 44.413 54.282 59.217 69.087FEM 14.861 29.833 44.919 55.035 60.123 70.305

29.834 44.920 55.042 70.31029.836 44.925 55.047 · · ·

Page 35: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

20/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

3D Harmonic Oscillator

V (r) =

12ω

2r2, inside the box a× a× a∞, outside

Analytic solution in the limit a→∞:

Enl =(

2n+ l +32

where n, l = 0, 1, 2, . . . . Degeneracy is 2l + 1, so: E00 = 32 , triple

E01 = 52 , E10 = 7

2 , quintuple E02 = 72 triple E11 = 9

2 , quintupleE12 = 11

2 :

Numerical solution (a = 15, ω = 1, 290620 nodes):E 1 2-4 5-10 11-

theory 1.5 2.5 3.5 4.5FEM 1.522 2.535 3.554 4.578

2.536 3.555 4.5792.536 3.555 4.579

3.555 · · ·3.5563.556

Page 36: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

21/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

3D Harmonic Oscillator

Eigenvectors:0th

10th

12th

12th

Page 37: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

22/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Hydrogen Atom

V (r) =

− 1

r , inside the box a× a× a∞, outside

Analytic solution in the limit a→∞:

En = − 12n2

where n = 1, 2, 3, . . . . Degeneracy is n2, so: E1 = − 12 = −0.5,

E2 = − 18 = −0.125, E3 = − 1

18 = −0.055, E4 = − 132 = −0.031.

Numerical solution (a = 15, 160000 nodes):E 1 2-5 6-14 15-

theory -0.5 -0.125 -0.055 -0.031FEM -0.481 -0.118 -0.006 · · ·

Page 38: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

23/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Hydrogen Atom

11th eigenvalue (calculated: -0.04398532, exact: -0.056), on the meshwith 976 691 tetrahedrons and 163 666 nodes, for the hydrogen atom(V=-1/r).

Page 39: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

24/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Conclusion

What is done

basic FE element engine:

finite-dimensionalapproximations of continuousfieldsvariables, boundary conditions,FE assemblingequations, terms, regionsmaterials, material caches

various solvers accessed viaabstract interface

unit tests, automaticdocumentation generation

mostly linear problems, butmultiphysical

What is not done

general FE engine, possibly withsymbolic evaluation (SymPy)

good documentation

fast problem-specific solvers (!)

adaptive mesh refinement (!)

parallelization (petsc4py)

What will not be done (?)

GUI

real symbolic parsing/evaluation ofequations

http://sfepy.org

Page 40: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

24/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Conclusion

What is done

basic FE element engine:

finite-dimensionalapproximations of continuousfieldsvariables, boundary conditions,FE assemblingequations, terms, regionsmaterials, material caches

various solvers accessed viaabstract interface

unit tests, automaticdocumentation generation

mostly linear problems, butmultiphysical

What is not done

general FE engine, possibly withsymbolic evaluation (SymPy)

good documentation

fast problem-specific solvers (!)

adaptive mesh refinement (!)

parallelization (petsc4py)

What will not be done (?)

GUI

real symbolic parsing/evaluation ofequations

http://sfepy.org

Page 41: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

24/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Conclusion

What is done

basic FE element engine:

finite-dimensionalapproximations of continuousfieldsvariables, boundary conditions,FE assemblingequations, terms, regionsmaterials, material caches

various solvers accessed viaabstract interface

unit tests, automaticdocumentation generation

mostly linear problems, butmultiphysical

What is not done

general FE engine, possibly withsymbolic evaluation (SymPy)

good documentation

fast problem-specific solvers (!)

adaptive mesh refinement (!)

parallelization (petsc4py)

What will not be done (?)

GUI

real symbolic parsing/evaluation ofequations

http://sfepy.org

Page 42: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

24/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Conclusion

What is done

basic FE element engine:

finite-dimensionalapproximations of continuousfieldsvariables, boundary conditions,FE assemblingequations, terms, regionsmaterials, material caches

various solvers accessed viaabstract interface

unit tests, automaticdocumentation generation

mostly linear problems, butmultiphysical

What is not done

general FE engine, possibly withsymbolic evaluation (SymPy)

good documentation

fast problem-specific solvers (!)

adaptive mesh refinement (!)

parallelization (petsc4py)

What will not be done (?)

GUI

real symbolic parsing/evaluation ofequations

http://sfepy.org

Page 43: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

25/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

Yes, the final slide!

Acknowledgements

The work on various parts of SfePy has been supported by the followinggrants and research projects funded by several agencies in the CzechRepublic:

Robert Cimrman:

grant project GACR 101/07/1471, entitled “Finite elementmodelling of linear, non-linear and multiscale effects in wavepropagation in solids and heterogeneous media”research project MSMT 1M06031research project MSMT 4977751303

Ondrej Certık:

research center project LC06040grant project GACR IAA100100637

Page 44: SfePy - Simple Finite Elements in Python - Short · PDF file1/26 Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion SfePy - Simple Finite Elements

26/26

Introduction Our choice Complete Example (Simple) Testing Example Problems Conclusion

This is not a slide!

1

1Do you like Monty Python’s Flying Circus? It helps! (Python FAQ 1.1.17)