如何从字典读入一串系数



  • 各位大佬好,我最近在写一个流体的热物性参数,计算Cp和he等需要使用多项式,需要读入一串的scalar,我采用的scalarList,但是编译时出现了类型错误,我想请问一下,需要读入一串scalar的时候,大家采用的是什么方法?
    LiquidThermo.C

    class LiquidThermo
    {
        private:
            //constructor
            LiquidThermo();
            //constructor 
            LiquidThermo(const fvMesh & mesh,const dictionary & dict);    
            protected:
            //rho
            scalarList Coerho_[8];
            scalarList Coepsi_[7];
            // NSRDfunc
            scalarList Coemu_[5];
            //cp
            scalarList CoeCp_[8];
            //kappa
            scalarList Coekappa_[6];
            //he
            scalarList Coehe_[8];
        public:
            //inline functions used for constructor and correct function 
            //rho
            inline scalar rho(const scalar p,const scalar T) const;
            inline scalar psi(const scalar p,const scalar T) const;
            inline scalar mu(const scalar p,const scalar T) const;
            inline scalar Cp(const scalar p,const scalar T) const;
            inline scalar kappa(const scalar p,const scalar T) const;
            inline scalar alpha(const scalar p,const scalar T) const;
            inline scalar he(const scalar p,const scalar T) const;
    ...
    

    LiquidThermo.H
    //constructor and member fucntion

    //constructor 
    Foam::LiquidThermo::LiquidThermo
    (
        const fvMesh & mesh,
        const dictionary& dict
    ):
        name_(dict.dictName()),
        mesh_(mesh),
        p_(mesh.thisDb().lookupObject<volScalarField>(dict.lookup("p"))),
        T_(mesh.thisDb().lookupObject<volScalarField>(dict.lookup("T"))),
        rho_
        (
    	IOobject
    	(
    	    IOobject::groupName("rho", name_),
    	    mesh.time().timeName(),
    	    mesh,
    	    IOobject::NO_READ,
    	    IOobject::NO_WRITE
    	),
        ...
    {
        //read scalar
        Coerho_ = dict.lookup("Coerho");
        Coepsi_ = dict.lookup("Coepsi");
        Coemu_ = dict.lookup("Coemu");
        CoeCp_ = dict.lookup("CoeCp");
        Coekappa_ = dict.lookup("Coekappa");
        Coehe_ = dict.lookup("Coehe");
        R_   = readScalar(dict.lookup("R"));
        ...
    }
    //inline function 
    inline Foam::scalar Foam::LiquidThermo::rho
    (
        const scalar p,
        const scalar T
    ) const
    {
        //Coerho_[6]=pref ,Coerho_[7]=Tref
        return Coerho_[0]+Coerho_[1]*(p-Coerho_[6])+Coerho_[2]*(T-Coerho_[7])
        +Coerho_[3]*Foam::pow(p-Coerho_[6],2.0)+Coerho_[4]*Foam::pow(T-Coerho_[7],2.0)+Coerho_[5]*(p-Coerho_[6])*(T-Coerho_[7]);
    }
    

    基于OpenFoam2.4,编译错误为:

    LiquidThermo.C: In member function ‘Foam::scalar Foam::LiquidThermo::rho(Foam::scalar, Foam::scalar) const’:
    LiquidThermo.C:20:123: error: cannot convert ‘Foam::tmp<Foam::Field<double> >’ to ‘Foam::scalar {aka double}’ in return
         +Coerho_[3]*Foam::pow(p-Coerho_[6],2.0)+Coerho_[4]*Foam::pow(T-Coerho_[7],2.0)+Coerho_[5]*(p-Coerho_[6])*(T-Coerho_[7]);
                                                                                                                               ^
    


  • 在http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2017/DavidSegersson/report_David_Segersson.pdf找到了相关的例子。

    定义变量: scalarList Coerho_;//8
    初始化:从字典读取 
     dict.lookup("Coerho")>>Coerho_;
    在字典中书写:
    Coerho   (1.0 2.0 3.0);
    

    以上为例子调用如下代码不会出错:

    Info<<Coerho_[0]+Coerho_[6];
    

    Coerho_[6]会被默认为0.



  • 感谢分享!:xiexie:


  • OpenFOAM教授

    OpenFOAM 中提供了专门描述多项式的类:Polynomial

    用法可以参考:src/thermophysicalModels/specie/transport/polynomial/polynomialTransport.H



  • 我是这样做的:
    在求解器中,添加这样的代码:

            IOdictionary meshProperties
            (
                IOobject
                (
                    "meshProperties",
                    runTime.constant(),
                    mesh,
                    IOobject::MUST_READ,
                    IOobject::NO_WRITE
                )
            );
    	scalar xnumber(int(readScalar(meshProperties.lookup("xnumber"))));
    	scalar ynumber(int(readScalar(meshProperties.lookup("ynumber"))));
    

    在算例文件夹的constant文件夹中,创建一个名为meshProperties的文件,内容为

    /*--------------------------------*- C++ -*----------------------------------*\
    | =========                 |                                                 |
    | \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
    |  \\    /   O peration     | Version:  2.0.x                                 |
    |   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
    |    \\/     M anipulation  |                                                 |
    \*---------------------------------------------------------------------------*/
    
    FoamFile
    {
        version     2.0;
        format      ascii;
        class       dictionary;
        object      meshProperties;
    }
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    xnumber                       20;
    ynumber                       10;
    

    这样一来,参数xnumber和参数ynumber的值,就是通过算例文件夹里面的参数设定文件读取的了



  • @范准 非常好,感谢分享 :xiexie:


Log in to reply