|
7 | 7 | //! - `ODEProblem`: Trait for defining an ODE problem. |
8 | 8 | //! - `ODEIntegrator`: Trait for ODE integrators. |
9 | 9 | //! - `ODESolver`: Trait for ODE solvers. |
| 10 | +//! - `ODEError`: Enum for ODE errors. |
| 11 | +//! - `ReachedMaxStepIter`: Reached maximum number of steps per step. (internal error) |
| 12 | +//! - `ConstraintViolation(f64, Vec<f64>, Vec<f64>)`: Constraint violation. (user-defined error) |
10 | 13 | //! |
11 | 14 | //! ## Available integrators |
12 | 15 | //! |
@@ -111,10 +114,39 @@ pub trait ODEIntegrator { |
111 | 114 |
|
112 | 115 |
|
113 | 116 | /// Enum for ODE errors. |
114 | | -#[derive(Debug, Clone, Copy, Error)] |
| 117 | +/// |
| 118 | +/// # Variants |
| 119 | +/// |
| 120 | +/// - `ReachedMaxStepIter`: Reached maximum number of steps per step. (internal error for integrator) |
| 121 | +/// - `ConstraintViolation`: Constraint violation. (user-defined error) |
| 122 | +/// |
| 123 | +/// If you define constraints in your problem, you can use this error to report constraint violations. |
| 124 | +/// |
| 125 | +/// # Example |
| 126 | +/// |
| 127 | +/// ```no_run |
| 128 | +/// use peroxide::fuga::*; |
| 129 | +/// |
| 130 | +/// struct ConstrainedProblem { |
| 131 | +/// y_constraint: f64 |
| 132 | +/// } |
| 133 | +/// |
| 134 | +/// impl ODEProblem for ConstrainedProblem { |
| 135 | +/// fn initial_conditions(&self) -> Vec<f64> { vec![0.0] } // y_0 = 0 |
| 136 | +/// fn rhs(&self, t: f64, y: &[f64], dy: &mut [f64]) -> Result<(), ODEError> { |
| 137 | +/// if y[0] < self.y_constraint { |
| 138 | +/// return Err(ODEError::ConstraintViolation(t, y.to_vec(), dy.to_vec())); |
| 139 | +/// } else { |
| 140 | +/// // some function |
| 141 | +/// Ok(()) |
| 142 | +/// } |
| 143 | +/// } |
| 144 | +/// } |
| 145 | +/// ``` |
| 146 | +#[derive(Debug, Clone, Error)] |
115 | 147 | pub enum ODEError { |
116 | 148 | #[error("constraint violation")] |
117 | | - ConstraintViolation, |
| 149 | + ConstraintViolation(f64, Vec<f64>, Vec<f64>), // t, y, dy |
118 | 150 | #[error("reached maximum number of iterations per step")] |
119 | 151 | ReachedMaxStepIter, |
120 | 152 | } |
@@ -154,7 +186,8 @@ pub trait ODESolver { |
154 | 186 | /// } |
155 | 187 | /// |
156 | 188 | /// fn rhs(&self, t: f64, y: &[f64], dy: &mut [f64]) -> Result<(), ODEError> { |
157 | | -/// Ok(dy[0] = (5f64 * t.powi(2) - y[0]) / (t + y[0]).exp()) |
| 189 | +/// dy[0] = (5f64 * t.powi(2) - y[0]) / (t + y[0]).exp(); |
| 190 | +/// Ok(()) |
158 | 191 | /// } |
159 | 192 | /// } |
160 | 193 | /// ``` |
|
0 commit comments