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/)
-   -   Problem with passing scalars from a field to an array (https://www.cfd-online.com/Forums/openfoam-programming-development/94177-problem-passing-scalars-field-array.html)

Hisham November 8, 2011 10:28

Problem with passing scalars from a field to an array
 
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

marupio November 8, 2011 11:27

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
)


Hisham November 8, 2011 16:32

Hi David,

This solved it. Thanks a lot.

just to complete the picture:

Code:

if (abs(value-value)<tolerance)
{ // code.....}

Regards,
Hisham

marupio November 8, 2011 16:35

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"...


All times are GMT -4. The time now is 01:28.