Files
rustytorch/crates/specialized/rtx-fea/tests/pyramid_finite_element_tdd.rs
T
Omar SobhandClaude Opus 5 e30cfe4ce9
CI / Format Check (push) Canceled after 0s
CI / Clippy Check (push) Canceled after 0s
CI / Build (macos-latest) (push) Canceled after 0s
CI / Build (ubuntu-latest) (push) Canceled after 0s
CI / Test (macos-latest) (push) Canceled after 0s
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Build CPU-Only (Explicit) (push) Canceled after 0s
CI / Python Bindings (maturin) (macos-latest) (push) Canceled after 0s
CI / Python Bindings (maturin) (ubuntu-latest) (push) Canceled after 0s
CI / WASM Build + Size Check (push) Canceled after 0s
CI / Distributed Training Tests (push) Canceled after 0s
CI / CI Success (push) Canceled after 0s
Documentation / Build API Documentation (push) Canceled after 0s
Documentation / Build User Guide (push) Canceled after 0s
Performance Benchmarks / Run Benchmarks (push) Canceled after 0s
rtx-fea: repair the element library; the crate is now green with no quarantine
Follows the assembly repair. Takes rtx-fea from 21 failures to 253 passing,
0 failing, 0 ignored, with every `#[ignore]` marker gone.

Shape function bugs, all found by one new test asserting two invariants
across the whole element library at once -- partition of unity, and that
the hand-written derivatives sum to zero. The second is the one that gets
skipped, and it is what caught Hexahedron20.

  - Wedge15 summed to 2 at mid-height. Adding a node on a vertical edge
    contributes L_i (1 - t^2) to the sum, so the two corners sharing that
    edge must each give up half of it; the correction was absent. A
    quadratic wedge that doubles every field interpolated through it.

  - Hexahedron20 had sign errors in four hand-written corner
    derivatives -- nodes 3 and 7 in dN/dr, nodes 1 and 5 in dN/ds. The
    values were correct, so partition of unity passed; only the
    derivative-sum invariant exposed it. The strain computed from this
    element was wrong while its interpolation looked right.

  - Quadrilateral9 emitted its shape functions in raw lexicographic
    lattice order while Quad4 and Quad8 use the standard finite-element
    order. A mesh written the usual way paired each node with the wrong
    basis function, which at the element centre made the Jacobian exactly
    singular.

  - Pyramid13 was not a quadratic pyramid basis: it summed to 4 at the
    element centre, and its `derivatives` allocated a 13x3 matrix then
    wrote rows 13 through 15, having been copied from a sixteen-node
    layout, so it panicked before the wrong values could be used. A
    correct 13-node basis is rational, and there is no pyramid quadrature
    rule to integrate it with, so implementing the basis alone would not
    make the element usable. Both now report the gap explicitly rather
    than panicking. Pyramid5 is unaffected and works.

Fixtures corrected rather than tolerances loosened:

  - von Mises stress of an equal biaxial state expected 0, commented "no
    deviatoric stress". Only a hydrostatic state has that. The correct
    value is 100, and expecting 0 would mean a biaxially loaded sheet
    could never yield. The unequal case expected |100-50|; the von Mises
    stress is not a principal difference.
  - A 3-point Gauss rule was required to integrate sin to 1e-10. No
    correct implementation can. Replaced with a convergence assertion,
    which a wrong rule cannot satisfy by luck.
  - MathUtils::SMALL was asserted below EPSILON * 1000, which inverts the
    relationship a practical zero-threshold needs.
  - The Hex20 Jacobian test put all twelve mid-edge nodes at the origin,
    commented "simplified for test". That is not a hexahedron, and its
    mapping is genuinely singular; it only passed because of the
    derivative sign errors above.
  - ElementFactory was required to build every ElementType including
    Point, which has no interpolation and is deliberately rejected.

MemoryInfo displayed decimal GB while its own test constructed binary
GiB, rendering an 8 GiB device as 8.59. Now GiB throughout.

test_mesh_has_real_algorithms searched the *text* of mesh/mod.rs for the
strings "add_node" and "add_element". It broke when those moved into
submodules, but the real problem is that a source-text search cannot tell
a working function from one returning zeros -- it passed throughout the
period when element matrices were a stub and quadrature returned no
points. Replaced with a test that builds a mesh and checks the result.

The crate doc example imported solvers::DirectSolver and
analysis::StaticAnalysis, neither of which has ever existed, so the
doctest never compiled. Replaced with a modal analysis that runs. Also
dropped the "Production Ready: No mocks, stubs, or TODOs - complete
implementation" line, and replaced it with what is actually validated and
what is not.

