ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
MassSource Class Reference

#include <ERF_MassSource.H>

Collaboration diagram for MassSource:

Classes

struct  LevelGeometry
 

Public Member Functions

amrex::Real x0 () const
 
amrex::Real y0 () const
 
amrex::Real rate (amrex::Real t) const
 Mass flow rate [kg/s] at time t (since the start of the run) More...
 
amrex::Real total_mass () const
 Total mass in the rate table [kg] (trapezoidal) More...
 
void update_geometry (int finest_level, const amrex::Vector< amrex::Geometry > &geom, const amrex::Vector< amrex::BoxArray > &grids, const amrex::Vector< amrex::DistributionMapping > &dmap, const amrex::Vector< amrex::IntVect > &ref_ratio, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &z_phys_nd, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &detJ, const amrex::Vector< const amrex::MultiFab * > &mf_mx, const amrex::Vector< const amrex::MultiFab * > &mf_my, const std::function< void(int, amrex::MultiFab &)> &fill_ground)
 
void add_sources (int level, amrex::MultiFab &cc_src, const amrex::MultiFab &cons, const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, amrex::Real time) const
 

Static Public Member Functions

static std::unique_ptr< MassSource > from_inputs ()
 

Private Member Functions

void read_rate_file (const std::string &fname)
 

Private Attributes

amrex::Real m_x0 = 0
 
amrex::Real m_y0 = 0
 
amrex::Real m_radius = 0
 
amrex::Real m_height = 0
 
amrex::Real m_theta = 0
 
bool m_fixed_theta = false
 
bool m_tracer = true
 
bool m_constant = false
 
amrex::Vector< amrex::Real > m_t
 
amrex::Vector< amrex::Real > m_mdot
 
amrex::Vector< LevelGeometry > m_lev
 
amrex::Real m_volume = 0
 

Member Function Documentation

◆ add_sources()

void MassSource::add_sources ( int  level,
amrex::MultiFab &  cc_src,
const amrex::MultiFab &  cons,
const amrex::MultiFab *  z_phys_nd,
const amrex::Geometry &  geom,
amrex::Real  time 
) const
inline

Add the source at time time to cc_src of level (per unit physical volume, as the other cell-centered sources). cons is the current conserved state and z_phys_nd the nodal heights of the level. update_geometry must have been called for the current grids.

