Mindwerks / plate-tectonics

A fork of platec http://sourceforge.net/projects/platec/
GNU Lesser General Public License v3.0
83 stars 24 forks source link

Fix simulation consistency and memory leaks #35

Open pkdawson opened 7 years ago

pkdawson commented 7 years ago

I was getting wildly different simulation results with Visual C++, using the same seed/parameters that I was using on Linux or OS X. Most of that was caused by the fact that sizeof(long) is 4, as opposed to 8 on Clang, GCC, etc.

float kc = (seed*seed) % 256;

On this line in particular, the signed 32-bit integer would often overflow into the negatives. Making it explicitly 64-bit keeps behavior consistent across platforms.

That mostly fixes it, but I was still seeing subtle inconsistencies. This is harder to explain. For some reason, sinf yields different results when MSVC (2013 or 2015) produces a 64-bit binary. For example, with floats expressed as their raw bytes:

sinf(5.516943f) = 0xbf3184cd on GCC, Clang, and MSVC x86 sinf(5.516943f) = 0xbf3184ce on MSVC x86-64

Tried adjusting some flags, but it didn't help. Using the double version of sin and then casting back to float gets the correct results. This solution is a little ugly, but it's the only thing that worked.

psi29a commented 7 years ago

Thanks for PR, can you have a look at linthub and see about addressing those issues? As for the rest.. it seems we lost python 2.6 support.

pkdawson commented 7 years ago

I changed the C-style casts. The other linthub complaints are about lines of code which I didn't touch.

psi29a commented 7 years ago

Great, thank you. :)

pkdawson commented 7 years ago

Found a very similar precision issue with sin and cos on Apple's version of Clang. I could make the code a little nicer by creating wrapper functions instead of macros, but I'm pretty sure that doing the calculation at double precision is the best way to fix this problem after all.