8

I'm currently writing a lid drive cavity CFD code on python. Currently, my code has some issues (values jumping bear b.c). I was wondering what are some good habits in debugging numerical codes. Hopefully, I would be able to learn some which I may apply to my future projects.

Daniel Shapero
  • 10,263
  • 1
  • 28
  • 59
Kelvin Loh
  • 123
  • 3
  • 12
    Write unit tests. Verify each piece of code before writing anything new. Devise simple tests that will clearly tell you if your code is buggy. And above all else, make sure you understand to a reasonable degree the mathematics of whatever you are trying to implement before implementing it. – coolguy1000000 Jul 29 '19 at 13:40
  • 4
    Use assert. Compile with -Wall -Wextra -Werror -Wpedantic. Test with -fallocate=sanitize. Test on cases with known analytical solutions. – Richard Jul 29 '19 at 15:49
  • Have a way to compute backwards error pointwise. – user14717 Jul 30 '19 at 18:00
  • @Richard what's that -fallocate=sanitize flag? Do you mean to suggest he can use e.g. -fsanitize=address? – Bjoern Dahlgren Aug 01 '19 at 11:04
  • @BjoernDahlgren: Shoot. It's -faddress=sanitize. That flag tracks memory allocations and usage. It identifies buffer overflows, bad writes, and unfreed memory at a cost of slowing a program by half. It's therefore a great tool to use for testing, especially in pointer-heavy code. – Richard Aug 01 '19 at 16:40
  • @Richard indeed, it has replaced valgrind in my workflow. When using it for python extensions (CFLAGS="-fsanitize=address" python3 setup.py build_ext -i), I need to add the asan so (given by ~ ldd mypyext/_mypyext.so) to LD_PRELOAD, not terribly hard but definitely also not very user friendly either ¯_(ツ)_/¯. – Bjoern Dahlgren Aug 03 '19 at 11:37
  • 1
    Thanks all! I started to unit test components of my code and was able to verify my code and debug more efficiently. However, due to not having actual analytical data for "assert", I just have to use the best of my effort to make the code clean. – Kelvin Loh Aug 05 '19 at 15:31

0 Answers0