oxirs-physics 0.3.0

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
//! Buckingham π theorem — dimensional analysis for similarity parameters.
//!
//! Given a set of physically meaningful variables, the theorem states that any
//! dimensionally consistent equation relating n variables built from k
//! independent base dimensions can be rewritten in terms of n − k
//! dimensionless groups (π groups).
//!
//! This module provides:
//! - [`BaseUnit`] — the seven SI base dimensions.
//! - [`PhysicalVar`] — a named variable with its dimensional exponents.
//! - [`PiGroup`] — a dimensionless combination of the input variables.
//! - [`BuckinghamPi`] — runs the algorithm via Gaussian elimination over ℚ.
//! - [`BuckinghamPiError`] — failure modes.
//!
//! # Example
//!
//! ```
//! use oxirs_physics::constraints::buckingham_pi::{BuckinghamPi, BaseUnit, PhysicalVar};
//!
//! let mut length = PhysicalVar::new("L");
//! length.set_dimension(BaseUnit::Length, 1);
//! let mut time = PhysicalVar::new("T");
//! time.set_dimension(BaseUnit::Time, 1);
//! let mut velocity = PhysicalVar::new("v");
//! velocity.set_dimension(BaseUnit::Length, 1);
//! velocity.set_dimension(BaseUnit::Time, -1);
//!
//! // v = L / T → the single π group is v·T/L (dimensionless).
//! let pi_groups = BuckinghamPi::analyze(&[length, time, velocity]).unwrap();
//! assert_eq!(pi_groups.len(), 1);
//! ```

use std::collections::HashMap;

// ──────────────────────────────────────────────────────────────────────────────
// BaseUnit
// ──────────────────────────────────────────────────────────────────────────────

/// The seven SI base dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BaseUnit {
    /// Length — metre (m).
    Length,
    /// Mass — kilogram (kg).
    Mass,
    /// Time — second (s).
    Time,
    /// Electric current — ampere (A).
    Current,
    /// Thermodynamic temperature — kelvin (K).
    Temperature,
    /// Amount of substance — mole (mol).
    Amount,
    /// Luminous intensity — candela (cd).
    LuminousIntensity,
}

impl BaseUnit {
    /// All seven base units in a fixed canonical order.
    pub const ALL: [BaseUnit; 7] = [
        BaseUnit::Length,
        BaseUnit::Mass,
        BaseUnit::Time,
        BaseUnit::Current,
        BaseUnit::Temperature,
        BaseUnit::Amount,
        BaseUnit::LuminousIntensity,
    ];
}

// ──────────────────────────────────────────────────────────────────────────────
// PhysicalVar
// ──────────────────────────────────────────────────────────────────────────────

/// A physical variable with its SI dimensional exponents.
///
/// Only dimensions with non-zero exponents need to be set.
#[derive(Debug, Clone)]
pub struct PhysicalVar {
    /// Human-readable name (symbol or description).
    pub name: String,
    /// Dimensional exponents: `{BaseUnit → exponent}`.
    pub dimensions: HashMap<BaseUnit, i32>,
}

