pyrucast 0.12.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
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
//! Per-element behaviour — one file per [`ElementType`], one trait to bind them.
//!
//! This module is to elements what [`crate::models`] is to physics. The
//! [`ElementType`] enum carries **storage and serialisation only**; everything
//! an element *knows about itself* — its reference nodes, its facets, its
//! reference domain — lives in a struct implementing [`ElementKind`], and a
//! single `match`, [`ElementType::as_kind`], binds the two.
//!
//! ```text
//! ElementType  (enum : stockage + sérialisation + nom cast3m)
//! ├── POI1, SEG2, TRI3, …, HEX27
//! └── as_kind(&self) -> &'static dyn ElementKind   ← l'unique match
//!
//! ElementKind  (trait : le comportement d'un type d'élément)
//! ├── ref_nodes / reversal_permutation / corner_count   ── l'identité
//! ├── facets / edges                                    ── la topologie
//! └── ref_centroid / ref_measure / contains_ref / clamp_ref  ── le domaine
//! ```
//!
//! Adding an element type means adding `atoms/element_kind/<name>.rs`, one variant
//! to [`ElementType`], one entry in [`ElementType::ALL`] and one arm to
//! `as_kind`. Every generic consumer — `skin`, `orient`, `border`, the
//! locators, the renderer — is written against the trait and does not change.
//!
//! # The reference-node convention
//!
//! One rule holds across every quadratic type and the rest of the crate leans
//! on it: **corners first, then one mid-side node per edge, in `edges()`
//! order**. So the mid node of edge `k` is always local index
//! `corner_count() + k`. `QUA9`/`HEX27` append their face and body centres
//! after that. The unit test `mid_edge_nodes_follow_the_convention` proves it
//! against the reference coordinates, for every type.

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;

/// One facet of an element: a lower-dimensional element in its own right.
///
/// `nodes` lists the facet's local node indices **in the local order of
/// `element_type`**, oriented so the facet's normal points out of the parent
/// element. A facet of a quadratic element is itself quadratic — a `TET10`
/// face is a `TRI6`, a `HEX27` face is a `QUA9` — so `nodes` carries the
/// mid-side (and centre) nodes, not only the corners.
///
/// [`corners`](Self::corners) narrows it to the corner indices, which is what
/// adjacency keying wants: two neighbouring cells agree on the corners of the
/// facet they share whatever their degree.
///
/// ```
/// # use pyrucast::atoms::ElementType;
/// // A TET10's face is a TRI6: it carries its mid-edge nodes.
/// let f = &ElementType::TET10.as_kind().facets()[0];
/// assert_eq!(f.element_type, ElementType::TRI6);
/// assert_eq!(f.nodes.len(), 6);
/// // The corners alone are enough to pair two neighbouring cells, whatever
/// // their degree.
/// assert_eq!(f.corners().len(), 3);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Facet {
    /// The facet seen as an element (`SEG2` for a surface's side, `TRI6` for a
    /// `TET10` face, …).
    pub element_type: ElementType,
    /// Local node indices in the parent, in `element_type`'s local order,
    /// outward-oriented.
    pub nodes: &'static [usize],
}

impl Facet {
    /// The facet's corner indices — the first `element_type.corner_count()`
    /// entries of [`nodes`](Self::nodes), by the corners-first convention.
    ///
    /// ```
    /// # use pyrucast::atoms::ElementType;
    /// let f = &ElementType::QUA8.as_kind().facets()[0];
    /// assert_eq!(f.element_type, ElementType::SEG3); // le côté est quadratique
    /// assert_eq!(f.corners(), &f.nodes[..2]); // ses deux extrémités
    /// ```
    pub fn corners(&self) -> &'static [usize] {
        &self.nodes[..self.element_type.as_kind().corner_count()]
    }
}

/// The first `n` entries of `0, 1, 2, …`: the node permutation of a format
/// that numbers a cell like pyrucast does. Static, so an interchange
/// permutation is always a slice — never an `Option` a hot loop would test.
///
/// ```
/// # use pyrucast::atoms::element_kind::identity_permutation;
/// assert_eq!(identity_permutation(4), [0, 1, 2, 3]);
/// ```
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]
}

