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/)
-   -   exp() function not working? (https://www.cfd-online.com/Forums/openfoam-programming-development/134054-exp-function-not-working.html)

CHARLES April 23, 2014 21:10

exp() function not working?
 
Hello,

For some reason I don't think that exp() is doing anything... can you please tell me what you think is wrong?

I have the following code:
Code:

argf2 = sqr(k_)/(6.0*nu()*epsilon_);
f2 = 1-2/9*Foam::exp(-1.0*sqr(argf2));

I can tell that argf2 is doing what is supposed to.
However, when I bring up f2 in paraview it just shows up as 1 everywhere.

If I define f2 in the following way:
Code:

f2 = 99-2/9*Foam::exp(-1.0*sqr(argf2));
f2 shows up as 99 everywhere.

If I define f2 in the following way:
Code:

f2 = -2/9*Foam::exp(-1.0*sqr(argf2));
Then the field f2 shows up as 0, even if it was initialized as a different value.

I have also tried to not use "Foam::" before exp() and it doesn't make a difference.

Additionally, when I look at the f2 that was output to the time directory it looks like this:
Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.2.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "0.001";
    object      f2;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 0 0 0 0];

internalField  uniform 1;

boundaryField
{
    plate
    {
        type            calculated;
        value          uniform 1;
    }
    back
    {
        type            empty;
    }
    front
    {
        type            empty;
    }
    inlet
    {
        type            calculated;
        value          uniform 1;
    }
    outlet
    {
        type            calculated;
        value          uniform 1;
    }
    top
    {
        type            calculated;
        value          uniform 1;
    }
    symmetry
    {
        type            symmetryPlane;
    }
}


// ************************************************************************* //

when in reality it should be an internalField nonuniform List<scalar>


Thank you! :confused:

alexeym April 24, 2014 04:31

Hi,

Code:

1-2/9*Foam::exp(-1.0*sqr(argf2))
2/9 is 0 as it is integer division. So your problem is not with exp but with 2/9 that should be 2.0/9.0 (or if you'd like 2 and 9 then you can write it as 2*Foam::exp(...)/9, in this case nominator will be converted to double due to exp function)

CHARLES April 24, 2014 13:30

Thank you Alexey!

Adding the ".0" to each integer made a difference. Instead of f2 being set to 1 everywhere, it is being set to 0.777778 (i.e.: 1.0 - 2.0/9.0). OpenFOAM still doesn't recognize the
Code:

Foam::exp(-1.0*sqr(argf2))
. Even if I try to define f2 as
Code:

f2 = 1-2.0*Foam::exp(-1.0*sqr(argf2))/9.0;
When I open the f2 output in the time directory, I see that it is still shown as
Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.2.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "0.001";
    object      f2;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 0 0 0 0];

internalField  uniform 1;

boundaryField
{
    plate
    {
        type            calculated;
        value          uniform 0.777778;
    }
    back
    {
        type            empty;
    }
    front
    {
        type            empty;
    }
    inlet
    {
        type            calculated;
        value          uniform 1;
    }
    outlet
    {
        type            calculated;
        value          uniform 1;
    }
    top
    {
        type            calculated;
        value          uniform 1;
    }
    symmetry
    {
        type            symmetryPlane;
    }
}


// ************************************************************************* //

instead of the nonuniform list.

Do you think it may have something to do with the way I have initialized f2 and argf2?

Code:

    argf2
    (
        IOobject
        (
            "argf2",
            runTime_.timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_,
        dimensionedScalar("argf2", dimless, 0.0)
    ),
    f2
    (
        IOobject
        (
            "f2",
            runTime_.timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_,
        dimensionedScalar("f2", dimless, 1.0)
    )

Code:

argf2= sqr(k_)/(6.0*nu()*epsilon_);
f2 = 1-2.0/9.0*Foam::exp(-1.0*sqr(argf2));


alexeym April 24, 2014 13:56

Hi!

Well, as I don't know other values in your problem, I'll try to guess

Code:

argf2= sqr(k_)/(6.0*nu()*epsilon_);
f2 = 1-2.0/9.0*Foam::exp(-1.0*sqr(argf2));

k_ is of order of unity, epsilon_ is almost like k_, nu is around 1e-7, so argf2 should be something around 1e6. And exp(-1e12), I guess it's just 0. At the plate patch argf2 is zero, so f2 there is equal 1 - 2.0/9.0.

CHARLES April 29, 2014 13:07

Alexey,

My values for the internal field in the 0 folder are the following:
k=2.16e-3
epsilon=6.27e-4

nu=1.53e-5

which would make argf2=(2.16e-3)^2/(6*1.53e-5*6.27e-4)=81.06 (based on initial conditions)

Based on your argument, I can tell that f2 should in fact approach 0.77778!
exp(-1.0*sqr(argf2)) = 0 -> f2=1.0-2.0/9.0

Thank you.

Now the next question is why does OpenFOAM not create the nonuniform list <scalar>?
Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.2.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "0.001";
    object      f2;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 0 0 0 0];

internalField  uniform 1;

boundaryField
{
    plate
    {
        type            calculated;
        value          uniform 0.777778;
    }
    back
    {
        type            empty;
    }
    front
    {
        type            empty;
    }
    inlet
    {
        type            calculated;
        value          uniform 1;
    }
    outlet
    {
        type            calculated;
        value          uniform 1;
    }
    top
    {
        type            calculated;
        value          uniform 1;
    }
    symmetry
    {
        type            symmetryPlane;
    }
}


// ************************************************************************* //


alexeym April 29, 2014 13:24

Hi,

Quote:

Originally Posted by CHARLES (Post 488962)
...
which would make argf2=(2.16e-3)^2/(6*1.53e-5*6.27e-4)=81.06 (based on initial conditions)

Based on your argument, I can tell that f2 should in fact approach 0.77778!
exp(-1.0*sqr(argf2)) = 0 -> f2=1.0-2.0/9.0

No, it should not.

Code:

f2 = 1.0 - 2.0/9.0*exp(-sqr(argf2))
Even if argf2 == 81, exp(-6561) is equal 0, so f2 = 1.0 - 2.0/9.0*0.0 == 1.0.

Quote:

Now the next question is why does OpenFOAM not create the nonuniform list <scalar>?
Guess, cause it's uniform.

CHARLES April 29, 2014 13:50

Ooops!
My apologies, I was thinking of the value of argf2 at a solid boundary, which yields argf2=0, leading to f2=1-2.0/9.0.*1.

Thank you for your help Alexey!


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