extern crate csv;
extern crate rayon;
use crate::errors::NyxError;
use crate::io::formatter::StateFormatter;
use crate::{Orbit, Spacecraft};
use std::error::Error;
use std::fmt;
use std::fs::File;
pub mod ui;
pub mod trajectory;
mod events;
pub use events::{Event, EventEvaluator};
pub mod objective;
pub mod opti;
pub use opti::optimizer;
pub type ScTraj = trajectory::Traj<Spacecraft>;
pub type Ephemeris = trajectory::Traj<Orbit>;
mod param;
pub use param::StateParameter;
pub use opti::target_variable::{Variable, Vary};
pub trait MdHdlr<StateType: Copy>: Send + Sync {
fn handle(&mut self, state: &StateType);
}
pub struct OrbitStateOutput {
csv_out: csv::Writer<File>,
fmtr: StateFormatter,
}
impl OrbitStateOutput {
pub fn new(fmtr: StateFormatter) -> Result<Self, NyxError> {
match csv::Writer::from_path(fmtr.filename.clone()) {
Ok(mut wtr) => {
wtr.serialize(&fmtr.headers)
.expect("could not write headers");
info!("Saving output to {}", fmtr.filename);
Ok(Self { csv_out: wtr, fmtr })
}
Err(e) => Err(NyxError::ExportError(e.to_string())),
}
}
}
impl MdHdlr<Spacecraft> for OrbitStateOutput {
fn handle(&mut self, state: &Spacecraft) {
self.csv_out
.serialize(self.fmtr.fmt(&state.orbit))
.expect("could not format state");
}
}
impl MdHdlr<Orbit> for OrbitStateOutput {
fn handle(&mut self, state: &Orbit) {
self.csv_out
.serialize(self.fmtr.fmt(state))
.expect("could not format state");
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum TargetingError {
UnderdeterminedProblem,
VariableError(String),
FrameError(String),
UnsupportedVariable(Variable),
Verification(String),
}
impl fmt::Display for TargetingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UnderdeterminedProblem => write!(f, "The variables to be adjusted lead to an under-determined of the problem for the targeter"),
Self::VariableError(e) => write!(f, "Incorrectly configured variable: {}", e),
Self::FrameError(e) => write!(f, "Frame error in targeter: {}", e),
Self::UnsupportedVariable(v) => write!(f, "Unsupported variable in problem: {:?}", v),
Self::Verification(e) => write!(f, "Verification of targeting solution failed: {}",e)
}
}
}
impl Error for TargetingError {}
impl From<TargetingError> for NyxError {
fn from(e: TargetingError) -> Self {
NyxError::Targeter(e)
}
}