oxirs-physics 0.2.4

Physics-informed digital twin simulation bridge for OxiRS
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! Conservation Laws, Physical Bounds, and Buckingham Pi Dimensional Analysis
//!
//! Provides:
//! - [`ConservationLaw`] trait + standard implementations
//!   (energy, momentum, mass, entropy).
//! - [`PhysicalBoundsValidator`] for range checks.
//! - [`BuckinghamPiAnalyzer`] implementing the Buckingham π theorem.

pub mod checkers;

pub use checkers::{
    ConservationReport, ConservationSuite, ConservationViolationDetail, EnergyConservationChecker,
    MassConservationChecker, MomentumConservationChecker, ViolationSeverity,
};

use crate::error::{PhysicsError, PhysicsResult};
use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// PhysState – thin wrapper around quantity map
// ─────────────────────────────────────────────────────────────────────────────

/// A snapshot of physical quantities at a single instant.
#[derive(Debug, Clone, Default)]
pub struct PhysState {
    /// Quantity name → value (SI).
    pub quantities: HashMap<String, f64>,
}

impl PhysState {
    /// Create an empty state.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert / update a quantity.
    pub fn set(&mut self, name: impl Into<String>, value: f64) {
        self.quantities.insert(name.into(), value);
    }

    /// Get a quantity, returning `None` if absent.
    pub fn get(&self, name: &str) -> Option<f64> {
        self.quantities.get(name).copied()
    }

