Py Foam Advanced

Download as pdf or txt
Download as pdf or txt
You are on page 1of 80
At a glance
Powered by AI
PyFoam is a Python library that can be used to manipulate OpenFOAM cases and control OpenFOAM runs. It allows simulations to be automated and parameters to be systematically varied to analyze results.

PyFoam allows OpenFOAM simulations to be run automatically and results to be evaluated programmatically using Python scripts. This avoids the need for manual and repetitive tasks in varying parameters and running simulations.

Some examples of how parameters can be varied include changing the inlet velocity, direction of the injector, cavity depths, and geometry of the upper channel wall through Python scripts.

Introduction

The problem
Manual case setup
Case parametrisation
Parameter variation

Automatization with pyFoam


How Python helps us to avoid contact with OpenFOAMTM
Bernhard F.W. Gschaider
bgschaid@ice-sf.at
ICE Str
omungsforschung

5th OpenFOAMTM -Workshop, Gothenburg


24.June 2010

bgschaid

Automatization with pyFoam

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

Automatization with pyFoam

2/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Aim of this presentation

This presentation shows how to use PyFoam to run a case


automatically and evaluate the results
It does not give
a full overview of Python
an overview of all the capabilities of PyFoam

It assumes
that you know your way around OpenFOAMTM
have programmed a little bit in some language (not

necessarily Python)
bgschaid

Automatization with pyFoam

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

Utilities based on that library

bgschaid

Automatization with pyFoam

4/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

What happened before


Last year in Montreal a presentation PyFoam - Happy

foaming with Python (URL below) was given


It is not necessary that you have read it
.... but helpful (Now its to late. Read it later.)

We were introduced to the CFD-engineer Ignaz

Gartengschirrl
A specialist on calculating the damBreak-case
... using PyFoam

During the last year Ignaz extended


his professional range
his knowledge of PyFoam
http://www.openfoamworkshop.org/2009/4th_Workshop/0_Feature_Presentations/OFW4_2009_Gschaider_PyFoam.pdf

bgschaid

Automatization with pyFoam

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

it will be found in a coloured box.


Sometimes there will be output,
too
Ignaz writes on the shell
1

Ignaz edits a file


1
3

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

Automatization with pyFoam

6/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Getting the git-archive with the data


The data and programs needed are in a tar-file on the stick
If you havent booted from the stick the paths may be different
for you
First we copy it to your home and create a working directory:

Preparing for work


2
4

>
>
>
>

cd $HOME
mkdir pyFoamAdvanced
cd pyFoamAdvanced
tar xzf / cdrom / OFW5 / Ad van ced _Tr aini ng / pyFoamAdvanced . git . tgz

Now there should be a directory pyFoamAdvanced.git

bgschaid

Automatization with pyFoam

7/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Getting to a specific step

What we extracted is a Git-archive


There are several branches for different phases
So if you couldnt follow you can catch up

Going to a specific step


2

> cd $HOME / pyFoamAdvanced


> git clone pyFoamAdvanced . git -b s t e p1 m a n u a l S t ar t i n g step1

Now there should be a directory step1

bgschaid

Automatization with pyFoam

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

many common tasks


Non essential parts (like regular expressions) were moved to

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

Automatization with pyFoam

9/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

3 things you have to know about Python

... to understand the programming examples


1

Indentation does the same thing { and } do for C++

[] signifies a list (which is an array)

{} is a dictionary (whose elements are accessed with [key])

self is the same as this in C++ (The object itself)


Aeh. The 4 things to know about Python are ....

bgschaid

Automatization with pyFoam

10/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

The Python Shell


Python-files usually end with the extension .py

Running a Python-file
> python test . py

But you can also use it interactively

Using the Python-shell


1
3
5
7
9

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

exit with Ctrl+D


bgschaid

Automatization with pyFoam

11/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Basic constructs - lists and dictionaries

Basic usage of lists and dictionaries


2
4
6
8
10
12
14

>>> lst =[2 , " interFoam " ,1.3]


>>> lst [0]
2
>>> lst +=[ " nothing " ]
>>> print lst
[2 , interFoam , 1.3 , nothing ]
>>> order ={ interFoam :1 , blockMesh :0 , paraFoam :2}
>>> order [ interFoam ]
1
>>> paraFoam in order
True
>>> order [ setFields ]=0.5
>>> print order
{ blockMesh : 0 , interFoam : 1 , setFields : 0.5 , paraFoam : 2}

bgschaid

Automatization with pyFoam

12/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Basic constructs - loops

Two kinds of loops


2
4
6
8
10
12

>>> vals =[2 ,3 ,5 ,7 ,11 ,13 ,17 ,19]


