Skip to content

Time-step the heat equation

Goal: build a spatial operator with LocalApproximation, then implement backward Euler and BDF2 using ordinary SciPy matrices. An optional symbolic comparison follows the complete time loop. For the Hermite version, see LHI and heat from matrices. Read custom matrix assembly first if sparse slicing is new.

1. Fix one mathematical problem and one recipe

On \((0,1)^2\), solve

\[u_t=\kappa\Delta u,\qquad u|_{\partial\Omega}=0,\qquad u(x,y,0)=\sin(\pi x)\sin(\pi y).\]
import numpy as np
import sympy as sp
import scipy.sparse as sparse
from scipy.sparse.linalg import splu
import rbflab as rbf
    cloud = rbf.geometry.unit_box_grid(6)  # 49 nodes, including the boundary
    dt, steps, kappa = .01, 5, 1.
    space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)

Both routes below use the same numerical recipe: 49 nodes, PHS5, quadratic polynomials, 20-node stencils, Float64, and default physical kernel scaling. Five steps reach \(t=0.05\). The point grid supplies locations; all spatial weights are RBF-FD.

The square cloud, initial temperature, and computed BDF2 temperature

2. Construct the spatial operator explicitly

    samples = {"u": rbf.Samples(cloud.points, size=20)}
    approximation = rbf.LocalApproximation(source=samples, trial=space.representers(samples))
    ops = approximation.operators(targets=cloud.points, operators={"lap": rbf.Laplacian()})
    matrix = ops.lap.matrix.tocsr()
    I, B = cloud.interior_indices, cloud.boundary_indices
    lap_ii, lap_ib = matrix[I, :][:, I], matrix[I, :][:, B]

ops.lap @ U approximates \(\Delta u\) at all nodes. Interior equations separate unknown interior values from prescribed boundary values:

\[\dot U_I=\kappa L_{II}U_I+\kappa L_{IB}g_B(t)+f_I(t).\]

lap_ii is \(25\times25\), while lap_ib maps 24 boundary values into those 25 interior equations. The present problem has \(g=f=0\); keeping the terms visible shows where nonzero data enter. You can also inspect ops.lap.local(i) and ops.lap.reconstruct_local(i) as in one stencil.

3. Translate the time formula into factors

Let \(q^{n+1}=\kappa L_{IB}g_B^{n+1}+f_I^{n+1}\). The first step is

\[(I-\Delta t\,\kappa L_{II})U_I^1=U_I^0+\Delta t\,q^1.\]

Subsequent BDF2 steps satisfy

\[(\tfrac32I-\Delta t\,\kappa L_{II})U_I^{n+1} =2U_I^n-\tfrac12U_I^{n-1}+\Delta t\,q^{n+1}.\]

Prepare the two fixed matrices once:

    identity = sparse.eye(len(I), format="csc")
    first_step = splu((identity-dt*kappa*lap_ii).tocsc())
    later_steps = splu((1.5*identity-dt*kappa*lap_ii).tocsc())
    X = cloud.points
    initial_values = np.sin(np.pi*X[:, 0])*np.sin(np.pi*X[:, 1])
    previous = initial_values[I].copy()
    older = None
    states = [initial_values.copy()]

The RBF construction determines \(L\). Backward Euler and BDF2 determine the time formula. They are separate choices, which is why another time integrator can reuse these spatial operators.

4. Advance and restore the full field

    for step in range(1, steps+1):
        time = step*dt
        g = np.zeros(len(B))  # prescribed values at this time
        f = np.zeros(len(I))  # forcing at interior nodes at this time
        source = kappa*(lap_ib @ g) + f
        if step == 1:
            current = first_step.solve(previous + dt*source)
        else:
            current = later_steps.solve(2*previous - .5*older + dt*source)
        full = np.empty(len(X))
        full[I], full[B] = current, g
        states.append(full)
        older, previous = previous, current
    states = np.asarray(states)

At each step, the code supplies boundary data and forcing, solves for interior values, then fills the full vector in original cloud order. states has shape (steps+1, 49), including the initial condition. No RBF coefficient solve occurs inside this loop: the spatial weights were assembled beforehand.

5. Optional symbolic convenience route

    method = rbf.RBFFD(rbf.PHS(5), stencil_size=20, polynomial_degree=2)
    model = rbf.SymbolicScalar(2, transient=True)
    u, t = model.field, model.time
    x, y = model.coordinates
    initial = sp.sin(sp.pi*x)*sp.sin(sp.pi*y)
    problem = model.evolution(
        sp.Eq(sp.diff(u, t)-kappa*model.laplacian(u), 0),
        initial=initial,
        boundary=[model.bc("boundary", sp.Eq(u, 0))],
    )
    trajectory = problem.solve(cloud, method, str(dt), steps, scheme="bdf2")
    symbolic_values = trajectory.final.evaluate(cloud.points)

