|
numerics 0.1.0
|
A C++ scientific computing library: linear algebra, ODE/PDE solvers, spectral methods, statistical mechanics, and gnuplot-based plotting – all under a single #include "numerics.hpp".
The Lorenz system is integrated with num::solve + num::RK45 (adaptive Dormand-Prince) and the trajectory is plotted with num::plt – the full program is ~30 lines (examples/lorenz.cpp):
Implicit backward Euler via sparse CG. Grid2D carries geometry; ScalarField2D carries data. The sparse system A = I - coeff*L is assembled once and reused at every step:
The inner-loop layer shared by all solvers. Three tiers:
kernel::raw): always-inline dot/axpy/scale, BLAS-dispatchedkernel::array, kernel::reduce): tag-dispatched ops with seq_t/par_t compile-time policy tags; zero runtime overheadkernel::subspace): LinearOp interface, mgs_orthogonalize, arnoldi_step — the shared inner loops for GMRES, Lanczos, and expvnum::Vector, num::Matrix, num::SparseMatrix, num::BandedMatrixnum::Backend – seq, blocked, simd, blas, omp, gpu (for matmul/matvec)num::spectral::fft, num::spectral::ifft – complex DFT / inverse DFTnum::spectral::rfft, num::spectral::irfft – real-to-complex and inversenum::spectral::FFTPlan – precomputed plan for repeated transformsFFTBackend::seq (Cooley-Tukey radix-2), FFTBackend::fftw (FFTW3, optional)num::markov::UmbrellaWindow, MetropolisStats, UmbrellaStatsnum::Grid2D – geometry only: N, h, coordinate accessors, flat indexnum::Grid3D – 3D analogue with nx, ny, nznum::ScalarField2D – owns Grid2D + data vector; callable constructor fills from f(x,y)num::ScalarField3D, num::VectorField3D – 3D field types with trilinear samplingnum::solve(problem, algorithm) – C++20 concept-dispatched entry pointnum::ODEProblem{f, u0, t0, tf} – explicit ODE formulationnum::MCMCProblem{accept_prob, propose, n_sites} – MCMC formulationnum::Euler, num::RK4, num::RK45, num::BackwardEuler, num::Metropolisnum::SparseMatrix – CSR format; num::sparse_matvecnum::BandedMatrix – band-storage; num::banded_matvec, num::banded_solvenum::LinearSolver – universal callable: (rhs, x) -> SolverResultnum::Gnuplot – popen wrapper: operator<< for commands, send1d() for inline datanum::Series – std::vector<std::pair<double,double>> for (x, y) datanum::apply_siam_style(gp) – clean SIAM-style themenum::save_png(gp, file, w, h) – redirect output to PNGnum::set_loglog(gp), num::set_logx(gp) – axis scaling helpersRaylib-rendered simulations built on numerics. Each runs as a batch renderer: simulate all frames, export PNGs, composite with make_video.sh.
page_app_ising
Metropolis dynamics and umbrella-sampled nucleation on a 300x300 lattice. Reproduces Brendel et al. (2005) nucleation experiments.
| Library feature | Role |
|---|---|
num::markov::metropolis_sweep_prob | Sweep hot path with precomputed Boltzmann table |
num::markov::umbrella_sweep_prob | Per-sweep rejection / save-restore for window sampling |
num::SparseMatrix + sparse_matvec | Neighbour-sum matrix for energy observable (SIMD path) |
num::RunningStats, num::Histogram | Online mean, variance, nucleus-size distribution per window |
num::newton | Mean-field self-consistency equation solver |
page_app_fluid
Weakly compressible SPH with heat transport, rigid bodies, and particle injection. Tait EOS, cubic-spline kernel, Morris viscosity Laplacian.
| Library feature | Role |
|---|---|
num::CellList2D | O(1) neighbour queries; Newton-3rd-law pair traversal |
num::Backend dispatch | seq (pair loop) <-> omp (per-particle) selectable at runtime |
num::Vector | Flat particle attribute arrays |
page_app_fluid3d
3D WCSPH with opposing hose jets, heat transport, and a free-orbit camera whose orientation rotates the gravity vector.
| Library feature | Role |
|---|---|
num::CellList3D | 3D counting-sort spatial hash; 13-stencil Newton-3rd-law pairs |
num::Backend dispatch | seq (pair traversal) <-> omp (per-particle) |
num::Vector | Flat particle attribute arrays |
page_app_ns
Chorin projection on a staggered MAC grid; semi-Lagrangian advection; matrix-free CG pressure solve. Kelvin-Helmholtz double shear layer initial condition.
| Library feature | Role |
|---|---|
num::cg_matfree | Matrix-free CG for -grad^2p = r; warm-start from previous pressure field |
num::dot | Inner products inside CG (dispatched to best available backend) |
num::Vector | Velocity faces u, v; pressure p; intermediates u*, v*, rhs |
page_app_tdse
Strang operator splitting with Crank-Nicolson kinetic sweeps; Thomas algorithm for O(N) tridiagonal solves; Lanczos eigendecomposition; five interchangeable potentials.
| Library feature | Role |
|---|---|
num::thomas | O(N) complex tridiagonal solve per row/column (kinetic sweeps) |
num::lanczos | Krylov subspace for lowest eigenmodes |
num::brent | Bessel zero-finding for exact circular-well energies |
num::gauss_legendre | Norm and energy quadrature |
page_app_em
DC current flow + magnetostatics on a 32^3 voxel grid. Four Poisson problems (one electric, three magnetic) solved with matrix-free CG; interactive magnetic dipole.
| Library feature | Role |
|---|---|
num::cg_matfree | All four Poisson solves (variable-coefficient electric + 3x magnetic) |
num::Grid3D | 3D scalar field storage for phi, A, J, B components |
num::Vector | Flattened field arrays passed to the solver |
Run cmake --build build --target report to generate output/REPORT.md – a full snapshot of the current build:
output/plots/Sections whose backend was not found at configure time are left as placeholders, so the report always reflects exactly what is installed.
Notes on hardware-aware implementation: cache blocking, register tiling, and SIMD vectorization applied to the matrix operations in this library.
For derivations, error bounds, and proofs of the algorithms implemented here (LU/QR factorization, CG, GMRES, Lanczos, eigenvalue methods, quadrature, root finding, ODE integrators), see the companion notes:
Scientific Computing — adityadendukuri.github.io/cs111-scientific-computing-notes