Architecture

This page describes the intended design. Only the parts listed under “Repository layout” exist today. Update the page whenever the code and the description drift apart.

The pipeline

pdemagic is a compiler for PDE problems. Input passes through these stages:

  1. Frontend. Turns what the user wrote into a problem description. The first frontend is Python with sympy. Julia, C++, a Rust builder API, a text format for the command line and the GUI follow later. A frontend contains no numerics.

  2. Problem description. A data structure in the Rust core that states the mathematical problem and nothing else: a set of unknowns, a set of equations coupling them, domains with their coordinate system, conditions (on the faces of a box, on the curves that bound a shape, at an initial time, at infinity), parameters and data fields. Data fields are declared here by name and grid; their values are passed to the solve separately (decision D23). It has its own symbolic expression type and can be serialized. It knows nothing about grids or methods.

  3. Planner. Analyses the problem description (equation type, linearity, structure, domain) and produces a plan. A system is analysed block by block, because its equations may differ in kind (decision D15).

  4. Plan. A data structure that names every choice needed to solve the problem: the method, the grid or mesh, the solver, each parameter. It can be inspected, edited, serialized and passed back in.

  5. Method. Executes a plan and produces the solution. Methods are free in how they work. Some discretize a local operator and solve a sparse system. Others evaluate a Green’s-function integral or apply a spectral transform.

  6. Solution. The computed fields together with the plan that produced them and diagnostics about the run.

Below the pipeline sits the runtime: fields on grids and meshes, parallelism, numerical kernels and input/output.

Principles

  • One path for beginners and experts. Defaults are what the planner writes into the plan. Control is editing the plan. There is no separate “advanced” API.

  • The core does not know its callers. Nothing in the core crate refers to Python, sympy or any other host language.

  • Borrowed kernels, own PDE machinery. See decision D4.

  • Methods are not assumed to be local. See decision D8.

  • The problem description is checked against the hardest targets. See decision D9.

  • Verification before breadth. A method is not done until a test asserts its convergence order against a known solution.

What using it feels like

import sympy as sp
import pdemagic as pm

x, y, z = sp.symbols("x y z", real=True)
phi = sp.Function("phi")(x, y, z)
rho = sp.exp(-(x**2 + y**2 + z**2))

problem = pm.Problem(
    sp.Eq(pm.laplacian(phi), -4 * sp.pi * rho),
    domain=pm.FreeSpace(x, y, z),
)

solution = pm.solve(problem)  # the planner chooses everything
print(solution.plan)  # and reports what it chose

plan = pm.plan(problem)  # or: take the plan, change it, pass it back
plan.grid.shape = (96, 96, 96)
solution = pm.solve(problem, plan)

docs/getting-started.md walks through this example.

Language bindings

Language

Binds through

Status

Rust

the core crate itself

exists

Python

PyO3, on the Rust API

exists

C++

a C ABI crate plus a header-only wrapper

planned

Julia

the same C ABI

planned

The command line application and the GUI are clients of the same pipeline. They build a problem description, usually from a file, and hand it to the core.

Repository layout

crates/pdemagic/       the core library
crates/pdemagic-py/    the Python extension module (pdemagic._core)
python/pdemagic/       the Python package: frontend and user-facing API
python/tests/          Python tests
docs/                  the documentation website
docs/dev/              developer documentation (this directory)
scripts/               development scripts
prototypes/            code that answered a question, and is no part of the library

The core crate

One module per stage of the pipeline:

Module

Role

expr

The expression type, its evaluation, and the extraction of the linear operator from an equation.

problem

The problem description and its validation.

error

The error type of the core.

analysis

Classification: what kind of problem is this? It brings the equation to the normal form of its kind: Poisson’s equation, a screened or weighted Poisson equation, a general operator with constant coefficients, or one whose coefficients vary.

planner

From a classified problem to a plan.

plan

The plan as data.

methods

One submodule per solution method.

solve

Executes a plan and assembles the solution with its diagnostics.

evolution

Planning and solving for problems that depend on time.

eigen

Classification, planning and solving for eigenproblems: the lowest eigenvalues of an operator on a periodic domain (decision D55.1).

system

Classification, planning and solving for coupled systems of linear equations.

bounded, bounded_evolution

Classification, planning and solving for problems on domains with boundary conditions, stationary and in time: boxes, disks, rings, balls, shells and cylinders.

shaped, shaped_evolution

Classification, planning and solving for problems on a shape in the plane that curves bound, stationary and in time (decisions D33.2 and D34.1).

shaped_coupled

Stationary systems on a shape: one linear problem for each unknown, how the others enter each equation, and the nonlinear terms, solved together (decision D54.1).

