numerics 0.1.0
Loading...
Searching...
No Matches
solve.hpp
Go to the documentation of this file.
1/// @file solve/solve.hpp
2/// @brief Problem-level solve(): the uniform solve(problem, algorithm) -> result
3/// verb over ODEProblem and LinearProblem. Repeated or warm-started linear solves
4/// use init(problem, algorithm) + solve(cache). The low-level cg()/gmres()/...
5/// kernels and ODE steppers remain the in-place primitives underneath. Stochastic
6/// sampling (MCMC) lives in solve/sample.hpp as sample(model, sampler).
7#pragma once
8
9#include "core/concepts.hpp"
11#include "linalg/solvers/cg.hpp"
16#include "ode/ode.hpp"
17#include "solve/algorithms.hpp"
18#include "solve/problems.hpp"
19#include <utility>
20
21namespace num {
22
23/// @brief Result of a linear solve: the solution vector plus convergence stats.
25 Vector u; ///< solution vector
26 idx iterations = 0; ///< iterations performed
27 real residual = 0.0; ///< final residual norm ||b - A u||
28 bool converged = false;
29};
30
31template<IsODEProblem P>
32ODEResult solve(const P& prob, const RK45& alg, ObserverFn obs = nullptr) {
33 ODEParams p{.t0 = prob.t0,
34 .tf = prob.tf,
35 .h = alg.h,
36 .rtol = alg.rtol,
37 .atol = alg.atol,
38 .max_steps = alg.max_steps};
39 return ode_rk45(prob.f, prob.u0, p, obs);
40}
41
42template<IsODEProblem P>
43ODEResult solve(const P& prob, const RK4& alg, ObserverFn obs = nullptr) {
44 return ode_rk4(prob.f, prob.u0, {.t0 = prob.t0, .tf = prob.tf, .h = alg.h}, obs);
45}
46
47template<IsODEProblem P>
48ODEResult solve(const P& prob, const Euler& alg, ObserverFn obs = nullptr) {
49 return ode_euler(prob.f, prob.u0, {.t0 = prob.t0, .tf = prob.tf, .h = alg.h}, obs);
50}
51
52// -- Linear systems: solve(LinearProblem, CG/GMRES/MINRES/PCG) -> LinearSolution --
53
54namespace detail {
55
56// The (operator x algorithm) dispatch, run in place into u (warm-startable).
57// cg()/gmres()/minres()/pcg() are themselves overloaded on the operand type.
58
59inline SolverResult
60run(const linalg::SPDMatrix<Matrix>& A, const Vector& b, Vector& u, const CG& a) {
61 return cg(A, b, u, a.tol, a.max_iter, a.backend);
62}
63
64template<class Op>
66SolverResult run(const Op& A, const Vector& b, Vector& u, const CG& a) {
67 return cg(A, b, u, a.tol, a.max_iter, a.backend);
68}
69
70inline SolverResult run(const Matrix& A, const Vector& b, Vector& u, const GMRES& a) {
71 return gmres(A, b, u, a.tol, a.max_iter, a.restart, a.backend);
72}
73
74inline SolverResult run(const SparseMatrix& A, const Vector& b, Vector& u, const GMRES& a) {
75 return gmres(A, b, u, a.tol, a.max_iter, a.restart);
76}
77
78template<class Op>
80SolverResult run(const Op& A, const Vector& b, Vector& u, const GMRES& a) {
81 return gmres(A, b, u, a.tol, a.max_iter, a.restart);
82}
83
84template<class Op>
86SolverResult run(const Op& A, const Vector& b, Vector& u, const MINRES& a) {
87 return minres(A, b, u, a.tol, a.max_iter, a.backend);
88}
89
90template<class Op, class M>
92SolverResult run(const Op& A, const Vector& b, Vector& u, const PCG<M>& a) {
93 return pcg(A, a.preconditioner, b, u, a.tol, a.max_iter, a.backend);
94}
95
96} // namespace detail
97
98/// @brief Reusable linear-solve cache (CommonSolve `init`/`solve!`): a view of
99/// the problem plus the algorithm and the current iterate. Re-solving warm-starts
100/// from cache.u. @note A and b are held by reference and must outlive the cache.
101template<class Op, class Alg>
103 const Op& A;
104 const Vector& b;
105 Alg alg;
106 Vector u; ///< warm-start on entry, solution on exit
107};
108
109/// @brief Build a solve cache; the iterate starts at zero.
110template<class Op, class Alg>
111LinearCache<Op, Alg> init(const LinearProblem<Op>& prob, const Alg& alg) {
112 return {prob.A, prob.b, alg, Vector(prob.b.size(), real(0))};
113}
114
115/// @brief Build a solve cache seeded with an initial guess u0 (warm start).
116template<class Op, class Alg>
117LinearCache<Op, Alg> init(const LinearProblem<Op>& prob, const Alg& alg, Vector u0) {
118 return {prob.A, prob.b, alg, std::move(u0)};
119}
120
121/// @brief Re-solve from a cache, warm-starting from its current iterate; cache.u
122/// is updated in place (the C++ spelling of CommonSolve `solve!`).
123template<class Op, class Alg>
125 const SolverResult r = detail::run(cache.A, cache.b, cache.u, cache.alg);
126 return {cache.u, r.iterations, r.residual, r.converged};
127}
128
129/// @brief One-shot linear solve: solve(problem, algorithm) == solve(init(...)).
130template<class Op, class Alg>
131LinearSolution solve(const LinearProblem<Op>& prob, const Alg& alg) {
132 Vector u(prob.b.size(), real(0));
133 const SolverResult r = detail::run(prob.A, prob.b, u, alg);
134 return {std::move(u), r.iterations, r.residual, r.converged};
135}
136
137} // namespace num
Algorithm tags: carry the numerics, not the mathematics.
Conjugate gradient solvers.
constexpr idx size() const noexcept
Definition vector.hpp:83
Sparse matrix in Compressed Sparse Row (CSR) format.
Definition sparse.hpp:17
Compile-time contract for the matrix-free product y = A*x.
Definition concepts.hpp:67
Operator declared to satisfy for all nonzero .
Definition concepts.hpp:82
Operator declared to satisfy .
Definition concepts.hpp:76
Storage and operator concepts for numerical routines.
Restarted GMRES for general linear systems.
Declared mathematical properties for stored matrices.
Minimum residual iteration for symmetric linear systems.
SolverResult run(const linalg::SPDMatrix< Matrix > &A, const Vector &b, Vector &u, const CG &a)
Definition solve.hpp:60
ODEResult ode_rk4(ODERhsFn f, Vector y0, ODEParams p={}, ObserverFn obs=nullptr)
Classic 4th-order Runge-Kutta, fixed step.
Definition ode.cpp:399
double real
Definition types.hpp:10
SolverResult gmres(const Op &A, const Vector &b, Vector &x, real tol=1e-6, idx max_iter=1000, idx restart=30)
Operator GMRES for any adapter.
Definition gmres.hpp:25
std::size_t idx
Definition types.hpp:11
LinearCache< Op, Alg > init(const LinearProblem< Op > &prob, const Alg &alg)
Build a solve cache; the iterate starts at zero.
Definition solve.hpp:111
SolverResult minres(const Op &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Definition minres.hpp:48
std::function< void(real t, const Vector &y)> ObserverFn
Definition ode.hpp:16
ODEResult solve(const P &prob, const RK45 &alg, ObserverFn obs=nullptr)
Definition solve.hpp:32
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:129
SolverResult pcg(const Op &A, const M &M_op, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Definition pcg.hpp:17
ODEResult ode_euler(ODERhsFn f, Vector y0, ODEParams p={}, ObserverFn obs=nullptr)
Forward Euler, 1st-order, fixed step.
Definition ode.cpp:392
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Definition cg.cpp:8
ODEResult ode_rk45(ODERhsFn f, Vector y0, ODEParams p={}, ObserverFn obs=nullptr)
Adaptive Dormand-Prince RK45 with FSAL and PI step-size control.
Definition ode.cpp:406
ODE and symplectic integrators.
Preconditioned conjugate gradient.
Problem types: carry the mathematics, not the numerics.
Common result type shared by all iterative solvers.
Backend backend
idx max_iter
Backend backend
Reusable linear-solve cache (CommonSolve init/solve!): a view of the problem plus the algorithm and t...
Definition solve.hpp:102
const Vector & b
Definition solve.hpp:104
const Op & A
Definition solve.hpp:103
Vector u
warm-start on entry, solution on exit
Definition solve.hpp:106
Linear system A x = b. A is any matrix or LinearOperator; b the RHS. Non-owning view over A and b (bi...
Definition problems.hpp:30
const Vector & b
Definition problems.hpp:32
Result of a linear solve: the solution vector plus convergence stats.
Definition solve.hpp:24
real residual
final residual norm ||b - A u||
Definition solve.hpp:27
idx iterations
iterations performed
Definition solve.hpp:26
Vector u
solution vector
Definition solve.hpp:25
Backend backend
Backend backend
const M & preconditioner
double atol
double rtol
bool converged
Whether tolerance was met.
idx iterations
Number of iterations performed.
real residual
Final residual norm ||b - Ax||.