    /// Get a quantity or return an error.
    pub fn require(&self, name: &str) -> PhysicsResult<f64> {
        self.quantities
            .get(name)
            .copied()
            .ok_or_else(|| PhysicsError::ConstraintViolation(format!("missing quantity: {name}")))
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// ConservationLaw trait
// ─────────────────────────────────────────────────────────────────────────────

/// A conservation law that can be checked against a pair of states.
pub trait ConservationLaw: Send + Sync {
    /// Name of the law (for reporting).
    fn name(&self) -> &str;

    /// Check the law given the state *before* (`initial`) and *after* (`final`)
    /// a process.
    fn check(&self, initial: &PhysState, final_state: &PhysState) -> PhysicsResult<()>;
}

// ─────────────────────────────────────────────────────────────────────────────
// Energy Conservation
// ─────────────────────────────────────────────────────────────────────────────

/// Checks that total mechanical energy is conserved within `tolerance`.
pub struct EnergyConservation {
    pub tolerance: f64,
}

impl EnergyConservation {
    pub fn new(tolerance: f64) -> Self {
        Self { tolerance }
    }

    fn total_energy(state: &PhysState) -> f64 {
        if let Some(e) = state.get("total_energy") {
            return e;
        }
        state.get("kinetic_energy").unwrap_or(0.0) + state.get("potential_energy").unwrap_or(0.0)
    }
}

impl ConservationLaw for EnergyConservation {
    fn name(&self) -> &str {
        "Energy Conservation"
    }

    fn check(&self, initial: &PhysState, final_state: &PhysState) -> PhysicsResult<()> {
        let e_i = Self::total_energy(initial);
        let e_f = Self::total_energy(final_state);
        if (e_f - e_i).abs() > self.tolerance {
            return Err(PhysicsError::ConservationViolation {
                law: self.name().to_string(),
                expected: e_i,
                actual: e_f,
            });
        }
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Momentum Conservation
// ─────────────────────────────────────────────────────────────────────────────

/// Checks conservation of linear and angular momentum.
pub struct MomentumConservation {
    pub tolerance: f64,
}

impl MomentumConservation {
    pub fn new(tolerance: f64) -> Self {
        Self { tolerance }
    }
}

impl ConservationLaw for MomentumConservation {
    fn name(&self) -> &str {
        "Momentum Conservation"
    }

    fn check(&self, initial: &PhysState, final_state: &PhysState) -> PhysicsResult<()> {
        for component in &["momentum_x", "momentum_y", "momentum_z"] {
            let p_i = initial.get(component).unwrap_or(0.0);
            let p_f = final_state.get(component).unwrap_or(0.0);
            if (p_f - p_i).abs() > self.tolerance {
                return Err(PhysicsError::ConservationViolation {
                    law: format!("{} ({})", self.name(), component),
                    expected: p_i,
                    actual: p_f,
                });
            }
        }
        let l_i = initial.get("angular_momentum").unwrap_or(0.0);
        let l_f = final_state.get("angular_momentum").unwrap_or(0.0);
        if (l_f - l_i).abs() > self.tolerance {
            return Err(PhysicsError::ConservationViolation {
                law: format!("{} (angular)", self.name()),
                expected: l_i,
                actual: l_f,
            });
        }
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Mass Conservation
// ─────────────────────────────────────────────────────────────────────────────

/// Checks that total mass does not change.
pub struct MassConservation {
    pub tolerance: f64,
}

impl MassConservation {
    pub fn new(tolerance: f64) -> Self {
        Self { tolerance }
    }
}

impl ConservationLaw for MassConservation {
    fn name(&self) -> &str {
        "Mass Conservation"
    }

    fn check(&self, initial: &PhysState, final_state: &PhysState) -> PhysicsResult<()> {
        let m_i = initial
            .get("total_mass")
            .or_else(|| initial.get("mass"))
            .unwrap_or(0.0);
        let m_f = final_state
            .get("total_mass")
            .or_else(|| final_state.get("mass"))
            .unwrap_or(0.0);
        if (m_f - m_i).abs() > self.tolerance {
            return Err(PhysicsError::ConservationViolation {
                law: self.name().to_string(),
                expected: m_i,
                actual: m_f,
            });
        }
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Entropy Law (2nd Law of Thermodynamics)
// ─────────────────────────────────────────────────────────────────────────────

/// Checks that entropy is non-decreasing: ΔS ≥ −`tolerance`.
pub struct EntropyLaw {
    pub tolerance: f64,
}

impl EntropyLaw {
    pub fn new(tolerance: f64) -> Self {
        Self { tolerance }
    }
}

impl ConservationLaw for EntropyLaw {
    fn name(&self) -> &str {
        "Second Law of Thermodynamics (Entropy Non-Decrease)"
    }

    fn check(&self, initial: &PhysState, final_state: &PhysState) -> PhysicsResult<()> {
        let s_i = initial.get("entropy").unwrap_or(0.0);
        let s_f = final_state.get("entropy").unwrap_or(0.0);
        if s_f - s_i < -self.tolerance {
            return Err(PhysicsError::ConservationViolation {
                law: self.name().to_string(),
                expected: s_i,
                actual: s_f,
            });
        }
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// PhysicalBoundsValidator
// ─────────────────────────────────────────────────────────────────────────────

/// Describes a physical bound on a named quantity.
#[derive(Debug, Clone)]
pub struct PhysicalBound {
    pub quantity: String,
    pub min: f64,
    pub max: f64,
    pub description: String,
}

/// A bound violation.
#[derive(Debug, Clone)]
pub struct BoundViolation {
    pub quantity: String,
    pub actual: f64,
    pub bound: PhysicalBound,
    /// "below_minimum" | "above_maximum"
    pub violation_kind: String,
}

/// Validates a set of physical quantities against registered bounds.
#[derive(Debug, Default)]
pub struct PhysicalBoundsValidator {
    bounds: Vec<PhysicalBound>,
}

impl PhysicalBoundsValidator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_bound(
        &mut self,
        quantity: impl Into<String>,
        min: f64,
        max: f64,
        description: impl Into<String>,
    ) {
        self.bounds.push(PhysicalBound {
            quantity: quantity.into(),
            min,
            max,
            description: description.into(),
        });
    }

    pub fn validate(&self, state: &HashMap<String, f64>) -> PhysicsResult<Vec<BoundViolation>> {
        let violations = self
            .bounds
            .iter()
            .filter_map(|bound| {
                state.get(&bound.quantity).and_then(|&actual| {
                    if actual < bound.min {
                        Some(BoundViolation {
                            quantity: bound.quantity.clone(),
                            actual,
                            bound: bound.clone(),
                            violation_kind: "below_minimum".to_string(),
                        })
                    } else if actual > bound.max {
                        Some(BoundViolation {
                            quantity: bound.quantity.clone(),
                            actual,
                            bound: bound.clone(),
                            violation_kind: "above_maximum".to_string(),
                        })
                    } else {
                        None
                    }
                })
            })
            .collect();
        Ok(violations)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Buckingham Pi Theorem
// ─────────────────────────────────────────────────────────────────────────────

/// SI base dimensions (exponents).  Order: [M, L, T, I, Θ, N, J].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dimensions {
    pub mass: i8,
    pub length: i8,
    pub time: i8,
    pub current: i8,
    pub temperature: i8,
    pub amount: i8,
    pub luminosity: i8,
}

impl Dimensions {
    pub const DIMENSIONLESS: Self = Self {
        mass: 0,
        length: 0,
        time: 0,
        current: 0,
        temperature: 0,
        amount: 0,
        luminosity: 0,
    };

    fn as_array(self) -> [i8; 7] {
        [
            self.mass,
            self.length,
            self.time,
            self.current,
            self.temperature,
            self.amount,
            self.luminosity,
        ]
    }

    fn is_zero(self) -> bool {
        self.as_array().iter().all(|&x| x == 0)
    }
}

/// A physical quantity used as input to the Buckingham π analyzer.
#[derive(Debug, Clone)]
pub struct PhysicalQuantity {
    pub name: String,
    pub value: f64,
    pub unit: String,
    pub dimensions: Dimensions,
}

impl PhysicalQuantity {
    pub fn new(
        name: impl Into<String>,
        value: f64,
        unit: impl Into<String>,
        dimensions: Dimensions,
    ) -> Self {
        Self {
            name: name.into(),
            value,
            unit: unit.into(),
            dimensions,
        }
    }
}

/// A dimensionless π group produced by the Buckingham π theorem.
#[derive(Debug, Clone)]
pub struct DimensionlessPi {
    pub label: String,
    pub quantity_indices: Vec<usize>,
    pub exponents: Vec<f64>,
    pub value: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Linear algebra helpers (clippy-clean, no raw index loops where avoidable)
// ─────────────────────────────────────────────────────────────────────────────

/// Row-reduce `matrix` (DIM rows × ncols columns) in-place and return the rank.
fn row_echelon_rank(matrix: &mut [Vec<f64>], ncols: usize) -> usize {
    let nrows = matrix.len();
    let mut rank = 0_usize;
    let mut pivot_col = 0_usize;
    let mut row = 0_usize;

    while row < nrows && pivot_col < ncols {
        // Find pivot in this column at or below `row`.
        let pivot_row = (row..nrows).find(|&r| matrix[r][pivot_col].abs() > 1e-12);
        if let Some(pr) = pivot_row {
            matrix.swap(row, pr);
            rank += 1;
            let pivot_val = matrix[row][pivot_col];
            // Normalise pivot row.
            matrix[row].iter_mut().for_each(|v| *v /= pivot_val);
            // Eliminate column entries in all other rows.
            for r in 0..nrows {
                if r != row {
                    let factor = matrix[r][pivot_col];
                    // borrow issue: collect target row first then update other row
                    let pivot_row_copy = matrix[row].clone();
                    matrix[r]
                        .iter_mut()
                        .zip(pivot_row_copy.iter())
                        .for_each(|(cell, &p)| *cell -= factor * p);
                }
            }
            row += 1;
        }
        pivot_col += 1;
    }
    rank
}

/// Return the matrix rank.
fn rank_of(matrix: &[Vec<f64>], ncols: usize) -> usize {
    let mut m = matrix.to_vec();
    row_echelon_rank(&mut m, ncols)
}

/// Return the pivot column indices after row reduction.
fn find_pivot_columns(matrix: &[Vec<f64>], ncols: usize, rank: usize) -> Vec<usize> {
    let nrows = matrix.len();
    let mut m = matrix.to_vec();
    let mut pivots = Vec::new();
    let mut pivot_col = 0_usize;
    let mut row = 0_usize;

    while row < nrows && pivot_col < ncols && pivots.len() < rank {
        let pivot_row = (row..nrows).find(|&r| m[r][pivot_col].abs() > 1e-12);
        if let Some(pr) = pivot_row {
            m.swap(row, pr);
            pivots.push(pivot_col);
            let pv = m[row][pivot_col];
            m[row].iter_mut().for_each(|v| *v /= pv);
            for r in 0..nrows {
                if r != row {
                    let f = m[r][pivot_col];
                    let pivot_copy = m[row].clone();
                    m[r].iter_mut()
                        .zip(pivot_copy.iter())
                        .for_each(|(cell, &p)| *cell -= f * p);
                }
            }
            row += 1;
        }
        pivot_col += 1;
    }
    pivots
}

fn build_pi_group(
    quantities: &[PhysicalQuantity],
    pivot_cols: &[usize],
    rem_idx: usize,
    dim_matrix: &[Vec<f64>],
    group_num: usize,
) -> DimensionlessPi {
    const DIM: usize = 7;
    let r = pivot_cols.len();

    // Build A (DIM × r) and b (DIM).
    let a: Vec<Vec<f64>> = (0..DIM)
        .map(|d| pivot_cols.iter().map(|&pc| dim_matrix[d][pc]).collect())
        .collect();
    let b: Vec<f64> = (0..DIM).map(|d| -dim_matrix[d][rem_idx]).collect();

    // Normal equations: (A^T A) x = A^T b.
    let mut ata: Vec<Vec<f64>> = vec![vec![0.0; r]; r];
    let mut atb: Vec<f64> = vec![0.0; r];
    for i in 0..r {
        for j in 0..r {
            ata[i][j] = (0..DIM).map(|d| a[d][i] * a[d][j]).sum();
        }
        atb[i] = (0..DIM).map(|d| a[d][i] * b[d]).sum();
    }

    let x = solve_linear_system(&ata, &atb);

    let mut indices = pivot_cols.to_vec();
    indices.push(rem_idx);
    let mut exponents = x;
    exponents.push(1.0);

    let value = indices
        .iter()
        .zip(exponents.iter())
        .map(|(&idx, &exp)| quantities[idx].value.powf(exp))
        .product::<f64>();

    DimensionlessPi {
        label: format!("π{group_num}"),
        quantity_indices: indices,
        exponents,
        value,
    }
}

/// Gaussian elimination with partial pivoting.
fn solve_linear_system(a: &[Vec<f64>], b: &[f64]) -> Vec<f64> {
    let n = b.len();
    if n == 0 {
        return Vec::new();
    }

    let mut m: Vec<Vec<f64>> = a.to_vec();
    let mut rhs: Vec<f64> = b.to_vec();

    for col in 0..n {
        // Partial pivot.
        if let Some(max_row) = (col..n).max_by(|&i, &j| {
            m[i][col]
                .abs()
                .partial_cmp(&m[j][col].abs())
                .unwrap_or(std::cmp::Ordering::Equal)
        }) {
            m.swap(col, max_row);
            rhs.swap(col, max_row);
        }
        let pv = m[col][col];
        if pv.abs() < 1e-14 {
            continue;
        }
        m[col].iter_mut().for_each(|v| *v /= pv);
        rhs[col] /= pv;

        for row in (col + 1)..n {
            let f = m[row][col];
            let pivot_copy = m[col].clone();
            m[row]
                .iter_mut()
                .zip(pivot_copy.iter())
                .for_each(|(cell, &p)| *cell -= f * p);
            rhs[row] -= f * rhs[col];
        }
    }

    // Back substitution.
    let mut x = vec![0.0_f64; n];
    for i in (0..n).rev() {
        x[i] = rhs[i] - ((i + 1)..n).map(|j| m[i][j] * x[j]).sum::<f64>();
    }
    x
}

fn is_dimensionless(quantities: &[PhysicalQuantity], pi: &DimensionlessPi) -> bool {
    let mut total = Dimensions::DIMENSIONLESS;
    for (&idx, &exp) in pi.quantity_indices.iter().zip(pi.exponents.iter()) {
        let d = quantities[idx].dimensions;
        let rounded = exp.round() as i8;
        total.mass += d.mass * rounded;
        total.length += d.length * rounded;
        total.time += d.time * rounded;
        total.current += d.current * rounded;
        total.temperature += d.temperature * rounded;
        total.amount += d.amount * rounded;
        total.luminosity += d.luminosity * rounded;
    }
    total.is_zero()
}

/// Analyzes a set of physical quantities and returns dimensionless π groups
/// using the Buckingham π theorem.
pub struct BuckinghamPiAnalyzer;

impl BuckinghamPiAnalyzer {
    pub fn analyze(quantities: &[PhysicalQuantity]) -> PhysicsResult<Vec<DimensionlessPi>> {
        if quantities.is_empty() {
            return Err(PhysicsError::ConstraintViolation(
                "no quantities provided for Buckingham Pi analysis".to_string(),
            ));
        }

        let n = quantities.len();
        const DIM: usize = 7;

        // Build dimensional matrix: DIM rows × n columns.
        let dim_matrix: Vec<Vec<f64>> = (0..DIM)
            .map(|i| {
                quantities
                    .iter()
                    .map(|q| q.dimensions.as_array()[i] as f64)
                    .collect()
            })
            .collect();

        let rank = rank_of(&dim_matrix, n);
        let num_pi = n.saturating_sub(rank);

        if num_pi == 0 {
            return Ok(Vec::new());
        }

        let pivot_cols = find_pivot_columns(&dim_matrix, n, rank);
        let remaining: Vec<usize> = (0..n).filter(|c| !pivot_cols.contains(c)).collect();

        let pis: Vec<DimensionlessPi> = remaining
            .iter()
            .enumerate()
            .map(|(k, &rem_idx)| {
                build_pi_group(quantities, &pivot_cols, rem_idx, &dim_matrix, k + 1)
            })
            .collect();

        for pi in &pis {
            if !is_dimensionless(quantities, pi) {
                return Err(PhysicsError::ConstraintViolation(
                    "Buckingham Pi group is not dimensionless (numerical error)".to_string(),
                ));
            }
        }

        Ok(pis)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn state_with(pairs: &[(&str, f64)]) -> PhysState {
        let mut s = PhysState::new();
        for &(k, v) in pairs {
            s.set(k, v);
        }
        s
    }

    // ── Energy Conservation ───────────────────────────────────────────────────

    #[test]
    fn energy_conservation_ok() {
        let law = EnergyConservation::new(1e-6);
        let s0 = state_with(&[("kinetic_energy", 100.0), ("potential_energy", 50.0)]);
        let s1 = state_with(&[("kinetic_energy", 140.0), ("potential_energy", 10.0)]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn energy_conservation_violated() {
        let law = EnergyConservation::new(1.0);
        let s0 = state_with(&[("total_energy", 100.0)]);
        let s1 = state_with(&[("total_energy", 200.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    // ── Momentum Conservation ─────────────────────────────────────────────────

    #[test]
    fn momentum_conservation_ok() {
        let law = MomentumConservation::new(1e-6);
        let s0 = state_with(&[
            ("momentum_x", 10.0),
            ("momentum_y", 5.0),
            ("momentum_z", 0.0),
        ]);
        let s1 = s0.clone();
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn momentum_conservation_violated() {
        let law = MomentumConservation::new(0.01);
        let s0 = state_with(&[("momentum_x", 10.0)]);
        let s1 = state_with(&[("momentum_x", 15.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    // ── Mass Conservation ─────────────────────────────────────────────────────

    #[test]
    fn mass_conservation_ok() {
        let law = MassConservation::new(1e-9);
        let s0 = state_with(&[("total_mass", 1.0)]);
        let s1 = state_with(&[("total_mass", 1.0 + 1e-12)]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn mass_conservation_violated() {
        let law = MassConservation::new(1e-6);
        let s0 = state_with(&[("mass", 2.0)]);
        let s1 = state_with(&[("mass", 3.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    // ── Entropy Law ───────────────────────────────────────────────────────────

    #[test]
    fn entropy_non_decreasing_ok() {
        let law = EntropyLaw::new(1e-9);
        let s0 = state_with(&[("entropy", 100.0)]);
        let s1 = state_with(&[("entropy", 105.0)]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn entropy_decrease_violates() {
        let law = EntropyLaw::new(1e-9);
        let s0 = state_with(&[("entropy", 100.0)]);
        let s1 = state_with(&[("entropy", 95.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    // ── PhysicalBoundsValidator ───────────────────────────────────────────────

    #[test]
    fn bounds_no_violation() {
        let mut v = PhysicalBoundsValidator::new();
        v.add_bound("temperature", 0.0, 1000.0, "Temperature in K");
        v.add_bound("pressure", 0.0, 1e8, "Pressure in Pa");

        let state: HashMap<String, f64> = [
            ("temperature".to_string(), 300.0),
            ("pressure".to_string(), 1e5),
        ]
        .into_iter()
        .collect();

        let violations = v.validate(&state).expect("should succeed");
        assert!(violations.is_empty());
    }

    #[test]
    fn bounds_below_minimum() {
        let mut v = PhysicalBoundsValidator::new();
        v.add_bound("temperature", 200.0, 1000.0, "Temperature must be >= 200 K");

        let state: HashMap<String, f64> = [("temperature".to_string(), 50.0)].into_iter().collect();

        let violations = v.validate(&state).expect("should succeed");
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].violation_kind, "below_minimum");
    }

    #[test]
    fn bounds_above_maximum() {
        let mut v = PhysicalBoundsValidator::new();
        v.add_bound("speed", 0.0, 300.0, "Speed <= 300 m/s");

        let state: HashMap<String, f64> = [("speed".to_string(), 400.0)].into_iter().collect();

        let violations = v.validate(&state).expect("should succeed");
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].violation_kind, "above_maximum");
    }

    // ── Buckingham Pi ─────────────────────────────────────────────────────────

    fn dim(mass: i8, length: i8, time: i8) -> Dimensions {
        Dimensions {
            mass,
            length,
            time,
            current: 0,
            temperature: 0,
            amount: 0,
            luminosity: 0,
        }
    }

    #[test]
    fn buckingham_pi_pendulum() {
        // n=3, rank=2 → 1 π group.
        let quantities = vec![
            PhysicalQuantity::new("T_period", 2.0, "s", dim(0, 0, 1)),
            PhysicalQuantity::new("L_length", 1.0, "m", dim(0, 1, 0)),
            PhysicalQuantity::new("g_accel", 9.81, "m/s^2", dim(0, 1, -2)),
        ];

        let pis = BuckinghamPiAnalyzer::analyze(&quantities).expect("should succeed");
        assert_eq!(pis.len(), 1, "expected 1 dimensionless group for pendulum");
    }

    #[test]
    fn buckingham_pi_empty_input_is_error() {
        let result = BuckinghamPiAnalyzer::analyze(&[]);
        assert!(result.is_err());
    }

    // ── Additional PhysState tests ────────────────────────────────────────────

    #[test]
    fn test_physstate_set_and_get() {
        let mut s = PhysState::new();
        s.set("velocity_x", 3.0);
        s.set("velocity_y", 4.0);
        assert_eq!(s.get("velocity_x"), Some(3.0));
        assert_eq!(s.get("velocity_y"), Some(4.0));
        assert_eq!(s.get("velocity_z"), None);
    }

    #[test]
    fn test_physstate_require_missing_is_error() {
        let s = PhysState::new();
        let result = s.require("mass");
        assert!(result.is_err(), "require on missing key should error");
    }

    #[test]
    fn test_physstate_require_present_ok() {
        let mut s = PhysState::new();
        s.set("pressure", 101325.0);
        let v = s.require("pressure").expect("should succeed");
        assert!((v - 101325.0).abs() < 1e-6);
    }

    #[test]
    fn test_physstate_overwrite() {
        let mut s = PhysState::new();
        s.set("temp", 300.0);
        s.set("temp", 350.0);
        assert_eq!(s.get("temp"), Some(350.0));
    }

    // ── EnergyConservation (trait impl) tests ─────────────────────────────────

    #[test]
    fn energy_law_pass_total_energy() {
        let law = EnergyConservation::new(1.0);
        let s0 = state_with(&[("total_energy", 500.0)]);
        let s1 = state_with(&[("total_energy", 500.5)]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn energy_law_fail_large_change() {
        let law = EnergyConservation::new(0.1);
        let s0 = state_with(&[("total_energy", 1000.0)]);
        let s1 = state_with(&[("total_energy", 2000.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    #[test]
    fn energy_law_name_correct() {
        let law = EnergyConservation::new(1.0);
        assert_eq!(law.name(), "Energy Conservation");
    }

    // ── MomentumConservation (trait impl) tests ───────────────────────────────

    #[test]
    fn momentum_law_pass() {
        let law = MomentumConservation::new(0.01);
        let s0 = state_with(&[
            ("momentum_x", 5.0),
            ("momentum_y", 0.0),
            ("momentum_z", 0.0),
        ]);
        let s1 = state_with(&[
            ("momentum_x", 5.0),
            ("momentum_y", 0.0),
            ("momentum_z", 0.0),
        ]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn momentum_law_fail() {
        let law = MomentumConservation::new(0.001);
        let s0 = state_with(&[("momentum_x", 10.0)]);
        let s1 = state_with(&[("momentum_x", 15.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    #[test]
    fn momentum_law_name_correct() {
        let law = MomentumConservation::new(0.1);
        assert_eq!(law.name(), "Momentum Conservation");
    }

    // ── MassConservation (trait impl) tests ──────────────────────────────────

    #[test]
    fn mass_law_pass() {
        let law = MassConservation::new(1e-6);
        let s0 = state_with(&[("mass", 5.0)]);
        let s1 = state_with(&[("mass", 5.0 + 1e-8)]);
        assert!(law.check(&s0, &s1).is_ok());
    }

    #[test]
    fn mass_law_fail() {
        let law = MassConservation::new(1e-6);
        let s0 = state_with(&[("mass", 1.0)]);
        let s1 = state_with(&[("mass", 3.0)]);
        assert!(law.check(&s0, &s1).is_err());
    }

    #[test]
    fn mass_law_name_correct() {
        let law = MassConservation::new(1e-6);
        assert_eq!(law.name(), "Mass Conservation");
    }

    // ── PhysicalBoundsValidator tests ─────────────────────────────────────────

    #[test]
    fn bounds_multiple_violations() {
        let mut v = PhysicalBoundsValidator::new();
        v.add_bound("temperature", 200.0, 1000.0, "K");
        v.add_bound("pressure", 0.0, 1e6, "Pa");
        v.add_bound("speed", 0.0, 300.0, "m/s");

        let state: HashMap<String, f64> = [
            ("temperature".to_string(), 50.0), // below min
            ("pressure".to_string(), 2e6),     // above max
            ("speed".to_string(), 100.0),      // ok
        ]
        .into_iter()
        .collect();

        let violations = v.validate(&state).expect("should succeed");
        assert_eq!(violations.len(), 2, "expected 2 violations");
    }

    #[test]
    fn bounds_empty_state_no_violations() {
        let mut v = PhysicalBoundsValidator::new();
        v.add_bound("temperature", 0.0, 1000.0, "K");
        let state: HashMap<String, f64> = HashMap::new();
        let violations = v.validate(&state).expect("should succeed");
        // Missing quantities are not violations (no value present)
        assert!(
            violations.is_empty(),
            "missing quantities should not trigger violations"
        );
    }

    // ── Buckingham Pi tests ───────────────────────────────────────────────────

    #[test]
    fn buckingham_pi_reynolds_number() {
        // Re = ρ*v*L/μ — 4 quantities, rank = 3 → 1 pi group
        fn dim(mass: i8, length: i8, time: i8) -> Dimensions {
            Dimensions {
                mass,
                length,
                time,
                current: 0,
                temperature: 0,
                amount: 0,
                luminosity: 0,
            }
        }
        let quantities = vec![
            PhysicalQuantity::new("rho", 1000.0, "kg/m3", dim(1, -3, 0)),
            PhysicalQuantity::new("v", 1.0, "m/s", dim(0, 1, -1)),
            PhysicalQuantity::new("L", 0.1, "m", dim(0, 1, 0)),
            PhysicalQuantity::new("mu", 1e-3, "Pa-s", dim(1, -1, -1)),
        ];
        let pis = BuckinghamPiAnalyzer::analyze(&quantities).expect("should succeed");
        assert_eq!(pis.len(), 1, "Reynolds number → 1 pi group");
    }

    #[test]
    fn buckingham_pi_two_groups() {
        // 5 quantities, rank = 3 → 2 pi groups
        fn dim(mass: i8, length: i8, time: i8) -> Dimensions {
            Dimensions {
                mass,
                length,
                time,
                current: 0,
                temperature: 0,
                amount: 0,
                luminosity: 0,
            }
        }
        let quantities = vec![
            PhysicalQuantity::new("rho", 1000.0, "kg/m3", dim(1, -3, 0)),
            PhysicalQuantity::new("v", 1.0, "m/s", dim(0, 1, -1)),
            PhysicalQuantity::new("L", 0.1, "m", dim(0, 1, 0)),
            PhysicalQuantity::new("mu", 1e-3, "Pa-s", dim(1, -1, -1)),
            PhysicalQuantity::new("dp", 100.0, "Pa", dim(1, -1, -2)), // pressure gradient
        ];
        let pis = BuckinghamPiAnalyzer::analyze(&quantities).expect("should succeed");
        assert_eq!(pis.len(), 2, "5 quantities - rank 3 = 2 pi groups");
    }
}