CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   ForAll Loop (https://www.cfd-online.com/Forums/openfoam-programming-development/128859-forall-loop.html)

sanj January 22, 2014 20:57

ForAll Loop
 
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

Lieven January 23, 2014 02:16

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

akidess January 23, 2014 05:08

Try this:
alpha1 = max(min(alpha1,1.0), 0.0);

sanj January 23, 2014 12:29

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

akidess January 23, 2014 12:44

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.

sanj January 23, 2014 12:50

Anton,

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

Sanj


All times are GMT -4. The time now is 10:53.