Choose the times that are recorded

Use this when a problem in time should be recorded at more times, at fewer, or only over a part of its time span.

Steps

  1. Ask for the plan: plan = pm.plan(problem). Its entry times holds start, end and count. The planner records 21 times over the whole span.

  2. Set plan.times.count for the number of times. They are equally spaced from plan.times.start to plan.times.end, which you can set too.

  3. Solve with the plan. solution.times holds the recorded times, and they count the first axis of the values.

Example

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)

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: sp.exp(-(x**2))},
)
assert len(pm.solve(cooling).times) == 21

plan = pm.plan(cooling)
plan.times.start = 0.5
plan.times.count = 6
history = pm.solve(cooling, plan)
history.plot()

assert np.allclose(history.times, [0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
assert history[T].shape[0] == 6

# The first recorded state is the one at 0.5: exp(-x²/2)/√2.
(xs,) = history.axes
assert abs(history[T][0] - np.exp(-(xs**2) / 2) / np.sqrt(2)).max() < 1e-8
The figure that the example above draws. The figure that the example above draws.

Notes

  • The recorded times must lie within the time span of the problem. To solve further, change time= in the problem.

  • With count = 1 only the end is recorded.

  • A linear equation is propagated exactly, so each recorded time is as accurate as any other, however many there are.

  • An animation has one frame for each recorded time: see Animate a solution.

  • solution.plot(time=0.7) draws the recorded time nearest to 0.7. The solution has no values at times between those that are recorded.

  • method.steps is something else: the steps between two recorded times, for a source or boundary values that depend on time.

  • Problems in time explains what is recorded, and the reference describes the plan.