russell_ode/
workspace.rs

1use crate::{Method, Stats};
2
3/// Holds workspace data shared among the ODE solver and actual implementations
4pub(crate) struct Workspace {
5    /// Holds statistics and benchmarking data
6    pub(crate) stats: Stats,
7
8    /// Indicates that the step follows a reject
9    pub(crate) follows_reject_step: bool,
10
11    /// Indicates that the iterations (in an implicit method) are diverging
12    pub(crate) iterations_diverging: bool,
13
14    /// Holds a multiplier to the stepsize when the iterations are diverging
15    pub(crate) h_multiplier_diverging: f64,
16
17    /// Holds the previous stepsize
18    pub(crate) h_prev: f64,
19
20    /// Holds the next stepsize estimate
21    pub(crate) h_new: f64,
22
23    /// Holds the previous relative error
24    pub(crate) rel_error_prev: f64,
25
26    /// Holds the current relative error
27    pub(crate) rel_error: f64,
28
29    /// Holds x at the moment of the first stiffness detection (when h·ρ > max(h·ρ))
30    pub(crate) stiff_x_first_detect: f64,
31
32    /// Holds the h·ρ value to detect stiffness
33    pub(crate) stiff_h_times_rho: f64,
34
35    /// Holds the number of negative stiffness detections
36    pub(crate) stiff_n_detection_no: usize,
37
38    /// Holds the number of positive stiffness detections
39    pub(crate) stiff_n_detection_yes: usize,
40
41    /// Indicates whether stiffness has been detected or not (after some "yes" steps have been found)
42    pub(crate) stiff_detected: bool,
43}
44
45impl Workspace {
46    /// Allocates a new instance
47    pub(crate) fn new(method: Method) -> Self {
48        Workspace {
49            stats: Stats::new(method),
50            follows_reject_step: false,
51            iterations_diverging: false,
52            h_multiplier_diverging: 1.0,
53            h_prev: 0.0,
54            h_new: 0.0,
55            rel_error_prev: 0.0,
56            rel_error: 0.0,
57            stiff_x_first_detect: f64::MAX,
58            stiff_h_times_rho: 0.0,
59            stiff_n_detection_no: 0,
60            stiff_n_detection_yes: 0,
61            stiff_detected: false,
62        }
63    }
64
65    /// Resets all values
66    pub(crate) fn reset(&mut self, h: f64, rel_error_prev_min: f64) {
67        self.stats.reset(h);
68        self.follows_reject_step = false;
69        self.iterations_diverging = false;
70        self.h_multiplier_diverging = 1.0;
71        self.h_prev = h;
72        self.h_new = h;
73        self.rel_error_prev = rel_error_prev_min;
74        self.rel_error = 0.0;
75        self.stiff_x_first_detect = f64::MAX;
76        self.stiff_h_times_rho = 0.0;
77        self.stiff_n_detection_no = 0;
78        self.stiff_n_detection_yes = 0;
79        self.stiff_detected = false;
80    }
81}