forked from ruohai0925/IAMReX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavierStokes.cpp
More file actions
2974 lines (2558 loc) · 94.1 KB
/
NavierStokes.cpp
File metadata and controls
2974 lines (2558 loc) · 94.1 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
// SPDX-FileCopyrightText: 1997 - 2023 Berkeley Lab; 2023 - 2025 Yadong Zeng<zdsjtu@gmail.com> & ZhuXu Li<1246206018@qq.com>
//
// SPDX-License-Identifier: LicenseRef-OpenSource
// Modified from IAMR, originally developed at Lawrence Berkeley National Lab.
// Original source: https://github.com/AMReX-Fluids/IAMR
#include <unistd.h>
#include <AMReX_Geometry.H>
#include <AMReX_Extrapolater.H>
#include <AMReX_ParmParse.H>
#include <AMReX_buildInfo.H>
#include <AMReX_BLProfiler.H>
#include <NavierStokes.H>
#include <NS_util.H>
#include <iamr_constants.H>
#include <NS_LS.H>
#include <NS_kernels.H>
#ifdef AMREX_PARTICLES
#ifdef PARTICLE_PARALLEL
#include "DiffusedIB_Parallel.h"
#else
#include "DiffusedIB.H"
#endif
#endif
#ifdef BL_USE_VELOCITY
#include <AMReX_DataServices.H>
#include <AMReX_AmrData.H>
#endif
#ifdef AMREX_USE_EB
#include <AMReX_EBMultiFabUtil.H>
#endif
using namespace amrex;
int NavierStokes::set_plot_coveredCell_val = 1;
namespace
{
bool initialized = false;
}
Vector<AMRErrorTag> NavierStokes::errtags;
GpuArray<GpuArray<Real, NavierStokes::NUM_STATE_MAX>, AMREX_SPACEDIM*2> NavierStokes::m_bc_values;
void
NavierStokes::Initialize ()
{
if (initialized) return;
NavierStokesBase::Initialize();
//
// Set number of state variables.
//
NUM_STATE = Density + 1;
Tracer = NUM_STATE++;
if (do_trac2)
Tracer2 = NUM_STATE++;
if (do_temp)
Temp = NUM_STATE++;
//
// ls related
//
if (do_phi)
phicomp = NUM_STATE++;
if (verbose)
amrex::Print() << "do_phi, phicomp, NUM_STATE " << do_phi << " " << phicomp << " " << NUM_STATE << std::endl;
//
// ls related
//
// NUM_STATE_MAX is defined in NavierStokes.H
// to be AMREX_SPACEDIM + 5 (for Density, 2 scalars, Temp, ls)
AMREX_ALWAYS_ASSERT(NUM_STATE <= NUM_STATE_MAX);
NUM_SCALARS = NUM_STATE - Density;
NavierStokes::Initialize_bcs();
NavierStokes::Initialize_diffusivities();
amrex::ExecOnFinalize(NavierStokes::Finalize);
initialized = true;
}
void
NavierStokes::Initialize_bcs ()
{
//
// Default BC values
//
int ntrac = do_trac2 ? 2 : 1;
for (OrientationIter face; face; ++face)
{
int ori = int(face());
AMREX_D_TERM(m_bc_values[ori][0] = 0.0;,
m_bc_values[ori][1] = 0.0;,
m_bc_values[ori][2] = 0.0;);
m_bc_values[ori][Density] = 1.0;
for ( int nc = 0; nc < ntrac; nc++ )
m_bc_values[ori][Tracer+nc] = 0.0;
if (do_temp)
m_bc_values[ori][Temp] = 1.0;
//
// ls related
//
if (do_phi)
m_bc_values[ori][phicomp] = 0.0;
}
ParmParse pp("ns");
//
// Check for integer BC type specification in inputs file (older style)
//
if ( pp.contains("lo_bc") )
{
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++)
{
phys_bc.setLo(i,lo_bc[i]);
phys_bc.setHi(i,hi_bc[i]);
}
}
//
// Read string BC specifications and BC values
//
{
int bc_tmp[2*AMREX_SPACEDIM];
auto f = [&bc_tmp] (std::string const& bcid, Orientation ori)
{
ParmParse pbc(bcid);
std::string bc_type_in = "null";
pbc.query("type", bc_type_in);
std::string bc_type = amrex::toLower(bc_type_in);
if (bc_type == "no_slip_wall" or bc_type == "nsw"
or phys_bc.data()[ori] == PhysBCType::noslipwall)
{
amrex::Print() << bcid <<" set to no-slip wall.\n";
bc_tmp[ori] = PhysBCType::noslipwall;
// Note that m_bc_velocity defaults to 0 above so we are ok if
// queryarr finds nothing
std::vector<Real> v;
if (pbc.queryarr("velocity", v, 0, AMREX_SPACEDIM)) {
// Here we make sure that we only use the tangential components
// of a specified velocity field -- the wall is not allowed
// to move in the normal direction
v[ori.coordDir()] = 0.0;
for (int i=0; i<AMREX_SPACEDIM; i++){
m_bc_values[ori][Xvel+i] = v[i];
}
}
}
else if (bc_type == "slip_wall" or bc_type == "sw")
{
amrex::Print() << bcid <<" set to slip wall.\n";
bc_tmp[ori] = PhysBCType::slipwall;
// These values are set by default above -
// note that we only actually use the zero value for the normal direction;
// the tangential components are set to be first order extrap
// m_bc_velocity[ori] = {0.0, 0.0, 0.0};
}
else if (bc_type == "mass_inflow" or bc_type == "mi"
or phys_bc.data()[ori] == PhysBCType::inflow)
{
amrex::Print() << bcid << " set to mass inflow.\n";
bc_tmp[ori] = PhysBCType::inflow;
std::vector<Real> v;
if (pbc.queryarr("velocity", v, 0, AMREX_SPACEDIM)) {
for (int i=0; i<AMREX_SPACEDIM; i++){
m_bc_values[ori][Xvel+i] = v[i];
}
}
pbc.query("density", m_bc_values[ori][Density]);
pbc.query("tracer", m_bc_values[ori][Tracer]);
if (do_trac2) {
if ( pbc.countval("tracer") > 1 )
amrex::Abort("NavierStokes::Initialize_specific: Please set tracer 2 inflow bc value with it's own entry in inputs file, e.g. xlo.tracer2 = bc_value");
pbc.query("tracer2", m_bc_values[ori][Tracer2]);
}
if (do_temp)
pbc.query("temp", m_bc_values[ori][Temp]);
}
else if (bc_type == "pressure_inflow" or bc_type == "pi")
{
amrex::Abort("NavierStokes::Initialize_specific: Pressure inflow boundary condition not yet implemented. If needed for your simulation, please contact us.");
// amrex::Print() << bcid << " set to pressure inflow.\n";
// bc_tmp[ori] = PhysBCType::pressure_inflow;
// pbc.get("pressure", m_bc_pressure[ori]);
}
else if (bc_type == "pressure_outflow" or bc_type == "po"
or phys_bc.data()[ori] == PhysBCType::outflow)
{
amrex::Print() << bcid << " set to pressure outflow.\n";
bc_tmp[ori] = PhysBCType::outflow;
//pbc.query("pressure", m_bc_pressure[ori]);
Real tmp = 0.;
pbc.query("pressure", tmp);
if ( tmp != 0. )
amrex::Abort("NavierStokes::Initialize_specific: Pressure outflow boundary condition != 0 not yet implemented. If needed for your simulation, please contact us.");
}
else if (bc_type == "symmetry" or bc_type == "sym")
{
amrex::Print() << bcid <<" set to symmetry.\n";
bc_tmp[ori] = PhysBCType::symmetry;
}
else
{
bc_tmp[ori] = BCType::bogus;
}
if ( DefaultGeometry().isPeriodic(ori.coordDir()) ) {
if (bc_tmp[ori] == BCType::bogus || phys_bc.data()[ori] == PhysBCType::interior) {
bc_tmp[ori] = PhysBCType::interior;
} else {
std::cerr << " Wrong BC type for periodic boundary at "
<< bcid << ". Please correct inputs file."<<std::endl;
amrex::Abort();
}
}
if ( bc_tmp[ori] == BCType::bogus && phys_bc.data()[ori] == BCType::bogus ) {
std::cerr << " No valid BC type specified for "
<< bcid << ". Please correct inputs file."<<std::endl;
amrex::Abort();
}
if ( bc_tmp[ori] != BCType::bogus && phys_bc.data()[ori] != BCType::bogus
&& bc_tmp[ori] != phys_bc.data()[ori] ) {
std::cerr<<" Multiple conflicting BCs specified for "
<< bcid << ": "<<bc_tmp[ori]<<", "<<phys_bc.data()[ori]
<<". Please correct inputs file."<<std::endl;
amrex::Abort();
}
};
f("xlo", Orientation(Direction::x,Orientation::low));
f("xhi", Orientation(Direction::x,Orientation::high));
f("ylo", Orientation(Direction::y,Orientation::low));
f("yhi", Orientation(Direction::y,Orientation::high));
#if (AMREX_SPACEDIM == 3)
f("zlo", Orientation(Direction::z,Orientation::low));
f("zhi", Orientation(Direction::z,Orientation::high));
#endif
if ( phys_bc.lo(0) == BCType::bogus )
{
//
// Then valid BC types must be in bc_tmp.
// Load them into phys_bc.
//
for (int dir = 0; dir < AMREX_SPACEDIM; dir++)
{
phys_bc.setLo(dir,bc_tmp[dir]);
phys_bc.setHi(dir,bc_tmp[dir+AMREX_SPACEDIM]);
}
} // else, phys_bc already has valid BC types
}
//
// This checks for RZ and makes sure phys_bc is consistent with that.
//
read_geometry();
}
void
NavierStokes::Initialize_diffusivities ()
{
//
// Read viscous/diffusive coeffs.
//
ParmParse pp("ns");
const int n_vel_visc_coef = pp.countval("vel_visc_coef");
const int n_temp_cond_coef = pp.countval("temp_cond_coef");
const int n_scal_diff_coefs = pp.countval("scal_diff_coefs");
if (n_vel_visc_coef != 1)
amrex::Abort("NavierStokesBase::Initialize(): Only one vel_visc_coef allowed");
if (do_temp && n_temp_cond_coef != 1)
amrex::Abort("NavierStokesBase::Initialize(): Only one temp_cond_coef allowed");
//
// ls related
//
if (do_phi==0) {
if (n_scal_diff_coefs+n_temp_cond_coef != NUM_SCALARS-1)
amrex::Abort("NavierStokesBase::Initialize(): One scal_diff_coef required for each tracer");
}
visc_coef.resize(NUM_STATE);
is_diffusive.resize(NUM_STATE);
pp.get("vel_visc_coef",visc_coef[0]);
for (int i = 1; i < AMREX_SPACEDIM; i++)
visc_coef[i] = visc_coef[0];
//
// Here we set the coefficient for density, which does not diffuse.
//
visc_coef[Density] = -1;
//
// Set the coefficients for the scalars, temperature.
//
Vector<Real> scal_diff_coefs(n_scal_diff_coefs);
pp.getarr("scal_diff_coefs",scal_diff_coefs,0,n_scal_diff_coefs);
int scalId = Density;
for (int i = 0; i < n_scal_diff_coefs; i++)
{
visc_coef[++scalId] = scal_diff_coefs[i];
}
//
// Set the coefficient for temperature.
//
if (do_temp)
{
pp.get("temp_cond_coef",visc_coef[++scalId]);
}
//
// ls related
//
if (do_phi)
{
visc_coef[phicomp] = -1;
}
}
void
NavierStokes::Finalize ()
{
initialized = false;
}
NavierStokes::NavierStokes () = default;
NavierStokes::NavierStokes (Amr& papa,
int lev,
const Geometry& level_geom,
const BoxArray& bl,
const DistributionMapping& dm,
Real time)
:
NavierStokesBase(papa,lev,level_geom,bl,dm,time)
{ }
//
// This function initializes the State and Pressure with data.
//
void
NavierStokes::initData ()
{
//
// Initialize the state and the pressure.
//
prob_initData();
//
// Initialize GradP
//
computeGradP(state[Press_Type].curTime());
//
// Initialize averaging, if using
//
if (avg_interval > 0){
MultiFab& Save_new = get_new_data(Average_Type);
Save_new.setVal(0.);
}
#ifdef BL_USE_VELOCITY
{
//
// We want to add the velocity from the supplied plotfile
// to what we already put into the velocity field via FORT_INITDATA.
//
// This code has a few drawbacks. It assumes that the physical
// domain size of the current problem is the same as that of the
// one that generated the pltfile. It also assumes that the pltfile
// has at least as many levels (with the same refinement ratios) as does
// the current problem. If either of these are false this code is
// likely to core dump.
//
ParmParse pp("ns");
std::string velocity_plotfile;
pp.query("velocity_plotfile", velocity_plotfile);
std::string velocity_plotfile_xvel_name = "x_velocity";
pp.query("velocity_plotfile_xvel_name", velocity_plotfile_xvel_name);
Real velocity_plotfile_scale(1.0);
pp.query("velocity_plotfile_scale",velocity_plotfile_scale);
if (!velocity_plotfile.empty())
{
Print() << "initData: reading data from: " << velocity_plotfile << " ("
<< velocity_plotfile_xvel_name << ")" << '\n';
DataServices::SetBatchMode();
Amrvis::FileType fileType(Amrvis::NEWPLT);
DataServices dataServices(velocity_plotfile, fileType);
if (!dataServices.AmrDataOk())
//
// This calls ParallelDescriptor::EndParallel() and exit()
//
DataServices::Dispatch(DataServices::ExitRequest, NULL);
AmrData& amrData = dataServices.AmrDataRef();
Vector<std::string> plotnames = amrData.PlotVarNames();
int idX = -1;
for (int i = 0; i < plotnames.size(); ++i)
if (plotnames[i] == velocity_plotfile_xvel_name) idX = i;
if (idX == -1)
Abort("Could not find velocity fields in supplied velocity_plotfile");
else
Print() << "Found " << velocity_plotfile_xvel_name << ", idX = " << idX << '\n';
MultiFab& S_new = get_new_data(State_Type);
MultiFab tmp(S_new.boxArray(), S_new.DistributionMap(), 1, 0);
for (int i = 0; i < AMREX_SPACEDIM; i++)
{
amrData.FillVar(tmp, level, plotnames[idX+i], 0);
MultiFab::Saxpy(S_new, velocity_plotfile_scale, tmp, 0, Xvel+i, 1, 0);
amrData.FlushGrids(idX+i);
}
Print() << "initData: finished init from velocity_plotfile" << '\n';
}
}
#endif /*BL_USE_VELOCITY*/
#ifdef AMREX_USE_EB
//
// Perform redistribution on initial fields
// This changes the input velocity fields
//
if (redistribution_type == "StateRedist") {
InitialRedistribution();
}
//
// Make sure EB covered cell are set, and that it's not zero, as we
// sometimes divide by rho.
//
{
MultiFab& S_new = get_new_data(State_Type);
EB_set_covered(S_new, COVERED_VAL);
//
// In some cases, it may be necessary for the covered cells to
// contain a value typical (or around the same order of magnitude)
// to the uncovered cells (e.g. if code to compute variable
// viscosity fails for COVERED_VAL).
//
// set_body_state(S_new);
}
#endif
//
// Make rho MFs with filled ghost cells
// Not really sure why these are needed as opposed to just filling the
// the ghost cells in state and using that.
//
make_rho_prev_time();
make_rho_curr_time();
//
// Initialize divU and dSdt.
//
if (have_divu)
{
const Real dt = 1.0;
const Real dtin = -1.0; // Dummy value denotes initialization.
const Real curTime = state[Divu_Type].curTime();
MultiFab& Divu_new = get_new_data(Divu_Type);
state[State_Type].setTimeLevel(curTime,dt,dt);
//Make sure something reasonable is in diffn_ec
calcDiffusivity(curTime);
calc_divu(curTime,dtin,Divu_new);
if (have_dsdt)
get_new_data(Dsdt_Type).setVal(0);
}
#ifdef AMREX_PARTICLES
if (do_nspc) {
initParticleData ();
}
#endif
}
//
// Build/fill any additional data structures after restart.
//
void
NavierStokes::post_restart ()
{
NavierStokesBase::post_restart();
//Get probtype, ub, and shearrate; NS_bcfill.H may expect them.
ParmParse pp("prob");
pp.query("probtype",probtype);
pp.query("ub",ub);
pp.query("shearrate",shearrate);
}
//
// ADVANCE FUNCTIONS
//
//
// This function ensures that the multifab registers and boundary
// flux registers needed for syncing the composite grid
//
// u_mac, Vsync, Ssync, rhoavg, fr_adv, fr_visc
//
// are initialized to zero. In general these quantities
// along with the pressure sync registers (sync_reg) and
// advective velocity registers (mac_reg) are compiled by first
// setting them to the coarse value acquired during a coarse timestep
// and then incrementing in the fine values acquired during the
// subcycled fine timesteps. This compilation procedure occurs in
// different parts for different quantities
//
// * u_mac is set in predict_velocity and mac_project.
// * fr_adv, fr_visc are set in velocity_advect and scalar_advect
// * Vsync, Ssync are set in subcycled calls to post_timestep
// * mac_reg is set in mac_project
// * sync_reg is set in level_project
// * rhoavg, pavg are set in advance_setup and advance
//
// After these quantities have been compiled during a coarse
// timestep and subcycled fine timesteps. The post_timestep function
// uses them to sync the fine and coarse levels. If the coarse level
// is not the base level, post_timestep modifies the next coarsest levels
// registers appropriately.
//
// Note :: There is a little ambiguity as to which level owns the
// boundary flux registers. The Multifab registers are quantities
// sized by the coarse level BoxArray and belong to the coarse level.
// The fine levels own the boundary registers, since they are sized by
// the boundaries of the fine level BoxArray.
//
//
// Compute a timestep at a level. Return largest safe timestep.
//
Real
NavierStokes::advance (Real time,
Real dt,
int iteration,
int ncycle)
{
BL_PROFILE("NavierStokes::advance()");
//if (verbose)
//{
Print() << "Advancing grids at level " << level
<< " : starting time = " << time
<< " with dt = " << dt
<< std::endl;
//}
advance_setup(time,dt,iteration,ncycle);
amrex::Real dt_test = 0.0;
if (isolver==0) {
dt_test = advance_semistaggered_twophase_ls(time,dt,iteration,ncycle);
}
else if(isolver==1 && do_diffused_ib==1) {
#ifdef AMREX_PARTICLES
dt_test = advance_semistaggered_fsi_diffusedib(time,dt,iteration,ncycle);
#endif
}
else if (isolver==2) { // To be implemented
dt_test = advance_semistaggered_twophase_phasefield(time,dt,iteration,ncycle);
}
else{
amrex::Abort("Wrong isolver");
}
//
// Clean up after the predicted value at t^n+1.
// Estimate new timestep from umac cfl.
//
advance_cleanup(iteration,ncycle);
//if (verbose)
//{
Print() << "NavierStokes::advance(): exiting." << std::endl;
printMaxValues();
//}
return dt_test; // Return estimate of best new timestep.
}
//
// This routine advects the scalars
//
void
NavierStokes::scalar_advection (Real dt,
int fscalar,
int lscalar)
{
BL_PROFILE("NavierStokes::scalar_advection()");
if (advect_and_update_scalar) {
if (verbose) Print() << "... advect scalars\n";
//
// Get simulation parameters.
//
const int num_scalars = lscalar - fscalar + 1;
const Real prev_time = state[State_Type].prevTime();
// divu
std::unique_ptr<MultiFab> divu_fp(getDivCond(nghost_force(),prev_time));
//
// Start FillPatchIterator block
//
MultiFab forcing_term( grids, dmap, num_scalars, nghost_force(),MFInfo(),Factory());
FillPatchIterator S_fpi(*this,forcing_term,nghost_state(),prev_time,State_Type,fscalar,num_scalars);
MultiFab& Smf=S_fpi.get_mf();
// Floor small values of states to be extrapolated
floor(Smf);
if ( advection_scheme == "Godunov_PLM" || advection_scheme == "Godunov_PPM" || advection_scheme == "BDS")
{
MultiFab visc_terms(grids,dmap,num_scalars,nghost_force(),MFInfo(),Factory());
FillPatchIterator U_fpi(*this,visc_terms,nghost_state(),prev_time,State_Type,Xvel,AMREX_SPACEDIM);
const MultiFab& Umf=U_fpi.get_mf();
{
std::unique_ptr<MultiFab> dsdt(getDsdt(nghost_force(),prev_time));
MultiFab::Saxpy(*divu_fp, 0.5*dt, *dsdt, 0, 0, 1, nghost_force());
}
// Compute viscous term
if (be_cn_theta != 1.0)
getViscTerms(visc_terms,fscalar,num_scalars,prev_time);
else
visc_terms.setVal(0.0,1);
#ifdef _OPENMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
for (MFIter S_mfi(Smf,TilingIfNotGPU()); S_mfi.isValid(); ++S_mfi)
{
// Box for forcing terms
auto const force_bx = S_mfi.growntilebox(nghost_force());
if (getForceVerbose)
{
Print() << "---" << '\n' << "C - scalar advection:" << '\n'
<< " Calling getForce..." << '\n';
}
getForce(forcing_term[S_mfi],force_bx,fscalar,num_scalars,
prev_time,Umf[S_mfi],Smf[S_mfi],0,S_mfi);
for (int n=0; n<num_scalars; ++n)
{
auto const& tf = forcing_term.array(S_mfi,n);
auto const& visc = visc_terms.const_array(S_mfi,n);
auto const& rho = Smf.const_array(S_mfi); //Previous time, nghost_state() grow cells filled. It's always true that nghost_state > nghost_force.
if ( do_temp && n+fscalar==Temp )
{
//
// Solving
// dT/dt + U dot del T = ( del dot lambda grad T + H_T ) / (rho c_p)
// with tforces = H_T/c_p (since it's always density-weighted), and
// visc = del dot mu grad T, where mu = lambda/c_p
//
amrex::ParallelFor(force_bx, [tf, visc, rho]
AMREX_GPU_DEVICE (int i, int j, int k) noexcept
{ tf(i,j,k) = ( tf(i,j,k) + visc(i,j,k) ) / rho(i,j,k); });
}
else
{
if (advectionType[fscalar+n] == Conservative)
{
//
// For tracers, Solving
// dS/dt + del dot (U S) = del dot beta grad (S/rho) + rho H_q
// where S = rho q, q is a concentration
// tforces = rho H_q (since it's always density-weighted)
// visc = del dot beta grad (S/rho)
//
amrex::ParallelFor(force_bx, [tf, visc]
AMREX_GPU_DEVICE (int i, int j, int k ) noexcept
{ tf(i,j,k) += visc(i,j,k); });
}
else
{
//
// Solving
// dS/dt + U dot del S = del dot beta grad S + H_q
// where S = q, q is a concentration
// tforces = rho H_q (since it's always density-weighted)
// visc = del dot beta grad S
//
amrex::ParallelFor(force_bx, [tf, visc, rho]
AMREX_GPU_DEVICE (int i, int j, int k) noexcept
{ tf(i,j,k) = tf(i,j,k) / rho(i,j,k) + visc(i,j,k); });
}
}
}
}
}
ComputeAofs(fscalar, num_scalars, Smf, 0, forcing_term, *divu_fp, false, dt);
}
}
//
// This subroutine updates the scalars, before the velocity update
// and the level projection
//
// AT this point in time, all we know is psi^n, rho^n+1/2, and the
// general forcing terms at t^n, and after solving in this routine
// viscous forcing at t^n+1/2. Note, unless more complicated logic
// is invoked earlier, we do not have any estimate of general forcing
// terms at t^n+1/2.
//
void
NavierStokes::scalar_update (Real dt,
int first_scalar,
int last_scalar)
{
BL_PROFILE("NavierStokes::scalar_update()");
if (advect_and_update_scalar) {
if (verbose) Print() << "... update scalars\n";
scalar_advection_update(dt, first_scalar, last_scalar);
bool do_any_diffuse = false;
for (int sigma = first_scalar; sigma <= last_scalar; sigma++)
if (is_diffusive[sigma]) do_any_diffuse = true;
if (do_any_diffuse)
scalar_diffusion_update(dt, first_scalar, last_scalar);
MultiFab& S_new = get_new_data(State_Type);
//#ifdef AMREX_USE_EB
// set_body_state(S_new);
//#endif
for (int sigma = first_scalar; sigma <= last_scalar; sigma++)
{
if (S_new.contains_nan(sigma,1,0))
{
Print() << "New scalar " << sigma << " contains Nans" << '\n';
exit(0);
}
}
}
}
void
NavierStokes::scalar_diffusion_update (Real dt,
int first_scalar,
int last_scalar)
{
BL_PROFILE("NavierStokes::scalar_diffusion_update()");
const MultiFab& Rh = get_rho_half_time();
int ng=1;
const Real prev_time = state[State_Type].prevTime();
const Real curr_time = state[State_Type].curTime();
FillPatch(*this,get_old_data(State_Type),ng,prev_time,State_Type,Density,NUM_SCALARS,Density);
FillPatch(*this,get_new_data(State_Type),ng,curr_time,State_Type,Density,NUM_SCALARS,Density);
auto Snc = std::make_unique<MultiFab>();
auto Snp1c = std::make_unique<MultiFab>();
if (level > 0) {
auto& crselev = getLevel(level-1);
Snc->define(crselev.boxArray(), crselev.DistributionMap(), NUM_STATE, ng, MFInfo(), crselev.Factory());
FillPatch(crselev,*Snc ,ng,prev_time,State_Type,0,NUM_STATE);
Snp1c->define(crselev.boxArray(), crselev.DistributionMap(), NUM_STATE, ng, MFInfo(), crselev.Factory());
FillPatch(crselev,*Snp1c,ng,curr_time,State_Type,0,NUM_STATE);
}
const int nlev = (level ==0 ? 1 : 2);
Vector<MultiFab*> Sn(nlev,nullptr), Snp1(nlev,nullptr);
Sn[0] = &(get_old_data(State_Type));
Snp1[0] = &(get_new_data(State_Type));
if (nlev>1) {
Sn[1] = Snc.get() ;
Snp1[1] = Snp1c.get() ;
}
const Vector<BCRec>& theBCs = AmrLevel::desc_lst[State_Type].getBCs();
FluxBoxes fb_diffn, fb_diffnp1;
MultiFab **cmp_diffn = nullptr, **cmp_diffnp1 = nullptr;
MultiFab *delta_rhs = nullptr;
MultiFab *alpha = nullptr;
const int rhsComp = 0, alphaComp = 0, fluxComp = 0;
FluxBoxes fb_fluxn (this);
FluxBoxes fb_fluxnp1(this);
MultiFab** fluxn = fb_fluxn.get();
MultiFab** fluxnp1 = fb_fluxnp1.get();
Vector<int> diffuse_comp(1);
for (int sigma = first_scalar; sigma <= last_scalar; sigma++)
{
if (verbose) {
Print()<<"scalar_diffusion_update "<<sigma<<" of "<<last_scalar<<"\n";
}
if (is_diffusive[sigma])
{
if (be_cn_theta != 1)
{
cmp_diffn = fb_diffn.define(this);
getDiffusivity(cmp_diffn, prev_time, sigma, 0, 1);
}
cmp_diffnp1 = fb_diffnp1.define(this);
getDiffusivity(cmp_diffnp1, curr_time, sigma, 0, 1);
diffuse_comp[0] = is_diffusive[sigma];
const int rho_flag = Diffusion::set_rho_flag(diffusionType[sigma]);
const bool add_old_time_divFlux = true;
const int betaComp = 0;
const int Rho_comp = Density;
const int bc_comp = sigma;
diffusion->diffuse_scalar (Sn, Sn, Snp1, Snp1, sigma, 1, Rho_comp,
prev_time,curr_time,be_cn_theta,Rh,rho_flag,
fluxn,fluxnp1,fluxComp,delta_rhs,rhsComp,
alpha,alphaComp,
cmp_diffn,cmp_diffnp1,betaComp,
crse_ratio,theBCs[bc_comp],geom,
add_old_time_divFlux,
diffuse_comp);
delete alpha;
//
// Increment the viscous flux registers
//
if (do_reflux)
{
FArrayBox fluxtot;
for (int d = 0; d < AMREX_SPACEDIM; d++)
{
MultiFab fluxes;
if (level < parent->finestLevel())
{
fluxes.define(fluxn[d]->boxArray(), fluxn[d]->DistributionMap(), 1, 0, MFInfo(), Factory());
}
for (MFIter fmfi(*fluxn[d]); fmfi.isValid(); ++fmfi)
{
const Box& ebox = (*fluxn[d])[fmfi].box();
fluxtot.resize(ebox,1);
Elixir fdata_i = fluxtot.elixir();
auto const& ftot = fluxtot.array();
auto const& fn = fluxn[d]->array(fmfi);
auto const& fnp1 = fluxnp1[d]->array(fmfi);
amrex::ParallelFor(ebox, [ftot, fn, fnp1 ]
AMREX_GPU_DEVICE (int i, int j, int k) noexcept
{
ftot(i,j,k) = fn(i,j,k) + fnp1(i,j,k);
});
if (level < parent->finestLevel()) {
fluxes[fmfi].copy<RunOn::Gpu>(fluxtot);
}
if (level > 0) {
getViscFluxReg().FineAdd(fluxtot,d,fmfi.index(),0,sigma,1,dt,RunOn::Gpu);
}
} // mfi
if (level < parent->finestLevel()) {
getLevel(level+1).getViscFluxReg().CrseInit(fluxes,d,0,sigma,1,-dt);
}
} // d
} // do_reflux
if (be_cn_theta != 1) {
fb_diffn.clear();
}
fb_diffnp1.clear();
}//end if(is_diffusive)
}
}
void
NavierStokes::velocity_diffusion_update (Real dt)
{
BL_PROFILE("NavierStokes::velocity_diffusion_update()");
const Real strt_time = ParallelDescriptor::second();
//
// Compute the viscous forcing.
// Do following except at initial iteration.
//
if (is_diffusive[Xvel])
{
int rho_flag = (do_mom_diff == 0) ? 1 : 3;
FluxBoxes fb_viscn, fb_viscnp1;
MultiFab** loc_viscn = nullptr;
MultiFab** loc_viscnp1 = nullptr;
Real viscTime = state[State_Type].prevTime();
loc_viscn = fb_viscn.define(this);
getViscosity(loc_viscn, viscTime);
viscTime = state[State_Type].curTime();
loc_viscnp1 = fb_viscnp1.define(this);
getViscosity(loc_viscnp1, viscTime);
diffusion->diffuse_velocity(dt,be_cn_theta,get_rho_half_time(),rho_flag,
nullptr,loc_viscn,viscn_cc,loc_viscnp1,viscnp1_cc);
}
if (verbose)
{
Real run_time = ParallelDescriptor::second() - strt_time;
const int IOProc = ParallelDescriptor::IOProcessorNumber();
ParallelDescriptor::ReduceRealMax(run_time,IOProc);
Print() << "NavierStokes:velocity_diffusion_update(): lev: " << level
<< ", time: " << run_time << '\n';
}
}
void
NavierStokes::sum_integrated_quantities ()
{
const int finest_level = parent->finestLevel();
const Real time = state[State_Type].curTime();