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/)
-   -   Division by zero exception - loop over scalarField (https://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,

I´m 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!

I´ve 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.

fluidflowsteel February 16, 2017 08:32

Hi guys,

X is a vol scalar field variable. I would like to take the value of X if X is greater than SMALL otherwise i will take the value SMALL if X is less than SMALL. I wrote in the following way


X = max(X, dimensionedScalar("minX", X.dimensions(), SMALL))

but getting the error in OF V3.0

/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:44:15: error: ‘Scalar’ was not declared in this scope
class pTraits<Scalar>
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:44:21: error: template argument 1 is invalid
class pTraits<Scalar>
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:101:17: error: ‘Scalar’ does not name a type
word name(const Scalar);
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:101:17: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:104:8: error: ‘Scalar’ does not name a type
inline Scalar& setComponent(Scalar& s, const direction)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:110:8: error: ‘Scalar’ does not name a type
inline Scalar component(const Scalar s, const direction)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:116:8: error: ‘Scalar’ does not name a type
inline Scalar sign(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:122:8: error: ‘Scalar’ does not name a type
inline Scalar pos(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:128:8: error: ‘Scalar’ does not name a type
inline Scalar neg(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:134:8: error: ‘Scalar’ does not name a type
inline Scalar posPart(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:140:8: error: ‘Scalar’ does not name a type
inline Scalar negPart(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:146:25: error: ‘Scalar’ does not name a type
inline bool equal(const Scalar& s1, const Scalar& s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:146:33: error: ISO C++ forbids declaration of ‘s1’ with no type [-fpermissive]
inline bool equal(const Scalar& s1, const Scalar& s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:146:43: error: ‘Scalar’ does not name a type
inline bool equal(const Scalar& s1, const Scalar& s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:146:51: error: ISO C++ forbids declaration of ‘s2’ with no type [-fpermissive]
inline bool equal(const Scalar& s1, const Scalar& s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H: In function ‘bool Foam::equal(const int&, const int&)’:
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:148:28: error: ‘ScalarVSMALL’ was not declared in this scope
return mag(s1 - s2) <= ScalarVSMALL;
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H: At global scope:
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:152:28: error: ‘Scalar’ does not name a type
inline bool notEqual(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:152:35: error: ISO C++ forbids declaration of ‘s1’ with no type [-fpermissive]
inline bool notEqual(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:152:45: error: ‘Scalar’ does not name a type
inline bool notEqual(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:152:52: error: ISO C++ forbids declaration of ‘s2’ with no type [-fpermissive]
inline bool notEqual(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H: In function ‘bool Foam::notEqual(int, int)’:
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:154:27: error: ‘ScalarVSMALL’ was not declared in this scope
return mag(s1 - s2) > ScalarVSMALL;
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H: At global scope:
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:158:8: error: ‘Scalar’ does not name a type
inline Scalar limit(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:164:8: error: ‘Scalar’ does not name a type
inline Scalar minMod(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:170:8: error: ‘Scalar’ does not name a type
inline Scalar magSqr(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:176:8: error: ‘Scalar’ does not name a type
inline Scalar sqr(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:182:8: error: ‘Scalar’ does not name a type
inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:198:8: error: ‘Scalar’ does not name a type
inline Scalar pow3(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:204:8: error: ‘Scalar’ does not name a type
inline Scalar pow4(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:210:8: error: ‘Scalar’ does not name a type
inline Scalar pow5(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:216:8: error: ‘Scalar’ does not name a type
inline Scalar pow6(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:222:8: error: ‘Scalar’ does not name a type
inline Scalar pow025(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:228:8: error: ‘Scalar’ does not name a type
inline Scalar inv(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:234:8: error: ‘Scalar’ does not name a type
inline Scalar dot(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:240:8: error: ‘Scalar’ does not name a type
inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:246:8: error: ‘Scalar’ does not name a type
inline Scalar cmptPow(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:252:8: error: ‘Scalar’ does not name a type
inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:258:8: error: ‘Scalar’ does not name a type
inline Scalar cmptMax(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:264:8: error: ‘Scalar’ does not name a type
inline Scalar cmptMin(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:270:8: error: ‘Scalar’ does not name a type
inline Scalar cmptAv(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:276:8: error: ‘Scalar’ does not name a type
inline Scalar cmptMag(const Scalar s)
^
/opt/openfoam30/src/OpenFOAM/lnInclude/Scalar.H:283:10: error: expected constructor, destructor, or type conversion before ‘(’ token
transFunc(sqrt)
^
mySolverFoam.C:128:1: error: expected ‘}’ at end of input
}
^


Can anyone help ?

With regards

gaza February 18, 2017 06:57

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)

Hi Bernhard,
My dT is a volScalarField.
I used your code but it works only for internalField cells. How to access boundaryField?


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