Data from arrays¶
Any term of a problem that is known can be an array instead of a formula: a charge density from a measurement, a permittivity from a model, an initial state from another simulation. This page says how an array is handed to a problem, and on which grid it must lie.
import numpy as np
import sympy as sp
import pdemagic as pm
from pdemagic import curves
x, y, t = sp.symbols("x y t", real=True)
A source given as an array¶
Write the term as an undefined function, like the unknown, and hand its
values to the problem as data=. A pm.GridData holds the values with
the grid that they lie on. Two charges that were measured on a grid of
65 by 65 points:
axis = np.linspace(-8.0, 8.0, 65)
gx, gy = np.meshgrid(axis, axis, indexing="ij")
charge = np.exp(-((gx - 1) ** 2 + gy**2)) - np.exp(-((gx + 1) ** 2 + gy**2))
phi = sp.Function("phi")(x, y)
rho = sp.Function("rho")(x, y)
measured = pm.Problem(
sp.Eq(pm.laplacian(phi), -rho),
domain=pm.FreeSpace(x, y),
data={rho: pm.GridData.from_axes(charge, axis, axis)},
)
potential = pm.solve(measured)
assert potential[phi].shape == charge.shape
assert potential.diagnostics["source_boundary_ratio"] < 1e-12
potential.plot()
The solution lives on the grid of the data: the planner has no grid to
choose. The array must cover the whole source, because whatever lies
outside is treated as zero. The diagnostic source_boundary_ratio tells
you whether it does.
A coefficient is given in the same way: a permittivity, a potential.
An initial state given as an array¶
In a problem in time the state at the start can be an array
as well. Pass the GridData itself as the initial value:
T = sp.Function("T")(x, t)
points = np.linspace(-9.0, 9.0, 73)
cooling = pm.Problem(
sp.Eq(sp.Derivative(T, t), sp.Derivative(T, x, 2) / 2),
domain=pm.FreeSpace(x),
time=(t, 0.0, 1.0),
initial={T: pm.GridData.from_axes(np.exp(-(points**2)), points)},
)
history = pm.solve(cooling)
assert history[T].shape == (21, 73)
The grid of each domain¶
An array lies on the kind of grid that the method solves on, so the grid depends on the domain.
Free directions. The grid is uniform, and includes both ends.
GridData.from_axes(values, xs, ys, ...)takes the points along each axis, andGridData(values, lower, upper)the two corners.Periodic directions. The points start at the lower end of the period and stop one step short of the upper end, as with
numpy.linspace(..., endpoint=False).Boxes with walls. The points are Chebyshev points, which
box.axes(shape)returns.Disks, balls and cylinders. The grid is fixed by the shape of the array.
domain.axes(shape)returns its points, and the data is passed without bounds, aspm.GridData(values). A cylinder with boundary conditions has another grid:cylinder.axes(shape, boundary=True).Shapes in the plane. A uniform grid over the rectangle that holds the shape: see below.
What is given on a face of a box can be an array too. It is given like any other, on the whole grid, and only its values on the face are read:
box = pm.Box({x: (0, 1), y: (0, 1)})
bx, by = np.meshgrid(*box.axes((16, 16)), indexing="ij")
u = sp.Function("u")(x, y)
load = sp.Function("load")(x, y)
edge = sp.Function("edge")(x, y)
from_arrays = pm.Problem(
sp.Eq(pm.laplacian(u), load),
domain=box,
data={load: pm.GridData(2 * by), edge: pm.GridData(bx**2 * by)},
boundary=sp.Eq(u, edge),
)
assert abs(pm.solve(from_arrays)[u] - bx**2 * by).max() < 1e-11
On a disk or a ball the rim is not a point of the grid. An array for the rim is read off the outermost ring of grid points.
All arrays of one problem lie on the same grid. Solving on another grid than the one the data is given on is not possible yet, except on a shape in the plane.
On a shape¶
On a shape in the plane an array lies on a uniform grid
over the rectangle that holds the shape. region.axes(shape) returns
its points. Everything that may be a formula may be an array here: the
source, a coefficient, what is given on a piece of the boundary, the
initial state of a problem in time, and the starting value of a
nonlinear one.
plate = pm.Region(x, y, curves.Circle((0, 0), 1), curves.Circle((0.3, 0.1), 0.3))
xs, ys = plate.axes((65, 65))
px, py = np.meshgrid(xs, ys, indexing="ij")
heating = np.exp(-4 * ((px + 0.4) ** 2 + py**2))
w = sp.Function("w")(x, y)
q = sp.Function("q")(x, y)
warm = pm.solve(
pm.Problem(
sp.Eq(-pm.laplacian(w), q),
domain=plate,
data={q: pm.GridData(heating)},
boundary=sp.Eq(w, 0),
)
)
Unlike elsewhere, the solution does not live on the grid of the data. The planner chooses the elements as it always does, and reads the arrays to see how fine they must be. Between the points of its grid an array is read by a polynomial of degree five, so what it adds to the error falls with the sixth power of its spacing.
A solution on a shape is given on such a grid, with nan outside the
shape. So the solution of one problem can be the data of the next:
v = sp.Function("v")(x, y)
s = sp.Function("s")(x, y)
spread = pm.solve(
pm.Problem(
sp.Eq(-pm.laplacian(v) + v, s),
domain=plate,
data={s: pm.GridData(warm[w])},
boundary=sp.Eq(v, 0),
)
)
assert np.nanmax(spread[v]) < np.nanmax(warm[w])
spread.plot()
Two things to know:
Where an array holds
nan, it is not read. Near such points, and on the boundary beside them, the value comes from the numbers on the inner side. Givenanwherever the quantity is not known, not zero: a zero is a value, and a jump to zero right outside the boundary would be read as part of the data.Another grid than the one over the rectangle of the shape takes its bounds:
GridData(values, lower, upper)orGridData.from_axes. It must be uniform, cover the shape, and have at least six points along each axis.
Complex data¶
An array holds real numbers. A complex quantity is given as two arrays,
its real and its imaginary part: declare two data fields A and B and
write A + sp.I * B. Complex unknowns has
an example.
See also¶
Solutions, for the grid that a solution comes on.
Recipes for tasks with arrays.
Reference: GridData.
Gallery: a source from an array.
Methods: grids and arrays on a shape.