nyx_space/propagators/rk_methods/
mod.rs

1/*
2    Nyx, blazing fast astrodynamics
3    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19mod rk;
20use std::str::FromStr;
21
22use serde::Deserialize;
23use serde::Serialize;
24
25use crate::io::ConfigError;
26
27use self::rk::*;
28mod dormand;
29use self::dormand::*;
30mod verner;
31use self::verner::*;
32
33use super::PropagationError;
34
35/// The `RK` trait defines a Runge Kutta integrator.
36#[allow(clippy::upper_case_acronyms)]
37trait RK
38where
39    Self: Sized,
40{
41    /// Returns the order of this integrator (as u8 because there probably isn't an order greater than 255).
42    /// The order is used for the adaptive step size only to compute the error between estimates.
43    const ORDER: u8;
44
45    /// Returns the stages of this integrator (as usize because it's used as indexing)
46    const STAGES: usize;
47
48    /// Returns a pointer to a list of f64 corresponding to the A coefficients of the Butcher table for that RK.
49    /// This module only supports *implicit* integrators, and as such, `Self.a_coeffs().len()` must be of
50    /// size (order+1)*(order)/2.
51    /// *Warning:* this RK trait supposes that the implementation is consistent, i.e. c_i = \sum_j a_{ij}.
52    const A_COEFFS: &'static [f64];
53    /// Returns a pointer to a list of f64 corresponding to the b_i and b^*_i coefficients of the
54    /// Butcher table for that RK. `Self.a_coeffs().len()` must be of size (order+1)*2.
55    const B_COEFFS: &'static [f64];
56}
57
58/// Enum of supported integration methods, all of which are part of the Runge Kutta family of ordinary differential equation (ODE) solvers.
59/// Nomenclature: X-Y means that this is an X order solver with a Y order error correction step.
60#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
61pub enum IntegratorMethod {
62    /// Runge Kutta 8-9 is the recommended integrator for most application.
63    #[default]
64    RungeKutta89,
65    /// `Dormand78` is a [Dormand-Prince integrator](https://en.wikipedia.org/wiki/Dormand%E2%80%93Prince_method). Coefficients taken from GMAT `src/base/propagator/PrinceDormand78.cpp`.
66    DormandPrince78,
67    /// `Dormand45` is a [Dormand-Prince integrator](https://en.wikipedia.org/wiki/Dormand%E2%80%93Prince_method).
68    DormandPrince45,
69    /// Runge Kutta 4 is a fixed step solver.
70    RungeKutta4,
71    /// Runge Kutta 4-5 [Cash Karp integrator](https://en.wikipedia.org/wiki/Cash%E2%80%93Karp_method).
72    CashKarp45,
73    /// Verner56 is an RK Verner integrator of order 5-6. Coefficients taken from [here (PDF)](http://people.math.sfu.ca/~jverner/classify.1992.ps).
74    Verner56,
75}
76
77impl IntegratorMethod {
78    /// Returns the order of this integrator (as u8 because there probably isn't an order greater than 255).
79    /// The order is used for the adaptive step size only to compute the error between estimates.
80    pub const fn order(self) -> u8 {
81        match self {
82            Self::RungeKutta89 => RK89::ORDER,
83            Self::DormandPrince78 => Dormand78::ORDER,
84            Self::DormandPrince45 => Dormand45::ORDER,
85            Self::RungeKutta4 => RK4Fixed::ORDER,
86            Self::CashKarp45 => CashKarp45::ORDER,
87            Self::Verner56 => Verner56::ORDER,
88        }
89    }
90
91    /// Returns the stages of this integrator, i.e. how many times the derivatives will be called
92    pub const fn stages(self) -> usize {
93        match self {
94            Self::RungeKutta89 => RK89::STAGES,
95            Self::DormandPrince78 => Dormand78::STAGES,
96            Self::DormandPrince45 => Dormand45::STAGES,
97            Self::RungeKutta4 => RK4Fixed::STAGES,
98            Self::CashKarp45 => CashKarp45::STAGES,
99            Self::Verner56 => Verner56::STAGES,
100        }
101    }
102
103    /// Returns a pointer to a list of f64 corresponding to the A coefficients of the Butcher table for that RK.
104    /// This module only supports *implicit* integrators, and as such, `Self.a_coeffs().len()` must be of
105    /// size (order+1)*(order)/2.
106    /// *Warning:* this RK trait supposes that the implementation is consistent, i.e. c_i = \sum_j a_{ij}.
107    pub const fn a_coeffs(self) -> &'static [f64] {
108        match self {
109            Self::RungeKutta89 => RK89::A_COEFFS,
110            Self::DormandPrince78 => Dormand78::A_COEFFS,
111            Self::DormandPrince45 => Dormand45::A_COEFFS,
112            Self::RungeKutta4 => RK4Fixed::A_COEFFS,
113            Self::CashKarp45 => CashKarp45::A_COEFFS,
114            Self::Verner56 => Verner56::A_COEFFS,
115        }
116    }
117    /// Returns a pointer to a list of f64 corresponding to the b_i and b^*_i coefficients of the
118    /// Butcher table for that RK. `Self.a_coeffs().len()` must be of size (order+1)*2.
119    pub const fn b_coeffs(self) -> &'static [f64] {
120        match self {
121            Self::RungeKutta89 => RK89::B_COEFFS,
122            Self::DormandPrince78 => Dormand78::B_COEFFS,
123            Self::DormandPrince45 => Dormand45::B_COEFFS,
124            Self::RungeKutta4 => RK4Fixed::B_COEFFS,
125            Self::CashKarp45 => CashKarp45::B_COEFFS,
126            Self::Verner56 => Verner56::B_COEFFS,
127        }
128    }
129}
130
131impl FromStr for IntegratorMethod {
132    type Err = PropagationError;
133
134    fn from_str(s: &str) -> Result<Self, Self::Err> {
135        match s.to_lowercase().as_str() {
136            "rungekutta89" => Ok(Self::RungeKutta89),
137            "dormandprince78" => Ok(Self::DormandPrince78),
138            "dormandprince45" => Ok(Self::DormandPrince45),
139            "rungekutta4" => Ok(Self::RungeKutta4),
140            "cashkarp45" => Ok(Self::CashKarp45),
141            "verner56" => Ok(Self::Verner56),
142            _ => {
143                let valid = [
144                    "RungeKutta89",
145                    "DormandPrince78",
146                    "DormandPrince45",
147                    "RungeKutta4",
148                    "CashKarp45",
149                    "Verner56",
150                ];
151                let valid_msg = valid.join(",");
152                Err(PropagationError::PropConfigError {
153                    source: ConfigError::InvalidConfig {
154                        msg: format!("unknow integration method `{s}`, must be one of {valid_msg}"),
155                    },
156                })
157            }
158        }
159    }
160}
161
162#[cfg(test)]
163mod ut_propagator {
164    use std::str::FromStr;
165
166    use super::IntegratorMethod;
167
168    #[test]
169    fn from_str_ok() {
170        let valid = [
171            "RungeKutta89",
172            "DormandPrince78",
173            "DormandPrince45",
174            "RungeKutta4",
175            "CashKarp45",
176            "Verner56",
177        ];
178        for method in valid {
179            assert!(IntegratorMethod::from_str(method.to_uppercase().as_str()).is_ok());
180        }
181        assert!(IntegratorMethod::from_str("blah").is_err());
182    }
183}