differential_equations/solout/
dense.rs

1//! Dense Solout Implementation, for outputting a dense set of points.
2//!
3//! This module provides an output strategy that generates additional interpolated
4//! points between each solver step, creating a denser output representation.
5
6use super::*;
7
8/// An output handler that provides a dense set of interpolated points between solver steps.
9///
10/// # Overview
11///
12/// `DenseSolout` enhances the solution output by interpolating additional points
13/// between the naturally computed solver steps. This creates a smoother, more
14/// detailed trajectory that can better represent the continuous solution,
15/// especially when the solver takes large steps.
16///
17/// # Example
18///
19/// ```
20/// use differential_equations::prelude::*;
21/// use differential_equations::solout::DenseSolout;
22/// use nalgebra::{Vector2, vector};
23///
24/// // Simple harmonic oscillator
25/// struct HarmonicOscillator;
26///
27/// impl ODE<f64, Vector2<f64>> for HarmonicOscillator {
28///     fn diff(&self, _t: f64, y: &Vector2<f64>, dydt: &mut Vector2<f64>) {
29///         // y[0] = position, y[1] = velocity
30///         dydt[0] = y[1];
31///         dydt[1] = -y[0];
32///     }
33/// }
34///
35/// // Create the system and solver
36/// let system = HarmonicOscillator;
37/// let t0 = 0.0;
38/// let tf = 10.0;
39/// let y0 = vector![1.0, 0.0];
40/// let mut solver = ExplicitRungeKutta::dop853().rtol(1e-6).atol(1e-8);
41///
42/// // Generate 9 additional points between each solver step (10 total per interval)
43/// let mut dense_output = DenseSolout::new(10);
44///
45/// // Solve with dense output
46/// let problem = ODEProblem::new(system, t0, tf, y0);
47/// let solution = problem.solout(&mut dense_output).solve(&mut solver).unwrap();
48///
49/// // Note: This is equivalent to using the convenience method:
50/// let solution = problem.dense(10).solve(&mut solver).unwrap();
51/// ```
52///
53/// # Output Characteristics
54///
55/// The output will contain both the original solver steps and additional interpolated
56/// points between them. The interpolated points are evenly spaced within each step.
57///
58/// For example, with n=5:
59/// - Original solver steps: t₀, t₁, t₂, ...
60/// - Dense output: t₀, t₀+h/5, t₀+2h/5, t₀+3h/5, t₀+4h/5, t₁, t₁+h/5, ...
61///
62/// # Performance Considerations
63///
64/// Increasing the number of interpolation points increases computational cost and
65/// memory usage. Choose a value that balances the need for smooth output with
66/// performance requirements.
67///
68pub struct DenseSolout {
69    /// Number of points between steps (including the endpoints)
70    n: usize,
71}
72
73impl<T, Y> Solout<T, Y> for DenseSolout
74where
75    T: Real,
76    Y: State<T>,
77{
78    fn solout<I>(
79        &mut self,
80        t_curr: T,
81        t_prev: T,
82        y_curr: &Y,
83        _y_prev: &Y,
84        interpolator: &mut I,
85        solution: &mut Solution<T, Y>,
86    ) -> ControlFlag<T, Y>
87    where
88        I: Interpolation<T, Y>,
89    {
90        // Interpolate between steps
91        if t_prev != t_curr {
92            for i in 1..self.n {
93                let h_old = t_curr - t_prev;
94                let ti =
95                    t_prev + T::from_usize(i).unwrap() * h_old / T::from_usize(self.n).unwrap();
96                let yi = interpolator.interpolate(ti).unwrap();
97                solution.push(ti, yi);
98            }
99        }
100
101        // Save actual calculated step as well
102        solution.push(t_curr, *y_curr);
103
104        // Continue the integration
105        ControlFlag::Continue
106    }
107}
108
109impl DenseSolout {
110    /// Creates a new DenseSolout instance with the specified number of points per interval.
111    ///
112    /// # Arguments
113    /// * `n` - Number of points per interval, including endpoints. For example, n=5 will
114    ///         add 4 interpolated points between each solver step, plus the solver step itself.
115    ///
116    /// # Returns
117    /// * A new `DenseSolout` instance
118    ///
119    pub fn new(n: usize) -> Self {
120        DenseSolout { n }
121    }
122}