aprender-simulate 0.30.0

Unified Simulation Engine for the Sovereign AI Stack
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Simulation state management.
//!
//! Implements the world state with:
//! - Entity positions and velocities
//! - Mass properties
//! - Energy computation
//! - Constraint tracking

use crate::error::SimResult;
use serde::{Deserialize, Serialize};

/// 3D vector for positions and velocities.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Vec3 {
    /// X component.
    pub x: f64,
    /// Y component.
    pub y: f64,
    /// Z component.
    pub z: f64,
}

impl Vec3 {
    /// Create a new vector.
    #[must_use]
    pub const fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Zero vector.
    #[must_use]
    pub const fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }

    /// Magnitude squared.
    #[must_use]
    pub fn magnitude_squared(&self) -> f64 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    /// Magnitude (length).
    #[must_use]
    pub fn magnitude(&self) -> f64 {
        self.magnitude_squared().sqrt()
    }

    /// Dot product.
    #[must_use]
    pub fn dot(&self, other: &Self) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Cross product.
    #[must_use]
    pub fn cross(&self, other: &Self) -> Self {
        Self {
            x: self.y * other.z - self.z * other.y,
            y: self.z * other.x - self.x * other.z,
            z: self.x * other.y - self.y * other.x,
        }
    }

    /// Normalize to unit vector.
    #[must_use]
    pub fn normalize(&self) -> Self {
        let mag = self.magnitude();
        if mag < f64::EPSILON {
            Self::zero()
        } else {
            Self {
                x: self.x / mag,
                y: self.y / mag,
                z: self.z / mag,
            }
        }
    }

    /// Scale by scalar.
    #[must_use]
    pub fn scale(&self, s: f64) -> Self {
        Self {
            x: self.x * s,
            y: self.y * s,
            z: self.z * s,
        }
    }

    /// Check if all components are finite.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // is_finite not const
    pub fn is_finite(&self) -> bool {
        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
    }
}

impl std::ops::Add for Vec3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

impl std::ops::Sub for Vec3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

impl std::ops::Mul<f64> for Vec3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        self.scale(rhs)
    }
}

impl std::ops::Neg for Vec3 {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self {
            x: -self.x,
            y: -self.y,
            z: -self.z,
        }
    }
}

/// Simulation event for state updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SimEvent {
    /// Add a body to the simulation.
    AddBody {
        /// Body mass.
        mass: f64,
        /// Initial position.
        position: Vec3,
        /// Initial velocity.
        velocity: Vec3,
    },
    /// Apply force to a body.
    ApplyForce {
        /// Body index.
        body_index: usize,
        /// Force vector.
        force: Vec3,
    },
    /// Set body position.
    SetPosition {
        /// Body index.
        body_index: usize,
        /// New position.
        position: Vec3,
    },
    /// Set body velocity.
    SetVelocity {
        /// Body index.
        body_index: usize,
        /// New velocity.
        velocity: Vec3,
    },
    /// Custom event.
    Custom {
        /// Event name.
        name: String,
        /// Serialized data.
        data: Vec<u8>,
    },
}

/// Simulation state.
///
/// Contains all state variables needed to reproduce the simulation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SimState {
    /// Body masses.
    masses: Vec<f64>,
    /// Body positions.
    positions: Vec<Vec3>,
    /// Body velocities.
    velocities: Vec<Vec3>,
    /// Accumulated forces (cleared each step).
    forces: Vec<Vec3>,
    /// Active constraints and their violations.
    constraints: Vec<(String, f64)>,
    /// Potential energy (set by domain engine).
    potential_energy: f64,
}

impl SimState {
    /// Create a new empty state.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Get number of bodies.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // Vec::len not const in older Rust
    pub fn num_bodies(&self) -> usize {
        self.masses.len()
    }

    /// Add a body to the simulation.
    pub fn add_body(&mut self, mass: f64, position: Vec3, velocity: Vec3) {
        self.masses.push(mass);
        self.positions.push(position);
        self.velocities.push(velocity);
        self.forces.push(Vec3::zero());
    }

    /// Get body masses.
    #[must_use]
    pub fn masses(&self) -> &[f64] {
        &self.masses
    }

