Scientific ML Studio
Learn/ Physics-Informed Neural…/ 13.3
13.3 · Worked problems

Heat in a bar: adding time to a PINN

Cool a heated metal bar over time. Time becomes one more input to the network, the starting temperature becomes a new loss term, and the exact solution lets you grade the answer.

The two Poisson guides were steady problems: nothing changed with time, so the answer was a single picture. This guide adds the clock. You will solve the heat equation in a thin bar, a problem where the temperature starts in one shape and slowly relaxes towards another.

Almost everything you already know carries over. What is new is small but important: time is just another input to the network, and the starting condition is a loss term of its own.

Before you start

Finish Poisson in one dimension first; this guide assumes you know what each block is for. Open the Studio and choose an empty canvas, or take the quick route in Section 3.

1. The problem

A thin bar of unit length is hot in the middle and held at zero degrees at both ends. Let $u(x,t)$ be its temperature at position $x$ and time $t$. Heat spreads according to

$$ \frac{\partial u}{\partial t} = a\,\frac{\partial^2 u}{\partial x^2}, \qquad 0 < x < 1,\quad 0 < t \le 1, $$

where $a$ is the diffusivity, how quickly the material conducts heat. We use $a = 0.1$, which is also the Studio's default. The bar starts with a smooth hump of heat,

$$ u(x, 0) = \sin(\pi x) \qquad \text{(initial condition)}, $$

and both ends are kept cold for all time,

$$ u(0, t) = 0, \qquad u(1, t) = 0 \qquad \text{(boundary conditions)}. $$

Three kinds of information now pin down the answer: what the equation says inside, what happens at the ends, and where we start. Remove any of the three and many different solutions would fit.

The exact answer (so we can check ourselves)

Try $u(x,t) = e^{-a\pi^2 t}\sin(\pi x)$. The shape never changes, only its height, which decays with time. Differentiating gives

$$ u_t = -a\pi^2\, e^{-a\pi^2 t}\sin(\pi x), \qquad u_{xx} = -\pi^2\, e^{-a\pi^2 t}\sin(\pi x), $$

so $u_t = a\,u_{xx}$ holds. At $t = 0$ the factor is 1, which gives $\sin(\pi x)$. At $x = 0$ and $x = 1$ the sine is zero. So

$$ u_{\text{exact}}(x,t) = e^{-a\pi^2 t}\,\sin(\pi x). $$

With $a = 0.1$ the hump decays by a factor $e^{-0.1\pi^2} \approx 0.373$ by $t = 1$. The network never sees this formula while it trains. We keep it only to grade the result.

2. How a PINN sees the problem

The unknown is now a function of two inputs, so the network is $u_\theta(x, t)$. Time is not special to the network. It is just a second coordinate, and the equation relates how the output changes along one axis to how it curves along the other.

The equation error. At many points $(x_i, t_i)$ inside the bar, over the whole time range, the network is differentiated by automatic differentiation, once in time and twice in space:

$$ r_\theta(x,t) = \frac{\partial u_\theta}{\partial t} - a\,\frac{\partial^2 u_\theta}{\partial x^2}. $$

The boundary error. The ends should stay at zero for all times: $u_\theta(0,t) = 0$ and $u_\theta(1,t) = 0$.

The initial error. At $t = 0$ the network should reproduce the starting profile: $u_\theta(x, 0) = \sin(\pi x)$.

The loss adds all three, each with a weight:

$$ \mathcal{L}(\theta) = \underbrace{\frac{1}{N_r}\sum_i r_\theta(x_i,t_i)^2}_{\text{PDE loss}} + w_{\text{bc}}\underbrace{\frac{1}{N_b}\sum_j u_\theta(x^b_j, t^b_j)^2}_{\text{boundary loss}} + w_{\text{ic}}\underbrace{\frac{1}{N_i}\sum_k \bigl(u_\theta(x_k, 0) - \sin(\pi x_k)\bigr)^2}_{\text{initial-condition loss}} . $$

Space and time together

The picture to hold in your head is a rectangle with $x$ across and $t$ up. The ends of the bar are the left and right sides, the starting condition is the bottom edge, and the equation must hold everywhere inside. The top edge, $t = 1$, is free: nothing is imposed there, the network simply inherits what the equation and the starting profile force on it.

3. The plan: one block per decision