/// Everything a single element type knows about itself.
///
/// Split, like [`crate::models::SubModelKind`], between **required** methods
/// (the compiler forces every element to answer) and **provided** ones derived
/// from a more primitive datum. A new element writes the required set; the
/// rest follows.
///
/// ```
/// # use pyrucast::atoms::ElementType;
/// // The single `match` per element type: `as_kind()`. All the rest — node
/// // count, facets, reference domain — goes through the trait.
/// let k = ElementType::TRI3.as_kind();
/// assert_eq!(k.corner_count(), 3);
/// assert_eq!(k.facets().len(), 3);
/// ```
pub trait ElementKind: Sync {
    // ── Identity (required) ─────────────────────────────────────────────

    /// The enum variant this kind implements — the way back from behaviour to
    /// storage.
    fn element_type(&self) -> ElementType;

    /// Reference coordinates of each node, in local order: the points where
    /// the matching shape function equals 1. **The root datum** — the node
    /// count and the topological dimension are read off it, and it is the
    /// ground truth every geometric table is tested against.
    fn ref_nodes(&self) -> &'static [&'static [f64]];

    /// Local-node permutation that reverses the element's orientation
    /// (output node `i` = input node `reversal_permutation()[i]`).
    ///
    /// Geometrically the orientation-reversing reflection swapping the first
    /// two reference axes, `(ξ, η, ζ) ↦ (η, ξ, ζ)` — for `SEG*`, the flip
    /// `ξ ↦ -ξ`. It carries **every** node, mid-side and centre included.
    fn reversal_permutation(&self) -> &'static [usize];

    /// Number of corner (vertex) nodes — the node count of the linear parent.
    /// Equals [`nodes_per_cell`](Self::nodes_per_cell) for a linear type.
    fn corner_count(&self) -> usize;

    // ── Reference topology (provided: nothing, for a point) ─────────────

    /// The element's facets — its edges if it is a surface, its faces if it is
    /// a volume — each oriented outwards. Empty for `POI1` and for `SEG*`,
    /// whose ends carry no orientation of their own.
    fn facets(&self) -> &'static [Facet] {
        &[]
    }

    /// The element's edges, as **corner** index pairs. For a quadratic type
    /// the mid node of edge `k` is `corner_count() + k` (see the module
    /// documentation), so this one table also describes the mid-side layout.
    fn edges(&self) -> &'static [[usize; 2]] {
        &[]
    }

    // ── Reference domain (required) ─────────────────────────────────────

    /// An interior point of the reference domain — the centroid. Doubles as
    /// the point of the one-point (`Reduced`) quadrature and as the Newton
    /// starting guess when inverting the geometric mapping.
    fn ref_centroid(&self) -> &'static [f64];

    /// Measure (length / area / volume) of the reference domain: 2 for `SEG2`,
    /// 1/2 for `TRI3`, 4/3 for `PYRA5`, 8 for `HEX8`. Quadrature weights sum
    /// to it.
    fn ref_measure(&self) -> f64;

    /// Is `ξ` inside the reference domain, allowing `tol` of slack?
    fn contains_ref(&self, xi: &[f64], tol: f64) -> bool;

    /// Clamp `ξ` into the reference domain, in place. Used when projecting a
    /// point onto a cell: a Newton step that leaves the element is pulled back
    /// to its closest admissible reference point.
    fn clamp_ref(&self, xi: &mut [f64]);

    // ── Interpolation (required) ────────────────────────────────────────

    /// The Lagrange degree this type carries, or `None` for `POI1`, which has
    /// no reference frame to interpolate over. This is what
    /// [`Interpolation::is_compatible_with`] answers from: the degree is a
    /// property **of the element**, not a free choice.
    fn degree(&self) -> Option<Interpolation>;

    /// Shape functions `N_i(ξ)`, written into `out` (length
    /// [`nodes_per_cell`](Self::nodes_per_cell)).
    ///
    /// The allocation-free form, and the one to implement: inverting the
    /// geometric mapping ([`crate::ops::geom::locate_points`],
    /// [`crate::ops::geom::project_points`]) evaluates it dozens of times per
    /// point inside a Newton loop, where a `Vec` per call would dominate the
    /// cost. [`shape`](Self::shape) is the allocating convenience over it.
    fn shape_into(&self, xi: &[f64], out: &mut [f64]);

    /// Reference derivatives `∂N_i/∂ξ_j`, written into `out` row-major:
    /// `out[i * topological_dim() + j]`, length
    /// `nodes_per_cell() × topological_dim()`.
    fn dshape_into(&self, xi: &[f64], out: &mut [f64]);

    // ── Quadrature (required: the full rule; the reduced one is derived) ──

    /// The element's standard integration rule, as `(xi, w)`: `xi` flat
    /// row-major over `n_g × topological_dim()`, `w` of length `n_g`.
    ///
    /// Calibrated to integrate the Lagrange-1 mass matrix exactly on a
    /// straight element. The weights **must** sum to
    /// [`ref_measure`](Self::ref_measure) — the invariant the tests check for
    /// every type at once.
    fn gauss(&self) -> (Vec<f64>, Vec<f64>);

    // ── Interchange (required) ──────────────────────────────────────────

    /// Legacy-VTK cell-type code. pyrucast's local node order **is** VTK's, so
    /// [`crate::ops::export`] writes the connectivity verbatim.
    fn vtk_code(&self) -> u8;

    /// gmsh element-type code, the other direction of the same contract.
    fn gmsh_code(&self) -> u32;

    /// Permutation mapping a gmsh cell's node order to pyrucast's:
    /// `pyrucast[i] = gmsh[perm[i]]`. The identity (the default) when the two
    /// orders already coincide — every linear type, plus
    /// `SEG3`/`TRI6`/`QUA8`/`QUA9`. gmsh numbers the mid-side nodes of the
    /// quadratic volumes differently.
    fn gmsh_permutation(&self) -> &'static [usize] {
        identity_permutation(self.nodes_per_cell())
    }

    /// Permutation mapping a MED cell's node order to pyrucast's:
    /// `pyrucast[i] = med[perm[i]]`. The identity (the default) for the
    /// segments and the surface types, which MED numbers like VTK. MED orients
    /// its volumes the other way round — the first face is walked clockwise
    /// seen from the rest of the cell — so every volume type overrides it.
    fn med_permutation(&self) -> &'static [usize] {
        identity_permutation(self.nodes_per_cell())
    }

    // ── Families (provided: standing alone) ─────────────────────────────

    /// This type's quadratic counterpart, if it has one: `TRI3` → `TRI6`.
    /// The mid-side nodes to create are exactly this element's
    /// [`edges`](Self::edges), in order.
    fn quadratic(&self) -> Option<ElementType> {
        None
    }

    /// This type's linear parent, if it is quadratic: `TRI6` → `TRI3`. Both
    /// `QUA8` and `QUA9` answer `QUA4`.
    fn linear_parent(&self) -> Option<ElementType> {
        None
    }

    /// How one cell of this type splits into cells of `target`, as local
    /// corner-index tuples — `HEX8` into six `TET4`, say. `None` when no such
    /// split is defined.
    fn split_into(&self, _target: ElementType) -> Option<&'static [&'static [usize]]> {
        None
    }

    // ── Provided: derived from ref_nodes ────────────────────────────────

    /// Number of nodes per cell.
    fn nodes_per_cell(&self) -> usize {
        self.ref_nodes().len()
    }

    /// Topological dimension (0 = point, 1 = segment, 2 = surface, 3 = volume).
    fn topological_dim(&self) -> usize {
        self.ref_nodes()[0].len()
    }

    /// Whether this type carries mid-side nodes, i.e. has more nodes than
    /// corners.
    fn is_quadratic(&self) -> bool {
        self.nodes_per_cell() > self.corner_count()
    }

    /// Shape functions `N_i(ξ)` as a fresh `Vec`. Convenience over
    /// [`shape_into`](Self::shape_into) for the callers that are not in a hot
    /// loop.
    fn shape(&self, xi: &[f64]) -> Vec<f64> {
        let mut out = vec![0.0; self.nodes_per_cell()];
        self.shape_into(xi, &mut out);
        out
    }

    /// Reference derivatives as a fresh `Vec`. Convenience over
    /// [`dshape_into`](Self::dshape_into).
    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
    }

    /// The **reduced** (one-point) integration rule: the centroid, carrying the
    /// whole reference measure.
    ///
    /// Wholly derived — no element writes it. It used to be a seventeen-line
    /// table that repeated, per type, what [`ref_centroid`](Self::ref_centroid)
    /// and [`ref_measure`](Self::ref_measure) already said.
    fn reduced(&self) -> (Vec<f64>, Vec<f64>) {
        (self.ref_centroid().to_vec(), vec![self.ref_measure()])
    }
}