>>> for v in vals :
...
print v *v ,
...
4 9 25 49 121 169 289 361
>>> i =0
>>> while i < len ( vals ) :
...
vals [ i ]= " no "
...
i +=2
...
>>> print vals
[ no , 3 , no , 7 , no , 13 , no , 19]

bgschaid

Automatization with pyFoam

13/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Basic constructs - branches

One kind of branch is sufficient


2
4
6
8

>>> for i in range ( -3 ,3) :


...
if i ==0:
...
print " zero " ,
...
elif ( i %2) ==0:
...
print " even " ,
...
else :
...
print " odd " ,
...
odd even odd zero odd even

bgschaid

Automatization with pyFoam

14/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Basic constructs - classes

Ignaz tells us all he knows


1
3
5
7
9
11
13

>>> class Ignaz :


...
def __init__ ( self , val ) :
...
self . val = val
...
def __str__ ( self ) :
...
return " Ignaz says " + str ( self . val )
...
def forget ( self ) :
...
self . val = None
...
>>> ig = Ignaz (1+1)
>>> print ig
Ignaz says 2
>>> ig . forget ()
>>> print ig
Ignaz says None

bgschaid

Automatization with pyFoam

15/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

Basic constructs - importing libraries


Standing on the shoulders of ... other programmers
2
4
6

>>> from time import time


>>> time ()
1 2 7 5 6 5 83 1 4. 7 9 8 8 5 2 9
>>> import sys
>>> dir ( sys )
[ __d is play h oo k_ _ , __doc__ , __egginsert , __excepthook__ , __name__ , <brk>
<cont> __package__ , __plen , __stderr__ , __stdin__ , __stdout__ , <brk>
<cont> _ c l e a r _ t y p e _ c a c he , _current_f rames , _getframe , api_version , <brk>
<cont> argv , b u i l t i n _ m o d u l e _ n a m e s , byteorder , call_tracing , <brk>
<cont> callstats , copyright , displayhook , d o n t _ wr i t e _ b y t e co d e , <brk>
<cont> exc_clear , exc_info , exc_type , excepthook , exec_prefix , <brk>
<cont> executable , exit , flags , float_info , getcheckinterval , <brk>
<cont> g e t d e f a u l t e n c o d i n g , getdlopenflags , g e t f i l e s y s t e m e n c o d i n g , <brk>
<cont> getprofile , g e t re cur si on lim it , getrefcount , getsizeof , <brk>
<cont> gettrace , hexversion , last_type , last_value , maxint , <brk>
<cont> maxsize , maxunicode , meta_path , modules , path , path_hooks ,<brk>
<cont> p a t h _ i m p o r t e r _ c a c h e , platform , prefix , ps1 , ps2 , <brk>
<cont> py3kwarning , s e t a p p d e f a u l t e n c o d i n g , setch eckinterval , <brk>
<cont> setd lopenfla gs , setprofile , set rec ursi onl imi t , settrace , <brk>
<cont> stderr , stdin , stdout , subversion , version , version_info , <brk>
<cont> warnoptions ]
>>> print sys . version
2.6.5 ( r265 :79063 , May 18 2010 , 10:54:45)
[ GCC 4.2.1 ( Apple Inc . build 5659) ]

bgschaid

Automatization with pyFoam

16/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Overview
PyFoam
Technicalities
Python - a crash course

How to get help - On the shell


What is that ParsedParameterFile anyway
1

3
5

>>> from PyFoam . RunDictionary . P a r s e d P a ra m e t e r F i l e import <brk>


