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
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
// *****************************************************************************
/*!
  \file      src/Inciter/ZalCG.cpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2024 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     ZalCG: Taylor-Galerkin, FCT, edge-based continuous Galerkin
*/
// *****************************************************************************

#include "XystBuildConfig.hpp"
#include "ZalCG.hpp"
#include "Vector.hpp"
#include "Reader.hpp"
#include "ContainerUtil.hpp"
#include "UnsMesh.hpp"
#include "ExodusIIMeshWriter.hpp"
#include "InciterConfig.hpp"
#include "DerivedData.hpp"
#include "Discretization.hpp"
#include "DiagReducer.hpp"
#include "IntegralReducer.hpp"
#include "Integrals.hpp"
#include "Refiner.hpp"
#include "Reorder.hpp"
#include "Around.hpp"
#include "Zalesak.hpp"
#include "Problems.hpp"
#include "EOS.hpp"
#include "BC.hpp"

namespace inciter {

extern ctr::Config g_cfg;

static CkReduction::reducerType IntegralsMerger;

} // inciter::

using inciter::g_cfg;
using inciter::ZalCG;

ZalCG::ZalCG( const CProxy_Discretization& disc,
              const std::map< int, std::vector< std::size_t > >& bface,
              const std::map< int, std::vector< std::size_t > >& bnode,
              const std::vector< std::size_t >& triinpoel ) :
  m_disc( disc ),
  m_nrhs( 0 ),
  m_nnorm( 0 ),
  m_naec( 0 ),
  m_nalw( 0 ),
  m_nlim( 0 ),
  m_ndea( 0 ),
  m_nact( 0 ),
  m_todeactivate( 0 ),
  m_toreactivate( 0 ),
  m_deactivated( 0 ),
  m_bnode( bnode ),
  m_bface( bface ),
  m_triinpoel( tk::remap( triinpoel, Disc()->Lid() ) ),
  m_u( Disc()->Gid().size(), g_cfg.get< tag::problem_ncomp >() ),
  m_p( m_u.nunk(), m_u.nprop()*2 ),
  m_q( m_u.nunk(), m_u.nprop()*2 ),
  m_a( m_u.nunk(), m_u.nprop() ),
  m_rhs( m_u.nunk(), m_u.nprop() ),
  m_vol( Disc()->Vol() ),
  m_dtp( m_u.nunk(), 0.0 ),
  m_tp( m_u.nunk(), g_cfg.get< tag::t0 >() ),
  m_finished( 0 ),
  m_freezeflow( 1.0 ),
  m_fctfreeze( 0 )
// *****************************************************************************
//  Constructor
//! \param[in] disc Discretization proxy
//! \param[in] bface Boundary-faces mapped to side sets used in the input file
//! \param[in] bnode Boundary-node lists mapped to side sets used in input file
//! \param[in] triinpoel Boundary-face connectivity where BCs set (global ids)
// *****************************************************************************
{
  usesAtSync = true;    // enable migration at AtSync

  auto d = Disc();

  // Compute total box IC volume
  d->boxvol();

  // Activate SDAG wait for initially computing integrals
  thisProxy[ thisIndex ].wait4int();
}

void
ZalCG::setupBC()
// *****************************************************************************
// Prepare boundary condition data structures
// *****************************************************************************
{
  // Query Dirichlet BC nodes associated to side sets
  std::unordered_map< int, std::unordered_set< std::size_t > > dir;
  for (const auto& s : g_cfg.get< tag::bc_dir >()) {
    auto k = m_bface.find(s[0]);
    if (k != end(m_bface)) {
      auto& n = dir[ k->first ];
      for (auto f : k->second) {
        const auto t = m_triinpoel.data() + f*3;
        n.insert( t[0] );
        n.insert( t[1] );
        n.insert( t[2] );
      }
    }
  }

  // Augment Dirichlet BC nodes with nodes not necessarily part of faces
  const auto& lid = Disc()->Lid();
  for (const auto& s : g_cfg.get< tag::bc_dir >()) {
    auto k = m_bnode.find(s[0]);
    if (k != end(m_bnode)) {
      auto& n = dir[ k->first ];
      for (auto g : k->second) {
        n.insert( tk::cref_find(lid,g) );
      }
    }
  }

  // Collect unique set of nodes + Dirichlet BC components mask
  auto ncomp = m_u.nprop();
  auto nmask = ncomp + 1;
  const auto& dbc = g_cfg.get< tag::bc_dir >();
  std::unordered_map< std::size_t, std::vector< int > > dirbcset;
  for (const auto& mask : dbc) {
    ErrChk( mask.size() == nmask, "Incorrect Dirichlet BC mask ncomp" );
    auto n = dir.find( mask[0] );
    if (n != end(dir))
      for (auto p : n->second) {
        auto& m = dirbcset[p];
        if (m.empty()) m.resize( ncomp, 0 );
        for (std::size_t c=0; c<ncomp; ++c)
          if (!m[c]) m[c] = mask[c+1];  // overwrite mask if 0 -> 1
      }
  }
  // Compile streamable list of nodes + Dirichlet BC components mask
  tk::destroy( m_dirbcmasks );
  for (const auto& [p,mask] : dirbcset) {
    m_dirbcmasks.push_back( p );
    m_dirbcmasks.insert( end(m_dirbcmasks), begin(mask), end(mask) );
  }
  ErrChk( m_dirbcmasks.size() % nmask == 0, "Dirichlet BC masks incomplete" );

  // Query pressure BC nodes associated to side sets
  std::unordered_map< int, std::unordered_set< std::size_t > > pre;
  for (const auto& ss : g_cfg.get< tag::bc_pre >()) {
    for (const auto& s : ss) {
      auto k = m_bface.find(s);
      if (k != end(m_bface)) {
        auto& n = pre[ k->first ];
        for (auto f : k->second) {
          const auto t = m_triinpoel.data() + f*3;
          n.insert( t[0] );
          n.insert( t[1] );
          n.insert( t[2] );
        }
      }
    }
  }

  // Augment Pressure BC nodes with nodes not necessarily part of faces
  for (const auto& s : g_cfg.get< tag::bc_pre >()) {
    auto k = m_bnode.find(s[0]);
    if (k != end(m_bnode)) {
      auto& n = pre[ k->first ];
      for (auto g : k->second) {
        n.insert( tk::cref_find(lid,g) );
      }
    }
  }

  // Prepare density and pressure values for pressure BC nodes
  const auto& pbc_set = g_cfg.get< tag::bc_pre >();
  if (!pbc_set.empty()) {
    const auto& pbc_r = g_cfg.get< tag::bc_pre_density >();
    ErrChk( pbc_r.size() == pbc_set.size(), "Pressure BC density unspecified" );
    const auto& pbc_p = g_cfg.get< tag::bc_pre_pressure >();
    ErrChk( pbc_p.size() == pbc_set.size(), "Pressure BC pressure unspecified" );
    tk::destroy( m_prebcnodes );
    tk::destroy( m_prebcvals );
    for (const auto& [s,n] : pre) {
      m_prebcnodes.insert( end(m_prebcnodes), begin(n), end(n) );
      for (std::size_t p=0; p<pbc_set.size(); ++p) {
        for (auto u : pbc_set[p]) {
          if (s == u) {
            for (std::size_t i=0; i<n.size(); ++i) {
              m_prebcvals.push_back( pbc_r[p] );
              m_prebcvals.push_back( pbc_p[p] );
            }
          }
        }
      }
    }
    ErrChk( m_prebcnodes.size()*2 == m_prebcvals.size(),
            "Pressure BC data incomplete" );
  }

  // Query symmetry BC nodes associated to side sets
  std::unordered_map< int, std::unordered_set< std::size_t > > sym;
  for (auto s : g_cfg.get< tag::bc_sym >()) {
    auto k = m_bface.find(s);
    if (k != end(m_bface)) {
      auto& n = sym[ k->first ];
      for (auto f : k->second) {
        const auto t = m_triinpoel.data() + f*3;
        n.insert( t[0] );
        n.insert( t[1] );
        n.insert( t[2] );
      }
    }
  }

  // Query farfield BC nodes associated to side sets
  std::unordered_map< int, std::unordered_set< std::size_t > > far;
  for (auto s : g_cfg.get< tag::bc_far >()) {
    auto k = m_bface.find(s);
    if (k != end(m_bface)) {
      auto& n = far[ k->first ];
      for (auto f : k->second) {
        const auto t = m_triinpoel.data() + f*3;
        n.insert( t[0] );
        n.insert( t[1] );
        n.insert( t[2] );
      }
    }
  }

  // Generate unique set of symmetry BC nodes
  tk::destroy( m_symbcnodeset );
  for (const auto& [s,n] : sym) m_symbcnodeset.insert( begin(n), end(n) );
  // Generate unique set of farfield BC nodes
  tk::destroy( m_farbcnodeset );
  for (const auto& [s,n] : far) m_farbcnodeset.insert( begin(n), end(n) );

  // If farfield BC is set on a node, will not also set symmetry BC
  for (auto i : m_farbcnodeset) m_symbcnodeset.erase(i);
  // If pressure BC is set on a node, will not also set symmetry BC
  for (auto i : m_prebcnodes) m_symbcnodeset.erase(i);
}

void
ZalCG::feop()
// *****************************************************************************
// Start (re-)computing finite element domain and boundary operators
// *****************************************************************************
{
  auto d = Disc();

  // Prepare boundary conditions data structures
  setupBC();

  // Compute local contributions to boundary normals and integrals
  bndint();
  // Compute contributions to domain edge integrals
  domint();

  // Send boundary point normal contributions to neighbor chares
  if (d->NodeCommMap().empty()) {
    comnorm_complete();
  } else {
    for (const auto& [c,nodes] : d->NodeCommMap()) {
      decltype(m_bnorm) exp;
      for (auto i : nodes) {
        for (const auto& [s,b] : m_bnorm) {
          auto k = b.find(i);
          if (k != end(b)) exp[s][i] = k->second;
        }
      }
      thisProxy[c].comnorm( exp );
    }
  }
  ownnorm_complete();
}

