CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Division by zero exception - loop over scalarField (http://www.cfd-online.com/Forums/openfoam-programming-development/121859-division-zero-exception-loop-over-scalarfield.html)

Pat84 August 6, 2013 18:04

Division by zero exception - loop over scalarField
 
Hiho,

Im programming a tool for the post processing in openFOAM.
In this code I divide a value by the denominator ( fvc::interpolate(T) - T_ref) which is a surfaceScalarField. To prevent the code from a division by zero, I do this:

Code:

surfaceScalarField dT = ( fvc::interpolate(T) - T_ref );
   
    forAll(dT.internalField(),i)
    {
      if(dT.internalField()[i] < 1e-10)
      {
        dT.internalField()[i] = 1e-10;
      }
    };
   
    forAll(dT.boundaryField(),patchi)
    {
      forAll(dT.boundaryField()[patchi],i)
      {
        if(dT.boundaryField()[patchi][i] < 1e-10)
        {
          dT.boundaryField()[patchi][i] = 1e-10;
        }
      };
    };

It is a very "unclean" solution which I'd like to replace. Is it possible to use only one loop over the whole surfaceScalarField (make no difference between the internal and boundary field)? And is it possible to use a pre defined value like "inf" for a value near zero?

Best regards,
Patrick

l_r_mcglashan August 7, 2013 04:44

This should work:

max(dT, dimensionedScalar("minDt", dimless, 1e-10);

Bernhard August 7, 2013 05:05

Quote:

Originally Posted by l_r_mcglashan (Post 444303)
This should work:

max(dT, dimensionedScalar("minDt", dimless, 1e-10);

I would prefer
Code:

max(dT, dimensionedScalar("minDt", dimless, SMALL);
Then SMALL depends on whether you are working with single or double precision (as defined in src/OpenFOAM/primitives/Scalar)

Pat84 August 7, 2013 07:32

Thank you very much!

Ive replaced the related part by:

Code:

dT = max(dT, dimensionedScalar("minDT", dT.dimensions(), SMALL));
This is a much "cleaner" solution :)

l_r_mcglashan August 7, 2013 16:39

Quote:

Originally Posted by Bernhard (Post 444307)
I would prefer
Code:

max(dT, dimensionedScalar("minDt", dimless, SMALL);
Then SMALL depends on whether you are working with single or double precision (as defined in src/OpenFOAM/primitives/Scalar)

In that case you could use VSMALL. Or even 0.001.

Choice of cutoff is not always so simple. I would lean towards making the function behave better towards zero or add the constant directly to the denominator of the function rather than change variables themselves.


All times are GMT -4. The time now is 08:27.