|
[Sponsors] |
March 12, 2010, 18:54 |
Numerical precision
|
#1 |
Senior Member
Join Date: Nov 2009
Posts: 411
Rep Power: 19 |
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 |
|
March 12, 2010, 21:28 |
|
#2 |
Senior Member
Join Date: Nov 2009
Posts: 411
Rep Power: 19 |
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 !!!!! |
|
February 11, 2011, 06:36 |
|
#3 |
Member
Join Date: Mar 2009
Location: Istanbul, Turkiye
Posts: 47
Rep Power: 17 |
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 |
|
February 11, 2011, 09:53 |
|
#4 |
Senior Member
Join Date: Nov 2009
Posts: 411
Rep Power: 19 |
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 |
|
February 11, 2011, 19:54 |
|
#5 | |
Senior Member
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,273
Rep Power: 34 |
Quote:
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). |
||
February 12, 2011, 15:54 |
|
#6 |
Senior Member
Martin Hegedus
Join Date: Feb 2011
Posts: 500
Rep Power: 19 |
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.
|
|
Thread Tools | Search this Thread |
Display Modes | |
|
|
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 |