CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

pointer to volScalarField

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 6, 2020, 00:41
Smile pointer to volScalarField
  #1
New Member
 
Join Date: Apr 2020
Posts: 3
Rep Power: 6
JOEE is on a distinguished road
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?
JOEE is offline   Reply With Quote

Old   April 6, 2020, 07:34
Default
  #2
Member
 
Join Date: Dec 2018
Location: Darmstadt, Germany
Posts: 87
Rep Power: 7
raumpolizei is on a distinguished road
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
raumpolizei is offline   Reply With Quote

Old   April 6, 2020, 20:09
Post
  #3
New Member
 
Join Date: Apr 2020
Posts: 3
Rep Power: 6
JOEE is on a distinguished road
Quote:
Originally Posted by raumpolizei View Post
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!
JOEE is offline   Reply With Quote

Old   April 7, 2020, 14:01
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,689
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
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).
olesen is offline   Reply With Quote

Old   April 7, 2020, 15:50
Default
  #5
New Member
 
Join Date: Apr 2020
Posts: 3
Rep Power: 6
JOEE is on a distinguished road
Quote:
Originally Posted by olesen View Post
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
JOEE is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error message Bruno_Jorge Main CFD Forum 1 February 5, 2019 11:12
namespace Foam Argen OpenFOAM 4 February 5, 2019 08:55
execFlowFunctionObjects - unknown field problem Toorop OpenFOAM Post-Processing 16 March 14, 2016 03:25
writing execFlowFunctionObjects immortality OpenFOAM Post-Processing 30 September 15, 2013 06:16
Pasing VolScalarField pointer to solve(), is this possible? arkangel OpenFOAM Programming & Development 0 March 15, 2011 13:07


All times are GMT -4. The time now is 07:23.