CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Community Contributions

[PyFoam] run pyFoam utilities from python script

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 9, 2014, 18:41
Default run pyFoam utilities from python script
  #1
Member
 
Davide Pasini
Join Date: Mar 2009
Posts: 34
Rep Power: 17
ilpaso is on a distinguished road
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
ilpaso is offline   Reply With Quote

Old   March 10, 2014, 14:23
Default
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by ilpaso View Post
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   March 10, 2014, 15:06
Default
  #3
Member
 
Davide Pasini
Join Date: Mar 2009
Posts: 34
Rep Power: 17
ilpaso is on a distinguished road
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 is offline   Reply With Quote

Old   March 10, 2014, 15:41
Default
  #4
Member
 
Davide Pasini
Join Date: Mar 2009
Posts: 34
Rep Power: 17
ilpaso is on a distinguished road
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
ilpaso is offline   Reply With Quote

Old   March 10, 2014, 17:26
Default
  #5
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by ilpaso View Post
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   March 11, 2014, 07:53
Default
  #6
Member
 
Davide Pasini
Join Date: Mar 2009
Posts: 34
Rep Power: 17
ilpaso is on a distinguished road
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 is offline   Reply With Quote

Old   March 11, 2014, 16:32
Default
  #7
Member
 
Davide Pasini
Join Date: Mar 2009
Posts: 34
Rep Power: 17
ilpaso is on a distinguished road
Hi again,
there was a space between the "-" and the "case" so it didn't work.
Now it works!

Thank you.
ilpaso is offline   Reply With Quote

Old   March 11, 2014, 16:44
Default
  #8
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by ilpaso View Post
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)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   January 26, 2022, 12:50
Default airfoil optimization with openfoam and python
  #9
New Member
 
hossein
Join Date: May 2016
Posts: 7
Rep Power: 9
hossein93 is on a distinguished road
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?
hossein93 is offline   Reply With Quote

Old   January 27, 2022, 03:04
Default
  #10
Senior Member
 
Domenico Lahaye
Join Date: Dec 2013
Posts: 722
Blog Entries: 1
Rep Power: 17
dlahaye is on a distinguished road
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.
dlahaye is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANSYS Meshing] how can I run .txt script file in ANSYS Meshing oozcan ANSYS Meshing & Geometry 7 December 8, 2016 11:13
Slicing extracted part of a volume in Python script wilmbas OpenFOAM Post-Processing 4 April 28, 2016 17:14
[General] How can I use python script to generate animation from 2000 frames of vtk datasets LieyuShi ParaView 3 March 10, 2016 13:37
Working directory via command line Luiz CFX 4 March 6, 2011 20:02
batch file script to run unsteady flow (les) andi FLUENT 0 April 20, 2004 03:50


All times are GMT -4. The time now is 03:01.