Code Verification
Theory: showing correctness in numbers
Overview
I've been asked to demonstrate the quality of an in-house analysis program. Isn't it enough that examples give plausible results?
It isn't. Small errors shift results only slightly, so they're almost never visible. Code verification checks, on problems with known answers, that error shrinks to round-off level or falls at the theoretical order under refinement. For finite elements, the basic check is the patch test: can the elements reproduce a solution they should represent, even when distorted? The key is that pass/fail comes out clearly in numbers.
Test problems
Both are exact solutions of Laplace's equation. The linear field is represented exactly by linear triangles, suiting the patch test; the exponential–sine product serves the order test.
Element stiffness and error measures
$A^e$ is triangle area and $B$ the shape-function gradient matrix. Look at maximum nodal error and the order $p$ when spacing is halved.
The birth of the patch test
The patch test was devised in the mid-1960s by Irons and colleagues while developing plate-bending elements. Some elements ran fine yet didn't approach the correct answer under refinement. Irons proposed applying a simple state the elements should represent (such as uniform strain) to a small 'patch' of elements and judging correctness by whether it was reproduced exactly. Strang and others later clarified its mathematical meaning, and it remains the first test for new elements and in-house programs.
Worked example: a small finite element program
Example 1: patch test
A unit square meshed 6×6 with linear triangles had interior nodes shifted by up to 30% of spacing. Imposing 1 + 2x + 3y on the boundary and solving gave a maximum nodal error of 1.8×10⁻¹⁵ — round-off level — evidence of correct implementation.
Example 2: order test and catching a bug
With the exponential–sine solution under refinement, the correct implementation was compared with one planted with an error halving the area of elements in x > 0.5:
| Divisions | Correct error | Order | Faulty error | Order |
|---|---|---|---|---|
| 4 | 5.8×10⁻⁴ | — | 8.8×10⁻² | — |
| 8 | 1.6×10⁻⁴ | 1.86 | 1.0×10⁻¹ | −0.21 |
| 16 | 4.1×10⁻⁵ | 1.98 | 1.0×10⁻¹ | 0.01 |
| 32 | 1.0×10⁻⁵ | 2.00 | 9.9×10⁻² | 0.02 |
The faulty implementation's error doesn't shrink at all with refinement.
Right. Halving the area doubles stiffness in those elements — a shift in the equations themselves that refinement can't cure, hence order 0. The same error also sent patch-test error up to 0.23. Conversely, passing the patch and order tests lets you say in numbers that at least this kind of error is absent. The key is automating these tests and running them on every change — passing once doesn't prevent later edits from breaking things.
Code verification workflow
- Confirm element basics with patch tests (uniform strain, linear temperature, etc.).
- On problems with exact solutions, check the theoretical order appears under refinement.
- Check equations or terms without exact solutions using manufactured solutions.
- Prepare tests for each boundary-condition type, element type and nonlinear setting.
- Automate tests, run them on every change and record results.
“New version shifted results 0.3%”
A new version of an in-house structural program, run on the same model, gave stresses differing by just 0.3%. It was nearly dismissed as within tolerance, but one automated patch test failed. Investigation found that element integration, rewritten for speed, swapped weights only on distorted elements. 0.3% may look small, but an error that fails the patch test won't vanish with refinement. A test with a numerical pass/fail caught it before release.
Common mistakes
Mistakes and fixes
| Mistake | Effect | Fix |
|---|---|---|
| Judging by plausible results | Small errors missed | Numerical pass/fail |
| Testing only regular meshes | Distorted-element bugs remain | Distorted patches |
| Single-grid comparison | Order drop missed | Refine and compute order |
| Not testing boundary conditions | Boundary implementation errors | Test each type |
| One-off testing | Later changes break things | Automate and run every time |
I'd like to learn related topics.
Try orders with the Richardson extrapolation tool. Related pages include the method of manufactured solutions, solution verification, Roache's GCI, ASME V&V 10 and NAFEMS LE1.
Related Topics
Experience the theory firsthand with the interactive simulator for this field
All Simulators