|
| 1 | +/* |
| 2 | + Nyx, blazing fast astrodynamics |
| 3 | + Copyright (C) 2023 Christopher Rabotin <[email protected]> |
| 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 | + |
| 19 | +use crate::errors::NyxError; |
| 20 | +use crate::linalg::allocator::Allocator; |
| 21 | +use crate::linalg::DefaultAllocator; |
| 22 | +use crate::md::prelude::{Interpolatable, Traj}; |
| 23 | +use crate::md::EventEvaluator; |
| 24 | +use crate::time::Duration; |
| 25 | +use core::fmt; |
| 26 | + |
| 27 | +/// Enumerates the possible edges of an event in a trajectory. |
| 28 | +/// |
| 29 | +/// `EventEdge` is used to describe the nature of a trajectory event, particularly in terms of its temporal dynamics relative to a specified condition or threshold. This enum helps in distinguishing whether the event is occurring at a rising edge, a falling edge, or if the edge is unclear due to insufficient data or ambiguous conditions. |
| 30 | +#[derive(Copy, Clone, Debug, PartialEq)] |
| 31 | +pub enum EventEdge { |
| 32 | + /// Represents a rising edge of the event. This indicates that the event is transitioning from a lower to a higher evaluation of the event. For example, in the context of elevation, a rising edge would indicate an increase in elevation from a lower angle. |
| 33 | + Rising, |
| 34 | + /// Represents a falling edge of the event. This is the opposite of the rising edge, indicating a transition from a higher to a lower value of the event evaluator. For example, if tracking the elevation of an object, a falling edge would signify a |
| 35 | + Falling, |
| 36 | + /// If the edge cannot be clearly defined, it will be marked as unclear. This happens if the event is at a saddle point and the epoch precision is too large to find the exact slope. |
| 37 | + Unclear, |
| 38 | +} |
| 39 | + |
| 40 | +/// Represents the details of an event occurring along a trajectory. |
| 41 | +/// |
| 42 | +/// `EventDetails` encapsulates the state at which a particular event occurs in a trajectory, along with additional information about the nature of the event. This struct is particularly useful for understanding the dynamics of the event, such as whether it represents a rising or falling edge, or if the edge is unclear. |
| 43 | +/// |
| 44 | +/// # Generics |
| 45 | +/// S: Interpolatable - A type that represents the state of the trajectory. This type must implement the `Interpolatable` trait, ensuring that it can be interpolated and manipulated according to the trajectory's requirements. |
| 46 | +#[derive(Clone, Debug, PartialEq)] |
| 47 | +pub struct EventDetails<S: Interpolatable> |
| 48 | +where |
| 49 | + DefaultAllocator: |
| 50 | + Allocator<f64, S::VecLength> + Allocator<f64, S::Size> + Allocator<f64, S::Size, S::Size>, |
| 51 | +{ |
| 52 | + /// The state of the trajectory at the found event. |
| 53 | + pub state: S, |
| 54 | + /// Indicates whether the event is a rising edge, falling edge, or unclear. This helps in understanding the direction of change at the event point. |
| 55 | + pub edge: EventEdge, |
| 56 | + /// Numerical evaluation of the event condition, e.g. if seeking the apoapsis, this returns the near zero |
| 57 | + pub value: f64, |
| 58 | + /// Numertical evaluation of the event condition one epoch step before the found event (used to compute the rising/falling edge). |
| 59 | + pub prev_value: Option<f64>, |
| 60 | + /// Numertical evaluation of the event condition one epoch step after the found event (used to compute the rising/falling edge). |
| 61 | + pub next_value: Option<f64>, |
| 62 | + /// Precision of the epoch for this value |
| 63 | + pub pm_duration: Duration, |
| 64 | + // Store the representation of this event as a string because we can't move or clone the event reference |
| 65 | + pub repr: String, |
| 66 | +} |
| 67 | + |
| 68 | +impl<S: Interpolatable> EventDetails<S> |
| 69 | +where |
| 70 | + DefaultAllocator: |
| 71 | + Allocator<f64, S::VecLength> + Allocator<f64, S::Size> + Allocator<f64, S::Size, S::Size>, |
| 72 | +{ |
| 73 | + /// Generates detailed information about an event at a specific epoch in a trajectory. |
| 74 | + /// |
| 75 | + /// This takes an `Epoch` as an input and returns a `Result<Self, NyxError>`. |
| 76 | + /// It is designed to determine the state of a trajectory at a given epoch, evaluate the specific event at that state, and ascertain the nature of the event (rising, falling, or unclear). |
| 77 | + /// The initialization intelligently determines the edge type of the event by comparing the event's value at the current, previous, and next epochs. |
| 78 | + /// It ensures robust event characterization in trajectories. |
| 79 | + /// |
| 80 | + /// # Returns |
| 81 | + /// - `Ok(EventDetails<S>)` if the state at the given epoch can be determined and the event details are successfully evaluated. |
| 82 | + /// - `Err(NyxError)` if there is an error in retrieving the state at the specified epoch. |
| 83 | + /// |
| 84 | + pub fn new<E: EventEvaluator<S>>( |
| 85 | + state: S, |
| 86 | + value: f64, |
| 87 | + event: &E, |
| 88 | + traj: &Traj<S>, |
| 89 | + ) -> Result<Self, NyxError> { |
| 90 | + let epoch = state.epoch(); |
| 91 | + let prev_value = if let Ok(state) = traj.at(epoch - event.epoch_precision()) { |
| 92 | + Some(event.eval(&state)) |
| 93 | + } else { |
| 94 | + None |
| 95 | + }; |
| 96 | + |
| 97 | + let next_value = if let Ok(state) = traj.at(epoch + event.epoch_precision()) { |
| 98 | + Some(event.eval(&state)) |
| 99 | + } else { |
| 100 | + None |
| 101 | + }; |
| 102 | + |
| 103 | + let edge = if let Some(prev_value) = prev_value { |
| 104 | + if let Some(next_value) = next_value { |
| 105 | + if prev_value > value && value > next_value { |
| 106 | + EventEdge::Falling |
| 107 | + } else if prev_value < value && value < next_value { |
| 108 | + EventEdge::Rising |
| 109 | + } else { |
| 110 | + warn!("could not determine edge of {} at {}", event, state.epoch(),); |
| 111 | + EventEdge::Unclear |
| 112 | + } |
| 113 | + } else if prev_value > value { |
| 114 | + EventEdge::Falling |
| 115 | + } else { |
| 116 | + EventEdge::Rising |
| 117 | + } |
| 118 | + } else if let Some(next_value) = next_value { |
| 119 | + if next_value > value { |
| 120 | + EventEdge::Rising |
| 121 | + } else { |
| 122 | + EventEdge::Falling |
| 123 | + } |
| 124 | + } else { |
| 125 | + warn!( |
| 126 | + "could not determine edge of {} because trajectory could be queried around {}", |
| 127 | + event, |
| 128 | + state.epoch() |
| 129 | + ); |
| 130 | + EventEdge::Unclear |
| 131 | + }; |
| 132 | + |
| 133 | + Ok(EventDetails { |
| 134 | + edge, |
| 135 | + state, |
| 136 | + value, |
| 137 | + prev_value, |
| 138 | + next_value, |
| 139 | + pm_duration: event.epoch_precision(), |
| 140 | + repr: event.eval_string(&state).to_string(), |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl<S: Interpolatable> fmt::Display for EventDetails<S> |
| 146 | +where |
| 147 | + DefaultAllocator: |
| 148 | + Allocator<f64, S::VecLength> + Allocator<f64, S::Size> + Allocator<f64, S::Size, S::Size>, |
| 149 | +{ |
| 150 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 151 | + let prev_fmt = match self.prev_value { |
| 152 | + Some(value) => format!("{value:.6}"), |
| 153 | + None => "".to_string(), |
| 154 | + }; |
| 155 | + |
| 156 | + let next_fmt = match self.next_value { |
| 157 | + Some(value) => format!("{value:.6}"), |
| 158 | + None => "".to_string(), |
| 159 | + }; |
| 160 | + |
| 161 | + write!( |
| 162 | + f, |
| 163 | + "{} and is {:?} (roots with {} intervals: {}, {:.6}, {})", |
| 164 | + self.repr, self.edge, self.pm_duration, prev_fmt, self.value, next_fmt |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +#[derive(Clone, Debug, PartialEq)] |
| 170 | +pub struct EventArc<S: Interpolatable> |
| 171 | +where |
| 172 | + DefaultAllocator: |
| 173 | + Allocator<f64, S::VecLength> + Allocator<f64, S::Size> + Allocator<f64, S::Size, S::Size>, |
| 174 | +{ |
| 175 | + pub rise: EventDetails<S>, |
| 176 | + pub fall: EventDetails<S>, |
| 177 | +} |
| 178 | + |
| 179 | +impl<S: Interpolatable> fmt::Display for EventArc<S> |
| 180 | +where |
| 181 | + DefaultAllocator: |
| 182 | + Allocator<f64, S::VecLength> + Allocator<f64, S::Size> + Allocator<f64, S::Size, S::Size>, |
| 183 | +{ |
| 184 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 185 | + write!( |
| 186 | + f, |
| 187 | + "{} until {} (lasts {})", |
| 188 | + self.rise, |
| 189 | + self.fall.state.epoch(), |
| 190 | + self.fall.state.epoch() - self.rise.state.epoch() |
| 191 | + ) |
| 192 | + } |
| 193 | +} |
0 commit comments