Skip to content

Commit

Permalink
Error Control struct is now an enum instead of a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherRabotin committed Sep 30, 2024
1 parent f59f8b2 commit 450e821
Show file tree
Hide file tree
Showing 35 changed files with 410 additions and 482 deletions.
6 changes: 3 additions & 3 deletions examples/03_geo_analysis/raise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use nyx::{
},
io::{gravity::HarmonicsMem, ExportCfg},
md::{prelude::Objective, StateParameter},
propagators::{PropOpts, Propagator, RSSCartesianStep},
propagators::{ErrorControl, IntegratorOptions, Propagator},
Spacecraft,
};
use std::{error::Error, sync::Arc};
Expand Down Expand Up @@ -117,9 +117,9 @@ fn main() -> Result<(), Box<dyn Error>> {
// We specify a minimum step in the propagator because the Ruggiero control would otherwise drive this step very low.
let (final_state, traj) = Propagator::rk89(
sc_dynamics.clone(),
PropOpts::builder()
IntegratorOptions::builder()
.min_step(10.0_f64.seconds())
.error_ctrl(RSSCartesianStep {})
.error_ctrl(ErrorControl::RSSCartesianStep)
.build(),
)
.with(sc, almanac.clone())
Expand Down
6 changes: 3 additions & 3 deletions examples/03_geo_analysis/stationkeeping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use nyx::{
io::{gravity::HarmonicsMem, ExportCfg},
mc::{MonteCarlo, MultivariateNormal, StateDispersion},
md::{prelude::Objective, StateParameter},
propagators::{PropOpts, Propagator, RSSCartesianStep},
propagators::{ErrorControl, IntegratorOptions, Propagator},
Spacecraft, State,
};
use std::{error::Error, sync::Arc};
Expand Down Expand Up @@ -103,9 +103,9 @@ fn main() -> Result<(), Box<dyn Error>> {
// Build the propagator setup.
let setup = Propagator::rk89(
sc_dynamics.clone(),
PropOpts::builder()
IntegratorOptions::builder()
.min_step(10.0_f64.seconds())
.error_ctrl(RSSCartesianStep {})
.error_ctrl(ErrorControl::RSSCartesianStep)
.build(),
);

Expand Down
26 changes: 13 additions & 13 deletions src/mc/montecarlo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::mc::results::{PropResult, Results, Run};
use crate::mc::DispersedState;
use crate::md::trajectory::Interpolatable;
use crate::md::EventEvaluator;
use crate::propagators::{ErrorCtrl, Propagator};
use crate::propagators::Propagator;
#[cfg(not(target_arch = "wasm32"))]
use crate::time::Unit;
use crate::time::{Duration, Epoch};
Expand Down Expand Up @@ -89,9 +89,9 @@ where

/// Generate states and propagate each independently until a specific event is found `trigger` times.
#[allow(clippy::needless_lifetimes)]
pub fn run_until_nth_event<D, E, F>(
pub fn run_until_nth_event<D, F>(
self,
prop: Propagator<D, E>,
prop: Propagator<D>,
almanac: Arc<Almanac>,
max_duration: Duration,
event: &F,
Expand All @@ -100,7 +100,7 @@ where
) -> Results<S, PropResult<S>>
where
D: Dynamics<StateType = S>,
E: ErrorCtrl,

F: EventEvaluator<S>,
DefaultAllocator: Allocator<<D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
Expand All @@ -113,9 +113,9 @@ where
/// Generate states and propagate each independently until a specific event is found `trigger` times.
#[must_use = "Monte Carlo result must be used"]
#[allow(clippy::needless_lifetimes)]
pub fn resume_run_until_nth_event<D, E, F>(
pub fn resume_run_until_nth_event<D, F>(
&self,
prop: Propagator<D, E>,
prop: Propagator<D>,
almanac: Arc<Almanac>,
skip: usize,
max_duration: Duration,
Expand All @@ -125,7 +125,7 @@ where
) -> Results<S, PropResult<S>>
where
D: Dynamics<StateType = S>,
E: ErrorCtrl,

F: EventEvaluator<S>,
DefaultAllocator: Allocator<<D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
Expand Down Expand Up @@ -188,16 +188,16 @@ where
/// Generate states and propagate each independently until a specific event is found `trigger` times.
#[must_use = "Monte Carlo result must be used"]
#[allow(clippy::needless_lifetimes)]
pub fn run_until_epoch<D, E>(
pub fn run_until_epoch<D>(
self,
prop: Propagator<D, E>,
prop: Propagator<D>,
almanac: Arc<Almanac>,
end_epoch: Epoch,
num_runs: usize,
) -> Results<S, PropResult<S>>
where
D: Dynamics<StateType = S>,
E: ErrorCtrl,

DefaultAllocator: Allocator<<D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::VecLength>,
Expand All @@ -209,17 +209,17 @@ where
/// Resumes a Monte Carlo run by skipping the first `skip` items, generating states only after that, and propagate each independently until the specified epoch.
#[must_use = "Monte Carlo result must be used"]
#[allow(clippy::needless_lifetimes)]
pub fn resume_run_until_epoch<D, E>(
pub fn resume_run_until_epoch<D>(
&self,
prop: Propagator<D, E>,
prop: Propagator<D>,
almanac: Arc<Almanac>,
skip: usize,
end_epoch: Epoch,
num_runs: usize,
) -> Results<S, PropResult<S>>
where
D: Dynamics<StateType = S>,
E: ErrorCtrl,

DefaultAllocator: Allocator<<D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::Size, <D::StateType as State>::Size>
+ Allocator<<D::StateType as State>::VecLength>,
Expand Down
2 changes: 1 addition & 1 deletion src/md/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod prelude {
pub use crate::dynamics::{Dynamics, NyxError};
pub use crate::io::gravity::HarmonicsMem;
pub use crate::md::objective::Objective;
pub use crate::propagators::{PropOpts, Propagator};
pub use crate::propagators::{IntegratorOptions, Propagator};
pub use crate::time::{Duration, Epoch, TimeUnits, Unit};
pub use crate::Spacecraft;
pub use crate::{State, TimeTagged};
Expand Down
5 changes: 2 additions & 3 deletions src/md/opti/multipleshooting/altitude_heuristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ use super::{
};
use crate::errors::TargetingError;
use crate::md::{prelude::*, PropSnafu};
use crate::propagators::error_ctrl::ErrorCtrl;
use crate::{Orbit, Spacecraft};

impl<'a, E: ErrorCtrl> MultipleShooting<'a, E, Node, 3, 3> {
impl<'a> MultipleShooting<'a, Node, 3, 3> {
/// Builds a multiple shooting structure assuming that the optimal trajectory is near a linear
/// heuristic in geodetic altitude and direction.
/// For example, if x0 has an altitude of 100 km and xf has an altitude
Expand All @@ -42,7 +41,7 @@ impl<'a, E: ErrorCtrl> MultipleShooting<'a, E, Node, 3, 3> {
node_count: usize,
angular_velocity_deg_s: f64,
body_frame: Frame,
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
almanac: Arc<Almanac>,
) -> Result<Self, MultipleShootingError> {
if node_count < 3 {
Expand Down
5 changes: 2 additions & 3 deletions src/md/opti/multipleshooting/equidistant_heuristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ use super::multishoot::MultipleShooting;
pub use super::CostFunction;
use crate::errors::TargetingError;
use crate::md::prelude::*;
use crate::propagators::error_ctrl::ErrorCtrl;
use crate::{Orbit, Spacecraft};

impl<'a, E: ErrorCtrl> MultipleShooting<'a, E, Node, 3, 3> {
impl<'a> MultipleShooting<'a, Node, 3, 3> {
/// Builds a multiple shooting structure assuming that the optimal trajectory is a straight line
/// between the start and end points. The position of the nodes will be update at each iteration
/// of the outer loop.
Expand All @@ -33,7 +32,7 @@ impl<'a, E: ErrorCtrl> MultipleShooting<'a, E, Node, 3, 3> {
x0: Spacecraft,
xf: Orbit,
node_count: usize,
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
) -> Result<Self, TargetingError> {
if node_count < 3 {
error!("At least three nodes are needed for a multiple shooting optimization");
Expand Down
23 changes: 7 additions & 16 deletions src/md/opti/multipleshooting/multishoot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::linalg::{DMatrix, DVector, SVector};
use crate::md::opti::solution::TargeterSolution;
use crate::md::optimizer::Optimizer;
use crate::md::{prelude::*, TargetingError};
use crate::propagators::error_ctrl::ErrorCtrl;
use crate::pseudo_inverse;
use crate::{Orbit, Spacecraft};

Expand All @@ -39,15 +38,9 @@ pub trait MultishootNode<const O: usize>: Copy + Into<[Objective; O]> {
/// Source of implementation: "Low Thrust Optimization in Cislunar and Translunar space", 2018 Nathan Re (Parrish)
/// OT: size of the objectives for each node (e.g. 3 if the objectives are X, Y, Z).
/// VT: size of the variables for targeter node (e.g. 4 if the objectives are thrust direction (x,y,z) and thrust level).
pub struct MultipleShooting<
'a,
E: ErrorCtrl,
T: MultishootNode<OT>,
const VT: usize,
const OT: usize,
> {
pub struct MultipleShooting<'a, T: MultishootNode<OT>, const VT: usize, const OT: usize> {
/// The propagator setup (kind, stages, etc.)
pub prop: &'a Propagator<SpacecraftDynamics, E>,
pub prop: &'a Propagator<SpacecraftDynamics>,
/// List of nodes of the optimal trajectory
pub targets: Vec<T>,
/// Starting point, must be a spacecraft equipped with a thruster
Expand All @@ -66,9 +59,7 @@ pub struct MultipleShooting<
pub all_dvs: Vec<SVector<f64, VT>>,
}

impl<'a, E: ErrorCtrl, T: MultishootNode<OT>, const VT: usize, const OT: usize>
MultipleShooting<'a, E, T, VT, OT>
{
impl<'a, T: MultishootNode<OT>, const VT: usize, const OT: usize> MultipleShooting<'a, T, VT, OT> {
/// Solve the multiple shooting problem by finding the arrangement of nodes to minimize the cost function.
pub fn solve(
&mut self,
Expand Down Expand Up @@ -287,8 +278,8 @@ impl<'a, E: ErrorCtrl, T: MultishootNode<OT>, const VT: usize, const OT: usize>
}
}

impl<'a, E: ErrorCtrl, T: MultishootNode<OT>, const VT: usize, const OT: usize> fmt::Display
for MultipleShooting<'a, E, T, VT, OT>
impl<'a, T: MultishootNode<OT>, const VT: usize, const OT: usize> fmt::Display
for MultipleShooting<'a, T, VT, OT>
{
#[allow(clippy::or_fun_call, clippy::clone_on_copy)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -357,9 +348,9 @@ impl<T: MultishootNode<O>, const O: usize> fmt::Display for MultipleShootingSolu
impl<T: MultishootNode<O>, const O: usize> MultipleShootingSolution<T, O> {
/// Allows building the trajectories between different nodes
/// This will rebuild the targeters and apply the solutions sequentially
pub fn build_trajectories<E: ErrorCtrl>(
pub fn build_trajectories(
&self,
prop: &Propagator<SpacecraftDynamics, E>,
prop: &Propagator<SpacecraftDynamics>,
almanac: Arc<Almanac>,
) -> Result<Vec<ScTraj>, MultipleShootingError> {
let mut trajz = Vec::with_capacity(self.nodes.len());
Expand Down
43 changes: 19 additions & 24 deletions src/md/opti/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ use crate::md::AstroSnafu;
use crate::md::PropSnafu;
use crate::md::StateParameter;
pub use crate::md::{Variable, Vary};
use crate::propagators::error_ctrl::ErrorCtrl;
use std::fmt;

use super::solution::TargeterSolution;

// TODO(now): rename to Differential Controller

/// An optimizer structure with V control variables and O objectives.
#[derive(Clone)]
pub struct Optimizer<'a, E: ErrorCtrl, const V: usize, const O: usize> {
pub struct Optimizer<'a, const V: usize, const O: usize> {
/// The propagator setup (kind, stages, etc.)
pub prop: &'a Propagator<SpacecraftDynamics, E>,
pub prop: &'a Propagator<SpacecraftDynamics>,
/// The list of objectives of this targeter
pub objectives: [Objective; O],
/// An optional frame (and Cosm) to compute the objectives in.
Expand All @@ -49,7 +50,7 @@ pub struct Optimizer<'a, E: ErrorCtrl, const V: usize, const O: usize> {
pub iterations: usize,
}

impl<'a, E: ErrorCtrl, const V: usize, const O: usize> fmt::Display for Optimizer<'a, E, V, O> {
impl<'a, const V: usize, const O: usize> fmt::Display for Optimizer<'a, V, O> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut objmsg = String::from("");
for obj in &self.objectives {
Expand All @@ -65,12 +66,9 @@ impl<'a, E: ErrorCtrl, const V: usize, const O: usize> fmt::Display for Optimize
}
}

impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 3, O> {
impl<'a, const O: usize> Optimizer<'a, 3, O> {
/// Create a new Targeter which will apply an impulsive delta-v correction.
pub fn delta_v(
prop: &'a Propagator<SpacecraftDynamics, E>,
objectives: [Objective; O],
) -> Self {
pub fn delta_v(prop: &'a Propagator<SpacecraftDynamics>, objectives: [Objective; O]) -> Self {
Self {
prop,
objectives,
Expand All @@ -86,10 +84,7 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 3, O> {
}

/// Create a new Targeter which will MOVE the position of the spacecraft at the correction epoch
pub fn delta_r(
prop: &'a Propagator<SpacecraftDynamics, E>,
objectives: [Objective; O],
) -> Self {
pub fn delta_r(prop: &'a Propagator<SpacecraftDynamics>, objectives: [Objective; O]) -> Self {
Self {
prop,
objectives,
Expand All @@ -105,7 +100,7 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 3, O> {
}

/// Create a new Targeter which will apply an impulsive delta-v correction on all components of the VNC frame. By default, max step is 0.5 km/s.
pub fn vnc(prop: &'a Propagator<SpacecraftDynamics, E>, objectives: [Objective; O]) -> Self {
pub fn vnc(prop: &'a Propagator<SpacecraftDynamics>, objectives: [Objective; O]) -> Self {
Self {
prop,
objectives,
Expand All @@ -121,10 +116,10 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 3, O> {
}
}

impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 4, O> {
impl<'a, const O: usize> Optimizer<'a, 4, O> {
/// Create a new Targeter which will apply a continuous thrust for the whole duration of the segment
pub fn thrust_dir(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
objectives: [Objective; O],
) -> Self {
Self {
Expand All @@ -143,10 +138,10 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 4, O> {
}
}

impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 7, O> {
impl<'a, const O: usize> Optimizer<'a, 7, O> {
/// Create a new Targeter which will apply a continuous thrust for the whole duration of the segment
pub fn thrust_dir_rate(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
objectives: [Objective; O],
) -> Self {
Self {
Expand All @@ -168,10 +163,10 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 7, O> {
}
}

impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 10, O> {
impl<'a, const O: usize> Optimizer<'a, 10, O> {
/// Create a new Targeter which will apply a continuous thrust for the whole duration of the segment
pub fn thrust_profile(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
objectives: [Objective; O],
) -> Self {
Self {
Expand All @@ -196,10 +191,10 @@ impl<'a, E: ErrorCtrl, const O: usize> Optimizer<'a, E, 10, O> {
}
}

impl<'a, E: ErrorCtrl, const V: usize, const O: usize> Optimizer<'a, E, V, O> {
impl<'a, const V: usize, const O: usize> Optimizer<'a, V, O> {
/// Create a new Targeter which will apply an impulsive delta-v correction.
pub fn new(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
variables: [Variable; V],
objectives: [Objective; O],
) -> Self {
Expand All @@ -215,7 +210,7 @@ impl<'a, E: ErrorCtrl, const V: usize, const O: usize> Optimizer<'a, E, V, O> {

/// Create a new Targeter which will apply an impulsive delta-v correction.
pub fn in_frame(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
variables: [Variable; V],
objectives: [Objective; O],
objective_frame: Frame,
Expand All @@ -232,7 +227,7 @@ impl<'a, E: ErrorCtrl, const V: usize, const O: usize> Optimizer<'a, E, V, O> {

/// Create a new Targeter which will apply an impulsive delta-v correction on the specified components of the VNC frame.
pub fn vnc_with_components(
prop: &'a Propagator<SpacecraftDynamics, E>,
prop: &'a Propagator<SpacecraftDynamics>,
variables: [Variable; V],
objectives: [Objective; O],
) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions src/md/opti/raphson_finite_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ use crate::md::{prelude::*, AstroSnafu, GuidanceSnafu, UnderdeterminedProblemSna
use crate::md::{PropSnafu, StateParameter};
pub use crate::md::{Variable, Vary};
use crate::polyfit::CommonPolynomial;
use crate::propagators::error_ctrl::ErrorCtrl;
use crate::pseudo_inverse;
use hifitime::TimeUnits;
use rayon::prelude::*;
use snafu::{ensure, ResultExt};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

impl<'a, E: ErrorCtrl, const V: usize, const O: usize> Optimizer<'a, E, V, O> {
impl<'a, const V: usize, const O: usize> Optimizer<'a, V, O> {
/// Differential correction using finite differencing
#[allow(clippy::comparison_chain)]
pub fn try_achieve_fd(
Expand Down
Loading

0 comments on commit 450e821

Please sign in to comment.