    /// Get body positions.
    #[must_use]
    pub fn positions(&self) -> &[Vec3] {
        &self.positions
    }

    /// Get mutable body positions.
    #[must_use]
    pub fn positions_mut(&mut self) -> &mut [Vec3] {
        &mut self.positions
    }

    /// Get body velocities.
    #[must_use]
    pub fn velocities(&self) -> &[Vec3] {
        &self.velocities
    }

    /// Get mutable body velocities.
    #[must_use]
    pub fn velocities_mut(&mut self) -> &mut [Vec3] {
        &mut self.velocities
    }

    /// Get body forces.
    #[must_use]
    pub fn forces(&self) -> &[Vec3] {
        &self.forces
    }

    /// Get mutable body forces.
    #[must_use]
    pub fn forces_mut(&mut self) -> &mut [Vec3] {
        &mut self.forces
    }

    /// Set position for a body.
    ///
    /// # Panics
    ///
    /// Panics if index is out of bounds.
    pub fn set_position(&mut self, index: usize, position: Vec3) {
        self.positions[index] = position;
    }

    /// Set velocity for a body.
    ///
    /// # Panics
    ///
    /// Panics if index is out of bounds.
    pub fn set_velocity(&mut self, index: usize, velocity: Vec3) {
        self.velocities[index] = velocity;
    }

    /// Apply force to a body.
    ///
    /// # Panics
    ///
    /// Panics if index is out of bounds.
    pub fn apply_force(&mut self, index: usize, force: Vec3) {
        self.forces[index] = self.forces[index] + force;
    }

    /// Clear all forces (called at start of each step).
    pub fn clear_forces(&mut self) {
        for f in &mut self.forces {
            *f = Vec3::zero();
        }
    }

    /// Set potential energy (called by domain engine).
    #[allow(clippy::missing_const_for_fn)] // Mutable const not stable
    pub fn set_potential_energy(&mut self, energy: f64) {
        self.potential_energy = energy;
    }

    /// Get total kinetic energy.
    #[must_use]
    pub fn kinetic_energy(&self) -> f64 {
        self.masses
            .iter()
            .zip(&self.velocities)
            .map(|(m, v)| 0.5 * m * v.magnitude_squared())
            .sum()
    }

    /// Get potential energy.
    #[must_use]
    pub const fn potential_energy(&self) -> f64 {
        self.potential_energy
    }

    /// Get total energy (kinetic + potential).
    #[must_use]
    pub fn total_energy(&self) -> f64 {
        self.kinetic_energy() + self.potential_energy
    }

    /// Add a constraint violation.
    pub fn add_constraint(&mut self, name: impl Into<String>, violation: f64) {
        self.constraints.push((name.into(), violation));
    }

    /// Clear all constraints.
    pub fn clear_constraints(&mut self) {
        self.constraints.clear();
    }

