Synthetic physics dataset generation: from verified solvers to ML-ready data
Updated · Scientifically reviewed by Priyank Srivastava, PhD on
Short answer
Generate synthetic physics data with a solver you have verified, sample the input parameters to cover the range the model must handle, store each sample with complete metadata (parameters, solver version, resolution, seed), and check the data physically before training: residuals of the governing equations, conservation, boundary conditions and convergence. Split training, validation and test sets by parameters rather than by random snapshots, and keep an out-of-distribution set to measure extrapolation honestly.
Who this is for
Scientific ML researchers and engineers creating training data for surrogates, neural operators or physics-aware AI models.
Software
Python ≥ 3.10NumPy ≥ 1.24
Prerequisites
- Python with NumPy (
pip install numpy). - A solver for your problem that has passed verification tests; see simulation verification. In the example below the solver is an exact solution, so it is verified by construction.
Step 1: define the problem and parameter ranges
We build a dataset for the 1D heat equation on with . Each sample has a diffusivity and an initial condition made of five sine modes. The exact solution is
Step 2: generate, check and save
import json
import numpy as np
rng = np.random.default_rng(seed=42) # fixed seed: the dataset is reproducible
nx, nt, n_samples, n_modes = 128, 64, 200, 5
x = np.linspace(0.0, 1.0, nx)
t = np.linspace(0.0, 0.1, nt)
def heat_solution(coeffs, alpha, x, t):
"""Exact solution of u_t = alpha * u_xx on [0, 1] with u = 0 at both ends."""
k = np.arange(1, len(coeffs) + 1)
modes = np.sin(np.pi * np.outer(k, x)) # (n_modes, nx)
decay = np.exp(-alpha * (np.pi * k[:, None]) ** 2 * t) # (n_modes, nt)
return np.einsum("k,kt,kx->tx", coeffs, decay, modes) # (nt, nx)
alphas = rng.uniform(0.01, 0.1, n_samples)
coeffs = rng.normal(0.0, 1.0, (n_samples, n_modes)) / np.arange(1, n_modes + 1)
u = np.stack([heat_solution(c, a, x, t) for c, a in zip(coeffs, alphas)])
# Physics check: finite-difference residual of the PDE, away from the edges
worst = 0.0
for i in range(0, n_samples, 20):
u_t = np.gradient(u[i], t, axis=0, edge_order=2)
u_xx = np.gradient(np.gradient(u[i], x, axis=1, edge_order=2), x, axis=1, edge_order=2)
residual = u_t - alphas[i] * u_xx
worst = max(worst, np.abs(residual[1:-1, 2:-2]).max() / np.abs(u_t).max())
print(f"max relative PDE residual = {worst:.4f}")
print(f"max boundary value = {np.abs(u[:, :, [0, -1]]).max():.1e}")
# Split by parameters: hold out the largest diffusivities as an extrapolation test
order = np.argsort(alphas)
train, test, ood = order[:140], order[140:170], order[170:]
np.savez_compressed("heat_dataset.npz", u=u, x=x, t=t, alpha=alphas, coeffs=coeffs,
train=train, test=test, ood=ood)
meta = {"equation": "u_t = alpha u_xx", "domain": [0, 1], "bc": "u=0 at x=0,1",
"units": "dimensionless", "seed": 42, "nx": nx, "nt": nt, "n_samples": n_samples}
with open("heat_dataset.json", "w") as f:
json.dump(meta, f, indent=2)
print("saved", u.shape)
The residual is not exactly zero because finite differences approximate the derivatives; it shrinks as the grid is refined, which is itself a useful check.
Step 3: real solvers
With a numerical solver instead of an exact solution, add three steps: run a convergence study on a few samples to choose the resolution (numerical convergence study); record solver name, version and settings with every sample; and flag or discard runs that fail conservation or stability checks rather than training on them silently.
Verify
- The printed PDE residual is small (a few per cent or less) and decreases when
nxandntare increased. - Boundary values are zero to rounding error.
- Parameter histograms cover the intended ranges, and the out-of-distribution set really lies outside the training range.
Troubleshooting
- Large residual: check that derivatives use the actual grid spacing (pass
xandttonp.gradient) and that the solver resolution is adequate. - Model does well on test but badly in use: the test set is too similar to the training set; split by parameters and add realistic inputs.
- Dataset too large for memory: write samples incrementally to HDF5 with chunking instead of stacking in memory.
Assumptions and limitations
- The heat equation example is deliberately simple; real datasets come from expensive solvers whose own errors propagate into the trained model.
- A synthetic dataset represents the physics in the solver, not physics the solver omits. Validate against experiments where the model will be used on real data.
- Training models on the data is covered in how to train surrogate models.
Frequently asked questions
How many samples do I need?+
It depends on the dimensionality of the parameter space and the model. Start with a few hundred, train, and plot test error against dataset size; if the error is still falling, generate more, preferably where the model is least accurate (active learning).
Should I add noise to synthetic data?+
Only if the model will see noisy inputs, such as experimental measurements, and then with a realistic noise model. Keep a clean copy so you can separate model error from noise.
Why split by parameters rather than randomly?+
Snapshots from the same simulation are strongly correlated. A random split puts near-duplicates in both training and test sets, so the test error looks better than the true generalisation error.
What file format should I use?+
HDF5 or NetCDF for large arrays with metadata, or compressed NumPy archives for small datasets. Include units and parameter names inside the file, not only in the file name.
References
- Takamoto et al., PDEBench: an extensive benchmark for scientific machine learning, NeurIPS Datasets and Benchmarks (2022) · arXiv preprint
- Wilkinson et al., The FAIR guiding principles for scientific data management and stewardship, Sci. Data 3, 160018 (2016) · Peer-reviewed article
- Li et al., Fourier neural operator for parametric partial differential equations (2020) · arXiv preprint
Want this set up and verified for you?
We install, configure and validate scientific software on your workstation, cluster or cloud, and train your team to run it.
Services listed on this page are available today. Products marked “early access” or “in development” on our platform page are not yet generally available.
Spotted an error or an outdated command? Tell us. We correct verified issues promptly.