<cont> P a r s e dP a r a m e t e r Fi l e
>>> print P a r s ed P a r a m e t e rF i l e . getCaseDir . __doc__
Return the path to the case of this file ( if any valid case is found ) .
Else return None
>>> help ( P a r s e dP a r a m e t e r Fi l e
Help on class P a r s e dP a r a m e t e r Fi l e in module PyFoam . RunDictionary .<brk>
<cont> P a r s e dP a r a m e t e r Fi l e :

7
9
11
13

class P a r s ed P a r a m e t e rF i l e ( PyFoam . RunDictionary . FileBasis . FileBasisBackup )


| Parameterfile whose complete representation is read into
| memory , can be manipulated and afterwards written to disk
|
| Method resolution order :
...

All these texts were extracted from the source:


Python comes with an included Doxygen
bgschaid

Automatization with pyFoam

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

friendly Html-documentation can be produced


This documentation is included in the normal distribution

bgschaid

Automatization with pyFoam

18/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

Separating particles of different size


Ignaz is asked to design a particle separator
He knows the physics: Drag-force vs. gravity

bgschaid

Automatization with pyFoam

19/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

The simplified particle separator


After 2 weeks the CAD-engineer came up with this complex

design
Inlet and particle injector at the left

bgschaid

Automatization with pyFoam

20/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

What a solution looks like


The goal is: Change the inlet velocity of the channel in such a

way that
The number of particles in the second bin maximizes
No (or as few as possible) particles leave through the oultet

bgschaid

Automatization with pyFoam

21/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

Basis: rhoPisoTwinParcelFoam

Our solver is based on rhoPisoTwinParcelFoam which can

be found in the tutorials. It is

transient
compressible
turbulent
has two different populations of particles

Changes in rhoPisoAbscheidParcelFoam are


Only one particle population
New particle class

bgschaid

Automatization with pyFoam

22/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

Where do the particles go


The whole solver had to be written for this method:

In basicAbscheideParcel.C
1
3
5
7
9

void Foam :: b a s i c A b s c h e i d e P a r c e l :: hitPatch


(
const polyPatch & p , trackData & td
)
{
Pout << " Abgeschieden at " << p . name () << " with m : " << mass ()
<< " N : " << nParticle () << " = total : "
<< mass () * nParticle () << endl ;
KinematicParcel < basicAbscheideParcel >:: hitPatch (p , td ) ;
}

When a particle hits a patch (not a wall), this is the output:

Output on the terminal


Abgeschieden at outlet with m : 3.605329877 e -10 N : 41386.01459 = total : 1.492102349 e -05

We notice that Ignaz never bothered to look up the English word for

abscheiden in his dictionary


bgschaid

Automatization with pyFoam

23/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

What were simulating


The solver
Getting our hands dirty

Building the solver


1

Get the first stage (if we havent already done so)

Getting it with Git


1
3

> cd $HOME / py FoamAdva nced


> git clone pyFoam Advanced . git -b s t e p 1 m a n u a l S t a r t i n g step1
> cd step1

Compiling the solver (we only have to do this once)

Making sure we use the right OpenFOAMTM -version


1

> pyFoamExecute . py -- foam =1.5 - dev wmake solver / 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 1 5


PyFoam WARNING on line 152 of file / software / PyFoam / Fo a mI nf or mat io n . py : 1.5 - dev is <brk>
<cont> already being used

Compiling it for debugging


> pyFoamExecute . py -- foam =1.5 - dev -- force - debug wmake solver / 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 1 5

bgschaid

Automatization with pyFoam

24/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

A clean slate

Ignaz likes to start with a new case

Making a copy
1

> p y Fo am CloneCase . py -- add = default . pvsm cases / twoCavityChannel <brk>


<cont> testSeparator
PyFoam WARNING on line 85 of file / Users / bgschaid / private_python / PyFoam /<brk>
<cont> Applications / CloneCase . py : Directory does not exist . Creating
> cd testSeparator

Now he can modify the case any way he likes

bgschaid

Automatization with pyFoam

25/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Editing the blockMesh


Making blockMesh less painful
1

> p y F o a m D i s p l a y B l o c k M e s h . py constant / polyMesh / blockMeshDict

bgschaid

Automatization with pyFoam

26/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Checking the boundary conditions


All boundary conditions at once
1

> pyFoamC aseRepo rt . py -- short - bc .

Table of boundary conditions for t = 0

| abscheider1 | abscheider2 | bottom


| defaultFaces | inlet
| outlet
| top
-------------------------------------------------------------------------------------------------------------------Patch Type | patch
| patch
| wall
| empty
| patch
| patch
| wall
Length
| 10
| 10
| 70
| 1400
| 10
| 10
| 50
-------------------------------------------------------------------------------------------------------------------T
| inletOutlet | inletOutlet | zeroGradient | empty
| fixedValue
| zeroGradient | zeroGradient
U
| fixedValue
| fixedValue
| fixedValue
| empty
| fixedValue
| inletOutlet | fixedValue
epsilon
| inletOutlet | inletOutlet | zeroGradient | empty
| fixedValue
| inletOutlet | zeroGradient
k
| inletOutlet | inletOutlet | zeroGradient | empty
| fixedValue
| inletOutlet | zeroGradient
p
| zeroGradient | zeroGradient | zeroGradient | empty
| zeroGradient | fixedValue
| zeroGradient

7
9
11
13

Other things the tool can report


> pyFoamC aseRepo rt . py -- help

bgschaid

Automatization with pyFoam

27/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Preparing for parallel runs


2 CPUs is all we have
1
3
5
7
9
11
13
15
17

> 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

Automatization with pyFoam

28/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Hey! Ho! Lets go!


At last Ignaz is eager to see some numbers ...
Removing old data and starting the run
1

> pyFoamRunner . py -- progress -- proc =2 -- clear 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


Reading regular expressions from / Volumes / Rest / StickTest / Shared / AdvanzedWork / step1 /<brk>
<cont> testSeparator / customRegexp
Clearing out old timesteps ....
t = 0.0736599

5
...
7
9
11
13
15
17

> less PyFoamRunner . 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


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*\
| =========
|
|
| \\
/ F ield
| OpenFOAM : The Open Source CFD Toolbox
|
| \\
/
O peration
| Version : 1.5 - dev
|
|
\\ /
A nd
| Revision : exported
|
|
\\/
M anipulation | Web :
http :// www . OpenFOAM . org
|
\* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
Exec
: rhoPisoAbscheidParcelFoam
...

bgschaid

Automatization with pyFoam

29/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Regular expressions in 3 minutes


Regular expressions are essential for customRegexp (therefor

the name)
Usually a letter is matched by itself
. matches any letter

Patterns are enclosed by ( and )


Patterns are numbered by the order in which they appear in
the expression
+ means repeat the previous pattern at least once
%f% is PyFoam-shorthand for a regular expression that

matches any floating-point number

bgschaid

Automatization with pyFoam

30/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Parcels in the system


Simple plot
1

Ignaz wants a plot of the parcels in


the system

parcels
{

theTitle " Number of parcels " ;


expr " Current number of <brk>
<cont> parcels
= (% f<brk>
<cont> %) " ;
titles ( nr ) ;

expr The regular


expression that should
be matched

theTitle Title of the plot


name List of annotations

bgschaid

Automatization with pyFoam

31/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Mass leaving the system


List of exits is not previously
known. type dynamic allows to
dynamically build it
idNr The pattern number
of the ID (name)
accumulation sum means that
several occurences of
a pattern in a
time-step will be
added (usually only
the first is used)
with How the data should
be plotted (see with
in GnuPlot)

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
}

Automatization with pyFoam

32/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Using it to plot
Displaying from a logfile
1

> p yF oa mP l ot W atc her . py PyFoamRunner . 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

Saving pictures afterwards


1

> p yF oa mP l ot W atc her . py -- progress PyFoamRunner . 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 --<brk>


<cont> hardcopy

Using matplotlib
1

> p yF oa mP l ot W atc her . py -- progress PyFoamRunner . 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 --<brk>


<cont> hardcopy -- implement ation = matplotlib

Or using stuff stored by the pyFoamPlotRunner


1

> pyFo amRedoPlot . py -- pickle PyFoamRunner . 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 . analyzed /<brk>


<cont> pickledPlots

bgschaid

Automatization with pyFoam

33/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Ignaz prepared a complex visualization


He saved a ParaView-statefile named default.pvsm
1

> p y F o a m P VSnapshot . py -- time =1.5 .

bgschaid

Automatization with pyFoam

34/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Other things to do with state-files


If you havent cloned default.pvsm
1

> p y F o a m P VSnapshot . py -- time =1.5 .


<cont> default . pvsm

-- state =../ cases / twoCavityChannel /<brk>

Plotting more than one time ... for a poster


1

> p y F o a m P VSnapshot . py -- time =2 -- time =1.5 . -- maginfication =3

Starting ParaView with that state


1

> p y F o a m P V Loa dSt ate . py

-- state =../ cases / twoCavityChannel / default . pvsm

bgschaid

Automatization with pyFoam

35/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Preparing the case


Running the simulation
customRegexp
Post processing

Exercise - What did I do?

Have a look at the PyFoamHistory-file in the case


What does it tell you?
Who has the fewest lines?
Who has the most?

bgschaid

Automatization with pyFoam

36/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Preparing for a new take


1

Get the second stage

Getting it with Git


1
3

> cd $HOME / pyFoamAdvanced


> git clone pyFoamAdvanced . git -b step2template step2
> cd step2

A clean copy

Cloning again
1

> p y Fo am CloneCase . py -- add = default . pvsm cases / twoCavityChannel <brk>


<cont> testSeparator
PyFoam WARNING on line 85 of file / Users / bgschaid / private_python / PyFoam /<brk>
<cont> Applications / CloneCase . py : Directory does not exist . Creating
> cd testSeparator

bgschaid

Automatization with pyFoam

37/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Getting to the heart of PyFoam

Now Ignaz wants to prepare his case for automatic setup


Changing the inlet velocity
Changing the geometry
Before testing a script we try it with commands

bgschaid

Automatization with pyFoam

38/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Changing the velocity

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

Automatization with pyFoam

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

file = P a r s e dP a r a m e t e r F il e ( " testDict " )

# none of these should fail


assert file [ " model " ]== " theModel "
assert len ( file [ " patches " ]) ==2
assert file [ " patches " ][0]== " inlet "
assert file [ " theModelCoeffs " ][ " beta "<brk>
<cont> ][1]== " inlet "

7
9
11

model theModel ;
patches ( inlet
outlet ) ;

13
15
17

theMo delCoeffs {
alpha 0.3;
beta 2 inlet ;
}

from PyFoam . RunDictionary .<brk>


<cont> P a r s ed P a r a m e t e r Fi l e import <brk>
<cont> P a r s ed P a r a m e t e r Fi l e

11
13

# manipulation is possible
file [ " theModelCoeffs " ][ " alpha " ]+=1
file [ " newCoeffs " ]={ " a " :1 , " b " :[2 ,3 ,4]}

15

print file

bgschaid

Automatization with pyFoam

40/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Trying half the original velocity


Not the most elegant way to set the velocity
> p y F o a m W r i t e D i c t i o n a r y . py 0/ U " boundaryField [ inlet ][ value ]" " uniform <brk>
<cont> (0.5 0 0) " -- strip - quotes - from - value

Ignaz checks what has changed


1

3
5
7

> p y F o a m C o m p a r e D i c t i o n a r y . py 0/ U ../ cases / twoCavityChannel


PyFoam WARNING on line 90 of file / software / PyFoam / Applications /<brk>
<cont> Co mpa reD icti ona ry . py : Found / data / step2 / cases / twoCavityChannel <brk>
<cont> /0/ U and using this
> ><< Field U [ boundaryField ][ inlet ][ value ] : Differs
> > Source :
uniform (0.5 0 0)
<< Destination :
uniform (1 0 0)

bgschaid

Automatization with pyFoam

41/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

How templates work

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

Everything else is copied to the output

bgschaid

Automatization with pyFoam

42/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Declaring helper variables

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

Automatization with pyFoam

43/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

The actual points

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

Automatization with pyFoam

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

Automatization with pyFoam

45/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Converting the template


The secret is to get a complete Python-dictionary into a
string
> p y F o a m F ro m Te m pl at e . py constant / polyMesh / blockMeshDict "{ preLen :0.2 , <brk>
<cont> cavityWidth :0.15 , channelHeight :0.1 , interLen :0.05 , <brk>
<cont> cavityDepth :0.02 , dx :0.005}"

