cfsem 11.1.0

Quasi-steady electromagnetics including filamentized approximations, Biot-Savart, and Grad-Shafranov.
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
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
//! Reduced-model assembly and solve wrapper for the 2D structural FEM.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use faer::Col;
use faer::linalg::solvers::Solve;
use faer::sparse::linalg::solvers::Lu;
use faer::sparse::{SparseColMat, SparseRowMat, SymbolicSparseColMat, Triplet};
use rayon::prelude::*;

use crate::mesh::elements::quad2d::quadrature::gauss_volume;
use crate::mesh::elements::quad2d::{mapping, quad4, quad9};
use crate::mesh::quad2d::{QuadReferenceElement, closest_reference_point};
use crate::mesh::{QuadMeshView2d, QuadratureRule};
use crate::physics::solenoid_stress::assembly::{
    assemble_stiffness_chunks_for_family_par, assemble_stiffness_for_family,
};
use crate::physics::solenoid_stress::axisym::{build_b_matrix, constitutive_times_strain};
use crate::physics::solenoid_stress::convenience::{
    Structural2dElementMeasures, rotate_material_in_plane, rotate_thermal_material_in_plane,
};
use crate::physics::solenoid_stress::family::{Quad4Family, Quad9Family, QuadElementFamily};
use crate::physics::solenoid_stress::loads::{
    SparseOperator, apply_body_force_rhs_for_family, apply_pressure_rhs_for_family,
    apply_temperature_rhs_for_family, apply_traction_rhs_for_family,
    body_force_operator_for_family, body_force_operator_for_family_par,
    pressure_operator_for_family, pressure_operator_for_family_par,
    temperature_operator_for_family, temperature_operator_for_family_par,
    thermal_reference_rhs_for_family, traction_operator_for_family,
    traction_operator_for_family_par,
};
use crate::physics::solenoid_stress::types::{
    PressureLoad, StiffnessTriplets, Structural2dFormulation, ThermalMaterial, TractionLoad,
    dof_per_element,
};

/// Element-owned point locations for structural 2D recovery.
#[derive(Debug, Clone)]
pub struct Structural2dPointLocations {
    /// Physical point coordinates in element-major or user-supplied order.
    pub points: Vec<[f64; 2]>,
    /// Owning element index for each point.
    pub element_indices: Vec<usize>,
    /// Reference coordinates for each point.
    pub reference_points: Vec<[f64; 2]>,
}

/// Element-major quadrature locations and mapped integration weights.
#[derive(Debug, Clone)]
pub struct Structural2dQuadrature {
    /// Locations at the model's quadrature points.
    pub locations: Structural2dPointLocations,
    /// Mapped analysis-plane quadrature weights.
    pub weights_area: Vec<f64>,
    /// Mapped represented-volume quadrature weights.
    pub weights_volume: Vec<f64>,
    /// Number of consecutive points contributed by each element for quadrature locations.
    pub points_per_element: usize,
}

/// Public element-family selector for the 2D structural solver.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Structural2dElementType {
    /// Bilinear four-node quadrilateral in the analysis plane.
    Quad4,
    /// Quadratic nine-node quadrilateral in the analysis plane.
    Quad9,
}

impl Structural2dElementType {
    /// Return the canonical public string spelling used by the Python wrapper and docs.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Quad4 => "quad4",
            Self::Quad9 => "quad9",
        }
    }

    /// Parse the compact numeric element code used by the low-level Python binding.
    pub fn from_code(code: u8) -> Result<Self, String> {
        match code {
            4 => Ok(Self::Quad4),
            9 => Ok(Self::Quad9),
            _ => Err(format!(
                "unsupported structural 2D FEM element code {code}; use 4 or 9"
            )),
        }
    }
}

