differential_equations/methods/apc/
mod.rs

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