Variational PINN: a network trial against the FEM test space
PINN FEM vpinn_poisson_2d.py
A variational PINN keeps the test functions of finite elements but makes the trial function a neural network.
Instead of solving a linear system for coefficients, it minimises the weak-form residual tested against the FE
basis. In jNO it is written like any other jno.fem problem, with the network where the unknown would be.

The network's \(u(x,y)\) during training, epochs 0 to 2500, on the colour range of the exact solution.
The problem
Poisson's equation \(-\Delta u=f\) on the unit square with \(u=x(1-x)y(1-y)\), so \(f=2[x(1-x)+y(1-y)]\). The loss is the weak residual against every interior test function \(\varphi_i\):
The jNO code
The trial is a network with a factor that vanishes on the boundary:
d = jno.shape.rect(0, 0, 1, 1, size=0.07).domain()
xi, yi, _ = d.variable("interior", split=True)
xb, yb, _ = d.variable("boundary", split=True)
u = d.unknown() # only to declare the Dirichlet condition: it tells the loss which test functions to drop
v = u.test()
net = jno.nn(foundax.mlp(2, hidden_dims=32, num_layers=3, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
net.optimizer(optax.adam(1e-2))
bubble = xi * (1 - xi) * yi * (1 - yi)
u_net = net(xi, yi) * bubble # hard u = 0 on the boundary
grad = lambda w: jno.np.jacobian(w, [xi, yi])
f = 2 * (xi * (1 - xi) + yi * (1 - yi)) # -Δ[x(1-x)y(1-y)]
The weak form is the standard one with the network as the trial; jno.fem detects the network and returns a
trainable residual. The Dirichlet term tells it which test functions live on the boundary, so their residual is
masked:
pde = jno.fem([jno.np.inner(grad(u_net), grad(v)) - f * v, u(xb, yb) - 0.0]) # ∫ ∇u_net·∇v - f v, v in the P1 basis
crux = jno.core([pde.mse], domain=d)
crux.solve(2500)
fine = jno.shape.rect(0, 0, 1, 1, size=0.04).domain()
u_h, u_ref = crux.eval([u_net, 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-2, f"VPINN did not solve Poisson: rel-L2={rel_l2:.3e}"
Result
After 2500 Adam steps the network matches \(x(1-x)y(1-y)\) to rel. \(L^2=8.3\times10^{-4}\) on a test mesh of size 0.04.
Full script
The whole script, as it runs in the tests
"""Variational PINN: a network trial function tested against the finite-element basis, -Δu = f on the unit square."""
import foundax
import jax
import optax
import jno
jax.config.update("jax_enable_x64", True) # the assembler builds in float64
run = jno.setup(__file__)
d = jno.shape.rect(0, 0, 1, 1, size=0.07).domain()
xi, yi, _ = d.variable("interior", split=True)
xb, yb, _ = d.variable("boundary", split=True)
u = d.unknown() # only to declare the Dirichlet condition: it tells the loss which test functions to drop
v = u.test()
net = jno.nn(foundax.mlp(2, hidden_dims=32, num_layers=3, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
net.optimizer(optax.adam(1e-2))
bubble = xi * (1 - xi) * yi * (1 - yi)
u_net = net(xi, yi) * bubble # hard u = 0 on the boundary
grad = lambda w: jno.np.jacobian(w, [xi, yi])
f = 2 * (xi * (1 - xi) + yi * (1 - yi)) # -Δ[x(1-x)y(1-y)]
pde = jno.fem([jno.np.inner(grad(u_net), grad(v)) - f * v, u(xb, yb) - 0.0]) # ∫ ∇u_net·∇v - f v, v in the P1 basis
crux = jno.core([pde.mse], domain=d)
crux.solve(2500)
fine = jno.shape.rect(0, 0, 1, 1, size=0.04).domain()
u_h, u_ref = crux.eval([u_net, 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-2, f"VPINN did not solve Poisson: rel-L2={rel_l2:.3e}"