一些OpenFOAM自带的有用小工具

前言
这篇博文总结自OpenFOAM的用户手册。OpenFOAM的用户手写得很好,初学OpenFOAM的时候没觉得什么,等差不多入门之后,回头再看一遍,能发现其中很多亮点,很多能解决实际使用痛点的操作。有空多读手册,多回头看看。(本文长期更新)

  • 场映射 mapFields
  • 插值得到中间步的场 temporalInterpolate
  • 重新进行并行区域划分redistributePar
  • OpenFOAM搜索&百科 foamSerch&foamInfo
  • 复制/清理算例 foamCloneCase&foamListTimes&foamCleanPolyMesh
  • 修改字典 foamDictionary
  • 配置文件获取 foamGet

场映射 mapFields

mapFields工具用来映射场。比如目前我们有一个100x100网格的运算结果,为了提高运算精度,我们决定将网格密度提高至200x200。为了不至于从0开始计算,我们可以使用mapFields工具将100x100网格的结果映射值200x200的网格中。关于mapFields工具,在OpenFOAM用户手册中多有探讨读者可以自行去阅读(用户手册第七版,2.1.5.3节,2.1.9节,5.6节)。值得强调的是,该技术实现的方法是进行插值,因此对于一些对有界性要求非常高的变量来说,进行场映射可能导致越界。(例如相界面处)。对于并行运算结果,该命令不需要进行重组也可以运行。

插值得到中间步的场 temporalInterpolate

使用 temporalInterpolate可以通过插值得到两个时间步之间的场。例如我们现在已经得到了0.2,0.4,0.6,0.8四步的数据,运行以下命令:

temporalInterpolate -time 0.2:0.8

我们可以插值得到0.3,0.5,0.7s时的数据,而运行:

temporalInterpolate -time 0.2,0.8

我们将得到0.2s和0.8s中间步的数据,即0.5s时的数据。
temporalInterpolate还可以指定插值方式等其他参数,可以使用附加-help参数来查看temporalInterpolate函数支持的参数。

重新进行并行区域划分redistributePar

当我们已经进行了并行区域的划分又想重新进行新的区域划分时,我们可以使用redistributePar命令进行。假设我们现在已经使用decomposePar命令划分好了2个并行区域,现在我们想重新划分为4个并行计算域,我们可以先可以修改system/decomposeParDict里的参数,进行4区域的划分,然后执行以下命令:

mpirun -np 4 redistributePar -parallel

如果想重新划分特定时间步(例如1s时)的数据,可以添加time关键词:

mpirun -np 4 redistributePar -time 1 -parallel

命令执行完毕后会命令提示,根据提示的内容可以删除原有的划分结果,例如上述命令执行完后会提示:

You can pick up the redecomposed mesh from the polyMesh directory in 1.003045.
If you redecomposed the mebash to less processors you can delete
the processor directories with 0 sized meshes in them.
Below is a sample set of commands to do this. Take care when issuing these
commands.

rm -r processor0/constant/polyMesh
mv processor0/1.003045/polyMesh processor0/constant/polyMesh
rm -r processor1/constant/polyMesh
mv processor1/1.003045/polyMesh processor1/constant/polyMesh
rm -r processor2/constant/polyMesh
mv processor2/1.003045/polyMesh processor2/constant/polyMesh
rm -r processor3/constant/polyMesh
mv processor3/1.003045/polyMesh processor3/constant/polyMesh

只需复制执行下面提示的一系列命令即可删除原有的划分结果。

OpenFOAM搜索&百科 foamSerch&foamInfo

foamSerch命令可以对OpenFOAM中某一路径下给定文件的给定字段进行搜索。终端输入foamSerch后回车可以得到其基本使用方法:

Usage: foamSearch [OPTIONS] <directory> <filename> <keyword>
options:
  -count | -c         prefix lines by the number of occurrences
  -help  | -h         print the usage

Searches the <directory> for files named <filename> and extracts entries with
<keyword>.  Sorts result into a list of unique entries (removing repeats).

Examples:
  * Default ddtSchemes entries in the fvSchemes files in all tutorials:
      foamSearch /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials fvSchemes  ddtSchemes.default

  * Relaxations factors for U in fvSolutions files in all tutorials:
      foamSearch -c /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials fvSolution  relaxationFactors.equations.U

例如,我们按照提示输入foamSearch /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials fvSchemes ddtSchemes.default即可得到算例目录下所有算例使用的默认瞬态项离散格式:

default         backward;
default         CrankNicolson 0.9;
default         CrankNicolson ocCoeff { type scale ; scale linearRamp ; duration 1 ; value 0.9 ; };
default         Euler;
default         localEuler;
default         none;
default         steadyState;

如果在foamSearch后附加-c 参数还可以统计每个值出现的次数。
foamInfo命令的使用比较简单,只需输入一个OpenFOAM的关键词即可得到关于这个关键词的解释,例如我们输入foamInfo icoFoam可以得到关于icoFoam的源代码位置,描述,以及使用icoFoam的例子:

File
    /home/cloudbird/OpenFOAM/OpenFOAM-7/applications/solvers/incompressible/icoFoam/icoFoam.C

Description
    Transient solver for incompressible, laminar flow of Newtonian fluids.

