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/)
-   -   Problem with 'operator<' (https://www.cfd-online.com/Forums/openfoam-programming-development/177668-problem-operator.html)

Jung hoo September 18, 2016 13:39

Problem with 'operator<'
 
Hello, foamers. I'm making a new boundary using ParabolicVelocity as base code.

In my equation, there is sqrt function.

for example,

Quote:

scalarField a;
scalarField b;
scalarField c = sqrt(a-b);
The resulting values seem to be reasonable.
Everything is Okay, but the calculating process is stopped in case of 'b>a'.

I think that it is because the value in the sqrt can't be negative.

So, I modified the code as below.

Quote:

scalarField a;
scalarField b;
scalarField c = a-b;

if(c<0)
{
c=0;
}
scalarField d = sqrt(c);
But I failed the compiling.

The error message is

Quote:

error: no match for 'operator<' in 'c<0'
fields/fvPatchFields/derived/cbVelocity2/cbVelocityFvPatchVectorField2.C:226:13: note: candidates are:
/home/lee/foam/foam-extend-3.1/src/foam/lnInclude/UList.C:179:6: note: bool Foam::UList<T>::operator<(const Foam::UList<T>&) const [with T = double]
I'm not goot at programming related to Openfoam yet..

Can anyone explain me how to make that 'if' function feasible?

Thanks!

A_Pete September 21, 2016 09:02

You are correct that (a-b) can't be negative when using sqrt(a-b). You could also use sqrt(max((a-b), 0)) to make sure your smalles value below the sqrt is a "0".

For your if statement you should maybe put some whitespaces in there. The problem is maybe arising, because you are trying to compare a whole field to one single scalar value. If you want to compare all the values in the field to "0" maybe go like this:

Code:

forAll(c, i)
{
    if (c[i] < 0)
    {
        c[i] = 0;
    }
}

An easier way to do this is the one that I stated above.

Jung hoo September 23, 2016 05:42

Hi, Pete. Thank you for your kind reply.

I modified the code like below

Quote:

scalarField a = asd; // 'asd' is a previously calculated scalarField in the code
scalarField b = qwe; // 'qwe' is a previously calculated scalarField in the code
scalarField vv=shields; // 'shields' is a previously calculated scalarField in the code

forAll(b, i)
{
if(a[i]<0)
{
b[i]=0;
}

else
{
b[i]=sqrt(a[i]);
}
}
On this case, there is not any problem. The compile and running application is Okay, but when I modified the code as below

Quote:

forAll(b, i)
{
if(a[i]==0)
{
b[i]=0;
}

else
{
b[i]=1/a[i];
}
}
I got an error message like this.

Quote:

time step continuity errors : sum local = 9.906e-07, global = -9.6315e-08, cumulative = -3.72647e-07
DILUPBiCG: Solving for epsilon, Initial residual = 0.603832, Final residual = 2.12043e-05, No Iterations 1
DILUPBiCG: Solving for k, Initial residual = 0.59472, Final residual = 2.44661e-05, No Iterations 1
Time = 0.00612753
3D_2D Absorption BC on patch bottom
Floating point exception (core dumped)
Is there something wrong in my code about 'follAll'?

my purpose is to avoid negative value in the sqrt, and zero value in denominator.

The 'if' function seems to be successful in sqrt, but not in denominator.

Thank you for your help!!

A_Pete September 29, 2016 07:09

Small numbers that are not exactly zero, but very small (1e-300) will also produce a floating point exception. Your if condition "a[i] == 0" is not sufficient in this case. Maybe you should do something like
Code:

if (mag(a[i]) < VSMALL)
{
bla
}

This is not really OF related, though. This is basic programming stuff.


All times are GMT -4. The time now is 04:14.