differential_equations/methods/apc/
mod.rs

1//! Adams Predictor Correction (APC) methods
2
3mod apcf4;
4mod apcv4;
5
6use std::marker::PhantomData;
7
8use crate::{
9    status::Status,
10    traits::{CallBackData, Real, State},
11};
12
13pub struct AdamsPredictorCorrector<E, F, T: Real, Y: State<T>, D: CallBackData, const S: usize> {
14    // Initial Step Size
15    pub h0: T,
16
17    // Current Step Size
18    h: T,
19
20    // Current State
21    t: T,
22    y: Y,
23    dydt: Y,
24
25    // Final Time
26    tf: T,
27
28    // Previous States
29    t_prev: [T; S],
30    y_prev: [Y; S],
31
32    // Previous step states
33    t_old: T,
34    y_old: Y,
35    dydt_old: Y,
36
37    // Predictor Correct Derivatives
38    k: [Y; S],
39
40    // Statistic Tracking
41    evals: usize,
42    steps: usize,
43
44    // Status
45    status: Status<T, Y, D>,
46
47    // Settings
48    pub tol: T,
49    pub h_max: T,
50    pub h_min: T,
51    pub max_steps: usize,
52
53    // Method info
54    pub stages: usize,
55
56    // Family classification
57    family: PhantomData<F>,
58
59    // Equation type
60    equation: PhantomData<E>,
61}
62
63impl<E, F, T: Real, Y: State<T>, D: CallBackData, const S: usize> Default
64    for AdamsPredictorCorrector<E, F, T, Y, D, S>
65{
66    fn default() -> Self {
67        Self {
68            h0: T::zero(),
69            h: T::zero(),
70            t: T::zero(),
71            y: Y::zeros(),
72            dydt: Y::zeros(),
73            t_prev: [T::zero(); S],
74            y_prev: [Y::zeros(); S],
75            t_old: T::zero(),
76            y_old: Y::zeros(),
77            dydt_old: Y::zeros(),
78            k: [Y::zeros(); S],
79            tf: T::zero(),
80            evals: 0,
81            steps: 0,
82            status: Status::Uninitialized,
83            tol: T::from_f64(1.0e-6).unwrap(),
84            h_max: T::infinity(),
85            h_min: T::zero(),
86            max_steps: 10_000,
87            stages: S,
88            family: PhantomData,
89            equation: PhantomData,
90        }
91    }
92}
93
94impl<E, F, T: Real, Y: State<T>, D: CallBackData, const S: usize>
95    AdamsPredictorCorrector<E, F, T, Y, D, S>
96{
97    /// Set the tolerance for error control
98    pub fn tol(mut self, rtol: T) -> Self {
99        self.tol = rtol;
100        self
101    }
102
103    /// Set the initial step size
104    pub fn h0(mut self, h0: T) -> Self {
105        self.h0 = h0;
106        self
107    }
108
109    /// Set the minimum allowed step size
110    pub fn h_min(mut self, h_min: T) -> Self {
111        self.h_min = h_min;
112        self
113    }
114
115    /// Set the maximum allowed step size
116    pub fn h_max(mut self, h_max: T) -> Self {
117        self.h_max = h_max;
118        self
119    }
120
121    /// Set the maximum number of steps allowed
122    pub fn max_steps(mut self, max_steps: usize) -> Self {
123        self.max_steps = max_steps;
124        self
125    }
126
127    /// Get the number of stages in the method
128    pub fn stages(&self) -> usize {
129        self.stages
130    }
131}