Produces a constant/polyMesh/blockMeshDict with lines


like
1
3
5
7
9

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

Automatization with pyFoam

46/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Isidor wants to see the results


Running it
2

> 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

Automatization with pyFoam

47/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Cloning again

Get the third stage

Getting it with Git


1
3

> cd $HOME / pyFoamAdvanced


> git clone pyFoamAdvanced . git -b step3shellScript step3
> cd step3

This time no pyFoamCloneCase.py

bgschaid

Automatization with pyFoam

48/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

One script to bind them all

Ignaz knows the commands by heart


.. but doesnt want to execute them every time by hand
So he copies them into a script
.. and adds some shell sugar

bgschaid

Automatization with pyFoam

49/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

The shell script


The file scripts/prepareRun.sh
1

# ! / 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

p yFoamCloneCase . py $TEMPLATE $TARGET


p yF o am Fr o mT em p l a t e . py $TARGET / constant / polyMesh / blockMeshDict $GEOMETRY
p y F o a m W r i t e D i c t i o n a r y . py $TARGET /0/ U " boundaryField [ inlet ][ value ] " " uniform ( $VELOCITY 0 0) "<brk>
<cont> -- strip - quotes - from - value

15
echo " Calling blockMesh and decompose "
17
19

pyFoamRunner . py -- clear -- progress blockMesh - case $TARGET


