Build one RBF-FD stencil¶
This lesson uses the explicit local API introduced in functionals to operators: samples describe the data, a space describes the trial functions, and targets describe what to evaluate.
You will learn: to turn local kernel interpolation into one sparse Laplacian row, inspect its weights, and check polynomial reproduction. Prerequisite: basic NumPy.
For the complete coefficient-elimination argument, read from approximation to weights.
The local approximation¶
For a target \(\boldsymbol{x}_i\) and nearby nodes \(S_i\), RBF-FD approximates
The example uses the signed PHS5 kernel \(\phi(r)=-r^5\), degree-two polynomials \(p=(1,x,y,x^2,xy,y^2)\), and 20 stencil nodes. The local augmented interpolation matrix and weight equation are
Here \(\Phi_{jk}=\phi(\lVert x_j-x_k\rVert)\). Polynomial multipliers \(\lambda\) enforce reproduction and are not nodal solution values. The transpose records the functional convention even when this scalar matrix is symmetric.
1. Select source and target locations¶
import numpy as np
import rbflab as rbf
cells, stencil_size = 5, 20
cloud = rbf.geometry.unit_box_grid(cells)
target_index = int(np.argmin(np.linalg.norm(cloud.points - [0.5, 0.5], axis=1)))
target = cloud.points[[target_index]]
cloud.points has 36 rows. target has shape (1, 2): one evaluation location
near \((0.5,0.5)\). The introductory figure shows the same construction on a
separate irregular cloud, not this 36-node demonstration.
2. Describe the local space and target functional¶
space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
source = {"u": rbf.Samples(cloud.points, size=min(stencil_size, len(cloud.points)))}
approximation = rbf.LocalApproximation(
source=source, trial=space.representers(source),
backend=rbf.PythonBackend(),
)
op = approximation.operators(
targets=target,
operators={"lap": rbf.Laplacian(2)},
).lap
ScalarSpace(PHS(5), 2) selects the trial family and quadratic polynomials.
The name u labels the source-value block; it is not a symbolic field.
space.representers(source) constructs the basis from those value functionals.
Requesting Laplacian(2) produces a FunctionalOperator with a (1, 36)
sparse matrix. op["u"] selects its only source block.
This example retains physical kernel scaling. Other lessons choose local scaling; scaling is a separate decision from polynomial degree and neighbor count.
3. Read the local system and its weights¶
local = op.local(0)
reconstructed = op.reconstruct_local(0)
local.indices selects 20 cloud rows. Scalar local.weights has shape (20, 1);
its polynomial multipliers have shape (6, 1). reconstructed.matrix is the
\(26\times26\) augmented interpolation system and .rhs is its target-functional
column. These are not the global PDE matrix and forcing.
points = cloud.points[local.indices]
weights = local.weights[:, 0]
augmented = np.r_[weights, local.multipliers[:, 0]]
gram_residual = float(np.linalg.norm(
reconstructed.matrix.T @ augmented - reconstructed.rhs[:, 0],
ord=np.inf,
))
constant_response = float(weights.sum())
quadratic_response = float(weights @ np.sum(points**2, axis=1))
This directly expresses the transpose equation in the derivation. Only the first
20 entries become nodal differentiation weights; polynomial multipliers are not
columns of the sparse row. op @ values applies it to a (36,) source vector.
4. Supply your own neighborhood¶
If your method selects neighbors another way, supply them explicitly:
chosen = {"u": rbf.Samples(points)} # all supplied points, in their given order
direct = rbf.LocalApproximation(
source=chosen, trial=space.representers(chosen),
).weights(target=target[0], operator=rbf.Laplacian(2)).weights[:, 0]
This returns nodal weights. You own the point selection and their scatter into
global columns. Modifying an inspection copy from op.local(0) does not rebuild
op.matrix.
A quick check¶
The constant response is zero; the response to \(x^2+y^2\) is four, up to roundoff. This makes polynomial reproduction tangible. Further diagnostic interpretation belongs in errors and residuals.
Complete example¶
Run python -m examples.tutorials.one_stencil from a source checkout.
Complete runnable script
"""Inspect and verify one augmented RBF-FD Laplacian stencil.
Run: python -m examples.tutorials.one_stencil
"""
import numpy as np
import rbflab as rbf
def run(cells=5, stencil_size=20):
cloud = rbf.geometry.unit_box_grid(cells)
target_index = int(np.argmin(np.linalg.norm(cloud.points - [0.5, 0.5], axis=1)))
target = cloud.points[[target_index]]
space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
source = {"u": rbf.Samples(cloud.points, size=min(stencil_size, len(cloud.points)))}
approximation = rbf.LocalApproximation(
source=source, trial=space.representers(source),
backend=rbf.PythonBackend(),
)
op = approximation.operators(
targets=target,
operators={"lap": rbf.Laplacian(2)},
).lap
local = op.local(0)
reconstructed = op.reconstruct_local(0)
points = cloud.points[local.indices]
weights = local.weights[:, 0]
augmented = np.r_[weights, local.multipliers[:, 0]]
gram_residual = float(np.linalg.norm(
reconstructed.matrix.T @ augmented - reconstructed.rhs[:, 0],
ord=np.inf,
))
constant_response = float(weights.sum())
quadratic_response = float(weights @ np.sum(points**2, axis=1))
chosen = {"u": rbf.Samples(points)} # all supplied points, in their given order
direct = rbf.LocalApproximation(
source=chosen, trial=space.representers(chosen),
).weights(target=target[0], operator=rbf.Laplacian(2)).weights[:, 0]
direct_difference = float(np.max(np.abs(weights - np.asarray(direct).reshape(-1))))
radius = float(np.max(np.linalg.norm(points - target[0], axis=1)))
print(f"target={target[0]}, stencil_nodes={len(points)}, "
f"polynomial_multipliers={len(local.multipliers)}, radius={radius:.3f}")
print(f"local_matrix={reconstructed.matrix.shape}, "
f"global_row={op.matrix.shape}, nonzeros={op.matrix.nnz}")
print(f"constant={constant_response:.3e} (expected 0), "
f"lap(x^2+y^2)={quadratic_response:.12g} (expected 4)")
print(f"local_equation_residual={gram_residual:.3e}, "
f"direct_weight_difference={direct_difference:.3e}")
return dict(constant=constant_response, quadratic=quadratic_response,
gram_residual=gram_residual, direct_difference=direct_difference)
if __name__ == "__main__":
run()
Next: Assemble a PDE or include PDE data through LHI.