VVUQ · Module 2 of 10

Code Verification and Order of Accuracy

Before a simulation can be believed, the software itself must be proven correct. The method of manufactured solutions builds an exact answer to test against, and the observed order of accuracy is the verdict.

01

Readiness check

Learning objectives

  • Construct a manufactured source term for a governing equation.
  • Calculate the observed order of accuracy from grid errors.
  • Distinguish code verification, which checks that the equations are solved right, from validation against reality.
  • Justify why the grids must lie in the asymptotic range before the observed order is trusted.

This module verifies the code itself. Tick only what you can do closed-notes.

  • Differentiate a function such as sin(πx) twice.
  • Recall the order of accuracy O(hn) of a scheme.
  • Compute a logarithm ratio.
  • Recall that refining a mesh should reduce the error.
  • Substitute a known function into a differential equation.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit order of accuracy in Numerical Methods, Module 9.
3 or more weak itemsRevisit the framework in Module 1.
02

The core idea

In this module, code verification is performed using the Method of Manufactured Solutions. MMS constructs a problem with a known exact solution so that the observed numerical convergence can be compared with the formal order of the implemented method.

choose uMS(x), derive f so L(uMS) = ferror at grid h, then refineobserved order p = ln(e1/e2) / ln(r)

Code verification asks whether the program solves its governing equations as intended. The difficulty is that real problems have no known exact solution to compare against. The method of manufactured solutions (MMS) sidesteps this: you simply choose a smooth function uMS(x) to be the exact solution, substitute it into the governing operator L, and compute the source term f = L(uMS) that would make it exact. Running the code with that source and the matching boundary conditions, the numerical result should reproduce uMS to within discretization error. Refining the mesh by a ratio r, the error should fall at the scheme's theoretical order p, so a second-order method's error should drop by four when the mesh is halved. The observed order of accuracy, computed from two grids as p = ln(e1/e2)/ln(r), is the decisive comparison. If the observed order is consistent with the formal order, the test provides evidence that the implementation exhibits the expected convergence behaviour for the equations, terms, boundary conditions, and options it exercises. It does not rule out errors that do not affect the observed order or occur in untested code pathways. If the observed order falls short, the expected asymptotic behaviour has not been demonstrated, and the test setup and implementation should be diagnosed before drawing a conclusion. Candidate causes, which are not mutually exclusive, include grids outside the asymptotic range, inconsistent or lower-order boundary treatment, incomplete iterative convergence, roundoff or numerical precision effects, inadequate smoothness or coverage of the manufactured solution, and an implementation defect. This is a check on the software, entirely separate from whether the equations describe reality.

The skill works when: the observed order of accuracy matches the scheme's theoretical order.
The skill breaks down when: a coding error lowers the observed order, or too coarse a grid hides the asymptotic rate.
The concept. Plotting error against mesh size on log axes, the slope is the observed order of accuracy. Matching the theoretical order verifies the code; a shallower slope reveals a bug.
03

The skills, taught in order

Five skills build the manufactured-solution procedure and the order-of-accuracy test.

2.1 The goal of code verification

Code verification provides evidence that the implemented algorithms behave as intended for the equations, terms, boundary conditions, and options the tests exercise, independent of any physics. It does not establish that a software product is free of defects. The evidence remains applicable only to the tested implementation and configuration; tests should be repeated or reevaluated when changes could affect the numerical behaviour or code pathways they exercise. This evidence should be in place before solution verification or validation work can be interpreted.

2.2 The method of manufactured solutions

MMS chooses an arbitrary smooth function as the exact solution, then computes the source term that the governing operator requires to make it exact. The chosen function need not be physical; it only needs to exercise every term in the equations.

2.3 Deriving the source term

Substituting uMS into the operator L gives f = L(uMS) by direct differentiation. Running the code with this manufactured source and boundary conditions, the output should match uMS up to discretization error, which shrinks as the mesh refines.

Scheme orderError scales asRefine h by 2 ⇒ error
First orderO(h)halves
Second orderO(h2)quarters
Fourth orderO(h4)drops by 16

The expected error reduction on mesh refinement. The observed reduction reveals the scheme's true order.

2.4 The observed order of accuracy

From errors e1 and e2 on two grids refined by a ratio r, the observed order is p = ln(e1/e2)/ln(r). Comparing it to the theoretical order is the decisive comparison: a match is evidence for the exercised capability, and a shortfall calls for diagnosis before relying on the tested capability. Two grid levels are sufficient to calculate a pairwise observed-order estimate when the exact error is known. A rigorous verification study normally uses several systematically refined grids to determine whether the observed order stabilizes as the asymptotic range is approached.

2.5 Consistency and the asymptotic range

The observed order is only reliable in the asymptotic range, where the mesh is fine enough that the leading error term dominates. Too coarse a grid can give a misleading order, so verification uses systematically refined, fine meshes.

Engineering connection: a solver that fails its order-of-accuracy test cannot be trusted for any prediction, no matter how well its results seem to match data.

04

Worked example 1: a manufactured source term

To verify a code that solves −u″(x) = f(x), choose the manufactured solution uMS(x) = sin(πx). Derive the source term f(x) and give its value at x = 0.5.

