Foundations to Frontier
Tests
J. Scientific Machine Learning › J2 Physics-Informed Neural Networks

Boundary and initial conditions: soft vs hard constraints

A differential equation has a unique solution only together with its boundary and initial conditions, so a PINN must enforce them. There are two ways. Soft constraints add a penalty λ times the squared boundary misfit to the loss; they are simple and work for any geometry, but the conditions hold only approximately and λ must be chosen. Hard constraints build the conditions into the network's form, for example u = α(1 − x) + βx + x(1 − x)N(x), so they hold exactly for every set of weights; they need a distance-like function for the geometry. This page compares both on u″ = f with u(0) = 1 and u(1) = −0.5. You build hard ansätze for Dirichlet and mixed conditions with exact derivatives, then train with Adam and Levenberg–Marquardt. With Adam, λ = 0.1 left boundary errors of 0.8 and a solution error of 0.4, λ = 10 gave 0.003, λ = 1000 0.04, and the hard ansatz 0.006 with zero boundary error. With Levenberg–Marquardt every variant reached errors of about 0.00001–0.00004, so the choice of λ mattered mainly for the weaker optimiser.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Making sure the edges are right

A rule like how a string bends under a load has many possible answers until we say where the string is tied down. That is the boundary condition. A physics-informed network can be told about it in two ways. We can add a penalty: every time the network’s value at the edge is wrong, the loss goes up. Or we can build the network’s formula so that the edge value is always right, whatever the weights are. The first is easy but only nearly right; the second is exactly right but needs a formula for the shape. The demo shows both.

PINN lab
Soft penalty with weight λ = 1: largest boundary error 1.0e-9. Relative error 0.12%. Set λ = 0 to see that the equation alone does not fix the solution.
Why engineers care

For a heat shield or a turbine blade, the temperature at the surface is often what we know best; building it into the model exactly means the surface values can never drift during training.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Without a boundary condition, an equation like u″ = f has…
  2. 2. A soft constraint is…
  3. 3. A hard constraint holds…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Soft constraints (penalties): L = L_PDE + λ_B L_B + λ_I L_I, where L_B and L_I are mean squared misfits of boundary and initial conditions at sampled points. Works for any condition and geometry; the conditions hold only approximately, and the weights λ must balance the terms (J2-3).
  • Hard constraints (ansatz): u_θ(x) = G(x) + D(x) N_θ(x), where G satisfies the conditions and D vanishes on the boundary. On [0, 1] with u(0) = α, u(1) = β: G = α(1 − x) + βx and D = x(1 − x).
  • Other conditions:
    • Initial conditions: u(x, t) = u₀(x) + t N(x, t) satisfies u(x, 0) = u₀ exactly.
    • Neumann or mixed: choose D so that its derivative also vanishes where needed, for example D = x(1 − x)² for u′(1) = β.
    • Periodic: feed the network cos 2πx and sin 2πx instead of x, so its output is periodic by construction.
  • Complex geometry: D can be an approximate distance function to the boundary, built from R-functions (Sukumar and Srivastava, 2022), or learned by a second network.
  • Other options: augmented Lagrangian methods update λ like a Lagrange multiplier; variational and weak-form losses build Neumann conditions into the integral.
📝 Quick check (Level 1)
0/3 answered
  1. 1. For u(x, t) with initial condition u₀, a hard ansatz is…
  2. 2. A network made periodic by construction uses inputs…
  3. 3. For complex shapes, D(x) can be…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Building constraints into the network

Exercise 1.

  • A tanh network N with exact N′ and N″.
  • The Dirichlet ansatz u = α(1 − x) + βx + x(1 − x)N with u′ and u″ by the product rule, checked against finite differences.
  • A mixed ansatz u = α + βx + x(1 − x)²N with u(0) = α and u′(1) = β exactly, and the soft-penalty loss for comparison.

Practice.

  • Prefer hard constraints for simple geometry (intervals, boxes, discs, initial conditions); they remove a loss term and a weight.
  • Keep D of order 1 inside the domain; a D that is tiny everywhere slows learning near the boundary.
  • With soft constraints, try λ over several orders of magnitude and look at both the boundary misfit and the residual; or use adaptive weights (J2-3) or an augmented Lagrangian.
  • Sample boundary points densely enough that the condition holds between them too.
  • Check the conditions after training on many points, not only at the training points.
  • Tools: DeepXDE’s apply_output_transform and PointSetBC, PhysicsNeMo’s geometry module with signed distance functions.
import torch

net = torch.nn.Sequential(torch.nn.Linear(1, 20), torch.nn.Tanh(), torch.nn.Linear(20, 1))
alpha, beta = 1.0, -0.5

def u(x):                                  # u(0) = alpha and u(1) = beta for any weights
    return alpha * (1 - x) + beta * x + x * (1 - x) * net(x)

# DeepXDE equivalent:
# model.net.apply_output_transform(lambda x, y: alpha * (1 - x) + beta * x + x * (1 - x) * y)
🐍 Exercise 1: hard-constraint ansätze
Write Dirichlet and mixed hard-constraint ansätze for a tanh network with exact first and second derivatives, and the soft-penalty loss; check the boundary values and derivatives.
📝 Quick check (Level 2)
0/3 answered
  1. 1. DeepXDE applies a hard constraint with…
  2. 2. With soft constraints, check…
  3. 3. A D(x) that is tiny everywhere…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the ansatz is exact. For u = G + D N, on the boundary D = 0 so u = G, which meets the conditions by construction; N is free inside. The derivatives follow from the product rule: for D = x(1 − x), u″ = G″ − 2N + 2(1 − 2x)N′ + x(1 − x)N″. For a Neumann condition at x = 1 we also need D′(1) = 0, which D = x(1 − x)² provides.

