OpenFOAM编程基本结构

使用OpenFOAM读取网格

使用foamNewApp命令创建一个新App,其中main函数部分已经自动填充了一些必要的代码.

本文的目的是读取文件夹下constant/polyMesh的网格信息,并输出单元中心坐标单元间连接关系. 原代码如下

/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2022 OpenFOAM Foundation
     \\/     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 <http://www.gnu.org/licenses/>.

Application
    volumeRatioCheck

Description

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"//这个是最基本的头文件,包括了openFOAM的基本库

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

int main(int argc, char *argv[])
{
    #include "setRootCase.H"//根据argc和argv[]设置案例,是必须的
    #include "createTime.H"//记录时间
    #include "createMesh.H"//从constant/polyMesh中读取网格文件

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

    const label c=mesh.C().size();//获取网格所有中心坐标的数量
    /*label相当于标准C语言中的int
      mesh是由createMesh.H文件创立的,记录了网格的所有信息
      C()函数返回单元中心坐标,为volVectorField类型,可使用数组的方式调用
      size()函数返回C()的长度,为一个整数
    */
    Info<< nl << "网格数为" << c << nl << endl;//使用Info可以打印各种各样的数据类型,比较实用

    const labelListList &neighbour =mesh.cellCells();
    /*
      cellCells()返回单元连接关系,是一个二维列表
      第i行第j列的含义是:与第i个单元所相邻的第j个单元的序号
    */
    
    forAll(neighbour,cellI)//forAll是openFOAM自定义的循环方式
    {
        List<label> neighboursOfPrime= neighbour[cellI];
        Info<< nl <<"第"<< cellI <<"个单元的体积为" << mesh.V()[cellI] << endl;
        Info<< "该单元的中心坐标为" << mesh.C()[cellI] << endl;
        Info<< "其相邻的单元序号分别为" << endl;
        forAll(neighboursOfPrime,i)
        {
            label index=neighboursOfPrime[i];
            Info<< index << endl;
        }
    }
    
    //nl表示新建一行,以下返回计算时间
    Info<< nl << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
        << "  ClockTime = " << runTime.elapsedClockTime() << " s"
        << nl << endl;

    Info<< "End\n" << endl;

    return 0;
}


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

使用wmake命令编译后,在命令行中调用编译好的可执行程序.
本文使用了openFOAM教学案例pitzDaily中的网格,部分计算结果如下

Create time

Create mesh for time = 0


网格数为122250个单元的体积为5.04673e-10
该单元的中心坐标为(-0.0198086 0.00015942 0)
其相邻的单元序号分别为
1
181个单元的体积为4.84509e-10
该单元的中心坐标为(-0.0182574 0.00015942 0)
其相邻的单元序号分别为
0
2
192个单元的体积为4.65152e-10
该单元的中心坐标为(-0.0167681 0.00015942 2.78877e-20)
其相邻的单元序号分别为
1
3
203个单元的体积为4.46567e-10
该单元的中心坐标为(-0.0153384 0.00015942 0)
其相邻的单元序号分别为
2
4
21
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jedi-knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值