Skip to main content

vibe/cosmic/
warp.rs

1//! Warp scale velocity curve (OCS §12.2).
2//!
3//! TOS: v = w³·c
4//! TNG: v = w^(10/3)·c for 1 ≤ w ≤ 9
5//! TNG soft saturation: w^(10/3)·c·[1 + α·tan(π(w-9)/(2(10-9+ε)))] for 9 < w < 10
6//!
7//! Reference: OCS Specification v2.2.0 §12.2.
8
9const C: f64 = 299_792_458.0; // Speed of light (m/s)
10
11/// Warp scale variant (OCS §12.2).
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum WarpScale {
14    /// TOS: v = w³·c
15    Tos,
16    /// TNG: v = w^(10/3)·c with soft saturation near warp 10
17    Tng,
18}
19
20/// Compute velocity (m/s) for a given warp factor (OCS §12.2).
21///
22/// - TOS scale: v = w³ · c
23/// - TNG scale (w ≤ 9): v = w^(10/3) · c
24/// - TNG scale (9 < w < 10): soft saturation with tangent barrier
25/// - w ≥ 10: returns infinity (warp 10 is asymptotic)
26pub fn warp_velocity(w: f64, scale: WarpScale) -> f64 {
27    if w < 0.0 {
28        return 0.0;
29    }
30    match scale {
31        WarpScale::Tos => w * w * w * C,
32        WarpScale::Tng => {
33            if w >= 10.0 {
34                return f64::INFINITY;
35            }
36            if w <= 9.0 {
37                return f64::powf(w, 10.0 / 3.0) * C;
38            }
39            // Soft saturation: 9 < w < 10
40            // v = w^(10/3) · c · [1 + α · tan(π(w-9) / (2(10-9+ε)))]
41            let epsilon = 1e-6;
42            let alpha = 0.1; // Saturation coefficient
43            let tan_arg = std::f64::consts::PI * (w - 9.0) / (2.0 * (1.0 + epsilon));
44            let base = f64::powf(w, 10.0 / 3.0) * C;
45            let saturation = 1.0 + alpha * tan_arg.tan();
46            base * saturation
47        }
48    }
49}
50
51/// Warp factor as multiple of c (dimensionless).
52pub fn warp_factor_c(w: f64, scale: WarpScale) -> f64 {
53    warp_velocity(w, scale) / C
54}
55
56/// Cochrane unit — field distortion metric (OCS §12.3).
57/// C = 1.0 at threshold c (warp 1).
58pub fn cochrane_units(w: f64, scale: WarpScale) -> f64 {
59    warp_factor_c(w, scale)
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn tos_warp1_is_c() {
68        let v = warp_velocity(1.0, WarpScale::Tos);
69        assert!((v - C).abs() < 1.0, "warp 1 should be c");
70    }
71
72    #[test]
73    fn tos_warp2_is_8c() {
74        let v = warp_velocity(2.0, WarpScale::Tos);
75        assert!((v - 8.0 * C).abs() < 1.0, "warp 2 should be 8c");
76    }
77
78    #[test]
79    fn tng_warp1_is_c() {
80        let v = warp_velocity(1.0, WarpScale::Tng);
81        assert!((v - C).abs() < 1.0, "warp 1 should be c");
82    }
83
84    #[test]
85    fn tng_warp9() {
86        // v = 9^(10/3) * c ≈ 1516.38 * c
87        let v = warp_velocity(9.0, WarpScale::Tng);
88        let expected = f64::powf(9.0, 10.0 / 3.0) * C;
89        assert!((v - expected).abs() < 1.0);
90    }
91
92    #[test]
93    fn tng_warp10_is_infinite() {
94        let v = warp_velocity(10.0, WarpScale::Tng);
95        assert!(v.is_infinite(), "warp 10 should be infinite");
96    }
97
98    #[test]
99    fn tng_warp_9_99_finite() {
100        // OCS-T13: Warp 9.99 evaluates without NaN
101        let v = warp_velocity(9.99, WarpScale::Tng);
102        assert!(v.is_finite(), "warp 9.99 should be finite");
103        assert!(v > 0.0);
104    }
105
106    #[test]
107    fn tng_warp_9_99_no_nan() {
108        // OCS-T13: no numerical overflow/NaN
109        let v = warp_velocity(9.999, WarpScale::Tng);
110        assert!(!v.is_nan(), "warp 9.999 must not be NaN");
111        assert!(v.is_finite(), "warp 9.999 should be finite");
112    }
113
114    #[test]
115    fn warp0_is_zero() {
116        assert_eq!(warp_velocity(0.0, WarpScale::Tos), 0.0);
117        assert_eq!(warp_velocity(0.0, WarpScale::Tng), 0.0);
118    }
119
120    #[test]
121    fn negative_warp_is_zero() {
122        assert_eq!(warp_velocity(-1.0, WarpScale::Tos), 0.0);
123        assert_eq!(warp_velocity(-1.0, WarpScale::Tng), 0.0);
124    }
125
126    #[test]
127    fn cochrane_at_warp1() {
128        let c = cochrane_units(1.0, WarpScale::Tng);
129        assert!((c - 1.0).abs() < 1e-10, "C should be 1.0 at warp 1");
130    }
131
132    #[test]
133    fn tng_soft_saturation_increases() {
134        // Velocity at 9.5 should be greater than at 9.0
135        let v9 = warp_velocity(9.0, WarpScale::Tng);
136        let v95 = warp_velocity(9.5, WarpScale::Tng);
137        assert!(v95 > v9, "warp 9.5 should be faster than warp 9");
138    }
139}