CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Numerical precision

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 12, 2010, 18:54
Default Numerical precision
  #1
Senior Member
 
Join Date: Nov 2009
Posts: 411
Rep Power: 19
DoHander is on a distinguished road
Hello,

I've recently implemented 3 small CFD solvers one in C++, one in Fortran 95/2003 and one in Matlab for comparation purposes, both compiled codes were parallelized using OpenMP.

I've noticed that for the first iterations all 3 solvers have comparable precision (about 10 digits), after a while (a few hundreds of iterations) the C++ performance was degraded.

I wonder if some of you have the same experience with numerical precision in C++.

As a note:

- Matlab works by default in double precision
- in Fortran I've used double precision (Intel Fortran )
- in C++ double (Intel C++)

Do
DoHander is offline   Reply With Quote

Old   March 12, 2010, 21:28
Default
  #2
Senior Member
 
Join Date: Nov 2009
Posts: 411
Rep Power: 19
DoHander is on a distinguished road
A simple example on a 64 bits computer

acos(-1) , exact value = pi

C/C++ using long double:
3.1415926535897932385128089594061862

Fortran 95 using selected_real_kind(32,99)
3.1415926535897932384626433832795028

arbitrary precision calculator, precision set to 36 digits:
3.14159265358979323846264338327950288

C/C++ gets about 18 exact decimals vs 15 for a simple double !!!!!
DoHander is offline   Reply With Quote

Old   February 11, 2011, 06:36
Default
  #3
Member
 
Join Date: Mar 2009
Location: Istanbul, Turkiye
Posts: 47
Rep Power: 17
gemini is on a distinguished road
Hi Do

I have a similar problem in C++ and MATLAB like yours.

I have two cfd code that do same calculation. the two codes give exactly the same results for low reynolds numbers. For high reynolds number, i.e. 10000, MATLAB code converges succesfully, but c++ code does not converge.

Do you know any way to increase the numerical precision of C++?

Thanks,

Gemini
gemini is offline   Reply With Quote

Old   February 11, 2011, 09:53
Default
  #4
Senior Member
 
Join Date: Nov 2009
Posts: 411
Rep Power: 19
DoHander is on a distinguished road
If you are sure both codes have no bugs, than you can try to play with precision in C++.
Try first to convert floats to doubles, and after that doubles to long double.

What I suspect is that your codes have different turbulence models, this will explain why for low Re numbers you have the same solution and for higher Re your results differs or one of the code does not converge.

Do
DoHander is offline   Reply With Quote

Old   February 11, 2011, 19:54
Default
  #5
Senior Member
 
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,273
Rep Power: 34
arjun will become famous soon enougharjun will become famous soon enough
Quote:
Originally Posted by DoHander View Post
A simple example on a 64 bits computer

acos(-1) , exact value = pi

C/C++ using long double:
3.1415926535897932385128089594061862

Fortran 95 using selected_real_kind(32,99)
3.1415926535897932384626433832795028

arbitrary precision calculator, precision set to 36 digits:
3.14159265358979323846264338327950288

C/C++ gets about 18 exact decimals vs 15 for a simple double !!!!!

this may be down to the algorithm used in calculating PI. It may be different in fortran and c/c++. This is why it is difficult to conclude anything about precision from this info.


Other than that i have written cfd solvers in c++ only and i have never faced problems at least in double precision.

There is one thing though, different compilers initialise vairables differently. i have noticed that if one is not careful about it then it might cause problems. (I am assuming this is not the case but i think it is worth mentioning because sometimes most obvious bugs are overlooked).
arjun is offline   Reply With Quote

Old   February 12, 2011, 15:54
Default
  #6
Senior Member
 
Martin Hegedus
Join Date: Feb 2011
Posts: 500
Rep Power: 19
Martin Hegedus is on a distinguished road
Sorry to say, but if the difference has become noticeable, and you are running in double precision, then there is probably something substantially different between the codes. For example you may be indexing incorrectly. Of there may be a bug such as overrunning an array or something hasn't been initialized correctly. Of course I'm not saying that the difference of precision isn't causing your problem. But, if it was, then you would also see noticeable differences if you were running optimized or unoptimized, the flow of the codes were different, some functions were inlined or not, etc.
Martin Hegedus is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch from single precision to double precision kk81 OpenFOAM Bugs 0 March 11, 2010 06:14
Number of elements x double precision matheusguzella ANSYS Meshing & Geometry 2 October 7, 2009 10:15
what's wrong about my code for 2d burgers equation morxio Main CFD Forum 3 April 27, 2007 10:38
numerical scheme ado Main CFD Forum 3 October 12, 2000 08:20
New Books and Numerical Software Eleuterio TORO Main CFD Forum 0 December 18, 1998 12:41


All times are GMT -4. The time now is 22:41.