Physics-constrained machine learning: building physics into models
Updated · Scientifically reviewed by Priyank Srivastava, PhD on
Short answer
Physics-constrained machine learning builds known physics into models instead of hoping they learn it from data. Constraints can be soft (penalty terms in the loss, as in PINNs) or hard (built into the architecture so they hold exactly, such as divergence-free outputs, conserved quantities, symmetries or Hamiltonian structure). Hard constraints guarantee the property but require more design work; soft constraints are flexible but only approximately satisfied. Either typically improves data efficiency and physical plausibility.
Who this is for
ML practitioners and physicists who want models that respect known physics, generalise better and need less data.
Software
PyTorch ≥ 2.0 (example)
How it works
Physics can enter an ML model at three points:
| Where | How | Guarantee | Example |
|---|---|---|---|
| Data | Augment with symmetric copies; generate data from a verified solver | None, but better coverage | Rotated copies of field snapshots |
| Loss | Penalise violations (PDE residuals, conservation errors) | Approximate | PINNs; conservation penalties |
| Architecture | Design outputs that satisfy the constraint by construction | Exact | Divergence-free fields from a vector potential; positive outputs; equivariant layers |
Hard-constraint examples
- Divergence-free magnetic field. Predict a vector potential and output , so exactly.
- Positivity. Output or apply softplus to guarantee .
- Conservation. Predict fluxes between cells instead of cell values, so that what leaves one cell enters its neighbour; totals are conserved exactly.
- Energy conservation in dynamics. Learn a Hamiltonian and obtain dynamics from Hamilton’s equations.
- Symmetries. Use equivariant layers for rotation or translation symmetry.
A small example: exact total conservation
A model that redistributes a conserved quantity among cells can guarantee that the total is unchanged by predicting fluxes:
import torch
import torch.nn as nn
class ConservativeStep(nn.Module):
"""Updates cell values u (batch, n) with learned fluxes between neighbours.
Periodic domain: whatever leaves cell i enters cell i+1, so sum(u) is conserved exactly."""
def __init__(self, width=32):
super().__init__()
self.flux_net = nn.Sequential(nn.Conv1d(1, width, 3, padding=1, padding_mode="circular"),
nn.Tanh(), nn.Conv1d(width, 1, 1))
def forward(self, u):
F = self.flux_net(u.unsqueeze(1)).squeeze(1) # flux through the right face of each cell
return u - (F - torch.roll(F, shifts=1, dims=-1)) # u_i - (F_{i+1/2} - F_{i-1/2})
u = torch.rand(4, 64)
step = ConservativeStep()
print(torch.allclose(step(u).sum(-1), u.sum(-1), atol=1e-4)) # True, whatever the weights
Whatever the network learns, it cannot create or destroy the conserved quantity. That is a property no amount of training data alone could guarantee.
When to use which
- Soft constraints when the physics is complex, partially known, or hard to build into an architecture; accept small violations and measure them.
- Hard constraints for properties that must hold exactly for stability or credibility: conservation in long rollouts, positivity, divergence-free fields.
- Both: hard constraints for structure, soft penalties for the remaining equations.
Assumptions and limitations
- The example demonstrates exact conservation of a sum, not a full physical model.
- Hard constraints require that the constraint truly holds. Open, driven or dissipative systems need correctly formulated sources and sinks.
- Constraints improve plausibility but do not replace validation against trusted solutions; see simulation verification.
Frequently asked questions
Why not just add more data?+
Data from simulations and experiments are often expensive and cover a limited range. Constraints let models respect physics everywhere, including where data are sparse, and reduce physically impossible predictions such as negative densities or energy creation.
What is an equivariant network?+
A network whose outputs transform consistently when inputs are rotated, translated or reflected, exactly matching the symmetry of the physics. It cannot learn symmetry-violating behaviour and usually needs less data.
Do hard constraints limit accuracy?+
They restrict the function space to physically valid models, which usually helps. Problems arise if the imposed constraint is wrong for the system (for example assuming conservation in an open, driven system), so choose constraints that truly hold.
How do I enforce positivity of a density or temperature?+
Predict the logarithm and exponentiate, or apply a softplus activation at the output. Both guarantee positive values; logarithms also help when quantities span orders of magnitude.
References
- Karniadakis et al., Physics-informed machine learning, Nat. Rev. Phys. 3, 422 (2021) · Review article
- Greydanus, Dzamba & Yosinski, Hamiltonian neural networks (NeurIPS 2019) · Conference paper (arXiv)
- Beucler et al., Enforcing analytic constraints in neural networks emulating physical systems, Phys. Rev. Lett. 126, 098302 (2021) · Peer-reviewed article
- Bronstein et al., Geometric deep learning: grids, groups, graphs, geodesics, and gauges (2021) · Monograph (arXiv)
Need help applying this?
Our physicists turn concepts into working, verified simulations for research groups and companies.
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.