/// Borrowed element-connectivity input for model assembly.
///
/// Each variant stores element-node connectivity in the node ordering expected by the
/// corresponding quadrilateral family. Element corner nodes must be ordered
/// counter-clockwise in the 2D analysis plane.
pub enum Structural2dElements<'a> {
    /// Four-node bilinear quadrilateral connectivity in local corner order.
    Quad4(&'a [[usize; 4]]),
    /// Nine-node quadratic quadrilateral connectivity in local corner, midside, center order.
    Quad9(&'a [[usize; 9]]),
}

/// Inputs retained so sparse exports and matrix-free operations can be built after assembly.
#[derive(Debug, Clone)]
struct Structural2dAssemblyData {
    material_ids: Vec<usize>,
    material_table: Vec<[[f64; 4]; 4]>,
    thermal_material_table: Option<Vec<ThermalMaterial>>,
    material_orientation_angles: Option<Vec<f64>>,
    global_to_reduced: Vec<usize>,
    par: bool,
}

/// Fully assembled reduced 2D structural model.
///
/// The public system stored here is the Dirichlet-reduced system. `stiffness` and `constant_rhs`
/// live in reduced displacement space. Sparse load and recovery operators are computed on demand
/// for explicit exports and are not retained by the model.
/// `analysis_nodes` has shape `(n_analysis_nodes, 2)`, `analysis_elements_flat` has shape
/// `(nelem * nodes_per_element,)`, `pressure_faces` has shape `(n_pressure_faces, 2)`, and
/// `traction_faces` has shape `(n_traction_faces, 2)`.
#[derive(Debug)]
pub struct Structural2dModel {
    /// Reduced structural stiffness matrix in CSC form.
    ///
    /// This matrix maps reduced displacements `[length]` to reduced generalized nodal forces
    /// `[energy / distance]`.
    ///
    /// Entry units: `[generalized force / displacement] = [energy / distance^2]`.
    pub stiffness: SparseColMat<usize, f64>,
    /// Constant reduced RHS contribution from prescribed displacements and thermal reference state.
    ///
    /// Shape: `(ndof_reduced,)`.
    /// Units: `[energy / distance]`.
    pub constant_rhs: Vec<f64>,
    /// Number of quadrature points contributed by each element.
    pub nq_per_element: usize,
    /// Number of nodal temperatures expected by thermal operators.
    pub n_temperature_nodes: usize,
    /// Metadata listing the loaded pressure faces as `[element_index, local_face]`.
    ///
    /// Shape: `(n_pressure_faces, 2)`.
    pub pressure_faces: Vec<[usize; 2]>,
    /// Metadata listing the loaded traction faces as `[element_index, local_face]`.
    ///
    /// Shape: `(n_traction_faces, 2)`.
    pub traction_faces: Vec<[usize; 2]>,
    /// Analysis mesh nodes used by the backend.
    ///
    /// Shape: `(n_analysis_nodes, 2)`.
    /// Units: `[length]`.
    pub analysis_nodes: Vec<[f64; 2]>,
    /// Flattened analysis connectivity in element-major order.
    ///
    /// Shape: `(nelem * nodes_per_element,)`.
    pub analysis_elements_flat: Vec<usize>,
    /// Number of nodes per analysis element.
    pub nodes_per_element: usize,
    /// Analysis element family used by the backend.
    pub element_type: Structural2dElementType,
    /// Volume and face quadrature rule used for assembly, matrix-free loads, and sparse exports.
    pub quadrature: QuadratureRule,
    /// Structural 2D formulation used by the backend.
    pub formulation: Structural2dFormulation,
    /// Number of displacement DOFs in the unreduced full system.
    pub ndof_full: usize,
    /// Number of displacement DOFs remaining after Dirichlet reduction.
    pub ndof_reduced: usize,
    /// Number of analysis elements.
    pub nelem: usize,
    /// Mapping from reduced displacement index to full-system DOF index.
    ///
    /// Shape: `(ndof_reduced,)`.
    pub free_dofs: Vec<usize>,
    /// Full-system DOF indices removed by Dirichlet reduction.
    ///
    /// Shape: `(n_fixed,)`.
    pub fixed_dofs: Vec<usize>,
    /// Prescribed displacement values for `fixed_dofs`.
    ///
    /// Shape: `(n_fixed,)`.
    /// Units: `[length]`.
    pub fixed_values: Vec<f64>,
    /// Assembly inputs retained for matrix-free operations and sparse exports.
    assembly: Structural2dAssemblyData,
    lu: Option<Lu<usize, f64>>,
}

impl Structural2dModel {
    fn analysis_elements<const NODES_PER_ELEMENT: usize>(
        &self,
    ) -> Result<Vec<[usize; NODES_PER_ELEMENT]>, String> {
        elements_from_flat::<NODES_PER_ELEMENT>(
            &self.analysis_elements_flat,
            self.nelem,
            self.element_type,
        )
    }

    fn pressure_loads(&self) -> Vec<PressureLoad> {
        self.pressure_faces
            .iter()
            .map(|face| PressureLoad {
                element: face[0],
                local_face: face[1] as u8,
            })
            .collect()
    }

    fn traction_loads(&self) -> Vec<TractionLoad> {
        self.traction_faces
            .iter()
            .map(|face| TractionLoad {
                element: face[0],
                local_face: face[1] as u8,
            })
            .collect()
    }

    fn reduce_load_operator(
        &self,
        operator: SparseOperator,
    ) -> Result<SparseRowMat<usize, f64>, String> {
        reduce_row_operator_to_csr(
            operator,
            &self.assembly.global_to_reduced,
            self.ndof_reduced,
        )
    }

    fn build_body_force_to_rhs_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
    ) -> Result<SparseRowMat<usize, f64>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let operator = if self.assembly.par {
            body_force_operator_for_family_par::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                self.formulation,
                self.quadrature,
            )
        } else {
            body_force_operator_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                self.formulation,
                self.quadrature,
            )
        }?;
        self.reduce_load_operator(operator)
    }

    fn build_pressure_to_rhs_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
    ) -> Result<SparseRowMat<usize, f64>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let pressure_faces = self.pressure_loads();
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let operator = if self.assembly.par {
            pressure_operator_for_family_par::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &pressure_faces,
                self.formulation,
                self.quadrature,
            )
        } else {
            pressure_operator_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &pressure_faces,
                self.formulation,
                self.quadrature,
            )
        }?;
        self.reduce_load_operator(operator)
    }

    fn build_traction_to_rhs_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
    ) -> Result<SparseRowMat<usize, f64>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let traction_faces = self.traction_loads();
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let operator = if self.assembly.par {
            traction_operator_for_family_par::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &traction_faces,
                self.formulation,
                self.quadrature,
            )
        } else {
            traction_operator_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &traction_faces,
                self.formulation,
                self.quadrature,
            )
        }?;
        self.reduce_load_operator(operator)
    }

    fn build_temperature_to_rhs_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
    ) -> Result<SparseRowMat<usize, f64>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        let Some(thermal_material_table) = self.assembly.thermal_material_table.as_ref() else {
            return csr_from_parts(self.ndof_reduced, 0, Vec::new(), Vec::new(), Vec::new());
        };
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let operator = if self.assembly.par {
            temperature_operator_for_family_par::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &self.assembly.material_ids,
                &self.assembly.material_table,
                thermal_material_table,
                self.assembly.material_orientation_angles.as_deref(),
                self.formulation,
                self.quadrature,
            )
        } else {
            temperature_operator_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &self.assembly.material_ids,
                &self.assembly.material_table,
                thermal_material_table,
                self.assembly.material_orientation_angles.as_deref(),
                self.formulation,
                self.quadrature,
            )
        }?;
        self.reduce_load_operator(operator.temperature_to_rhs)
    }

    pub fn body_force_to_rhs(&self) -> Result<SparseRowMat<usize, f64>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.build_body_force_to_rhs_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(),
            Structural2dElementType::Quad9 => self.build_body_force_to_rhs_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(),
        }
    }

    pub fn pressure_to_rhs(&self) -> Result<SparseRowMat<usize, f64>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.build_pressure_to_rhs_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(),
            Structural2dElementType::Quad9 => self.build_pressure_to_rhs_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(),
        }
    }

    pub fn traction_to_rhs(&self) -> Result<SparseRowMat<usize, f64>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.build_traction_to_rhs_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(),
            Structural2dElementType::Quad9 => self.build_traction_to_rhs_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(),
        }
    }

    pub fn temperature_to_rhs(&self) -> Result<SparseRowMat<usize, f64>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.build_temperature_to_rhs_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(),
            Structural2dElementType::Quad9 => self.build_temperature_to_rhs_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(),
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn apply_matrix_free_loads_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
        body_force: Option<&[f64]>,
        pressure_values: Option<&[f64]>,
        traction_values: Option<&[f64]>,
        nodal_temperature: Option<&[f64]>,
        rhs: &mut [f64],
    ) -> Result<(), String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        if let Some(body_force) = body_force {
            apply_body_force_rhs_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                self.formulation,
                self.quadrature,
                body_force,
                &self.assembly.global_to_reduced,
                rhs,
            )?;
        }
        if let Some(pressure_values) = pressure_values {
            let pressure_faces = self.pressure_loads();
            apply_pressure_rhs_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &pressure_faces,
                self.formulation,
                self.quadrature,
                pressure_values,
                &self.assembly.global_to_reduced,
                rhs,
            )?;
        }
        if let Some(traction_values) = traction_values {
            let traction_faces = self.traction_loads();
            apply_traction_rhs_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                &traction_faces,
                self.formulation,
                self.quadrature,
                traction_values,
                &self.assembly.global_to_reduced,
                rhs,
            )?;
        }
        match (
            self.assembly.thermal_material_table.as_ref(),
            nodal_temperature,
        ) {
            (Some(thermal_material_table), Some(nodal_temperature)) => {
                apply_temperature_rhs_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                    mesh,
                    &self.assembly.material_ids,
                    &self.assembly.material_table,
                    thermal_material_table,
                    self.assembly.material_orientation_angles.as_deref(),
                    self.formulation,
                    self.quadrature,
                    nodal_temperature,
                    &self.assembly.global_to_reduced,
                    rhs,
                )?;
            }
            (Some(_), None) => {
                return Err(
                    "nodal_temperature is required because this model includes thermal materials"
                        .to_string(),
                );
            }
            (None, Some(nodal_temperature)) if !nodal_temperature.is_empty() => {
                return Err(
                    "nodal_temperature was provided, but this model has no thermal operator"
                        .to_string(),
                );
            }
            (None, _) => {}
        }
        Ok(())
    }

    fn apply_matrix_free_loads(
        &self,
        body_force: Option<&[f64]>,
        pressure_values: Option<&[f64]>,
        traction_values: Option<&[f64]>,
        nodal_temperature: Option<&[f64]>,
        rhs: &mut [f64],
    ) -> Result<(), String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.apply_matrix_free_loads_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(body_force, pressure_values, traction_values, nodal_temperature, rhs),
            Structural2dElementType::Quad9 => self.apply_matrix_free_loads_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(body_force, pressure_values, traction_values, nodal_temperature, rhs),
        }
    }

    fn evaluate_strain_for_locations_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        displacements_full: &[f64],
    ) -> Result<Vec<[f64; 4]>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        validate_location_lengths(element_indices, reference_points)?;
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let mut strain = Vec::with_capacity(element_indices.len());
        for (&element_index, &reference) in element_indices.iter().zip(reference_points) {
            let coords = mesh.element_coords(element_index)?;
            let nodes = mesh.element_nodes(element_index)?;
            let mut local_u = [0.0; DOF_PER_ELEMENT];
            for local_node in 0..NODES_PER_ELEMENT {
                let global_node = nodes[local_node];
                local_u[2 * local_node] = displacements_full[2 * global_node];
                local_u[2 * local_node + 1] = displacements_full[2 * global_node + 1];
            }
            let shape = Family::ReferenceElement::shape(reference[0], reference[1]);
            let grad_ref = Family::ReferenceElement::grad_ref(reference[0], reference[1]);
            let jac = mapping::jacobian(&coords, &grad_ref);
            let inv_jac = mapping::inv_j(&jac)?;
            let grad_phys = mapping::grad_phys(&grad_ref, &inv_jac);
            let point = mapping::map_point(&coords, &shape);
            let b = build_b_matrix::<NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                self.formulation,
                &shape,
                &grad_phys,
                point,
            )?;
            let mut sample_strain = [0.0; 4];
            for component in 0..4 {
                let mut value = 0.0;
                for dof in 0..DOF_PER_ELEMENT {
                    value += b[component][dof] * local_u[dof];
                }
                sample_strain[component] = value;
            }
            strain.push(sample_strain);
        }
        Ok(strain)
    }

    fn evaluate_stress_for_locations_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const DOF_PER_ELEMENT: usize,
    >(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        displacements_full: &[f64],
    ) -> Result<Vec<[f64; 4]>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        validate_location_lengths(element_indices, reference_points)?;
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let mut stress = Vec::with_capacity(element_indices.len());
        for (&element_index, &reference) in element_indices.iter().zip(reference_points) {
            let coords = mesh.element_coords(element_index)?;
            let nodes = mesh.element_nodes(element_index)?;
            let material_id = self.assembly.material_ids[element_index];
            let material = self
                .assembly
                .material_table
                .get(material_id)
                .ok_or_else(|| {
                    format!("material_id {material_id} on element {element_index} is out of range")
                })?;
            let material_storage;
            let material = if let Some(angles) = self.assembly.material_orientation_angles.as_ref()
            {
                material_storage = rotate_material_in_plane(material, angles[element_index]);
                &material_storage
            } else {
                material
            };
            let mut local_u = [0.0; DOF_PER_ELEMENT];
            for local_node in 0..NODES_PER_ELEMENT {
                let global_node = nodes[local_node];
                local_u[2 * local_node] = displacements_full[2 * global_node];
                local_u[2 * local_node + 1] = displacements_full[2 * global_node + 1];
            }
            let shape = Family::ReferenceElement::shape(reference[0], reference[1]);
            let grad_ref = Family::ReferenceElement::grad_ref(reference[0], reference[1]);
            let jac = mapping::jacobian(&coords, &grad_ref);
            let inv_jac = mapping::inv_j(&jac)?;
            let grad_phys = mapping::grad_phys(&grad_ref, &inv_jac);
            let point = mapping::map_point(&coords, &shape);
            let b = build_b_matrix::<NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                self.formulation,
                &shape,
                &grad_phys,
                point,
            )?;
            let mut sample_strain = [0.0; 4];
            for component in 0..4 {
                let mut value = 0.0;
                for dof in 0..DOF_PER_ELEMENT {
                    value += b[component][dof] * local_u[dof];
                }
                sample_strain[component] = value;
            }
            stress.push(constitutive_times_strain(material, &sample_strain));
        }
        Ok(stress)
    }

    fn evaluate_thermal_for_locations_for_family<
        Family,
        const NODES_PER_ELEMENT: usize,
        const STRESS: bool,
    >(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        nodal_temperature: &[f64],
    ) -> Result<Vec<[f64; 4]>, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        validate_location_lengths(element_indices, reference_points)?;
        let Some(thermal_material_table) = self.assembly.thermal_material_table.as_ref() else {
            return Ok(vec![[0.0; 4]; element_indices.len()]);
        };
        if nodal_temperature.len() != self.n_temperature_nodes {
            return Err(format!(
                "nodal_temperature has length {}, but thermal recovery expects {} values",
                nodal_temperature.len(),
                self.n_temperature_nodes
            ));
        }

        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let mut values = Vec::with_capacity(element_indices.len());
        for (&element_index, &reference) in element_indices.iter().zip(reference_points) {
            let nodes = mesh.element_nodes(element_index)?;
            let material_id = self.assembly.material_ids[element_index];
            let material = self
                .assembly
                .material_table
                .get(material_id)
                .ok_or_else(|| {
                    format!("material_id {material_id} on element {element_index} is out of range")
                })?;
            let thermal = thermal_material_table.get(material_id).ok_or_else(|| {
                format!(
                    "thermal material_id {material_id} on element {element_index} is out of range"
                )
            })?;
            let material_storage;
            let thermal_storage;
            let (material, thermal) = if let Some(angles) =
                self.assembly.material_orientation_angles.as_ref()
            {
                material_storage = rotate_material_in_plane(material, angles[element_index]);
                thermal_storage = rotate_thermal_material_in_plane(thermal, angles[element_index]);
                (&material_storage, &thermal_storage)
            } else {
                (material, thermal)
            };
            let thermal_stress_unit =
                STRESS.then(|| constitutive_times_strain(material, &thermal.alpha));

            let shape = Family::ReferenceElement::shape(reference[0], reference[1]);
            let mut temperature_delta = -thermal.reference_temperature;
            for local_node in 0..NODES_PER_ELEMENT {
                temperature_delta += shape[local_node] * nodal_temperature[nodes[local_node]];
            }
            let mut sample_value = [0.0; 4];
            if let Some(thermal_stress_unit) = thermal_stress_unit.as_ref() {
                for component in 0..4 {
                    sample_value[component] = thermal_stress_unit[component] * temperature_delta;
                }
            } else {
                for component in 0..4 {
                    sample_value[component] = thermal.alpha[component] * temperature_delta;
                }
            }
            values.push(sample_value);
        }
        Ok(values)
    }

    fn locate_points_in_elements_for_family<Family, const NODES_PER_ELEMENT: usize>(
        &self,
        points: &[[f64; 2]],
        element_indices: &[usize],
        max_iterations: usize,
    ) -> Result<Structural2dPointLocations, String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        if points.len() != element_indices.len() {
            return Err(format!(
                "points has length {}, but element_indices has length {}",
                points.len(),
                element_indices.len()
            ));
        }
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let mesh = QuadMeshView2d {
            nodes_rz: &self.analysis_nodes,
            elements: &elements,
        };
        let mut projected_points = Vec::with_capacity(points.len());
        let mut reference_points = Vec::with_capacity(points.len());
        for (&point, &element_index) in points.iter().zip(element_indices) {
            let coords = mesh.element_coords(element_index)?;
            let (reference, projected, _distance) = closest_reference_point::<
                Family::ReferenceElement,
                f64,
                NODES_PER_ELEMENT,
            >(&coords, point, max_iterations);
            projected_points.push(projected);
            reference_points.push(reference);
        }
        Ok(Structural2dPointLocations {
            points: projected_points,
            element_indices: element_indices.to_vec(),
            reference_points,
        })
    }

    /// Build one reduced structural right-hand side.
    ///
    /// Args:
    ///     body_force: Optional body-force amplitudes with shape `(2 * nelem,)`, grouped as
    ///         `[b_r, b_z]` per element. Units are `[force / volume]`.
    ///     pressure_values: Optional pressure amplitudes with shape `(n_pressure_faces,)`.
    ///         Units are `[force / area]`. Positive values act in the inward normal direction.
    ///     traction_values: Optional traction amplitudes with shape `(2 * n_traction_faces,)`,
    ///         grouped as `[t_r, t_z]` per traction face. Units are `[force / area]`.
    ///     nodal_temperature: Optional nodal temperatures with shape `(n_temperature_nodes,)`.
    ///         Units are `[temperature]`. Required only when the model includes thermal materials.
    ///
    /// Returns:
    ///     Reduced right-hand side with shape `(ndof_reduced,)` and units
    ///     `[generalized force] = [energy / distance]`.
    pub fn build_rhs(
        &self,
        body_force: Option<&[f64]>,
        pressure_values: Option<&[f64]>,
        traction_values: Option<&[f64]>,
        nodal_temperature: Option<&[f64]>,
    ) -> Result<Vec<f64>, String> {
        let mut rhs = self.constant_rhs.clone();
        self.apply_matrix_free_loads(
            body_force,
            pressure_values,
            traction_values,
            nodal_temperature,
            &mut rhs,
        )?;
        Ok(rhs)
    }

    /// Solve the reduced structural system and recover the full displacement vector.
    ///
    /// The model caches the sparse LU factorization of `stiffness` on first use, so repeated
    /// calls reuse the same factorization.
    ///
    /// Args:
    ///     rhs: Reduced right-hand side with shape `(ndof_reduced,)` and units
    ///         `[generalized force] = [energy / distance]`.
    ///
    /// Returns:
    ///     Full displacement vector with shape `(ndof_full,)` and component ordering
    ///     `[u_r0, u_z0, u_r1, u_z1, ...]`. Units are `[length]`.
    pub fn solve(&mut self, rhs: &[f64]) -> Result<Vec<f64>, String> {
        let reduced_solution = self.solve_direct_reduced(rhs)?;
        let displacement = self.recover_full(&reduced_solution);
        // Certify that rest nodes + displacement is still a non-inverted mesh -- positive element
        // Jacobian everywhere -- so the displacement field returned to callers describes a
        // physically admissible deformed geometry rather than a folded one.
        self.assert_deformed_mesh_valid(&displacement)?;
        Ok(displacement)
    }

    /// Verify the deformed mesh (rest nodes + displacement) keeps every element non-degenerate.
    ///
    /// Adds the displacement to the rest nodes and checks the mapping Jacobian stays strictly
    /// positive at each family's certifying reference points. A non-positive determinant means an
    /// element has folded through zero area into an inverted, physically inadmissible state. At
    /// the microstrain displacements of a converged structural
    /// solve this never trips; it fires only on a corrupted solve or a load large enough to invert
    /// an element.
    fn assert_deformed_mesh_valid(&self, displacements_full: &[f64]) -> Result<(), String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self
                .assert_deformed_mesh_valid_for_family::<Quad4Family, { quad4::NODES_PER_ELEMENT }>(
                    displacements_full,
                ),
            Structural2dElementType::Quad9 => self
                .assert_deformed_mesh_valid_for_family::<Quad9Family, { quad9::NODES_PER_ELEMENT }>(
                    displacements_full,
                ),
        }
    }

    /// Family-generic core of the deformed-mesh guard.
    fn assert_deformed_mesh_valid_for_family<Family, const NODES_PER_ELEMENT: usize>(
        &self,
        displacements_full: &[f64],
    ) -> Result<(), String>
    where
        Family: QuadElementFamily<NODES_PER_ELEMENT>,
    {
        debug_assert_eq!(displacements_full.len(), self.ndof_full);
        let elements = self.analysis_elements::<NODES_PER_ELEMENT>()?;
        let deformed = self.deformed_nodes(displacements_full);
        let (num_inverted, worst) = find_degenerate_quads::<Family, NODES_PER_ELEMENT>(
            &deformed,
            &elements,
            self.quadrature,
        );
        let Some(worst_index) = worst.filter(|_| num_inverted > 0) else {
            return Ok(());
        };
        let corners = gather_coords(&deformed, &elements[worst_index]);
        Err(format!(
            "{num_inverted} of {} elements invert under the displacement: the deformed mesh has a \
             non-positive Jacobian -- an element has folded through zero area. Worst deformed \
             element corners (r, z) [m]: {:?}, {:?}, {:?}, {:?}. Likely cause: an unphysically \
             large displacement (check loads and boundary conditions).",
            elements.len(),
            corners[0],
            corners[1],
            corners[2],
            corners[3],
        ))
    }

    /// Move every node to its deformed position: each rest node plus its `(u_r, u_z)`.
    fn deformed_nodes(&self, displacements_full: &[f64]) -> Vec<[f64; 2]> {
        self.analysis_nodes
            .iter()
            .enumerate()
            .map(|(node, coord)| {
                [
                    coord[0] + displacements_full[2 * node],
                    coord[1] + displacements_full[2 * node + 1],
                ]
            })
            .collect()
    }

    /// Solve the reduced structural system with the cached sparse LU factorization.
    fn solve_direct_reduced(&mut self, rhs: &[f64]) -> Result<Vec<f64>, String> {
        if rhs.len() != self.ndof_reduced {
            return Err(format!(
                "rhs has length {}, but reduced system has {} rows",
                rhs.len(),
                self.ndof_reduced
            ));
        }
        if self.ndof_reduced == 0 {
            return Ok(Vec::new());
        }
        if self.lu.is_none() {
            self.lu = Some(
                self.stiffness
                    .sp_lu()
                    .map_err(|err| format!("failed to factorize reduced stiffness: {err:?}"))?,
            );
        }
        let lu = self.lu.as_ref().expect("lu cache should be initialized");
        let mut reduced_solution = Col::<f64>::zeros(self.ndof_reduced);
        for (index, value) in rhs.iter().copied().enumerate() {
            reduced_solution[index] = value;
        }
        lu.solve_in_place(reduced_solution.as_mut());
        let reduced_solution = (0..self.ndof_reduced)
            .map(|index| reduced_solution[index])
            .collect::<Vec<_>>();
        Ok(reduced_solution)
    }

    /// Reinsert prescribed Dirichlet values into a reduced displacement vector.
    ///
    /// Args:
    ///     reduced_solution: Reduced displacement vector with shape `(ndof_reduced,)`.
    ///         Units are `[length]`.
    ///
    /// Returns:
    ///     Full displacement vector with shape `(ndof_full,)` and component ordering
    ///     `[u_r0, u_z0, u_r1, u_z1, ...]`. Units are `[length]`.
    pub fn recover_full(&self, reduced_solution: &[f64]) -> Vec<f64> {
        assert!(
            reduced_solution.len() == self.ndof_reduced,
            "reduced_solution has length {}, but reduced system has {} rows",
            reduced_solution.len(),
            self.ndof_reduced
        );
        let mut full = vec![0.0; self.ndof_full];
        for (&dof, &value) in self.fixed_dofs.iter().zip(&self.fixed_values) {
            full[dof] = value;
        }
        for (&dof, &value) in self.free_dofs.iter().zip(reduced_solution) {
            full[dof] = value;
        }
        full
    }

    /// Return element-major quadrature locations and mapped weights.
    pub fn quadrature(&self) -> Result<Structural2dQuadrature, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => {
                quadrature_locations_for_family::<Quad4Family, { quad4::NODES_PER_ELEMENT }>(
                    &self.analysis_nodes,
                    &self.analysis_elements_flat,
                    self.nelem,
                    self.formulation,
                    self.quadrature,
                )
            }
            Structural2dElementType::Quad9 => {
                quadrature_locations_for_family::<Quad9Family, { quad9::NODES_PER_ELEMENT }>(
                    &self.analysis_nodes,
                    &self.analysis_elements_flat,
                    self.nelem,
                    self.formulation,
                    self.quadrature,
                )
            }
        }
    }

    /// Locate physical points when their owning element indices are already known.
    pub fn locate_points_in_elements(
        &self,
        points: &[[f64; 2]],
        element_indices: &[usize],
        max_iterations: usize,
    ) -> Result<Structural2dPointLocations, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self
                .locate_points_in_elements_for_family::<Quad4Family, { quad4::NODES_PER_ELEMENT }>(
                    points,
                    element_indices,
                    max_iterations,
                ),
            Structural2dElementType::Quad9 => self
                .locate_points_in_elements_for_family::<Quad9Family, { quad9::NODES_PER_ELEMENT }>(
                    points,
                    element_indices,
                    max_iterations,
                ),
        }
    }

    /// Return per-element analysis-plane area and represented volume from the model quadrature data.
    ///
    /// Returns:
    ///     Per-element measures with:
    ///     - `areas` shape `(nelem,)` and units `[area]`
    ///     - `volumes` shape `(nelem,)` and units `[volume]`
    pub fn element_measures(&self) -> Result<Structural2dElementMeasures, String> {
        let quadrature = self.quadrature()?;
        let mut areas = vec![0.0; self.nelem];
        let mut volumes = vec![0.0; self.nelem];
        for element in 0..self.nelem {
            let start = element * quadrature.points_per_element;
            let end = start + quadrature.points_per_element;
            for &weight in &quadrature.weights_area[start..end] {
                areas[element] += weight;
            }
            for &weight in &quadrature.weights_volume[start..end] {
                volumes[element] += weight;
            }
        }
        Ok(Structural2dElementMeasures { areas, volumes })
    }

    /// Evaluate total strain at located element/reference points without sparse recovery matrices.
    pub fn strain(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        displacements_full: &[f64],
    ) -> Result<Vec<[f64; 4]>, String> {
        if displacements_full.len() != self.ndof_full {
            return Err(format!(
                "displacements_full has length {}, but full system has {} DOFs",
                displacements_full.len(),
                self.ndof_full
            ));
        }
        match self.element_type {
            Structural2dElementType::Quad4 => self.evaluate_strain_for_locations_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(element_indices, reference_points, displacements_full),
            Structural2dElementType::Quad9 => self.evaluate_strain_for_locations_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(element_indices, reference_points, displacements_full),
        }
    }

    /// Evaluate stress at located element/reference points without sparse recovery matrices.
    pub fn stress(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        displacements_full: &[f64],
    ) -> Result<Vec<[f64; 4]>, String> {
        if displacements_full.len() != self.ndof_full {
            return Err(format!(
                "displacements_full has length {}, but full system has {} DOFs",
                displacements_full.len(),
                self.ndof_full
            ));
        }
        match self.element_type {
            Structural2dElementType::Quad4 => self.evaluate_stress_for_locations_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                { dof_per_element(quad4::NODES_PER_ELEMENT) },
            >(element_indices, reference_points, displacements_full),
            Structural2dElementType::Quad9 => self.evaluate_stress_for_locations_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                { dof_per_element(quad9::NODES_PER_ELEMENT) },
            >(element_indices, reference_points, displacements_full),
        }
    }

    /// Evaluate thermal strain at located element/reference points without sparse recovery matrices.
    pub fn thermal_strain(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        nodal_temperature: &[f64],
    ) -> Result<Vec<[f64; 4]>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.evaluate_thermal_for_locations_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                false,
            >(element_indices, reference_points, nodal_temperature),
            Structural2dElementType::Quad9 => self.evaluate_thermal_for_locations_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                false,
            >(element_indices, reference_points, nodal_temperature),
        }
    }

    /// Evaluate thermal stress at located element/reference points without sparse recovery matrices.
    pub fn thermal_stress(
        &self,
        element_indices: &[usize],
        reference_points: &[[f64; 2]],
        nodal_temperature: &[f64],
    ) -> Result<Vec<[f64; 4]>, String> {
        match self.element_type {
            Structural2dElementType::Quad4 => self.evaluate_thermal_for_locations_for_family::<
                Quad4Family,
                { quad4::NODES_PER_ELEMENT },
                true,
            >(element_indices, reference_points, nodal_temperature),
            Structural2dElementType::Quad9 => self.evaluate_thermal_for_locations_for_family::<
                Quad9Family,
                { quad9::NODES_PER_ELEMENT },
                true,
            >(element_indices, reference_points, nodal_temperature),
        }
    }
}

