pyrucast 0.1.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! ElementField — multi-component values per `(cell, Gauss point)` on a
//! [`crate::containers::finite_element_space::FiniteElementSpace`].
//!
//! Hierarchy mirroring [`crate::containers::finite_element_space`]:
//!
//! - [`SubElementField`] — multi-component values per `(cell, Gauss point)`
//!   on a single [`crate::containers::finite_element_space::SubFiniteElementSpace`]. Where
//!   [`crate::containers::node_field::SubNodeField`] stores values **at nodes**, a
//!   `SubElementField` stores them **at the Gauss points of every cell**
//!   of a finite-element subspace.
//! - [`ElementField`] — aggregate of `SubElementField`, one per subspace
//!   of a [`crate::containers::finite_element_space::FiniteElementSpace`], in the same order.
//!
//! Typical uses:
//!
//! - **material properties** (Young's modulus, density, conductivity, …)
//!   evaluated at the Gauss points where the integrals are computed;
//! - **state / internal variables** (plastic strain, damage, hardening, …)
//!   that need to be remembered cell-by-cell, point-by-point;
//! - **derived quantities** (stresses, strains, fluxes, …) extracted from
//!   a solution for post-treatment.
//!
//! # Snapshot of the FE space layout
//!
//! On construction every `SubElementField` captures three dimensions of
//! its host `SubFiniteElementSpace`:
//!
//! - `cell_count`  — number of cells (`SubMesh::cell_count` at that moment);
//! - `gauss_count` — number of Gauss points per cell;
//! - `component_count` — chosen by the caller.
//!
//! The internal buffer is sized accordingly and **never reallocated**. The
//! mesh topology underlying the FE space is expected to stay frozen for
//! the lifetime of the field (per the contract documented on
//! [`crate::containers::finite_element_space::FiniteElementSpace`]). The Gauss-point coordinates
//! and weights are kept as reference data on the `SubFiniteElementSpace` itself and
//! may be re-read on demand; only the user data lives here.
//!
//! # Layout
//!
//! Values are stored flat, row-major, in the order **cell → gauss →
//! component** so that contiguous reads of a single cell or a single
//! Gauss point are cache-friendly:
//!
//! ```text
//! values[cell_idx * gauss_count * component_count
//!        + g * component_count
//!        + c]
//! ```
//!
//! # Example
//!
//! ```
//! use pyrucast::aggregate::Aggregate;
//! use pyrucast::containers::field::SubField;
//! use pyrucast::containers::mesh::Coords;
//! use pyrucast::containers::element_field::ElementField;
//! use pyrucast::containers::mesh::ElementType;
//! use pyrucast::containers::finite_element_space::FiniteElementSpace;
//! use pyrucast::containers::mesh::{Mesh, SubMesh};
//! use pyrucast::containers::mesh::Node;
//! use pyrucast::store::{insert, read, write};
//!
//! let coords = insert(Coords::new(2).unwrap());
//! let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
//! let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
//! let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
//! let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::TRI3));
//! mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
//!
//! let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
//!
//! // Linear elasticity 2D — two material properties (E, nu) on every subspace.
//! let mat = ElementField::new(&fes, vec!["E".into(), "nu".into()]).unwrap();
//! assert_eq!(mat.len(), 1);
//!
//! let sub0 = mat.get(0).unwrap();
//! {
//!     let mut s = write(&sub0).unwrap();
//!     s.set_uniform("E", 210e9).unwrap();
//!     s.set_uniform("nu", 0.3).unwrap();
//! }
//!
//! let s = read(&sub0).unwrap();
//! assert_eq!(s.cell_count(), 1);
//! assert_eq!(s.gauss_count(), 3);   // TRI3 Hammer
//! assert_eq!(s.component_count(), 2);
//! assert_eq!(s.value(0, 0, "E").unwrap(), 210e9);
//! ```

use crate::aggregate::Aggregate;
use crate::containers::field::SubField;
use crate::containers::finite_element_space::{FiniteElementSpace, SubFiniteElementSpace};
use crate::error::{PyrucastError, Result};
use crate::store::{insert, read, Handle};
use serde::{Deserialize, Serialize};
use std::fmt;

// ─── SubElementField ───────────────────────────────────────────────────────

/// Multi-component values per `(cell, Gauss point)` on a single
/// [`SubFiniteElementSpace`].
///
/// Layout: flat row-major in the order *cell → gauss → component*
/// (see the module-level documentation).
#[derive(Serialize, Deserialize)]
pub struct SubElementField {
    support: Handle<SubFiniteElementSpace>,
    components: Vec<String>,
    /// Dimensions captured at construction; the buffer is never resized.
    n_cells: usize,
    n_gauss: usize,
    /// Flat row-major buffer of length `n_cells * n_gauss * components.len()`.
    values: Vec<f64>,
}

