計算流體和OpenFoam

來源:互聯網
上載者:User

標籤:

從碩士的計算數學到現在的流體的應用數學,算來也有五年的時間了.一直希望能尋找一個半專業的軟體可以做計算流體的學習和研究.

所謂半專業,是說一方面我們可以很傻瓜的如ansys,fluent那樣,使用他們的模型直接進行數值類比,另一方面我們又可以很輕易的修改模型.構建自己的模型.編寫自己的演算法.

所以第一次接觸OpenFoam時,就被她完全的吸引了.如此的貼和我的要求.更有魅力的是,她是一個開源軟體,展現給我們的不僅僅是cfd,更是一種編寫類比軟體的教程.

甚至我們自己可以試著學習怎麼去開發這樣的大型數值計算軟體.

下面的例子來源於蘇軍偉博士的新浪部落格,及我們園區的浪子禾月.

一 代碼解釋

例:OpenFOAM>>solver>>basic>>laplacianFoam

//createFields.H

/************************************************/

//-工具提示。Info 等價於 C++中std::cout,Info間接調用cout    Info<< "Reading field T\n" << endl;   //這是一個類的初始化.volScalarField 類T對象的初始化.//-聲明一個標量場,網格中心儲存變數。該場是通過讀入檔案(IOobject and MUST_READ)進行設定,並根據controlDict中的設定進行自動write,由write.H中的runTime.write()來執行write();。     volScalarField T    (        IOobject        (            "T",  //對應case根目錄下的目錄T,是資料存放區的方式            runTime.timeName(),// T目錄下的時間名稱, 初始的是0目錄,            mesh,               IOobject::MUST_READ,            IOobject::AUTO_WRITE        ),        mesh    );     //- 提示讀入參數控制檔案    Info<< "Reading transportProperties\n" << endl;    //- 參數控制檔案聲明過檔案形式讀入    IOdictionary transportProperties    (        IOobject        (            "transportProperties", //檔案名稱字            runTime.constant(), //檔案位置,case檔案夾中constant子檔案夾            mesh,                          IOobject::MUST_READ,//通過read一個檔案,初始化            IOobject::NO_WRITE  //並不根據時間對檔案進行寫        )    );     //-提出讀入擴散律    Info<< "Reading diffusivity DT\n" << endl;     //-通過查詢參數控制檔案,初始化帶有單位的標量,lookup中的“DT”為關鍵字    dimensionedScalar DT    (        transportProperties.lookup("DT")    );

//laplacianFoam.C (OpenFoam2.4.0)

 1 #include "fvCFD.H"//-cfd標頭檔,包括大多數cfd計算需要的標頭檔,在src » finiteVolume » cfdTools » general » include
 2 #include "simpleControl.H"  
3
4 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
5
6 int main(int argc, char *argv[])
7 {
8 #include "setRootCase.H"
9
10 #include "createTime.H"
11 #include "createMesh.H"
12 #include "createFields.H"
13
14 simpleControl simple(mesh);
15
16 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
17
18 Info<< "\nCalculating temperature distribution\n" << endl;
19
20 while (simple.loop())
21 {
22 Info<< "Time = " << runTime.timeName() << nl << endl;
23
24 while (simple.correctNonOrthogonal())
25 {
26 solve
27 (
28 fvm::ddt(T) - fvm::laplacian(DT, T)
29 );
30 }
31
32 #include "write.H"
33
34 Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
35 << " ClockTime = " << runTime.elapsedClockTime() << " s"
36 << nl << endl;
37 }
38
39 Info<< "End\n" << endl;
40
41 return 0;
42 }

 





