Skip to content

Commit

Permalink
Refactor two body state vector representations
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Jan 19, 2024
1 parent e8be9dc commit 8d9395f
Show file tree
Hide file tree
Showing 11 changed files with 855 additions and 351 deletions.
7 changes: 2 additions & 5 deletions crates/lox-space/examples/iss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ fn main() {
let epoch = Epoch::from_date_and_time(TimeScale::TDB, date, time);
let position = DVec3::new(6068279.27, -1692843.94, -2516619.18) * 1e-3;
let velocity = DVec3::new(-660.415582, 5495.938726, -5303.093233) * 1e-3;
let iss = Cartesian::new(epoch, Earth, position, velocity);
let iss = Cartesian::new(epoch, Earth, Icrf, position, velocity);

println!(
"ISS Orbit for Julian Day {}",
iss.epoch().days_since_j2000(),
);
println!("ISS Orbit for Julian Day {}", iss.time().days_since_j2000(),);
println!("=============================");
println!("Semi-major axis: {:.3} km", iss.semi_major());
println!("Eccentricity: {:.6}", iss.eccentricity());
Expand Down
3 changes: 1 addition & 2 deletions crates/lox-space/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/

pub use lox_core::bodies::*;

pub use lox_core::frames::*;
pub use lox_core::time::dates::*;
pub use lox_core::time::epochs::*;

pub use lox_core::two_body::{Cartesian, DVec3, Keplerian, TwoBody};
49 changes: 36 additions & 13 deletions crates/lox_core/src/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ macro_rules! body {
}
}

impl Display for $i {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}

Check warning on line 98 in crates/lox_core/src/bodies.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox_core/src/bodies.rs#L96-L98

Added lines #L96 - L98 were not covered by tests
}
};
($i:ident, $t:ident, $name:literal, $naif_id:literal) => {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

Check warning on line 102 in crates/lox_core/src/bodies.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox_core/src/bodies.rs#L102

Added line #L102 was not covered by tests
pub struct $i;

impl $t for $i {}

impl Body for $i {
fn id(&self) -> NaifId {
NaifId($naif_id)
}

Check warning on line 110 in crates/lox_core/src/bodies.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox_core/src/bodies.rs#L108-L110

Added lines #L108 - L110 were not covered by tests

fn name(&self) -> &'static str {
$name
}
}

