CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   Numerical precision (http://www.cfd-online.com/Forums/main/73630-numerical-precision.html)

 DoHander March 12, 2010 19:54

Numerical precision

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 March 12, 2010 22:28

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 !!!!!

 gemini February 11, 2011 07:36

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. :confused:

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

Thanks,

Gemini

 DoHander February 11, 2011 10:53

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

 arjun February 11, 2011 20:54

Quote:
 Originally Posted by DoHander (Post 249771) 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).

 Martin Hegedus February 12, 2011 16:54

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.

 All times are GMT -4. The time now is 17:49.