pyrucast 0.6.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
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
//! Linear (small-strain) elasticity — `K = ∫ Bᵀ D B dΩ`.
//!
//! Works in 2-D (TRI3 / QUA4) and 3-D (TET4 / HEX8). 2-D supports **plane
//! stress**, **plane strain** and **axisymmetric**; 3-D is the full solid.
//! Voigt convention, with **engineering** shear `γ = 2ε` and stress in the
//! matching order:
//!
//! | kinematics | Voigt vector |
//! |---|---|
//! | plane stress / plane strain | `[εxx, εyy, γxy]` |
//! | axisymmetric | `[εrr, εzz, εθθ, γrz]`, named `[εxx, εyy, εzz, γxy]` |
//! | solid | `[εxx, εyy, εzz, γyz, γxz, γxy]` |
//!
//! The axisymmetric naming follows Cast3M: `x = r`, `y = z` (axis of
//! revolution) and the **`zz` component is the hoop** `θθ`, whose strain is
//! `ε_θθ = u_r / r`. It requires an axisymmetric geometry
//! ([`Coords::axisymmetric`](crate::coords::Coords::axisymmetric)),
//! which is also what puts the `2πr` in the integration measure.
//!
//! Primal `u_x, u_y(, u_z)` (displacement), dual `f_x, …` (nodal force).
//! Material components `E` (Young) and `nu` (Poisson).

use crate::containers::element_field::SubElementField;
use crate::containers::field::ABSENT_COMPONENT;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::matrix::DofOrdering;
use crate::containers::mesh::SubMesh;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::kernel::MAX_CELL_DOFS;
use crate::models::owned_components;
use crate::models::symmetry::{self, MaterialSymmetry};
use crate::models::tensor::{dual_name, primal_name, stress_names, Kinematics};
use crate::models::ZoneLayout;
use crate::models::{CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use crate::models::{ElementLayout, MatrixKind};
use serde::{Deserialize, Serialize};

/// Material components required by **isotropic** linear elasticity.
const MATERIAL_COMPONENTS: &[&str] = &["E", "nu"];
/// Material an elastic law **accepts without requiring**: the thermal-expansion
/// coefficient, consumed by
/// [`thermal_strain`](fn@crate::ops::element_field::thermal_strain), and the
/// density, which only the mass matrix wants. The order is the contract —
/// `ElementLayout::optional_material` resolves it slot for slot.
/// Six lignes de Voigt sur la largeur d'une maille — la forme des tampons `B` et
/// `D·B`. Sur la **pile** : un point de Gauss n'alloue rien, et six `Vec` par
/// point, c'est ce que coûtait la version précédente.
type VoigtRows = [[f64; MAX_CELL_DOFS]; 6];

const OPTIONAL_COMPONENTS: &[&str] = &["alpha", "rho"];
/// Position of `rho` in [`OPTIONAL_COMPONENTS`].
const RHO_SLOT: usize = 1;
/// Orthotropic constants plus the in-plane material axis (2-D).
const ORTHOTROPIC_2D: &[&str] = &[
    "E_1", "E_2", "E_3", "nu_12", "nu_13", "nu_23", "G_12", "G_13", "G_23", "V1X", "V1Y",
];
/// Orthotropic constants plus the two material axes (3-D).
const ORTHOTROPIC_3D: &[&str] = &[
    "E_1", "E_2", "E_3", "nu_12", "nu_13", "nu_23", "G_12", "G_13", "G_23", "V1X", "V1Y", "V1Z",
    "V2X", "V2Y", "V2Z",
];
/// The 21 anisotropic constants plus the in-plane material axis (2-D).
const ANISOTROPIC_2D: &[&str] = &[
    "C_11", "C_12", "C_13", "C_14", "C_15", "C_16", "C_22", "C_23", "C_24", "C_25", "C_26", "C_33",
    "C_34", "C_35", "C_36", "C_44", "C_45", "C_46", "C_55", "C_56", "C_66", "V1X", "V1Y",
];
/// The 21 anisotropic constants plus the two material axes (3-D).
const ANISOTROPIC_3D: &[&str] = &[
    "C_11", "C_12", "C_13", "C_14", "C_15", "C_16", "C_22", "C_23", "C_24", "C_25", "C_26", "C_33",
    "C_34", "C_35", "C_36", "C_44", "C_45", "C_46", "C_55", "C_56", "C_66", "V1X", "V1Y", "V1Z",
    "V2X", "V2Y", "V2Z",
];

/// The material contract of a symmetry in a space of dimension `space_dim`:
/// the constants of the law, followed by the frame components it needs. Because
/// the assembler resolves a material zone by its **required component set**
/// ([`crate::ops::matrix::assemble_kind`]), these disjoint contracts let an
/// isotropic and an orthotropic zone live on one mesh without any consolidation.
pub(crate) fn material_contract(
    symmetry: MaterialSymmetry,
    space_dim: usize,
) -> &'static [&'static str] {
    match (symmetry, space_dim) {
        (MaterialSymmetry::Isotropic, _) => MATERIAL_COMPONENTS,
        (MaterialSymmetry::Orthotropic, 2) => ORTHOTROPIC_2D,
        (MaterialSymmetry::Orthotropic, _) => ORTHOTROPIC_3D,
        (MaterialSymmetry::Anisotropic, 2) => ANISOTROPIC_2D,
        (MaterialSymmetry::Anisotropic, _) => ANISOTROPIC_3D,
    }
}

/// Voigt component count: 3 in 2-D plane, **4** axisymmetric (the hoop joins
/// them), 6 in 3-D.
fn voigt_size(space_dim: usize, kinematics: Kinematics) -> usize {
    match (space_dim, kinematics) {
        (2, Kinematics::Axisymmetric) => 4,
        (2, _) => 3,
        _ => 6,
    }
}

/// Reject an FE subspace whose elements are a **manifold** in their space
/// (`ref_dim < space_dim`) for a continuum-mechanics physics named `label`.
///
/// The continuum kernels build `B` from `∂N_i/∂x_a`, which on a manifold is the
/// *tangent* gradient: the resulting `Bᵀ D B` would be rank-deficient in the
/// normal direction and silently meaningless. A boundary sub-mesh (`SEG2` in
/// 2-D, `TRI3` in 3-D) is a support for loads
/// ([`flux`](fn@crate::ops::node_field::flux)) or convection, not a solid — and a
/// structural element (bar, beam) is a different physics with its own kernel.
/// Shared by [`Elasticity`], [`Plasticity`](crate::models::plasticity) and
/// [`Mazars`](crate::models::damage).
pub(crate) fn check_continuum_dimensions(
    label: &str,
    space_dim: usize,
    ref_dim: usize,
) -> Result<()> {
    if ref_dim != space_dim {
        return Err(PyrucastError::Message(format!(
            "{label}: a {ref_dim}-D element in a {space_dim}-D space is a manifold, not a \
             solid — a boundary mesh carries loads (flux, convection), and a bar or beam \
             is a structural physics of its own (truss, frame, timoshenko)"
        )));
    }
    Ok(())
}

