Use measured data as a source

Use this when a term of the equation is known as an array and not as a formula: a charge density from a measurement, a heating from another program.

Steps

  1. Write the term as an undefined function of the coordinates, like the unknown: rho = sp.Function("rho")(x, y).

  2. Wrap the array with its grid. pm.GridData.from_axes(values, xs, ys) takes the points along each axis, and pm.GridData(values, lower, upper) the two corners of the grid.

  3. Hand it to the problem: data={rho: ...}.

  4. Solve, and read the diagnostics: they tell whether the array holds the whole source and resolves it.

Example

Two charges of opposite sign in the plane, given at the points of a grid:

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)
rho = sp.Function("rho")(x, y)

# The measurement: values[i, j] belongs to the point (xs[i], ys[j]).
xs = np.linspace(-8.0, 8.0, 129)
ys = np.linspace(-6.0, 6.0, 97)
gx, gy = np.meshgrid(xs, ys, indexing="ij")
values = np.exp(-((gx - 1) ** 2 + gy**2)) - np.exp(-((gx + 1) ** 2 + gy**2))

problem = pm.Problem(
    sp.Eq(pm.laplacian(u), -rho),
    domain=pm.FreeSpace(x, y),
    data={rho: pm.GridData.from_axes(values, xs, ys)},
)
solution = pm.solve(problem)
solution.plot()

# The solution lives on the grid of the data.
assert solution[u].shape == values.shape
assert "the grid of the data" in solution.plan.notes[1]
# The array holds the whole source, and resolves it.
assert solution.diagnostics["source_boundary_ratio"] < 1e-12
assert solution.diagnostics["spectral_tail"] < 1e-10
The figure that the example above draws. The figure that the example above draws.

Notes

  • Where a direction is free, the grid of the data is uniform and includes both ends. Whatever lies outside the array is treated as zero, so the array must cover the whole source.

  • The first axis of the array belongs to the first coordinate: np.meshgrid(..., indexing="ij") gives that layout.

  • The solution cannot be asked for on another grid: a plan whose grid differs from that of the data is refused.

  • Other domains have their own points, and the array is then passed without bounds, as pm.GridData(values). box.axes(shape) returns the Chebyshev points of a box with walls, and domain.axes(shape) the points of a disk, a ball or a cylinder. Data that was measured elsewhere must be brought to these points first.

  • On a shape in the plane the array lies on any uniform grid that covers the shape, with nan where the quantity is not known, and the planner chooses its own mesh.

  • Complex data is given as two real arrays: declare two functions A and B and write A + sp.I * B.

  • A coefficient, a value on a face and an initial state can be arrays in the same way: see Start from an array and Data from arrays. The gallery has a source from an array, and the reference describes pm.GridData.