# Block The question it answers Our choice
1 DOM Domain Where does the problem live, and how is it sampled? Line, $x\in[0,1]$, Time dimension ticked, $t\in[0,1]$. 2000 interior points, 100 per boundary, 100 initial points
2 PDE Which equation must hold inside? Parabolic → Heat, $u_t = a\,u_{xx}$, diffusivity $a = 0.1$
3 BC × 2 What is fixed at the ends? Dirichlet, $u = 0$: one block on BC1 ($x = 0$), one on BC2 ($x = 1$)
4 IC Where do we start? $u(x, t_\text{init}) = \sin(\pi x)$, no initial velocity
5 NET Network What function family do we search in? Hidden layers 32, 32, 32, tanh. The two inputs, $x$ and $t$, come from the Domain
6 LOSS Loss How are the errors weighted? Mean square. PDE weight 1, BC weights [10, 10], IC weights [10]
7 OPT Optimiser How do the weights change, and for how long? Adam, learning rate 0.001, 5000 epochs
8 RUN Train How is the run carried out? Defaults: random seed 0, checkpoint model.pt

The quick route

The Heat in a bar starter already has this graph, wired. Create a notebook from it, then change three things: Hidden layers to 32, 32, 32 on the Network block, Default boundary points and Initial points to 100 on the Domain block, and the Loss block's weights to [10, 10] and [10]. The steps below build the same graph from an empty canvas, which is the better way to learn it.

4. Step by step in the Studio

Step 1: Domain (DOM)

Add a Domain block. Keep Geometry type Line, x min 0 and x max 1. Tick Time dimension: t initial and t final appear, with defaults 0 and 1, which is what we want. Set Interior points to 2000, Default boundary points to 100 and Initial points to 100.

Check: the block now has an Initial output next to BC1 and BC2. If it does not, time is off.

Step 2: Equation (PDE)

Add a PDE block. Wire Domain Coords → PDE Coords and Domain Interior → PDE Interior. Set Equation type to Parabolic and Equation to Heat. Diffusivity a is already 0.1.

Check: the block's summary reads heat (parabolic).

Step 3: Boundary conditions (two BC blocks)

Add two Boundary condition blocks. Wire Domain BC1 into the first one's Boundary input and Domain BC2 into the second's. On a line, BC1 is $x = x_\text{min}$ and BC2 is $x = x_\text{max}$; hovering over a port says which. Both blocks keep the default Condition type, Dirichlet (value), with u = 0.0.

Step 4: Initial condition (IC)

Add an Initial condition block and wire Domain Initial → IC Initial. Set u(x, t_init) = to sin(pi*x). Formulas use ** for powers and pi for $\pi$. Leave Initial velocity unticked: the heat equation is first order in time, so the starting shape is all it needs.

Step 5: Network (NET)

Add a Network block and wire Domain Coords → Network Coords. Set Hidden layers to 32, 32, 32. Keep Activation tanh and Initialisation Xavier / Glorot.

Check: Plot network diagram draws 2 inputs. If it draws 1, time is off in the Domain.

Step 6: Loss (LOSS)

Add a Loss block. Wire PDE PDE residual → Loss PDE residual and Network Network → Loss Network, then the outputs of both BC blocks and of the IC block → Loss BC / IC (that input takes many wires). Keep Term norm Mean square and PDE weight 1. Set BC weights to [10, 10] and IC weights to [10]. Each list needs exactly one number per connected condition; the panel shows which block each position refers to.

Section 5 shows what a weight of 1 and of 100 would have done. Heat is more forgiving about weights than the 2D Poisson problem was, but too large a value is not free.

Step 7: Optimiser and Train

Add an Optimiser block: Adam, Learning rate 0.001, Epochs 5000. Add a Train block and wire Loss Loss → Train Loss and Optimiser Optimiser → Train Optimiser. Its defaults are fine. Add Visualisation and Report blocks, wired from Train Model, if you want the figures and the results table.

Check: the strip under the canvas reads Graph check: nothing to flag. A clean check means the configuration is consistent, not that the answer is accurate.

Step 8: Generate and run

Generate the Python code, download the script and run it on your own computer (pip install torch, then python your_script.py), as in Run the script. The Studio builds and checks the configuration, but it does not train the model. Training runs in the script you downloaded. On a laptop-class CPU this problem takes about a minute.

Step 9: Judge the result

Compare the prediction against the exact solution on a grid that was not used for training: here a $101\times101$ grid in $(x,t)$.

Exact heat solution, PINN prediction and absolute error over x and t

The first two panels are the same picture: a hump that fades as time goes up. The third panel shows where the error lives. It is largest along the bottom edge, where the starting profile is imposed and the network has to match the initial condition against the equation, and in a few soft patches in the interior. It is smallest at the cold ends.

A second view makes the decay concrete. Four snapshots in time, with the PINN dashed over the exact curve:

Temperature profiles at four times, exact against PINN

The measure we use is the relative $L^2$ error over the whole $(x,t)$ grid,

$$ \varepsilon = \frac{\lVert u_\theta - u_{\text{exact}} \rVert_2}{\lVert u_{\text{exact}} \rVert_2} . $$