/// Linear-elasticity physics on an FE subspace.
///
/// Material data is supplied at assembly time via
/// [`crate::ops::matrix::stiffness`], not stored here — `E`, `nu` for the
/// isotropic default, the orthotropic or anisotropic constants plus the material
/// axes otherwise (see [`crate::models::symmetry`]).
///
/// Two orthogonal axes: `kinematics` is the **kinematic** hypothesis (plane stress,
/// plane strain, axisymmetric, solid) and `symmetry` is the **material** one.
/// They combine freely — an orthotropic axisymmetric body is as ordinary as an
/// isotropic plane one.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Interpolation, Node};
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::{Domain, SubModelKind};
/// # 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.iter().map(|x| x.id()).collect::<Vec<_>>()).unwrap();
/// # let maillage = Mesh::from_submesh(sm);
/// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # use pyrucast::models::elasticity::Elasticity;
/// # use pyrucast::models::tensor::Kinematics;
/// let e = Elasticity::new(zone.clone(), Kinematics::PlaneStress)?;
/// assert_eq!(e.material_components(), vec!["E".to_string(), "nu".to_string()]);
/// // La dilatation thermique est **facultative** : sans `alpha`, le modèle
/// // s'assemble sans elle.
/// assert!(e.optional_material_components().contains(&"alpha"));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Serialize, Deserialize)]
pub struct Elasticity {
    pub(crate) fespace: Handle<SubFiniteElementSpace>,
    /// POI1 support over the subspace's unique nodes (row/col support).
    pub(crate) support: Handle<SubMesh>,
    pub(crate) space_dim: usize,
    pub(crate) kinematics: Kinematics,
    pub(crate) symmetry: MaterialSymmetry,
}

impl Elasticity {
    /// **Isotropic** linear elasticity on an FE subspace, with the given
    /// 2-D/3-D kinematics. Errors if `kinematics` is inconsistent with the space dimension.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Interpolation, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::{Domain, SubModelKind};
    /// # 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.iter().map(|x| x.id()).collect::<Vec<_>>()).unwrap();
    /// # let maillage = Mesh::from_submesh(sm);
    /// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # use pyrucast::models::elasticity::Elasticity;
    /// # use pyrucast::models::tensor::Kinematics;
    /// let e = Elasticity::new(zone.clone(), Kinematics::PlaneStress)?;
    /// assert_eq!(e.material_components(), vec!["E".to_string(), "nu".to_string()]);
    /// // La dilatation thermique est **facultative** : sans `alpha`, le modèle
    /// // s'assemble sans elle.
    /// assert!(e.optional_material_components().contains(&"alpha"));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn new(fespace: Handle<SubFiniteElementSpace>, kinematics: Kinematics) -> Result<Self> {
        Self::with_symmetry(fespace, kinematics, MaterialSymmetry::Isotropic)
    }

    /// Linear elasticity with an explicit material symmetry — the general
    /// constructor, of which [`new`](Self::new) is the isotropic case.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Interpolation, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::{Domain, SubModelKind};
    /// # 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.iter().map(|x| x.id()).collect::<Vec<_>>()).unwrap();
    /// # let maillage = Mesh::from_submesh(sm);
    /// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # use pyrucast::models::elasticity::Elasticity;
    /// # use pyrucast::models::tensor::Kinematics;
    /// # use pyrucast::models::symmetry::MaterialSymmetry;
    /// // Le constructeur général : une symétrie orthotrope élargit le contrat
    /// // matériau, qui porte alors les modules **et** les axes.
    /// let o = Elasticity::with_symmetry(
    ///     zone.clone(), Kinematics::PlaneStress, MaterialSymmetry::Orthotropic)?;
    /// assert!(o.material_components().len() > 2);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    /// # use pyrucast::models::tensor::Kinematics;
    pub fn with_symmetry(
        fespace: Handle<SubFiniteElementSpace>,
        kinematics: Kinematics,
        symmetry: MaterialSymmetry,
    ) -> Result<Self> {
        let (submesh, space_dim, ref_dim, axisymmetric) = {
            let s = fespace.read();
            (
                s.submesh(),
                s.space_dim(),
                s.ref_dim()?,
                s.is_axisymmetric(),
            )
        };
        check_continuum_dimensions("Elasticity", space_dim, ref_dim)?;
        #[allow(clippy::match_like_matches_macro)]
        let ok = match (space_dim, kinematics) {
            (2, Kinematics::PlaneStress | Kinematics::PlaneStrain) => true,
            (2, Kinematics::Axisymmetric) => true,
            (3, Kinematics::Full3D) => true,
            _ => false,
        };
        if !ok {
            return Err(PyrucastError::Message(format!(
                "Elasticity: kinematics {kinematics:?} is incompatible with a {space_dim}-D space \
                 (2-D ⇒ plane_stress|plane_strain|axisymmetric, 3-D ⇒ full_3d)"
            )));
        }
        // The kinematics and the geometry must agree **both ways**: the 2πr measure
        // comes from the Coords while the hoop row comes from the kinematics, so a
        // mismatch would silently mix a plane constitutive law with a revolved
        // measure (or the reverse) and quietly produce wrong results.
        if axisymmetric != kinematics.is_axisymmetric() {
            return Err(PyrucastError::Message(if axisymmetric {
                format!(
                    "Elasticity: kinematics {kinematics:?} on an axisymmetric geometry — a body of \
                     revolution requires the `axisymmetric` kinematics (its integrals already \
                     carry the 2πr factor)"
                )
            } else {
                "Elasticity: the `axisymmetric` kinematics requires an axisymmetric geometry \
                 (build the Coords with Coords::axisymmetric)"
                    .into()
            }));
        }
        let support = submesh.read().to_poi1()?;
        Ok(Self {
            fespace,
            support,
            space_dim,
            kinematics,
            symmetry,
        })
    }
}

impl SubModelKind for Elasticity {
    fn primal_vars(&self) -> Vec<String> {
        (0..self.space_dim).map(primal_name).collect()
    }

    fn dual_vars(&self) -> Vec<String> {
        (0..self.space_dim).map(dual_name).collect()
    }

    fn as_domain(&self) -> Option<&dyn Domain> {
        Some(self)
    }

    fn stiffness_layout(&self) -> Option<MatrixLayout> {
        Some(MatrixLayout {
            fespaces: vec![self.fespace.clone()],
            support: self.support.clone(),
            dual_vars: self.dual_vars(),
            primal_vars: self.primal_vars(),
            ordering: DofOrdering::NodesThenVars,
            symmetric: true,
        })
    }

    /// The consistent mass matrix shares the stiffness layout (same fespace,
    /// support, DOF numbering) — only the kernel differs.
    fn mass_layout(&self) -> Option<MatrixLayout> {
        self.stiffness_layout()
    }

    /// The geometric (initial-stress) stiffness shares the stiffness layout.
    fn geometric_layout(&self) -> Option<MatrixLayout> {
        self.stiffness_layout()
    }

    /// Linear elasticity: the consistent tangent **is** the elastic stiffness.
    fn tangent_layout(&self) -> Option<MatrixLayout> {
        self.stiffness_layout()
    }

    fn physics(&self) -> &'static [Physics] {
        &[Physics::Mechanical]
    }

    fn label(&self) -> &'static str {
        "Elasticity"
    }

    fn render(&self, _opts: &DumpOptions) -> String {
        let primal = self.primal_vars().join(", ");
        let dual = self.dual_vars().join(", ");
        let n = self.support.read().cell_count();
        format!(
            "SubModel<Elasticity({:?}, {})>\n  primal var(s): {primal}\n  dual var(s):   {dual}\n  \
             support: {n} node(s)",
            self.kinematics, self.symmetry
        )
    }
}

