[OpenFOAM] 编程基础01 “Hello World!” in OpenFOAM

发布于 2023-05-04  461 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

"Hello World!" in OpenFOAM

本文是OpenFOAM编程基础系列文章的第一篇,也是自己学习的笔记,将随着学习的深入逐渐更新。本文的内容是在开源项目Basic OpenFOAM Programming Tutorials的基础上改写而来。由于原项目只有源代码和英文注释,本系列文章计划将代码进行分解、添加更加详细的注释,即是分享,也是自己学习。
下面,开始学习吧!首先,我们从OpenFOAM环境下输出“Hello World”开始。注意,此处假设你已经正确安装OpenFOAM,并且对linux系统的使用有一定的了解

在终端中输入以下命令创建一个空白的求解器文件:

foamNewApp helloWorld

其中,helloWorld为编译后的求解器的名称。打开生成的helloWorld.C文件,将代码修改成如下内容:

#include "fvCFD.H"
int main(int argc, char *argv[])
{
    // OpenFOAM screen output is very similar to rudimentary C++ with its std::cout, std::nl and std::endl
    // being replaced with Foam::Info, Foam::nl, and Foam::endl.
    Info << "Hello there, I'm an OpenFOAM program!" << nl
         << "You don't need a mesh or anything to run it, just a bare OpenFOAM case will do." << nl
        << tab << "This is me again, just creating a tabulated new line, move along." << nl << endl;
    Info<< "End\n" << endl;
    return 0;
}

此时,在helloWorld文件下打开终端,运行对文件进行编译:

wmake

编译完成后,在任意位置的终端内运行:

helloWorld

你应该能得到如下输出:

$ helloWorld
Hello there, I'm an OpenFOAM program!
You don't need a mesh or anything to run it, just a bare OpenFOAM case will do.
        This is me again, just creating a tabulated new line, move along.

End
Everything not saved will be lost.
最后更新于 2023-05-04