impl Display for $i {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
Expand All @@ -118,16 +140,19 @@ body! { Neptune, Planet, 899 }
body! { Pluto, Planet, 999 }

// Barycenters.
body! { SolarSystemBarycenter, "Solar System Barycenter", 0 }
body! { MercuryBarycenter, "Mercury Barycenter", 1 }
body! { VenusBarycenter, "Venus Barycenter", 2 }
body! { EarthBarycenter, "Earth Barycenter", 3 }
body! { MarsBarycenter, "Mars Barycenter", 4 }
body! { JupiterBarycenter, "Jupiter Barycenter", 5 }
body! { SaturnBarycenter, "Saturn Barycenter", 6 }
body! { UranusBarycenter, "Uranus Barycenter", 7 }
body! { NeptuneBarycenter, "Neptune Barycenter", 8 }
body! { PlutoBarycenter, "Pluto Barycenter", 9 }
pub trait Barycenter: PointMass + DynClone {}
clone_trait_object!(Barycenter);

body! { SolarSystemBarycenter, Barycenter, "Solar System Barycenter", 0 }
body! { MercuryBarycenter, Barycenter, "Mercury Barycenter", 1 }
body! { VenusBarycenter, Barycenter, "Venus Barycenter", 2 }
body! { EarthBarycenter, Barycenter, "Earth Barycenter", 3 }
body! { MarsBarycenter, Barycenter, "Mars Barycenter", 4 }
body! { JupiterBarycenter, Barycenter, "Jupiter Barycenter", 5 }
body! { SaturnBarycenter, Barycenter, "Saturn Barycenter", 6 }
body! { UranusBarycenter, Barycenter, "Uranus Barycenter", 7 }
body! { NeptuneBarycenter, Barycenter, "Neptune Barycenter", 8 }
body! { PlutoBarycenter, Barycenter, "Pluto Barycenter", 9 }

impl PointMass for SolarSystemBarycenter {
fn gravitational_parameter(&self) -> f64 {
Expand Down Expand Up @@ -545,12 +570,10 @@ pub trait TriAxial: Ellipsoid {
fn along_orbit_radius(&self) -> f64;
}

pub trait PointMass: Body + DynClone {
pub trait PointMass: Body {
fn gravitational_parameter(&self) -> f64;
}

clone_trait_object!(PointMass);

pub type PolynomialCoefficients = (f64, f64, f64, &'static [f64]);

pub type NutationPrecessionCoefficients = (&'static [f64], &'static [f64]);
Expand Down
27 changes: 19 additions & 8 deletions crates/lox_core/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@ pub mod iau;
// TODO: Replace with proper `Epoch` type
type Epoch = f64;

pub trait ReferenceFrame {
fn is_inertial(&self) -> bool;
pub trait ReferenceFrame {}

pub trait InertialFrame: ReferenceFrame {
fn is_inertial(&self) -> bool {
true
}

Check warning on line 23 in crates/lox_core/src/frames.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox_core/src/frames.rs#L21-L23

Added lines #L21 - L23 were not covered by tests

fn is_rotating(&self) -> bool {
false
}

Check warning on line 27 in crates/lox_core/src/frames.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox_core/src/frames.rs#L25-L27

Added lines #L25 - L27 were not covered by tests
}

pub trait RotatingFrame: ReferenceFrame {
fn is_inertial(&self) -> bool {
false
}

fn is_rotating(&self) -> bool {
!self.is_inertial()
true
}
}

Expand All @@ -32,11 +46,8 @@ impl Display for Icrf {
}
}

impl ReferenceFrame for Icrf {
fn is_inertial(&self) -> bool {
true
}
}
impl ReferenceFrame for Icrf {}
impl InertialFrame for Icrf {}

pub fn rotation_matrix_derivative(m: DMat3, v: DVec3) -> DMat3 {
let sx = DVec3::new(0.0, v.z, v.y);
Expand Down
14 changes: 9 additions & 5 deletions crates/lox_core/src/frames/iau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*/

use std::fmt::Debug;
use std::fmt::{Debug, Display, Formatter};

use glam::{DMat3, DVec3};

use crate::bodies::RotationalElements;
use crate::frames::{Epoch, FromFrame, Icrf, ReferenceFrame, Rotation};
use crate::frames::{Epoch, FromFrame, Icrf, ReferenceFrame, RotatingFrame, Rotation};

#[derive(Debug, Copy, Clone)]
pub struct BodyFixed<T: RotationalElements>(pub T);

impl<T: RotationalElements> ReferenceFrame for BodyFixed<T> {
fn is_inertial(&self) -> bool {
false
impl<T: RotationalElements> Display for BodyFixed<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "IAU_{}", &self.0.name().to_uppercase())
}
}

impl<T: RotationalElements> ReferenceFrame for BodyFixed<T> {}
impl<T: RotationalElements> RotatingFrame for BodyFixed<T> {}

impl<T: RotationalElements> FromFrame<Icrf> for BodyFixed<T> {
fn rotation_from(&self, _: Icrf, t: Epoch) -> Rotation {
let (right_ascension, declination, prime_meridian) = T::rotational_elements(t);
Expand Down Expand Up @@ -49,6 +52,7 @@ mod tests {
#[test]
fn test_bodyfixed() {
let iau_jupiter = BodyFixed(Jupiter);
assert_eq!(format!("{}", iau_jupiter), "IAU_JUPITER");
assert!(!iau_jupiter.is_inertial());
assert!(iau_jupiter.is_rotating());

Expand Down
Loading

0 comments on commit 8d9395f

Please sign in to comment.