Skip to main content

nyx_space/od/simulator/
trackdata.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 anise::almanac::Almanac;
20use anise::errors::AlmanacResult;
21use hifitime::Epoch;
22use indexmap::IndexSet;
23use nalgebra::{DimName, OMatrix, OVector};
24use rand_pcg::Pcg64Mcg;
25
26use crate::Orbit;
27use crate::io::ConfigRepr;
28use crate::linalg::DefaultAllocator;
29use crate::linalg::allocator::Allocator;
30use crate::md::prelude::{Frame, Traj};
31use crate::md::trajectory::Interpolatable;
32use crate::od::ODError;
33use crate::od::msr::MeasurementType;
34use crate::od::msr::measurement::Measurement as NewMeasurement;
35
36/// Tracking device simulator.
37pub trait TrackingDevice<MsrIn>: Clone + ConfigRepr
38where
39    MsrIn: Interpolatable,
40    DefaultAllocator:
41        Allocator<MsrIn::Size> + Allocator<MsrIn::Size, MsrIn::Size> + Allocator<MsrIn::VecLength>,
42{
43    /// Returns the name of this tracking data simulator
44    fn name(&self) -> String;
45
46    /// Returns the _enabled_ measurement types for thie device.
47    fn measurement_types(&self) -> &IndexSet<MeasurementType>;
48
49    /// Performs a measurement of the input trajectory at the provided epoch (with integration times if relevant), and returns a measurement from itself to the input state. Returns None of the object is not visible.
50    /// This trait function takes in a trajectory and epoch so it can properly simulate integration times for the measurements.
51    /// If the random number generator is provided, it shall be used to add noise to the measurement.
52    ///
53    /// # Choice of the random number generator
54    /// The Pcg64Mcg is chosen because it is fast, space efficient, and has a good statistical distribution.
55    ///
56    /// # Errors
57    /// + A specific measurement is requested but the noise on that measurement type is not configured.
58    ///
59    fn measure(
60        &mut self,
61        epoch: Epoch,
62        traj: &Traj<MsrIn>,
63        rng: Option<&mut Pcg64Mcg>,
64        almanac: &Almanac,
65    ) -> Result<Option<NewMeasurement>, ODError>;
66
67    /// Returns the device location at the given epoch and in the given frame.
68    fn location(&self, epoch: Epoch, frame: Frame, almanac: &Almanac) -> AlmanacResult<Orbit>;
69
70    // Perform an instantaneous measurement (without integration times, i.e. one-way). Returns None if the object is not visible, else returns the measurement.
71    fn measure_instantaneous(
72        &mut self,
73        rx: MsrIn,
74        rng: Option<&mut Pcg64Mcg>,
75        almanac: &Almanac,
76    ) -> Result<Option<NewMeasurement>, ODError>;
77
78    // Return the noise statistics of this tracking device for the provided measurement type at the requested epoch.
79    fn measurement_covar(&self, msr_type: MeasurementType, epoch: Epoch) -> Result<f64, ODError>;
80
81    // Return the measurement bias for the provided measurement type at the requested epoch.
82    fn measurement_bias(&self, msr_type: MeasurementType, epoch: Epoch) -> Result<f64, ODError>;
83
84    fn measurement_covar_matrix<M: DimName>(
85        &self,
86        msr_types: &IndexSet<MeasurementType>,
87        epoch: Epoch,
88    ) -> Result<OMatrix<f64, M, M>, ODError>
89    where
90        DefaultAllocator: Allocator<M, M>,
91    {
92        // Rebuild the R matrix of the measurement noise.
93        let mut r_mat = OMatrix::<f64, M, M>::zeros();
94
95        for (i, msr_type) in msr_types.iter().enumerate() {
96            if self.measurement_types().contains(msr_type) {
97                r_mat[(i, i)] = self.measurement_covar(*msr_type, epoch)?;
98            }
99        }
100
101        Ok(r_mat)
102    }
103
104    fn measurement_bias_vector<M: DimName>(
105        &self,
106        msr_types: &IndexSet<MeasurementType>,
107        epoch: Epoch,
108    ) -> Result<OVector<f64, M>, ODError>
109    where
110        DefaultAllocator: Allocator<M>,
111    {
112        // Rebuild the R matrix of the measurement noise.
113        let mut b_vec = OVector::<f64, M>::zeros();
114
115        for (i, msr_type) in msr_types.iter().enumerate() {
116            if self.measurement_types().contains(msr_type) {
117                b_vec[i] = self.measurement_bias(*msr_type, epoch)?;
118            }
119        }
120
121        Ok(b_vec)
122    }
123}