|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: MIT |
| 3 | + * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +//! Shared test fixtures for the physics test suite. |
| 7 | +//! |
| 8 | +//! These helpers build discrete manifolds and cochains used by the |
| 9 | +//! `SolenoidalField` tests. They live in the source tree (rather than inside |
| 10 | +//! the `tests/` folder) because the Bazel `rust_test_suite` compiles each |
| 11 | +//! `*_tests.rs` file as a standalone crate, so test files cannot share helpers |
| 12 | +//! across one another — only the crate under test is visible to all of them. |
| 13 | +
|
| 14 | +use alloc::{vec, vec::Vec}; |
| 15 | +use deep_causality_num::{FromPrimitive, RealField}; |
| 16 | +use deep_causality_tensor::CausalTensor; |
| 17 | +use deep_causality_topology::{ChainComplex, CubicalReggeGeometry, LatticeComplex, Manifold}; |
| 18 | + |
| 19 | +/// A 2D square-torus lattice manifold of side `n` with unit cubical metric. |
| 20 | +pub fn unit_manifold<R>(n: usize) -> Manifold<LatticeComplex<2, R>, R> |
| 21 | +where |
| 22 | + R: RealField |
| 23 | + + deep_causality_par::MaybeParallel |
| 24 | + + FromPrimitive |
| 25 | + + Default |
| 26 | + + PartialEq |
| 27 | + + core::fmt::Debug |
| 28 | + + core::fmt::Display, |
| 29 | +{ |
| 30 | + let lattice: LatticeComplex<2, R> = LatticeComplex::square_torus(n); |
| 31 | + let total: usize = (0..=2).map(|k| lattice.num_cells(k)).sum(); |
| 32 | + let data = CausalTensor::new(vec![R::zero(); total], vec![total]).unwrap(); |
| 33 | + let metric: CubicalReggeGeometry<2, R> = CubicalReggeGeometry::unit(); |
| 34 | + Manifold::from_cubical_with_metric(lattice, data, metric, 0) |
| 35 | +} |
| 36 | + |
| 37 | +/// A deterministic pseudo-random cochain of length `len` in `[-1, 1]`. |
| 38 | +pub fn random_cochain<R: RealField + FromPrimitive>(len: usize, seed: u64) -> Vec<R> { |
| 39 | + let mut state = seed |
| 40 | + .wrapping_mul(6364136223846793005) |
| 41 | + .wrapping_add(1442695040888963407); |
| 42 | + (0..len) |
| 43 | + .map(|_| { |
| 44 | + state = state |
| 45 | + .wrapping_mul(6364136223846793005) |
| 46 | + .wrapping_add(1442695040888963407); |
| 47 | + let unit = (state >> 11) as f64 / (1u64 << 53) as f64; |
| 48 | + R::from_f64(2.0 * unit - 1.0).expect("[-1,1] lifts") |
| 49 | + }) |
| 50 | + .collect() |
| 51 | +} |
| 52 | + |
| 53 | +/// Discrete divergence of an edge cochain: place at grade 1, apply δ. |
| 54 | +pub fn divergence<R>(manifold: &Manifold<LatticeComplex<2, R>, R>, one_form: &[R]) -> Vec<R> |
| 55 | +where |
| 56 | + R: RealField |
| 57 | + + deep_causality_par::MaybeParallel |
| 58 | + + FromPrimitive |
| 59 | + + Default |
| 60 | + + PartialEq |
| 61 | + + core::fmt::Debug |
| 62 | + + core::fmt::Display, |
| 63 | +{ |
| 64 | + let lattice = LatticeComplex::<2, R>::square_torus( |
| 65 | + // shape is square by fixture construction |
| 66 | + manifold.complex().shape()[0], |
| 67 | + ); |
| 68 | + let total: usize = (0..=2).map(|g| lattice.num_cells(g)).sum(); |
| 69 | + let n0 = lattice.num_cells(0); |
| 70 | + let mut data = vec![R::zero(); total]; |
| 71 | + data[n0..n0 + one_form.len()].copy_from_slice(one_form); |
| 72 | + let tensor = CausalTensor::new(data, vec![total]).unwrap(); |
| 73 | + let metric: CubicalReggeGeometry<2, R> = CubicalReggeGeometry::unit(); |
| 74 | + let m = Manifold::from_cubical_with_metric(lattice, tensor, metric, 0); |
| 75 | + m.codifferential(1).as_slice().to_vec() |
| 76 | +} |
| 77 | + |
| 78 | +/// Supremum (max-abs) norm of a slice. |
| 79 | +pub fn sup_norm<R: RealField>(v: &[R]) -> R { |
| 80 | + v.iter() |
| 81 | + .map(|x| x.abs()) |
| 82 | + .fold(R::zero(), |m, x| if x > m { x } else { m }) |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_sup_norm() { |
| 91 | + assert_eq!(sup_norm(&[-3.0_f64, 1.0, 2.0]), 3.0); |
| 92 | + assert_eq!(sup_norm::<f64>(&[]), 0.0); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_random_cochain_is_bounded_and_deterministic() { |
| 97 | + let a = random_cochain::<f64>(16, 7); |
| 98 | + let b = random_cochain::<f64>(16, 7); |
| 99 | + assert_eq!(a, b); |
| 100 | + assert!(a.iter().all(|&x| (-1.0..=1.0).contains(&x))); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_unit_manifold_and_divergence_dimensions() { |
| 105 | + let manifold = unit_manifold::<f64>(4); |
| 106 | + let n1 = manifold.complex().num_cells(1); |
| 107 | + let div = divergence(&manifold, &random_cochain::<f64>(n1, 1)); |
| 108 | + assert_eq!(div.len(), manifold.complex().num_cells(0)); |
| 109 | + } |
| 110 | +} |
0 commit comments