    /// Get constraint violations.
    pub fn constraint_violations(&self) -> impl Iterator<Item = (String, f64)> + '_ {
        self.constraints.iter().cloned()
    }

    /// Check if all state values are finite.
    #[must_use]
    pub fn all_finite(&self) -> bool {
        self.positions.iter().all(Vec3::is_finite)
            && self.velocities.iter().all(Vec3::is_finite)
            && self.masses.iter().all(|m| m.is_finite())
    }

    /// Apply an event to the state.
    ///
    /// # Errors
    ///
    /// Returns error if event cannot be applied.
    pub fn apply_event(&mut self, event: &SimEvent) -> SimResult<()> {
        match event {
            SimEvent::AddBody {
                mass,
                position,
                velocity,
            } => {
                self.add_body(*mass, *position, *velocity);
            }
            SimEvent::ApplyForce { body_index, force } => {
                if *body_index < self.num_bodies() {
                    self.apply_force(*body_index, *force);
                }
            }
            SimEvent::SetPosition {
                body_index,
                position,
            } => {
                if *body_index < self.num_bodies() {
                    self.set_position(*body_index, *position);
                }
            }
            SimEvent::SetVelocity {
                body_index,
                velocity,
            } => {
                if *body_index < self.num_bodies() {
                    self.set_velocity(*body_index, *velocity);
                }
            }
            SimEvent::Custom { .. } => {
                // Custom events are handled by domain-specific code
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vec3_operations() {
        let v1 = Vec3::new(1.0, 2.0, 3.0);
        let v2 = Vec3::new(4.0, 5.0, 6.0);

        // Addition
        let sum = v1 + v2;
        assert!((sum.x - 5.0).abs() < f64::EPSILON);
        assert!((sum.y - 7.0).abs() < f64::EPSILON);
        assert!((sum.z - 9.0).abs() < f64::EPSILON);

        // Subtraction
        let diff = v2 - v1;
        assert!((diff.x - 3.0).abs() < f64::EPSILON);

        // Dot product
        let dot = v1.dot(&v2);
        assert!((dot - 32.0).abs() < f64::EPSILON); // 1*4 + 2*5 + 3*6 = 32

        // Cross product
        let cross = v1.cross(&v2);
        assert!((cross.x - (-3.0)).abs() < f64::EPSILON);
        assert!((cross.y - 6.0).abs() < f64::EPSILON);
        assert!((cross.z - (-3.0)).abs() < f64::EPSILON);

        // Magnitude
        let v = Vec3::new(3.0, 4.0, 0.0);
        assert!((v.magnitude() - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_normalize() {
        let v = Vec3::new(3.0, 4.0, 0.0);
        let n = v.normalize();

        assert!((n.magnitude() - 1.0).abs() < f64::EPSILON);
        assert!((n.x - 0.6).abs() < f64::EPSILON);
        assert!((n.y - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_add_body() {
        let mut state = SimState::new();

        state.add_body(1.0, Vec3::new(1.0, 0.0, 0.0), Vec3::zero());
        state.add_body(2.0, Vec3::new(0.0, 1.0, 0.0), Vec3::zero());

        assert_eq!(state.num_bodies(), 2);
        assert!((state.masses()[0] - 1.0).abs() < f64::EPSILON);
        assert!((state.masses()[1] - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_energy() {
        let mut state = SimState::new();

        // Body with mass 2, velocity (3, 0, 0) -> KE = 0.5 * 2 * 9 = 9
        state.add_body(2.0, Vec3::zero(), Vec3::new(3.0, 0.0, 0.0));

        assert!((state.kinetic_energy() - 9.0).abs() < f64::EPSILON);

        state.set_potential_energy(-5.0);
        assert!((state.total_energy() - 4.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_apply_event() {
        let mut state = SimState::new();

        let event = SimEvent::AddBody {
            mass: 1.0,
            position: Vec3::new(1.0, 2.0, 3.0),
            velocity: Vec3::zero(),
        };

        state.apply_event(&event).ok();
        assert_eq!(state.num_bodies(), 1);
        assert!((state.positions()[0].x - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_constraints() {
        let mut state = SimState::new();

        state.add_constraint("test1", 0.001);
        state.add_constraint("test2", -0.002);

        let violations: Vec<_> = state.constraint_violations().collect();
        assert_eq!(violations.len(), 2);

        state.clear_constraints();
        let violations: Vec<_> = state.constraint_violations().collect();
        assert!(violations.is_empty());
    }

    #[test]
    fn test_state_all_finite() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::new(1.0, 2.0, 3.0), Vec3::zero());

        assert!(state.all_finite());

        state.set_position(0, Vec3::new(f64::NAN, 0.0, 0.0));
        assert!(!state.all_finite());
    }

    #[test]
    fn test_vec3_scale() {
        let v = Vec3::new(1.0, 2.0, 3.0);
        let scaled = v.scale(2.0);
        assert!((scaled.x - 2.0).abs() < f64::EPSILON);
        assert!((scaled.y - 4.0).abs() < f64::EPSILON);
        assert!((scaled.z - 6.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_mul_scalar() {
        let v = Vec3::new(1.0, 2.0, 3.0);
        let scaled = v * 2.5;
        assert!((scaled.x - 2.5).abs() < f64::EPSILON);
        assert!((scaled.y - 5.0).abs() < f64::EPSILON);
        assert!((scaled.z - 7.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_neg() {
        let v = Vec3::new(1.0, -2.0, 3.0);
        let neg = -v;
        assert!((neg.x - (-1.0)).abs() < f64::EPSILON);
        assert!((neg.y - 2.0).abs() < f64::EPSILON);
        assert!((neg.z - (-3.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_is_finite() {
        let v1 = Vec3::new(1.0, 2.0, 3.0);
        assert!(v1.is_finite());

        let v2 = Vec3::new(f64::INFINITY, 0.0, 0.0);
        assert!(!v2.is_finite());

        let v3 = Vec3::new(0.0, f64::NEG_INFINITY, 0.0);
        assert!(!v3.is_finite());

        let v4 = Vec3::new(0.0, 0.0, f64::NAN);
        assert!(!v4.is_finite());
    }

    #[test]
    fn test_vec3_normalize_zero() {
        let v = Vec3::zero();
        let n = v.normalize();
        assert!((n.x).abs() < f64::EPSILON);
        assert!((n.y).abs() < f64::EPSILON);
        assert!((n.z).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_default() {
        let v = Vec3::default();
        assert!((v.x).abs() < f64::EPSILON);
        assert!((v.y).abs() < f64::EPSILON);
        assert!((v.z).abs() < f64::EPSILON);
    }

    #[test]
    fn test_vec3_partial_eq() {
        let v1 = Vec3::new(1.0, 2.0, 3.0);
        let v2 = Vec3::new(1.0, 2.0, 3.0);
        let v3 = Vec3::new(1.0, 2.0, 4.0);
        assert_eq!(v1, v2);
        assert_ne!(v1, v3);
    }

    #[test]
    fn test_vec3_magnitude_squared() {
        let v = Vec3::new(3.0, 4.0, 0.0);
        assert!((v.magnitude_squared() - 25.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_positions_mut() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::new(1.0, 2.0, 3.0), Vec3::zero());

        {
            let positions = state.positions_mut();
            positions[0] = Vec3::new(10.0, 20.0, 30.0);
        }

        assert!((state.positions()[0].x - 10.0).abs() < f64::EPSILON);
        assert!((state.positions()[0].y - 20.0).abs() < f64::EPSILON);
        assert!((state.positions()[0].z - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_velocities_mut() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::new(1.0, 2.0, 3.0));

        {
            let velocities = state.velocities_mut();
            velocities[0] = Vec3::new(5.0, 6.0, 7.0);
        }

        assert!((state.velocities()[0].x - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_forces_mut() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        {
            let forces = state.forces_mut();
            forces[0] = Vec3::new(100.0, 200.0, 300.0);
        }

        assert!((state.forces()[0].x - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_clear_forces() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());
        state.apply_force(0, Vec3::new(100.0, 200.0, 300.0));

        assert!((state.forces()[0].x - 100.0).abs() < f64::EPSILON);

        state.clear_forces();

        assert!((state.forces()[0].x).abs() < f64::EPSILON);
        assert!((state.forces()[0].y).abs() < f64::EPSILON);
        assert!((state.forces()[0].z).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_apply_event_apply_force() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::ApplyForce {
            body_index: 0,
            force: Vec3::new(10.0, 20.0, 30.0),
        };

        state.apply_event(&event).unwrap();
        assert!((state.forces()[0].x - 10.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_apply_event_apply_force_out_of_bounds() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::ApplyForce {
            body_index: 100, // out of bounds
            force: Vec3::new(10.0, 20.0, 30.0),
        };

        // Should not panic, just ignore
        state.apply_event(&event).unwrap();
    }

    #[test]
    fn test_state_apply_event_set_position() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::SetPosition {
            body_index: 0,
            position: Vec3::new(5.0, 6.0, 7.0),
        };

        state.apply_event(&event).unwrap();
        assert!((state.positions()[0].x - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_apply_event_set_position_out_of_bounds() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::SetPosition {
            body_index: 100, // out of bounds
            position: Vec3::new(5.0, 6.0, 7.0),
        };

        // Should not panic, just ignore
        state.apply_event(&event).unwrap();
    }

    #[test]
    fn test_state_apply_event_set_velocity() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::SetVelocity {
            body_index: 0,
            velocity: Vec3::new(8.0, 9.0, 10.0),
        };

        state.apply_event(&event).unwrap();
        assert!((state.velocities()[0].x - 8.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_apply_event_set_velocity_out_of_bounds() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());

        let event = SimEvent::SetVelocity {
            body_index: 100, // out of bounds
            velocity: Vec3::new(8.0, 9.0, 10.0),
        };

        // Should not panic, just ignore
        state.apply_event(&event).unwrap();
    }

    #[test]
    fn test_state_apply_event_custom() {
        let mut state = SimState::new();

        let event = SimEvent::Custom {
            name: "custom_event".to_string(),
            data: vec![1, 2, 3, 4],
        };

        // Custom events are no-ops at this level
        state.apply_event(&event).unwrap();
    }

    #[test]
    fn test_sim_event_clone() {
        let event = SimEvent::AddBody {
            mass: 1.0,
            position: Vec3::new(1.0, 2.0, 3.0),
            velocity: Vec3::zero(),
        };
        let cloned = event.clone();
        match cloned {
            SimEvent::AddBody { mass, .. } => assert!((mass - 1.0).abs() < f64::EPSILON),
            _ => panic!("unexpected event type"),
        }
    }

    #[test]
    fn test_sim_event_debug() {
        let event = SimEvent::AddBody {
            mass: 1.0,
            position: Vec3::zero(),
            velocity: Vec3::zero(),
        };
        let debug = format!("{:?}", event);
        assert!(debug.contains("AddBody"));
    }

    #[test]
    fn test_state_clone() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::new(1.0, 2.0, 3.0), Vec3::zero());
        state.set_potential_energy(-10.0);
        state.add_constraint("test", 0.5);

        let cloned = state.clone();
        assert_eq!(cloned.num_bodies(), 1);
        assert!((cloned.potential_energy() - (-10.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn test_state_debug() {
        let state = SimState::new();
        let debug = format!("{:?}", state);
        assert!(debug.contains("SimState"));
    }

    #[test]
    fn test_state_all_finite_with_infinity_in_velocity() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::new(f64::INFINITY, 0.0, 0.0));
        assert!(!state.all_finite());
    }

    #[test]
    fn test_state_all_finite_with_nan_in_mass() {
        let mut state = SimState::new();
        state.add_body(f64::NAN, Vec3::zero(), Vec3::zero());
        assert!(!state.all_finite());
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        /// Falsification: dot product is commutative.
        #[test]
        fn prop_dot_commutative(
            x1 in -1e6f64..1e6, y1 in -1e6f64..1e6, z1 in -1e6f64..1e6,
            x2 in -1e6f64..1e6, y2 in -1e6f64..1e6, z2 in -1e6f64..1e6,
        ) {
            let v1 = Vec3::new(x1, y1, z1);
            let v2 = Vec3::new(x2, y2, z2);

            let d1 = v1.dot(&v2);
            let d2 = v2.dot(&v1);

            prop_assert!((d1 - d2).abs() < 1e-9 * d1.abs().max(1.0));
        }

        /// Falsification: normalized vectors have unit length.
        #[test]
        fn prop_normalize_unit_length(
            x in -1e6f64..1e6, y in -1e6f64..1e6, z in -1e6f64..1e6,
        ) {
            let v = Vec3::new(x, y, z);

            // Skip zero vectors
            if v.magnitude() < f64::EPSILON {
                return Ok(());
            }

            let n = v.normalize();
            prop_assert!((n.magnitude() - 1.0).abs() < 1e-9);
        }

        /// Falsification: kinetic energy is non-negative.
        #[test]
        fn prop_kinetic_energy_nonnegative(
            mass in 0.1f64..1e6,
            vx in -1e3f64..1e3, vy in -1e3f64..1e3, vz in -1e3f64..1e3,
        ) {
            let mut state = SimState::new();
            state.add_body(mass, Vec3::zero(), Vec3::new(vx, vy, vz));

            prop_assert!(state.kinetic_energy() >= 0.0);
        }
    }
}