CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   pointer to volScalarField (https://www.cfd-online.com/Forums/openfoam-programming-development/225724-pointer-volscalarfield.html)

JOEE April 6, 2020 00:41

pointer to volScalarField
 
Hello everyone,

In C++, we can assign a pointer to an array, for example:
double a[3] = {1,2,4};
double *p =a;

Then we can use p to access the array elements.

So, my question is there a pointer to volScalarFiled type variable, like volScalarField p?

raumpolizei April 6, 2020 07:34

Hey Joe,
in OF, it is vary rare to encounter a raw array, unless you are working with low level libraries. However is it always possible to define a pointer to a volScalarField and use the field by derefencing the pointer. I don't recommend doing this in OF, since they are better alternatives like autoPtr and tmp which implement so called smart-pointer classes. It is much safer to use these pointer classes, especially when passing pointers around.

Hope this helps.

RP

JOEE April 6, 2020 20:09

Quote:

Originally Posted by raumpolizei (Post 764376)
Hey Joe,
in OF, it is vary rare to encounter a raw array, unless you are working with low level libraries. However is it always possible to define a pointer to a volScalarField and use the field by derefencing the pointer. I don't recommend doing this in OF, since they are better alternatives like autoPtr and tmp which implement so called smart-pointer classes. It is much safer to use these pointer classes, especially when passing pointers around.

Hope this helps.

RP

Dear Raum Polizei,

Thanks for your reply, since I'm not familiar with the autoPtr and tmp, I decide to use the reference to do it. My code is as following,

class GetSet{

public:
double getUx(int m) const{return Ux_[m];}
double getUy(int m) const{return Uy_[m];}
double getUz(int m) const{return Uz_[m];}
double getP(int m) const{return p_[m];}

GetSet(const volScalarField& Ux,const volScalarField& Uy,
const volScalarField& Uz,const volScalarField& p)
{
Ux_ = Ux;
Uy_ = Uy;
Uz_ = Uz;
p_ = p;
}

private:
volScalarField Ux_;
volScalarField Uy_;
volScalarField Uz_;
volScalarField p_;
};

I want to use this class by

GetSet* gset = new GetSet(Ux,Uy,Uz,p);

and use gset->getUx(m) to get the values, but it seems that the form of the constructor has no match function (Ux,Uy and Uz have already been registered), does anybody know how to fix this problem? Thanks a lot!

olesen April 7, 2020 14:01

Still not really sure what you are attempting, but if you need then to be write-modifiable, then store your Ux_, Uy_ etc as references (volScalarField&). If they are to be read-only, store as const references.

What you currently had would make a full local copy of your fields (probably not what you want).

JOEE April 7, 2020 15:50

Quote:

Originally Posted by olesen (Post 764635)
Still not really sure what you are attempting, but if you need then to be write-modifiable, then store your Ux_, Uy_ etc as references (volScalarField&). If they are to be read-only, store as const references.

What you currently had would make a full local copy of your fields (probably not what you want).

Dear Olesen,
Thank you so much for your reply!!
I've solved this problem! Have a good day!

Joe


All times are GMT -4. The time now is 09:47.