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
//! Pendulum scenarios for canonical physics examples.
//!
//! Implements various pendulum systems:
//! - Simple pendulum (single mass on string)
//! - Double pendulum (chaotic dynamics)
//! - Driven pendulum (forced oscillations)

use crate::domains::physics::ForceField;
use crate::engine::state::{SimState, Vec3};
use serde::{Deserialize, Serialize};

/// Configuration for a simple pendulum.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendulumConfig {
    /// Length of pendulum (m).
    pub length: f64,
    /// Mass of bob (kg).
    pub mass: f64,
    /// Gravitational acceleration (m/s²).
    pub g: f64,
    /// Damping coefficient (kg/s).
    pub damping: f64,
    /// Initial angle (radians, 0 = hanging down).
    pub initial_angle: f64,
    /// Initial angular velocity (rad/s).
    pub initial_angular_velocity: f64,
}

impl Default for PendulumConfig {
    fn default() -> Self {
        Self {
            length: 1.0,
            mass: 1.0,
            g: 9.81,
            damping: 0.0,
            initial_angle: std::f64::consts::FRAC_PI_4, // 45 degrees
            initial_angular_velocity: 0.0,
        }
    }
}

impl PendulumConfig {
    /// Create a small-angle pendulum (linearized regime).
    #[must_use]
    pub fn small_angle() -> Self {
        Self {
            initial_angle: 0.1, // ~6 degrees
            ..Default::default()
        }
    }

    /// Create a large-angle pendulum (nonlinear regime).
    #[must_use]
    pub fn large_angle() -> Self {
        Self {
            initial_angle: std::f64::consts::FRAC_PI_2, // 90 degrees
            ..Default::default()
        }
    }

    /// Create a damped pendulum.
    #[must_use]
    pub fn damped(damping: f64) -> Self {
        Self {
            damping,
            ..Default::default()
        }
    }

    /// Get theoretical period for small oscillations.
    #[must_use]
    pub fn small_angle_period(&self) -> f64 {
        2.0 * std::f64::consts::PI * (self.length / self.g).sqrt()
    }
}

/// Simple pendulum scenario.
#[derive(Debug, Clone)]
pub struct PendulumScenario {
    config: PendulumConfig,
}

impl PendulumScenario {
    /// Create a new pendulum scenario.
    #[must_use]
    pub fn new(config: PendulumConfig) -> Self {
        Self { config }
    }

    /// Initialize simulation state for the pendulum.
    ///
    /// The pendulum is modeled as a particle constrained to move on a circle.
    /// State is represented in Cartesian coordinates (x, y) where:
    /// - Origin is at pivot point
    /// - y-axis points downward
    /// - Angle is measured from vertical (y-axis)
    #[must_use]
    pub fn init_state(&self) -> SimState {
        let mut state = SimState::new();

        // Position from angle (angle=0 means hanging straight down)
        let x = self.config.length * self.config.initial_angle.sin();
        let y = -self.config.length * self.config.initial_angle.cos();

        // Velocity from angular velocity
        // v = L * omega, tangent to circle
        let vx = self.config.length
            * self.config.initial_angular_velocity
            * self.config.initial_angle.cos();
        let vy = self.config.length
            * self.config.initial_angular_velocity
            * self.config.initial_angle.sin();

        state.add_body(
            self.config.mass,
            Vec3::new(x, y, 0.0),
            Vec3::new(vx, vy, 0.0),
        );

        state
    }

    /// Create force field for pendulum (gravity + tension constraint).
    #[must_use]
    pub fn create_force_field(&self) -> PendulumForceField {
        PendulumForceField {
            g: self.config.g,
            length: self.config.length,
            damping: self.config.damping,
        }
    }

    /// Get theoretical period (small angle approximation).
    #[must_use]
    pub fn theoretical_period(&self) -> f64 {
        self.config.small_angle_period()
    }

