CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   if-loop, volScalarField comparison (http://www.cfd-online.com/Forums/openfoam/75210-if-loop-volscalarfield-comparison.html)

volker April 19, 2010 11:29

if-loop, volScalarField comparison
 
Hi all,

I have a problem with a task, which seemed to be straight-forward to solve at first.
I need to program an if-loop in order to compare one volScalarField (P1) to another one (P2), and for each grid point, where P1 >= P2, a certain value (d) has to be written to another volScalarField (N) at exactly the same grid point.
I wrote something like:
(a, b, c, d are placeholder for arbitrary volScalarFields)
Code:

volScalarField P1 = a;
volScalarField P2 = b;
volScalarField N = c;

    if (P1 >= P2)
    {
    N = d;
    }
    else
    {
    N = -d;
    }

The problem is that the code in the actual version does not compare the values for every single grid point and hence does not write the value (d) to (N) for all the grid points where the logical expression is true (P1 >= P2).
It's more that it just compares just one (the first?!) point of P1 with P2.
Right now, I don't have any idea how to get rid of that problem, though it seems to be a very silly one.
Thanks for any idea.

greets, volker

alberto April 22, 2010 00:30

Hi,

you have to loop over your cells to compare information in each point:

Code:

forAll(P1, cellI)
{
  if (P1[cellI] > P2[cellI])
  {
    N[cellI] = ...
  }
  else
  {
    ...
  }
}

Best,

volker April 22, 2010 03:42

hi alberto,

so many thanks for your help!!! It worked great.
I wasn't aware of that class 'cell'.

Do you know if there is a way to loop over the boundary patches as well?

best
volker

alberto April 22, 2010 10:11

Quote:

Originally Posted by volker (Post 255814)
hi alberto,
so many thanks for your help!!! It worked great.
I wasn't aware of that class 'cell'.

It's not a class, it's just an index created locally by the forAll loop :D

Quote:

Do you know if there is a way to loop over the boundary patches as well?
Yes, let's assume you want to loop over the boundaries of a field p:

Code:

forAll(p.boundaryField(), patchi)
    {
        // Check if it's zeroGradient (optional, depending on your case)
        if (isA<zeroGradientFvPatchScalarField>(p.boundaryField()[patchi]))
        {
            otherFieldA.boundaryField()[patchi] = 0.0;
            otherFieldB.boundaryField()[patchi] = 0.0;
        }
    }

Best,

volker April 23, 2010 13:02

perfect, big thanks to you!
:)

alberto April 23, 2010 14:18

You're welcome :D


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