311  {
312  AMREX_ALWAYS_ASSERT(level < static_cast<int>(m_lev.size()) && m_volume > 0);
313  const LevelGeometry& lg = m_lev[level];
314  const amrex::Real mdot = rate(time);
315  if (mdot == amrex::Real(0) || lg.volume == amrex::Real(0)) { return; }
316 
317  const auto dx = geom.CellSizeArray();
318  const auto problo = geom.ProbLoArray();
319  const int kg = geom.Domain().smallEnd(2);
320  const auto period = erf_mass_source::periodic_lengths(geom);
321  const amrex::Real x0 = m_x0, y0 = m_y0, r2 = m_radius * m_radius, hmax = m_height;
322  AMREX_ALWAYS_ASSERT(z_phys_nd != nullptr);
323 
324  const amrex::Real S = mdot / m_volume; // [kg/m^3/s]
325  const bool fixed_theta = m_fixed_theta;
326  const amrex::Real theta_src = m_theta;
327  const bool tracer = m_tracer;
328 
329  for (amrex::MFIter mfi(cons); mfi.isValid(); ++mfi) {
330  const amrex::Box& bx = mfi.validbox();
331  auto z = z_phys_nd->const_array(mfi);
332  auto zg = lg.ground->const_array(mfi);
333  auto c = cons.const_array(mfi);
334  auto src = cc_src.array(mfi);
335  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
336  {
337  if (!erf_mass_source::in_source(i,j,k,z,zg,kg,problo,dx,period,x0,y0,r2,hmax)) { return; }
338  const amrex::Real th = fixed_theta ? theta_src : c(i,j,k,RhoTheta_comp) / c(i,j,k,Rho_comp);
339  src(i,j,k,Rho_comp) += S;
340  src(i,j,k,RhoTheta_comp) += S * th;
341  if (tracer) { src(i,j,k,RhoScalar_comp) += S; }
342  });
343  }
344  }
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
#define RhoScalar_comp
Definition: ERF_IndexDefines.H:65
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
auto problo
Definition: ERF_InitCustomPertVels_ABL.H:36
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
amrex::Real y0() const
Definition: ERF_MassSource.H:176
amrex::Real m_x0
Definition: ERF_MassSource.H:384
amrex::Real rate(amrex::Real t) const
Mass flow rate [kg/s] at time t (since the start of the run)
Definition: ERF_MassSource.H:179
amrex::Vector< LevelGeometry > m_lev
Definition: ERF_MassSource.H:391
amrex::Real m_volume
Definition: ERF_MassSource.H:392
amrex::Real x0() const
Definition: ERF_MassSource.H:175
amrex::Real m_height
Definition: ERF_MassSource.H:384
amrex::Real m_y0
Definition: ERF_MassSource.H:384
amrex::Real m_radius
Definition: ERF_MassSource.H:384
bool m_tracer
Definition: ERF_MassSource.H:387
amrex::Real m_theta
Definition: ERF_MassSource.H:385
bool m_fixed_theta
Definition: ERF_MassSource.H:386
constexpr int S
Definition: ERF_TwoStreamColumn.H:654
@ cons
Definition: ERF_IndexDefines.H:231
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool in_source(int i, int j, int k, amrex::Array4< amrex::Real const > const &z, amrex::Array4< amrex::Real const > const &zg, int kg, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &problo, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &dx, amrex::GpuArray< amrex::Real, 2 > const &period, amrex::Real x0, amrex::Real y0, amrex::Real r2, amrex::Real hmax) noexcept
Definition: ERF_MassSource.H:66
amrex::GpuArray< amrex::Real, 2 > periodic_lengths(const amrex::Geometry &geom)
The domain length in x and y where geom is periodic, 0 where it is not.
Definition: ERF_MassSource.H:87
Here is the call graph for this function:

◆ from_inputs()

static std::unique_ptr<MassSource> MassSource::from_inputs ( )
inlinestatic

The source the inputs ask for, or nullptr when no erf.mass_source input is given. Stops on an unknown erf.mass_source key (a misspelled one would otherwise be ignored, and a misspelled rate would turn the source off) and on inputs without a rate.

102  {
103  const std::string prefix = "erf.mass_source.";
104  const std::set<std::string> known = {"rate", "rate_file", "center", "radius",
105  "height", "theta", "tracer"};
106  bool any = false;
107  for (const auto& name : amrex::ParmParse::getEntries("erf.mass_source")) {
108  if (name.rfind(prefix, 0) != 0) { continue; }
109  any = true;
110  if (known.count(name.substr(prefix.size())) == 0) {
111  amrex::Abort("erf.mass_source: unknown input " + name + "; the inputs are rate, "
112  "rate_file, center, radius, height, theta and tracer");
113  }
114  }
115  amrex::ParmParse pp("erf.mass_source");
116  if (!pp.contains("rate") && !pp.contains("rate_file")) {
117  if (any) {
118  amrex::Abort("erf.mass_source: inputs are given but neither erf.mass_source.rate "
119  "nor erf.mass_source.rate_file");
120  }
121  return nullptr;
122  }
123  if (pp.contains("rate") && pp.contains("rate_file")) {
124  amrex::Abort("erf.mass_source: give rate or rate_file, not both");
125  }
126 
127  auto ms = std::make_unique<MassSource>();
128  amrex::Vector<amrex::Real> c;
129  pp.getarr("center", c);
130  if (c.size() != 2) { amrex::Abort("erf.mass_source.center must be two values: x y"); }
131  ms->m_x0 = c[0];
132  ms->m_y0 = c[1];
133  pp.get("radius", ms->m_radius);
134  pp.get("height", ms->m_height);
135  if (!(ms->m_radius > 0) || !(ms->m_height > 0)) {
136  amrex::Abort("erf.mass_source.radius and height must be positive");
137  }
138  ms->m_fixed_theta = pp.query("theta", ms->m_theta);
139  if (ms->m_fixed_theta && !(ms->m_theta > 0)) {
140  amrex::Abort("erf.mass_source.theta must be positive");
141  }
142  pp.query("tracer", ms->m_tracer);
143 
144  if (pp.contains("rate")) {
145  amrex::Real r;
146  pp.get("rate", r);
147  if (!(r >= 0)) { amrex::Abort("erf.mass_source.rate must be zero or positive"); }
148  ms->m_t = {amrex::Real(0)};
149  ms->m_mdot = {r};
150  ms->m_constant = true;
151  } else {
152  std::string fname;
153  pp.get("rate_file", fname);
154  ms->read_rate_file(fname);
155  }
156 
157  amrex::Print() << "Mass source: centre (" << ms->m_x0 << ", " << ms->m_y0 << ") m, radius "
158  << ms->m_radius << " m, up to " << ms->m_height << " m above the ground; ";
159  if (ms->m_constant) {
160  amrex::Print() << "constant rate " << ms->m_mdot[0] << " kg/s";
161  } else {
162  amrex::Print() << ms->m_t.size() << "-point rate table, t = " << ms->m_t.front()
163  << " to " << ms->m_t.back() << " s, total "
164  << ms->total_mass() << " kg";
165  }
166  if (ms->m_fixed_theta) {
167  amrex::Print() << "; theta " << ms->m_theta << " K";
168  } else {
169  amrex::Print() << "; at the theta of each cell";
170  }
171  amrex::Print() << (ms->m_tracer ? "; tracer in rhoadv_0" : "") << std::endl;
172  return ms;
173  }
ParmParse pp("prob")
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139

