Skip to content

Solve a PDE with local Hermite interpolation

Goal: construct Hermite trial functions, extract their named weight blocks, and assemble the PDE yourself. Read one stencil first.

1. Separate unknown values and known data

For \(Lu=f\) with Dirichlet boundary data \(u=g\), use three point sets: \(X_u\) for unknown solution samples, \(X_b\) for boundary samples, and \(X_f\) for PDE samples. Here \(X_f=X_u\), but their functionals differ.

import numpy as np
from scipy.sparse.linalg import spsolve
import rbflab as rbf
    cloud = rbf.geometry.unit_box_grid(5)
    Xu = cloud.interior
    Xb = cloud.points[cloud.boundary_indices]
    Xf = Xu.copy()
    L = -rbf.Laplacian(2)

    def exact(X):
        return np.sin(np.pi*X[:, 0])*np.sin(np.pi*X[:, 1])

    fu, ff, gb = 2*np.pi**2*exact(Xu), 2*np.pi**2*exact(Xf), exact(Xb)
Source block Sampled functional Values supplied after assembly
u \(s(X_u)\) Unknown \(U\)
boundary \(s(X_b)\) Prescribed \(g_b\)
pde \(Ls(X_f)\) Prescribed \(f_f\)

A point may occur in both the value and PDE groups. Exclude a target from its own PDE data: otherwise the requested \(Ls(x_i)\) is already a supplied datum, giving a tautology instead of a useful equation for \(U\).

Solution, boundary and PDE samples on the square

This illustration shows the original combined-neighbor selection: 12 solution, 8 boundary and 11 PDE samples at the first target. Below the counts are explicit for every target, with independent nearest-neighbor selection in each group. Tied distances can give different memberships from the illustration.

2. Translate the Hermite ansatz into code

Stack the selected functionals as \(\ell=(I_u,I_b,L_f)\). Write

\[s_i(x)=\sum_j a_j\ell_j^y K(x,y_j)+\sum_m b_mp_m(x).\]

Then

\[H_i=\begin{bmatrix}G_i&P_i\\P_i^T&0\end{bmatrix},\qquad (G_i)_{kj}=\ell_k^x\ell_j^yK,\qquad(P_i)_{km}=\ell_kp_m.\]

The target-functional weight equation is

\[H_i^T\begin{bmatrix}w_i\\\eta_i\end{bmatrix} =\begin{bmatrix}(L_x\ell_j^yK)(x_i,y_j)\\(Lp_m)(x_i)\end{bmatrix}.\]

space.representers(source) encodes the source-side \(\ell_j^y\). The requested target operator supplies \(L_x\). The same engine used in RBF-FD now builds Hermite weights; no separate local solve implementation is needed.

    space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
    source = {
        "u": rbf.Samples(Xu, size=12, target="require"),
        "boundary": rbf.Samples(Xb, size=8),
        "pde": rbf.Samples(Xf, operator=L, size=11, target="exclude"),
    }
    approximation = rbf.LocalApproximation(source=source, trial=space.representers(source))
    op = approximation.operators(targets=Xu, operators={"L": L}).L
    Su, Sb, Sf = (op[name].matrix for name in ("u", "boundary", "pde"))
    rhs = fu - Sb @ gb - Sf @ ff
    interior_values = spsolve(Su, rhs)

The three sparse blocks express

\[Lu(X_u)\approx S_uU+S_bg_b+S_ff_f.\]

Therefore the stationary system is

\[\boxed{S_uU=f_u-S_bg_b-S_ff_f.}\]

There are 16 global solution unknowns. Each patch uses \(12+8+11=31\) functionals and six polynomial terms, giving a \(37\times37\) local system. The Samples objects do not store forcing or decide which values are unknown: that interpretation belongs to the equation you assemble.

3. Inspect one local and one global equation

    row = 0
    patch = op.local(row)
    algebra = op.reconstruct_local(row)
    S, B, F = (patch.groups[name] for name in ("u", "boundary", "pde"))
    nS, nB = len(S), len(B)
    w = patch.weights[:, 0]  # source insertion order: u, boundary, pde
    wS, wB, wF = w[:nS], w[nS:nS+nB], w[nS+nB:]

patch.groups contains indices into each group's own point array. Weights follow the source dictionary's insertion order. Polynomial multipliers are separate from the data weights and do not become global solution unknowns. reconstruct_local returns the actual local matrix and target column on demand.

    reduced_rhs = fu[row] - wB @ gb[B] - wF @ ff[F]
    sparse_row = np.zeros(len(Xu))
    sparse_row[S] = wS

Here S already indexes the interior unknown vector. No conversion from whole-cloud indices is needed. The complete script checks this row against Su and checks its right-hand side against rhs.

4. Evaluate between solution nodes

