本文是OpenFOAM编程基础系列文章的第2篇,也是自己学习的笔记,将随着学习的深入逐渐更新。本文的内容是在开源项目Basic OpenFOAM Programming Tutorials的基础上改写而来。由于原项目只有源代码和英文注释,本系列文章计划将代码进行分解、添加更加详细的注释,即是分享,也是自己学习。

从现在起,在运行案例前请检查案例的路径不包含空格!!否则会报错。

本文食用方法:

  1. 你可以按顺序阅读,理解代码之后再下载案例代码自己运行;或者
  2. 先跳到最后,下载案例代码,一遍读一边测试运行代码。

开始正文吧:

一个求解器的主程序(.c为后缀的文件)都会包含以下代码:

#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"  // more code linesInfo<< "End\n" << endl;return 0;
}

在编写新的求解器时,我们需要做的是根据需求在第12行的位置添加更多功能性代码。注意,程序的第9行需要检查./system/controlDict文件是否存在,并读取其中的设置参数,若不存在则会报错,求解器无法运行:

-->FOAM FATAL ERROR:
cannot find file "/mnt/BasicOpenFOAMProgrammingTutorials-master/helloWorld/system/controlDict"

同样,第10行代码则检查./constant文件下的网格文件是否存在, 若不存在同样会报错并退出运行:

--> FOAM FATAL ERROR:
Cannot find file "points" in directory "polyMesh" in times "0" down to constantFrom function virtual Foam::IOobject Foam::fileOperation::findInstance(const Foam::IOobject&, Foam::scalar, const Foam::word&) constin file global/fileOperations/fileOperation/fileOperation.C at line 871.FOAM exiting

下面,将根据输入输出的数据类型分别讲解相应的代码。

1. OpenFOAM格式的字典文件

本案例以读取一个名为customProperties的字典为例。字典的内容如下:

