Skip to content

Green's functions

For a linear differential operator \(\mathcal{L}\) the Green's function solves \(\mathcal{L} G(\cdot, \vect{x}') = \delta(\cdot - \vect{x}')\), and the response to a source \(f\) is the integral operator \(u(\vect{x}) = \int G(\vect{x}, \vect{x}') f(\vect{x}') \mathrm{d}\vect{x}'\). In free space, with \(r = \lVert \vect{x} - \vect{x}' \rVert\), integrix ships three.

Operator D \(G(r)\) Name
\(-\nabla^2\) 2 \(-\frac{1}{2\pi} \ln r\) PoissonGreen(2)
\(-\nabla^2\) 3 \(\frac{1}{4\pi r}\) PoissonGreen(3)
\(\kappa^2 - \nabla^2\) 2 \(\frac{1}{2\pi} K_0(\kappa r)\) ScreenedPoissonGreen(2, κ)
\(\kappa^2 - \nabla^2\) 3 \(\frac{1}{4\pi r} e^{-\kappa r}\) ScreenedPoissonGreen(3, κ)
\(\partial_t - K \nabla^2\) D \((4\pi K t)^{-D/2} e^{-r^2 / (4 K t)}\) HeatGreen(D, K, t)

\(K_0\) is the modified Bessel function of the second kind. The screened-Poisson rows are the Matérn link: the field with \((\kappa^2 - \nabla^2) x = w\) for white noise \(w\), which gaussx's spde_precision discretises, has covariance \(G \star G\).

Each kernel is a KernelLike with a cell_average, the kernel averaged over a cell centred on the source. green_operator builds the IntegralOperator \(u(\vect{x}_i) = \sum_j G(\vect{x}_i, \vect{s}_j) w_j f_j\) and, given cell_size, puts the cell average where a target coincides with a node, where a singular kernel is infinite. The cell averages are exact for the Poisson kernel (over a rectangle in 2-D, \(\langle \ln r \rangle = \ln h - \frac{1}{2} \ln 2 - \frac{3}{2} + \frac{\pi}{4}\) for a square of side \(h\), and over a box in 3-D) and for the heat kernel (a product of error functions); for the screened kernel they add the bounded remainder at \(r = 0\) to the Poisson average, which is accurate to \(O((\kappa h)^2)\).

Singular quadrature stops at that diagonal cell average (geostack's open question GS-O3): every other entry is the point value of the kernel. With targets on the nodes and a smooth source, a manufactured 2-D Poisson solution converges at second order in the cell size; a target within a cell of a node but not on it sees the singularity unaveraged and loses that order, which the tests show. Kernels with physics parameters, such as the Gaussian plume, stay with their physics in plumax. The design is missing_operators.md in geostack.

green

Green's functions: free-space kernels for −∇², κ² − ∇² and ∂_t − K∇².

PoissonGreen, ScreenedPoissonGreen and HeatGreen are KernelLike kernels with a cell_average for coincident points; green_operator builds the integrix.operator.IntegralOperator u(xᵢ) = ∑ⱼ G(xᵢ, sⱼ) wⱼ fⱼ from one of them and a rule. Kernels with physics parameters (the Gaussian plume) live with their physics, in plumax.

HeatGreen

Bases: Module

The heat kernel, the Green's function of ∂_t − K∇² at time t.

G(r) = (4πKt)^(−D/2) exp(−r²/(4Kt))

It integrates to 1 over ℝᴰ and is a semigroup: ∫ G_t(x, z) G_s(z, y) dz = G_(t+s)(x, y).

Attributes:

Name Type Description
dim int

D, at least 1.

diffusivity Float[Array, '']

K, positive.

time Float[Array, '']

t, positive.

Raises:

Type Description
ValidationError

If dim is not a positive int.

Examples:

>>> import jax.numpy as jnp
>>> green = HeatGreen(1, 0.5, 2.0)
>>> value = green.pairwise(jnp.zeros(1), jnp.zeros(1))
>>> bool(jnp.allclose(value, 1 / jnp.sqrt(4 * jnp.pi)))
True
pairwise
pairwise(x: Float[Array, ' dim'], x_prime: Float[Array, ' dim']) -> Float[Array, '']

G(x, x′) at the kernel's time.

Parameters:

Name Type Description Default
x Float[Array, ' dim']

The field point.

required
x_prime Float[Array, ' dim']

The source point.

required

Returns:

Type Description
Float[Array, '']

The kernel value.

Source code in src/integrix/_src/green/_kernels.py
def pairwise(
    self, x: Float[Array, " dim"], x_prime: Float[Array, " dim"]
) -> Float[Array, ""]:
    """G(x, x′) at the kernel's time.

    Args:
        x: The field point.
        x_prime: The source point.

    Returns:
        The kernel value.
    """
    spread = 4 * self.diffusivity * self.time
    squared = jnp.sum((x - x_prime) ** 2)
    # (4πKt)^(−D/2) exp(−r²/(4Kt))
    return jnp.exp(-squared / spread) / (math.pi * spread) ** (self.dim / 2)
cell_average
cell_average(cell_size: Float[Array, ' dim']) -> Float[Array, '']

G averaged over a box centred on the source, in closed form.

⟨G⟩ = ∏_d erf(a_d / (4 √(Kt))) / a_d

Parameters:

Name Type Description Default
cell_size Float[Array, ' dim']

The box's side lengths a_d, one per dimension.

required

Returns:

Type Description
Float[Array, '']

(1/|cell|) ∫_cell G(x, 0) dx.

Raises:

Type Description
ValidationError

If cell_size does not have one entry per dimension.

Source code in src/integrix/_src/green/_kernels.py
def cell_average(self, cell_size: Float[Array, " dim"]) -> Float[Array, ""]:
    """G averaged over a box centred on the source, in closed form.

        ⟨G⟩ = ∏_d erf(a_d / (4 √(Kt))) / a_d

    Args:
        cell_size: The box's side lengths a_d, one per dimension.

    Returns:
        (1/|cell|) ∫_cell G(x, 0) dx.

    Raises:
        ValidationError: If ``cell_size`` does not have one entry per
            dimension.
    """
    cell_size = jnp.asarray(cell_size)
    _check_cell(cell_size, self.dim)
    width = 4 * jnp.sqrt(self.diffusivity * self.time)
    return jnp.prod(erf(cell_size / width) / cell_size)

PoissonGreen

Bases: Module

The free-space Green's function of −∇² in two or three dimensions.

G(r) = −(1/2π) ln r  (D = 2),    G(r) = 1/(4π r)  (D = 3),    r = ‖x − x′‖

so u = ∫ G(x, x′) f(x′) dx′ solves −∇²u = f with u decaying (D = 3) or growing like −(1/2π) ln r times the total source (D = 2).

Attributes:

Name Type Description
dim int

D, 2 or 3.

Raises:

Type Description
ValidationError

If dim is not 2 or 3.

Examples:

>>> import jax.numpy as jnp
>>> green = PoissonGreen(3)
>>> value = green.pairwise(jnp.zeros(3), jnp.array([0.0, 0.0, 2.0]))
>>> bool(jnp.allclose(value, 1 / (8 * jnp.pi)))
True

On the diagonal it is infinite; the cell average is finite:

>>> bool(jnp.isinf(green.pairwise(jnp.zeros(3), jnp.zeros(3))))
True
>>> bool(jnp.isfinite(green.cell_average(jnp.ones(3))))
True
pairwise
pairwise(x: Float[Array, ' dim'], x_prime: Float[Array, ' dim']) -> Float[Array, '']

G(x, x′), +∞ where x = x′.

Parameters:

Name Type Description Default
x Float[Array, ' dim']

The field point.

required
x_prime Float[Array, ' dim']

The source point.

required

Returns:

Type Description
Float[Array, '']

The kernel value.

Source code in src/integrix/_src/green/_kernels.py
def pairwise(
    self, x: Float[Array, " dim"], x_prime: Float[Array, " dim"]
) -> Float[Array, ""]:
    """G(x, x′), +∞ where x = x′.

    Args:
        x: The field point.
        x_prime: The source point.

    Returns:
        The kernel value.
    """
    distance, positive = _distance(x, x_prime)
    if self.dim == 2:
        # G = −(1/2π) ln r
        value = -jnp.log(distance) / (2 * math.pi)
    else:
        # G = 1/(4π r)
        value = 1.0 / (4 * math.pi * distance)
    return jnp.where(positive, value, jnp.inf)
cell_average
cell_average(cell_size: Float[Array, ' dim']) -> Float[Array, '']

G averaged over a cell centred on the source, in closed form.

For D = 2 it is −(1/2π) ⟨ln r⟩ over an a × b rectangle, and for D = 3 it is ⟨1/r⟩/(4π) over an a × b × c box; for a square of side h, ⟨ln r⟩ = ln h − ½ ln 2 − 3/2 + π/4.

Parameters:

Name Type Description Default
cell_size Float[Array, ' dim']

The cell's side lengths, one per dimension.

required

Returns:

Type Description
Float[Array, '']

(1/|cell|) ∫_cell G(x, 0) dx.

Raises:

Type Description
ValidationError

If cell_size does not have one entry per dimension.

Source code in src/integrix/_src/green/_kernels.py
def cell_average(self, cell_size: Float[Array, " dim"]) -> Float[Array, ""]:
    """G averaged over a cell centred on the source, in closed form.

    For D = 2 it is −(1/2π) ⟨ln r⟩ over an a × b rectangle, and for
    D = 3 it is ⟨1/r⟩/(4π) over an a × b × c box; for a square of side
    h, ⟨ln r⟩ = ln h − ½ ln 2 − 3/2 + π/4.

    Args:
        cell_size: The cell's side lengths, one per dimension.

    Returns:
        (1/|cell|) ∫_cell G(x, 0) dx.

    Raises:
        ValidationError: If ``cell_size`` does not have one entry per
            dimension.
    """
    cell_size = jnp.asarray(cell_size)
    _check_cell(cell_size, self.dim)
    if self.dim == 2:
        return -_mean_log_distance_rectangle(cell_size) / (2 * math.pi)
    return _mean_inverse_distance_box(cell_size) / (4 * math.pi)

ScreenedPoissonGreen

Bases: Module

The free-space Green's function of κ² − ∇² in two or three dimensions.

G(r) = K₀(κr)/(2π)  (D = 2),    G(r) = e^(−κr)/(4π r)  (D = 3)

with K₀ the modified Bessel function of the second kind. The field x with (κ² − ∇²) x = w for white noise w has covariance G ⋆ G, the Matérn link that gaussx's spde_precision discretises.

Attributes:

Name Type Description
dim int

D, 2 or 3.

inverse_length Float[Array, '']

κ, an inverse length, positive.

Raises:

Type Description
ValidationError

If dim is not 2 or 3.

Examples:

>>> import jax.numpy as jnp
>>> from scipy.special import k0
>>> green = ScreenedPoissonGreen(2, 3.0)
>>> value = green.pairwise(jnp.zeros(2), jnp.array([0.5, 0.0]))
>>> bool(jnp.allclose(value, k0(1.5) / (2 * jnp.pi), rtol=1e-13))
True
pairwise
pairwise(x: Float[Array, ' dim'], x_prime: Float[Array, ' dim']) -> Float[Array, '']

G(x, x′), +∞ where x = x′.

Parameters:

Name Type Description Default
x Float[Array, ' dim']

The field point.

required
x_prime Float[Array, ' dim']

The source point.

required

Returns:

Type Description
Float[Array, '']

The kernel value.

Source code in src/integrix/_src/green/_kernels.py
def pairwise(
    self, x: Float[Array, " dim"], x_prime: Float[Array, " dim"]
) -> Float[Array, ""]:
    """G(x, x′), +∞ where x = x′.

    Args:
        x: The field point.
        x_prime: The source point.

    Returns:
        The kernel value.
    """
    distance, positive = _distance(x, x_prime)
    scaled = self.inverse_length * distance
    if self.dim == 2:
        # G = K₀(κr)/(2π)
        value = _bessel_k0(scaled) / (2 * math.pi)
    else:
        # G = e^(−κr)/(4π r)
        value = jnp.exp(-scaled) / (4 * math.pi * distance)
    return jnp.where(positive, value, jnp.inf)
cell_average
cell_average(cell_size: Float[Array, ' dim']) -> Float[Array, '']

G averaged over a cell centred on the source, to O((κh)²).

G is the Poisson kernel plus a bounded remainder: K₀(κr) = −ln(κr/2) − γ + O((κr)² ln κr) in 2-D, with γ Euler's constant, and e^(−κr)/r = 1/r − κ + O(κ² r) in 3-D. The singular part is averaged exactly (PoissonGreen.cell_average) and the remainder is taken at r = 0, so the error is O((κh)² ln κh) in 2-D and O(κ² h) in 3-D for a cell of size h; it suits cells smaller than the screening length 1/κ.

Parameters:

Name Type Description Default
cell_size Float[Array, ' dim']

The cell's side lengths, one per dimension.

required

Returns:

Type Description
Float[Array, '']

(1/|cell|) ∫_cell G(x, 0) dx, approximately.

Raises:

Type Description
ValidationError

If cell_size does not have one entry per dimension.

Source code in src/integrix/_src/green/_kernels.py
def cell_average(self, cell_size: Float[Array, " dim"]) -> Float[Array, ""]:
    """G averaged over a cell centred on the source, to O((κh)²).

    G is the Poisson kernel plus a bounded remainder: K₀(κr) =
    −ln(κr/2) − γ + O((κr)² ln κr) in 2-D, with γ Euler's constant, and
    e^(−κr)/r = 1/r − κ + O(κ² r) in 3-D. The singular part is averaged
    exactly (`PoissonGreen.cell_average`) and the remainder is taken at
    r = 0, so the error is O((κh)² ln κh) in 2-D and O(κ² h) in 3-D for a
    cell of size h; it suits cells smaller than the screening length 1/κ.

    Args:
        cell_size: The cell's side lengths, one per dimension.

    Returns:
        (1/|cell|) ∫_cell G(x, 0) dx, approximately.

    Raises:
        ValidationError: If ``cell_size`` does not have one entry per
            dimension.
    """
    singular = PoissonGreen(self.dim).cell_average(cell_size)
    if self.dim == 2:
        # K₀(κr)/(2π) + (1/2π) ln r → (−ln(κ/2) − γ)/(2π) as r → 0
        remainder = (-jnp.log(self.inverse_length / 2) - _EULER_GAMMA) / (
            2 * math.pi
        )
    else:
        # (e^(−κr) − 1)/(4π r) → −κ/(4π) as r → 0
        remainder = -self.inverse_length / (4 * math.pi)
    return singular + remainder

green_operator

green_operator(kernel: KernelLike, rule: RuleLike, targets: Float[Array, 'target dim'] | None = None, *, cell_size: Float[Array, ' dim'] | None = None) -> IntegralOperator

The Green's operator u(xᵢ) = ∑ⱼ G(xᵢ, sⱼ) wⱼ fⱼ, cell averages on the diagonal.

Where a target coincides with a node a singular kernel is infinite, so with cell_size the entry takes the kernel's cell_average instead: wⱼ ⟨G⟩_cell ≈ ∫_cell G, exact when the node's weight is its cell's volume. Singular quadrature stops there (geostack's GS-O3): every other entry is the point value G(xᵢ, sⱼ). With a smooth source on a regular grid and targets on the nodes the result is second order in the cell size; a target closer to a node than a cell but not on it sees the singularity unaveraged and loses that order.

Parameters:

Name Type Description Default
kernel KernelLike

G, any KernelLike; cell_average is needed with cell_size.

required
rule RuleLike

The nodes sⱼ and weights wⱼ, the cell centres and volumes of a grid (functax's cell_area) for a field of sources.

required
targets Float[Array, 'target dim'] | None

The target points xᵢ; the rule's nodes when omitted.

None
cell_size Float[Array, ' dim'] | None

The side lengths of the cell around each node, for the diagonal; without it the kernel is used as it is.

None

Returns:

Type Description
IntegralOperator

The operator, an IntegralOperator.

Raises:

Type Description
ValidationError

If cell_size is given and the kernel has no cell_average.

Examples:

The Poisson operator on a square grid, targets on the nodes:

>>> import jax.numpy as jnp
>>> from functax.quad import Rule
>>> centres = (jnp.arange(4) + 0.5) / 4
>>> x, y = jnp.meshgrid(centres, centres, indexing="ij")
>>> grid = Rule(jnp.stack([x, y], -1), jnp.full((4, 4), 1 / 16))
>>> cell = jnp.array([0.25, 0.25])
>>> operator = green_operator(PoissonGreen(2), grid, cell_size=cell)
>>> potential = operator(jnp.ones(16))
>>> potential.shape, bool(jnp.all(jnp.isfinite(potential)))
((16,), True)
Source code in src/integrix/_src/green/_kernels.py
def green_operator(
    kernel: KernelLike,
    rule: RuleLike,
    targets: Float[Array, "target dim"] | None = None,
    *,
    cell_size: Float[Array, " dim"] | None = None,
) -> IntegralOperator:
    """The Green's operator u(xᵢ) = ∑ⱼ G(xᵢ, sⱼ) wⱼ fⱼ, cell averages on the diagonal.

    Where a target coincides with a node a singular kernel is infinite, so
    with ``cell_size`` the entry takes the kernel's ``cell_average`` instead:
    wⱼ ⟨G⟩_cell ≈ ∫_cell G, exact when the node's weight is its cell's
    volume. Singular quadrature stops there (geostack's GS-O3): every other
    entry is the point value G(xᵢ, sⱼ). With a smooth source on a regular
    grid and targets on the nodes the result is second order in the cell
    size; a target closer to a node than a cell but not on it sees the
    singularity unaveraged and loses that order.

    Args:
        kernel: G, any `KernelLike`; ``cell_average`` is needed with
            ``cell_size``.
        rule: The nodes sⱼ and weights wⱼ, the cell centres and volumes of a
            grid (functax's ``cell_area``) for a field of sources.
        targets: The target points xᵢ; the rule's nodes when omitted.
        cell_size: The side lengths of the cell around each node, for the
            diagonal; without it the kernel is used as it is.

    Returns:
        The operator, an `IntegralOperator`.

    Raises:
        ValidationError: If ``cell_size`` is given and the kernel has no
            ``cell_average``.

    Examples:
        The Poisson operator on a square grid, targets on the nodes:

        >>> import jax.numpy as jnp
        >>> from functax.quad import Rule
        >>> centres = (jnp.arange(4) + 0.5) / 4
        >>> x, y = jnp.meshgrid(centres, centres, indexing="ij")
        >>> grid = Rule(jnp.stack([x, y], -1), jnp.full((4, 4), 1 / 16))
        >>> cell = jnp.array([0.25, 0.25])
        >>> operator = green_operator(PoissonGreen(2), grid, cell_size=cell)
        >>> potential = operator(jnp.ones(16))
        >>> potential.shape, bool(jnp.all(jnp.isfinite(potential)))
        ((16,), True)
    """
    nodes, _ = _rule_arrays(rule)
    if targets is None:
        targets = nodes
    if cell_size is not None:
        cell_average = getattr(kernel, "cell_average", None)
        if not callable(cell_average):
            raise ValidationError(
                "'cell_size' needs a kernel with 'cell_average', got "
                f"{type(kernel).__name__}"
            )
        diagonal = jnp.asarray(cell_average(jnp.asarray(cell_size)))
        kernel = _CellAveraged(kernel, diagonal)
    return IntegralOperator(kernel, rule, targets)