# Problems in time A problem in time asks how a state changes: heat that spreads, a string that swings, a drum that rings. This page says how such a problem is stated, why a linear equation needs no steps in time, and what changes from one kind of domain to the next. ```python import math import numpy as np import sympy as sp import pdemagic as pm from pdemagic import curves x, y, t = sp.symbols("x y t", real=True) ``` ## The time and the state at the start A problem that depends on time names its time and the time span, as `time=`, and the state at the start, as `initial=`. The unknown is a function of space and time. Heat that spreads in the plane: ```python T = sp.Function("T")(x, y, t) cooling = pm.Problem( sp.Eq(sp.Derivative(T, t), pm.laplacian(T) / 2), domain=pm.FreeSpace(x, y), time=(t, 0.0, 1.0), initial={T: sp.exp(-(x**2 + y**2))}, ) history = pm.solve(cooling) assert history[T].shape == (21, 128, 128) assert history.times[0] == 0.0 and history.times[-1] == 1.0 # The hot spot cools from 1 to a third. assert abs(history[T][-1].max() - 1 / 3) < 1e-2 history.plot(time=0.25) ``` The first axis of the result is the time: `history[T][i]` is the temperature at `history.times[i]`. A plot shows one time, by default the last, and `time=` picks another. The initial state is a formula, a number or an [array](data.md). ## Equations of second order in time The wave equation is of second order in time, and needs the initial rate of change as well, under the key `sp.Derivative(u, t)`: ```python s = sp.Function("s")(x, t) string = pm.Problem( sp.Eq(sp.Derivative(s, t, 2), 9 * sp.Derivative(s, x, 2)), domain=pm.FreeSpace(x), time=(t, 0.0, 1.0), initial={s: sp.exp(-(x**2)), sp.Derivative(s, t): 0}, ) plucked = pm.solve(string) # The bump splits into two halves that move apart with the speed 3. assert abs(plucked[s][-1].max() - 0.5) < 1e-2 film = plucked.animate() ``` `animate` shows all recorded times, one after the other. [Plotting](plotting.md) says what to do with the film. A first derivative in time on the left damps the waves, and the unknown itself on the right gives them a mass. ## Linear equations are propagated exactly pdemagic does not step through time with a difference scheme. It evaluates the exact solution for every wave of the grid, so a result far in the future costs no more than one nearby, and is no less accurate. The error is that of the grid in space alone. That holds for linear equations with constant coefficients. Beside the second derivatives, the right-hand side may contain first derivatives in space, which make the solution drift, the unknown itself, which makes it grow or decay, and a source that may depend on time. The Schrödinger equation is of this kind, with a [complex unknown](complex.md). Terms that are nonlinear, or whose coefficients vary, are stepped in time while the rest stays exact: see [Nonlinear equations in time](nonlinear-time.md). ## Derivatives above the second order In free space and on a periodic domain the right-hand side can be any linear operator with constant coefficients. A third derivative lets waves travel at speeds that depend on their length. In the linear Korteweg–de Vries equation the short ones are the fastest, and a hump sheds a train of ripples: ```python h = sp.Function("h")(x, t) ripples = pm.Problem( sp.Eq(h.diff(t) + h.diff(x, 3), 0), domain=pm.FreeSpace(x), time=(t, 0, 0.3), initial={h: sp.exp(-(x**2))}, ) train = pm.solve(ripples) # The ripples run to the left, and the box follows them. assert train[h][-1].min() < -0.2 assert train.plan.grid.lower[0] < -40 ``` pdemagic bounds how far such a solution travels from the waves of the initial state, and holds everything that leaves the box. A beam, $u_{tt} = -u_{xxxx}$, and a plate, with $\nabla^4$, are solved alike. ## The times that are recorded When the solution is recorded is part of the [plan](planner.md), as `plan.times`, with the `start`, the `end` and the `count`: 21 at first. ```python cooling_plan = pm.plan(cooling) cooling_plan.times.count = 5 assert len(pm.solve(cooling, cooling_plan).times) == 5 ``` When nothing that is given depends on time, each recorded time is exact in time, however few there are: they decide what you see, not how accurate it is. ## Between walls On a [box](boxes.md) a problem in time is stated in the same way, with the conditions on the faces added. What is given on a face may depend on time. One end of this rod is heated and cooled in turns, and the other end is insulated: ```python R = sp.Function("R")(x, t) driven = pm.Problem( sp.Eq(R.diff(t), R.diff(x, 2) / 10), domain=pm.Box({x: (0, 1)}), time=(t, 0, 2), initial={R: 0}, boundary={ (x, 0): sp.Eq(R, sp.sin(2 * sp.pi * t)), (x, 1): sp.Eq(R.diff(x), 0), }, ) waves_of_heat = pm.solve(driven) heated_end = waves_of_heat[R][:, 0] assert abs(heated_end - np.sin(2 * np.pi * waves_of_heat.times)).max() < 1e-12 waves_of_heat.plot() ``` In one dimension a plot shows several times at once, from light to dark. Heat, waves and the Schrödinger equation between walls are propagated exactly, as in free space. A condition cannot contain a derivative in time yet, so a wall that lets waves out without reflection is not available. ## On round domains A [disk, a ring, a ball, a shell or a cylinder](round.md) takes problems in time as a box does. A round drum that is struck off its centre: ```python r, phi = sp.symbols("r phi", real=True) disk = pm.Disk(r, phi, radius=1) d = sp.Function("d")(r, phi, t) X, Y = disk.cartesian kettledrum = pm.Problem( sp.Eq(d.diff(t, 2), pm.laplacian(d)), domain=disk, time=(t, 0, 2), initial={d: sp.exp(-40 * ((X - 0.3) ** 2 + Y**2)), sp.Derivative(d, t): 0}, boundary=sp.Eq(d, 0), ) ringing = pm.solve(kettledrum) assert ringing.plan.method.kind == "mode_propagator" ringing.plot(time=1.2) ``` The drum is propagated in its modes, whose frequencies lie at the zeros of the Bessel functions. On a round domain the operator in space is $a\,\nabla^2 u + c\,u$, written with `pm.laplacian`: it holds no first derivatives. ## On a shape On a [shape in the plane](regions.md) heat, waves and the Schrödinger equation are exact in time as well. pdemagic computes the modes of the shape, the patterns in which it cools or rings, and each of them evolves by itself. Half a disk whose diameter is warmed up while its arc is kept cold: ```python arc = curves.Arc((0, 0), 1, 0, math.pi) diameter = curves.Line((-1, 0), (1, 0)) H = sp.Function("H")(x, y, t) warming = pm.Problem( sp.Eq(H.diff(t), pm.laplacian(H)), domain=pm.Region(x, y, curves.Curve(arc, diameter)), time=(t, 0, 0.5), initial={H: 0}, boundary={ arc: sp.Eq(H, 0), diameter: sp.Eq(H, (1 - x**2) * (1 - sp.exp(-20 * t))), }, ) warmed = pm.solve(warming) assert warmed.plan.method.kind == "element_propagator" assert warmed.plan.method.steps > 1 warmed.plot(time=0.1) ``` What is given on the boundary changes in time here. Between two recorded times the plan then takes `method.steps`, within which what is given is interpolated in time. The planner chooses as many as that takes. On a shape the equation may hold more and stay exact: a potential, a medium inside a divergence, as in `pm.div(tension * pm.grad(w))`, and a density in front of the derivative in time, all of which may vary in space. A steady drift is part of the modes unless it is strong. The density and the medium must be positive. A density beside a damping, $\rho\,w_{tt} + \gamma\,w_t$, is not taken yet. Waves on a shape are less accurate than heat: a wave keeps every detail that it starts with. [Accuracy and diagnostics](accuracy.md) says how much to expect. ## See also - [Nonlinear equations in time](nonlinear-time.md), for terms that are stepped, and [Systems of equations](systems.md), for several unknowns. - [Recipes](../howto/index.md) for common tasks, and [What is solved where](../ref/equations.md) for every equation in time on every domain. - Gallery: [heat that spreads](../gallery/heat-that-spreads.md), [a rod with a flame](../gallery/rod-with-a-flame.md), [a plucked string](../gallery/plucked-string.md), [a hump that sheds ripples](../gallery/hump-that-sheds-ripples.md), [a struck drum](../gallery/struck-drum.md) and [heat in a shape](../gallery/heat-in-a-shape.md). - Methods: [linear equations in time](../methods/propagator.md), [boxes with boundary conditions](../methods/collocation.md), [round domains](../methods/round-domains.md) and [shapes in the plane, in time](../methods/elements-in-time.md).