求解器中如何使用私有变量
- 
							
							
							
							
							
							
我想在自己的求解器中使用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是否要定义?如何进行读取? 
- 
							
							
							
							
							
							
@东岳 您好,我仅在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? 
- 
							
							
							
							
你好,我看了一下 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其头文件。祝顺利~ 
 
			
