use spacelab_autodiff::{Dual, Scalar, jacobian};
use crate::light_time::SPEED_OF_LIGHT;
use crate::measurement::{
Station, Topocentric, azimuth, declination, elevation, range, range_rate, right_ascension,
};
use crate::radiometric::{CoronaSeries, PlasmaSignature, RadiometricError, solar_corona_delay};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModifierError {
ParameterOutOfRange {
index: usize,
parameters: usize,
},
}
impl core::fmt::Display for ModifierError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::ParameterOutOfRange { index, parameters } => write!(
f,
"estimated parameter {index} is past the {parameters} the measurement carries"
),
}
}
}
impl core::error::Error for ModifierError {}
pub trait Observable<const K: usize> {
fn value<S: Scalar>(&self, satellite: &[S; 6], parameters: &[S; K]) -> S;
}
pub trait Modifier<const K: usize> {
fn delta<S: Scalar>(&self, satellite: &[S; 6], parameters: &[S; K]) -> S;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Modified<O, M> {
pub observable: O,
pub modifier: M,
}
impl<O, M> Modified<O, M> {
#[must_use]
pub const fn new(observable: O, modifier: M) -> Self {
Self {
observable,
modifier,
}
}
#[must_use]
pub const fn with<N>(self, modifier: N) -> Modified<Self, N> {
Modified::new(self, modifier)
}
}
impl<const K: usize, O: Observable<K>, M: Modifier<K>> Observable<K> for Modified<O, M> {
fn value<S: Scalar>(&self, satellite: &[S; 6], parameters: &[S; K]) -> S {
self.observable.value(satellite, parameters) + self.modifier.delta(satellite, parameters)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Range {
pub station: Station,
}
impl<const K: usize> Observable<K> for Range {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
range(satellite, &self.station)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RangeRate {
pub station: Station,
}
impl<const K: usize> Observable<K> for RangeRate {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
range_rate(satellite, &self.station)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RightAscension {
pub station: Station,
}
impl<const K: usize> Observable<K> for RightAscension {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
right_ascension(satellite, &self.station)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Declination {
pub station: Station,
}
impl<const K: usize> Observable<K> for Declination {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
declination(satellite, &self.station)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Elevation {
pub topocentric: Topocentric,
}
impl<const K: usize> Observable<K> for Elevation {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
elevation(satellite, &self.topocentric)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Azimuth {
pub topocentric: Topocentric,
}
impl<const K: usize> Observable<K> for Azimuth {
fn value<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
azimuth(satellite, &self.topocentric)
}
}
#[must_use]
pub fn shapiro_delay<S: Scalar>(r_emitter: S, r_receiver: S, path: S, gm: f64) -> S {
let sum = r_emitter + r_receiver;
S::from_f64(2.0 * gm / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)) * ((sum + path) / (sum - path)).ln()
}
#[must_use]
pub fn shapiro_delay_ppn<S: Scalar>(r_emitter: S, r_receiver: S, path: S, gm: f64, gamma: S) -> S {
let sum = r_emitter + r_receiver;
(gamma + S::from_f64(1.0))
* S::from_f64(gm / (SPEED_OF_LIGHT * SPEED_OF_LIGHT))
* ((sum + path) / (sum - path)).ln()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShapiroDelay {
pub station: [f64; 3],
pub gm: f64,
}
impl<const K: usize> Modifier<K> for ShapiroDelay {
fn delta<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
let r_satellite = (satellite[0] * satellite[0]
+ satellite[1] * satellite[1]
+ satellite[2] * satellite[2])
.sqrt();
let r_station = S::from_f64(libm::sqrt(
self.station[0] * self.station[0]
+ self.station[1] * self.station[1]
+ self.station[2] * self.station[2],
));
let path = range(satellite, &Station::fixed(self.station));
shapiro_delay(r_satellite, r_station, path, self.gm)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bias {
pub value: f64,
}
impl<const K: usize> Modifier<K> for Bias {
fn delta<S: Scalar>(&self, _satellite: &[S; 6], _parameters: &[S; K]) -> S {
S::from_f64(self.value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EstimatedBias<const K: usize> {
index: usize,
}
impl<const K: usize> EstimatedBias<K> {
pub const fn new(index: usize) -> Result<Self, ModifierError> {
if index < K {
Ok(Self { index })
} else {
Err(ModifierError::ParameterOutOfRange {
index,
parameters: K,
})
}
}
#[must_use]
pub const fn index(&self) -> usize {
self.index
}
}
impl<const K: usize> Modifier<K> for EstimatedBias<K> {
fn delta<S: Scalar>(&self, _satellite: &[S; 6], parameters: &[S; K]) -> S {
parameters[self.index]
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EstimatedShapiroDelay<const K: usize> {
pub station: [f64; 3],
pub gm: f64,
index: usize,
}
impl<const K: usize> EstimatedShapiroDelay<K> {
pub const fn new(station: [f64; 3], gm: f64, index: usize) -> Result<Self, ModifierError> {
if index < K {
Ok(Self { station, gm, index })
} else {
Err(ModifierError::ParameterOutOfRange {
index,
parameters: K,
})
}
}
#[must_use]
pub const fn index(&self) -> usize {
self.index
}
}
impl<const K: usize> Modifier<K> for EstimatedShapiroDelay<K> {
fn delta<S: Scalar>(&self, satellite: &[S; 6], parameters: &[S; K]) -> S {
let r_satellite = (satellite[0] * satellite[0]
+ satellite[1] * satellite[1]
+ satellite[2] * satellite[2])
.sqrt();
let r_station = S::from_f64(libm::sqrt(
self.station[0] * self.station[0]
+ self.station[1] * self.station[1]
+ self.station[2] * self.station[2],
));
let path = range(satellite, &Station::fixed(self.station));
shapiro_delay_ppn(
r_satellite,
r_station,
path,
self.gm,
parameters[self.index],
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EstimatedDrift<const K: usize> {
pub elapsed: f64,
bias: usize,
rate: usize,
}
impl<const K: usize> EstimatedDrift<K> {
pub const fn new(elapsed: f64, bias: usize, rate: usize) -> Result<Self, ModifierError> {
if bias >= K {
return Err(ModifierError::ParameterOutOfRange {
index: bias,
parameters: K,
});
}
if rate >= K {
return Err(ModifierError::ParameterOutOfRange {
index: rate,
parameters: K,
});
}
Ok(Self {
elapsed,
bias,
rate,
})
}
#[must_use]
pub const fn bias_index(&self) -> usize {
self.bias
}
#[must_use]
pub const fn rate_index(&self) -> usize {
self.rate
}
}
impl<const K: usize> Modifier<K> for EstimatedDrift<K> {
fn delta<S: Scalar>(&self, _satellite: &[S; 6], parameters: &[S; K]) -> S {
parameters[self.bias] + parameters[self.rate] * S::from_f64(self.elapsed)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SolarCoronaDelay<'a> {
pub station: [f64; 3],
pub sun: [f64; 3],
series: CoronaSeries<'a>,
frequency: f64,
signature: PlasmaSignature,
}
impl<'a> SolarCoronaDelay<'a> {
pub const fn new(
station: [f64; 3],
sun: [f64; 3],
series: CoronaSeries<'a>,
frequency: f64,
signature: PlasmaSignature,
) -> Result<Self, RadiometricError> {
if frequency <= 0.0 || !frequency.is_finite() {
return Err(RadiometricError::NonPositiveFrequency { hertz: frequency });
}
Ok(Self {
station,
sun,
series,
frequency,
signature,
})
}
#[must_use]
pub fn occulted(&self, satellite: &[f64; 6]) -> bool {
let station = self.heliocentric(&[self.station[0], self.station[1], self.station[2]]);
let target = self.heliocentric(&[satellite[0], satellite[1], satellite[2]]);
solar_corona_delay(
&self.series,
&target,
&station,
self.frequency,
self.signature,
)
.is_err()
}
fn heliocentric<S: Scalar>(&self, position: &[S; 3]) -> [S; 3] {
[
position[0] - S::from_f64(self.sun[0]),
position[1] - S::from_f64(self.sun[1]),
position[2] - S::from_f64(self.sun[2]),
]
}
}
impl<const K: usize> Modifier<K> for SolarCoronaDelay<'_> {
fn delta<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
let station = self.heliocentric(&[
S::from_f64(self.station[0]),
S::from_f64(self.station[1]),
S::from_f64(self.station[2]),
]);
let target = self.heliocentric(&[satellite[0], satellite[1], satellite[2]]);
solar_corona_delay(
&self.series,
&target,
&station,
self.frequency,
self.signature,
)
.unwrap_or_else(|_| S::from_f64(f64::NAN))
}
}
pub trait SlantDelay {
fn delay<S: Scalar>(&self, elevation: S, azimuth: S) -> S;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AtmosphericDelay<D> {
pub topocentric: Topocentric,
pub model: D,
}
impl<const K: usize, D: SlantDelay> Modifier<K> for AtmosphericDelay<D> {
fn delta<S: Scalar>(&self, satellite: &[S; 6], _parameters: &[S; K]) -> S {
self.model.delay(
elevation(satellite, &self.topocentric),
azimuth(satellite, &self.topocentric),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Partials<const K: usize> {
pub value: f64,
pub state: [f64; 6],
pub parameters: [f64; K],
}
#[must_use]
pub fn partials<const K: usize, O: Observable<K>>(
observable: &O,
satellite: &[f64; 6],
parameters: &[f64; K],
) -> Partials<K> {
let mut lifted_parameters = [Dual::<6>::constant(0.0); K];
for (slot, p) in lifted_parameters.iter_mut().zip(parameters) {
*slot = Dual::constant(*p);
}
let (value, state_jacobian) =
jacobian::<6, 1, _>(|s| [observable.value(s, &lifted_parameters)], satellite);
let mut lifted_state = [Dual::<K>::constant(0.0); 6];
for (slot, x) in lifted_state.iter_mut().zip(satellite) {
*slot = Dual::constant(*x);
}
let (_, parameter_jacobian) =
jacobian::<K, 1, _>(|p| [observable.value(&lifted_state, p)], parameters);
let mut state = [0.0; 6];
for (slot, row) in state.iter_mut().zip(&state_jacobian) {
*slot = row[0];
}
let mut columns = [0.0; K];
for (slot, row) in columns.iter_mut().zip(¶meter_jacobian) {
*slot = row[0];
}
Partials {
value: value[0],
state,
parameters: columns,
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::excessive_precision,
clippy::expect_used,
clippy::unwrap_used,
clippy::many_single_char_names
)]
use spacelab_testkit::assert_within_ulps;
use super::*;
use crate::radiometric::{AKSIM_PAVLOV_2022, SOLAR_RADIUS};
const EARTH_GM: f64 = 3.986_004_418e14;
const EARTH_RADIUS: f64 = 6_378_137.0;
fn station() -> Station {
Station::fixed([4_075_530.0, 931_781.0, 4_801_618.0])
}
fn topocentric() -> Topocentric {
Topocentric::from_geodetic([4_075_530.0, 931_781.0, 4_801_618.0], 0.8977, 0.2251)
}
fn satellite() -> [f64; 6] {
[
3_961_000.0,
2_614_000.0,
5_215_000.0,
-3_140.0,
6_190.0,
-0.7e3,
]
}
#[test]
fn every_observable_reproduces_the_free_function_to_the_ulp() {
let (sat, st, tp) = (satellite(), station(), topocentric());
let none: [f64; 0] = [];
assert_within_ulps(
Range { station: st }.value(&sat, &none),
range(&sat, &st),
0,
"range",
);
assert_within_ulps(
RangeRate { station: st }.value(&sat, &none),
range_rate(&sat, &st),
0,
"range rate",
);
assert_within_ulps(
RightAscension { station: st }.value(&sat, &none),
right_ascension(&sat, &st),
0,
"right ascension",
);
assert_within_ulps(
Declination { station: st }.value(&sat, &none),
declination(&sat, &st),
0,
"declination",
);
assert_within_ulps(
Elevation { topocentric: tp }.value(&sat, &none),
elevation(&sat, &tp),
0,
"elevation",
);
assert_within_ulps(
Azimuth { topocentric: tp }.value(&sat, &none),
azimuth(&sat, &tp),
0,
"azimuth",
);
}
#[test]
fn a_chain_is_the_hand_written_composition_to_the_ulp() {
let (sat, st) = (satellite(), station());
let none: [f64; 0] = [];
let shapiro = ShapiroDelay {
station: st.position,
gm: EARTH_GM,
};
let bias = Bias { value: -0.012 };
let chained = Modified::new(Range { station: st }, shapiro)
.with(bias)
.value(&sat, &none);
let by_hand = range(&sat, &st) + shapiro.delta(&sat, &none) + bias.delta(&sat, &none);
assert_within_ulps(chained, by_hand, 0, "the chained composition");
}
#[test]
fn the_shapiro_delay_matches_the_maxima_closed_form() {
let maxima = 1.102_527_717_326_717_568_931_198e-3;
assert_within_ulps(
shapiro_delay(7.0e6, 7.5e6, 9.0e5, EARTH_GM),
maxima,
1,
"the Shapiro delay against Maxima",
);
}
#[test]
fn the_shapiro_delay_vanishes_when_the_endpoints_coincide() {
assert_within_ulps(
shapiro_delay(EARTH_RADIUS, EARTH_RADIUS, 0.0, EARTH_GM),
0.0,
0,
"the delay over no path",
);
}
#[test]
fn the_shapiro_modifier_is_the_closed_form_at_the_leg_geometry() {
let (sat, st) = (satellite(), station());
let none: [f64; 0] = [];
let modifier = ShapiroDelay {
station: st.position,
gm: EARTH_GM,
};
let r_sat = libm::sqrt(sat[0] * sat[0] + sat[1] * sat[1] + sat[2] * sat[2]);
let p = st.position;
let r_sta = libm::sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
assert_within_ulps(
modifier.delta(&sat, &none),
shapiro_delay(r_sat, r_sta, range(&sat, &st), EARTH_GM),
0,
"the modifier against the closed form",
);
}
#[test]
fn the_shapiro_partials_are_the_closed_form_derivative() {
let (sat, st) = (satellite(), station());
let none: [f64; 0] = [];
let modifier = ShapiroDelay {
station: st.position,
gm: EARTH_GM,
};
let r_sat = libm::sqrt(sat[0] * sat[0] + sat[1] * sat[1] + sat[2] * sat[2]);
let q = st.position;
let r_sta = libm::sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2]);
let path = range(&sat, &st);
let s = r_sat + r_sta;
let k = 2.0 * EARTH_GM / (SPEED_OF_LIGHT * SPEED_OF_LIGHT);
let (_, delay_jacobian) = jacobian::<6, 1, _>(|z| [modifier.delta(z, &[])], &sat);
let bare = partials(&Range { station: st }, &sat, &none);
let decorated = partials(&Modified::new(Range { station: st }, modifier), &sat, &none);
for i in 0..3 {
let ds = sat[i] / r_sat;
let dp = (sat[i] - q[i]) / path;
let want = k * ((ds + dp) / (s + path) - (ds - dp) / (s - path));
assert!(
want.abs() > 1e-11,
"the delay partial {want} is too small to test"
);
assert!(
(delay_jacobian[i][0] - want).abs() <= 1e-14 * want.abs(),
"∂(Shapiro)/∂r[{i}] = {} against the closed form {want}",
delay_jacobian[i][0]
);
let carried = decorated.state[i] - bare.state[i];
assert!(
(carried - want).abs() < 1e-16,
"the chain carried {carried} of the delay partial {want}"
);
}
for i in 3..6 {
assert_within_ulps(decorated.state[i], bare.state[i], 0, "a velocity partial");
}
}
#[test]
fn an_estimated_bias_is_a_unit_column_and_leaves_the_state_partials_alone() {
let (sat, st) = (satellite(), station());
let bias = EstimatedBias::<2>::new(1).expect("index 1 of 2");
assert_eq!(bias.index(), 1);
let observable = Modified::new(Range { station: st }, bias);
let p = partials(&observable, &sat, &[0.0, 4.75]);
let bare = partials(&Range { station: st }, &sat, &[0.0, 4.75]);
assert_within_ulps(p.value, bare.value + 4.75, 0, "the biased value");
assert_within_ulps(p.parameters[1], 1.0, 0, "∂range/∂bias");
assert_within_ulps(p.parameters[0], 0.0, 0, "∂range/∂(unused parameter)");
for i in 0..6 {
assert_within_ulps(p.state[i], bare.state[i], 0, "a state partial");
}
}
#[test]
fn an_estimated_parameter_past_the_vector_is_refused() {
assert_eq!(
EstimatedBias::<2>::new(2),
Err(ModifierError::ParameterOutOfRange {
index: 2,
parameters: 2,
})
);
assert!(EstimatedBias::<2>::new(1).is_ok());
assert!(EstimatedBias::<0>::new(0).is_err());
}
#[test]
fn an_estimated_bias_reports_the_parameter_it_reads() {
let parameters = [-2.0, 3.5, 7.25];
for index in 0..parameters.len() {
let bias = EstimatedBias::<3>::new(index).expect("index within the vector");
assert_eq!(bias.index(), index);
assert_within_ulps(
bias.delta(&[0.0; 6], ¶meters),
parameters[index],
0,
"the bias read from its own parameter",
);
}
}
#[test]
fn the_out_of_range_refusal_names_both_numbers() {
let error = ModifierError::ParameterOutOfRange {
index: 3,
parameters: 2,
};
let expected = "estimated parameter 3 is past the 2 the measurement carries";
assert_eq!(format!("{error}"), expected);
let dynamic: &dyn core::error::Error = &error;
assert_eq!(format!("{dynamic}"), expected);
}
struct MappedDelay {
zenith: f64,
gradient: f64,
}
impl SlantDelay for MappedDelay {
fn delay<S: Scalar>(&self, elevation: S, azimuth: S) -> S {
(S::from_f64(self.zenith) + S::from_f64(self.gradient) * azimuth.cos())
* elevation.sin().recip()
}
}
#[test]
fn a_supplied_delay_reaches_the_observable_through_the_line_of_sight() {
let (sat, tp) = (satellite(), topocentric());
let none: [f64; 0] = [];
let modifier = AtmosphericDelay {
topocentric: tp,
model: MappedDelay {
zenith: 2.31,
gradient: 0.04,
},
};
assert_within_ulps(
modifier.delta(&sat, &none),
modifier
.model
.delay(elevation(&sat, &tp), azimuth(&sat, &tp)),
0,
"the delay at the line of sight",
);
let observable = Modified::new(Elevation { topocentric: tp }, modifier);
let p = partials(&observable, &sat, &none);
let h = 1.0;
for i in 0..3 {
let (mut lo, mut hi) = (sat, sat);
lo[i] -= h;
hi[i] += h;
let central = (observable.value(&hi, &none) - observable.value(&lo, &none)) / (2.0 * h);
assert!(
(p.state[i] - central).abs() <= 1e-7 * central.abs(),
"∂/∂r[{i}] = {} against the central difference {central}",
p.state[i]
);
}
}
const SUN_GM: f64 = 1.327_124_4e20;
const ASTRONOMICAL_UNIT: f64 = 1.495_978_707e11;
fn conjunction(impact: f64) -> ([f64; 3], [f64; 6]) {
const U: [f64; 3] = [2.0 / 7.0, 3.0 / 7.0, 6.0 / 7.0];
let root61 = libm::sqrt(61.0);
let v = [3.0 / root61, 6.0 / root61, -4.0 / root61];
let (r_earth, r_probe) = (ASTRONOMICAL_UNIT, 8.43 * ASTRONOMICAL_UNIT);
let mut delta = libm::asin(impact * (r_earth + r_probe) / (r_earth * r_probe));
for _ in 0..4 {
let path = libm::sqrt(
r_earth * r_earth + r_probe * r_probe + 2.0 * r_earth * r_probe * libm::cos(delta),
);
delta = libm::asin(impact * path / (r_earth * r_probe));
}
let (sin_delta, cos_delta) = (libm::sin(delta), libm::cos(delta));
let mut earth = [0.0_f64; 3];
let mut probe = [0.0_f64; 6];
for i in 0..3 {
earth[i] = r_earth * U[i];
probe[i] = r_probe * (v[i] * sin_delta - U[i] * cos_delta);
}
(earth, probe)
}
fn impact_parameter(a: &[f64; 3], b: &[f64; 3]) -> f64 {
let cross = [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
];
let separation = libm::sqrt(
(a[0] - b[0]) * (a[0] - b[0])
+ (a[1] - b[1]) * (a[1] - b[1])
+ (a[2] - b[2]) * (a[2] - b[2]),
);
libm::sqrt(cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]) / separation
}
#[test]
fn the_ppn_shapiro_delay_is_the_relativistic_one_at_unit_gamma() {
for path in [9.0e5, 9.0e6, 6.0e7, 1.2e9] {
let (r1, r2) = (7.0e6 + path, 7.5e6 + path);
assert_within_ulps(
shapiro_delay_ppn(r1, r2, path, EARTH_GM, 1.0),
shapiro_delay(r1, r2, path, EARTH_GM),
0,
"the PPN form at γ = 1",
);
}
}
#[test]
fn an_estimated_gamma_is_the_delay_divided_by_one_plus_gamma() {
let (earth, probe) = conjunction(1.6 * SOLAR_RADIUS);
let impact = impact_parameter(&earth, &[probe[0], probe[1], probe[2]]);
assert!(
(impact - 1.6 * SOLAR_RADIUS).abs() < 1e-6 * SOLAR_RADIUS,
"the conjunction geometry was placed at {impact} m from the Sun"
);
let gamma = 1.0;
let modifier = EstimatedShapiroDelay::<1>::new(earth, SUN_GM, 0).expect("index 0 of 1");
assert_eq!(modifier.index(), 0);
let observable = Modified::new(
Range {
station: Station::fixed(earth),
},
modifier,
);
let p = partials(&observable, &probe, &[gamma]);
let delay = modifier.delta(&probe, &[gamma]);
let r_probe = libm::sqrt(probe[0] * probe[0] + probe[1] * probe[1] + probe[2] * probe[2]);
let r_earth = libm::sqrt(earth[0] * earth[0] + earth[1] * earth[1] + earth[2] * earth[2]);
let asymptote = libm::log(4.0 * r_earth * r_probe / (impact * impact));
let approximate = (1.0 + gamma) * SUN_GM / (SPEED_OF_LIGHT * SPEED_OF_LIGHT) * asymptote;
assert!(
(delay - approximate).abs() < 1e-5 * delay,
"the closed form {delay} m against the near-conjunction asymptote {approximate} m"
);
assert!(
(39_300.0..39_400.0).contains(&delay),
"the conjunction delay is {delay} m, not the 39.33 km the doc comment claims"
);
assert!(
(p.parameters[0] - delay / (1.0 + gamma)).abs() < 1e-12 * delay,
"∂ρ/∂γ = {} against Δρ/(1 + γ) = {}",
p.parameters[0],
delay / (1.0 + gamma)
);
assert!(
(19_650.0..19_690.0).contains(&p.parameters[0]),
"∂ρ/∂γ is {} m, not the 19.67 km the doc comment claims",
p.parameters[0]
);
assert_within_ulps(
modifier.delta(&probe, &[3.0]),
2.0 * delay,
0,
"the delay at γ = 3",
);
assert_within_ulps(
modifier.delta(&probe, &[-1.0]),
0.0,
0,
"the delay at γ = −1",
);
}
#[test]
fn an_estimated_gamma_past_the_vector_is_refused() {
assert_eq!(
EstimatedShapiroDelay::<2>::new([0.0, 0.0, 0.0], SUN_GM, 2),
Err(ModifierError::ParameterOutOfRange {
index: 2,
parameters: 2,
})
);
assert!(EstimatedShapiroDelay::<2>::new([0.0, 0.0, 0.0], SUN_GM, 1).is_ok());
}
#[test]
fn an_estimated_gamma_reports_and_reads_its_own_slot() {
let (earth, probe) = conjunction(1.6 * SOLAR_RADIUS);
let relativity = ShapiroDelay {
station: earth,
gm: SUN_GM,
};
let none: [f64; 0] = [];
let expected: f64 = relativity.delta(&probe, &none);
for index in 0..3 {
let modifier =
EstimatedShapiroDelay::<3>::new(earth, SUN_GM, index).expect("within the vector");
assert_eq!(modifier.index(), index);
let mut parameters = [-7.5_f64; 3];
parameters[index] = 1.0;
assert_within_ulps(
modifier.delta(&probe, ¶meters),
expected,
0,
"the delay at γ = 1 read from its own slot",
);
}
}
#[test]
fn a_drifting_clock_contributes_a_unit_column_and_a_ramp_column() {
let (sat, st) = (satellite(), station());
let elapsed = 1_837.5;
let drift =
EstimatedDrift::<3>::new(elapsed, 2, 1).expect("both indices within the vector");
assert_eq!(drift.bias_index(), 2);
assert_eq!(drift.rate_index(), 1);
let parameters = [0.0, 1.25e-3, -0.4];
let observable = Modified::new(Range { station: st }, drift);
let p = partials(&observable, &sat, ¶meters);
let bare = partials(&Range { station: st }, &sat, ¶meters);
assert_within_ulps(
p.value,
bare.value + parameters[2] + parameters[1] * elapsed,
0,
"the drifted range",
);
assert_within_ulps(p.parameters[2], 1.0, 0, "∂range/∂bias");
assert_within_ulps(p.parameters[1], elapsed, 0, "∂range/∂rate");
assert_within_ulps(p.parameters[0], 0.0, 0, "∂range/∂(unused parameter)");
for i in 0..6 {
assert_within_ulps(p.state[i], bare.state[i], 0, "a state partial");
}
for (bias, rate) in [(0, 1), (1, 2), (2, 0)] {
let placed = EstimatedDrift::<3>::new(7.0, bias, rate).expect("within the vector");
assert_eq!((placed.bias_index(), placed.rate_index()), (bias, rate));
}
let at_epoch = EstimatedDrift::<3>::new(0.0, 2, 1).expect("both indices within the vector");
assert_within_ulps(
at_epoch.delta(&sat, ¶meters),
parameters[2],
0,
"the drift at Δt = 0",
);
}
#[test]
fn a_drift_names_whichever_index_is_past_the_vector() {
assert_eq!(
EstimatedDrift::<2>::new(0.0, 2, 1),
Err(ModifierError::ParameterOutOfRange {
index: 2,
parameters: 2,
})
);
assert_eq!(
EstimatedDrift::<2>::new(0.0, 1, 7),
Err(ModifierError::ParameterOutOfRange {
index: 7,
parameters: 2,
})
);
assert_eq!(
EstimatedDrift::<2>::new(0.0, 5, 7),
Err(ModifierError::ParameterOutOfRange {
index: 5,
parameters: 2,
})
);
assert!(EstimatedDrift::<2>::new(0.0, 1, 0).is_ok());
}
#[test]
fn the_corona_modifier_is_the_path_integral_referred_to_the_sun() {
let series = CoronaSeries::new(&AKSIM_PAVLOV_2022).expect("the published one-term series");
let (earth_from_sun, probe_from_sun) = conjunction(4.0 * SOLAR_RADIUS);
let sun = [1.0e10, -2.0e10, 3.0e10];
let station = [
sun[0] + earth_from_sun[0],
sun[1] + earth_from_sun[1],
sun[2] + earth_from_sun[2],
];
let mut satellite = [0.0; 6];
for i in 0..3 {
satellite[i] = sun[i] + probe_from_sun[i];
}
let frequency = 8.4e9;
let none: [f64; 0] = [];
let modifier =
SolarCoronaDelay::new(station, sun, series, frequency, PlasmaSignature::GroupDelay)
.expect("a positive X-band frequency");
let direct = solar_corona_delay(
&series,
&[probe_from_sun[0], probe_from_sun[1], probe_from_sun[2]],
&earth_from_sun,
frequency,
PlasmaSignature::GroupDelay,
)
.expect("a line of sight outside the Sun");
assert_within_ulps(
modifier.delta(&satellite, &none),
direct,
0,
"the corona modifier against the path integral",
);
let shifted_sun = [sun[0] - 7.0e11, sun[1] + 5.0e11, sun[2] - 1.0e11];
let shifted_station = [
station[0] - 7.0e11,
station[1] + 5.0e11,
station[2] - 1.0e11,
];
let mut shifted_satellite = satellite;
shifted_satellite[0] -= 7.0e11;
shifted_satellite[1] += 5.0e11;
shifted_satellite[2] -= 1.0e11;
let shifted = SolarCoronaDelay::new(
shifted_station,
shifted_sun,
series,
frequency,
PlasmaSignature::GroupDelay,
)
.expect("a positive X-band frequency");
assert_within_ulps(
shifted.delta(&shifted_satellite, &none),
direct,
32,
"the corona delay under a common translation",
);
let phase = SolarCoronaDelay::new(
station,
sun,
series,
frequency,
PlasmaSignature::PhaseAdvance,
)
.expect("a positive X-band frequency");
assert_within_ulps(
phase.delta(&satellite, &none),
-direct,
0,
"the phase-advance signature",
);
assert!(
direct > 0.0 && direct.is_finite(),
"the X-band corona delay at four solar radii is {direct} m"
);
}
#[test]
fn occultation_is_exactly_where_the_corona_delay_has_no_value() {
let series = CoronaSeries::new(&AKSIM_PAVLOV_2022).expect("the published one-term series");
let none: [f64; 0] = [];
let mut inside = 0;
let mut outside = 0;
for step in 0..16 {
let radii = 0.25 * f64::from(step) + 0.125;
let (earth_from_sun, probe) = conjunction(radii * SOLAR_RADIUS);
let modifier = SolarCoronaDelay::new(
earth_from_sun,
[0.0, 0.0, 0.0],
series,
8.4e9,
PlasmaSignature::GroupDelay,
)
.expect("a positive X-band frequency");
let value: f64 = modifier.delta(&probe, &none);
assert_eq!(
modifier.occulted(&probe),
value.is_nan(),
"at {radii} solar radii the verdict and the value disagree ({value} m)"
);
if radii < 1.0 {
assert!(modifier.occulted(&probe), "{radii} solar radii is inside");
inside += 1;
} else {
assert!(
!modifier.occulted(&probe) && value > 0.0,
"{radii} solar radii is a link, and the delay is {value} m"
);
outside += 1;
}
}
assert_eq!(
(inside, outside),
(4, 12),
"the sweep crossed the limb once"
);
}
#[test]
fn a_corona_link_without_a_frequency_is_refused_at_construction() {
let series = CoronaSeries::new(&AKSIM_PAVLOV_2022).expect("the published one-term series");
for frequency in [0.0, -8.4e9, f64::NAN, f64::INFINITY] {
assert!(
matches!(
SolarCoronaDelay::new(
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
series,
frequency,
PlasmaSignature::GroupDelay,
),
Err(RadiometricError::NonPositiveFrequency { .. })
),
"{frequency} Hz was accepted as a carrier"
);
}
assert!(
SolarCoronaDelay::new(
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
series,
8.4e9,
PlasmaSignature::GroupDelay,
)
.is_ok()
);
}
#[test]
fn partials_agree_with_a_direct_ad_pass() {
let (sat, st) = (satellite(), station());
let observable = Modified::new(
Range { station: st },
ShapiroDelay {
station: st.position,
gm: EARTH_GM,
},
)
.with(EstimatedBias::<1>::new(0).expect("index 0 of 1"));
let parameters = [-0.031];
let p = partials(&observable, &sat, ¶meters);
let lifted = [Dual::<6>::constant(parameters[0]); 1];
let (value, jac) = jacobian::<6, 1, _>(|s| [observable.value(s, &lifted)], &sat);
assert_within_ulps(p.value, value[0], 0, "the value");
for (i, row) in jac.iter().enumerate() {
assert_within_ulps(p.state[i], row[0], 0, "a state partial");
}
}
}