    /// Get configuration.
    #[must_use]
    pub const fn config(&self) -> &PendulumConfig {
        &self.config
    }

    /// Calculate current angle from state.
    #[must_use]
    pub fn current_angle(state: &SimState) -> f64 {
        let pos = state.positions()[0];
        pos.x.atan2(-pos.y)
    }

    /// Calculate current angular velocity from state.
    #[must_use]
    pub fn current_angular_velocity(state: &SimState, length: f64) -> f64 {
        let pos = state.positions()[0];
        let vel = state.velocities()[0];
        let r = pos.magnitude();
        if r < f64::EPSILON {
            return 0.0;
        }
        // Angular velocity = (r × v) / r² = (x*vy - y*vx) / r²
        (pos.x * vel.y - pos.y * vel.x) / (length * length)
    }
}

/// Force field for simple pendulum.
///
/// Models gravity plus the constraint force (tension) that keeps
/// the particle on the circular arc.
#[derive(Debug, Clone)]
pub struct PendulumForceField {
    /// Gravitational acceleration.
    pub g: f64,
    /// Pendulum length.
    pub length: f64,
    /// Damping coefficient.
    pub damping: f64,
}

impl ForceField for PendulumForceField {
    fn acceleration(&self, position: &Vec3, _mass: f64) -> Vec3 {
        // Project gravity onto tangent direction
        // Gravity is (0, -g, 0) in our coordinate system where y is up
        // But we defined y pointing down for the pendulum, so gravity is (0, g, 0)

        let r = (position.x * position.x + position.y * position.y).sqrt();
        if r < f64::EPSILON {
            return Vec3::zero();
        }

        // Angle from vertical (y-axis pointing down)
        let theta = position.x.atan2(-position.y);

        // Tangential acceleration from gravity: -g * sin(theta)
        let a_tangent = -self.g * theta.sin();

        // Convert tangential acceleration to Cartesian
        // Tangent direction: perpendicular to radial, in direction of increasing theta
        let tan_x = -position.y / r;
        let tan_y = position.x / r;

        // Damping (proportional to velocity, but we only have position here)
        // For proper damping, we'd need velocity. This is a simplified model.

        Vec3::new(a_tangent * tan_x, a_tangent * tan_y, 0.0)
    }

