A few days ago I published a post on software unit testing in MATLAB. I’d figure I’d share more perspective on what I have learned so far, in terms of strategy and tactics for software testing, especially as it pertains to scientific and engineering applications. For reference, this presentation, this presentation, and this paper have been informative. I also direct the reader’s attention to this paper in PLOS Biology from January 2014, titled “Best Practices for Scientific Computing“.

# The Tougher Path

You see friends, we scientists and engineers have it tougher than the average programmer. With typical programming, the inputs and outputs to a function are well defined. Expected outputs are known well in advance. Heck, there is even a software engineering paradigm called “test-driven development“, where the actual unit tests for the code are written before the actual code is written! We also are not exposed to much software engineering methods in school, and most of us just “picked up” programming and tried to roll with the punches. I would complain we are also babied too much with MATLAB, and that engineering schools would better serve their students by switching to teaching Python and its scientific computing add-ons.

In complex scientific and engineering applications, it can be very difficult to know what your program is supposed to be spitting out, without becoming bogged down by huge numbers of over-specific test cases. We also encounter small (but unavoidable) errors due to the numerical precision of our machine. Solution algorithms also have a given amount of error attached to their solution (for example, Runge-Kutta integration possesses error ). Sometimes, we even use a stochastic algorithm to solve a problem, making the results, well… *random*! How do you debug an algorithm with random inputs? These hurdles make it difficult to know if a program is operating correctly but is simply overcome with numerical imprecision, or is genuinely bugged.

# More Ideas on Unit Testing Scientific and Engineering Simulation Codes: The example of a mixing point

As an example, consider a mixing point that is mixing two streams of the same material at two different temperatures and two different mass flowrates, and the assumption of constant specific heats **cannot** be made. What is the outlet temperature? To solve this problem, a nonlinear algebraic equation needs to be solved for :

Where is a heat capacity polynomial.

From what we know about how heat transfer works, there is an upper and lower bound on what can be. The lower bound is , and the upper bound is . Bracketing the root between these bounds is guaranteed to find the root, e.g. using the regula falsi method. The procedure to solve this problem is fairly straightforward: use fzero() in MATLAB and bracket the root.

But let’s think a little further. What should happen if we alter the two mass flowrates, and ? Logically, we should observe the following asymptotic behavior:

- As tends to infinity, should asymptotically approach .
- As tends to infinity, should asymptotically approach .
- If , exactly.
- If , exactly.
- If , over small temperature differences, (if we had constant specific heats, this equation would be exact).
- The choice of should not change the results of cases 1-5 whatsoever.

These 6 items represent 6 test cases that a code would need to pass. Passing all these tests wouldn’t prove the code right, but failing any of these tests would prove the code wrong. Furthermore, the code would need to be dummy-proofed to ensure and . It would also need to be dummy-proofed to accept the correct range of temperatures over which the expression has good accuracy.

# Some Questions for Yourself

- How might you generalize the solution of the problem for the case of inlet streams, with specific temperatures and mass flowrates?
- What should the bracketing bounds be? What should the asymptotic behavior be with regards to the inputs?
- How should we handle the case of
**all**flowrates being equal to zero? - Now let’s make the problem even more complicated: What if we have different substances being mixed, each with a different heat capacity? What are the new bounds on ? Do they change?

# More Ideas on Testing Scientific Codes

Unfortunately, the testing of scientific and engineering computer codes is something of an art, and as such, I can only brainstorm general advice. The details of your specific code and specific problem will probably generate the bulk of your unit test cases. Again, passing your test cases doesn’t really prove your code is working correctly – but failing them proves it is working incorrectly.

- Should something ALWAYS be equal to something else?
- Should a particular input be INVARIANT with certain output? In the mixing point example above, should be totally invariant with .
- Think up a simple approximation to your problem. Does your code (which solves the exact problem) come “close” to the approximation, or are you way off? See point 5 in the mixing point example above.
- Do you have access to experimental data? Can you reproduce it to within experimental uncertainty?
- If you are testing an optimization algorithm, can your algorithm identify the global optima for the numerous toy problems encountered in the literature?
- What
**should**happen as one of your inputs grows extremely large or extremely small? What is the asymptotic behavior of the inputs to your code? Does it line up with physical reality, reason, and logic? - Are there upper and lower bounds?
- Does your code produce physically unreasonable behavior, e.g. does it violate the 1st or 2nd laws of thermodynamics, or does not balance momentum?
- Is mass and energy always conserved in the system you are simulating, to within numerical accuracy?
- What if your user is a bonehead, and enters zero in for ALL the inputs?
- Can the user enter inputs that violate the assumptions of your model? For example, if you are solving a laminar flow problem, can the user insert inputs that make the Reynolds number unreasonably high?
- Does a closed-form solution exist for a particular case? Do your numbers match the closed-form solution?