cfd-craft 0.2.0

Research solver for incompressible Cartesian flow and the dispersion of gases and particles around buildings, with bounded, independently checked runs
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
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
//! The case contract `cfd-craft.case/0.1`: the typed JSON input of a native
//! run, its semantic checks and its identities.
//!
//! A [`Case`] describes one calculation completely: domain and grid, boundary
//! conditions, the flow model, solid boxes, passive constituents with their
//! sources, initial conditions and receptors, numerics, run budgets and the
//! output policy. Nothing outside the case configures the solver.
//!
//! Checking happens in three layers:
//!
//! 1. Deserialization is strict: unknown fields and variants are rejected.
//!    [`input::parse_case`](crate::input::parse_case) reports a failure as
//!    `CFD-E000` with the JSON path of the offending element.
//! 2. [`Case::check`] applies the semantic checks (units, ranges, budgets,
//!    geometry, physical combinations) and returns a [`CheckReport`] of coded
//!    [`Diagnostic`]s; [`Case::validate`] turns an invalid report into an
//!    error.
//! 3. Checks that need the built grid (box representation, source and
//!    receptor support) run in [`Grid::build`](crate::grid::Grid::build),
//!    [`transport::source_fields`](crate::transport::source_fields) and
//!    [`input::full_check`](crate::input::full_check).
//!
//! Every code is listed in the
//! [diagnostics catalog](https://github.com/dietmarwo/CFD-craft/blob/main/docs/diagnostics.md).
//!
//! Units follow the field names: `_m` meters, `_m_s` m/s, `_m2_s` m2/s,
//! `_kg_s` kg/s, `_kg_m3` kg/m3, `_s` seconds, `_mib` MiB. Coordinates are
//! local east (x), north (y) and up (z) from the lower domain corner.
//!
//! These types derive `JsonSchema`, and `cfd-craft describe` prints the
//! generated `case_schema`; the schemas there are authoritative for JSON
//! clients. [`Case::physics_hash`] identifies the physical and numerical
//! problem for restarts, and [`hash_bytes`] is the digest used for every
//! identity.
//!
//! # Example
//!
//! ```
//! use cfd_craft::case::Case;
//!
//! let json = r#"{
//!   "schema_version": "cfd-craft.case/0.1", "name": "tiny",
//!   "domain": {"size_m": [4.0, 1.0, 1.0]}, "grid": {"cells": [8, 2, 2]},
//!   "boundaries": {"x": "open", "y": "periodic", "z": "periodic"},
//!   "flow": {"kind": "prescribed_uniform", "velocity_m_s": [1.0, 0.0, 0.0]},
//!   "constituents": [{"id": "tracer", "diffusivity_m2_s": 0.01}],
//!   "sources": [{"kind": "point", "id": "S0", "constituent": "tracer",
//!                "point_m": [0.5, 0.5, 0.5], "rate_kg_s": 1e-6}],
//!   "receptors": [{"kind": "point", "id": "R1", "point_m": [3.0, 0.5, 0.5]}],
//!   "numerics": {"transport": "linear_upwind", "max_time_step_s": 1.0,
//!     "advective_cfl_max": 0.4, "pressure_relative_tolerance": 1e-10,
//!     "pressure_max_iterations": 100, "steady_residual_tolerance": 1e-5,
//!     "steady_monitor_tolerance": 1e-3, "monitor_window": 10},
//!   "run": {"mode": "steady", "end_time_s": 1.0, "max_steps": 1000,
//!     "wall_time_limit_s": 30.0, "threads": 1, "memory_limit_mib": 64},
//!   "output": {"fields": false, "checkpoint": false, "max_total_mib": 4}
//! }"#;
//! let case: Case = serde_json::from_str(json).unwrap();
//! let report = case.validate().unwrap();
//! assert!(report.valid);
//! assert_eq!(report.cells, 32);
//!
//! // Unknown fields are a deserialization error, not a semantic warning.
//! let typo = json.replace("\"point_m\": [0.5", "\"position_m\": [0.5");
//! assert!(serde_json::from_str::<Case>(&typo).is_err());
//! ```

// Doc comments on these types become `description` entries of the generated
// case schema. Two kinds of item carry rustdoc-only docs
// (`#[cfg_attr(doc, doc = ...)]`) instead: unit variants of string enums, where
// a `///` comment would turn the schema's `enum` into a `oneOf`, and fields
// whose type is another schema type, where it would wrap the field's `$ref` in
// an `allOf`. Both would change the schema's shape. The meaning of those items
// is also stated in the documented container, which the schema keeps.
use crate::Result;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;