Figure 1. The manufactured solution is chosen, not physical. Differentiating it twice gives the source term that makes it exact, which the code must then reproduce.
  1. ProblemDerive the manufactured source term for the solution in Figure 1.
  2. Given / findOperator −u″ = f, uMS(x) = sin(πx). Find f(x) and f(0.5).
  3. AssumptionsuMS is smooth and exercises the second-derivative operator.
  4. ModelSubstitute uMS into the operator: f = −uMS″.
  5. EquationsuMS = sin(πx), uMS′ = π cos(πx)uMS″ = −π² sin(πx)
  6. Solvef = −uMS″ = π² sin(πx). At x = 0.5, sin(π/2) = 1, so f(0.5) = π² = 9.87.
  7. CheckRunning the code with this f and u(0) = u(1) = 0 must return sin(πx) up to discretization error; any larger, order-reducing deviation signals a bug.
  8. ConclusionThe manufactured source is π²sin(πx), reaching 9.87 at the midpoint. MMS turns an arbitrary function into an exact benchmark for the code.
Result. f(x) = π² sin(πx), with f(0.5) = π² = 9.87.
05

Worked example 2: the observed order of accuracy

Running the verified problem, the error is e1 = 0.04 on a grid of size h and e2 = 0.01 on a grid of size h/2 (refinement ratio r = 2). Find the observed order of accuracy and judge the scheme.

Figure 2. Halving the mesh cut the error by a factor of four, giving a pairwise observed-order estimate of two. That supports the expected convergence behaviour for the capability the test exercises, and does not rule out errors that leave the order unchanged or sit in untested pathways. A shortfall would call for diagnosis rather than an immediate assumption of a defect.
  1. ProblemFind the observed order of accuracy for the errors in Figure 2.
  2. Given / finde1 = 0.04, e2 = 0.01, r = 2. Find p.
  3. AssumptionsThe grids are in the asymptotic range, so the leading error term dominates.
  4. Modelp = ln(e1/e2)/ln(r).
  5. Equationsp = ln(e1/e2) / ln(r)
  6. Solvep = ln(0.04/0.01)/ln(2) = ln(4)/ln(2) = 2.0.
  7. CheckA pairwise observed-order estimate of 2.0 is consistent with the formal order, so the test supports the expected convergence behaviour for the capability it exercises. This is one piece of code-verification evidence, not a guarantee that the implementation is free of defects. Had p come out near 1, the expected asymptotic behaviour would not have been demonstrated, and the setup and implementation would need diagnosis before any conclusion.
  8. ConclusionThis result is consistent with the formal second-order behaviour for the capability exercised by this test. Additional systematically refined grids would provide stronger evidence that the asymptotic rate has been reached.
Result. Pairwise observed-order estimate p = 2.0, consistent with a second-order scheme.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Manufactured solution must be physicalStruggling to find a realistic uMS"Does it need to be physical?"Any smooth function that exercises the terms works.
Order test on a coarse gridObserved order far from theoretical"Am I in the asymptotic range?"Refine until the leading term dominates.
Ignoring an order shortfallObserved p below theoretical"What else could lower the order?"The asymptotic behaviour has not been demonstrated. Diagnose before assuming a defect: asymptotic range, boundary treatment, iterative convergence, precision, and the smoothness and coverage of uMS.
Skipping the manufactured sourceNo exact solution to compare"Did I add f = L(uMS)?"Include the derived source term in the run.
07

Practice ladder

Level 1 · Direct skill

For −u″ = f with uMS = x2, find the source term f.

Show answer

uMS″ = 2, so f = −uMS″ = −2 (a constant source).

Level 2 · Mixed concept

Errors are 0.08 and 0.02 on grids refined by r = 2. Find the observed order.

Show answer

p = ln(0.08/0.02)/ln(2) = ln(4)/ln(2) = 2.0.

Level 3 · Independent problem

A first-order scheme should give what error at h/2 if the error at h is 0.10, and what observed order?

Show answer

First order halves the error: about 0.05 at h/2. Observed order p = ln(0.10/0.05)/ln(2) = ln(2)/ln(2) = 1.0.

Transfer task | Real engineering

You inherit a solver with no verification evidence. Describe how you would use MMS to check it before trusting any result.

What good work looks like

Choose a smooth manufactured solution, derive the source term, run on systematically refined meshes, compute the observed order of accuracy, and confirm it matches the scheme's theoretical order before any validation or production use.

08

Working with AI, and proving it yourself

Use AI as an examiner, not a solver

"Check that my manufactured source is the correct derivative of uMS."
"Give me three error pairs; I will compute the observed order of each."
"Verify my code for me." Deriving the source and computing the order is the skill.
"Is the order good enough?" Comparing to the theoretical order is the point.

Portfolio task

Apply MMS to a solver you use: choose a solution, derive the source, refine the mesh, and report the observed order of accuracy against the theoretical value.

Must include: a manufactured solution and source, an error table, and an observed order.
09

Retrieval and spaced review

Closed notes. Answer out loud, then reveal.

1. What does code verification check?

That the implementation exhibits the expected convergence behaviour for the terms, boundary conditions, and options the tests exercise. It does not rule out errors that leave the observed order unchanged, or defects in untested code pathways.

2. What is the method of manufactured solutions?

Choosing an exact solution and deriving the source term that makes it exact.

3. Must the manufactured solution be physical?

No: any smooth function that exercises the equation's terms will do.

4. Write the observed order of accuracy.

p = ln(e1/e2)/ln(r) from two refined grids.

5. Why must the grids be in the asymptotic range?

So the leading error term dominates and the order is meaningful.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-derive a manufactured source and an observed order from a blank page.
+3 daysVerify the order of three schemes from error data.
+7 daysMove from the code to the specific solution, Module 3.
+30 daysReuse MMS whenever you adopt a new solver.