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

Odd problem with reference to cell centres

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By akidess

Reply
 
LinkBack Thread Tools Display Modes
Old   July 16, 2012, 07:38
Default Odd problem with reference to cell centres
  #1
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
Hello all, suppose I have the following code

Code:
volScalarField ypos = mesh.C().component(1);
volScalarField xpos = mesh.C().component(0);

Info << ypos.internalField() << endl;
Info << xpos.internalField() << endl;
This will print out all the x and y coordinates of the cell centres in my mesh. I thought I could be smart and use references, i. e.

Code:
const volScalarField& ypos = mesh.C().component(1);
const volScalarField& xpos = mesh.C().component(0);

Info << ypos.internalField() << endl;
Info << xpos.internalField() << endl;
But now I get the same field printed twice (always the field for which I get the reference last, i.e. X in this example). Can anyone explain what's happening here?
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Help define the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oam-technology
akidess is offline   Reply With Quote

Old   July 16, 2012, 10:02
Default
  #2
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,301
Blog Entries: 34
Rep Power: 84
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
Hi Anton,

Possibly because the component method is giving a result as its own variable, not as a reference to the real variable. And the reference value is pointing to the variable space of the method itself.

Although, if I found the correct method (source):
Code:
00062 template<class Form, class Cmpt, int nCmpt>
00063 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
00064 (
00065     const direction d
00066 ) const
00067 {
00068 #   ifdef FULLDEBUG
00069     if (d >= nCmpt)
00070     {
00071         FatalErrorIn
00072         (
00073             "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
00074         )   << "index out of range"
00075             << abort(FatalError);
00076     }
00077 #   endif
00078 
00079     return v_[d];
00080 }
It does seem a bit odd that this didn't work as intended...

Which Gcc version are you using? It might influence this result you're getting, since this method is part of a template.

Best regards,
Bruno
wyldckat is offline   Reply With Quote

Old   July 16, 2012, 10:32
Default
  #3
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
Thanks for digging in the code for me, I should have done that myself as a first step. The method returns "const Cmpt& v_[d];", which is part of the array "Cmpt v_[nCmpt];". I think it's not possible to reference an element within an array, I should have used a pointer. But I'll investigate further and report back.

I'm using gcc 4.4.6.
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Help define the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oam-technology
akidess is offline   Reply With Quote

Old   July 16, 2012, 11:26
Default
  #4
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
Ok, so I still do not fully understand whats happening with the references, but I do now see that I was doing something very stupid

mesh.C().component(N) gives back a tmp field, which is already kind of a reference. I then forced unpacking of the tmp field by using () and was then able to copy over the reference. Of course this doesn't make sense.

I conclude that finally the original formulation without any explicit reference/pointer-handling was already the optimal solution.
wyldckat likes this.
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
*Help define the OpenFOAM stackexchange Q&A site: http://area51.stackexchange.com/prop...oam-technology
akidess is offline   Reply With Quote

Old   July 16, 2012, 18:36
Default
  #5
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 8,301
Blog Entries: 34
Rep Power: 84
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
I did think about "tmp" being the culprit here, but since I couldn't find it in a quick search via Doxygen, I simply didn't have the proof to point the finger
wyldckat is offline   Reply With Quote

Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem / Misunderstanding with mesh.C() : get the cell centers Edy OpenFOAM Programming & Development 17 May 7, 2015 16:42
OpenFOAM 1.6-ext git installation on Ubuntu 11.10 x64 Attesz OpenFOAM Installation 45 January 13, 2012 13:38
Problem with reference area alfaben STAR-CCM+ 2 September 17, 2011 03:46
mass flow in is not equal to mass flow out saii CFX 2 September 18, 2009 08:07
G95 + CGNS Bruno Main CFD Forum 1 January 30, 2007 01:34


All times are GMT -4. The time now is 13:02.