pyFoamDecompose . py -- progress $TARGET 2

21

p yFoamCaseRep ort . py -- decomposition $TARGET

23

echo " Running "

25

pyFoamRunner . py -- progress -- proc =2

-- foam =1.5 - dev 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 - case $TARGET

bgschaid

Automatization with pyFoam

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

> ./ scripts / prepareRun . sh cases / twoCaviyChannel testSeparator 0.7 0.01


Creating case testSeparator
...

Thats all you hear about shell-scripts here

bgschaid

Automatization with pyFoam

51/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Theres more to git than clone

... but we dont use it


The only git command you see in this presentation
1
3

> cd $HOME / pyFoamAdvanced


> git clone pyFoamAdvanced . git -b s tep4 pyt hon Scr ipt step4
> cd step4

bgschaid

Automatization with pyFoam

52/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Script reloaded

Now Ignaz becomes ambitious


He wants to do it better ... in pure Python
The end result is longer
... but will be much easier to modify

bgschaid

Automatization with pyFoam

53/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Reading the parameters


No PyFoam here - Start of scripts/prepareRun.py
1

# ! / usr / bin / env python

import sys
from os import path

5
7

if len ( sys . argv ) <4:


print " Need < template > < target > < velocity > [ < dx >] "
sys . exit ( -1)

9
11

template = sys . argv [1]


