Introduction

OpenFOAM 是一个用于创建可执行程序 (applications) 的 C++ 库。

  -----------------------------------------------------------------------
  | Open Source Field Operation and Manipulation (OpenFOAM) C++ Library |
  -----------------------------------------------------------------------
          |                          |                          |
  --------v---------            -----v-----             --------v----------
  | pre-processing |            | solving |             | post-processing |
  ----^---------^---            ---^----^--             ----^----------^---
      |         |                 /      \                  |          |
----------------------- ------------------------------ ---------------------------
| Utilities | Meshing | |    User     |  Standard    | | ParaView |    Others    |
|           |  Tools  | |Applications | Applications | |          | e.g. EnSight |
----------------------- ------------------------------ ---------------------------

Application

Applications:

  • 求解器 (solver): 依据 CFD 模型创建的求解器,每个用于求解一个特定的问题
    • 数值计算 (流体流动、化学反应、换热、结构动力学、电磁场、金融评估等)
  • 工具 (utility): 执行简单的前后处理任务,主要负责数据操作和代数几何运算
    • 前处理 (建模、网格、边界条件)
    • 后处理 (计算结果显示和处理)

Classification

  1. 直接利用求解器
  2. 自定义求解器
    • 按照自己的求解流程来编写求解器,注重求解过程
  3. 自定义离散方法

Install

Install from source

~/OpenFOAM/
	OpenFOAM-4.0
	ThirdParty-4.0
export FOAM_INST_DIR=$HOME/$WM_PROJECT
yum install mpich mpich-devel
module load mpi/openmpi
yum install flex bison zlib-devel
wget http://download.openfoam.org/source/4-0 -O OpenFOAM.tgz
wget http://download.openfoam.org/third_party/4-0 -O third_party.tgz
tar zxf OpenFOAM.tgz
tar zxf third_party.tgz
source $HOME/OpenFOAM/OpenFOAM-4.0/etc/bashrc
./Allwmake -j nproc

Install from repo

sudo add-apt-repository http://dl.openfoam.org/ubuntu
sudo sh -c 'wget -O - http://dl.openfoam.org/gpg.key | apt-key add -'
sudo apt-get update
sudo apt-get -y install openfoam4

Verify

cd $FOAM_RUN
cp -r $FOMA_TUTORIALS/incompressible/simpleFoam/pitzDaily .
cd pitzDaily
blockMesh
simpleFoam
paraFoam

User Guide

OpenFOAM User Guide

Table of Contents

Tutorials

顶盖驱动流

带孔盘体应力分析

溃坝

cp -r $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/damBreak/ $FOAM_RUN
blockMesh
setFields
interFoam
paraFoam

需要注意 setFields 的执行顺序。

Applications and libraries

求解器代码作为数学算法和方程组的体现。 相对于编程语言,理解方程组、模型和求解步骤更加重要。

$WP_PROJECT_DIR/doc/Doxygen/html/index.html
  • wmake
  • wclean
  • rmdepall

directory layout:

  • newApp
    • newApp.c
    • otherHeader.H
    • Make
      • files
      • options

Debug:

foamDebugSwitches

Compiling with wmake

EXE = $(FOAM_USER_APPBIN)/icoFOAM

Parallel Computing

OpenFOAM 使用的并行计算方法为计算域分解法。 在这个方法中,几何和附属场被拆分为单独的块,每个块用单独的 CPU 来进行计算。

并行计算主要涉及到:

  1. 网格和场的分解
  2. 并行运行程序
  3. 分解场的后处理

decomposePar 程序用来分解网格和场。

system/decomposeParDict

method:

  • simple
  • hierarchical
  • scotch
  • manual

在并行计算中使用第三方 MPI 工具,例如 open MPI:

mpirun --hostfile <matchines> -np <nproc> <foamExec> <args> -parallel > log

OpenFOAM cases

File structure of OpenFOAM cases

  • case
    • system
      • controlDict
      • fvSchemes
      • fvSolution
    • constant
      • properties
      • polyMesh
        • points
        • faces
        • owner
        • neighbour
        • boundary
    • time directories

cavity

  • 0 : 初始条件和边界条件
    • p: 压力
    • U: 速度
  • constant: 参数和网格
    • transportProperties 物理参数设定文件
    • polyMesh 网格文件
      • blockMeshDict // blockMesh 网格生成文件
      • boundary 边界条件,blockMesh 自动生成
  • system
    • controlDict 程序运行控制文件
    • fvSchemes 离散格式设定文件
    • fvSolution 代数方程求解器设定文件

Mesh generation and conversion

Post-processing

ParaView/paraFoam graphical user interface (GUI)

Visualization Toolkit (VTK)

OpenFOAM includes the foamToVTK utility to convert data from its native format to VTK format, which means that any VTK-based graphics tools can be used to post-process OpenFOAM cases.

File->SaveAnimation… -> ogv(on ubuntu)

Models and physical properties

Inside the Solver

Layout

  • src: the core OpenFOAM libraries
  • applications: solvers and utilities
  • tutorials: test-cases that demostrate a wide-range of OpenFOAM functionality
  • doc: documentation

C++ Source Guide

OpenFOAM C++ Source Guide

目前看到求解器一般是在一个循环里边,多次调用 solve 方法。 然后核心应该是在时间控制器中,也就是 Time 类。 从代码中可以看到,Time 类中控制了 solve 的调度,具体是在每个 deltaT 间隔,调用 functionObjects 的 execute 方法。循环结束之前调用 functionObjects 的 end 方法。

functionObject 类派生出了许多求解器相关的基类。

