Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/kete_core/src/frames/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ pub fn ecliptic_to_galactic(vec: &Vector3<f64>) -> Vector3<f64> {
/// * `vec` - Vector, arbitrary units.
#[inline(always)]
pub fn ecliptic_to_equatorial(vec: &Vector3<f64>) -> Vector3<f64> {
ECLIPTIC_EQUATORIAL_ROT.transform_vector(vec)
// This is a rotation about X: only the Y/Z plane changes.
let rotation = &*ECLIPTIC_EQUATORIAL_ROT;
Vector3::new(
vec.x,
rotation[(1, 1)] * vec.y + rotation[(1, 2)] * vec.z,
rotation[(2, 1)] * vec.y + rotation[(2, 2)] * vec.z,
)
}

/// Convert a vector from equatorial to ecliptic J2000 frames assuming the 1984 JPL DE
Expand All @@ -233,7 +239,12 @@ pub fn ecliptic_to_equatorial(vec: &Vector3<f64>) -> Vector3<f64> {
/// * `vec` - Vector, arbitrary units.
#[inline(always)]
pub fn equatorial_to_ecliptic(vec: &Vector3<f64>) -> Vector3<f64> {
ECLIPTIC_EQUATORIAL_ROT.inverse().transform_vector(vec)
let rotation = &*ECLIPTIC_EQUATORIAL_ROT;
Vector3::new(
vec.x,
rotation[(1, 1)] * vec.y + rotation[(2, 1)] * vec.z,
rotation[(1, 2)] * vec.y + rotation[(2, 2)] * vec.z,
)
}

/// Derivative of the z rotation matrix with respect to the rotation angle.
Expand Down
29 changes: 23 additions & 6 deletions src/kete_core/src/propagation/acceleration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ pub fn spk_accel(
meta: &mut AccelSPKMeta,
exact_eval: bool,
) -> KeteResult<Vector3<f64>> {
let spk = &LOADED_SPK.try_read().unwrap();
spk_accel_with(time, pos, vel, meta, exact_eval, |_, id| {
spk.try_get_state(id, time, 0, Frame::Equatorial)
.map(|state| (state.pos, state.vel))
})
}

/// Evaluate acceleration using caller-supplied planetary states.
pub(super) fn spk_accel_with<F>(
time: f64,
pos: &Vector3<f64>,
vel: &Vector3<f64>,
meta: &mut AccelSPKMeta,
exact_eval: bool,
mut get_state: F,
) -> KeteResult<Vector3<f64>>
where
F: FnMut(usize, i64) -> KeteResult<([f64; 3], [f64; 3])>,
{
let mut accel = Vector3::<f64>::zeros();

if exact_eval {
Expand All @@ -135,14 +154,12 @@ pub fn spk_accel(
}
}

let spk = &LOADED_SPK.try_read().unwrap();

for grav_params in meta.massive_obj.iter() {
for (index, grav_params) in meta.massive_obj.iter().enumerate() {
let id = grav_params.naif_id;
let radius = grav_params.radius;
let state = spk.try_get_state(id, time, 0, Frame::Equatorial)?;
let rel_pos: Vector3<f64> = pos - Vector3::from(state.pos);
let rel_vel: Vector3<f64> = vel - Vector3::from(state.vel);
let (planet_pos, planet_vel) = get_state(index, id)?;
let rel_pos: Vector3<f64> = pos - Vector3::from(planet_pos);
let rel_vel: Vector3<f64> = vel - Vector3::from(planet_vel);

if exact_eval {
let r = rel_pos.norm();
Expand Down
36 changes: 35 additions & 1 deletion src/kete_core/src/propagation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::prelude::{Desig, KeteResult};
use crate::spice::LOADED_SPK;
use crate::state::State;
use nalgebra::{DVector, Vector3};
use std::{cell::RefCell, collections::VecDeque};

mod acceleration;
mod kepler;
Expand Down Expand Up @@ -93,9 +94,42 @@ pub fn propagate_n_body_spk(
massive_obj: mass_list,
};

// Radau revisits its seven intermediate times during convergence. Planetary
// states depend only on time, not on the trial position of the test particle.
// Keep one step's nodes plus its endpoint; the SPK read guard stays held.
// Force evaluations only need the vectors, not the designation or frame metadata.
let planet_cache = RefCell::new(VecDeque::<(f64, Vec<([f64; 3], [f64; 3])>)>::with_capacity(
8,
));
let acceleration =
|time, pos: &Vector3<f64>, vel: &Vector3<f64>, meta: &mut AccelSPKMeta, exact| {
let mut cache = planet_cache.borrow_mut();
let slot = match cache.iter().position(|(jd, _)| *jd == time) {
Some(slot) => slot,
None => {
let mut states = if cache.len() == 8 {
cache.pop_front().unwrap().1
} else {
Vec::with_capacity(mass_list.len())
};
states.clear();
cache.push_back((time, states));
cache.len() - 1
}
};
let states = &mut cache[slot].1;
spk_accel_with(time, pos, vel, meta, exact, |index, id| {
if index == states.len() {
let state = spk.try_get_state(id, time, 0, Frame::Equatorial)?;
states.push((state.pos, state.vel));
}
Ok(states[index])
})
};

let (pos, vel, _meta) = {
RadauIntegrator::integrate(
&spk_accel,
&acceleration,
state.pos.into(),
state.vel.into(),
state.jd,
Expand Down
9 changes: 3 additions & 6 deletions src/kete_core/src/spice/spk_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
//!
//! <https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/spk.html#Supported%20Data%20Types>
//!
//! Large interpolation routines use the compiler's normal inlining heuristics to
//! avoid inflating the hot planetary lookup path. Types 2 and 3 remain inlined.
//!
//! There is a lot of repetition in this file, as many of the segment types have very
//! similar internal structures.
use super::interpolation::*;
Expand Down Expand Up @@ -203,7 +206,6 @@ impl SpkSegmentType1 {
}
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
// Records are laid out as so:
//
Expand Down Expand Up @@ -545,7 +547,6 @@ impl SpkSegmentType9 {
}
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
let jd = jd_to_spice_jd(jd);
let times = self.get_times();
Expand Down Expand Up @@ -642,7 +643,6 @@ impl SpkSegmentType10 {
.expect("Failed to load orbit values")
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
// TODO: this does not yet implement the interpolation between two neighboring states
// which is present in the cSPICE implementation.
Expand Down Expand Up @@ -760,7 +760,6 @@ impl SpkSegmentType13 {
}
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
let jd = jd_to_spice_jd(jd);
let times = self.get_times();
Expand Down Expand Up @@ -875,7 +874,6 @@ impl SpkSegmentType18 {
}
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
let jd = jd_to_spice_jd(jd);
let times = self.get_times();
Expand Down Expand Up @@ -1003,7 +1001,6 @@ impl SpkSegmentType21 {
}
}

#[inline(always)]
fn try_get_pos_vel(&self, _: &SpkSegment, jd: f64) -> KeteResult<([f64; 3], [f64; 3])> {
// Records are laid out as so:
//
Expand Down