设为首页 加入收藏

TOP

openfoam文件读取(一)
2023-07-23 13:40:43 】 浏览:59
Tags:openfoam 文件读

对于openfoam或其他c++程序而言,文件的读取是尤为重要的

我们最开始学习C++时,会学到类的初始化,或者是变量定义为某个值,再对某个值进行遍历,,,
类似如下:

点击查看代码
#include <iostream>         // 包含头文件。

using namespace std;        // 指定缺省的命名空间。

int main()
{
    int a = 10;
    int b = (10);
    int c(10);
    int e = {10};
    int f{ 10 };

    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    cout << "c=" << c << endl;
    cout << "e=" << e << endl;
    cout << "f=" << f << endl;
}

尤其c++,初学时候内心非常感慨,怎么这么多初始化方式,我该用哪个
现在回头看,其实能熟悉一个,其他看懂就行,看不懂都没关系,毕竟我们还有gdb或者typeid()帮忙,可以看看这个变量类型具体是什么
说到底我们要问题导向目标导向,我们学习C++是为了类似openfoam这样的大工程服务的
那学这么多初始化赋值,有必要吗
如果是个大工程或者大程序是完全没必要的,
因为大工程的数据都是从文件或者数据库中获得的
你想想,要计算一个流体问题,如果有一万个网格,手打一万个三维位置就已经非常让人头疼了,更不要说上百个组分和压力等参数了,不现实
所以又回到了问题的起点,读取文件很重要,那么如何从文件中初始化程序,如何读取文件


在看of源码之前,不妨复习下C++是怎么读取文件的,进行下知识巩固

C++多使用fstream类或ifstream类

点击查看代码
#include <iostream>
#include <fstream>  // ifstream类需要包含的头文件。
#include <string>     // getline()函数需要包含的头文件。
using  namespace std;

int main()
{
    string filename = R"(./test.txt)";

    //ifstream fin(filename, ios::in);
    ifstream fin;
    fin.open(filename , ios::in);

    // 判断打开文件是否成功。
    // 失败的原因主要有:1)目录不存在;2)文件不存在;3)没有权限,Linux平台下很常见。
    if (fin.is_open() == false)
    {
        cout << "打开文件" << filename << "失败。\n";  return 0;
    }

    string buffer;
    while (fin >> buffer)
    {
        cout << buffer << endl;
    }

    fin.close();	   // 关闭文件,fin对象失效前会自动调用close()。

    cout << "操作文件完成。\n";
}


OPENFOAM的文件读写,主要是用IOdictionary类

打开IOdictionary类的头文件,并不复杂

点击查看代码
class IOdictionary
:
    public baseIOdictionary
{

public:

    // Constructors

        //- Construct given an IOobject
        IOdictionary(const IOobject&);

        //- Construct given an IOobject and dictionary
        IOdictionary(const IOobject&, const dictionary&);

        //- Construct given an IOobject and Istream
        IOdictionary(const IOobject&, Istream&);

        //- Copy constructor
        IOdictionary(const IOdictionary&);

        //- Move constructor
        IOdictionary(IOdictionary&&);


    //- Destructor
    virtual ~IOdictionary();


    // Member Functions

        //- Is object global
        virtual bool global() const
        {
            return true;
        }

        //- Return complete path + object name if the file exists
        //  either in the case/processor or case otherwise null
        virtual fileName filePath() const
        {
            return globalFilePath(type());
        }


    // Member Operators

        //- Move assignment
        void operator=(IOdictionary&&);
};

继承于baseIOdictionary类,借助于IOobject接口有五个构造函数,这五个中一个移动构造一个拷贝构造,其他类继承可以创建自己成员函数global()和filePath(),头文件还对移动构造创建了等号赋值运算符
我们再看IOdictionary类的关键先生IOobject


IOobject就稍显复杂了,我们先看openfoam对其的描述:

IOobject defines the attributes of an object for which implicit objectRegistry management is supported, and provides the infrastructure for performing stream I/O.
An IOobject is constructed with an object name, a class name, an instance path, a reference to a objectRegistry, and parameters determining its storage status.

下面这张图是openfoam与该接口有关的类谱图
image
打开regIOobject类,这是个抽象类,openfoam对regIOobject的描述为:

regIOobject is an abstract class derived from IOobject to handle automatic object registration with the objectRegistry.

这里不赘述了,最后查了好半天,大概是哈希表来的,方便查询读取。openfoam中/0,/constant以及/syste

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇docker安装jenkins使用docker-com.. 下一篇第一周学习--linux

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目