nyx_space/od/process/
mod.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};
21use crate::md::trajectory::{Interpolatable, Traj};
22pub use crate::od::estimate::*;
23pub use crate::od::ground_station::*;
24pub use crate::od::snc::*;
25pub use crate::od::*;
26use crate::propagators::Propagator;
27pub use crate::time::{Duration, Unit};
28use anise::prelude::Almanac;
29use indexmap::IndexSet;
30use msr::sensitivity::TrackerSensitivity;
31use snafu::prelude::*;
32use solution::kalman::KalmanVariant;
33use std::collections::BTreeMap;
34use std::marker::PhantomData;
35use std::ops::Add;
36use typed_builder::TypedBuilder;
37
38mod rejectcrit;
39use self::kalman::KalmanFilter;
40use self::msr::TrackingDataArc;
41pub use self::rejectcrit::ResidRejectCrit;
42mod solution;
43pub use solution::ODSolution;
44mod initializers;
45
46/// An orbit determination process (ODP) which filters OD measurements through a Kalman filter.
47#[derive(Clone, TypedBuilder)]
48#[builder(doc)]
49#[allow(clippy::upper_case_acronyms)]
50pub struct KalmanODProcess<
51    D: Dynamics,
52    MsrSize: DimName,
53    Accel: DimName,
54    Trk: TrackerSensitivity<D::StateType, D::StateType>,
55> where
56    D::StateType:
57        Interpolatable + Add<OVector<f64, <D::StateType as State>::Size>, Output = D::StateType>,
58    <DefaultAllocator as Allocator<<D::StateType as State>::VecLength>>::Buffer<f64>: Send,
59    <DefaultAllocator as Allocator<<D::StateType as State>::Size>>::Buffer<f64>: Copy,
60    <DefaultAllocator as Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>>::Buffer<f64>: Copy,
61    DefaultAllocator: Allocator<<D::StateType as State>::Size>
62        + Allocator<<D::StateType as State>::VecLength>
63        + Allocator<MsrSize>
64        + Allocator<MsrSize, <D::StateType as State>::Size>
65        + Allocator<<D::StateType as State>::Size, MsrSize>
66        + Allocator<MsrSize, MsrSize>
67        + Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
68        + Allocator<Accel>
69        + Allocator<Accel, Accel>
70        + Allocator<<D::StateType as State>::Size, Accel>
71        + Allocator<Accel, <D::StateType as State>::Size>,
72{
73    /// Propagator used for the estimation
74    pub prop: Propagator<D>,
75    /// Kalman filter variant
76    #[builder(default)]
77    pub kf_variant: KalmanVariant,
78    /// Residual rejection criteria allows preventing bad measurements from affecting the estimation.
79    #[builder(default, setter(strip_option))]
80    pub resid_crit: Option<ResidRejectCrit>,
81    /// Tracking devices
82    #[builder(default_code = "BTreeMap::new()")]
83    pub devices: BTreeMap<String, Trk>,
84    /// A sets of process noise (usually noted Q), must be ordered chronologically
85    #[builder(default_code = "vec![]")]
86    pub process_noise: Vec<ProcessNoise<Accel>>,
87    /// Maximum step size where the STM linearization is assumed correct (1 minute is usually fine)
88    #[builder(default_code = "1 * Unit::Minute")]
89    pub max_step: Duration,
90    /// Precision of the measurement epoch when processing measurements.
91    #[builder(default_code = "1 * Unit::Microsecond")]
92    pub epoch_precision: Duration,
93    pub almanac: Arc<Almanac>,
94    #[builder(default_code = "PhantomData::<MsrSize>")]
95    _msr_size: PhantomData<MsrSize>,
96}
97
98impl<
99        D: Dynamics,
100        MsrSize: DimName,
101        Accel: DimName,
102        Trk: TrackerSensitivity<D::StateType, D::StateType>,
103    > KalmanODProcess<D, MsrSize, Accel, Trk>
104where
105    D::StateType:
106        Interpolatable + Add<OVector<f64, <D::StateType as State>::Size>, Output = D::StateType>,
107    <DefaultAllocator as Allocator<<D::StateType as State>::VecLength>>::Buffer<f64>: Send,
108    <DefaultAllocator as Allocator<<D::StateType as State>::Size>>::Buffer<f64>: Copy,
109    <DefaultAllocator as Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>>::Buffer<f64>: Copy,
110    DefaultAllocator: Allocator<<D::StateType as State>::Size>
111        + Allocator<<D::StateType as State>::VecLength>
112        + Allocator<MsrSize>
113        + Allocator<MsrSize, <D::StateType as State>::Size>
114        + Allocator<<D::StateType as State>::Size, MsrSize>
115        + Allocator<MsrSize, MsrSize>
116        + Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
117        + Allocator<Accel>
118        + Allocator<Accel, Accel>
119        + Allocator<<D::StateType as State>::Size, Accel>
120        + Allocator<Accel, <D::StateType as State>::Size>
121        + Allocator<nalgebra::Const<1>, MsrSize>,
122{
123    /// Process the provided tracking arc for this orbit determination process.
124    #[allow(clippy::erasing_op)]
125    pub fn process_arc(
126        &self,
127        initial_estimate: KfEstimate<D::StateType>,
128        arc: &TrackingDataArc,
129    ) -> Result<ODSolution<D::StateType, KfEstimate<D::StateType>, MsrSize, Trk>, ODError> {
130        // Initialize the solution.
131        let mut od_sol = ODSolution::new(self.devices.clone(), arc.unique_types());
132
133        let measurements = &arc.measurements;
134        ensure!(
135            measurements.len() >= 2,
136            TooFewMeasurementsSnafu {
137                need: 2_usize,
138                action: "running a Kalman filter"
139            }
140        );
141
142        ensure!(
143            !self.max_step.is_negative() && self.max_step != Duration::ZERO,
144            StepSizeSnafu { step: self.max_step }
145        );
146
147        // Check proper configuration.
148        if MsrSize::USIZE > arc.unique_types().len() {
149            error!("Filter misconfigured: expect high rejection count!");
150            error!(
151                "Arc only contains {} measurement types, but filter configured for {}.",
152                arc.unique_types().len(),
153                MsrSize::USIZE
154            );
155            error!("Filter should be configured for these numbers to match.");
156            error!("Consider running subsequent arcs if ground stations provide different measurements.")
157        }
158
159        // Start by propagating the estimator.
160        let num_msrs = measurements.len();
161
162        // Set up the propagator instance.
163        let prop = self.prop.clone();
164        let mut prop_instance = prop.with(initial_estimate.nominal_state().with_stm(), self.almanac.clone()).quiet();
165
166        // Update the step size of the navigation propagator if it isn't already fixed step
167        if !prop_instance.fixed_step {
168            prop_instance.set_step(self.max_step, false);
169        }
170
171        let prop_time = arc.end_epoch().unwrap() - initial_estimate.epoch();
172        info!("Navigation propagating for a total of {prop_time} with step size {}", self.max_step);
173
174        let resid_crit = if arc.force_reject {
175            warn!("Rejecting all measurements from {arc} as requested");
176            Some(ResidRejectCrit { num_sigmas: 0.0 })
177        } else {
178            self.resid_crit
179        };
180
181        let mut epoch = prop_instance.state.epoch();
182
183        let mut reported = [false; 11];
184        reported[0] = true; // Prevent showing "0% done"
185        info!(
186            "Processing {num_msrs} measurements from {:?}",
187            arc.unique_aliases()
188        );
189
190        // Set up the Kalman filter.
191        let mut kf = KalmanFilter::<D::StateType, Accel> {
192            prev_estimate: initial_estimate,
193            process_noise: self.process_noise.clone(),
194            variant: self.kf_variant,
195            prev_used_snc: 0,
196        };
197
198        kf.initialize_process_noises();
199
200        let mut devices = self.devices.clone();
201
202        // We'll build a trajectory of the estimated states. This will be used to compute the measurements.
203        let mut traj: Traj<D::StateType> = Traj::new();
204
205        let mut msr_accepted_cnt: usize = 0;
206        let mut msr_rejected_cnt: usize = 0;
207        let tick = Epoch::now().unwrap();
208
209        for (msr_cnt, (epoch_ref, msr)) in measurements.iter().enumerate() {
210            let next_msr_epoch = *epoch_ref;
211
212            // Advance the propagator
213            loop {
214                let delta_t = next_msr_epoch - epoch;
215
216                // Propagate for the minimum time between the maximum step size, the next step size, and the duration to the next measurement.
217                let next_step_size = delta_t.min(prop_instance.step_size).min(self.max_step);
218
219                // Remove old states from the trajectory
220                // This is a manual implementation of `retaint` because we know it's a sorted vec, so no need to resort every time
221                let mut index = traj.states.len();
222                while index > 0 {
223                    index -= 1;
224                    if traj.states[index].epoch() >= epoch {
225                        break;
226                    }
227                }
228                traj.states.truncate(index);
229
230                debug!("propagate for {next_step_size} (Δt to next msr: {delta_t})");
231                let (_, traj_covar) = prop_instance
232                    .for_duration_with_traj(next_step_size)
233                    .context(ODPropSnafu)?;
234
235                for state in traj_covar.states {
236                    // NOTE: At the time being, only spacecraft estimation is possible, and the trajectory will always be the exact state
237                    // that was propagated. Even once ground station biases are estimated, these won't go through the propagator.
238                    traj.states.push(state);
239                }
240
241                // Now that we've advanced the propagator, let's see whether we're at the time of the next measurement.
242
243                // Extract the state and update the STM in the filter.
244                let nominal_state = prop_instance.state;
245                // Get the datetime and info needed to compute the theoretical measurement according to the model
246                epoch = nominal_state.epoch();
247
248                // Perform a measurement update, accounting for possible errors in measurement timestamps
249                if (nominal_state.epoch() - next_msr_epoch).abs() < self.epoch_precision {
250                    // Get the computed observations
251                    match devices.get_mut(&msr.tracker) {
252                        Some(device) => {
253                            if let Some(computed_meas) =
254                                device.measure(epoch, &traj, None, self.almanac.clone())?
255                            {
256                                let msr_types = device.measurement_types();
257
258                                // Perform several measurement updates to ensure the desired dimensionality.
259                                let windows = msr_types.len() / MsrSize::USIZE;
260                                let mut msr_rejected = false;
261                                for wno in 0..=windows {
262                                    let mut cur_msr_types = IndexSet::new();
263                                    for msr_type in msr_types
264                                        .iter()
265                                        .copied()
266                                        .skip(wno * MsrSize::USIZE)
267                                        .take(MsrSize::USIZE)
268                                    {
269                                        cur_msr_types.insert(msr_type);
270                                    }
271
272                                    if cur_msr_types.is_empty() {
273                                        // We've processed all measurements.
274                                        break;
275                                    }
276
277                                    // If this measurement type is unavailable, continue to the next one.
278                                    if !msr.availability(&cur_msr_types).iter().any(|avail| *avail)
279                                    {
280                                        continue;
281                                    }
282
283                                    // Grab the un-modulo'd real observation
284                                    let mut real_obs: OVector<f64, MsrSize> =
285                                        msr.observation(&cur_msr_types);
286
287                                    // Check that the observation is valid.
288                                    for val in real_obs.iter().copied() {
289                                        ensure!(
290                                            val.is_finite(),
291                                            InvalidMeasurementSnafu {
292                                                epoch: *epoch_ref,
293                                                val
294                                            }
295                                        );
296                                    }
297
298                                    // Compute device specific matrices
299                                    let h_tilde = device.h_tilde::<MsrSize>(
300                                        msr,
301                                        &cur_msr_types,
302                                        &nominal_state,
303                                        self.almanac.clone(),
304                                    )?;
305
306                                    let measurement_covar =
307                                        device.measurement_covar_matrix(&cur_msr_types, epoch)?;
308
309                                    // Apply any biases on the computed observation
310                                    let computed_obs = computed_meas
311                                        .observation::<MsrSize>(&cur_msr_types)
312                                        - device.measurement_bias_vector::<MsrSize>(
313                                            &cur_msr_types,
314                                            epoch,
315                                        )?;
316
317                                    // Apply the modulo to the real obs
318                                    if let Some(moduli) = &arc.moduli {
319                                        let mut obs_ambiguity = OVector::<f64, MsrSize>::zeros();
320
321                                        for (i, msr_type) in cur_msr_types.iter().enumerate() {
322                                            if let Some(modulus) = moduli.get(msr_type) {
323                                                let k = computed_obs[i].div_euclid(*modulus);
324                                                // real_obs = measured_obs + k * modulus
325                                                obs_ambiguity[i] = k * *modulus;
326                                            }
327                                        }
328                                        real_obs += obs_ambiguity;
329                                    }
330
331                                    let (estimate, mut residual, gain) = kf.measurement_update(
332                                        nominal_state,
333                                        real_obs,
334                                        computed_obs,
335                                        measurement_covar,
336                                        h_tilde,
337                                        resid_crit,
338                                    )?;
339
340                                    debug!("processed measurement #{msr_cnt} for {cur_msr_types:?} @ {epoch} from {}", device.name());
341
342                                    residual.tracker = Some(device.name());
343                                    residual.msr_types = cur_msr_types;
344
345                                    if residual.rejected {
346                                        msr_rejected = true;
347                                    }
348
349                                    if kf.replace_state() {
350                                        prop_instance.state = estimate.state();
351                                    }
352
353                                    prop_instance.state.reset_stm();
354
355                                    od_sol
356                                        .push_measurement_update(estimate, residual, gain);
357                                }
358                                if msr_rejected {
359                                    msr_rejected_cnt += 1;
360                                } else {
361                                    msr_accepted_cnt += 1;
362                                }
363                            } else {
364                                debug!("Device {} does not expect measurement at {epoch}, skipping", msr.tracker);
365                            }
366                        }
367                        None => {
368                            error!(
369                                "Tracker {} is not in the list of configured devices",
370                                msr.tracker
371                            )
372                        }
373                    }
374
375                    let msr_prct = (10.0 * (msr_cnt as f64) / (num_msrs as f64)) as usize;
376                    if !reported[msr_prct] {
377                        let msg = format!(
378                            "{:>3}% done - {msr_accepted_cnt:.0} measurements accepted, {:.0} rejected",
379                            10 * msr_prct, msr_rejected_cnt
380                        );
381                        if msr_accepted_cnt < msr_rejected_cnt {
382                            warn!("{msg}");
383                        } else {
384                            info!("{msg}");
385                        }
386                        reported[msr_prct] = true;
387                    }
388
389                    break;
390                } else {
391                    // No measurement can be used here, let's just do a time update and continue advancing the propagator.
392                    debug!("time update {epoch:?}, next msr {next_msr_epoch:?}");
393                    match kf.time_update(nominal_state) {
394                        Ok(est) => {
395                            // State deviation is always zero for an EKF time update so we don't do anything different than for a CKF.
396                            od_sol.push_time_update(est);
397                        }
398                        Err(e) => return Err(e),
399                    }
400                    prop_instance.state.reset_stm();
401                }
402            }
403        }
404
405        // Always report the 100% mark
406        if !reported[10] {
407            let tock_time = Epoch::now().unwrap() - tick;
408            info!(
409                "100% done - {msr_accepted_cnt} measurements accepted, {msr_rejected_cnt} rejected (done in {tock_time})",
410            );
411        }
412
413        Ok(od_sol)
414    }
415
416    /// Perform a time update. Continuously predicts the trajectory until the provided end epoch, with covariance mapping at each step.
417    pub fn predict_until(
418        &self,
419        initial_estimate: KfEstimate<D::StateType>,
420        end_epoch: Epoch,
421    ) -> Result<ODSolution<D::StateType, KfEstimate<D::StateType>, MsrSize, Trk>, ODError> {
422        // Initialize the solution with no measurement types.
423        let mut od_sol = ODSolution::new(self.devices.clone(), IndexSet::new());
424
425        // Set up the propagator instance.
426        let prop = self.prop.clone();
427        let mut prop_instance = prop.with(initial_estimate.nominal_state().with_stm(), self.almanac.clone()).quiet();
428
429        // Set up the Kalman filter.
430        let mut kf = KalmanFilter::<D::StateType, Accel> {
431            prev_estimate: initial_estimate,
432            process_noise: self.process_noise.clone(),
433            variant: self.kf_variant,
434            prev_used_snc: 0,
435        };
436
437        let prop_time = end_epoch - kf.previous_estimate().epoch();
438        info!("Mapping covariance for {prop_time} every {} until {end_epoch}", self.max_step);
439
440        loop {
441            let nominal_state = prop_instance.for_duration(self.max_step).context(ODPropSnafu)?;
442            // Extract the state and update the STM in the filter.
443            // Get the datetime and info needed to compute the theoretical measurement according to the model
444            let epoch = nominal_state.epoch();
445            // No measurement can be used here, let's just do a time update
446            debug!("time update {epoch}");
447            match kf.time_update(nominal_state) {
448                Ok(est) => {
449                    od_sol.push_time_update(est);
450                }
451                Err(e) => return Err(e),
452            }
453            prop_instance.state.reset_stm();
454            if epoch >= end_epoch {
455                break;
456            }
457        }
458
459        Ok(od_sol)
460    }
461
462    /// Perform a time update. Continuously predicts the trajectory for the provided duration, with covariance mapping at each step. In other words, this performs a time update.
463    pub fn predict_for(
464        &self,
465        initial_estimate: KfEstimate<D::StateType>,
466        duration: Duration,
467    ) -> Result<ODSolution<D::StateType, KfEstimate<D::StateType>, MsrSize, Trk>, ODError> {
468        let end_epoch = initial_estimate.nominal_state().epoch() + duration;
469        self.predict_until(initial_estimate, end_epoch)
470    }
471}