impl Domain for Elasticity {
    fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
        self.fespace.clone()
    }

    fn material_components(&self) -> Vec<String> {
        owned_components(material_contract(self.symmetry, self.space_dim))
    }

    /// `alpha` (thermal-expansion coefficient) — accepted through the material
    /// field when doing thermomechanics, never required for a plain elastic
    /// assembly. Consumed by
    /// [`crate::ops::element_field::thermal_strain`](fn@crate::ops::element_field::thermal_strain).
    fn optional_material_components(&self) -> &'static [&'static str] {
        OPTIONAL_COMPONENTS
    }

    fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
        self.fespace.clone()
    }

    fn behavior_output_components(&self) -> Vec<String> {
        stress_names(self.space_dim, self.kinematics)
    }

    /// Linear stress σ = D·ε at one Gauss point (material constants per cell).
    fn deformation_reads(&self) -> Vec<String> {
        strain_reads(self.space_dim, self.kinematics)
    }

    fn integrate_point(
        &self,
        _geom: &CellGeom,
        _g: usize,
        lay: &ZoneLayout,
        deformation: &[f64],
        _prev: &[f64],
        material: &[f64],
        _dt: f64,
        out: &mut [f64],
    ) -> Result<()> {
        let d = self.space_dim;
        let mut dmat = [[0.0_f64; 6]; 6];
        let v = symmetry::elastic_constitutive_into(
            material,
            &lay.material,
            self.symmetry,
            self.kinematics,
            d,
            &mut dmat,
        )?;
        let strain = read_voigt_strain(deformation, &lay.deformation, d, self.kinematics);
        for r in 0..v {
            out[r] = (0..v).map(|c| dmat[r][c] * strain[c]).sum();
        }
        Ok(())
    }

    /// The geometric stiffness reads the current stress; the consistent tangent,
    /// the algorithmic moduli the integrator wrote. Both are declared here, once
    /// per zone, so the kernels below index instead of searching.
    fn element_state_reads(&self, kind: MatrixKind) -> Vec<String> {
        match kind {
            // La tangente d'une loi **linéaire** est sa raideur : elle lit le
            // matériau, et surtout pas des modules algorithmiques qu'aucun
            // intégrateur n'a écrits ici. Déclarer ces lectures ferait échouer
            // la résolution de zone sur un état qui n'a aucune raison de les
            // porter — c'est la plasticité qui les produit, pas l'élasticité.
            MatrixKind::Tangent => Vec::new(),
            _ => continuum_element_state_reads(kind, self.space_dim, self.kinematics),
        }
    }

    fn element_geometric(
        &self,
        geoms: &[CellGeom],
        _material: &SubElementField,
        lay: &ElementLayout,
        state: &SubElementField,
        ke: &mut [f64],
    ) -> Result<()> {
        element_geometric(&geoms[0], state, lay, ke)
    }

    fn element_tangent(
        &self,
        geoms: &[CellGeom],
        material: &SubElementField,
        lay: &ElementLayout,
        _state: &SubElementField,
        ke: &mut [f64],
    ) -> Result<()> {
        // A linear law's consistent tangent **is** its elastic stiffness, so it
        // reads the material and ignores the moduli it also declared.
        element_stiffness(&geoms[0], material, lay, self.kinematics, self.symmetry, ke)
    }

    fn element_matrix(
        &self,
        geoms: &[CellGeom],
        material: &SubElementField,
        lay: &ElementLayout,
        ke: &mut [f64],
    ) -> Result<()> {
        element_stiffness(&geoms[0], material, lay, self.kinematics, self.symmetry, ke)
    }

    fn element_mass(
        &self,
        geoms: &[CellGeom],
        material: &SubElementField,
        lay: &ElementLayout,
        ke: &mut [f64],
    ) -> Result<()> {
        element_mass(&geoms[0], material, lay, ke)
    }
}

/// Isotropic constitutive (Voigt) matrix `D` from `E`, `nu` and the kinematics.
///
/// ```
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::models::elasticity::{self};
/// // Contraintes planes : σ_zz = 0, la souplesse hors plan est condensée.
/// // Déformations planes : ε_zz = 0, le matériau est plus **raide**.
/// let cp = elasticity::constitutive(210e3, 0.3, Kinematics::PlaneStress, 2);
/// let dp = elasticity::constitutive(210e3, 0.3, Kinematics::PlaneStrain, 2);
/// assert!(dp[0][0] > cp[0][0]);
/// // En contraintes planes, D₀₀ = E/(1−ν²).
/// assert!((cp[0][0] - 210e3 / (1.0 - 0.09)).abs() < 1e-6);
/// // Le bloc de cisaillement vaut μ dans les deux cas (Voigt de l'ingénieur).
/// assert!((cp[2][2] - dp[2][2]).abs() < 1e-6);
/// ```
/// # use pyrucast::models::tensor::Kinematics;
pub fn constitutive(e: f64, nu: f64, kinematics: Kinematics, space_dim: usize) -> Vec<Vec<f64>> {
    let mut d = [[0.0_f64; 6]; 6];
    let v = constitutive_into(e, nu, kinematics, space_dim, &mut d);
    d[..v].iter().map(|r| r[..v].to_vec()).collect()
}

/// [`constitutive`] writing into a caller-owned buffer, returning the Voigt size
/// `v` it filled (`d[..v][..v]`).
///
/// The form a constitutive kernel calls: at most thirty-six numbers, on the
/// stack. Building two levels of `Vec` for them is nothing once per assembly and
/// a great deal once per Gauss point of every iteration.
/// ```
/// # use pyrucast::models::elasticity;
/// # use pyrucast::models::tensor::Kinematics;
/// // La même matrice que `constitutive`, écrite sur la pile : `v` dit
/// // combien de lignes et de colonnes ont été remplies.
/// let mut d = [[0.0_f64; 6]; 6];
/// let v = elasticity::constitutive_into(210_000.0, 0.3, Kinematics::PlaneStress, 2, &mut d);
/// assert_eq!(v, 3);
/// let attendu = elasticity::constitutive(210_000.0, 0.3, Kinematics::PlaneStress, 2);
/// assert_eq!(d[0][..v], attendu[0][..]);
/// ```
pub fn constitutive_into(
    e: f64,
    nu: f64,
    kinematics: Kinematics,
    space_dim: usize,
    d: &mut [[f64; 6]; 6],
) -> usize {
    match (space_dim, kinematics) {
        (2, Kinematics::PlaneStress) => {
            let c = e / (1.0 - nu * nu);
            d[0][0] = c;
            d[0][1] = c * nu;
            d[1][0] = c * nu;
            d[1][1] = c;
            d[2][2] = c * (1.0 - nu) / 2.0;
            3
        }
        (2, Kinematics::PlaneStrain) => {
            let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
            d[0][0] = c * (1.0 - nu);
            d[0][1] = c * nu;
            d[1][0] = c * nu;
            d[1][1] = c * (1.0 - nu);
            d[2][2] = c * (1.0 - 2.0 * nu) / 2.0;
            3
        }
        (2, Kinematics::Axisymmetric) => {
            // Voigt order [rr, zz, θθ, rz]: the three normal directions are
            // mutually orthogonal, so the 3×3 normal block is the isotropic one
            // (as in plane strain, with θθ restored) and `rz` is the lone shear.
            let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
            let (d_n, d_off) = (c * (1.0 - nu), c * nu);
            for i in 0..3 {
                for j in 0..3 {
                    d[i][j] = if i == j { d_n } else { d_off };
                }
            }
            d[3][3] = c * (1.0 - 2.0 * nu) / 2.0;
            4
        }
        _ => {
            // 3-D solid (Voigt order [xx, yy, zz, yz, xz, xy]).
            let c = e / ((1.0 + nu) * (1.0 - 2.0 * nu));
            let g = c * (1.0 - 2.0 * nu) / 2.0;
            for i in 0..3 {
                for j in 0..3 {
                    d[i][j] = if i == j { c * (1.0 - nu) } else { c * nu };
                }
            }
            d[3][3] = g;
            d[4][4] = g;
            d[5][5] = g;
            6
        }
    }
}