#[allow(clippy::too_many_arguments)]
/// Assemble the reduced 2D structural model and all associated operators.
pub fn assemble_structural_2d(
    nodes_rz: &[[f64; 2]],
    elements: Structural2dElements<'_>,
    material_ids: &[usize],
    material_table: &[[[f64; 4]; 4]],
    pressure_faces: &[PressureLoad],
    traction_faces: &[TractionLoad],
    thermal_material_table: Option<&[ThermalMaterial]>,
    material_orientation_angles: Option<&[f64]>,
    prescribed: &[(usize, f64)],
    formulation: Structural2dFormulation,
    quadrature: QuadratureRule,
    par: bool,
) -> Result<Structural2dModel, String> {
    match elements {
        Structural2dElements::Quad4(elements) => build_model_for_family::<
            Quad4Family,
            { quad4::NODES_PER_ELEMENT },
            { dof_per_element(quad4::NODES_PER_ELEMENT) },
        >(
            nodes_rz,
            elements,
            material_ids,
            material_table,
            pressure_faces,
            traction_faces,
            thermal_material_table,
            material_orientation_angles,
            prescribed,
            formulation,
            quadrature,
            par,
        ),
        Structural2dElements::Quad9(elements) => build_model_for_family::<
            Quad9Family,
            { quad9::NODES_PER_ELEMENT },
            { dof_per_element(quad9::NODES_PER_ELEMENT) },
        >(
            nodes_rz,
            elements,
            material_ids,
            material_table,
            pressure_faces,
            traction_faces,
            thermal_material_table,
            material_orientation_angles,
            prescribed,
            formulation,
            quadrature,
            par,
        ),
    }
}

