其他分享
首页 > 其他分享> > OFtutorial1_input_output

OFtutorial1_input_output

作者:互联网

OFtutorial1_input_output

教程1:输入与输出

代码:

#include "createTime.H"
#include "createMesh.H"

作用:

创建时间系统(实例化为runTime)和fvMesh(实例化为mesh)

代码:

dictionary customDict;
const word dictName("customProperties");

作用:

提供custom dictionary的接口

代码:

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

作用:

创建输入输出对象,指定了文件夹的路径和名称

dicName:文件的名称

mesh.time().constant():文件的路径

mesh:需要与网格对应

IOobject::MUST_READ:必须读取

代码:

if (!dictIO.typeHeaderOk<dictionary>(true))
    FatalErrorIn(args.executable()) << "Cannot open specified refinement dictionary "
        << dictName << exit(FatalError);

作用:

检查文件夹是否存在,并且符合openfoam的要求

代码:

customDict = IOdictionary(dictIO);

作用:

初始化dictionary对象

代码:

word someWord;
customDict.lookup("someWord") >> someWord;

作用:

从dictionary中读取各种各样的信息,lookup不需要指定读取的数据类型。可以通过标准化的C++ 的stringstream语法读取

代码:

scalar someScalar( customDict.lookupOrDefault<scalar>("someScalar", 1.0) );

作用:

这种模板化的方法不需要事先指定要读取的数据类型,并且在对象未在dictionary中找到的时候可以提供一个初始值。

代码:

bool someBool ( customDict.lookupOrDefault<Switch>("someBool",true) );

作用:

switch可以让一个bool型变量从dict中读取值,自动支持yes/on/true/1 和 no/off/false/0等值。

代码:

List<scalar> someList ( customDict.lookup("someList") );

作用:

list类型的数据也可以用同样的方法读取

代码:

HashTable<vector,word> someHashTable ( customDict.lookup("someHashTable") );

作用:

这个类型的容器很有意思,它可以把一组数与一个key联系起来(这里key用的字符,但是可以用任何类型的数据)。当通过列表管理不方便的时候很有用。

代码:

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;

作用:

输出信息

代码:

fileName outputDir = mesh.time().path()/"postProcessing";
mkDir(outputDir);

作用:

指定输出文件的文件路径

创建文件夹

代码:

autoPtr<OFstream> outputFilePtr;
outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat"));
outputFilePtr() << "# This is a header" << endl;
outputFilePtr() << "0 1 2 3 4 5" << endl;

作用:

创建文件指针,并打开新创建的文件。向文件中写入内容

代码:

someHashTable.insert("newKey", vector(1., 0., 0.));
outputFilePtr() << someHashTable << endl;

作用:

向hash table中附加内容,并写入文件中

customProperties文件的内容:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  3.0.1                                 |
|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    location    "constant";
    object      transportProperties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

someWord myWord;

someScalar 0.01;

someBool on;

someList
(
    0
    1
    2
);

someHashTable
(
    key0 (0 0 0)
    key1 (1 0 0)
);



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


总体代码:

/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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"

    // ---
    // Get access to a custom dictionary
    dictionary customDict;
    const word dictName("customProperties");

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

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

    // Initialise the dictionary object
    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) );

    // 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";
    // Creathe the directory
    mkDir(outputDir);

    // File pointer to direct the output to
    autoPtr<OFstream> outputFilePtr;
    // 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;

    // Append to the imported hash table and wirte it too
    someHashTable.insert("newKey", vector(1., 0., 0.));
    outputFilePtr() << someHashTable << endl;

    Info<< "End\n" << endl;
    return 0;
}


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




标签:outputFilePtr,customDict,dictionary,OFtutorial1,代码,OpenFOAM,mesh,output,input
来源: https://www.cnblogs.com/xubonan/p/15547926.html