target = sys . argv [2]
velocity = float ( sys . argv [3])

13
15

dx =0.01
if len ( sys . argv ) >4:
dx = float ( sys . argv [4])

sys.argv is the list of command-line parameters


bgschaid

Automatization with pyFoam

54/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Cloning the directory

First time we use the library - scripts/prepareRun.py


from PyFoam . RunDictionary . Sol uti onD irec tor y import So lut ionD ire cto ry
2
4
6

orig = S o l u tio nDir ect ory ( template ,


archive = None ,
paraviewLink = False )
work = orig . cloneCase ( target )

SolutionDirectory represents an OpenFOAMTM -case


Including time-steps etc
cloneCase returns another SolutionDirectory

bgschaid

Automatization with pyFoam

55/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Using the template


The dictionary now looks more natural scripts/prepareRun.py
from PyFoam . Basics . TemplateFile import TemplateFile
2
bmName = path . join ( work . polyMeshDir () ," blockMeshDict " )
4
6
8
10

template = TemplateFile ( bmName + " . template " )


template . writeToFile ( bmName ,{ preLen :0.2 ,
cavityWidth :0.15 ,
channelHeight :0.1 ,
interLen :0.05 ,
cavityDepth :0.02 ,
dx : dx })

os.path is a convenient way to keep pathnames portable


This line should need no change on Windows (never tested it,
though)
bgschaid

Automatization with pyFoam

56/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Modifying the velocity


Dictionary modification is trivial - scripts/prepareRun.py
1

from PyFoam . RunDictionary . P a r s e d P a ra m e t e r F i l e import P a r s e dP a r a m e t e r Fi l e


from PyFoam . Basics . DataStructures import Vector

3
5

velFile = P a rs e d P a r a m e te r F i l e ( path . join ( work . initialDir () ," U " ) )


velFile [ " boundaryField " ][ " inlet " ][ " value " ]. setUniform ( Vector ( velocity ,0 ,0) )
velFile . writeFile ()

ParsedParameterFile is the workhorse of PyFoam


Mastering it is easy:
1 Create it. The file is read
2 Manipulate it like a regular Python-dictionary
3 Write it back

bgschaid

Automatization with pyFoam

57/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Administrative stuff

Make sure we use the right version - scripts/prepareRun.py


2

from PyFoam . FoamInformation import ch ang eFo amVe rsi on


ch a n g e F o a mVe rsi on ( " 1.5 - dev " )

from PyFoam . Error import error

changeFoamVersion makes sure that the right

OpenFOAMTM -version is used


error prints an error message and aborts the program

bgschaid

Automatization with pyFoam

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

from PyFoam . Execution . BasicRunner import BasicRunner


blockRun = BasicRunner ( argv =[ " blockMesh " ,
" - case " , work . name ] ,
silent = True ,
server = False ,
logname = " Blocky " )
print " Running blockMesh "
blockRun . start ()
if not blockRun . runOK () :
error ( " There was a problem with blockMesh " )

BasicRunner runs any OpenFOAMTM -command

argv
silent
server
logname

Command-line passed to OpenFOAMTM


No output to the terminal
No server-process that controls the execution
Name of the logfile
bgschaid

Automatization with pyFoam

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

from PyFoam . Applications . Decomposer import Decomposer


print " Decomposing "
Decomposer ( args =[ " -- progress " ,
work . name ,
2])

6
8

from PyFoam . Applications . CaseReport import CaseReport


CaseReport ( args =[ " -- decomposition " ,
work . name ])

Most utilities are implemented by a class


Found in the PyFoam.Applications-sublibrary
Name is usually the utility-name without pyFoam and .py
The parameter args is the command line we would use on the

shell
The command is executed immediately at construction
bgschaid

Automatization with pyFoam

60/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Setting up for a parallel run

Still using only 2 CPUs - scripts/prepareRun.py


1

from PyFoam . Execution . Par all elE xec uti on import LAMMachine
machine = LAMMachine ( nr =2)

LAMMachine is used to interface with MPI


It is named like that for historic reasons
Also works with machine-files

bgschaid

Automatization with pyFoam

61/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Setting up the run


Ignaz only wants a progress indicator scripts/prepareRun.py
2
4
6

from PyFoam . Execution . AnalyzedRunner import AnalyzedRunner


from PyFoam . LogAnalysis . FoamLogAnalyzer import FoamLogAnalyzer
from PyFoam . LogAnalysis . TimeLineAnalyzer import TimeLineAnalyzer
analyzer = FoamLogAnalyzer ()
analyzer . time =0. # fix around a bug
analyzer . addAnalyzer ( " time " , TimeLineAnalyzer ( progress = True ) )

xxxLineAnalyzers look at one line of output at a time