/// Internal bundle returned by `reduce_layout`.
///
/// The tuple stores, in order:
/// - the reduced-to-full free DOF mapping,
/// - fixed DOF indices,
/// - fixed DOF values,
/// - the full-to-reduced lookup table,
/// - and a dense lookup of fixed values by full DOF index.
type ReducedLayout = (
    Vec<usize>,
    Vec<usize>,
    Vec<f64>,
    Vec<usize>,
    Vec<Option<f64>>,
);

#[allow(clippy::too_many_arguments)]
/// Assemble the full set of reduced operators for one specific quadrilateral family.
///
/// This is the family-generic core of the public assembly path.  It builds the unreduced
/// operators, applies Dirichlet reduction, compresses the final sparse matrices, and packages the
/// result into the public `Structural2dModel`.
fn build_model_for_family<Family, const NODES_PER_ELEMENT: usize, const DOF_PER_ELEMENT: usize>(
    nodes_rz: &[[f64; 2]],
    elements: &[[usize; NODES_PER_ELEMENT]],
    material_ids: &[usize],
    material_table: &[[[f64; 4]; 4]],
    pressure_faces: &[PressureLoad],
    traction_faces: &[TractionLoad],
    thermal_material_table: Option<&[ThermalMaterial]>,
    material_orientation_angles: Option<&[f64]>,
    prescribed: &[(usize, f64)],
    formulation: Structural2dFormulation,
    quadrature: QuadratureRule,
    par: bool,
) -> Result<Structural2dModel, String>
where
    Family: QuadElementFamily<NODES_PER_ELEMENT>,
{
    let mesh = QuadMeshView2d { nodes_rz, elements };
    // Reject a degenerate rest mesh before assembly. Connectivity must be sound first so the
    // geometry check can index nodes safely; assembly re-validates it as part of its own gate.
    mesh.validate_connectivity()?;
    assert_rest_mesh_valid::<Family, NODES_PER_ELEMENT>(nodes_rz, elements, quadrature)?;
    let ndof_full = nodes_rz.len() * 2;
    let nelem = elements.len();
    // Build the full-space -> reduced-space maps once. Every stored operator after this point will
    // live in the constrained reduced system, while `fixed_lookup` carries the prescribed values
    // needed to fold eliminated DOFs back into constant RHS terms.
    let (free_dofs, fixed_dofs, fixed_values, global_to_reduced, fixed_lookup) =
        reduce_layout(ndof_full, prescribed)?;
    let ndof_reduced = free_dofs.len();

    let mut constant_rhs = vec![0.0; ndof_reduced];
    // Assemble stiffness in the full displacement space first, then apply Dirichlet reduction.
    // The parallel path keeps worker chunks separate so each chunk can be reduced and sorted
    // independently before a k-way merge builds the canonical CSC structure.
    let stiffness = if par {
        let stiffness_chunks =
            assemble_stiffness_chunks_for_family_par::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                material_ids,
                material_table,
                material_orientation_angles,
                formulation,
                quadrature,
            )?;
        let sorted_chunks = reduce_sort_stiffness_chunks(
            stiffness_chunks,
            &global_to_reduced,
            &fixed_lookup,
            &mut constant_rhs,
            ndof_reduced,
        );
        csc_from_sorted_triplet_chunks(ndof_reduced, ndof_reduced, sorted_chunks)
    } else {
        let stiffness_full =
            assemble_stiffness_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                mesh,
                material_ids,
                material_table,
                material_orientation_angles,
                formulation,
                quadrature,
            )?;
        let stiffness_reduced = reduce_square_triplets(
            &stiffness_full.rows,
            &stiffness_full.cols,
            &stiffness_full.vals,
            &global_to_reduced,
            &fixed_lookup,
            &mut constant_rhs,
        );
        csc_from_triplets(ndof_reduced, ndof_reduced, stiffness_reduced)
    };
    let (thermal_reference_rhs, n_temperature_nodes) =
        if let Some(thermal_material_table) = thermal_material_table {
            let thermal_reference_full =
                thermal_reference_rhs_for_family::<Family, NODES_PER_ELEMENT, DOF_PER_ELEMENT>(
                    mesh,
                    material_ids,
                    material_table,
                    thermal_material_table,
                    material_orientation_angles,
                    formulation,
                    quadrature,
                )?;
            let reduced_reference_rhs = free_dofs
                .iter()
                .map(|&dof| thermal_reference_full[dof])
                .collect::<Vec<_>>();
            (reduced_reference_rhs, nodes_rz.len())
        } else {
            (vec![0.0; ndof_reduced], 0)
        };
    // `constant_rhs` already contains the Dirichlet offset from stiffness reduction. Add the
    // reference-temperature contribution so all load-independent terms live in one vector.
    for (dst, src) in constant_rhs.iter_mut().zip(&thermal_reference_rhs) {
        *dst += *src;
    }

    Ok(Structural2dModel {
        stiffness,
        constant_rhs,
        nq_per_element: quadrature.points_per_element(),
        n_temperature_nodes,
        pressure_faces: pressure_faces
            .iter()
            .map(|face| [face.element, usize::from(face.local_face)])
            .collect(),
        traction_faces: traction_faces
            .iter()
            .map(|face| [face.element, usize::from(face.local_face)])
            .collect(),
        analysis_nodes: nodes_rz.to_vec(),
        analysis_elements_flat: Family::flatten_elements(elements),
        nodes_per_element: NODES_PER_ELEMENT,
        element_type: Family::element_type(),
        quadrature,
        formulation,
        ndof_full,
        ndof_reduced,
        nelem,
        free_dofs,
        fixed_dofs,
        fixed_values,
        assembly: Structural2dAssemblyData {
            material_ids: material_ids.to_vec(),
            material_table: material_table.to_vec(),
            thermal_material_table: thermal_material_table.map(|table| table.to_vec()),
            material_orientation_angles: material_orientation_angles.map(|angles| angles.to_vec()),
            global_to_reduced,
            par,
        },
        lu: None,
    })
}

