Evaluate a solution at a point¶
Use this when you need the solution at points that are not on its grid: at a probe, along a line, on the boundary.
Steps¶
Call the solution with one argument for each coordinate:
solution(0.5, 0.5).Pass arrays for many points at once. They are broadcast against each other, and the result has their shape.
Take a single value out of the result with
float(...).
Example¶
import numpy as np
import sympy as sp
import pdemagic as pm
x, y = sp.symbols("x y", real=True)
u = sp.Function("u")(x, y)
box = pm.Box({x: (0, 1), y: (0, 1)})
conditions = dict.fromkeys(box.faces, sp.Eq(u, 0))
conditions[y, 1] = sp.Eq(u, sp.sin(sp.pi * x))
heated = pm.solve(
pm.Problem(sp.Eq(pm.laplacian(u), 0), domain=box, boundary=conditions)
)
def exact(px, py):
return np.sin(np.pi * px) * np.sinh(np.pi * py) / np.sinh(np.pi)
# One point.
middle = float(heated(0.5, 0.5))
assert abs(middle - exact(0.5, 0.5)) < 1e-12
# Seven points along the line y = 0.25.
line = np.linspace(0, 1, 7)
along = heated(line, 0.25)
assert along.shape == (7,)
assert abs(along - exact(line, 0.25)).max() < 1e-12
# A grid of your own: 5 by 3 points.
own = heated(line[:5, None], np.array([0.1, 0.5, 0.9])[None, :])
assert own.shape == (5, 3)
# A point outside the domain has no value.
assert np.isnan(heated(1.5, 0.5))
Notes¶
The value is not interpolated from the neighbours of the point. It is the value of the polynomial or the trigonometric sum that the method solved with, so it is as accurate as the values on the grid.
This works on boxes with walls, on disks, rings and cylinders with boundary conditions, on shapes in the plane, and on domains that are periodic in every direction. On a round domain the arguments are its own coordinates, such as the radius and the angle.
Where a direction is free it does not work yet, and neither in a ball nor in a cylinder with vacuum outside. pdemagic raises
UnsupportedProblemErrorthere. Choose the grid so that it holds your points: see Choose the grid yourself.For a problem in time the first axis of the result is the time, and for an eigenproblem the state. There are no values at times between those that are recorded.
With several unknowns, name the one you mean:
solution(0, 0, unknown=w).On a shape in the plane this is the way to values on the boundary, where the grid has no points.
A value sums over the whole grid, so many points on a large grid take time: a thousand points on a grid of \(64^3\) take a tenth of a second.
Solutions explains what a solution holds, and the reference lists its methods.