The TimeLineAnalyzer searches for an expression Time =
x.xx and updates its parents
The parameter progress makes sure that only the time is
printed to the standard-output
FoamLogAnalyzer takes care of one or more LineAnalyzers

and feeds them the log-lines


bgschaid

Automatization with pyFoam

62/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Dictionaries
Templates
An old-school script
A python script

Starting the actual run


Finally the run is started - scripts/prepareRun.py
2
4
6

print " Running "


theRun = AnalyzedRunner ( analyzer ,
argv =[ " 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 " ,
" - case " , work . name ] ,
silent = True ,
lam = machine )
theRun . start ()

AnalyzedRunner is a sub-class of BasicRunner


Any LogAnalyzer can be added and will be handed the
output for analysis
The lam-parameter starts the run in parallel

bgschaid

Automatization with pyFoam

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

> ./ scripts / prepareRun . py cases / twoCavityChannel testSeparator 0.7 0.01


PyFoam WARNING on line 152 of file / software / PyFoam / FoamInformation . py : <brk>
<cont> 1.5 - dev is already being used
Running blockMesh
Decomposing
Case is decomposed for 2 processors
|
0
1
----------------------------Points |
906
918
Faces |
1658
1682
Cells |
402
408
----------------------------abscheider1 |
0
15
abscheider2 |
15
0
bottom |
27
26
defaultFaces |
804
816
inlet |
0
10
outlet |
10
0
top |
37
38
Running
t = 0.02567

bgschaid

Automatization with pyFoam

64/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Last git of the day

... well miss it


Once more with feeling
2

> cd $HOME / pyFoamAdvanced


> git clone pyFoamAdvanced . git -b s t e p 5 p a r a m e t e r V a r i a t i o n step5
> cd step5

bgschaid

Automatization with pyFoam

65/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Aiming for world domination

Now Ignaz wants it all:


Run a parameter variation unattended
Have the data analyzed
And written to a file he can open with a spreadsheet-program
Get pictures of every run

bgschaid

Automatization with pyFoam

66/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

The class that stares at goats

First we write a class that inherits from LogLineAnalyzer


The purpose of this class is to look at every line of the

OpenFOAMTM output and


See what it can learn from it
Store that data
Give it back if asked to

In this concrete case this means: every time a particle leaves

through a patch its mass will be added


to the total mass
to the data entry of the patch

bgschaid

Automatization with pyFoam

67/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Setting up the class


Constructor - scripts/AbscheideAnalyzer.py
1

import re

from PyFoam . LogAnalysis . LogLineAnalyzer import LogLineAnalyzer

class A b s ch eide Ana lyz er ( LogLineAnalyzer ) :


def __init__ ( self ) :
L ogLineAnalyzer . __init__ ( self )

7
9

self . massLeft =0
self . removed ={}

11

13

