CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Scripts to manage OpenFOAM cases (https://www.cfd-online.com/Forums/openfoam-solving/124249-scripts-manage-openfoam-cases.html)

kiddmax September 30, 2013 14:27

Scripts to manage OpenFOAM cases
 
Dear foamers,


I am wondering how to write a script to manage OpenFOAM cases. Since I need to simulate 2D airfoil flow at different angle of attacks. Everytime I have to copy the case files and change the velocity and lift and drag direction. This method is time consuming and use a lot of disk. I know that using a script to mange the case and change the boundary condition is possible, anyone can give me some hints?

Best regards,
Ye

simon-2 September 30, 2013 20:24

you can use a custom dict, containing AoA in your case.

in your files 0/U etc include your dict und use variables depending on the entries in your dict.

for example
Ux #calc "$myVelocity*cos($myAoA)";
...
fixedValue ($Ux $Uy $Uz)


referring to myVelocity and myAoA in your dict.

this way, you only have to change the myAoA entry in a single dict to modify the case.

Further, you can also use a shellscript to do the following steps for a whole list of prepared dict-files:
copy dict_#i into the case,
./Allrun case,
make directory anywhere/#i
copy relevant output to anywhere/#i,
./Allclean case

there might be more elegant ways, but it should work ;)

Logan Page October 1, 2013 09:08

I had a similar requirement, for which I created Python scripts (using some of pyFoam's functionality) to manage my case files and the automation of multiple CFD simulations.

kiddmax October 3, 2013 16:50

Thank you Logan!

Cause I am quite new to OpenFOAM. It will be better if you can describe it more detail.

Best regards,
Ye

Logan Page October 4, 2013 05:50

4 Attachment(s)
Hi

I effectively set up code as described in the attached images.
Attachment 25806 Attachment 25807

The OpenFoam utilities and solvers where called (run) through the use of pyFoam. http://openfoamwiki.net/index.php/Contrib_PyFoam

In my case the mesh file was also scripted / generated in Python and then used in blockMesh, but you can easily use another meshing program like gmsh coupled to Python.

The general process I followed is:
  1. setup a base case directory and files (e.g. AOA = 0), that has the 0, constant and system files needed for your simulations
  2. create a new directory for a new sim (e.g. AOA = 3) [I used Python to manage my directories based on the value for a given variable, e.g. `os.makedirs(some_path/AOA_03)`]
  3. I used pyFoam to clone the base case files into the new directory `pyFoam.SolutionDirectory.cloneCase`
  4. create the mesh / meshFile -- there are multiple options here depending on what meshing program you are using, for example:
    • gmsh
      • create an initial parameterized .geo file
      • there are probably ways to improve this, but I then copied the .geo file text to Python and used Python to change the parameter values and write a new .geo file to the new directory
      • I can't remember if I used `pyFoam.UtilityRunner` or a Python subprocess to then run gmsh with new the .geo file
      • you can probably also play around with the `-merge` option when calling gmsh and only use Python to write a file with the parameters in it (haven't tried this)
      • then I used `pyFoam.UtilityRunner` to run `gmshToFoam'
      • It's been awhile since I've done this, but if I recall correctly there may have been issues with boundary types after `gmshToFoam` and you may need to also update the types in the constant/polyMesh/boundary
    • blockMeshDict with m4
    • create the blockMeshDict file in Python
      • this requires the most work and I definitely don't have an optimal process / solution for this, but it is doable.
  5. I then used `pyFoam.UtilityRunner` to run`blockMesh`
  6. if I had any boundary conditions or fluid properties that needs changing, I used `pyFoam.ParsedParameterFile` to edit / change values in the specific files
  7. then finally I used one the the `pyFoam` runners to call the OpenFoam solver, e.g. `pyFoam.PlotRunner` / `pyFoam.SteadyRunner`
  8. I then used Python to extract the results I wanted from the simulation and pop the results for each simulation into a central log file.
Here are the two Python files I used that may help you on your way.
Attachment 25808
Attachment 25809

olivierG October 4, 2013 05:54

hello,

Like Logan say, take a look at pyFoam
http://openfoamwiki.net/index.php/Contrib_PyFoam
There is a full section about manipulating case.

You can also change boundary with the changeDictionary commande, usin a "changeDictionnaryDict" file.

regards,
olivier

liwenpeng July 17, 2020 05:09

Quote:

Originally Posted by olivierG (Post 455010)
hello,

Like Logan say, take a look at pyFoam
http://openfoamwiki.net/index.php/Contrib_PyFoam
There is a full section about manipulating case.

You can also change boundary with the changeDictionary commande, usin a "changeDictionnaryDict" file.

regards,
olivier

Do I need to install Python on Linux

gschaider July 17, 2020 05:14

Quote:

Originally Posted by liwenpeng (Post 778018)
Do I need to install Python on Linux


It's hard to find a Linux distribution that hasn't got Python installed


But if you want to use PyFoam: yes. Python is necessary

liwenpeng July 17, 2020 05:18

Quote:

Originally Posted by gschaider (Post 778019)
It's hard to find a Linux distribution that hasn't got Python installed


But if you want to use PyFoam: yes. Python is necessary

My computer is a Windows system. In order to run openfoam, I have installed Ubuntu Linux. If I want to use pyform, what should I do?. Do I install Python on windows or Ubuntu Linux。
thanks

bennn July 17, 2020 10:38

If you want your life to be easy and enjoyable, I'd suggest sticking to a linux distribution and never use Windows for Openfoam simulations.

liwenpeng July 17, 2020 22:41

Quote:

Originally Posted by bennn (Post 778060)
If you want your life to be easy and enjoyable, I'd suggest sticking to a linux distribution and never use Windows for Openfoam simulations.

thanks for your advice


All times are GMT -4. The time now is 02:16.