Sig Numerical Optimization / Scripting and automatic post-processing

From OpenFOAMWiki

After running a bunch of simulations I got bored with stupid errors like forgetting to change patch type in boundery file, waiting decomposePar or reconstructPar, running few hundred simulations and then changing fvSchemes to second order discretizations... In that manner I created few bash scripts to ease the pain and hopefully they will help someone else.

1 OpenFOAM's little helpers

It is easily possible to couple this scripts with Salome and cfMesh and create complete mesh generation and simulation automatically. To do that follow steps on page: Salome and cfMesh parametrization

1.1 prepBoundary

First helper is prepBoundary. This bash script checks your patches names and according to it changes their type. If your patches name has specific pattern, script will recognize and change its type.
Example: I have a wall patch whose type should be wall, so when I create mesh I will name that patch bottomWall or bottomWALL; or
if I have an inletOutlet patch that should be type patch, when I create mesh I will name that patch upperInletOutlet or upperINLETOutlet

This script reads patches names so their names should have specific string inside them. You can easily change what will these strings be by adding/removing words from part:
# setting familiar words in arrays.
In these script those key words are:

  • For type wall
  • wall, Wall and WALL
  • For type patch
  • inlet, Inlet, INLET, outlet, Outlet and OUTLET
  • For type slip
  • slip, Slip and SLIP
  • For type empty
  • empty, Empty, EMPTY and frontAndBackPlanes
  • For type symmetry
  • symmetry, Symmetry and SYMMETRY
  • For type wedge
  • wedge, Wedge and WEDGE
  • For type cyclic
  • cyclic, Cyclic and CYCLIC

Script needs two things to work:

  • Needs header in boundery file because it looks for word FoamFile to navigate trough it
  • Needs ONE LINE between patch name and type (in standard Foam files in that line bracket { is located):
       type            patch;

Here goes the script: File:PrepBoundary.gz

Start the script with command: . [PATH TO SCRIPT]/prepBoundary [PATH TO BASE CASE DIRECTORY]

1.2 foamParallelRunner

Second helper is foamParallelRunner. This bash script runes your simulations in parallel way.
Start script with command: . [PATH TO SCRIPT]/foamParallelRunner [PATH TO BASE CASE DIRECTORY] [PATH TO ANOTHER BASE CASE DIRECTORY] ....
At the end script compresses all folders and uploads it to Dropbox. To use those features (those are important because you might end up compressing all hard drive!!):


If not using those features remove that parts from bash and it doesn't matter where you keep your script and case directories

Script loops trough specified base simulation directories (where directories 0, constant and system are located) and does the following:

  • Enters case base directory
  • Does decomposePar if it already haven't been done
  • Gets the number of processors from decomposeParDict
  • Gets solver name from controlDict
  • Runs first simulation in parallel way
  • Checks if there are setup files for second run of same simulation, and if there are:
  • Swaps controlDict and controlDict2Order; fvSchemes and fvSchemes2Order
  • Get new solver name from new controlDict
  • Runs second simulation in parallel way
  • Swaps back controlDict and fvSchemes
  • Does reconstructPar
  • Removes processors directories
  • Calculates y+

At the end of loop script:

  • Compresses everything in base directory (beware of given instructions for this one)
  • Uploads to Dropbox

To use the simulation type switch you need to specify controlDict2Order and fvSchemes2Order or only one of them. Just create controlDict for second simulation, put it in system directory and name it controlDict2Order (same thing for fvSchemes).

To use Drobox feature look at Dropbox Uploader by Andrea Fabrizi

Script logs everything, checks and reports for errors. Here goes the script: File:FoamParallelRunner.gz

EDIT: Don't know why, but on some computers there is a problem with awk command in line 27:

       procNum=$(awk -v RS=[0-9]+ '{print RT+0;exit}' <<< "$string")

If it is not working please change that line to cut or sed provided below (both of them should work):

   procNum=$(echo "$string" | cut -d ' ' -f2 | rev | cut -c 2- | rev)
   procNum=$(echo "$string" | sed 's/[^0-9]//g')