impl SubElementField {
    /// Build a sub-field on the given FE subspace with the supplied
    /// component names. Every value is initialized to `0.0`.
    ///
    /// # Errors
    ///
    /// - `components` is empty;
    /// - `components` contains a duplicate name.
    pub fn new(fespace: Handle<SubFiniteElementSpace>, components: Vec<String>) -> Result<Self> {
        crate::containers::field::check_components("SubElementField", &components)?;
        let (n_cells, n_gauss) = {
            let s = read(&fespace)?;
            (s.cell_count()?, s.gauss_count())
        };
        let n_comp = components.len();
        let values = vec![0.0; n_cells * n_gauss * n_comp];
        Ok(Self {
            support: fespace,
            components,
            n_cells,
            n_gauss,
            values,
        })
    }

    /// Convenience: build a sub-field with a uniform value per component.
    ///
    /// `values_per_component` must have the same length as `components`.
    pub fn from_uniform_per_component(
        fespace: Handle<SubFiniteElementSpace>,
        components: Vec<String>,
        values_per_component: &[f64],
    ) -> Result<Self> {
        if values_per_component.len() != components.len() {
            return Err(PyrucastError::Message(format!(
                "from_uniform_per_component: {} values supplied for {} components",
                values_per_component.len(),
                components.len()
            )));
        }
        let names = components.clone();
        let mut field = Self::new(fespace, components)?;
        for (name, &v) in names.iter().zip(values_per_component) {
            field.set_uniform(name, v)?;
        }
        Ok(field)
    }

    // ── Structural accessors ────────────────────────────────────────────────

    /// Number of cells captured at construction.
    pub fn cell_count(&self) -> usize {
        self.n_cells
    }

    /// Number of Gauss points per cell.
    pub fn gauss_count(&self) -> usize {
        self.n_gauss
    }

    // `components`, `component_count`, `component_index`, … come from
    // the [`crate::containers::field::SubField`] trait.

    // ── Value access by indices ─────────────────────────────────────────────

    /// Read the value at `(cell, gauss, component_index)`.
    pub fn get(&self, cell: usize, gauss: usize, comp: usize) -> Result<f64> {
        let idx = self.linear_index(cell, gauss, comp)?;
        Ok(self.values[idx])
    }

    /// Write the value at `(cell, gauss, component_index)`.
    pub fn set(&mut self, cell: usize, gauss: usize, comp: usize, value: f64) -> Result<()> {
        let idx = self.linear_index(cell, gauss, comp)?;
        self.values[idx] = value;
        Ok(())
    }

    /// All component values at `(cell, gauss)`, in component order
    /// (length = `component_count`).
    pub fn point_values(&self, cell: usize, gauss: usize) -> Result<&[f64]> {
        self.check_cell(cell)?;
        self.check_gauss(gauss)?;
        let n_comp = self.components.len();
        let start = (cell * self.n_gauss + gauss) * n_comp;
        Ok(&self.values[start..start + n_comp])
    }

    // ── Value access by component name ──────────────────────────────────────

    /// Read by `(cell, gauss, component name)`.
    pub fn value(&self, cell: usize, gauss: usize, component: &str) -> Result<f64> {
        let c = self.component_index_or_err(component)?;
        self.get(cell, gauss, c)
    }

    /// Write by `(cell, gauss, component name)`.
    pub fn set_value(
        &mut self,
        cell: usize,
        gauss: usize,
        component: &str,
        value: f64,
    ) -> Result<()> {
        let c = self.component_index_or_err(component)?;
        self.set(cell, gauss, c, value)
    }

    // ── Bulk fillers ────────────────────────────────────────────────────────

    // `set_uniform` (constant-per-domain material properties) comes from
    // the [`crate::containers::field::SubField`] trait.

    /// Set every Gauss point of a given cell to the same value for one
    /// component (cell-piecewise-constant material).
    pub fn set_cell_uniform(&mut self, cell: usize, component: &str, value: f64) -> Result<()> {
        self.check_cell(cell)?;
        let c = self.component_index_or_err(component)?;
        let n_comp = self.components.len();
        for g in 0..self.n_gauss {
            self.values[(cell * self.n_gauss + g) * n_comp + c] = value;
        }
        Ok(())
    }

    // Scalar per-component operations (`add_to_component`, …) come from
    // the [`crate::containers::field::SubField`] trait.

    // ── Internals ───────────────────────────────────────────────────────────