/// Partition full-system displacement DOFs into free and fixed sets for Dirichlet reduction.
fn reduce_layout(ndof_full: usize, prescribed: &[(usize, f64)]) -> Result<ReducedLayout, String> {
    let mut prescribed_sorted = prescribed.to_vec();
    prescribed_sorted.sort_by_key(|&(dof, _)| dof);
    for window in prescribed_sorted.windows(2) {
        if window[0].0 == window[1].0 {
            return Err(format!(
                "prescribed DOF {} is specified more than once",
                window[0].0
            ));
        }
    }
    let mut fixed_dofs = Vec::with_capacity(prescribed_sorted.len());
    let mut fixed_values = Vec::with_capacity(prescribed_sorted.len());
    let mut fixed_lookup = vec![None; ndof_full];
    for &(dof, value) in &prescribed_sorted {
        if dof >= ndof_full {
            return Err(format!(
                "prescribed DOF {dof} is out of bounds for a system with {ndof_full} DOFs"
            ));
        }
        fixed_dofs.push(dof);
        fixed_values.push(value);
        fixed_lookup[dof] = Some(value);
    }
    let mut is_fixed = vec![false; ndof_full];
    for &dof in &fixed_dofs {
        is_fixed[dof] = true;
    }
    let mut free_dofs = Vec::with_capacity(ndof_full - fixed_dofs.len());
    let mut global_to_reduced = vec![usize::MAX; ndof_full];
    for dof in 0..ndof_full {
        if !is_fixed[dof] {
            global_to_reduced[dof] = free_dofs.len();
            free_dofs.push(dof);
        }
    }
    Ok((
        free_dofs,
        fixed_dofs,
        fixed_values,
        global_to_reduced,
        fixed_lookup,
    ))
}

