Scientific ML Studio
Learn/ Studio Tour/ 5.2
5.2 · Put it together

Change a starter

The quickest way to your own problem is to start from one that already works and change one thing at a time. We take Heat in a bar, change a number, then change a boundary condition.

Building from an empty canvas is good for learning the blocks. For real work you will usually start from something close to what you want. A starter is a complete, wired problem, so a change you make has exactly one possible cause: the thing you changed. When a run goes wrong, you know where to look.

The problem here is heat flowing along a bar, with the ends held at zero temperature:

$$ u_t = a\,u_{xx}, \qquad u(0,t) = u(1,t) = 0, \qquad u(x, 0) = \sin(\pi x), \qquad a = 0.1 . $$

Its exact solution is $u(x,t) = e^{-a\pi^2 t}\sin(\pi x)$: the sine shape stays, and its height decays with time.

1. Open the starter and look first

On the Lab page, choose Heat in a bar under Start from, and press Create notebook. Before you change anything, read the canvas.

  • The Domain is a line, and Time dimension is ticked. That is why it has an Initial output as well as BC1 and BC2.
  • The PDE block is set to Heat, with Diffusivity a equal to 0.1.
  • There are two BC blocks, both Dirichlet with u = 0.0: the cold ends.
  • There is one IC block, with u(x, t_init) = sin(pi*x).
  • The rest are the usual: Network, Loss, Optimiser, Train, Visualisation and Report.

The Graph check reads nothing to flag. A starter is always complete.

Your screenshot · the Heat in a bar starter as you first see it, with the Domain's Time group open in the panel.

2. Run it unchanged

Generate, download and run it, as in Run the script. Because this problem has a known answer you can judge the result. One simple check is the temperature at the middle of the bar at $t = 1$:

$$ u(0.5,\,1) = e^{-0.1\,\pi^2} \approx 0.373 . $$

Open figures/field_t1.png. The curve should be a half-sine whose peak is near 0.37, compared with 1.0 in figures/field_t0.png.

For a number to compare against, we ran this starter twice from different starting weights, and measured the relative error against the exact solution over the whole $(x, t)$ square. It came out at about $1\times10^{-2}$ in one run and $1\times10^{-3}$ in the other. A check you can do by eye is the cold end: at $x = 0$ the temperature should be exactly zero, and in those two runs the network gave $-0.0076$ and $-0.0012$ at $t = 1$. Two runs are enough to show that the spread between runs is as big as the error itself.

3. Change a number

Select the PDE block and change Diffusivity a from 0.1 to 0.05. Nothing else. Regenerate and run.

A smaller diffusivity means slower decay, so the peak at $t = 1$ should now be

$$ e^{-0.05\,\pi^2} \approx 0.610 . $$

You predicted the answer before running. That is the habit worth building: say what you expect first. If the field at $t=1$ showed a peak of 0.37 again, you would know at once that the script you ran was the old one.

Regenerate after every change

The script is a snapshot. Changing the notebook does not change a file you downloaded earlier. After each edit, press Generate code or Download .py again and run the new file.

4. Change a boundary condition

Now make the left end insulated instead of cold: no heat flows through it. This is a Neumann condition, and two blocks have to change.

  1. Select BC 1. It is attached to the Domain's BC1, which on a line is $x = x_{\min}$, the left end (hover over its output to confirm). Set Condition type to Neumann (normal flux). The field du/dn = appears. Leave it at 0.0, which means no flux.
  2. Select the IC block and change the formula to cos(pi*x/2).

Why the second change? The old profile $\sin(\pi x)$ starts at zero at the left end and rises steeply there, so its slope at $x = 0$ is $\pi$, not zero. It contradicts the new boundary condition at the very first instant, and the network has to compromise at the corner. The profile $\cos(\pi x/2)$ has zero slope at $x=0$ and is zero at $x=1$, so it fits both ends. Choosing initial data that agrees with the boundary conditions is part of setting up a problem.

The exact solution of the new problem is

$$ u(x,t) = e^{-a\,(\pi/2)^2\,t}\cos\!\left(\tfrac{\pi x}{2}\right), \qquad a = 0.1 . $$

At the insulated end, $x = 0$, and at $t = 1$ it is $e^{-0.1\,\pi^2/4} \approx 0.781$. Our two runs of this version gave $0.7818$ and $0.7806$ there, and relative errors over the whole $(x,t)$ square of about $5\times10^{-4}$ and $1\times10^{-3}$. That the left end now sits at 0.78 and not 0 is the insulated condition at work: the heat cannot escape that way, so the bar cools more slowly.

Your screenshot · BC 1's panel with Neumann (normal flux) chosen and du/dn = 0.0, and the IC panel with cos(pi*x/2).

Which way is outward?

A Neumann value is the derivative along the outward normal. At the left end that normal points left, so $\partial u/\partial n = -u_x$. For a value of zero the sign does not matter. For a non-zero flux it matters a great deal; see Set the boundary and initial conditions.

5. What happens when you break something

The Graph check is easy to meet by doing something wrong on purpose. On the Domain block, untick Time dimension and look at the strip:

PDE: a parabolic equation evolves in time, but the Domain has no time dimension, so its time derivative is dropped and the steady-state form is solved. Tick the time box on the Domain block for the time-dependent problem.

An Initial condition is wired in but the Domain has no time dimension, so there is no initial surface to apply it on.

Both notes disappear when you tick the box again. Every note is listed on When the Graph check complains.

6. Habits for changing a notebook

  • One change at a time, and run after each. When two changes are made together and the result is wrong, you cannot tell which one did it.
  • Predict first. Write down the exact answer, or at least the size and sign you expect.
  • Keep a good version. The Lab keeps one copy of each notebook. Before a risky experiment, press Download notebook, or create a second notebook from the same starter and work in that.
  • Compare to something. An exact solution is best. A simpler case of the same problem is the next best.

Next: every note the Graph check can show.