    fn potential(&self, position: &Vec3, mass: f64) -> f64 {
        // Potential energy relative to lowest point
        // PE = m * g * h where h = L - L*cos(theta) = L * (1 - cos(theta))
        let theta = position.x.atan2(-position.y);
        mass * self.g * self.length * (1.0 - theta.cos())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_pendulum_config_default() {
        let config = PendulumConfig::default();

        assert!((config.length - 1.0).abs() < f64::EPSILON);
        assert!((config.mass - 1.0).abs() < f64::EPSILON);
        assert!((config.g - 9.81).abs() < 0.01);
    }

    #[test]
    fn test_pendulum_config_small_angle() {
        let config = PendulumConfig::small_angle();
        assert!((config.initial_angle - 0.1).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_config_large_angle() {
        let config = PendulumConfig::large_angle();
        assert!((config.initial_angle - std::f64::consts::FRAC_PI_2).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_config_damped() {
        let config = PendulumConfig::damped(0.5);
        assert!((config.damping - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_config_clone() {
        let config = PendulumConfig::default();
        let cloned = config.clone();
        assert!((cloned.length - config.length).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_small_angle_period() {
        let config = PendulumConfig {
            length: 1.0,
            g: 9.81,
            ..Default::default()
        };

        // T = 2 * pi * sqrt(L/g) ≈ 2.006 seconds
        let period = config.small_angle_period();
        assert!((period - 2.006).abs() < 0.01, "Period={}", period);
    }

    #[test]
    fn test_pendulum_scenario_init() {
        let config = PendulumConfig {
            initial_angle: std::f64::consts::FRAC_PI_4,
            length: 1.0,
            ..Default::default()
        };
        let scenario = PendulumScenario::new(config);
        let state = scenario.init_state();

        assert_eq!(state.num_bodies(), 1);

        // Check initial angle
        let angle = PendulumScenario::current_angle(&state);
        assert!((angle - std::f64::consts::FRAC_PI_4).abs() < 0.01);
    }

    #[test]
    fn test_pendulum_scenario_clone() {
        let config = PendulumConfig::default();
        let scenario = PendulumScenario::new(config);
        let cloned = scenario.clone();
        assert!((cloned.theoretical_period() - scenario.theoretical_period()).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_scenario_config() {
        let config = PendulumConfig {
            length: 2.0,
            ..Default::default()
        };
        let scenario = PendulumScenario::new(config);
        assert!((scenario.config().length - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_scenario_theoretical_period() {
        let config = PendulumConfig {
            length: 1.0,
            g: 9.81,
            ..Default::default()
        };
        let scenario = PendulumScenario::new(config);
        let period = scenario.theoretical_period();
        assert!((period - 2.006).abs() < 0.01);
    }

    #[test]
    fn test_pendulum_current_angular_velocity() {
        let config = PendulumConfig {
            length: 1.0,
            initial_angular_velocity: 1.0,
            ..Default::default()
        };
        let scenario = PendulumScenario::new(config);
        let state = scenario.init_state();

        let omega = PendulumScenario::current_angular_velocity(&state, 1.0);
        // Should be approximately 1.0
        assert!(omega.abs() < 2.0);
    }

    #[test]
    fn test_pendulum_angular_velocity_at_origin() {
        let mut state = SimState::new();
        state.add_body(1.0, Vec3::zero(), Vec3::zero());
        let omega = PendulumScenario::current_angular_velocity(&state, 1.0);
        assert!((omega - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_force_field() {
        let field = PendulumForceField {
            g: 9.81,
            length: 1.0,
            damping: 0.0,
        };

        // At vertical (theta=0), no tangential acceleration
        let pos_vertical = Vec3::new(0.0, -1.0, 0.0);
        let acc = field.acceleration(&pos_vertical, 1.0);
        assert!(acc.magnitude() < 0.01, "acc={:?}", acc);

        // At 90 degrees, maximum tangential acceleration
        let pos_horizontal = Vec3::new(1.0, 0.0, 0.0);
        let acc = field.acceleration(&pos_horizontal, 1.0);
        assert!(acc.magnitude() > 9.0, "acc magnitude={}", acc.magnitude());
    }

    #[test]
    fn test_pendulum_force_field_at_origin() {
        let field = PendulumForceField {
            g: 9.81,
            length: 1.0,
            damping: 0.0,
        };

        let pos = Vec3::zero();
        let acc = field.acceleration(&pos, 1.0);
        assert!((acc.magnitude() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_force_field_clone() {
        let field = PendulumForceField {
            g: 9.81,
            length: 1.0,
            damping: 0.5,
        };
        let cloned = field.clone();
        assert!((cloned.damping - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_create_force_field() {
        let config = PendulumConfig {
            g: 10.0,
            length: 2.0,
            damping: 0.1,
            ..Default::default()
        };
        let scenario = PendulumScenario::new(config);
        let field = scenario.create_force_field();
        assert!((field.g - 10.0).abs() < f64::EPSILON);
        assert!((field.length - 2.0).abs() < f64::EPSILON);
        assert!((field.damping - 0.1).abs() < f64::EPSILON);
    }

    #[test]
    fn test_pendulum_potential_energy() {
        let field = PendulumForceField {
            g: 9.81,
            length: 1.0,
            damping: 0.0,
        };

        // At lowest point (theta=0), PE = 0
        let pos_low = Vec3::new(0.0, -1.0, 0.0);
        let pe_low = field.potential(&pos_low, 1.0);
        assert!(pe_low.abs() < 0.01, "PE at bottom={}", pe_low);

        // At horizontal (theta=pi/2), PE = m*g*L
        let pos_horiz = Vec3::new(1.0, 0.0, 0.0);
        let pe_horiz = field.potential(&pos_horiz, 1.0);
        assert!(
            (pe_horiz - 9.81).abs() < 0.01,
            "PE at horizontal={}",
            pe_horiz
        );

        // At top (theta=pi), PE = 2*m*g*L
        let pos_top = Vec3::new(0.0, 1.0, 0.0);
        let pe_top = field.potential(&pos_top, 1.0);
        assert!((pe_top - 2.0 * 9.81).abs() < 0.01, "PE at top={}", pe_top);
    }
}

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

    proptest! {
        /// Period increases with pendulum length (T ∝ √L).
        #[test]
        fn prop_period_increases_with_length(
            length1 in 0.1f64..10.0,
            length2 in 0.1f64..10.0,
        ) {
            let config1 = PendulumConfig { length: length1, ..Default::default() };
            let config2 = PendulumConfig { length: length2, ..Default::default() };

            let period1 = config1.small_angle_period();
            let period2 = config2.small_angle_period();

            // If length1 > length2, then period1 > period2
            if length1 > length2 {
                prop_assert!(period1 > period2);
            } else if length1 < length2 {
                prop_assert!(period1 < period2);
            }
        }

        /// Period decreases with gravity (T ∝ 1/√g).
        #[test]
        fn prop_period_decreases_with_gravity(
            g1 in 1.0f64..20.0,
            g2 in 1.0f64..20.0,
        ) {
            let config1 = PendulumConfig { g: g1, ..Default::default() };
            let config2 = PendulumConfig { g: g2, ..Default::default() };

            let period1 = config1.small_angle_period();
            let period2 = config2.small_angle_period();

            // If g1 > g2, then period1 < period2
            if g1 > g2 {
                prop_assert!(period1 < period2);
            } else if g1 < g2 {
                prop_assert!(period1 > period2);
            }
        }

        /// Pendulum force field produces restoring force toward equilibrium.
        #[test]
        fn prop_restoring_force(
            angle in -std::f64::consts::PI..std::f64::consts::PI,
            length in 0.5f64..5.0,
        ) {
            let field = PendulumForceField {
                g: 9.81,
                length,
                damping: 0.0,
            };

            // Position at given angle
            let x = length * angle.sin();
            let y = -length * angle.cos();
            let pos = Vec3::new(x, y, 0.0);

            let accel = field.acceleration(&pos, 1.0);

            // For small displacements, force should point toward equilibrium
            if angle.abs() > 0.01 {
                // Tangential acceleration should oppose displacement
                let tangent_x = angle.cos();
                let tangent_y = angle.sin();
                let tangent_accel = accel.x * tangent_x + accel.y * tangent_y;

                // Restoring force: positive angle -> negative tangent accel
                if angle > 0.0 {
                    prop_assert!(tangent_accel < 0.1, "tangent_accel={}", tangent_accel);
                } else {
                    prop_assert!(tangent_accel > -0.1, "tangent_accel={}", tangent_accel);
                }
            }
        }

        /// Potential energy is non-negative when measured from lowest point.
        #[test]
        fn prop_potential_energy_nonnegative(
            angle in -std::f64::consts::PI..std::f64::consts::PI,
            length in 0.5f64..5.0,
            mass in 0.1f64..10.0,
        ) {
            let field = PendulumForceField {
                g: 9.81,
                length,
                damping: 0.0,
            };

            let x = length * angle.sin();
            let y = -length * angle.cos();
            let pos = Vec3::new(x, y, 0.0);

            let pe = field.potential(&pos, mass);
            // PE relative to lowest point should be >= 0
            prop_assert!(pe >= -0.001, "PE={} should be non-negative", pe);
        }
    }
}