CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Xcoordinate of cell centre in volScalarField (https://www.cfd-online.com/Forums/openfoam-solving/59560-xcoordinate-cell-centre-volscalarfield.html)

pda July 18, 2007 07:58

Hello, I would like to hav
 
Hello,

I would like to have a volScalarField variable which contains the x-position of each cell centre. After looking around for hours, I don't know how to do this. I tried with cellCentres and others, but I cannot find the commands I am looking for.

Any help is greatly appreciated,

PJ

lr103476 July 18, 2007 08:09

the following should work:
 
the following should work:

const volScalarField& xPos = mesh.C().component(vector::X)

Frank

hjasak July 18, 2007 08:19

Close (the above was a memory
 
Close (the above was a memory violation):


volScalarField xPos = mesh.C().component(vector::X);

Hrv

lr103476 July 18, 2007 08:32

Hi Hrv, I thought: 1) A
 
Hi Hrv,

I thought:

1) A reference &, since you don't want a real copy of the cell centres, only the right to have a look at it.

2) A const, since you're not allowed to change the value of the cell centres.

Why memory violation?

Regards, Frank

hjasak July 18, 2007 08:42

All you said is correct, but y
 
All you said is correct, but you cannot decide on the return type: the function does that for you. Function signature says:


//- Return a component of the field
tmp<geometricfield<cmpttype,> > component
(
const direction
) const;


In other words, the function will return a tmp<volscalarfield>. If you take a reference and not a copy, nobody is holding the original object and tmp will delete the memory in its destructor. You are now left holding something called a reference to a temporary, ie. looking at a piece of memory that has been deleted and is waiting for re-use. If somebody else uses that memory, you get garbage: memory violation.

Clear?

Hrv

lr103476 July 18, 2007 08:52

Perfectly clear, but one more
 
Perfectly clear, but one more thing.... I just remembered a piece of code which you showed at the lunch training in Zagreb (with const and a ref &):

const volVectorField& centres = mesh.C();
point origin(0.5,0.5,0.05);
vector axis(0,0,-1);
U.internalField() = axis ^ (centres.internalField() - origin);

What is the difference? Is it the component function?

Frank

hjasak July 18, 2007 08:57

Easy. Have a look in the libr
 
Easy. Have a look in the library and find out what the return type is for a function mesh.C(). The file is fvMesh.H and the function signature is:


//- Return cell centres as volVectorField
const volVectorField& C() const;


As you can see, this one returns a const volVectorField& and I can accept it as such. In the other, I have no choice - I have to take a copy.

Clear?

Hrv

lr103476 July 18, 2007 09:01

thanks, totally clear! Fran
 
thanks, totally clear!

Frank


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