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

Problem with simple if-comparison

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 7, 2009, 16:09
Default Problem with simple if-comparison
  #1
Senior Member
 
sega's Avatar
 
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20
sega is on a distinguished road
Hello World.

I'm completely confused ...
This is really embarrassing as it's about a simple if condition ...

Have a look at the code-(sniped):
Don't mind what it's actually doing, concentrate on the bold red if-condition.

Code:
int main(int argc, char *argv[])
{
    faceSet facesSet(mesh, setName);
    labelList faces(facesSet.toc());
    vectorField faceCenterCoordinates(facesSet.size());

     for (label i=startTime; i<endTime; i++)
    {

        for (label k=0; k<facesSet.size(); k++)
        {
            int faceNumber = faces[k];
            if (i == startTime)
            {
                faceCenterCoordinates[k] = mesh.Cf()[faceNumber];
            }

            if (i == startTime)
           {
               Info<< "faceCenterCoordinates[1].component(2) = " << faceCenterCoordinates[1].component(2) <<endl;
               Info<< "faceCenterCoordinates[2].component(2) = " << faceCenterCoordinates[2].component(2) <<endl;

               if ( faceCenterCoordinates[1].component(2) == faceCenterCoordinates[2].component(2) )
               {
                   Info << " Components identical !!! " << endl;
               }
            }
        }

}
Although this is looking rather cryptic its about the bold red if-condition.
It is not satisfied (meaning its Info-line "Components identical !!!" is not displayed while running the program) although the bold black italic Info-line displays the following:

Code:
faceCenterCoordinates[1].component(2) = 1.5625
faceCenterCoordinates[2].component(2) = 1.5625
So from the Info-output the if-condition should be satisfied as the two numbers (1.5625) appear to be equal, but it's not because the Info-line (Components identical !!!) in this particular if-bracket is not displayed!

So what's wrong with this?!
Is there any difference between the displayed values and the contents of the vectorField?!


In the further progress of this code I'm writing this faceCenterCoordinates vectorField to a file an it displayed 5-digit-numbers (4 digits after the dot). So there can't be any problem with this ...

Hope for your ideas ...
Sebastian
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!"
sega is offline   Reply With Quote

Old   September 7, 2009, 16:17
Default
  #2
Senior Member
 
mkraposhin's Avatar
 
Matvey Kraposhin
Join Date: Mar 2009
Location: Moscow, Russian Federation
Posts: 355
Rep Power: 21
mkraposhin is on a distinguished road
Values displayed with some tolerance (it means, that difference could be after 6 or N digits after dot)

Try to calculate and output difference between two values, or refomulate condition like inequality:

if
(
mag(faceCenterCoordinates[1].component(2) - faceCenterCoordinates[2].component(2)) < VSMALL
)
{
/// do something
}
mkraposhin is offline   Reply With Quote

Old   September 8, 2009, 08:06
Default
  #3
Senior Member
 
sega's Avatar
 
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20
sega is on a distinguished road
Yes this worked!
The output from
Code:
mag(faceCenterCoordinates[1].component(2) - faceCenterCoordinates[2].component(2))
was 2.22045e-16 in my case and I had to use SMALL instead of VSMALL to make the comparison work.

By the way: What is the difference between SMALL and VSMALL and where is it defined?
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!"
sega is offline   Reply With Quote

Old   September 8, 2009, 08:21
Default
  #4
Senior Member
 
mkraposhin's Avatar
 
Matvey Kraposhin
Join Date: Mar 2009
Location: Moscow, Russian Federation
Posts: 355
Rep Power: 21
mkraposhin is on a distinguished road
VSMALL - means Very SMALL.

For single precision:
SMALL = 1e-6, VSMALL = 1e-37

For double precision:
SMALL = 1e-15, VSMALL = 1e-300

this definitions are related with machine word length, and, i think, it's done for independence of platform type

for more information, see file scalar.H
mkraposhin is offline   Reply With Quote

Reply


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
Simple mathematical problem? Frank Wedburn Main CFD Forum 8 March 23, 2012 18:26
A simple problem. raintung FLUENT 0 March 31, 2003 08:56
simple problem Taher FLUENT 2 September 3, 2002 11:32
extremely simple problem... can you solve it properly? Mikhail Main CFD Forum 40 September 9, 1999 10:11
? Simple CFD problem Davy Main CFD Forum 0 August 21, 1998 07:19


All times are GMT -4. The time now is 10:06.