From 944f146b08264cdfdffa372d910755ee7c7faac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Castel=C3=A3o?= Date: Thu, 11 Jul 2024 18:03:35 -0700 Subject: [PATCH] test: Roundtrip z_from_p x p_from_z Confirm that there is an issue with dynamic height and geopotential extra arguments. Fix in the following PR. --- src/conversions.rs | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/conversions.rs b/src/conversions.rs index f6bec36..3d686c6 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -379,6 +379,39 @@ pub fn z_from_p( -2.0 * c / (b + libm::sqrt(b * b - 4.0 * a * c)) } +#[cfg(test)] +mod test_z_from_p { + use super::{p_from_z, z_from_p}; + + #[test] + fn surface() { + assert!((0.0 - z_from_p(0.0, 33.3482, 0.0, 0.0)).abs() < f64::EPSILON); + // assert_eq!(-6010.85496, z_from_p(6131.0, 11.0, 0.0, 0.0)); + } + + #[test] + fn roundtrip() { + let p1 = 6131.0; + let lat = 11.0; + let p2 = p_from_z(z_from_p(p1, lat, 0.0, 0.0), lat, Some(0.0), Some(0.0)).unwrap(); + // Precision defined by (McDougall and Wotherspoon, 2013) + assert!((p1 - p2).abs() <= 1.6e-10); + } + + #[test] + fn roundtrip_with_extras() { + let p1 = 6131.0; + let lat = 11.0; + for dh in [0.0, 1.0] { + for gp in [0.0, 1.0] { + let p2 = p_from_z(z_from_p(p1, lat, dh, gp), lat, Some(dh), Some(gp)).unwrap(); + // Precision defined by (McDougall and Wotherspoon, 2013) + assert!((p1 - p2).abs() <= 1.6e-10); + } + } + } +} + /// Pressure from height (75-term polynomial approximation) /// /// # Arguments @@ -511,7 +544,7 @@ gsw_ionic_strength_from_SA #[cfg(test)] mod tests { - use super::{t90_from_t68, z_from_p}; + use super::t90_from_t68; #[test] fn test_t90_from_t68() { @@ -522,10 +555,4 @@ mod tests { assert!((10.0 - t90_from_t68(10.0024)).abs() < f64::EPSILON); } } - - #[test] - fn test_z_from_p() { - assert!((0.0 - z_from_p(0.0, 33.3482, 0.0, 0.0)).abs() < f64::EPSILON); - // assert_eq!(-6010.85496, z_from_p(6131.0, 11.0, 0.0, 0.0)); - } }