uPDE
A lightweight Python library for solving 1D and 2D PDEs using the method of lines.
uPDE lets you describe a PDE — its terms, coefficients, boundary conditions, and initial
data — and hands the spatial right-hand side directly to scipy.integrate.solve_ivp.
You get adaptive step control, stiffness detection, and a choice of explicit and implicit
integrators without writing a single line of time-stepping code.
Features:
- 1D and 2D problems on uniform Cartesian grids
- Coupled multi-field systems
- Scalar, array, and callable (nonlinear, field-dependent, or time-dependent) coefficients
- Dirichlet, Neumann, and periodic boundary conditions
- Interior boundary conditions for obstacles and inclusions
- Pre-built equation prototypes for common PDE families
- Flamelet-based combustion via mixture-fraction transport and
FlameletTable - Pure NumPy / SciPy — no compilation, no external mesh libraries
Table of Contents
- Installation
- Quick Start
- Core Concepts
- User Guide
- Pre-built Equations
- Combustion and Flamelet Chemistry
- API Reference
- Numerical Methods
- Troubleshooting
Installation
uPDE has no compiled dependencies and is available on PyPI:
pip install upde
Or install from source for development:
git clone https://github.com/saadgroup/uPDE.git
cd uPDE
pip install -e .
Requirements:
| Package | Version |
|---|---|
| numpy | ≥ 1.22 |
| scipy | ≥ 1.8 |
| python | ≥ 3.9 |
Optional — for high-fidelity flamelet table generation:
| Package | Version | Purpose |
|---|---|---|
| cantera | ≥ 3.0 | FlameletTable.from_cantera() — 1-D counterflow flame |
Cantera is only needed to generate a flamelet table. Once saved with
table.save('ch4_air.npz'), the table can be reloaded with
FlameletTable.from_file() on any machine without Cantera installed.
Quick Start
import numpy as np
from upde import PDE, PDESystem
x = np.linspace(0, 1, 256)
eq = PDE('T', x=x)
eq.add_diffusion(diffusivity=0.05)
eq.set_bc(side='left', kind='dirichlet', value=1.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
eq.set_ic(0.0)
sol = PDESystem([eq]).solve(t_span=(0, 1), method='RK45')
# sol.T shape (256, nt)
# sol.T[:, -1] final profile
# sol.t time points
Or use a pre-built equation prototype and call solve directly on it:
from upde import PDESystem, HeatEquation
eq = HeatEquation('T', x=x, diffusivity=0.05)
eq.set_bc(side='left', kind='dirichlet', value=1.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
eq.set_ic(0.0)
sol = eq.solve(t_span=(0, 1), method='BDF') # shorthand for uncoupled equations
Core Concepts
PDE — equation descriptor
A PDE object holds the mathematical description of one equation: its field name,
spatial grid, terms (advection, diffusion, source, flux), boundary conditions, and
initial condition.
An uncoupled PDE (one that references no field other than its own) can be solved
directly with eq.solve(...) for time-dependent problems or eq.solve_steady(...) for
the steady state. Both are shorthand for PDESystem([eq]).solve(...) and
PDESystem([eq]).solve_steady(...). Coupled equations must be solved together through
PDESystem; calling solve() or solve_steady() on them raises a ValueError.
PDESystem — the solver
PDESystem takes a list of PDE objects, validates that all field references are
consistent, and assembles the joint state vector. solve(...) integrates the system in
time with solve_ivp; solve_steady(...) solves directly for the steady state
$\mathrm{RHS}(\phi) = 0$. Even single-equation problems can use PDESystem([eq]).
PDESolution — the result
Fields are accessed by name as attributes. Spatial indices come first, time last:
sol.T # (nx, nt) in 1D | (nx, ny, nt) in 2D
sol.T[:, -1] # final 1D profile
sol.T[:, :, -1] # final 2D field
sol.t # time points (nt,)
sol.success # bool
sol.message # integrator status string
sol.raw # raw scipy OdeResult
A steady solution has no time axis: sol.T has shape (nx,) in 1D and (nx, ny) in 2D.
See Steady-State Problems.
User Guide
Coefficients and Callables
Every coefficient in uPDE — diffusivity, velocity, source — accepts three forms:
| Form | Example |
|---|---|
| scalar | 0.05 |
| ndarray | D_arr — used as-is; must match the field shape |
| callable | lambda x, T: 1 + T**2 — called at every RHS evaluation |
| string | 'u' — resolved from the live coupled state (field coupling) |
Callable signature convention
Callables always receive coordinates first, then keyword arguments. Declare only
what you need — uPDE uses inspect.signature to inject only the parameters your
function actually declares:
| Dimension | Signature | Notes |
|---|---|---|
| 1D | f(x) |
coordinates only |
| 1D | f(x, t) |
coordinates + current time |
| 1D | f(x, fieldA) |
coordinates + one field |
| 1D | f(x, t, fieldA) |
coordinates + time + one field |
| 1D | f(x, t, **fields) |
coordinates + time + all fields |
| 2D | f(x, y) |
coordinates only |
| 2D | f(x, y, t) |
coordinates + time |
| 2D | f(x, y, fieldA) |
coordinates + one field |
| 2D | f(x, y, t, fieldA) |
coordinates + time + one field |
| 2D | f(x, y, t, **fields) |
coordinates + time + all fields |
The ordering rule is: coordinates → t → field names. t is a reserved name
and cannot be used as a field name. Fixed parameters go in via closures:
k1 = 10.0
eq.add_source(expr=lambda x, cA, cB: -k1 * cA * cB) # k1 captured by closure
Boundary Conditions
Dirichlet — prescribed value
eq.set_bc(side='left', kind='dirichlet', value=1.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
# time-varying
eq.set_bc(side='left', kind='dirichlet', value=lambda t: np.sin(t))
Neumann — prescribed normal derivative
eq.set_bc(side='right', kind='neumann', value=0.0) # zero-flux
eq.set_bc(side='left', kind='neumann', value=2.5) # dphi/dn = 2.5
value is the outward normal derivative $\partial\phi/\partial n$: on the right wall
$\partial\phi/\partial n = \partial\phi/\partial x$, on the left wall
$\partial\phi/\partial n = -\partial\phi/\partial x$. With diffusion $D$, a positive value
means a flux $D,\partial\phi/\partial n$ into the domain.
In 1D, a side with no BC set behaves as a zero-flux wall.
Periodic
eq.set_bc(kind='periodic') # 1D
eq.set_bc(kind='periodic', side='x') # 2D — wrap left ↔ right
eq.set_bc(kind='periodic', side='y') # 2D — wrap bottom ↔ top
eq.set_bc(kind='periodic', side='all') # 2D — fully periodic
Side shortcuts
| Dimension | side= |
Edge |
|---|---|---|
| 1D | 'left' |
x[0] |
| 1D | 'right' |
x[-1] |
| 2D | 'left' |
i=0 (all j) |
| 2D | 'right' |
i=-1 (all j) |
| 2D | 'bottom' |
j=0 (all i) |
| 2D | 'top' |
j=-1 (all i) |
| 1D / 2D | 'all' |
Every edge — both ends in 1D, all four walls in 2D (scalar value only) |
| 1D / 2D | ['left', 'right', ...] |
Each listed edge; value may be a scalar or a matching list |
A later set_bc on a named side replaces any earlier BC on that side, so the natural
pattern is a default followed by targeted overrides:
eq.set_bc(side='all', kind='dirichlet', value=0.0)
eq.set_bc(side='top', kind='neumann', value=0.0) # replaces the top-wall BC
Custom mask BCs (2D)
mask = np.zeros((nx, ny), dtype=bool)
mask[0, :ny//2] = True # bottom half of left wall
eq.set_bc(mask=mask, kind='dirichlet', value=2.0)
Initial Conditions
eq.set_ic(0.0) # scalar constant
eq.set_ic(np.zeros(n)) # 1D array
eq.set_ic(lambda x: np.sin(np.pi * x)) # 1D callable
eq.set_ic(lambda x, y: np.sin(np.pi*x)*np.sin(np.pi*y)) # 2D callable
# override at solve time (useful for parameter sweeps)
sol = PDESystem([eq]).solve(t_span=(0, 1), ICs={'T': new_ic})
Coupled Systems
Multi-field problems are built by creating one PDE per field and passing them all to
PDESystem. Fields reference each other by string name. PDESystem validates all
references at construction time.
k1 = 10.0
eqA = PDE('cA', x=x)
eqA.add_diffusion(diffusivity=1.0)
eqA.add_source(expr=lambda x, cA, cB: -k1 * cA * cB)
eqB = PDE('cB', x=x)
eqB.add_diffusion(diffusivity=1.0)
eqB.add_source(expr=lambda x, cA, cB: -k1 * cA * cB)
sol = PDESystem([eqA, eqB]).solve(t_span=(0, 0.5), method='RK45')
# sol.cA, sol.cB — each shape (n, nt)
2D Problems
Pass a second grid y to the PDE constructor. Everything else uses the same API.
x = np.linspace(0, 1, 64)
y = np.linspace(0, 1, 64)
eq = PDE('T', x=x, y=y)
eq.add_diffusion(diffusivity=0.05) # isotropic
eq.add_diffusion(diffusivity_x=0.1, diffusivity_y=0.005) # anisotropic
eq.add_advection(velocity_x=1.0, velocity_y=0.5) # 2D advection
# sol.T has shape (64, 64, nt)
Stiffness: 2D diffusion is always stiff — explicit stability requires Δt ∼ Δx². Use
method='BDF'for diffusion-dominated 2D problems.
Time-Dependent Callables
Any coefficient — velocity, diffusivity, or source — can depend on the current
solver time t by declaring it as a parameter after the spatial coordinates.
No other changes to your code are required.
import numpy as np
from upde import PDE
x = np.linspace(0, 1, 128)
eq = PDE('C', x=x)
# Velocity that ramps up linearly over the first 10 seconds
eq.add_advection(velocity=lambda x, t: min(t / 10.0, 1.0) * np.ones_like(x))
# Source that shuts off after 6 hours
eq.add_source(expr=lambda x, t, C: (1.0 if t < 6*3600 else 0.0) - 0.01 * C)
# Diffusivity from a time-interpolated dataset (e.g. ERA5 reanalysis)
import scipy.interpolate as interp
D_interp = interp.interp1d(time_points, D_values)
eq.add_diffusion(diffusivity=lambda x, t: D_interp(t) * np.ones_like(x))
eq.set_bc(kind='periodic')
eq.set_ic(ic)
sol = eq.solve(t_span=(0, 86400), method='RK45')
For 2D, t comes after both spatial coordinates:
eq2d = PDE('C', x=x, y=y)
# Time-varying 2D wind field
eq2d.add_advection(
velocity_x=lambda x, y, t: interp_U(t) * np.ones_like(x),
velocity_y=lambda x, y, t: interp_V(t) * np.ones_like(x),
)
Note:
tis a reserved name.PDE('t', ...)raises aValueError. Use'T','tau', or any other name for a temperature-like field.
Interior Boundary Conditions
set_interior_bc enforces a condition on an arbitrary boolean mask at every RHS
evaluation — useful for obstacles, cylinders, and inclusions.
# Circular obstacle — no-slip
cyl = (X - cx)**2 + (Y - cy)**2 < radius**2
eq.set_interior_bc(cyl, kind='dirichlet', value=0.0)
# Insulating inclusion — zero flux (approximate)
eq.set_interior_bc(inclusion_mask, kind='neumann')
Limitations: the mask representation is first-order accurate (staircase surface). The Neumann condition freezes the interior RHS to zero, which approximates $\partial\phi/\partial n = 0$ but does not enforce it exactly on curved boundaries.
Choosing a Solver
| Method | Type | Best for |
|---|---|---|
'RK45' |
Explicit | Advection-dominated, 1D diffusion, default |
'RK23' |
Explicit | Quick exploratory runs |
'BDF' |
Implicit | 2D diffusion, stiff reaction-diffusion |
'Radau' |
Implicit | Very stiff systems, higher accuracy than BDF |
'DOP853' |
Explicit | Smooth non-stiff problems, high accuracy |
sol = PDESystem([eq]).solve(
t_span = (0, 1),
method = 'BDF',
rtol = 1e-4,
atol = 1e-6,
t_eval = np.linspace(0, 1, 50),
)
Advection Schemes
add_advection and add_flux take a scheme keyword that sets the spatial
discretisation of the advective term:
scheme |
Order | Character |
|---|---|---|
'upwind' (default) |
1st | Monotone, but smears fronts (numerical diffusion $\tfrac{c,\Delta x}{2},\partial_{xx}$) |
'central' |
2nd | No numerical diffusion, but oscillates at steep gradients |
'minmod', 'van_leer', 'mc', 'superbee' |
2nd in smooth regions | Limited (TVD): sharp fronts, no new oscillations |
eq.add_advection(velocity=1.0, scheme='van_leer')
eq.add_flux(flux=lambda u: 0.5 * u**2, scheme='superbee')
# 2D
eq2.add_advection(velocity_x='u', velocity_y='v', scheme='mc')
The limited schemes use MUSCL reconstruction. Each cell gets a limited slope $\sigma_i = \psi(r_i),(\phi_{i+1} - \phi_i)$ with $r_i = (\phi_i - \phi_{i-1})/(\phi_{i+1} - \phi_i)$, and the face values are extrapolated from the upwind cell, e.g. $\phi_{i+1/2}^{L} = \phi_i + \tfrac{1}{2}\sigma_i$. The limiter $\psi(r)$ is zero at local extrema ($r \le 0$), so the scheme drops to first order there and creates no new maxima or minima:
| Limiter | $\psi(r)$ for $r > 0$ | Behaviour |
|---|---|---|
'minmod' |
$\min(1, r)$ | Most diffusive, very robust |
'van_leer' |
$\dfrac{2r}{1 + r}$ | Smooth, good general-purpose choice |
'mc' |
$\min(2r, \tfrac{1+r}{2}, 2)$ | Sharper than van Leer |
'superbee' |
$\max\big(\min(2r, 1), \min(r, 2)\big)$ | Sharpest; tends to square off smooth peaks |
For add_flux, the reconstructed face states are combined with the local
Lax–Friedrichs (Rusanov) flux, which reduces to plain upwinding for a linear flux and
handles sonic points correctly (e.g. Burgers expansion fans through $u = 0$). The
first-order 'upwind' flux is the same Rusanov flux with zero slopes.
Notes:
- In 2D the reconstruction is applied dimension by dimension (along $x$, then along $y$).
- Boundary cells fall back to first-order upwind (except with periodic BCs).
- Limited schemes are nonlinear in $\phi$ even for linear advection, so
solve_steady(method='auto')uses the Newton solver. - The no-new-oscillations property is formally guaranteed with strong-stability-preserving
(SSP) time integrators. The adaptive
solve_ivpmethods are not SSP but behave well at typical tolerances.
Steady-State Problems
solve_steady() skips time integration and solves $\mathrm{RHS}(\phi) = 0$ directly,
where $\mathrm{RHS}$ is the same spatial operator (terms + boundary conditions) used by
solve(). It is available on both PDE (uncoupled equations) and PDESystem.
x = np.linspace(0, 1, 101)
eq = PDE('T', x=x)
eq.add_diffusion(diffusivity=1.0)
eq.add_source(expr=lambda x: 1.0)
eq.set_bc(side='left', kind='dirichlet', value=0.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
sol = eq.solve_steady() # sol.T has shape (nx,)
The initial condition is not used. To give the solver a starting point, pass guess:
a scalar or array for PDE.solve_steady, or a dict {field: scalar | array} for
PDESystem.solve_steady (unspecified fields default to zero).
sol = PDESystem([eqA, eqB]).solve_steady(guess={'cA': 1.0, 'cB': 1.0})
Method selection (method=):
| Method | Algorithm | Use when |
|---|---|---|
'auto' (default) |
Tests the RHS for linearity, then picks one of the two below | Almost always |
'linear' |
One sparse linear solve | The RHS is linear in the fields |
'nonlinear' |
Sparse Newton iteration; Jacobian by coloured finite differences | Nonlinear sources, coefficients, or coupling |
For large linear 2D problems, iterative=True (with method='linear') replaces the
direct sparse LU with GMRES, preconditioned by algebraic multigrid if pyamg is installed
and by incomplete LU otherwise. maxiter (default 50) caps the Newton iterations and
tol (default 1e-8) sets the convergence tolerance.
Check convergence with sol.success and sol.residual ($\max|\mathrm{RHS}(\phi)|$ at the
returned solution). A steady state must exist for the problem as posed: for example, pure
diffusion with Neumann conditions on every boundary has no unique steady solution.
Pre-built Equations
equations.py provides factory functions for common PDE families.
All factories return plain PDE objects (or NamedPDESystem for multi-field systems)
so the full API remains available after construction.
from upde import (HeatEquation, AdvectionDiffusion, Burgers,
ConservationLaw, ReactionDiffusion,
WaveEquation, GrayScott, NavierStokes2D,
MixtureFraction)
1D vs 2D: all single-field factories support both 1D and 2D — pass y to activate
2D mode. Burgers is 1D only. WaveEquation, GrayScott, and NavierStokes2D
require y (2D only).
Direct solve: single-field equations that reference no external fields can be
solved without constructing a PDESystem:
sol = eq.solve(t_span=(0, 1), method='BDF')
If the equation references an external field, solve() raises a ValueError with a
clear message directing you to use PDESystem.
HeatEquation
$$\frac{\partial T}{\partial t} = \nabla \cdot (D,\nabla T)$$
HeatEquation(field, x, y=None, diffusivity=1.0)
| Parameter | Description |
|---|---|
field |
Field name string |
x |
1D coordinate array |
y |
1D coordinate array (activates 2D mode) |
diffusivity |
$D$ — scalar, array, or callable; may depend on space or the field |
# 1D rod: T=1 on left, T=0 on right
eq = HeatEquation('T', x=x, diffusivity=0.01)
eq.set_bc(side='left', kind='dirichlet', value=1.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
eq.set_ic(0.0)
sol = eq.solve(t_span=(0, 1), method='BDF')
# 2D with spatially varying diffusivity
eq2 = HeatEquation('T', x=x, y=y, diffusivity=lambda x, y: 1 + x)
AdvectionDiffusion
$$\frac{\partial T}{\partial t} + c,\frac{\partial T}{\partial x} = \nabla \cdot (D,\nabla T)$$
AdvectionDiffusion(field, x, y=None,
velocity=None, velocity_x=None, velocity_y=None,
diffusivity=0.0,
diffusivity_x=None, diffusivity_y=None,
scheme='upwind')
Advection uses the convective form, upwinded on sign(c); scheme selects the
discretisation (see Advection Schemes).
Diffusion is conservative central.
All coefficients may be scalars, arrays, callables, or string field names
(for coupling to a velocity field from another equation).
# 1D — scalar transport with constant velocity
eq = AdvectionDiffusion('T', x=x, velocity=1.0, diffusivity=0.005)
eq.set_bc(kind='periodic')
eq.set_ic(np.exp(-((x - 0.3)**2) / 0.01))
sol = eq.solve(t_span=(0, 1), method='RK45')
# 2D — passive scalar advected by a velocity field from another equation
eq_T = AdvectionDiffusion('T', x=x, y=y,
velocity_x='u', velocity_y='v',
diffusivity=0.001)
# eq_T.solve() would raise ValueError here — it references 'u' and 'v'
# Must use PDESystem([eq_u, eq_v, eq_T]).solve(...)
Burgers
$$\frac{\partial u}{\partial t} + \frac{\partial}{\partial x}!\left(\frac{u^2}{2}\right) = \nu,\frac{\partial^2 u}{\partial x^2}$$
Burgers(field, x, viscosity=0.0, scheme='upwind')
Written in conservation form with flux $F(u) = u^2/2$.
Wave speed $a = u$ is inferred automatically and used in the upwind face flux.
Set viscosity=0 for the inviscid case (shock forms in finite time).
For inviscid problems a limited scheme (e.g. scheme='van_leer') keeps shocks sharp
without oscillations; see Advection Schemes.
1D only.
x = np.linspace(0, 2*np.pi, 512)
# Viscous — shock is regularised
eq = Burgers('u', x=x, viscosity=0.05)
eq.set_bc(kind='periodic')
eq.set_ic(np.sin(x))
sol = eq.solve(t_span=(0, 3), method='RK45')
# Inviscid — wave steepens to a shock
eq_inv = Burgers('u', x=x, viscosity=0.0)
eq_inv.set_bc(kind='periodic')
eq_inv.set_ic(np.sin(x))
sol_inv = eq_inv.solve(t_span=(0, 1), method='RK45')
ConservationLaw
$$\frac{\partial u}{\partial t} + \frac{\partial F(u)}{\partial x} = 0$$
ConservationLaw(field, x, flux, flux_y=None, scheme='upwind')
| Parameter | Description |
|---|---|
flux |
Callable $F(u)$ — 1D flux, or x-direction flux in 2D |
flux_y |
Callable $G(u)$ — y-direction flux (2D only) |
scheme |
'upwind' (default), 'central', or a limiter: 'minmod', 'van_leer', 'mc', 'superbee' — see Advection Schemes |
The wave speed $a = dF/du$ is inferred automatically via finite difference.
The default 'upwind' scheme uses the local Lax–Friedrichs (Rusanov) face flux
$F_{i+1/2} = \tfrac12(F_i + F_{i+1}) - \tfrac12\alpha,(u_{i+1} - u_i)$ with
$\alpha = \max(|a_i|, |a_{i+1}|)$: exact upwinding for a linear flux, conservative and
entropy-correct where $a$ changes sign (e.g. traffic flow, Burgers with $u$ of both signs).
# LWR traffic flow: F(ρ) = ρ(1−ρ), wave speed a = 1−2ρ
eq = ConservationLaw('rho', x=x, flux=lambda rho: rho * (1 - rho))
eq.set_bc(kind='periodic')
eq.set_ic(0.2 + 0.6 * np.exp(-((x - 0.3)**2) / 0.005))
sol = eq.solve(t_span=(0, 0.5), method='RK45')
# Buckley-Leverett (two-phase flow)
eq2 = ConservationLaw('s', x=x,
flux=lambda s: s**2 / (s**2 + (1 - s)**2))
ReactionDiffusion
$$\frac{\partial u}{\partial t} = \nabla \cdot (D,\nabla u) + R(u,,x,,\ldots)$$
ReactionDiffusion(field, x, y=None, diffusivity=1.0, reaction=None)
The reaction callable R may reference other coupled fields by name,
enabling multi-species systems to be built by combining multiple instances.
# Fisher-KPP travelling wave
r = 1.0; D = 0.5
eq = ReactionDiffusion('u', x=x, diffusivity=D,
reaction=lambda x, u: r * u * (1 - u))
eq.set_bc(side='left', kind='dirichlet', value=1.0)
eq.set_bc(side='right', kind='dirichlet', value=0.0)
eq.set_ic(lambda x: (x < 1.0).astype(float))
sol = eq.solve(t_span=(0, 20), method='BDF')
# Theoretical wave speed: c* = 2*sqrt(r*D)
# FitzHugh-Nagumo (two coupled species)
eps, beta, gamma = 0.1, 0.5, 1.0
eq_v = ReactionDiffusion('v', x=x, diffusivity=1.0,
reaction=lambda x, v, w: v - v**3/3 - w)
eq_w = ReactionDiffusion('w', x=x, diffusivity=0.0,
reaction=lambda x, v, w: eps*(v - beta*w + gamma))
sol = PDESystem([eq_v, eq_w]).solve(t_span=(0, 100), method='BDF')
WaveEquation
$$\frac{\partial^2 u}{\partial t^2} = c^2,\nabla^2 u$$
Split into a first-order system:
$$\frac{\partial u}{\partial t} = u_t \qquad \frac{\partial u_t}{\partial t} = c^2,\nabla^2 u$$
WaveEquation(u_name, ut_name, x, y=None, speed=1.0)
Returns a NamedPDESystem with attributes named after the two fields.
BCs and ICs must be set on both fields.
wave = WaveEquation('u', 'ut', x=x, speed=1.0)
wave.u.set_bc(side='left', kind='dirichlet', value=0.0)
wave.u.set_bc(side='right', kind='dirichlet', value=0.0)
wave.ut.set_bc(side='left', kind='dirichlet', value=0.0)
wave.ut.set_bc(side='right', kind='dirichlet', value=0.0)
wave.u.set_ic(lambda x: np.sin(np.pi * x)) # plucked string
wave.ut.set_ic(0.0) # released from rest
sol = wave.solve(t_span=(0, 2), method='RK45')
# Analytical solution: u(x,t) = cos(π c t) sin(π x)
GrayScott
$$\frac{\partial u}{\partial t} = D_u,\nabla^2 u - uv^2 + F(1-u)$$ $$\frac{\partial v}{\partial t} = D_v,\nabla^2 v + uv^2 - (F+k)v$$
GrayScott(u_name, v_name, x, y=None,
Du=2e-5, Dv=1e-5, F=0.04, k=0.06)
Returns a NamedPDESystem. The autocatalytic $uv^2$ term drives Turing-type
pattern formation. Pattern type depends on $F$ and $k$:
| Pattern | $F$ | $k$ |
|---|---|---|
| Spots | 0.035 | 0.065 |
| Stripes | 0.040 | 0.060 |
| Solitons | 0.025 | 0.055 |
| Chaos | 0.026 | 0.051 |
gs = GrayScott('u', 'v', x=x, y=y, Du=2e-5, Dv=1e-5, F=0.035, k=0.065)
gs.u.set_bc(kind='periodic')
gs.v.set_bc(kind='periodic')
u0 = np.ones((nx, ny)); v0 = np.zeros((nx, ny))
cx, cy = nx//2, ny//2; r = nx//10
u0[cx-r:cx+r, cy-r:cy+r] = 0.5
v0[cx-r:cx+r, cy-r:cy+r] = 0.25
gs.u.set_ic(u0); gs.v.set_ic(v0)
sol = gs.solve(t_span=(0, 3000), method='BDF', rtol=1e-4, atol=1e-6)
# sol.u, sol.v — shape (nx, ny, nt)
Solver note: Gray-Scott is stiff. Always use
method='BDF'or'Radau'. Pattern formation requires long integration times (t ~ 1000–5000).
NavierStokes2D
$$\frac{\partial u}{\partial t} + u\frac{\partial u}{\partial x} + v\frac{\partial u}{\partial y} = -\frac{1}{\rho}\frac{\partial p}{\partial x} + \nu\nabla^2 u$$
$$\frac{\partial v}{\partial t} + u\frac{\partial v}{\partial x} + v\frac{\partial v}{\partial y} = -\frac{1}{\rho}\frac{\partial p}{\partial y} + \nu\nabla^2 v$$
$$\frac{\partial p}{\partial t} = -\beta!\left(\frac{\partial u}{\partial x}
- \frac{\partial v}{\partial y}\right) + \varepsilon,\nabla^2 p$$
NavierStokes2D(u_name, v_name, p_name, x, y,
nu=0.01, rho=1.0, beta=0.5,
pressure_stabilisation=None)
| Parameter | Description |
|---|---|
nu |
Kinematic viscosity |
rho |
Density |
beta |
Artificial compressibility parameter. Larger → tighter divergence constraint but stiffer system |
pressure_stabilisation |
$\varepsilon$ for $\varepsilon\nabla^2 p$ term. Default None sets $\varepsilon = 0.5\Delta x^2$ automatically to suppress checkerboard oscillations. Set to 0.0 to disable |
Returns a NamedPDESystem with attributes named after the three fields.
BCs and ICs must be set on all three fields.
Field names are user-supplied — pass whatever names you want:
ns = NavierStokes2D('u', 'v', 'p', x=x, y=y, nu=0.01)
# or
ns = NavierStokes2D('vx', 'vy', 'pressure', x=x, y=y, nu=0.01)
Typical BC pattern:
ns = NavierStokes2D('u', 'v', 'p', x=x, y=y, nu=0.01, rho=1.0, beta=0.5)
# u: inflow left, outflow right, no-slip top/bottom
ns.u.set_bc(side='left', kind='dirichlet', value=U_in)
ns.u.set_bc(side='right', kind='neumann', value=0.0)
ns.u.set_bc(side='bottom', kind='dirichlet', value=0.0)
ns.u.set_bc(side='top', kind='dirichlet', value=0.0)
# v: no-slip / outflow
ns.v.set_bc(side='left', kind='dirichlet', value=0.0)
ns.v.set_bc(side='right', kind='neumann', value=0.0)
ns.v.set_bc(side='bottom', kind='dirichlet', value=0.0)
ns.v.set_bc(side='top', kind='dirichlet', value=0.0)
# p: Neumann everywhere
for side in ('left', 'right', 'bottom', 'top'):
ns.p.set_bc(side=side, kind='neumann', value=0.0)
# ICs
ns.u.set_ic(U_in); ns.v.set_ic(0.0); ns.p.set_ic(0.0)
sol = ns.solve(t_span=(0, 60), method='RK45', rtol=1e-4, atol=1e-6)
Cylinder obstacle:
cyl = (X - cx)**2 + (Y - cy)**2 < radius**2
ns.u.set_interior_bc(cyl, kind='dirichlet', value=0.0)
ns.v.set_interior_bc(cyl, kind='dirichlet', value=0.0)
ns.p.set_interior_bc(cyl, kind='neumann')
Important: The artificial compressibility method is not divergence-free. The residual $\nabla\cdot\mathbf{u} = O(1/\beta)$ is expected behaviour, not a bug. For tighter incompressibility, increase $\beta$ (at the cost of stiffness).
MixtureFraction
$$\frac{\partial Z}{\partial t} + \mathbf{u}\cdot\nabla Z = \nabla\cdot(D,\nabla Z)$$
MixtureFraction(field, x, y=None,
velocity=None, velocity_x=None, velocity_y=None,
diffusivity=1e-4)
Mixture-fraction transport for flamelet-based non-premixed combustion.
$Z \in [0,1]$ is a conserved scalar: $Z=0$ is pure oxidiser, $Z=1$ is pure fuel.
No chemical source term appears — the stiff combustion chemistry is entirely
pre-tabulated in a FlameletTable and evaluated after the solve.
This is a named wrapper around AdvectionDiffusion with a combustion-appropriate
default diffusivity ($D = 10^{-4}$ m²/s, the thermal diffusivity of air at 300 K).
from upde import MixtureFraction, PDESystem
from upde.chemistry import FlameletTable
x = np.linspace(0, 1, 256)
table = FlameletTable.burke_schumann()
eq = MixtureFraction('Z', x=x, diffusivity=1e-4)
eq.set_bc(side='left', kind='dirichlet', value=0.0) # oxidiser
eq.set_bc(side='right', kind='dirichlet', value=1.0) # fuel
eq.set_ic(lambda x: x)
sol = PDESystem([eq]).solve(t_span=(0, 5000), method='BDF')
# Reconstruct thermochemistry from the flamelet table
T_field = table.T(sol.Z[:, -1]) # temperature [K]
Y_CH4 = table.Y('CH4', sol.Z[:, -1]) # fuel mass fraction
Couple to a velocity field from NavierStokes2D by passing string references:
zz = MixtureFraction('Z', x=x, y=y,
velocity_x='u', velocity_y='v',
diffusivity=1e-4)
sol = PDESystem([ns.u_eq, ns.v_eq, ns.p_eq, zz]).solve(...)
Solver note: use
method='BDF'for diffusion-dominated problems. Advection-dominated jets at moderate Péclet number can use'RK45'.
Combustion and Flamelet Chemistry
uPDE supports non-premixed combustion via the mixture-fraction / flamelet approach:
the transport equation for $Z$ is solved by solve_ivp as a pure convection-diffusion
problem (no stiff chemistry), and temperature and species are reconstructed afterward
from a pre-integrated flamelet table.
This completely avoids the extreme stiffness of full chemistry mechanisms — the
solve_ivp integrator never sees chemical timescales.
FlameletTable
from upde.chemistry import FlameletTable
Maps $Z \in [0,1]$ to temperature and species mass fractions via numpy.interp.
Construction
# 1. Analytic Burke-Schumann (no dependencies — good for education and testing)
table = FlameletTable.burke_schumann(
Z_st=0.055, # stoichiometric mixture fraction (CH4/air default)
T_fuel=300.0, # fuel-stream temperature [K]
T_ox=300.0, # oxidiser-stream temperature [K]
T_ad=2230.0, # adiabatic flame temperature [K]
)
# 2. Load a previously saved table (no Cantera at solve time)
table = FlameletTable.from_file('ch4_air.npz')
# 3. From raw arrays (user-supplied data)
table = FlameletTable(Z_grid, T, species={'CH4': Y_CH4, 'O2': Y_O2, ...})
# 4. From Cantera counterflow flame (requires pip install cantera)
table = FlameletTable.from_cantera(
mechanism='gri30.yaml',
fuel='CH4',
oxidizer='O2:0.21,N2:0.79',
T_fuel=300.0, T_ox=300.0,
)
table.save('ch4_air_gri30.npz')
Accessors
All accessors accept any numpy array shape. $Z$ is automatically clipped to $[0,1]$.
T_field = table.T(Z) # temperature [K]
Y_CH4 = table.Y('CH4', Z) # species mass fraction
rho_field = table.rho(Z, P=101325.0) # density [kg/m³] from ideal gas law
print(table.species) # ['CH4', 'O2', 'CO2', 'H2O', 'N2']
print(table.Z_st) # stoichiometric mixture fraction (location of peak T)
Serialisation
table.save('ch4_air.npz')
table = FlameletTable.from_file('ch4_air.npz')
Workflow
import numpy as np
from upde import MixtureFraction, PDESystem
from upde.chemistry import FlameletTable
# Step 1 — build the chemistry table (once)
table = FlameletTable.burke_schumann()
# Step 2 — set up Z transport
x = np.linspace(0, 1, 256)
eq = MixtureFraction('Z', x=x, diffusivity=1e-4)
eq.set_bc(side='left', kind='dirichlet', value=0.0)
eq.set_bc(side='right', kind='dirichlet', value=1.0)
eq.set_ic(0.0)
# Step 3 — solve (no stiff chemistry in the ODE!)
sol = PDESystem([eq]).solve(t_span=(0, 5000), method='BDF',
rtol=1e-6, atol=1e-8)
# Step 4 — reconstruct thermochemistry (instant — just numpy.interp)
Z_final = sol.Z[:, -1]
T = table.T(Z_final)
Y_CH4 = table.Y('CH4', Z_final)
rho = table.rho(Z_final)
x_flame = x[np.argmax(T)]
print(f'Flame location: x = {x_flame:.4f} (Z_st = {table.Z_st:.4f})')
Using Cantera
FlameletTable.from_cantera() requires pip install cantera. It runs a 1-D
counterflow diffusion flame, extracts $Z$–$T$–$Y_k$ profiles via Bilger's
definition, and returns a ready-to-use FlameletTable. Save the result so
Cantera is only needed once:
# Run once (needs Cantera)
table = FlameletTable.from_cantera(
mechanism='gri30.yaml',
fuel='CH4',
oxidizer='O2:0.21,N2:0.79',
)
table.save('ch4_air_gri30.npz')
# All subsequent runs — no Cantera needed
table = FlameletTable.from_file('ch4_air_gri30.npz')
If Cantera is not installed, a helpful ImportError is raised. Use
FlameletTable.burke_schumann() as a self-contained alternative.
API Reference
PDE
PDE(field, x, y=None)
| Method | Description |
|---|---|
add_advection(velocity=, scheme=) |
1D convective form $-c,\partial_x\phi$, upwind on sign(c) |
add_advection(velocity_x=, velocity_y=, scheme=) |
2D convective form |
add_diffusion(diffusivity=) |
Isotropic diffusion $\nabla\cdot(D\nabla\phi)$ |
add_diffusion(diffusivity_x=, diffusivity_y=) |
Anisotropic diffusion |
add_source(expr=) |
Pointwise source $S(x[,y], \texttt{**fields})$ |
add_flux(flux=, scheme=) |
1D conservation law $-\partial_x F(\phi)$, wave speed inferred |
add_flux(flux_x=, flux_y=, scheme=) |
2D conservation law |
add_term(fn) |
Generic operator term — see Operator Reference |
set_bc(kind, side=, mask=, value=) |
Domain boundary condition |
set_interior_bc(mask, kind, value=) |
Interior obstacle BC |
set_ic(ic) |
Initial condition: scalar, array, or callable |
solve(t_span, **kwargs) |
Solve directly (uncoupled equations only) |
solve_steady(guess=None, method='auto', tol=1e-8, **kwargs) |
Steady state directly (uncoupled equations only) |
PDESystem
PDESystem(equations)
sol = PDESystem([eq1, eq2, ...]).solve(t_span, ICs=None, method='RK45',
t_eval=None, **kwargs)
sol = PDESystem([eq1, eq2, ...]).solve_steady(guess=None, method='auto', tol=1e-8,
iterative=False, maxiter=50)
Validates all field references at construction time.
For solve, **kwargs are forwarded to scipy.integrate.solve_ivp.
For solve_steady, method is 'auto', 'linear', or 'nonlinear'; iterative
applies to 'linear' only and maxiter to 'nonlinear' only.
See Steady-State Problems.
PDESolution
| Attribute | Description |
|---|---|
sol.<field> |
Field array — shape (nx, nt) in 1D, (nx, ny, nt) in 2D |
sol.t |
Time points (nt,) |
sol.success |
True if the integrator reached t_span[1] |
sol.message |
Integrator status string |
sol.raw |
Raw scipy.OdeResult |
Returned by solve_steady():
| Attribute | Description |
|---|---|
sol.<field> |
Steady field — shape (nx,) in 1D, (nx, ny) in 2D |
sol.success |
True if the solver converged |
sol.message |
Solver status string |
sol.residual |
$\max\lvert\mathrm{RHS}(\phi)\rvert$ at the solution |
sol.solver |
'linear' or 'nonlinear' — the path actually taken |
sol.nfev |
Number of RHS evaluations |
Operator Reference
Operators are injected into add_term functions by name matching — the order of
arguments does not matter. Any parameter whose name matches an operator or a declared
field is automatically injected; the rest are ignored.
# All equivalent — order doesn't matter, only names do
def my_rhs(u, Dx, Dxx): ...
def my_rhs(Dxx, Dx, u): ...
def my_rhs(Dx, u, Dxx): ...
# Use **fields to receive all coupled fields at once
def my_rhs(Dx, Dy, **fields):
u = fields['u']; v = fields['v']
...
Available operators
| Name | Expression | Notes |
|---|---|---|
Dx(phi) |
$\partial\phi/\partial x$ | 2nd-order central |
Dx(phi, 'upwind', c=v) |
$\partial\phi/\partial x$ | 1st-order upwind on sign(v) |
Dy(phi) |
$\partial\phi/\partial y$ | 2D only |
Dy(phi, 'upwind', c=v) |
$\partial\phi/\partial y$ | 2D only |
Dxx(phi) |
$\partial^2\phi/\partial x^2$ | 2nd-order central |
Dyy(phi) |
$\partial^2\phi/\partial y^2$ | 2D only |
Div_x(c, phi) |
$-\partial(c\phi)/\partial x$ | Central conservative |
Div_y(c, phi) |
$-\partial(c\phi)/\partial y$ | 2D only |
Div_flux_x(k, phi) |
$\partial(k,\partial\phi/\partial x)/\partial x$ | Diffusive flux |
Div_flux_y(k, phi) |
$\partial(k,\partial\phi/\partial y)/\partial y$ | 2D only |
Rule: never nest operators — use
Dxx(phi)notDx(Dx(phi)).
Boundaries: operators use the boundary conditions of the equation they are called in.
Div_flux_x/Div_flux_ytreat Neumann and unset walls exactly likeadd_diffusion, so the two can be mixed freely.Dx,Dy,Dxx,Dyyuse a zero-gradient ghost at Neumann and unset walls, consistently with each other.
Numerical Methods
| Term | Scheme | Order |
|---|---|---|
add_advection (upwind) |
Convective upwind | 1st in space |
add_advection (central) |
Convective central | 2nd in space |
add_advection (limited) |
MUSCL + slope limiter, TVD | 2nd in smooth regions, 1st at extrema |
add_diffusion |
Conservative central | 2nd in space |
add_flux (upwind) |
Rusanov (local Lax–Friedrichs) face flux, wave speed inferred | 1st in space |
add_flux (central) |
Central flux divergence | 2nd in space |
add_flux (limited) |
MUSCL + slope limiter, Rusanov face flux | 2nd in smooth regions, 1st at extrema |
Dx, Dy |
Central difference | 2nd in space |
Dxx, Dyy |
Central difference | 2nd in space |
Div_x, Div_y |
Central conservative | 2nd in space |
| Time integration | solve_ivp adaptive |
depends on method |
All spatial operators are BC-aware — ghost cells are constructed from the boundary conditions before each stencil application.
Troubleshooting
Solver fails or takes tiny steps
- Switch to
method='BDF'for diffusion-dominated or stiff problems - Loosen tolerances:
rtol=1e-3, atol=1e-5 - Check that BCs are consistent with the IC at
t=0
Fronts or shocks are smeared
- First-order
'upwind'adds numerical diffusion $\tfrac{c,\Delta x}{2},\partial_{xx}$ - Use a limited scheme, e.g.
add_advection(velocity=c, scheme='van_leer')
Solution blows up
- Add or increase diffusivity to stabilise advection
- Reduce
betain NavierStokes2D if the pressure equation is causing instability - Check that source terms don't have the wrong sign
Checkerboard oscillations in pressure (NavierStokes2D)
- The default
pressure_stabilisation=Nonesets $\varepsilon = 0.5\Delta x^2$ automatically - If oscillations persist, increase it:
pressure_stabilisation=dx**2 - Do not set it so large that the physical pressure gradient is smoothed away
ValueError: references external field(s)
PDE.solve()andPDE.solve_steady()only work for uncoupled equations- Use
PDESystem([eq1, eq2, ...]).solve(...)or.solve_steady(...)for coupled problems
Field not found in solution
- Check the field name string passed to
PDE(field, ...)matches what you access onsol - Field names are case-sensitive:
'T'and't'are different 't'is reserved for the solver time —PDE('t', ...)raisesValueError
Metadata
Release files for upde 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| upde-1.1.0.tar.gz | 69.4 kB | Without provenance | Details for upde-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| upde-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for upde-1.1.0-py3-none-any.whl |
Total release size: 129.2 kB
Release files / upde-1.1.0.tar.gz
| Download URL | upde-1.1.0.tar.gz |
|---|---|
| Size | 69.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e5859725be3faac64322c0c747e699f6f017e20a8946c30c7a70ee24d44f81c
|
|
BLAKE2b-256 checksum How to use checksums |
2faea225aba7a1b53ee98cf5deeff38e234c4381744e6d869fcd4ab352127e3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Release files / upde-1.1.0-py3-none-any.whl
| Download URL | upde-1.1.0-py3-none-any.whl |
|---|---|
| Size | 59.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eed99a78699ae48a470497e46b8ce6c8c4a579df251f8d8b5752bdc235ea9e27
|
|
BLAKE2b-256 checksum How to use checksums |
e43979129f313734548f533de0ba2bd578a05bc53a6fb4e118dc587502affb9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Security for upde 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.