The penalty trade-off. With a soft constraint the minimiser of L_PDE + λ L_B satisfies the boundary condition only in the limit λ → ∞. A small λ lets the boundary drift: in the exercise Adam with λ = 0.1 left boundary errors of 0.8, and because the equation fixes u only up to a + bx, the whole solution was off by 0.4. A large λ makes the problem ill-conditioned: the boundary term dominates the gradient and a first-order optimiser makes slow progress on the residual (0.04 at λ = 1000 against 0.003 at λ = 10).

The hard ansatz changes the optimisation. It removes the boundary term but multiplies the network by D, which changes the scale of the residual’s gradient near the ends. With Adam it gave exact boundaries and errors of 0.006–0.02, similar to a good λ rather than better. With Levenberg–Marquardt, which uses curvature, all variants reached errors of 0.00001–0.0002: the trade-off is mostly a problem of first-order training.

Augmented Lagrangian. Instead of increasing λ, add a multiplier term μᵀ(u_B − g) and update μ ← μ + λ(u_B − g) after each inner solve; the constraint then holds with a moderate λ.

📝 Quick check (Level 3)
0/3 answered
  1. 1. In u = G + D N, D must be…
  2. 2. A very large penalty λ makes training…
  3. 3. For u′(1) = β with u = α + βx + D N we need…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Should a PINN enforce its boundary conditions with penalties or by construction?

History.

  • I. Lagaris, A. Likas and D. Fotiadis (1998) used trial solutions of the form G + D N that satisfy Dirichlet and Neumann conditions exactly.
  • M. Raissi, P. Perdikaris and G. Karniadakis (2019) used soft penalties, which handle any geometry and condition and became the default.
  • L. Lu et al. (2021) studied hard constraints for inverse design; N. Sukumar and A. Srivastava (2022) built exact boundary conditions on complex domains with approximate distance functions from R-functions.
  • S. Dong and N. Ni (2021) enforced periodic conditions exactly with Fourier-feature inputs.

Exercise 2. u″ = f on [0, 1] with u(0) = 1, u(1) = −0.5 and exact solution sin πx + 0.3 sin 4πx + 1 − 1.5x; 32 collocation points; width-20 tanh network; Adam 2000 steps with a decaying learning rate, Levenberg–Marquardt 150 steps. Entries are relative L2 error on 501 points (largest boundary error in brackets), median of 3 initialisations, rounded to one significant figure.

constraint Adam Levenberg–Marquardt
soft, λ = 0.1 0.4 (0.8) 0.00004 (0.00009)
soft, λ = 10 0.003 (0.007) 0.00002 (0.00002)
soft, λ = 1000 0.04 (0.001) 0.00001 (0.00002)
hard ansatz 0.006 (0) 0.00002 (0)
  • Too small a λ ruins the solution, not just the boundary. The boundary errors of 0.8 shifted the whole solution by a linear function; the error was 0.4.
  • Too large a λ slows the residual down. At λ = 1000 the boundary was good but the interior error was ten times worse than at λ = 10.
  • The hard ansatz was exact at the boundary but not more accurate inside with Adam; it removed the need to choose λ.
  • A second-order optimiser made the choice almost irrelevant. Levenberg–Marquardt reached about 0.00002 in every case on seed 0.
  • Seeds 1 and 2: Adam soft λ = 10 gave 0.002 and 0.09 (one initialisation on seed 2 kept a boundary error of 0.2), hard gave 0.02 and 0.02; Levenberg–Marquardt hard 0.00004 and 0.000008, soft λ = 1000 0.0002 and 0.00001.
🐍 Exercise 2 (research): should a PINN enforce its boundary conditions with penalties or by construction?
Train PINNs for u″ = f with non-zero boundary values using soft penalties λ = 0.1, 10, 1000 and a hard ansatz, with Adam and with Levenberg–Marquardt, and compare solution and boundary errors.

Boundary conditions in practice

  • Fluid flow: no-slip walls are often built in with distance functions; inflow profiles with penalties. For airfoils and vehicles, PhysicsNeMo uses signed distance functions from CAD geometry.
  • Time-dependent problems: the ansatz u₀(x) + t N(x, t) makes the initial condition exact, which avoids training a model that starts from the wrong state.
  • Periodic domains: climate and plasma models on periodic boxes use Fourier-feature inputs so periodicity holds exactly.
  • Inverse problems: when boundary data are noisy measurements, soft penalties are preferable because exact enforcement would fit the noise (J2-5).

References

  1. I. E. Lagaris, A. Likas and D. I. Fotiadis (1998). “Artificial neural networks for solving ordinary and partial differential equations.” IEEE Transactions on Neural Networks.
  2. M. Raissi, P. Perdikaris and G. E. Karniadakis (2019). “Physics-informed neural networks.” Journal of Computational Physics.
  3. N. Sukumar and A. Srivastava (2022). “Exact imposition of boundary conditions with distance functions in physics-informed deep neural networks.” Computer Methods in Applied Mechanics and Engineering.
  4. L. Lu et al. (2021). “Physics-informed neural networks with hard constraints for inverse design.” SIAM Journal on Scientific Computing.
  5. S. Dong and N. Ni (2021). “A method for representing periodic functions and enforcing exactly periodic boundary conditions with deep neural networks.” Journal of Computational Physics.