-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadvect.hh
794 lines (612 loc) · 25.1 KB
/
advect.hh
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
/*
* =====================================================================================
*
* Filename: advect.hh
*
* Description: Simple advection routine
*
* Version: 1.0
* Created: 11.06.2018 19:18:06
* Revision: none
* Compiler: gcc
*
* Author: Elias Roland Most (ERM), [email protected]
* Organization: Goethe University Frankfurt
*
* =====================================================================================
*/
#pragma once
#include "storage.hh"
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <memory>
template <bool high_order, typename Tsystem, typename T, typename grid_t,
typename Treconstruct, typename TriemannSolver>
class McCorquodale_FV {
private:
using storage_t = SimpleStorage<grid_t, Tsystem>;
storage_t scratch, scratchL, scratchR, scratchL2, scratchR2, scratchF, scratchC, scratchP;
// ,final_flux;
using TR = Treconstruct;
using TS = Tsystem;
using TRiem = TriemannSolver;
grid_t &grid;
template <int dir, typename Tstorage>
inline void calculate_flux(Tstorage &U) {
static_assert(std::is_same<grid_t, typename Tstorage::grid_t>::value,
"Grids don't match");
// Actual reconstruction step
TR::template reconstruct<TS::ndim, dir>(U, scratchR, scratchL);
//Need to fill aux
TS::fill_aux(scratchR);
TS::fill_aux(scratchL);
if (TS::ndim == 2 && high_order) {
auto correct_rec = [&](auto sL, auto sO) {
std::memset(scratch.U, 0,
U.ndof * Tstorage::nsystem *
sizeof(typename Tstorage::data_t));
// Need to correct with the transverse laplacian
if (dir == 0) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij =
i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
auto const tmp = sL.U[ijp] + sL.U[ijm] - 2. * sL.U[ij];
sO.U[ij] = sL.U[ij] - 1. / 24. * tmp;
};
} else { // dir==1
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij =
i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
auto const tmp = (sL.U[ipj] + sL.U[imj] - 2. * sL.U[ij]);
sO.U[ij] = sL.U[ij] - 1. / 24. * tmp;
};
}; // switch dir
sO.is_primitive = sL.is_primitive;
}; // lambda correct_rec
/// For the high order algorithm need to correct using the transverse
// Laplacian
correct_rec(scratchR, scratchR2);
correct_rec(scratchL, scratchL2);
TS::fill_aux(scratchR2);
TS::fill_aux(scratchL2);
}; // ndim ==2
if (Tsystem::ndim == 3 && high_order) {
assert(!"Not implemented yet");
}
TRiem::template solve<dir>(scratchL, scratchR, scratch);
// Call Riemann solver
if (TS::ndim > 1 && high_order){
TRiem::template solve<dir>(scratchL2, scratchR2, scratchF);
}
//We use scratchF in the rest of the routine...
if(TS::ndim ==1 || !high_order ) scratchF = scratch;
if (Tsystem::ndim == 2 && high_order) {
// Need to correct with the transverse laplacian
if (dir == 0) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij =
i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
auto const tmp = scratch[ijp] + scratch[ijm] - 2. * scratch[ij];
scratchF.U[ij] += 1. / 24. * tmp;
};
} else { // dir==1
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij =
i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
auto const tmp = (scratch[ipj] + scratch[imj] - 2. * scratch[ij]);
scratchF[ij] += 1. / 24. * tmp;
};
}; // switch dir
}
if (Tsystem::ndim == 3 && high_order) {
assert(!"Not implemented yet");
}
/*
//Apply positivity preserving limiter (FIXME For now only to first component by default!)
//U is naturally UR, but UL is U shifted by one!
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
scratchL[i] = U[i-1];
};
}
if (Tsystem::ndim == 2) {
if(dir==0){
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
for (int j = 0; j < U.grid.extent[1]; ++j)
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
scratchL[ij] = U[imj];
}
};
if(dir==1){
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
for (int j = 1; j < U.grid.extent[1]-1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j -1 + U.grid.extent[1] * nv);
scratchL[ij] = U[ijm];
}
};
};
if (Tsystem::ndim == 3 ) {
assert(!"Not implemented yet");
}
HLL_RiemannSolver<TS,true,true>::template solve<dir>(scratchL,U,scratchR);
auto const posp = [&] (auto rLFm, auto rm, auto drm, auto rLFp, auto rp,auto drp, auto epsmin){
T thetam = 1.;
T thetap = 1.;
// thetap = std::min( (epsmin - rLFp)/(rp - rLFp + 1.e-200), 1.0);
// thetam = std::min( (epsmin - rLFm)/(rm - rLFm + 1.e-200), 1.0);
//
if(rp < epsmin)
thetap = std::min( (epsmin - rLFp)/(drp + 1.e-200), 1.0);
if(rm < epsmin)
thetam = std::min( (epsmin - rLFm)/(drm + 1.e-200), 1.0);
auto result = std::max(0.,std::min(thetap,thetam));
// assert( result =>0. && result <=1.0);
return result;
};
auto const acfl = 2.*0.45; //Strict CFL condition!!
if (Tsystem::ndim == 1) {
int const nv = 0; //FIXME!!
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
auto rLFm = scratchC[i-1] - acfl*scratchR[i];
auto rm = scratchC[i-1] - acfl*scratchF[i];
auto drm = - acfl*scratchF[i] + acfl*scratchR[i];
auto rLFp = scratchC[i] + acfl*scratchR[i];
auto rp = scratchC[i] + acfl*scratchF[i];
auto drp = acfl*scratchF[i] - acfl*scratchR[i];
scratchL[i] = posp(rLFm, rm, drm, rLFp, rp,drp, TS::rho_atmo);
};
}
if (Tsystem::ndim == 2) {
int const nv = 0; //FIXME!!
if(dir==0){
for (int j = 0; j < U.grid.extent[1]; ++j)
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
auto rLFm = scratchC[imj] - acfl*scratchR[ij];
auto rm = scratchC[imj] - acfl*scratchF[ij];
auto drm = - acfl*scratchF[ij] + acfl*scratchR[ij];
auto rLFp = scratchC[ij] + acfl*scratchR[ij];
auto rp = scratchC[ij] + acfl*scratchF[ij];
auto drp = acfl*scratchF[ij] - acfl*scratchR[ij];
scratchL[ij] = posp(rLFm, rm,drm, rLFp, rp,drp, TS::rho_atmo);
}
};
if(dir==1){
for (int j = 1; j < U.grid.extent[1]-1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j -1 + U.grid.extent[1] * nv);
auto rLFm = scratchC[ijm] - acfl*scratchR[ij];
auto rm = scratchC[ijm] - acfl*scratchF[ij];
auto drm = - acfl*scratchF[ij] + acfl*scratchR[ij];
auto rLFp = scratchC[ij] + acfl*scratchR[ij];
auto rp = scratchC[ij] + acfl*scratchF[ij];
auto drp = acfl*scratchF[ij] - acfl*scratchR[ij];
scratchL[ij] = posp(rLFm, rm,drm, rLFp, rp, drp, TS::rho_atmo);
}
};
};
if (Tsystem::ndim == 3 ) {
assert(!"Not implemented yet");
}
//Hybridize fluxes!
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
for (int ijk = 0; ijk < U.ndof; ++ijk) {
size_t const ijkO = ijk + U.ndof*nv;
scratchF[ijkO] = scratchL[ijk]*scratchF[ijkO] + (1.-scratchL[ijk])*scratchR[ijkO];
};
*/
/*
MinMod_Reconstruct<false,T>::template reconstruct<TS::ndim, dir>(scratchP, scratchR, scratchL);
HLL_RiemannSolver<TS,true,false>::template solve<dir>(scratchL,scratchR, scratch);
//Test PLUTO fix with 1st order
pluto_flattener<Tsystem>(scratchP,scratchL.U,scratchR.U);
//Hybridize fluxes!
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
for (int ijk = 0; ijk < U.ndof; ++ijk) {
size_t const ijkO = ijk + U.ndof*nv;
// PLUTO works the other way!
scratchF[ijkO] = (1.-scratchL[ijk])*scratchF[ijkO] + scratchL[ijk]*scratch[ijkO];
};
*/
};
public:
McCorquodale_FV<high_order, Tsystem, T, grid_t, Treconstruct, TriemannSolver>(
grid_t &_grid)
: grid(_grid), scratch(_grid), scratchL(_grid), scratchR(_grid),
scratchL2(_grid), scratchR2(_grid), scratchF(_grid), scratchC(_grid), scratchP(_grid) {};
// ,final_flux(_grid){};
template <typename Tstorage> inline void switch_to_volume(Tstorage &U){
if(!high_order) return;
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
scratch[i] = U[i] + 1. / 24. * (U[i + 1] + U[i - 1] - 2. * U[i]);
};
}
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
scratch[ij] = U[ij] + 1. / 24 * (U[ipj] + U[imj] - 2. * U[ij]);
};
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
scratch[ij] += 1. / 24. * (U[ijp] + U[ijm] - 2. * U[ij]);
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
}
//Switch pointers
U = scratch;
};
template <typename Tstorage> inline void switch_to_point(Tstorage &U){
if(!high_order) return;
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
scratch.U[i] =
U.U[i] - 1. / 24. * (U.U[i + 1] + U.U[i - 1] - 2. * U.U[i]);
};
}
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
scratch.U[ij] =
U.U[ij] - 1. / 24 * (U.U[ipj] + U.U[imj] - 2. * U.U[ij]);
};
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
scratch.U[ij] -= 1. / 24. * (U.U[ijp] + U.U[ijm] - 2. * U.U[ij]);
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
}
//Switch pointers
U = scratch;
};
template <typename Tstorage> inline decltype(auto) advect(Tstorage &U, Tstorage & final_flux) {
static_assert(std::is_same<grid_t, typename Tstorage::grid_t>::value,
"Grids don't match");
// Need some scratch space
std::memset(scratch.U, 0,
U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
// Step 0. (only if Tsystem::needs_c2p)
// Compute values at cell centre
if (Tsystem::needs_c2p) {
std::memcpy(scratchC.U,U.U, U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
if(high_order){
switch_to_point(U);
U = scratch;
// Finally compute the primitive values at the centre of the cell
Tsystem::switch_to_prims(U);
Tsystem::switch_to_prims(scratch);
std::memcpy(scratchP.U,U.U, U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
// Now need to translate back
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
scratch.U[i] += 1. / 24. * (U.U[i + 1] + U.U[i - 1] - 2. * U.U[i]);
};
}
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
scratch.U[ij] += 1. / 24 * (U.U[ipj] + U.U[imj] - 2. * U.U[ij]);
};
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
scratch.U[ij] += 1. / 24. * (U.U[ijp] + U.U[ijm] - 2. * U.U[ij]);
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
}
//swap pointers
U=scratch;
/*
Tsystem::switch_to_prims(U);
std::memcpy(scratchP.U,U.U, U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
Tsystem::switch_to_cons(U);
switch_to_point(U);
Tsystem::switch_to_prims(U);
switch_to_volume(U);
*/
}else{
Tsystem::switch_to_prims(U);
std::memcpy(scratchP.U,U.U, U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
}//low order
} // If high_order && needs_c2p
calculate_flux<0>(U);
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = scratchF.grid.gz[0];
ijk < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++ijk) {
int const i = ijk + scratchF.grid.extent[0] * nv;
int const ip = ijk + scratchF.grid.extent[0] * nv + 1;
final_flux[i] = (scratchF[i] - scratchF[ip]) * scratchF.grid.idx[0];
};
};
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = scratchF.grid.gz[1];
j < scratchF.grid.extent[1] - scratchF.grid.gz[1]; ++j)
#pragma omp simd
for (int i = scratchF.grid.gz[0];
i < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++i) {
int const ij =
i + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
int const ipj =
i + 1 + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
final_flux.U[ij] =
(scratchF[ij] - scratchF[ipj]) * scratchF.grid.idx[0];
};
calculate_flux<1>(U);
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = scratchF.grid.gz[1];
j < scratchF.grid.extent[1] - scratchF.grid.gz[1]; ++j)
for (int i = scratchF.grid.gz[0];
i < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++i) {
int const ij =
i + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
int const ijp =
i + scratchF.grid.extent[0] * (j + 1) +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
final_flux.U[ij] +=
(scratchF[ij] - scratchF[ijp]) * scratchF.grid.idx[1];
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
};
//Need to switch back to conservatives!
if (Tsystem::needs_c2p) {
U=scratchC;
} // If high_order && needs_c2p
// return final_flux;
};
};
template <bool high_order, typename Tsystem, typename T, typename grid_t,
typename Treconstruct, typename TriemannSolver>
class DelZanna_FD {
private:
using storage_t = SimpleStorage<grid_t, Tsystem>;
storage_t scratch, scratchL, scratchR, scratchF,scratchC;
// ,final_flux;
using TR = Treconstruct;
using TS = Tsystem;
using TRiem = TriemannSolver;
grid_t &grid;
template <int dir, typename Tstorage>
inline void calculate_flux(Tstorage &U) {
static_assert(std::is_same<grid_t, typename Tstorage::grid_t>::value,
"Grids don't match");
// Actual reconstruction step
TR::template reconstruct<TS::ndim, dir>(U, scratchR, scratchL);
//Need to fill aux
TS::fill_aux(scratchR);
TS::fill_aux(scratchL);
//Riemann solver
TRiem::template solve<dir>(scratchL, scratchR, scratch);
//We use scratchF in the rest of the routine...
if(!high_order) scratchF = scratch;
else{
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = 1; ijk < U.grid.extent[0] - 1; ++ijk) {
const int i = ijk + nv * U.grid.extent[0];
scratchF[i] =
scratch[i] - 1. / 24. * (scratch[i + 1] + scratch[i - 1] - 2. * scratch[i]);
};
}
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 0; j < U.grid.extent[1]; ++j)
#pragma omp simd
for (int i = 1; i < U.grid.extent[0] - 1; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ipj =
i + 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int imj =
i - 1 + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
scratchF[ij] =
scratch[ij] - 1. / 24 * (scratch[ipj] + scratch[imj] - 2. * scratch[ij]);
};
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = 1; j < U.grid.extent[1] - 1; ++j)
for (int i = 0; i < U.grid.extent[0]; ++i) {
const int ij = i + U.grid.extent[0] * (j + U.grid.extent[1] * nv);
const int ijp =
i + U.grid.extent[0] * (j + 1 + U.grid.extent[1] * nv);
const int ijm =
i + U.grid.extent[0] * (j - 1 + U.grid.extent[1] * nv);
scratchF[ij] -= 1. / 24. * (scratch[ijp] + scratch[ijm] - 2. * scratch[ij]);
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
}
}
};
public:
DelZanna_FD<high_order, Tsystem, T, grid_t, Treconstruct, TriemannSolver>(
grid_t &_grid)
: grid(_grid), scratch(_grid), scratchL(_grid), scratchR(_grid),
scratchF(_grid),scratchC(_grid) {};
// ,final_flux(_grid){};
template <typename Tstorage> inline void switch_to_volume(Tstorage &U){
return;
};
template <typename Tstorage> inline void switch_to_point(Tstorage &U){
return;
};
template <typename Tstorage> inline decltype(auto) advect(Tstorage &U, Tstorage & final_flux) {
static_assert(std::is_same<grid_t, typename Tstorage::grid_t>::value,
"Grids don't match");
// Need some scratch space
std::memset(scratch.U, 0,
U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
// Step 0. (only if Tsystem::needs_c2p)
if(Tsystem::needs_c2p){
std::memcpy(scratchC.U,U.U, U.ndof * Tstorage::nsystem * sizeof(typename Tstorage::data_t));
Tsystem::switch_to_prims(U);
}
calculate_flux<0>(U);
if (Tsystem::ndim == 1) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp simd
for (int ijk = scratchF.grid.gz[0];
ijk < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++ijk) {
int const i = ijk + scratchF.grid.extent[0] * nv;
int const ip = ijk + scratchF.grid.extent[0] * nv + 1;
final_flux[i] = (scratchF[i] - scratchF[ip]) * scratchF.grid.idx[0];
};
};
if (Tsystem::ndim == 2) {
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = scratchF.grid.gz[1];
j < scratchF.grid.extent[1] - scratchF.grid.gz[1]; ++j)
#pragma omp simd
for (int i = scratchF.grid.gz[0];
i < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++i) {
int const ij =
i + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
int const ipj =
i + 1 + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
final_flux.U[ij] =
(scratchF[ij] - scratchF[ipj]) * scratchF.grid.idx[0];
};
calculate_flux<1>(U);
for (int nv = 0; nv < Tsystem::num_vars; ++nv)
#pragma omp parallel for
for (int j = scratchF.grid.gz[1];
j < scratchF.grid.extent[1] - scratchF.grid.gz[1]; ++j)
for (int i = scratchF.grid.gz[0];
i < scratchF.grid.extent[0] - scratchF.grid.gz[0]; ++i) {
int const ij =
i + scratchF.grid.extent[0] * j +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
int const ijp =
i + scratchF.grid.extent[0] * (j + 1) +
scratchF.grid.extent[0] * scratchF.grid.extent[1] * nv;
final_flux.U[ij] +=
(scratchF[ij] - scratchF[ijp]) * scratchF.grid.idx[1];
};
};
if (Tsystem::ndim == 3) {
assert(!"Not implemented yet");
};
if (Tsystem::needs_c2p) U=scratchC;
// return final_flux;
};
};