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 | // *****************************************************************************
/*!
\file src/Inciter/KozCG.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 KozCG: Taylor-Galerkin, FCT, element-based continuous Galerkin
*/
// *****************************************************************************
#include "XystBuildConfig.hpp"
#include "KozCG.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 "Kozak.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::KozCG;
KozCG::KozCG( 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_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_dtp( m_u.nunk(), 0.0 ),
m_tp( m_u.nunk(), g_cfg.get< tag::t0 >() ),
m_finished( 0 ),
m_freezeflow( 1.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
KozCG::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
KozCG::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();
// 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
KozCG::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 );
for (const auto& [ setid, faceids ] : m_bface) { // for all side sets
for (auto f : faceids) { // for all side set triangles
const std::array< std::size_t, 3 >
N{ m_triinpoel[f*3+0], m_triinpoel[f*3+1], m_triinpoel[f*3+2] };
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 A = tk::length( n );
n[0] /= A;
n[1] /= A;
n[2] /= A;
A /= 2.0;
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];
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] * A / 3.0; // bnd-point integral
b[1] += n[1] * A / 3.0;
b[2] += n[2] * A / 3.0;
}
}
}
}
void
KozCG::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
KozCG::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
KozCG::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
KozCG::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
d->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
KozCG::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
KozCG::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
KozCG::streamable()
// *****************************************************************************
// Convert integrals into streamable data structures
// *****************************************************************************
{
// 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;
}
}
// 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
// cppcheck-suppress unusedFunction
KozCG::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_KozCG::start(), thisProxy[thisIndex]) );
} else {
feop_complete();
}
}
void
KozCG::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
KozCG::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();
// 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 ].wait4step();
// Contribute to minimum dt across all chares and advance to next step
contribute( sizeof(tk::real), &mindt, CkReduction::min_double,
CkCallback(CkReductionTarget(KozCG,advance), thisProxy) );
}
void
KozCG::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 );
// Compute rhs
rhs();
}
void
KozCG::rhs()
// *****************************************************************************
// Compute right-hand side
// *****************************************************************************
{
auto d = Disc();
// Compute own portion of right-hand side for all equations
kozak::rhs( d->Inpoel(), d->Coord(), d->T(), d->Dt(), m_tp, m_dtp, m_u,
m_rhs );
// Communicate rhs to other chares on chare-boundary
if (d->NodeCommMap().empty()) {
comrhs_complete();
} else {
const auto& lid = d->Lid();
for (const auto& [c,n] : d->NodeCommMap()) {
decltype(m_rhsc) exp;
for (auto g : n) exp[g] = m_rhs[ tk::cref_find(lid,g) ];
thisProxy[c].comrhs( exp );
}
}
ownrhs_complete();
}
void
KozCG::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 == Disc()->NodeCommMap().size()) {
m_nrhs = 0;
comrhs_complete();
}
}
void
KozCG::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
KozCG::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 );
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];
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 );
for (std::size_t c=0; c<ncomp; ++c) {
auto p = c*2;
auto n = p+1;
tk::real aec[4] = { 0.0, 0.0, 0.0, 0.0 };<--- Shadow variable
for (std::size_t a=0; a<4; ++a) {
for (std::size_t b=0; b<4; ++b) {
auto m = J/120.0 * ((a == b) ? 3.0 : -1.0);
aec[a] += m * ctau * m_u(N[b],c);
}
m_p(N[a],p) += std::max(0.0,aec[a]);
m_p(N[a],n) += std::min(0.0,aec[a]);
}
}
}
// 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()) {
comaec_complete();
} else {
for (const auto& [c,n] : d->NodeCommMap()) {
decltype(m_pc) exp;
for (auto g : n) exp[g] = m_p[ tk::cref_find(lid,g) ];
thisProxy[c].comaec( exp );
}
}
ownaec_complete();
}
void
KozCG::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 == Disc()->NodeCommMap().size()) {
m_naec = 0;
comaec_complete();
}
}
void
KozCG::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();
const auto& vol = d->Vol();
const auto& inpoel = d->Inpoel();
// 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 p = c*2;
auto n = p+1;
m_p(i,p) /= vol[i];
m_p(i,n) /= vol[i];
// low-order solution
m_rhs(i,c) = m_u(i,c) + dt*m_rhs(i,c)/vol[i] - m_p(i,p) - m_p(i,n);
}
}
// 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;
}
}
for (std::size_t e=0; e<inpoel.size()/4; ++e) {
const auto N = inpoel.data() + e*4;
for (std::size_t c=0; c<ncomp; ++c) {
auto alwp = -large;
auto alwn = +large;
for (std::size_t a=0; a<4; ++a) {
if (g_cfg.get< tag::fctclip >()) {
alwp = max( alwp, m_rhs(N[a],c) );
alwn = min( alwn, m_rhs(N[a],c) );
} else {
alwp = max( alwp, max(m_rhs(N[a],c), m_u(N[a],c)) );
alwn = min( alwn, min(m_rhs(N[a],c), m_u(N[a],c)) );
}
}
auto p = c*2;
auto n = p+1;
for (std::size_t a=0; a<4; ++a) {
m_q(N[a],p) = max(m_q(N[a],p), alwp);
m_q(N[a],n) = min(m_q(N[a],n), alwn);
}
}
}
// Communicate allowed limits contributions
if (d->NodeCommMap().empty()) {
comalw_complete();
} else {
for (const auto& [c,n] : d->NodeCommMap()) {
decltype(m_qc) exp;
for (auto g : n) exp[g] = m_q[ tk::cref_find(lid,g) ];
thisProxy[c].comalw( exp );
}
}
ownalw_complete();
}
void
KozCG::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 p = c*2;
auto n = p+1;
q[p] = std::max( q[p], alw[p] );
q[n] = std::min( q[n], alw[n] );
}
}
// When we have heard from all chares we communicate with, this chare is done
if (++m_nalw == Disc()->NodeCommMap().size()) {
m_nalw = 0;
comalw_complete();
}
}
void
KozCG::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 p = c*2;
auto n = p+1;
m_q(i,p) = max( m_q(i,p), alw[p] );
m_q(i,n) = min( m_q(i,n), alw[n] );
}
}
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 p = c*2;
auto n = p+1;
m_q(i,p) -= m_rhs(i,c);
m_q(i,n) -= 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 p = c*2;
auto n = p+1;
auto eps = std::numeric_limits< tk::real >::epsilon();
m_q(i,p) = m_p(i,p) < eps ? 0.0 : min(1.0, m_q(i,p)/m_p(i,p));
m_q(i,n) = m_p(i,n) > -eps ? 0.0 : min(1.0, m_q(i,n)/m_p(i,n));
}
}
// Limited antidiffusive contributions
auto ctau = g_cfg.get< tag::fctdif >();
m_a.fill( 0.0 );
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];
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
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 );
tk::real coef[ncomp], aec[ncomp][4];<--- Shadow variable
for (std::size_t c=0; c<ncomp; ++c) {<--- Assuming condition is false
auto p = c*2;
auto n = p+1;
coef[c] = 1.0;
for (std::size_t a=0; a<4; ++a) {
aec[c][a] = 0.0;
for (std::size_t b=0; b<4; ++b) {
auto m = J/120.0 * ((a == b) ? 3.0 : -1.0);
aec[c][a] += m * ctau * m_u(N[b],c);
}
coef[c] = min(coef[c], aec[c][a] > 0.0 ? m_q(N[a],p) : m_q(N[a],n));
}
}
tk::real cs = 1.0;
for (auto c : fctsys) cs = min( cs, coef[c] );<--- Uninitialized variable: coef<--- Assuming container is not empty
for (auto c : fctsys) coef[c] = cs;
for (std::size_t c=0; c<ncomp; ++c) {
for (std::size_t a=0; a<4; ++a) {
m_a(N[a],c) += coef[c] * aec[c][a];
}
}
}
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(STRICT_GNUC)
#pragma GCC diagnostic pop
#endif
// Communicate limited antidiffusive contributions
if (d->NodeCommMap().empty()) {
comlim_complete();
} else {
for (const auto& [c,n] : d->NodeCommMap()) {
decltype(m_ac) exp;
for (auto g : n) exp[g] = m_a[ tk::cref_find(lid,g) ];
thisProxy[c].comlim( exp );
}
}
ownlim_complete();
}
void
KozCG::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 == Disc()->NodeCommMap().size()) {
m_nlim = 0;
comlim_complete();
}
}
void
KozCG::solve()
// *****************************************************************************
// Compute limit coefficients
// *****************************************************************************
{
auto d = Disc();
const auto npoin = m_u.nunk();
const auto ncomp = m_u.nprop();
const auto& lid = d->Lid();
const auto& vol = d->Vol();
const auto steady = g_cfg.get< tag::steady >();
// Combine own and communicated contributions to limited antidiffusive
// contributions
for (const auto& [g,a] : m_ac) {
auto i = tk::cref_find( 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)/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)/vol[i];
}
}
}
// 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
KozCG::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
// *****************************************************************************
{
if (g_cfg.get< tag::steady >()) {
const auto rc = g_cfg.get< tag::rescomp >() - 1;
Disc()->residual( l2res[rc] );
}
refine();
}
void
KozCG::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
KozCG::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();
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<m_u.nprop(); ++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
KozCG::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();
// Field output
std::vector< std::string > nodefieldnames
{"density", "velocityx", "velocityy", "velocityz", "energy", "pressure"};
if (g_cfg.get< tag::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 (g_cfg.get< tag::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 (g_cfg.get< tag::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 (g_cfg.get< tag::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) );
}
}
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::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::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::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
KozCG::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_KozCG::integrals(), thisProxy[thisIndex]) );
} else {
integrals();
}
}
void
KozCG::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 mass flow rate for surfaces requested
for (const auto& [s,sint] : m_surfint) {
// cppcheck-suppress unreadVariable
auto& mfr = ints[ MASS_FLOW_RATE ][ s ];
const auto& nodes = sint.first;
const auto& ndA = sint.second;
for (std::size_t i=0; i<nodes.size(); ++i) {
auto p = nodes[i];
mfr += ndA[i*3+0] * m_u(p,1)
+ ndA[i*3+1] * m_u(p,2)
+ ndA[i*3+2] * m_u(p,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
KozCG::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
KozCG::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
KozCG::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/kozcg.def.h"
|