/// A complete native case, schema version `cfd-craft.case/0.1`. Nothing
/// outside the case configures the solver. Unknown fields are rejected; the
/// lists default to empty when omitted. Deserialization checks only the
/// types: the semantic checks (ranges, units, budgets, geometry) are those of
/// `cfd-craft check`, which reports coded diagnostics with field paths.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Case {
    /// Contract version; must be `cfd-craft.case/0.1` (CFD-E001).
    pub schema_version: String,
    /// Nonempty experiment label (CFD-E002), copied into the result. It is
    /// excluded from the physics hash, so renaming keeps checkpoints
    /// compatible.
    pub name: String,
    #[cfg_attr(doc, doc = "Physical extent of the domain; see [`Domain`].")]
    #[allow(missing_docs)]
    pub domain: Domain,
    #[cfg_attr(doc, doc = "Uniform cell counts per axis; see [`GridSpec`].")]
    #[allow(missing_docs)]
    pub grid: GridSpec,
    #[cfg_attr(doc, doc = "Boundary condition per axis; see [`Boundaries`].")]
    #[allow(missing_docs)]
    pub boundaries: Boundaries,
    #[cfg_attr(doc, doc = "Flow model; see [`Flow`].")]
    #[allow(missing_docs)]
    pub flow: Flow,
    /// Axis-aligned solid boxes (buildings, obstacles). A cell is solid when
    /// its center lies inside a box. Not allowed with `prescribed_uniform`
    /// flow (CFD-E013). Optional footprint_m selects polygon prisms.
    /// Masked multigrid is supported. Default: empty.
    #[serde(default)]
    pub geometry: Vec<Block>,
    /// Optional aerodynamic roughness of building walls in meters, independent
    /// of cell width. Omission reproduces the legacy wall recipe (CFD-E085).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub building_roughness_m: Option<f64>,
    /// Warn if a represented street between solids is narrower than this
    /// number of horizontal cells. Omitted for legacy cases (CFD-W011).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub geometry_min_passage_cells: Option<usize>,
    /// Tangentially moving `wall` faces of the domain, such as a cavity lid;
    /// laminar flow only (CFD-E021). Default: empty.
    #[serde(default)]
    pub moving_walls: Vec<MovingWall>,
    /// Passive gases transported by the flow. Default: empty, which solves
    /// the flow only.
    #[serde(default)]
    pub constituents: Vec<Constituent>,
    /// Opt-in porous canopy boxes; zero leaf density has no numerical effect.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub vegetation: Vec<Vegetation>,
    /// Continuous emissions into the constituents. Default: empty.
    #[serde(default)]
    pub sources: Vec<Source>,
    /// Initial concentration fields, at most one per constituent (CFD-E018);
    /// a constituent without one starts at zero. Default: empty.
    #[serde(default)]
    pub initial_conditions: Vec<Initial>,
    /// Named concentration samples, reported for every constituent in the
    /// result's `receptors_kg_m3` and in probes.csv. Default: empty.
    #[serde(default)]
    pub receptors: Vec<Receptor>,
    #[cfg_attr(
        doc,
        doc = "Discretization choices, tolerances and the steady monitor window; see [`Numerics`]."
    )]
    #[allow(missing_docs)]
    pub numerics: Numerics,
    #[cfg_attr(doc, doc = "Mode, endpoint and execution budgets; see [`Run`].")]
    #[allow(missing_docs)]
    pub run: Run,
    #[cfg_attr(doc, doc = "Artifact retention and output budget; see [`Output`].")]
    #[allow(missing_docs)]
    pub output: Output,
}
/// Physical extent of the domain: a box from the origin to `size_m`.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Domain {
    /// Lengths along x (east), y (north) and z (up) in meters, each finite
    /// and within 1e-6..1e9 (CFD-E003).
    pub size_m: [f64; 3],
}
/// Uniform Cartesian grid: the cell size along an axis is the domain length
/// divided by the cell count.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GridSpec {
    /// Cell counts along x, y and z: 2..4096 per axis, or 1..4096 on a
    /// periodic axis, which makes a two-dimensional run (CFD-E004). The total
    /// enters the memory estimate and may not exceed 1e8 cells (CFD-E006).
    pub cells: [usize; 3],
}
/// Boundary condition of the pair of domain faces normal to one axis.
/// `periodic` identifies the two faces. `open` lets flow in or out: each face
/// is an inlet or an outlet by the sign of the case flow's normal velocity
/// there; inlets prescribe that velocity and a zero concentration increment,
/// outlets fix the pressure level, and faces without normal flow are slip
/// faces. `wall` is impermeable and no-slip (a rough wall with
/// `neutral_rans`). `slip` is impermeable and stress-free. `ground_open_top`
/// (z only, CFD-E005) is a rough ground with a top that applies the
/// boundary-layer shear stress; `neutral_rans` requires it (CFD-E011) and
/// laminar flow may not use it (CFD-E023).
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Boundary {
    #[cfg_attr(
        doc,
        doc = "The two faces are identified, so flow and constituents leaving one enter the other. A periodic axis may have one cell."
    )]
    #[allow(missing_docs)]
    Periodic,
    #[cfg_attr(
        doc,
        doc = "Inflow or outflow, decided per face by the normal component of [`Flow::vector`] at the face center: inlets prescribe that velocity and a zero concentration increment, outlets fix the pressure level and anchor their fluid component, and faces without normal flow act as slip faces."
    )]
    #[allow(missing_docs)]
    Open,
    #[cfg_attr(
        doc,
        doc = "Impermeable no-slip wall, optionally moving tangentially ([`MovingWall`]); with `neutral_rans` a rough wall with the wall functions of the flow model card."
    )]
    #[allow(missing_docs)]
    Wall,
    #[cfg_attr(
        doc,
        doc = "Impermeable and stress-free: zero normal velocity and zero tangential gradient."
    )]
    #[allow(missing_docs)]
    Slip,
    #[cfg_attr(
        doc,
        doc = "z only: a rough ground at z = 0 with roughness `roughness_m`, and an impermeable top that applies the equilibrium shear stress of the `neutral_rans` inflow. Required by `neutral_rans`, rejected for laminar flow, where the top would act as a no-slip lid."
    )]
    #[allow(missing_docs)]
    GroundOpenTop,
}
/// Boundary conditions of the domain: `x`, `y` and `z` each set the condition
/// of the two domain faces normal to that axis (west and east, south and
/// north, ground and top).
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Boundaries {
    #[cfg_attr(doc, doc = "Condition at x = 0 and x = Lx (west and east).")]
    #[allow(missing_docs)]
    pub x: Boundary,
    #[cfg_attr(doc, doc = "Condition at y = 0 and y = Ly (south and north).")]
    #[allow(missing_docs)]
    pub y: Boundary,
    #[cfg_attr(doc, doc = "Condition at z = 0 and z = Lz (ground and top).")]
    #[allow(missing_docs)]
    pub z: Boundary,
}
impl Boundaries {
    /// The condition of axis `a` (0 = x, 1 = y, 2 = z).
    ///
    /// # Panics
    ///
    /// If `a > 2`.
    pub fn at(&self, a: usize) -> Boundary {
        [self.x, self.y, self.z][a]
    }
}
/// Flow model, selected by `kind`: `prescribed_uniform` (a fixed uniform
/// velocity; only the constituents are solved), `laminar` (incompressible
/// flow with constant viscosity) or `neutral_rans` (a neutral atmospheric
/// boundary layer with the k-epsilon model). The numeric fields are checked
/// as CFD-E012.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Flow {
    /// A fixed uniform velocity with no flow solve. Its normal component must
    /// be zero at impermeable boundaries (CFD-E024), and solid boxes are not
    /// allowed (CFD-E013).
    PrescribedUniform {
        /// Velocity [u, v, w] in m/s, finite.
        velocity_m_s: [f64; 3],
    },
    /// Incompressible laminar flow with constant kinematic viscosity, started
    /// from a uniform velocity.
    Laminar {
        /// Uniform initial velocity [u, v, w] in m/s, finite. It also sets
        /// the velocity of `open` inlet faces and decides which open faces are
        /// inlets or outlets.
        initial_velocity_m_s: [f64; 3],
        /// Kinematic viscosity in m2/s, finite and positive.
        viscosity_m2_s: f64,
        /// Uniform body acceleration [x, y, z] in m/s2, for example the
        /// pressure-gradient drive of a periodic channel. Default: zero.
        #[serde(default)]
        acceleration_m_s2: [f64; 3],
    },
    /// Neutral atmospheric boundary layer with the k-epsilon model (C_mu 0.09,
    /// C_epsilon1 1.44, C_epsilon2 1.92, kappa 0.4). Inflow and initial fields
    /// are the logarithmic equilibrium profiles U = u*/kappa ln((z + z0)/z0),
    /// k = u*^2/sqrt(C_mu) and epsilon = u*^3/(kappa (z + z0)), with u* set by
    /// `speed_m_s` at `reference_height_m`. Requires `boundaries.z =
    /// ground_open_top` (CFD-E011).
    NeutralRans {
        /// Mean wind speed at `reference_height_m` in m/s, finite and
        /// positive. It is also the velocity scale of the normalized checks.
        speed_m_s: f64,
        /// Height of `speed_m_s` above the ground in meters, positive and not
        /// above the domain top. It is also the length scale of the
        /// normalized checks.
        reference_height_m: f64,
        /// Aerodynamic roughness length z0 of the ground in meters, positive.
        /// Building and other wall faces use min(z0, 0.001 cell width), which
        /// ties that wall roughness to the grid (see the flow model card).
        roughness_m: f64,
        /// Meteorological wind direction in degrees: where the wind blows
        /// from, clockwise from north. 270 blows toward +x (east), 180 toward
        /// +y (north). Finite; any value is accepted.
        direction_deg_from: f64,
        /// Molecular kinematic viscosity in m2/s, positive; the eddy viscosity
        /// C_mu k^2/epsilon is added to it.
        viscosity_m2_s: f64,
        /// Turbulent Schmidt number Sc_t, positive: constituents diffuse with
        /// their molecular diffusivity plus nu_t / Sc_t.
        turbulent_schmidt: f64,
    },
}
impl Flow {
    /// True for `prescribed_uniform`, whose velocity stays fixed: no
    /// momentum or pressure equations are solved.
    pub fn prescribed(&self) -> bool {
        matches!(self, Self::PrescribedUniform { .. })
    }
    /// True for `neutral_rans`, which adds the k and epsilon fields.
    pub fn rans(&self) -> bool {
        matches!(self, Self::NeutralRans { .. })
    }
    /// Molecular kinematic viscosity in m2/s; zero for prescribed flow.
    pub fn viscosity(&self) -> f64 {
        match self {
            Self::PrescribedUniform { .. } => 0.0,
            Self::Laminar { viscosity_m2_s, .. } | Self::NeutralRans { viscosity_m2_s, .. } => {
                *viscosity_m2_s
            }
        }
    }
    /// The case's reference velocity at height `z` (meters), in m/s: the
    /// prescribed velocity, the laminar initial velocity, or the logarithmic
    /// `neutral_rans` profile in the wind direction (negative heights are
    /// evaluated at the ground).
    ///
    /// It initializes the face velocities, sets the velocity of `open` inlet
    /// faces and decides whether an open face is an inlet, an outlet or a
    /// slip face.
    ///
    /// ```
    /// use cfd_craft::case::Flow;
    ///
    /// let flow: Flow = serde_json::from_str(
    ///     r#"{"kind": "neutral_rans", "speed_m_s": 3.0, "reference_height_m": 10.0,
    ///         "roughness_m": 0.1, "direction_deg_from": 270.0,
    ///         "viscosity_m2_s": 1.5e-5, "turbulent_schmidt": 0.7}"#,
    /// )
    /// .unwrap();
    /// let [u, v, w] = flow.vector(10.0);
    /// // A wind from the west blows toward +x, at the declared reference speed.
    /// assert!((u - 3.0).abs() < 1e-12 && v.abs() < 1e-12 && w == 0.0);
    /// assert!(flow.vector(1.0)[0] < u);
    /// ```
    pub fn vector(&self, z: f64) -> [f64; 3] {
        match self {
            Self::PrescribedUniform { velocity_m_s } => *velocity_m_s,
            Self::Laminar {
                initial_velocity_m_s,
                ..
            } => *initial_velocity_m_s,
            Self::NeutralRans {
                speed_m_s,
                reference_height_m,
                roughness_m,
                direction_deg_from,
                ..
            } => {
                let u = speed_m_s * ((z.max(0.0) + roughness_m) / roughness_m).ln()
                    / ((reference_height_m + roughness_m) / roughness_m).ln();
                let t = direction_deg_from.to_radians();
                [-u * t.sin(), -u * t.cos(), 0.0]
            }
        }
    }
    /// Velocity scale of the normalized checks and divergence limits, in m/s:
    /// `speed_m_s` for `neutral_rans`, otherwise the magnitude of
    /// [`vector`](Self::vector) at z = 1 m, but at least 1 m/s.
    pub fn reference_speed(&self) -> f64 {
        match self {
            Self::NeutralRans { speed_m_s, .. } => *speed_m_s,
            _ => self
                .vector(1.0)
                .iter()
                .map(|v| v * v)
                .sum::<f64>()
                .sqrt()
                .max(1.0),
        }
    }
    /// Friction velocity of the `neutral_rans` inflow profile in m/s,
    /// u* = kappa speed / ln((z_ref + z0)/z0) with kappa = 0.4; zero for the
    /// other flow models.
    pub fn ustar(&self) -> f64 {
        match self {
            Self::NeutralRans {
                speed_m_s,
                reference_height_m,
                roughness_m,
                ..
            } => 0.4 * speed_m_s / ((reference_height_m + roughness_m) / roughness_m).ln(),
            _ => 0.0,
        }
    }
    /// Equilibrium `(k, epsilon)` of the `neutral_rans` inflow at height `z`
    /// (meters): k = u*^2/sqrt(C_mu) in m2/s2 and epsilon = u*^3/(kappa (z +
    /// z0)) in m2/s3. `(0, 0)` for the other flow models. These values
    /// initialize the fields and set the inlet and top values.
    pub fn turbulence(&self, z: f64) -> (f64, f64) {
        if let Self::NeutralRans { roughness_m, .. } = self {
            let u = self.ustar();
            (u * u / 0.3, u.powi(3) / (0.4 * (z + roughness_m)))
        } else {
            (0.0, 0.0)
        }
    }
}
/// An axis-aligned solid box. Cells whose centers lie inside it (lower bound
/// inclusive, upper bound exclusive) are solid; boxes may overlap. The bounds
/// must increase on every axis and lie inside the domain (CFD-E014), and the
/// box must contain at least one cell center (CFD-E028). Fewer than ten cells
/// across its height draws warning CFD-W001.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Block {
    /// Unique nonempty ID (CFD-E014); the key of the box's cell count in
    /// geometry.json.
    pub id: String,
    /// Lower corner [x, y, z] in meters.
    pub min_m: [f64; 3],
    /// Upper corner [x, y, z] in meters.
    pub max_m: [f64; 3],
    /// Optional simple footprint polygon in local x/y meters. With this field
    /// the shape is a prism between `min_m[2]` and `max_m[2]`; the other bounds
    /// must equal the footprint bounds. Holes and self-intersections are
    /// rejected (CFD-E084). Omission selects the original box.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub footprint_m: Option<Vec<[f64; 2]>>,
}
#[cfg(test)]
thread_local! {
    /// Calls of [`Block::contains`] on this thread, for tests that bound the
    /// work of building masks.
    pub(crate) static CONTAINS_CALLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
impl Block {
    /// Cell-center containment, with lower bounds inclusive and upper exclusive.
    pub fn contains(&self, p: [f64; 3]) -> bool {
        #[cfg(test)]
        CONTAINS_CALLS.with(|c| c.set(c.get() + 1));
        (0..3).all(|a| p[a] >= self.min_m[a] && p[a] < self.max_m[a])
            && self
                .footprint_m
                .as_ref()
                .is_none_or(|v| crate::geometry::contains(v, [p[0], p[1]]))
    }
    /// Exact geometric volume of this box or polygon prism in m3.
    pub fn volume(&self) -> f64 {
        let area = self.footprint_m.as_ref().map_or(
            (self.max_m[0] - self.min_m[0]) * (self.max_m[1] - self.min_m[1]),
            |p| crate::geometry::area(p),
        );
        area * (self.max_m[2] - self.min_m[2])
    }
}
/// A tangential velocity of one `wall` face of the domain, such as a cavity
/// lid. Laminar flow only, at most one per face, and the velocity component
/// normal to the wall must be zero (CFD-E021).
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct MovingWall {
    /// Axis normal to the wall: 0 (x), 1 (y) or 2 (z). That axis must have
    /// `wall` boundaries.
    pub axis: usize,
    /// True for the face at the upper end of the axis (for example z = Lz),
    /// false for the face at 0.
    pub upper: bool,
    /// Wall velocity [u, v, w] in m/s, finite; the component along `axis`
    /// must be zero.
    pub velocity_m_s: [f64; 3],
}
/// A passive gas: transported and diffused by the flow without affecting it.
/// Concentrations are in kg/m3.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Constituent {
    /// Unique nonempty ID (CFD-E015), referenced by sources and initial
    /// conditions and used in the result.
    pub id: String,
    /// Molecular diffusivity in m2/s, finite and nonnegative (CFD-E015).
    /// With `neutral_rans` the turbulent diffusivity nu_t/Sc_t is added.
    pub diffusivity_m2_s: f64,
    /// Optional spherical aerosol bin. Omitted for gases, preserving old hashes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub particle: Option<ParticleBin>,
}
/// Dilute spherical particle bin, with no feedback, chemistry or resuspension.
/// The declared deposition velocities exclude gravitational settling, which is
/// added separately on upward-facing ground and roofs.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ParticleBin {
    /// Geometric diameter in meters (1e-8..1e-4).
    pub diameter_m: f64,
    /// Particle material density in kg/m3, above the reference air density.
    pub density_kg_m3: f64,
    /// Direct nonnegative settling speed, bypassing the Stokes validity gate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub settling_velocity_m_s: Option<f64>,
    /// Additional ground deposition speed, in m/s, excluding settling.
    #[serde(default)]
    pub ground_deposition_velocity_m_s: f64,
    /// Additional roof deposition speed, in m/s, excluding settling.
    #[serde(default)]
    pub roof_deposition_velocity_m_s: f64,
}
impl ParticleBin {
    /// Cunningham-corrected spherical terminal speed at the declared reference
    /// atmosphere (293.15 K, 101325 Pa): rho_air=1.204 kg/m3,
    /// mu=1.81e-5 Pa s, lambda=6.6e-8 m, g=9.80665 m/s2.
    pub fn settling_speed(&self) -> f64 {
        self.settling_velocity_m_s.unwrap_or_else(|| {
            (self.density_kg_m3 - 1.204)
                * 9.80665
                * self.diameter_m.powi(2)
                * Self::slip(self.diameter_m)
                / (18.0 * 1.81e-5)
        })
    }
    /// Particle Reynolds number under the reference atmosphere.
    pub fn reynolds_number(&self) -> f64 {
        1.204 * self.settling_speed() * self.diameter_m / 1.81e-5
    }
    /// Equivalent unit-density spherical aerodynamic diameter, including slip.
    /// PM sums use this diameter, not the geometric diameter.
    pub fn aerodynamic_diameter_m(&self) -> f64 {
        if self.density_kg_m3 == 1000.0 {
            return self.diameter_m;
        }
        let target = (self.density_kg_m3 - 1.204) / (1000.0 - 1.204)
            * self.diameter_m.powi(2)
            * Self::slip(self.diameter_m);
        let (mut lo, mut hi) = (0.0, self.diameter_m * self.density_kg_m3.sqrt().max(1.0));
        for _ in 0..80 {
            let mid = 0.5 * (lo + hi);
            if mid * mid * Self::slip(mid) < target {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        0.5 * (lo + hi)
    }
    fn slip(d: f64) -> f64 {
        let kn = 2.0 * 6.6e-8 / d;
        1.0 + kn * (1.257 + 0.4 * (-1.1 / kn).exp())
    }
}
/// Published canopy source-coefficient set; the ambient k-epsilon constants
/// are unchanged. Both alternatives are unvalidated in this urban solver.
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CanopyClosure {
    /// Green (1992): beta_p=1, beta_d=4, C_e4=C_e5=1.5.
    #[default]
    Green1992,
    /// Sanz/Katul: beta_p=1, beta_d=5.1, C_e4=C_e5=0.9.
    Sanz2003,
}
impl CanopyClosure {
    /// (beta_p, beta_d, C_e4, C_e5), in the published source-term convention.
    pub fn coefficients(self) -> [f64; 4] {
        match self {
            Self::Green1992 => [1.0, 4.0, 1.5, 1.5],
            Self::Sanz2003 => [1.0, 5.1, 0.9, 0.9],
        }
    }
}
/// Flat-ground porous vegetation volume. Box membership uses cell centers,
/// lower-inclusive and upper-exclusive, exactly as building masks.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Vegetation {
    /// Unique nonempty volume ID.
    pub id: String,
    /// Box lower corner in meters.
    pub min_m: [f64; 3],
    /// Box upper corner in meters.
    pub max_m: [f64; 3],
    /// Leaf area per air volume, in m2/m3 (inverse meters).
    pub leaf_area_density_m2_m3: f64,
    /// Dimensionless form-drag coefficient.
    pub drag_coefficient: f64,
    /// Particle collection velocity per leaf area, m/s; excludes settling.
    #[serde(default)]
    pub leaf_deposition_velocity_m_s: f64,
    /// Canopy k-epsilon source coefficient set.
    #[serde(default)]
    pub turbulence_closure: CanopyClosure,
}
/// A continuous emission into one constituent, selected by `kind`. Rates are
/// finite and nonnegative (CFD-E016); the support is nondegenerate and inside
/// the domain (CFD-E017). The integrated rate is distributed over the cells
/// the support covers. Support touching a solid cell is rejected (CFD-E040),
/// as is a steady source in a fluid region without an outlet (CFD-E043, and
/// CFD-E020 when no boundary is open).
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Source {
    /// A point emission, deposited in the cell that contains the point.
    Point {
        /// Unique nonempty source ID (CFD-E016).
        id: String,
        /// ID of the emitted constituent (CFD-E016).
        constituent: String,
        /// Position [x, y, z] in meters, inside the domain (each coordinate
        /// at least 0 and below the domain length).
        point_m: [f64; 3],
        /// Emission rate in kg/s.
        rate_kg_s: f64,
    },
    /// A straight line emission. The integrated rate is `rate_kg_s_m` times
    /// the line length, distributed over the cells the segment crosses in
    /// proportion to its length in each.
    Line {
        /// Unique nonempty source ID (CFD-E016).
        id: String,
        /// ID of the emitted constituent (CFD-E016).
        constituent: String,
        /// Start point [x, y, z] in meters, inside the domain.
        from_m: [f64; 3],
        /// End point [x, y, z] in meters, inside the domain and different
        /// from `from_m`.
        to_m: [f64; 3],
        /// Emission rate per meter of line, kg/(s m).
        rate_kg_s_m: f64,
    },
    /// A uniform emission over a box. The integrated rate is `rate_kg_s_m3`
    /// times the box volume, distributed over the cells by their exact overlap
    /// with the box.
    Volume {
        /// Unique nonempty source ID (CFD-E016).
        id: String,
        /// ID of the emitted constituent (CFD-E016).
        constituent: String,
        /// Lower corner [x, y, z] in meters.
        min_m: [f64; 3],
        /// Upper corner [x, y, z] in meters; every bound above `min_m` and
        /// inside the domain.
        max_m: [f64; 3],
        /// Emission rate per cubic meter, kg/(s m3).
        rate_kg_s_m3: f64,
    },
}
impl Source {
    /// The source ID.
    pub fn id(&self) -> &str {
        match self {
            Self::Point { id, .. } | Self::Line { id, .. } | Self::Volume { id, .. } => id,
        }
    }
    /// ID of the constituent the source emits.
    pub fn constituent(&self) -> &str {
        match self {
            Self::Point { constituent, .. }
            | Self::Line { constituent, .. }
            | Self::Volume { constituent, .. } => constituent,
        }
    }
    /// The rate field as declared, with its name (per second, per meter or per cubic meter).
    pub fn declared_rate(&self) -> (&'static str, f64) {
        match self {
            Self::Point { rate_kg_s, .. } => ("rate_kg_s", *rate_kg_s),
            Self::Line { rate_kg_s_m, .. } => ("rate_kg_s_m", *rate_kg_s_m),
            Self::Volume { rate_kg_s_m3, .. } => ("rate_kg_s_m3", *rate_kg_s_m3),
        }
    }
    /// Integrated emission rate in kg/s: `rate_kg_s`, `rate_kg_s_m` times the
    /// line length, or `rate_kg_s_m3` times the box volume. The represented
    /// source fields emit exactly this rate.
    ///
    /// ```
    /// use cfd_craft::case::Source;
    ///
    /// let line: Source = serde_json::from_str(
    ///     r#"{"kind": "line", "id": "road", "constituent": "tracer",
    ///         "from_m": [1.0, 1.0, 2.0], "to_m": [1.0, 6.0, 2.0], "rate_kg_s_m": 2e-8}"#,
    /// )
    /// .unwrap();
    /// // 2e-8 kg/s per meter over 5 m.
    /// assert!((line.rate() - 1e-7).abs() < 1e-20);
    /// assert_eq!(line.declared_rate(), ("rate_kg_s_m", 2e-8));
    /// ```
    pub fn rate(&self) -> f64 {
        match self {
            Self::Point { rate_kg_s, .. } => *rate_kg_s,
            Self::Line {
                rate_kg_s_m,
                from_m,
                to_m,
                ..
            } => {
                rate_kg_s_m
                    * (0..3)
                        .map(|a| (to_m[a] - from_m[a]).powi(2))
                        .sum::<f64>()
                        .sqrt()
            }
            Self::Volume {
                rate_kg_s_m3,
                min_m,
                max_m,
                ..
            } => rate_kg_s_m3 * (0..3).map(|a| max_m[a] - min_m[a]).product::<f64>(),
        }
    }
}
/// Initial concentration of one constituent, selected by `kind`. Each
/// constituent has at most one (CFD-E018); without one it starts at zero.
/// Solid cells always hold zero.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Initial {
    /// The same concentration in every fluid cell.
    Uniform {
        /// ID of the initialized constituent.
        constituent: String,
        /// Concentration in kg/m3, finite and nonnegative (CFD-E025).
        concentration_kg_m3: f64,
    },
    /// A Gaussian puff, periodized over the domain, integrated per cell and
    /// scaled to exactly `total_mass_kg` of fluid mass. Needs periodic
    /// boundaries on all three axes (CFD-E026); used by the transport
    /// verification fixtures.
    PeriodicGaussian {
        /// ID of the initialized constituent.
        constituent: String,
        /// Center [x, y, z] in meters, inside the domain.
        center_m: [f64; 3],
        /// Standard deviations along x, y and z in meters, each positive and
        /// at most twice the domain length.
        sigma_m: [f64; 3],
        /// Mass of the puff in kg, positive.
        total_mass_kg: f64,
    },
}
impl Initial {
    /// ID of the constituent this condition initializes.
    pub fn constituent(&self) -> &str {
        match self {
            Self::Uniform { constituent, .. } | Self::PeriodicGaussian { constituent, .. } => {
                constituent
            }
        }
    }
}
/// A named concentration sample, selected by `kind` and reported in kg/m3 for
/// every constituent (`receptors_kg_m3` of the result, probes.csv). IDs are
/// unique and nonempty (CFD-E019); the support is nondegenerate and inside the
/// domain (CFD-E027) and may not sample a solid cell (CFD-E042).
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Receptor {
    /// Trilinear interpolation between the eight surrounding cell centers,
    /// clamped at the domain boundary.
    Point {
        /// Unique nonempty receptor ID (CFD-E019).
        id: String,
        /// Position [x, y, z] in meters, inside the domain.
        point_m: [f64; 3],
    },
    /// The average over a box, weighted by the exact overlap of each cell
    /// with the box. Not a point sample or a peak bound.
    VolumeAverage {
        /// Unique nonempty receptor ID (CFD-E019).
        id: String,
        /// Lower corner [x, y, z] in meters.
        min_m: [f64; 3],
        /// Upper corner [x, y, z] in meters; every bound above `min_m` and
        /// inside the domain.
        max_m: [f64; 3],
    },
    /// Polyline sampling at a constant height. Samples are equally spaced
    /// in arc length, including both endpoints, with spacing at most the
    /// declared maximum. The receptor mean is normalized trapezoidal quadrature.
    Path {
        /// Unique nonempty receptor ID (CFD-E019).
        id: String,
        /// Local metric x/y vertices; 2..4097, without repeated neighbors.
        polyline_m: Vec<[f64; 2]>,
        /// Constant sample height above flat ground, in meters.
        height_m: f64,
        /// Positive finite maximum arc-length spacing, in meters.
        sample_spacing_m: f64,
    },
}
/// Maximum generated sample points in one path receptor.
pub const PATH_SAMPLES_MAX: usize = 20001;
/// Maximum generated sample points across all path receptors in one case.
pub const PATH_SAMPLES_TOTAL_MAX: usize = 50000;
impl Receptor {
    /// Generated sample count for a path (None for other receptor kinds).
    /// Invalid/nonfinite lengths or spacings return usize::MAX, so preflight
    /// can reject them before allocation.
    pub fn path_sample_count(&self) -> Option<usize> {
        let Self::Path {
            polyline_m,
            sample_spacing_m,
            ..
        } = self
        else {
            return None;
        };
        let length = polyline_m
            .windows(2)
            .map(|p| (p[1][0] - p[0][0]).hypot(p[1][1] - p[0][1]))
            .sum::<f64>();
        if !length.is_finite()
            || length <= 0.0
            || !sample_spacing_m.is_finite()
            || *sample_spacing_m <= 0.0
        {
            return Some(usize::MAX);
        }
        Some(
            ((length / sample_spacing_m).ceil() as usize)
                .max(1)
                .saturating_add(1),
        )
    }
    /// The receptor ID.
    pub fn id(&self) -> &str {
        match self {
            Self::Point { id, .. } | Self::VolumeAverage { id, .. } | Self::Path { id, .. } => id,
        }
    }
}
/// Advection scheme of the constituents. `linear_upwind` is first-order
/// upwind; it is linear in the concentrations, so responses to sources
/// superpose exactly in a fixed flow. `muscl` adds a second-order slope with
/// the bounded monotonized-central limiter; it is nonlinear, so superposition
/// is not exact.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Advection {
    #[cfg_attr(
        doc,
        doc = "First-order upwind, linear in the concentrations: exact source superposition in a fixed flow."
    )]
    #[allow(missing_docs)]
    LinearUpwind,
    #[cfg_attr(
        doc,
        doc = "MUSCL with the monotonized-central limiter: second order in smooth regions and bounded, but nonlinear."
    )]
    #[allow(missing_docs)]
    Muscl,
}
/// Preconditioner of the pressure PCG solve. `ic0` (the default) is an
/// incomplete Cholesky factor built once per grid and works on every grid.
/// `multigrid` is a fixed symmetric multigrid cycle. On an unmasked grid it is
/// geometric, and strongly anisotropic spacings select line or plane
/// smoothing. With geometry it aggregates connected fluid cells and uses
/// point smoothing only, so strong anisotropy can make it slower than `ic0`.
/// Strong anisotropy draws warning CFD-W004 in both cases. Both
/// preconditioners solve the same equation to the same tolerance and
/// recomputed residual.
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PressurePreconditioner {
    #[cfg_attr(
        doc,
        doc = "Incomplete Cholesky IC(0) of a slightly shifted operator, built on the first pressure solve and cached in the grid (default)."
    )]
    #[allow(missing_docs)]
    #[default]
    Ic0,
    #[cfg_attr(
        doc,
        doc = "Fixed symmetric multigrid cycle: geometric on unmasked grids, connected aggregates with geometry. See the [pressure multigrid guide](https://github.com/dietmarwo/CFD-craft/blob/main/docs/developer/pressure-multigrid.md)."
    )]
    #[allow(missing_docs)]
    Multigrid,
}
impl PressurePreconditioner {
    fn is_default(&self) -> bool {
        *self == Self::Ic0
    }
}
/// Momentum advection on the staggered grid. Donor-cell (advective form) is the
/// default; the others are conservative flux forms with an upwind-biased
/// reconstruction of the transported component (van Leer's MUSCL in Sweby's notation).
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MomentumAdvection {
    /// First-order donor-cell upwind in advective form (default).
    #[default]
    DonorCell,
    /// Unlimited second-order upwind: face value 1.5 phi_U - 0.5 phi_UU. Not bounded.
    SecondOrderUpwind,
    /// Second-order upwind with the limiter in `numerics.momentum_limiter`.
    LimitedUpwind,
}
impl MomentumAdvection {
    fn is_default(&self) -> bool {
        *self == Self::DonorCell
    }
}
/// Steady pseudo-time algorithm. `explicit` is the SSPRK2 march of the earlier
/// release. The implicit options solve for a correction driven by the full
/// steady residual (delta form), so they share its fixed point.
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SteadyAlgorithm {
    /// SSPRK2 pseudo-time steps with a projection per stage, limited by
    /// advection and diffusion together (default).
    #[default]
    Explicit,
    /// SSPRK2 stages with an implicit viscous term: (I − Δτ L_ν) δ = Δτ R.
    ImplicitViscous,
    /// One implicit correction per outer iteration, A δ = R with a Picard
    /// first-order upwind and viscous Jacobian and a CFL ramp, followed by a
    /// projection at the pseudo-time step (a SIMPLEC-type coupling).
    ImplicitProjection,
}
impl SteadyAlgorithm {
    fn is_default(&self) -> bool {
        *self == Self::Explicit
    }
}
/// Limiter of `limited_upwind` momentum advection, in Sweby's notation
/// s(a, b) with a and b the upwind and downwind differences: `van_albada`
/// s = ab(a + b)/(a^2 + b^2), `van_leer` s = 2ab/(a + b), or
/// `monotonized_central` s = sign(b) min(2|a|, |a + b|/2, 2|b|). Each is zero
/// where a and b differ in sign.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MomentumLimiter {
    #[cfg_attr(doc, doc = "van Albada: s = ab(a + b)/(a² + b²).")]
    #[allow(missing_docs)]
    VanAlbada,
    #[cfg_attr(doc, doc = "van Leer: s = 2ab/(a + b).")]
    #[allow(missing_docs)]
    VanLeer,
    #[cfg_attr(
        doc,
        doc = "Monotonized central: s = sign(b) min(2|a|, |a + b|/2, 2|b|)."
    )]
    #[allow(missing_docs)]
    MonotonizedCentral,
}
/// Discretization choices, tolerances and the steady monitor window. The
/// optional selections `pressure_preconditioner`, `momentum_advection`,
/// `momentum_limiter` and `steady_algorithm` are left out of the normalized
/// case when they have their default, so cases written before they existed
/// keep their case and physics hashes.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Numerics {
    #[cfg_attr(doc, doc = "Advection scheme of the constituents; see [`Advection`].")]
    #[allow(missing_docs)]
    pub transport: Advection,
    /// Upper bound of the time step in seconds, finite and positive
    /// (CFD-E009). The step is the smaller of this and the stability limit;
    /// in transient mode the last step is clipped to end at `run.end_time_s`.
    pub max_time_step_s: f64,
    /// Stability factor of explicit steps, 0 < CFL <= 0.45 (CFD-E010): with
    /// the explicit algorithm and for constituent sub-steps, dt = CFL / (sum
    /// of max|u|/dx + 2 D sum of 1/dx^2), D the largest viscosity or
    /// diffusivity. `implicit_viscous` applies it to advection only;
    /// `implicit_projection` uses its own CFL ramp for the flow.
    pub advective_cfl_max: f64,
    /// Relative residual target of each pressure solve, finite and positive
    /// (CFD-E009). A solve is accepted when the recomputed residual is within
    /// 10 times this plus 1e-12. A loose value changes the path to a steady
    /// state, not the steady checks.
    pub pressure_relative_tolerance: f64,
    /// PCG iteration limit of each pressure solve, nonzero (CFD-E008).
    /// Reaching it fails the step with CFD-E033.
    pub pressure_max_iterations: usize,
    /// Preconditioner of the pressure solve: `ic0` (default) or `multigrid`.
    /// Omitted from the normalized case when `ic0`, which keeps existing case
    /// and checkpoint hashes.
    #[serde(default, skip_serializing_if = "PressurePreconditioner::is_default")]
    pub pressure_preconditioner: PressurePreconditioner,
    /// Momentum advection: `donor_cell` (default), `second_order_upwind` or
    /// `limited_upwind`. Omitted from the normalized case when `donor_cell`,
    /// which keeps existing case and checkpoint hashes.
    #[serde(default, skip_serializing_if = "MomentumAdvection::is_default")]
    pub momentum_advection: MomentumAdvection,
    /// Limiter of `limited_upwind`: required with it and rejected otherwise
    /// (CFD-E037). Omitted when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub momentum_limiter: Option<MomentumLimiter>,
    /// Pseudo-time algorithm of steady runs: `explicit` (default),
    /// `implicit_viscous` (laminar only) or `implicit_projection` (laminar or
    /// `neutral_rans`). The implicit options need `run.mode = steady`
    /// (CFD-E045) and a solved flow (CFD-E047). Omitted from the normalized
    /// case when `explicit`, which keeps existing hashes.
    #[serde(default, skip_serializing_if = "SteadyAlgorithm::is_default")]
    pub steady_algorithm: SteadyAlgorithm,
    /// Threshold of the normalized steady equation residuals (momentum,
    /// turbulence and each constituent), finite and positive (CFD-E009).
    /// Steady mode only.
    pub steady_residual_tolerance: f64,
    /// Threshold of `monitor_stability`, the largest relative change between
    /// the means of three successive monitor windows; finite and positive
    /// (CFD-E009). Steady mode only.
    pub steady_monitor_tolerance: f64,
    /// Steps per monitor window, 2..100000 (CFD-E008). A steady run assesses
    /// its checks every `monitor_window` steps and needs three full windows
    /// before `monitor_stability` can pass.
    pub monitor_window: usize,
}
/// Run mode. `steady` iterates in pseudo-time until every steady check passes;
/// `run.end_time_s` does not bound it. `transient` integrates the physical
/// interval up to `run.end_time_s`.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Mode {
    #[cfg_attr(
        doc,
        doc = "Pseudo-time iteration until every steady check passes; success is `completed` / `steady_discrete_solution`."
    )]
    #[allow(missing_docs)]
    Steady,
    #[cfg_attr(
        doc,
        doc = "Time-accurate integration up to `run.end_time_s`; success is `completed` / `transient_interval_completed`."
    )]
    #[allow(missing_docs)]
    Transient,
}
/// Mode, endpoint and execution budgets. The whole group is excluded from the
/// physics hash, so budgets can change between a run and its restart.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Run {
    #[cfg_attr(doc, doc = "Steady or transient; see [`Mode`].")]
    #[allow(missing_docs)]
    pub mode: Mode,
    /// Physical end time of a transient run in seconds: finite, positive and
    /// at most 1e12 (CFD-E009, CFD-E022). It does not bound a steady run.
    pub end_time_s: f64,
    /// Maximum number of retained steps (outer iterations with an implicit
    /// steady algorithm), counted from the cold start including the steps of
    /// a restart checkpoint; nonzero (CFD-E008). Reaching it ends the run as
    /// `iteration_limit`.
    pub max_steps: usize,
    /// Wall-time allowance in seconds: finite, positive and at most seven
    /// days (CFD-E009, CFD-E022). The CLI and the Python binding enforce it;
    /// a Rust caller passes it, or its own limit, to `Control::new`.
    pub wall_time_limit_s: f64,
    /// Worker threads, 1..64 (CFD-E008). The CLI and the Python binding solve
    /// on a pool of this size; the Rust library does not create one.
    pub threads: usize,
    /// Memory budget in MiB for the preflight estimate (CFD-E006); a restart
    /// checkpoint may be at most half of it (CFD-E065). An estimate check, not
    /// an enforced process limit.
    pub memory_limit_mib: usize,
}
/// Which optional artifacts a run archive keeps, and its output budget.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Output {
    /// Write fields.json, the full final state including the native face
    /// velocities.
    pub fields: bool,
    /// Write checkpoint.json, a checksummed restart state.
    pub checkpoint: bool,
    /// Budget for all artifacts in MiB. The preflight estimate must fit
    /// (CFD-E007); at publication, fields and checkpoint are omitted and
    /// listed in the manifest when they would exceed it (CFD-E061).
    pub max_total_mib: usize,
}
/// One finding of a check: a stable code, its severity, the path of the
/// offending field and a message.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Diagnostic {
    /// Stable code such as `CFD-E010` (error) or `CFD-W001` (warning), listed
    /// in docs/diagnostics.md.
    pub code: String,
    /// `error`, which makes the case invalid, or `warning`.
    pub severity: String,
    /// Path of the offending field, for example `numerics.advective_cfl_max`
    /// or `sources[0].rate_kg_s`; empty for a document-level parse error.
    pub path: String,
    /// Explanation for people. Programs should match `code` and `path`, not
    /// this text.
    pub message: String,
}
/// Result of a case check (schema `cfd-craft.check/0.1`), as `cfd-craft
/// check` prints it: validity, the size and resource estimates, and every
/// diagnostic.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct CheckReport {
    /// Always `cfd-craft.check/0.1`.
    pub schema_version: String,
    /// True when no diagnostic has severity `error`.
    pub valid: bool,
    /// Total cell count (saturating at `usize::MAX`); 0 in the report of an
    /// unparsable case.
    pub cells: usize,
    /// Conservative estimate of the solver's peak memory in MiB, checked
    /// against `run.memory_limit_mib`; not measured process memory.
    pub estimated_peak_mib: f64,
    /// Estimated size of all archive artifacts in MiB, checked against
    /// `output.max_total_mib`.
    pub estimated_output_mib: f64,
    /// Every error and warning, in check order.
    pub diagnostics: Vec<Diagnostic>,
}
impl Case {
    /// SHA-256 (lowercase hex) of the case's compact JSON without `name`,
    /// `run` and `output`: the identity of the physical and numerical
    /// problem. A checkpoint restarts only a case with the same physics hash
    /// (CFD-E060), so budgets, the endpoint and the output policy may change
    /// between a run and its restart, while grid, physics, sources, receptors
    /// and numerics may not.
    ///
    /// ```
    /// # let json = r#"{"schema_version": "cfd-craft.case/0.1", "name": "tiny",
    /// #   "domain": {"size_m": [4.0, 1.0, 1.0]}, "grid": {"cells": [8, 2, 2]},
    /// #   "boundaries": {"x": "open", "y": "periodic", "z": "periodic"},
    /// #   "flow": {"kind": "prescribed_uniform", "velocity_m_s": [1.0, 0.0, 0.0]},
    /// #   "constituents": [{"id": "tracer", "diffusivity_m2_s": 0.01}],
    /// #   "sources": [{"kind": "point", "id": "S0", "constituent": "tracer",
    /// #                "point_m": [0.5, 0.5, 0.5], "rate_kg_s": 1e-6}],
    /// #   "numerics": {"transport": "linear_upwind", "max_time_step_s": 1.0,
    /// #     "advective_cfl_max": 0.4, "pressure_relative_tolerance": 1e-10,
    /// #     "pressure_max_iterations": 100, "steady_residual_tolerance": 1e-5,
    /// #     "steady_monitor_tolerance": 1e-3, "monitor_window": 10},
    /// #   "run": {"mode": "steady", "end_time_s": 1.0, "max_steps": 1000,
    /// #     "wall_time_limit_s": 30.0, "threads": 1, "memory_limit_mib": 64},
    /// #   "output": {"fields": false, "checkpoint": false, "max_total_mib": 4}}"#;
    /// use cfd_craft::case::{Case, Source};
    ///
    /// let case: Case = serde_json::from_str(json).unwrap();
    /// let mut longer = case.clone();
    /// longer.name = "tiny, longer budget".into();
    /// longer.run.max_steps *= 10;
    /// assert_eq!(case.physics_hash(), longer.physics_hash());
    ///
    /// let mut weaker = case.clone();
    /// if let Source::Point { rate_kg_s, .. } = &mut weaker.sources[0] {
    ///     *rate_kg_s *= 0.5;
    /// }
    /// assert_ne!(case.physics_hash(), weaker.physics_hash());
    /// ```
    pub fn physics_hash(&self) -> String {
        let mut v = serde_json::to_value(self).expect("serializable case");
        for k in ["run", "output", "name"] {
            v.as_object_mut().unwrap().remove(k);
        }
        hash_bytes(&serde_json::to_vec(&v).unwrap())
    }
    /// The semantic checks that need no grid: schema version, ranges and
    /// units, budgets, physical combinations, IDs, and that geometry, sources,
    /// receptors and initial conditions lie inside the domain. It also
    /// estimates peak memory and output size and checks them against
    /// `run.memory_limit_mib` (CFD-E006) and `output.max_total_mib`
    /// (CFD-E007).
    ///
    /// Every problem is reported at once; the report is invalid when any
    /// diagnostic is an error. It never fails or allocates the grid. The
    /// checks that need the built grid are in
    /// [`input::full_check`](crate::input::full_check), which the CLI and
    /// the Python binding run, and are repeated by
    /// [`solver::run`](crate::solver::run).
    ///
    /// ```
    /// # let json = r#"{"schema_version": "cfd-craft.case/0.1", "name": "tiny",
    /// #   "domain": {"size_m": [4.0, 1.0, 1.0]}, "grid": {"cells": [8, 2, 2]},
    /// #   "boundaries": {"x": "open", "y": "periodic", "z": "periodic"},
    /// #   "flow": {"kind": "prescribed_uniform", "velocity_m_s": [1.0, 0.0, 0.0]},
    /// #   "numerics": {"transport": "linear_upwind", "max_time_step_s": 1.0,
    /// #     "advective_cfl_max": 0.4, "pressure_relative_tolerance": 1e-10,
    /// #     "pressure_max_iterations": 100, "steady_residual_tolerance": 1e-5,
    /// #     "steady_monitor_tolerance": 1e-3, "monitor_window": 10},
    /// #   "run": {"mode": "steady", "end_time_s": 1.0, "max_steps": 1000,
    /// #     "wall_time_limit_s": 30.0, "threads": 1, "memory_limit_mib": 64},
    /// #   "output": {"fields": false, "checkpoint": false, "max_total_mib": 4}}"#;
    /// use cfd_craft::case::Case;
    ///
    /// let mut case: Case = serde_json::from_str(json).unwrap();
    /// case.numerics.advective_cfl_max = 0.9;
    /// case.run.threads = 0;
    /// let report = case.check();
    /// assert!(!report.valid);
    /// let found: Vec<_> = report
    ///     .diagnostics
    ///     .iter()
    ///     .map(|d| (d.code.as_str(), d.path.as_str()))
    ///     .collect();
    /// assert!(found.contains(&("CFD-E010", "numerics.advective_cfl_max")));
    /// assert!(found.contains(&("CFD-E008", "run.threads")));
    /// ```
    pub fn check(&self) -> CheckReport {
        let cells = self
            .grid
            .cells
            .iter()
            .try_fold(1usize, |x, n| x.checked_mul(*n))
            .unwrap_or(usize::MAX);
        let species = self.constituents.len() as f64;
        // Volume receptors keep sparse (cell, weight) pairs; Vec growth can double them.
        let receptor_bytes: f64 = self
            .receptors
            .iter()
            .map(|r| match r {
                Receptor::Point { .. } => 8.0 * 16.0,
                Receptor::VolumeAverage { min_m, max_m, .. } => {
                    32.0 * self.covered_cells(min_m, max_m)
                }
                Receptor::Path { .. } => {
                    512.0 * r.path_sample_count().unwrap_or(0).min(PATH_SAMPLES_MAX + 1) as f64
                }
            })
            .sum();
        // One dense weight vector exists while each source is represented.
        let source_bytes = if self.sources.is_empty() {
            0.0
        } else {
            8.0 * cells as f64
        };
        // The state keeps one monitor row per step for the last three windows: the flow
        // norm, each constituent's mass and receptor values, and mean k and epsilon.
        let monitor_rows = self
            .numerics
            .monitor_window
            .saturating_mul(3)
            .min(self.run.max_steps) as f64;
        let monitor_values = monitor_rows * (3.0 + species * (1.0 + self.receptors.len() as f64));
        let monitor_bytes = 8.0 * monitor_values + 32.0 * monitor_rows;
        // Include contiguous pressure factor construction and a compact per-solve
        // operator allowance (112 bytes/cell), before the existing 25% margin.
        // Extra hierarchy, reusable cycle vectors and construction peak allowance.
        // This is a conservative preflight allowance, not measured process RSS.
        let multigrid_bytes =
            if self.numerics.pressure_preconditioner == PressurePreconditioner::Multigrid {
                let dx = std::array::from_fn(|a| self.domain.size_m[a] / self.grid.cells[a] as f64);
                1024.0
                    + if crate::multigrid::line_axes(self.grid.cells, dx).is_empty() {
                        0.0
                    } else {
                        512.0
                    }
            } else {
                0.0
            };
        // Canopy raster coefficients, cell speeds, staggered drag and four
        // turbulence source/sink arrays coexist with the ordinary workspaces.
        let canopy_bytes = if crate::vegetation::active(self) {
            256.0
        } else {
            0.0
        };
        let peak = ((cells as f64) * (880.0 + multigrid_bytes + canopy_bytes + 96.0 * species) * 1.25
            + receptor_bytes
            + source_bytes
            + monitor_bytes)
            / 1048576.0
            // Process baseline (measured near 19 MiB) and the worker threads' stacks.
            + 24.0
            + self.run.threads as f64;
        // JSON text of f64 values is at most about 24 bytes; fields hold six flow arrays
        // (three face-velocity sets, pressure, k, epsilon) plus one array per constituent,
        // and both fields and checkpoint carry the monitor history.
        let output_fields = (cells as f64) * (24.0 * (6.0 + species) + 4.0) + 26.0 * monitor_values;
        let output_checkpoint =
            (cells as f64) * (26.0 * (6.0 + species) + 4.0) + 27.0 * monitor_values;
        // case.json, summary.json, geometry.json and probes.csv list every receptor, box
        // and source as pretty-printed text, receptor values once per constituent in
        // summary.json and probes.csv; identifiers count twice for JSON escaping.
        let ids: f64 = self.constituents.iter().map(|x| x.id.len() as f64).sum();
        let element_text: f64 = self
            .receptors
            .iter()
            .map(|r| {
                let id = r.id().len() as f64;
                400.0 + 2.0 * id + species * (100.0 + 4.0 * id) + 2.0 * ids
            })
            .chain(
                self.geometry
                    .iter()
                    .map(|b| 500.0 + 4.0 * b.id.len() as f64),
            )
            .chain(
                self.sources
                    .iter()
                    .map(|x| 600.0 + 2.0 * x.id().len() as f64),
            )
            .chain(
                self.vegetation
                    .iter()
                    .map(|v| 900.0 + 4.0 * v.id.len() as f64),
            )
            .chain(
                self.constituents
                    .iter()
                    .filter(|sp| sp.particle.is_some())
                    .map(|_| 700.0 + 200.0 * self.receptors.len() as f64),
            )
            .sum();
        let path_output_bytes = self
            .receptors
            .iter()
            .filter_map(Receptor::path_sample_count)
            .map(|n| n.min(PATH_SAMPLES_MAX + 1) as f64 * (160.0 + 64.0 * species))
            .sum::<f64>();
        let output = (element_text
            + path_output_bytes
            + if self.output.fields {
                output_fields
            } else {
                0.0
            }
            + if self.output.checkpoint {
                output_checkpoint
            } else {
                0.0
            })
            / 1048576.0
            + 2.0;
        let mut d = Vec::new();
        let mut add = |code: &str, severity: &str, path: &str, message: &str| {
            d.push(Diagnostic {
                code: code.into(),
                severity: severity.into(),
                path: path.into(),
                message: message.into(),
            })
        };
        if self.schema_version != "cfd-craft.case/0.1" {
            add(
                "CFD-E001",
                "error",
                "schema_version",
                "Use cfd-craft.case/0.1.",
            );
        }
        if self.name.is_empty() {
            add("CFD-E002", "error", "name", "Supply a nonempty case name.");
        }
        match (
            self.numerics.momentum_advection,
            self.numerics.momentum_limiter,
        ) {
            (MomentumAdvection::LimitedUpwind, None) => add(
                "CFD-E037",
                "error",
                "numerics.momentum_limiter",
                "limited_upwind needs a limiter: van_albada, van_leer or monotonized_central.",
            ),
            (MomentumAdvection::DonorCell | MomentumAdvection::SecondOrderUpwind, Some(_)) => add(
                "CFD-E037",
                "error",
                "numerics.momentum_limiter",
                "A momentum limiter is used only with momentum_advection = limited_upwind.",
            ),
            _ => {}
        }
        if self.numerics.steady_algorithm != SteadyAlgorithm::Explicit {
            let path = "numerics.steady_algorithm";
            if self.run.mode != Mode::Steady {
                add(
                    "CFD-E045",
                    "error",
                    path,
                    "Implicit pseudo-time changes the time accuracy; use it only with run.mode = steady.",
                );
            }
            if self.flow.prescribed()
                || (self.flow.rans()
                    && self.numerics.steady_algorithm == SteadyAlgorithm::ImplicitViscous)
            {
                add(
                    "CFD-E047",
                    "error",
                    path,
                    "Prescribed flow needs no steady algorithm, and implicit_viscous supports laminar flow only; use explicit, or implicit_projection for neutral_rans.",
                );
            }
        }
        if self.numerics.pressure_preconditioner == PressurePreconditioner::Multigrid {
            let dx = std::array::from_fn(|a| self.domain.size_m[a] / self.grid.cells[a] as f64);
            let axes = crate::multigrid::line_axes(self.grid.cells, dx);
            if !axes.is_empty() {
                add(
                    "CFD-W004",
                    "warning",
                    "numerics.pressure_preconditioner",
                    if self.geometry.is_empty() {
                        "Strong pressure anisotropy selects symmetric line-block smoothing with additional setup and memory; compare measured cost with ic0. Extreme anisotropy can still reach the pressure limit (CFD-E033)."
                    } else {
                        // Geometry always masks at least one cell (CFD-E028), which selects the aggregate path.
                        "Strong pressure anisotropy with geometry: the masked multigrid path uses point smoothing and has no line-block smoothing, so it can need more iterations and time than ic0; compare measured cost with ic0. Extreme anisotropy can still reach the pressure limit (CFD-E033)."
                    },
                );
            }
        }

        for a in 0..3 {
            if !(self.domain.size_m[a].is_finite()
                && self.domain.size_m[a] >= 1e-6
                && self.domain.size_m[a] <= 1e9)
            {
                add(
                    "CFD-E003",
                    "error",
                    &format!("domain.size_m[{a}]"),
                    "Supported lengths are 1e-6..1e9 m.",
                );
            }
            let minimum = if self.boundaries.at(a) == Boundary::Periodic {
                1
            } else {
                2
            };
            if self.grid.cells[a] < minimum || self.grid.cells[a] > 4096 {
                add(
                    "CFD-E004",
                    "error",
                    &format!("grid.cells[{a}]"),
                    "Each axis needs 2..4096 cells; a periodic axis may have one (a two-dimensional run).",
                );
            }
            if a != 2 && self.boundaries.at(a) == Boundary::GroundOpenTop {
                add(
                    "CFD-E005",
                    "error",
                    &format!("boundaries.{}", ["x", "y", "z"][a]),
                    "ground_open_top is supported on z only.",
                );
            }
        }
        if cells > 100_000_000 || peak > self.run.memory_limit_mib as f64 {
            add(
                "CFD-E006",
                "error",
                "run.memory_limit_mib",
                "Estimated memory exceeds the budget; reduce grid/constituents or raise an available budget.",
            );
        }
        if output > self.output.max_total_mib as f64 {
            add(
                "CFD-E007",
                "error",
                "output.max_total_mib",
                "Estimated artifacts exceed the output budget.",
            );
        }
        for (path, ok, message) in [
            (
                "run.threads",
                (1..=64).contains(&self.run.threads),
                "Use 1..64 threads.",
            ),
            (
                "run.max_steps",
                self.run.max_steps > 0,
                "Use a nonzero step budget.",
            ),
            (
                "numerics.pressure_max_iterations",
                self.numerics.pressure_max_iterations > 0,
                "Use a nonzero pressure iteration budget.",
            ),
            (
                "numerics.monitor_window",
                (2..=100000).contains(&self.numerics.monitor_window),
                "Use a monitor window of 2..100000 steps.",
            ),
        ] {
            if !ok {
                add("CFD-E008", "error", path, message);
            }
        }
        if self.run.wall_time_limit_s > 604800.0 {
            add(
                "CFD-E022",
                "error",
                "run.wall_time_limit_s",
                "The wall-time limit may not exceed seven days.",
            );
        }
        if self.run.end_time_s > 1e12 {
            add(
                "CFD-E022",
                "error",
                "run.end_time_s",
                "The physical horizon may not exceed 1e12 seconds.",
            );
        }
        for (path, x) in [
            ("run.end_time_s", self.run.end_time_s),
            ("run.wall_time_limit_s", self.run.wall_time_limit_s),
            ("numerics.max_time_step_s", self.numerics.max_time_step_s),
            (
                "numerics.pressure_relative_tolerance",
                self.numerics.pressure_relative_tolerance,
            ),
            (
                "numerics.steady_residual_tolerance",
                self.numerics.steady_residual_tolerance,
            ),
            (
                "numerics.steady_monitor_tolerance",
                self.numerics.steady_monitor_tolerance,
            ),
        ] {
            if !x.is_finite() || x <= 0.0 {
                add("CFD-E009", "error", path, "Must be finite and positive.");
            }
        }
        if !(self.numerics.advective_cfl_max > 0.0 && self.numerics.advective_cfl_max <= 0.45) {
            add(
                "CFD-E010",
                "error",
                "numerics.advective_cfl_max",
                "Use 0 < CFL <= 0.45; the combined advective and diffusive step allowance is capped at 0.45 for positivity.",
            );
        }
        if self.flow.rans() && self.boundaries.z != Boundary::GroundOpenTop {
            add(
                "CFD-E011",
                "error",
                "boundaries.z",
                "Neutral RANS requires ground_open_top.",
            );
        }
        if matches!(self.flow, Flow::Laminar { .. }) && self.boundaries.z == Boundary::GroundOpenTop
        {
            add(
                "CFD-E023",
                "error",
                "boundaries.z",
                "ground_open_top has an open top only with the neutral RANS stress closure; for laminar flow use wall, slip or periodic.",
            );
        }
        match &self.flow {
            Flow::NeutralRans {
                speed_m_s,
                reference_height_m,
                roughness_m,
                direction_deg_from,
                viscosity_m2_s,
                turbulent_schmidt,
            } => {
                for (path, x) in [
                    ("flow.speed_m_s", *speed_m_s),
                    ("flow.reference_height_m", *reference_height_m),
                    ("flow.roughness_m", *roughness_m),
                    ("flow.viscosity_m2_s", *viscosity_m2_s),
                    ("flow.turbulent_schmidt", *turbulent_schmidt),
                ] {
                    if !x.is_finite() || x <= 0.0 {
                        add("CFD-E012", "error", path, "Must be finite and positive.");
                    }
                }
                if !direction_deg_from.is_finite() {
                    add(
                        "CFD-E012",
                        "error",
                        "flow.direction_deg_from",
                        "Must be finite.",
                    );
                }
                if *reference_height_m > self.domain.size_m[2] {
                    add(
                        "CFD-E012",
                        "error",
                        "flow.reference_height_m",
                        "The reference height must lie inside the domain.",
                    );
                }
            }
            Flow::Laminar {
                initial_velocity_m_s,
                viscosity_m2_s,
                acceleration_m_s2,
            } => {
                if !viscosity_m2_s.is_finite()
                    || *viscosity_m2_s <= 0.0
                    || initial_velocity_m_s
                        .iter()
                        .chain(acceleration_m_s2)
                        .any(|x| !x.is_finite())
                {
                    add(
                        "CFD-E012",
                        "error",
                        "flow",
                        "Laminar viscosity must be positive; initial_velocity_m_s and acceleration_m_s2 must be finite.",
                    );
                }
            }
            Flow::PrescribedUniform { velocity_m_s } => {
                if velocity_m_s.iter().any(|x| !x.is_finite()) {
                    add(
                        "CFD-E012",
                        "error",
                        "flow.velocity_m_s",
                        "Velocity must be finite.",
                    );
                }
                if !self.geometry.is_empty() {
                    add(
                        "CFD-E013",
                        "error",
                        "geometry",
                        "Uniform prescribed flow supports no obstacles; use laminar or neutral_rans flow.",
                    );
                }
                for (a, &v) in velocity_m_s.iter().enumerate() {
                    if v != 0.0
                        && matches!(
                            self.boundaries.at(a),
                            Boundary::Wall | Boundary::Slip | Boundary::GroundOpenTop
                        )
                    {
                        add(
                            "CFD-E024",
                            "error",
                            &format!("flow.velocity_m_s[{a}]"),
                            "Prescribed velocity must have zero normal component at impermeable boundaries.",
                        );
                    }
                }
            }
        }
        let inside = |p: &[f64; 3]| {
            (0..3).all(|a| p[a].is_finite() && p[a] >= 0.0 && p[a] < self.domain.size_m[a])
        };
        let box_ok = |lo: &[f64; 3], hi: &[f64; 3]| {
            (0..3).all(|a| {
                lo[a].is_finite()
                    && hi[a].is_finite()
                    && lo[a] >= 0.0
                    && hi[a] <= self.domain.size_m[a]
                    && hi[a] > lo[a]
            })
        };
        let mut ids = BTreeSet::new();
        let mut wall_ids = BTreeSet::new();
        for (i, wall) in self.moving_walls.iter().enumerate() {
            if wall.axis > 2
                || !wall_ids.insert((wall.axis, wall.upper))
                || wall.velocity_m_s.iter().any(|v| !v.is_finite())
                || (wall.axis < 3
                    && (self.boundaries.at(wall.axis) != Boundary::Wall
                        || wall.velocity_m_s[wall.axis] != 0.0))
                || !matches!(self.flow, Flow::Laminar { .. })
            {
                add(
                    "CFD-E021",
                    "error",
                    &format!("moving_walls[{i}]"),
                    "Use unique tangential moving domain walls in laminar flow only.",
                );
            }
        }
        for (i, b) in self.geometry.iter().enumerate() {
            if b.id.is_empty() || !ids.insert(b.id.clone()) {
                add(
                    "CFD-E014",
                    "error",
                    &format!("geometry[{i}].id"),
                    "Boxes need unique nonempty IDs.",
                );
            }
            if !box_ok(&b.min_m, &b.max_m) {
                add(
                    "CFD-E014",
                    "error",
                    &format!("geometry[{i}]"),
                    "Box bounds must increase on every axis and lie inside the domain.",
                );
                continue;
            }
            if let Some(polygon) = &b.footprint_m
                && !crate::geometry::valid_polygon(polygon, &b.min_m, &b.max_m)
            {
                add(
                    "CFD-E084",
                    "error",
                    &format!("geometry[{i}].footprint_m"),
                    "Use a finite, simple, nonzero-area polygon with 3..2048 vertices and matching bounding box; holes and repeated closing vertices are not supported.",
                );
            }
            let cells_high = (b.max_m[2] - b.min_m[2])
                / (self.domain.size_m[2] / self.grid.cells[2].max(1) as f64);
            if cells_high < 10.0 {
                add(
                    "CFD-W001",
                    "warning",
                    &format!("geometry[{i}]"),
                    &format!(
                        "Box {} spans {cells_high:.1} vertical cells; fewer than 10 across a building height is too coarse for physical interpretation.",
                        b.id
                    ),
                );
            }
        }
        if let Some(z0) = self.building_roughness_m
            && (!z0.is_finite() || z0 <= 0.0 || !self.flow.rans())
        {
            add(
                "CFD-E085",
                "error",
                "building_roughness_m",
                "Building roughness must be finite and positive and requires neutral_rans.",
            );
        }
        if let Some(minimum) = self.geometry_min_passage_cells {
            if minimum == 0 {
                add(
                    "CFD-E084",
                    "error",
                    "geometry_min_passage_cells",
                    "Minimum passage resolution must be positive.",
                );
            } else if cells > 10_000_000 {
                // The scan is bounded; a skipped request must not read as satisfied.
                if !self.geometry.is_empty() {
                    add(
                        "CFD-W011",
                        "warning",
                        "geometry_min_passage_cells",
                        "The passage resolution was not evaluated: this check scans at most 10000000 cells. Read narrowest_fluid_passage_cells of each shape in geometry.json.",
                    );
                }
            } else if self.grid.cells.iter().all(|n| *n > 0)
                && let Some(width) = crate::geometry::minimum_passage(self)
                && width < minimum
            {
                add(
                    "CFD-W011",
                    "warning",
                    "geometry_min_passage_cells",
                    &format!(
                        "Narrowest represented horizontal passage is {width} cells; requested at least {minimum}."
                    ),
                );
            }
        }
        if self.flow.rans() {
            self.domain_warnings(&mut add);
        }
        let mut vegetation_ids = BTreeSet::new();
        for (i, v) in self.vegetation.iter().enumerate() {
            if v.id.is_empty() || !vegetation_ids.insert(v.id.clone()) {
                add(
                    "CFD-E082",
                    "error",
                    &format!("vegetation[{i}].id"),
                    "Use unique nonempty canopy IDs.",
                );
            }
            if !(0..3).all(|a| {
                v.min_m[a].is_finite()
                    && v.max_m[a].is_finite()
                    && v.min_m[a] >= 0.0
                    && v.max_m[a] <= self.domain.size_m[a]
                    && v.max_m[a] > v.min_m[a]
            }) {
                add(
                    "CFD-E082",
                    "error",
                    &format!("vegetation[{i}].min_m"),
                    "Canopy box must be finite, nondegenerate and inside the domain.",
                );
            }
            if self.flow.prescribed() && v.leaf_area_density_m2_m3 > 0.0 && v.drag_coefficient > 0.0
            {
                add(
                    "CFD-E083",
                    "error",
                    &format!("vegetation[{i}].drag_coefficient"),
                    "Prescribed flow cannot respond to canopy drag; set zero drag for a leaf-only study or solve laminar/RANS flow.",
                );
            }
            for (name, value, max) in [
                ("leaf_area_density_m2_m3", v.leaf_area_density_m2_m3, 100.0),
                ("drag_coefficient", v.drag_coefficient, 10.0),
                (
                    "leaf_deposition_velocity_m_s",
                    v.leaf_deposition_velocity_m_s,
                    10.0,
                ),
            ] {
                if !value.is_finite() || !(0.0..=max).contains(&value) {
                    add(
                        "CFD-E082",
                        "error",
                        &format!("vegetation[{i}].{name}"),
                        "Canopy parameters must be finite and within the documented nonnegative limits.",
                    );
                }
            }
        }
        let mut species = BTreeSet::new();
        for (i, s) in self.constituents.iter().enumerate() {
            if s.id.is_empty() || !species.insert(s.id.clone()) {
                add(
                    "CFD-E015",
                    "error",
                    &format!("constituents[{i}].id"),
                    "Use unique nonempty constituent IDs.",
                );
            }
            if !s.diffusivity_m2_s.is_finite() || s.diffusivity_m2_s < 0.0 {
                add(
                    "CFD-E015",
                    "error",
                    &format!("constituents[{i}].diffusivity_m2_s"),
                    "Use a finite nonnegative diffusivity.",
                );
            }
        }
        for (i, species) in self.constituents.iter().enumerate() {
            if let Some(p) = &species.particle {
                let prefix = format!("constituents[{i}].particle");
                if !p.diameter_m.is_finite() || !(1e-8..=1e-4).contains(&p.diameter_m) {
                    add(
                        "CFD-E080",
                        "error",
                        &format!("{prefix}.diameter_m"),
                        "Spherical bin diameter must be finite and in 1e-8..1e-4 m.",
                    );
                }
                if !p.density_kg_m3.is_finite()
                    || p.density_kg_m3 <= 1.204
                    || p.density_kg_m3 > 30000.0
                {
                    add(
                        "CFD-E080",
                        "error",
                        &format!("{prefix}.density_kg_m3"),
                        "Density must be finite, above reference air and at most 30000 kg/m3.",
                    );
                }
                for (name, value) in [
                    (
                        "settling_velocity_m_s",
                        p.settling_velocity_m_s.unwrap_or(0.0),
                    ),
                    (
                        "ground_deposition_velocity_m_s",
                        p.ground_deposition_velocity_m_s,
                    ),
                    (
                        "roof_deposition_velocity_m_s",
                        p.roof_deposition_velocity_m_s,
                    ),
                ] {
                    if !value.is_finite() || !(0.0..=10.0).contains(&value) {
                        add(
                            "CFD-E080",
                            "error",
                            &format!("{prefix}.{name}"),
                            "Use a finite speed in 0..10 m/s.",
                        );
                    }
                }
                if p.settling_velocity_m_s.is_none()
                    && (!p.reynolds_number().is_finite() || p.reynolds_number() > 0.1)
                {
                    add(
                        "CFD-E081",
                        "error",
                        &format!("{prefix}.settling_velocity_m_s"),
                        "Stokes-Cunningham requires particle Reynolds number <= 0.1; supply a separately justified direct settling speed.",
                    );
                }
            }
        }
        let mut source_ids = BTreeSet::new();
        for (i, s) in self.sources.iter().enumerate() {
            if s.id().is_empty() || !source_ids.insert(s.id().to_string()) {
                add(
                    "CFD-E016",
                    "error",
                    &format!("sources[{i}].id"),
                    "Use unique nonempty source IDs.",
                );
            }
            if !species.contains(s.constituent()) {
                add(
                    "CFD-E016",
                    "error",
                    &format!("sources[{i}].constituent"),
                    "The source must name a declared constituent.",
                );
            }
            let (rate_field, declared) = s.declared_rate();
            if !declared.is_finite() || declared < 0.0 {
                add(
                    "CFD-E016",
                    "error",
                    &format!("sources[{i}].{rate_field}"),
                    "Emission rates must be finite and nonnegative.",
                );
            }
            let ok = match s {
                Source::Point { point_m, .. } => inside(point_m),
                Source::Line { from_m, to_m, .. } => {
                    inside(from_m) && inside(to_m) && from_m != to_m
                }
                Source::Volume { min_m, max_m, .. } => box_ok(min_m, max_m),
            };
            if !ok {
                add(
                    "CFD-E017",
                    "error",
                    &format!("sources[{i}]"),
                    "Source support must be nondegenerate and inside the domain.",
                );
            }
        }
        let mut initial_species = BTreeSet::new();
        for (i, s) in self.initial_conditions.iter().enumerate() {
            if !species.contains(s.constituent())
                || !initial_species.insert(s.constituent().to_string())
            {
                add(
                    "CFD-E018",
                    "error",
                    &format!("initial_conditions[{i}].constituent"),
                    "One initial condition per declared constituent is supported.",
                );
            }
            match s {
                Initial::Uniform {
                    concentration_kg_m3,
                    ..
                } => {
                    if !concentration_kg_m3.is_finite() || *concentration_kg_m3 < 0.0 {
                        add(
                            "CFD-E025",
                            "error",
                            &format!("initial_conditions[{i}].concentration_kg_m3"),
                            "Initial concentration must be nonnegative and finite.",
                        );
                    }
                }
                Initial::PeriodicGaussian {
                    center_m,
                    sigma_m,
                    total_mass_kg,
                    ..
                } => {
                    if !inside(center_m)
                        || (0..3).any(|a| {
                            !sigma_m[a].is_finite()
                                || sigma_m[a] <= 0.0
                                || sigma_m[a] > 2.0 * self.domain.size_m[a]
                        })
                        || !total_mass_kg.is_finite()
                        || *total_mass_kg <= 0.0
                        || (0..3).any(|a| self.boundaries.at(a) != Boundary::Periodic)
                    {
                        add(
                            "CFD-E026",
                            "error",
                            &format!("initial_conditions[{i}]"),
                            "Periodic Gaussian needs three periodic axes, positive sigmas/mass and a center inside the domain.",
                        );
                    }
                }
            }
        }
        let mut receptor_ids = BTreeSet::new();
        let mut path_samples_total = 0usize;
        for (i, r) in self.receptors.iter().enumerate() {
            if r.id().is_empty() || !receptor_ids.insert(r.id().to_string()) {
                add(
                    "CFD-E019",
                    "error",
                    &format!("receptors[{i}].id"),
                    "Use unique, nonempty receptor IDs.",
                );
            }
            let ok = match r {
                Receptor::Point { point_m, .. } => inside(point_m),
                Receptor::VolumeAverage { min_m, max_m, .. } => box_ok(min_m, max_m),
                Receptor::Path {
                    polyline_m,
                    height_m,
                    sample_spacing_m,
                    ..
                } => {
                    if !(2..=4097).contains(&polyline_m.len())
                        || polyline_m.windows(2).any(|p| p[0] == p[1])
                        || polyline_m.iter().any(|p| {
                            !(0..2).all(|a| {
                                p[a].is_finite() && p[a] >= 0.0 && p[a] < self.domain.size_m[a]
                            })
                        })
                    {
                        add(
                            "CFD-E086",
                            "error",
                            &format!("receptors[{i}].polyline_m"),
                            "Path needs 2..4097 finite in-domain vertices and positive-length segments.",
                        );
                    }
                    if !height_m.is_finite()
                        || *height_m < 0.0
                        || *height_m >= self.domain.size_m[2]
                    {
                        add(
                            "CFD-E086",
                            "error",
                            &format!("receptors[{i}].height_m"),
                            "Path height must be finite and inside the vertical domain.",
                        );
                    }
                    if !sample_spacing_m.is_finite() || *sample_spacing_m <= 0.0 {
                        add(
                            "CFD-E086",
                            "error",
                            &format!("receptors[{i}].sample_spacing_m"),
                            "Use a finite positive maximum sample spacing.",
                        );
                    }
                    let count = r.path_sample_count().unwrap_or(usize::MAX);
                    path_samples_total = path_samples_total.saturating_add(count);
                    if count > PATH_SAMPLES_MAX {
                        add(
                            "CFD-E088",
                            "error",
                            &format!("receptors[{i}].sample_spacing_m"),
                            "A path may produce at most 20001 samples; increase spacing or split the study.",
                        );
                    }
                    true
                }
            };
            if !ok {
                add(
                    "CFD-E027",
                    "error",
                    &format!("receptors[{i}]"),
                    "Receptor support must be nondegenerate and inside the domain.",
                );
            }
        }
        if path_samples_total > PATH_SAMPLES_TOTAL_MAX {
            add(
                "CFD-E088",
                "error",
                "receptors",
                "A case may produce at most 50000 total path samples.",
            );
        }
        if self.run.mode == Mode::Steady
            && !self.sources.is_empty()
            && !(0..3).any(|a| self.boundaries.at(a) == Boundary::Open)
            && !self.sources.iter().all(|source| {
                self.constituents
                    .iter()
                    .find(|sp| sp.id == source.constituent())
                    .and_then(|sp| sp.particle.as_ref())
                    .is_some_and(|particle| {
                        // This is only a candidate screen. The source/grid
                        // preflight requires a represented positive sink in
                        // every source-bearing fluid component (CFD-E043).
                        ((matches!(self.boundaries.z, Boundary::Wall | Boundary::GroundOpenTop)
                            && particle.settling_speed() + particle.ground_deposition_velocity_m_s
                                > 0.0)
                            || (!self.geometry.is_empty()
                                && particle.settling_speed()
                                    + particle.roof_deposition_velocity_m_s
                                    > 0.0))
                            || self.vegetation.iter().any(|v| {
                                v.leaf_area_density_m2_m3 > 0.0
                                    && v.leaf_deposition_velocity_m_s > 0.0
                            })
                    })
            })
        {
            add(
                "CFD-E020",
                "error",
                "run.mode",
                "Steady sources need an open removal boundary, or for particles a represented deposition sink in their fluid component.",
            );
        }
        CheckReport {
            schema_version: "cfd-craft.check/0.1".into(),
            valid: !d.iter().any(|d| d.severity == "error"),
            cells,
            estimated_peak_mib: peak,
            estimated_output_mib: output,
            diagnostics: d,
        }
    }
    /// Number of cells whose control volume intersects a box (for memory estimates).
    fn covered_cells(&self, lo: &[f64; 3], hi: &[f64; 3]) -> f64 {
        (0..3)
            .map(|a| {
                let n = self.grid.cells[a] as f64;
                let h = self.domain.size_m[a] / n.max(1.0);
                if !(h > 0.0 && lo[a].is_finite() && hi[a].is_finite()) {
                    return 0.0;
                }
                let first = (lo[a] / h).floor().clamp(0.0, n);
                let last = (hi[a] / h).ceil().clamp(0.0, n);
                (last - first).max(0.0)
            })
            .product()
    }
    /// Best-practice domain warnings for RANS boundary-layer cases with buildings, following
    /// the COST 732 guideline: about 5 H_max of clearance at the inlet, sides and top, 15 H_max
    /// behind the buildings, and a blockage ratio below 3 %.
    fn domain_warnings(&self, add: &mut impl FnMut(&str, &str, &str, &str)) {
        let boxes: Vec<&Block> = self
            .geometry
            .iter()
            .filter(|b| (0..3).all(|a| b.max_m[a] > b.min_m[a]))
            .collect();
        if boxes.is_empty() {
            return;
        }
        let h = boxes.iter().map(|b| b.max_m[2]).fold(0.0, f64::max);
        let lo: [f64; 3] =
            std::array::from_fn(|a| boxes.iter().map(|b| b.min_m[a]).fold(f64::MAX, f64::min));
        let hi: [f64; 3] =
            std::array::from_fn(|a| boxes.iter().map(|b| b.max_m[a]).fold(0.0, f64::max));
        let wind = self.flow.vector(h.max(1e-9));
        let speed = wind[0].hypot(wind[1]).max(1e-300);
        // Periodic sides repeat the buildings, so clearance does not apply to them; open,
        // slip and wall sides all need it.
        for (a, name) in [(0, "x"), (1, "y")] {
            if self.boundaries.at(a) == Boundary::Periodic {
                continue;
            }
            let along = wind[a] / speed;
            let (low, high) = (lo[a], self.domain.size_m[a] - hi[a]);
            let message = if along.abs() > 0.1 {
                let (upstream, downstream) = if along > 0.0 {
                    (low, high)
                } else {
                    (high, low)
                };
                (upstream < 5.0 * h || downstream < 15.0 * h).then(|| format!(
                    "Along {name} the buildings have {:.1} H_max of clearance upstream and {:.1} H_max downstream; the COST 732 guideline recommends about 5 H_max and 15 H_max.",
                    upstream / h,
                    downstream / h
                ))
            } else {
                (low.min(high) < 5.0 * h).then(|| format!(
                    "Across the wind, along {name}, the buildings have {:.1} H_max and {:.1} H_max of lateral clearance; the COST 732 guideline recommends about 5 H_max on each side.",
                    low / h,
                    high / h
                ))
            };
            if let Some(message) = message {
                add(
                    "CFD-W003",
                    "warning",
                    &format!("domain.size_m[{a}]"),
                    &message,
                );
            }
        }
        if self.domain.size_m[2] - h < 5.0 * h {
            add(
                "CFD-W003",
                "warning",
                "domain.size_m[2]",
                &format!(
                    "The domain top is {:.1} H_max above the tallest building; the COST 732 guideline recommends about 5 H_max.",
                    (self.domain.size_m[2] - h) / h
                ),
            );
        }
        // Frontal area for wind along x (projection onto y-z) and along y (onto x-z),
        // as the union of box shadows, weighted by the wind components.
        let shadow = |u: usize| -> f64 {
            let mut edges: Vec<f64> = boxes
                .iter()
                .flat_map(|b| [b.min_m[u], b.max_m[u]])
                .collect();
            edges.sort_by(f64::total_cmp);
            edges.dedup();
            edges
                .windows(2)
                .map(|w| {
                    let mid = 0.5 * (w[0] + w[1]);
                    let top = boxes
                        .iter()
                        .filter(|b| b.min_m[u] <= mid && mid < b.max_m[u])
                        .map(|b| b.max_m[2])
                        .fold(0.0, f64::max);
                    (w[1] - w[0]) * top
                })
                .sum()
        };
        let (wx, wy) = ((wind[0] / speed).abs(), (wind[1] / speed).abs());
        let frontal = wx * shadow(1) + wy * shadow(0);
        let section =
            (wx * self.domain.size_m[1] + wy * self.domain.size_m[0]) * self.domain.size_m[2];
        let blockage = frontal / section.max(1e-300);
        if blockage > 0.03 {
            add(
                "CFD-W002",
                "warning",
                "geometry",
                &format!(
                    "The blockage ratio is about {:.1} %; the COST 732 guideline recommends below 3 %. Enlarge the domain cross-section.",
                    100.0 * blockage
                ),
            );
        }
    }
    /// [`check`](Self::check) as a `Result`: the report when the case is
    /// valid (it may still carry warnings), otherwise an error whose text is
    /// the report serialized as JSON, so its codes and paths can be parsed
    /// back.
    ///
    /// ```
    /// # let json = r#"{"schema_version": "cfd-craft.case/0.1", "name": "tiny",
    /// #   "domain": {"size_m": [4.0, 1.0, 1.0]}, "grid": {"cells": [8, 2, 2]},
    /// #   "boundaries": {"x": "open", "y": "periodic", "z": "periodic"},
    /// #   "flow": {"kind": "prescribed_uniform", "velocity_m_s": [1.0, 0.0, 0.0]},
    /// #   "numerics": {"transport": "linear_upwind", "max_time_step_s": 1.0,
    /// #     "advective_cfl_max": 0.4, "pressure_relative_tolerance": 1e-10,
    /// #     "pressure_max_iterations": 100, "steady_residual_tolerance": 1e-5,
    /// #     "steady_monitor_tolerance": 1e-3, "monitor_window": 10},
    /// #   "run": {"mode": "steady", "end_time_s": 1.0, "max_steps": 1000,
    /// #     "wall_time_limit_s": 30.0, "threads": 1, "memory_limit_mib": 64},
    /// #   "output": {"fields": false, "checkpoint": false, "max_total_mib": 4}}"#;
    /// use cfd_craft::case::{Case, CheckReport};
    ///
    /// let mut case: Case = serde_json::from_str(json).unwrap();
    /// assert!(case.validate().is_ok());
    /// case.schema_version = "cfd-craft.case/9".into();
    /// let error = case.validate().unwrap_err();
    /// let report: CheckReport = serde_json::from_str(&error).unwrap();
    /// assert_eq!(report.diagnostics[0].code, "CFD-E001");
    /// assert_eq!(report.diagnostics[0].path, "schema_version");
    /// ```
    pub fn validate(&self) -> Result<CheckReport> {
        let r = self.check();
        if r.valid {
            Ok(r)
        } else {
            Err(serde_json::to_string(&r).unwrap())
        }
    }
}
/// Lowercase hex SHA-256 of `bytes`: the digest behind the case, physics,
/// state, mask, checkpoint and archive identities.
pub fn hash_bytes(bytes: &[u8]) -> String {
    format!("{:x}", Sha256::digest(bytes))
}