A first field surrogate

A network that takes a point and a design and returns the field value at that point.

Outcome: Explain a coordinate network: one point and the design in, the field value out; add Fourier features to a small MLP, and test whether they help on a given dataset; train a neural field on a point-wise table, and know its limits.

Builds on Overfitting and regularisation, Physical fields

So far the networks mapped a design to a few numbers. With the location added to the inputs, a network predicts a field: a neural field (or coordinate network).

DefinitionNeural field

A network that takes the coordinates of one point (here also the surface normal there) and the design parameters, and returns the field value at that point: Cp(x,n,α,Λ)C_p(\mathbf{x}, \mathbf{n}, \alpha, \Lambda).

Evaluated at all the surface points of a run with the same α\alpha and Λ\Lambda, it gives the pressure on the whole aircraft. Anywhere else on the surface it gives a value too, though no detail finer than the training data and the network can represent.

It trains on the point-wise table: each minibatch is a random set of points from random training runs, and the loss is the mean squared error on CpC_p. The loop of Example 4-5 applies, with a minibatch of points in place of designs.

Fourier features

A plain MLP tends to learn smooth, slowly varying functions first, its spectral bias (Rahaman et al. 2019), and to struggle with sharp features, such as the suction peak at a wing’s leading edge. A common remedy is to feed it sines and cosines of the coordinates at several frequencies, called Fourier features (Tancik et al. 2020). The network then combines ready-made oscillations instead of building them from scratch. The site uses fixed frequencies doubling at each step (octaves), the positional encoding of Mildenhall et al. 2020. They do not always help: on the download’s runs they lower the error over the surface but not at the suction peaks (an exercise of a first surrogate, end to end).

γ(x)=[sin⁡(2kπx), cos⁡(2kπx)]\gamma(x) = \bigl[ \sin(2^k \pi x), \ \cos(2^k \pi x) \bigr]
(4-6)
For each coordinate x,y,zx, y, z and k=0,…,K−1k = 0, \dots, K - 1 with KK = 4: 4 frequencies doubling each time, 24 features in all.
x, n, α, Λ8 values+ sin, cos32 inputs48SiLU48SiLU48SiLUCp1 valueone surface point in, one pressure out · 6,337 parameters
Figure 4-3. The site’s neural field: a point, its normal and the design, plus Fourier features, through three hidden layers to CpC_p.

The site’s model takes as inputs the point’s position (3 values, scaled by 0.25), its surface normal (3), the normalised angle of attack and sweep (2) and 24 Fourier features of the position: 32 numbers. Three hidden layers of 48 SiLU neurons, one output, 6,337 parameters. In the browser the same weights run in a graphics shader, once per vertex, whenever a slider moves.

The site’s network was trained this way on 36 runs: 3,000 steps of 512 surface points, with Adam and the learning rate decaying along a cosine from 3×10−33 \times 10^{-3} to 10−410^{-4}, in a loop written in TypeScript so that it can also run in the browser. Targets are clipped to CpC_p between −3 and 1.2, and the site’s per-design errors are measured against the clipped values.

10.30.10.03015003000
training RMSE in Cp validation RMSE in Cp (4,000 points of the validation runs)log scale, against the training step
Figure 4-4. The site’s neural field: RMSE in CpC_p on the training minibatches (a running average) and on 4,000 points drawn from the 8 validation runs, against the training step, on a log scale where each gridline is about three times the one below.

In Figure 4-4 the curves stay together, ending at an RMSE of 0.057 and 0.058, so this model does not overfit.

Both are high, however: about 16 % of the standard deviation of CpC_p in the training data (0.36, clipped as in training). This small network underfits, limited by its size and its 3,000 steps: a larger model or longer training is the remedy, and regularisation would likely make it worse.

A neural field like this is small and cheap to evaluate. It can be queried at any point (though a finer query adds no information), and the points are independent, so evaluation parallelises easily on a GPU.

Its limit is the same independence: each point is predicted on its own, without looking at its neighbours. The model knows the geometry only through coordinates and a few design parameters, so it covers one parametric family of shapes and generalises poorly to new ones.

Models that read the geometry itself come next: grids, meshes and point clouds, then neural operators, which generalise the idea of querying any point: the trunk network of DeepONet is a coordinate network of this kind.

Example 4-7 adds the network to aircraft.py, then evaluates it, untrained, on the 2,000 points of the download’s first run.

Example 4-7. A neural field with Fourier features, added to aircraft.py, in PyTorch
# Add to aircraft.py, with its other imports at the top:
import math

def fourier(x, octaves=4):
    # sin and cos of each coordinate at π, 2π, 4π, 8π.
    k = 2.0 ** torch.arange(octaves) * math.pi1
    # (..., 3, octaves)
    xk = x[..., None] * k2
    sc = torch.cat([torch.sin(xk), torch.cos(xk)], dim=-1)
    return sc.flatten(-2)3

class NeuralField(nn.Module):
    def __init__(self, width=48):
        super().__init__()
        # 3 + 3 + 2 + 24 = 32 inputs.
        n_in = 3 + 3 + 2 + 3 * 2 * 4
        self.net = nn.Sequential(
            nn.Linear(n_in, width), nn.SiLU(),
            nn.Linear(width, width), nn.SiLU(),
            nn.Linear(width, width), nn.SiLU(),
            nn.Linear(width, 1))

    def forward(self, xyz, normal, design):
        # One row per point.
        f = [xyz, normal, design, fourier(xyz)]
        return self.net(torch.cat(f, dim=-1))4

# Then, next to aircraft.py: run 0, all 2,000 points.
import torch
from aircraft import (NeuralField, load_runs, scale_designs,
                      split_runs)

d = load_runs()
train, _, _, _ = split_runs(d)
Xn, _, _ = scale_designs(d, train)
T = lambda a: torch.tensor(a, dtype=torch.float32)
xyz, normal = T(d["xyz"][0]), T(d["normal"][0])
# The scaling of 4.3, the same design for every point.
design = T(Xn[0]).expand(2000, 2)5
torch.manual_seed(0)
print(NeuralField()(0.25 * xyz, normal, design).shape)
  1. The frequencies 2kπ2^k \pi of Equation 4-6, for k=0,…,3k = 0, \dots, 3.
  2. Broadcasting: x[..., None] has the shape (…, 3, 1) and k has the shape (4,), so every coordinate is multiplied by every frequency.
  3. flatten(-2) merges the last two axes, (…, 3, 8) into (…, 24): the 24 Fourier features.
  4. All inputs of a point side by side on the last axis: 3 + 3 + 2 + 24 = 32 numbers per row.
  5. expand repeats the run’s two design values on all 2,000 rows without copying them.

Example 4-7 prints torch.Size([2000, 1]): one CpC_p per point. The project script of a first surrogate trains this network on 1,024 points per step and tests it.