shaped_system

Systems in time on a shape: one problem on the shape for each unknown, and the terms that couple them, stepped together (decision D53.1).

shaped_modes

Eigenproblems on a shape: the equation read as the one in time whose modes are asked for (decision D57.1), and the lowest of those modes by the block iteration of subspace on the stationary solve of the elements (decision D58.1).

asked_modes

An eigenproblem read as the problem in time whose modes it asks for: for shapes, boxes and round domains (decision D57.1).

bounded_modes

Eigenproblems on boxes with walls and on round domains: the modes of collocation, sorted by their eigenvalue and returned as real functions (decision D59.1), also with a potential that leaves them separate, on the diagonal of the matrix of each axis (decision D61.1).

in_modes

Problems in time in the modes of a domain: exact propagation, stepping and recording for any collocation::Modes and any number of unknowns, the choice of the steps in time, and the note on them. bounded_evolution, shaped_evolution, bounded_system and shaped_system say what each unknown is given, as a Driven (decision D73.1).

route

Which path takes a problem: one function from the family of the domain and the kind of the problem, which the planner, the solve, values_at and integrals read (decision D75.2).

travel

How far the solution of a linear equation in time travels in free space, for any operator with constant coefficients: a bound from the symbol of the operator at complex wave numbers and from the waves that what is given contains, by which evolution pads the transform (decisions D71.1 to D71.3).

bounded_system

Systems in time on boxes and round domains: one problem on the domain for each unknown, with its own conditions and modes, and the terms that couple them, stepped together on the coefficients of all unknowns (decisions D70.1 to D70.3).

bounded_inverse_modes

Eigenproblems on boxes and round domains whose modes are no products: the shift below every eigenvalue, and the iteration on a block of functions with the stationary solve of bounded_varying or bounded_plate (decisions D67.1 to D67.3).

bounded_plate

The stationary plate equation on a box: the split into two equations of second order, the conditions in the two unknowns, the matrix of the influences of the values on the faces, and the fit that the corners need (decisions D66.1 to D66.3).

bounded_varying

Stationary equations on boxes and round domains whose coefficients vary: the reference that the modes hold, the remainder, and the iteration on the direct solve (decision D63.1). Nonlinear ones by Newton’s method on that iteration (decision D64.1). A mixed derivative on a box is a term of the remainder with nothing that stands for it (decision D65.1).

shaped_data

The data fields of a problem on a shape: arrays on a uniform grid, read at the points of the elements (decision D52.1).

varying

Planning and solving for stationary equations whose coefficients vary in space (decision D71), and for ones with a nonlinear term (decision D72).

grid, fft, quadrature

Runtime: tensor-product grids, fields, transforms, Gauss–Legendre rules and Legendre functions.

scalar

Runtime: real and complex numbers behind one trait, so that a method is written once for both.

threads

Runtime: when a loop is shared among the threads of rayon, and the pool that a solve runs in (decision D69).

vector_state

Runtime: clears the vector registers after linear algebra. The one module with unsafe code (decision D68).

profile

Runtime: where the time of a solve goes. The heavy parts of the core mark themselves with spans (decision D67).

surface

Runtime: closed surfaces as triangle meshes, and the shapes that are meshed here.

curve

Runtime: closed curves in the plane, made of lines and arcs: the boundary of a shape.

mesh

Runtime: a shape in the plane cut into quadrilaterals with curved sides, by way of a triangulation (decision D33.5), with layers that shrink towards the corners at which a solution has a singularity (decision D40.1).

The methods so far are fourier (Cartesian coordinates, free and periodic directions; besides Poisson’s equation it solves screened ones, and any operator with constant coefficients on a periodic box), preconditioned (linear equations whose coefficients vary in space, by an iteration on fourier with gmres, and nonlinear stationary ones by Newton’s method on that iteration), harmonics (disks and balls), cylinder (finite cylinders), boundary (conductors and sharp-surfaced dielectrics in free space, by boundary elements on top of fourier) and propagator (equations of first and second order in time, by exact propagation in Fourier space). exponential steps nonlinear equations of first order in time, with the linear part exact, and in free space terms whose coefficients vary. helmholtz solves the Helmholtz equation with free directions for the outgoing solution, and preconditioned iterates on it for a medium. coupled solves systems of linear equations with constant coefficients, stationary and in time, with one small matrix per Fourier wave, and coupled_stepping steps systems with nonlinear terms and with constraints. collocation solves linear equations on boxes with boundary conditions, at Chebyshev points and in the basis of the modes of each axis. round does the same on round domains: disks and rings with Fourier waves along the angle, balls and shells with spherical harmonics, and radial modes for each (decisions D77 and D78). cylindrical puts the modes of a disk and of an axis of a box together for cylinders (decision D79). All three stand behind one type, collocation::Modes, and so do the modes of a shape in the plane (decision D34.1), and the points of a domain themselves, for an unknown of a system that has no derivative in space (decision D77.1). In time, propagator and exponential work in those modes as they do in Fourier waves (decision D65). cylinder and boundary are second order in space, the others spectral.

