Py Foam Advanced
Py Foam Advanced
Py Foam Advanced
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
1/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
1
Introduction
Overview
PyFoam
Technicalities
Python - a crash course
The problem
What were simulating
The solver
Getting our hands dirty
Manual case setup
Preparing the case
Running the simulation
bgschaid
customRegexp
Post processing
Case parametrisation
Dictionaries
Templates
An old-school script
A python script
Parameter variation
Looking at one line after another
Simple scripts using it
Script with variations
Using it and results
2/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
It assumes
that you know your way around OpenFOAMTM
have programmed a little bit in some language (not
necessarily Python)
bgschaid
3/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
What is PyFoam
PyFoam is
1 a Python library that
manipulates OpenFOAMTM -cases
controls OpenFOAMTM -runs
2
bgschaid
4/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
Gartengschirrl
A specialist on calculating the damBreak-case
... using PyFoam
bgschaid
5/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
Conventions
Whenever Ignaz (and also you)
writes something
Ignazs Python-code
on the shell
in an editor
> date
Fri May 15 01:56:12 CEST 2009
bgschaid
sum =0
for v in [7 ,13 ,42]:
sum += v
print " The sum is " , sum
# this is a long line that will be <brk>
<cont> continued in the next line
fooCoeffs {
bar 23;
lst ( inlet outlet ) ;
}
6/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
>
>
>
>
cd $HOME
mkdir pyFoamAdvanced
cd pyFoamAdvanced
tar xzf / cdrom / OFW5 / Ad van ced _Tr aini ng / pyFoamAdvanced . git . tgz
bgschaid
7/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
8/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
What is Python
Python
Is a scripting language
No compilation required
Is object-oriented
Comes batteries included: has a large standard-library for
the library
Widely used
Pre-installed on most Linux-systems because many system
tools (installers for instance) use it
Becomes scripting language of choice for a number of
programs (amongst others the post-processors ParaView
and Visit and the pre-processor Salome)
bgschaid
9/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
10/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
Running a Python-file
> python test . py
> python
Python 2.6.5 ( r265 :79063 , May 18 2010 , 10:54:45)
[ GCC 4.2.1 ( Apple Inc . build 5659) ] on darwin
Type " help " , " copyright " , " credits " or " license " for more information .
> >> 1+1
2
> >> import sys
> >> print sys . platform
darwin
> >>
11/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
12/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
13/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
14/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
15/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
bgschaid
16/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
3
5
7
9
11
13
17/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Overview
PyFoam
Technicalities
Python - a crash course
Help on PyFoam
With the help of a program called epydoc a more user
bgschaid
18/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
19/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
design
Inlet and particle injector at the left
bgschaid
20/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
way that
The number of particles in the second bin maximizes
No (or as few as possible) particles leave through the oultet
bgschaid
21/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Basis: rhoPisoTwinParcelFoam
transient
compressible
turbulent
has two different populations of particles
bgschaid
22/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
In basicAbscheideParcel.C
1
3
5
7
9
We notice that Ignaz never bothered to look up the English word for
23/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
24/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
A clean slate
Making a copy
1
bgschaid
25/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
26/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
7
9
11
13
bgschaid
27/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
> p y Fo am Decompose . py . 2
...
> p y F o a m C aseReport . py -- decomposition .
Case is decomposed for 2 processors
|
0
1
----------------------------Points |
814
770
Faces |
1486
1404
Cells |
360
340
----------------------------abscheider1 |
10
0
abscheider2 |
0
10
bottom |
36
34
defaultFaces |
720
680
inlet |
10
0
outlet |
0
10
top |
26
24
bgschaid
28/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
5
...
7
9
11
13
15
17
bgschaid
29/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
the name)
Usually a letter is matched by itself
. matches any letter
bgschaid
30/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
parcels
{
bgschaid
31/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
Simple plot
2
amount
{
theTitle " Mass removed " ;
expr " Abgeschieden at (.+) <brk>
<cont> with .+ total : (% f %)<brk>
<cont> " ;
type dynamic ;
idNr 1;
with impulses ;
accumulation sum ;
6
8
}
32/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Using it to plot
Displaying from a logfile
1
Using matplotlib
1
bgschaid
33/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
34/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
35/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
36/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
A clean copy
Cloning again
1
bgschaid
37/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
bgschaid
38/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
This is the part of 0/U that Ignaz does not want to touch
with his text editor
1
dimensions
[0 1 -1 0 0 0 0];
internalField
uniform (0 0 0) ;
boundaryField
{
inlet
{
type
value
}
...
7
9
11
fixedValue ;
uniform (1 0 0) ;
bgschaid
39/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Reading a dictionary
This abstract example
shows how a
dictionary-file is seen
inside a
Python-program
The dictionary
testDict
The Python-code
1
7
9
11
model theModel ;
patches ( inlet
outlet ) ;
13
15
17
theMo delCoeffs {
alpha 0.3;
beta 2 inlet ;
}
11
13
# manipulation is possible
file [ " theModelCoeffs " ][ " alpha " ]+=1
file [ " newCoeffs " ]={ " a " :1 , " b " :[2 ,3 ,4]}
15
print file
bgschaid
40/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
3
5
7
bgschaid
41/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
The template file is called the same as the file it will produce
Just with .template appended
Lines that start with $$ are variable declarations
They dont appear in the final output
Strings between $ and $ are treated as Python-expressions
and evaluated
Previously declared variables are used
bgschaid
42/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
In constant/polyMeshDict.template
1
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <brk>
<cont> * //
$$
$$
$$
$$
$$
$$
$$
5
7
9
x1 = preLen
x2 = preLen + cavityWidth
x3 = preLen + cavityWidth + interLen
x4 = preLen +2* cavityWidth + interLen
x5 =2* preLen +2* cavityWidth + interLen
y1 = channelHeight
y2 = - cavityDepth
11
c on ve rt T oMeters 1.0;
13
...
bgschaid
43/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Later in constant/polyMeshDict.template
1
3
5
7
9
11
13
...
vertices
(
(0
(0
( $x1$
( $x1$
(0
(0
( $x1$
( $x1$
( $x2$
( $x2$
...
0
-0.01)
0
0.01)
0
0.01)
0
-0.01)
$y1$ -0.01)
$y1$ 0.01)
$y1$ 0.01)
$y1$ -0.01)
0
0.01)
0
-0.01)
bgschaid
44/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
The blocks
Further down in constant/polyMeshDict.template
2
4
10
12
....
blocks
(
hex (0 1 2 3 4 5 6 7) (1 $int ( ceil ( preLen / dx ) ) $ $int ( ceil ( channelHeight<brk>
<cont> / dx ) ) $ ) simpleGrading (1 1 1)
hex (3 2 8 9 7 6 10 11) (1 $int ( ceil ( cavityWidth / dx ) ) $ $int ( ceil (<brk>
<cont> channelHeight / dx ) ) $ ) simpleGrading (1 1 1)
hex (9 8 12 13 11 10 14 15) (1 $int ( ceil ( interLen / dx ) ) $ $int ( ceil (<brk>
<cont> channelHeight / dx ) ) $ ) simpleGrading (1 1 1)
hex (13 12 16 17 15 14 18 19) (1 $int ( ceil ( cavityWidth / dx ) ) $ $int ( ceil (<brk>
<cont> channelHeight / dx ) ) $ ) simpleGrading (1 1 1)
hex (17 16 20 21 19 18 22 23) (1 $int ( ceil ( preLen / dx ) ) $ $int ( ceil (<brk>
<cont> channelHeight / dx ) ) $ ) simpleGrading (1 1 1)
hex (25 24 26 27 3 2 8 9) (1 $int ( ceil ( cavityWidth / dx ) ) $ $int ( ceil (<brk>
<cont> cavityDepth / dx ) ) $ ) simpleGrading (1 1 1)
hex (29 28 30 31 13 12 16 17) (1 $int ( ceil ( cavityWidth / dx ) ) $ $int ( ceil (<brk>
<cont> cavityDepth / dx ) ) $ ) simpleGrading (1 1 1)
);
....
bgschaid
45/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
blocks
(
hex
hex
hex
hex
hex
hex
hex
);
(0 1 2 3 4 5 6 7) (1 40 20) simpleGrading (1 1 1)
(3 2 8 9 7 6 10 11) (1 30 20) simpleGrading (1 1 1)
(9 8 12 13 11 10 14 15) (1 10 20) simpleGrading (1 1 1)
(13 12 16 17 15 14 18 19) (1 30 20) simpleGrading (1 1 1)
(17 16 20 21 19 18 22 23) (1 40 20) simpleGrading (1 1 1)
(25 24 26 27 3 2 8 9) (1 30 4) simpleGrading (1 1 1)
(29 28 30 31 13 12 16 17) (1 30 4) simpleGrading (1 1 1)
bgschaid
46/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
> blockMesh
> p y F o a m P lotRunner . py -- hardcopy -- progress -- clear <brk>
<cont> r h o P i s o A b s c h e i d P a r c e l F o a m
> p y F o a m P VSnapshot . py -- time =1.5 .
bgschaid
47/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Cloning again
bgschaid
48/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
bgschaid
49/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
# ! / bin / sh
TEMPLATE = $1
TARGET = $2
VELOCITY = $3
DX = $4
5
7
GEOMETRY = " { preLen :0.2 , cavityWidth :0.15 , channelHeight :0.1 , interLen :0.05 , cavityDepth <brk>
<cont> :0.02 , dx : $DX } "
9
echo " Creating case $TARGET "
11
13
15
echo " Calling blockMesh and decompose "
17
19
21
23
25
bgschaid
50/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Executing it
... is easy
1
3
bgschaid
51/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
bgschaid
52/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Script reloaded
bgschaid
53/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
import sys
from os import path
5
7
9
11
13
15
dx =0.01
if len ( sys . argv ) >4:
dx = float ( sys . argv [4])
54/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
bgschaid
55/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
56/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
3
5
bgschaid
57/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Administrative stuff
bgschaid
58/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Running blockMesh
Running and checking - scripts/prepareRun.py
2
4
6
8
10
argv
silent
server
logname
59/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Decomposing
Using the functionality of a command line-tool scripts/prepareRun.py
2
4
6
8
shell
The command is executed immediately at construction
bgschaid
60/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
from PyFoam . Execution . Par all elE xec uti on import LAMMachine
machine = LAMMachine ( nr =2)
bgschaid
61/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
62/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
bgschaid
63/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Dictionaries
Templates
An old-school script
A python script
Running it
Ignaz tries the script
1
3
5
7
9
11
13
15
17
19
bgschaid
64/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
65/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
66/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
bgschaid
67/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
import re
7
9
self . massLeft =0
self . removed ={}
11
13
expressions
bgschaid
68/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Analyzing a line
These two methods are
necessary scripts/AbscheideAnalyzer.py
1
3
5
7
9
The try/except-construct
11
try :
13
15
17
19
analyzeLine
Checks whether one of the
regular expressions was
matched
Updates the data according
to the matches
bgschaid
69/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Reporting
These two methods are Ignazs
idea scripts/AbscheideAnalyzer.py
1
3
5
7
11
13
15
bgschaid
70/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
An analyzer
A class to call the LineAnalyzer scripts/AbscheideAnalyzer.py
1
class A b s c h e i d e L o g A n a l y z e r ( FoamLogAnalyzer ) :
def __init__ ( self ) :
super ( AbscheideLogAnalyzer , self ) . __init__ ()
self . addAnalyzer ( " abscheid " , A bsc heid eAn aly zer () )
5
7
9
bgschaid
71/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
import sys
if __name__ == __main__ :
log = sys . argv [1]
abscheid = A b s c h e i d e L o g A n a l y z e r ()
from PyFoam . LogAnalysis . L o g A n a l y z e r A p p l i c a t i o n import <brk>
<cont> L o g A n a l y z e r A p p l i c a t i o n
analyze = L o g A n a l y z e r A p p l i c a t i o n ( abscheid )
analyze . run ( log )
9
abscheid . doReport ()
> ./ scripts / A bsc hei deAn aly zer . py ../ step1 / testSeparator / PyFoamRunner .<brk>
<cont> r h o P i s o A b s c h e i d P a r c e l F o a m . logfile
Mass left in system : 2.920114636 e -05
Removed by outlet : 0.00997079885178
Total mass accounted for : 0.00999999999814
bgschaid
72/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
73/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Setting up stuff
Start of scripts/inletVelocityVariation.py
1
import re , sys
from os import path
3
sys . path . append ( path . dirname ( path . abspath ( sys . argv [0]) ) )
5
from A b s c h eid eAna lyz er import Ab sch eid eAna lyz er
7
9
11
from
from
from
from
from
13
15
17
74/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
14
16
18
abscheid . doReport ()
20
22
24
26
75/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
While this is running you can have a look at the data this will
produce in exampleResults/velTest.csv
Another remark: due to a bug in ParaView or PVSnapshot
bgschaid
76/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
in system)
bgschaid
77/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Suggested exercises
Try to find the optimum velocity (where the most mass comes
to abscheider2)
Obviously extend the simulation time. But not too long.
injector
Check whether the cavities are to shallow
For this the mesh preparation would have to be pulled into the
loop
Try varying the upper channel wall
uniform (just the height)
making it narrower or wider in the middle
bgschaid
78/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Acknowledgments
Thanks to
Holger Marschall for the idea for this tutorial
Astrid Mahrla for some pictures
bgschaid
79/80
Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation
Suggestions/Questions
Bug reports at
http://sourceforge.net/apps/mantisbt/openfoam-extend/view_all_bug_page.php
bgschaid
80/80