/// Reduce full-system stiffness triplets to the free DOF subspace.
///
/// This helper also accumulates the Dirichlet offset `-K_fc u_c` into `constant_rhs`.
fn reduce_square_triplets(
    rows: &[usize],
    cols: &[usize],
    vals: &[f64],
    global_to_reduced: &[usize],
    fixed_lookup: &[Option<f64>],
    constant_rhs: &mut [f64],
) -> Vec<Triplet<usize, usize, f64>> {
    let mut triplets = Vec::with_capacity(vals.len());
    for ((&row, &col), &value) in rows.iter().zip(cols).zip(vals) {
        let reduced_row = global_to_reduced[row];
        let reduced_col = global_to_reduced[col];
        if reduced_row != usize::MAX && reduced_col != usize::MAX {
            triplets.push(Triplet::new(reduced_row, reduced_col, value));
        } else if reduced_row != usize::MAX
            && let Some(fixed_value) = fixed_lookup[col]
        {
            constant_rhs[reduced_row] -= value * fixed_value;
        }
    }
    triplets
}

/// Reduce, sort, and coalesce each stiffness chunk independently.
///
/// Each Rayon worker returns full-system triplets for a disjoint element range.  Reduction can
/// still create duplicate `(row, col)` entries inside a chunk, so this function canonicalizes each
/// chunk before the final k-way merge.  The Dirichlet RHS offsets are accumulated per chunk to
/// avoid shared mutable state during the parallel pass.
fn reduce_sort_stiffness_chunks(
    chunks: Vec<StiffnessTriplets>,
    global_to_reduced: &[usize],
    fixed_lookup: &[Option<f64>],
    constant_rhs: &mut [f64],
    ndof_reduced: usize,
) -> Vec<Vec<Triplet<usize, usize, f64>>> {
    let reduced_chunks = chunks
        .into_par_iter()
        .map(|chunk| {
            let mut local_rhs = vec![0.0; ndof_reduced];
            let triplets = reduce_square_triplets(
                &chunk.rows,
                &chunk.cols,
                &chunk.vals,
                global_to_reduced,
                fixed_lookup,
                &mut local_rhs,
            );
            (sort_coalesce_triplets(triplets), local_rhs)
        })
        .collect::<Vec<_>>();

    let mut sorted_chunks = Vec::with_capacity(reduced_chunks.len());
    for (triplets, local_rhs) in reduced_chunks {
        for (dst, src) in constant_rhs.iter_mut().zip(local_rhs) {
            *dst += src;
        }
        sorted_chunks.push(triplets);
    }
    sorted_chunks
}

/// Return triplets in canonical CSC order with duplicate `(column, row)` entries coalesced.
fn sort_coalesce_triplets(
    mut triplets: Vec<Triplet<usize, usize, f64>>,
) -> Vec<Triplet<usize, usize, f64>> {
    triplets.sort_by(|a, b| a.col.cmp(&b.col).then_with(|| a.row.cmp(&b.row)));

    let mut coalesced = Vec::with_capacity(triplets.len());
    let mut pending: Option<Triplet<usize, usize, f64>> = None;
    for triplet in triplets {
        if triplet.val == 0.0 {
            continue;
        }
        match pending {
            Some(mut current) if current.col == triplet.col && current.row == triplet.row => {
                current.val += triplet.val;
                pending = Some(current);
            }
            Some(current) => {
                if current.val != 0.0 {
                    coalesced.push(current);
                }
                pending = Some(triplet);
            }
            None => pending = Some(triplet),
        }
    }
    if let Some(current) = pending
        && current.val != 0.0
    {
        coalesced.push(current);
    }
    coalesced
}

/// Drop rows belonging to fixed displacement DOFs from one full-system load operator.
fn reduce_row_operator(
    operator: SparseOperator,
    global_to_reduced: &[usize],
    nrow_reduced: usize,
) -> SparseOperator {
    let mut rows = Vec::with_capacity(operator.vals.len());
    let mut cols = Vec::with_capacity(operator.vals.len());
    let mut vals = Vec::with_capacity(operator.vals.len());
    for ((row, col), value) in operator
        .rows
        .into_iter()
        .zip(operator.cols)
        .zip(operator.vals)
    {
        let reduced_row = global_to_reduced[row];
        if reduced_row != usize::MAX {
            rows.push(reduced_row);
            cols.push(col);
            vals.push(value);
        }
    }
    SparseOperator {
        rows,
        cols,
        vals,
        nrow: nrow_reduced,
        ncol: operator.ncol,
    }
}

/// Reduce one full-system load operator and compress it into CSR form.
fn reduce_row_operator_to_csr(
    operator: SparseOperator,
    global_to_reduced: &[usize],
    nrow_reduced: usize,
) -> Result<SparseRowMat<usize, f64>, String> {
    let operator = reduce_row_operator(operator, global_to_reduced, nrow_reduced);
    csr_from_parts(
        operator.nrow,
        operator.ncol,
        operator.rows,
        operator.cols,
        operator.vals,
    )
}

/// Compress triplet data into a CSR matrix with checked shape and index validity.
fn csr_from_parts(
    nrow: usize,
    ncol: usize,
    rows: Vec<usize>,
    cols: Vec<usize>,
    vals: Vec<f64>,
) -> Result<SparseRowMat<usize, f64>, String> {
    let triplets = rows
        .into_iter()
        .zip(cols)
        .zip(vals)
        .map(|((row, col), val)| Triplet::new(row, col, val))
        .collect::<Vec<_>>();
    SparseRowMat::try_new_from_triplets(nrow, ncol, &triplets)
        .map_err(|err| format!("failed to build CSR operator: {err:?}"))
}