void
ZalCG::bndint()
// *****************************************************************************
//! Compute local contributions to boundary normals and integrals
// *****************************************************************************
{
  auto d = Disc();
  const auto& coord = d->Coord();
  const auto& gid = d->Gid();
  const auto& x = coord[0];
  const auto& y = coord[1];
  const auto& z = coord[2];

  // Lambda to compute the inverse distance squared between boundary face
  // centroid and boundary point. Here p is the global node id and c is the
  // the boundary face centroid.
  auto invdistsq = [&]( const tk::real c[], std::size_t p ){
    return 1.0 / ( (c[0] - x[p]) * (c[0] - x[p]) +
                   (c[1] - y[p]) * (c[1] - y[p]) +
                   (c[2] - z[p]) * (c[2] - z[p]) );
  };

  tk::destroy( m_bnorm );
  tk::destroy( m_bndpoinint );
  tk::destroy( m_bndedgeint );

  for (const auto& [ setid, faceids ] : m_bface) { // for all side sets
    for (auto f : faceids) { // for all side set triangles
      const auto N = m_triinpoel.data() + f*3;
      const std::array< tk::real, 3 >
        ba{ x[N[1]]-x[N[0]], y[N[1]]-y[N[0]], z[N[1]]-z[N[0]] },
        ca{ x[N[2]]-x[N[0]], y[N[2]]-y[N[0]], z[N[2]]-z[N[0]] };
      auto n = tk::cross( ba, ca );
      auto A2 = tk::length( n );
      n[0] /= A2;
      n[1] /= A2;
      n[2] /= A2;
      const tk::real centroid[3] = {
        (x[N[0]] + x[N[1]] + x[N[2]]) / 3.0,
        (y[N[0]] + y[N[1]] + y[N[2]]) / 3.0,
        (z[N[0]] + z[N[1]] + z[N[2]]) / 3.0 };
      // contribute all edges of triangle
      for (const auto& [i,j] : tk::lpoet) {
        auto p = N[i];
        auto q = N[j];
        tk::real r = invdistsq( centroid, p );
        auto& v = m_bnorm[setid];      // associate side set id
        auto& bn = v[gid[p]];          // associate global node id of bnd pnt
        bn[0] += r * n[0];             // inv.dist.sq-weighted normal
        bn[1] += r * n[1];
        bn[2] += r * n[2];
        bn[3] += r;                    // inv.dist.sq of node from centroid
        auto& b = m_bndpoinint[gid[p]];// assoc global id of bnd point
        b[0] += n[0] * A2 / 6.0;       // bnd-point integral
        b[1] += n[1] * A2 / 6.0;
        b[2] += n[2] * A2 / 6.0;
        tk::UnsMesh::Edge ed{ gid[p], gid[q] };
        tk::real sig = 1.0;
        if (ed[0] > ed[1]) {
          std::swap( ed[0], ed[1] );
          sig = -1.0;
        }
        auto& e = m_bndedgeint[ ed ];
        e[0] += sig * n[0] * A2 / 24.0; // bnd-edge integral
        e[1] += sig * n[1] * A2 / 24.0;
        e[2] += sig * n[2] * A2 / 24.0;
      }
    }
  }
}

void
ZalCG::domint()
// *****************************************************************************
//! Compute local contributions to domain edge integrals
// *****************************************************************************
{
  auto d = Disc();

  const auto& gid = d->Gid();
  const auto& inpoel = d->Inpoel();

  const auto& coord = d->Coord();
  const auto& x = coord[0];
  const auto& y = coord[1];
  const auto& z = coord[2];

  tk::destroy( m_domedgeint );

  for (std::size_t e=0; e<inpoel.size()/4; ++e) {
    const auto N = inpoel.data() + e*4;
    const std::array< tk::real, 3 >
      ba{{ x[N[1]]-x[N[0]], y[N[1]]-y[N[0]], z[N[1]]-z[N[0]] }},
      ca{{ x[N[2]]-x[N[0]], y[N[2]]-y[N[0]], z[N[2]]-z[N[0]] }},
      da{{ x[N[3]]-x[N[0]], y[N[3]]-y[N[0]], z[N[3]]-z[N[0]] }};
    const auto J = tk::triple( ba, ca, da );        // J = 6V
    Assert( J > 0, "Element Jacobian non-positive" );
    std::array< std::array< tk::real, 3 >, 4 > grad;
    grad[1] = tk::cross( ca, da );
    grad[2] = tk::cross( da, ba );
    grad[3] = tk::cross( ba, ca );
    for (std::size_t i=0; i<3; ++i)
      grad[0][i] = -grad[1][i]-grad[2][i]-grad[3][i];
    auto J120 = J/120.0;
    for (const auto& [p,q] : tk::lpoed) {
      tk::UnsMesh::Edge ed{ gid[N[p]], gid[N[q]] };
      tk::real sig = 1.0;
      if (ed[0] > ed[1]) {
        std::swap( ed[0], ed[1] );
        sig = -1.0;
      }
      auto& n = m_domedgeint[ ed ];
      n[0] += sig * (grad[p][0] - grad[q][0]) / 48.0;
      n[1] += sig * (grad[p][1] - grad[q][1]) / 48.0;
      n[2] += sig * (grad[p][2] - grad[q][2]) / 48.0;
      n[3] += J120;
    }
  }
}

void
ZalCG::comnorm( const decltype(m_bnorm)& inbnd )
// *****************************************************************************
//  Receive contributions to boundary point normals on chare-boundaries
//! \param[in] inbnd Incoming partial sums of boundary point normals
// *****************************************************************************
{
  // Buffer up incoming boundary point normal vector contributions
  for (const auto& [s,b] : inbnd) {
    auto& bndnorm = m_bnormc[s];
    for (const auto& [p,n] : b) {
      auto& norm = bndnorm[p];
      norm[0] += n[0];
      norm[1] += n[1];
      norm[2] += n[2];
      norm[3] += n[3];
    }
  }

  if (++m_nnorm == Disc()->NodeCommMap().size()) {
    m_nnorm = 0;
    comnorm_complete();
  }
}

void
ZalCG::registerReducers()
// *****************************************************************************
//  Configure Charm++ reduction types initiated from this chare array
//! \details Since this is a [initnode] routine, the runtime system executes the
//!   routine exactly once on every logical node early on in the Charm++ init
//!   sequence. Must be static as it is called without an object. See also:
//!   Section "Initializations at Program Startup" at in the Charm++ manual
//!   http://charm.cs.illinois.edu/manuals/html/charm++/manual.html.
// *****************************************************************************
{
  NodeDiagnostics::registerReducers();
  IntegralsMerger = CkReduction::addReducer( integrals::mergeIntegrals );
}

void
// cppcheck-suppress unusedFunction
ZalCG::ResumeFromSync()<--- Unmatched suppression: unusedFunction
// *****************************************************************************
//  Return from migration
//! \details This is called when load balancing (LB) completes. The presence of
//!   this function does not affect whether or not we block on LB.
// *****************************************************************************
{
  if (Disc()->It() == 0) Throw( "it = 0 in ResumeFromSync()" );

  if (!g_cfg.get< tag::nonblocking >()) dt();
}

void
ZalCG::setup( tk::real v )
// *****************************************************************************
// Start setup for solution
//! \param[in] v Total volume within user-specified box
// *****************************************************************************
{
  auto d = Disc();

  // Store user-defined box IC volume
  Disc()->Boxvol() = v;

  // Set initial conditions
  problems::initialize( d->Coord(), m_u, d->T(), d->BoxNodes() );

  // Query time history field output labels from all PDEs integrated
  if (!g_cfg.get< tag::histout >().empty()) {
    std::vector< std::string > var
      {"density", "xvelocity", "yvelocity", "zvelocity", "energy", "pressure"};
    auto ncomp = m_u.nprop();
    for (std::size_t c=5; c<ncomp; ++c)
      var.push_back( "c" + std::to_string(c-5) );
    d->histheader( std::move(var) );
  }

  // Compute finite element operators
  feop();
}

void
ZalCG::start()
// *****************************************************************************
// Start time stepping
// *****************************************************************************
{
  // Set flag that indicates that we are now during time stepping
  Disc()->Initial( 0 );
  // Start timer measuring time stepping wall clock time
  Disc()->Timer().zero();
  // Zero grind-timer
  Disc()->grindZero();
  // Continue to first time step
  dt();
}

void
ZalCG::bnorm()
// *****************************************************************************
// Combine own and communicated portions of the boundary point normals
// *****************************************************************************
{
  const auto& lid = Disc()->Lid();

  // Combine own and communicated contributions to boundary point normals
  for (const auto& [s,b] : m_bnormc) {
    auto& bndnorm = m_bnorm[s];
    for (const auto& [g,n] : b) {
      auto& norm = bndnorm[g];
      norm[0] += n[0];
      norm[1] += n[1];
      norm[2] += n[2];
      norm[3] += n[3];
    }
  }
  tk::destroy( m_bnormc );

  // Divide summed point normals by the sum of the inverse distance squared
  for (auto& [s,b] : m_bnorm) {
    for (auto& [g,n] : b) {
      n[0] /= n[3];
      n[1] /= n[3];
      n[2] /= n[3];
      Assert( (n[0]*n[0] + n[1]*n[1] + n[2]*n[2] - 1.0) <
              1.0e+3*std::numeric_limits< tk::real >::epsilon(),
              "Non-unit normal" );
    }
  }

  // Replace global->local ids associated to boundary point normals
  decltype(m_bnorm) loc;
  for (auto& [s,b] : m_bnorm) {
    auto& bnd = loc[s];
    for (auto&& [g,n] : b) {
      bnd[ tk::cref_find(lid,g) ] = std::move(n);
    }
  }
  m_bnorm = std::move(loc);
}

