# Division by zero exception - loop over scalarField

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

 August 6, 2013, 18:04 Division by zero exception - loop over scalarField #1 Member   Patrick Wollny Join Date: Apr 2010 Posts: 58 Rep Power: 7 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

 August 7, 2013, 04:44 #2 Senior Member   Laurence R. McGlashan Join Date: Mar 2009 Posts: 370 Rep Power: 14 This should work: max(dT, dimensionedScalar("minDt", dimless, 1e-10); Jonathan likes this. __________________ Laurence R. McGlashan :: Website

August 7, 2013, 05:05
#3
Senior Member

Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
Quote:
 Originally Posted by l_r_mcglashan 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)

 August 7, 2013, 07:32 #4 Member   Patrick Wollny Join Date: Apr 2010 Posts: 58 Rep Power: 7 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

August 7, 2013, 16:39
#5
Senior Member

Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 14
Quote:
 Originally Posted by Bernhard 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

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 23 August 5, 2015 02:09 Tobi OpenFOAM Programming & Development 502 July 23, 2015 21:14 rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30 maka OpenFOAM Pre-Processing 2 August 27, 2007 05:10 Franny Main CFD Forum 13 July 7, 2007 15:57

All times are GMT -4. The time now is 16:51.