/// The strain components a continuum law reads, **in Voigt order** — the
/// convention its indices assume, declared for
/// [`crate::models::Domain::deformation_reads`].
///
/// Axisymmetry is the odd one: its fourth slot is the *measured* hoop `eps_zz`,
/// produced by
/// [`crate::ops::element_field::deformation`](fn@crate::ops::element_field::deformation),
/// not an assumption.
pub(crate) fn strain_reads(space_dim: usize, kinematics: Kinematics) -> Vec<String> {
    let names: &[&str] = if space_dim == 2 && kinematics.is_axisymmetric() {
        &["eps_xx", "eps_yy", "eps_zz", "eps_xy"]
    } else if space_dim == 2 {
        &["eps_xx", "eps_yy", "eps_xy"]
    } else {
        &["eps_xx", "eps_yy", "eps_zz", "eps_yz", "eps_xz", "eps_xy"]
    };
    owned_components(names)
}

/// How many **normal** components open the Voigt order: the ones the
/// engineering convention leaves alone. The shears that follow are doubled
/// (`γ = 2ε`), which is what a `D` matrix in engineering Voigt expects.
pub(crate) fn normal_count(space_dim: usize, kinematics: Kinematics) -> usize {
    if space_dim == 2 && kinematics.is_axisymmetric() {
        3
    } else if space_dim == 2 {
        2
    } else {
        3
    }
}

/// Read the engineering-Voigt strain of one Gauss point out of its row.
///
/// No name, no allocation: the row is the field's own buffer and `idx` says
/// where each component sits, resolved once for the zone.
pub(crate) fn read_voigt_strain(
    deformation: &[f64],
    idx: &[u32],
    space_dim: usize,
    kinematics: Kinematics,
) -> [f64; 6] {
    let mut eps = [0.0_f64; 6];
    let n = normal_count(space_dim, kinematics);
    for (r, &i) in idx.iter().enumerate() {
        let v = deformation[i as usize];
        eps[r] = if r < n { v } else { 2.0 * v };
    }
    eps
}

/// Strain-displacement matrix `B` (Voigt) from `∂N_i/∂x_a` (`dn_dx`, layout
/// `[i*space_dim + a]`). Shape `voigt_size × (space_dim·nodes)`, node-major
/// columns (matching [`DofOrdering::NodesThenVars`]).
///
/// `hoop` carries the axisymmetric extra: `Some((N, r))` — the shape values and
/// the radius at the Gauss point — adds the fourth row `ε_θθ = Σ_i N_i u_{r,i} / r`
/// and orders the rows `[rr, zz, θθ, rz]`. `None` gives the plane / solid `B`.
fn b_matrix_into(
    dn_dx: &[f64],
    n_nodes: usize,
    space_dim: usize,
    hoop: Option<(&[f64], f64)>,
    b: &mut VoigtRows,
) -> usize {
    let v = match hoop {
        Some(_) => 4,
        None => voigt_size(space_dim, Kinematics::PlaneStrain),
    };
    let dofs = space_dim * n_nodes;
    for row in b[..v].iter_mut() {
        row[..dofs].fill(0.0);
    }
    let dn = |i: usize, a: usize| dn_dx[i * space_dim + a];
    for i in 0..n_nodes {
        if let Some((n, r)) = hoop {
            let (cr, cz) = (2 * i, 2 * i + 1);
            b[0][cr] = dn(i, 0); // εrr
            b[1][cz] = dn(i, 1); // εzz
            b[2][cr] = n[i] / r; // εθθ = u_r / r
            b[3][cr] = dn(i, 1); // γrz
            b[3][cz] = dn(i, 0);
        } else if space_dim == 2 {
            let (cx, cy) = (2 * i, 2 * i + 1);
            b[0][cx] = dn(i, 0); // εxx
            b[1][cy] = dn(i, 1); // εyy
            b[2][cx] = dn(i, 1); // γxy
            b[2][cy] = dn(i, 0);
        } else {
            let (cx, cy, cz) = (3 * i, 3 * i + 1, 3 * i + 2);
            b[0][cx] = dn(i, 0); // εxx
            b[1][cy] = dn(i, 1); // εyy
            b[2][cz] = dn(i, 2); // εzz
            b[3][cy] = dn(i, 2); // γyz
            b[3][cz] = dn(i, 1);
            b[4][cx] = dn(i, 2); // γxz
            b[4][cz] = dn(i, 0);
            b[5][cx] = dn(i, 1); // γxy
            b[5][cy] = dn(i, 0);
        }
    }
    v
}

/// Element kernel: local stiffness `K_e = Σ_g (Bᵀ D B) |J| w` of one cell,
/// written into `ke` (flat row-major, side `space_dim·n_nodes`, **node-major /
/// component-minor** dof order `dof = node·space_dim + component`). Pure and
/// sequential — driven in parallel by [`crate::models::kernel::assemble_block`].
/// Reused as-is by [`crate::models::plasticity`] and [`crate::models::damage`]
/// (their iteration operator is the elastic stiffness).
///
/// ```
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::elasticity::{self};
/// # use pyrucast::models::kernel::assemble_block;
/// # use pyrucast::models::symmetry::MaterialSymmetry;
/// # 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 fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let vars = || (vec!["f_x".to_string(), "f_y".to_string()],
/// #                vec!["u_x".to_string(), "u_y".to_string()]);
/// # let mat = Handle::new(SubElementField::from_uniform_per_component(
/// #     zone.clone(), vec!["E".into(), "nu".into()], &[210_000.0, 0.3])?);
/// // Le noyau d'élément, tel que le pilote l'appelle. La matrice de
/// // raideur d'un solide libre est **singulière** : ses lignes somment à
/// // zéro, un mouvement de corps rigide n'engendrant aucune force.
/// # use pyrucast::models::ElementLayout;
/// // `E` puis `nu` : le champ est rangé dans l'ordre du contrat, donc la
/// // table est l'identité. Un vrai assemblage la ferait résoudre par
/// // `Domain::element_layout`, qui accepte n'importe quel ordre.
/// let lay = ElementLayout { material: vec![0, 1], optional_material: vec![], state: vec![] };
/// let (duals, primals) = vars();
/// let bloc = assemble_block(
///     std::slice::from_ref(&zone), &support, &support, duals, primals,
///     DofOrdering::NodesThenVars, true, &mat, None,
///     |geoms, m, _s, ke| elasticity::element_stiffness(
///         &geoms[0], m, &lay, Kinematics::PlaneStress,
///         MaterialSymmetry::Isotropic, ke),
/// )?;
/// let total: f64 = bloc.iter_entries().into_iter().map(|(_, _, _, _, v)| v).sum();
/// assert!(total.abs() < 1e-6);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_stiffness(
    geom: &CellGeom,
    material: &SubElementField,
    lay: &ElementLayout,
    kinematics: Kinematics,
    symmetry: MaterialSymmetry,
    ke: &mut [f64],
) -> Result<()> {
    let n_nodes = geom.n_nodes;
    let space_dim = geom.space_dim;
    let dofs = space_dim * n_nodes;
    // Constants read at Gauss 0 — constant material per cell — **by index**:
    // the zone resolved the names once, so this matches none and allocates
    // nothing.
    let mut d = [[0.0_f64; 6]; 6];
    let v = symmetry::elastic_constitutive_into(
        material.row(geom.cell, 0),
        &lay.material,
        symmetry,
        kinematics,
        space_dim,
        &mut d,
    )?;
    // Les trois tampons vivent **hors** de la boucle : un point de Gauss ne
    // doit rien allouer, et `B` comme `D·B` sont de taille fixe une fois la
    // maille connue.
    let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
    let mut b: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
    let mut db: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
    for g in 0..geom.n_gauss {
        // On a body of revolution the hoop row needs `N` and `r` at this point.
        let hoop = if kinematics.is_axisymmetric() {
            Some((geom.n_at_g(g), geom.radius(g)))
        } else {
            None
        };
        geom.dn_dx(g, &mut dn_buf[..n_nodes * space_dim])?;
        b_matrix_into(
            &dn_buf[..n_nodes * space_dim],
            n_nodes,
            space_dim,
            hoop,
            &mut b,
        );
        // DB = D·B  (voigt × dofs).
        for r in 0..v {
            for c in 0..dofs {
                let mut acc = 0.0;
                for w in 0..v {
                    acc += d[r][w] * b[w][c];
                }
                db[r][c] = acc;
            }
        }
        let w = geom.det_j_w(g);
        for r in 0..dofs {
            for c in 0..dofs {
                let mut acc = 0.0;
                for vv in 0..v {
                    acc += b[vv][r] * db[vv][c];
                }
                ke[r * dofs + c] += acc * w;
            }
        }
    }
    Ok(())
}

