You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/lib.rs
+55-5Lines changed: 55 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
//!
3
3
//! DiffSol is a library for solving differential equations. It provides a simple interface to solve ODEs and semi-explicit DAEs.
4
4
//!
5
-
//! ## Getting Started
5
+
//! ## Solving ODEs
6
6
//!
7
7
//! To create a new problem, use the [OdeBuilder] struct. You can set the initial time, initial step size, relative tolerance, absolute tolerance, and parameters,
8
8
//! or leave them at their default values. Then, call the [OdeBuilder::build_ode] method with the ODE equations, or the [OdeBuilder::build_ode_with_mass] method
@@ -11,8 +11,11 @@
11
11
//! You will also need to choose a matrix type to use. DiffSol can use the [nalgebra](https://nalgebra.org) `DMatrix` type, or any other type that implements the
12
12
//! [Matrix] trait. You can also use the [sundials](https://computation.llnl.gov/projects/sundials) library for the matrix and vector types (see [SundialsMatrix]).
13
13
//!
14
-
//! To solve the problem, you need to choose a solver. DiffSol provides a pure rust [Bdf] solver, or you can use the [SundialsIda] solver from the sundials library (requires the `sundials` feature).
15
-
//! See the [OdeSolverMethod] trait for a more detailed description of the available methods on the solver.
14
+
//! To solve the problem, you need to choose a solver. DiffSol provides the following solvers:
15
+
//! - A pure rust Backwards Difference Formulae [Bdf] solver, suitable for stiff problems and singular mass matrices.
16
+
//! - The IDA solver solver from the sundials library ([SundialsIda], requires the `sundials` feature).
17
+
//!
18
+
//! See the [OdeSolverMethod] trait for a more detailed description of the available methods on each solver.
16
19
//!
17
20
//! ```rust
18
21
//! use diffsol::{OdeBuilder, Bdf, OdeSolverState, OdeSolverMethod};
@@ -45,6 +48,53 @@
45
48
//! }
46
49
//! let y = solver.interpolate(&state, t);
47
50
//! ```
51
+
//!
52
+
//! ## DiffSL
53
+
//!
54
+
//! DiffSL is a domain-specific language for specifying differential equations <https://github.com/martinjrobins/diffsl>. It uses the LLVM compiler framwork
55
+
//! to compile the equations to efficient machine code and uses the EnzymeAD library to compute the jacobian. You can use DiffSL with DiffSol by enabling one of the `diffsl-llvm*` features
56
+
//! corresponding to the version of LLVM you have installed, and using the [OdeBuilder::build_diffsl] method.
57
+
//!
58
+
//! For more information on the DiffSL language, see the [DiffSL documentation](https://martinjrobins.github.io/diffsl/)
59
+
//!
60
+
//! ## Custom ODE problems
61
+
//!
62
+
//! The [OdeBuilder] struct can be used to create an ODE problem from a set of closures.
63
+
//! If this is not suitable for your problem or you want more control over how your equations are implemented, you can also implement the [OdeEquations] trait manually.
64
+
//!
65
+
//! ## Nonlinear and linear solvers
66
+
//!
67
+
//! DiffSol provides generic nonlinear and linear solvers that are used internally by the ODE solver. You can use the solvers provided by DiffSol, or implement your own following the provided traits.
68
+
//! The linear solver trait is [LinearSolver], and the nonlinear solver trait is [NonLinearSolver]. The [SolverProblem] struct is used to define the problem to solve.
69
+
//!
70
+
//! The provided linear solvers are:
71
+
//! - [LU]: a direct solver that uses the LU decomposition implemented in the [nalgebra](https://nalgebra.org) library.
72
+
//! - [SundialsLinearSolver]: a linear solver that uses the [sundials](https://computation.llnl.gov/projects/sundials) library (requires the `sundials` feature).
73
+
//!
74
+
//! The provided nonlinear solvers are:
75
+
//! - [NewtonNonlinearSolver]: a nonlinear solver that uses the Newton method.
76
+
//!
77
+
//! ## Jacobian and Mass matrix calculation
78
+
//!
79
+
//! Via [OdeEquations], the user provides the action of the jacobian on a vector `J(x) v`. By default DiffSol uses this to generate a jacobian matrix for the ODE solver.
80
+
//! Generally this requires `n` evaluations of the jacobian action for a system of size `n`, so it is often more efficient if the user can provide the jacobian matrix directly
81
+
//! by implementing the [OdeEquations::jacobian_matrix] and the [OdeEquations::mass_matrix] (is applicable) functions.
82
+
//!
83
+
//! DiffSol also provides an experimental feature to calculate sparse jacobians more efficiently by automatically detecting the sparsity pattern of the jacobian and using
84
+
//! colouring \[1\] to reduce the number of jacobian evaluations. You can enable this feature by enabling [OdeBuilder::use_coloring()] option when building the ODE problem.
85
+
//!
86
+
//! \[1\] Gebremedhin, A. H., Manne, F., & Pothen, A. (2005). What color is your Jacobian? Graph coloring for computing derivatives. SIAM review, 47(4), 629-705.
87
+
//!
88
+
//! ## Matrix and vector types
89
+
//!
90
+
//! When solving ODEs, you will need to choose a matrix and vector type to use. DiffSol uses the following types:
91
+
//! - [nalgebra::DMatrix] and [nalgebra::DVector] from the [nalgebra](https://nalgebra.org) library.
92
+
//! - [SundialsMatrix] and [SundialsVector] from the [sundials](https://computation.llnl.gov/projects/sundials) library (requires the `sundials` feature).
93
+
//!
94
+
//! If you wish to use your own matrix and vector types, you will need to implement the following traits:
95
+
//! - For matrices: [Matrix], [MatrixView], [MatrixViewMut], [DenseMatrix], and [MatrixCommon].
96
+
//! - For vectors: [Vector], [VectorIndex], [VectorView], [VectorViewMut], and [VectorCommon].
97
+
//!
48
98
49
99
#[cfg(feature = "diffsl-llvm10")]
50
100
pubexterncrate diffsl10_0 as diffsl;
@@ -100,7 +150,7 @@ pub use linear_solver::sundials::SundialsLinearSolver;
100
150
#[cfg(feature = "sundials")]
101
151
pubuse ode_solver::sundials::SundialsIda;
102
152
103
-
use matrix::{DenseMatrix,Matrix,MatrixViewMut};
153
+
use matrix::{DenseMatrix,Matrix,MatrixCommon,MatrixView,MatrixViewMut};
/// returns the initial condition, i.e. $y(t_0, p)$
50
60
fninit(&self,t:Self::T) -> Self::V;
51
61
62
+
/// calculates the right-hand side function $F(t, y, p)$ where $y$ is given in `y`. The result is allocated and returned.
63
+
/// The default implementation calls [Self::rhs_inplace()] and allocates a new vector for the result.
52
64
fnrhs(&self,t:Self::T,y:&Self::V) -> Self::V{
53
65
letmut rhs_y = Self::V::zeros(self.nstates());
54
66
self.rhs_inplace(t, y,&mut rhs_y);
55
67
rhs_y
56
68
}
57
69
70
+
/// calculates $y = J(x)v$, where $J(x)$ is the Jacobian matrix of the right-hand side function $F(t, y, p)$ at $y = x$. The result is allocated and returned.
71
+
/// The default implementation calls [Self::rhs_jac_inplace()] and allocates a new vector for the result.
/// calculate and return the statistics of the ODE equation object (i.e. how many times the right-hand side function was evaluated, how many times the jacobian was multiplied, etc.)
124
+
/// The default implementation returns an empty statistics object.
/// This struct implements the ODE equation trait [OdeEquations] for a given right-hand side function, jacobian function, mass matrix function, and initial condition function.
131
+
/// These functions are provided as closures, and the parameters are assumed to be constant.
106
132
pubstructOdeSolverEquations<M,F,G,H,I>
107
133
where
108
134
M:Matrix,
@@ -179,7 +205,6 @@ where
179
205
}
180
206
}
181
207
182
-
// impl Op
183
208
impl<M,F,G,H,I>OpforOdeSolverEquations<M,F,G,H,I>
184
209
where
185
210
M:Matrix,
@@ -288,6 +313,9 @@ where
288
313
}
289
314
}
290
315
316
+
/// This struct implements the ODE equation trait [OdeEquations] for a given right-hand side function, jacobian function, and initial condition function.
317
+
/// These functions are provided as closures, and the parameters are assumed to be constant.
318
+
/// The mass matrix is assumed to be the identity matrix.
Copy file name to clipboardExpand all lines: src/solver/mod.rs
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ pub struct SolverStatistics {
10
10
pubnmaxiter:IndexType,
11
11
}
12
12
13
+
/// A generic linear or nonlinear solver problem, containing the function to solve $f(t, y)$, the current time $t$, and the relative and absolute tolerances.
13
14
pubstructSolverProblem<C:Op>{
14
15
pubf:Rc<C>,
15
16
pubt:C::T,
@@ -46,6 +47,8 @@ impl<C: Op> SolverProblem<C> {
46
47
}
47
48
48
49
impl<C:NonLinearOp>SolverProblem<C>{
50
+
/// Create a new solver problem from a nonlinear operator that solves for the linearised operator.
51
+
/// That is, if the original function is $f(t, y)$, this function creates a new problem $f'$ that solves $f' = J(x) v$, where $J(x)$ is the Jacobian of $f$ at $x$.
0 commit comments