transient=True adds the time symbol. initial gives the field at \(t=0\); boundary equations supply values at subsequent times. scheme="bdf2" requests BDF2 with a backward-Euler startup. The returned trajectory stores computed states; .final supplies the final field reconstruction.

The RBFFD adapter keeps equation-driven assembly available; the explicit spatial construction above uses Samples and LocalApproximation.

Modify the problem

  • Replace g and f with arrays evaluated at the current time for nonzero data. Make the corresponding change in the symbolic declaration.
  • Change the initial expression and initial_values together.
  • Replace the update formula to experiment with time integration.
  • If dt, \(\kappa\), or \(L\) changes, rebuild the affected factors.
  • Change geometry while preserving boundary groups; see heat on a flower.

For an extended implementation with selectable time schemes and nonzero-data examples, examples/tutorials/heat_matrices.py provides rbf_fd_laplacian, march, and symbolic_solution. The compact script here keeps one case visible.

A quick check

The full script compares the two final nodal arrays. Their agreement checks the translation between API routes, not independent spatial accuracy. The continuous solution is \(e^{-2\kappa\pi^2t}\sin(\pi x)\sin(\pi y)\) if you want a separate check.

Complete example

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

Complete runnable script
"""One heat problem expressed symbolically and with explicit RBF-FD matrices."""
import numpy as np
import sympy as sp
import scipy.sparse as sparse
from scipy.sparse.linalg import splu
import rbflab as rbf


def run():
    cloud = rbf.geometry.unit_box_grid(6)  # 49 nodes, including the boundary
    dt, steps, kappa = .01, 5, 1.
    space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
    samples = {"u": rbf.Samples(cloud.points, size=20)}
    approximation = rbf.LocalApproximation(source=samples, trial=space.representers(samples))
    ops = approximation.operators(targets=cloud.points, operators={"lap": rbf.Laplacian()})
    matrix = ops.lap.matrix.tocsr()
    I, B = cloud.interior_indices, cloud.boundary_indices
    lap_ii, lap_ib = matrix[I, :][:, I], matrix[I, :][:, B]
    identity = sparse.eye(len(I), format="csc")
    first_step = splu((identity-dt*kappa*lap_ii).tocsc())
    later_steps = splu((1.5*identity-dt*kappa*lap_ii).tocsc())
    X = cloud.points
    initial_values = np.sin(np.pi*X[:, 0])*np.sin(np.pi*X[:, 1])
    previous = initial_values[I].copy()
    older = None
    states = [initial_values.copy()]
    for step in range(1, steps+1):
        time = step*dt
        g = np.zeros(len(B))  # prescribed values at this time
        f = np.zeros(len(I))  # forcing at interior nodes at this time
        source = kappa*(lap_ib @ g) + f
        if step == 1:
            current = first_step.solve(previous + dt*source)
        else:
            current = later_steps.solve(2*previous - .5*older + dt*source)
        full = np.empty(len(X))
        full[I], full[B] = current, g
        states.append(full)
        older, previous = previous, current
    states = np.asarray(states)
    method = rbf.RBFFD(rbf.PHS(5), stencil_size=20, polynomial_degree=2)
    model = rbf.SymbolicScalar(2, transient=True)
    u, t = model.field, model.time
    x, y = model.coordinates
    initial = sp.sin(sp.pi*x)*sp.sin(sp.pi*y)
    problem = model.evolution(
        sp.Eq(sp.diff(u, t)-kappa*model.laplacian(u), 0),
        initial=initial,
        boundary=[model.bc("boundary", sp.Eq(u, 0))],
    )
    trajectory = problem.solve(cloud, method, str(dt), steps, scheme="bdf2")
    symbolic_values = trajectory.final.evaluate(cloud.points)
    result = {"route_difference": float(np.max(np.abs(states[-1]-symbolic_values))),
              "error": float(np.max(np.abs(states[-1]-np.exp(-2*kappa*np.pi**2*steps*dt)*initial_values)))}
    print(f"states={states.shape}, final time={steps*dt}, interior matrix={lap_ii.shape}")
    print(result)
    return result


if __name__ == "__main__":
    run()

Download the script.

Next: Heat on a curved domain, or coupled Stokes fields.