/// Element kernel: local **consistent mass** `M_e = Σ_g ρ (Nᵀ N) |J| w` of one
/// cell, written into `ke` (same flat row-major, **node-major / component-minor**
/// dof order as [`element_stiffness`]). The vector shape-function matrix is
/// block-diagonal, so `M[(i,a),(j,b)] = δ_ab ρ ∫ N_i N_j`. Density `ρ` is read
/// from the material component `rho` (constant per cell). Pure and sequential,
/// law-independent — reused as-is by [`crate::models::plasticity`] and
/// [`crate::models::damage`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::elasticity::{self};
/// # use pyrucast::models::kernel::assemble_block;
/// # use pyrucast::models::symmetry::MaterialSymmetry;
/// # 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 fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let vars = || (vec!["f_x".to_string(), "f_y".to_string()],
/// #                vec!["u_x".to_string(), "u_y".to_string()]);
/// # let mat = Handle::new(SubElementField::from_uniform_per_component(
/// #     zone.clone(), vec!["rho".into()], &[2.0])?);
/// # use pyrucast::models::ElementLayout;
/// # use pyrucast::containers::field::ABSENT_COMPONENT;
/// // La masse ne lit que `rho`, deuxième composante **facultative** du
/// // contrat élastique (`["alpha", "rho"]`) : `alpha` est absente ici.
/// let lay = ElementLayout {
///     material: vec![],
///     optional_material: vec![ABSENT_COMPONENT, 0],
///     state: vec![],
/// };
/// // La masse totale se retrouve dans la somme des entrées, une fois par
/// // direction : ρ × aire × space_dim.
/// let (duals, primals) = vars();
/// let bloc = assemble_block(
///     std::slice::from_ref(&zone), &support, &support, duals, primals,
///     DofOrdering::NodesThenVars, true, &mat, None,
///     |geoms, m, _s, ke| elasticity::element_mass(&geoms[0], m, &lay, ke),
/// )?;
/// let total: f64 = bloc.iter_entries().into_iter().map(|(_, _, _, _, v)| v).sum();
/// assert!((total - 2.0 * 0.5 * 2.0).abs() < 1e-9);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_mass(
    geom: &CellGeom,
    material: &SubElementField,
    lay: &ElementLayout,
    ke: &mut [f64],
) -> Result<()> {
    let n_nodes = geom.n_nodes;
    let space_dim = geom.space_dim;
    let dofs = space_dim * n_nodes;
    // `rho` is the second optional component of the elastic contract
    // (`["alpha", "rho"]`): absent, there is no mass to integrate.
    let i_rho = lay.optional_material[RHO_SLOT];
    if i_rho == ABSENT_COMPONENT {
        return Err(PyrucastError::Message(
            "Elasticity mass matrix: material component `rho` (density) is required".into(),
        ));
    }
    let rho = material.row(geom.cell, 0)[i_rho as usize];
    for g in 0..geom.n_gauss {
        let n = geom.n_at_g(g);
        let w = geom.det_j_w(g) * rho;
        for i in 0..n_nodes {
            for j in 0..n_nodes {
                let m = n[i] * n[j] * w;
                for a in 0..space_dim {
                    let r = i * space_dim + a;
                    let c = j * space_dim + a;
                    ke[r * dofs + c] += m;
                }
            }
        }
    }
    Ok(())
}