1 #include "fvCFD.H" //-cfd標頭檔,包括大多數cfd計算需要的標頭檔,在src » finiteVolume » cfdTools » general » include 2 3 4 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 5 //主程式入口 6 int main(int argc, char *argv[]) 7 { 8 //設定rootcase,根據輸入參數argc 和 argv對 9 # include "setRootCase.H"10 11 //-建立時間,下面的runTime控制12 # include "createTime.H"13 14 //建立網格,根據constant檔案中polyMesh檔案夾中的網格資料,建立網格對象mesh,位於src » OpenFOAM » include15 # include "createMesh.H"16 17 //建立場對象,在前面已經說明18 # include "createFields.H"19 20 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //21 //提示計算溫度分布22 Info<< "\nCalculating temperature distribution\n" << endl;23 //計算主控制流程程24 for (runTime++; !runTime.end(); runTime++)25 {26 //提示當前計算時間27 Info<< "Time = " << runTime.timeName() << nl << endl;28 //讀入simple演算法參數,位於29 30 src » finiteVolume » cfdTools » general » include31 32 # include "readSIMPLEControls.H"33 //對於網格非正交迴圈修正。34 for (int nonOrth=0; nonOrth<=nNonOrthCorr; nonOrth++)35 {36 //求解拉普拉斯方程,這裡的solve是Foam空間的全域函數,內參數為fvMatrix,fvm表示隱式離散,返回有限容積疏鬆陣列類fvMatrix對象,具體對象中內容,以後說明37 solve38 (39 fvm::ddt(T) - fvm::laplacian(DT, T)40 );41 }42 //對求解變數進行寫43 # include "write.H"44 //提示執行時間及CPU耗時45 Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"46 << " ClockTime = " << runTime.elapsedClockTime() << " s"47 << nl << endl;48 }49 //提示程式結束50 Info<< "End\n" << endl;51 52 return(0);53 }

//write.H

 1 if (runTime.outputTime()) 2 { 3     volVectorField gradT = fvc::grad(T);  //計算溫度梯度,向量場 4   5    //聲明3個變數,分別以gradT的三個方向的分量進行初始化。 6   7     volScalarField gradTx 8     ( 9         IOobject10         (11             "gradTx", //變數名字12             runTime.timeName(), //位置13             mesh,               //mesh,主要用於對象註冊,根據runTime進行寫14             IOobject::NO_READ,  //15             IOobject::AUTO_WRITE //自動寫16         ),17         gradT.component(vector::X)  //用場初始話,vector::X 枚舉變數,可直接寫0 //gradT.component(0)18     );19  20     volScalarField gradTy21     (22         IOobject23         (24             "gradTy",25             runTime.timeName(),26             mesh,27             IOobject::NO_READ,28             IOobject::AUTO_WRITE29         ),30         gradT.component(vector::Y)31     );32  33     volScalarField gradTz34     (35         IOobject36         (37             "gradTz",38             runTime.timeName(),39             mesh,40             IOobject::NO_READ,41             IOobject::AUTO_WRITE42         ),43         gradT.component(vector::Z)44     );45  46     //對場進行寫47     runTime.write();48 }
二 C檔案

我們重點分析C檔案

//laplacianFoam.C (OpenFoam2.4.0)

 1 #include "fvCFD.H"//-cfd標頭檔,包括大多數cfd計算需要的標頭檔,在src » finiteVolume » cfdTools » general » include
 2 #include "simpleControl.H"  
3
4 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
5
6 int main(int argc, char *argv[])
7 {
8 #include "setRootCase.H"
9
10 #include "createTime.H"
11 #include "createMesh.H"
12 #include "createFields.H"
13
14 simpleControl simple(mesh);
15
16 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
17
18 Info<< "\nCalculating temperature distribution\n" << endl;
19
20 while (simple.loop())
21 {
22 Info<< "Time = " << runTime.timeName() << nl << endl;
23
24 while (simple.correctNonOrthogonal())
25 {
26 solve
27 (
28 fvm::ddt(T) - fvm::laplacian(DT, T)
29 );
30 }
31
32 #include "write.H"
33
34 Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
35 << " ClockTime = " << runTime.elapsedClockTime() << " s"
36 << nl << endl;
37 }
38
39 Info<< "End\n" << endl;
40
41 return 0;
42 }

 10-12行我們建立了場的網格,即我們將向量附著在了幾何空間上,形成了一個場.也就是說,目前我們有了數學上所描述的溫度場T.此時,T還沒有任何東西,是0標量場.

計算流體和OpenFoam

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.