void
ZalCG::streamable()
// *****************************************************************************
// Convert integrals into streamable data structures
// *****************************************************************************
{
  const auto& lid = Disc()->Lid();

  // Convert boundary point integrals into streamable data structures
  m_bpoin.resize( m_bndpoinint.size() );
  m_bpint.resize( m_bndpoinint.size() * 3 );
  std::size_t i = 0;
  for (const auto& [g,b] : m_bndpoinint) {
    m_bpoin[i] = tk::cref_find( lid, g );
    m_bpint[i*3+0] = b[0];
    m_bpint[i*3+1] = b[1];
    m_bpint[i*3+2] = b[2];
    ++i;
  }

  m_besym.resize( m_triinpoel.size() );
  i = 0;
  for (auto p : m_triinpoel) {
    m_besym[i++] = static_cast< std::uint8_t >(m_symbcnodeset.count(p));
  }

  // Query surface integral output nodes
  std::unordered_map< int, std::vector< std::size_t > > surfintnodes;
  const auto& is = g_cfg.get< tag::integout >();
  std::set< int > outsets( begin(is), end(is) );
  for (auto s : outsets) {
    auto m = m_bface.find(s);
    if (m != end(m_bface)) {
      auto& n = surfintnodes[ m->first ];       // associate set id
      for (auto f : m->second) {                // face ids on side set
        n.push_back( m_triinpoel[f*3+0] );      // nodes on side set
        n.push_back( m_triinpoel[f*3+1] );
        n.push_back( m_triinpoel[f*3+2] );
      }
    }
  }
  for (auto& [s,n] : surfintnodes) tk::unique( n );
  // Prepare surface integral data
  tk::destroy( m_surfint );
  const auto& gid = Disc()->Gid();
  for (auto&& [s,n] : surfintnodes) {
    auto& sint = m_surfint[s];  // associate set id
    auto& nodes = sint.first;
    auto& ndA = sint.second;
    nodes = std::move(n);
    ndA.resize( nodes.size()*3 );
    std::size_t a = 0;
    for (auto p : nodes) {
      const auto& b = tk::cref_find( m_bndpoinint, gid[p] );
      ndA[a*3+0] = b[0];        // store ni * dA
      ndA[a*3+1] = b[1];
      ndA[a*3+2] = b[2];
      ++a;
    }
  }
  tk::destroy( m_bndpoinint );

  // Generate edges along chare boundary
  chbnded();
  // Adjust node volumes along inactive neighbor chares
  deavol();
  // Generate superedges for domain integral
  domsuped();

  // Convert symmetry BC data to streamable data structures
  tk::destroy( m_symbcnodes );
  tk::destroy( m_symbcnorms );
  for (auto p : m_symbcnodeset) {
    for (const auto& s : g_cfg.get< tag::bc_sym >()) {
      auto m = m_bnorm.find(s);
      if (m != end(m_bnorm)) {
        auto r = m->second.find(p);
        if (r != end(m->second)) {
          m_symbcnodes.push_back( p );
          m_symbcnorms.push_back( r->second[0] );
          m_symbcnorms.push_back( r->second[1] );
          m_symbcnorms.push_back( r->second[2] );
        }
      }
    }
  }
  tk::destroy( m_symbcnodeset );

  // Convert farfield BC data to streamable data structures
  tk::destroy( m_farbcnodes );
  tk::destroy( m_farbcnorms );
  for (auto p : m_farbcnodeset) {
    for (const auto& s : g_cfg.get< tag::bc_far >()) {
      auto n = m_bnorm.find(s);
      if (n != end(m_bnorm)) {
        auto a = n->second.find(p);
        if (a != end(n->second)) {
          m_farbcnodes.push_back( p );
          m_farbcnorms.push_back( a->second[0] );
          m_farbcnorms.push_back( a->second[1] );
          m_farbcnorms.push_back( a->second[2] );
        }
      }
    }
  }
  tk::destroy( m_farbcnodeset );
  tk::destroy( m_bnorm );
}

void
ZalCG::domsuped()
// *****************************************************************************
// Generate superedge-groups for domain-edge loops
//! \see See Lohner, Sec. 15.1.6.2, An Introduction to Applied CFD Techniques,
//!      Wiley, 2008.
// *****************************************************************************
{
  Assert( !m_domedgeint.empty(), "No domain edges to group" );

  #ifndef NDEBUG
  auto nedge = m_domedgeint.size();
  #endif

  const auto& inpoel = Disc()->Inpoel();
  const auto& lid = Disc()->Lid();
  const auto& gid = Disc()->Gid();

  tk::destroy( m_dsupedge[0] );
  tk::destroy( m_dsupedge[1] );
  tk::destroy( m_dsupedge[2] );

  tk::destroy( m_dsupint[0] );
  tk::destroy( m_dsupint[1] );
  tk::destroy( m_dsupint[2] );

  tk::UnsMesh::FaceSet untri;
  for (std::size_t e=0; e<inpoel.size()/4; e++) {
    const auto N = inpoel.data() + e*4;
    for (const auto& [a,b,c] : tk::lpofa) untri.insert( { N[a], N[b], N[c] } );
  }

  for (std::size_t e=0; e<inpoel.size()/4; ++e) {
    const auto N = inpoel.data() + e*4;
    int f = 0;
    tk::real sig[6];
    decltype(m_domedgeint)::const_iterator d[6];
    for (const auto& [p,q] : tk::lpoed) {
      tk::UnsMesh::Edge ed{ gid[N[p]], gid[N[q]] };
      sig[f] = ed[0] < ed[1] ? 1.0 : -1.0;
      d[f] = m_domedgeint.find( ed );
      if (d[f] == end(m_domedgeint)) break; else ++f;
    }
    if (f == 6) {
      m_dsupedge[0].push_back( N[0] );
      m_dsupedge[0].push_back( N[1] );
      m_dsupedge[0].push_back( N[2] );
      m_dsupedge[0].push_back( N[3] );
      for (const auto& [a,b,c] : tk::lpofa) untri.erase( { N[a], N[b], N[c] } );
      for (int ed=0; ed<6; ++ed) {
        const auto& ded = d[ed]->second;
        m_dsupint[0].push_back( sig[ed] * ded[0] );
        m_dsupint[0].push_back( sig[ed] * ded[1] );
        m_dsupint[0].push_back( sig[ed] * ded[2] );
        m_dsupint[0].push_back( ded[3] );
        m_domedgeint.erase( d[ed] );
      }
    }
  }

  for (const auto& N : untri) {
    int f = 0;
    tk::real sig[3];
    decltype(m_domedgeint)::const_iterator d[3];
    for (const auto& [p,q] : tk::lpoet) {
      tk::UnsMesh::Edge ed{ gid[N[p]], gid[N[q]] };
      sig[f] = ed[0] < ed[1] ? 1.0 : -1.0;
      d[f] = m_domedgeint.find( ed );
      if (d[f] == end(m_domedgeint)) break; else ++f;
    }
    if (f == 3) {
      m_dsupedge[1].push_back( N[0] );
      m_dsupedge[1].push_back( N[1] );
      m_dsupedge[1].push_back( N[2] );
      for (int ed=0; ed<3; ++ed) {
        const auto& ded = d[ed]->second;
        m_dsupint[1].push_back( sig[ed] * ded[0] );
        m_dsupint[1].push_back( sig[ed] * ded[1] );
        m_dsupint[1].push_back( sig[ed] * ded[2] );
        m_dsupint[1].push_back( ded[3] );
        m_domedgeint.erase( d[ed] );
      }
    }
  }

  m_dsupedge[2].resize( m_domedgeint.size()*2 );
  m_dsupint[2].resize( m_domedgeint.size()*4 );
  std::size_t k = 0;
  for (const auto& [ed,d] : m_domedgeint) {
    auto e = m_dsupedge[2].data() + k*2;
    e[0] = tk::cref_find( lid, ed[0] );
    e[1] = tk::cref_find( lid, ed[1] );
    auto n = m_dsupint[2].data() + k*4;
    n[0] = d[0];
    n[1] = d[1];
    n[2] = d[2];
    n[3] = d[3];
    ++k;
  }

  if (g_cfg.get< tag::fct >()) {
    const auto ncomp = m_u.nprop();
    m_dsuplim[0].resize( m_dsupedge[0].size() * 6 * ncomp );
    m_dsuplim[1].resize( m_dsupedge[1].size() * 3 * ncomp );
    m_dsuplim[2].resize( m_dsupedge[2].size() * ncomp );
  }

  tk::destroy( m_domedgeint );

  //std::cout << std::setprecision(2)
  //          << "superedges: ntet:" << m_dsupedge[0].size()/4 << "(nedge:"
  //          << m_dsupedge[0].size()/4*6 << ","
  //          << 100.0 * static_cast< tk::real >( m_dsupedge[0].size()/4*6 ) /
  //                     static_cast< tk::real >( nedge )
  //          << "%) + ntri:" << m_dsupedge[1].size()/3
  //          << "(nedge:" << m_dsupedge[1].size() << ","
  //          << 100.0 * static_cast< tk::real >( m_dsupedge[1].size() ) /
  //                     static_cast< tk::real >( nedge )
  //          << "%) + nedge:"
  //          << m_dsupedge[2].size()/2 << "("
  //          << 100.0 * static_cast< tk::real >( m_dsupedge[2].size()/2 ) /
  //                     static_cast< tk::real >( nedge )
  //          << "%) = " << m_dsupedge[0].size()/4*6 + m_dsupedge[1].size() +
  //             m_dsupedge[2].size()/2 << " of "<< nedge << " total edges\n";

  Assert( m_dsupedge[0].size()/4*6 + m_dsupedge[1].size() +
          m_dsupedge[2].size()/2 == nedge,
          "Not all edges accounted for in superedge groups" );
}

void
ZalCG::chbnded()
// *****************************************************************************
// Generate chare-boundary edge data structures for deactivation
// *****************************************************************************
{
  auto d = Disc();
  if (not g_cfg.get< tag::deactivate >() or d->NodeCommMap().empty()) return;

  const auto& inpoel = Disc()->Inpoel();
  const auto& lid = Disc()->Lid();
  const auto& gid = Disc()->Gid();

  // Generate elements surrounding points
  auto esup = tk::genEsup( inpoel, 4 );

  // Collect edges of all tetrahedra surrounding chare-boundary points
  tk::destroy( m_chbndedge );
  for (const auto& [c,nodes] : d->NodeCommMap()) {
    std::unordered_map< tk::UnsMesh::Edge, tk::real,
                        tk::UnsMesh::Hash<2>, tk::UnsMesh::Eq<2> > edges;
    for (auto g : nodes) {
      auto i = tk::cref_find(lid,g);
      for (auto e : tk::Around(esup,i)) {
        const auto N = inpoel.data() + e*4;
        for (const auto& [p,q] : tk::lpoed) {
          tk::UnsMesh::Edge ged{ gid[N[p]], gid[N[q]] };
          edges[ { N[p], N[q] } ] = tk::cref_find(m_domedgeint,ged)[3];
        }
      }
    }
    auto& ed = m_chbndedge[c];
    for (const auto& [e,sint] : edges) ed.emplace_back( e, sint );
  }
}