/// Element kernel: local **geometric (initial-stress) stiffness**
///   `Kg[(i,a),(j,b)] = δ_ab Σ_g Σ_cd (∂N_i/∂x_c) σ_cd (∂N_j/∂x_e) |J| w`
/// of one cell, written into `ke` (same flat, node-major / component-minor dof
/// order as [`element_stiffness`]). The scalar `∇N_i·σ·∇N_j` is applied to each
/// displacement component's diagonal block (`δ_ab`). The current Cauchy stress
/// `σ` (Voigt-named) is read from `state` per Gauss point. Pure and sequential,
/// law-independent — reused as-is by [`crate::models::plasticity`] and
/// [`crate::models::damage`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::elasticity::{self};
/// # use pyrucast::models::kernel::assemble_block;
/// # use pyrucast::models::symmetry::MaterialSymmetry;
/// # 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 fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let vars = || (vec!["f_x".to_string(), "f_y".to_string()],
/// #                vec!["u_x".to_string(), "u_y".to_string()]);
/// # let etat = Handle::new(SubElementField::from_uniform_per_component(
/// #     zone.clone(),
/// #     vec!["sigma_xx".into(), "sigma_yy".into(), "sigma_xy".into()],
/// #     &[100.0, 0.0, 0.0])?);
/// # // La raideur géométrique ne lit aucun matériau ; l'assembleur en veut
/// # // un, on lui en donne un qui ne sert à rien plutôt qu'une Option.
/// # let bidon = Handle::new(SubElementField::from_uniform_per_component(
/// #     zone.clone(), vec!["E".into(), "nu".into()], &[210_000.0, 0.3])?);
/// # use pyrucast::models::ElementLayout;
/// // La convention lit `[σ_xx, σ_xy, σ_yy]` ; le champ ci-dessus est rangé
/// // `[σ_xx, σ_yy, σ_xy]`. C'est tout ce que la table absorbe.
/// let lay = ElementLayout {
///     material: vec![],
///     optional_material: vec![],
///     state: vec![0, 2, 1],
/// };
/// // La raideur **géométrique**, celle du flambement : elle vient de l'état
/// // de contrainte, non du matériau. Sous traction elle est définie
/// // positive ; c'est son signe qui décide de la charge critique.
/// let (duals, primals) = vars();
/// let bloc = assemble_block(
///     std::slice::from_ref(&zone), &support, &support, duals, primals,
///     DofOrdering::NodesThenVars, true, &bidon, Some(&etat),
///     |geoms, _m, s, ke| elasticity::element_geometric(&geoms[0], s.unwrap(), &lay, ke),
/// )?;
/// // Elle est singulière elle aussi : les modes rigides n'y coûtent rien.
/// let total: f64 = bloc.iter_entries().into_iter().map(|(_, _, _, _, v)| v).sum();
/// assert!(total.abs() < 1e-9);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_geometric(
    geom: &CellGeom,
    stress: &SubElementField,
    lay: &ElementLayout,
    ke: &mut [f64],
) -> Result<()> {
    let n_nodes = geom.n_nodes;
    let d = geom.space_dim;
    let dofs = d * n_nodes;
    // `lay.state` is the Voigt stress in `stress_matrix_reads` order, closed by
    // the hoop `σ_θθ` on a body of revolution — resolved once for the zone.
    let lay = &lay.state;
    let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
    for g in 0..geom.n_gauss {
        let dn = &mut dn_buf[..n_nodes * d]; // [i * d + c]
        geom.dn_dx(g, dn)?;
        let w = geom.det_j_w(g);
        let row = stress.row(geom.cell, g);
        let mut sig = [0.0_f64; 9]; // [c * d + e]
        crate::models::voigt_stress_matrix(row, lay, d, &mut sig);
        // On a body of revolution the hoop strain's own non-linear part,
        // ½(u_r/r)², contributes `σ_θθ N_i N_j / r²` on the radial diagonal —
        // the initial-stress counterpart of the `N_i / r` row of `B`.
        let hoop = if geom.axisymmetric {
            let r = geom.radius(g);
            // The hoop closes the read list, exactly as it does for `Bᵀσ`.
            Some((geom.n_at_g(g), row[lay[lay.len() - 1] as usize] / (r * r)))
        } else {
            None
        };
        for i in 0..n_nodes {
            for j in 0..n_nodes {
                // Scalar gᵢⱼ = Σ_{c,e} (∂N_i/∂x_c) σ_ce (∂N_j/∂x_e).
                let mut gij = 0.0;
                for c in 0..d {
                    for e in 0..d {
                        gij += dn[i * d + c] * sig[c * d + e] * dn[j * d + e];
                    }
                }
                gij *= w;
                // Same scalar on every component's diagonal block (δ_ab).
                for a in 0..d {
                    ke[(i * d + a) * dofs + (j * d + a)] += gij;
                }
                if let Some((n, s_hoop)) = hoop {
                    ke[(i * d) * dofs + (j * d)] += s_hoop * n[i] * n[j] * w;
                }
            }
        }
    }
    Ok(())
}

/// What a continuum-mechanics **matrix** kernel reads from the state field, by
/// [`MatrixKind`] — the shared declaration of elasticity, plasticity and damage,
/// which run the very same kernels.
///
/// ```
/// # use pyrucast::models::MatrixKind;
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::models::elasticity::continuum_element_state_reads;
/// // Une raideur ne lit aucun état ; la raideur géométrique lit la contrainte.
/// let k = Kinematics::PlaneStress;
/// assert!(continuum_element_state_reads(MatrixKind::Stiffness, 2, k).is_empty());
/// assert_eq!(continuum_element_state_reads(MatrixKind::Geometric, 2, k),
///            ["sigma_xx", "sigma_xy", "sigma_yy"]);
/// // Sur un corps de révolution, le cerceau ferme la liste.
/// let axi = Kinematics::Axisymmetric;
/// let noms = continuum_element_state_reads(MatrixKind::Geometric, 2, axi);
/// assert_eq!(noms.last().unwrap(), "sigma_zz");
/// ```
pub fn continuum_element_state_reads(
    kind: MatrixKind,
    space_dim: usize,
    kinematics: Kinematics,
) -> Vec<String> {
    match kind {
        // The current Cauchy stress, Voigt-named, closed by the hoop `σ_θθ` on a
        // body of revolution — the same list `Bᵀσ` reads.
        MatrixKind::Geometric => {
            let mut names = crate::models::stress_matrix_reads(space_dim);
            if kinematics.is_axisymmetric() {
                names.push("sigma_zz".to_string());
            }
            names
        }
        // The upper triangle of the algorithmic modulus the integrator wrote.
        MatrixKind::Tangent => tangent_component_names(space_dim, kinematics),
        // A stiffness or a mass reads the material and nothing else.
        MatrixKind::Stiffness | MatrixKind::Mass => Vec::new(),
    }
}

/// Names of the **consistent-tangent** state components a non-linear physics
/// (plasticity, Mazars) emits: the upper triangle of the symmetric `v×v`
/// algorithmic modulus `D_alg` in the kinematics's engineering-Voigt order, named
/// `ktan_{i}_{j}` for `i ≤ j`. `v = 3` in 2-D plane, `4` axisymmetric, `6` in
/// 3-D — so 6, 10 or 21 names.
/// The tangent assembler reads them back with [`read_tangent_matrix`].
///
/// ```
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::models::elasticity::{self};
/// // Le triangle supérieur d'un module symétrique v×v : 6, 10 ou 21 noms.
/// assert_eq!(elasticity::tangent_component_names(2, Kinematics::PlaneStress).len(), 6);
/// assert_eq!(elasticity::tangent_component_names(2, Kinematics::Axisymmetric).len(), 10);
/// assert_eq!(elasticity::tangent_component_names(3, Kinematics::Full3D).len(), 21);
/// assert_eq!(elasticity::tangent_component_names(2, Kinematics::PlaneStress)[0],
///            "ktan_0_0");
/// ```
pub fn tangent_component_names(space_dim: usize, kinematics: Kinematics) -> Vec<String> {
    let v = voigt_size(space_dim, kinematics);
    let mut names = Vec::with_capacity(v * (v + 1) / 2);
    for i in 0..v {
        for j in i..v {
            names.push(format!("ktan_{i}_{j}"));
        }
    }
    names
}

/// Reconstruct the symmetric `v×v` consistent tangent `D_alg` at `(cell, g)` from
/// the `ktan_{i}_{j}` state components emitted by the constitutive integrator.
///
/// ```
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::elasticity::{self};
/// # use pyrucast::models::kernel::assemble_block;
/// # use pyrucast::models::symmetry::MaterialSymmetry;
/// # 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 fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let vars = || (vec!["f_x".to_string(), "f_y".to_string()],
/// #                vec!["u_x".to_string(), "u_y".to_string()]);
/// // Le producteur écrit `ktan_i_j`, le consommateur les relit ici : c'est
/// // tout le contrat entre l'intégrateur constitutif et l'assembleur.
/// let noms = elasticity::tangent_component_names(2, Kinematics::PlaneStress);
/// let d0 = elasticity::constitutive(210e3, 0.3, Kinematics::PlaneStress, 2);
/// let mut etat = SubElementField::new(zone.clone(), noms.clone())?;
/// let mut k = 0;
/// for i in 0..3 {
///     for j in i..3 {
///         etat.set_uniform(&noms[k], d0[i][j])?;
///         k += 1;
///     }
/// }
/// // Relu, le module est **symétrique** et identique à ce qu'on a écrit.
/// let d = elasticity::read_tangent_matrix(&etat, 0, 0, 2, Kinematics::PlaneStress)?;
/// assert_eq!(d, d0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn read_tangent_matrix(
    state: &SubElementField,
    cell: usize,
    g: usize,
    space_dim: usize,
    kinematics: Kinematics,
) -> Result<Vec<Vec<f64>>> {
    let v = voigt_size(space_dim, kinematics);
    let mut d = vec![vec![0.0; v]; v];
    for i in 0..v {
        for j in i..v {
            let val = state.value(cell, g, &format!("ktan_{i}_{j}"))?;
            d[i][j] = val;
            d[j][i] = val;
        }
    }
    Ok(d)
}

