qualia_core_db/modalities/
spatiotemporal_predicates.rs1use crate::NQuin;
10
11pub const SPEED_OF_LIGHT_M_S: f64 = 299_792_458.0;
13pub const STANDARD_GRAVITY_M_S2: f64 = 9.80665;
14pub const C_SQUARED: f64 = SPEED_OF_LIGHT_M_S * SPEED_OF_LIGHT_M_S;
15
16pub const OP_SPATIOTEMPORAL_ANCHOR: u8 = 0x70; pub const OP_TIME_AT_OBSERVER: u8 = 0x71; pub const OP_PROPER_TIME_GRADIENT: u8 = 0x72; pub const OP_SPATIAL_DECOHERENCE: u8 = 0x73; pub const OP_TEMPORAL_CONTAINMENT: u8 = 0x74; pub const OP_PERSISTENCE_HORIZON: u8 = 0x75; pub const OP_ECOLOGICAL_RESIDUE: u8 = 0x76; pub const OP_BIOMAGNIFICATION_LAG: u8 = 0x77; pub const OP_SPATIOTEMPORAL_COINCIDENCE: u8 = 0x78; pub const OP_ASYMMETRIC_RECURRENCE: u8 = 0x79; pub const OP_LATENT_EPOCH: u8 = 0x7A; pub const OP_CIRCADIAN_OVERRIDE: u8 = 0x7B; pub const OP_MEREOLOGY_LIFESPAN: u8 = 0x7C; pub const OP_INDIGENOUS_EPOCH: u8 = 0x7D; pub const OP_INTRODUCTION_EVENT: u8 = 0x7E; pub const OP_NATURALIZATION_ONSET: u8 = 0x7F; #[repr(u8)]
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum ObserverStatus {
38 Stationary = 0,
39 InTransit = 1,
40 Accelerating = 2,
41 Obstructed = 3,
42}
43
44#[repr(C)]
46#[derive(Debug, Clone, Copy, PartialEq)]
47pub struct SpatiotemporalPoint {
48 pub pos_xyz_m: [f64; 3],
50 pub srid_code: u32,
52 pub unix_time_nanos: u64,
54 pub elevation_m: f64,
56 pub velocity_m_s: [f64; 3],
58 pub reference_frame_id: u64,
60 pub observer_status: ObserverStatus,
62}
63
64impl Default for SpatiotemporalPoint {
65 fn default() -> Self {
68 Self {
69 pos_xyz_m: [0.0; 3],
70 srid_code: 7844, unix_time_nanos: 0,
72 elevation_m: 0.0,
73 velocity_m_s: [0.0; 3],
74 reference_frame_id: 0,
75 observer_status: ObserverStatus::Stationary,
76 }
77 }
78}
79
80impl SpatiotemporalPoint {
81 pub fn new(x: f64, y: f64, z: f64, srid: u32, nanos: u64, elevation_m: f64) -> Self {
85 Self {
86 pos_xyz_m: [x, y, z],
87 srid_code: srid,
88 unix_time_nanos: nanos,
89 elevation_m,
90 velocity_m_s: [0.0; 3],
91 reference_frame_id: 0,
92 observer_status: ObserverStatus::Stationary,
93 }
94 }
95
96 #[inline]
98 pub fn speed_m_s(&self) -> f64 {
99 let v2 = self.velocity_m_s[0] * self.velocity_m_s[0]
100 + self.velocity_m_s[1] * self.velocity_m_s[1]
101 + self.velocity_m_s[2] * self.velocity_m_s[2];
102 v2.sqrt()
103 }
104}
105
106#[inline]
110pub fn evaluate_light_travel_offset_nanos(
111 source: &SpatiotemporalPoint,
112 observer: &SpatiotemporalPoint,
113) -> u64 {
114 let dx = observer.pos_xyz_m[0] - source.pos_xyz_m[0];
115 let dy = observer.pos_xyz_m[1] - source.pos_xyz_m[1];
116 let dz = observer.pos_xyz_m[2] - source.pos_xyz_m[2];
117 let distance_m = (dx * dx + dy * dy + dz * dz).sqrt();
118 let seconds = distance_m / SPEED_OF_LIGHT_M_S;
119 (seconds * 1_000_000_000.0).round() as u64
120}
121
122#[inline]
126pub fn evaluate_observed_source_time(
127 observer_time_nanos: u64,
128 source: &SpatiotemporalPoint,
129 observer: &SpatiotemporalPoint,
130) -> u64 {
131 let offset_nanos = evaluate_light_travel_offset_nanos(source, observer);
132 observer_time_nanos.saturating_sub(offset_nanos)
133}
134
135#[inline]
140pub fn evaluate_proper_time_rate(elevation_ahd_m: f64, speed_m_s: f64) -> f64 {
141 let grav_term = (STANDARD_GRAVITY_M_S2 * elevation_ahd_m) / C_SQUARED;
142 let kin_term = (speed_m_s * speed_m_s) / (2.0 * C_SQUARED);
143 1.0 + grav_term - kin_term
144}
145
146#[inline]
151pub fn evaluate_proper_time_drift_nanos(
152 elevation_ahd_m: f64,
153 speed_m_s: f64,
154 duration_seconds: f64,
155) -> f64 {
156 let rate = evaluate_proper_time_rate(elevation_ahd_m, speed_m_s);
157 (rate - 1.0) * duration_seconds * 1_000_000_000.0
158}
159
160#[inline]
164pub fn evaluate_quantum_gradient_nanos(base_time_nanos: u64, dilation_factor: f64) -> u64 {
165 ((base_time_nanos as f64) * dilation_factor).round() as u64
166}
167
168#[inline]
172pub fn evaluate_persistence_horizon_nanos(
173 app_time_nanos: u64,
174 half_life_nanos: u64,
175 half_lives: u32,
176) -> u64 {
177 app_time_nanos.saturating_add((half_lives as u64).saturating_mul(half_life_nanos))
178}
179
180#[inline]
185pub fn evaluate_spatiotemporal_exposure_window(
186 subject_span: (u64, u64),
187 contaminant_span: (u64, u64),
188) -> Option<(u64, u64)> {
189 let (sub_start, sub_end) = subject_span;
190 let (con_start, con_end) = contaminant_span;
191
192 if sub_start <= con_end && sub_end >= con_start {
194 let start = sub_start.max(con_start);
195 let end = sub_end.min(con_end);
196 Some((start, end))
197 } else {
198 None
199 }
200}
201
202#[inline]
205pub fn evaluate_temporal_containment(
206 child_span: (u64, u64),
207 parent_span: (u64, u64),
208) -> bool {
209 child_span.0 >= parent_span.0 && child_span.1 <= parent_span.1
210}
211
212#[inline]
214pub fn evaluate_spatial_coincidence(
215 pos_a: &[f64; 3],
216 pos_b: &[f64; 3],
217 tolerance_m: f64,
218) -> bool {
219 let dx = pos_a[0] - pos_b[0];
220 let dy = pos_a[1] - pos_b[1];
221 let dz = pos_a[2] - pos_b[2];
222 (dx * dx + dy * dy + dz * dz) <= (tolerance_m * tolerance_m)
223}
224
225pub fn compile_spatiotemporal_quin(
234 entity_did_hash: u64,
235 property_hash: u64,
236 opcode: u8,
237 timestamp_nanos: u64,
238 context_did_hash: u64,
239 lamport_clock: u32,
240) -> NQuin {
241 let predicate = ((property_hash & 0x007F_FFFF_FFFF_FFFF) << 8) | (opcode as u64);
242 let metadata = ((lamport_clock as u64 & 0x1FFF_FFFF) << 32) | (opcode as u64);
243 let parity = entity_did_hash ^ predicate ^ timestamp_nanos ^ context_did_hash;
244
245 NQuin {
246 subject: entity_did_hash,
247 predicate,
248 object: timestamp_nanos,
249 context: context_did_hash,
250 metadata,
251 parity,
252 }
253}
254
255#[cfg(test)]
256mod tests {
257 use super::*;
258
259 #[test]
260 fn test_light_travel_offset_and_source_time() {
261 let src = SpatiotemporalPoint::new(0.0, 0.0, 0.0, 7844, 1_000_000_000, 385.0);
262 let obs = SpatiotemporalPoint::new(SPEED_OF_LIGHT_M_S / 1000.0, 0.0, 0.0, 7844, 0, 385.0);
264
265 let dt_nanos = evaluate_light_travel_offset_nanos(&src, &obs);
266 assert_eq!(dt_nanos, 1_000_000); let obs_time = 2_000_000_000u64;
269 let deduced_src_time = evaluate_observed_source_time(obs_time, &src, &obs);
270 assert_eq!(deduced_src_time, 1_999_000_000u64);
271 }
272
273 #[test]
274 fn test_gravitational_and_kinematic_proper_time_dilation() {
275 let rate_sea_level = evaluate_proper_time_rate(0.0, 0.0);
277 assert!((rate_sea_level - 1.0).abs() < 1e-15);
278
279 let rate_summit = evaluate_proper_time_rate(2228.0, 0.0);
281 assert!(rate_summit > 1.0);
282 let fractional_advance = rate_summit - 1.0;
283 assert!(fractional_advance > 2.0e-13 && fractional_advance < 3.0e-13);
285
286 let drift_day_nanos = evaluate_proper_time_drift_nanos(2228.0, 0.0, 86400.0);
288 assert!(drift_day_nanos > 15.0 && drift_day_nanos < 25.0);
290 }
291
292 #[test]
293 fn test_quantum_proper_time_gradient() {
294 let base_nanos = 1_729_123_456_789_000_000u64;
295 let dilation_factor = 1.0000000000000002;
296 let target_nanos = evaluate_quantum_gradient_nanos(base_nanos, dilation_factor);
297 assert!(target_nanos >= base_nanos);
298 }
299
300 #[test]
301 fn test_persistence_horizon_and_allen_exposure() {
302 let app_time = 1_000_000_000u64;
303 let half_life = 40_000_000u64; let horizon = evaluate_persistence_horizon_nanos(app_time, half_life, 5); assert_eq!(horizon, 1_200_000_000);
306
307 let crop_span = (1_050_000_000, 1_300_000_000);
309 let residue_span = (app_time, horizon);
310 let exposure = evaluate_spatiotemporal_exposure_window(crop_span, residue_span);
311 assert_eq!(exposure, Some((1_050_000_000, 1_200_000_000)));
312
313 let late_crop = (1_250_000_000, 1_400_000_000);
315 let no_exposure = evaluate_spatiotemporal_exposure_window(late_crop, residue_span);
316 assert_eq!(no_exposure, None);
317 }
318
319 #[test]
320 fn test_temporal_mereology_containment() {
321 let host_lifespan = (1_000_000_000u64, 5_000_000_000u64);
322 let rbc_lifespan = (2_000_000_000u64, 2_120_000_000u64); assert!(evaluate_temporal_containment(rbc_lifespan, host_lifespan));
324
325 let out_of_bounds = (900_000_000u64, 2_000_000_000u64);
326 assert!(!evaluate_temporal_containment(out_of_bounds, host_lifespan));
327 }
328
329 #[test]
330 fn test_spatial_coincidence() {
331 let pos1 = [148.29, -35.78, 350.0];
332 let pos2 = [148.29, -35.78, 350.005]; assert!(evaluate_spatial_coincidence(&pos1, &pos2, 0.01)); assert!(!evaluate_spatial_coincidence(&pos1, &pos2, 0.001)); }
336
337 #[test]
338 fn test_compile_spatiotemporal_quin() {
339 let quin = compile_spatiotemporal_quin(
340 0x1234_5678,
341 0x9ABC_DEF0,
342 OP_SPATIOTEMPORAL_ANCHOR,
343 1_744_675_200_000_000_000,
344 0xFEED_FACE,
345 42,
346 );
347 assert_eq!(quin.subject, 0x1234_5678);
348 assert_eq!(quin.object, 1_744_675_200_000_000_000);
349 assert_eq!((quin.predicate & 0xFF) as u8, OP_SPATIOTEMPORAL_ANCHOR);
350 assert_eq!(
351 quin.parity,
352 quin.subject ^ quin.predicate ^ quin.object ^ quin.context
353 );
354 }
355}