CFD Online Logo CFD Online URL
Home > Forums > OpenFOAM Programming & Development

Problem with time comparision

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

LinkBack Thread Tools Display Modes
Old   December 3, 2016, 17:40
Default Problem with time comparision
Senior Member
Join Date: Jan 2015
Posts: 143
Rep Power: 4
Svensen is on a distinguished road
I try to identify some specific moment during a simulation. That's why I got a current time value and tried to compare it with specific time moment, e.g. 0.0006. Unfortunately the comparison doesn't work, because when currentTime == 0.0006 and stopTime_ == 0.0006, the if statement evaluates condition as FALSE. However, I know that currentTime and stopTime are equal (according to output of Info object).

The only condition which is working is "if (0.0006 == stopTime_)". If I use currentTime in condition statement, then FALSE is generated. It seems that there is some problem with currentTime variable. Yes, it has a type of scalar, which is typedef of double. Variable stopTime_ also has the same time. That's why I really don't know why condition with stopTime_ works fine, while using currentTime variable it fails.

Here is a part of my code:
    const scalar currentTime = obr_.time().value();
    scalar stopTime_;
    dict.lookup("stopTime") >> stopTime_;
    //if (currentTime == stopTime_) - fail
    //if (0.0006 == stopTime_) - OK
    if (0.0006 == currentTime) //- fail
        //if == true
        //if == false
Svensen is offline   Reply With Quote

Old   December 5, 2016, 08:43
New Member
Paul Bomke
Join Date: Mar 2010
Location: Bremen, Germany
Posts: 16
Rep Power: 9
paul b is on a distinguished road
Hi Svensen,

I think your problem is the comparison of two floating point numbers.
They might both look like 0.0006 but may indeed be 0.0005999999 and 0.0006000000. That's why you get false.

Subtract them and check if the results' absolute value is below a certain tolerance:
if( abs(0.0005999999 - 0.000600000) < 0.000001 )
paul b is offline   Reply With Quote

Old   December 5, 2016, 14:14
Senior Member
Join Date: Jan 2015
Posts: 143
Rep Power: 4
Svensen is on a distinguished road
Yes, Paul. It works fine!
Svensen is offline   Reply With Quote


openfoam-dev, programming, source code

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
Floating point exception error lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 02:50
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33
Problem with FloatingObject Leech OpenFOAM Running, Solving & CFD 10 March 29, 2012 15:24
IcoFoam parallel woes msrinath80 OpenFOAM Running, Solving & CFD 9 July 22, 2007 02:58
Could anybody help me see this error and give help liugx212 OpenFOAM Running, Solving & CFD 3 January 4, 2006 19:07

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