    fn linear_index(&self, cell: usize, gauss: usize, comp: usize) -> Result<usize> {
        self.check_cell(cell)?;
        self.check_gauss(gauss)?;
        self.check_comp(comp)?;
        let n_comp = self.components.len();
        Ok((cell * self.n_gauss + gauss) * n_comp + comp)
    }

    fn check_cell(&self, cell: usize) -> Result<()> {
        if cell >= self.n_cells {
            return Err(PyrucastError::Message(format!(
                "SubElementField: cell index {} ≥ cell_count {}",
                cell, self.n_cells
            )));
        }
        Ok(())
    }

    fn check_gauss(&self, gauss: usize) -> Result<()> {
        if gauss >= self.n_gauss {
            return Err(PyrucastError::Message(format!(
                "SubElementField: gauss index {} ≥ gauss_count {}",
                gauss, self.n_gauss
            )));
        }
        Ok(())
    }

    fn check_comp(&self, comp: usize) -> Result<()> {
        if comp >= self.components.len() {
            return Err(PyrucastError::Message(format!(
                "SubElementField: component index {} ≥ component_count {}",
                comp,
                self.components.len()
            )));
        }
        Ok(())
    }
}

impl crate::containers::field::SubField for SubElementField {
    type Support = SubFiniteElementSpace;
    fn support(&self) -> Handle<SubFiniteElementSpace> {
        self.support.clone()
    }
    fn components(&self) -> &[String] {
        &self.components
    }
    fn values(&self) -> &[f64] {
        &self.values
    }
    fn values_mut(&mut self) -> &mut [f64] {
        &mut self.values
    }
    fn same_support_with(&self, components: Vec<String>) -> Result<Self> {
        Self::new(self.support.clone(), components)
    }
}

// ─── Clone ─────────────────────────────────────────────────────────────────

impl Clone for SubElementField {
    fn clone(&self) -> Self {
        Self {
            support: self.support.clone(),
            components: self.components.clone(),
            n_cells: self.n_cells,
            n_gauss: self.n_gauss,
            values: self.values.clone(),
        }
    }
}

// ─── Debug / Display ───────────────────────────────────────────────────────

impl fmt::Debug for SubElementField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Bounded structure only — the per-(cell, gauss) values live in `dump()`.
        f.debug_struct("SubElementField")
            .field("support", &self.support)
            .field("cell_count", &self.n_cells)
            .field("gauss_count", &self.n_gauss)
            .field("components", &self.components)
            .finish()
    }
}

impl fmt::Display for SubElementField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "SubElementField: {} cell(s) × {} gauss × {} component(s) [{}]",
            self.n_cells,
            self.n_gauss,
            self.components.len(),
            self.components.join(", ")
        )
    }
}

impl crate::dump::Dump for SubElementField {
    fn render(&self, opts: &crate::dump::DumpOptions) -> String {
        use crate::dump::{fmt_float, table};
        let ncomp = self.components.len();
        let mut headers = vec!["cell".to_string(), "gauss".to_string()];
        headers.extend(self.components.iter().cloned());
        let mut rows = Vec::with_capacity(self.n_cells * self.n_gauss);
        for cell in 0..self.n_cells {
            for g in 0..self.n_gauss {
                let base = (cell * self.n_gauss + g) * ncomp;
                let mut row = vec![cell.to_string(), g.to_string()];
                for c in 0..ncomp {
                    row.push(fmt_float(self.values[base + c], opts.precision));
                }
                rows.push(row);
            }
        }
        format!("{self}\n{}", table(&headers, &rows, opts))
    }
}

// ─── Operators field OP f64 ────────────────────────────────────────────────
//
// Consuming versions (mutate self in place); reference versions clone first.
// Generated by the shared macro, exactly like `SubNodeField`.

crate::impl_subfield_scalar_ops!(SubElementField);

// ─── Operators field OP field (same support) ───────────────────────────────
//
// `&a + &b` (and `a + b`) delegate to `SubField::merge_components` (union of
// components with passthrough); fallible (same support required) ⇒ output
// `Result<SubElementField>`.

crate::impl_subfield_field_ops!(SubElementField);

// ─── ElementField (aggregate) ──────────────────────────────────────────────

/// Aggregate of [`SubElementField`] — one per subspace of a
/// [`FiniteElementSpace`], in the same order.
///
/// Mirrors the [`FiniteElementSpace`] / [`SubFiniteElementSpace`] hierarchy: an
/// `ElementField` is to `FiniteElementSpace` what a `SubElementField` is
/// to `SubFiniteElementSpace`. The component lists captured by the underlying
/// sub-fields may differ from one subspace to the next.
#[derive(Serialize, Deserialize, Default)]
pub struct ElementField {
    subs: Vec<Handle<SubElementField>>,
}

