CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Community Contributions (https://www.cfd-online.com/Forums/openfoam-community-contributions/)
-   -   [PyFoam] run pyFoam utilities from python script (https://www.cfd-online.com/Forums/openfoam-community-contributions/131121-run-pyfoam-utilities-python-script.html)

ilpaso March 9, 2014 18:41

run pyFoam utilities from python script
 
Hi,

sorry for this stupid question.
The pyFoam utilities are very useful! How can I run these utilities from a python script.
Example:
How to run a solver on a case as with pyFoamPlotRunner.py?

Regards

gschaider March 10, 2014 14:23

Quote:

Originally Posted by ilpaso (Post 479041)
Hi,

sorry for this stupid question.
The pyFoam utilities are very useful! How can I run these utilities from a python script.
Example:
How to run a solver on a case as with pyFoamPlotRunner.py?

Regards

As described in http://openfoamwiki.net/images/d/de/...WithPyFoam.pdf (Slides 43/44) ALL utilities (except pyFoamVersion.py) are implemented as classes (check the scripts). Parameter for them is a list named args with the command-line parameters

ilpaso March 10, 2014 15:06

Hi gschaider,
thank you for your kind reply.
I've read all your presentations. I'm climbing the learning curve ;) .
I'll try what is written in the slides 43/44. Thanks for the help!

bye

ilpaso March 10, 2014 15:41

In the wiki I can find:
pyFoamPlotRunner.py: Runs an OpenFoam solver. Needs the usual 3 arguments (<solver> <directory> <case>)

If I run (from another directory "/anotherDir", not /mydir/)
pyFoamPlotRunner.py icoFoam /mydir mycase
it returns an error: "Case /anotherDir does not have a 'system' directory' in Application-class: PlotRunner Case anotherDir does not have a 'system' directory' in Application-class: PlotRunner"

This is the first step to solve in order to use the utilities as classes.

thank you

gschaider March 10, 2014 17:26

Quote:

Originally Posted by ilpaso (Post 479211)
In the wiki I can find:
pyFoamPlotRunner.py: Runs an OpenFoam solver. Needs the usual 3 arguments (<solver> <directory> <case>)

That is the old (pre-1.5) calling convention. To select a case-directory nowadays the -case-option is used

ilpaso March 11, 2014 07:53

If I write the case variable directly in the script it returs the error:
"... does not have a 'system' directory' in Application-class: PlotRunner Case anotherDir does not have a 'system' directory'..."

How have I to modify the code in order to run the script in a different path and not in the case path?

Code:

import sys
case = "/mydir/caseName"
proc = 1

from PyFoam . Applications . Decomposer import Decomposer
from PyFoam . Applications . Runner import Runner
from PyFoam . Applications . PlotRunner import PlotRunner

Runner ( args =["--clear","blockMesh","-case", case ])
Runner ( args =["setFields","-case", case ])
Decomposer ( args =[ case , str ( proc ) ])
PlotRunner ( args =["--proc=%d"% proc ,"--with-all","--progress","icoFoam","-case", case ])
Runner ( args =["reconstructPar","-case", case ])

thank you

ilpaso March 11, 2014 16:32

Hi again,
there was a space between the "-" and the "case" so it didn't work.
Now it works!

Thank you.

gschaider March 11, 2014 16:44

Quote:

Originally Posted by ilpaso (Post 479343)
If I write the case variable directly in the script it returs the error:
"... does not have a 'system' directory' in Application-class: PlotRunner Case anotherDir does not have a 'system' directory'..."

How have I to modify the code in order to run the script in a different path and not in the case path?

Code:

import sys
case = "/mydir/caseName"
proc = 1

from PyFoam . Applications . Decomposer import Decomposer
from PyFoam . Applications . Runner import Runner
from PyFoam . Applications . PlotRunner import PlotRunner

Runner ( args =["--clear","blockMesh","-case", case ])
Runner ( args =["setFields","-case", case ])
Decomposer ( args =[ case , str ( proc ) ])
PlotRunner ( args =["--proc=%d"% proc ,"--with-all","--progress","icoFoam","-case", case ])
Runner ( args =["reconstructPar","-case", case ])

thank you

Stack trace would be helpful.

BTW: which version of PyFoam do you use? I can't reproduce your problem (but I've got others. For instance does the decomposer complain that 1 CPU is very little)

hossein93 January 26, 2022 12:50

airfoil optimization with openfoam and python
 
Hey guys,

I want to do a shape optimization for an airfoil in python and want to have open foam as my fluid solver. My question is how to link these two to get the fluid flow data in open foam as a result of shape optimization?

dlahaye January 27, 2022 03:04

A naive approach consists of the following three steps:

1/ define an Allrun bash shell script that define geometry, generate mesh, run simulation and writes quantities (pressure distribution) of interest to file;

2/ define python function that runs the Allrun script (via shell commands), reads quantities on interest to file (possibly using dataframes) and compute the quality (cost functional) of the current design (shape of airfoil);c

3/ pass above python function as argument to function that does optimization (genetic algorithm or Cobyla algorithm);

A less naive (and possibly smarter approach) is to use some form of a mesh morphing technique to avoid remeshing at each iteration.


All times are GMT -4. The time now is 09:50.