Python + FEM Introduction to SFE Robert Cimrman Department of Mechanics & New Technologies Research Centre University of West Bohemia Plzeň, Czech Republic April 3, 2007, Plzeň 1/22
Outline 1 Introduction Programming Languages Programming Techniques 2 Our choice Mixing Languages Best of Both Worlds Python Software Dependencies 3 Example Problem Continuous Formulation Weak Formulation Problem Description File 4 Final slide :-) 2/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Programming Languages Pros Pros 1 compiled (fortran, C, C++, Java,... ) speed, speed, speed,..., did I say speed? large code base (legacy codes), tradition Cons (often) complicated build process, recompile after any change low-level lots of lines to get basic stuff done code size maintenance problems static! 2 interpreted or scripting (sh, tcl, matlab, perl, ruby, python,... ) no compiling (very) high-level a few of lines to get (complex) stuff done code size easy maintenance dynamic! (often) large code base Cons many are relatively new lack of speed, or even utterly slow! 3/22
Example Python Code 4/22
Example C Code char s [ ] = s e l tudy, mel dudy. a s e l opravdu tudy. ; i n t s t r f i n d ( char s t r, char t g t ) { i n t t l e n = s t r l e n ( t g t ) ; i n t max = s t r l e n ( s t r ) t l e n ; r e g i s t e r i n t i ; f o r ( i = 0 ; i < max ; i ++) { i f ( strncmp (& s t r [ i ], tgt, t l e n ) == 0) r e t u r n i ; r e t u r n 1; i n t i 1 ; i 1 = s t r f i n d ( s, tudy ) ; p r i n t f ( %d\n, i 1 ) ; 5/22
Programming Techniques Random remarks: spaghetti code modular programming OOP design patterns (gang-of-four, GO4) post-oop dynamic languages... all used, but I try to move down the list! :-) 6/22
Programming Techniques Random remarks: spaghetti code modular programming OOP design patterns (gang-of-four, GO4) post-oop dynamic languages... all used, but I try to move down the list! :-) 6/22
Mixing Languages Best of Both Worlds Low level: C FE matrix evaluations costly mesh-related functions (listing faces, edges, surface generation,... )... can use also fortran High level: Python logic of the code configuration files! problem input files! + www.python.org batteries included 7/22
Mixing Languages Best of Both Worlds Low level: C FE matrix evaluations costly mesh-related functions (listing faces, edges, surface generation,... )... can use also fortran High level: Python logic of the code configuration files! problem input files! + www.python.org batteries included 7/22
Python I Python r is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code.... batteries included 8/22
Python II 1.1.16 Why is it called Python? At the same time he began implementing Python, Guido van Rossum was also reading the published scripts from Monty Python s Flying Circus (a BBC comedy series from the seventies, in the unlikely case you didn t know). It occurred to him that he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python. 1.1.17 Do I have to like Monty Python s Flying Circus? No, but it helps. :)... General Python FAQ 9/22
Python III NASA uses Python...... so does Rackspace, Industrial Light and Magic, AstraZeneca, Honeywell, and many others. http://wiki.python.org/moin/numericandscientific 10/22
Python III NASA uses Python...... so does Rackspace, Industrial Light and Magic, AstraZeneca, Honeywell, and many others. http://wiki.python.org/moin/numericandscientific 10/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Software Dependencies 1 libraries & modules SciPy: free (BSD license) collection of numerical computing libraries for Python enables Matlab-like array/matrix manipulations and indexing UMFPACK: very fast direct solver for sparse systems Python wrappers are part of SciPy :-) output files (results) and data files in HDF5: general purpose library and file format for storing scientific data, efficient storage and I/O... via PyTables misc: Pyparsing, Matplotlib 2 tools SWIG: automatic generation of Python-C interface code Medit: simple mesh and data viewer ParaView: VTK-based advanced mesh and data viewer in Tcl/Tk MayaVi/MayaVi2: VTK-based advanced mesh and data viewer in Python 11/22
Shape Optimization in Incompressible Flow Problems Objective Function Ψ Γ (u) ν u 2 min (1) 2 c 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 \ Γ (2) Γ D Γ in Γ D D Γ C C Γout 12/22
Shape Optimization in Incompressible Flow Problems Objective Function Ψ Γ (u) ν u 2 min (1) 2 c 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 \ Γ (2) Γ D Γ in Γ D D Γ C C Γout 12/22
Shape Optimization in Incompressible Flow Problems Objective Function Ψ Γ (u) ν u 2 min (1) 2 c 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 \ Γ (2) Γ D Γ in Γ D D Γ C C Γout 12/22
Appetizer flow and spline boxes, left: initial, right: final connectivity of spline boxes (6 boxes in different colours) 13/22
Continuous Formulation laminar steady state incompressible Navier-Stokes equations ν 2 u + u u + p = 0 in (3) u = 0 in (4) boundary conditions u = u 0 on Γ in pn + ν u n = pn on Γ out u = 0 on Γ walls (5) adjoint equations for computing the adjoint solution w: ν 2 w + u w u w r = χ c ν 2 u in w = 0 in (6) w = 0 on Γ in Γ walls and χ c is the characteristic function of c 14/22
Continuous Formulation laminar steady state incompressible Navier-Stokes equations ν 2 u + u u + p = 0 in (3) u = 0 in (4) boundary conditions u = u 0 on Γ in pn + ν u n = pn on Γ out u = 0 on Γ walls (5) adjoint equations for computing the adjoint solution w: ν 2 w + u w u w r = χ c ν 2 u in w = 0 in (6) w = 0 on Γ in Γ walls and χ c is the characteristic function of c 14/22
Weak Formulation state problem: find u, p (from suitable function spaces) such that ν v : u + (u u) v p v = pv n v V 0, Γ out q u = 0 q Q adjoint equations: find w, r (from suitable function spaces) such that ν v : w + (v u) w + (u v) w + r v = ν v : u v V 0, (8) c q w = 0 q Q + proper BC... 15/22 (7)
Weak Formulation state problem: find u, p (from suitable function spaces) such that ν v : u + (u u) v p v = pv n v V 0, Γ out q u = 0 q Q adjoint equations: find w, r (from suitable function spaces) such that ν v : w + (v u) w + (u v) w + r v = ν v : u v V 0, (8) c q w = 0 q Q + proper BC... 15/22 (7)
Weak Formulation state problem: find u, p (from suitable function spaces) such that ν v : u + (u u) v p v = pv n v V 0, Γ out q u = 0 q Q adjoint equations: find w, r (from suitable function spaces) such that ν v : w + (v u) w + (u v) w + r v = ν v : u v V 0, (8) c q w = 0 q Q + proper BC... 15/22 (7)
Problem Description File I a regular python file all python machinery is accessible! requires/recognizes several keywords to define mesh regions subdomains, boundaries, characteristic functions,... boundary conditions Dirichlet fields field approximation per subdomain variables unknown fields, test fields, parameters equations materials constitutive relations, e.g. newton fluid, elastic solid,... solver parameters nonlinear solver, flow solver, optimization solver,...... 16/22
Problem Description File I a regular python file all python machinery is accessible! requires/recognizes several keywords to define mesh regions subdomains, boundaries, characteristic functions,... boundary conditions Dirichlet fields field approximation per subdomain variables unknown fields, test fields, parameters equations materials constitutive relations, e.g. newton fluid, elastic solid,... solver parameters nonlinear solver, flow solver, optimization solver,...... 16/22
Problem Description File II mesh f i lename m e s h = s i m p l e. mesh regions r e g i o n 1 0 0 0 = { name : Omega, s e l e c t : e l e m e n t s o f group 6, r e g i o n 0 = { name : Walls, s e l e c t : nodes o f s u r f a c e n r. Outlet, r e g i o n 1 = { name : I n l e t, s e l e c t : nodes by c i n c ( x, y, z, 0 ), r e g i o n 2 = { name : Outlet, s e l e c t : nodes by c i n c ( x, y, z, 1 ), r e g i o n 1 0 0 = { name : Omega C, # C o n t r o l domain. s e l e c t : nodes i n ( x > 25.9999 e 3) & ( x < 18.990 e 3), r e g i o n 1 0 1 = { name : Omega D, # Design domain. s e l e c t : a l l e r. Omega C, 17/22
Problem Description File III fields f i e l d 1 = { name : 3 v e l o c i t y, dim : ( 3, 1 ), f l a g s : ( BQP, ), bases : ( ( Omega, 3 4 P1B ), ) f i e l d 2 = { name : p r e s s u r e, dim : ( 1, 1 ), f l a g s : ( ), bases : ( ( Omega, 3 4 P1 ), ) variables v a r i a b l e s = { u : ( f i e l d, parameter, 3 v e l o c i t y, ( 3, 4, 5 ) ), w 0 : ( f i e l d, parameter, 3 v e l o c i t y, ( 3, 4, 5 ) ), w : ( f i e l d, unknown, 3 v e l o c i t y, ( 3, 4, 5 ), 0 ), v : ( f i e l d, t e s t, 3 v e l o c i t y, ( 3, 4, 5 ), w ), p : ( f i e l d, parameter, p r e s s u r e, ( 9, ) ), r : ( f i e l d, unknown, p r e s s u r e, ( 9, ), 1 ), q : ( f i e l d, t e s t, p r e s s u r e, ( 9, ), r ), Nu : ( f i e l d, m e s h V e l o c i t y, p r e s s u r e, ( 9, ) ), 18/22
Problem Description File III fields f i e l d 1 = { name : 3 v e l o c i t y, dim : ( 3, 1 ), f l a g s : ( BQP, ), bases : ( ( Omega, 3 4 P1B ), ) f i e l d 2 = { name : p r e s s u r e, dim : ( 1, 1 ), f l a g s : ( ), bases : ( ( Omega, 3 4 P1 ), ) variables v a r i a b l e s = { u : ( f i e l d, parameter, 3 v e l o c i t y, ( 3, 4, 5 ) ), w 0 : ( f i e l d, parameter, 3 v e l o c i t y, ( 3, 4, 5 ) ), w : ( f i e l d, unknown, 3 v e l o c i t y, ( 3, 4, 5 ), 0 ), v : ( f i e l d, t e s t, 3 v e l o c i t y, ( 3, 4, 5 ), w ), p : ( f i e l d, parameter, p r e s s u r e, ( 9, ) ), r : ( f i e l d, unknown, p r e s s u r e, ( 9, ), 1 ), q : ( f i e l d, t e s t, p r e s s u r e, ( 9, ), r ), Nu : ( f i e l d, m e s h V e l o c i t y, p r e s s u r e, ( 9, ) ), 18/22
Problem Description File IV Dirichlet BC ebc = { Walls : ( ( Walls, ( 3, 4, 5 ), 0. 0 ), ), I n l e t : ( ( V e l o c i t y I n l e t x, ( 4, ), 1. 0 ), ( V e l o c i t y I n l e t y z, ( 3, 5 ), 0. 0 ) ), state equations (compare with (7)) e q u a t i o n s = { b a l a n c e : + d w d i v g r a d. Omega ( f l u i d, v, w ) + dw convect. Omega ( v, w ) dw grad. Omega ( v, r ) = d w s u r f a c e l t r. O u t l e t ( b p r e s s, v ), i n c o m p r e s s i b i l i t y : d w d i v. Omega ( q, w ) = 0, 19/22
Problem Description File IV Dirichlet BC ebc = { Walls : ( ( Walls, ( 3, 4, 5 ), 0. 0 ), ), I n l e t : ( ( V e l o c i t y I n l e t x, ( 4, ), 1. 0 ), ( V e l o c i t y I n l e t y z, ( 3, 5 ), 0. 0 ) ), state equations (compare with (7)) e q u a t i o n s = { b a l a n c e : + d w d i v g r a d. Omega ( f l u i d, v, w ) + dw convect. Omega ( v, w ) dw grad. Omega ( v, r ) = d w s u r f a c e l t r. O u t l e t ( b p r e s s, v ), i n c o m p r e s s i b i l i t y : d w d i v. Omega ( q, w ) = 0, 19/22
Problem Description File V adjoint equations (compare with (8)) e q u a t i o n s a d j o i n t = { b a l a n c e : + d w d i v g r a d. Omega ( f l u i d, v, w ) + dw adj convect1. Omega ( v, w, u ) + dw adj convect2. Omega ( v, w, u ) + dw grad. Omega ( v, r ) = d w a d j d i v g r a d 2. Omega C ( one, f l u i d, v, u ), i n c o m p r e s s i b i l i t y : d w d i v. Omega ( q, w ) = 0, materials m a t e r i a l 2 = { name : f l u i d, mode : here, r e g i o n : Omega, v i s c o s i t y : 1. 2 5 e 1, m a t e r i a l 1 0 0 = { name : b p r e s s, mode : here, r e g i o n : Omega, v a l : 0. 0, 20/22
Problem Description File V adjoint equations (compare with (8)) e q u a t i o n s a d j o i n t = { b a l a n c e : + d w d i v g r a d. Omega ( f l u i d, v, w ) + dw adj convect1. Omega ( v, w, u ) + dw adj convect2. Omega ( v, w, u ) + dw grad. Omega ( v, r ) = d w a d j d i v g r a d 2. Omega C ( one, f l u i d, v, u ), i n c o m p r e s s i b i l i t y : d w d i v. Omega ( q, w ) = 0, materials m a t e r i a l 2 = { name : f l u i d, mode : here, r e g i o n : Omega, v i s c o s i t y : 1. 2 5 e 1, m a t e r i a l 1 0 0 = { name : b p r e s s, mode : here, r e g i o n : Omega, v a l : 0. 0, 20/22
Yes, the final slide! What is done basic FE element engine approximations up to P2 on simplexes (possibly with bubble) Q1 tensor-product approximation on rectangles handling of fields and variables boundary conditions, DOF handling FE assembling equations, terms, regions materials, material caches generic solvers (Newton iteration, direct linear system solution) What is not done general FE engine, with symbolic evaluation (à la SyFi - merge it?) documentation, unit tests organization of solvers fast problem-specific solvers parallelization of both assembling and solving use PETSC? What will not be done (?) GUI real symbolic parsing/evaluation of equations 21/22
Yes, the final slide! What is done basic FE element engine approximations up to P2 on simplexes (possibly with bubble) Q1 tensor-product approximation on rectangles handling of fields and variables boundary conditions, DOF handling FE assembling equations, terms, regions materials, material caches generic solvers (Newton iteration, direct linear system solution) What is not done general FE engine, with symbolic evaluation (à la SyFi - merge it?) documentation, unit tests organization of solvers fast problem-specific solvers parallelization of both assembling and solving use PETSC? What will not be done (?) GUI real symbolic parsing/evaluation of equations 21/22
Yes, the final slide! What is done basic FE element engine approximations up to P2 on simplexes (possibly with bubble) Q1 tensor-product approximation on rectangles handling of fields and variables boundary conditions, DOF handling FE assembling equations, terms, regions materials, material caches generic solvers (Newton iteration, direct linear system solution) What is not done general FE engine, with symbolic evaluation (à la SyFi - merge it?) documentation, unit tests organization of solvers fast problem-specific solvers parallelization of both assembling and solving use PETSC? What will not be done (?) GUI real symbolic parsing/evaluation of equations 21/22
This is not a slide! 22/22