Physics-informed neural networks
Overview
A finite-element solver discretizes a domain into cells and solves a linear system. A physics-informed neural network (PINN) takes the opposite approach: it leaves the domain continuous and represents the solution as a neural network, then trains the network so that it satisfies the governing equation everywhere.
For the Poisson problem $-\Delta u = f$ with Dirichlet data $u = g$ on the boundary, the network is trained on two terms: the residual of the PDE at interior points, and the boundary mismatch at boundary points. The result is a smooth, mesh-free field that can be evaluated anywhere — no mesh, no linear solve.
The residual
The core of a PINN is the strong-form residual. For Poisson it is $\Delta u + f$, which should be zero wherever the equation holds:
Rasmah.Neural.laplacian_fd — Function
laplacian_fd(net, θ, x; act, ε=1e-4)Laplacian ∇²u(x) of the scalar field u = apply(net, θ, x; act)[1], evaluated by central finite differences over each spatial coordinate of x.
Rasmah.Neural.poisson_residual — Function
poisson_residual(net, θ, x, f; act, ε=1e-4)Strong-form residual Δu + f(x) for the Poisson problem -Δu = f (equivalently steady heat conduction with unit conductivity).
net = MLP([2, 16, 16, 1])
θ = xavier_init(MersenneTwister(0), net.sizes)
f(x) = 2 * ((x[1] * (1 - x[1])) + (x[2] * (1 - x[2])))
poisson_residual(net, θ, [0.3, 0.7], f; act=tanh)1.9662552668256377Solving with a PINN
pinn_solve assembles the loss (residual + boundary) and trains:
Rasmah.Neural.pinn_solve — Function
pinn_solve(net, θ0, f, g, lo, hi; …) -> PINNResultSolve the Poisson problem -Δu = f on the box [lo, hi] with Dirichlet boundary data u = g, using a physics-informed neural network. f(x) is the source term; g(x) the boundary value.
Keywords: act (hidden activation, default tanh), n (interior collocation points), nb (boundary points), epochs, lr, λ_bc (boundary-loss weight), seed, backend (defaults to neural_backend()).
Rasmah.Neural.PINNResult — Type
PINNResult(net, θ, act, loss_history)A trained PINN field. It is callable: r(x) = apply(net, θ, x; act)[1] evaluates the solution at x.
The result is callable — result(x) evaluates the solution at any point:
sol = pinn_solve(net, θ, f, x -> 0.0, [0.0, 0.0], [1.0, 1.0];
act=tanh, n=100, nb=50, epochs=150, lr=1e-3, seed=0)PINNResult{MLP, typeof(tanh), Float64}(MLP([2, 16, 16, 1]), [-0.23099423470334143, 0.06950679869076795, -0.006471274031838356, 0.2507282973503786, -0.1705546446182183, 0.11132165516156245, -0.33636349395640897, -0.24727040357154453, 0.2732415419460274, -0.46615361861528115 … -0.19751699864363098, -0.5254977706660903, 0.5167102470456246, -0.2695813754041712, 0.34306525701264273, 0.5166554152626137, 0.48084051133179656, 0.4119365552044619, 0.6112650848056584, -0.03918576546685848], tanh, [3.5848119673878482, 3.4227988926627964, 3.2662850041179654, 3.115275928962823, 2.969756843546561, 2.8296914313499704, 2.6950352356522567, 2.565740160404623, 2.441750274081379, 2.323000133499855 … 0.07359845531131351, 0.0717194938553413, 0.06988771745186756, 0.06810265096207313, 0.06636379631060428, 0.06467065548559317, 0.06302271422258282, 0.06141945717960508, 0.05986033837175682, 0.05834480992306983])sol([0.5, 0.5])0.13496559375764697Here the source $f = 2[x(1-x) + y(1-y)]$ was chosen so the exact solution is $u = x(1-x)\,y(1-y)$ with $u = 0$ on the boundary. At the centre the exact value is $(0.5)^4 = 0.0625$; the PINN gets close after a short training run.
sol.loss_history[end]0.05834480992306983Next steps
With geometry, meshing, and simulation covered, the handbook continues with the materials and solvers that a simulation consumes.
