use crate::atoms::ElementType;
pub use interpolation::Interpolation;
pub use quadrature::QuadratureRule;
mod hermite;
mod hex20;
mod hex27;
mod hex8;
mod interpolation;
mod penta15;
mod penta6;
mod poi1;
mod pyra5;
mod qua4;
mod qua8;
mod qua9;
mod quadrature;
mod seg2;
mod seg3;
mod tet10;
mod tet4;
mod tri3;
mod tri6;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Facet {
pub element_type: ElementType,
pub nodes: &'static [usize],
}
impl Facet {
pub fn corners(&self) -> &'static [usize] {
&self.nodes[..self.element_type.as_kind().corner_count()]
}
}
pub fn identity_permutation(n: usize) -> &'static [usize] {
const IDENTITY: [usize; 27] = {
let mut t = [0; 27];
let mut i = 0;
while i < 27 {
t[i] = i;
i += 1;
}
t
};
&IDENTITY[..n]
}
pub trait ElementKind: Sync {
fn element_type(&self) -> ElementType;
fn ref_nodes(&self) -> &'static [&'static [f64]];
fn reversal_permutation(&self) -> &'static [usize];
fn corner_count(&self) -> usize;
fn facets(&self) -> &'static [Facet] {
&[]
}
fn edges(&self) -> &'static [[usize; 2]] {
&[]
}
fn ref_centroid(&self) -> &'static [f64];
fn ref_measure(&self) -> f64;
fn contains_ref(&self, xi: &[f64], tol: f64) -> bool;
fn clamp_ref(&self, xi: &mut [f64]);
fn degree(&self) -> Option<Interpolation>;
fn shape_into(&self, xi: &[f64], out: &mut [f64]);
fn dshape_into(&self, xi: &[f64], out: &mut [f64]);
fn gauss(&self) -> (Vec<f64>, Vec<f64>);
fn vtk_code(&self) -> u8;
fn gmsh_code(&self) -> u32;
fn gmsh_permutation(&self) -> &'static [usize] {
identity_permutation(self.nodes_per_cell())
}
fn med_permutation(&self) -> &'static [usize] {
identity_permutation(self.nodes_per_cell())
}
fn quadratic(&self) -> Option<ElementType> {
None
}
fn linear_parent(&self) -> Option<ElementType> {
None
}
fn split_into(&self, _target: ElementType) -> Option<&'static [&'static [usize]]> {
None
}
fn nodes_per_cell(&self) -> usize {
self.ref_nodes().len()
}
fn topological_dim(&self) -> usize {
self.ref_nodes()[0].len()
}
fn is_quadratic(&self) -> bool {
self.nodes_per_cell() > self.corner_count()
}
fn shape(&self, xi: &[f64]) -> Vec<f64> {
let mut out = vec![0.0; self.nodes_per_cell()];
self.shape_into(xi, &mut out);
out
}
fn dshape(&self, xi: &[f64]) -> Vec<f64> {
let mut out = vec![0.0; self.nodes_per_cell() * self.topological_dim()];
self.dshape_into(xi, &mut out);
out
}
fn reduced(&self) -> (Vec<f64>, Vec<f64>) {
(self.ref_centroid().to_vec(), vec![self.ref_measure()])
}
}
impl ElementType {
pub fn as_kind(self) -> &'static dyn ElementKind {
match self {
ElementType::POI1 => &poi1::Poi1,
ElementType::SEG2 => &seg2::Seg2,
ElementType::TRI3 => &tri3::Tri3,
ElementType::QUA4 => &qua4::Qua4,
ElementType::TET4 => &tet4::Tet4,
ElementType::PENTA6 => &penta6::Penta6,
ElementType::PYRA5 => &pyra5::Pyra5,
ElementType::HEX8 => &hex8::Hex8,
ElementType::SEG3 => &seg3::Seg3,
ElementType::TRI6 => &tri6::Tri6,
ElementType::QUA8 => &qua8::Qua8,
ElementType::TET10 => &tet10::Tet10,
ElementType::PENTA15 => &penta15::Penta15,
ElementType::HEX20 => &hex20::Hex20,
ElementType::QUA9 => &qua9::Qua9,
ElementType::HEX27 => &hex27::Hex27,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fe_types() -> impl Iterator<Item = ElementType> {
ElementType::ALL
.iter()
.copied()
.filter(|et| et.topological_dim() > 0)
}
#[test]
fn as_kind_round_trips_and_agrees_with_the_enum_metadata() {
for &et in ElementType::ALL {
let k = et.as_kind();
assert_eq!(k.element_type(), et);
assert_eq!(k.nodes_per_cell(), et.nodes_per_cell(), "{et}: node count");
assert_eq!(
k.topological_dim(),
et.topological_dim(),
"{et}: topological dim"
);
assert_eq!(
k.reversal_permutation(),
et.reversal_permutation(),
"{et}: reversal"
);
assert_eq!(k.ref_nodes().len(), et.nodes_per_cell(), "{et}: ref_nodes");
for (i, n) in k.ref_nodes().iter().enumerate() {
assert_eq!(n.len(), et.topological_dim(), "{et}: node {i} dimension");
}
}
}
#[test]
fn mid_edge_nodes_follow_the_convention() {
for et in fe_types() {
let k = et.as_kind();
if !k.is_quadratic() {
continue;
}
let nodes = k.ref_nodes();
let nc = k.corner_count();
for (e, [a, b]) in k.edges().iter().enumerate() {
let mid = nc + e;
assert!(
mid < nodes.len(),
"{et}: edge {e} has no mid node at slot {mid}"
);
for d in 0..k.topological_dim() {
let want = 0.5 * (nodes[*a][d] + nodes[*b][d]);
assert!(
(nodes[mid][d] - want).abs() < 1e-12,
"{et}: node {mid} is not the midpoint of edge {e} = ({a}, {b})"
);
}
}
}
}
#[test]
fn edges_are_well_formed() {
for et in fe_types() {
let k = et.as_kind();
let nc = k.corner_count();
let mut seen = std::collections::HashSet::new();
for &[a, b] in k.edges() {
assert!(
a < nc && b < nc,
"{et}: edge ({a}, {b}) is not a corner pair"
);
assert_ne!(a, b, "{et}: degenerate edge");
let key = if a < b { (a, b) } else { (b, a) };
assert!(seen.insert(key), "{et}: edge ({a}, {b}) listed twice");
}
if k.topological_dim() > 0 {
for c in 0..nc {
assert!(
k.edges().iter().any(|&[a, b]| a == c || b == c),
"{et}: corner {c} lies on no edge"
);
}
}
}
}
#[test]
fn facets_are_consistent_with_the_parent_geometry() {
for et in fe_types() {
let k = et.as_kind();
let npc = k.nodes_per_cell();
for (f, facet) in k.facets().iter().enumerate() {
let fk = facet.element_type.as_kind();
assert_eq!(
facet.nodes.len(),
fk.nodes_per_cell(),
"{et}: facet {f} has {} nodes, {} wants {}",
facet.nodes.len(),
facet.element_type,
fk.nodes_per_cell()
);
assert_eq!(
fk.topological_dim(),
k.topological_dim() - 1,
"{et}: facet {f} is not of codimension 1"
);
assert_eq!(
fk.is_quadratic(),
k.is_quadratic(),
"{et}: facet {f} does not share its parent's degree"
);
for &n in facet.nodes {
assert!(n < npc, "{et}: facet {f} references node {n} ≥ {npc}");
}
let mut seen = std::collections::HashSet::new();
for &n in facet.nodes {
assert!(seen.insert(n), "{et}: facet {f} repeats node {n}");
}
if !fk.is_quadratic() {
continue;
}
let parent_nodes = k.ref_nodes();
let fnc = fk.corner_count();
for (e, [a, b]) in fk.edges().iter().enumerate() {
let mid = facet.nodes[fnc + e];
let (pa, pb) = (facet.nodes[*a], facet.nodes[*b]);
for d in 0..k.topological_dim() {
let want = 0.5 * (parent_nodes[pa][d] + parent_nodes[pb][d]);
assert!(
(parent_nodes[mid][d] - want).abs() < 1e-12,
"{et}: facet {f} node {mid} is not the midpoint of ({pa}, {pb})"
);
}
}
}
}
}
#[test]
fn facet_corner_sets_are_distinct() {
for et in fe_types() {
let k = et.as_kind();
let mut seen = std::collections::HashSet::new();
for (f, facet) in k.facets().iter().enumerate() {
let mut key = facet.corners().to_vec();
key.sort_unstable();
assert!(seen.insert(key), "{et}: facet {f} duplicates another");
}
}
}
#[test]
fn interchange_codes_are_unique() {
let mut vtk = std::collections::HashMap::new();
let mut gmsh = std::collections::HashMap::new();
for &et in ElementType::ALL {
let k = et.as_kind();
if let Some(other) = vtk.insert(k.vtk_code(), et) {
panic!("{et} and {other} share VTK code {}", k.vtk_code());
}
if let Some(other) = gmsh.insert(k.gmsh_code(), et) {
panic!("{et} and {other} share gmsh code {}", k.gmsh_code());
}
}
}
#[test]
fn interchange_permutations_are_bijections() {
for &et in ElementType::ALL {
let k = et.as_kind();
for (format, perm) in [("gmsh", k.gmsh_permutation()), ("med", k.med_permutation())] {
assert_eq!(
perm.len(),
k.nodes_per_cell(),
"{et}: {format} permutation length"
);
let mut seen = vec![false; perm.len()];
for &i in perm {
assert!(i < perm.len(), "{et}: {format} index {i} out of range");
assert!(!seen[i], "{et}: {format} index {i} repeated");
seen[i] = true;
}
}
}
}
#[test]
fn the_quadratic_pairing_is_consistent() {
for &et in ElementType::ALL {
let k = et.as_kind();
if let Some(q) = k.quadratic() {
let qk = q.as_kind();
assert_eq!(qk.linear_parent(), Some(et), "{et} → {q} is not mutual");
assert!(qk.is_quadratic(), "{q} is not quadratic");
assert_eq!(qk.corner_count(), k.corner_count(), "{et} → {q}: corners");
assert_eq!(qk.edges(), k.edges(), "{et} → {q}: edges");
assert_eq!(
qk.nodes_per_cell(),
k.corner_count() + k.edges().len(),
"{et} → {q}: one mid-side node per edge"
);
}
if let Some(l) = k.linear_parent() {
let lk = l.as_kind();
assert!(!lk.is_quadratic(), "{l} is not linear");
assert_eq!(lk.corner_count(), k.corner_count(), "{q}: corners", q = et);
assert_eq!(lk.edges(), k.edges(), "{et} ← {l}: edges");
}
}
}
#[test]
fn splits_are_well_formed() {
for &src in ElementType::ALL {
for &target in ElementType::ALL {
let Some(cells) = src.as_kind().split_into(target) else {
continue;
};
let want = target.as_kind().corner_count();
for cell in cells {
assert_eq!(cell.len(), want, "{src} → {target}: cell arity");
for &n in *cell {
assert!(
n < src.as_kind().corner_count(),
"{src} → {target}: node {n} is not a corner"
);
}
}
}
}
}
#[test]
fn the_centroid_is_inside_and_survives_clamping() {
for et in fe_types() {
let k = et.as_kind();
let c = k.ref_centroid();
assert_eq!(c.len(), k.topological_dim(), "{et}: centroid dimension");
assert!(k.contains_ref(c, 0.0), "{et}: centroid is outside");
let mut xi = c.to_vec();
k.clamp_ref(&mut xi);
for (a, b) in xi.iter().zip(c.iter()) {
assert!((a - b).abs() < 1e-12, "{et}: clamping moved the centroid");
}
assert!(
k.ref_measure() > 0.0,
"{et}: non-positive reference measure"
);
}
}
#[test]
fn reference_nodes_lie_in_the_domain() {
for et in fe_types() {
let k = et.as_kind();
for (i, n) in k.ref_nodes().iter().enumerate() {
assert!(k.contains_ref(n, 1e-12), "{et}: node {i} is outside");
}
}
}
#[test]
fn clamping_brings_an_outside_point_back_in() {
for et in fe_types().filter(|et| et.topological_dim() < 3) {
let k = et.as_kind();
let mut xi = vec![7.5; k.topological_dim()];
k.clamp_ref(&mut xi);
assert!(k.contains_ref(&xi, 1e-9), "{et}: clamp left {xi:?} outside");
let mut xi = vec![-7.5; k.topological_dim()];
k.clamp_ref(&mut xi);
assert!(k.contains_ref(&xi, 1e-9), "{et}: clamp left {xi:?} outside");
}
}
}