I have been doing a parametric study and it was getting heavier on me to manually load a ParaView state file every time I change the working directory. Later I found out that I could change the folder path in the state file and then load it from the terminal. So, I semi-automated the process. However, this still requires going manually to a state file and changing the folder path to the current working directory. This also started to bother me

so I found a way to fully automate the process but I am not sure whether this is the most efficient way to do it. So, I will explain below how I did it step by step which might be useful for those who are looking for something similar and I would appreciate it if anyone could tell me an easier way to do it. I also wanted to share the bash script that I am using to automatically run my simulations in parallel. So, the script does everything from pre-processing to post-process.
Task: Automatically run a simulation and display the results in ParaView using an existing state file.
- Save a state file (*.pvsm) after displaying all the results you want. (Make sure to give it a unique name)
- Go to the (*.pvsm) file and search for ".foam" ( or however you loaded the time directories, perhaps .OpenFOAM)
- Find the line that contains your folder path and change it to a unique string as shown in the box below.
Code:
<Element index="0" value="YOUR_FOLDER_PATH/YOUR_FILE_NAME.foam"/>
<Element index="0" value="myUniqueString/myUniqueFileName.foam"/>
- Save the new state file with a unique name this will be your template.
- Use the 'sed' command to go into the saved state file replace the folder path and save it as a new file using the current directory name. (Notice that we are using '?' instead '/' and the reason is explained here)
Code:
sed 's?myUniqueString?'`pwd`'?' EARLIER_SAVED_STATE_FILE_NAME.pvsm > ${PWD##*/}.pvsm
- Finally, run the command below to display your results in ParaView
Code:
paraview --state=${PWD##*/}.pvsm
Now the explanation is out of the way, here is my bash script to run a case in parallel using multiphaseEulerFoam. It can be easily tweaked to adjust to your case. Please note that the script assumes you have saved the '0' time directory as '0.orig' which is a good practice in my opinion.
Save the code below as whatever name you want (e.g. cRun) and run it as '/cRun ' from the command line. It is also a good practice to separate the bash file into two files in case you want to only clean or only run sometimes.
Code:
# CLEANING PART
# clean the previous simulation data
. $WM_PROJECT_DIR/bin/tools/CleanFunctions
# omit this if you don't use '0.orig'
rm -rf ./0
cleanCase
# the run will be saved to 'log.parallelRun' file with the current directory
# name at the first line
echo "Case ${PWD##*/}" >> log.parallelRun
clear
# RUNNING PART
# generate mesh
# you can just use 'blockMesh' if you use built-in mesh generation utility
ideasUnvToFoam yoshidaMeshFiner.unv | tee log.mesh
# optional 'changeDictionary' unless you have to change names in the boundaries
changeDictionary | tee -a log.mesh
checkMesh | tee -a log.mesh
# copy '0.orig' folder to '0' folder
cp -r ./0.orig ./0
# optional to set the initial conditions properly
setFields | tee log.preRun
# decompose the mesh for parallel run
decomposePar | tee -a log.preRun
# run the simulation in parallel with the desired number of cores
mpirun -np 16 multiphaseEulerFoam -parallel | tee -a log.parallelRun
# reconstruct the time driectories
reconstructPar
# create dummy.foam
touch myUniqueFileName.foam
# change the folder path in the file and save it as foldername.pvsm
sed 's?myUniqueString?'`pwd`'?' EARLIER_SAVED_STATE_FILE_NAME.pvsm > ${PWD##*/}.pvsm
# open in paraview
paraview --state=${PWD##*/}.pvsm
Let me know if it works for you.
Happy new year to you all.