|
numerics 0.1.0
|
Header: #include "pde/pde.hpp" Namespace: num, num::pde
Finite-difference PDE infrastructure: stencil operators, backward-Euler matrix assembly, Crank-Nicolson ADI, and explicit diffusion steps. Grid geometry and field storage live in fields/; time integration lives in ode/. The pde/ module is spatial operators only.
| Header | What it provides |
|---|---|
pde/stencil.hpp | 2D/3D Laplacians, gradient, divergence, curl, fiber sweeps |
pde/diffusion.hpp | backward_euler_matrix, make_cg_solver; implicit backward-Euler setup |
pde/fields.hpp | ScalarField3D, VectorField3D, FieldSolver, MagneticSolver |
pde/adi.hpp | CrankNicolsonADI – Strang-split CN solver for parabolic equations |
pde/poisson.hpp | poisson2d_fd, poisson2d – DST-based 2D Poisson solver, O(N^2 log N) |
All three stencils return h² ∇²u — divide by h² to recover the Laplacian:
| Function | Points | Order |
|---|---|---|
laplacian_stencil_2d | 5 (cross) | O(h²), Dirichlet |
laplacian_stencil_2d_periodic | 5 (cross) | O(h²), periodic |
laplacian_stencil_2d_4th | 13 (extended cross) | O(h⁴), Dirichlet |
The standard 5-point stencil:
\[y_{i,j} = x_{i+1,j} + x_{i-1,j} + x_{i,j+1} + x_{i,j-1} - 4\,x_{i,j}\]
The 4th-order 13-point stencil:
\[ y_{i,j} = \frac{1}{12}\bigl( -x_{i-2,j} + 16\,x_{i-1,j} - 30\,x_{i,j} + 16\,x_{i+1,j} - x_{i+2,j} -x_{i,j-2} + 16\,x_{i,j-1} + 16\,x_{i,j+1} - x_{i,j+2} \bigr) \]
with Dirichlet or periodic treatment at the boundaries.
Used internally by CrankNicolsonADI and available directly:
All operate on Grid3D objects with central differences and one-sided stencils at boundaries:
neg_laplacian_3d computes \((-\nabla^2 x)\) with Dirichlet BCs (boundary rows = identity), giving an SPD system suitable for CG.
See fields.md for full documentation. The classes live in num:::
ScalarField3D – uniform-grid scalar potential with trilinear samplingVectorField3D – three ScalarField3D componentsFieldSolver – static methods: solve_poisson, gradient, divergence, curlMagneticSolver – static methods: current_density, solve_magnetic_fieldPrefactored Crank-Nicolson solver for 2D parabolic equations via Strang splitting. Suitable for TDSE, heat equations with complex coefficients.
Factorises two ComplexTriDiag systems on construction ( \(O(N)\)):
td_half_ – for half-step sweeps ( \(\tau = dt/2\))td_full_ – for full-step sweep ( \(\tau = dt\))Applies one CN sub-step along the chosen axis:
x_axis = true – processes each column fiber (x-direction)x_axis = false – processes each row fiber (y-direction)td_half_ when tau < 0.75*dt, td_full_ otherwiseEach sweep solves the 1D tridiagonal system per fiber:
\[\left(I - i\alpha\,\nabla^2_{1D}\right)\psi^{n+1} = \left(I + i\alpha\,\nabla^2_{1D}\right)\psi^n, \qquad \alpha = \frac{\tau}{4h^2}\]
The LHS tridiagonal has sub/super-diagonal \(-i\alpha\) and main diagonal \(1 + 2i\alpha\); it is factored once on construction and reused for every fiber.
Assembly for the implicit system A = I - coeff*L where L is the 2D Dirichlet Laplacian. A is sparse SPD — conjugate gradient converges in O(N) iterations.
coeff = kappa * dt / h². Implicit stepping removes the dt < 0.25 h² Von Neumann stability constraint.
Forward Euler diffusion steps for 2D uniform grids. Von Neumann stability requires coeff <= 0.25.
Solves \(-\nabla^2 u = f\) on \((0,1)^2\) with homogeneous Dirichlet BCs via the Discrete Sine Transform. Two variants:
pde::poisson2d_fd – FD eigenvalues \(D_k = 2(1-\cos(k\pi/(N+1)))\), error \(O(h^2)\).pde::poisson2d – exact eigenvalues \((k\pi)^2\), machine-precision error for \(f\) in the DST eigenbasis.Both run in \(O(N^2 \log N)\) and require \(N = 2^p - 1\). See poisson.md for the full derivation and convergence table.