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

Difference: A.internalField()[celli] and A[celli] ?

Register Blogs Community New Posts Updated Threads Search

Like Tree12Likes
  • 12 Post By hjasak

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 29, 2012, 03:44
Default Difference: A.internalField()[celli] and A[celli] ?
  #1
Senior Member
 
Karl-Johan Nogenmyr
Join Date: Mar 2009
Location: Linköping
Posts: 279
Rep Power: 21
kalle is on a distinguished road
Hi,

When accessing cell values in a volScalarField, it seems that both A.internalField()[celli] and A[celli] does what I want. I can both read values and set values. I've seen that both practices are found in the source code. The thermo libraries usually creates scalarField references to internalField, and then use them, hence they use the first method (A.internalField()[celli]), while other classes use A[celli].

Does anyone know what the difference is? Is there any, and are there any performance related differences?

Regards,
Kalle
kalle is offline   Reply With Quote

Old   November 30, 2012, 08:56
Default
  #2
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,905
Rep Power: 33
hjasak will become famous soon enough
The difference is only some safety. The access is the same because of derivation, but there is a side-effect.

Whey you do runTime++, the field needs to decide when to safe the old-time level (if required). For this purpose, the non-const access to internalField() and boundaryField() will check if the old-time level has been updated; if not, a copy of CURRENT FIELD is stored as the old-time level before granting access.

This means:
- if you use the derivation access A[cellI], you will circumvent this mechanism and may screw up your old-time level handling (it would use the reset value)
- if you are accessing internal field of A, it is really bad to do:

forAll (cells, cellI)
{
A.internalField()[celli] = 55;
}

because this puts an if in the loop (old-time checking), which kills your performance.

Instead you should do:

scalarField& AIn = A.internalField();

forAll (AIn, cellI)
{
AIn[cellI] = 55;
}

and you are MUCH faster.

Clear?

Hrv

(P.S. Good question; I wonder how many people actually know this)
kaifu, vishwakarma, Moslem and 9 others like this.
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   December 3, 2012, 01:24
Default
  #3
Senior Member
 
Karl-Johan Nogenmyr
Join Date: Mar 2009
Location: Linköping
Posts: 279
Rep Power: 21
kalle is on a distinguished road
Dear Hrv,

thanks a lot for your answer. I did not realized there was a significant difference if you used a scalarField&, instead of directly addressing the internalField() in the loop. This will clearly have impact on my coding habits. (and likely on my code's performance :-)

Regards,
Kalle
kalle 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



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