Start from an array

Use this when the state at the start of a problem in time is known as an array: a measured temperature, the last state of another simulation.

Steps

  1. Wrap the array with its grid, as for any other data: pm.GridData.from_axes(values, xs).

  2. Pass the GridData itself as the initial value: initial={T: pm.GridData.from_axes(values, xs)}. No data= is needed.

  3. Solve. The solution lives on the grid of the array.

Example

Heat that spreads along a line, from a temperature that is given at 73 points:

import numpy as np
import sympy as sp
import pdemagic as pm

x, t = sp.symbols("x t", real=True)
T = sp.Function("T")(x, t)

points = np.linspace(-9.0, 9.0, 73)
measured = np.exp(-(points**2))

cooling = pm.Problem(
    sp.Eq(T.diff(t), T.diff(x, 2) / 2),
    domain=pm.FreeSpace(x),
    time=(t, 0.0, 1.0),
    initial={T: pm.GridData.from_axes(measured, points)},
)
history = pm.solve(cooling)
history.plot()

assert history[T].shape == (21, 73)  # 21 times, each on the grid of the array
assert abs(history[T][0] - measured).max() < 1e-12
# At the end the bump is exp(-x²/3)/√3.
assert abs(history[T][-1] - np.exp(-(points**2) / 3) / np.sqrt(3)).max() < 1e-10
The figure that the example above draws. The figure that the example above draws.

Notes

  • An equation of second order in time takes two initial values. Each can be an array: initial={s: start, s.diff(t): rate} with two GridData on the same grid.

  • Where a direction is free, the grid is uniform and includes both ends, and the array must hold the whole state: outside it the state is taken to be zero. Along a periodic direction the points stop one step short of the upper end.

  • On a box with walls the array lies at the Chebyshev points that box.axes(shape) returns, and is passed without bounds: pm.GridData(values).

  • The solution of one problem can start the next: with (xs,) = earlier.axes, the last state is pm.GridData.from_axes(earlier[T][-1], xs).

  • The diagnostic spectral_tail tells whether the array resolves the state.

  • Data from arrays explains the grids of every domain, and Problems in time the initial values.