void
ZalCG::deavol()
// *****************************************************************************
// Adjust node volumes along inactive neighbor chares
// *****************************************************************************
{
  auto d = Disc();
  if (not g_cfg.get< tag::deactivate >() or d->NodeCommMap().empty()) return;

  m_vol = d->Vol();
  const auto& cvolc = d->Cvolc();
  for (auto i : m_inactive) {
    for (const auto& [p,v] : tk::cref_find(cvolc,i)) {
      m_vol[p] -= v;
    }
  }
}

void
// cppcheck-suppress unusedFunction
ZalCG::merge()<--- Unmatched suppression: unusedFunction
// *****************************************************************************
// Combine own and communicated portions of the integrals
// *****************************************************************************
{
  auto d = Disc();

  // Combine own and communicated contributions to boundary point normals
  bnorm();

  // Convert integrals into streamable data structures
  streamable();

  // Enforce boundary conditions using (re-)computed boundary data
  BC( m_u, d->T() );

  if (d->Initial()) {
    // Output initial conditions to file
    writeFields( CkCallback(CkIndex_ZalCG::start(), thisProxy[thisIndex]) );
  } else {
    feop_complete();
  }
}

void
ZalCG::BC( tk::Fields& u, tk::real t )
// *****************************************************************************
// Apply boundary conditions
//! \param[in,out] u Solution to apply BCs to
//! \param[in] t Physical time
// *****************************************************************************
{
  auto d = Disc();

  // Apply Dirichlet BCs
  physics::dirbc( u, t, d->Coord(), d->BoxNodes(), m_dirbcmasks );

  // Apply symmetry BCs
  physics::symbc( u, m_symbcnodes, m_symbcnorms, /*pos=*/1 );

  // Apply farfield BCs
  physics::farbc( u, m_farbcnodes, m_farbcnorms );

  // Apply pressure BCs
  physics::prebc( u, m_prebcnodes, m_prebcvals );
}

void
ZalCG::dt()
// *****************************************************************************
// Compute time step size
// *****************************************************************************
{
  tk::real mindt = std::numeric_limits< tk::real >::max();

  auto const_dt = g_cfg.get< tag::dt >();
  auto eps = std::numeric_limits< tk::real >::epsilon();
  auto d = Disc();

  if (not m_deactivated) {

    // Adjust node volumes along inactive neighbor chares for next step
    deavol();

    // use constant dt if configured
    if (std::abs(const_dt) > eps) {

      // cppcheck-suppress redundantInitialization
      mindt = const_dt;<--- Unmatched suppression: redundantInitialization

    } else {

      const auto& vol = d->Vol();
      auto cfl = g_cfg.get< tag::cfl >();

      if (g_cfg.get< tag::steady >()) {

        for (std::size_t p=0; p<m_u.nunk(); ++p) {
          auto r = m_u(p,0);
          auto u = m_u(p,1)/r;
          auto v = m_u(p,2)/r;
          auto w = m_u(p,3)/r;
          auto pr = eos::pressure( m_u(p,4) - 0.5*r*(u*u + v*v + w*w) );
          auto c = eos::soundspeed( r, std::max(pr,0.0) );
          auto L = std::cbrt( vol[p] );
          auto vel = std::sqrt( u*u + v*v + w*w );
          m_dtp[p] = L / std::max( vel+c, 1.0e-8 ) * cfl;
        }
        mindt = *std::min_element( begin(m_dtp), end(m_dtp) );

      } else {

        for (std::size_t p=0; p<m_u.nunk(); ++p) {
          auto r = m_u(p,0);
          auto u = m_u(p,1)/r;
          auto v = m_u(p,2)/r;
          auto w = m_u(p,3)/r;
          auto pr = eos::pressure( m_u(p,4) - 0.5*r*(u*u + v*v + w*w) );
          auto c = eos::soundspeed( r, std::max(pr,0.0) );
          auto L = std::cbrt( vol[p] );
          auto vel = std::sqrt( u*u + v*v + w*w );
          auto euler_dt = L / std::max( vel+c, 1.0e-8 );
          mindt = std::min( mindt, euler_dt );
        }
        mindt *= cfl;

      }

      mindt *= m_freezeflow;

    }

  }

  // Actiavate SDAG waits for next time step
  thisProxy[ thisIndex ].wait4rhs();
  thisProxy[ thisIndex ].wait4aec();
  thisProxy[ thisIndex ].wait4alw();
  thisProxy[ thisIndex ].wait4sol();
  thisProxy[ thisIndex ].wait4dea();
  thisProxy[ thisIndex ].wait4act();
  thisProxy[ thisIndex ].wait4step();

  // Contribute to minimum dt across all chares and advance to next step
  contribute( sizeof(tk::real), &mindt, CkReduction::min_double,
              CkCallback(CkReductionTarget(ZalCG,advance), thisProxy) );
}

void
ZalCG::advance( tk::real newdt )
// *****************************************************************************
// Advance equations to next time step
//! \param[in] newdt The smallest dt across the whole problem
// *****************************************************************************
{
  // Set new time step size
  Disc()->setdt( newdt );

  if (m_deactivated) solve(); else rhs();
}

void
ZalCG::rhs()
// *****************************************************************************
// Compute right-hand side of transport equations
// *****************************************************************************
{
  auto d = Disc();
  const auto& lid = d->Lid();

  // Compute own portion of right-hand side for all equations
  zalesak::rhs( m_dsupedge, m_dsupint, d->Coord(), m_triinpoel, m_besym,
                d->T(), d->Dt(), m_tp, m_dtp, m_u, m_rhs );

  // Communicate rhs to other chares on chare-boundary
  if (d->NodeCommMap().empty() or m_inactive.size() == d->NodeCommMap().size()){
    comrhs_complete();
  } else {
    for (const auto& [c,n] : d->NodeCommMap()) {
      if (m_inactive.count(c)) continue;
      std::unordered_map< std::size_t, std::vector< tk::real > > exp;
      for (auto g : n) exp[g] = m_rhs[ tk::cref_find(lid,g) ];
      thisProxy[c].comrhs( exp );
    }
  }
  ownrhs_complete();
}

void
ZalCG::comrhs(
  const std::unordered_map< std::size_t, std::vector< tk::real > >& inrhs )
// *****************************************************************************
//  Receive contributions to right-hand side vector on chare-boundaries
//! \param[in] inrhs Partial contributions of RHS to chare-boundary nodes. Key:
//!   global mesh node IDs, value: contributions for all scalar components.
// *****************************************************************************
{
  using tk::operator+=;
  for (const auto& [g,r] : inrhs) m_rhsc[g] += r;

  // When we have heard from all chares we communicate with, this chare is done
  if (++m_nrhs + m_inactive.size() == Disc()->NodeCommMap().size()) {
    m_nrhs = 0;
    comrhs_complete();
  }
}

void
ZalCG::fct()
// *****************************************************************************
// Continue with flux-corrected transport if enabled
// *****************************************************************************
{
  auto d = Disc();
  const auto& lid = d->Lid();

  // Combine own and communicated contributions to rhs
  for (const auto& [g,r] : m_rhsc) {
    auto i = tk::cref_find( lid, g );
    for (std::size_t c=0; c<r.size(); ++c) m_rhs(i,c) += r[c];
  }
  tk::destroy(m_rhsc);

  if (g_cfg.get< tag::fct >()) aec(); else solve();
}

void
// cppcheck-suppress unusedFunction
ZalCG::aec()<--- Unmatched suppression: unusedFunction
// *****************************************************************************
// Compute antidiffusive contributions: P+/-
// *****************************************************************************
{
  auto d = Disc();
  const auto ncomp = m_u.nprop();
  const auto& lid = d->Lid();

  // Antidiffusive contributions: P+/-

  auto ctau = g_cfg.get< tag::fctdif >();
  m_p.fill( 0.0 );

  // tetrahedron superedges
  for (std::size_t e=0; e<m_dsupedge[0].size()/4; ++e) {
    const auto N = m_dsupedge[0].data() + e*4;
    const auto D = m_dsupint[0].data();<--- Variable 'D' can be declared as pointer to const
    std::size_t i = 0;
    for (const auto& [p,q] : tk::lpoed) {
      auto dif = D[(e*6+i)*4+3];
      for (std::size_t c=0; c<ncomp; ++c) {
        auto aec = -dif * ctau * (m_u(N[p],c) - m_u(N[q],c));<--- Shadow variable
        auto a = c*2;
        auto b = a+1;
        if (aec > 0.0) std::swap(a,b);
        m_p(N[p],a) -= aec;
        m_p(N[q],b) += aec;
      }
      ++i;
    }
  }

  // triangle superedges
  for (std::size_t e=0; e<m_dsupedge[1].size()/3; ++e) {
    const auto N = m_dsupedge[1].data() + e*3;
    const auto D = m_dsupint[1].data();<--- Variable 'D' can be declared as pointer to const
    std::size_t i = 0;
    for (const auto& [p,q] : tk::lpoet) {
      auto dif = D[(e*3+i)*4+3];
      for (std::size_t c=0; c<ncomp; ++c) {
        auto aec = -dif * ctau * (m_u(N[p],c) - m_u(N[q],c));<--- Shadow variable
        auto a = c*2;
        auto b = a+1;
        if (aec > 0.0) std::swap(a,b);
        m_p(N[p],a) -= aec;
        m_p(N[q],b) += aec;
      }
      ++i;
    }
  }

  // edges
  for (std::size_t e=0; e<m_dsupedge[2].size()/2; ++e) {
    const auto N = m_dsupedge[2].data() + e*2;
    const auto dif = m_dsupint[2][e*4+3];
    for (std::size_t c=0; c<ncomp; ++c) {
      auto aec = -dif * ctau * (m_u(N[0],c) - m_u(N[1],c));<--- Shadow variable
      auto a = c*2;
      auto b = a+1;
      if (aec > 0.0) std::swap(a,b);
      m_p(N[0],a) -= aec;
      m_p(N[1],b) += aec;
    }
  }

  // Apply symmetry BCs on AEC
  for (std::size_t i=0; i<m_symbcnodes.size(); ++i) {
    auto p = m_symbcnodes[i];
    auto nx = m_symbcnorms[i*3+0];
    auto ny = m_symbcnorms[i*3+1];
    auto nz = m_symbcnorms[i*3+2];
    auto rvnp = m_p(p,2)*nx + m_p(p,4)*ny + m_p(p,6)*nz;
    auto rvnn = m_p(p,3)*nx + m_p(p,5)*ny + m_p(p,7)*nz;
    m_p(p,2) -= rvnp * nx;
    m_p(p,3) -= rvnn * nx;
    m_p(p,4) -= rvnp * ny;
    m_p(p,5) -= rvnn * ny;
    m_p(p,6) -= rvnp * nz;
    m_p(p,7) -= rvnn * nz;
  }

  // Communicate antidiffusive edge and low-order solution contributions
  if (d->NodeCommMap().empty() or m_inactive.size() == d->NodeCommMap().size()){
    comaec_complete();
  } else {
    for (const auto& [c,n] : d->NodeCommMap()) {
      if (m_inactive.count(c)) continue;
      decltype(m_pc) exp;
      for (auto g : n) exp[g] = m_p[ tk::cref_find(lid,g) ];
      thisProxy[c].comaec( exp );
    }
  }
  ownaec_complete();
}

