1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use super::celestial::CelestialDynamics;
use super::propulsion::Propulsion;
use super::thrustctrl::ThrustControl;
use super::{Dynamics, ForceModel};
use crate::dimensions::{Vector1, VectorN, U6, U7};
use celestia::State;
use std::fmt;
use std::marker::PhantomData;

pub struct Spacecraft<'a, T: ThrustControl> {
    pub celestial: CelestialDynamics<'a>,
    pub force_models: Vec<Box<dyn ForceModel + 'a>>,
    pub prop: Option<Propulsion<T>>,
    /// in kg
    pub dry_mass: f64,
    /// in kg
    pub fuel_mass: f64,
    _marker: PhantomData<T>,
}

impl<'a, T: ThrustControl> Spacecraft<'a, T> {
    /// Initialize a Spacecraft with a set of celestial dynamics and a propulsion subsystem.
    pub fn with_prop(
        celestial: CelestialDynamics<'a>,
        prop: Propulsion<T>,
        dry_mass: f64,
        fuel_mass: f64,
    ) -> Self {
        Self {
            celestial,
            prop: Some(prop),
            force_models: Vec::new(),
            dry_mass,
            fuel_mass,
            _marker: PhantomData,
        }
    }

    /// Initialize a Spacecraft with a set of celestial dynamics and with SRP enabled.
    pub fn new(celestial: CelestialDynamics<'a>, dry_mass: f64) -> Self {
        // Set the dry mass of the propulsion system
        Self {
            celestial,
            prop: None,
            force_models: Vec::new(),
            dry_mass,
            fuel_mass: 0.0,
            _marker: PhantomData,
        }
    }

    pub fn add_model(&mut self, force_model: Box<dyn ForceModel + 'a>) {
        self.force_models.push(force_model);
    }
}

impl<'a, T: ThrustControl> Dynamics for Spacecraft<'a, T> {
    type StateSize = U7;
    type StateType = SpacecraftState;

    fn time(&self) -> f64 {
        self.celestial.time()
    }

    fn state(&self) -> Self::StateType {
        SpacecraftState {
            orbit: self.celestial.state(),
            dry_mass: self.dry_mass,
            fuel_mass: self.fuel_mass,
        }
    }

    fn state_vector(&self) -> VectorN<f64, Self::StateSize> {
        VectorN::<f64, U7>::from_iterator(
            self.celestial
                .state_vector()
                .iter()
                .chain(Vector1::new(self.fuel_mass).iter())
                .cloned(),
        )
    }

    fn set_state(&mut self, new_t: f64, new_state: &VectorN<f64, Self::StateSize>) {
        let celestial_state = new_state.fixed_rows::<U6>(0).into_owned();
        self.celestial.set_state(new_t, &celestial_state);
        self.fuel_mass = new_state[6];
        if let Some(prop) = &self.prop {
            if prop.decrement_mass {
                assert!(
                    self.fuel_mass >= 0.0,
                    "negative fuel mass at {:?}",
                    self.celestial.state().dt
                );
            }
        }
        if let Some(prop) = self.prop.as_mut() {
            prop.set_state(&self.celestial.state());
        }
    }

    fn eom(&self, t: f64, state: &VectorN<f64, Self::StateSize>) -> VectorN<f64, Self::StateSize> {
        // Compute the celestial dynamics
        let celestial_vec = state.fixed_rows::<U6>(0).into_owned();
        let d_x_celestial = self.celestial.eom(t, &celestial_vec);
        let mut d_x = VectorN::<f64, U7>::from_iterator(
            d_x_celestial
                .iter()
                .chain(Vector1::new(0.0).iter())
                .cloned(),
        );

        let celestial_state = self.celestial.state_ctor(t, &celestial_vec);
        let mut total_mass = self.dry_mass;

        // Now compute the other dynamics as needed.
        if let Some(prop) = &self.prop {
            let (thrust_force, fuel_usage) = prop.eom(&celestial_state);
            // Add the fuel mass to the total mass, minus the change in fuel
            total_mass += self.fuel_mass + fuel_usage;
            for i in 0..3 {
                d_x[i + 3] += thrust_force[i] / (self.dry_mass + state[6]);
            }
            d_x[6] += fuel_usage;
        }

        // Compute additional force models as needed
        for model in &self.force_models {
            let model_frc = model.eom(&celestial_state) / total_mass;
            for i in 0..3 {
                d_x[i + 3] += model_frc[i];
            }
        }

        d_x
    }
}

#[derive(Clone, Copy, Debug)]
pub struct SpacecraftState {
    pub orbit: State,
    pub dry_mass: f64,
    pub fuel_mass: f64,
}

impl fmt::Display for SpacecraftState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:o}\t{} kg", self.orbit, self.dry_mass + self.fuel_mass)
    }
}