6
$\begingroup$

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.

$\endgroup$
  • 11
    $\begingroup$ 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. $\endgroup$ – sssssssssssss Jul 29 at 13:40
  • 4
    $\begingroup$ Use assert. Compile with -Wall -Wextra -Werror -Wpedantic. Test with -fallocate=sanitize. Test on cases with known analytical solutions. $\endgroup$ – Richard Jul 29 at 15:49
  • $\begingroup$ Have a way to compute backwards error pointwise. $\endgroup$ – user14717 Jul 30 at 18:00
  • $\begingroup$ @Richard what's that -fallocate=sanitize flag? Do you mean to suggest he can use e.g. -fsanitize=address? $\endgroup$ – Bjoern Dahlgren Aug 1 at 11:04
  • $\begingroup$ @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. $\endgroup$ – Richard Aug 1 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.