/// Compress stiffness triplets into the CSC format used by the cached sparse LU factorization.
fn csc_from_triplets(
    nrow: usize,
    ncol: usize,
    mut triplets: Vec<Triplet<usize, usize, f64>>,
) -> SparseColMat<usize, f64> {
    triplets.sort_by(|a, b| a.col.cmp(&b.col).then_with(|| a.row.cmp(&b.row)));

    let (col_ptr, row_idx, vals) = pack_sorted_triplets_to_csc(nrow, ncol, triplets);
    let symbolic = SymbolicSparseColMat::new_checked(nrow, ncol, col_ptr, None, row_idx);
    SparseColMat::new(symbolic, vals)
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
struct TripletCursor {
    /// Current triplet column in a sorted chunk.
    col: usize,
    /// Current triplet row in a sorted chunk.
    row: usize,
    /// Index of the source chunk in the outer chunk slice.
    chunk: usize,
    /// Index of the current triplet inside that source chunk.
    index: usize,
}

impl Ord for TripletCursor {
    fn cmp(&self, other: &Self) -> Ordering {
        // `BinaryHeap` is a max-heap, so reverse the ordering to pop the smallest `(col, row)`
        // cursor first during the k-way merge.
        other
            .col
            .cmp(&self.col)
            .then_with(|| other.row.cmp(&self.row))
            .then_with(|| other.chunk.cmp(&self.chunk))
            .then_with(|| other.index.cmp(&self.index))
    }
}

impl PartialOrd for TripletCursor {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

struct CscPartsBuilder {
    /// Number of matrix rows.
    nrow: usize,
    /// Number of matrix columns.
    ncol: usize,
    /// CSC column offsets under construction.
    col_ptr: Vec<usize>,
    /// Row index for each stored value.
    row_idx: Vec<usize>,
    /// Nonzero values in column-major CSC order.
    vals: Vec<f64>,
    /// First column whose pointer has not yet been finalized.
    next_col: usize,
    /// Most recent sorted entry, held back so duplicates can be coalesced before writing.
    pending: Option<Triplet<usize, usize, f64>>,
}

impl CscPartsBuilder {
    /// Start a CSC builder that accepts triplets sorted by `(column, row)`.
    fn new(nrow: usize, ncol: usize, capacity: usize) -> Self {
        let mut col_ptr = Vec::with_capacity(ncol + 1);
        col_ptr.push(0);
        Self {
            nrow,
            ncol,
            col_ptr,
            row_idx: Vec::with_capacity(capacity),
            vals: Vec::with_capacity(capacity),
            next_col: 0,
            pending: None,
        }
    }

    /// Append one sorted triplet, coalescing duplicates and skipping exact zeros.
    fn push_sorted(&mut self, triplet: Triplet<usize, usize, f64>) {
        debug_assert!(triplet.row < self.nrow);
        debug_assert!(triplet.col < self.ncol);
        if triplet.val == 0.0 {
            return;
        }
        match self.pending {
            Some(mut current) if current.col == triplet.col && current.row == triplet.row => {
                current.val += triplet.val;
                self.pending = Some(current);
            }
            Some(current) => {
                self.push_entry(current);
                self.pending = Some(triplet);
            }
            None => self.pending = Some(triplet),
        }
    }

    /// Finalize pending entries and close all remaining column pointers.
    fn finish(mut self) -> (Vec<usize>, Vec<usize>, Vec<f64>) {
        if let Some(current) = self.pending.take() {
            self.push_entry(current);
        }
        while self.next_col < self.ncol {
            self.col_ptr.push(self.row_idx.len());
            self.next_col += 1;
        }
        debug_assert_eq!(self.col_ptr.len(), self.ncol + 1);
        (self.col_ptr, self.row_idx, self.vals)
    }

    /// Write one already coalesced CSC entry and fill empty-column pointers before it.
    fn push_entry(&mut self, entry: Triplet<usize, usize, f64>) {
        while self.next_col < entry.col {
            self.col_ptr.push(self.row_idx.len());
            self.next_col += 1;
        }
        if entry.val != 0.0 {
            self.row_idx.push(entry.row);
            self.vals.push(entry.val);
        }
    }
}

/// Merge already sorted/coalesced triplet chunks into the CSC format used by sparse LU.
fn csc_from_sorted_triplet_chunks(
    nrow: usize,
    ncol: usize,
    chunks: Vec<Vec<Triplet<usize, usize, f64>>>,
) -> SparseColMat<usize, f64> {
    let (col_ptr, row_idx, vals) = merge_sorted_triplet_chunks_to_csc(nrow, ncol, &chunks);
    let symbolic = SymbolicSparseColMat::new_checked(nrow, ncol, col_ptr, None, row_idx);
    SparseColMat::new(symbolic, vals)
}

fn merge_sorted_triplet_chunks_to_csc(
    nrow: usize,
    ncol: usize,
    chunks: &[Vec<Triplet<usize, usize, f64>>],
) -> (Vec<usize>, Vec<usize>, Vec<f64>) {
    let capacity = chunks.iter().map(Vec::len).sum::<usize>();
    let mut builder = CscPartsBuilder::new(nrow, ncol, capacity);
    let mut heap = BinaryHeap::with_capacity(chunks.len());
    // Seed the heap with the first triplet from each sorted chunk.  Every pop advances only that
    // source chunk, giving a k-way merge without flattening and sorting all triplets again.
    for (chunk_index, chunk) in chunks.iter().enumerate() {
        if let Some(first) = chunk.first() {
            heap.push(TripletCursor {
                col: first.col,
                row: first.row,
                chunk: chunk_index,
                index: 0,
            });
        }
    }

    while let Some(cursor) = heap.pop() {
        let triplet = chunks[cursor.chunk][cursor.index];
        builder.push_sorted(triplet);

        let next_index = cursor.index + 1;
        if next_index < chunks[cursor.chunk].len() {
            let next = chunks[cursor.chunk][next_index];
            heap.push(TripletCursor {
                col: next.col,
                row: next.row,
                chunk: cursor.chunk,
                index: next_index,
            });
        }
    }

    builder.finish()
}

/// Pack triplets sorted by `(column, row)` into canonical CSC arrays.
fn pack_sorted_triplets_to_csc(
    nrow: usize,
    ncol: usize,
    triplets: Vec<Triplet<usize, usize, f64>>,
) -> (Vec<usize>, Vec<usize>, Vec<f64>) {
    let mut builder = CscPartsBuilder::new(nrow, ncol, triplets.len());
    for triplet in triplets {
        builder.push_sorted(triplet);
    }
    builder.finish()
}

/// Gather one element's node coordinates from the flattened analysis connectivity.
fn element_coords_from_flat<const NODES_PER_ELEMENT: usize>(
    analysis_nodes: &[[f64; 2]],
    analysis_elements_flat: &[usize],
    element_index: usize,
) -> Result<[[f64; 2]; NODES_PER_ELEMENT], String> {
    let start = element_index * NODES_PER_ELEMENT;
    let end = start + NODES_PER_ELEMENT;
    let conn = analysis_elements_flat
        .get(start..end)
        .ok_or_else(|| format!("element index {element_index} out of bounds"))?;
    let mut coords = [[0.0; 2]; NODES_PER_ELEMENT];
    for (local_node, &node) in conn.iter().enumerate() {
        coords[local_node] = *analysis_nodes.get(node).ok_or_else(|| {
            format!(
                "element {element_index} references node {node}, but analysis mesh has only {} nodes",
                analysis_nodes.len()
            )
        })?;
    }
    Ok(coords)
}

/// Gather one element's node coordinates from a node array in local-node order.
fn gather_coords<const NODES_PER_ELEMENT: usize>(
    nodes: &[[f64; 2]],
    element: &[usize; NODES_PER_ELEMENT],
) -> [[f64; 2]; NODES_PER_ELEMENT] {
    std::array::from_fn(|local_node| nodes[element[local_node]])
}

/// Find any element whose mapping Jacobian is non-positive at the family's certifying points.
///
/// Returns the count of degenerate elements and, when any exist, the index of the worst one (the
/// most negative minimum Jacobian) so the caller can localize the failure. The same kernel
/// certifies both shapes the mesh ever takes -- the rest mesh at assembly and the deformed mesh
/// after a solve -- so the two checkpoints cannot drift apart on what counts as a valid quad.
fn find_degenerate_quads<Family, const NODES_PER_ELEMENT: usize>(
    nodes: &[[f64; 2]],
    elements: &[[usize; NODES_PER_ELEMENT]],
    quadrature: QuadratureRule,
) -> (usize, Option<usize>)
where
    Family: QuadElementFamily<NODES_PER_ELEMENT>,
{
    // Shape-function gradients at the certifying points are element-independent; evaluate once.
    let grads = Family::jacobian_check_points(quadrature)
        .into_iter()
        .map(|reference| Family::ReferenceElement::grad_ref(reference[0], reference[1]))
        .collect::<Vec<_>>();
    let mut num_degenerate = 0usize;
    let mut worst: Option<(usize, f64)> = None;
    for (element_index, element) in elements.iter().enumerate() {
        let coords = gather_coords(nodes, element);
        let element_min = grads
            .iter()
            .map(|grad| mapping::det_j(&mapping::jacobian(&coords, grad)))
            .fold(f64::INFINITY, f64::min);
        if element_min <= 0.0 {
            num_degenerate += 1;
        }
        if worst.is_none_or(|(_, worst_min)| element_min < worst_min) {
            worst = Some((element_index, element_min));
        }
    }
    (num_degenerate, worst.map(|(index, _)| index))
}

/// Reject a rest mesh that already contains non-convex or degenerate quads, before assembly.
///
/// gmsh recombination can emit a sliver or locally non-convex quad near sharp geometry features,
/// where the corner Jacobian goes non-positive even though the global winding looks fine. Catching
/// it here stops a degenerate mesh from reaching the assembler, with the offending element's
/// corners named for diagnosis.
fn assert_rest_mesh_valid<Family, const NODES_PER_ELEMENT: usize>(
    nodes: &[[f64; 2]],
    elements: &[[usize; NODES_PER_ELEMENT]],
    quadrature: QuadratureRule,
) -> Result<(), String>
where
    Family: QuadElementFamily<NODES_PER_ELEMENT>,
{
    let (num_degenerate, worst) =
        find_degenerate_quads::<Family, NODES_PER_ELEMENT>(nodes, elements, quadrature);
    let Some(worst_index) = worst.filter(|_| num_degenerate > 0) else {
        return Ok(());
    };
    let corners = gather_coords(nodes, &elements[worst_index]);
    Err(format!(
        "{num_degenerate} of {} elements are non-convex or degenerate in the rest mesh, before any \
         load is applied: the mapping Jacobian is non-positive, so the assembled stiffness and any \
         recovered field are invalid. Worst element corners (r, z) [m]: {:?}, {:?}, {:?}, {:?}. Fix \
         the mesh upstream (recombination, or geometry near sharp features).",
        elements.len(),
        corners[0],
        corners[1],
        corners[2],
        corners[3],
    ))
}

/// Reconstruct typed element connectivity from the stored flattened analysis connectivity.
fn elements_from_flat<const NODES_PER_ELEMENT: usize>(
    analysis_elements_flat: &[usize],
    nelem: usize,
    element_type: Structural2dElementType,
) -> Result<Vec<[usize; NODES_PER_ELEMENT]>, String> {
    let expected = nelem * NODES_PER_ELEMENT;
    if analysis_elements_flat.len() != expected {
        return Err(format!(
            "{} analysis elements with element type {} require {expected} flattened node entries, got {}",
            nelem,
            element_type.as_str(),
            analysis_elements_flat.len()
        ));
    }
    let mut elements = Vec::with_capacity(nelem);
    for chunk in analysis_elements_flat.chunks_exact(NODES_PER_ELEMENT) {
        let mut conn = [0usize; NODES_PER_ELEMENT];
        conn.copy_from_slice(chunk);
        elements.push(conn);
    }
    Ok(elements)
}

fn validate_location_lengths(
    element_indices: &[usize],
    reference_points: &[[f64; 2]],
) -> Result<(), String> {
    if element_indices.len() != reference_points.len() {
        return Err(format!(
            "element_indices has length {}, but reference_points has length {}",
            element_indices.len(),
            reference_points.len()
        ));
    }
    Ok(())
}

/// Recompute element-major quadrature locations and mapped weights for one stored element family.
fn quadrature_locations_for_family<Family, const NODES_PER_ELEMENT: usize>(
    analysis_nodes: &[[f64; 2]],
    analysis_elements_flat: &[usize],
    nelem: usize,
    formulation: Structural2dFormulation,
    quadrature: QuadratureRule,
) -> Result<Structural2dQuadrature, String>
where
    Family: QuadElementFamily<NODES_PER_ELEMENT>,
{
    let references = gauss_volume::<f64>(quadrature);
    let nq_per_element = references.len();
    let npoints = nelem * nq_per_element;
    let mut points = Vec::with_capacity(npoints);
    let mut element_indices = Vec::with_capacity(npoints);
    let mut reference_points = Vec::with_capacity(npoints);
    let mut weights_area = Vec::with_capacity(npoints);
    let mut weights_volume = Vec::with_capacity(npoints);
    for element_index in 0..nelem {
        let coords = element_coords_from_flat::<NODES_PER_ELEMENT>(
            analysis_nodes,
            analysis_elements_flat,
            element_index,
        )?;
        for &(reference, weight) in &references {
            let shape = Family::ReferenceElement::shape(reference[0], reference[1]);
            let grad_ref = Family::ReferenceElement::grad_ref(reference[0], reference[1]);
            let jac = mapping::jacobian(&coords, &grad_ref);
            let det_j = mapping::det_j(&jac);
            let point = mapping::map_point(&coords, &shape);
            let area_weight = det_j * weight;
            element_indices.push(element_index);
            reference_points.push(reference);
            points.push(point);
            weights_area.push(area_weight);
            weights_volume.push(formulation.volume_scale(point, det_j, weight)?);
        }
    }
    Ok(Structural2dQuadrature {
        locations: Structural2dPointLocations {
            points,
            element_indices,
            reference_points,
        },
        weights_area,
        weights_volume,
        points_per_element: nq_per_element,
    })
}

#[cfg(test)]
mod tests {
    use super::{Structural2dElements, Structural2dModel, assemble_structural_2d, csr_from_parts};
    use crate::mesh::QuadMeshView2d;
    use crate::mesh::QuadratureRule;
    use crate::mesh::quad2d::{Quad4ReferenceElement, quad_mesh_strain_operator};
    use crate::physics::solenoid_stress::convenience::{
        isotropic_axisymmetric_material, isotropic_axisymmetric_thermal_material,
    };
    use crate::physics::solenoid_stress::test_utils::{SINGLE_QUAD4_ELEMENTS, SINGLE_QUAD4_NODES};
    use crate::physics::solenoid_stress::types::{
        PressureLoad, Structural2dFormulation, TractionLoad,
    };
    use faer::sparse::SparseRowMat;

    fn assert_allclose(actual: &[f64], expected: &[f64]) {
        assert_eq!(actual.len(), expected.len());
        for (&actual, &expected) in actual.iter().zip(expected) {
            let scale = actual.abs().max(expected.abs()).max(1.0);
            assert!(
                (actual - expected).abs() <= 1.0e-12 * scale,
                "actual {actual} != expected {expected}"
            );
        }
    }

    fn csr_matvec(operator: &SparseRowMat<usize, f64>, input: &[f64]) -> Vec<f64> {
        assert_eq!(operator.ncols(), input.len());
        let mut output = vec![0.0; operator.nrows()];
        for row in 0..operator.nrows() {
            for index in operator.row_ptr()[row]..operator.row_ptr()[row + 1] {
                output[row] += operator.val()[index] * input[operator.col_idx()[index]];
            }
        }
        output
    }

    fn add_assign(dst: &mut [f64], src: &[f64]) {
        assert_eq!(dst.len(), src.len());
        for (dst, src) in dst.iter_mut().zip(src) {
            *dst += *src;
        }
    }

    fn single_element_thermal_model() -> Structural2dModel {
        let material = isotropic_axisymmetric_material(200.0e9, 0.27);
        let thermal = isotropic_axisymmetric_thermal_material(1.2e-5, 293.15);
        assemble_structural_2d(
            &SINGLE_QUAD4_NODES,
            Structural2dElements::Quad4(&SINGLE_QUAD4_ELEMENTS),
            &[0],
            &[material],
            &[PressureLoad {
                element: 0,
                local_face: 1,
            }],
            &[TractionLoad {
                element: 0,
                local_face: 2,
            }],
            Some(&[thermal]),
            None,
            &[(0, 1.0e-6)],
            Structural2dFormulation::Axisymmetric,
            QuadratureRule::GaussLegendre3,
            false,
        )
        .expect("single-element model assembly should succeed")
    }

    #[test]
    fn assembly_rejects_a_degenerate_rest_mesh() {
        // A single quad4 whose third corner is dragged across its own diagonal, so the element is
        // non-convex (one corner cross product non-positive) before any load is applied.
        let nodes = [[0.0_f64, 0.0], [1.0, 0.0], [-0.5, -0.5], [0.0, 1.0]];
        let elements = [[0usize, 1, 2, 3]];
        let error = assemble_structural_2d(
            &nodes,
            Structural2dElements::Quad4(&elements),
            &[0],
            &[isotropic_axisymmetric_material(200.0e9, 0.27)],
            &[],
            &[],
            None,
            None,
            &[],
            Structural2dFormulation::Axisymmetric,
            QuadratureRule::GaussLegendre3,
            false,
        )
        .expect_err("a non-convex rest quad must be rejected at assembly");
        assert!(error.contains("degenerate in the rest mesh"));
    }

    #[test]
    fn deformed_mesh_guard_passes_small_and_rejects_inverting_displacement() {
        let model = single_element_thermal_model();

        // A uniform microstrain-scale displacement is nearly a rigid shift: every quad stays convex.
        let benign = vec![1.0e-6; model.ndof_full];
        model
            .assert_deformed_mesh_valid(&benign)
            .expect("a microstrain displacement must keep the mesh valid");

        // Drag one corner far across the element's interior so the quad folds over on itself.
        let elements = model.analysis_elements::<4>().expect("analysis elements");
        let folded_corner = elements[0][2];
        let mut inverting = vec![0.0; model.ndof_full];
        inverting[2 * folded_corner] = -10.0;
        inverting[2 * folded_corner + 1] = -10.0;
        let error = model
            .assert_deformed_mesh_valid(&inverting)
            .expect_err("a corner dragged across the element must invert it");
        assert!(error.contains("invert under the displacement"));
    }

    #[test]
    fn build_rhs_matrix_free_matches_sparse_export_assembly() {
        let model = single_element_thermal_model();

        let body_force = [1.25e3, -2.5e3];
        let pressure = [3.0e5];
        let traction = [4.0e4, -1.5e4];
        let temperature = [296.0, 297.0, 298.0, 299.0];
        let matrix_free = model
            .build_rhs(
                Some(&body_force),
                Some(&pressure),
                Some(&traction),
                Some(&temperature),
            )
            .expect("matrix-free RHS should build");

        let mut sparse = model.constant_rhs.clone();
        add_assign(
            &mut sparse,
            &csr_matvec(
                &model.body_force_to_rhs().expect("body-force operator"),
                &body_force,
            ),
        );
        add_assign(
            &mut sparse,
            &csr_matvec(
                &model.pressure_to_rhs().expect("pressure operator"),
                &pressure,
            ),
        );
        add_assign(
            &mut sparse,
            &csr_matvec(
                &model.traction_to_rhs().expect("traction operator"),
                &traction,
            ),
        );
        add_assign(
            &mut sparse,
            &csr_matvec(
                &model.temperature_to_rhs().expect("temperature operator"),
                &temperature,
            ),
        );
        assert_allclose(&matrix_free, &sparse);
    }

    #[test]
    fn matrix_free_strain_matches_sparse_recovery_exports() {
        let model = single_element_thermal_model();
        let displacements_full = [
            1.0e-6, -2.0e-6, 2.0e-6, 1.0e-6, -1.5e-6, 2.5e-6, 3.0e-6, -3.5e-6,
        ];
        let locations = model
            .quadrature()
            .expect("quadrature should evaluate")
            .locations;
        let matrix_free = model
            .strain(
                &locations.element_indices,
                &locations.reference_points,
                &displacements_full,
            )
            .expect("matrix-free strain should evaluate");

        let elements = model.analysis_elements::<4>().expect("analysis elements");
        let mesh = QuadMeshView2d {
            nodes_rz: &model.analysis_nodes,
            elements: &elements,
        };
        let strain_operator = quad_mesh_strain_operator::<Quad4ReferenceElement, 4, 8>(
            mesh,
            &locations.element_indices,
            &locations.reference_points,
            model.formulation,
        )
        .expect("location-based sparse strain operator should build");
        let strain_operator = csr_from_parts(
            strain_operator.nrow,
            strain_operator.ncol,
            strain_operator.rows,
            strain_operator.cols,
            strain_operator.vals,
        )
        .expect("strain operator should convert to CSR");
        let expected = csr_matvec(&strain_operator, &displacements_full);
        let actual = matrix_free
            .into_iter()
            .flat_map(|sample| sample.into_iter())
            .collect::<Vec<_>>();
        assert_allclose(&actual, &expected);
    }
}