OpenFOAM中polyMesh,fvMesh初步
引言
OpenFOAM使用有限體積法求解PDE。所以張量場和網格之間有聯絡。polyMesh類用來構建多面體網格,通常在constant/polymesh資料夾下。fvMesh繼承與polyMesh類,包括使用fvm法離散所需要的額外資訊,可參考test/mesh。geometricField將張量場和fvMesh類結合起來。這個是分析相關程式碼的起始步驟。
基礎程式碼
這裡可參考$FOAM_SOLVERS/incompressible/icoFoam/icoFoam。C。開啟這個檔案會幫助理解一些內容。OF是高度抽象的。很多東西不太好理解。
<
header
files
>
int
main
(
int
argc
,
char
*
argv
[])
{
#include
“setRootCase。H”
#include
“createTime。H”
通常這裡的標頭檔案是
“fvCFD。H”
嚴格按照下面命令操作
cd
$WM_PROJECT_USER_DIR
/applications/myTests
foamNewApp meshAndField
cd
meshAndField
wmake
meshAndField
當你使用了第二行的命令之後,會生成一個資料夾meshAndField。
使用wmake命令進行編譯
這裡可能不同系統有略微差別,不要緊。
然後使用meshAndField命令
提示錯誤資訊為:system/controlDict不存在,這就證明編譯對了。萬里長征第一步。
開啟檔案meshAndField。C檔案,原始碼如下。
/*——————————————————————————————————————-*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 AUTHOR,AFFILIATION
\\/ M anipulation |
————————————————————————————————————————-
License
This file is part of OpenFOAM。
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version。
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE。 See the GNU General Public License
for more details。
You should have received a copy of the GNU General Public License
along with OpenFOAM。 If not, see
Application
meshAndField
Description
\*——————————————————————————————————————-*/
#include
“fvCFD。H”
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int
main
(
int
argc
,
char
*
argv
[])
{
#include
“setRootCase。H”
#include
“createTime。H”
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Info
<<
nl
<<
“ExecutionTime = ”
<<
runTime
。
elapsedCpuTime
()
<<
“ s”
<<
“ ClockTime = ”
<<
runTime
。
elapsedClockTime
()
<<
“ s”
<<
nl
<<
endl
;
Info
<<
“End
\n
”
<<
endl
;
return
0
;
}
// ************************************************************************* //
以後的文章,就不再提供上面的註釋欄了。大概應該一模一樣的。
注意下函式體內呼叫了兩個標頭檔案。實際上他們並不是標頭檔案,只是插入了一段程式碼。
setRootCase。H
等效於
Foam::argList args(argc, argv);
if (!args。checkRootCase())
{
Foam::FatalError。exit()
}
createTime。H
等效於
Foam::Info << “Create time\n” << Foam::endl;
Foam::Time runTime(Foam::Time::controlDictName, args);
目前我的感覺是,凡是
int函式
內的呼叫標頭檔案,大機率是插入一段程式碼。這也是OF的套路之一。
算例分析
透過以下命令實現一些基本操作。
cp -r
$FOAM_TUTORIALS
/incompressible/icoFoam/cavity/cavity
$FOAM_RUN
/cavityFourCells
sed -i s/
‘20 20 1’
/
‘2 2 1’
/g
$FOAM_RUN
/cavityFourCells/system/blockMeshDict
blockMesh -case
$FOAM_RUN
/cavityFourCells
meshAndField -case
$FOAM_RUN
/cavityFourCells
第一句是從tut中複製cavity到算例檔案。
第二句是修改blockMeshDict中的劃分網格數量。從原來20×20×1的網格數,變為2×2×1的網格數。
第三句是使用blockMesh劃分網格。
第四句是使用meshAndField命令分析網格,這個時候使用這個命令就不會報錯了。
polyMesh類
修改meshAndField。C檔案
\
*——————————————————————————————————————-
*/
#include
“fvCFD。H”
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int
main
(
int
argc
,
char
*
argv
[])
{
#include
“setRootCase。H”
#include
“createTime。H”
Info
<<
“Create mesh”
<<
endl
;
polyMesh
mesh
(
IOobject
(
fvMesh
::
defaultRegion
,
runTime
。
timeName
(),
runTime
,
IOobject
::
MUST_READ
)
);
Info
<<
“Cell centres”
<<
nl
<<
mesh
。
cellCentres
()
<<
endl
;
Info
<<
“Cell volumes”
<<
nl
<<
mesh
。
cellVolumes
()
<<
endl
;
Info
<<
“Cell face centres”
<<
nl
<<
mesh
。
faceCentres
()
<<
endl
;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Info
<<
nl
<<
“ExecutionTime = ”
<<
runTime
。
elapsedCpuTime
()
<<
“ s”
<<
“ ClockTime = ”
<<
runTime
。
elapsedClockTime
()
<<
“ s”
<<
nl
<<
endl
;
Info
<<
“End
\n
”
<<
endl
;
return
0
;
}
// ************************************************************************* //
使用wmake進行編譯
再次執行meshAndField命令
(base) yangwang@yangwang-Super-Server:~/OpenFOAM/yangwang-v1712/run/cavityFourCells$ meshAndField
/*——————————————————————————————————————-*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v1712 |
| \\ / A nd | Web: www。OpenFOAM。com |
| \\/ M anipulation | |
\*——————————————————————————————————————-*/
Build : v1712
Arch : “LSB;label=32;scalar=64”
Exec : meshAndField
Date : Nov 10 2019
Time : 08:40:02
Host : “yangwang-Super-Server”
PID : 20367
I/O : uncollated
Case : /home/yangwang/OpenFOAM/
yangwang-v1712
/run/cavityFourCells
nProcs : 1
trapFpe: Floating point exception trapping enabled (FOAM_SIGFPE)。
fileModificationChecking : Monitoring run-time modified files using timeStampMaster (fileModificationSkew 10)
allowSystemOperations : Allowing user-supplied system call operations
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time
Create mesh
Cell centres
4((0。025 0。025 0。005) (0。075 0。025 0。005) (0。025 0。075 0。005) (0。075 0。075 0。005))
Cell volumes
4(2。5e-05 2。5e-05 2。5e-05 2。5e-05)
Cell face centres
20
(
(0。05 0。025 0。005)
(0。025 0。05 0。005)
(0。075 0。05 0。005)
(0。05 0。075 0。005)
(0。025 0。1 0。005)
(0。075 0。1 0。005)
(0 0。025 0。005)
(0 0。075 0。005)
(0。1 0。025 0。005)
(0。1 0。075 0。005)
(0。025 0 0。005)
(0。075 0 0。005)
(0。025 0。025 0)
(0。025 0。075 0)
(0。075 0。025 0)
(0。075 0。075 0)
(0。025 0。025 0。01)
(0。025 0。075 0。01)
(0。075 0。025 0。01)
(0。075 0。075 0。01)
)
ExecutionTime = 0。01 s ClockTime = 0 s
End
這就是polyMesh類
fvMesh類
只要將meshAndField。C檔案中polyMesh mesh修改為fvMesh mesh,再新增三行
Info << mesh。C() << endl;
Info << mesh。V() << endl;
Info << mesh。Cf() << endl;
重新編譯之後執行meshAndField。結果如下
(
base
)
yangwang@yangwang-Super-Server:~/OpenFOAM/yangwang-v1712/run/cavityFourCells$ meshAndField
/*——————————————————————————————————————-*
\
|
=========
|
|
|
\\
/ F ield
|
OpenFOAM: The Open Source CFD Toolbox
|
|
\\
/ O peration
|
Version: v1712
|
|
\\
/ A nd
|
Web: www。OpenFOAM。com
|
|
\\
/ M anipulation
|
|
\*
——————————————————————————————————————-*/
Build : v1712
Arch :
“LSB;label=32;scalar=64”
Exec : meshAndField
Date : Nov
10
2019
Time : 08:45:10
Host :
“yangwang-Super-Server”
PID :
20710
I/O : uncollated
Case : /home/yangwang/OpenFOAM/yangwang-v1712/run/cavityFourCells
nProcs :
1
trapFpe: Floating point exception trapping enabled
(
FOAM_SIGFPE
)
。
fileModificationChecking : Monitoring run-time modified files using timeStampMaster
(
fileModificationSkew 10
)
allowSystemOperations : Allowing user-supplied system call operations
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create
time
Create mesh
dimensions
[
0
1
0
0
0
0
0
]
;
internalField nonuniform List
((
0。025 0。025 0。005
)
(
0。075 0。025 0。005
)
(
0。025 0。075 0。005
)
(
0。075 0。075 0。005
))
;
boundaryField
{
movingWall
{
type
sliced
;
value nonuniform List
((
0。025 0。1 0。005
)
(
0。075 0。1 0。005
))
;
}
fixedWalls
{
type
sliced
;
value nonuniform List
((
0
0。025 0。005
)
(
0
0。075 0。005
)
(
0。1 0。025 0。005
)
(
0。1 0。075 0。005
)
(
0。025
0
0。005
)
(
0。075
0
0。005
))
;
}
frontAndBack
{
type
sliced
;
value nonuniform 0
()
;
}
}
dimensions
[
0
3
0
0
0
0
0
]
;
value nonuniform List
(
2。5e-05 2。5e-05 2。5e-05 2。5e-05
)
;
dimensions
[
0
1
0
0
0
0
0
]
;
internalField nonuniform List
((
0。05 0。025 0。005
)
(
0。025 0。05 0。005
)
(
0。075 0。05 0。005
)
(
0。05 0。075 0。005
))
;
boundaryField
{
movingWall
{
type
sliced
;
value nonuniform List
((
0。025 0。1 0。005
)
(
0。075 0。1 0。005
))
;
}
fixedWalls
{
type
sliced
;
value nonuniform List
((
0
0。025 0。005
)
(
0
0。075 0。005
)
(
0。1 0。025 0。005
)
(
0。1 0。075 0。005
)
(
0。025
0
0。005
)
(
0。075
0
0。005
))
;
}
frontAndBack
{
type
sliced
;
value nonuniform 0
()
;
}
}
Cell centres
4
((
0。025 0。025 0。005
)
(
0。075 0。025 0。005
)
(
0。025 0。075 0。005
)
(
0。075 0。075 0。005
))
Cell volumes
4
(
2。5e-05 2。5e-05 2。5e-05 2。5e-05
)
Cell face centres
20
(
(
0。05 0。025 0。005
)
(
0。025 0。05 0。005
)
(
0。075 0。05 0。005
)
(
0。05 0。075 0。005
)
(
0。025 0。1 0。005
)
(
0。075 0。1 0。005
)
(
0
0。025 0。005
)
(
0
0。075 0。005
)
(
0。1 0。025 0。005
)
(
0。1 0。075 0。005
)
(
0。025
0
0。005
)
(
0。075
0
0。005
)
(
0。025 0。025 0
)
(
0。025 0。075 0
)
(
0。075 0。025 0
)
(
0。075 0。075 0
)
(
0。025 0。025 0。01
)
(
0。025 0。075 0。01
)
(
0。075 0。025 0。01
)
(
0。075 0。075 0。01
)
)
ExecutionTime
=
0。01 s
ClockTime
=
0
s
End
兩相比較,polyMesh類有基本資訊,fvMesh類有更多資訊。
更方便的使用fvMesh類
\
*——————————————————————————————————————-
*/
#include
“fvCFD。H”
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int
main
(
int
argc
,
char
*
argv
[])
{
#include
“setRootCase。H”
#include
“createTime。H”
#include
“createMesh。H” //這個內容要明白
Info
<<
“Create mesh”
<<
endl
;
/* fvMesh mesh
(
IOobject
(
fvMesh::defaultRegion,
runTime。timeName(),
runTime,
IOobject::MUST_READ
)
);
*/
Info
<<
mesh
。
C
()
<<
endl
;
Info
<<
mesh
。
V
()
<<
endl
;
Info
<<
mesh
。
Cf
()
<<
endl
;
Info
<<
“Cell centres”
<<
nl
<<
mesh
。
cellCentres
()
<<
endl
;
Info
<<
“Cell volumes”
<<
nl
<<
mesh
。
cellVolumes
()
<<
endl
;
Info
<<
“Cell face centres”
<<
nl
<<
mesh
。
faceCentres
()
<<
endl
;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Info
<<
nl
<<
“ExecutionTime = ”
<<
runTime
。
elapsedCpuTime
()
<<
“ s”
<<
“ ClockTime = ”
<<
runTime
。
elapsedClockTime
()
<<
“ s”
<<
nl
<<
endl
;
Info
<<
“End
\n
”
<<
endl
;
return
0
;
}
// ************************************************************************* //
在檔案中使用
#include “createMesh。H”
等效於
fvMesh mesh
(
IOobject
(
fvMesh::defaultRegion,
runTime。timeName(),
runTime,
IOobject::MUST_READ
)
);
這段程式碼
所以你看到主函式中呼叫了#include “createMesh。H”剩下的內容就不用寫了。所以通常來說主函式中呼叫標頭檔案可能就是插入一段程式碼。
總結:
polyMesh和fvMesh都能呼叫網格資訊,但是網格資訊fvMesh給的更多更細。
一般計算過程都是呼叫fvMesh。
通常不會顯式使用fvMesh,而是使用#include “createMesh。H”來呼叫。OF中有很多這種玩法要注意。
上一篇:想念,真的會有聲音···