Skip to main content

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    pub filter: fn(T) -> T,
53
54    // Method info
55    pub stages: usize,
56
57    // Family classification
58    family: PhantomData<F>,
59
60    // Equation type
61    equation: PhantomData<E>,
62}
63
64impl<E, F, T: Real, Y: State<T>, const S: usize> Default
65    for AdamsPredictorCorrector<E, F, T, Y, S>
66{
67    fn default() -> Self {
68        Self {
69            h0: T::zero(),
70            h: T::zero(),
71            t: T::zero(),
72            y: Y::zeros(),
73            dydt: Y::zeros(),
74            t_prev: [T::zero(); S],
75            y_prev: core::array::from_fn(|_| Y::zeros()),
76            t_old: T::zero(),
77            y_old: Y::zeros(),
78            dydt_old: Y::zeros(),
79            k: core::array::from_fn(|_| Y::zeros()),
80            tf: T::zero(),
81            evals: 0,
82            steps: 0,
83            status: Status::Uninitialized,
84            tol: T::from_f64(1.0e-6).unwrap(),
85            h_max: T::infinity(),
86            h_min: T::zero(),
87            max_steps: 10_000,
88            filter: |h| h,
89            stages: S,
90            family: PhantomData,
91            equation: PhantomData,
92        }
93    }
94}
95
96impl<E, F, T: Real, Y: State<T>, const S: usize> AdamsPredictorCorrector<E, F, T, Y, S> {
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    /// Set the step size filter (default: identity)
128    pub fn filter(mut self, filter: fn(T) -> T) -> Self {
129        self.filter = filter;
130        self
131    }
132
133    /// Get the number of stages in the method
134    pub fn stages(&self) -> usize {
135        self.stages
136    }
137}