002-OpenFOAM的文件IO
002-OpenFOAM的文件IO
OpenFOAM的IO,代码如下
/*------------------------- OFtutorial1_inputOutput -------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 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/>. \*---------------------------------------------------------------------------*/ #include "fvCFD.H" int main(int argc, char *argv[]) { // Initialise OF case #include "setRootCase.H" // These two create the time system (instance called runTime) and fvMesh (instance called mesh). #include "createTime.H" #include "createMesh.H" /* setRootCase,createTime,createMesh是三个非常重要的头文件,基本上所有的程序都需要include OpenFoam与常见的C++编程习惯有所不同,很多具体类函数的实现也是放在头文件中 例如很多曳力模型 */ // --- // Get access to a custom dictionary dictionary customDict; // 初始化了一个dictionary,这个dictionary类叫做customDict const word dictName("customProperties"); //初始化了一个常量,这个常量的类为word,常量名为DictName,值为一个string:"NewcustomProperties", //word类继承自string //const word dictName("customProperties1"); // Create and input-output object - this holds the path to the dict and its name IOobject dictIO ( dictName, // name of the file mesh.time().constant(), // path to where the file is mesh, // reference to the mesh needed by the constructor IOobject::MUST_READ // indicate that reading this dictionary is compulsory ); /* IOobject 是OpenFoAM里面的一个重要的流处理对象,我们可以看看IOobject.H中对这个类的声明: IOobject类有两种构造函数 1. 从对象名称,实例路径,objectRegistry引用和读写设置来构造。 IOobject ( const word & name, const word & instance, const objectRegistry & registry, readOption r = NO_READ, // MUST_READ: 在对象构造时必须从Istream中读取,如果Istream不存在或者不能读取时会产生一个错误信息。 // READ_IF_PRESENT: 如果Istream存在,则读取对象,否则不读取。仅仅在Istream存在但是不可读取的情况下才会产生错误信息。 // NO_READ: 不读取对象 writeOption w = NO_WRITE, // AUTO_WRITE: 当objectRegistry要求写的时候会自动写。 // NO_WRITE: 在对象析构的时候不会自动写,但是可以显式调用写操作。 // bool registerObject = true ) 2. 从对象名称,实例路径,位置,objectRegistry引用和读写设置来构造。 IOobject ( const word & name, const word & instance, const fileName & local, const objectRegistry & registry, readOption r = NO_READ, writeOption w = NO_WRITE, bool registerObject = true ) */ // Check the if the dictionary is present and follows the OF format // 抛出异常并终止程序 if (!dictIO.headerOk()) FatalErrorIn(args.executable()) << "Cannot open specified refinement dictionary " << dictName << exit(FatalError); // Initialise the dictionary object /* IOdictionary由dictionary和IOobject派生而来,通过objectRegistry赋予字典自动IO功能。 为了方便IO, IOdictionary提供了来自IOobject和writeData和write函数的构造函数。 */ customDict = IOdictionary(dictIO); // --- // Read various pieces of information from the main part of the dictionary // Lookup which does not need to be told what type of variable we're looking for and // uses the standard C++ stringstream syntax word someWord; customDict.lookup("someWord") >> someWord; // This template method needs to know the type of the variable and can provide // a default value if the entry is not found in the dictionary scalar someScalar( customDict.lookupOrDefault<scalar>("someScalar", 1.0) ); // scalar是对C++中float的继承,按需,可为double也可以为float16 // A switch is a neat feature allowing boolean values to be read from a dict, // it supports the OpenFOAM yes/on/true/1 and no/off/false/0 values automatically. bool someBool ( customDict.lookupOrDefault<Switch>("someBool",true) ); // Lists of values may also be read in the same way List<scalar> someList ( customDict.lookup("someList") ); // This type of container is particularly interesting - it associates entries with // given key values (here of word type but can be anything); useful when // associating things by indices in a list is less handy HashTable<vector,word> someHashTable ( customDict.lookup("someHashTable") ); // Summarise what's been read and print in the console Info << nl << "Read the following:" << nl << nl << "someWord " << someWord << nl << nl << "someScalar " << someScalar << nl << nl << "someList " << someList << nl << nl << "someHashTable " << someHashTable << nl << nl << "someBool " << someBool << nl << nl << endl; // --- // Create a custom directory and write an output file // Create the output path directory fileName outputDir = mesh.time().path()/"postProcessing"; // '/'经过了Opreator重载, 返回了一个fileName类,也是对string类的继承,表示加上 // mesh.time(): $WM_PROJECT/ 这个路径, .path()方法返回了路径的位置,是一个fileName类, // 继承string Info << outputDir << endl; // 这句是我加的,返回结果是"/home/wli/OF_Programming/OFtutorial1_inputOutput/testCase/postProcessing" // Creathe the directory mkDir(outputDir); // mkDir函数以一个fileName类作为变量,其功能是创建一个文件夹 //mkDir("/home/wli/OF_Programming/OFtutorial1_inputOutput/testCase/mkDirTest"); // 可以看到testCase下面创建了一个新的文件夹mkDirTest // File pointer to direct the output to autoPtr<OFstream> outputFilePtr; // autoPtr是OpenFOAM中自带的一类自动指针,还有一类自动指针叫tmp,实现了智能指针的效果,但是还要当作“class” // Open the file in the newly created directory outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat")); // 注意这里的指针方法用的是 “.” 而不是传统指针的 “->” // Write stuff outputFilePtr() << "# This is a header" << endl; outputFilePtr() << "0 1 2 3 4 5" << endl; Info<< "End\n" << endl; return 0; } // ************************************************************************* //
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)