openFoam測試代碼文件
OpenFoam作為強大的開源計算流體工具,同時又是很多功能強大的C++類庫,因此,其在用戶根據自己特定的問題通過修改某個求解器或工具,就能很方便的實現對這一問題的解決。但是OpenFoam的類非常龐大,而且對很多類進行了封裝,因此,對于初學者來說,對OpenFoam中類的使用難度比較大,好在OpenFoam中applications\test文件夾中有對一些常用的類示例,用戶對示例的例子的研究能對各個類的使用有更深的理解。本次小編就看一下test文件夾中的例子。
首先,我們先來看一下fileName這個文件夾,文件夾中含有一個Test-fileName.c文件和Make文件夾,Make文件夾中是files文件和options文件。files文件是申明編譯文件和編譯后可執行文件或庫的所在位置和名字,options文件是編譯過程的依賴庫的位置。
下面我們來看一下Test-fileName.c文件。
#include "fileName.H"http://fileName類的頭文件,該類用來處理文件名的,由沒有空格和引號的字符串組成
#include "SubList.H"http://SubList類頭文件,用于從一個列表中獲得一個子列表
#include "IOobject.H"http://定義了對象的一些由objectRegistry隱式管理時所需要的屬性,同時還提供了基礎性的輸入/輸出流。
#include "IOstreams.H"http://IOstreams結合了輸入輸出流等頭文件
#include "OSspecific.H"
using namespace Foam;//使用Foam的命名空間
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main()
{
wordList wrdList(5);//創建wrdList對象,可由char和string等類型構造
wrdList[0] = "hello";
wrdList[1] = "hello1";
wrdList[2] = "hello2";
wrdList[3] = "hello3";
wrdList[4] = "hello4.hmm";
fileName pathName(wrdList);//由word列表對象,構造創建pathName對象,為文件路徑和名字
Info<< "pathName = " << pathName << nl//輸出文件路徑
<< "pathName.name() = >" << pathName.name() << "<\n"http://輸出pathName對象的文件名
<< "pathName.path() = " << pathName.path() << nl//輸出pathName對象的路徑
<< "pathName.ext() = >" << pathName.ext() << "<\n"http://輸出文件名的后綴
<< "pathName.name(true) = >" << pathName.name(true) << "<\n";\\輸出文件名不含后綴的部分
Info<< "pathName.components() = " << pathName.components() << nl\\輸出pathName對象的路徑的各個分量
<< "pathName.component(2) = " << pathName.component(2) << nl\\輸出pathName對象路徑的第三個分量
<< endl;
// try with different combination
// The final one should emit warnings
for (label start = 0; start <= wrdList.size(); ++start)
{
fileName instance, local;//創建instance、local對象
word name;//創建word對象
fileName path(SubList<word>(wrdList, wrdList.size()-start, start));//用wrdList的子列表構建path對象,子列表從start位置開始,長度為wrdList.size()-start。
fileName path2 = "."/path;//由"."/path構建path2對象
IOobject::fileNameComponents
(
path,
instance,
local,
name
);//調用IOobject中的fileNameComponents函數獲得instance、local和name的值
Info<< "IOobject::fileNameComponents for " << path << nl
<< " instance = " << instance << nl
<< " local = " << local << nl
<< " name = " << name << endl;
IOobject::fileNameComponents
(
path2,
instance,
local,
name
);
Info<< "IOobject::fileNameComponents for " << path2 << nl
<< " instance = " << instance << nl
<< " local = " << local << nl
<< " name = " << name << endl;
}
Info<< "\n\nfindEtcFile tests:" << nl
<< " controlDict => " << findEtcFile("controlDict") << nl//findEtcFile("controlDict")函數用于查找安裝路徑下etc文件夾下的文件并返回文件路徑,找不到放回空值
<< " badName => " << findEtcFile("badName") << endl;
Info<< "This should emit a fatal error:" << endl;
Info<< " badName(die) => " << findEtcFile("badName", true) << nl
<< endl;//findEtcFile("badName",true)用于判斷etc文件夾下文件是否存在,存在則返回文件路徑,不存在則跳出程序報出文件不存在的提示。
Info<< "\nEnd\n" << endl;
return 0;
}
在fileName文件夾下打開終端,輸入wmake進行編譯,編譯結果如下:

在終端中輸入:Test-filename,輸出結果如下:
.jpg@!jslnk)
.jpg@!jslnk)
在etc文件夾下新建一個badName文件,再執行以下可以Test-fileName命令輸出結果如下:
.jpg@!jslnk)
這次的內容就到此為止,注在4.0版本中需將最后一個頭文件改成#include "etcFiles.H",findEtcFile函數的申明換到了etcFile.H中。
個人理解,如有錯誤敬請諒解,歡迎提出共同進步。
也歡迎正在學習openFoam同學關注免費試用EasyCAE云仿真平臺。
工程師必備
- 項目客服
- 培訓客服
- 平臺客服
TOP




