FoamFile
{version     2.0;format      ascii;class       dictionary;location    "constant";object      transportProperties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //someWord myWord;someScalar 0.01;someBool on;someList
(012
);someHashTable
(key0 (0 0 0)key1 (1 0 0)
);

首先,现创建一个字典类型,指定文件名称、文件路径和读取要求:

    // Get access to a custom dictionarydictionary customDict;const word dictName("customProperties");// Create and input-output object - this holds the path to the dict and its nameIOobject dictIO(dictName, // name of the filemesh.time().constant(), // path to where the file is: ./constantmesh, // reference to the mesh needed by the constructorIOobject::MUST_READ // indicate that reading this dictionary is compulsory);

可以采用以下代码检查输入的字典文件是否符合OpenFOAM的格式要求,检查通过后则将字典初始化:

    // Check the if the dictionary is present and follows the OF formatif (!dictIO.typeHeaderOk<dictionary>(true))FatalErrorIn(args.executable()) << "Cannot open specified refinement dictionary "<< dictName << exit(FatalError);// Initialise the dictionary objectcustomDict = IOdictionary(dictIO);

下面,就可以从字典中读取信息了。这里我们使用.lookup函数实现。使用该函数不需要指定变量类型。

    // Lookup which does not need to be told what type of variable we're looking for and// uses the standard C++ stringstream syntaxword 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 dictionaryscalar someScalar( customDict.lookupOrDefault<scalar>("someScalar", 1.0) );

也可以从字典中读取Bool类型的数据:

    // 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) );

读取列表数据(List)和哈希表(HashTable)数据:

    // Lists of values may also be read in the same wayList<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 handyHashTable<vector,word> someHashTable ( customDict.lookup("someHashTable") );

最后,我们将所有读取到的数据输出到终端:

    // Summarise what's been read and print in the consoleInfo << 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;

2. 创建一个目录并写入一个输出文件

首先,在案例的根目录下创建一个名为postProcessing的文件夹,然后打开新创建的文件:

    // Create the output path directory; "Case/postProcessing"fileName outputDir = mesh.time().path()/"postProcessing"; // Creathe the directorymkDir(outputDir);// File pointer to direct the output toautoPtr<OFstream> outputFilePtr;// Open the file in the newly created directoryoutputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat"));

向文件中写数据可以使用以下语句实现:

    // Write stuffoutputFilePtr() << "# This is a header" << endl;outputFilePtr() << "0 1 2 3 4 5" << endl;// Append to the imported hash table and wirte it toosomeHashTable.insert("newKey", vector(1., 0., 0.));outputFilePtr() << someHashTable << endl;

3 案例文件下载

点击下载
提取码:cjkn

OpenFOAM编程基础(2) -数据读取与保存相关推荐

  1. Flash数据读取和保存

    实现方法 Flash数据读取和保存的目的是在单片机的程序存储区开辟一块空间专门用来保存系统需要记忆的参数和数据,从而完全取代EEROM,达到降低成本和数据保密的目的.该实现方法主要分为四个部分: FL ...

  2. pandas 数据读取与保存

    pandas 数据读取与保存 一:读取表格数据 例:在一个text.xlsx文件中,有text1,text2,text3三张表格 sheetname 切换sheet表格 可以传入整形,表示从0开始的索 ...

  3. OpenFOAM编程基础(1) - Hello World

    本文是OpenFOAM编程基础系列文章的第一篇,也是自己学习的笔记,将随着学习的深入逐渐更新.本文的内容是在开源项目Basic OpenFOAM Programming Tutorials的基础上改写 ...

  4. TDMS数据 读取/转换/保存 为MATLAB/Python 可读取的通用数据格式的的方法

    TDMS格式是NI主推的高速测试测量采集系统中的一种二进制数据存储类型,适合存储海量才几级数据,兼有高速.方便和易存取等多种优点.做过实际测量项目的筒子们在NI的相关平台例如:CompactRIO/L ...

  5. sparksql 保存点_Spark(十二)【SparkSql中数据读取和保存】

    一. 读取和保存说明 SparkSQL提供了通用的保存数据和数据加载的方式,还提供了专用的方式 读取:通用和专用 保存 保存有四种模式: 默认: error : 输出目录存在就报错 append: 向 ...

  6. 【python数据处理基础】--数据读取、清洗数据

    python 数据处理实战 目录 数据读取 选择特列 清洗数据 目录 随着网络数据的爆发式的增长,数据处理工作日益显示出它的重要性,我们的目的是从大量的杂乱无章的数据中找出对我们的工作有益的数据或者发 ...

  7. Spark学习笔记:数据读取和保存

    spark所支持的文件格式 1.文本文件 在 Spark 中读写文本文件很容易. 当我们将一个文本文件读取为 RDD 时,输入的每一行 都会成为 RDD 的 一个元素. 也可以将多个完整的文本文件一次 ...

  8. python 对json数据读取及保存与读取,对dump,dumps,load,loads的理解

    一.对json文件的读取 data1={"programmers":[{ "firstName": "Brett", "lastN ...

  9. Kanzi编程基础3 - 图片读取与显示

    Kanzi开发的时候会遇到需要从外部读取图片的情况.Kanzi2.8版本和3.3版本读取方法稍有不同,我们先看看2.8版本的api. [2.8版本] 1)首先要从文件中读取一张图片 struct Kz ...

  10. 一、flink基础之数据读取

    Flink读取文件的几种方式 1.从文本文件中读取数据 2.从容器中读取数据 3.从流处理组件中读取数据 4.自定义源读取数据 1.从文本文件中读取数据 我们尝试读取一份用户访问网址的数据: 用户名 ...

最新文章

  1. SQL Relay 0.47 发布,SQL 中间层
  2. python开发的前景_python开发前景怎么样
  3. 2022年初,给5年内还想做产品经理的提个醒!
  4. int型数据占用的内存空间及ASCII码表
  5. datetime模块日期转换和列表sorted排序
  6. mysql gz 安装_Linux下安装mysql 5.7.17.tar.gz的教程详解
  7. 这个华人程序员是如何 6 个月狂赚 125 亿的?
  8. ScrollView滑动控制
  9. 在点光源的基础上利用光域网来分布光的传播范围及方向_daiding
  10. Windows下microsip和pjsip通话
  11. 企业微信API全局错误码 enum枚举类
  12. sar图像matlab,用Matlab制作SAR仿真图像
  13. TCP SYN洪水 (SYN Flood) 攻击原理与实现
  14. 阿里正式交棒在即 普通股“一拆八”为赴港上市铺路?
  15. aws的sdk异常 unable to execute HTTP ec2-north-1.amazonaws
  16. 陶  朱  商  经
  17. 不敢相信!那些真实存在的机器人女友们!
  18. java防止注册刷短信攻击_java面试(1)如何防止恶意攻击短信验证码接口
  19. BOSS直聘自动投简历聊天机器人的实现过程
  20. [Python图像处理] 合成微缩效果

热门文章

  1. UVA 11909 Soya Milk(简单数学三角函数计算)
  2. 华为在线笔试题-python
  3. 计算三角形的周长和面积
  4. 阿里云实践训练营第七天——Class6 NAS快速搭建个人网盘
  5. gds文件 导出_将gds导入virtuoso以后,再将该gds导出,为什么元素顺序就变了?谢谢...
  6. 部署项目 Failure obtaining db row lock: Table ‘XXX.qrtz_LOCKS‘ doesn‘t exist
  7. 程序员:耐得住寂寞,禁得住诱惑
  8. Pytorch:二、数据加载与数据集的划分(猫狗)
  9. 时间序列预测系列文章总结(代码使用方法)
  10. linux桌面网络连接是个X,Xbrowser如何运行多个X桌面