More solver, utility and library tutorials – how to learn yourself

More solver, utility and library tutorials
– how to learn yourself
• We will start by searching the source code for information on how to use solvers,
utilities and libraries.
• Then we will learn how to use a small number of useful utilities and libraries.
Some of them are described in the UserGuide and ProgrammersGuide, some are
described in the OpenFOAM Wiki (e.g. Turbomachinery Working Group) and
some of them have been discussed in the Forum.
• In your home assignment you will be asked to go through some of the written
tutorials in the UserGuide and ProgrammersGuide, where you will find some
written tutorials. It is HIGHLY recommended that you dig through ALL of the
UserGuide and ProgrammersGuide (before complaining that there is not enough
OpenFOAM documentation).
• If you find a utility, solver or library that is not described anywhere (or insufficiently described), you can include a description of it in your project tutorial. I
would prefer if you then use my LATEX slide template, so that I can easily use it in
coming courses (then please proved the raw tex-file and accompanying graphical
files).
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
68
How to search for solver tutorials in the source code
• Type tut to go to the $FOAM_TUTORIALS directory. Here you find many
case-setups for the solvers in OpenFOAM. Unfortunately, they are not
well-described. Describing these tutorials in words and figures may be
part of your project.
• Type:
tree -d -L 2 $FOAM_TUTORIALS
to get a list of which solvers there are tutorial cases available.
• Type:
tree -d -L 1 $FOAM_TUTORIALS/incompressible/icoFoam
to get a list of which tutorial cases are available for the icoFoam solver.
• All the solver tutorials have Allrun scripts that describe the use of those
tutorials. We will now have a look at the Allrun script of the
$FOAM_TUTORIALS/incompressible/icoFoam tutorials. This is actually what you will do manually when you do the cavity tutorials in the
UserGuide. In other words, you can use the Allrun script as a short
summary of the description in the UserGuide.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
69
Run the icoFoam cavity tutorials using the Allrun script (1/7)
(Note that the following description shows the principle. There might be small differences in
exactly what is done by the Allrun script between versions.)
In the icoFoam tutorial directory there is an Allrun script.
When running this script it is preferred to copy the entire directory to your run directory. Type:
cp -r $FOAM_TUTORIALS/incompressible/icoFoam $FOAM_RUN
cd $FOAM_RUN/icoFoam
./Allrun >& log_Allrun&
Looking in the Allrun script, you can see a list of cases that will be executed:
cavityCases="cavity cavityFine cavityGrade cavityHighRe cavityClipped"
Some of those cases are actually created by the script.
At the end of the script it also runs the elbow case.
The script contains Linux commands and calls for OpenFOAM applications in order to set up
and run the simulations.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
70
Run the icoFoam cavity tutorials using the Allrun script (2/7)
The Allrun script for the icoFoam cavity tutorials actually
first runs the cavity case
#Running blockMesh on cavity:
blockMesh
#Running icoFoam on cavity:
icoFoam
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
71
Run the icoFoam cavity tutorials using the Allrun script (3/7)
The Allrun script for the icoFoam cavity tutorials actually
then runs the cavityFine case:
#Cloning cavityFine case from cavity:
mkdir cavityFine
cp -r cavity/{0,system,constant} cavityFine
[change "20 20 1" in blockMeshDict to "41 41 1"]
[set startTime in controlDict to 0.5]
[set endTime in controlDict to 0.7]
[set deltaT in controlDict to 0.0025]
[set writeControl in controlDict to runTime]
[set writeInterval in controlDict to 0.1]
#Running blockMesh on cavityFine
blockMesh
#Running mapFields from cavity to cavityFine
mapFields -case cavity -sourceTime latestTime -consistent
#Running icoFoam on cavityFine
icoFoam
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
72
Run the icoFoam cavity tutorials using the Allrun script (4/7)
The Allrun script for the icoFoam cavity tutorials actually
then runs the cavityGrade case:
#Running blockMesh on cavityGrade
blockMesh
#Running mapFields from cavityFine to cavityGrade
mapFields -case cavityFine -sourceTime latestTime -consistent
#Running icoFoam on cavityGrade
icoFoam
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
73
Run the icoFoam cavity tutorials using the Allrun script (5/7)
The Allrun script for the icoFoam cavity tutorials actually
then runs the cavityHighRe case:
#Cloning cavityHighRe case from cavity
mkdir cavityHighRe
cp -r cavity/{0,system,constant} cavityHighRe
#Setting cavityHighRe to generate a secondary vortex
[set startFrom in controlDict to latestTime;]
[set endTime in controlDict to 2.0;]
[change 0.01 in transportProperties to 0.001]
#Copying cavity/0* directory to cavityHighRe
cp -r cavity/0* cavityHighRe
#Running blockMesh on cavityHighRe
blockMesh
#Running icoFoam on cavityHighRe
icoFoam
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
74
Run the icoFoam cavity tutorials using the Allrun script (6/7)
The Allrun script for the icoFoam cavity tutorials actually
then runs the cavityClipped case:
#Running blockMesh on cavityClipped
blockMesh
#Running mapFields from cavity to cavityClipped
cp -r cavityClipped/0 cavityClipped/0.5
mapFields -case cavity -sourceTime latestTime
[Reset the boundary condition for fixedWalls to:]
[
type
fixedValue;
]
[
value
uniform (0 0 0);
]
[
We do this since the fixedWalls got
]
[
interpolated values by cutting the domain
]
#Running icoFoam on cavityClipped
icoFoam
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
75
Run the icoFoam cavity tutorials using the Allrun script (7/7)
The Allrun script for the icoFoam cavity tutorials actually
finally runs the elbow case
Now, open each case with paraFoam and have a look.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
76
Run ALL the tutorials using the Allrun scripts
We will not do this now!
• You can also run another Allrun script, located in the $FOAM_TUTORIALS directory. This script will run through ALL the tutorials (calls Allrun in each solver
directory).
• You can use this script as a tutorial of how to generate the meshes, how to run
the solvers, how to clone cases, how to map the results between different cases
etc.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
77
Finding tutorials for the utilities in OpenFOAM
• There are no ’case’ tutorials for the utilities, but we can search for examples:
find $WM_PROJECT_DIR -name \*Dict | grep -v blockMeshDict | grep -v controlDict
You will get a list of example dictionaries for the utilities that use a dictionary. Some of
those examples can be found next to the source code of each particular utility, and some
are also used in the solver tutorials. The ones that don’t use a dictionary are usually
easier to learn how to use, in particular when using the -help flag.
Now you should be ready to go on exploring the applications by yourself.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
78
More tutorials can be found in
• The UserGuide
• The Programmer’s guide, chapter 3
• The OpenFOAM Wiki
(in particular the Turbomachinery Working Group)
• The OpenFOAM Forum
We will now have a look at some utilities and libraries. Please feel free to improve/expand
these descriptions as part of your assignment (e.g. 2.0.x, figures etc.)! Use previously mentioned
dictionary hints to find alternatives for entries.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
79
The mapFields utility
• The mapFields utility maps the results of one case to another case. You will use this
utility when you do the cavityClipped tutorial in the UserGuide (Allrun:
mapFields $1 -case $2 -sourceTime latestTime > $2/log.mapFields 2>&1)
Try: mapFields cavity -case cavityClipped -sourceTime latestTime
• Usage (type mapFields -help, version dependent):
mapFields <source dir> [-parallelTarget] [-consistent]
[-sourceTime scalar] [-parallelSource] [-case dir]
[-help] [-doc] [-srcDoc]
• The time used for the mapping is specified by startFrom/startTime in the target case.
The flag -sourceTime can specify another time directory in the source case.
• The flag -consistent is used if the geometry and boundary conditions are identical in
both cases. This is useful when modifying the mesh density of a case. For non-consistent
cases a mapFieldsDict dictionary must be edited, see the icoFoam/cavityClipped
tutorial.
• The flags -parallelSource and -parallelTarget are used if any, or both, of the
cases are decomposed for parallel simulations.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
80
The sample utility
• The sample utility is used to produce graphs for publication or to extract surfaces. You
will use this utility when you do the tutorials of the UserGuide, and when you do the
ercoftacConicalDiffuser case-study.
• Usage (sample -help, version dependent):
sample [-latestTime] [-time ranges] [-parallel] [-constant]
[-noZero] [-case dir] [-region name] [-help] [-doc] [-srcDoc]
• Copy and modify sampleDict from the plateHole tutorial:
cd $FOAM_RUN/icoFoam
cp $FOAM_TUTORIALS/stressAnalysis/solidDisplacementFoam/plateHole/system/sampleDict cavity/system
sed -i s/"leftPatch"/"horizontalLine"/g cavity/system/sampleDict
sed -i s/"0 0.5 0.25"/"0.001 0.05 0.005"/g cavity/system/sampleDict
sed -i s/"0 2 0.25"/"0.099 0.05 0.005"/g cavity/system/sampleDict
sed -i s/"axis
y"/"axis
distance"/g cavity/system/sampleDict
sed -i s/"sigmaxx"/"p"/g cavity/system/sampleDict
Running sample -case cavity, the pressure, p, is extracted along a horizontal line
at 100 points, and the results are written in cavity/sets.
• Plot in gnuplot:
plot "cavity/sets/0.5/horizontalLine_p.xy"
• sample can also sample surfaces...
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
81
The sample utility - surfaces
• Additions for extracting surface cuts (an additional example is commented):
surfaceFormat
vtk;
surfaces
(
outputName
{
//type
patch;
//patchName
movingWall;
//triangulate false;
type plane;
basePoint (0.05 0.05 0.005);
normalVector (0 0 1);
}
);
• Run with sample -case cavity
• The result is written in cavity/surfaces
• Visualize the surfaces in paraview (File / Open and find a vtk file in the
surfaces directory).
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
82
The sample utility - interpolationScheme
• Use dummy entries to see the alternatives for interpolationScheme
(interpolationScheme dummy;):
cell
cellPoint
cellPointFace
cellPointWallModified
(version dependent)
• See the source code for exact definitions (and help me expand this information):
$FOAM_UTILITIES/postProcessing/sampling/sample/sample.C
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
83
The sample utility - formats
• Use dummy entries to see the alternatives for formats (version dependent):
• setFormat:
xmgr
jplot
gnuplot
raw
• surfaceFormat:
foamFile
null
raw
vtk
• See the source code for exact definitions (and help me expand this information):
$FOAM_UTILITIES/postProcessing/sampling/sample/sample.C
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
84
The sample utility - types
• Use dummy entries to see the alternatives for types (version dependent):
• Sets:
uniform
midPointAndFace
face
midPoint
cloud
curve
• Surfaces:
thresholdCellFaces
cuttingPlane
isoSurfaceCell
patch
isoSurface
distanceSurface
plane
• See the source code for exact definitions (and help me expand this information):
$FOAM_UTILITIES/postProcessing/sampling/sample/sample.C
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
85
The foamCalc utility
• This utility calculates new fields from existing ones. This replaces some
of the utilities in previous versions of OpenFOAM, such as magU and
Ucomponents.
• Usage (NOTE that foamCalc doesn’t accept usual flags, and must be run
from within the case directory):
foamCalc <calcType> <fieldName1 ... fieldNameN>
• To get a list of available <calcType>s, write:
foamCalc xxx and get the following list (version dependent):
randomise, magSqr, magGrad, addSubtract, div, mag,
interpolate, components
• Examples:
foamCalc div U #(needs: div(U) Gauss linear; in: system/fvSchemes)
foamCalc components U
• The new fields are written in the time directories.
• An advanced user could try to find out how to add a <calcType>
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
86
The setFields utility
• The setFields utility is used to set values to the fields in specific regions. You will use
this when you do the interFoam/damBreak tutorial in the UserGuide.
• Usage (setFields -help, version dependent):
setFields [-latestTime] [-time ranges] [-parallel]
[-constant] [-noZero] [-case dir] [-help] [-doc] [-srcDoc]
• A setFieldsDict dictionary is used. Find an example in the damBreak tutorial:
cd $FOAM_RUN/cavity
cp $FOAM_TUTORIALS/multiphase/interFoam/ras/damBreak/system/setFieldsDict cavity/system/
sed -i s/"alpha1"/"p"/g cavity/system/setFieldsDict
sed -i s/"box (0 0 -1) (0.1461 0.292 1)"/"box (0 0 -1) (0.05 0.05 1)"/g cavity/system/setFieldsDict
setFields -case cavity
- The defaultFieldValues sets the default values of the fields.
- A boxToCell bounding box is used to define a set of cells where the fieldValues
should be different than the defaultFieldValues.
- Use a dummy instead of boxToCell to see the topoSetSource alternatives.
- An advanced user could describe different topoSetSource’s
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
87
The funkySetFields, groovyBC and swak4Foam utilities
This are really useful community contributions!
• funkySetFields is a development of the setFields utility, and it includes the option
of specifying mathematical expressions etc.:
http://openfoamwiki.net/index.php/Contrib_funkySetFields
• The groovyBC utility is similar, but for boundaries:
http://openfoamwiki.net/index.php/Contrib_groovyBC
It should be noted that in 2.0.x, there is a new way of setting boundary conditions similar
to groovyBC, but with C++ syntax.
• The above have now been merged into swak4Foam:
http://openfoamwiki.net/index.php/Contrib/swak4Foam
http://www.openfoamworkshop.org/6th_OpenFOAM_Workshop_2011/Program/Training/gschaider_slides.pdf
http://www.openfoamworkshop.org/6th_OpenFOAM_Workshop_2011/Program/Training/gschaider_material.tgz
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
88
The foamToVTK, checkMesh, and flattenMesh utilities
• The foamToVTK utility can be used in many different ways. Example:
• The two empty sides of a 2D mesh must have the same mesh distribution. Add 0.0001
to the z-position of one of the constant/polyMesh/points of the cavity case.
• The checkMesh utility can be used to verify this. If not, it will complain:
***Number of edges not aligned with or perpendicular to non-empty directions: ????
Writing ???? points on non-aligned edges to set nonAlignedEdges
• The point labels are written to constant/polyMesh/sets/nonAlignedEdges
• Take the opportunity to visualize the point set in paraFoam: First open the cavity case
in paraFoam, then use File/Open <case>.OpenFOAM to read in the same case again.
This time mark Include Sets, mark only Mesh Parts/NonAlignedEdges, and visualize
using box glyphs.
• Another way to view the problematic points in paraview (not paraFoam):
foamToVTK -case cavity -pointSet nonAlignedEdges
The result appears in the VTK directory.
• The flattenMesh utility can sometimes fix the problem, like in this case.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
89
The transformPoints utility
• Moves, rotates and scales the mesh.
• Usage (transformPoints -help, version dependent):
transformPoints [-translate vector] [-yawPitchRoll (yaw pitch roll)]
[-rotateFields] [-parallel] [-rotate (vector vector)]
[-rollPitchYaw (roll pitch yaw)] [-scale vector] [-case dir]
[-help] [-doc] [-srcDoc]
• Example:
run
cp -r cavity cavityMoved
transformPoints -case cavityMoved -translate "(0.1 0 0)"
• Have a look in paraFoam:
run
touch cavityMoved/cavityMoved.OpenFOAM
paraFoam -case cavity
Click Accept and then use File/Open to open the cavityMoved.OpenFOAM file at the
same time.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
90
The mergeMeshes utility
• Takes the meshes from two different cases and merges them into the master case.
• mergeMeshes reads the system/controlDict of both cases and uses the startTime,
so be careful if you have a moving mesh for example. The first case that you specify
will be the master, and a new time (startTime+deltaT) will be written in which a new
polymesh is located. Move it to the correct position (constant/polyMesh), and you
have a case with the merged mesh.
• Example (start from clean cases):
run
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity cavityMerged
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity cavityTransformed
blockMesh -case cavityMerged
blockMesh -case cavityTransformed
transformPoints -case cavityTransformed -translate "(0.1 0 0)"
mergeMeshes . cavityMerged . cavityTransformed #No dots in 2.0.x!
mv cavityMerged/0.005/polyMesh/* cavityMerged/constant/polyMesh
• Note that the two meshes will keep all their original boundary conditions, so they are
not automatically coupled. Try icoFoam! To couple the meshes, use stitchMesh...
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
91
The stitchMesh utility
• Couples two uncoupled parts of the mesh that belong to the same case.
• You should have a patch in one part of the mesh (masterPatch) that fits with a corresponding patch on the other part of the mesh (slavePatch). If you have that, then the
command is:
stitchMesh masterPatch slavePatch
• masterPatch and slavePatch are important, as the face and cell numbers will be
renamed after the masterPatch.
• After stitchMesh, masterPatch and slavePatch are still present in the new
polymesh/boundary, but they are empty so just delete them. The same thing can be
done as well for the boundary conditions in the 0 folder.
• We have to re-organize the patches for this to work with our cavityMerged case.
• See www.openfoamwiki.net for more details:
http://openfoamwiki.net/index.php/Sig_Turbomachinery_/_ERCOFTAC_centrifugal_pump_with_a_vaned_diffuser#stitchMesh
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
92
The decomposePar utility
• decomposePar makes a domain decomposition for parallel computations. This is described in the UserGuide.
• Usage:
decomposePar [-fields] [-force] [-copyUniform] [-cellDist]
[-filterPatches] [-ifRequired] [-case dir] [-region name]
[-help] [-doc] [-srcDoc]
• A decomposeParDict specifies how the grid should be decomposed. An example can be
found in the interFoam/damBreak tutorial: system/decomposeParDict.
• There are some different alternatives for which method to use for the decomposition.
See the UserGuide. numberOfSubdomains specifies the number of subdomains the grid
should be decomposed into. Make sure that you specify the same number of subdomains
in the specific decomposition method you will use, otherwise your simulation might not
run optimal.
• We will discuss parallel processing later on.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
93
The reconstructPar utility
• reconstructPar is the reverse of decomposePar, reassembling the grid and the results.
• Usage:
reconstructPar [-zeroTime] [-fields "(list of fields)"]
[-latestTime] [-time ranges] [-constant] [-noZero]
[-noLagrangian] [-case dir] [-region name]
[-help] [-doc] [-srcDoc]
• This is usually done for post-processing, although it is also possible to post-process each
domain separately by treating an individual processor directory as a separate case when
starting paraFoam.
• We will discuss parallel processing later on.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
94
functionObjects
• functionObjects are general libraries that can be attached run-time to any
solver, without having to re-compile the solver.
• An example can be found in the incompressible/pisoFoam/les/pitzDaily
tutorial.
• A functionObject is added to a solver by adding a functions entry in
system/controlDict
• You can find functionObjects in the source code, in the OpenFOAM
Wiki (www.openfoamwiki.net), and in the OpenFOAM-extend project
(www.sourceforge.net).
• Search the tutorials for examples using:
grep -r functionObjectLibs $FOAM_TUTORIALS
• The implementations can be found in:
$FOAM_SRC/postProcessing/functionObjects
• I need your help documenting this.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
95
The probes and fieldAverage functionObjects
• The probes functionObject probes the development of the results during a
simulation, writing to a file in the directory probes.
• The fieldAverage functionObject calculates the time-average of specified
fields and writes the results in the time directories.
• Copy and modify the functions part at the end of the controlDict of
the incompressible/pisoFoam/les/pitzDaily tutorial to your case
and run it.
• Visualize the output file of sample in Gnuplot:
plot "probes/0/p" using 1:2,"probes/0/p" using 1:3
• Visualize the output from fieldAverage in paraFoam.
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
96
The surfaces functionObjects
The surfaces functionObject writes out surface interpolated results to disk.
If the surfaceFormat is VTK, those can be viewed in paraview.
Two examples (see the commented lines for the second one):
functions(
pressure{
type surfaces;
functionObjectLibs ("libsampling.so");
outputControl timeStep;
outputInterval 1;
surfaceFormat vtk;
fields ( p );
surfaces
(
walls
{
//type
patch;
//patchName
movingWall;
//triangulate
false;
type
plane;
normalVector (0 0 1);
basePoint (0 0 0.005);
} ); } );
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
97
The forceCoeffs functionObject
• The forceCoeffs functionObject prints out the lift and drag coefficients.
• See the sonicFoam/ras tutorial: system/controlDict
• Note that you must specify realistic reference values!
• See the source code for the details!
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
98
More functionObjects
• http://openfoamwiki.net/index.php/Contrib_simpleFunctionObjects
•
http://openfoamwiki.net/index.php/Sig_Turbomachienry_/_ERCOFTAC_centrifugal_pump_with_a_vaned_diffuser#Optional_tools
˚
Hakan
Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics
99