Navigation

    CFD中文网

    CFD中文网

    • Login
    • Search
    • 最新

    求解器中如何使用私有变量

    OpenFOAM
    3
    5
    1729
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stergopilot
      stergopilot last edited by 李东岳

      我想在自己的求解器中使用incident radiation 'G',它在'fvDOM.H'中是一个private data

      相关代码如下:

      /*---------------------------------------------------------------------------*\
                                 Class fvDOM Declaration
      \*---------------------------------------------------------------------------*/
      
      class fvDOM
      :
          public radiationModel
      {
          // Private data
      
      
              //- Incident radiation  [W/m2]
              volScalarField G_;
      …………………………………………………
      //- Const access to incident radiation field
                  inline const volScalarField& G() const;
      

      在我的求解器中,我打算调用G来计算温度:

      T=pow(G/(4*sigma),0.25);
      

      但G是否要定义?如何进行读取?

      1 Reply Last reply Reply Quote
      • 李东岳
        李东岳 管理员 last edited by

        需要提供成员函数,例如:

        inline const volScalarField& G() const
        {
            return G_;
        }
        

        然后在你的求解器里面可以

        T=pow(yourClass.G()/(4*sigma),0.25);
        

        线上CFD课程 7月1日报名截止 http://dyfluid.com/class.html
        CFD高性能服务器 http://dyfluid.com/servers.html

        stergopilot 1 Reply Last reply Reply Quote
        • stergopilot
          stergopilot @李东岳 last edited by 李东岳

          @东岳 您好,我仅在createfields.h中定义这个成员函数,

          inline const volScalarField& G() const
          {
              return G_;
          }
          

          但是编译无法通过,出现错误

          In file included from radiationFoam.C:45:0:
          createFields.H: In function ‘int main(int, char**)’:
          createFields.H:49:1: error: a function-definition is not allowed here before ‘{’ token
           {
          

          此外,yourClass指的是什么?fvDOM?

          1 Reply Last reply Reply Quote
          • 浪
            浪迹天大 last edited by

            你好,我看了一下 fvDOM.H 这个文件,发现它里边已经定义了一个函数 G() 用于调用 G_,即该文件中的:

             //- Const access to incident radiation field
             inline const volScalarField& G() const;
            

            这样你就不需要在 solver 中定义了,通过这个函数使用它即可。
            然后我看 fireFoam 里使用了辐射模型,它创建了一个指针 radiation,类型是 radiationModel。但是这个类里边并没有 G() 这个函数,因此我们需要把它转换成它的派生类 fvDOM ,这样才能使用 G() 这个函数。
            你可以这样做,在求解器里边:

            const volScalarField & G = dynamic_cast<const Foam::radiation::fvDOM &>(radiation()).G();
            

            这句话有两个作用,1. 把 radiation 转换成它的派生类 fvDOM,2. 创建一个体标量场的引用,通过 G() 这个函数指向 fvDOM 类的私有数据 G_。
            于是就可以使用 G 来更新你的温度了:T=pow(G/(4*sigma),0.25);。我编译了一下改动后的代码,发现这里的 T 是一个 const引用 ,编译失败,如果你也遇到这个问题,可以在创建它的地方,把 const volScalarField& T = thermo.T(); 改为
            volScalarField& T = thermo.T(); 即可。
            对了,我们在求解器中使用了 fvDOM 这个类,所以需要在求解器最开始的地方 include 其头文件。

            祝顺利~

            OpenFOAM 学习交流:https://openfoam.top

            stergopilot 1 Reply Last reply Reply Quote
            • stergopilot
              stergopilot @浪迹天大 last edited by

              @浪迹天大 非常感谢您的帮助,我已经完美解决这个问题。

              1 Reply Last reply Reply Quote
              • First post
                Last post

              CFD中文网 | 东岳流体 | 京ICP备15017992号-2
              论坛登录问题反馈可联系 li.dy@dyfluid.com