CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Division by zero exception - loop over scalarField

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By l_r_mcglashan

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 6, 2013, 19:04
Default Division by zero exception - loop over scalarField
  #1
Member
 
Patrick Wollny
Join Date: Apr 2010
Posts: 58
Rep Power: 16
Pat84 is on a distinguished road
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
Pat84 is offline   Reply With Quote

Old   August 7, 2013, 05:44
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
This should work:

max(dT, dimensionedScalar("minDt", dimless, 1e-10);
Jonathan likes this.
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   August 7, 2013, 06:05
Default
  #3
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Quote:
Originally Posted by l_r_mcglashan View Post
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)
Bernhard is offline   Reply With Quote

Old   August 7, 2013, 08:32
Default
  #4
Member
 
Patrick Wollny
Join Date: Apr 2010
Posts: 58
Rep Power: 16
Pat84 is on a distinguished road
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
Pat84 is offline   Reply With Quote

Old   August 7, 2013, 17:39
Default
  #5
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
Quote:
Originally Posted by Bernhard View Post
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.
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   February 16, 2017, 08:32
Default
  #6
New Member
 
fluidflowsteel
Join Date: Jun 2016
Posts: 21
Rep Power: 10
fluidflowsteel is on a distinguished road
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
fluidflowsteel is offline   Reply With Quote

Old   February 18, 2017, 06:57
Default
  #7
Senior Member
 
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 16
gaza is on a distinguished road
Quote:
Originally Posted by Bernhard View Post
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?
__________________
best regards
pblasiak
gaza is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[openSmoke] libOpenSMOKE Tobi OpenFOAM Community Contributions 562 January 25, 2023 10:21
[Gmsh] Problem with Gmsh nishant_hull OpenFOAM Meshing & Mesh Conversion 23 August 5, 2015 03:09
[CAD formats] my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30
ScalarField division maka OpenFOAM Pre-Processing 2 August 27, 2007 06:10
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 16:57


All times are GMT -4. The time now is 21:15.