Reuse a plan

Use this when a solve should be repeated exactly: on another machine, next month, or for a report that must say how its numbers were made.

Steps

  1. Get the plan that you want to keep: pm.plan(problem) with your changes, or solution.plan of a solve that went well.

  2. Write it out as text: plan.to_json().

  3. Later, state the same problem, read the plan back with pm.Plan.from_json(text), and pass it to pm.solve(problem, plan). The planner is not asked again.

Example

import tempfile
from pathlib import Path

import sympy as sp
import pdemagic as pm

x, y = sp.symbols("x y", real=True)
u = sp.Function("u")(x, y)
problem = pm.Problem(
    sp.Eq(pm.laplacian(u), -sp.exp(-(x**2 + y**2) / 2)),
    domain=pm.FreeSpace(x, y),
)

# Today: a plan with a grid of your own, kept in a file.
plan = pm.plan(problem)
plan.grid.shape = (96, 96)
first = pm.solve(problem, plan)

file = Path(tempfile.mkdtemp()) / "plan.json"
file.write_text(plan.to_json())

# Later: the same problem, solved with the plan from the file.
kept = pm.Plan.from_json(file.read_text())
again = pm.solve(problem, kept)

assert kept.grid.shape == [96, 96]
assert (again[u] == first[u]).all()
assert again.plan.to_json() == plan.to_json()

Notes

  • A plan is plain data: the method, the grid, the tolerances and the notes. It holds neither the problem nor the solution, so keep the script that states the problem beside it.

  • A plan fits the problem it was made for. With another problem it is either executed as it stands or refused with InvalidPlanError: it is not adapted.

  • Two solves from one plan give the same numbers. The solve that made the plan, pm.solve(problem) without a plan, may differ from them in the last digits.

  • plan.copy() returns a plan of its own that you can change, for solving one problem in several ways.

  • A solution that is saved to a file carries its plan: pm.load(path).plan. See Save and load a solution.

  • plan.to_dict() gives the same content as a dictionary.

  • The planner and its plan explains the entries, and the reference describes pm.Plan.