void
ZalCG::comaec( const std::unordered_map< std::size_t,
                       std::vector< tk::real > >& inaec )
// *****************************************************************************
//  Receive antidiffusive and low-order contributions on chare-boundaries
//! \param[in] inaec Partial contributions of antidiffusive edge and low-order
//!   solution contributions on chare-boundary nodes. Key: global mesh node IDs,
//!   value: 0: antidiffusive contributions, 1: low-order solution.
// *****************************************************************************
{
  using tk::operator+=;
  for (const auto& [g,a] : inaec) m_pc[g] += a;

  // When we have heard from all chares we communicate with, this chare is done
  if (++m_naec + m_inactive.size() == Disc()->NodeCommMap().size()) {
    m_naec = 0;
    comaec_complete();
  }
}

void
ZalCG::alw()
// *****************************************************************************
// Compute allowed limits, Q+/-
// *****************************************************************************
{
  auto d = Disc();
  const auto steady = g_cfg.get< tag::steady >();
  const auto npoin = m_u.nunk();
  const auto ncomp = m_u.nprop();
  const auto& lid = d->Lid();

  // Combine own and communicated contributions to antidiffusive contributions
  // and low-order solution
  for (const auto& [g,p] : m_pc) {
    auto i = tk::cref_find( lid, g );
    for (std::size_t c=0; c<p.size(); ++c) m_p(i,c) += p[c];
  }
  tk::destroy(m_pc);

  // Finish computing antidiffusive contributions and low-order solution
  auto dt = d->Dt();<--- Shadow variable
  for (std::size_t i=0; i<npoin; ++i) {
    if (steady) dt = m_dtp[i];
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      m_p(i,a) /= m_vol[i];
      m_p(i,b) /= m_vol[i];
      // low-order solution
      m_rhs(i,c) = m_u(i,c) - dt*m_rhs(i,c)/m_vol[i] - m_p(i,a) - m_p(i,b);
    }
  }

  // Allowed limits: Q+/-

  using std::max;
  using std::min;

  auto large = std::numeric_limits< tk::real >::max();
  for (std::size_t i=0; i<m_q.nunk(); ++i) {
    for (std::size_t c=0; c<m_q.nprop()/2; ++c) {
      m_q(i,c*2+0) = -large;
      m_q(i,c*2+1) = +large;
    }
  }

  // tetrahedron superedges
  for (std::size_t e=0; e<m_dsupedge[0].size()/4; ++e) {
    const auto N = m_dsupedge[0].data() + e*4;
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      for (const auto& [p,q] : tk::lpoed) {
        tk::real alwp, alwn;
        if (g_cfg.get< tag::fctclip >()) {
          alwp = max( m_rhs(N[p],c), m_rhs(N[q],c) );
          alwn = min( m_rhs(N[p],c), m_rhs(N[q],c) );
        } else {
          alwp = max( max(m_rhs(N[p],c), m_u(N[p],c)),
                      max(m_rhs(N[q],c), m_u(N[q],c)) );
          alwn = min( min(m_rhs(N[p],c), m_u(N[p],c)),
                      min(m_rhs(N[q],c), m_u(N[q],c)) );
        }
        m_q(N[p],a) = max(m_q(N[p],a), alwp);
        m_q(N[p],b) = min(m_q(N[p],b), alwn);
        m_q(N[q],a) = max(m_q(N[q],a), alwp);
        m_q(N[q],b) = min(m_q(N[q],b), alwn);
      }
    }
  }

  // triangle superedges
  for (std::size_t e=0; e<m_dsupedge[1].size()/3; ++e) {
    const auto N = m_dsupedge[1].data() + e*3;
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      for (const auto& [p,q] : tk::lpoet) {
        tk::real alwp, alwn;
        if (g_cfg.get< tag::fctclip >()) {
          alwp = max( m_rhs(N[p],c), m_rhs(N[q],c) );
          alwn = min( m_rhs(N[p],c), m_rhs(N[q],c) );
        } else {
          alwp = max( max(m_rhs(N[p],c), m_u(N[p],c)),
                      max(m_rhs(N[q],c), m_u(N[q],c)) );
          alwn = min( min(m_rhs(N[p],c), m_u(N[p],c)),
                      min(m_rhs(N[q],c), m_u(N[q],c)) );
        }
        m_q(N[p],a) = max(m_q(N[p],a), alwp);
        m_q(N[p],b) = min(m_q(N[p],b), alwn);
        m_q(N[q],a) = max(m_q(N[q],a), alwp);
        m_q(N[q],b) = min(m_q(N[q],b), alwn);
      }
    }
  }

  // edges
  for (std::size_t e=0; e<m_dsupedge[2].size()/2; ++e) {
    const auto N = m_dsupedge[2].data() + e*2;
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      tk::real alwp, alwn;
      if (g_cfg.get< tag::fctclip >()) {
        alwp = max( m_rhs(N[0],c), m_rhs(N[1],c) );
        alwn = min( m_rhs(N[0],c), m_rhs(N[1],c) );
      } else {
        alwp = max( max(m_rhs(N[0],c), m_u(N[0],c)),
                    max(m_rhs(N[1],c), m_u(N[1],c)) );
        alwn = min( min(m_rhs(N[0],c), m_u(N[0],c)),
                    min(m_rhs(N[1],c), m_u(N[1],c)) );
      }
      m_q(N[0],a) = max(m_q(N[0],a), alwp);
      m_q(N[0],b) = min(m_q(N[0],b), alwn);
      m_q(N[1],a) = max(m_q(N[1],a), alwp);
      m_q(N[1],b) = min(m_q(N[1],b), alwn);
    }
  }

  // Communicate allowed limits contributions
  if (d->NodeCommMap().empty() or m_inactive.size() == d->NodeCommMap().size()){
    comalw_complete();
  } else {
    for (const auto& [c,n] : d->NodeCommMap()) {
      if (m_inactive.count(c)) continue;
      decltype(m_qc) exp;
      for (auto g : n) exp[g] = m_q[ tk::cref_find(lid,g) ];
      thisProxy[c].comalw( exp );
    }
  }
  ownalw_complete();
}

void
ZalCG::comalw( const std::unordered_map< std::size_t,
                       std::vector< tk::real > >& inalw )
// *****************************************************************************
//  Receive allowed limits contributions on chare-boundaries
//! \param[in] inalw Partial contributions of allowed limits contributions on
//!   chare-boundary nodes. Key: global mesh node IDs, value: allowed limit
//!   contributions.
// *****************************************************************************
{
  for (const auto& [g,alw] : inalw) {<--- Shadow variable
    auto& q = m_qc[g];
    q.resize( alw.size() );
    for (std::size_t c=0; c<alw.size()/2; ++c) {
      auto a = c*2;
      auto b = a+1;
      q[a] = std::max( q[a], alw[a] );
      q[b] = std::min( q[b], alw[b] );
    }
  }

  // When we have heard from all chares we communicate with, this chare is done
  if (++m_nalw + m_inactive.size() == Disc()->NodeCommMap().size()) {
    m_nalw = 0;
    comalw_complete();
  }
}