impl PhysicalVar {
    /// Create a dimensionless variable (all exponents zero).
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            dimensions: HashMap::new(),
        }
    }

    /// Set the exponent for one SI base dimension.
    pub fn set_dimension(&mut self, unit: BaseUnit, exponent: i32) {
        if exponent == 0 {
            self.dimensions.remove(&unit);
        } else {
            self.dimensions.insert(unit, exponent);
        }
    }

    /// Return the exponent for a base unit (0 if absent).
    pub fn exponent(&self, unit: BaseUnit) -> i32 {
        self.dimensions.get(&unit).copied().unwrap_or(0)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// PiGroup
// ──────────────────────────────────────────────────────────────────────────────

/// A dimensionless π group expressed as a product of powers of the input
/// variables: `π = V₀^a₀ · V₁^a₁ · … · Vₙ^aₙ`.
///
/// Exponents are rational numbers represented as `(numerator, denominator)` in
/// lowest terms.
#[derive(Debug, Clone)]
pub struct PiGroup {
    /// Name assigned to the group (e.g. `"π1"`, `"Re"`, …).
    pub name: String,
    /// Variable-name → rational exponent (as `(numerator, denominator)`).
    pub exponents: Vec<(String, (i64, i64))>,
}

impl PiGroup {
    /// Return the exponent for `var_name` as an `f64`, or 0.0 if absent.
    pub fn exponent_f64(&self, var_name: &str) -> f64 {
        self.exponents
            .iter()
            .find(|(n, _)| n == var_name)
            .map(|(_, (num, den))| *num as f64 / *den as f64)
            .unwrap_or(0.0)
    }

    /// Human-readable representation, e.g. `"π1 = v^1 * L^(-1) * T^1"`.
    pub fn display(&self) -> String {
        let terms: Vec<String> = self
            .exponents
            .iter()
            .filter(|(_, (num, _))| *num != 0)
            .map(|(name, (num, den))| {
                if *den == 1 {
                    format!("{name}^{num}")
                } else {
                    format!("{name}^({num}/{den})")
                }
            })
            .collect();
        if terms.is_empty() {
            format!("{} = 1 (trivially dimensionless)", self.name)
        } else {
            format!("{} = {}", self.name, terms.join(" · "))
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Error type
// ──────────────────────────────────────────────────────────────────────────────

/// Errors returned by [`BuckinghamPi::analyze`].
#[derive(Debug, Clone, PartialEq)]
pub enum BuckinghamPiError {
    /// Fewer than two variables supplied.
    TooFewVariables,
    /// All variables are dimensionless — no analysis needed.
    AllDimensionless,
    /// n ≤ k: no dimensionless groups exist (more dimensions than variables).
    NoPiGroupsPossible {
        /// Number of variables.
        n: usize,
        /// Rank of dimensional matrix (independent base dimensions).
        k: usize,
    },
}

impl std::fmt::Display for BuckinghamPiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BuckinghamPiError::TooFewVariables => {
                write!(f, "at least 2 variables are required")
            }
            BuckinghamPiError::AllDimensionless => {
                write!(f, "all variables are already dimensionless")
            }
            BuckinghamPiError::NoPiGroupsPossible { n, k } => {
                write!(
                    f,
                    "no π groups possible: n={n} variables, k={k} independent dimensions (n ≤ k)"
                )
            }
        }
    }
}

impl std::error::Error for BuckinghamPiError {}

// ──────────────────────────────────────────────────────────────────────────────
// BuckinghamPi analyser
// ──────────────────────────────────────────────────────────────────────────────

/// Implements the Buckingham π theorem via Gaussian elimination over ℚ.
///
/// The algorithm:
/// 1. Build a `k × n` dimensional matrix `A` where rows are SI base dimensions
///    and columns are variables.
/// 2. Compute the rank `r` of `A` (= number of independent dimensions used).
/// 3. Produce `n − r` dimensionless π groups from the null space of `A`.
pub struct BuckinghamPi;

impl BuckinghamPi {
    /// Analyse a list of physical variables and return the π groups.
    ///
    /// # Errors
    ///
    /// Returns a [`BuckinghamPiError`] if:
    /// - fewer than 2 variables are given,
    /// - all variables are dimensionless, or
    /// - no π groups exist (n ≤ k).
    pub fn analyze(variables: &[PhysicalVar]) -> Result<Vec<PiGroup>, BuckinghamPiError> {
        let n = variables.len();
        if n < 2 {
            return Err(BuckinghamPiError::TooFewVariables);
        }

        // Determine which base dimensions are actually used.
        let active_dims: Vec<BaseUnit> = BaseUnit::ALL
            .iter()
            .copied()
            .filter(|&dim| variables.iter().any(|v| v.exponent(dim) != 0))
            .collect();

        if active_dims.is_empty() {
            return Err(BuckinghamPiError::AllDimensionless);
        }

        let m = active_dims.len(); // number of rows (dimensions)

        // Build dimensional matrix as rational numbers (num, den).
        // Row i = dimension active_dims[i], column j = variable j.
        // Stored row-major: matrix[i][j] = (numerator, denominator).
        let matrix: Vec<Vec<(i64, i64)>> = (0..m)
            .map(|i| {
                (0..n)
                    .map(|j| (variables[j].exponent(active_dims[i]) as i64, 1i64))
                    .collect()
            })
            .collect();

        // Augment with identity block for the column space tracking.
        // We need to track transformations to find the null space.
        // Strategy: work on the full m×(n+m) augmented system.
        // Column 0..n are the variable columns; columns n..n+m are the identity.
        let mut aug: Vec<Vec<(i64, i64)>> = (0..m)
            .map(|i| {
                let mut row: Vec<(i64, i64)> = matrix[i].clone();
                for k in 0..m {
                    row.push(if k == i { (1, 1) } else { (0, 1) });
                }
                row
            })
            .collect();

        // ── Gaussian elimination (row echelon form) ────────────────────────

        let mut pivot_cols: Vec<usize> = Vec::new(); // pivot column indices
        let mut row = 0_usize;

        for col in 0..n {
            // Find a non-zero entry in column `col` at or below `row`.
            let pivot = (row..m).find(|&r| aug[r][col].0 != 0);
            if let Some(p) = pivot {
                // Swap rows `row` and `p`.
                aug.swap(row, p);
                pivot_cols.push(col);

                // Scale the pivot row so that aug[row][col] = 1.
                let pivot_val = aug[row][col];
                for entry in &mut aug[row] {
                    *entry = rat_div(*entry, pivot_val);
                }

                // Eliminate column `col` in all other rows.
                // Clone the normalized pivot row to allow independent mutable access.
                let pivot_row = aug[row].clone();
                for (r, aug_row) in aug.iter_mut().enumerate() {
                    if r != row && aug_row[col].0 != 0 {
                        let factor = aug_row[col];
                        for (entry, &pv) in aug_row.iter_mut().zip(pivot_row.iter()) {
                            let sub = rat_mul(factor, pv);
                            *entry = rat_sub(*entry, sub);
                        }
                    }
                }
                row += 1;
            }
        }

        let rank = pivot_cols.len(); // = r
        let pi_count = n - rank;

        if pi_count == 0 {
            return Err(BuckinghamPiError::NoPiGroupsPossible { n, k: rank });
        }

        // ── Identify free (non-pivot) column indices ────────────────────────
        let pivot_set: std::collections::HashSet<usize> = pivot_cols.iter().copied().collect();
        let free_cols: Vec<usize> = (0..n).filter(|c| !pivot_set.contains(c)).collect();

        // ── Build null-space vectors (one per free variable) ────────────────
        //
        // For each free column f, set the exponent of variable f to 1 and the
        // exponents of the other free variables to 0.  The exponents of the
        // pivot variables are read from the reduced row-echelon form.
        let mut pi_groups: Vec<PiGroup> = Vec::with_capacity(pi_count);

        for (pi_idx, &free_col) in free_cols.iter().enumerate() {
            let mut exponents: Vec<(String, (i64, i64))> = Vec::with_capacity(n);

            // Pivot variable exponents from RREF.
            for (piv_row, &piv_col) in pivot_cols.iter().enumerate() {
                // Exponent = -A[piv_row][free_col] (negated because we moved
                // the free-variable term to the right-hand side).
                let coeff = rat_neg(aug[piv_row][free_col]);
                let red = rat_reduce(coeff);
                exponents.push((variables[piv_col].name.clone(), red));
            }

            // Free variable exponents: 1 for this free column, 0 for others.
            for &fc in &free_cols {
                let exp = if fc == free_col { (1, 1) } else { (0, 1) };
                exponents.push((variables[fc].name.clone(), exp));
            }

            // Remove zero entries for clarity.
            exponents.retain(|(_, (num, _))| *num != 0);

            pi_groups.push(PiGroup {
                name: format!("π{}", pi_idx + 1),
                exponents,
            });
        }

        let _ = matrix; // suppress warning — used above for construction
        Ok(pi_groups)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Rational arithmetic helpers
// ──────────────────────────────────────────────────────────────────────────────

fn gcd(a: i64, b: i64) -> i64 {
    let (mut a, mut b) = (a.abs(), b.abs());
    while b != 0 {
        a %= b;
        std::mem::swap(&mut a, &mut b);
    }
    a.max(1)
}

fn rat_reduce((num, den): (i64, i64)) -> (i64, i64) {
    if num == 0 {
        return (0, 1);
    }
    let sign = if (num < 0) ^ (den < 0) { -1 } else { 1 };
    let g = gcd(num.abs(), den.abs());
    (sign * num.abs() / g, den.abs() / g)
}

fn rat_neg((num, den): (i64, i64)) -> (i64, i64) {
    (-num, den)
}

fn rat_mul((an, ad): (i64, i64), (bn, bd): (i64, i64)) -> (i64, i64) {
    rat_reduce((an * bn, ad * bd))
}

fn rat_div((an, ad): (i64, i64), (bn, bd): (i64, i64)) -> (i64, i64) {
    rat_reduce((an * bd, ad * bn))
}

fn rat_sub((an, ad): (i64, i64), (bn, bd): (i64, i64)) -> (i64, i64) {
    rat_reduce((an * bd - bn * ad, ad * bd))
}

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

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

    fn var(name: &str, dims: &[(BaseUnit, i32)]) -> PhysicalVar {
        let mut v = PhysicalVar::new(name);
        for &(unit, exp) in dims {
            v.set_dimension(unit, exp);
        }
        v
    }

    // ── pendulum ─────────────────────────────────────────────────────────────

    /// Simple pendulum: variables are L (length), g (acceleration), T (period).
    /// n = 3, k = 2 (Length, Time) → 1 π group.
    /// Expected: T·√(g/L) = dimensionless → π1 = T * g^(1/2) * L^(-1/2).
    #[test]
    fn test_pendulum_one_pi_group() {
        let length = var("L", &[(BaseUnit::Length, 1)]);
        let gravity = var("g", &[(BaseUnit::Length, 1), (BaseUnit::Time, -2)]);
        let period = var("T", &[(BaseUnit::Time, 1)]);

        let groups = BuckinghamPi::analyze(&[length, gravity, period]).unwrap();
        assert_eq!(groups.len(), 1, "Pendulum should produce exactly 1 π group");
    }

    // ── Reynolds number ───────────────────────────────────────────────────────

    /// Reynolds number: ρ·v·L / μ.
    /// Variables: ρ (density: M·L⁻³), v (velocity: L·T⁻¹), L (length: L),
    ///            μ (dynamic viscosity: M·L⁻¹·T⁻¹).
    /// n = 4, k = 3 (M, L, T) → 1 π group (Re).
    #[test]
    fn test_reynolds_number_one_pi_group() {
        let rho = var("rho", &[(BaseUnit::Mass, 1), (BaseUnit::Length, -3)]);
        let v = var("v", &[(BaseUnit::Length, 1), (BaseUnit::Time, -1)]);
        let l = var("L", &[(BaseUnit::Length, 1)]);
        let mu = var(
            "mu",
            &[
                (BaseUnit::Mass, 1),
                (BaseUnit::Length, -1),
                (BaseUnit::Time, -1),
            ],
        );

        let groups = BuckinghamPi::analyze(&[rho, v, l, mu]).unwrap();
        assert_eq!(groups.len(), 1, "Reynolds number: 1 π group expected");
    }

    // ── two pi groups ─────────────────────────────────────────────────────────

    /// Drag force: F (M·L·T⁻²), ρ (M·L⁻³), v (L·T⁻¹), L (L), μ (M·L⁻¹·T⁻¹).
    /// n = 5, k = 3 (M, L, T) → 2 π groups (drag coefficient and Re).
    #[test]
    fn test_drag_two_pi_groups() {
        let f = var(
            "F",
            &[
                (BaseUnit::Mass, 1),
                (BaseUnit::Length, 1),
                (BaseUnit::Time, -2),
            ],
        );
        let rho = var("rho", &[(BaseUnit::Mass, 1), (BaseUnit::Length, -3)]);
        let v = var("v", &[(BaseUnit::Length, 1), (BaseUnit::Time, -1)]);
        let l = var("L", &[(BaseUnit::Length, 1)]);
        let mu = var(
            "mu",
            &[
                (BaseUnit::Mass, 1),
                (BaseUnit::Length, -1),
                (BaseUnit::Time, -1),
            ],
        );

        let groups = BuckinghamPi::analyze(&[f, rho, v, l, mu]).unwrap();
        assert_eq!(groups.len(), 2, "Drag: 2 π groups expected");
    }

    // ── all dimensionless ─────────────────────────────────────────────────────

    #[test]
    fn test_all_dimensionless_error() {
        let a = PhysicalVar::new("a");
        let b = PhysicalVar::new("b");
        let result = BuckinghamPi::analyze(&[a, b]);
        assert!(matches!(result, Err(BuckinghamPiError::AllDimensionless)));
    }

    // ── too few variables ─────────────────────────────────────────────────────

    #[test]
    fn test_too_few_variables_error() {
        let v = var("L", &[(BaseUnit::Length, 1)]);
        let result = BuckinghamPi::analyze(&[v]);
        assert!(matches!(result, Err(BuckinghamPiError::TooFewVariables)));
    }

    // ── pi group is dimensionless ─────────────────────────────────────────────

    /// Verify that the π groups output are genuinely dimensionless by
    /// checking that the sum of dimensional contributions is zero for each
    /// base dimension.
    #[test]
    fn test_pi_groups_are_dimensionless() {
        // pendulum
        let length = var("L", &[(BaseUnit::Length, 1)]);
        let gravity = var("g", &[(BaseUnit::Length, 1), (BaseUnit::Time, -2)]);
        let period = var("T", &[(BaseUnit::Time, 1)]);
        let variables = [length, gravity, period];

        let groups = BuckinghamPi::analyze(&variables).unwrap();
        for group in &groups {
            for &dim in &BaseUnit::ALL {
                let dim_sum: f64 = group
                    .exponents
                    .iter()
                    .map(|(vname, (num, den))| {
                        let var = variables.iter().find(|v| v.name == *vname).unwrap();
                        var.exponent(dim) as f64 * (*num as f64 / *den as f64)
                    })
                    .sum();
                assert!(
                    dim_sum.abs() < 1e-10,
                    "π group '{}' is not dimensionless in {dim:?}: sum = {dim_sum}",
                    group.name
                );
            }
        }
    }

    // ── display ───────────────────────────────────────────────────────────────

    #[test]
    fn test_pi_group_display_non_empty() {
        let length = var("L", &[(BaseUnit::Length, 1)]);
        let gravity = var("g", &[(BaseUnit::Length, 1), (BaseUnit::Time, -2)]);
        let period = var("T", &[(BaseUnit::Time, 1)]);

        let groups = BuckinghamPi::analyze(&[length, gravity, period]).unwrap();
        let s = groups[0].display();
        assert!(s.contains("π1"), "Display should contain 'π1'");
    }

    // ── rational helpers ──────────────────────────────────────────────────────

    #[test]
    fn test_rat_reduce() {
        assert_eq!(rat_reduce((6, 4)), (3, 2));
        assert_eq!(rat_reduce((-6, 4)), (-3, 2));
        assert_eq!(rat_reduce((0, 5)), (0, 1));
    }

    #[test]
    fn test_rat_mul() {
        assert_eq!(rat_mul((2, 3), (3, 4)), (1, 2));
    }

    #[test]
    fn test_rat_div() {
        assert_eq!(rat_div((1, 2), (1, 4)), (2, 1));
    }

    #[test]
    fn test_rat_sub() {
        assert_eq!(rat_sub((3, 4), (1, 4)), (1, 2));
    }
}