crate::impl_aggregate!(ElementField, SubElementField, subfield, "subfield(s)", {
    /// Validate the zone decomposition after a union (`a | b`): **no component
    /// may be carried by two zones on the same support**. Zones on the same
    /// support with disjoint components are kept side by side (no fusion, no new
    /// `SubElementField`); a duplicated component is an error. To fuse zones that
    /// legitimately share a support, call
    /// [`crate::ops::field::consolidate_element`](fn@crate::ops::field::consolidate_element)
    /// explicitly.
    fn finalize(&mut self) -> Result<()> {
        crate::ops::field::check_unique_component_per_support(self)
    }
});
crate::impl_aggregate_dump!(ElementField);

// ─── Operators ElementField OP {ElementField, f64} ─────────────────────────
//
// `&a + &b` (zone by zone, same decomposition) via `Field::merge_field`;
// `&a + 2.0` (scalar broadcast) via `Field::combine_scalar`. Fallible (store
// reads, zone pairing) ⇒ output `Result<ElementField>`.

crate::impl_field_ops!(ElementField);

impl ElementField {
    /// Build an `ElementField` on `fespace` with the **same** `components`
    /// on every subspace.
    ///
    /// `fespace` must have at least one subspace.
    pub fn new(fespace: &FiniteElementSpace, components: Vec<String>) -> Result<Self> {
        let n_sub = fespace.len();
        if n_sub == 0 {
            return Err(PyrucastError::Message(
                "ElementField: FE space has no subspace".into(),
            ));
        }
        crate::containers::field::check_components("SubElementField", &components)?;
        let mut subs = Vec::with_capacity(n_sub);
        for i in 0..n_sub {
            let sub = fespace.get(i)?;
            let sf = SubElementField::new(sub, components.clone())?;
            subs.push(insert(sf));
        }
        Ok(Self { subs })
    }

    /// Every [`SubElementField`] living on `fespace`, in aggregate order.
    ///
    /// The honest multi-zone accessor: a union (`a | b`) may leave several
    /// **component-disjoint** zones on one support (see
    /// [`crate::ops::field::check_unique_component_per_support`]); this returns
    /// all of them. Empty when none match. Prefer this whenever a support may
    /// legitimately carry more than one zone.
    pub(crate) fn subs_for_fespace(
        &self,
        fespace: &Handle<SubFiniteElementSpace>,
    ) -> Result<Vec<Handle<SubElementField>>> {
        let mut out = Vec::new();
        for h in self {
            let matches = {
                let f = read(h)?.support();
                f.index() == fespace.index() && f.generation() == fespace.generation()
            };
            if matches {
                out.push(h.clone());
            }
        }
        Ok(out)
    }

    /// The **unique** [`SubElementField`] on `fespace`.
    ///
    /// The per-zone field-matching primitive shared by the assembly and
    /// behaviour operators, for the common case of one zone per support. Errors
    /// if no sub-field of the aggregate lives on `fespace` (a missing per-zone
    /// material / deformation is always a caller error, never a silent skip),
    /// **and also if more than one zone lives on the support** — it never
    /// silently returns the first of many (that footgun once let `strain | state`
    /// drop the state). When a support may carry several zones, use
    /// [`subs_for_fespace`](Self::subs_for_fespace) or fuse them with
    /// [`crate::ops::field::consolidate_element`].
    pub(crate) fn sub_for_fespace(
        &self,
        fespace: &Handle<SubFiniteElementSpace>,
    ) -> Result<Handle<SubElementField>> {
        let mut zones = self.subs_for_fespace(fespace)?;
        match zones.len() {
            1 => Ok(zones.pop().expect("length checked to be 1")),
            0 => Err(PyrucastError::Message(format!(
                "no SubElementField in this ElementField matches the \
                 SubFiniteElementSpace at slot {} (generation {})",
                fespace.index(),
                fespace.generation()
            ))),
            n => Err(PyrucastError::Message(format!(
                "{n} SubElementFields live on the SubFiniteElementSpace at slot {} \
                 (generation {}); sub_for_fespace needs a unique zone. Use \
                 subs_for_fespace, or consolidate_element to fuse the zones.",
                fespace.index(),
                fespace.generation()
            ))),
        }
    }