void
ZalCG::lim()
// *****************************************************************************
// Compute limit coefficients
// *****************************************************************************
{
  auto d = Disc();
  const auto npoin = m_u.nunk();
  const auto ncomp = m_u.nprop();
  const auto& lid = d->Lid();

  using std::max;
  using std::min;

  // Combine own and communicated contributions to allowed limits
  for (const auto& [g,alw] : m_qc) {<--- Shadow variable
    auto i = tk::cref_find( lid, g );
    for (std::size_t c=0; c<alw.size()/2; ++c) {
      auto a = c*2;
      auto b = a+1;
      m_q(i,a) = max( m_q(i,a), alw[a] );
      m_q(i,b) = min( m_q(i,b), alw[b] );
    }
  }
  tk::destroy(m_qc);

  // Finish computing allowed limits
  for (std::size_t i=0; i<npoin; ++i) {
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      m_q(i,a) -= m_rhs(i,c);
      m_q(i,b) -= m_rhs(i,c);
    }
  }

  // Limit coefficients, C

  for (std::size_t i=0; i<npoin; ++i) {
    for (std::size_t c=0; c<ncomp; ++c) {
      auto a = c*2;
      auto b = a+1;
      auto eps = std::numeric_limits< tk::real >::epsilon();
      m_q(i,a) = m_p(i,a) <  eps ? 0.0 : min(1.0, m_q(i,a)/m_p(i,a));
      m_q(i,b) = m_p(i,b) > -eps ? 0.0 : min(1.0, m_q(i,b)/m_p(i,b));
    }
  }

  // Limited antidiffusive contributions

  auto ctau = g_cfg.get< tag::fctdif >();
  m_a.fill( 0.0 );

  auto fctsys = g_cfg.get< tag::fctsys >();
  for (auto& c : fctsys) --c;

  #if defined(__clang__)
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wvla"
    #pragma clang diagnostic ignored "-Wvla-extension"
  #elif defined(STRICT_GNUC)
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wvla"
  #endif

  // tetrahedron superedges
  for (std::size_t e=0; e<m_dsupedge[0].size()/4; ++e) {
    const auto N = m_dsupedge[0].data() + e*4;
    const auto D = m_dsupint[0].data();<--- Variable 'D' can be declared as pointer to const
    auto C = m_dsuplim[0].data();
    std::size_t i = 0;
    for (const auto& [p,q] : tk::lpoed) {
      auto dif = D[(e*6+i)*4+3];
      auto coef = C + (e*6+i)*ncomp;
      tk::real aec[ncomp];<--- Shadow variable
      for (std::size_t c=0; c<ncomp; ++c) {
        aec[c] = -dif * ctau * (m_u(N[p],c) - m_u(N[q],c));
        if (m_fctfreeze) continue;
        auto a = c*2;
        auto b = a+1;
        coef[c] = min( aec[c] < 0.0 ? m_q(N[p],a) : m_q(N[p],b),
                       aec[c] > 0.0 ? m_q(N[q],a) : m_q(N[q],b) );
      }
      tk::real cs = 1.0;
      for (auto c : fctsys) cs = min( cs, coef[c] );
      for (auto c : fctsys) coef[c] = cs;
      for (std::size_t c=0; c<ncomp; ++c) {
        aec[c] *= coef[c];
        m_a(N[p],c) -= aec[c];
        m_a(N[q],c) += aec[c];
      }
      ++i;
    }
  }

  // triangle superedges
  for (std::size_t e=0; e<m_dsupedge[1].size()/3; ++e) {
    const auto N = m_dsupedge[1].data() + e*3;
    const auto D = m_dsupint[1].data();<--- Variable 'D' can be declared as pointer to const
    auto C = m_dsuplim[0].data();
    std::size_t i = 0;
    for (const auto& [p,q] : tk::lpoet) {
      auto dif = D[(e*3+i)*4+3];
      auto coef = C + (e*3+i)*ncomp;
      tk::real aec[ncomp];<--- Shadow variable
      for (std::size_t c=0; c<ncomp; ++c) {
        aec[c] = -dif * ctau * (m_u(N[p],c) - m_u(N[q],c));
        if (m_fctfreeze) continue;
        auto a = c*2;
        auto b = a+1;
        coef[c] = min( aec[c] < 0.0 ? m_q(N[p],a) : m_q(N[p],b),
                       aec[c] > 0.0 ? m_q(N[q],a) : m_q(N[q],b) );
      }
      tk::real cs = 1.0;
      for (auto c : fctsys) cs = min( cs, coef[c] );
      for (auto c : fctsys) coef[c] = cs;
      for (std::size_t c=0; c<ncomp; ++c) {
        aec[c] *= coef[c];
        m_a(N[p],c) -= aec[c];
        m_a(N[q],c) += aec[c];
      }
      ++i;
    }
  }

  // edges
  for (std::size_t e=0; e<m_dsupedge[2].size()/2; ++e) {
    const auto N = m_dsupedge[2].data() + e*2;
    const auto dif = m_dsupint[2][e*4+3];
    auto coef = m_dsuplim[2].data() + e*ncomp;
    tk::real aec[ncomp];<--- Shadow variable
    for (std::size_t c=0; c<ncomp; ++c) {
      aec[c] = -dif * ctau * (m_u(N[0],c) - m_u(N[1],c));
      auto a = c*2;
      auto b = a+1;
      if (m_fctfreeze) continue;
      coef[c] = min( aec[c] < 0.0 ? m_q(N[0],a) : m_q(N[0],b),
                     aec[c] > 0.0 ? m_q(N[1],a) : m_q(N[1],b) );
    }
    tk::real cs = 1.0;
    for (auto c : fctsys) cs = min( cs, coef[c] );
    for (auto c : fctsys) coef[c] = cs;
    for (std::size_t c=0; c<ncomp; ++c) {
      aec[c] *= coef[c];
      m_a(N[0],c) -= aec[c];
      m_a(N[1],c) += aec[c];
    }
  }

  #if defined(__clang__)
    #pragma clang diagnostic pop
  #elif defined(STRICT_GNUC)
    #pragma GCC diagnostic pop
  #endif

  // Communicate limited antidiffusive contributions
  if (d->NodeCommMap().empty() or m_inactive.size() == d->NodeCommMap().size()){
    comlim_complete();
  } else {
    for (const auto& [c,n] : d->NodeCommMap()) {
      if (m_inactive.count(c)) continue;
      decltype(m_ac) exp;
      for (auto g : n) exp[g] = m_a[ tk::cref_find(lid,g) ];
      thisProxy[c].comlim( exp );
    }
  }
  ownlim_complete();
}

void
ZalCG::comlim( const std::unordered_map< std::size_t,
                       std::vector< tk::real > >& inlim )
// *****************************************************************************
//  Receive limited antidiffusive contributions on chare-boundaries
//! \param[in] inlim Partial contributions of limited contributions on
//!   chare-boundary nodes. Key: global mesh node IDs, value: limited
//!   contributions.
// *****************************************************************************
{
  using tk::operator+=;
  for (const auto& [g,a] : inlim) m_ac[g] += a;

  // When we have heard from all chares we communicate with, this chare is done
  if (++m_nlim + m_inactive.size() == Disc()->NodeCommMap().size()) {
    m_nlim = 0;
    comlim_complete();
  }
}

void
ZalCG::solve()
// *****************************************************************************
// Advance systems of equations
// *****************************************************************************
{
  auto d = Disc();
  const auto npoin = m_u.nunk();
  const auto ncomp = m_u.nprop();
  const auto steady = g_cfg.get< tag::steady >();

  if (m_deactivated) {

    m_a = m_u;

  } else {

    // Combine own and communicated contributions to limited antidiffusive
    // contributions
    for (const auto& [g,a] : m_ac) {
      auto i = tk::cref_find( d->Lid(), g );
      for (std::size_t c=0; c<a.size(); ++c) m_a(i,c) += a[c];
    }
    tk::destroy(m_ac);

    tk::Fields u;
    std::size_t cstart = m_freezeflow > 1.0 ? 5 : 0;
    if (cstart) u = m_u;

    if (g_cfg.get< tag::fct >()) {
      // Apply limited antidiffusive contributions to low-order solution
      for (std::size_t i=0; i<npoin; ++i) {
        for (std::size_t c=0; c<ncomp; ++c) {
          m_a(i,c) = m_rhs(i,c) + m_a(i,c)/m_vol[i];
        }
      }
    } else {
      // Apply rhs
      auto dt = d->Dt();<--- Shadow variable
      for (std::size_t i=0; i<npoin; ++i) {
        if (steady) dt = m_dtp[i];
        for (std::size_t c=0; c<ncomp; ++c) {
          m_a(i,c) = m_u(i,c) - dt*m_rhs(i,c)/m_vol[i];
        }
      }
    }

    // Configure and apply scalar source to solution (if defined)
    auto src = problems::PHYS_SRC();<--- Shadow variable
    if (src) src( d->Coord(), d->T(), m_a );

    // Freeze flow if configured and apply multiplier on scalar(s)
    if (d->T() > g_cfg.get< tag::freezetime >()) {
      m_freezeflow = g_cfg.get< tag::freezeflow >();
    }

    // Enforce boundary conditions
    BC( m_a, d->T() + d->Dt() );

    // Explicitly zero out flow for freezeflow
    if (cstart) {
      for (std::size_t i=0; i<npoin; ++i) {
        for (std::size_t c=0; c<cstart; ++c) {
          m_a(i,c) = u(i,c);
        }
      }
    }

  }

  // Compute diagnostics, e.g., residuals
  auto diag_iter = g_cfg.get< tag::diag_iter >();
  auto diag = m_diag.rhocompute( *d, m_a, m_u, diag_iter );

  // Update solution
  m_u = m_a;
  m_a.fill( 0.0 );

  // Increase number of iterations and physical time
  d->next();

  // Advance physical time for local time stepping
  if (steady) {
    using tk::operator+=;
    m_tp += m_dtp;
  }

  // Evaluate residuals
  if (!diag) evalres( std::vector< tk::real >( ncomp, 1.0 ) );
}

void
ZalCG::evalres( const std::vector< tk::real >& l2res )
// *****************************************************************************
//  Evaluate residuals
//! \param[in] l2res L2-norms of the residual for each scalar component
//!   computed across the whole problem
// *****************************************************************************
{
  auto d = Disc();

  if (g_cfg.get< tag::steady >()) {
    const auto rc = g_cfg.get< tag::rescomp >() - 1;
    d->residual( l2res[rc] );
    if (l2res[rc] < g_cfg.get< tag::fctfreeze >()) m_fctfreeze = 1;
  }

  if (d->deactivate()) deactivate(); else refine();
}

int
ZalCG::active( std::size_t p,
               std::size_t q,
               const std::vector< uint64_t >& sys ) const
// *****************************************************************************
//  Decide if edge is active
//! \param[in] p Local id of left edge-end point
//! \param[in] q Local id of right edge-end point
//! \param[in] sys List of components to consider
//! \return True if active, false if not
// *****************************************************************************
{
  auto tol = g_cfg.get< tag::deatol >();

  tk::real maxdiff = 0.0;
  for (auto c : sys) {
    auto e = std::abs( m_u(p,c) - m_u(q,c) );
    maxdiff = std::max( maxdiff, e );
  }

  return maxdiff > tol;
}

int
ZalCG::dea( const std::vector< uint64_t >& sys ) const
// *****************************************************************************
//  Decide whether to deactivate this chare
//! \param[in] sys List of components to consider
//! \return Nonzero to deactivate, zero to keep active
// *****************************************************************************
{
  if (m_toreactivate) return 0;
  if (m_deactivated) return 1;

  // tetrahedron superedges
  for (std::size_t e=0; e<m_dsupedge[0].size()/4; ++e) {
    const auto N = m_dsupedge[0].data() + e*4;
    for (const auto& [p,q] : tk::lpoed) {
      if (active(N[p],N[q],sys)) return 0;
    }
  }

  // triangle superedges
  for (std::size_t e=0; e<m_dsupedge[1].size()/3; ++e) {
    const auto N = m_dsupedge[1].data() + e*3;
    for (const auto& [p,q] : tk::lpoet) {
      if (active(N[p],N[q],sys)) return 0;
    }
  }

  // edges
  for (std::size_t e=0; e<m_dsupedge[2].size()/2; ++e) {
    const auto N = m_dsupedge[2].data() + e*2;
    if (active(N[0],N[1],sys)) return 0;
  }

  return 1;
}

void
ZalCG::rea( const std::vector< uint64_t >& sys, std::unordered_set< int >& req )
const
// *****************************************************************************
//  Decide whether to reactivate any neighbor chare
//! \param[in] sys List of components to consider
//! \param[in,out] req Set of neighbor chares to reactivate
// *****************************************************************************
{
  for (const auto& [c,edges] : m_chbndedge) {
    if (not m_inactive.count(c)) continue;
    for (const auto& [e,sint] : edges) {
      if (active(e[0],e[1],sys)) {
        req.insert(c);
        break;
      }
    }
  }
}