impl ElementType {
    /// Borrow the variant as its [`ElementKind`] behaviour.
    ///
    /// This is the **only** per-variant `match` in the element layer; every
    /// generic consumer (facets, reference domain, rendering) dispatches
    /// through it. Mirrors
    /// [`SubModel::as_kind`](crate::containers::model::SubModel::as_kind).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let maillage = Mesh::from_submesh(sm);
    /// // **The single** `match` per element type in the whole library: all the
    /// // rest — corner count, facets, reference domain — goes through the trait
    /// // it returns.
    /// assert_eq!(ElementType::TRI3.as_kind().corner_count(), 3);
    /// assert_eq!(ElementType::HEX8.as_kind().facets().len(), 6);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    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::*;

    /// Every type with a reference frame (`POI1` has none).
    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");
            }
        }
    }

    /// The convention the whole crate leans on: corners first, then the mid
    /// node of edge `k` at local index `corner_count() + k`, sitting exactly
    /// at that edge's midpoint in the reference frame.
    #[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})"
                    );
                }
            }
        }
    }

    /// Edges are corner pairs, distinct, and every corner is on some edge.
    #[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"
                    );
                }
            }
        }
    }

    /// A facet's nodes are valid indices, its degree matches its parent's, and
    /// its geometry is consistent: the facet's own reference nodes map onto
    /// the parent's, so the hand-written tables cannot drift.
    #[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}");
                }
                // A mid-side node of the facet must be the midpoint of the
                // matching corner pair, measured in the *parent's* frame.
                // Linear facets have no mid node to check.
                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})"
                        );
                    }
                }
            }
        }
    }

    /// Every facet corner set appears exactly once: a volume's faces partition
    /// its boundary, so no face is listed twice or omitted by symmetry.
    #[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");
            }
        }
    }

    /// Interchange codes identify a type: two elements sharing a VTK or gmsh
    /// code would make an export ambiguous and a read wrong. `gmsh_code` in
    /// particular is now looked up by scanning, so a duplicate would silently
    /// shadow one of the two.
    #[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());
            }
        }
    }

    /// A gmsh or MED permutation must be a bijection of the element's nodes —
    /// otherwise a read silently drops or duplicates connectivity.
    #[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;
                }
            }
        }
    }

    /// The two directions of the linear ↔ quadratic pairing must agree, and a
    /// quadratic type must really be its parent's degree bumped: same corner
    /// count, same edges, one mid-side node per edge.
    #[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");
            }
        }
    }

    /// A split names cells of the target type, built from the source's corners.
    #[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"
            );
        }
    }

    /// Every reference node is in the (closed) reference domain, and a point
    /// pushed well outside comes back inside once clamped — for the types
    /// that define a clamp (surfaces and lines; volumes are never projected
    /// onto, so their clamp is a no-op).
    #[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");
        }
    }
}