/// [`read_tangent_matrix`] **by index, into a caller-owned buffer** — the form
/// the tangent kernel calls.
///
/// `lay` gives the position of each `ktan_i_j` in the state row, in the
/// upper-triangle order [`tangent_component_names`] declares, resolved once per
/// zone. Reading by name at each Gauss point cost a `format!` and a string
/// search per entry, for a table the zone already knew — and the matrix itself
/// cost `v + 1` allocations where a fixed `6×6` on the pile does.
fn read_tangent_matrix_into(row: &[f64], lay: &[u32], v: usize, d: &mut [[f64; 6]; 6]) {
    let mut k = 0;
    for i in 0..v {
        for j in i..v {
            let val = row[lay[k] as usize];
            d[i][j] = val;
            d[j][i] = val;
            k += 1;
        }
    }
}

/// Element kernel: local **consistent tangent** `K_t = Σ_g Bᵀ D_alg B |J| w` of
/// one cell, with the per-Gauss algorithmic modulus `D_alg` read from `state`
/// (the constitutive integrator's `ktan_*` output). Same `ke` layout as
/// [`element_stiffness`]; law-independent given `D_alg`, so plasticity and Mazars
/// share it — only the `D_alg` they produce differs.
///
/// ```
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::elasticity::{self};
/// # use pyrucast::models::kernel::assemble_block;
/// # use pyrucast::models::symmetry::MaterialSymmetry;
/// # 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 fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let vars = || (vec!["f_x".to_string(), "f_y".to_string()],
/// #                vec!["u_x".to_string(), "u_y".to_string()]);
/// // Indépendante de la loi une fois `D_alg` donné : plasticité et Mazars
/// // partagent ce noyau, seul le module algorithmique qu'elles produisent
/// // diffère. Avec le module **élastique** en entrée, on retrouve la
/// // raideur élastique.
/// # let noms = elasticity::tangent_component_names(2, Kinematics::PlaneStress);
/// # let d0 = elasticity::constitutive(210e3, 0.3, Kinematics::PlaneStress, 2);
/// # let mut e = SubElementField::new(zone.clone(), noms.clone())?;
/// # let mut k = 0;
/// # for i in 0..3 { for j in i..3 { e.set_uniform(&noms[k], d0[i][j])?; k += 1; } }
/// # let etat = Handle::new(e);
/// # let mat = Handle::new(SubElementField::from_uniform_per_component(
/// #     zone.clone(), vec!["E".into(), "nu".into()], &[210_000.0, 0.3])?);
/// # use pyrucast::models::ElementLayout;
/// // Les six `ktan_i_j` ont été écrits dans l'ordre du triangle supérieur,
/// // et `E`, `nu` dans celui du contrat : deux identités.
/// let tan_lay = ElementLayout {
///     material: vec![], optional_material: vec![], state: (0..6).collect(),
/// };
/// let mat_lay = ElementLayout {
///     material: vec![0, 1], optional_material: vec![], state: vec![],
/// };
/// let (duals, primals) = vars();
/// let tangente = assemble_block(
///     std::slice::from_ref(&zone), &support, &support, duals.clone(), primals.clone(),
///     DofOrdering::NodesThenVars, true, &mat, Some(&etat),
///     |geoms, _m, s, ke| elasticity::element_tangent_from_state(
///         &geoms[0], s.unwrap(), &tan_lay, Kinematics::PlaneStress, ke),
/// )?;
/// let raideur = assemble_block(
///     std::slice::from_ref(&zone), &support, &support, duals, primals,
///     DofOrdering::NodesThenVars, true, &mat, None,
///     |geoms, m, _s, ke| elasticity::element_stiffness(
///         &geoms[0], m, &mat_lay, Kinematics::PlaneStress,
///         MaterialSymmetry::Isotropic, ke),
/// )?;
/// assert_eq!(tangente.dense(), raideur.dense());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_tangent_from_state(
    geom: &CellGeom,
    state: &SubElementField,
    lay: &ElementLayout,
    kinematics: Kinematics,
    ke: &mut [f64],
) -> Result<()> {
    let n_nodes = geom.n_nodes;
    let space_dim = geom.space_dim;
    let dofs = space_dim * n_nodes;
    let v = voigt_size(space_dim, kinematics);
    // Mêmes tampons de pile que `element_stiffness`, hors de la boucle.
    let mut dn_buf = [0.0_f64; MAX_CELL_DOFS];
    let mut b: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
    let mut db: VoigtRows = [[0.0; MAX_CELL_DOFS]; 6];
    let mut d = [[0.0_f64; 6]; 6];
    for g in 0..geom.n_gauss {
        // Same hoop row as `element_stiffness` on a body of revolution.
        let hoop = if kinematics.is_axisymmetric() {
            Some((geom.n_at_g(g), geom.radius(g)))
        } else {
            None
        };
        geom.dn_dx(g, &mut dn_buf[..n_nodes * space_dim])?;
        b_matrix_into(
            &dn_buf[..n_nodes * space_dim],
            n_nodes,
            space_dim,
            hoop,
            &mut b,
        );
        read_tangent_matrix_into(state.row(geom.cell, g), &lay.state, v, &mut d);
        // DB = D·B (voigt × dofs), then Kᵉ += Bᵀ (DB) · |J| w.
        for r in 0..v {
            for c in 0..dofs {
                let mut acc = 0.0;
                for w in 0..v {
                    acc += d[r][w] * b[w][c];
                }
                db[r][c] = acc;
            }
        }
        let w = geom.det_j_w(g);
        for r in 0..dofs {
            for c in 0..dofs {
                let mut acc = 0.0;
                for vv in 0..v {
                    acc += b[vv][r] * db[vv][c];
                }
                ke[r * dofs + c] += acc * w;
            }
        }
    }
    Ok(())
}

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

/// Lamé coefficients `(λ, μ)` from `E`, `nu`.
///
/// ```
/// # use pyrucast::models::elasticity;
/// # use pyrucast::models::plasticity::law;
/// let (lambda, mu) = elasticity::lame(210_000.0, 0.3);
/// // μ = E / 2(1+ν), λ = Eν / (1+ν)(1−2ν).
/// assert!((mu - 210_000.0 / 2.6).abs() < 1e-9);
/// assert!((lambda - 121_153.846_153_85).abs() < 1e-6);
/// ```
pub fn lame(e: f64, nu: f64) -> (f64, f64) {
    let lambda = e * nu / ((1.0 + nu) * (1.0 - 2.0 * nu));
    let mu = e / (2.0 * (1.0 + nu));
    (lambda, mu)
}