void
ZalCG::huldif()
// *****************************************************************************
// Apply diffusion on active hull
// *****************************************************************************
{
  const auto eps = std::numeric_limits< tk::real >::epsilon();
  const auto dif = g_cfg.get< tag::deadif >();
  if (std::abs(dif) < eps) return;

  const auto npoin = m_u.nunk();
  const auto ncomp = m_u.nprop();

  m_rhs.fill( 0.0 );
  for (const auto& [ch,edges] : m_chbndedge) {
    if (not m_inactive.count(ch)) continue;
    for (const auto& [e,sint] : edges) {
      auto p = e[0];
      auto q = e[1];
      auto fw = dif * sint;
      for (std::size_t c=0; c<ncomp; ++c) {
       auto f = fw*(m_u(p,c) - m_u(q,c));
        m_rhs(p,c) -= f;
        m_rhs(q,c) += f;
      }
    }
  }

  for (std::size_t i=0; i<npoin; ++i) {
    for (std::size_t c=0; c<ncomp; ++c) {
      m_u(i,c) += m_rhs(i,c)/m_vol[i];
    }
  }
}

void
ZalCG::deactivate()
// *****************************************************************************
// Deactivate regions
// *****************************************************************************
{
  auto d = Disc();

  std::unordered_set< int > reactivate;

  if (not m_deactivated) {
    // Apply diffusion on active hull
    huldif();
    // Query scalar components to deactivate based on
    auto sys = g_cfg.get< tag::deasys >();
    for (auto& c : sys) --c;
    // Decide whether to deactivate this chare
    if (d->deastart() and dea(sys)) m_todeactivate = 1;
    // Decide whether to reactivate any neighbor chare
    rea( sys, reactivate );
  }

  // Communicate deactivation requests
  for (const auto& [c,n] : d->NodeCommMap()) {
    thisProxy[c].comdea( reactivate.count(c) );
  }
  owndea_complete();
}

void
ZalCG::comdea( std::size_t reactivate )
// *****************************************************************************
//  Communicate deactivation desires
//! \param[in] reactivate 1 if sender wants to reactivate this chare, 0 if not
// *****************************************************************************
{
  auto d = Disc();

  if (m_deactivated and reactivate) m_toreactivate = 1;

  // Communicate activation status to neighbors
  if (++m_ndea == d->NodeCommMap().size()) {
    m_ndea = 0;
    comdea_complete();
  }
}

void
ZalCG::activate()
// *****************************************************************************
// Compute deactivation status
// *****************************************************************************
{
  auto d = Disc();

  if (m_todeactivate) m_deactivated = 1;
  if (m_toreactivate) m_deactivated = 0;

  // Communicate deactivation status
  for (const auto& [c,n] : d->NodeCommMap()) {
    thisProxy[c].comact( thisIndex, m_deactivated );
  }
  ownact_complete();
}

void
ZalCG::comact( int ch, int deactivated )
// *****************************************************************************
//  Communicate deactivation status
//! \param[in] ch Sender chare id
//! \param[in] deactivated 1 if sender was deactivated, 0 if not
// *****************************************************************************
{
  auto d = Disc();

  if (deactivated) m_inactive.insert(ch); else m_inactive.erase(ch);

  // When we have heard from all chares we communicate with, this chare is done
  if (++m_nact == d->NodeCommMap().size()) {
    // Aggregate deactivation status to every chare
    contribute( sizeof(int), &m_deactivated, CkReduction::sum_int,
                CkCallback(CkReductionTarget(ZalCG,deastat), thisProxy) );
    m_todeactivate = 0;
    m_toreactivate = 0;
    m_nact = 0;
    comact_complete();
  }
}

void
ZalCG::deastat( int dea )
// *****************************************************************************
//  Receive aggregate deactivation status
//! \param[in] dea Sum of deactived chares
// *****************************************************************************
{
  auto d = Disc();

  // Disallow deactivating all chares
  if (dea == d->nchare()) {
    dea = 0;
    m_deactivated = 0;
    m_inactive.clear();
  }

  // Report number of deactivated chares for diagnostics
  if (thisIndex == 0) d->deactivated( dea );

  deastat_complete();
}

void
ZalCG::refine()
// *****************************************************************************
// Optionally refine/derefine mesh
// *****************************************************************************
{
  auto d = Disc();

  // See if this is the last time step
  if (d->finished()) m_finished = 1;

  auto dtref = g_cfg.get< tag::href_dt >();
  auto dtfreq = g_cfg.get< tag::href_dtfreq >();

  // if t>0 refinement enabled and we hit the frequency
  if (dtref && !(d->It() % dtfreq)) {   // refine

    d->refined() = 1;
    d->startvol();
    d->Ref()->dtref( m_bface, m_bnode, m_triinpoel );

    // Activate SDAG waits for re-computing the integrals
    thisProxy[ thisIndex ].wait4int();

  } else {      // do not refine

    d->refined() = 0;
    feop_complete();
    resize_complete();

  }
}

void
ZalCG::resizePostAMR(
  const std::vector< std::size_t >& /*ginpoel*/,
  const tk::UnsMesh::Chunk& chunk,
  const tk::UnsMesh::Coords& coord,
  const std::unordered_map< std::size_t, tk::UnsMesh::Edge >& addedNodes,
  const std::unordered_map< std::size_t, std::size_t >& /*addedTets*/,
  const std::set< std::size_t >& removedNodes,
  const std::unordered_map< int, std::unordered_set< std::size_t > >&
    nodeCommMap,
  const std::map< int, std::vector< std::size_t > >& bface,
  const std::map< int, std::vector< std::size_t > >& bnode,
  const std::vector< std::size_t >& triinpoel )
// *****************************************************************************
//  Receive new mesh from Refiner
//! \param[in] ginpoel Mesh connectivity with global node ids
//! \param[in] chunk New mesh chunk (connectivity and global<->local id maps)
//! \param[in] coord New mesh node coordinates
//! \param[in] addedNodes Newly added mesh nodes and their parents (local ids)
//! \param[in] addedTets Newly added mesh cells and their parents (local ids)
//! \param[in] removedNodes Newly removed mesh node local ids
//! \param[in] nodeCommMap New node communication map
//! \param[in] bface Boundary-faces mapped to side set ids
//! \param[in] bnode Boundary-node lists mapped to side set ids
//! \param[in] triinpoel Boundary-face connectivity
// *****************************************************************************
{
  auto d = Disc();
  auto ncomp = m_u.nprop();

  d->Itf() = 0;  // Zero field output iteration count if AMR
  ++d->Itr();    // Increase number of iterations with a change in the mesh

  // Resize mesh data structures after mesh refinement
  d->resizePostAMR( chunk, coord, nodeCommMap, removedNodes );

  Assert(coord[0].size() == m_u.nunk()-removedNodes.size()+addedNodes.size(),
    "Incorrect vector length post-AMR: expected length after resizing = " +
    std::to_string(coord[0].size()) + ", actual unknown vector length = " +
    std::to_string(m_u.nunk()-removedNodes.size()+addedNodes.size()));

  // Remove newly removed nodes from solution vectors
  m_u.rm( removedNodes );
  m_rhs.rm( removedNodes );

  // Resize auxiliary solution vectors
  auto npoin = coord[0].size();
  m_u.resize( npoin );
  m_rhs.resize( npoin );

  // Update solution on new mesh
  for (const auto& n : addedNodes)
    for (std::size_t c=0; c<ncomp; ++c) {
      Assert(n.first < m_u.nunk(), "Added node index out of bounds post-AMR");
      Assert(n.second[0] < m_u.nunk() && n.second[1] < m_u.nunk(),
        "Indices of parent-edge nodes out of bounds post-AMR");
      m_u(n.first,c) = (m_u(n.second[0],c) + m_u(n.second[1],c))/2.0;
    }

  // Update physical-boundary node-, face-, and element lists
  m_bnode = bnode;
  m_bface = bface;
  m_triinpoel = tk::remap( triinpoel, d->Lid() );

  auto meshid = d->MeshId();
  contribute( sizeof(std::size_t), &meshid, CkReduction::nop,
              CkCallback(CkReductionTarget(Transporter,resized), d->Tr()) );
}

