|
[Sponsors] |
May 7, 2013, 18:10 |
ForAll(List, i)
|
#1 |
Senior Member
Join Date: Nov 2012
Posts: 171
Rep Power: 14 |
Dear ALl,
Can I ask you a question about forAll()? Thank you in advance! In openfoam, there are lots of forAll used, for example (it is from hPsiMixture.C): const scalarField& hCells = h_.internalField(); const scalarField& pCells = p_.internalField(); scalarField& TCells = T_.internalField(); scalarField& psiCells = psi_.internalField(); scalarField& muCells = mu_.internalField(); scalarField& alphaCells = alpha_.internalField(); forAll(TCells, celli) { const typename MixtureType::thermoType& mixture = this->cellMixture(celli); TCells[celli] = mixture.TH(hCells[celli], TCells[celli]); psiCells[celli] = mixture.psi(pCells[celli], TCells[celli]); muCells[celli] = mixture.mu(TCells[celli]); alphaCells[celli] = mixture.alpha(TCells[celli]); } Here, why is the Tcells in forAll() used? I mean why not use other quantities like muCells or alphaCells? I read the source code src/OpenFOAM/containers/Lists/UList/UList.H, but still not got a clear understanding about it. best regards, h |
|
May 7, 2013, 18:56 |
|
#2 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
Hi hz283,
From what I can see, you can choose either one of the arrays as a reference for the cell ID, as long as you know that all of them have the same IDs. The choice for "TCells" is probably because:
For example, to give another point of view on a similar way of going through an array: in C#, it's possible to go over a complete array with a source code like this: Code:
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } C# foreach is basically the same thing as OpenFOAM's forAll. But OpenFOAM's is simpler and easier to use! Besides all of this, the other advantage is that forAll is generic enough to work in parallel, therefore it won't go through all of the cells of the global mesh in all of the processors, it only goes through the list of cells present in the current processor (at least as far as I can understand). Best regards, Bruno
__________________
|
|
May 7, 2013, 19:30 |
|
#3 | |
Senior Member
Join Date: Nov 2012
Posts: 171
Rep Power: 14 |
Dear Bruno,
Thank you so much for the continuous help! About the last point you mentioned, I indeed have some difficulty to understand. For example. I write a loop forAll() in fireFoam. C, which is the top level code we can see. In this case, forAll will be run in a parallel way for a parallel computations? Thank you ! Quote:
|
||
May 7, 2013, 19:39 |
|
#4 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
If I remember this correctly:
__________________
|
|
May 7, 2013, 19:39 |
|
#5 |
Senior Member
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23 |
forAll is simply:
Code:
#define forAll(list, i) \ for (Foam::label i=0; i<(list).size(); i++) Here's your example using the new c++11 standard (no need to switch language!): Code:
int fibarray[7] = { 0, 1, 2, 3, 5, 8, 13 }; for (const auto& i : fibarray) { cout<< i << endl; }
__________________
Laurence R. McGlashan :: Website |
|
August 29, 2019, 17:53 |
|
#6 | |
Member
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 41
Rep Power: 9 |
Quote:
I know this is an old thread, but the sake of clarity (Believe it or not there are now more than 5 questions about forAll) I will clarify somethings: 1. In the provided code snippet, the use of TCells, psiCells, hCells, pCells, muCells and alphaCells with the forAll loop would result in the exact same effect; There is no difference (they all have the same size because they are internalFields on the same mesh - I suppose). The choice of what to use is a question of "good practice": Notice that the loop is centered around TCells; It updates its values based on older ones and use them to update other variables as well. 2. forAll is just a macro to a standard for loop, and there are other similar macros. Check My post on FoamScience Website to learn more on this. |
||
|
|