Is my code correct?

May 27, 2016
categories: tools, programming, research

In computational physics we use simulations to understand experiments, and try to shed some light on the underlying processes. So how do we know if the code we're using is correct, and producing sensible results? The first way is to eyeball the results and see if they make sense and don't appear to be gibberish. This isn't very systematic or reassuring though, and we may still have some sleepless nights worrying that our code is broken in some subtle way. A better method is to compare the results against a known solution, in a case where the answer is already known. This still leaves a loophole though: Even if you know the code is working in cases where you already know the answer, how do you know it still works in more complicated cases, where you don't already know the answer? After all, the only reason to use the code is to find answers you don't already know. The Method of Manufactured Solutions solves this problem by choosing the solution, and modifying your code in ways which can be calculated exactly, such that the modified code should produce this chosen solution. This allows quite complicated, realistic problems to be tested systematically. In a recent paper, we have applied this method to testing the BOUT++ framework. This has involved nearly a year's worth of frustration to get everything working and fix some subtle (but thankfully not critical) bugs, but I will sleep a little better from now on.