Skip to content

Deep Ritz: solve a PDE by minimising an energy

PINN deep_ritz_poisson_2d.py

A stretched membrane settles where its energy is lowest. The Deep Ritz method uses that directly: the network is the trial function and the loss is the energy, with no test functions and no assembled system. Only first derivatives appear. The method is that of E & Yu, Commun. Math. Stat. 6:1 (2018), §2 (arXiv:1710.00211).

The network's u(x, y) on the unit square in inferno colours during training: a faint, uneven field at the first epochs that becomes the single round bump x(1-x)y(1-y) with peak 0.0625, with the epoch counter running from 0 to 4000.

The network's \(u(x,y)\) during training, epochs 0 to 4000, on the colour range of the exact solution.

The problem

For \(-\Delta u=f\) with \(u=0\) on \(\partial\Omega\), the weak solution minimises the energy over \(H^1_0\):

\[J[u] = \int_\Omega \Big( \tfrac12\,|\nabla u|^2 - f\,u \Big)\,dx,\]

with \(f\) chosen so that the exact solution is \(u=x(1-x)y(1-y)\).

The jNO code

expr.integrate() integrates a scalar expression over the mesh, so the method is: the network trial with a hard boundary factor, the energy density, its integral:

d = jno.shape.rect(0, 0, 1, 1, size=0.05).domain()
x, y, _ = d.variable("interior", split=True)

net = jno.nn(foundax.mlp(2, hidden_dims=48, num_layers=4, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
net.optimizer(optax.adam(3e-3))

bubble = x * (1 - x) * y * (1 - y)
w = net(x, y) * bubble  # hard u = 0 on the boundary
u = w.scalar.bind(x=x, y=y)
f = 2 * (x * (1 - x) + y * (1 - y))  # -Δ[x(1-x)y(1-y)]

energy = (0.5 * (u.x**2 + u.y**2) - f * u).integrate(quadrature="gauss")  # J[u], element Gauss points

The loss is the signed energy, not a square; jno.core minimises it as it is, so it converges to \(J_{\min}<0\):

crux = jno.core([energy], domain=d)  # the signed energy is the loss; it converges to J_min < 0
crux.solve(4000)

fine = jno.shape.rect(0, 0, 1, 1, size=0.035).domain()
u_h, u_ref = crux.eval([w, bubble], domain=fine)  # reference: x(1-x)y(1-y)
rel_l2 = float(np.linalg.norm(u_h - u_ref) / np.linalg.norm(u_ref))
print(f"Relative L2 error: {rel_l2:.4e}")
assert rel_l2 < 1e-3, f"Deep Ritz did not solve Poisson: rel-L2={rel_l2:.3e}"

quadrature="gauss" integrates at Gauss points inside every element. A rule that samples only the nodes lets an expressive network build structure between them and push the estimated energy below the true minimum.

Result

The network matches \(x(1-x)y(1-y)\) to rel. \(L^2\approx8\times10^{-5}\). The energy reaches the true minimum within a few hundred epochs and stays there.

Full script

The whole script, as it runs in the tests
"""Deep Ritz: minimise the energy J[u] = ∫ ½|∇u|² - f u of -Δu = f on the unit square with a network."""

import foundax
import jax
import optax

import jno

jax.config.update("jax_enable_x64", True)  # the mesh integral accumulates in float64
run = jno.setup(__file__)

d = jno.shape.rect(0, 0, 1, 1, size=0.05).domain()
x, y, _ = d.variable("interior", split=True)

net = jno.nn(foundax.mlp(2, hidden_dims=48, num_layers=4, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
net.optimizer(optax.adam(3e-3))

bubble = x * (1 - x) * y * (1 - y)
w = net(x, y) * bubble  # hard u = 0 on the boundary
u = w.scalar.bind(x=x, y=y)
f = 2 * (x * (1 - x) + y * (1 - y))  # -Δ[x(1-x)y(1-y)]

energy = (0.5 * (u.x**2 + u.y**2) - f * u).integrate(quadrature="gauss")  # J[u], element Gauss points

crux = jno.core([energy], domain=d)  # the signed energy is the loss; it converges to J_min < 0
crux.solve(4000)

fine = jno.shape.rect(0, 0, 1, 1, size=0.035).domain()
u_h, u_ref = crux.eval([w, bubble], domain=fine)  # reference: x(1-x)y(1-y)
rel_l2 = float(np.linalg.norm(u_h - u_ref) / np.linalg.norm(u_ref))
print(f"Relative L2 error: {rel_l2:.4e}")
assert rel_l2 < 1e-3, f"Deep Ritz did not solve Poisson: rel-L2={rel_l2:.3e}"