void
ZalCG::writeFields( CkCallback cb )
// *****************************************************************************
// Output mesh-based fields to file
//! \param[in] cb Function to continue with after the write
// *****************************************************************************
{
  if (g_cfg.get< tag::benchmark >()) { cb.send(); return; }

  auto d = Disc();
  auto ncomp = m_u.nprop();
  auto steady = g_cfg.get< tag::steady >();

  // Field output

  std::vector< std::string > nodefieldnames
    {"density", "velocityx", "velocityy", "velocityz", "energy", "pressure"};
  if (steady) nodefieldnames.push_back( "mach" );

  using tk::operator/=;
  auto r = m_u.extract(0);
  auto u = m_u.extract(1);  u /= r;
  auto v = m_u.extract(2);  v /= r;
  auto w = m_u.extract(3);  w /= r;
  auto e = m_u.extract(4);  e /= r;
  std::vector< tk::real > pr( m_u.nunk() ), ma;
  if (steady) ma.resize( m_u.nunk() );
  for (std::size_t i=0; i<pr.size(); ++i) {
    auto vv = u[i]*u[i] + v[i]*v[i] + w[i]*w[i];
    pr[i] = eos::pressure( r[i]*(e[i] - 0.5*vv) );
    if (steady) ma[i] = std::sqrt(vv) / eos::soundspeed( r[i], pr[i] );
  }

  std::vector< std::vector< tk::real > > nodefields{
    std::move(r), std::move(u), std::move(v), std::move(w), std::move(e),
    std::move(pr) };
  if (steady) nodefields.push_back( std::move(ma) );

  for (std::size_t c=0; c<ncomp-5; ++c) {
    nodefieldnames.push_back( "c" + std::to_string(c) );
    nodefields.push_back( m_u.extract(5+c) );
  }

  // query function to evaluate analytic solution (if defined)
  auto sol = problems::SOL();

  if (sol) {
    const auto& coord = d->Coord();
    const auto& x = coord[0];
    const auto& y = coord[1];
    const auto& z = coord[2];
    auto an = m_u;
    std::vector< tk::real > ap( m_u.nunk() );
    for (std::size_t i=0; i<an.nunk(); ++i) {
      auto s = sol( x[i], y[i], z[i], d->T() );
      s[1] /= s[0];
      s[2] /= s[0];
      s[3] /= s[0];
      s[4] /= s[0];
      for (std::size_t c=0; c<s.size(); ++c) an(i,c) = s[c];
      s[4] -= 0.5*(s[1]*s[1] + s[2]*s[2] + s[3]*s[3]);
      ap[i] = eos::pressure( s[0]*s[4] );
    }
    for (std::size_t c=0; c<5; ++c) {
      nodefieldnames.push_back( nodefieldnames[c] + "_analytic" );
      nodefields.push_back( an.extract(c) );
    }
    nodefieldnames.push_back( nodefieldnames[5] + "_analytic" );
    nodefields.push_back( std::move(ap) );
    for (std::size_t c=0; c<ncomp-5; ++c) {
      nodefieldnames.push_back( nodefieldnames[6+c] + "_analytic" );
      nodefields.push_back( an.extract(5+c) );
    }
  }

  std::vector< tk::real > bnded, hull, dea;<--- Shadow variable
  if (g_cfg.get< tag::deactivate >()) {
    nodefieldnames.push_back( "chbnded" );
    bnded.resize( m_u.nunk(), 0.0 );
    for (const auto& [ch,edges] : m_chbndedge) {
      for (const auto& [ed,sint] : edges) {
        bnded[ed[0]] = bnded[ed[1]] = 1.0;
      }
    }
    nodefields.push_back( bnded );

    nodefieldnames.push_back( "activehull" );
    hull.resize( m_u.nunk(), 0.0 );
    for (const auto& [ch,edges] : m_chbndedge) {
      if (m_inactive.count(ch)) {
        for (const auto& [ed,sint] : edges) {
          hull[ed[0]] = hull[ed[1]] = 1.0;
        }
      }
    }
    nodefields.push_back( hull );

    nodefieldnames.push_back( "deactivated" );
    dea.resize( m_u.nunk(), static_cast<tk::real>(m_deactivated) );
    nodefields.push_back( dea );
  }

  Assert( nodefieldnames.size() == nodefields.size(), "Size mismatch" );

  // Surface output

  std::vector< std::string > nodesurfnames;
  std::vector< std::vector< tk::real > > nodesurfs;

  const auto& f = g_cfg.get< tag::fieldout >();

  if (!f.empty()) {
    nodesurfnames.push_back( "density" );
    nodesurfnames.push_back( "velocityx" );
    nodesurfnames.push_back( "velocityy" );
    nodesurfnames.push_back( "velocityz" );
    nodesurfnames.push_back( "energy" );
    nodesurfnames.push_back( "pressure" );

    for (std::size_t c=0; c<ncomp-5; ++c) {
      nodesurfnames.push_back( "c" + std::to_string(c) );
    }

    if (g_cfg.get< tag::deactivate >()) {
      nodesurfnames.push_back( "chbnded" );
      nodesurfnames.push_back( "activehull" );
      nodesurfnames.push_back( "deactivated" );
    }

    if (steady) nodesurfnames.push_back( "mach" );

    auto bnode = tk::bfacenodes( m_bface, m_triinpoel );
    std::set< int > outsets( begin(f), end(f) );
    for (auto sideset : outsets) {
      auto b = bnode.find(sideset);
      if (b == end(bnode)) continue;
      const auto& nodes = b->second;
      auto i = nodesurfs.size();
      auto ns = ncomp + 1;
      if (g_cfg.get< tag::deactivate >()) ns += 3;
      if (steady) ++ns;
      nodesurfs.insert( end(nodesurfs), ns,
                        std::vector< tk::real >( nodes.size() ) );
      std::size_t j = 0;
      for (auto n : nodes) {
        const auto s = m_u[n];
        std::size_t p = 0;
        nodesurfs[i+(p++)][j] = s[0];
        nodesurfs[i+(p++)][j] = s[1]/s[0];
        nodesurfs[i+(p++)][j] = s[2]/s[0];
        nodesurfs[i+(p++)][j] = s[3]/s[0];
        nodesurfs[i+(p++)][j] = s[4]/s[0];
        auto vv = (s[1]*s[1] + s[2]*s[2] + s[3]*s[3])/s[0]/s[0];
        auto ei = s[4]/s[0] - 0.5*vv;
        auto sp = eos::pressure( s[0]*ei );
        nodesurfs[i+(p++)][j] = sp;
        for (std::size_t c=0; c<ncomp-5; ++c) nodesurfs[i+(p++)+c][j] = s[5+c];
        if (g_cfg.get< tag::deactivate >()) {
          nodesurfs[i+(p++)][j] = bnded[n];
          nodesurfs[i+(p++)][j] = hull[n];
          nodesurfs[i+(p++)][j] = dea[n];
        }
        if (steady) {
          nodesurfs[i+(p++)][j] = std::sqrt(vv) / eos::soundspeed( s[0], sp );
        }
        ++j;
      }
    }
  }

  // Send mesh and fields data (solution dump) for output to file
  d->write( d->Inpoel(), d->Coord(), m_bface, tk::remap(m_bnode,d->Lid()),
            m_triinpoel, {}, nodefieldnames, {}, nodesurfnames,
            {}, nodefields, {}, nodesurfs, cb );
}

void
ZalCG::out()
// *****************************************************************************
// Output mesh field data
// *****************************************************************************
{
  auto d = Disc();

  // Time history
  if (d->histiter() or d->histtime() or d->histrange()) {
    auto ncomp = m_u.nprop();
    const auto& inpoel = d->Inpoel();
    std::vector< std::vector< tk::real > > hist( d->Hist().size() );
    std::size_t j = 0;
    for (const auto& p : d->Hist()) {
      auto e = p.get< tag::elem >();        // host element id
      const auto& n = p.get< tag::fn >();   // shapefunctions evaluated at point
      hist[j].resize( ncomp+1, 0.0 );
      for (std::size_t i=0; i<4; ++i) {
        const auto u = m_u[ inpoel[e*4+i] ];
        hist[j][0] += n[i] * u[0];
        hist[j][1] += n[i] * u[1]/u[0];
        hist[j][2] += n[i] * u[2]/u[0];
        hist[j][3] += n[i] * u[3]/u[0];
        hist[j][4] += n[i] * u[4]/u[0];
        auto ei = u[4]/u[0] - 0.5*(u[1]*u[1] + u[2]*u[2] + u[3]*u[3])/u[0]/u[0];
        hist[j][5] += n[i] * eos::pressure( u[0]*ei );
        for (std::size_t c=5; c<ncomp; ++c) hist[j][c+1] += n[i] * u[c];
      }
      ++j;
    }
    d->history( std::move(hist) );
  }

  // Field data
  if (d->fielditer() or d->fieldtime() or d->fieldrange() or m_finished) {
    writeFields( CkCallback(CkIndex_ZalCG::integrals(), thisProxy[thisIndex]) );
  } else {
    integrals();
  }
}

void
ZalCG::integrals()
// *****************************************************************************
// Compute integral quantities for output
// *****************************************************************************
{
  auto d = Disc();

  if (d->integiter() or d->integtime() or d->integrange()) {

    using namespace integrals;
    std::vector< std::map< int, tk::real > > ints( NUMINT );

    // Prepend integral vector with metadata on the current time step:
    // current iteration count, current physical time, time step size
    ints[ ITER ][ 0 ] = static_cast< tk::real >( d->It() );
    ints[ TIME ][ 0 ] = d->T();
    ints[ DT ][ 0 ] = d->Dt();

    // Compute integrals requested for surfaces requested
    const auto& reqv = g_cfg.get< tag::integout_integrals >();
    std::unordered_set< std::string > req( begin(reqv), end(reqv) );
    if (req.count("mass_flow_rate")) {
      for (const auto& [s,sint] : m_surfint) {
        auto& mfr = ints[ MASS_FLOW_RATE ][ s ];<--- Variable 'mfr' is assigned a value that is never used.
        const auto& nodes = sint.first;
        const auto& ndA = sint.second;
        auto n = ndA.data();
        for (auto p : nodes) {
          mfr += n[0]*m_u(p,1) + n[1]*m_u(p,2) + n[2]*m_u(p,3);
          n += 3;
        }
      }
    }

    auto stream = serialize( d->MeshId(), ints );
    d->contribute( stream.first, stream.second.get(), IntegralsMerger,
      CkCallback(CkIndex_Transporter::integrals(nullptr), d->Tr()) );

  } else {

    step();

  }
}

void
ZalCG::evalLB( int nrestart )
// *****************************************************************************
// Evaluate whether to do load balancing
//! \param[in] nrestart Number of times restarted
// *****************************************************************************
{
  auto d = Disc();

  // Detect if just returned from a checkpoint and if so, zero timers and
  // finished flag
  if (d->restarted( nrestart )) m_finished = 0;

  // Load balancing if user frequency is reached or after the second time-step
  if (d->lb()) {

    AtSync();
    if (g_cfg.get< tag::nonblocking >()) dt();

  } else {

    dt();

  }
}

void
ZalCG::evalRestart()
// *****************************************************************************
// Evaluate whether to save checkpoint/restart
// *****************************************************************************
{
  auto d = Disc();

  const auto rsfreq = g_cfg.get< tag::rsfreq >();
  const auto benchmark = g_cfg.get< tag::benchmark >();

  if ( !benchmark && (d->It()) % rsfreq == 0 ) {

    std::vector< std::size_t > meshdata{ /* finished = */ 0, d->MeshId() };
    contribute( meshdata, CkReduction::nop,
      CkCallback(CkReductionTarget(Transporter,checkpoint), d->Tr()) );

  } else {

    evalLB( /* nrestart = */ -1 );

  }
}

void
ZalCG::step()
// *****************************************************************************
// Evaluate whether to continue with next time step
// *****************************************************************************
{
  auto d = Disc();

  // Output one-liner status report to screen
  if (thisIndex == 0) d->status();

  if (not m_finished) {

    evalRestart();

  } else {

    auto meshid = d->MeshId();
    d->contribute( sizeof(std::size_t), &meshid, CkReduction::nop,
                   CkCallback(CkReductionTarget(Transporter,finish), d->Tr()) );

  }
}

#include "NoWarning/zalcg.def.h"