forked from AMReX-Astro/Castro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGravity.cpp
More file actions
3750 lines (2955 loc) · 125 KB
/
Gravity.cpp
File metadata and controls
3750 lines (2955 loc) · 125 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 <cmath>
#include <limits>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <AMReX_ParmParse.H>
#include <Gravity.H>
#include <Castro.H>
#include <AMReX_FillPatchUtil.H>
#include <AMReX_MLMG.H>
#include <AMReX_MLPoisson.H>
#include <castro_limits.H>
#include <fundamental_constants.H>
#include <Gravity_util.H>
#include <MGutils.H>
using namespace amrex;
#ifdef AMREX_DEBUG
int Gravity::test_solves = 1;
#else
int Gravity::test_solves = 0;
#endif
Real Gravity::mass_offset = 0.0;
// ************************************************************************************** //
// Ggravity is defined as 4 * pi * G, where G is the gravitational constant.
// In CGS, this constant is currently
// Gconst = 6.67428e-8 cm^3/g/s^2 , which results in
// Ggravity = 83.8503442814844e-8 cm^3/g/s^2
// ************************************************************************************** //
const Real Ggravity = 4.0 * M_PI * C::Gconst;
///
/// Multipole gravity data
///
AMREX_GPU_MANAGED Real multipole::volumeFactor;
AMREX_GPU_MANAGED Real multipole::parityFactor;
AMREX_GPU_MANAGED Real multipole::rmax;
AMREX_GPU_MANAGED Array1D<bool, 0, 2> multipole::doSymmetricAddLo;
AMREX_GPU_MANAGED Array1D<bool, 0, 2> multipole::doSymmetricAddHi;
AMREX_GPU_MANAGED bool multipole::doSymmetricAdd;
AMREX_GPU_MANAGED Array1D<bool, 0, 2> multipole::doReflectionLo;
AMREX_GPU_MANAGED Array1D<bool, 0, 2> multipole::doReflectionHi;
AMREX_GPU_MANAGED Array2D<Real, 0, multipole::lnum_max, 0, multipole::lnum_max> multipole::factArray;
AMREX_GPU_MANAGED Array1D<Real, 0, multipole::lnum_max> multipole::parity_q0;
AMREX_GPU_MANAGED Array2D<Real, 0, multipole::lnum_max, 0, multipole::lnum_max> multipole::parity_qC_qS;
Gravity::Gravity(Amr* Parent, int _finest_level, BCRec* _phys_bc, int _density)
:
parent(Parent),
LevelData(MAX_LEV),
grad_phi_curr(MAX_LEV),
grad_phi_prev(MAX_LEV),
grids(Parent->boxArray()),
dmap(Parent->DistributionMap()),
abs_tol(MAX_LEV),
rel_tol(MAX_LEV),
level_solver_resnorm(MAX_LEV),
volume(MAX_LEV),
area(MAX_LEV),
phys_bc(_phys_bc)
{
amrex::ignore_unused(_finest_level);
AMREX_ALWAYS_ASSERT(parent->maxLevel() < MAX_LEV);
Density = _density;
read_params();
finest_level_allocated = -1;
radial_grav_old.resize(MAX_LEV);
radial_grav_new.resize(MAX_LEV);
radial_mass.resize(MAX_LEV);
radial_vol.resize(MAX_LEV);
#ifdef GR_GRAV
radial_pres.resize(MAX_LEV);
#endif
if (gravity::gravity_type == "PoissonGrav") {
make_mg_bc();
init_multipole_grav();
}
max_rhs = 0.0;
numpts_at_level = -1;
}
Gravity::~Gravity() = default;
void
Gravity::read_params ()
{
static bool done = false;
if (!done)
{
const Geometry& dgeom = DefaultGeometry();
ParmParse pp("gravity");
if ( (gravity::gravity_type != "ConstantGrav") &&
(gravity::gravity_type != "PoissonGrav") &&
(gravity::gravity_type != "MonopoleGrav") )
{
std::cout << "Sorry -- dont know this gravity type" << std::endl;
amrex::Abort("Options are ConstantGrav, PoissonGrav, or MonopoleGrav");
}
#if (AMREX_SPACEDIM == 1)
if (gravity::gravity_type == "PoissonGrav")
{
amrex::Abort(" gravity::gravity_type = PoissonGrav doesn't work well in 1-d -- please set gravity::gravity_type = MonopoleGrav");
}
else if (gravity::gravity_type == "MonopoleGrav" && !(dgeom.IsSPHERICAL()))
{
amrex::Abort("Only use MonopoleGrav in 1D spherical coordinates");
}
else if (gravity::gravity_type == "ConstantGrav" && dgeom.IsSPHERICAL())
{
amrex::Abort("Can't use constant gravity in 1D spherical coordinates");
}
#elif (AMREX_SPACEDIM == 2)
if (gravity::gravity_type == "MonopoleGrav" && dgeom.IsCartesian() )
{
amrex::Abort(" gravity::gravity_type = MonopoleGrav doesn't make sense in 2D Cartesian coordinates");
}
#endif
if (pp.contains("get_g_from_phi") && !gravity::get_g_from_phi && gravity::gravity_type == "PoissonGrav") {
amrex::Print() << "Warning: gravity::gravity_type = PoissonGrav assumes get_g_from_phi is true" << std::endl;
}
int nlevs = parent->maxLevel() + 1;
// Allow run-time input of solver tolerance. If the user
// provides no value, set a reasonable default value on the
// coarse level, and then increase it by ref_ratio**2 as the
// levels get finer to account for the change in the absolute
// scale of the Laplacian. If the user provides one value, use
// that on the coarse level, and increase it the same way for
// the fine levels. If the user provides more than one value,
// we expect them to provide one for every level, and we do
// not apply the ref_ratio effect.
int n_abs_tol = pp.countval("abs_tol");
if (n_abs_tol <= 1) {
Real tol;
if (n_abs_tol == 1) {
pp.get("abs_tol", tol);
} else {
if (dgeom.IsCartesian()) {
tol = 1.e-11;
} else {
tol = 1.e-10;
}
}
abs_tol[0] = tol;
// Account for the fact that on finer levels, the scale of the
// Laplacian changes due to the zone size changing. We assume
// dx == dy == dz, so it is fair to say that on each level the
// tolerance should increase by the factor ref_ratio**2, since
// in absolute terms the Laplacian increases by that ratio too.
// The actual tolerance we'll send in is the effective tolerance
// on the finest level that we solve for.
for (int lev = 1; lev < nlevs; ++lev) {
abs_tol[lev] = abs_tol[lev - 1] * std::pow(parent->refRatio(lev - 1)[0], 2);
}
} else if (n_abs_tol >= nlevs) {
pp.getarr("abs_tol", abs_tol, 0, nlevs);
} else {
amrex::Abort("If you are providing multiple values for abs_tol, you must provide at least one value for every level up to amr.max_level.");
}
// For the relative tolerance, we can again accept a single
// scalar (same for all levels) or one for all levels. The
// default value is zero, so that we only use the absolute
// tolerance. The multigrid always chooses the looser of the
// two criteria in determining whether the solve has
// converged.
// Note that the parameter rel_tol used to be known as ml_tol,
// so if we detect that the user has set ml_tol but not
// rel_tol, we'll accept that for specifying the relative
// tolerance. ml_tol is now considered deprecated and will be
// removed in a future release.
std::string rel_tol_name = "rel_tol";
if (pp.contains("ml_tol")) {
amrex::Warning("The gravity parameter ml_tol has been renamed rel_tol. ml_tol is now deprecated.");
if (!pp.contains("rel_tol")) {
rel_tol_name = "ml_tol";
}
}
int n_rel_tol = pp.countval(rel_tol_name);
if (n_rel_tol <= 1) {
Real tol;
if (n_rel_tol == 1) {
pp.get(rel_tol_name, tol);
} else {
tol = 0.0;
}
for (int lev = 0; lev < MAX_LEV; ++lev) {
rel_tol[lev] = tol;
}
} else if (n_rel_tol >= nlevs) {
pp.getarr(rel_tol_name, rel_tol, 0, nlevs);
} else {
amrex::Abort("If you are providing multiple values for rel_tol, you must provide at least one value for every level up to amr.max_level.");
}
done = true;
}
}
void
Gravity::output_job_info_params(std::ostream& jobInfoFile)
{
#include <gravity_job_info_tests.H>
}
void
Gravity::set_numpts_in_gravity (int numpts)
{
numpts_at_level = numpts;
}
void
Gravity::install_level (int level,
AmrLevel* level_data,
MultiFab& _volume,
MultiFab* _area)
{
if (gravity::verbose > 1) {
amrex::Print() << "Installing Gravity level " << level << '\n';
}
LevelData[level] = level_data;
volume[level] = &_volume;
area[level] = _area;
level_solver_resnorm[level] = 0.0;
const Geometry& geom = level_data->Geom();
if (gravity::gravity_type == "PoissonGrav") {
const DistributionMapping& dm = level_data->DistributionMap();
grad_phi_prev[level].resize(AMREX_SPACEDIM);
for (int n=0; n<AMREX_SPACEDIM; ++n) {
grad_phi_prev[level][n] = std::make_unique<MultiFab>(level_data->getEdgeBoxArray(n),dm,1,1);
}
grad_phi_curr[level].resize(AMREX_SPACEDIM);
for (int n=0; n<AMREX_SPACEDIM; ++n) {
grad_phi_curr[level][n] = std::make_unique<MultiFab>(level_data->getEdgeBoxArray(n),dm,1,1);
}
} else if (gravity::gravity_type == "MonopoleGrav") {
if (!geom.isAllPeriodic())
{
int n1d = gravity::drdxfac*numpts_at_level;
radial_grav_old[level].resize(n1d);
radial_grav_new[level].resize(n1d);
radial_mass[level].resize(n1d);
radial_vol[level].resize(n1d);
#ifdef GR_GRAV
radial_pres[level].resize(n1d);
#endif
}
}
finest_level_allocated = level;
}
std::string Gravity::get_gravity_type()
{
return gravity::gravity_type;
}
int Gravity::get_max_solve_level()
{
return gravity::max_solve_level;
}
int Gravity::NoSync()
{
return gravity::no_sync;
}
int Gravity::DoCompositeCorrection()
{
return gravity::do_composite_phi_correction;
}
int Gravity::test_results_of_solves()
{
return test_solves;
}
Vector<std::unique_ptr<MultiFab> >&
Gravity::get_grad_phi_prev(int level)
{
return grad_phi_prev[level];
}
MultiFab*
Gravity::get_grad_phi_prev_comp(int level, int comp)
{
return grad_phi_prev[level][comp].get();
}
Vector<std::unique_ptr<MultiFab> >&
Gravity::get_grad_phi_curr(int level)
{
return grad_phi_curr[level];
}
void
Gravity::plus_grad_phi_curr(int level, Vector<std::unique_ptr<MultiFab> >& addend)
{
for (int n = 0; n < AMREX_SPACEDIM; n++) {
grad_phi_curr[level][n]->plus(*addend[n],0,1,0);
}
}
void
Gravity::swapTimeLevels (int level)
{
BL_PROFILE("Gravity::swapTimeLevels()");
if (gravity::gravity_type == "PoissonGrav") {
for (int n=0; n < AMREX_SPACEDIM; n++) {
std::swap(grad_phi_prev[level][n], grad_phi_curr[level][n]);
grad_phi_curr[level][n]->setVal(1.e50);
}
}
}
void
Gravity::solve_for_phi (int level,
MultiFab& phi,
const Vector<MultiFab*>& grad_phi,
int is_new)
{
BL_PROFILE("Gravity::solve_for_phi()");
if (gravity::verbose > 1) {
amrex::Print() << " ... solve for phi at level " << level << std::endl;
}
const Real strt = ParallelDescriptor::second();
if (is_new == 0) {
sanity_check(level);
}
Real time;
if (is_new == 1) {
time = LevelData[level]->get_state_data(PhiGrav_Type).curTime();
} else {
time = LevelData[level]->get_state_data(PhiGrav_Type).prevTime();
}
// If we are below the max_solve_level, do the Poisson solve.
// Otherwise, interpolate using a fillpatch from max_solve_level.
if (level <= gravity::max_solve_level) {
Vector<MultiFab*> phi_p(1, &phi);
const auto& g_rhs = get_rhs(level, 1, is_new);
Vector< Vector<MultiFab*> > grad_phi_p(1);
grad_phi_p[0].resize(AMREX_SPACEDIM);
for (int i = 0; i < AMREX_SPACEDIM ; i++) {
grad_phi_p[0][i] = grad_phi[i];
}
Vector<MultiFab*> res_null;
level_solver_resnorm[level] = solve_phi_with_mlmg(level, level,
phi_p,
amrex::GetVecOfPtrs(g_rhs),
grad_phi_p,
res_null,
time);
}
else {
LevelData[level]->FillCoarsePatch(phi, 0, time, PhiGrav_Type, 0, 1, 1);
}
if (gravity::verbose)
{
const int IOProc = ParallelDescriptor::IOProcessorNumber();
amrex::Real end = ParallelDescriptor::second() - strt;
amrex::Real llevel = level;
#ifdef BL_LAZY
Lazy::QueueReduction( [=] () mutable {
#endif
ParallelDescriptor::ReduceRealMax(end,IOProc);
amrex::Print() << "Gravity::solve_for_phi() time = " << end << " on level "
<< llevel << std::endl << std::endl;
#ifdef BL_LAZY
});
#endif
}
}
void
Gravity::gravity_sync (int crse_level, int fine_level, const Vector<MultiFab*>& drho, const Vector<MultiFab*>& dphi)
{
BL_PROFILE("Gravity::gravity_sync()");
// There is no need to do a synchronization if
// we didn't solve on the fine levels.
if (fine_level > gravity::max_solve_level) {
return;
} else {
fine_level = amrex::min(fine_level, gravity::max_solve_level);
}
BL_ASSERT(parent->finestLevel()>crse_level);
if (gravity::verbose > 1 && ParallelDescriptor::IOProcessor()) {
std::cout << " ... gravity_sync at crse_level " << crse_level << '\n';
std::cout << " ... up to finest_level " << fine_level << '\n';
}
const Geometry& crse_geom = parent->Geom(crse_level);
const Box& crse_domain = crse_geom.Domain();
int nlevs = fine_level - crse_level + 1;
// Construct delta(phi) and delta(grad_phi). delta(phi)
// needs a ghost zone for holding the boundary condition
// in the same way that phi does.
Vector<std::unique_ptr<MultiFab> > delta_phi(nlevs);
for (int lev = crse_level; lev <= fine_level; ++lev) {
delta_phi[lev - crse_level] = std::make_unique<MultiFab>(grids[lev], dmap[lev], 1, 1);
delta_phi[lev - crse_level]->setVal(0.0);
}
Vector< Vector<std::unique_ptr<MultiFab> > > ec_gdPhi(nlevs);
for (int lev = crse_level; lev <= fine_level; ++lev) {
ec_gdPhi[lev - crse_level].resize(AMREX_SPACEDIM);
const DistributionMapping& dm = LevelData[lev]->DistributionMap();
for (int n = 0; n < AMREX_SPACEDIM; ++n) {
ec_gdPhi[lev - crse_level][n] = std::make_unique<MultiFab>(LevelData[lev]->getEdgeBoxArray(n), dm, 1, 0);
ec_gdPhi[lev - crse_level][n]->setVal(0.0);
}
}
// Construct a container for the right-hand-side (4 * pi * G * drho + dphi).
// dphi appears in the construction of the boundary conditions because it
// indirectly represents a change in mass on the domain (the mass motion that
// occurs on the fine grid, whose gravitational effects are now indirectly
// being propagated to the coarse grid).
// We will temporarily leave the RHS divided by (4 * pi * G) because that
// is the form expected by the boundary condition routine.
Vector<std::unique_ptr<MultiFab> > g_rhs(nlevs);
for (int lev = crse_level; lev <= fine_level; ++lev) {
g_rhs[lev - crse_level] = std::make_unique<MultiFab>(LevelData[lev]->boxArray(), LevelData[lev]->DistributionMap(), 1, 0);
MultiFab::Copy(*g_rhs[lev - crse_level], *dphi[lev - crse_level], 0, 0, 1, 0);
g_rhs[lev - crse_level]->mult(1.0 / Ggravity);
MultiFab::Add(*g_rhs[lev - crse_level], *drho[lev - crse_level], 0, 0, 1, 0);
}
// Construct the boundary conditions for the Poisson solve.
if (crse_level == 0 && !crse_geom.isAllPeriodic()) {
if (gravity::verbose > 1) {
amrex::Print() << " ... Making bc's for delta_phi at crse_level 0" << std::endl;
}
#if (AMREX_SPACEDIM == 3)
if ( gravity::direct_sum_bcs )
fill_direct_sum_BCs(crse_level,fine_level,amrex::GetVecOfPtrs(g_rhs),*delta_phi[crse_level]);
else {
fill_multipole_BCs(crse_level,fine_level,amrex::GetVecOfPtrs(g_rhs),*delta_phi[crse_level]);
}
#elif (AMREX_SPACEDIM == 2)
fill_multipole_BCs(crse_level,fine_level,amrex::GetVecOfPtrs(g_rhs),*delta_phi[crse_level]);
#else
fill_multipole_BCs(crse_level,fine_level,amrex::GetVecOfPtrs(g_rhs),*delta_phi[crse_level]);
#endif
}
// Restore the factor of (4 * pi * G) for the Poisson solve.
for (int lev = crse_level; lev <= fine_level; ++lev)
g_rhs[lev - crse_level]->mult(Ggravity);
// In the all-periodic case we enforce that the RHS sums to zero.
// We only do this if we're periodic and the coarse level covers the whole domain.
// In principle this could be true for level > 0, so we'll test on whether the number
// of points on the level is equal to the number of points possible on the level.
// Note that since we did the average-down, we can stick with the data on the coarse
// level since the averaging down is conservative.
if (crse_geom.isAllPeriodic() && (grids[crse_level].numPts() == crse_domain.numPts()))
{
// We assume that if we're fully periodic then we're going to be in Cartesian
// coordinates, so to get the average value of the RHS we can divide the sum
// of the RHS by the number of points. This correction should probably be
// volume weighted if we somehow got here without being Cartesian.
Real local_correction = g_rhs[0]->sum() / static_cast<Real>(grids[crse_level].numPts());
if (gravity::verbose > 1) {
amrex::Print() << "WARNING: Adjusting RHS in gravity_sync solve by " << local_correction << '\n';
}
for (int lev = fine_level; lev >= crse_level; --lev) {
g_rhs[lev-crse_level]->plus(-local_correction, 0, 1, 0);
}
}
// Do multi-level solve for delta_phi.
solve_for_delta_phi(crse_level, fine_level,
amrex::GetVecOfPtrs(g_rhs),
amrex::GetVecOfPtrs(delta_phi),
amrex::GetVecOfVecOfPtrs(ec_gdPhi));
// In the all-periodic case we enforce that delta_phi averages to zero.
if (crse_geom.isAllPeriodic() && (grids[crse_level].numPts() == crse_domain.numPts()) ) {
Real local_correction = delta_phi[0]->sum() / static_cast<Real>(grids[crse_level].numPts());
for (int lev = crse_level; lev <= fine_level; ++lev) {
delta_phi[lev - crse_level]->plus(-local_correction, 0, 1, 1);
}
}
// Add delta_phi to phi_new, and grad(delta_phi) to grad(delta_phi_curr) on each level.
// Update the cell-centered gravity too.
for (int lev = crse_level; lev <= fine_level; lev++) {
LevelData[lev]->get_new_data(PhiGrav_Type).plus(*delta_phi[lev - crse_level], 0, 1, 0);
for (int n = 0; n < AMREX_SPACEDIM; n++) {
grad_phi_curr[lev][n]->plus(*ec_gdPhi[lev - crse_level][n], 0, 1, 0);
}
get_new_grav_vector(lev, LevelData[lev]->get_new_data(Gravity_Type),
LevelData[lev]->get_state_data(State_Type).curTime());
}
int is_new = 1;
for (int lev = fine_level-1; lev >= crse_level; --lev)
{
// Average phi_new from fine to coarse level
const IntVect& ratio = parent->refRatio(lev);
amrex::average_down(LevelData[lev+1]->get_new_data(PhiGrav_Type),
LevelData[lev ]->get_new_data(PhiGrav_Type),
0, 1, ratio);
// Average the edge-based grad_phi from finer to coarser level
average_fine_ec_onto_crse_ec(lev, is_new);
// Average down the gravitational acceleration too.
amrex::average_down(LevelData[lev+1]->get_new_data(Gravity_Type),
LevelData[lev ]->get_new_data(Gravity_Type),
0, 1, ratio);
}
}
void
Gravity::GetCrsePhi(int level,
MultiFab& phi_crse,
Real time )
{
BL_PROFILE("Gravity::GetCrsePhi()");
BL_ASSERT(level!=0);
phi_crse.clear();
phi_crse.define(grids[level-1], dmap[level-1], 1, 1); // BUT NOTE we don't trust phi's ghost cells.
const Real t_old = LevelData[level-1]->get_state_data(PhiGrav_Type).prevTime();
const Real t_new = LevelData[level-1]->get_state_data(PhiGrav_Type).curTime();
Real alpha = (time - t_old) / (t_new - t_old);
Real omalpha = 1.0_rt - alpha;
const Real threshold = 1.e-6_rt;
MultiFab const& phi_new = LevelData[level-1]->get_new_data(PhiGrav_Type);
if (std::abs(omalpha) < threshold) {
MultiFab::Copy(phi_crse, phi_new, 0, 0, 1, 1);
}
else if (std::abs(alpha) < threshold) {
// Note we only access the old time if it's actually needed, to guard against
// scenarios where it may not be allocated yet, for example after a restart when
// the old time was not dumped to the checkpoint.
MultiFab const& phi_old = LevelData[level-1]->get_old_data(PhiGrav_Type);
MultiFab::Copy(phi_crse, phi_old, 0, 0, 1, 1);
}
else {
MultiFab const& phi_old = LevelData[level-1]->get_old_data(PhiGrav_Type);
MultiFab::LinComb(phi_crse, alpha, phi_new, 0, omalpha, phi_old, 0, 0, 1, 1);
}
const Geometry& geom = parent->Geom(level-1);
phi_crse.FillBoundary(geom.periodicity());
}
void
Gravity::multilevel_solve_for_new_phi (int level, int finest_level_in)
{
BL_PROFILE("Gravity::multilevel_solve_for_new_phi()");
if (gravity::verbose > 1) {
amrex::Print() << "... multilevel solve for new phi at base level " << level << " to finest level " << finest_level_in << std::endl;
}
const Real strt = ParallelDescriptor::second();
for (int lev = level; lev <= finest_level_in; lev++) {
BL_ASSERT(grad_phi_curr[lev].size()==AMREX_SPACEDIM);
for (int n=0; n<AMREX_SPACEDIM; ++n)
{
grad_phi_curr[lev][n] = std::make_unique<MultiFab>(LevelData[lev]->getEdgeBoxArray(n),
LevelData[lev]->DistributionMap(),1,1);
}
}
int is_new = 1;
actual_multilevel_solve(level, finest_level_in, amrex::GetVecOfVecOfPtrs(grad_phi_curr), is_new);
if (gravity::verbose)
{
const int IOProc = ParallelDescriptor::IOProcessorNumber();
Real end = ParallelDescriptor::second() - strt;
#ifdef BL_LAZY
Lazy::QueueReduction( [=] () mutable {
#endif
ParallelDescriptor::ReduceRealMax(end,IOProc);
amrex::Print() << "Gravity::multilevel_solve_for_new_phi() time = " << end << std::endl << std::endl;
#ifdef BL_LAZY
});
#endif
}
}
void
Gravity::actual_multilevel_solve (int crse_level, int finest_level_in,
const Vector<Vector<MultiFab*> >& grad_phi,
int is_new)
{
BL_PROFILE("Gravity::actual_multilevel_solve()");
for (int ilev = crse_level; ilev <= finest_level_in ; ++ilev) {
sanity_check(ilev);
}
int nlevels = finest_level_in - crse_level + 1;
Vector<MultiFab*> phi_p(nlevels);
for (int ilev = 0; ilev < nlevels; ilev++)
{
int amr_lev = ilev + crse_level;
if (is_new == 1) {
phi_p[ilev] = &LevelData[amr_lev]->get_new_data(PhiGrav_Type);
} else {
phi_p[ilev] = &LevelData[amr_lev]->get_old_data(PhiGrav_Type);
}
}
const auto& g_rhs = get_rhs(crse_level, nlevels, is_new);
Vector<Vector<MultiFab*> > grad_phi_p(nlevels);
for (int ilev = 0; ilev < nlevels; ilev++)
{
int amr_lev = ilev + crse_level;
grad_phi_p[ilev] = grad_phi[amr_lev];
}
Real time;
if (is_new == 1) {
time = LevelData[crse_level]->get_state_data(PhiGrav_Type).curTime();
} else {
time = LevelData[crse_level]->get_state_data(PhiGrav_Type).prevTime();
}
int fine_level = amrex::min(finest_level_in, gravity::max_solve_level);
if (fine_level >= crse_level) {
Vector<MultiFab*> res_null;
solve_phi_with_mlmg(crse_level, fine_level,
phi_p, amrex::GetVecOfPtrs(g_rhs), grad_phi_p, res_null,
time);
// Average phi from fine to coarse level
for (int amr_lev = fine_level; amr_lev > crse_level; amr_lev--)
{
const IntVect& ratio = parent->refRatio(amr_lev-1);
if (is_new == 1)
{
amrex::average_down(LevelData[amr_lev ]->get_new_data(PhiGrav_Type),
LevelData[amr_lev-1]->get_new_data(PhiGrav_Type),
0, 1, ratio);
}
else if (is_new == 0)
{
amrex::average_down(LevelData[amr_lev ]->get_old_data(PhiGrav_Type),
LevelData[amr_lev-1]->get_old_data(PhiGrav_Type),
0, 1, ratio);
}
}
// Average grad_phi from fine to coarse level
for (int amr_lev = fine_level; amr_lev > crse_level; amr_lev--) {
average_fine_ec_onto_crse_ec(amr_lev-1,is_new);
}
}
// For all levels on which we're not doing the solve, interpolate from
// the coarsest level with correct data. Note that since FillCoarsePatch
// fills from the coarse level just below it, we need to fill from the
// lowest level upwards using successive interpolations.
for (int amr_lev = gravity::max_solve_level+1; amr_lev <= finest_level_in; amr_lev++) {
// Interpolate the potential.
if (is_new == 1) {
MultiFab& phi = LevelData[amr_lev]->get_new_data(PhiGrav_Type);
LevelData[amr_lev]->FillCoarsePatch(phi,0,time,PhiGrav_Type,0,1,1);
}
else {
MultiFab& phi = LevelData[amr_lev]->get_old_data(PhiGrav_Type);
LevelData[amr_lev]->FillCoarsePatch(phi,0,time,PhiGrav_Type,0,1,1);
}
// Interpolate the grad_phi.
// Instantiate a bare physical BC function for grad_phi. It doesn't do anything
// since the fine levels for Poisson gravity do not touch the physical boundary.
GradPhiPhysBCFunct gp_phys_bc;
// We need to use a interpolater that works with data on faces.
Interpolater* gp_interp = &face_linear_interp;
// For the BCs, we will use the Gravity_Type BCs for convenience, but these will
// not do anything because we do not fill on physical boundaries.
const Vector<BCRec>& gp_bcs = LevelData[amr_lev]->get_desc_lst()[Gravity_Type].getBCs();
for (int n = 0; n < AMREX_SPACEDIM; ++n) {
amrex::InterpFromCoarseLevel(*grad_phi[amr_lev][n], time, *grad_phi[amr_lev-1][n],
0, 0, 1,
parent->Geom(amr_lev-1), parent->Geom(amr_lev),
gp_phys_bc, 0, gp_phys_bc, 0, parent->refRatio(amr_lev-1),
gp_interp, gp_bcs, 0);
}
}
}
void
Gravity::get_old_grav_vector(int level, MultiFab& grav_vector, Real time)
{
BL_PROFILE("Gravity::get_old_grav_vector()");
int ng = grav_vector.nGrow();
// Fill data from the level below if we're not doing a solve on this level.
if (level > gravity::max_solve_level) {
LevelData[level]->FillCoarsePatch(grav_vector,0,time,Gravity_Type,0,3,ng);
return;
}
// Note that grav_vector coming into this routine always has three components.
// So we'll define a temporary MultiFab with AMREX_SPACEDIM dimensions.
// Then at the end we'll copy in all AMREX_SPACEDIM dimensions from this into
// the outgoing grav_vector, leaving any higher dimensions unchanged.
MultiFab grav(grids[level], dmap[level], AMREX_SPACEDIM, ng);
grav.setVal(0.0,ng);
const Geometry& geom = parent->Geom(level);
if (gravity::gravity_type == "ConstantGrav") {
if (AMREX_SPACEDIM == 2 && geom.Coord() == 2) {
// 2D spherical r-theta, we want g in the radial direction
grav.setVal(gravity::const_grav, 0, 1, ng);
} else {
// Set to constant value in the AMREX_SPACEDIM direction and zero in all others.
grav.setVal(gravity::const_grav, AMREX_SPACEDIM-1, 1, ng);
}
} else if (gravity::gravity_type == "MonopoleGrav") {
const Real prev_time = LevelData[level]->get_state_data(State_Type).prevTime();
make_radial_gravity(level,prev_time,radial_grav_old[level]);
interpolate_monopole_grav(level,radial_grav_old[level],grav);
} else if (gravity::gravity_type == "PoissonGrav") {
amrex::average_face_to_cellcenter(grav, amrex::GetVecOfConstPtrs(grad_phi_prev[level]), geom);
grav.mult(-1.0, ng); // g = - grad(phi)
} else {
amrex::Abort("Unknown gravity_type in get_old_grav_vector");
}
// Do the copy to the output vector.
for (int dir = 0; dir < 3; dir++) {
if (dir < AMREX_SPACEDIM) {
MultiFab::Copy(grav_vector, grav, dir, dir, 1, ng);
} else {
grav_vector.setVal(0.,dir,1,ng);
}
}
#if (AMREX_SPACEDIM > 1)
if (gravity::gravity_type != "ConstantGrav") {
// Fill ghost cells
AmrLevel* amrlev = &parent->getLevel(level) ;
AmrLevel::FillPatch(*amrlev,grav_vector,ng,time,Gravity_Type,0,AMREX_SPACEDIM);
}
#endif
auto* cs = dynamic_cast<Castro*>(&parent->getLevel(level));
if (cs->using_point_mass()) {
MultiFab& phi = LevelData[level]->get_old_data(PhiGrav_Type);
add_pointmass_to_gravity(level,phi,grav_vector);
}
}
void
Gravity::get_new_grav_vector(int level, MultiFab& grav_vector, Real time)
{
BL_PROFILE("Gravity::get_new_grav_vector()");
int ng = grav_vector.nGrow();
// Fill data from the level below if we're not doing a solve on this level.
if (level > gravity::max_solve_level) {
LevelData[level]->FillCoarsePatch(grav_vector,0,time,Gravity_Type,0,3,ng);
return;
}
// Note that grav_vector coming into this routine always has three components.
// So we'll define a temporary MultiFab with AMREX_SPACEDIM dimensions.
// Then at the end we'll copy in all AMREX_SPACEDIM dimensions from this into
// the outgoing grav_vector, leaving any higher dimensions unchanged.
MultiFab grav(grids[level],dmap[level],AMREX_SPACEDIM,ng);
grav.setVal(0.0,ng);
const Geometry& geom = parent->Geom(level);
if (gravity::gravity_type == "ConstantGrav") {
if (AMREX_SPACEDIM == 2 && geom.Coord() == 2) {
// 2D spherical r-theta, we want g in the radial direction
grav.setVal(gravity::const_grav, 0, 1, ng);
} else {
// Set to constant value in the AMREX_SPACEDIM direction
grav.setVal(gravity::const_grav, AMREX_SPACEDIM-1, 1, ng);
}
} else if (gravity::gravity_type == "MonopoleGrav") {
// We always fill radial_grav_new (at every level)
const Real cur_time = LevelData[level]->get_state_data(State_Type).curTime();
make_radial_gravity(level,cur_time,radial_grav_new[level]);
interpolate_monopole_grav(level,radial_grav_new[level],grav);
} else if (gravity::gravity_type == "PoissonGrav") {
amrex::average_face_to_cellcenter(grav, amrex::GetVecOfConstPtrs(grad_phi_curr[level]), geom);
grav.mult(-1.0, ng); // g = - grad(phi)
} else {
amrex::Abort("Unknown gravity_type in get_new_grav_vector");
}
// Do the copy to the output vector.
for (int dir = 0; dir < 3; dir++) {
if (dir < AMREX_SPACEDIM) {
MultiFab::Copy(grav_vector, grav, dir, dir, 1, ng);
} else {
grav_vector.setVal(0.,dir,1,ng);
}
}
#if (AMREX_SPACEDIM > 1)
if (gravity::gravity_type != "ConstantGrav" && ng>0) {
// Fill ghost cells
AmrLevel* amrlev = &parent->getLevel(level) ;
AmrLevel::FillPatch(*amrlev,grav_vector,ng,time,Gravity_Type,0,AMREX_SPACEDIM);
}
#endif
auto* cs = dynamic_cast<Castro*>(&parent->getLevel(level));
if (cs->using_point_mass()) {