A very physical extra check: the peak temperature at the middle of the bar at $t = 1$ should be $e^{-a\pi^2} = 0.3727$. The reference run gave 0.3725.

What to expect

These figures come from a hand-written reference implementation with exactly the settings above (random seed 0). The Studio's script may initialise and sample slightly differently, so your numbers will differ, but you should land in the same neighbourhood.

Epoch Total loss Relative $L^2$ error
0 $1 \times 10^{1}$ $1.7$
500 $5 \times 10^{-2}$ $5.0 \times 10^{-2}$
1000 $6 \times 10^{-3}$ $1.1 \times 10^{-2}$
2000 $2.5 \times 10^{-3}$ $8.3 \times 10^{-3}$
3000 $7 \times 10^{-4}$ $3.9 \times 10^{-3}$
5000 $2 \times 10^{-4}$ $1.3 \times 10^{-3}$

The largest pointwise error at the end was about $3 \times 10^{-3}$, on a solution whose largest value is 1.

The three loss terms and the error against the exact solution during training

Do not worry about spikes

Around epoch 4500 the error briefly jumped to about $2\times10^{-2}$ and then recovered by epoch 5000. That is ordinary Adam behaviour on a PINN loss. Judge a run by where it settles, and keep the best result, not simply the last.

5. How much do the weights matter?

The same problem was run three times with only the boundary-and-initial weight changed.

Weight on boundary and initial terms Relative $L^2$ error after 5000 epochs
1 $2.0 \times 10^{-3}$
10 $1.3 \times 10^{-3}$
100 $9.0 \times 10^{-3}$

Read this carefully. These are single runs with one random seed, so the gap between 1 and 10 is within the run-to-run scatter. The only clear message is that a weight of 100 was worse here: when the constraint terms dominate, the equation term barely matters to the optimiser and the interior is learned slowly. In the 2D Poisson guide a large boundary weight helped enormously; here a modest one is enough. There is no universal best weight. Start at 10, look at the three loss terms, and adjust only if one of them lags far behind the others.

6. If something goes wrong

What you see Likely cause What to try
The Graph check says the time derivative is dropped and the steady-state form is solved Time is off in the Domain Tick Time dimension in Step 1
The Graph check says no Initial condition block is wired in The IC block is missing, or not wired to Domain Initial and Loss BC / IC Add and wire it as in Step 4
The solution does not decay, or decays too fast or too slowly Wrong diffusivity $a$ Check $a = 0.1$ in Step 2. The decay rate is $a\pi^2$
The profile at $t=0$ is the wrong shape Typo in the initial-condition expression Re-enter sin(pi*x). Remember pi, not 3.14
Error is worst along the bottom edge The initial condition is under-weighted Raise its weight, for example to 10 or 30
Loss is nan Learning rate too high Lower to $10^{-4}$ and recheck the expressions
Right shape at early times, drifting later Too few interior points over the time range Raise Interior points on the Domain to 4000

7. Try it yourself

  1. A different diffusivity. Set $a = 0.5$. The exact solution is $e^{-0.5\pi^2 t}\sin(\pi x)$, which decays about five times faster. Only the PDE block changes. By $t = 1$ the bar is almost cold, so the relative error will look worse even if the absolute error is small. Why?
  2. A different mode. Start from $\sin(2\pi x)$. The exact solution is $e^{-4a\pi^2 t}\sin(2\pi x)$. Higher modes decay faster, by a factor of $n^2$ for mode $n$. Change only the initial condition.
  3. A hotter end. Hold $u(1,t)=1$ and start from $u(x,0)=\sin(\pi x)+x$ (the two ends are then consistent with the start). The exact solution is $e^{-a\pi^2 t}\sin(\pi x) + x$: the hump fades and the bar settles towards the straight line $u = x$. Change the initial condition and one boundary condition.
  4. Fewer points in time. Cut the interior points to 500. Does the error grow more at late times or early times?

8. Recap

  • A time-dependent PINN is the same machine with time as an extra input: the network is $u_\theta(x,t)$.
  • The initial condition is a constraint on the line $t=0$. It is a loss term of its own, with its own weight.
  • The Domain must have time switched on, and the Network must have 2 inputs. The Studio warns when either is wrong.
  • Check the answer against the exact solution on a fresh grid, and look at where the error lives. In this run it was largest at the start.
  • Weights matter, but unevenly. Heat tolerated a wide range; a very large weight was the one setting that hurt.

Next: a problem with no tidy formula, where you invent the answer first and build the problem around it, in Manufactured solutions.


Reference script for the numbers quoted above: heat_1d_reference.py. It is written by hand for checking this guide and is not the Studio's generated output.