Skip to content

Write a PDE as a symbolic problem

Goal: translate an equation, boundary data, and an approximation choice into separate Python objects. The first problem is a shorter introduction.

1. State the problem

On an ellipse, solve

\[-\Delta u+\alpha u=f\quad\text{in }\Omega,\qquad u=g\quad\text{on }\Gamma_{\rm wall}.\]

Choose \(\alpha=2\), \(g=\sin x\cos y\), and \(f=(2+\alpha)\sin x\cos y\). A known solution makes this example concrete; ordinary applications supply their own forcing and boundary expressions without knowing the solution.

import numpy as np
import sympy as sp
import rbflab as rbf
from rbflab import geometry, meshgen

2. Give the geometry a named boundary

    domain = geometry.Ellipse(a=1.3, b=.8, labels=("wall",))
    cloud = meshgen.generate(domain, interior=200, boundary=80, seed=42)

The cloud contains 200 interior and 80 boundary nodes. wall connects the geometric subset to its boundary equation; it is not an unknown field name. Generation also supplies the outward normals needed for flux conditions.

Ellipse nodes and the introductory field

The left panel shows the same 280-node cloud. The right panel illustrates the same analytic field in the introductory Poisson example. See mesh generation for composing shapes and boundary groups.

3. Create the unknown and coordinate symbols

    model = rbf.SymbolicScalar(2)
    u = model.field
    x, y = model.coordinates
    alpha = 2
    source = (2+alpha)*sp.sin(x)*sp.cos(y)

u is a symbolic function, not a numerical vector. x and y are its coordinates. alpha is a coefficient in the equation, independent of the kernel's shape parameter. NumPy arrays enter when evaluating the solution.

4. Write the equations

    wall = model.bc("wall", sp.Eq(u, sp.sin(x)*sp.cos(y)))
    problem = model.stationary(sp.Eq(-model.laplacian(u)+alpha*u, source),
                               boundary=[wall])

sp.Eq(left, right) constructs an equation; use it instead of Python ==. model.laplacian(u) expresses \(u_{xx}+u_{yy}\). Ordinary SymPy derivatives such as sp.diff(u, x) can appear in the same linear differential expression.

At this point problem specifies what to solve. It does not choose an RBF kernel, a stencil size, or a numerical backend.

5. Choose the approximation and assemble

    method = rbf.RBFFD(rbf.PHS(5), stencil_size=35, polynomial_degree=3,
                       stencil_policy=rbf.StencilPolicy(scaling="local"))
    system = method.assemble(problem, cloud)
    solution = system.solve()
Object Mathematical role
problem \(\mathcal Lu=f\), \(\mathcal Bu=g\)
cloud Interior/boundary points and geometric data
method Approximation and stencil rules
system Assembled equations \(A_hU=b_h\)
solution Computed values and field reconstruction

PHS5, cubic augmentation, and 35-node neighborhoods define this example's recipe. scaling="local" normalizes kernel distances by each stencil radius and restores physical derivative factors automatically; see local scaling. problem.solve(cloud, method) combines assembly and solution; separate steps let you inspect system.matrix and system.rhs first.

6. Evaluate and display

    query = np.array([[0., 0.], [.3, .2], [-.4, .1]])
    values = solution.evaluate(query)
    dx = solution.evaluate(query, rbf.Derivative(0))

Both arrays contain one scalar per query. The derivative query differentiates the method's reconstruction.

from rbflab import viz
fig, ax = viz.plot_scalar(solution, domain=domain, title="Reaction-diffusion")

Plotting needs rbflab[examples]. Its display grid does not change the numerical cloud.

7. Change the mathematics

Reaction or transport: change alpha, or add sp.diff(u, x) to the left-hand side. Supply the forcing for your new problem; changing an operator does not automatically change an independently supplied forcing.

Robin data: replace wall before constructing problem:

        known_field = sp.sin(x)*sp.cos(y)
        wall = model.bc("wall", sp.Eq(model.normal_derivative(u)+u,
                         model.normal_derivative(known_field)+known_field))

This imposes \(\partial_nu+u=h\) using the cloud's outward normals, with \(h\) chosen from a known field. Run the script with --robin. To prescribe your own flux, replace the right-hand side. Multiple labels accept different model.bc(...) entries.

Approximation: keep problem and replace the method with a compatible GlobalCollocation(...) or LHI(...). Their global and LHI lessons explain why their matrices and unknown vectors differ.

A quick check

Evaluate a few points against \(\sin x\cos y\) to check the entered equation and data.

Complete example

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

Complete runnable script
"""Translate reaction-diffusion and Dirichlet/Robin equations into the scalar API."""
import argparse
import numpy as np
import sympy as sp
import rbflab as rbf
from rbflab import geometry, meshgen


def run(robin=False):
    domain = geometry.Ellipse(a=1.3, b=.8, labels=("wall",))
    cloud = meshgen.generate(domain, interior=200, boundary=80, seed=42)
    model = rbf.SymbolicScalar(2)
    u = model.field
    x, y = model.coordinates
    alpha = 2
    source = (2+alpha)*sp.sin(x)*sp.cos(y)
    wall = model.bc("wall", sp.Eq(u, sp.sin(x)*sp.cos(y)))
    if robin:
        known_field = sp.sin(x)*sp.cos(y)
        wall = model.bc("wall", sp.Eq(model.normal_derivative(u)+u,
                         model.normal_derivative(known_field)+known_field))
    problem = model.stationary(sp.Eq(-model.laplacian(u)+alpha*u, source),
                               boundary=[wall])
    method = rbf.RBFFD(rbf.PHS(5), stencil_size=35, polynomial_degree=3,
                       stencil_policy=rbf.StencilPolicy(scaling="local"))
    system = method.assemble(problem, cloud)
    solution = system.solve()
    query = np.array([[0., 0.], [.3, .2], [-.4, .1]])
    values = solution.evaluate(query)
    dx = solution.evaluate(query, rbf.Derivative(0))
    error = float(np.max(np.abs(values-np.sin(query[:, 0])*np.cos(query[:, 1]))))
    print(f"boundary={'Robin' if robin else 'Dirichlet'}, system={system.matrix.shape}")
    print(f"query values={values}, derivative shape={dx.shape}, sampled error={error:.3e}")
    return error


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--robin", action="store_true")
    run(**vars(parser.parse_args()))

Download the script.

Next: Construct the sparse equation yourself, or use mixed boundaries.