CFD中文网

    CFD中文网

    • 登录
    • 搜索
    • 最新

    如何从字典读入一串系数

    OpenFOAM
    4
    6
    1547
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • Y
      yfclark 讲师 最后由 编辑

      各位大佬好,我最近在写一个流体的热物性参数,计算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]);
                                                                                                                                 ^
      
      1 条回复 最后回复 回复 引用
      • Y
        yfclark 讲师 最后由 编辑

        在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.

        1 条回复 最后回复 回复 引用
        • 李东岳
          李东岳 管理员 最后由 编辑

          感谢分享!:xiexie:

          线上CFD课程开始报名:http://www.dyfluid.com/class.html

          CFD高性能服务器 http://dyfluid.com/servers.html

          1 条回复 最后回复 回复 引用
          • W
            wwzhao 教授 最后由 编辑

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

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

            1 条回复 最后回复 回复 引用
            • 范准
              范准 最后由 编辑

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

                      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的值,就是通过算例文件夹里面的参数设定文件读取的了

              李东岳 1 条回复 最后回复 回复 引用
              • 李东岳
                李东岳 管理员 @范准 最后由 编辑

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

                线上CFD课程开始报名:http://www.dyfluid.com/class.html

                CFD高性能服务器 http://dyfluid.com/servers.html

                1 条回复 最后回复 回复 引用
                • First post
                  Last post