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::{Real, State},
11};
12
13pub struct AdamsPredictorCorrector<E, F, T: Real, Y: State<T>, 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>,
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>, const S: usize> Default
64    for AdamsPredictorCorrector<E, F, T, Y, 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>, const S: usize> AdamsPredictorCorrector<E, F, T, Y, S> {
95    /// Set the tolerance for error control
96    pub fn tol(mut self, rtol: T) -> Self {
97        self.tol = rtol;
98        self
99    }
100
101    /// Set the initial step size
102    pub fn h0(mut self, h0: T) -> Self {
103        self.h0 = h0;
104        self
105    }
106
107    /// Set the minimum allowed step size
108    pub fn h_min(mut self, h_min: T) -> Self {
109        self.h_min = h_min;
110        self
111    }
112
113    /// Set the maximum allowed step size
114    pub fn h_max(mut self, h_max: T) -> Self {
115        self.h_max = h_max;
116        self
117    }
118
119    /// Set the maximum number of steps allowed
120    pub fn max_steps(mut self, max_steps: usize) -> Self {
121        self.max_steps = max_steps;
122        self
123    }
124
125    /// Get the number of stages in the method
126    pub fn stages(&self) -> usize {
127        self.stages
128    }
129}