Referenced by ERF::ReadParameters().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rate()

amrex::Real MassSource::rate ( amrex::Real  t) const
inline

Mass flow rate [kg/s] at time t (since the start of the run)

180  {
181  if (m_constant) { return m_mdot[0]; }
182  if (t < m_t.front() || t > m_t.back()) { return amrex::Real(0); }
183  amrex::Long n = 1;
184  while (n < m_t.size() - 1 && m_t[n] < t) { ++n; }
185  const amrex::Real w = (t - m_t[n-1]) / (m_t[n] - m_t[n-1]);
186  return (amrex::Real(1) - w) * m_mdot[n-1] + w * m_mdot[n];
187  }
bool m_constant
Definition: ERF_MassSource.H:388
amrex::Vector< amrex::Real > m_t
Definition: ERF_MassSource.H:389
amrex::Vector< amrex::Real > m_mdot
Definition: ERF_MassSource.H:389
@ t
Definition: ERF_WSM6.H:295

Referenced by add_sources().

Here is the caller graph for this function:

◆ read_rate_file()

void MassSource::read_rate_file ( const std::string &  fname)
inlineprivate
348  {
349  amrex::Vector<char> chars;
350  amrex::ParallelDescriptor::ReadAndBcastFile(fname, chars);
351  std::istringstream is(std::string(chars.dataPtr()));
352  std::string line;
353  while (std::getline(is, line)) {
354  std::string data = line.substr(0, line.find('#'));
355  if (data.find_first_not_of(" \t\r") == std::string::npos) { continue; } // blank or comment
356  std::istringstream ls(data);
357  amrex::Real t, m;
358  std::string extra;
359  if (!(ls >> t >> m) || (ls >> extra)) {
360  amrex::Abort("erf.mass_source.rate_file " + fname + ": a line is not two numbers, "
361  "time and rate: " + line);
362  }
363  if (!m_t.empty() && t <= m_t.back()) {
364  amrex::Abort("erf.mass_source.rate_file " + fname + ": times must increase strictly");
365  }
366  if (!(m >= 0)) {
367  amrex::Abort("erf.mass_source.rate_file " + fname + ": rates must be zero or positive");
368  }
369  m_t.push_back(t);
370  m_mdot.push_back(m);
371  }
372  if (m_t.size() < 2) {
373  amrex::Abort("erf.mass_source.rate_file " + fname + ": needs at least two rows");
374  }
375  }

◆ total_mass()

amrex::Real MassSource::total_mass ( ) const
inline

Total mass in the rate table [kg] (trapezoidal)