Examples using "icoFoam"
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/incompressible/icoFoam/elbow
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/incompressible/icoFoam/cavity/cavityGrade
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/incompressible/icoFoam/cavity/cavity
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/incompressible/icoFoam/cavity/cavityClipped

对于其他的关键词,例如离散格式和边界条件也可以同样进行搜索,例如下面是我们对vanLeer离散格式和fixedFluxPressure边界条件的搜索结果:
vanLeer:

File
    /home/cloudbird/OpenFOAM/OpenFOAM-7/src/finiteVolume/interpolation/surfaceInterpolation/limitedSchemes/vanLeer/vanLeer.H

Description
    Class with limiter function which returns the limiter for the
    vanLeer differencing scheme based on r obtained from the LimiterFunc
    class.

    Used in conjunction with the template class LimitedScheme.

Examples using "vanLeer"
    *** Listing 10 out of 85; run with "-a" option to list all ***
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/biconic25-55Run35
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/forwardStep
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/LadenburgJet60psi
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/movingCone
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/obliqueShock
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/compressible/rhoCentralFoam/wedge15Ma5
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/mesh/foamyHexMesh/mixerVessel
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/multiphase/cavitatingFoam/LES/throttle
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/multiphase/cavitatingFoam/LES/throttle3D
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/multiphase/compressibleInterFoam/laminar/climbingRod

fixedFluxPressure:

File
    /home/cloudbird/OpenFOAM/OpenFOAM-7/src/finiteVolume/fields/fvPatchFields/derived/fixedFluxPressure/fixedFluxPressureFvPatchScalarField.H

Description
    This boundary condition sets the pressure gradient to the provided value
    such that the flux on the boundary is that specified by the velocity
    boundary condition.

    Example of the boundary condition specification:
    \verbatim
    <patchName>
    {
        type            fixedFluxPressure;
    }
    \endverbatim

Examples using "fixedFluxPressure"
    *** Listing 10 out of 109; run with "-a" option to list all ***
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/combustion/fireFoam/LES/flameSpreadWaterSuppressionPanel
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/combustion/fireFoam/LES/oppositeBurningPanels
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/combustion/fireFoam/LES/smallPoolFire2D
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/combustion/fireFoam/LES/smallPoolFire3D
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/combustion/reactingFoam/RAS/membrane
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/heatTransfer/buoyantPimpleFoam/BernardCells
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/heatTransfer/buoyantPimpleFoam/hotRoom
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/heatTransfer/buoyantPimpleFoam/hotRoomBoussinesq
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/heatTransfer/buoyantSimpleFoam/buoyantCavity
    /home/cloudbird/OpenFOAM/OpenFOAM-7/tutorials/heatTransfer/buoyantSimpleFoam/circuitBoardCooling

复制/清理算例 foamCloneCase&foamListTimes&foamCleanPolyMesh

使用foamCloneCase命令可以复制现有算例,如:

foamCloneCase damBreak damBreak2

该命令可以将当前目录下的damBreak算例复制为同文件夹下的damBreak2算例。
使用foamListTimes命令可以输出当前算例除0之外的所有时间步,附加参数-rm可以删除除了0之外的所有时间步。foamCleanPolyMesh 命令则可以删除当前算例的网格文件。
以上命令在没有GUI环境的服务器上特别实用。

修改字典 foamDictionary

在远程登录服务器的时候,如果发现需要修改算例的配置,通常做法是在终端使用vim打开相应的配置字典进行修改,这在某些情况下将显得十分麻烦,而如果使用foamDictionary工具将简化这一操作。以下举例说明:

  • 查看fvSchemes文件:
foamDictionary system/fvSchemes
  • 查看fvSchemes文件中的divScheme:
foamDictionary -entry divSchemes system/fvSchemes
  • 查看fvSchemes文件中的divScheme中的div(phi,U)
foamDictionary -entry "divSchemes.div(phi,U)" system/fvSchemes

或者:

foamDictionary -entry "divSchemes.div(phi,U)" -value system/fvSchemes

二者的差别读者可以自己实践。

  • 修改div(phi,U)为迎风格式
foamDictionary -entry "divSchemes.div(phi,U)" system/fvSchemes -set "bounded Gauss upwind"

注: 这里和东岳翻译的OpenFOAM手册中的参数顺序不一致,实测按照该顺序运行将无法使用该函数。另外,手册中说,如果div(phi,U)字段不存在则需要将-set替换为-add,实测表明,-set参数也可以实现空白字段的添加。例如上面的命令中如果div(phi,U)不存在,那么将会新建一个div(phi,U)关键词并进行赋值。

配置文件获取 foamGet

我们知道,使用OpenFOAM自带的后处理工具进行数据提取需要复制一些配置文件到算例的system文件夹下。例如要提取线上的数据,则需要复制singGraph文件到system文件夹下。使用foamGet工具可以免去自己去OpenFOAM文件夹中寻找这些文件的过程,而直接将配置文件复制到system文件夹下,例如:

foamGet singleGraph

如果搜索的结果发现关键词对应多个文件,则程序会提示用户选择将哪一个文件复制到system文件夹中。

你可能感兴趣的:(OpenFOAM使用笔记)