self . removedExpr = re . compile ( " Abgeschieden at (.+) with m : (.+) N : <brk>


<cont> (.+) = total : (.+) " )
self . massExpr = re . compile ( " Current mass in system
= (.+) " )

Making it a sub-class of LogLineAnalyzer


The module re is Pythons module for parsing regular

expressions
bgschaid

Automatization with pyFoam

68/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Analyzing a line
These two methods are
necessary scripts/AbscheideAnalyzer.py
1

def doAnalysis ( self , line ) :


self . analyzeLine ( line )

3
5
7
9

def analyzeLine ( self , line ) :


m = self . removedExpr . match ( line )
if m != None :
name = m . groups () [0]
mass = float ( m . groups () [1])
nr = float ( m . groups () [2])
total = float ( m . groups () [3])

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

self . removed [ name ]+=<brk>


<cont> total
except KeyError :
self . removed [ name ] =<brk>
<cont> total

makes sure that only field


entries that are needed are
added

m = self . massExpr . match ( line )


if m != None :
self . massLeft = float ( m .<brk>
<cont> groups () [0])

bgschaid

Automatization with pyFoam

69/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Reporting
These two methods are Ignazs
idea scripts/AbscheideAnalyzer.py
1
3

5
7

11

def doReport ( self ) :


summe = self . massLeft
print " Mass left in system : " ,<brk>
<cont> self . massLeft
for k , v in self . removed .<brk>
<cont> iteritems () :
print " Removed by " ,k , " : " ,v
summe += v
print " Total mass accounted <brk>
<cont> for : " , summe
def addCSVLine ( self , csv ) :
csv [ " mass left in system " ]=<brk>
<cont> self . massLeft
for k , v in self . removed .<brk>
<cont> iteritems () :
csv [ k ]= v

doReport Prints the


summarized
information
addCSVLine Add data to a
CSVCollector (a
convenience-class for
generating a
consistent csv-file
addTimeListener Desgin relict.
Forget it

13
15

def a ddTime Listener ( self , other ) :


pass

bgschaid

Automatization with pyFoam

70/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

An analyzer
A class to call the LineAnalyzer scripts/AbscheideAnalyzer.py
1

from PyFoam . LogAnalysis . FoamLogAnalyzer import FoamLogAnalyzer

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

def doReport ( self ) :


self . getAnalyzer ( " abscheid " ) . doReport ()

A wrapper for the AbscheideAnalyzer


Only needed for the testing-code

bgschaid

Automatization with pyFoam

71/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Some code to test the class


Usually not used - scripts/AbscheideAnalyzer.py
1
3
5

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

Using the script


2
4

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

Automatization with pyFoam

72/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Testing the waters


Ignaz creates two simple scripts:
prepareTheCase.py A lobotomized version of the script in the
last chapter:
Creates the case
Does not run the solver
This script has to be run also to prepare a case for
the parameter variation
runSolverAndAnalyze.py Runs the solver and prints the report
from the analyzer
Using the two scripts
2
4

> ./ scripts / prepareTheCase . py cases / twoCavityChannel testCase 0.01


Running blockMesh
> ./ scripts / r u n S ol v e r A n d A n al y z e . py testCase
Mass left in system : 0.01
Total mass accounted for : 0.01
bgschaid

Automatization with pyFoam

73/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

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

PyFoam . Execution . AnalyzedRunner import AnalyzedRunner


PyFoam . Applications . ClearCase import ClearCase
PyFoam . RunDictionary . P a r s e d P a ra m e t e r F i l e import P a r s e dP a r a m e t e r Fi l e
PyFoam . Basics . CSVCollection import CSVCollection
PyFoam . Applications . PVSnapshot import PVSnapshot

13
15
17

from PyFoam . FoamInformation import ch ang eFo amVe rsi on


c h a n g e F o a mVe rsi on ( " 1.5 - dev " )
case = sys . argv [1]
csv = CSVCollection ( sys . argv [2]+ " . csv " )

Nothing new here


bgschaid

Automatization with pyFoam

74/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

The actual variation


Second part and end of scripts/inletVelocityVariation.py
2
4
6
8
10
12

for v in [0.1 , 0.5 , 1 , 1.5 , 2 , 3]:


print " Velocity set to " ,v
ClearCase ( args =[ case ])
uInit = P ar s e d P a r a m et e r F i l e ( path . join ( case , " 0 " ," U " ) )
uInit [ " boundaryField " ][ " inlet " ][ " value " ]. setUniform ([ v ,0 ,0])
uInit . writeFile ()
abscheid = A bs c he i d e An a l yz e r ()
run = AnalyzedRunner ( abscheid ,
argv =[ " 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 " ," - case " , case ] ,
silent = True )
run . start ()

14
16

csv [ " Velocity " ]= v


abscheid . addCSVLine ( csv )
csv . write ()

18
abscheid . doReport ()
20
22
24
26

PVSnapshot ( args =[ case ,


" -- time =1 " ,
" -- time =1.5 " ,
" -- time =2 " ,
" -- time =3 " ,
" -- file - prefix = velocity =% g " % v ])

The only new thing is the loop


bgschaid

Automatization with pyFoam

75/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Running the variation

One command. 6 runs. One result file. 24 Pictures


2

> ./ scripts / prepareTheCase . py cases / twoCavityChannel testVariation 0.005


Running blockMesh
> ./ scripts / i n l e t V e l o c i t y V a r i a t i o n . py testVariation variationData

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

each iteration of the loop will produce multiple copies of the


same pictures

bgschaid

Automatization with pyFoam

76/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

The end result


The data from the CSV-file in one picture:

Most variations were inefficient (no particles separated)


Too slow is bad, too
For low velocities the simulation time is too short (still mass

in system)
bgschaid

Automatization with pyFoam

77/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Suggested exercises
Try to find the optimum velocity (where the most mass comes

to abscheider2)
Obviously extend the simulation time. But not too long.

Suggestion: set it dependent on the inlet-velocity


For the optimal velocity try varying the direction of the

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

Automatization with pyFoam

78/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Acknowledgments

Thanks to
Holger Marschall for the idea for this tutorial
Astrid Mahrla for some pictures

bgschaid

Automatization with pyFoam

79/80

Introduction
The problem
Manual case setup
Case parametrisation
Parameter variation

Looking at one line after another


Simple scripts using it
Script with variations
Using it and results

Suggestions/Questions

Thanks for listening


Questions?
Suggestions for the library?

Bug reports at

http://sourceforge.net/apps/mantisbt/openfoam-extend/view_all_bug_page.php

bgschaid

Automatization with pyFoam

80/80

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy