Measuring error
RMSE, relative L2, R² and max error: which number to report, and what each one hides.
Outcome: Compute MAE, RMSE, max error and R²; compute the relative L2 error per design, a common metric of field-surrogate papers; read a benchmark table, and check integrated quantities as well as fields; report errors in each case’s reference units, against a reference that needs no training, with the median and the seed spread.
Builds on Train, validation and test
A metric compares predictions with solver values over the test runs (and, for fields, over all their points). Each answers a different question, and hides something.
The simplest field model is the mean of the training fields, returned for every design whatever its inputs. On the 8 validation runs of the aircraft, Example 3-3 scores it 0.946, yet its relative L2 error is 21 % and its lift is off by 0.27 on average.
- MAE, mean absolute error: the typical size of an error, in the units of the output.
- RMSE, root mean squared error: like MAE but weights large errors more. The square root of Equation 3-1, the loss of fitting.
- Max error: the single worst point. For fields it usually sits where the field changes fastest, such as a suction peak.
- : , one minus the squared error over the spread of the data around its mean : the share of the variation the model explains. 1 is perfect, 0 is no better than predicting the mean, and on held-out runs it can go negative. It is easily high when the data vary a lot.
Relative L2, a common metric of field papers
Many field-surrogate papers report the relative L2 error.
DefinitionRelative L2 error
It makes designs with large and small fields comparable. If the field is close to zero, though, the denominator is small and the error explodes. It also depends on what is predicted: the same model scores differently on and on pressure in pascals, where the large free-stream value inflates .
VRMSE, the headline metric of The Well, a public collection of physics simulations, is the RMSE divided by the standard deviation of the true field, so 1.0 means no better than predicting the mean.
Area weighting
A mesh refined near the wall or at the leading edge puts many nodes there. A plain average over nodes then weights those regions heavily, and the far field barely counts. The average used should be stated; on the same model the two can differ noticeably.
For the error per square metre of surface (or cubic metre of volume), each point is weighted by the area or volume it stands for: the cell areas the solver already uses to integrate forces.
The weighted sums are a quadrature of the integrals they stand for (a Riemann sum), so they converge as the mesh is refined. A plain average over nodes converges instead to a mean weighted by the mesher’s point density.
Errors in context
Against the spread of the data. An RMSE means little alone: read against the standard deviation of the target, as in VRMSE, it gives the typical error as a share of the variation.
In each case’s own units. When test cases differ in speed or size, an error in SI units averaged over them is set mostly by the fastest or largest. Each case’s error is better computed in its own reference units, as for (physical fields): velocity divided by the case’s free-stream speed, pressure by its dynamic pressure. Unlike the relative L2 error, this does not explode where the true field is small, since the divisor is a known input of the case.
Against a trivial reference. Weather forecasters score a forecast against one that needs no skill, such as persistence or climatology, as a skill score: one minus the forecast’s mean squared error over the reference’s (Murphy 1988). is the skill score whose reference is the mean of the data. For a flow, a reference that needs no training is the free stream predicted everywhere. A model whose error is near that reference’s shows no skill, however small the number looks.
Over cases and seeds. A few hard cases can set a mean, so the median belongs beside it, and so does the spread over training seeds: one run per model cannot separate two models whose errors differ by less than that spread.
On the integrated quantities. Lift and drag computed from the predicted field are compared with the solver’s. Small field errors that push the same way add up: a error of 0.02 over the whole upper surface of a wing shifts by about 0.02, while a uniform offset over a closed surface cancels exactly. Drag is a small difference of large pressure forces. Drag is often quoted in counts (one count is ), as a mean absolute error and a maximum.
As a ranking. To pick among designs, the right order often matters more than exact values; a limit on stress or temperature needs the value itself. The Spearman rank correlation between predicted and true drag (1 = same order) checks that; AirfRANS scores it. Small field errors can still scramble the order when designs differ by little.
Against the solver’s own error. For an absolute value, a 1 % surrogate error matters little next to a 5 % solver error against the tunnel, but it can decide a comparison of designs that differ by 1 % (see is the training data right?).
By region. An average can hide a large error where the design is decided; a map of the error over the geometry shows it. On the site’s surrogate the largest errors sit at the leading-edge suction peak and the wing tips (see testing on unseen designs).
Example 3-3 adds two functions to aircraft.py: lift, Equation 2-2 on the aircraft’s own points, and scores, every metric of this lesson for a predicted field. It then scores the mean-field baseline.
Example 3-3. Point, field, area-weighted, lift and ranking metrics, with NumPy, SciPy and scikit-learn
# Add to aircraft.py, with its other imports at the top:
from scipy.stats import spearmanr
from sklearn.metrics import (mean_absolute_error, r2_score,
root_mean_squared_error)
def lift(cp, d, idx):
"""CL of runs idx, integrated from their Cp."""
a = np.radians(d["alpha"][idx])[:, None]
n = d["normal"][idx]
n_L = n[..., 1] * np.cos(a) - n[..., 0] * np.sin(a)1
f = cp * n_L * d["area"][idx]
return -f.sum(axis=1) / d["S_ref"][0]
def scores(pred, d, idx):
"""Errors of pred, (runs, points), on the runs idx."""
true = d["cp"][idx]
err = pred - true
t, p = true.ravel(), pred.ravel()2
a = d["area"][idx]3
rel = (np.linalg.norm(err, axis=1)
/ np.linalg.norm(true, axis=1))4
cl_p, cl_t = lift(pred, d, idx), lift(true, d, idx)
return {
"RMSE": root_mean_squared_error(t, p),
"MAE": mean_absolute_error(t, p),
"max": np.abs(err).max(),
"R2": r2_score(t, p),
"rel_L2": rel.mean(),
"RMSE_A": np.sqrt((a * err**2).sum() / a.sum()),
"dCL": np.abs(cl_p - cl_t).mean(),
"Spearman": spearmanr(cl_p, cl_t)[0],5
}
# Then, in a script:
import numpy as np
from aircraft import load_runs, scores, split_runs
d = load_runs()
train, val, test, in_box = split_runs(d)
# The mean-field baseline: one field for every design,
# the mean of the training fields.
mean_field = d["cp"][train].mean(axis=0)
pred = np.tile(mean_field, (len(val), 1))
for name, value in scores(pred, d, val).items():
print(f"{name:9s}{value:7.3f}")
- The component of each outward normal in the lift direction, perpendicular to the flow at angle of attack (with along the free stream at zero angle of attack and up): the of Equation 2-2.
ravelflattens (runs, points) into one long vector, so RMSE, MAE and pool every point of every run, as the max error does.root_mean_squared_errorneeds scikit-learn 1.4 or later.- The area of every point of every run, shape (runs, points): the of Equation 3-3. The sums run over both axes, so
RMSE_Apools every point of every run, like the RMSE. axis=1takes the norm over the points of each run: one relative L2 error per run (Equation 3-2), then the mean.spearmanrreturns the correlation and a p-value;[0]keeps the correlation, here of the runs ranked by lift.
RMSE 0.149 MAE 0.060 max 1.231 R2 0.946 rel_L2 0.209 RMSE_A 0.138 dCL 0.271 Spearman 0.786
On the 8 validation runs Example 3-3 prints an of 0.946: a model that ignores its inputs explains 95 % of the variance, because most of the variation of is from point to point over the surface, and the mean field already has it. Weighting by area lowers the RMSE from 0.149 to 0.138: the largest errors sit where the points are densest. Even the ranking by lift scores 0.786, though the predicted field is the same for every run; only the lift direction and the geometry change from run to run. Example 3-5 scores a real surrogate with the same function.