191  {
192  amrex::Real m = 0;
193  for (amrex::Long n = 1; n < m_t.size(); ++n) {
194  m += amrex::Real(0.5) * (m_mdot[n] + m_mdot[n-1]) * (m_t[n] - m_t[n-1]);
195  }
196  return m;
197  }

◆ update_geometry()

void MassSource::update_geometry ( int  finest_level,
const amrex::Vector< amrex::Geometry > &  geom,
const amrex::Vector< amrex::BoxArray > &  grids,
const amrex::Vector< amrex::DistributionMapping > &  dmap,
const amrex::Vector< amrex::IntVect > &  ref_ratio,
const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &  z_phys_nd,
const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &  detJ,
const amrex::Vector< const amrex::MultiFab * > &  mf_mx,
const amrex::Vector< const amrex::MultiFab * > &  mf_my,
const std::function< void(int, amrex::MultiFab &)> &  fill_ground 
)
inline

Bring the ground heights and the composite source volume up to date with the grids: a no-op unless the BoxArray or DistributionMapping of a level, or the finest level, has changed since the last call. Aborts if no level has a cell center in the region. z_phys_nd and detJ must be allocated on every level. fill_ground(lev, slab) must fill a one-node-thick nodal slab at the bottom of the domain, on the footprint of every box of the level, with the ground heights (ERF::fill_surface_nodes, which interpolates them from coarser levels where the level does not reach the ground).

218  {
219  bool same = (finest_level + 1 == static_cast<int>(m_lev.size()));
220  for (int lev = 0; same && lev <= finest_level; ++lev) {
221  same = (m_lev[lev].ba == grids[lev]) && (m_lev[lev].dm == dmap[lev]);
222  }
223  if (same) { return; }
224 
225  m_lev.clear();
226  m_lev.resize(finest_level + 1);
227  m_volume = 0;
228  for (int lev = 0; lev <= finest_level; ++lev) {
229  LevelGeometry& lg = m_lev[lev];
230  lg.ba = grids[lev];
231  lg.dm = dmap[lev];
232 
233  // The nodal height of the ground under every column of every box, on the
234  // box's own (i,j) range, so that a box that does not reach the bottom (a patch
235  // aloft, or a grid split in z) can tell how high its cells are
236  const int kg = geom[lev].Domain().smallEnd(2);
237  AMREX_ALWAYS_ASSERT(z_phys_nd[lev] && detJ[lev]);
238  amrex::BoxList bl = z_phys_nd[lev]->boxArray().boxList();
239  for (auto& b : bl) { b.setRange(2, kg); }
240  lg.ground = std::make_unique<amrex::MultiFab>(amrex::BoxArray(std::move(bl)),
241  z_phys_nd[lev]->DistributionMap(), 1, 0);
242  fill_ground(lev, *lg.ground);
243 
244  // Cells not covered by the next finer level count towards the composite volume
245  std::unique_ptr<amrex::iMultiFab> uncovered;
246  if (lev < finest_level) {
247  uncovered = std::make_unique<amrex::iMultiFab>(
248  amrex::makeFineMask(grids[lev], dmap[lev], grids[lev+1], ref_ratio[lev], 1, 0));
249  }
250 
251  const auto dx = geom[lev].CellSizeArray();
252  const auto problo = geom[lev].ProbLoArray();
253  const auto period = erf_mass_source::periodic_lengths(geom[lev]);
254  const amrex::Real x0 = m_x0, y0 = m_y0, r2 = m_radius * m_radius, hmax = m_height;
255  // dx in place of a dy over one cell, and the reverse: on an x-z slice mdot is the
256  // rate per dx of span
257  const auto len = LengthScaleCellSize(geom[lev]);
258  const amrex::Real dV0 = len[0] * len[1] * len[2];
259 
260  amrex::ReduceOps<amrex::ReduceOpSum, amrex::ReduceOpSum> reduce_op;
261  amrex::ReduceData<amrex::Real, amrex::Real> reduce_data(reduce_op);
262  for (amrex::MFIter mfi(grids[lev], dmap[lev]); mfi.isValid(); ++mfi) {
263  const amrex::Box& bx = mfi.validbox();
264  auto z = z_phys_nd[lev]->const_array(mfi);
265  auto zg = lg.ground->const_array(mfi);
266  auto dj = detJ[lev]->const_array(mfi);
267  auto mx = mf_mx[lev]->const_array(mfi);
268  auto my = mf_my[lev]->const_array(mfi);
269  const bool has_mask = (uncovered != nullptr);
270  auto msk = has_mask ? uncovered->const_array(mfi) : amrex::Array4<int const>{};
271  reduce_op.eval(bx, reduce_data,
272  [=] AMREX_GPU_DEVICE (int i, int j, int k) -> amrex::GpuTuple<amrex::Real, amrex::Real>
273  {
274  if (!erf_mass_source::in_source(i,j,k,z,zg,kg,problo,dx,period,x0,y0,r2,hmax)) {
275  return {amrex::Real(0), amrex::Real(0)};
276  }
277  const amrex::Real dV = dV0 * dj(i,j,k) / (mx(i,j,0) * my(i,j,0));
278  const amrex::Real u = (!has_mask || msk(i,j,k) == 1) ? dV : amrex::Real(0);
279  return {dV, u};
280  });
281  }
282  auto hv = reduce_data.value(reduce_op);
283  amrex::Real v[2] = {amrex::get<0>(hv), amrex::get<1>(hv)};
284  amrex::ParallelDescriptor::ReduceRealSum(v, 2);
285  lg.volume = v[0];
286  m_volume += v[1];
287  }
288 
289  if (!(m_volume > 0)) {
290  amrex::Abort("erf.mass_source: no cell centre lies inside the source region on any level; "
291  "make radius or height larger, or check that center is inside the domain");
292  }
293  amrex::Print() << "Mass source: region volume " << m_volume << " m^3 on the composite grid";
294  for (int lev = 0; lev <= finest_level; ++lev) {
295  amrex::Print() << (lev == 0 ? " (" : ", ") << "level " << lev << ": " << m_lev[lev].volume << " m^3";
296  }
297  amrex::Print() << ")" << std::endl;
298  }
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > LengthScaleCellSize(const amrex::Geometry &geom)
Definition: ERF_LengthScaleCellSize.H:28
Here is the call graph for this function:

◆ x0()

amrex::Real MassSource::x0 ( ) const
inline
175 { return m_x0; }

Referenced by add_sources(), and update_geometry().

Here is the caller graph for this function:

◆ y0()

amrex::Real MassSource::y0 ( ) const
inline
176 { return m_y0; }

Referenced by add_sources(), and update_geometry().

Here is the caller graph for this function:

Member Data Documentation

◆ m_constant

bool MassSource::m_constant = false
private

Referenced by rate().

◆ m_fixed_theta

bool MassSource::m_fixed_theta = false
private

Referenced by add_sources().

◆ m_height

amrex::Real MassSource::m_height = 0
private

Referenced by add_sources(), and update_geometry().

◆ m_lev

amrex::Vector<LevelGeometry> MassSource::m_lev
private

Referenced by add_sources(), and update_geometry().

◆ m_mdot

amrex::Vector<amrex::Real> MassSource::m_mdot
private

Referenced by rate(), read_rate_file(), and total_mass().

◆ m_radius

amrex::Real MassSource::m_radius = 0
private

Referenced by add_sources(), and update_geometry().

◆ m_t

amrex::Vector<amrex::Real> MassSource::m_t
private

Referenced by rate(), read_rate_file(), and total_mass().

◆ m_theta

amrex::Real MassSource::m_theta = 0
private

Referenced by add_sources().

◆ m_tracer

bool MassSource::m_tracer = true
private

Referenced by add_sources().

◆ m_volume

amrex::Real MassSource::m_volume = 0
private

Referenced by add_sources(), and update_geometry().

◆ m_x0

amrex::Real MassSource::m_x0 = 0
private

Referenced by add_sources(), update_geometry(), and x0().

◆ m_y0

amrex::Real MassSource::m_y0 = 0
private

Referenced by add_sources(), update_geometry(), and y0().


The documentation for this class was generated from the following file: