|
[Sponsors] |
Difference: A.internalField()[celli] and A[celli] ? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
November 29, 2012, 04:44 |
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 |
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 |
|
November 30, 2012, 09:56 |
|
#2 |
Senior Member
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,907
Rep Power: 33 |
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)
__________________
Hrvoje Jasak Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk |
|
December 3, 2012, 02:24 |
|
#3 |
Senior Member
Karl-Johan Nogenmyr
Join Date: Mar 2009
Location: Linköping
Posts: 279
Rep Power: 21 |
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 |
|
|
|