forked from AMReX-Astro/Castro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadiation.cpp
More file actions
2708 lines (2193 loc) · 87 KB
/
Radiation.cpp
File metadata and controls
2708 lines (2193 loc) · 87 KB
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
#include <AMReX_LO_BCTYPES.H>
#include <AMReX_ParmParse.H>
#include <Radiation.H>
#include <RadSolve.H>
#include <rad_util.H>
#include <filt_prim.H>
#include <opacity.H>
#include <format>
#include <iostream>
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace amrex;
Radiation::Solver_Type Radiation::SolverType = Radiation::InvalidSolver;
Real Radiation::radtoE = 0.;
//Real Radiation::radtoJ = 0.;
Real Radiation::Etorad = 0.;
Real Radiation::radfluxtoF = 0.;
int Radiation::do_multigroup = 0;
int Radiation::nGroups = NGROUPS;
int Radiation::accelerate = 1;
int Radiation::rad_hydro_combined = 0;
int Radiation::Er_Lorentz_term = 1;
int Radiation::icomp_lambda = -1;
int Radiation::icomp_kp = -1;
int Radiation::icomp_kr = -1;
int Radiation::icomp_lab_Er = -1;
int Radiation::icomp_lab_Fr = -1;
int Radiation::icomp_com_Fr = -1;
int Radiation::nplotvar = 0;
Vector<std::string> Radiation::plotvar_names;
int Radiation::filter_lambda_T = 0;
int Radiation::filter_lambda_S = 0;
int Radiation::filter_prim_int = 0;
int Radiation::filter_prim_T = 4;
int Radiation::filter_prim_S = 0;
// These physical constants get their values in the Radiation constructor:
Real Radiation::convert_MeV_erg = 0.0;
Real Radiation::clight = 0.0;
Real Radiation::hPlanck = 0.0;
Real Radiation::kBoltz = 0.0;
Real Radiation::Avogadro = 0.0;
Real Radiation::c = 0.0;
Real Radiation::sigma = 0.0;
Real Radiation::aRad = 0.0;
int Radiation::current_group_number = -1;
std::string Radiation::current_group_name = "Radiation";
int Radiation::pure_hydro = 0;
// static initialization, must be called before Castro::variableSetUp
void Radiation::read_static_params()
{
ParmParse pp("radiation");
{
int solver_type = Radiation::SolverType;
pp.get("SolverType", solver_type);
SolverType = static_cast<Solver_Type>(solver_type);
}
if (Radiation::SolverType == Radiation::MGFLDSolver) {
do_multigroup = 1;
}
else {
do_multigroup = 0;
}
// check that we're not using a single group solver if NGROUPS > 1
if (!do_multigroup && Radiation::nGroups > 1) {
amrex::Error("Radiation::nGroups > 1 but using single group solver");
}
if (Radiation::SolverType == Radiation::MGFLDSolver) {
accelerate = 2;
}
pp.query("accelerate", accelerate);
if (Radiation::SolverType == Radiation::SGFLDSolver ||
Radiation::SolverType == Radiation::MGFLDSolver ) {
Radiation::rad_hydro_combined = 1;
pp.query("rad_hydro_combined", rad_hydro_combined);
}
if (SolverType == SGFLDSolver) {
radiation::fspace_advection_type = 1;
if (radiation::comoving) {
Er_Lorentz_term = 0;
}
else {
Er_Lorentz_term = 1;
pp.query("Er_Lorentz_term", Er_Lorentz_term);
}
}
pp.query("filter_lambda_T", filter_lambda_T);
filter_lambda_S = filter_lambda_T - 1;
pp.query("filter_lambda_S", filter_lambda_S);
if (filter_lambda_T > 4) {
amrex::Error("filter_lambda_T > 4");
}
if (filter_lambda_T < 0) {
amrex::Error("filter_lambda_T < 0");
}
if (filter_lambda_T > 0) {
if (filter_lambda_S >= filter_lambda_T) {
amrex::Error("Invalid filter_lambda_S; S must be less than T when T > 0.");
}
}
pp.query("filter_prim_int", filter_prim_int);
pp.query("filter_prim_T", filter_prim_T);
filter_prim_S = filter_prim_T - 1;
pp.query("filter_prim_S", filter_prim_S);
if (filter_prim_T > 4) {
amrex::Error("filter_prim_T > 4");
}
if (filter_prim_T < 0) {
amrex::Error("filter_prim_T < 0");
}
if (filter_prim_T > 0) {
if (filter_prim_S >= filter_prim_T) {
amrex::Error("Invalid filter_prim_S; S must be less than T when T > 0.");
}
}
if (Radiation::SolverType == Radiation::MGFLDSolver) {
Radiation::nGroups = NGROUPS;
// sanity check -- in the old style, we allowed the number of groups to be
// set at compile time, so ensure that, if the user set this, it matches what
// we expect
int test_groups = -1;
pp.query("nGroups", test_groups);
if (test_groups > 0 && test_groups != Radiation::nGroups) {
amrex::Error("you set the number of groups at runtime, but this does not match the compiled value");
}
}
else if (Radiation::SolverType != Radiation::SingleGroupSolver &&
Radiation::SolverType != Radiation::SGFLDSolver) {
amrex::Error("Unknown Radiation::SolverType");
}
// set up the extra plot variables
{
if (radiation::plot_lambda) {
icomp_lambda = plotvar_names.size();
if (!do_multigroup || radiation::limiter == 0) {
plotvar_names.push_back("lambda");
} else {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "lambda" << g;
plotvar_names.push_back(ss.str());
}
}
}
if (radiation::plot_kappa_p) {
icomp_kp = plotvar_names.size();
if (!do_multigroup) {
plotvar_names.push_back("kappa_P");
} else {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "kappa_P" << g;
plotvar_names.push_back(ss.str());
}
}
}
if (radiation::plot_kappa_r) {
icomp_kr = plotvar_names.size();
if (!do_multigroup) {
plotvar_names.push_back("kappa_R");
} else {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "kappa_R" << g;
plotvar_names.push_back(ss.str());
}
}
}
if (radiation::plot_lab_Er) {
icomp_lab_Er = plotvar_names.size();
if (!do_multigroup) {
plotvar_names.push_back("Erlab");
} else {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "Erlab" << g;
plotvar_names.push_back(ss.str());
}
}
}
if (radiation::plot_lab_flux) {
icomp_lab_Fr = plotvar_names.size();
std::string frame = "lab";
Vector<std::string> dimname;
dimname.push_back("x");
dimname.push_back("y");
dimname.push_back("z");
if (!do_multigroup) {
for (int idim=0; idim<AMREX_SPACEDIM; ++idim) {
std::ostringstream ss;
ss << "Fr" << frame << dimname[idim];
plotvar_names.push_back(ss.str());
}
} else {
for (int idim=0; idim<AMREX_SPACEDIM; ++idim) {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "Fr" << frame << g << dimname[idim];
plotvar_names.push_back(ss.str());
}
}
}
}
if (radiation::plot_com_flux) {
icomp_com_Fr = plotvar_names.size();
std::string frame = "com";
Vector<std::string> dimname;
dimname.push_back("x");
dimname.push_back("y");
dimname.push_back("z");
if (!do_multigroup) {
for (int idim=0; idim<AMREX_SPACEDIM; ++idim) {
std::ostringstream ss;
ss << "Fr" << frame << dimname[idim];
plotvar_names.push_back(ss.str());
}
} else {
for (int idim=0; idim<AMREX_SPACEDIM; ++idim) {
for (int g=0; g<nGroups; ++g) {
std::ostringstream ss;
ss << "Fr" << frame << g << dimname[idim];
plotvar_names.push_back(ss.str());
}
}
}
}
nplotvar = plotvar_names.size();
}
amrex::Print() << "radiation initialized, nGroups = " << Radiation::nGroups << std::endl;
}
Radiation::Radiation(Amr* Parent, Castro* castro, int restart)
: parent(Parent)
{
// castro is passed in, rather than obtained from parent, because this
// routine will be called in some cases before any AmrLevels have
// been installed into the parent's array of levels.
ParmParse pp("radiation");
do_sync = 1; pp.query("do_sync", do_sync);
{
clight = C::c_light;
hPlanck = C::hplanck;
kBoltz = C::k_B;
Avogadro = C::n_A;
convert_MeV_erg = 1.e6_rt * C::ev2erg;
aRad = 4.*C::sigma_SB / C::c_light;
c = clight;
sigma = C::sigma_SB;
if (!do_multigroup) {
// In single group and abstract test problems we can play with
// c and sigma independent of physical reality, but messing with
// them in a multigroup problem is likely to be bad.
pp.query("c", c);
pp.query("sigma", sigma);
}
// Set Hypre flux factors here. Since this only occurs once,
// every instance of Hypre must use the same factor (or
// be responsible for changing it internally).
HypreABec::fluxFactor() = c;
HypreMultiABec::fluxFactor() = c;
}
radtoE = 1.0;
// radtoJ = c/(4.*M_PI);
Etorad = 1.0;
radfluxtoF = 1.0;
reltol = 1.e-6; pp.query("reltol", reltol);
if (SolverType == SGFLDSolver || SolverType == MGFLDSolver) {
abstol = 0.0;
}
else {
abstol = 1.e-6;
}
pp.query("abstol", abstol);
maxiter = 50; pp.query("maxiter", maxiter);
miniter = 1; pp.query("miniter", miniter);
convergence_check_type = 0;
pp.query("convergence_check_type", convergence_check_type);
if (SolverType == SGFLDSolver && radiation::limiter == 1) {
amrex::Abort("SGFLDSolver does not support limiter = 1");
}
if (SolverType == MGFLDSolver && radiation::limiter == 1) {
amrex::Abort("MGFLDSolver does not support limiter = 1");
}
inner_update_limiter = 0;
pp.query("inner_update_limiter", inner_update_limiter);
update_opacity = 1000;
if (SolverType == SGFLDSolver || SolverType == MGFLDSolver) {
update_planck = 1000;
update_rosseland = 1000;
update_limiter = 1000;
}
else {
update_planck = 50;
update_rosseland = 50;
update_limiter = 4;
}
pp.query("update_planck", update_planck);
pp.query("update_rosseland", update_rosseland);
pp.query("update_opacity", update_opacity);
pp.query("update_limiter", update_limiter);
dT = 1.0; pp.query("delta_temp", dT);
// for inner iterations of neutrino J equation
relInTol = 1.e-4; pp.query("relInTol", relInTol);
if (SolverType == SGFLDSolver || SolverType == MGFLDSolver) {
absInTol = 0.0;
}
else {
absInTol = 1.e-4;
}
pp.query("absInTol", absInTol);
maxInIter = 30; pp.query("maxInIter", maxInIter);
minInIter = 1; pp.query("minInIter", minInIter);
skipAccelAllowed = 0;
pp.query("skipAccelAllowed", skipAccelAllowed);
matter_update_type = 0;
pp.query("matter_update_type", matter_update_type);
n_bisect = 1000;
pp.query("n_bisect", n_bisect);
dedT_fac = 1.0;
pp.query("dedT_fac", dedT_fac);
inner_convergence_check = 2;
pp.query("inner_convergence_check", inner_convergence_check);
delta_e_rat_dt_tol = 100.0;
pp.query("delta_e_rat_dt_tol", delta_e_rat_dt_tol);
delta_T_rat_dt_tol = 100.0;
pp.query("delta_T_rat_dt_tol", delta_T_rat_dt_tol);
underfac = 1.0; pp.query("underfac", underfac);
use_WiensLaw = 0;
pp.query("use_WiensLaw", use_WiensLaw);
Tf_Wien = -1.0;
pp.query("Tf_Wien", Tf_Wien);
verbose = 0; pp.query("v", verbose); pp.query("verbose", verbose);
do_kappa_stm_emission = 0;
pp.query("do_kappa_stm_emission", do_kappa_stm_emission);
use_dkdT = 0;
pp.query("use_dkdT", use_dkdT);
if (verbose > 2) {
Vector<int> temp;
if (pp.queryarr("spot",temp,0,AMREX_SPACEDIM)) {
IntVect tempi(temp);
spot = tempi;
}
if (ParallelDescriptor::IOProcessor()) std::cout << "Spot: " << spot << std::endl;
}
if (verbose > 0 && ParallelDescriptor::IOProcessor()) {
std::cout << "Creating Radiation object" << std::endl;
}
if (verbose >= 1 && ParallelDescriptor::IOProcessor()) {
std::cout << "processors = " << ParallelDescriptor::NProcs() << std::endl;
std::cout << "do_sync = " << do_sync << std::endl;
std::cout << "c = " << c << std::endl;
std::cout << "sigma = " << sigma << std::endl;
std::cout << "reltol = " << reltol << std::endl;
std::cout << "abstol = " << abstol << std::endl;
std::cout << "maxiter = " << maxiter << std::endl;
std::cout << "relInTol = " << relInTol << std::endl;
std::cout << "absInTol = " << absInTol << std::endl;
std::cout << "maxInIter = " << maxInIter << std::endl;
std::cout << "delta_e_rat_dt_tol = " << delta_e_rat_dt_tol << std::endl;
std::cout << "delta_T_rat_dt_tol = " << delta_T_rat_dt_tol << std::endl;
std::cout << "limiter = " << radiation::limiter << std::endl;
std::cout << "closure = " << radiation::closure << std::endl;
std::cout << "update_limiter = " << update_limiter << std::endl;
std::cout << "update_planck = " << update_planck << std::endl;
std::cout << "update_rosseland = " << update_rosseland << std::endl;
std::cout << "delta_temp = " << dT << std::endl;
std::cout << "underfac = " << underfac << std::endl;
std::cout << "do_multigroup = " << do_multigroup << std::endl;
std::cout << "accelerate = " << accelerate << std::endl;
std::cout << "verbose = " << verbose << std::endl;
if (SolverType == SingleGroupSolver) {
std::cout << "SolverType = 0: SingleGroupSolver " << std::endl;
}
else if (SolverType == SGFLDSolver) {
std::cout << "SolverType = 5: SGFLDSolver " << std::endl;
}
else if (SolverType == MGFLDSolver) {
std::cout << "SolverType = 6: MGFLDSolver " << std::endl;
}
if (SolverType == MGFLDSolver || SolverType == SGFLDSolver) {
std::cout << "rad_hydro_combined = " << rad_hydro_combined << std::endl;
std::cout << "comoving = " << radiation::comoving << std::endl;
}
if (SolverType == MGFLDSolver) {
std::cout << "fspace_advection_type = " << radiation::fspace_advection_type << std::endl;
}
if (SolverType == SGFLDSolver && radiation::comoving == 0) {
std::cout << "Er_Lorentz_term = " << Er_Lorentz_term << std::endl;
}
}
if (do_multigroup) {
get_groups(verbose);
}
else {
// xnu is a dummy for single group
xnu.resize(2, 1.0);
nugroup.resize(1, 1.0);
}
// current implementation of the Radiation boundary condition reads
// incoming flux information in the RadBndry constructor. we just
// set the boundary condition type here:
Vector<int> lo_bc(AMREX_SPACEDIM), hi_bc(AMREX_SPACEDIM);
pp.getarr("lo_bc",lo_bc,0,AMREX_SPACEDIM);
pp.getarr("hi_bc",hi_bc,0,AMREX_SPACEDIM);
for (int i = 0; i < AMREX_SPACEDIM; i++) {
rad_bc.setLo(i,lo_bc[i]);
rad_bc.setHi(i,hi_bc[i]);
if (verbose > 1 && ParallelDescriptor::IOProcessor()) {
std::cout << "dimension " << i << " rad boundary conditions = "
<< lo_bc[i] << ", " << hi_bc[i] << std::endl;
}
}
// size flux register arrays and persistent MultiFabs:
int levels = parent->maxLevel() + 1; // maximum allowable number of levels
flux_cons.resize(levels);
flux_cons_old.resize(levels);
flux_trial.resize(levels);
dflux.resize(levels);
plotvar.resize(levels);
delta_t_old.resize(levels, 0.0);
delta_e_rat_level.resize(levels, 0.0);
delta_T_rat_level.resize(levels, 0.0);
pp.query("pure_hydro", pure_hydro);
}
void Radiation::regrid(int level, const BoxArray& grids, const DistributionMapping& dmap)
{
BL_PROFILE("Radiation::Regrid");
if (verbose > 0 && ParallelDescriptor::IOProcessor()) {
std::cout << "Regridding radiation object at level " << level
<< "..." << std::endl;
}
if (level > 0) {
IntVect crse_ratio = parent->refRatio(level-1);
flux_cons[level].reset(new FluxRegister(grids, dmap, crse_ratio, level, nGroups));
flux_cons[level]->setVal(0.0);
// For deferred sync, flux_cons_old does not need to be defined here.
// It will be set in the deferred_sync_setup routine.
flux_trial[level].reset(new FluxRegister(grids, dmap, crse_ratio, level, nGroups));
flux_trial[level]->setVal(0.0);
}
dflux[level].reset(new MultiFab(grids, dmap, 1, 0));
if (nplotvar > 0) {
plotvar[level].reset(new MultiFab(grids, dmap, nplotvar, 0));
plotvar[level]->setVal(0.0);
}
// This array will not be used on the finest level. I create it here,
// though, in case a finer level is created before this level is next
// regridded:
if (verbose > 1 && ParallelDescriptor::IOProcessor()) {
std::cout << " done" << std::endl;
}
}
void Radiation::close(int level)
{
// Only appropriate when a level disappears, otherwise see regrid:
if (level > parent->finestLevel()) {
if (verbose > 0 && ParallelDescriptor::IOProcessor()) {
std::cout << "Clearing radiation object at level " << level
<< "..." << std::endl;
}
flux_cons[level].reset();
flux_trial[level].reset();
// flux_cons_old is not deleted here because if it exists it still
// has energy in it. It will be deleted once it is finally used.
dflux[level].reset();
plotvar[level].reset();
if (verbose > 1 && ParallelDescriptor::IOProcessor()) {
std::cout << " done" << std::endl;
}
// When a level is closed, then we have to make sure there is
// consistent flux information in level-1 to start the next
// time step. We need to do this if we are in the middle of
// a level-2 timestep, or if there is no level-2. In either
// case the operations in init_flux are appropriate.
BL_ASSERT(level > 0);
int clev = level - 1;
// Check in case more than one level was removed:
if (clev == parent->finestLevel()) {
int ncycle = parent->nCycle(clev);
int iteration = parent->levelSteps(clev);
iteration = (clev > 0) ? iteration % ncycle : iteration;
if (iteration > 0) {
init_flux(clev, ncycle);
}
}
}
}
void Radiation::restart(int level, const BoxArray& grids,
const DistributionMapping& dmap,
const std::string& dir, std::istream& is)
{
//
// With the deferred sync option, we have to restart the rad flux register
//
std::string Path, aString;
do {
is >> aString;
if (aString.find("delta_e_rat") == 0) {
is >> delta_e_rat_level[level];
}
else if (aString.find("delta_T_rat") == 0) {
is >> delta_T_rat_level[level];
}
else {
Path = aString;
}
} while (Path.empty());
//
// Read flux register only if present in the chkfile.
//
std::string Flag;
is >> Flag;
if (Flag == "Present") {
BL_ASSERT(level > 0);
//
// Read delta_t associated with this flux information.
//
is >> delta_t_old[level-1];
//
// Prepend the name of the chkfile directory.
//
std::string FullPathName = dir;
if (!dir.empty() && dir[dir.length()-1] != '/')
FullPathName += '/';
FullPathName += Path;
//
// Input conservation flux register.
//
const IntVect& crse_ratio = parent->refRatio(level-1);
flux_cons_old[level].reset(new FluxRegister(grids, dmap, crse_ratio, level, nGroups));
flux_cons_old[level]->read(FullPathName, is);
}
}
void Radiation::checkPoint(int level,
const std::string& dir,
std::ostream& os,
VisMF::How how)
{
//
// With the deferred sync option, we have to restart the rad flux register
//
char buf[64];
//
// Write deltas to header for timestep control.
//
if (ParallelDescriptor::IOProcessor()) {
int oldprec = os.precision(20);
auto DeltaString = std::format("delta_e_rat_level[{}]= ", level);
os << DeltaString << delta_e_rat_level[level] << '\n';
DeltaString = std::format("delta_T_rat_level[{}]= ", level);
os << DeltaString << delta_T_rat_level[level] << '\n';
os.precision(oldprec);
}
// Path name construction stolen from AmrLevel::checkPoint
std::string Level = std::format("Level_{}", level);
//
// Write name of conservation flux register to header.
//
std::string PathNameInHeader = Level;
PathNameInHeader += "/RadFlux";
if (ParallelDescriptor::IOProcessor()) {
os << PathNameInHeader;
}
if (flux_cons_old[level]) {
//
// Conservation flux register exists.
//
if (ParallelDescriptor::IOProcessor()) {
BL_ASSERT(level > 0);
int oldprec = os.precision(20);
os << " Present " << delta_t_old[level-1] << '\n';
os.precision(oldprec);
}
//
// This is the full pathname for the written FluxRegister.
//
std::string FullPathName = dir;
if (!FullPathName.empty() &&
FullPathName[FullPathName.length()-1] != '/') {
FullPathName += '/';
}
FullPathName += Level;
FullPathName += "/RadFlux";
//
// Output conservation flux register.
//
flux_cons_old[level]->write(FullPathName, os /* , how */ );
}
else {
//
// Conservation flux register does not exist.
//
if (ParallelDescriptor::IOProcessor()) {
os << " Absent\n";
}
}
}
void Radiation::post_init(int level)
{
return;
}
void Radiation::pre_timestep(int level)
{
BL_PROFILE("Radiation::pre_timestep");
int fine_level = parent->finestLevel();
int ncycle = parent->nCycle(level);
static int done = 0;
if (level < fine_level) {
// For deferred sync, we may have moved flux_cons into flux_cons_old
// and not rebuilt flux_cons, so check for that here.
int flevel = level + 1;
if (!flux_cons[flevel]) {
const BoxArray& grids = parent->getLevel(flevel).boxArray();
const DistributionMapping& dmap = parent->getLevel(flevel).DistributionMap();
const IntVect& crse_ratio = parent->refRatio(level);
flux_cons[flevel].reset(new FluxRegister(grids, dmap, crse_ratio, flevel, nGroups));
flux_cons[flevel]->setVal(0.0);
}
}
// If we aren't doing a multilevel solve, we still need to initialize
// dflux and load the flux registers at each level. For a single-level
// calculation this only needs to be done once per run, whether
// at initialization or at restart. We can't trust iteration to
// tell us, since at restart iteration is not 0.
int iteration = parent->levelSteps(level);
iteration = (level > 0) ? iteration % ncycle : done;
if (level < fine_level || iteration == 0) {
init_flux(level, ncycle);
done = 1;
}
}
void Radiation::init_flux(int level, int ncycle)
{
BL_PROFILE("Radiation::init_flux");
if (verbose > 0 && ParallelDescriptor::IOProcessor()) {
std::cout << "Radiation flux initialization at level " << level
<< "..." << std::endl;
}
int fine_level = parent->finestLevel();
dflux[level]->setVal(0.0);
if (level < fine_level) {
flux_cons[level+1]->setVal(0.0);
}
if (verbose > 1 && ParallelDescriptor::IOProcessor()) {
std::cout << " done" << std::endl;
}
}
// Overwrites temperature with exchange term, exch = temp on input:
// This version used by single group, multigroup
void Radiation::compute_exchange(MultiFab& exch,
MultiFab& Er,
MultiFab& fkp, int igroup)
{
BL_PROFILE("Radiation::compute_exchange");
#ifdef _OPENMP
#pragma omp parallel
#endif
for (MFIter mfi(exch, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
const Box& bx = mfi.tilebox();
auto exch_arr = exch[mfi].array();
auto Er_arr = Er[mfi].array();
auto fkp_arr = fkp[mfi].array();
Real lsigma = sigma;
Real lc = c;
amrex::ParallelFor(bx,
[=] AMREX_GPU_HOST_DEVICE (int i, int j, int k)
{
exch_arr(i,j,k) = fkp_arr(i,j,k) * (4.e0_rt * lsigma * std::pow(exch_arr(i,j,k), 4) - lc * Er_arr(i,j,k));
});
}
}
void Radiation::compute_eta(MultiFab& eta, MultiFab& etainv,
MultiFab& state, MultiFab& temp,
MultiFab& fkp, MultiFab& Er,
Real delta_t, Real c,
Real underrel, int lag_planck, int igroup)
{
BL_PROFILE("Radiation::compute_eta");
#ifdef _OPENMP
#pragma omp parallel
#endif
{
FArrayBox c_v;
for (MFIter mfi(eta, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
const Box& bx = mfi.tilebox();
if (lag_planck) {
Array4<Real> const eta_arr = eta.array(mfi);
Array4<Real> const fkp_arr = fkp.array(mfi);
AMREX_PARALLEL_FOR_3D(bx, i, j, k,
{ eta_arr(i,j,k) = fkp_arr(i,j,k); });
}
else {
// This is the only case where we need a direct call for
// Planck mean as a function of temperature.
auto eta_arr = eta[mfi].array();
auto state_arr = state[mfi].array();
const Real nu = nugroup[igroup];
const Real dT_loc = dT;
amrex::ParallelFor(bx,
[=] AMREX_GPU_HOST_DEVICE (int i, int j, int k)
{
Real rho = state_arr(i,j,k,URHO);
Real temp = state_arr(i,j,k,UTEMP) + dT_loc;
Real Ye;
if (NumAux > 0) {
Ye = state_arr(i,j,k,UFX);
} else {
Ye = 0.e0_rt;
}
Real kp, kr;
bool comp_kp = true;
bool comp_kr = false;
opacity(kp, kr, rho, temp, Ye, nu, comp_kp, comp_kr);
eta_arr(i,j,k,igroup) = kp;
});
}
c_v.resize(bx);
Elixir c_v_elix = c_v.elixir();
get_c_v(c_v, temp[mfi], state[mfi], bx);
auto eta_arr = eta[mfi].array();
auto etainv_arr = etainv[mfi].array();
auto frho_arr = state[mfi].array(URHO);
auto temp_arr = temp[mfi].array();
auto c_v_arr = c_v.array();
auto fkp_arr = fkp[mfi].array();
auto Er_arr = Er[mfi].array(igroup);
Real dT_loc = dT;
const Real fac1 = 16.e0_rt * sigma * delta_t;
const Real fac0 = 0.25e0_rt * fac1 / dT;
const Real fac2 = delta_t * c / dT;
amrex::ParallelFor(bx,
[=] AMREX_GPU_HOST_DEVICE (int i, int j, int k)
{
Real d;
if (lag_planck != 0)
{
// assume eta and fkp are the same
d = fac1 * fkp_arr(i,j,k) * std::pow(temp_arr(i,j,k), 3);
}
else
{
d = fac0 * (eta_arr(i,j,k) * std::pow(temp_arr(i,j,k) + dT_loc, 4) -
fkp_arr(i,j,k) * std::pow(temp_arr(i,j,k), 4)) -
fac2 * (eta_arr(i,j,k) - fkp_arr(i,j,k)) * Er_arr(i,j,k);
// alternate form, sometimes worse, sometimes better:
// d = fac1 * fkp_arr(i,j,k) * std::pow(temp_arr(i,j,k), 3) +
// fac0 * (eta_arr(i,j,k) - fkp_arr(i,j,k)) * std::pow(temp(i,j,k), 4) -
// fac2 * (eta_arr(i,j,k) - fkp_arr(i,j,k)) * Er_arr(i,j,k);
// another alternate form (much worse):
// d = fac1 * fkp_arr(i,j,k) * std::pow(temp_arr(i,j,k) + dtTloc, 3) +
// fac0 * (eta_arr(i,j,k) - fkp_arr(i,j,k)) * std::pow(temp(i,j,k) + dT_loc, 4) -
// fac2 * (eta_arr(i,j,k) - fkp_arr(i,j,k)) * Er_arr(i,j,k);
}
Real frc = frho_arr(i,j,k) * c_v_arr(i,j,k) + 1.0e-50_rt;
eta_arr(i,j,k) = d / (d + frc);
etainv_arr(i,j,k) = underrel * frc / (d + frc);
eta_arr(i,j,k) = 1.e0_rt - etainv_arr(i,j,k);
// eta_arr(i,j,k) = 1.e0_rt - underrel * (1.e0_rt - eta_arr(i,j,k));
});
}
}
}
void Radiation::internal_energy_update(Real& relative, Real& absolute,
MultiFab& frhoes,
MultiFab& frhoem,
MultiFab& eta,
MultiFab& etainv,
MultiFab& dflux_old,
MultiFab& dflux_new,
MultiFab& exch,
Real delta_t)
{
BL_PROFILE("Radiation::internal_energy_update");
ReduceOps<ReduceOpMax, ReduceOpMax> reduce_op;
ReduceData<Real, Real> reduce_data(reduce_op);
using ReduceTuple = typename decltype(reduce_data)::Type;
Real theta = 1.0;
#ifdef _OPENMP
#pragma omp parallel
#endif
for (MFIter mfi(eta, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
const Box& bx = mfi.tilebox();
const auto eta_arr = eta[mfi].array();
const auto etainv_arr = etainv[mfi].array();
const auto frhoem_arr = frhoem[mfi].array();
const auto exch_arr = exch[mfi].array();
const auto dfo = dflux_old[mfi].array();
const auto dfn = dflux_new[mfi].array();
auto frhoes_arr = frhoes[mfi].array();
reduce_op.eval(bx, reduce_data,
[=] AMREX_GPU_HOST_DEVICE (int i, int j, int k) -> ReduceTuple
{
Real chg = 0.e0_rt;
Real tot = 0.e0_rt;
Real tmp = eta_arr(i,j,k) * frhoes_arr(i,j,k) +
etainv_arr(i,j,k) *
(frhoem_arr(i,j,k) -
delta_t * ((1.e0_rt - theta) *
(dfo(i,j,k) - dfn(i,j,k)) +
exch_arr(i,j,k)));
chg = std::abs(tmp - frhoes_arr(i,j,k));
tot = std::abs(frhoes_arr(i,j,k));
frhoes_arr(i,j,k) = tmp;
Real absres = chg;
Real relres = chg / (tot + 1.e-50_rt);
return {relres, absres};
});
}
ReduceTuple hv = reduce_data.value();
relative = amrex::get<0>(hv);
absolute = amrex::get<1>(hv);
ParallelDescriptor::ReduceRealMax(relative);
ParallelDescriptor::ReduceRealMax(absolute);
}
void Radiation::internal_energy_update(Real& relative, Real& absolute,
MultiFab& frhoes,
MultiFab& frhoem,
MultiFab& eta,
MultiFab& etainv,
MultiFab& dflux_old,
MultiFab& dflux_new,
MultiFab& exch,
MultiFab& Dterm,
Real delta_t)
{
BL_PROFILE("Radiation::internal_energy_update_d");
relative = 0.0;
absolute = 0.0;
ReduceOps<ReduceOpMax, ReduceOpMax> reduce_op;
ReduceData<Real, Real> reduce_data(reduce_op);
using ReduceTuple = typename decltype(reduce_data)::Type;
const Real theta = 1.0;
const Real tiny = 1.e-50_rt;
#ifdef _OPENMP
#pragma omp parallel
#endif