differential_equations/methods/erk/
mod.rs1mod adaptive;
4mod dormandprince;
5mod fixed;
6
7use std::{collections::VecDeque, marker::PhantomData};
8
9use crate::{
10 methods::Delay,
11 status::Status,
12 traits::{CallBackData, Real, State},
13};
14
15pub struct ExplicitRungeKutta<
31 E,
32 F,
33 T: Real,
34 Y: State<T>,
35 D: CallBackData,
36 const O: usize,
37 const S: usize,
38 const I: usize,
39> {
40 t0: T,
42
43 pub h0: T,
45
46 h: T,
48
49 t: T,
51 y: Y,
52 dydt: Y,
53
54 h_prev: T,
56 t_prev: T,
57 y_prev: Y,
58 dydt_prev: Y,
59
60 k: [Y; I],
62
63 c: [T; I],
65 a: [[T; I]; I],
66 b: [T; S],
67 bh: Option<[T; S]>, er: Option<[T; S]>, bi: Option<[[T; I]; I]>, cont: [Y; O],
73
74 pub rtol: T,
76 pub atol: T,
77 pub h_max: T,
78 pub h_min: T,
79 pub max_steps: usize,
80 pub max_rejects: usize,
81 pub safety_factor: T,
82 pub min_scale: T,
83 pub max_scale: T,
84
85 stiffness_counter: usize,
87 non_stiffness_counter: usize,
88 steps: usize,
89
90 status: Status<T, Y, D>,
92
93 order: usize,
95 stages: usize,
96 dense_stages: usize,
97 fsal: bool, family: PhantomData<F>,
101
102 equation: PhantomData<E>,
104
105 history: VecDeque<(T, Y, Y)>, max_delay: Option<T>, }
109
110impl<E, F, T: Real, Y: State<T>, D: CallBackData, const O: usize, const S: usize, const I: usize>
111 Default for ExplicitRungeKutta<E, F, T, Y, D, O, S, I>
112{
113 fn default() -> Self {
114 Self {
115 t0: T::zero(),
116 h0: T::zero(),
117 h: T::zero(),
118 t: T::zero(),
119 y: Y::zeros(),
120 dydt: Y::zeros(),
121 h_prev: T::zero(),
122 t_prev: T::zero(),
123 y_prev: Y::zeros(),
124 dydt_prev: Y::zeros(),
125 k: [Y::zeros(); I],
126 c: [T::zero(); I],
127 a: [[T::zero(); I]; I],
128 b: [T::zero(); S],
129 bh: None,
130 er: None,
131 bi: None,
132 cont: [Y::zeros(); O],
133 rtol: T::from_f64(1.0e-6).unwrap(),
134 atol: T::from_f64(1.0e-6).unwrap(),
135 h_max: T::infinity(),
136 h_min: T::zero(),
137 max_steps: 10_000,
138 max_rejects: 100,
139 safety_factor: T::from_f64(0.9).unwrap(),
140 min_scale: T::from_f64(0.2).unwrap(),
141 max_scale: T::from_f64(10.0).unwrap(),
142 stiffness_counter: 0,
143 non_stiffness_counter: 0,
144 steps: 0,
145 status: Status::Uninitialized,
146 order: O,
147 stages: S,
148 dense_stages: I,
149 fsal: false,
150 family: PhantomData,
151 equation: PhantomData,
152 history: VecDeque::new(),
153 max_delay: None,
154 }
155 }
156}
157
158impl<E, F, T: Real, Y: State<T>, D: CallBackData, const O: usize, const S: usize, const I: usize>
159 ExplicitRungeKutta<E, F, T, Y, D, O, S, I>
160{
161 pub fn rtol(mut self, rtol: T) -> Self {
163 self.rtol = rtol;
164 self
165 }
166
167 pub fn atol(mut self, atol: T) -> Self {
169 self.atol = atol;
170 self
171 }
172
173 pub fn h0(mut self, h0: T) -> Self {
175 self.h0 = h0;
176 self
177 }
178
179 pub fn h_min(mut self, h_min: T) -> Self {
181 self.h_min = h_min;
182 self
183 }
184
185 pub fn h_max(mut self, h_max: T) -> Self {
187 self.h_max = h_max;
188 self
189 }
190
191 pub fn max_steps(mut self, max_steps: usize) -> Self {
193 self.max_steps = max_steps;
194 self
195 }
196
197 pub fn max_rejects(mut self, max_rejects: usize) -> Self {
199 self.max_rejects = max_rejects;
200 self
201 }
202
203 pub fn safety_factor(mut self, safety_factor: T) -> Self {
205 self.safety_factor = safety_factor;
206 self
207 }
208
209 pub fn min_scale(mut self, min_scale: T) -> Self {
211 self.min_scale = min_scale;
212 self
213 }
214
215 pub fn max_scale(mut self, max_scale: T) -> Self {
217 self.max_scale = max_scale;
218 self
219 }
220
221 pub fn order(&self) -> usize {
223 self.order
224 }
225
226 pub fn stages(&self) -> usize {
228 self.stages
229 }
230
231 pub fn dense_stages(&self) -> usize {
233 self.dense_stages
234 }
235}
236
237impl<F, T: Real, Y: State<T>, D: CallBackData, const O: usize, const S: usize, const I: usize>
238 ExplicitRungeKutta<Delay, F, T, Y, D, O, S, I>
239{
240 pub fn max_delay(mut self, max_delay: T) -> Self {
242 self.max_delay = Some(max_delay);
243 self
244 }
245}