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

Problem with passing scalars from a field to an array

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 8, 2011, 11:28
Default Problem with passing scalars from a field to an array
  #1
Senior Member
 
Hisham's Avatar
 
Hisham Elsafti
Join Date: Apr 2011
Location: Braunschweig, Germany
Posts: 257
Blog Entries: 10
Rep Power: 17
Hisham is on a distinguished road
Dear Foamers,

I have this bug that I can't figure out! Three scalar fields are read from a dictionary:
Code:
// Override the Ux and Uy Gains
	OverrideFreq	
	(0.1   0.2    0.3    0.4     0.5);
	GainUx
	(5 5 6 7  8);
	GainUy
	(6 6 6 9 3);
Then values of scalars from the field are passed to a dynamic array using:

Code:
	Info << "Freq =  "<<filterFreqs_[i] << endl;
	forAll(OverrideFreq, fi)
	  {
	    Info << (OverrideFreq[fi])<< endl ;
	    if (double(OverrideFreq[fi]) == double(filterFreqs_ [i]))
	      {
		realGainUx_[i] = GainUx[fi];
		realGainUy_[i] = GainUy[fi]; 
		Info << " Condition Met : " << GainUx[fi] << "     " << GainUy[fi] << endl;
	      }
	  }     
	Info << "Real Gain Ux  =  " << realGainUx_[i] << "    Freq = " <<  filterFreqs_[i] << endl;
I get the result:
Code:
Freq =  0.1
0.1
 Condition Met : 5     6
0.2
0.3
0.4
0.5
Real Gain Ux  =  5    Freq = 0.1
Freq =  0.2
0.1
0.2
 Condition Met : 5     6
0.3
0.4
0.5
Real Gain Ux  =  5    Freq = 0.2
Freq =  0.3
0.1
0.2
0.3
0.4
0.5
Real Gain Ux  =  0    Freq = 0.3
Freq =  0.4
0.1
0.2
0.3
0.4
 Condition Met : 7     9
0.5
Real Gain Ux  =  7    Freq = 0.4
Freq =  0.5
0.1
0.2
0.3
0.4
0.5
 Condition Met : 8     3
Real Gain Ux  =  8    Freq = 0.5
For some reason, the 0.3 frequency is not recognized as a condition met!!!!
I fail to see what may be the problem!

Best regards,
Hisham
Hisham is offline   Reply With Quote

Old   November 8, 2011, 12:27
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
Info is probably formatting away the extra decimals. You are comparing double precision float numbers and you want an exact match? Try instead to take a difference:

Code:
// instead of:
if (double(OverrideFreq[fi]) == double(filterFreqs_ [i]))
// use:
if
(
    (double(OverrideFreq[fi]) - double(filterFreqs_[i]))
  < SMALL // or something even smaller - but not VSMALL
)
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   November 8, 2011, 17:32
Default
  #3
Senior Member
 
Hisham's Avatar
 
Hisham Elsafti
Join Date: Apr 2011
Location: Braunschweig, Germany
Posts: 257
Blog Entries: 10
Rep Power: 17
Hisham is on a distinguished road
Hi David,

This solved it. Thanks a lot.

just to complete the picture:

Code:
if (abs(value-value)<tolerance)
{ // code.....}
Regards,
Hisham
Hisham is offline   Reply With Quote

Old   November 8, 2011, 17:35
Default
  #4
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
Right... forgot about the absolute value. I'd suggest using OpenFOAM's built-in mag() function instead of abs()... just because I think it is more "type-safe"...
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Reply

Tags
scalar array, scalarfield


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
execFlowFunctionObjects - unknown field problem Toorop OpenFOAM Post-Processing 16 March 14, 2016 04:25
Gambit - meshing over airfoil wrapping (?) problem JFDC FLUENT 1 July 11, 2011 06:59
Turbulence dampening due to magnetic field in LES and RAS eelcovv OpenFOAM 0 June 8, 2010 12:35
Aeroacustic problem in Automotive field Gabriele Velenich Main CFD Forum 5 December 11, 2001 04:43
extremely simple problem... can you solve it properly? Mikhail Main CFD Forum 40 September 9, 1999 10:11


All times are GMT -4. The time now is 14:37.