At query points, request identity and derivative maps from the same trial recipe:

    query = np.array([[.25, .3], [.6, .7]])
    # Rebuild neighborhoods at the query locations; no nearest-patch ownership.
    query_source = {
        "u": rbf.Samples(Xu, size=12),
        "boundary": rbf.Samples(Xb, size=8),
        "pde": rbf.Samples(Xf, operator=L, size=11, target="exclude"),
    }
    reconstruction = rbf.LocalApproximation(
        source=query_source, trial=space.representers(query_source),
    ).operators(targets=query, operators={"value": rbf.Identity(2), "dx": rbf.Derivative(0)})
    data = {"u": interior_values, "boundary": gb, "pde": ff}
    predicted = reconstruction.value @ data
    gradient_x = reconstruction.dx @ data

The query construction relaxes target="require" for the solution samples: an off-node target cannot be a member of the solution cloud. Each query selects its own neighborhoods and applies its map to computed \(U\) and prescribed \(g,f\). This differs from reusing the nearest assembled patch. Changing memberships can still produce nonsmooth transitions; this is not a globally smooth interpolant.

Modify the construction

  • Change operator=L and the target operator together for another PDE.
  • Supply a boundary functional and its measured/prescribed data for Neumann or Robin data; normal-dependent operators also need sample normals.
  • Change each group's size, or specify exact memberships with indices.
  • Choose backend=rbf.CppBackend() or rbf.TorchBackend() on the approximation for supported local assembly. Export blocks with to_scipy() if using a SciPy solve, as in LHI and heat from matrices.
  • For time dependence, PDE samples contain \(f-u_t\); use the mass-matrix derivation in that lesson rather than inserting \(f\) as stationary data.

Complete example

Run python -m examples.tutorials.lhi_construction from a source checkout.

Complete runnable script
"""Build scalar LHI from named functional maps and an explicit sparse equation."""
import numpy as np
from scipy.sparse.linalg import spsolve
import rbflab as rbf


def run():
    cloud = rbf.geometry.unit_box_grid(5)
    Xu = cloud.interior
    Xb = cloud.points[cloud.boundary_indices]
    Xf = Xu.copy()
    L = -rbf.Laplacian(2)

    def exact(X):
        return np.sin(np.pi*X[:, 0])*np.sin(np.pi*X[:, 1])

    fu, ff, gb = 2*np.pi**2*exact(Xu), 2*np.pi**2*exact(Xf), exact(Xb)
    space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
    source = {
        "u": rbf.Samples(Xu, size=12, target="require"),
        "boundary": rbf.Samples(Xb, size=8),
        "pde": rbf.Samples(Xf, operator=L, size=11, target="exclude"),
    }
    approximation = rbf.LocalApproximation(source=source, trial=space.representers(source))
    op = approximation.operators(targets=Xu, operators={"L": L}).L
    Su, Sb, Sf = (op[name].matrix for name in ("u", "boundary", "pde"))
    rhs = fu - Sb @ gb - Sf @ ff
    interior_values = spsolve(Su, rhs)
    row = 0
    patch = op.local(row)
    algebra = op.reconstruct_local(row)
    S, B, F = (patch.groups[name] for name in ("u", "boundary", "pde"))
    nS, nB = len(S), len(B)
    w = patch.weights[:, 0]  # source insertion order: u, boundary, pde
    wS, wB, wF = w[:nS], w[nS:nS+nB], w[nS+nB:]
    reduced_rhs = fu[row] - wB @ gb[B] - wF @ ff[F]
    sparse_row = np.zeros(len(Xu))
    sparse_row[S] = wS
    query = np.array([[.25, .3], [.6, .7]])
    # Rebuild neighborhoods at the query locations; no nearest-patch ownership.
    query_source = {
        "u": rbf.Samples(Xu, size=12),
        "boundary": rbf.Samples(Xb, size=8),
        "pde": rbf.Samples(Xf, operator=L, size=11, target="exclude"),
    }
    reconstruction = rbf.LocalApproximation(
        source=query_source, trial=space.representers(query_source),
    ).operators(targets=query, operators={"value": rbf.Identity(2), "dx": rbf.Derivative(0)})
    data = {"u": interior_values, "boundary": gb, "pde": ff}
    predicted = reconstruction.value @ data
    gradient_x = reconstruction.dx @ data
    result = {"row_difference": float(np.max(np.abs(sparse_row-Su.getrow(row).toarray()[0]))),
              "rhs_difference": float(abs(reduced_rhs-rhs[row])),
              "off_node_error": float(np.max(np.abs(predicted-exact(query)))),
              "center_excluded": bool(row not in F)}
    print(f"centers: solution={nS}, boundary={nB}, PDE={len(F)}; global={Su.shape}")
    print(f"local system={algebra.matrix.shape}; derivative shape={gradient_x.shape}")
    print(result)
    return result


if __name__ == "__main__":
    run()

Download the script.

Next: independent functional centers, then LHI heat matrices.