/// Isotropic elastic stress (full 3-D, order `[xx, yy, zz, yz, xz, xy]`) from a
/// **tensor** strain: `σ = λ tr(ε) I + 2μ ε`.
///
/// ```
/// # use pyrucast::models::elasticity;
/// # use pyrucast::models::plasticity::law;
/// let (lambda, mu) = elasticity::lame(210_000.0, 0.3);
/// // Un cisaillement pur **tensoriel** ε_xy = 1 donne σ_xy = 2μ.
/// let s = elasticity::elastic_stress(&[0.0, 0.0, 0.0, 0.0, 0.0, 1.0], lambda, mu);
/// assert!((s[5] - 2.0 * mu).abs() < 1e-9);
/// assert!(s[0].abs() < 1e-9); // trace nulle ⇒ pas de part sphérique
/// ```
pub fn elastic_stress(eps: &[f64; 6], lambda: f64, mu: f64) -> [f64; 6] {
    let tr = eps[0] + eps[1] + eps[2];
    [
        lambda * tr + 2.0 * mu * eps[0],
        lambda * tr + 2.0 * mu * eps[1],
        lambda * tr + 2.0 * mu * eps[2],
        2.0 * mu * eps[3],
        2.0 * mu * eps[4],
        2.0 * mu * eps[5],
    ]
}

/// The elastic modulus in full-3-D engineering Voigt — the tangent wherever the
/// step stayed elastic, and the starting point of every analytic one.
///
/// ```
/// # use pyrucast::models::elasticity;
/// # use pyrucast::models::plasticity::law;
/// let (lambda, mu) = elasticity::lame(210_000.0, 0.3);
/// let d = elasticity::elastic_tangent(lambda, mu);
/// // Voigt **de l'ingénieur** : le bloc de cisaillement vaut μ, non 2μ.
/// assert!((d[3][3] - mu).abs() < 1e-9);
/// assert!((d[0][0] - (lambda + 2.0 * mu)).abs() < 1e-9);
/// ```
/// # use pyrucast::models::tensor::Kinematics;
pub fn elastic_tangent(lambda: f64, mu: f64) -> [[f64; 6]; 6] {
    let mut c = [[0.0; 6]; 6];
    for i in 0..3 {
        for j in 0..3 {
            c[i][j] = if i == j { lambda + 2.0 * mu } else { lambda };
        }
    }
    for i in 3..6 {
        c[i][i] = mu;
    }
    c
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The rest state of `d` on its material — the `prev` of a first step,
    /// which the behaviour operator materializes for a caller who has none.
    fn rest<D: Domain>(d: &D, mat: &Handle<SubElementField>) -> Handle<SubElementField> {
        Handle::new(d.initial_state(&mat.read()).unwrap())
    }
    use crate::aggregate::Aggregate;
    use crate::atoms::{ElementType, Node, NodeId};
    use crate::containers::field::SubField;
    use crate::containers::finite_element_space::FiniteElementSpace;
    use crate::containers::mesh::Mesh;
    use crate::coords::Coords;
    use crate::handle::Handle;

    fn unit_quad(kinematics: Kinematics) -> Elasticity {
        let coords = Handle::new(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(), &[1.0, 1.0]).unwrap();
        let d = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::QUA4));
        mesh.add_cell(&[a.id(), b.id(), c.id(), d.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        Elasticity::new(fes.get(0).unwrap(), kinematics).unwrap()
    }

    #[test]
    fn vars_and_model_validation() {
        let el = unit_quad(Kinematics::PlaneStress);
        assert_eq!(el.primal_vars(), vec!["u_x", "u_y"]);
        assert_eq!(el.dual_vars(), vec!["f_x", "f_y"]);
        // 2-D space cannot be Solid.
        let coords = Handle::new(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();
        assert!(Elasticity::new(fes.get(0).unwrap(), Kinematics::Full3D).is_err());
    }

    #[test]
    fn plane_stress_constitutive_known_values() {
        let (e, nu) = (1.0, 0.25);
        let d = constitutive(e, nu, Kinematics::PlaneStress, 2);
        let c = e / (1.0 - nu * nu);
        assert!((d[0][0] - c).abs() < 1e-12);
        assert!((d[0][1] - c * nu).abs() < 1e-12);
        assert!((d[2][2] - c * (1.0 - nu) / 2.0).abs() < 1e-12);
        assert!((d[2][2] - e / (2.0 * (1.0 + nu))).abs() < 1e-12); // = G
    }

    /// COMP: uniaxial tensor strain `εxx = ε₀` in plane stress gives
    /// `σxx = E/(1-ν²)·ε₀`, `σyy = ν·σxx`, `σxy = 0`.
    #[test]
    fn integrate_behavior_plane_stress_uniaxial() {
        let (e, nu, eps0) = (210.0, 0.3, 0.001);
        let el = unit_quad(Kinematics::PlaneStress);
        let mut mat =
            SubElementField::new(el.fespace.clone(), vec!["E".into(), "nu".into()]).unwrap();
        mat.set_uniform("E", e).unwrap();
        mat.set_uniform("nu", nu).unwrap();
        let mat = Handle::new(mat);

        let mut strain = SubElementField::new(
            el.fespace.clone(),
            vec!["eps_xx".into(), "eps_xy".into(), "eps_yy".into()],
        )
        .unwrap();
        strain.set_uniform("eps_xx", eps0).unwrap();
        let strain = Handle::new(strain);

        let out = el
            .integrate_behavior(&strain, &rest(&el, &mat), &mat, 0.0)
            .unwrap();
        let c = e / (1.0 - nu * nu);
        for g in 0..out.gauss_count() {
            assert!((out.value(0, g, "sigma_xx").unwrap() - c * eps0).abs() < 1e-9);
            assert!((out.value(0, g, "sigma_yy").unwrap() - c * nu * eps0).abs() < 1e-9);
            assert!(out.value(0, g, "sigma_xy").unwrap().abs() < 1e-9);
        }
    }

    /// Element stiffness is symmetric and the rigid-body modes are in its
    /// kernel (zero row sums per axis).
    #[test]
    fn element_stiffness_symmetric_and_rigid_body_free() {
        let el = unit_quad(Kinematics::PlaneStrain);
        let mut mat =
            SubElementField::new(el.fespace.clone(), vec!["E".into(), "nu".into()]).unwrap();
        mat.set_uniform("E", 200.0).unwrap();
        mat.set_uniform("nu", 0.3).unwrap();
        let mat = Handle::new(mat);
        let blocks = el.build_stiffness_blocks(&mat).unwrap();
        let k = &blocks[0];
        let nodes: Vec<NodeId> = el.support.read().connectivity().to_vec();
        let tol = 1e-9;
        // Symmetry K[(i,f_a),(j,u_b)] == K[(j,f_b),(i,u_a)].
        for &ni in &nodes {
            for &nj in &nodes {
                for a in ["x", "y"] {
                    for b in ["x", "y"] {
                        let lhs = k.get(ni, &format!("f_{a}"), nj, &format!("u_{b}"));
                        let rhs = k.get(nj, &format!("f_{b}"), ni, &format!("u_{a}"));
                        assert!((lhs - rhs).abs() < tol);
                    }
                }
            }
        }
        // A uniform translation in x ⇒ zero force everywhere (row sum = 0).
        for &ni in &nodes {
            let row: f64 = nodes.iter().map(|&nj| k.get(ni, "f_x", nj, "u_x")).sum();
            assert!(row.abs() < tol, "row sum {row} ≠ 0");
        }
    }
}