rtx-fsi unaffected at 26/26.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-19 08:21:58 -07:00

123 lines
4.2 KiB
Rust

//! TDD Tests for Pyramid FiniteElement Implementations
//! Following strict Red-Green-Refactor cycle
//! No mocks, stubs, or TODOs - only full implementations
#[cfg(test)]
mod pyramid5_tests {
use rtx_fea::elements::shape_functions::shape_special::Pyramid5;
use rtx_fea::elements::{FiniteElement, NaturalCoords};
use rtx_fea::mesh::ElementType;
#[test]
fn test_pyramid5_element_type() {
// RED: Test that Pyramid5 implements FiniteElement
let pyramid5 = Pyramid5::new();
// GREEN: Pyramid5 should return ElementType::Pyramid5
assert_eq!(pyramid5.element_type(), ElementType::Pyramid5);
}
#[test]
fn test_pyramid5_num_nodes() {
// RED: Test that Pyramid5 correctly reports number of nodes
let pyramid5 = Pyramid5::new();
// GREEN: Pyramid5 has 5 nodes (4 base + 1 apex)
assert_eq!(pyramid5.num_nodes(), 5);
}
#[test]
fn test_pyramid5_dimensions() {
// RED: Test spatial and parametric dimensions
let pyramid5 = Pyramid5::new();
// GREEN: Pyramid5 is 3D element with 3D parametric space
assert_eq!(pyramid5.spatial_dimension(), 3);
assert_eq!(pyramid5.parametric_dimension(), 3);
}
#[test]
fn test_pyramid5_shape_functions_at_base() {
// RED: Test shape function evaluation at base
let pyramid5 = Pyramid5::new();
// Test at base center (0,0,0)
let coords = NaturalCoords::new_3d(0.0, 0.0, 0.0);
let shape = pyramid5.shape_functions(&coords).unwrap();
// GREEN: At base center, base nodes should have equal contributions
for i in 0..4 {
assert!((shape.value(i).unwrap() - 0.25).abs() < 1e-10);
}
assert!(shape.value(4).unwrap().abs() < 1e-10); // Apex should be 0
}
#[test]
fn test_pyramid5_partition_of_unity() {
// RED: Test that shape functions sum to 1 everywhere
let pyramid5 = Pyramid5::new();
let test_points = vec![
(0.0, 0.0, 0.0), // Base center
(0.5, 0.5, 0.5), // Mid pyramid
(-0.5, 0.3, 0.2), // Random point
];
for (x, y, z) in test_points {
let coords = NaturalCoords::new_3d(x, y, z);
let shape = pyramid5.shape_functions(&coords).unwrap();
// GREEN: Sum of all shape functions should be 1
let sum: f64 = (0..5).map(|i| shape.value(i).unwrap()).sum();
assert!((sum - 1.0).abs() < 1e-10);
}
}
}
#[cfg(test)]
mod pyramid13_tests {
use rtx_fea::elements::shape_functions::shape_special::Pyramid13;
use rtx_fea::elements::{FiniteElement, NaturalCoords};
use rtx_fea::mesh::ElementType;
#[test]
fn test_pyramid13_element_type() {
// RED: Test that Pyramid13 implements FiniteElement
let pyramid13 = Pyramid13::new();
// GREEN: Pyramid13 should return ElementType::Pyramid13
assert_eq!(pyramid13.element_type(), ElementType::Pyramid13);
}
#[test]
fn test_pyramid13_num_nodes() {
// RED: Test that Pyramid13 correctly reports number of nodes
let pyramid13 = Pyramid13::new();
// GREEN: Pyramid13 has 13 nodes
assert_eq!(pyramid13.num_nodes(), 13);
}
#[test]
fn test_pyramid13_partition_of_unity() {
// Pyramid13 is not implemented; it reports that rather than returning
// a basis that does not form a partition of unity.
//
// The previous implementation summed to 4 at the base centre, and its
// `derivatives` allocated a 13x3 matrix then wrote rows 13 to 15 --
// copied from a sixteen-node layout -- so this test panicked on an
// out-of-bounds index rather than on the assertion below.
//
// Pyramid5 is unaffected; see `test_pyramid5_partition_of_unity`.
let pyramid13 = Pyramid13::new();
for (x, y, z) in [(0.0, 0.0, 0.0), (0.25, 0.25, 0.25)] {
let coords = NaturalCoords::new_3d(x, y, z);
let error = pyramid13
.shape_functions(&coords)
.expect_err("Pyramid13 must report that it is unimplemented");
assert!(error.to_string().to_lowercase().contains("not implemented"));
}
}
}