RegEx (= Regular Expression) can be quite handy while working with OpenFOAM files. Here are some useful examples.
Update automatically the number of processors in decomposePar from your Allrun script
Suppose you have the following shell script :
#!/bin/sh
cd ${0%/*} || exit 1 # run from this directory
# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions
# set here the number of cores to be used in parallel
totNumberOfCores=46
restore0Dir
decomposePar 2>&1 | tee log.decomposePar
mpirun -np $totNumberOfCores overInterDyMFoam -parallel 2>&1 | tee log.overInterDyMFoam
If you want to automatically update the number of cores inside decomposeParDict so to match “totNumberOfCores=46”, then it is sufficient to add just before the decomposePar command the following expression :
sed -i "/numberOfSubdomains/ s/numberOfSubdomains.*/numberOfSubdomains $totNumberOfCores;/" system/decomposeParDict
In a even more general way, in case you don't know a priori the location where the script will be launched and you need to find the location of the decomposeParDict you can also use:
find . -name "decomposeParDict" -print | xargs sed -i "/numberOfSubdomains/ s/numberOfSubdomains.*/numberOfSubdomains $totNumberOfCores;/"
Update patch type without remeshing.
Suppose you launched blockMesh and snappyHexMesh and realized that you would like to set one of your inlet as a wall. In that case you would need to change all the inlet’s “type patch” into “type wall”. You can do that by automatically launching:
find /directory/path -type f -exec sed -i 's/type patch;/type wall;/g' {} \;
Substitute name in all the files inside a directory
In Unix to find and replace a string in all the files recursively we can use:
find . -type f|xargs perl -pi -e 's/source/target/g'
Suppose you have a chtMultiRegionFoam case: inside there is a region “heater” that you want to rename as “cooler”. Then we can use :
find . -type f|xargs perl -pi -e 's/heater/cooler/g'
Regex to make settings files more compact
Suppose you have a U file with the following boundary conditions:
leftWall
{
type fixedValue;
value uniform (0 0 0);
}
rightWall
{
type fixedValue;
value uniform (0 0 0);
}
topWall
{
type fixedValue;
value uniform (0 0 0);
}
Instead of creating a subdictionary for each patch we can compact them into one subdictionary by using the following regular expression:
(left|right|top)Wall
{
type fixedValue;
value uniform (0 0 0);
}
Or in a even more concise way:
.*Wall
{
type fixedValue;
value uniform (0 0 0);
}
Delete a selected range of time directories from a transient analysis
Suppose that you have launched in parallel a transient simulation giving the following serie of time directories:
0.073 0.0735 0.074 0.0745 0.075 0.0755 0.076 0.0765 0.077 0.0775 0.078 0.0785 0.079 0.0795 0.08 0.0805
But they are occupying too much space on disk, so we would like to delete all time directories “*.***5” so to leave only those every millisecond. Then we can launch in terminal :
find . -type d | grep -P "0\....5$" | xargs -d"\n" rm -rf {}
And we will be left only with the millisecond directories:
0.073 0.074 0.075 0.076 0.077 0.078 0.079 0.08 0.081 0.082 0.083
Let’s now consider the following hypothetical situation: we launched a transient analysis in parallel but it is diverging. And instead of restarting from zero, we just want to delete the latest time directories while keeping those where the Courant number was still < 1. Let’s suppose that we have
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19
and that from 0.11 the analysis starts to diverge. So how to cancel all the directories from 0.11 in each of the processor* directories? We can use the following
find . -type d | grep -P "0.1[1-9]*" | xargs -d"\n" rm -rf {}
And in each of the processor* directories only the following time will be present:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10
シンクタンクにてリスクマネージメントにCAEを活用,その後,外資系CAEベンダーでサポートエンジニア,技術営業を得て,ESIに入社.OpenFOAMを軸としたCFD関連のエンジニアリングサービスを担当