Time is not an axis of the domain. A problem that depends on time names its time and the time span, and gives initial conditions (decision D47). The plan says at which times the solution is recorded, and the solution holds one snapshot per recorded time.

A problem with several unknowns is a system (decision D56). So far it is classified as a whole, as a matrix of operators, and takes a path of its own through the pipeline: system next to analysis, planner and solve.

A domain with a bounded interval along a Cartesian coordinate is a box. It has faces, and each face carries a boundary condition: an equation that holds on it (decision D63). Such a problem also takes a path of its own, bounded. A round domain with a condition on its surface takes the same path: its faces are those of the radius, and for a cylinder those of its axis too. A system in time on such a domain is read unknown by unknown, each as a problem of this kind, by bounded_system. Without a condition a disk, a ball or a cylinder has vacuum outside and is classified by analysis, as before.

An unknown is real or complex (decision D54). It is complex if the imaginary unit appears in its equation or in what the equation is given. The methods in Fourier space are generic over the two, and a solution holds real or complex values per unknown.

A domain that closed curves bound is a shape in the plane (decision D33.2). Each piece of a curve carries a boundary condition. Such a problem takes a path of its own as well, shaped, and is solved by the method elements: spectral elements on quadrilaterals, with a sparse factorization. sparse factorizes the matrix as what it is: in real numbers where it is real, and by Cholesky’s method where it is symmetric and of one sign (decision D62.1). The method takes any linear equation of second order, with coefficients that vary (decision D35.1), and nonlinear stationary equations by Newton’s method on the same solve (decision D36.1). Its solution is handed back on a uniform grid over the rectangle that holds the shape (decision D33.4). In time, the same elements give the modes of the shape by one dense eigenproblem, and the propagator of boxes evolves them (decision D34.1): the modes of a shape are one more kind of collocation::Modes. Terms that are nonlinear or have coefficients that vary are stepped in those modes by exponential (decision D38.1).

Bodies are part of the problem description: a shape and a material each. A shape is a formula (sphere, box and so on) or a triangle mesh written out in the description. How finely a shape is meshed is an entry of the plan (decision D40).

Modules are split into separate crates only when a boundary has proven itself.

What is written once

A path of its own for each kind of domain and each kind of problem is how the core grew, and not how it is meant to stay (decision D72.1). What is the same on several paths is written once, and a path says what is its own:

  • Which path takes a problem is one function, route::Path::of, from the family of the domain and the kind of the problem. The planner and the solve are each a table over its answer (decision D75.2).

  • Newton’s method is one loop, methods::newton. A discretization says what a step solves and how the residual at its end is known (decision D72.2).

  • The control of the step in time is one function, exponential::controlled, for a number or a matrix for each member of a basis (decisions D72.3 and D74.1).

  • Problems in time in the modes of a domain are propagated, stepped and recorded by one driver, in_modes, for boxes, round domains and shapes, for one equation and for a system (decision D73.1).

  • What an equation holds of time is read by analysis::read_time (decision D75.3), and the grid of its data, the starting value of Newton’s method and the refusal of a magnitude by one function each of analysis.

  • A system in time on a domain with modes is read by in_modes::read_system, which has the path of the domain read the problem of each unknown (decision D76.1).

  • A nonlinear term and its slope are compiled by newton::Compiled.

  • gmres and subspace take closures and always were shared.

Fourier grids and the modes of a domain stay two. A real problem is solved in real numbers on a Fourier grid, its terms are evaluated in Fourier space, and with free directions its state lives on a padded box: one interface over both would cost speed or change results (decision D75.1). They share the kernels, not a driver.

New code for a kind of problem is written against what is shared, and a second copy of a loop is a reason to stop and share it. What was left as it is, and why, is decision D76.2.

Plotting

Plotting is part of the Python package and of nothing else (decision D45). python/pdemagic/_plot.py holds all of it: what is drawn for which kind of solution, and the one place where colours and line widths are set. matplotlib is imported when a plot is asked for, so the package works without it.

How data crosses into Python

The Python package builds the problem as nested dictionaries, serializes it to JSON and calls the extension module. Plans come back as JSON and fields as numpy arrays, of real or of complex numbers. The Python Plan class is a generic view on that JSON (decision D19), so a new method or option in the core needs no change in any frontend.