CFD Online URL
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

ForAll(List, i)

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

Like Tree9Likes
  • 1 Post By hz283
  • 5 Post By wyldckat
  • 3 Post By l_r_mcglashan

Reply
 
LinkBack Thread Tools Display Modes
Old   May 7, 2013, 18:10
Default ForAll(List, i)
  #1
Senior Member
 
Join Date: Nov 2012
Posts: 168
Rep Power: 4
hz283 is on a distinguished road
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
mgg likes this.
hz283 is offline   Reply With Quote

Old   May 7, 2013, 18:56
Default
  #2
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 7,511
Blog Entries: 33
Rep Power: 74
wyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the rough
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:
  1. It's the shortest name.
  2. It's the first array of all 4 being used.
  3. It's the one more likely to remain unchanged.
"forAll" is a very quick and simple way of iterating over all items of a list. You can read it as: "for all items in TCells, look at each one as celli".


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);
}
In this case, it reads like: "for each item 'i' (of type int) of the array 'fibarray', do..."

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
wyldckat is offline   Reply With Quote

Old   May 7, 2013, 19:30
Question
  #3
Senior Member
 
Join Date: Nov 2012
Posts: 168
Rep Power: 4
hz283 is on a distinguished road
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:
Originally Posted by wyldckat View Post
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:
  1. It's the shortest name.
  2. It's the first array of all 4 being used.
  3. It's the one more likely to remain unchanged.
"forAll" is a very quick and simple way of iterating over all items of a list. You can read it as: "for all items in TCells, look at each one as celli".


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);
}
In this case, it reads like: "for each item 'i' (of type int) of the array 'fibarray', do..."

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
hz283 is offline   Reply With Quote

Old   May 7, 2013, 19:39
Default
  #4
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 7,511
Blog Entries: 33
Rep Power: 74
wyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the roughwyldckat is a jewel in the rough
If I remember this correctly:
  1. When running in parallel, the mesh is already decomposed.
  2. Therefore, each processor only has access to its own sub-domain of items in the array.
  3. This way, forAll will only go through the list of items on the sub-domain, which enables the ability to run in parallel.
You'll need to study a few more source code files from OpenFOAM's own applications (solvers and utilities) to confirm this. The folder "applications/test" also has some nice hints on what can be done with arrays and so on!
wyldckat is offline   Reply With Quote

Old   May 7, 2013, 19:39
Default
  #5
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 13
l_r_mcglashan will become famous soon enough
forAll is simply:

Code:
#define forAll(list, i) \
    for (Foam::label i=0; i<(list).size(); i++)
It does not do anything special. You can find other convenience macros at the bottom of src/OpenFOAM/containers/Lists/UList/UList.H

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;
}
wyldckat, sharonyue and hz283 like this.
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan 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



All times are GMT -4. The time now is 00:15.