Planning and solving

pm.plan returns the plan for a problem, pm.solve solves it, with the planner’s plan or with yours, and pm.load reads a solution back from a file.

Plan and solve

pdemagic.plan(problem)

Choose how to solve a problem, without solving it.

Parameters:

problem (Problem) – The problem to plan for.

Returns:

The plan the planner would use. Change it and pass it to solve() to take control of any choice.

Return type:

Plan

Raises:
pdemagic.solve(problem, plan=None)

Solve a problem.

Parameters:
  • problem (Problem) – The problem to solve.

  • plan (Plan | None) – How to solve it. If omitted, the planner chooses everything.

Returns:

The computed unknowns, with the plan that produced them.

Return type:

Solution

Raises:

The plan

What a plan holds, entry by entry, is on the page of the methods of a plan.

class pdemagic.Plan(data)

Every choice needed to solve a problem.

pdemagic.plan() returns the plan the planner would use. Read it, change what you want to control, and pass it to pdemagic.solve(). Each entry is an attribute, and nested entries are reached with dots.

Parameters:

data (Mapping[str, Any]) – The plan as a dictionary, as returned by to_dict().

Examples

>>> plan = Plan(
...     {
...         "method": {"kind": "fourier"},
...         "grid": {"lower": [-1.0], "upper": [1.0], "shape": [16]},
...         "notes": [],
...     }
... )
>>> plan.grid.shape = (32,)
>>> plan.grid.shape
[32]
>>> print(plan)
method:
  kind: fourier
grid:
  lower: [-1.0]
  upper: [1.0]
  shape: [32]
notes:
  - Changed after planning: grid.shape. The other notes are the planner's.
classmethod from_json(text)

Read a plan from JSON.

Parameters:

text (str)

Return type:

Plan

to_json()

Return the plan as JSON.

Return type:

str

copy()

Return an independent copy of the plan.

Return type:

Plan

to_dict()

Return an independent copy of the settings as a dictionary.

Return type:

dict[str, Any]

Solutions in files

A solution is written by its method save, which is on the page of the solution.

pdemagic.load(path)

Read a solution from a file that Solution.save() wrote.

The solution comes back with its values, its plan, its diagnostics and its recorded times, or its eigenvalues. It can be indexed, called for values at any points, plotted and animated, as the one that was saved. Its problem is a SavedProblem: the domain and what the solver read, without the sympy expressions of the equations.

Loading executes nothing that the file holds: a file from somebody else is safe to open.

Parameters:

path (str | Path) – The file. The ending .npz may be left out.

Returns:

The solution that was saved.

Return type:

Solution

Raises:

ValueError – If the file is not a solution that pdemagic saved, or was written in a format that this version does not read.

Examples

>>> import tempfile
>>> import sympy as sp
>>> import pdemagic as pm
>>> x = sp.Symbol("x", real=True)
>>> u = sp.Function("u")(x)
>>> ring = pm.Space(x, periodic={x: (0, 1)})
>>> problem = pm.Problem(sp.Eq(u.diff(x, 2), sp.sin(2 * sp.pi * x)), domain=ring)
>>> solution = pm.solve(problem)
>>> with tempfile.TemporaryDirectory() as folder:
...     kept = pm.load(solution.save(f"{folder}/ring"))
>>> bool((kept[u] == solution[u]).all())
True
class pdemagic.SavedProblem(description, frontend)

The problem of a solution that was loaded from a file.

It holds what a solution needs of its problem: the domain, the time span, and the description that the solver read. It does not hold the equations as sympy expressions, and cannot be solved again: keep the script that states the problem for that.

Parameters:
  • description (str)

  • frontend (dict[str, Any])

domain

The domain, made anew from how it was described. Its coordinates are symbols with the names of the original ones.

Type:

Space, Box, Disk, Ball, Cylinder or Region

time

The symbol of the time with the start and the end of the time span, or None for a problem that does not depend on time.

Type:

tuple or None

eigenvalue

The symbol of the eigenvalue with the number of eigenvalues that were asked for, or None for a problem that is no eigenproblem.

Type:

tuple or None

to_json()

Return the problem as the solver read it, as JSON.

Return type:

str

to_dict()

Return the problem as the solver read it, as plain data.

Return type:

dict[str, Any]

See also: The planner and its plan and Solutions.