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

ForAll Loop

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

Reply
 
LinkBack Thread Tools Display Modes
Old   January 22, 2014, 21:57
Default ForAll Loop
  #1
New Member
 
Sanj M
Join Date: Dec 2013
Location: Ohio
Posts: 10
Rep Power: 3
sanj is on a distinguished road
Hello Guys,

Is there an alternative to the forAll loop? Basically I need to run the following code every time step and as a result it takes a long time. The code is:

forAll(alpha1, celli)
{
if (alpha1[celli] < 0)
{
alpha1[celli] = 0;
}
else if (alpha1[celli] > 1)
{
alpha1[celli]=1;
}
else
{
alpha1[celli]=alpha1[celli];
}
}//end for loop

This ensures that alpha1 in any cell is always between 0 and 1. I have heard about field operations to make it faster but dont know how to implement it. Can some one please shed some light on this matter.

Thanks,
Sanj
sanj is offline   Reply With Quote

Old   January 23, 2014, 03:16
Default
  #2
Senior Member
 
Lieven
Join Date: Dec 2011
Location: Mol, Belgium
Posts: 295
Rep Power: 13
Lieven will become famous soon enough
I don't think there is a lot of alternative, but you can already simplify (read 'speed up') it a little bit by removing the identity alpha1[celli]=alpha1[celli]:
Code:
forAll(alpha1, celli)
{
   if (alpha1[celli] < 0)
   {
     alpha1[celli] = 0;
   }
   if (alpha1[celli] > 1)
   {
     alpha1[celli]=1;
   }
}
Or you can write the second 'if' with 'else if'. Nevertheless, it remains an expensive operation.

Cheers,

L
Lieven is offline   Reply With Quote

Old   January 23, 2014, 06:08
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
Try this:
alpha1 = max(min(alpha1,1.0), 0.0);
__________________
*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   January 23, 2014, 13:29
Default
  #4
New Member
 
Sanj M
Join Date: Dec 2013
Location: Ohio
Posts: 10
Rep Power: 3
sanj is on a distinguished road
Dear Liven and Anton,

Thanks for your swift responses. I appreciate taking the time to help me.

Lieven: Yes thanks for the code it works perfectly but as you said it is still time expensive.

Anotn: I understand that the max and min will bound the alpha1 values. But, how will it update each cell if celli is missing? Pardon me for my poor knowledge in openfoam and c++.

Thanks,

Sanj
sanj is offline   Reply With Quote

Old   January 23, 2014, 13:44
Default
  #5
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
Foam functions are smart. Foam::min / Foam::max will see that you are passing a vol*Field and then automatically know to loop over all cells. You can completely throw out the forAll loop - just this single line will do.
__________________
*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   January 23, 2014, 13:50
Default
  #6
New Member
 
Sanj M
Join Date: Dec 2013
Location: Ohio
Posts: 10
Rep Power: 3
sanj is on a distinguished road
Anton,

Thank you so much it worked like a charm!!!

Sanj
sanj 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 with Gmsh nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 23 August 5, 2015 02:09
forAll loop getting stuck? I'm stuck too! CHARLES OpenFOAM Programming & Development 6 October 11, 2013 16:46
basic question with 'ForAll' loop Pascal_doran OpenFOAM Post-Processing 10 December 14, 2012 18:39
my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 15:57


All times are GMT -4. The time now is 11:22.