OpenFOAM编程 | Hello OpenFOAM

写在前面

OpenFOAM 是一个非常好用的开源程序包,笔者一直在研究和使用,其编程语言是笔者非常喜欢使用的 C++。但是笔者不是很喜欢 OpenFOAM 自己的构建工具 wmake,更倾向于使用 CMake 管理 C++ 项目。这样做有几点好处(个人观点),一方面是很多集成开发环境支持CMake方便开发,另外一方面是很多优秀的C++开源库也可以方便集成到一起,例如amgcl

因此笔者采用默认设置,在自己的笔记本电脑上安装了OpenFOAM-v2112,部分信息如下

                          ./+o+-       fitanium@Local
                  yyyyy- -yyyyyy+      OS: Ubuntu 22.04 jammy
               ://+//////-yyyyyyo      Kernel: x86_64 Linux 5.15.0-40-generic
           .++ .:/++++++/-.+sss/`      Uptime: 3d 5h 46m
         .:++o:  /++++++++/:--:/-      Packages: 2047
        o:+o+:++.`..```.-/oo+++++/     Shell: bash 5.1.16
       .:+o:+o/.          `+sssoo+/    Resolution: 1920x1080
  .++/+:+oo+o:`             /sssooo.   DE: Xfce
 /+++//+:`oo+o               /::--:.   WM: Xfwm4
 \+/+o+++`o++o               ++////.   WM Theme: Prof--XFCE- 2.1
  .++.o+++oo+:`             /dddhhh.   GTK Theme: Prof--XFCE- 2.1 [GTK2]
       .+.o+oo:.          `oddhhhh+    Icon Theme: Numix-Circle
        \+.++o+o``-````.:ohdhhhhh+     Font: WenQuanYi Micro Hei Mono 11
         `:o+++ `ohhhhhhhhyo++os:      Disk: 49G / 468G (11%)
           .o:`.syhhhhhhh/.oo++o`      CPU: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx @ 8x 2.1GHz
               /osyyyyyyo++ooo+++/     GPU: AMD Radeon Vega 8 Graphics (raven, LLVM 13.0.1, DRM 3.42, 5.15.0-40-generic)
                   ````` +oo+++o\:     RAM: 3642MiB / 5870MiB
                          `oo++.      

gcc 版本 : 11.2.0

main.cpp

在这里,我们要写一个非常非常入门的OpenFOAM版的Hello World.,代码如下所示。

#include <iostream>

#include <foamVersion.H>

int main(int argc, char* argv[])
{
    std::cout << "Hello OpenFOAM-" << Foam::foamVersion::version << std::endl;
    return 0;
}

CMakeLIsts.txt

这里我们使用CMake管理项目,内容如下所示。

cmake_minimum_required (VERSION 3.8)
project(HelloOpenFOAM)

# OpenFOAM 安装路径
set( FOAM_PREFIX "/opt/OpenFOAM-v2112" )

# 包含路径
set( FOAM_SRC ${FOAM_PREFIX}/OpenFOAM-v2112/src )
include_directories( ${FOAM_SRC}/atmosphericModels/lnInclude )
include_directories( ${FOAM_SRC}/combustionModels/lnInclude )
include_directories( ${FOAM_SRC}/conversion/lnInclude )
include_directories( ${FOAM_SRC}/dummyThirdParty/lnInclude )
include_directories( ${FOAM_SRC}/dynamicFaMesh/lnInclude )
include_directories( ${FOAM_SRC}/dynamicFvMesh/lnInclude )
include_directories( ${FOAM_SRC}/dynamicMesh/lnInclude )
include_directories( ${FOAM_SRC}/faOptions/lnInclude )
include_directories( ${FOAM_SRC}/fileFormats/lnInclude )
include_directories( ${FOAM_SRC}/finiteArea/lnInclude )
include_directories( ${FOAM_SRC}/finiteVolume/lnInclude )
include_directories( ${FOAM_SRC}/functionObjects/lnInclude )
include_directories( ${FOAM_SRC}/fvAgglomerationMethods/lnInclude )
include_directories( ${FOAM_SRC}/fvMotionSolver/lnInclude )
include_directories( ${FOAM_SRC}/genericPatchFields/lnInclude )
include_directories( ${FOAM_SRC}/lagrangian/lnInclude )
include_directories( ${FOAM_SRC}/lumpedPointMotion/lnInclude )
include_directories( ${FOAM_SRC}/mesh/lnInclude )
include_directories( ${FOAM_SRC}/meshTools/lnInclude )
include_directories( ${FOAM_SRC}/ODE/lnInclude )
include_directories( ${FOAM_SRC}/OpenFOAM/lnInclude )
include_directories( ${FOAM_SRC}/optimisation/lnInclude )
include_directories( ${FOAM_SRC}/OSspecific/POSIX/lnInclude )
include_directories( ${FOAM_SRC}/overset/lnInclude )
include_directories( ${FOAM_SRC}/parallel/lnInclude )
include_directories( ${FOAM_SRC}/phaseSystemModels/lnInclude )
include_directories( ${FOAM_SRC}/Pstream/lnInclude )
include_directories( ${FOAM_SRC}/randomProcesses/lnInclude )
include_directories( ${FOAM_SRC}/regionFaModels/lnInclude )
include_directories( ${FOAM_SRC}/regionModels/lnInclude )
include_directories( ${FOAM_SRC}/renumber/lnInclude )
include_directories( ${FOAM_SRC}/rigidBodyDynamics/lnInclude )
include_directories( ${FOAM_SRC}/rigidBodyMeshMotion/lnInclude )
include_directories( ${FOAM_SRC}/sampling/lnInclude )
include_directories( ${FOAM_SRC}/semiPermeableBaffle/lnInclude )
include_directories( ${FOAM_SRC}/sixDoFRigidBodyMotion/lnInclude )
include_directories( ${FOAM_SRC}/sixDoFRigidBodyState/lnInclude )
include_directories( ${FOAM_SRC}/surfMesh/lnInclude )
include_directories( ${FOAM_SRC}/thermophysicalModels/lnInclude )
include_directories( ${FOAM_SRC}/topoChangerFvMesh/lnInclude )
include_directories( ${FOAM_SRC}/transportModels/lnInclude )
include_directories( ${FOAM_SRC}/TurbulenceModels/lnInclude )
include_directories( ${FOAM_SRC}/waveModels/lnInclude )
include_directories( . )

link_directories(
    ${FOAM_PREFIX}/ThirdParty-v2112/platforms/linux64Gcc/boost_1_74_0/lib64
    ${FOAM_PREFIX}/ThirdParty-v2112/platforms/linux64Gcc/fftw-3.3.10/lib64
    ${FOAM_PREFIX}/ThirdParty-v2112/platforms/linux64Gcc/kahip-3.14/lib64
    ${FOAM_PREFIX}/ThirdParty-v2112/platforms/linux64GccDPInt32/lib
    ${FOAM_PREFIX}/ThirdParty-v2112/platforms/linux64GccDPInt32/lib/sys-openmpi

    ${FOAM_PREFIX}/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib
    ${FOAM_PREFIX}/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/dummy
    ${FOAM_PREFIX}/OpenFOAM-v2112/platforms/linux64GccDPInt32Opt/lib/sys-openmpi
    # 采用默认设置,大概就需要写这些就可以了。
    # 如果自定义设置,可能还需要添加其他路径。
)
set(EXTRA_LIBS dl m)
set(LIBS
    Pstream
    OpenFOAM
    # 下面这几个是一般常用的库,可根据需要取消注释或添加其他库
    # finiteVolume
    # meshTools
    # fileFormats
    ${EXTRA_LIBS}
)

# C++ 11
set( CMAKE_CXX_STANDARD 11 )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker --no-as-needed -Xlinker --add-needed" )
add_definitions(-Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -DNoRepository -m64 -fPIC )

# 添加可执行文件
add_executable (${PROJECT_NAME} "main.cpp")

# 链接库
target_link_libraries(${PROJECT_NAME} ${LIBS})

运行结果

除了libOpenFOAM.solibPstream.so外,我们可以看到编译出来的可执行文件依赖的都是系统库。
依赖库

接下来,看一下运行结果。
运行结果

posted @ 2022-06-29 22:34  Fitanium  阅读(900)  评论(0编辑  收藏  举报