$FOAM_SRC/OpenFOAM/db/Time/Time.{H,C}
//- Return true if run should continue,
//  also invokes the functionObjectList::end() method
//  when the time goes out of range
//  \note
//  For correct behaviour, the following style of time-loop
//  is recommended:
//  \code
//      while (runTime.run())
//      {
//          runTime++;
//          solve;
//          runTime.write();
//      }
//  \endcode
virtual bool run() const;

//- Return true if run should continue and if so increment time
//  also invokes the functionObjectList::end() method
//  when the time goes out of range
//  \note
//  For correct behaviour, the following style of time-loop
//  is recommended:
//  \code
//      while (runTime.loop())
//      {
//          solve;
//          runTime.write();
//      }
//  \endcode
virtual bool loop();

//- Return true if end of run,
//  does not invoke any functionObject methods
//  \note
//      The rounding heuristics near endTime mean that
//      \code run() \endcode and \code !end() \endcode may
//      not yield the same result
virtual bool end() const;
bool Foam::Time::run() const
{
    bool running = value() < (endTime_ - 0.5*deltaT_);

    if (!subCycling_)
    {
        // only execute when the condition is no longer true
        // ie, when exiting the control loop
        if (!running && timeIndex_ != startTimeIndex_)
        {
            // Note, end() also calls an indirect start() as required
            functionObjects_.end();
        }
    }

    if (running)
    {
        if (!subCycling_)
        {
            const_cast<Time&>(*this).readModifiedObjects();

            if (timeIndex_ == startTimeIndex_)
            {
                functionObjects_.start();
            }
            else
            {
                functionObjects_.execute();
            }
        }

        // Update the "running" status following the
        // possible side-effects from functionObjects
        running = value() < (endTime_ - 0.5*deltaT_);
    }

    return running;
}


bool Foam::Time::loop()
{
    bool running = run();

    if (running)
    {
        operator++();
    }

    return running;
}
$FOAM_SRC/OpenFOAM/db/functionObjects/functionObject/functionObject.H
class functionObject {
// Member Functions

//- Return the name of this functionObject
	const word& name() const;

	//- Read and set the function object if its data have changed
	virtual bool read(const dictionary&);

	//- Called at each ++ or += of the time-loop.
	//  postProcess overrides the usual executeControl behaviour and
	//  forces execution (used in post-processing mode)
	virtual bool execute() = 0;

	//- Called at each ++ or += of the time-loop.
	//  postProcess overrides the usual writeControl behaviour and
	//  forces writing always (used in post-processing mode)
	virtual bool write() = 0;

	//- Called when Time::run() determines that the time-loop exits.
	//  By default it simply calls execute().
	virtual bool end();

	//- Called at the end of Time::adjustDeltaT() if adjustTime is true
	virtual bool adjustTimeStep();

	//- Update for changes of mesh
	virtual void updateMesh(const mapPolyMesh& mpm);

	//- Update for changes of mesh
	virtual void movePoints(const polyMesh& mesh);
};
$FOAM_SRC/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H
class regionFunctionObject : public functionObject
$FOAM_SRC/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.H
class fvMeshFunctionObject : public regionFunctionObject
$FOAM_SRC/functionObjects/solvers/lnInclude/scalarTransport.H
class scalarTransport : public fvMeshFunctionObject

Finite Volume Method

The Finite Volume Method (FVM) is a numerical technique that transforms the partial differential equations representing conservation laws over differential volumes into discrete algebraic equations over finite volumes (or elements or cells).

In a similar fashion to the finite difference or finite element method, the first step in the solution process is the discretization of the geometric domain, which, in the FVM, is discretized into non-overlapping elements or finite volumes. The partial differential equations are then discretized/transformed into algebraic equations by integrating them over each discrete element. The system of algebraic equations is then solved to compute the values of the dependent vaiable for each of the elements.

In the finite volume method, some of the terms in the conservation equation are turned into face fluxes and evaluated at the finite volume faces. Because the flux entering a given volume is identical to that leaving the adjacent volume, the FVM is strictly conservative. The inherent conservation property of the FVM makes it the preferred method in CFD. Another important attribute of the FVM is that it can be formulated in the physical space on unstructured polygenal meshes. Finally in the FVM it is quite easy to implement a variety of boundary conditions in a noninvasive manner, since the unknown variables are evaluated at the centroids of the volume elements, not at their boundary faces.

The Finite Volume Method in Computational Fluid Dynamics and Advanced Introduction with OpenFOAM and Matlab

https://feaweb.aub.edu.lb/research/cfd

Development

  • OpenFOAM
    • 求解器开发
    • 速度优化
      • openmpi
      • ZEROMQ
      • GPU
  • OpenFOAM 虚拟机镜像准备
    • OpenFOAM 安装配置
    • paraView web server
      • apache httpd ?
  • 前端开发
    • 参数提交
    • 结果展现
      • 网格化结果
      • 求解过程
  • 后端开发
    • 需求评审
    • 接口定义
      • 输入参数
        • 什么样的参数?
        • 不通的参数如何响应?
          • 不认识的参数?
          • 参数范围不合理?
          • 精度过高?不支持?
          • 参数缺失?
          • 参数互斥?
          • 参数覆盖?
      • 输出结果
        • 参数转换失败?
        • 任务运行失败?
          • 内存不足?
          • 程序本身错误?
          • 客户端 abort ?
        • 结果返回
          • 结果过大?
          • 网络失败?
          • 客户端 abort ?
    • 功能实现
      • 参数校验
      • 参数变换
      • 参数传递
      • 运行控制
        • 失败重试?
          • 重试次数?
          • 自动 or 手动?具体策略?
        • 任务中止?
      • 结果获取
      • 结果传输
    • 负载均衡
    • 安全防范
  • 前端后台交互
    • 参数传递
    • 结果转换
    • 任务调度
    • 那些接口是可以保证的?那些不是?

References