OpenFOAM 构建每个cell的邻接数组
问题来自 The Finite Volumn Method in Computational Fluid Dynamics–Exercise 7.5.1[1]
Ex7.5.1(2): 通过遍历内部单元面,并使用所属单元-邻接单元信息,构建每一个单元的连接数组。
首先,使用blockMesh指令,生成一组 4 × 4 × 1 的网格作为测试用例:
Writing polyMesh
----------------
Mesh Information
----------------
boundingBox: (0 0 0) (1 1 0.1)
nPoints: 50
nCells: 16
nFaces: 72
nInternalFaces: 24
----------------
Patches
----------------
patch 0 (start: 24 size: 4) name: movingWall
patch 1 (start: 28 size: 12) name: fixedWalls
patch 2 (start: 40 size: 32) name: frontAndBack
End
然后创建 Exercise7_5_1.C
文件:
// 网格总数
label cell_n = mesh.owner().size();
// 创建二维数组存储每个网格的相邻网格
labelListList Addr(cell_n);
// 创建保存每个网格相邻网格数量的列表
labelList nNbrs(cell_n, Zero);
// 遍历mesh.neighbour()列表,生成每个网格的相邻网格数量
for(label facei = 0; facei < mesh.neighbour().size(); facei++)
{
nNbrs[mesh.owner()[facei]]++;
}
// 用nNbr对二维数组作初始化
forAll(mesh.owner(), facei)
{
Addr[facei].setSize(nNbrs[facei], -1);
}
// 存储相邻网格的序列
for(label facei = 0; facei < mesh.neighbour().size(); facei++)
{
const label c0 = mesh.owner()[facei];
const label c1 = mesh.neighbour()[facei];
// 如果此网格存在相邻网格
if (Addr[c0].size()){
// 从二维数组的第0列开始遍历
label col_num(0);
while (Addr[c0][col_num] != -1) col_num++;
// 赋值操作
Addr[c0][col_num] = c1;
}
}
// 输出网格信息
for(label celli = 0; celli < mesh.nCells(); celli++){
// 如果存在相邻网格
if (Addr[celli].size()) Info << " This cell is the owner cell : " << celli << ", with neighbour cells of : " << Addr[celli] << endl;
else Info << " This cell is not the owner cell : " << celli << ", with neighbour cells of : " << Addr[celli] << endl;
}
// 输出 executionTime 和 clockTime
Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << " ClockTime = " << runTime.elapsedClockTime() << " s" << nl << endl;
Info<< "End\n" << endl;
return 0;
编译运行,可以得到如下的log信息:
Create time
Create mesh for time = 0
This cell is the owner cell : 0, with neighbour cells of : 2(1 4)
This cell is the owner cell : 1, with neighbour cells of : 2(2 5)
This cell is the owner cell : 2, with neighbour cells of : 2(3 6)
This cell is the owner cell : 3, with neighbour cells of : 1(7)
This cell is the owner cell : 4, with neighbour cells of : 2(5 8)
This cell is the owner cell : 5, with neighbour cells of : 2(6 9)
This cell is the owner cell : 6, with neighbour cells of : 2(7 10)
This cell is the owner cell : 7, with neighbour cells of : 1(11)
This cell is the owner cell : 8, with neighbour cells of : 2(9 12)
This cell is the owner cell : 9, with neighbour cells of : 2(10 13)
This cell is the owner cell : 10, with neighbour cells of : 2(11 14)
This cell is the owner cell : 11, with neighbour cells of : 1(15)
This cell is the owner cell : 12, with neighbour cells of : 1(13)
This cell is the owner cell : 13, with neighbour cells of : 1(14)
This cell is the owner cell : 14, with neighbour cells of : 1(15)
This cell is not the owner cell : 15, with neighbour cells of : 0()
ExecutionTime = 0 s ClockTime = 0 s
End
可以看到,网格0~14为owner cell,均有相邻网格 neighbour,而网格 15 不属于owner。