# Heat and waves: problems in time So far nothing moved. This part lets heat spread and a string swing. A problem in time is stated like the ones before, with two things added: the time span, and the state at the start. ## Step 1: heat that spreads ```python import numpy as np import sympy as sp import pdemagic as pm x, y, t = sp.symbols("x y t", real=True) ``` The unknown is now a function of space and time. A hot spot in the plane spreads by the heat equation, $\partial T/\partial t = \tfrac12 \nabla^2 T$: ```python T = sp.Function("T")(x, y, t) spreading = pm.Problem( sp.Eq(T.diff(t), pm.laplacian(T) / 2), domain=pm.FreeSpace(x, y), time=(t, 0, 1), initial={T: sp.exp(-(x**2 + y**2))}, ) history = pm.solve(spreading) print(history) ``` ```text Solution(unknowns=['T'], grid=128x128, times=21) ``` `time=` names the symbol of time, and when the problem starts and ends. `initial=` gives the state at the start, for each unknown. ## Step 2: the solution over time The solution is recorded at 21 times, from the start to the end. The first axis of the values counts them: `history[T][i]` is the temperature at the time `history.times[i]`. ```python print(history[T].shape) print(history.times[:4]) ``` ```text (21, 128, 128) [0. 0.05 0.1 0.15] ``` Two numbers tell the story. The hottest point cools from 1 to a third, and the heat on the whole grid stays what it was, $\pi$: ```python hottest = history[T].max(axis=(1, 2)) print(np.round(hottest[[0, 10, 20]], 2)) print(np.round(history.integral()[[0, 10, 20]], 6)) ``` ```text [0.99 0.5 0.33] [3.141593 3.141593 3.141593] ``` The first number is 0.99 and not 1 because no grid point lies exactly on the centre of the spot. A plot shows one time, the last unless `time=` names another. The recorded time nearest to it is taken. The planner's box is wide, because it has to hold the heat at the end of the time span, so zoom in on the axes that `plot` returns: ```python ax = history.plot(time=0.25) ax.set(xlim=(-4, 4), ylim=(-4, 4)) ``` ## Step 3: heat between walls A problem in time on a [box](tutorial-2-boundaries.md) takes the conditions on its faces as before. This rod is warm in the middle and kept cold at both ends: ```python R = sp.Function("R")(x, t) rod = pm.Problem( sp.Eq(R.diff(t), R.diff(x, 2)), domain=pm.Box({x: (0, 1)}), time=(t, 0, 0.2), initial={R: sp.sin(sp.pi * x)}, boundary=sp.Eq(R, 0), ) cooling = pm.solve(rod) cooling.plot() ``` In one dimension a plot shows several times at once, from light to dark. The rod keeps its shape and cools by the factor $e^{-\pi^2 t}$, which the result follows to eleven digits: ```python (xs,) = cooling.axes exact = np.exp(-0.2 * np.pi**2) * np.sin(np.pi * xs) print(abs(cooling[R][-1] - exact).max() < 1e-11) ``` ```text True ``` ## Step 4: a string that swings The wave equation, $\partial^2 s/\partial t^2 = \partial^2 s/\partial x^2$, is of second order in time. It needs to know where the string is at the start, and how fast it moves. Leave the second out, and pdemagic asks for it: ```python s = sp.Function("s")(x, t) bump = sp.exp(-300 * (x - 0.3) ** 2) def string(initial): return pm.Problem( sp.Eq(s.diff(t, 2), s.diff(x, 2)), domain=pm.Box({x: (0, 1)}), time=(t, 0, 2), initial=initial, boundary=sp.Eq(s, 0), ) try: pm.solve(string({s: bump})) except pm.InvalidProblemError as error: print(error) ``` ```text `s` has no initial condition for its derivative in time, which an equation of second order in time needs ``` The rate of change at the start goes under the key `sp.Derivative(s, t)`. This string is pulled into a bump and let go from rest: ```python swinging = pm.solve(string({s: bump, sp.Derivative(s, t): 0})) swinging.plot(time=[0, 0.2, 0.5]) ``` `time=` takes a list in one dimension. The bump splits into two halves that run apart. The half that reaches the end at $x = 0$ comes back upside down, as a wave does at an end that is held. ## Step 5: a film `animate` shows all recorded times, one after the other: ```python film = swinging.animate() ``` `film` is a matplotlib animation. `film.save("string.gif")` writes it to a file, and in a notebook `film.to_jshtml()` shows it. ## Step 6: exact in time pdemagic does not step through time here. For a linear equation like these it evaluates the exact solution at every recorded time, so a result far in the future costs no more than one nearby, and is no less accurate. The string shows it. Its waves travel with the speed 1 on a string of the length 1, so after the time 2 they have gone back and forth once, and the string is where it started: ```python print(abs(swinging[s][-1] - swinging[s][0]).max() < 1e-10) ``` ```text True ``` Equations with nonlinear terms are stepped through time, with a controlled error. [Nonlinear equations in time](../topics/nonlinear-time.md) has those. ## What you have learned - A problem in time has `time=(t, start, end)` and `initial={unknown: state}`. The unknown is a function of space and time. - An equation of second order in time also needs `sp.Derivative(unknown, t)` in `initial`. - The first axis of `solution[unknown]` is the time, and `solution.times` holds the recorded times. - `plot(time=...)` draws one time, or several in one dimension, and `animate()` makes a film. - Linear equations are exact in time. [Problems in time](../topics/time.md) has the whole subject, and the gallery has [a plucked string](../gallery/plucked-string.md) and [heat that spreads](../gallery/heat-that-spreads.md). Next: [what pdemagic decided](tutorial-4-plans.md), where you read the plan and change it.