    /// The **unique** [`SubElementField`] on `fespace` that carries **every**
    /// component in `required`.
    ///
    /// Like [`sub_for_fespace`](Self::sub_for_fespace) but discriminates zones by
    /// their component set, not by support alone. A support may then legitimately
    /// hold several **component-disjoint** zones — e.g. the per-physics material
    /// zones a union leaves side by side (`k` for conduction, `E`/`nu` for
    /// elasticity on one shared mesh) — and each caller resolves *its own* zone by
    /// naming the components it needs, without an explicit
    /// [`consolidate_element`](crate::ops::field::consolidate_element). It stays
    /// safe: it never silently returns the first of several — it errors if **no**
    /// zone carries the full set, or if **more than one** does (a genuine
    /// ambiguity the caller must resolve). `required` must be non-empty (an empty
    /// set matches every zone and is a caller error).
    pub(crate) fn sub_for_fespace_with(
        &self,
        fespace: &Handle<SubFiniteElementSpace>,
        required: &[&str],
    ) -> Result<Handle<SubElementField>> {
        let mut matching: Vec<Handle<SubElementField>> = Vec::new();
        for h in self.subs_for_fespace(fespace)? {
            let carries_all = {
                let comps = read(&h)?.components().to_vec();
                required
                    .iter()
                    .all(|&r| comps.iter().any(|c| c.as_str() == r))
            };
            if carries_all {
                matching.push(h);
            }
        }
        match matching.len() {
            1 => Ok(matching.pop().expect("length checked to be 1")),
            0 => Err(PyrucastError::Message(format!(
                "no SubElementField on the SubFiniteElementSpace at slot {} \
                 (generation {}) carries all of {:?} — supply it via material_field",
                fespace.index(),
                fespace.generation(),
                required
            ))),
            n => Err(PyrucastError::Message(format!(
                "{n} SubElementFields on the SubFiniteElementSpace at slot {} \
                 (generation {}) each carry all of {:?}; the zone is ambiguous — \
                 fuse them with consolidate_element or narrow the components",
                fespace.index(),
                fespace.generation(),
                required
            ))),
        }
    }

    /// Build an `ElementField` with an explicit `components` list per
    /// subspace. `components_per_subspace.len()` must equal
    /// `fespace.len()`.
    pub fn with(
        fespace: &FiniteElementSpace,
        components_per_subspace: &[Vec<String>],
    ) -> Result<Self> {
        let n_sub = fespace.len();
        if n_sub == 0 {
            return Err(PyrucastError::Message(
                "ElementField: FE space has no subspace".into(),
            ));
        }
        if components_per_subspace.len() != n_sub {
            return Err(PyrucastError::Message(format!(
                "ElementField: {} component list(s) supplied for {} subspace(s)",
                components_per_subspace.len(),
                n_sub
            )));
        }
        let mut subs = Vec::with_capacity(n_sub);
        for (i, comps) in components_per_subspace.iter().enumerate() {
            let sub = fespace.get(i)?;
            let sf = SubElementField::new(sub, comps.clone())?;
            subs.push(insert(sf));
        }
        Ok(Self { subs })
    }

    /// Visualize this field on its own support: each zone knows its
    /// submesh through its FE subspace, so the mesh is reconstructed
    /// (shared, not copied) and coloured by `component` — per-element
    /// nodal fit of the Gauss values, discontinuities preserved.
    /// `smooth` is the subdivision level (`0` = flat per cell).
    #[cfg(feature = "viz")]
    pub fn plot(
        &self,
        view: Option<crate::viz::View>,
        save: Option<&std::path::Path>,
        component: Option<&str>,
        scale: crate::viz::ColorScale,
        smooth: usize,
    ) -> Result<()> {
        let mut mesh = crate::containers::mesh::Mesh::empty();
        for h in self {
            let fespace = read(h)?.support();
            let sm = read(&fespace)?.submesh();
            mesh.add_sub(sm)?;
        }
        crate::viz::render_mesh_with_field(
            &mesh,
            crate::viz::FieldArg::Element(self),
            component,
            scale,
            smooth,
            view,
            save,
        )
    }
}

/// Zero-copy view of an [`ElementField`]'s zones — the
/// [`crate::containers::field::FieldView`] aggregate view specialised
/// to Gauss-point fields (built by `Field::view`). A zone is resolved
/// from its support submesh by handle identity (idx + generation),
/// the reciprocal of [`ElementField::sub_for_fespace`].
pub(crate) type ElementFieldView = crate::containers::field::FieldView<SubElementField>;

impl ElementFieldView {
    /// The zone supported on `submesh` (matched through the zone's FE
    /// subspace), or `None` if no zone lives on it.
    // Consumed by the viz layer (feature-gated).
    #[allow(dead_code)]
    pub(crate) fn zone_for_submesh(
        &self,
        submesh: &Handle<crate::containers::mesh::SubMesh>,
    ) -> Result<Option<&SubElementField>> {
        for z in &self.zones {
            let sm = read(&z.support())?.submesh();
            if sm.index() == submesh.index() && sm.generation() == submesh.generation() {
                return Ok(Some(z));
            }
        }
        Ok(None)
    }
}

