ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TI_utils.H
Go to the documentation of this file.
1 /**
2  * Define the primitive variables by dividing the conserved variables by density
3  */
4  auto cons_to_prim = [&](const MultiFab& cons_state, int ng)
5  {
6  BL_PROFILE("cons_to_prim()");
7 
8  int ncomp_prim = S_prim.nComp();
9 
10 #ifdef _OPENMP
11 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
12 #endif
13  for (MFIter mfi(cons_state,TilingIfNotGPU()); mfi.isValid(); ++mfi)
14  {
15  const Box& gbx = mfi.growntilebox(ng);
16  const Array4<const Real>& cons_arr = cons_state.array(mfi);
17  const Array4< Real>& prim_arr = S_prim.array(mfi);
18 
19  //
20  // We may need > one ghost cells of prim in order to compute higher order advective terms
21  //
22  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
23  {
24  Real rho = cons_arr(i,j,k,Rho_comp);
25  Real rho_theta = cons_arr(i,j,k,RhoTheta_comp);
26  prim_arr(i,j,k,PrimTheta_comp) = rho_theta / rho;
27  for (int n = 1; n < ncomp_prim; ++n) {
28  prim_arr(i,j,k,PrimTheta_comp + n) = cons_arr(i,j,k,RhoTheta_comp + n) / rho;
29  }
30  });
31 
32  //
33  // We only use one ghost cell of pi_stage so we only fill one here
34  //
35  const Box& gbx1 = mfi.growntilebox(1);
36 
37  const Array4< Real>& pi_stage_arr = pi_stage.array(mfi);
38  const Real rdOcp = solverChoice.rdOcp;
39 
40  amrex::ParallelFor(gbx1, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
41  {
42  pi_stage_arr(i,j,k) = getExnergivenRTh(cons_arr(i,j,k,RhoTheta_comp), rdOcp);
43  });
44  } // mfi
45  };
46 
47 /**
48  * This routine is called after the scalars are updated for each RK stage
49  */
50  auto apply_bcs = [&](Vector<MultiFab>& S_data,
51  const Real time_for_fp, int ng_cons, int ng_vel,
52  bool fast_only, bool vel_and_mom_synced)
53  {
54  BL_PROFILE("apply_bcs()");
55 
56  int scomp_cons;
57  int ncomp_cons;
58  bool cons_only;
59 
60  int ng_cons_to_use;
61 
62  // **********************************************************************************
63  // Because momentum is updated in the time-stepping routine, but boundary conditions
64  // are imposed on velocities, we must first update the velocity from the momentum
65  // before applying bcs.
66  // **********************************************************************************
67  if (!vel_and_mom_synced) {
68 
69  // **********************************************************************************
70  // Call FillPatch routines for the density only because we need it to convert between
71  // momentum and velocity
72  // This fills ghost cells/faces from
73  // 1) coarser level if lev > 0
74  // 2) physical boundaries
75  // 3) other grids at the same level
76  // **********************************************************************************
77 
78  // We must have at least one ghost cell of density to convert from momentum to velocity
79  // on the valid region
80  AMREX_ALWAYS_ASSERT (ng_cons >= 1);
81 
82  // We must have at least one extra ghost cell of density to convert from velocity to momentum
83  // on the valid region
84  ng_cons_to_use = std::max(ng_cons, ng_vel+1);
85 
86  scomp_cons = 0;
87  ncomp_cons = 1;
88  cons_only = true;
89  // **********************************************************************************
90  // Fill ghost cells of density only
91  // **********************************************************************************
92  FillIntermediatePatch(level, time_for_fp,
93  {&S_data[IntVars::cons], &xvel_new, &yvel_new, &zvel_new},
94  {&S_data[IntVars::cons], &S_data[IntVars::xmom],
95  &S_data[IntVars::ymom], &S_data[IntVars::zmom]},
96  ng_cons_to_use, 0, cons_only, scomp_cons, ncomp_cons);
97  }
98 
99  // ***************************************************************************************
100  // Call FillPatch routines for all data except rho which was filled above
101  // This fills ghost cells/faces from
102  // 1) coarser level if lev > 0
103  // 2) physical boundaries
104  // 3) other grids at the same level
105  // ***************************************************************************************
106  if (vel_and_mom_synced) {
107  if (fast_only) {
108  scomp_cons = 0;
109  ncomp_cons = 2; // rho and (rho theta) only
110  } else {
111  scomp_cons = 0;
112  ncomp_cons = S_data[IntVars::cons].nComp();
113  }
114  // We must have at least one extra ghost cell of density to convert from velocity to momentum
115  // on the valid region
116  ng_cons_to_use = std::max(ng_cons, ng_vel+1);
117 
118  } else {
119  if (fast_only) {
120  scomp_cons = 1;
121  ncomp_cons = 1; // (rho theta) only since we filled rho above
122  } else {
123  scomp_cons = 1;
124  ncomp_cons = S_data[IntVars::cons].nComp()-1; // since we filled rho above
125  }
126  ng_cons_to_use = ng_cons;
127  }
128 
129  bool allow_most_bcs = true;
130  if (fast_only) {
131  allow_most_bcs = false;
132  } else {
133  allow_most_bcs = true;
134  }
135 
136  // **********************************************************************************
137  // NOTE: FillIntermediatePatch takes momenta at the new time, and returns
138  // BOTH updated velocities and momenta
139  // **********************************************************************************
140  cons_only = false;
141  FillIntermediatePatch(level, time_for_fp,
142  {&S_data[IntVars::cons], &xvel_new, &yvel_new, &zvel_new},
143  {&S_data[IntVars::cons], &S_data[IntVars::xmom],
144  &S_data[IntVars::ymom], &S_data[IntVars::zmom]},
145  ng_cons_to_use, ng_vel, cons_only, scomp_cons, ncomp_cons,
146  allow_most_bcs);
147  };
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getExnergivenRTh(const amrex::Real rhotheta, const amrex::Real rdOcp, const amrex::Real qv=0.0)
Definition: ERF_EOS.H:159
#define Rho_comp
Definition: ERF_IndexDefines.H:36
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:37
#define PrimTheta_comp
Definition: ERF_IndexDefines.H:50
auto apply_bcs
Definition: ERF_TI_utils.H:50
auto cons_to_prim
Definition: ERF_TI_utils.H:4
@ ymom
Definition: ERF_IndexDefines.H:141
@ cons
Definition: ERF_IndexDefines.H:139
@ zmom
Definition: ERF_IndexDefines.H:142
@ xmom
Definition: ERF_IndexDefines.H:140
@ rho
Definition: ERF_Kessler.H:30