PINN: 1-D Heat Equation
A physics-informed neural network that learns the 1-D heat equation u_t = nu * u_xx by minimizing the PDE residual computed with forward-mode automatic differentiation, then re-evaluates the residual in Q16.16 fixed point for deployment.
How it works
- A
PinnMlp<2, 12, 1>fieldu(x, t): a single-hidden-layer tanh MLP with 12 units, fromcpp/pinn.hpp. The autodiff and training paths run indoublewithstd::tanh; the deployment residual benchmark runs the same field in Q16.16 fixed point (QValue<16, 16>) using the tanh lookup table (TINYMIND_USE_TANH_16_16). - Demonstrates differentiating the network with respect to its input coordinates:
u_tvia a first-order dual andu_xxvia a nested second-order dual, the exact derivatives a PDE residual needs and that weight-gradient backprop cannot supply. - Training is exact on both sides. The residual comes from the nested duals above, and the loss gradient with respect to all 49 weights is obtained in one forward pass by vector forward-mode (
MultiDual<V, N>) insidetinymind::pinn::sgdStep— no finite differences anywhere in the training loop. Finite differences appear in this example only as an independent reference for checking the input derivatives (see the autodiff-vs-FD check below). - The residual is enforced on an interior collocation grid over
xin[0, 1],tin[0, T], with the analytic referenceu(x, t) = exp(-nu*pi^2*t) * sin(pi*x). After training, the residual is recomputed in fixed point so the int8/fixed-point error against the double residual is bounded, giving a fixed-point residual-accuracy benchmark.
Build and run
cd examples/pinn_heat1d
make release
make run
make plot # needs matplotlib; a venv/pyenv works if it is not already in your Python
make run runs the verification checks (exact-solution residual, autodiff vs central finite differences, fixed-point residual benchmark). To produce the data plot.py reads, run the host-side training loop first with make train (built -O3, ~thousands of epochs but under a second), which writes pinn_loss.csv (residual loss and solution L2 error per epoch) and pinn_solution.csv (learned vs analytic field at two times). The fixed-point inference path pulls in lookupTables.cpp for the Q16.16 tanh table.
Results
make run (verification):
| check | measured |
|---|---|
| residual on a field with a closed form | 0.000e+00 |
u_x vs central finite differences | 9.207e-11 |
u_t vs central finite differences | 3.753e-11 |
u_xx vs central finite differences | 3.263e-06 (finite-difference round-off, ~eps/h², not autodiff error) |
| Q16.16 vs float inference | 1.655e-03 |
make train (8000 epochs, PinnMlp 2->12->1, 49 parameters):
| start | end | |
|---|---|---|
| residual loss | 4.3726e+00 | 2.0989e-02 (208x lower) |
| solution L2 error | 4.7870e-01 | 1.2615e-02 |
Recomputing the trained residual in Q16.16 stays within 8.776e-03 of the double version.
Output

The left panel shows the PINN residual loss and the solution L2 error both falling over training (the periodic spikes are optimizer transients that immediately recover). The right panel overlays the learned field on the analytic solution at T/2 and T; the learned curves match the closed-form heat-diffusion profiles closely, including the correct exponential decay of the peak between the two times.