// ─── Unit tests ────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregate::Aggregate;
    use crate::containers::finite_element_space::Interpolation;
    use crate::containers::finite_element_space::QuadratureRule;
    use crate::containers::finite_element_space::{FiniteElementSpace, SubFiniteElementSpace};
    use crate::containers::mesh::Coords;
    use crate::containers::mesh::ElementType;
    use crate::containers::mesh::Node;
    use crate::containers::mesh::{Mesh, SubMesh};
    use crate::store::{insert, read, write};

    fn make_tri3_subfespace() -> Handle<SubFiniteElementSpace> {
        let coords = insert(Coords::new(2).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
        let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let sm = {
            let mut sm = SubMesh::new(coords, ElementType::TRI3);
            sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
            insert(sm)
        };
        insert(
            SubFiniteElementSpace::new(sm, Interpolation::Lagrange1, QuadratureRule::Gauss)
                .unwrap(),
        )
    }

    fn make_multi_cell_tri3_subfespace(n_cells: usize) -> Handle<SubFiniteElementSpace> {
        // n_cells triangles sharing a common apex, like a fan from origin.
        let coords = insert(Coords::new(2).unwrap());
        let apex = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let mut perimeter = Vec::with_capacity(n_cells + 1);
        for i in 0..=n_cells {
            let t = i as f64 / n_cells as f64;
            perimeter.push(Node::create_in(coords.clone(), &[1.0, t]).unwrap());
        }
        let sm = {
            let mut sm = SubMesh::new(coords, ElementType::TRI3);
            for i in 0..n_cells {
                sm.add_cell(&[apex.id(), perimeter[i].id(), perimeter[i + 1].id()])
                    .unwrap();
            }
            insert(sm)
        };
        insert(
            SubFiniteElementSpace::new(sm, Interpolation::Lagrange1, QuadratureRule::Gauss)
                .unwrap(),
        )
    }

    fn make_mesh_with_tri_and_qua() -> Mesh {
        let coords = insert(Coords::new(2).unwrap());
        let n0 = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let n1 = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
        let n2 = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let n3 = Node::create_in(coords.clone(), &[1.0, 1.0]).unwrap();
        let mut mesh = Mesh::empty();
        let sm_tri = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
            sm.add_cell(&[n0.id(), n1.id(), n2.id()]).unwrap();
            insert(sm)
        };
        let sm_qua = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::QUA4);
            sm.add_cell(&[n0.id(), n1.id(), n3.id(), n2.id()]).unwrap();
            insert(sm)
        };
        mesh.add_sub(sm_tri).unwrap();
        mesh.add_sub(sm_qua).unwrap();
        mesh
    }

    // ── SubElementField ─────────────────────────────────────────────────────

    #[test]
    fn sub_new_zero_initialized() {
        let sub = make_tri3_subfespace();
        let f = SubElementField::new(sub, vec!["E".into(), "nu".into()]).unwrap();
        assert_eq!(f.cell_count(), 1);
        assert_eq!(f.gauss_count(), 3);
        assert_eq!(f.component_count(), 2);
        for g in 0..3 {
            assert_eq!(f.get(0, g, 0).unwrap(), 0.0);
            assert_eq!(f.get(0, g, 1).unwrap(), 0.0);
        }
    }

    #[test]
    fn sub_new_rejects_empty_components() {
        let sub = make_tri3_subfespace();
        assert!(SubElementField::new(sub, vec![]).is_err());
    }

    #[test]
    fn sub_new_rejects_duplicate_components() {
        let sub = make_tri3_subfespace();
        assert!(SubElementField::new(sub, vec!["E".into(), "E".into()]).is_err());
    }

    #[test]
    fn sub_get_set_roundtrip() {
        let sub = make_multi_cell_tri3_subfespace(3);
        let mut f = SubElementField::new(sub, vec!["sigma_xx".into(), "sigma_yy".into()]).unwrap();
        assert_eq!(f.cell_count(), 3);
        f.set(0, 0, 0, 1.0).unwrap();
        f.set(1, 2, 1, -3.5).unwrap();
        assert_eq!(f.get(0, 0, 0).unwrap(), 1.0);
        assert_eq!(f.get(1, 2, 1).unwrap(), -3.5);
        assert_eq!(f.get(0, 0, 1).unwrap(), 0.0);
    }

    #[test]
    fn sub_value_and_set_value_by_name() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["T".into(), "P".into()]).unwrap();
        f.set_value(0, 1, "P", 42.0).unwrap();
        assert_eq!(f.value(0, 1, "P").unwrap(), 42.0);
        assert!(f.value(0, 0, "unknown").is_err());
        assert!(f.set_value(0, 0, "unknown", 1.0).is_err());
    }

    #[test]
    fn sub_point_values_returns_all_components() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["a".into(), "b".into(), "c".into()]).unwrap();
        f.set(0, 1, 0, 1.0).unwrap();
        f.set(0, 1, 1, 2.0).unwrap();
        f.set(0, 1, 2, 3.0).unwrap();
        assert_eq!(f.point_values(0, 1).unwrap(), &[1.0, 2.0, 3.0]);
    }

    #[test]
    fn sub_out_of_bounds_errors() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        assert!(f.get(99, 0, 0).is_err());
        assert!(f.get(0, 99, 0).is_err());
        assert!(f.get(0, 0, 99).is_err());
        assert!(f.set(99, 0, 0, 0.0).is_err());
        assert!(f.point_values(99, 0).is_err());
    }

    #[test]
    fn sub_set_uniform_fills_every_point_of_one_component() {
        let sub = make_multi_cell_tri3_subfespace(2);
        let mut f = SubElementField::new(sub, vec!["E".into(), "nu".into()]).unwrap();
        f.set_uniform("E", 210e9).unwrap();
        for cell in 0..2 {
            for g in 0..3 {
                assert_eq!(f.get(cell, g, 0).unwrap(), 210e9);
                assert_eq!(f.get(cell, g, 1).unwrap(), 0.0);
            }
        }
    }

    #[test]
    fn sub_set_cell_uniform_touches_only_one_cell() {
        let sub = make_multi_cell_tri3_subfespace(2);
        let mut f = SubElementField::new(sub, vec!["rho".into()]).unwrap();
        f.set_cell_uniform(1, "rho", 7800.0).unwrap();
        for g in 0..3 {
            assert_eq!(f.get(0, g, 0).unwrap(), 0.0);
            assert_eq!(f.get(1, g, 0).unwrap(), 7800.0);
        }
    }

    #[test]
    fn sub_from_uniform_per_component_constructor() {
        let sub = make_tri3_subfespace();
        let f = SubElementField::from_uniform_per_component(
            sub,
            vec!["E".into(), "nu".into(), "rho".into()],
            &[210e9, 0.3, 7800.0],
        )
        .unwrap();
        for g in 0..3 {
            assert_eq!(f.get(0, g, 0).unwrap(), 210e9);
            assert_eq!(f.get(0, g, 1).unwrap(), 0.3);
            assert_eq!(f.get(0, g, 2).unwrap(), 7800.0);
        }
    }

    #[test]
    fn sub_from_uniform_per_component_length_mismatch_errors() {
        let sub = make_tri3_subfespace();
        assert!(SubElementField::from_uniform_per_component(
            sub,
            vec!["a".into(), "b".into()],
            &[1.0]
        )
        .is_err());
    }

    #[test]
    fn sub_component_scalar_ops_isolate_components() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["a".into(), "b".into()]).unwrap();
        f.set_uniform("a", 10.0).unwrap();
        f.set_uniform("b", 1.0).unwrap();
        f.add_to_component("a", 5.0).unwrap();
        f.sub_to_component("a", 2.0).unwrap();
        f.mul_to_component("a", 3.0).unwrap();
        f.div_to_component("a", 13.0).unwrap();
        // a went 10 → 15 → 13 → 39 → 3.0
        for g in 0..3 {
            assert!((f.get(0, g, 0).unwrap() - 3.0).abs() < 1e-12);
            assert_eq!(f.get(0, g, 1).unwrap(), 1.0); // unchanged
        }
    }

    #[test]
    fn sub_component_scalar_div_by_zero_errors() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        assert!(f.div_to_component("x", 0.0).is_err());
    }

    #[test]
    fn sub_component_scalar_unknown_name_errors() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        assert!(f.add_to_component("missing", 1.0).is_err());
        assert!(f.sub_to_component("missing", 1.0).is_err());
        assert!(f.mul_to_component("missing", 1.0).is_err());
        assert!(f.div_to_component("missing", 1.0).is_err());
    }

    #[test]
    fn sub_clone_is_independent() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        f.set(0, 0, 0, 1.0).unwrap();
        let g = f.clone();
        f.set(0, 0, 0, 99.0).unwrap();
        assert_eq!(g.get(0, 0, 0).unwrap(), 1.0);
    }

    #[test]
    fn sub_operator_add_f64_reference_keeps_self_intact() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        f.set(0, 1, 0, 4.0).unwrap();
        let g = &f + 10.0;
        assert_eq!(g.get(0, 1, 0).unwrap(), 14.0);
        assert_eq!(f.get(0, 1, 0).unwrap(), 4.0); // f unchanged
    }

    #[test]
    fn sub_operator_chained_sub_mul_div() {
        let sub = make_tri3_subfespace();
        let mut f = SubElementField::new(sub, vec!["x".into()]).unwrap();
        f.set_uniform("x", 12.0).unwrap();
        let g = (f.clone() - 2.0) * 3.0 / 2.0;
        for gp in 0..3 {
            assert!((g.get(0, gp, 0).unwrap() - 15.0).abs() < 1e-12);
        }
    }

    #[test]
    fn sub_debug_and_display() {
        let sub = make_multi_cell_tri3_subfespace(2);
        let f = SubElementField::new(sub, vec!["E".into(), "nu".into()]).unwrap();
        let d = format!("{:?}", f);
        assert!(d.contains("SubElementField"));
        assert!(d.contains("cell_count"));
        assert!(d.contains("E"));
        let s = format!("{}", f);
        assert!(s.contains("SubElementField"));
        assert!(s.contains("2 cell(s)"));
        assert!(s.contains("3 gauss"));
        assert!(s.contains("2 component(s)"));
        assert!(s.contains("E, nu"));
    }

    // ── ElementField (aggregate) ────────────────────────────────────────────

    #[test]
    fn ef_new_creates_one_subfield_per_subspace() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let ef = ElementField::new(&fes, vec!["E".into(), "nu".into()]).unwrap();
        assert_eq!(ef.len(), 2);

        // TRI3: 1 cell × 3 gauss × 2 components
        {
            let s = read(&ef.get(0).unwrap()).unwrap();
            assert_eq!(s.cell_count(), 1);
            assert_eq!(s.gauss_count(), 3);
            assert_eq!(s.component_count(), 2);
            assert_eq!(s.components(), &["E", "nu"]);
        }

        // QUA4: 1 cell × 4 gauss × 2 components
        {
            let s = read(&ef.get(1).unwrap()).unwrap();
            assert_eq!(s.cell_count(), 1);
            assert_eq!(s.gauss_count(), 4);
            assert_eq!(s.component_count(), 2);
        }
    }

    #[test]
    fn ef_with_supports_per_subspace_components() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let comps = vec![vec!["k".into()], vec!["E".into(), "nu".into()]];
        let ef = ElementField::with(&fes, &comps).unwrap();
        assert_eq!(ef.len(), 2);
        assert_eq!(read(&ef.get(0).unwrap()).unwrap().components(), &["k"]);
        assert_eq!(
            read(&ef.get(1).unwrap()).unwrap().components(),
            &["E", "nu"]
        );
    }

    #[test]
    fn ef_with_rejects_mismatched_length() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let comps_one = vec![vec!["k".into()]];
        assert!(ElementField::with(&fes, &comps_one).is_err());
        let comps_three = vec![vec!["k".into()], vec!["k".into()], vec!["k".into()]];
        assert!(ElementField::with(&fes, &comps_three).is_err());
    }

    #[test]
    fn ef_subfield_out_of_bounds_errors() {
        let coords = insert(Coords::new(2).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
        let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::TRI3));
        mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let ef = ElementField::new(&fes, vec!["k".into()]).unwrap();
        assert!(ef.get(5).is_err());
    }

    #[test]
    fn ef_aggregate_iter_and_index() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let ef = ElementField::new(&fes, vec!["k".into()]).unwrap();
        // Iteration walks all subfields in order.
        let counts: Vec<usize> = ef
            .into_iter()
            .map(|h| read(h).unwrap().gauss_count())
            .collect();
        assert_eq!(counts, vec![3, 4]);
        // Indexing matches subfield().
        let _h = &ef[0];
    }

    #[test]
    fn ef_subfields_are_mutated_independently() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let ef = ElementField::new(&fes, vec!["k".into()]).unwrap();
        write(&ef.get(0).unwrap())
            .unwrap()
            .set_uniform("k", 1.5)
            .unwrap();
        write(&ef.get(1).unwrap())
            .unwrap()
            .set_uniform("k", 2.5)
            .unwrap();
        assert_eq!(
            read(&ef.get(0).unwrap()).unwrap().value(0, 0, "k").unwrap(),
            1.5
        );
        assert_eq!(
            read(&ef.get(1).unwrap()).unwrap().value(0, 0, "k").unwrap(),
            2.5
        );
    }

    #[test]
    fn ef_debug_and_display() {
        let mesh = make_mesh_with_tri_and_qua();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let ef = ElementField::new(&fes, vec!["k".into()]).unwrap();
        let d = format!("{:?}", ef);
        assert!(d.contains("ElementField"));
        let s = format!("{}", ef);
        assert!(s.contains("ElementField"));
        assert!(s.contains("2 subfield"));
    }
}