Skip to main content

nyx_space/od/process/solution/
smooth.rs

1/*
2    Nyx, blazing fast astrodynamics
3    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19use crate::linalg::allocator::Allocator;
20use crate::linalg::{DefaultAllocator, DimName, OMatrix};
21use crate::md::trajectory::Interpolatable;
22pub use crate::od::estimate::*;
23pub use crate::od::*;
24use anise::prelude::Almanac;
25use log::info;
26use msr::sensitivity::TrackerSensitivity;
27use std::ops::Add;
28
29use super::ODSolution;
30
31impl<StateType, EstType, MsrSize, Trk> ODSolution<StateType, EstType, MsrSize, Trk>
32where
33    StateType: Interpolatable + Add<OVector<f64, <StateType as State>::Size>, Output = StateType>,
34    EstType: Estimate<StateType>,
35    MsrSize: DimName,
36    Trk: TrackerSensitivity<StateType, StateType>,
37    <DefaultAllocator as Allocator<<StateType as State>::VecLength>>::Buffer<f64>: Send,
38    DefaultAllocator: Allocator<<StateType as State>::Size>
39        + Allocator<<StateType as State>::VecLength>
40        + Allocator<MsrSize>
41        + Allocator<MsrSize, <StateType as State>::Size>
42        + Allocator<MsrSize, MsrSize>
43        + Allocator<<StateType as State>::Size, <StateType as State>::Size>
44        + Allocator<<StateType as State>::Size, MsrSize>,
45{
46    /// Smoothes this OD solution, returning a new OD solution and the filter-smoother consistency ratios, with updated **postfit** residuals, and where the ratio now represents the filter-smoother consistency ratio.
47    ///
48    /// Notes:
49    ///  1. Gains will be scrubbed because the smoother process does not recompute the gain.
50    ///  2. Prefit residuals, ratios, and measurement covariances are not updated, as these depend on the filtering process.
51    ///  3. Note: this function consumes the current OD solution to prevent reusing the wrong one.
52    ///
53    ///
54    /// To assess whether the smoothing process improved the solution, compare the RMS of the postfit residuals from the filter and the smoother process.
55    ///
56    /// # Filter-Smoother consistency ratio
57    ///
58    /// The **filter-smoother consistency ratio** is used to evaluate the consistency between the state estimates produced by a filter (e.g., Kalman filter) and a smoother.
59    /// This ratio is called "filter smoother consistency test" in the ODTK MathSpec.
60    ///
61    /// It is computed as follows:
62    ///
63    /// #### 1. Define the State Estimates
64    /// **Filter state estimate**:
65    /// $ \hat{X}_{f,k} $
66    /// This is the state estimate at time step $ k $ from the filter.
67    ///
68    /// **Smoother state estimate**:
69    /// $ \hat{X}_{s,k} $
70    /// This is the state estimate at time step $ k $ from the smoother.
71    ///
72    /// #### 2. Define the Covariances
73    ///
74    /// **Filter covariance**:
75    /// $ P_{f,k} $
76    /// This is the covariance of the state estimate at time step $ k $ from the filter.
77    ///
78    /// **Smoother covariance**:
79    /// $ P_{s,k} $
80    /// This is the covariance of the state estimate at time step $ k $ from the smoother.
81    ///
82    /// #### 3. Compute the Differences
83    ///
84    /// **State difference**:
85    /// $ \Delta X_k = \hat{X}_{s,k} - \hat{X}_{f,k} $
86    ///
87    /// **Covariance difference**:
88    /// $ \Delta P_k = P_{s,k} - P_{f,k} $
89    ///
90    /// #### 4. Calculate the Consistency Ratio
91    /// For each element $ i $ of the state vector, compute the ratio:
92    ///
93    /// $$
94    /// R_{i,k} = \frac{\Delta X_{i,k}}{\sqrt{\Delta P_{i,k}}}
95    /// $$
96    ///
97    /// #### 5. Evaluate Consistency
98    /// - If $ |R_{i,k}| \leq 3 $ for all $ i $ and $ k $, the filter-smoother consistency test is satisfied, indicating good consistency.
99    /// - If $ |R_{i,k}| > 3 $ for any $ i $ or $ k $, the test fails, suggesting potential modeling inconsistencies or issues with the estimation process.
100    ///
101    pub fn smooth(self, almanac: Arc<Almanac>) -> Result<Self, ODError> {
102        let l = self.estimates.len() - 1;
103
104        let mut smoothed = Self {
105            estimates: Vec::with_capacity(self.estimates.len()),
106            residuals: Vec::with_capacity(self.residuals.len()),
107            gains: Vec::with_capacity(self.estimates.len()),
108            filter_smoother_ratios: Vec::with_capacity(self.estimates.len()),
109            devices: self.devices.clone(),
110            measurement_types: self.measurement_types.clone(),
111        };
112
113        // Set the first item of the smoothed estimates to the last estimate (we cannot smooth the very last estimate)
114        smoothed
115            .estimates
116            .push(self.estimates.last().unwrap().clone());
117        smoothed
118            .residuals
119            .push(self.residuals.last().unwrap().clone());
120        smoothed.gains.push(None);
121        smoothed.filter_smoother_ratios.push(None);
122
123        loop {
124            let k = l - smoothed.estimates.len();
125            // Borrow the previously smoothed estimate of the k+1 estimate
126            let sm_est_kp1 = &self.estimates[k + 1];
127            let x_kp1_l = sm_est_kp1.state_deviation();
128            let p_kp1_l = sm_est_kp1.covar();
129            // Borrow the k-th estimate, which we're smoothing with the next estimate
130            let est_k = &self.estimates[k];
131            // Borrow the k+1-th estimate, which we're smoothing with the next estimate
132            let est_kp1 = &self.estimates[k + 1];
133
134            // Compute the STM between both steps taken by the filter
135            // The filter will reset the STM between each estimate it computes, time update or measurement update.
136            // Therefore, the STM is simply the inverse of the one we used previously.
137            // est_kp1 is the estimate that used the STM from time k to time k+1. So the STM stored there
138            // is \Phi_{k \to k+1}. Let's invert that via a UDU decomposition for stability.
139            let phi_kp1_k = match est_kp1.stm().clone().udu() {
140                Some(stm_udu) => {
141                    // Invert both parts
142                    match stm_udu.u.try_inverse() {
143                        None => return Err(ODError::SingularStateTransitionMatrix),
144                        Some(u_inv) => {
145                            let d_inv = OMatrix::from_diagonal(&OVector::<
146                                f64,
147                                <StateType as State>::Size,
148                            >::from_iterator(
149                                stm_udu.d.iter().map(|d_ii| 1.0 / d_ii),
150                            ));
151                            let y = d_inv * &u_inv;
152                            u_inv.transpose() * y
153                        }
154                    }
155                }
156                None => return Err(ODError::SingularStateTransitionMatrix),
157            };
158
159            // Compute smoothed state deviation
160            let x_k_l = &phi_kp1_k * x_kp1_l;
161            // Compute smoothed covariance
162            let p_k_l = &phi_kp1_k * p_kp1_l * &phi_kp1_k.transpose();
163            // Store into vector
164            let mut smoothed_est_k = est_k.clone();
165            // Compute the smoothed state deviation
166            smoothed_est_k.set_state_deviation(x_k_l);
167            // Compute the smoothed covariance
168            smoothed_est_k.set_covar(p_k_l);
169            // Recompute the residual if available.
170            if let Some(mut residual) = self.residuals[k + 1].clone() {
171                let tracker = residual
172                    .tracker
173                    .as_ref()
174                    .expect("tracker unset in smoother process");
175
176                let device = smoothed
177                    .devices
178                    .get_mut(tracker)
179                    .expect("unknown tracker in smoother process");
180
181                let new_state_est = smoothed_est_k.state();
182                let epoch = new_state_est.epoch();
183
184                if let Some(computed_meas) =
185                    device.measure_instantaneous(new_state_est, None, almanac.clone())?
186                {
187                    // Only recompute the computed observation from the update state estimate.
188                    residual.computed_obs = computed_meas
189                        .observation::<MsrSize>(&residual.msr_types)
190                        - device.measurement_bias_vector::<MsrSize>(&residual.msr_types, epoch)?;
191
192                    // Update the postfit residual.
193                    residual.postfit = &residual.real_obs - &residual.computed_obs;
194
195                    // Store the updated data.
196                    smoothed.residuals.push(Some(residual));
197                } else {
198                    smoothed.residuals.push(None);
199                }
200            } else {
201                smoothed.residuals.push(None);
202            }
203
204            // Compute the filter-smoother consistency ratio.
205            let delta_covar = est_k.covar() - smoothed_est_k.covar();
206            let delta_state =
207                est_k.state().to_state_vector() - smoothed_est_k.state().to_state_vector();
208
209            let fs_ratios = OVector::<f64, <StateType as State>::Size>::from_iterator(
210                delta_state
211                    .iter()
212                    .enumerate()
213                    .map(|(i, dx)| dx / delta_covar[(i, i)].sqrt()),
214            );
215
216            smoothed.estimates.push(smoothed_est_k);
217            smoothed.filter_smoother_ratios.push(Some(fs_ratios));
218            // Set all gains to None.
219            smoothed.gains.push(None);
220
221            if smoothed.estimates.len() == self.estimates.len() {
222                break;
223            }
224        }
225
226        // Note that we have yet to reverse the list, so we print them backward
227        info!(
228            "Smoothed {} estimates (from {} to {})",
229            smoothed.estimates.len(),
230            smoothed.estimates.last().unwrap().epoch(),
231            smoothed.estimates[0].epoch(),
232        );
233
234        // Now, let's add all of the other estimates so that the same indexing can be done
235        // between all the estimates and the smoothed estimates
236        if smoothed.estimates.len() < self.estimates.len() {
237            // Add the estimates that might have been skipped.
238            let mut k = self.estimates.len() - smoothed.estimates.len();
239            loop {
240                smoothed.estimates.push(self.estimates[k].clone());
241                if k == 0 {
242                    break;
243                }
244                k -= 1;
245            }
246        }
247
248        // And reverse to maintain the order of estimates
249        smoothed.estimates.reverse();
250        smoothed.residuals.reverse();
251        smoothed.filter_smoother_ratios.reverse();
252
253        Ok(smoothed)
254    }
255}