CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > 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 Display Modes
Old   August 6, 2013, 18:04
Default Division by zero exception - loop over scalarField
  #1
Member
 
Patrick Wollny
Join Date: Apr 2010
Posts: 58
Rep Power: 7
Pat84 is on a distinguished road
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
Pat84 is online now   Reply With Quote

Old   August 7, 2013, 04:44
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 14
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, 05:05
Default
  #3
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
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, 07:32
Default
  #4
Member
 
Patrick Wollny
Join Date: Apr 2010
Posts: 58
Rep Power: 7
Pat84 is on a distinguished road
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
Pat84 is online now   Reply With Quote

Old   August 7, 2013, 16:39
Default
  #5
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 14
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

Reply

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
libOpenSMOKE Tobi OpenFOAM Programming & Development 496 July 3, 2015 02:41
Problem with Gmsh nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 18 April 22, 2015 08:43
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 05:10
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 15:57


All times are GMT -4. The time now is 05:45.