ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SampleData.H
Go to the documentation of this file.
1 /**
2  * \file ERF_SampleData.H
3  */
4 #ifndef ERF_SAMPLEDATA_H
5 #define ERF_SAMPLEDATA_H
6 
7 #include <algorithm>
8 #include <fstream>
9 #include <iomanip>
10 #include <memory>
11 
12 #include <AMReX_ParmParse.H>
13 #include <AMReX_MultiFab.H>
14 #include <AMReX_MultiFabUtil.H>
15 #include <AMReX_PlotFileUtil.H>
16 #include <AMReX_RealVect.H>
17 
18 #include <ERF_IndexDefines.H>
19 #include <ERF_EOS.H>
20 #include <ERF_Container.H>
21 #include <ERF_DataStruct.H>
22 #include <ERF_LatLonMap.H>
23 
24 #include <iostream>
25 
26 /**
27  * The value line and plane sampling write at cells covered by immersed terrain
28  * (erf.sampling_covered_value)
29  */
30 inline amrex::Real
32 {
33  amrex::Real value = amrex::Real(-9999.0);
34  amrex::ParmParse pp("erf");
35  pp.queryAdd("sampling_covered_value", value);
36  return value;
37 }
38 
39 /**
40  * The number of levels that exist, counted from level 0: vars_new holds a slot for every
41  * level up to amr.max_level, but the state of a level that has not been made is undefined
42  */
43 inline int
44 sampling_num_levels (const amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_new)
45 {
46  int nlev = 0;
47  while (nlev < static_cast<int>(vars_new.size()) &&
48  static_cast<int>(vars_new[nlev].size()) > Vars::cons &&
49  vars_new[nlev][Vars::cons].ok()) {
50  ++nlev;
51  }
52  return nlev;
53 }
54 
56 {
58  {
59  amrex::ParmParse pp("erf");
60 
61  bool has_line_idx = pp.contains("sample_line_lo") || pp.contains("sample_line_hi");
62  bool has_line_real = pp.contains("sample_line_lo_real") || pp.contains("sample_line_hi_real");
63  bool has_line_latlon = pp.contains("sample_line_lo_latlon") || pp.contains("sample_line_hi_latlon");
64  if (int(has_line_idx) + int(has_line_real) + int(has_line_latlon) > 1) {
65  amrex::Abort("Specify only one of erf.sample_line_lo/hi, erf.sample_line_lo_real/hi_real"
66  " or erf.sample_line_lo_latlon/hi_latlon");
67  }
68 
69  // Count number of lo and hi points define the line
70  int n_line_lo = pp.countval("sample_line_lo") / AMREX_SPACEDIM;
71  int n_line_hi = pp.countval("sample_line_hi") / AMREX_SPACEDIM;
72  int n_line_lo_real = pp.countval("sample_line_lo_real") / AMREX_SPACEDIM;
73  int n_line_hi_real = pp.countval("sample_line_hi_real") / AMREX_SPACEDIM;
74  int n_line_lo_latlon = pp.countval("sample_line_lo_latlon") / 3;
75  int n_line_hi_latlon = pp.countval("sample_line_hi_latlon") / 3;
76  int n_line_dir = pp.countval("sample_line_dir");
77  if (has_line_idx) {
78  AMREX_ALWAYS_ASSERT( (n_line_lo==n_line_hi ) &&
79  (n_line_lo==n_line_dir) );
80  } else if (has_line_real) {
81  AMREX_ALWAYS_ASSERT( (n_line_lo_real==n_line_hi_real ) &&
82  (n_line_lo_real==n_line_dir) );
83  } else if (has_line_latlon) {
84  if (pp.countval("sample_line_lo_latlon") != 3*n_line_lo_latlon ||
85  pp.countval("sample_line_hi_latlon") != 3*n_line_hi_latlon ||
86  n_line_lo_latlon != n_line_hi_latlon || n_line_lo_latlon != n_line_dir) {
87  amrex::Abort("erf.sample_line_lo_latlon and erf.sample_line_hi_latlon must each give"
88  " lat, long and z for every line of erf.sample_line_dir");
89  }
90  }
91 
92  // Parse the data
93  int nline = 0;
94  if (has_line_idx) {
95  nline = n_line_lo;
96  if (nline > 0) {
97  // Parse lo
98  amrex::Vector<int> idx_lo; idx_lo.resize(nline*AMREX_SPACEDIM);
99  amrex::Vector<amrex::IntVect> iv_lo; iv_lo.resize(nline);
100  pp.queryarr("sample_line_lo",idx_lo,0,nline*AMREX_SPACEDIM);
101  for (int i(0); i < nline; i++) {
102  amrex::IntVect iv(idx_lo[AMREX_SPACEDIM*i+0],
103  idx_lo[AMREX_SPACEDIM*i+1],
104  idx_lo[AMREX_SPACEDIM*i+2]);
105  iv_lo[i] = iv;
106  }
107 
108  // Parse hi
109  amrex::Vector<int> idx_hi; idx_hi.resize(nline*AMREX_SPACEDIM);
110  amrex::Vector<amrex::IntVect> iv_hi; iv_hi.resize(nline);
111  pp.queryarr("sample_line_hi",idx_hi,0,nline*AMREX_SPACEDIM);
112  for (int i(0); i < nline; i++) {
113  amrex::IntVect iv(idx_hi[AMREX_SPACEDIM*i+0],
114  idx_hi[AMREX_SPACEDIM*i+1],
115  idx_hi[AMREX_SPACEDIM*i+2]);
116  iv_hi[i] = iv;
117  }
118 
119  // Construct vector of bounding boxes
120  m_bnd_bx.resize(nline);
121  for (int i = 0; i < nline; i++){
122  amrex::Box lbx(iv_lo[i],iv_hi[i]);
123  m_bnd_bx[i] = lbx;
124  }
125 
126  // The indices are those of level 0; on a finer level the same indices are
127  // somewhere else, so these lines are sampled on level 0 only
128  m_max_level = 0;
129  int amr_max_level = 0;
130  amrex::ParmParse("amr").query("max_level", amr_max_level);
131  if (amr_max_level > 0) {
132  amrex::Print() << "Line sampling: erf.sample_line_lo/hi are level-0 indices, so "
133  << "these lines are sampled on level 0 only; give "
134  << "erf.sample_line_lo_real/hi_real to sample the finest level "
135  << "that holds a line" << std::endl;
136  }
137  }
138  } else if (has_line_real) {
139  m_use_real_bx = true;
140  nline = n_line_lo_real;
141  if (nline > 0) {
142  // Parse lo
143  amrex::Vector<amrex::Real> real_lo; real_lo.resize(nline*AMREX_SPACEDIM);
144  amrex::Vector<amrex::Vector<amrex::Real>> rv_lo;
145  pp.queryarr("sample_line_lo_real",real_lo,0,nline*AMREX_SPACEDIM);
146  for (int i(0); i < nline; i++) {
147  amrex::Vector<amrex::Real> rv = {real_lo[AMREX_SPACEDIM*i+0],
148  real_lo[AMREX_SPACEDIM*i+1],
149  real_lo[AMREX_SPACEDIM*i+2]};
150  rv_lo.push_back(rv);
151  }
152 
153  // Parse hi
154  amrex::Vector<amrex::Real> real_hi; real_hi.resize(nline*AMREX_SPACEDIM);
155  amrex::Vector<amrex::Vector<amrex::Real>> rv_hi;
156  pp.queryarr("sample_line_hi_real",real_hi,0,nline*AMREX_SPACEDIM);
157  for (int i(0); i < nline; i++) {
158  amrex::Vector<amrex::Real> rv = {real_hi[AMREX_SPACEDIM*i+0],
159  real_hi[AMREX_SPACEDIM*i+1],
160  real_hi[AMREX_SPACEDIM*i+2]};
161  rv_hi.push_back(rv);
162  }
163 
164  // Construct vector of bounding real boxes
165  m_bnd_rbx.resize(nline);
166  for (int i = 0; i < nline; i++){
167  amrex::RealBox rbx(rv_lo[i].data(),rv_hi[i].data());
168  m_bnd_rbx[i] = rbx;
169  }
170 
171  // Construct vector of bounding boxes
172  m_bnd_bx.resize(nline);
173  }
174  } else if (has_line_latlon) {
175  // The ends are turned into domain coordinates by resolve_latlon, once the run
176  // has its latitude/longitude arrays; until then the line is not along any
177  // direction, so its ends are checked there
178  m_use_real_bx = true;
179  m_use_latlon = true;
180  nline = n_line_lo_latlon;
181  if (nline > 0) {
182  pp.getarr("sample_line_lo_latlon", m_latlon_lo, 0, 3*nline);
183  pp.getarr("sample_line_hi_latlon", m_latlon_hi, 0, 3*nline);
184  m_bnd_rbx.resize(nline);
185  m_bnd_bx.resize(nline);
186  }
187  }
188 
189  if (nline > 0) {
190  // Parse directionality
191  m_dir.resize(n_line_dir);
192  pp.queryarr("sample_line_dir",m_dir,0,n_line_dir);
193 
194  // A line runs along one direction: its two ends must agree in the other two, or
195  // the sampler would take a slab of cells and add them up at each point
196  for (int iline(0); iline<nline; ++iline) {
197  const int dir = m_dir[iline];
198  if (dir < 0 || dir >= AMREX_SPACEDIM) {
199  amrex::Abort("erf.sample_line_dir must be 0, 1 or 2");
200  }
201  if (m_use_latlon) { continue; }
202  for (int d(0); d<AMREX_SPACEDIM; ++d) {
203  const bool differ = has_line_idx
204  ? (m_bnd_bx[iline].smallEnd(d) != m_bnd_bx[iline].bigEnd(d))
205  : (m_bnd_rbx[iline].lo(d) != m_bnd_rbx[iline].hi(d));
206  const bool reversed = has_line_idx
207  ? (m_bnd_bx[iline].smallEnd(d) > m_bnd_bx[iline].bigEnd(d))
208  : (m_bnd_rbx[iline].lo(d) > m_bnd_rbx[iline].hi(d));
209  if (d != dir && differ) {
210  amrex::Abort("Line " + std::to_string(iline) + " of erf.sample_line_lo/hi"
211  + std::string(has_line_idx ? "" : "_real")
212  + " is not along erf.sample_line_dir: its ends differ in direction "
213  + std::to_string(d));
214  }
215  if (d == dir && reversed) {
216  amrex::Abort("Line " + std::to_string(iline) + " of erf.sample_line_lo/hi"
217  + std::string(has_line_idx ? "" : "_real")
218  + " has its lo end above its hi end");
219  }
220  }
221  }
222 
223  // Parse names
224  std::string name_base = "plt_line_";
225  m_name.resize(nline);
226  int n_names = pp.countval("sample_line_name");
227  if (n_names > 0) {
228  AMREX_ALWAYS_ASSERT( n_names==nline );
229  pp.queryarr("sample_line_name",m_name,0,n_names);
230  } else {
231  for (int iline(0); iline<nline; ++iline) {
232  m_name[iline] = amrex::Concatenate(name_base, iline , 5);
233  }
234  }
235 
236  // Allocate space for level indicator
237  m_lev.resize(n_line_dir,0);
238 
239  // Allocate space for MF pointers
240  m_ls_mf.resize(nline);
241 
242  // Get requested vars. They are stored, sampled and written in the order of
243  // line_sampling_var_order(), whatever order they are requested in
244  if (pp.countval("line_sampling_vars") > 0) {
245  m_varnames.clear();
246  amrex::Vector<std::string> requested_vars;
247  pp.queryarr("line_sampling_vars",requested_vars);
248  amrex::Print() << "Selected line sampling vars :";
249  for (const auto& name : line_sampling_var_order()) {
250  if (containerHasElement(requested_vars, name)) {
251  m_varnames.push_back(name);
252  amrex::Print() << " " << name;
253  }
254  }
255  amrex::Print() << std::endl;
256  }
257 
258  // Write outputs to text files, one file per variable, with all
259  // times appended to the same file. The files are opened at the first
260  // write, once "conserved" has been expanded into its components
261  pp.queryAdd("line_sampling_text_output",m_write_ascii);
262 
263  // The z of erf.sample_line_lo_real/hi_real of a vertical line is the height above
264  // the ground under it instead of the height above sea level
265  pp.queryAdd("sample_line_z_agl",m_z_agl);
266 
267  // Interpolate to the requested heights rather than take the cells that hold them:
268  // a vertical line at erf.sample_line_npts heights from its lo z to its hi z, a
269  // horizontal line at its z in every column it crosses
270  pp.queryAdd("sample_line_interp",m_interp);
271  if (m_interp) {
272  if (!m_use_real_bx) {
273  amrex::Abort("erf.sample_line_interp needs lines given by heights, with "
274  "erf.sample_line_lo_real/hi_real or erf.sample_line_lo_latlon/hi_latlon; "
275  "a line given by erf.sample_line_lo/hi names cells, not heights");
276  }
277  m_npts.assign(nline, 0);
278  const int n_npts = pp.countval("sample_line_npts");
279  if (n_npts > 0) {
280  if (n_npts != nline) {
281  amrex::Abort("erf.sample_line_npts must give one value per line (0 for a horizontal line)");
282  }
283  pp.getarr("sample_line_npts", m_npts, 0, nline);
284  } else {
285  // A vertical line whose two ends are at one height is sampled at that height
286  for (int iline(0); iline<nline; ++iline) {
287  if (m_dir[iline] == 2) { m_npts[iline] = 1; }
288  }
289  }
290  // The heights of lines given by latitude and longitude are checked once they are placed
291  if (!m_use_latlon) {
292  for (int iline(0); iline<nline; ++iline) { check_interp_heights(iline); }
293  }
294  } else if (pp.contains("sample_line_npts")) {
295  amrex::Abort("erf.sample_line_npts is used only with erf.sample_line_interp = true");
296  }
297 
298  // The cells of a line placed by height are found once, so they do not follow a
299  // mesh that moves; lines given by indices do not use heights
301  SolverChoice::terrain_type == TerrainType::MovingFittedMesh) {
302  amrex::Abort("Line sampling with erf.sample_line_lo_real/hi_real is not supported "
303  "with erf.terrain_type = MovingFittedMesh");
304  }
305 
306  // Where the levels are not equally spaced, the z of the plotfile of a line is not
307  // the height of its samples, so the height is written with them; an interpolated
308  // line writes the height of each sample above sea level on any mesh
309  if ((SolverChoice::mesh_type != MeshType::ConstantDz || m_interp) &&
310  !containerHasElement(m_varnames, std::string("z"))) {
311  const bool cons_first = !m_varnames.empty() && m_varnames[0] == "conserved";
312  m_varnames.insert(m_varnames.begin() + (cons_first ? 1 : 0), "z");
313  }
314  }
315  }
316 
317  /**
318  * Sample the full-depth vertical column through each cell on level 0, with the
319  * variables erf.sample_line writes: every conserved component, the cell-centered
320  * velocities and the six stresses at cell centers. Each column is written to its own
321  * log, one row per sample: the time, then each variable over the column, bottom to top.
322  *
323  * @param[in] cells a cell in each column; its k is ignored
324  * @param[in] log_names the log file of each column
325  * @param[in] domain the level-0 domain
326  */
327  LineSampler (const amrex::Vector<amrex::IntVect>& cells,
328  const amrex::Vector<std::string>& log_names,
329  const amrex::Box& domain)
330  {
331  AMREX_ALWAYS_ASSERT(cells.size() == log_names.size());
332  const int nline = static_cast<int>(cells.size());
333  constexpr int dir = 2;
334 
335  m_bnd_bx.resize(nline);
336  for (int iline(0); iline<nline; ++iline) {
337  amrex::IntVect lo(cells[iline]); lo[dir] = domain.smallEnd(dir);
338  amrex::IntVect hi(cells[iline]); hi[dir] = domain.bigEnd(dir);
339  m_bnd_bx[iline] = amrex::Box(lo, hi);
340  }
341  m_dir.assign(nline, dir);
342  m_lev.assign(nline, 0);
343  m_ls_mf.resize(nline);
344  m_name = log_names;
345  m_max_level = 0;
346  m_varnames = {"conserved", "x_velocity", "y_velocity", "z_velocity",
347  "sgs_tau11", "sgs_tau12", "sgs_tau13", "sgs_tau22", "sgs_tau23", "sgs_tau33"};
348 
349  open_logs(log_names);
350  }
351 
352  /**
353  * Sample single cells with every conserved component, as erf.sample_point does: each
354  * point is written to its own log, one row per sample, the time and then each component.
355  * A point given by a cell (erf.sample_point) is that cell of level 0. A point given by
356  * real coordinates (erf.sample_point_real, or erf.sample_point_latlon once placed) is a
357  * line of one cell, sampled on the finest level that holds it; on a stretched or
358  * terrain-fitted mesh its cell is found from the heights of its column, and with z_agl
359  * its z is the height above the ground under it.
360  *
361  * @param[in] cells the cell of each point given by indices, or empty
362  * @param[in] points the (x,y,z) of each point given by real coordinates, or empty
363  * @param[in] log_names the log file of each point
364  * @param[in] z_agl whether the z of points are heights above the ground
365  */
366  LineSampler (const amrex::Vector<amrex::IntVect>& cells,
367  const amrex::Vector<amrex::RealVect>& points,
368  const amrex::Vector<std::string>& log_names,
369  bool z_agl)
370  {
371  AMREX_ALWAYS_ASSERT(cells.empty() || points.empty());
372  const int nline = static_cast<int>(log_names.size());
373  AMREX_ALWAYS_ASSERT(static_cast<int>(cells.size() + points.size()) == nline);
374 
375  m_bnd_bx.resize(nline);
376  if (!cells.empty()) {
377  for (int iline(0); iline<nline; ++iline) {
378  m_bnd_bx[iline] = amrex::Box(cells[iline], cells[iline]);
379  }
380  m_max_level = 0;
381  } else {
382  m_use_real_bx = true;
383  m_z_agl = z_agl;
384  m_bnd_rbx.resize(nline);
385  for (int iline(0); iline<nline; ++iline) {
386  m_bnd_rbx[iline] = amrex::RealBox(points[iline].dataPtr(), points[iline].dataPtr());
387  }
388  // As for lines placed by height, the cells are found once
389  if (use_column_heights() && SolverChoice::terrain_type == TerrainType::MovingFittedMesh) {
390  amrex::Abort("erf.sample_point_real and erf.sample_point_latlon are not supported "
391  "with erf.terrain_type = MovingFittedMesh");
392  }
393  }
394  // A point is a vertical line of one cell
395  m_dir.assign(nline, 2);
396  m_lev.assign(nline, 0);
397  m_ls_mf.resize(nline);
398  m_name = log_names;
399  m_varnames = {"conserved"};
400 
401  open_logs(log_names);
402  }
403 
404  //! Open one log per line, appended to, on the I/O rank
405  void
406  open_logs (const amrex::Vector<std::string>& log_names)
407  {
408  m_write_log = true;
409  if (amrex::ParallelDescriptor::IOProcessor()) {
410  const int nline = static_cast<int>(log_names.size());
411  m_logstream.resize(nline);
412  for (int iline(0); iline<nline; ++iline) {
413  m_logstream[iline] = std::make_unique<std::fstream>();
414  m_logstream[iline]->open(log_names[iline].c_str(),std::ios::out|std::ios::app);
415  if (!m_logstream[iline]->good()) {
416  amrex::FileOpenFailed(log_names[iline]);
417  }
418  }
419  }
420  amrex::ParallelDescriptor::Barrier("LineSampler::open_logs");
421  }
422 
423  //! The variables that may be sampled, in the order they are stored and written
424  static const amrex::Vector<std::string>&
426  {
427  static const amrex::Vector<std::string> order = {
428  "conserved", "z", "volfrac", "density", "x_velocity", "y_velocity", "z_velocity", "magvel", "theta",
429  "qv", "qc", "pressure", "sgs_tke",
430  "sgs_tau11", "sgs_tau12", "sgs_tau13", "sgs_tau22", "sgs_tau23", "sgs_tau33", "sgs_hfx3"};
431  return order;
432  }
433 
434  //! Replace "conserved" by one name per component of the conserved state, cons_0, cons_1, ...
435  void
436  expand_conserved (int ncons)
437  {
438  auto it = std::find(m_varnames.begin(), m_varnames.end(), std::string("conserved"));
439  if (it == m_varnames.end()) { m_ncons = 0; return; }
440  amrex::Vector<std::string> names;
441  for (int n(0); n<ncons; ++n) { names.push_back(amrex::Concatenate("cons_", n, 1)); }
442  it = m_varnames.erase(it);
443  m_varnames.insert(it, names.begin(), names.end());
444  m_ncons = ncons;
445  }
446 
447  void
449  {
450  if (!m_datastream.empty() || !amrex::ParallelDescriptor::IOProcessor()) { return; }
451  int nline = static_cast<int>(m_name.size());
452  int nvar = static_cast<int>(m_varnames.size());
453  m_datastream.resize(nline * nvar);
454  int i = 0;
455  for (int iline(0); iline<nline; ++iline) {
456  for (int ivar(0); ivar<nvar; ++ivar) {
457  std::string filename = m_name[iline] + "." + m_varnames[ivar];
458  m_datastream[i] = std::make_unique<std::fstream>();
459  m_datastream[i]->open(filename.c_str(),std::ios::out|std::ios::app);
460  if (!m_datastream[i]->good()) {
461  amrex::FileOpenFailed(filename);
462  }
463  i++;
464  }
465  }
466  }
467 
468  //! Whether the lines are given by latitude and longitude (erf.sample_line_lo/hi_latlon)
469  [[nodiscard]] bool
470  uses_latlon () const
471  {
472  return m_use_latlon;
473  }
474 
475  /**
476  * Turn the ends of the lines given by latitude and longitude into domain coordinates,
477  * with the level-0 latitude/longitude map of the run, and check them. A vertical line
478  * gives one lat/lon at both ends. The two ends of a horizontal line are in general not
479  * on one row of cells: they must lie within one cell of each other across the line,
480  * which is then sampled along the line of cells through its lo end. Collective.
481  */
482  void
483  resolve_latlon (const LatLonMap& latlon_map, const amrex::Geometry& geom0)
484  {
485  const int nline = static_cast<int>(m_bnd_rbx.size());
486  for (int iline(0); iline<nline; ++iline) {
487  const int dir = m_dir[iline];
488  const std::string line = "Line " + std::to_string(iline) + " of erf.sample_line_lo/hi_latlon";
489  const amrex::Real* req[2] = {&m_latlon_lo[3*iline], &m_latlon_hi[3*iline]};
490 
491  if (dir == 2) {
492  if (req[0][0] != req[1][0] || req[0][1] != req[1][1]) {
493  amrex::Abort(line + " is vertical (erf.sample_line_dir = 2), so its two ends"
494  " must give the same latitude and longitude");
495  }
496  if (req[0][2] > req[1][2]) {
497  amrex::Abort(line + " has its lo end above its hi end");
498  }
499  } else if (req[0][2] != req[1][2]) {
500  amrex::Abort(line + " is horizontal, so its two ends must give the same z");
501  }
502 
503  // A vertical line has one lat/lon
504  const int nend = (dir == 2) ? 1 : 2;
505  LatLonLocation where[2];
506  for (int e(0); e<nend; ++e) {
507  where[e] = latlon_map.locate_or_abort(req[e][0], req[e][1],
508  line + (e == 0 ? ", lo end," : ", hi end,"));
509  }
510  if (dir == 2) { where[1] = where[0]; }
511 
512  amrex::Real lo[AMREX_SPACEDIM] = {where[0].x, where[0].y, req[0][2]};
513  amrex::Real hi[AMREX_SPACEDIM] = {where[1].x, where[1].y, req[1][2]};
514  amrex::Real snap = amrex::Real(0.0);
515  if (dir != 2) {
516  const int across = 1 - dir;
517  snap = hi[across] - lo[across];
518  const amrex::Real gap = std::abs(snap) / geom0.CellSize(across);
519  if (gap > amrex::Real(1.0)) {
520  amrex::Abort(line + " is not along erf.sample_line_dir: its ends are " +
521  std::to_string(gap) + " cells apart in direction " +
522  std::to_string(across) + ", and a line between two lat/lon"
523  " points is sampled along one grid direction, so its ends may"
524  " be at most one cell apart across it");
525  }
526  if (hi[dir] < lo[dir]) {
527  amrex::Abort(line + " has its lo end beyond its hi end in direction " +
528  std::to_string(dir) + "; swap the two ends");
529  }
530  hi[across] = lo[across];
531  }
532  m_bnd_rbx[iline] = amrex::RealBox(lo, hi);
533  if (m_interp) { check_interp_heights(iline); }
534 
535  amrex::Print() << line << ": lo end at lat=" << where[0].lat << " long=" << where[0].lon
536  << " (x=" << lo[0] << " y=" << lo[1] << ")";
537  if (dir != 2) {
538  amrex::Print() << ", hi end at lat=" << where[1].lat << " long=" << where[1].lon
539  << " (x=" << hi[0] << " y=" << hi[1] << "), sampled along the line of cells"
540  << " through the lo end, " << std::abs(snap) << " m from the hi end";
541  }
542  amrex::Print() << std::endl;
543  }
544  }
545 
546  //! Whether the sampler needs the ground under its lines (erf.sample_line_z_agl, or
547  //! erf.sample_line_interp, which writes the samples below the ground as missing)
548  [[nodiscard]] bool
549  needs_ground () const
550  {
551  return m_use_real_bx && (m_z_agl || m_interp);
552  }
553 
554  //! Abort unless the number of heights of line iline suits its ends (erf.sample_line_interp)
555  void
556  check_interp_heights (int iline) const
557  {
558  const std::string line = "Line " + std::to_string(iline) + " of erf.sample_line_interp";
559  const int n = m_npts[iline];
560  const amrex::RealBox& rb = m_bnd_rbx[iline];
561  if (m_dir[iline] != 2) {
562  if (n != 0) {
563  amrex::Abort(line + " is horizontal, so it is sampled once in every column it crosses: "
564  "its erf.sample_line_npts must be 0");
565  }
566  } else if (n < 1) {
567  amrex::Abort(line + " is vertical: give erf.sample_line_npts, the number of heights (at least 1)");
568  } else if (n == 1 && rb.lo(2) != rb.hi(2)) {
569  amrex::Abort(line + " has one height, so its lo z and hi z must be equal; give erf.sample_line_npts "
570  "of at least 2 for heights from its lo z to its hi z");
571  } else if (n > 1 && !(rb.lo(2) < rb.hi(2))) {
572  amrex::Abort(line + " has " + std::to_string(n) + " heights, so its lo z must be below its hi z");
573  }
574  }
575 
576  //! The requested heights of line iline (erf.sample_line_interp): erf.sample_line_npts evenly
577  //! spaced from lo z to hi z for a vertical line, its one z for a horizontal line
578  [[nodiscard]] amrex::Vector<amrex::Real>
579  interp_heights (int iline) const
580  {
581  const amrex::RealBox& rb = m_bnd_rbx[iline];
582  if (m_dir[iline] != 2) { return {rb.lo(2)}; }
583  const int n = m_npts[iline];
584  amrex::Vector<amrex::Real> h(n, rb.lo(2));
585  for (int m(1); m<n; ++m) {
586  h[m] = rb.lo(2) + (rb.hi(2) - rb.lo(2)) * amrex::Real(m) / amrex::Real(n - 1);
587  }
588  return h;
589  }
590 
591  //! The samples of line iline, along its direction: its heights (vertical) or its columns (horizontal)
592  [[nodiscard]] amrex::Box
593  interp_sample_box (int iline, const amrex::Box& cols) const
594  {
595  if (m_dir[iline] != 2) { return cols; }
596  amrex::IntVect lo(cols.smallEnd()); lo[2] = 0;
597  amrex::IntVect hi(cols.smallEnd()); hi[2] = m_npts[iline] - 1;
598  return amrex::Box(lo, hi);
599  }
600 
601  /**
602  * Components scomp .. scomp+ncomp-1 of src over the cells (or nodes) of bx, in the order of
603  * a FArrayBox on bx, on the I/O rank, and on every rank with to_all. Collective.
604  */
605  static amrex::Vector<amrex::Real>
606  gather_box (const amrex::MultiFab& src, int scomp, int ncomp, const amrex::Box& bx, bool to_all)
607  {
608  const int io = amrex::ParallelDescriptor::IOProcessorNumber();
609  amrex::MultiFab dst(amrex::BoxArray(bx), amrex::DistributionMapping(amrex::Vector<int>{io}),
610  ncomp, 0, amrex::MFInfo().SetArena(amrex::The_Pinned_Arena()));
611  dst.setVal(amrex::Real(0.0));
612  dst.ParallelCopy(src, scomp, 0, ncomp);
613  amrex::Gpu::streamSynchronize();
614  amrex::Vector<amrex::Real> v(static_cast<std::size_t>(bx.numPts()) * ncomp, amrex::Real(0.0));
615  if (amrex::ParallelDescriptor::IOProcessor()) {
616  const amrex::Real* p = dst[0].dataPtr();
617  std::copy(p, p + v.size(), v.begin());
618  }
619  if (to_all) { amrex::ParallelDescriptor::Bcast(v.data(), v.size(), io); }
620  return v;
621  }
622 
623  /**
624  * Where the samples of a line with erf.sample_line_interp come from on one level, with the
625  * grids that was found on
626  */
627  struct InterpPlan
628  {
629  amrex::BoxArray ba; //!< the grids of the level
630  bool holds{false}; //!< the level holds every cell the samples read
631  amrex::Box cols; //!< the columns of the line on the level (k = the domain's bottom)
632  amrex::Box cells; //!< the cells the samples read (empty if none)
633  amrex::Vector<amrex::Real> zt; //!< the height of each sample above sea level
634  amrex::Vector<int> col, k0, k1; //!< per sample: its column along the line, the cells it reads
635  amrex::Vector<amrex::Real> w; //!< the weight of k1
636  amrex::Vector<char> missing; //!< the sample is below the ground or above the top, or no fluid brackets it
637  };
638 
639  /**
640  * The samples of line iline on a level (erf.sample_line_interp). Each sample is in a column of
641  * cells of the level: the one that holds the line (vertical), or each one it crosses
642  * (horizontal). Its height is the requested one, above sea level or, with
643  * erf.sample_line_z_agl, above the ground under the column. The ground and the top of a
644  * column are those of the level, the means of the four nodes under and over it, where the
645  * level holds the column's bottom or top cell; elsewhere the ground is the level-0 ground,
646  * bilinear at the column's center (a level that stops above the ground). Its value is
647  * linear in height between the two cell centers of the column that bracket it; between the
648  * ground and the lowest cell center with fluid it is that cell's, and between the top cell
649  * center and the top the top cell's. A sample below the ground or above the top is
650  * missing. The level holds the line if it holds every cell the samples read; level 0
651  * always does. Collective: every rank gets the same plan.
652  *
653  * @param[in] z_nd the nodal heights of the level, or nullptr (the top is then prob_hi)
654  * @param[in] volfrac the volume fraction of the level with immersed terrain, or nullptr
655  * @param[in] ground the ground of the level (z_nd, or the immersed terrain surface), or nullptr
656  * @param[in] ground0 the ground of level 0, or nullptr (the ground is then prob_lo)
657  */
658  InterpPlan
659  make_interp_plan (int iline, int lev, const amrex::Geometry& geom, const amrex::Geometry& geom0,
660  const amrex::MultiFab& z_cc, const amrex::MultiFab* z_nd,
661  const amrex::MultiFab* volfrac, const amrex::MultiFab* ground,
662  const amrex::MultiFab* ground0) const
663  {
664  InterpPlan plan;
665  plan.ba = z_cc.boxArray();
666  plan.cols = lineIndexBoxXY(m_bnd_rbx[iline], geom);
667  const amrex::Box& dom = geom.Domain();
668  const int kdlo = dom.smallEnd(2);
669  const int nz = dom.length(2);
670  const int ncol = static_cast<int>(plan.cols.numPts());
671  const int dir = m_dir[iline];
672  const int along = (dir == 2) ? 0 : dir;
673  const amrex::IntVect clo = plan.cols.smallEnd();
674 
675  // The heights of the cell centers of the columns, which cells the level holds, and their
676  // volume fractions, from the bottom of the domain to its top
677  amrex::Box colbox(plan.cols);
678  colbox.setSmall(2, kdlo);
679  colbox.setBig(2, dom.bigEnd(2));
680  amrex::MultiFab t(z_cc.boxArray(), z_cc.DistributionMap(), 3, 0);
681  amrex::MultiFab::Copy(t, z_cc, 0, 0, 1, 0);
682  t.setVal(amrex::Real(1.0), 1, 1, 0);
683  if (volfrac != nullptr) {
684  amrex::MultiFab::Copy(t, *volfrac, 0, 2, 1, 0);
685  } else {
686  t.setVal(amrex::Real(1.0), 2, 1, 0);
687  }
688  const amrex::Vector<amrex::Real> c = gather_box(t, 0, 3, colbox, true);
689  const auto ccol = static_cast<std::size_t>(colbox.numPts());
690  auto at = [&] (int icol, int k, int n) {
691  return c[static_cast<std::size_t>(icol) + static_cast<std::size_t>(ncol) * k + ccol * n];
692  };
693  auto held_at = [&] (int icol, int k) { return at(icol, k, 1) > amrex::Real(0.5); };
694 
695  // The mean of the four nodes at node level k of mf under or over each column of the level
696  auto column_node_mean = [&] (const amrex::MultiFab& mf, int k) {
697  amrex::IntVect nlo(clo); nlo[2] = k;
698  amrex::IntVect nhi(plan.cols.bigEnd() + amrex::IntVect(1,1,0)); nhi[2] = k;
699  const amrex::Box nbx(nlo, nhi, amrex::IntVect::TheNodeVector());
700  const amrex::Vector<amrex::Real> g = gather_box(mf, 0, 1, nbx, true);
701  const int nnx = nbx.length(0);
702  amrex::Vector<amrex::Real> mean(ncol);
703  for (int icol(0); icol<ncol; ++icol) {
704  amrex::IntVect cell(clo);
705  cell[along] += icol;
706  const int i = cell[0] - nlo[0], j = cell[1] - nlo[1];
707  mean[icol] = amrex::Real(0.25) * (g[i + nnx*j] + g[i+1 + nnx*j] + g[i + nnx*(j+1)] + g[i+1 + nnx*(j+1)]);
708  }
709  return mean;
710  };
711 
712  // The ground under the center of each column, bilinear between the level-0 nodes around
713  // it: for the columns whose bottom the level does not hold
714  amrex::Vector<amrex::Real> ground_col(ncol, geom0.ProbLo(2));
715  if (ground0 != nullptr) {
716  const amrex::Box& dom0 = geom0.Domain();
717  amrex::Vector<int> i0(ncol), j0(ncol);
718  amrex::Vector<amrex::Real> fx(ncol), fy(ncol);
719  for (int icol(0); icol<ncol; ++icol) {
720  amrex::IntVect cell(clo);
721  cell[along] += icol;
722  int* idx[2] = {&i0[icol], &j0[icol]};
723  amrex::Real* f[2] = {&fx[icol], &fy[icol]};
724  for (int d(0); d<2; ++d) {
725  const amrex::Real xc = geom.ProbLo(d) + (cell[d] + amrex::Real(0.5)) * geom.CellSize(d);
726  const amrex::Real s = (xc - geom0.ProbLo(d)) / geom0.CellSize(d);
727  *idx[d] = std::min(std::max(static_cast<int>(std::floor(s)), dom0.smallEnd(d)), dom0.bigEnd(d));
728  *f[d] = std::min(std::max(s - *idx[d], amrex::Real(0.0)), amrex::Real(1.0));
729  }
730  }
731  const amrex::IntVect nlo(*std::min_element(i0.begin(), i0.end()), *std::min_element(j0.begin(), j0.end()),
732  dom0.smallEnd(2));
733  const amrex::IntVect nhi(*std::max_element(i0.begin(), i0.end()) + 1,
734  *std::max_element(j0.begin(), j0.end()) + 1, dom0.smallEnd(2));
735  const amrex::Box nbx(nlo, nhi, amrex::IntVect::TheNodeVector());
736  const amrex::Vector<amrex::Real> g = gather_box(*ground0, 0, 1, nbx, true);
737  const int nnx = nbx.length(0);
738  auto node = [&] (int i, int j) { return g[(i - nlo[0]) + nnx * (j - nlo[1])]; };
739  for (int icol(0); icol<ncol; ++icol) {
740  const int i = i0[icol], j = j0[icol];
741  const amrex::Real wx = fx[icol], wy = fy[icol];
742  ground_col[icol] = (amrex::Real(1.0) - wy) * ((amrex::Real(1.0) - wx) * node(i,j) + wx * node(i+1,j))
743  + wy * ((amrex::Real(1.0) - wx) * node(i,j+1) + wx * node(i+1,j+1));
744  }
745  }
746  // the level's own ground where it holds the column's bottom cell
747  if (ground != nullptr) {
748  const amrex::Vector<amrex::Real> own = column_node_mean(*ground, kdlo);
749  for (int icol(0); icol<ncol; ++icol) {
750  if (held_at(icol, 0)) { ground_col[icol] = own[icol]; }
751  }
752  }
753  // The top of each column, where the level holds its top cell; elsewhere no sample above
754  // the level's highest cell is bracketed, so the level does not hold it
755  amrex::Vector<amrex::Real> top_col(ncol, geom.ProbHi(2));
756  if (z_nd != nullptr) {
757  const amrex::Vector<amrex::Real> own = column_node_mean(*z_nd, dom.bigEnd(2) + 1);
758  for (int icol(0); icol<ncol; ++icol) {
759  if (held_at(icol, nz-1)) { top_col[icol] = own[icol]; }
760  }
761  }
762 
763  // The samples: the heights of a vertical line in its column, or one height in each column
764  const amrex::Vector<amrex::Real> h = interp_heights(iline);
765  const int nsamp = (dir == 2) ? static_cast<int>(h.size()) : ncol;
766  plan.zt.resize(nsamp);
767  plan.col.resize(nsamp);
768  plan.k0.assign(nsamp, 0);
769  plan.k1.assign(nsamp, 0);
770  plan.w.assign(nsamp, amrex::Real(0.0));
771  plan.missing.assign(nsamp, 0);
772  plan.holds = true;
773  int kmin = nz, kmax = -1;
774  for (int s(0); s<nsamp; ++s) {
775  const int icol = (dir == 2) ? 0 : s;
776  const amrex::Real hs = (dir == 2) ? h[s] : h[0];
777  plan.col[s] = icol;
778  plan.zt[s] = hs + (m_z_agl ? ground_col[icol] : amrex::Real(0.0));
779  const amrex::Real z = plan.zt[s];
780  if (z < ground_col[icol] || z > top_col[icol]) { plan.missing[s] = 1; continue; }
781 
782  auto held = [&] (int k) { return held_at(icol, k); };
783  auto fluid = [&] (int k) { return held(k) && at(icol, k, 2) > amrex::Real(0.0); };
784  auto zc = [&] (int k) { return at(icol, k, 0); };
785  int k0 = -1, k1 = -1;
786  amrex::Real w = amrex::Real(0.0);
787  // between two cell centers with fluid
788  for (int k(0); k<nz-1 && k0 < 0; ++k) {
789  if (fluid(k) && fluid(k+1) && zc(k) <= z && z < zc(k+1)) {
790  k0 = k; k1 = k+1; w = (z - zc(k)) / (zc(k+1) - zc(k));
791  }
792  }
793  // between the top cell center and the top
794  if (k0 < 0 && fluid(nz-1) && z >= zc(nz-1)) { k0 = k1 = nz-1; }
795  // between the ground and the lowest cell center with fluid, if the level holds the
796  // column up to it
797  if (k0 < 0) {
798  int kf = 0;
799  while (kf < nz && held(kf) && !fluid(kf)) { ++kf; }
800  if (kf < nz && fluid(kf) && z < zc(kf)) { k0 = k1 = kf; }
801  }
802  if (k0 < 0) {
803  // Level 0 holds every cell, so there no cells with fluid bracket the height (a
804  // column without fluid, or a height under an overhang of immersed terrain); a
805  // finer level does not hold the cells around it
806  if (lev == 0) { plan.missing[s] = 1; continue; }
807  plan.holds = false;
808  return plan;
809  }
810  plan.k0[s] = k0;
811  plan.k1[s] = k1;
812  plan.w[s] = w;
813  kmin = std::min(kmin, k0);
814  kmax = std::max(kmax, k1);
815  }
816  if (kmax >= kmin) {
817  plan.cells = plan.cols;
818  plan.cells.setSmall(2, kdlo + kmin);
819  plan.cells.setBig(2, kdlo + kmax);
820  }
821  return plan;
822  }
823 
824  //! The entry of level lev, or nullptr past the end
825  static const amrex::MultiFab*
826  level_ptr (const amrex::Vector<const amrex::MultiFab*>& v, int lev)
827  {
828  return lev < static_cast<int>(v.size()) ? v[lev] : nullptr;
829  }
830 
831  //! make_interp_plan, kept until the grids of the level change
832  const InterpPlan&
833  cachedInterpPlan (int iline, int lev, const amrex::Geometry& geom, const amrex::Geometry& geom0,
834  const amrex::MultiFab& z_cc, const amrex::MultiFab* z_nd,
835  const amrex::MultiFab* volfrac, const amrex::MultiFab* ground,
836  const amrex::MultiFab* ground0)
837  {
838  if (lev >= static_cast<int>(m_interp_plan.size())) { m_interp_plan.resize(lev+1); }
839  auto& plans = m_interp_plan[lev];
840  const int nline = static_cast<int>(m_bnd_rbx.size());
841  if (static_cast<int>(plans.size()) != nline) { plans.assign(nline, InterpPlan{}); }
842  if (plans[iline].ba.empty() || !(plans[iline].ba == z_cc.boxArray())) {
843  plans[iline] = make_interp_plan(iline, lev, geom, geom0, z_cc, z_nd, volfrac, ground, ground0);
844  }
845  return plans[iline];
846  }
847 
848  /**
849  * The samples of an interpolated line from the variables data of its level, as a MultiFab of
850  * one box (interp_sample_box) on the I/O rank. z is the height of each sample above sea
851  * level; a missing sample has volume fraction 0 and erf.sampling_covered_value elsewhere.
852  * Collective.
853  */
854  amrex::MultiFab
855  interp_line_data (int iline, const InterpPlan& plan, const amrex::MultiFab& data, int z_comp, int vf_comp) const
856  {
857  const int ncomp = data.nComp();
858  const int nsamp = static_cast<int>(plan.zt.size());
859  amrex::Vector<amrex::Real> d;
860  if (plan.cells.ok()) { d = gather_box(data, 0, ncomp, plan.cells, false); }
861 
862  const amrex::Box sbx = interp_sample_box(iline, plan.cols);
863  const int io = amrex::ParallelDescriptor::IOProcessorNumber();
864  amrex::MultiFab out(amrex::BoxArray(sbx), amrex::DistributionMapping(amrex::Vector<int>{io}), ncomp, 0);
865  if (amrex::ParallelDescriptor::IOProcessor()) {
866  const int ncol = static_cast<int>(plan.cols.numPts());
867  const auto ncell = static_cast<std::size_t>(plan.cells.ok() ? plan.cells.numPts() : 0);
868  const int kc0 = plan.cells.ok() ? plan.cells.smallEnd(2) - plan.cols.smallEnd(2) : 0;
869  auto cell = [&] (int icol, int k, int n) {
870  return d[static_cast<std::size_t>(icol) + static_cast<std::size_t>(ncol) * (k - kc0) + ncell * n];
871  };
872  // in the order of the box's FArrayBox, whose samples run along its one long direction
873  amrex::Vector<amrex::Real> v(static_cast<std::size_t>(nsamp) * ncomp);
874  for (int n(0); n<ncomp; ++n) {
875  for (int s(0); s<nsamp; ++s) {
876  amrex::Real val;
877  if (n == z_comp) {
878  val = plan.zt[s];
879  } else if (plan.missing[s] != 0) {
880  val = (n == vf_comp) ? amrex::Real(0.0) : m_covered_value;
881  } else {
882  const amrex::Real w = plan.w[s];
883  val = (amrex::Real(1.0) - w) * cell(plan.col[s], plan.k0[s], n);
884  if (w > amrex::Real(0.0)) { val += w * cell(plan.col[s], plan.k1[s], n); }
885  }
886  v[static_cast<std::size_t>(s) + static_cast<std::size_t>(nsamp) * n] = val;
887  }
888  }
889  amrex::Gpu::copy(amrex::Gpu::hostToDevice, v.begin(), v.end(), out[0].dataPtr());
890  }
891  return out;
892  }
893 
894  //! Whether the z of a line given by real coordinates is found from the heights of a
895  //! column: on a stretched or terrain-fitted mesh, or for a height above ground
896  [[nodiscard]] bool
898  {
899  return m_z_agl || SolverChoice::mesh_type != MeshType::ConstantDz;
900  }
901 
902  /**
903  * The heights of the faces of the column through cell (i,j) on a level, from the nodal
904  * heights z_nd: the lower face of each cell, its upper face, and 1 where the level holds
905  * the cell (0 where it does not). Each is indexed from the bottom of the domain.
906  */
907  static amrex::Vector<amrex::Gpu::HostVector<amrex::Real>>
908  column_face_heights (const amrex::MultiFab& z_nd, const amrex::Geometry& geom,
909  const amrex::IntVect& cell)
910  {
911  const amrex::Box& dom = geom.Domain();
912  amrex::IntVect lo(cell); lo[2] = dom.smallEnd(2);
913  amrex::IntVect hi(cell); hi[2] = dom.bigEnd(2);
914  const amrex::Box col(lo, hi);
915 
916  amrex::MultiFab f(amrex::convert(z_nd.boxArray(), amrex::IntVect(0)),
917  z_nd.DistributionMap(), 3, 0);
918  f.setVal(amrex::Real(0.0));
919  for (amrex::MFIter mfi(f); mfi.isValid(); ++mfi) {
920  const amrex::Box b = mfi.validbox() & col;
921  if (!b.ok()) { continue; }
922  const auto& out = f.array(mfi);
923  const auto& z = z_nd.const_array(mfi);
924  amrex::ParallelFor(b, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
925  out(i,j,k,0) = amrex::Real(0.25) * (z(i,j,k ) + z(i+1,j,k ) + z(i,j+1,k ) + z(i+1,j+1,k ));
926  out(i,j,k,1) = amrex::Real(0.25) * (z(i,j,k+1) + z(i+1,j,k+1) + z(i,j+1,k+1) + z(i+1,j+1,k+1));
927  out(i,j,k,2) = amrex::Real(1.0);
928  });
929  }
930  return {amrex::sumToLine(f, 0, 1, col, 2), amrex::sumToLine(f, 1, 1, col, 2),
931  amrex::sumToLine(f, 2, 1, col, 2)};
932  }
933 
934  /**
935  * The height of the ground under cell (i,j) of level 0: the mean of the four nodes at the
936  * bottom of its column in ground_nd, which is z_phys_nd or, with immersed terrain, the
937  * terrain surface (ERF::station_ground)
938  */
939  static amrex::Real
940  column_ground (const amrex::MultiFab& ground_nd, const amrex::Geometry& geom,
941  const amrex::IntVect& cell)
942  {
943  const int i = cell[0], j = cell[1], k = geom.Domain().smallEnd(2);
944  amrex::ReduceOps<amrex::ReduceOpSum> reduce_op;
945  amrex::ReduceData<amrex::Real> reduce_data(reduce_op);
946  for (amrex::MFIter mfi(ground_nd); mfi.isValid(); ++mfi) {
947  // The nodal box that holds the cell, so that a node shared by two boxes counts once
948  const amrex::Box& vb = mfi.validbox();
949  if (i < vb.smallEnd(0) || i >= vb.bigEnd(0) ||
950  j < vb.smallEnd(1) || j >= vb.bigEnd(1) ||
951  k < vb.smallEnd(2) || k > vb.bigEnd(2)) { continue; }
952  const auto& z = ground_nd.const_array(mfi);
953  reduce_op.eval(amrex::Box(amrex::IntVect(i,j,k), amrex::IntVect(i,j,k)), reduce_data,
954  [=] AMREX_GPU_DEVICE (int ii, int jj, int kk) -> amrex::GpuTuple<amrex::Real> {
955  return { amrex::Real(0.25) * (z(ii,jj,kk) + z(ii+1,jj,kk) + z(ii,jj+1,kk) + z(ii+1,jj+1,kk)) };
956  });
957  }
958  amrex::Real ground = amrex::get<0>(reduce_data.value(reduce_op));
959  amrex::ParallelAllReduce::Sum(ground, amrex::ParallelContext::CommunicatorSub());
960  return ground;
961  }
962 
963  //! The ground under line iline, found once: level 0 and the terrain do not change
964  amrex::Real
965  line_ground (int iline, const amrex::Geometry& geom0, const amrex::MultiFab* ground_nd)
966  {
967  if (!m_z_agl) { return amrex::Real(0.0); }
968  if (m_ground.empty()) {
969  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ground_nd != nullptr,
970  "erf.sample_line_z_agl needs the height of the ground on level 0");
971  const int nline = static_cast<int>(m_bnd_rbx.size());
972  m_ground.resize(nline);
973  for (int l(0); l<nline; ++l) {
974  const amrex::Box ibx = lineIndexBoxXY(m_bnd_rbx[l], geom0);
975  m_ground[l] = column_ground(*ground_nd, geom0, ibx.smallEnd());
976  }
977  }
978  return m_ground[iline];
979  }
980 
981  /**
982  * lineIndexBox, found once per level and kept until the grids of the level change: the
983  * mesh does not move, so only which cells the level holds can change
984  */
985  const amrex::Box&
986  cachedLineIndexBox (int iline, int lev, const amrex::Geometry& geom,
987  const amrex::MultiFab& z_nd, amrex::Real ground)
988  {
989  if (lev >= static_cast<int>(m_box_cache.size())) {
990  m_box_cache.resize(lev+1);
991  m_box_cache_found.resize(lev+1);
992  m_box_cache_ba.resize(lev+1);
993  }
994  const int nline = static_cast<int>(m_bnd_rbx.size());
995  if (static_cast<int>(m_box_cache[lev].size()) != nline || !(m_box_cache_ba[lev] == z_nd.boxArray())) {
996  m_box_cache[lev].assign(nline, amrex::Box());
997  m_box_cache_found[lev].assign(nline, 0);
998  m_box_cache_ba[lev] = z_nd.boxArray();
999  }
1000  if (m_box_cache_found[lev][iline] == 0) {
1001  m_box_cache[lev][iline] = lineIndexBox(iline, geom, z_nd, ground);
1002  m_box_cache_found[lev][iline] = 1;
1003  }
1004  return m_box_cache[lev][iline];
1005  }
1006 
1007  /**
1008  * The cells of a line given by real coordinates on a level, from the face heights of the
1009  * column through its first point. A vertical line runs from the cell that holds the lower
1010  * z to the cell that holds the upper z (each clamped to the column); a horizontal line is
1011  * the row of cells at the level of cells that holds z in that column, so it follows the
1012  * terrain-fitted mesh. With erf.sample_line_z_agl, ground is added to z. The box is
1013  * empty if the level does not hold these cells of the column.
1014  */
1015  amrex::Box
1016  lineIndexBox (int iline, const amrex::Geometry& geom, const amrex::MultiFab& z_nd,
1017  amrex::Real ground)
1018  {
1019  const amrex::RealBox& rb = m_bnd_rbx[iline];
1020  const amrex::Box ibx = lineIndexBoxXY(rb, geom);
1021  const amrex::IntVect cell = ibx.smallEnd();
1022  const auto h = column_face_heights(z_nd, geom, cell);
1023  const auto& zl = h[0];
1024  const auto& zu = h[1];
1025  const auto& held = h[2];
1026  const int n = static_cast<int>(zl.size());
1027  const int kdlo = geom.Domain().smallEnd(2);
1028  const amrex::Real offset = m_z_agl ? ground : amrex::Real(0.0);
1029 
1030  // The cell (from the bottom) that holds height z, or -1 where the level does not hold it
1031  auto find_cell = [&] (amrex::Real z) -> int {
1032  if (held[0] > amrex::Real(0.5) && z < zl[0]) { return 0; }
1033  if (held[n-1] > amrex::Real(0.5) && z >= zu[n-1]) { return n-1; }
1034  for (int m = 0; m < n; ++m) {
1035  if (held[m] > amrex::Real(0.5) && zl[m] <= z && z < zu[m]) { return m; }
1036  }
1037  return -1;
1038  };
1039  const int mlo = find_cell(rb.lo(2) + offset);
1040  const int mhi = (m_dir[iline] == 2) ? find_cell(rb.hi(2) + offset) : mlo;
1041  if (mlo < 0 || mhi < 0 || mhi < mlo) { return amrex::Box(); }
1042  for (int m = mlo; m <= mhi; ++m) {
1043  if (held[m] < amrex::Real(0.5)) { return amrex::Box(); }
1044  }
1045  amrex::IntVect lo(ibx.smallEnd()); lo[2] = kdlo + mlo;
1046  amrex::IntVect hi(ibx.bigEnd()); hi[2] = kdlo + mhi;
1047  return amrex::Box(lo, hi);
1048  }
1049 
1050  //! The cells of a real box in x and y on a level, with k = 0: its k comes from heights
1051  static amrex::Box
1052  lineIndexBoxXY (const amrex::RealBox& rb, const amrex::Geometry& geom)
1053  {
1054  amrex::IntVect lo, hi;
1055  for (int d(0); d<2; ++d) {
1056  lo[d] = static_cast<int>(std::floor((rb.lo(d) - geom.ProbLo(d))/geom.CellSize(d)));
1057  hi[d] = static_cast<int>(std::floor((rb.hi(d) - geom.ProbLo(d))/geom.CellSize(d)));
1058  lo[d] = std::max(lo[d], geom.Domain().smallEnd(d));
1059  hi[d] = std::min(hi[d], geom.Domain().bigEnd(d));
1060  }
1061  lo[2] = hi[2] = geom.Domain().smallEnd(2);
1062  return amrex::Box(lo, hi);
1063  }
1064 
1065  amrex::Box
1066  getIndexBox (const amrex::RealBox& real_box,
1067  const amrex::Geometry& geom) {
1068  amrex::IntVect slice_lo, slice_hi;
1069 
1070  AMREX_D_TERM(slice_lo[0]=static_cast<int>(std::floor((real_box.lo(0) - geom.ProbLo(0))/geom.CellSize(0)));,
1071  slice_lo[1]=static_cast<int>(std::floor((real_box.lo(1) - geom.ProbLo(1))/geom.CellSize(1)));,
1072  slice_lo[2]=static_cast<int>(std::floor((real_box.lo(2) - geom.ProbLo(2))/geom.CellSize(2))););
1073 
1074  AMREX_D_TERM(slice_hi[0]=static_cast<int>(std::floor((real_box.hi(0) - geom.ProbLo(0))/geom.CellSize(0)));,
1075  slice_hi[1]=static_cast<int>(std::floor((real_box.hi(1) - geom.ProbLo(1))/geom.CellSize(1)));,
1076  slice_hi[2]=static_cast<int>(std::floor((real_box.hi(2) - geom.ProbLo(2))/geom.CellSize(2))););
1077 
1078  return amrex::Box(slice_lo, slice_hi) & geom.Domain();
1079  }
1080 
1081  void
1082  write_coords (amrex::Vector<std::unique_ptr<amrex::MultiFab> >& z_phys_cc,
1083  amrex::Vector<std::unique_ptr<amrex::MultiFab> >& z_phys_nd,
1084  amrex::Vector<amrex::Geometry>& geom,
1085  const amrex::MultiFab* ground_nd = nullptr,
1086  const amrex::Vector<const amrex::MultiFab*>& ground_lev = {},
1087  const amrex::Vector<const amrex::MultiFab*>& volfrac_lev = {})
1088  {
1089  if (!m_write_ascii) return;
1090 
1091  // Only the levels that are sampled
1092  int nlev = static_cast<int>(z_phys_cc.size());
1093  if (m_max_level >= 0) { nlev = std::min(nlev, m_max_level + 1); }
1094  for (int lev(0); lev < nlev; ++lev) {
1095  // A level allowed by amr.max_level that has not been made has no heights, and
1096  // neither does any level above it
1097  if (!z_phys_cc[lev] || lev >= static_cast<int>(z_phys_nd.size()) || !z_phys_nd[lev]) { break; }
1098 
1099  // Written for each set of grids of the level: when the level is made, and again
1100  // when a regrid changes its grids
1101  if (lev >= static_cast<int>(m_zcc_ba.size())) { m_zcc_ba.resize(lev+1); }
1102  if (m_zcc_ba[lev] == z_phys_cc[lev]->boxArray()) { continue; }
1103  m_zcc_ba[lev] = z_phys_cc[lev]->boxArray();
1104 
1105  amrex::Print() << "Writing out line coordinates of level " << lev << " to text" << std::endl;
1106 
1107  // Write one text file per level
1108  std::ofstream outfile;
1109  if (amrex::ParallelDescriptor::IOProcessor()) {
1110  std::string fname = amrex::Concatenate("plt_line_lev", lev, 1);
1111  fname += ".zcc";
1112  outfile.open(fname);
1113 
1114  if (!outfile.is_open()) {
1115  amrex::AllPrint() << "Could not open " << fname << std::endl;
1116  }
1117  }
1118 
1119  // Loop over each line
1120  int nline = static_cast<int>(m_ls_mf.size());
1121  for (int iline(0); iline<nline; ++iline) {
1122  int dir = m_dir[iline];
1123  if (m_interp) {
1124  // The height of each sample above sea level, where the level holds the line
1125  // (the plan the samples are then taken with)
1126  const InterpPlan& plan = cachedInterpPlan(iline, lev, geom[lev], geom[0], *z_phys_cc[lev],
1127  z_phys_nd[lev].get(), level_ptr(volfrac_lev, lev),
1128  level_ptr(ground_lev, lev), ground_nd);
1129  if (amrex::ParallelDescriptor::IOProcessor()) {
1130  if (plan.holds) {
1131  for (const auto& zval : plan.zt) { outfile << " " << zval; }
1132  }
1133  outfile << std::endl;
1134  }
1135  continue;
1136  }
1137  amrex::Box bnd_bx;
1138  if (m_use_real_bx && use_column_heights()) {
1139  // As in get_sample_data: the cells of the line come from the heights
1140  const amrex::Real ground = line_ground(iline, geom[0], ground_nd);
1141  bnd_bx = cachedLineIndexBox(iline, lev, geom[lev], *z_phys_nd[lev], ground);
1142  if (bnd_bx.isEmpty()) {
1143  // This level does not hold the line
1144  if (amrex::ParallelDescriptor::IOProcessor()) { outfile << std::endl; }
1145  continue;
1146  }
1147  } else {
1148  bnd_bx = (m_use_real_bx) ? getIndexBox(m_bnd_rbx[iline], geom[lev])
1149  : m_bnd_bx[iline];
1150  }
1151  amrex::IntVect first_cell = bnd_bx.smallEnd();
1152 
1153  // Create multifab with "sampled" z_phys values
1154  amrex::MultiFab line_coords_mf = get_line_data(
1155  *z_phys_cc[lev], dir, first_cell, bnd_bx
1156  );
1157 
1158  // Convert multifab to vector
1159  amrex::Gpu::HostVector<amrex::Real> vec = sumToLine(
1160  line_coords_mf, 0, 1, bnd_bx, dir
1161  );
1162 
1163  // Append to file
1164  if (amrex::ParallelDescriptor::IOProcessor()) {
1165  for (const auto& zval : vec) {
1166  outfile << " " << zval;
1167  }
1168  outfile << std::endl;
1169  }
1170  } // line loop
1171 
1172  outfile.close();
1173  } // level loop
1174  }
1175 
1176  void
1177  get_sample_data (amrex::Vector<amrex::Geometry>& geom,
1178  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_new,
1179  const amrex::Vector<amrex::Vector<amrex::MultiFab*>>& tau_lev = {},
1180  const amrex::Vector<amrex::MultiFab*>& hfx3_lev = {},
1181  const amrex::Vector<int>& prognostic_tke_available = {},
1182  const amrex::Vector<amrex::MultiFab*>& z_cc_lev = {},
1183  const amrex::Vector<amrex::MultiFab*>& z_nd_lev = {},
1184  const amrex::MultiFab* ground_nd = nullptr,
1185  const amrex::Vector<const amrex::MultiFab*>& volfrac_lev = {},
1186  const amrex::Vector<const amrex::MultiFab*>& ground_lev = {})
1187  {
1188  if (m_ncons < 0) { expand_conserved(vars_new[0][Vars::cons].nComp()); }
1189 
1190  int nlev = sampling_num_levels(vars_new);
1191  if (m_max_level >= 0) { nlev = std::min(nlev, m_max_level + 1); }
1192  int nline = static_cast<int>(m_bnd_bx.size());
1193  int ncomp = static_cast<int>(m_varnames.size());
1194 
1195  int qv_comp = -1;
1196 
1197  // The stress tau_type on level ilev, or nullptr where it is not stored
1198  auto get_tau = [&] (int ilev, int tau_type) -> amrex::MultiFab* {
1199  if (ilev >= static_cast<int>(tau_lev.size()) ||
1200  tau_type >= static_cast<int>(tau_lev[ilev].size())) { return nullptr; }
1201  return tau_lev[ilev][tau_type];
1202  };
1203 
1204  // For a line given by real coordinates, its cells come from the heights of the column
1205  // through its first point, and a height above ground from the ground under it
1206  const bool by_height = m_use_real_bx && use_column_heights();
1207  if (by_height) {
1208  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!z_nd_lev.empty() && z_nd_lev[0] != nullptr,
1209  "Line sampling by height needs the nodal heights of each level");
1210  }
1211  // An interpolated line is sampled on the finest level that holds every cell it reads
1212  auto interp_plan = [&] (int iline, int ilev) -> const InterpPlan& {
1213  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ilev < static_cast<int>(z_cc_lev.size()) && z_cc_lev[ilev] != nullptr,
1214  "Line sampling with erf.sample_line_interp needs the cell-centre heights of each level");
1215  const amrex::MultiFab* z_nd = ilev < static_cast<int>(z_nd_lev.size()) ? z_nd_lev[ilev] : nullptr;
1216  return cachedInterpPlan(iline, ilev, geom[ilev], geom[0], *z_cc_lev[ilev], z_nd,
1217  level_ptr(volfrac_lev, ilev), level_ptr(ground_lev, ilev), ground_nd);
1218  };
1219  auto line_box = [&] (int iline, int ilev) -> amrex::Box {
1220  if (m_interp) {
1221  const InterpPlan& plan = interp_plan(iline, ilev);
1222  return plan.holds ? plan.cols : amrex::Box();
1223  }
1224  if (by_height) {
1225  if (ilev >= static_cast<int>(z_nd_lev.size()) || z_nd_lev[ilev] == nullptr) { return amrex::Box(); }
1226  return cachedLineIndexBox(iline, ilev, geom[ilev], *z_nd_lev[ilev],
1227  line_ground(iline, geom[0], ground_nd));
1228  }
1229  return (m_use_real_bx) ? getIndexBox(m_bnd_rbx[iline], geom[ilev]) : m_bnd_bx[iline];
1230  };
1231 
1232  // Search each level, from the finest, to get the finest data possible. The data of a
1233  // level are built once for all the lines that are sampled there
1234  amrex::Vector<int> done(nline, 0);
1235  for (int ilev(nlev-1); ilev>=0; --ilev) {
1236  amrex::Vector<int> lines;
1237  amrex::Vector<amrex::Box> boxes;
1238  for (int iline(0); iline<nline; ++iline) {
1239  if (done[iline] != 0) { continue; }
1240  const amrex::Box bnd_bx = line_box(iline, ilev);
1241  if (bnd_bx.isEmpty()) { continue; }
1242  lines.push_back(iline);
1243  boxes.push_back(bnd_bx);
1244  }
1245  if (lines.empty()) { continue; }
1246 
1247  // Construct CC velocities
1248  amrex::MultiFab mf_cc_vel;
1249  auto ba = vars_new[ilev][Vars::cons].boxArray();
1250  auto dm = vars_new[ilev][Vars::cons].DistributionMap();
1251  mf_cc_vel.define(ba, dm, AMREX_SPACEDIM, amrex::IntVect(1,1,1));
1252  average_face_to_cellcenter(mf_cc_vel,0,
1253  amrex::Array<const amrex::MultiFab*,3>{&vars_new[ilev][Vars::xvel],
1254  &vars_new[ilev][Vars::yvel],
1255  &vars_new[ilev][Vars::zvel]});
1256 
1257  // Construct MultiFab holding requested variables
1258  amrex::MultiFab mf_cc_data;
1259  mf_cc_data.define(ba, dm, ncomp, 1);
1260 
1261  int mf_comp = 0;
1262  int z_comp = -1;
1263  int vf_comp = -1;
1264 
1265  // The raw conserved components, cons_0, cons_1, ..., come first
1266  if (m_ncons > 0) {
1267  amrex::MultiFab::Copy(mf_cc_data, vars_new[ilev][Vars::cons], 0, mf_comp, m_ncons, 0);
1268  mf_comp += m_ncons;
1269  }
1270 
1271  // The height of each cell center
1272  if (containerHasElement(m_varnames, std::string("z"))) {
1273  amrex::MultiFab* z_cc = ilev < static_cast<int>(z_cc_lev.size()) ? z_cc_lev[ilev] : nullptr;
1274  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(z_cc != nullptr,
1275  "Line sampling of z needs the cell-centre heights of each level");
1276  amrex::MultiFab::Copy(mf_cc_data, *z_cc, 0, mf_comp, 1, 0);
1277  z_comp = mf_comp;
1278  mf_comp += 1;
1279  }
1280 
1281  // The volume fraction of each cell: 1 without immersed terrain, 0 where covered
1282  const amrex::MultiFab* volfrac = ilev < static_cast<int>(volfrac_lev.size())
1283  ? volfrac_lev[ilev] : nullptr;
1284  if (containerHasElement(m_varnames, std::string("volfrac"))) {
1285  if (volfrac != nullptr) {
1286  amrex::MultiFab::Copy(mf_cc_data, *volfrac, 0, mf_comp, 1, 0);
1287  } else {
1288  mf_cc_data.setVal(1.0, mf_comp, 1, 0);
1289  }
1290  vf_comp = mf_comp;
1291  mf_comp += 1;
1292  }
1293 
1294  if (containerHasElement(m_varnames, "density")) {
1295  amrex::MultiFab::Copy(mf_cc_data, vars_new[ilev][Vars::cons], Rho_comp, mf_comp, 1, 0);
1296  mf_comp += 1;
1297  }
1298 
1299  if (containerHasElement(m_varnames, "x_velocity")) {
1300  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 0, mf_comp, 1, 0);
1301  mf_comp += 1;
1302  }
1303  if (containerHasElement(m_varnames, "y_velocity")) {
1304  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 1, mf_comp, 1, 0);
1305  mf_comp += 1;
1306  }
1307  if (containerHasElement(m_varnames, "z_velocity")) {
1308  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 2, mf_comp, 1, 0);
1309  mf_comp += 1;
1310  }
1311 
1312  if (containerHasElement(m_varnames, "magvel")) {
1313 #ifdef _OPENMP
1314 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1315 #endif
1316  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1317  const amrex::Box& tbx = mfi.tilebox();
1318  auto const& dfab = mf_cc_data.array(mfi);
1319  auto const& vfab = mf_cc_vel.array(mfi);
1320 
1321  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1322  {
1323  dfab(i,j,k,mf_comp) = std::sqrt(vfab(i,j,k,0)*vfab(i,j,k,0)
1324  + vfab(i,j,k,1)*vfab(i,j,k,1)
1325  + vfab(i,j,k,2)*vfab(i,j,k,2)) ;
1326  });
1327  }
1328  mf_comp += 1;
1329  }
1330 
1331  if (containerHasElement(m_varnames, "theta")) {
1332 #ifdef _OPENMP
1333 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1334 #endif
1335  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1336  const amrex::Box& tbx = mfi.tilebox();
1337  auto const& dfab = mf_cc_data.array(mfi);
1338  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1339 
1340  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1341  {
1342  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoTheta_comp) / cfab(i,j,k,Rho_comp);
1343  });
1344  }
1345  mf_comp += 1;
1346  }
1347 
1348  if (containerHasElement(m_varnames, "qv")) {
1349  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vars_new[ilev][Vars::cons].nComp() > RhoQ1_comp,
1350  "qv sampling requested but moisture components not present in state");
1351  if (qv_comp >= 0) AMREX_ALWAYS_ASSERT(qv_comp == mf_comp);
1352  qv_comp = mf_comp;
1353 #ifdef _OPENMP
1354 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1355 #endif
1356  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1357  const amrex::Box& tbx = mfi.tilebox();
1358  auto const& dfab = mf_cc_data.array(mfi);
1359  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1360 
1361  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1362  {
1363  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoQ1_comp) / cfab(i,j,k,Rho_comp);
1364  });
1365  }
1366  mf_comp += 1;
1367  }
1368  if (containerHasElement(m_varnames, "qc")) {
1369  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vars_new[ilev][Vars::cons].nComp() > RhoQ2_comp,
1370  "qc sampling requested but moisture components not present in state");
1371 #ifdef _OPENMP
1372 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1373 #endif
1374  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1375  const amrex::Box& tbx = mfi.tilebox();
1376  auto const& dfab = mf_cc_data.array(mfi);
1377  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1378 
1379  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1380  {
1381  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoQ2_comp) / cfab(i,j,k,Rho_comp);
1382  });
1383  }
1384  mf_comp += 1;
1385  }
1386 
1387  if (containerHasElement(m_varnames, "pressure")) {
1388  if (vars_new[ilev][Vars::cons].nComp() > RhoQ1_comp) {
1389  // moist pressure: use qv from dfab if already sampled, else compute inline
1390 #ifdef _OPENMP
1391 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1392 #endif
1393  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1394  const amrex::Box& tbx = mfi.tilebox();
1395  auto const& dfab = mf_cc_data.array(mfi);
1396  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1397 
1398  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1399  {
1400  amrex::Real qv_val = (qv_comp >= 0) ? dfab(i,j,k,qv_comp)
1401  : cfab(i,j,k,RhoQ1_comp) / cfab(i,j,k,Rho_comp);
1402  dfab(i,j,k,mf_comp) = getPgivenRTh(cfab(i,j,k,RhoTheta_comp), qv_val);
1403  });
1404  }
1405  } else {
1406  // dry pressure
1407 #ifdef _OPENMP
1408 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1409 #endif
1410  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1411  const amrex::Box& tbx = mfi.tilebox();
1412  auto const& dfab = mf_cc_data.array(mfi);
1413  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1414 
1415  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1416  {
1417  dfab(i,j,k,mf_comp) = getPgivenRTh(cfab(i,j,k,RhoTheta_comp));
1418  });
1419  }
1420  }
1421  mf_comp += 1;
1422  }
1423 
1424  if (containerHasElement(m_varnames, "sgs_tke")) {
1425  const bool tke_available =
1426  ilev < static_cast<int>(prognostic_tke_available.size()) &&
1427  prognostic_tke_available[ilev] != 0;
1428  if (!tke_available) {
1429  mf_cc_data.setVal(amrex::Real(0.0), mf_comp, 1, 0);
1430  } else {
1431 #ifdef _OPENMP
1432 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1433 #endif
1434  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1435  const amrex::Box& tbx = mfi.tilebox();
1436  const auto& out = mf_cc_data.array(mfi);
1437  const auto& state = vars_new[ilev][Vars::cons].const_array(mfi);
1438  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
1439  out(i,j,k,mf_comp) = state(i,j,k,RhoKE_comp) / state(i,j,k,Rho_comp);
1440  });
1441  }
1442  }
1443  mf_comp += 1;
1444  }
1445 
1446  // The stresses at cell centers: tau11, tau22 and tau33 are stored there, and the
1447  // others are averaged from the four edges around the cell
1448  auto sample_tau = [&] (const char* name, int tau_type,
1449  amrex::IntVect const& e1, amrex::IntVect const& e2) {
1450  if (!containerHasElement(m_varnames, std::string(name))) { return; }
1451  amrex::MultiFab* tau = get_tau(ilev, tau_type);
1452  if (tau != nullptr) {
1453  const bool at_centre = (e1 == amrex::IntVect(0));
1454  const int comp = mf_comp;
1455  const int ax = e1[0], ay = e1[1], az = e1[2];
1456  const int bx = e2[0], by = e2[1], bz = e2[2];
1457 #ifdef _OPENMP
1458 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1459 #endif
1460  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1461  const amrex::Box& tbx = mfi.tilebox();
1462  const auto& out = mf_cc_data.array(mfi);
1463  const auto& t = tau->const_array(mfi);
1464  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
1465  out(i,j,k,comp) = at_centre ? t(i,j,k) : amrex::Real(0.25) *
1466  (t(i,j,k) + t(i+ax,j+ay,k+az) +
1467  t(i+bx,j+by,k+bz) + t(i+ax+bx,j+ay+by,k+az+bz));
1468  });
1469  }
1470  } else {
1471  mf_cc_data.setVal(0.0, mf_comp, 1, 0);
1472  }
1473  mf_comp += 1;
1474  };
1475  const amrex::IntVect ex(1,0,0), ey(0,1,0), ez(0,0,1), e0(0);
1476  sample_tau("sgs_tau11", TauType::tau11, e0, e0);
1477  sample_tau("sgs_tau12", TauType::tau12, ex, ey);
1478  sample_tau("sgs_tau13", TauType::tau13, ex, ez);
1479  sample_tau("sgs_tau22", TauType::tau22, e0, e0);
1480  sample_tau("sgs_tau23", TauType::tau23, ey, ez);
1481  sample_tau("sgs_tau33", TauType::tau33, e0, e0);
1482 
1483  if (containerHasElement(m_varnames, "sgs_hfx3")) {
1484  amrex::MultiFab* hfx3 = ilev < static_cast<int>(hfx3_lev.size())
1485  ? hfx3_lev[ilev] : nullptr;
1486  if (hfx3 != nullptr) {
1487 #ifdef _OPENMP
1488 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1489 #endif
1490  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1491  const amrex::Box& tbx = mfi.tilebox();
1492  const auto& out = mf_cc_data.array(mfi);
1493  const auto& hfx = hfx3->const_array(mfi);
1494  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
1495  out(i,j,k,mf_comp) = amrex::Real(0.5) * (hfx(i,j,k) + hfx(i,j,k+1));
1496  });
1497  }
1498  } else {
1499  mf_cc_data.setVal(0.0, mf_comp, 1, 0);
1500  }
1501  mf_comp += 1;
1502  }
1503 
1504  // A cell covered by immersed terrain holds no fluid, so every variable but its
1505  // height and volume fraction is written there as erf.sampling_covered_value
1506  if (volfrac != nullptr) {
1507  const amrex::Real covered_value = m_covered_value;
1508 #ifdef _OPENMP
1509 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1510 #endif
1511  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1512  const amrex::Box& tbx = mfi.tilebox();
1513  const auto& out = mf_cc_data.array(mfi);
1514  const auto& vf = volfrac->const_array(mfi);
1515  amrex::ParallelFor(tbx, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept {
1516  if (vf(i,j,k) == amrex::Real(0.0) && n != z_comp && n != vf_comp) {
1517  out(i,j,k,n) = covered_value;
1518  }
1519  });
1520  }
1521  }
1522 
1523  for (int n(0); n<static_cast<int>(lines.size()); ++n) {
1524  const int iline = lines[n];
1525  const amrex::Box& bnd_bx = boxes[n];
1526  m_lev[iline] = ilev;
1527  if (m_interp) {
1528  m_ls_mf[iline] = interp_line_data(iline, interp_plan(iline, ilev), mf_cc_data, z_comp, vf_comp);
1529  done[iline] = 1;
1530  continue;
1531  }
1532  m_ls_mf[iline] = get_line_data(mf_cc_data, m_dir[iline], bnd_bx.smallEnd(), bnd_bx);
1533 
1534  // A line is done once a level holds all of it
1535  if (bnd_bx == m_ls_mf[iline].boxArray().minimalBox()) { done[iline] = 1; }
1536  }
1537  } // ilev
1538 
1539  // Fill bnd_bx if use_rbx
1540  if (m_use_real_bx) {
1541  for (int iline(0); iline<nline; ++iline) {
1542  m_bnd_bx[iline] = m_ls_mf[iline].boxArray().minimalBox();
1543  }
1544  }
1545  }
1546 
1547  void
1548  write_sample_data (amrex::Vector<double>& time,
1549  amrex::Vector<int>& level_steps,
1550  amrex::Vector<amrex::IntVect>& ref_ratio,
1551  amrex::Vector<amrex::Geometry>& geom)
1552  {
1553  if (m_write_log) {
1554  write_line_log(time);
1555  } else if (m_write_ascii) {
1556  write_line_ascii(time);
1557  } else {
1558  write_line_plotfile(time, level_steps, ref_ratio, geom);
1559  }
1560  }
1561 
1562  //! One row per line in its log: the time, then each variable along the line
1563  void
1564  write_line_log (amrex::Vector<double>& time)
1565  {
1566  // same as definitions in ERF.H
1567  constexpr int datwidth = 14;
1568  constexpr int datprecision = 6;
1569 
1570  int nline = static_cast<int>(m_ls_mf.size());
1571  int nvar = static_cast<int>(m_varnames.size());
1572  for (int iline(0); iline<nline; ++iline) {
1573  int dir = m_dir[iline];
1574  double m_time = time[m_lev[iline]];
1575  amrex::Box m_dom = m_bnd_bx[iline];
1576 
1577  amrex::Vector<amrex::Gpu::HostVector<amrex::Real>> vecs(nvar);
1578  for (int ivar(0); ivar<nvar; ++ivar) {
1579  vecs[ivar] = sumToLine(m_ls_mf[iline], ivar, 1, m_dom, dir);
1580  }
1581 
1582  if (amrex::ParallelDescriptor::IOProcessor()) {
1583  std::ostream& fs = *m_logstream[iline];
1584  if (fs.good()) {
1585  fs << std::setw(datwidth) << std::setprecision(datprecision) << m_time;
1586  for (const auto& vec : vecs) {
1587  for (const auto& val : vec) {
1588  fs << std::setw(datwidth) << std::setprecision(datprecision) << val;
1589  }
1590  }
1591  fs << std::endl;
1592  }
1593  }
1594  }
1595  }
1596 
1597  void
1598  write_line_ascii (amrex::Vector<double>& time)
1599  {
1600  // same as definitions in ERF.H
1601  constexpr int datwidth = 14;
1602  constexpr int datprecision = 6;
1603  constexpr int timeprecision = 13;
1604 
1606 
1607  int nline = static_cast<int>(m_ls_mf.size());
1608  int nvar = static_cast<int>(m_varnames.size());
1609  for (int iline(0); iline<nline; ++iline) {
1610  int dir = m_dir[iline];
1611  int lev = m_lev[iline];
1612  double m_time = time[lev];
1613  amrex::Box m_dom = m_bnd_bx[iline];
1614 
1615  for (int ivar(0); ivar<nvar; ++ivar) {
1616  // Convert multifab to vector
1617  amrex::Gpu::HostVector<amrex::Real> vec = sumToLine(m_ls_mf[iline], ivar, 1, m_dom, dir);
1618 
1619  if (amrex::ParallelDescriptor::IOProcessor()) {
1620  int ifile = iline*nvar + ivar;
1621  std::ostream& fs = *m_datastream[ifile];
1622  fs << std::setw(datwidth) << std::setprecision(timeprecision) << m_time
1623  << std::setw(datwidth) << std::setprecision(datprecision);
1624  for (const auto& val : vec) {
1625  fs << " " << val;
1626  }
1627  fs << std::endl;
1628  }
1629  }
1630  }
1631  }
1632 
1633  void
1634  write_line_plotfile (amrex::Vector<double>& time,
1635  amrex::Vector<int>& level_steps,
1636  amrex::Vector<amrex::IntVect>& ref_ratio,
1637  amrex::Vector<amrex::Geometry>& geom)
1638  {
1639  int nline = static_cast<int>(m_ls_mf.size());
1640  for (int iline(0); iline<nline; ++iline) {
1641  // Data members that can be used as-is
1642  int dir = m_dir[iline];
1643  int lev = m_lev[iline];
1644  double m_time = time[lev];
1645  amrex::Vector<int> m_level_steps = {level_steps[lev]};
1646  amrex::Vector<amrex::IntVect> m_ref_ratio = {ref_ratio[lev]};
1647 
1648  // Create modified geometry object corresponding to the line
1649  auto plo = geom[lev].ProbLo();
1650  auto dx = geom[lev].CellSize();
1651  amrex::Vector<amrex::Geometry> m_geom; m_geom.resize(1);
1652  amrex::Vector<int> is_per(AMREX_SPACEDIM,0);
1653  amrex::Box m_dom = m_bnd_bx[iline];
1654  amrex::RealBox m_rb;
1655  for (int d(0); d<AMREX_SPACEDIM; ++d) {
1656  amrex::Real offset = (d==dir) ? 0 : myhalf;
1657  amrex::Real lo = plo[d] + ( m_dom.smallEnd(d) - offset ) * dx[d];
1658  amrex::Real hi = plo[d] + ( m_dom.bigEnd(d) + offset ) * dx[d];
1659 
1660  m_rb.setLo(d,lo);
1661  m_rb.setHi(d,hi);
1662 
1663  is_per[d] = geom[lev].isPeriodic(d);
1664  }
1665  // An interpolated line is at its requested heights (above the ground with
1666  // erf.sample_line_z_agl): its z cells are centered on them
1667  if (m_interp) {
1668  const amrex::Vector<amrex::Real> h = interp_heights(iline);
1669  const int nh = static_cast<int>(h.size());
1670  const amrex::Real dh = (nh > 1) ? (h[nh-1] - h[0]) / amrex::Real(nh - 1) : dx[2];
1671  m_rb.setLo(2, h[0] - myhalf * dh);
1672  m_rb.setHi(2, h[nh-1] + myhalf * dh);
1673  is_per[2] = 0;
1674  }
1675  m_geom[0].define(m_dom, &m_rb, geom[lev].Coord(), is_per.data());
1676 
1677  // Create plotfile name
1678  std::string name_line = m_name[iline];
1679  name_line += "_step_";
1680  std::string plotfilename = amrex::Concatenate(name_line, m_level_steps[0], 5);
1681 
1682  // Get the data
1683  amrex::Vector<const amrex::MultiFab*> mf = {&(m_ls_mf[iline])};
1684 
1685  // Write each line
1686  WriteMultiLevelPlotfile(plotfilename, 1, mf,
1687  m_varnames, m_geom, static_cast<amrex::Real>(m_time),
1688  m_level_steps, m_ref_ratio);
1689  }
1690  }
1691 
1692  amrex::Vector<int> m_dir;
1693  amrex::Vector<int> m_lev;
1694  amrex::Vector<amrex::Box> m_bnd_bx;
1695  amrex::Vector<amrex::RealBox> m_bnd_rbx;
1696  amrex::Vector<amrex::MultiFab> m_ls_mf;
1697  amrex::Vector<std::string> m_name;
1698 
1699  bool m_use_real_bx{false};
1700  bool m_use_latlon{false}; // erf.sample_line_lo/hi_latlon
1701  amrex::Vector<amrex::Real> m_latlon_lo, m_latlon_hi; // lat, long, z of each line's ends
1702  bool m_write_ascii{false};
1703  bool m_write_log{false};
1704  bool m_z_agl{false}; // erf.sample_line_z_agl
1705  bool m_interp{false}; // erf.sample_line_interp
1706  amrex::Vector<int> m_npts; // erf.sample_line_npts: the heights of each vertical line (0 for a horizontal one)
1707  amrex::Real m_covered_value{sampling_covered_value()}; // erf.sampling_covered_value
1708  int m_max_level{-1}; // finest level to sample; -1 for all
1709  int m_ncons{-1}; // components "conserved" expands into; -1 until the first sample
1710  amrex::Vector<std::string> m_varnames {"magvel","theta"};
1711  amrex::Vector<std::unique_ptr<std::fstream> > m_datastream;
1712  amrex::Vector<std::unique_ptr<std::fstream> > m_logstream;
1713 
1714  // The ground under each line, and the cells of each line on each level with the grids
1715  // they were found on (lines given by real coordinates on a stretched or terrain-fitted mesh)
1716  amrex::Vector<amrex::Real> m_ground;
1717  amrex::Vector<amrex::Vector<amrex::Box>> m_box_cache;
1718  amrex::Vector<amrex::Vector<int>> m_box_cache_found;
1719  amrex::Vector<amrex::BoxArray> m_box_cache_ba;
1720  // The grids of each level when its line coordinates were last written (write_coords)
1721  amrex::Vector<amrex::BoxArray> m_zcc_ba;
1722  // The samples of each interpolated line on each level (erf.sample_line_interp)
1723  amrex::Vector<amrex::Vector<InterpPlan>> m_interp_plan;
1724 };
1725 
1726 
1728 {
1730  {
1731  amrex::ParmParse pp("erf");
1732 
1733  // Count number of lo and hi points define the plane
1734  int n_plane_lo = pp.countval("sample_plane_lo") / AMREX_SPACEDIM;
1735  int n_plane_hi = pp.countval("sample_plane_hi") / AMREX_SPACEDIM;
1736  int n_plane_dir = pp.countval("sample_plane_dir");
1737  AMREX_ALWAYS_ASSERT( (n_plane_lo==n_plane_hi ) &&
1738  (n_plane_lo==n_plane_dir) );
1739 
1740  // Parse the data
1741  if (n_plane_lo > 0) {
1742  // Parse lo
1743  amrex::Vector<amrex::Real> r_lo; r_lo.resize(n_plane_lo*AMREX_SPACEDIM);
1744  amrex::Vector<amrex::Vector<amrex::Real>> rv_lo;
1745  pp.queryarr("sample_plane_lo",r_lo,0,n_plane_lo*AMREX_SPACEDIM);
1746  for (int i(0); i < n_plane_lo; i++) {
1747  amrex::Vector<amrex::Real> rv = {r_lo[AMREX_SPACEDIM*i+0],
1748  r_lo[AMREX_SPACEDIM*i+1],
1749  r_lo[AMREX_SPACEDIM*i+2]};
1750  rv_lo.push_back(rv);
1751  }
1752 
1753  // Parse hi
1754  amrex::Vector<amrex::Real> r_hi; r_hi.resize(n_plane_hi*AMREX_SPACEDIM);
1755  amrex::Vector<amrex::Vector<amrex::Real>> rv_hi;
1756  pp.queryarr("sample_plane_hi",r_hi,0,n_plane_hi*AMREX_SPACEDIM);
1757  for (int i(0); i < n_plane_hi; i++) {
1758  amrex::Vector<amrex::Real> rv = {r_hi[AMREX_SPACEDIM*i+0],
1759  r_hi[AMREX_SPACEDIM*i+1],
1760  r_hi[AMREX_SPACEDIM*i+2]};
1761  rv_hi.push_back(rv);
1762  }
1763 
1764  // Construct vector of bounding real boxes
1765  m_bnd_rbx.resize(n_plane_lo);
1766  for (int i(0); i < n_plane_hi; i++){
1767  amrex::RealBox rbx(rv_lo[i].data(),rv_hi[i].data());
1768  m_bnd_rbx[i] = rbx;
1769  }
1770 
1771  // Parse directionality
1772  m_dir.resize(n_plane_dir);
1773  pp.queryarr("sample_plane_dir",m_dir,0,n_plane_dir);
1774 
1775  // Parse names
1776  std::string name_base = "plt_plane_";
1777  m_name.resize(n_plane_lo);
1778  int n_names = pp.countval("sample_plane_name");
1779  if (n_names > 0) {
1780  AMREX_ALWAYS_ASSERT( n_names==n_plane_lo );
1781  pp.queryarr("sample_plane_name",m_name,0,n_names);
1782  } else {
1783  for (int iplane(0); iplane<n_plane_lo; ++iplane) {
1784  m_name[iplane] = amrex::Concatenate(name_base, iplane , 5);
1785  }
1786  }
1787 
1788  // Optional cap on the finest level written (-1 => all intersecting levels)
1789  pp.queryAdd("plane_sampling_max_level", m_max_level);
1790 
1791  // Allocate per-plane vectors of per-level MF pointers
1792  m_ps_mf.resize(n_plane_lo);
1793 
1794  // Get requested vars
1795  if (pp.countval("plane_sampling_vars") > 0) {
1796  m_varnames.clear();
1797  amrex::Vector<std::string> requested_vars;
1798  pp.queryarr("plane_sampling_vars",requested_vars);
1799  amrex::Print() << "Selected plane sampling vars :";
1800  if (containerHasElement(requested_vars, "density")) {
1801  m_varnames.push_back("density");
1802  amrex::Print() << " " << "density";
1803  }
1804  if (containerHasElement(requested_vars, "x_velocity")) {
1805  m_varnames.push_back("x_velocity");
1806  amrex::Print() << " " << "x_velocity";
1807  }
1808  if (containerHasElement(requested_vars, "y_velocity")) {
1809  m_varnames.push_back("y_velocity");
1810  amrex::Print() << " " << "y_velocity";
1811  }
1812  if (containerHasElement(requested_vars, "z_velocity")) {
1813  m_varnames.push_back("z_velocity");
1814  amrex::Print() << " " << "z_velocity";
1815  }
1816  if (containerHasElement(requested_vars, "magvel")) {
1817  m_varnames.push_back("magvel");
1818  amrex::Print() << " " << "magvel";
1819  }
1820  if (containerHasElement(requested_vars, "theta")) {
1821  m_varnames.push_back("theta");
1822  amrex::Print() << " " << "theta";
1823  }
1824  if (containerHasElement(requested_vars, "qv")) {
1825  m_varnames.push_back("qv");
1826  amrex::Print() << " " << "qv";
1827  }
1828  if (containerHasElement(requested_vars, "qc")) {
1829  m_varnames.push_back("qc");
1830  amrex::Print() << " " << "qc";
1831  }
1832  if (containerHasElement(requested_vars, "pressure")) {
1833  m_varnames.push_back("pressure");
1834  amrex::Print() << " " << "pressure";
1835  }
1836  // "z", as for line sampling, is the same variable
1837  if (containerHasElement(requested_vars, "z_phys") ||
1838  containerHasElement(requested_vars, "z")) {
1839  m_varnames.push_back("z_phys");
1840  amrex::Print() << " " << "z_phys";
1841  }
1842  if (containerHasElement(requested_vars, "volfrac")) {
1843  m_varnames.push_back("volfrac");
1844  amrex::Print() << " " << "volfrac";
1845  }
1846  amrex::Print() << std::endl;
1847  }
1848 
1849  // The plane is placed, and its plotfile written, in the computational coordinates,
1850  // which are not the physical height on a stretched or terrain-fitted mesh, so there
1851  // the height is always written (before volfrac, as when it is requested)
1852  if (SolverChoice::mesh_type != MeshType::ConstantDz &&
1853  !containerHasElement(m_varnames, "z_phys")) {
1854  auto it = std::find(m_varnames.begin(), m_varnames.end(), "volfrac");
1855  m_varnames.insert(it, "z_phys");
1856  }
1857  }
1858  }
1859 
1860  // This must match what is in AMReX_MultiFabUtil.H
1861  amrex::Box
1862  getIndexBox (const amrex::RealBox& real_box,
1863  const amrex::Geometry& geom) {
1864  amrex::IntVect slice_lo, slice_hi;
1865 
1866  AMREX_D_TERM(slice_lo[0]=static_cast<int>(std::floor((real_box.lo(0) - geom.ProbLo(0))/geom.CellSize(0)));,
1867  slice_lo[1]=static_cast<int>(std::floor((real_box.lo(1) - geom.ProbLo(1))/geom.CellSize(1)));,
1868  slice_lo[2]=static_cast<int>(std::floor((real_box.lo(2) - geom.ProbLo(2))/geom.CellSize(2))););
1869 
1870  AMREX_D_TERM(slice_hi[0]=static_cast<int>(std::floor((real_box.hi(0) - geom.ProbLo(0))/geom.CellSize(0)));,
1871  slice_hi[1]=static_cast<int>(std::floor((real_box.hi(1) - geom.ProbLo(1))/geom.CellSize(1)));,
1872  slice_hi[2]=static_cast<int>(std::floor((real_box.hi(2) - geom.ProbLo(2))/geom.CellSize(2))););
1873 
1874  return amrex::Box(slice_lo, slice_hi) & geom.Domain();
1875  }
1876 
1877  void
1878  get_sample_data (amrex::Vector<amrex::Geometry>& geom,
1879  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_new,
1880  const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& z_phys_cc,
1881  const amrex::Vector<const amrex::MultiFab*>& volfrac_lev = {})
1882  {
1883  int nlev = sampling_num_levels(vars_new);
1884  int nplane = static_cast<int>(m_bnd_rbx.size());
1885  int ncomp = static_cast<int>(m_varnames.size());
1886  bool interpolate = true;
1887 
1888  int qv_comp = -1;
1889 
1890  // Loop over each plane
1891  for (int iplane(0); iplane<nplane; ++iplane) {
1892  int dir = m_dir[iplane];
1893  amrex::RealBox bnd_rbx = m_bnd_rbx[iplane];
1894  amrex::Real point = bnd_rbx.lo(dir);
1895 
1896  // Collect every level 0..lev_cap whose grids intersect the plane.
1897  int lev_cap = (m_max_level >= 0 && m_max_level < nlev-1) ? m_max_level : nlev-1;
1898  m_ps_mf[iplane].clear();
1899 
1900  for (int ilev(0); ilev<=lev_cap; ++ilev) {
1901 
1902  // Stop ascending once a level's grids no longer touch the flattened plane
1903  {
1904  amrex::Box plane_bx = getIndexBox(bnd_rbx, geom[ilev]);
1905  int k_l = static_cast<int>(std::floor((point - geom[ilev].ProbLo(dir))
1906  / geom[ilev].CellSize(dir)));
1907  plane_bx.setSmall(dir, k_l); plane_bx.setBig(dir, k_l);
1908  if (!vars_new[ilev][Vars::cons].boxArray().intersects(plane_bx)) { break; }
1909  }
1910 
1911  // Construct CC velocities
1912  amrex::MultiFab mf_cc_vel;
1913  auto ba = vars_new[ilev][Vars::cons].boxArray();
1914  auto dm = vars_new[ilev][Vars::cons].DistributionMap();
1915  mf_cc_vel.define(ba, dm, AMREX_SPACEDIM, amrex::IntVect(1,1,1));
1916  average_face_to_cellcenter(mf_cc_vel,0,
1917  amrex::Array<const amrex::MultiFab*,3>{&vars_new[ilev][Vars::xvel],
1918  &vars_new[ilev][Vars::yvel],
1919  &vars_new[ilev][Vars::zvel]});
1920 
1921  // Construct MultiFab holding requested variables, and after them whether
1922  // each cell is covered by immersed terrain
1923  amrex::MultiFab mf_cc_data;
1924  mf_cc_data.define(ba, dm, ncomp+1, 1);
1925 
1926  int mf_comp = 0;
1927 
1928  if (containerHasElement(m_varnames, "density")) {
1929  amrex::MultiFab::Copy(mf_cc_data, vars_new[ilev][Vars::cons], Rho_comp, mf_comp, 1, 0);
1930  mf_comp += 1;
1931  }
1932 
1933  if (containerHasElement(m_varnames, "x_velocity")) {
1934  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 0, mf_comp, 1, 0);
1935  mf_comp += 1;
1936  }
1937  if (containerHasElement(m_varnames, "y_velocity")) {
1938  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 1, mf_comp, 1, 0);
1939  mf_comp += 1;
1940  }
1941  if (containerHasElement(m_varnames, "z_velocity")) {
1942  amrex::MultiFab::Copy(mf_cc_data, mf_cc_vel, 2, mf_comp, 1, 0);
1943  mf_comp += 1;
1944  }
1945 
1946  if (containerHasElement(m_varnames, "magvel")) {
1947 #ifdef _OPENMP
1948 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1949 #endif
1950  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1951  const amrex::Box& tbx = mfi.tilebox();
1952  auto const& dfab = mf_cc_data.array(mfi);
1953  auto const& vfab = mf_cc_vel.array(mfi);
1954 
1955  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1956  {
1957  dfab(i,j,k,mf_comp) = std::sqrt(vfab(i,j,k,0)*vfab(i,j,k,0)
1958  + vfab(i,j,k,1)*vfab(i,j,k,1)
1959  + vfab(i,j,k,2)*vfab(i,j,k,2));
1960  });
1961  }
1962  mf_comp += 1;
1963  }
1964 
1965  if (containerHasElement(m_varnames, "theta")) {
1966 #ifdef _OPENMP
1967 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1968 #endif
1969  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1970  const amrex::Box& tbx = mfi.tilebox();
1971  auto const& dfab = mf_cc_data.array(mfi);
1972  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1973 
1974  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1975  {
1976  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoTheta_comp) / cfab(i,j,k,Rho_comp);
1977  });
1978  }
1979  mf_comp += 1;
1980  }
1981 
1982  if (containerHasElement(m_varnames, "qv")) {
1983  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vars_new[ilev][Vars::cons].nComp() > RhoQ1_comp,
1984  "qv sampling requested but moisture components not present in state");
1985  if (qv_comp >= 0) AMREX_ALWAYS_ASSERT(qv_comp == mf_comp);
1986  qv_comp = mf_comp;
1987 #ifdef _OPENMP
1988 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
1989 #endif
1990  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
1991  const amrex::Box& tbx = mfi.tilebox();
1992  auto const& dfab = mf_cc_data.array(mfi);
1993  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
1994 
1995  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
1996  {
1997  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoQ1_comp) / cfab(i,j,k,Rho_comp);
1998  });
1999  }
2000  mf_comp += 1;
2001  }
2002  if (containerHasElement(m_varnames, "qc")) {
2003  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vars_new[ilev][Vars::cons].nComp() > RhoQ2_comp,
2004  "qc sampling requested but moisture components not present in state");
2005 #ifdef _OPENMP
2006 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
2007 #endif
2008  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
2009  const amrex::Box& tbx = mfi.tilebox();
2010  auto const& dfab = mf_cc_data.array(mfi);
2011  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
2012 
2013  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2014  {
2015  dfab(i,j,k,mf_comp) = cfab(i,j,k,RhoQ2_comp) / cfab(i,j,k,Rho_comp);
2016  });
2017  }
2018  mf_comp += 1;
2019  }
2020 
2021  if (containerHasElement(m_varnames, "pressure")) {
2022  if (vars_new[ilev][Vars::cons].nComp() > RhoQ1_comp) {
2023  // moist pressure: use qv from dfab if already sampled, else compute inline
2024 #ifdef _OPENMP
2025 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
2026 #endif
2027  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
2028  const amrex::Box& tbx = mfi.tilebox();
2029  auto const& dfab = mf_cc_data.array(mfi);
2030  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
2031 
2032  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2033  {
2034  amrex::Real qv_val = (qv_comp >= 0) ? dfab(i,j,k,qv_comp)
2035  : cfab(i,j,k,RhoQ1_comp) / cfab(i,j,k,Rho_comp);
2036  dfab(i,j,k,mf_comp) = getPgivenRTh(cfab(i,j,k,RhoTheta_comp), qv_val);
2037  });
2038  }
2039  } else {
2040  // dry pressure
2041 #ifdef _OPENMP
2042 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
2043 #endif
2044  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
2045  const amrex::Box& tbx = mfi.tilebox();
2046  auto const& dfab = mf_cc_data.array(mfi);
2047  auto const& cfab = vars_new[ilev][Vars::cons].array(mfi);
2048 
2049  amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept
2050  {
2051  dfab(i,j,k,mf_comp) = getPgivenRTh(cfab(i,j,k,RhoTheta_comp));
2052  });
2053  }
2054  }
2055  mf_comp += 1;
2056  }
2057 
2058  // The physical height of each cell center
2059  int z_comp = -1;
2060  if (containerHasElement(m_varnames, "z_phys")) {
2061  amrex::MultiFab::Copy(mf_cc_data, *z_phys_cc[ilev], 0, mf_comp, 1, 0);
2062  z_comp = mf_comp;
2063  mf_comp += 1;
2064  }
2065 
2066  // The volume fraction, 1 without immersed terrain, and 1 where a cell is
2067  // covered (0 elsewhere)
2068  const amrex::MultiFab* volfrac = ilev < static_cast<int>(volfrac_lev.size())
2069  ? volfrac_lev[ilev] : nullptr;
2070  int vf_comp = -1;
2071  if (containerHasElement(m_varnames, "volfrac")) {
2072  if (volfrac != nullptr) {
2073  amrex::MultiFab::Copy(mf_cc_data, *volfrac, 0, mf_comp, 1, 1);
2074  } else {
2075  mf_cc_data.setVal(1.0, mf_comp, 1, 1);
2076  }
2077  vf_comp = mf_comp;
2078  mf_comp += 1;
2079  }
2080  if (volfrac != nullptr) {
2081  const int cov_comp = ncomp;
2082 #ifdef _OPENMP
2083 #pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
2084 #endif
2085  for (amrex::MFIter mfi(mf_cc_data, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
2086  const amrex::Box& gbx = mfi.growntilebox(1);
2087  const auto& out = mf_cc_data.array(mfi);
2088  const auto& vf = volfrac->const_array(mfi);
2089  amrex::ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
2090  out(i,j,k,cov_comp) = (vf(i,j,k) == amrex::Real(0.0)) ? amrex::Real(1.0)
2091  : amrex::Real(0.0);
2092  });
2093  }
2094  } else {
2095  mf_cc_data.setVal(0.0, ncomp, 1, 1);
2096  }
2097 
2098  // The interpolation reads the cell on the other side of the plane, which may be
2099  // a ghost cell. It is taken from the neighboring grid, or periodic image, where
2100  // there is one; at the domain boundary or the edge of the level it is the cell
2101  // itself, so the plane there takes the value of the cell it lies in
2102  {
2103  const int fdir = dir;
2104  for (amrex::MFIter mfi(mf_cc_data); mfi.isValid(); ++mfi) {
2105  const amrex::Box& vbx = mfi.validbox();
2106  const int vlo = vbx.smallEnd(fdir);
2107  const int vhi = vbx.bigEnd(fdir);
2108  auto const& dfab = mf_cc_data.array(mfi);
2109  amrex::ParallelFor(amrex::grow(vbx, fdir, 1), ncomp+1,
2110  [=] AMREX_GPU_DEVICE(int i, int j, int k, int n) noexcept
2111  {
2112  amrex::IntVect iv(i,j,k);
2113  if (iv[fdir] < vlo || iv[fdir] > vhi) {
2114  iv[fdir] = amrex::min(amrex::max(iv[fdir], vlo), vhi);
2115  dfab(i,j,k,n) = dfab(iv,n);
2116  }
2117  });
2118  }
2119  mf_cc_data.FillBoundary(geom[ilev].periodicity());
2120  }
2121 
2122  auto slice = get_slice_data(dir, point, mf_cc_data, geom[ilev],
2123  0, ncomp+1, interpolate, bnd_rbx);
2124 
2125  // Defensive: an empty return means nothing was actually sampled.
2126  if (!slice || slice->boxArray().size() == 0) { break; }
2127 
2128  // Broadcast the single sampled plane across 2^ilev cells in dir so
2129  // every level shares an isotropic refinement ratio. The dir extent
2130  // becomes [0, 2^ilev-1]; each cell holds the interpolated plane value
2131  // replicated across the shared level-0 slab thickness.
2132  const int rr_l = 1 << ilev;
2133  const int k_slice = slice->boxArray().minimalBox().smallEnd(dir);
2134  const int l_dir = dir;
2135  const amrex::Real covered_value = m_covered_value;
2136 
2137  amrex::BoxList bl;
2138  const amrex::BoxArray& slice_ba = slice->boxArray();
2139  for (int ib(0); ib<static_cast<int>(slice_ba.size()); ++ib) {
2140  amrex::Box b = slice_ba[ib];
2141  b.setSmall(dir, 0);
2142  b.setBig(dir, rr_l - 1);
2143  bl.push_back(b);
2144  }
2145  amrex::BoxArray out_ba(std::move(bl));
2146  auto out_mf = std::make_unique<amrex::MultiFab>(out_ba, slice->DistributionMap(),
2147  ncomp, 0);
2148 
2149  for (amrex::MFIter mfi(*out_mf); mfi.isValid(); ++mfi) {
2150  const amrex::Box& obx = mfi.validbox();
2151  auto const& ofab = out_mf->array(mfi);
2152  auto const& sfab = slice->array(mfi);
2153  amrex::ParallelFor(obx, ncomp, [=] AMREX_GPU_DEVICE(int i, int j, int k, int n) noexcept
2154  {
2155  int si = i, sj = j, sk = k;
2156  if (l_dir == 0) { si = k_slice; }
2157  else if (l_dir == 1) { sj = k_slice; }
2158  else { sk = k_slice; }
2159  // A sample interpolated from a covered cell is written as
2160  // erf.sampling_covered_value, except for the height and volume fraction
2161  ofab(i,j,k,n) = (sfab(si,sj,sk,ncomp) > amrex::Real(0.0) &&
2162  n != vf_comp && n != z_comp)
2163  ? covered_value : sfab(si,sj,sk,n);
2164  });
2165  }
2166 
2167  m_ps_mf[iplane].push_back(std::move(out_mf));
2168 
2169  } // ilev
2170  }// iplane
2171  }
2172 
2173  void
2174  write_sample_data (amrex::Vector<double>& time,
2175  amrex::Vector<int>& level_steps,
2176  amrex::Vector<amrex::IntVect>& ref_ratio,
2177  amrex::Vector<amrex::Geometry>& geom)
2178  {
2179  amrex::ignore_unused(ref_ratio);
2180  int nplane = m_ps_mf.size();
2181  for (int iplane(0); iplane<nplane; ++iplane) {
2182  int nlev_c = static_cast<int>(m_ps_mf[iplane].size());
2183  if (nlev_c == 0) { continue; }
2184 
2185  int dir = m_dir[iplane];
2186  amrex::RealBox bnd_rbx = m_bnd_rbx[iplane];
2187  amrex::Real point = bnd_rbx.lo(dir);
2188 
2189  // One shared physical RealBox for every level: full in-plane extent of the
2190  // requested plane, and a dir-slab of level-0 thickness centered on the plane.
2191  // Level 0 supplies prob_lo/hi to the writer, so this object must be shared.
2192  amrex::Real dx0 = geom[0].CellSize(dir);
2193  amrex::RealBox shared_rbx = bnd_rbx;
2194  shared_rbx.setLo(dir, point - myhalf*dx0);
2195  shared_rbx.setHi(dir, point + myhalf*dx0);
2196 
2197  amrex::Vector<int> is_per(AMREX_SPACEDIM,0);
2198  for (int d(0); d<AMREX_SPACEDIM; ++d) { is_per[d] = geom[0].isPeriodic(d); }
2199 
2200  // Build the index chain by refining the level-0 plane box so that
2201  // Domain_l == refine(Domain_{l-1},2) holds exactly in every axis.
2202  amrex::Box B0 = getIndexBox(bnd_rbx, geom[0]);
2203 
2204  amrex::Vector<const amrex::MultiFab*> mf(nlev_c);
2205  amrex::Vector<amrex::Geometry> m_geom(nlev_c);
2206  amrex::Vector<int> m_level_steps(nlev_c);
2207  amrex::Vector<amrex::IntVect> m_ref_ratio(nlev_c-1, amrex::IntVect(2));
2208 
2209  for (int l(0); l<nlev_c; ++l) {
2210  const int rr_l = 1 << l;
2211 
2212  // The nested plane plotfile stores a single scalar refinement
2213  // ratio per level, so multi-level output requires factor-2
2214  // isotropic refinement.
2215  for (int d(0); d<AMREX_SPACEDIM; ++d) {
2216  amrex::Real ratio = geom[0].CellSize(d) / geom[l].CellSize(d);
2218  std::abs(ratio - static_cast<amrex::Real>(rr_l)) < amrex::Real(1.e-6) * rr_l,
2219  "PlaneSampler multi-level output requires factor-2 isotropic refinement (amr.ref_ratio = 2 2 2)");
2220  }
2221 
2222  amrex::Box domain_l = amrex::refine(B0, rr_l);
2223  domain_l.setSmall(dir, 0);
2224  domain_l.setBig(dir, rr_l - 1);
2225 
2226  mf[l] = m_ps_mf[iplane][l].get();
2227  m_geom[l].define(domain_l, &shared_rbx, geom[l].Coord(), is_per.data());
2228  m_level_steps[l] = level_steps[l];
2229  AMREX_ASSERT(domain_l.contains(mf[l]->boxArray().minimalBox()));
2230  }
2231 
2232  // Create plotfile name
2233  std::string name_plane = m_name[iplane];
2234  name_plane += "_step_";
2235  std::string plotfilename = amrex::Concatenate(name_plane, m_level_steps[0], 5);
2236 
2237  // Write the nested multi-level plane plotfile
2238  WriteMultiLevelPlotfile(plotfilename, nlev_c, mf,
2239  m_varnames, m_geom, static_cast<amrex::Real>(time[0]),
2240  m_level_steps, m_ref_ratio);
2241  } // iplane
2242  }
2243 
2244  int m_max_level = -1;
2245  amrex::Vector<int> m_dir;
2246  amrex::Vector<amrex::RealBox> m_bnd_rbx;
2247  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> m_ps_mf;
2248  amrex::Vector<std::string> m_name;
2249 
2250  amrex::Vector<std::string> m_varnames {"magvel","theta"};
2251  amrex::Real m_covered_value{sampling_covered_value()}; // erf.sampling_covered_value
2252 };
2253 #endif
std::vector< int > point
Definition: ERF_ActuatorSpreading.cpp:221
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
bool containerHasElement(const V &iterable, const T &query)
Definition: ERF_Container.H:5
@ tau12
Definition: ERF_DataStruct.H:51
@ tau23
Definition: ERF_DataStruct.H:51
@ tau33
Definition: ERF_DataStruct.H:51
@ tau22
Definition: ERF_DataStruct.H:51
@ tau11
Definition: ERF_DataStruct.H:51
@ tau13
Definition: ERF_DataStruct.H:51
Coord
Coordinate-axis selector.
Definition: ERF_DataStruct.H:226
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getPgivenRTh(const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:81
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define RhoQ2_comp
Definition: ERF_IndexDefines.H:79
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:78
#define RhoKE_comp
Definition: ERF_IndexDefines.H:63
pp_for_pert_vels get("prob_type", prob_type)
const Real zc
Definition: ERF_InitCustomPert_ABL.H:112
const Real xc
Definition: ERF_InitCustomPert_ABL.H:110
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
ParmParse pp("prob")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:404
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);})
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
AMREX_FORCE_INLINE IntVect offset(const int face_dir, const int normal)
Definition: ERF_ReadBndryPlanes.cpp:36
amrex::Real sampling_covered_value()
Definition: ERF_SampleData.H:31
int sampling_num_levels(const amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_new)
Definition: ERF_SampleData.H:44
Definition: ERF_LatLonMap.H:152
LatLonLocation locate_or_abort(amrex::Real req_lat, amrex::Real req_lon, const std::string &what) const
Definition: ERF_LatLonMap.cpp:233
@ fs
Definition: ERF_AdvanceMorrison.cpp:124
@ xvel
Definition: ERF_IndexDefines.H:232
@ cons
Definition: ERF_IndexDefines.H:231
@ zvel
Definition: ERF_IndexDefines.H:234
@ yvel
Definition: ERF_IndexDefines.H:233
@ t
Definition: ERF_WSM6.H:295
@ p
Definition: ERF_WSM6.H:303
Definition: ERF_ConsoleIO.cpp:17
real(c_double), parameter g
Definition: ERF_module_model_constants.F90:19
real(c_double), private k1
Definition: ERF_module_mp_morr_two_moment.F90:213
Definition: ERF_LatLonMap.H:87
amrex::Real lat
Definition: ERF_LatLonMap.H:94
amrex::Real x
Definition: ERF_LatLonMap.H:89
amrex::Real y
Definition: ERF_LatLonMap.H:90
amrex::Real lon
Definition: ERF_LatLonMap.H:95
Definition: ERF_SampleData.H:628
amrex::Box cols
the columns of the line on the level (k = the domain's bottom)
Definition: ERF_SampleData.H:631
amrex::Vector< amrex::Real > zt
the height of each sample above sea level
Definition: ERF_SampleData.H:633
amrex::Vector< int > k1
per sample: its column along the line, the cells it reads
Definition: ERF_SampleData.H:634
amrex::Vector< int > k0
Definition: ERF_SampleData.H:634
amrex::Vector< int > col
Definition: ERF_SampleData.H:634
amrex::BoxArray ba
the grids of the level
Definition: ERF_SampleData.H:629
amrex::Vector< amrex::Real > w
the weight of k1
Definition: ERF_SampleData.H:635
amrex::Vector< char > missing
the sample is below the ground or above the top, or no fluid brackets it
Definition: ERF_SampleData.H:636
amrex::Box cells
the cells the samples read (empty if none)
Definition: ERF_SampleData.H:632
bool holds
the level holds every cell the samples read
Definition: ERF_SampleData.H:630
Definition: ERF_SampleData.H:56
void expand_conserved(int ncons)
Replace "conserved" by one name per component of the conserved state, cons_0, cons_1,...
Definition: ERF_SampleData.H:436
void write_line_plotfile(amrex::Vector< double > &time, amrex::Vector< int > &level_steps, amrex::Vector< amrex::IntVect > &ref_ratio, amrex::Vector< amrex::Geometry > &geom)
Definition: ERF_SampleData.H:1634
amrex::MultiFab interp_line_data(int iline, const InterpPlan &plan, const amrex::MultiFab &data, int z_comp, int vf_comp) const
Definition: ERF_SampleData.H:855
amrex::Vector< std::string > m_varnames
Definition: ERF_SampleData.H:1710
static amrex::Vector< amrex::Real > gather_box(const amrex::MultiFab &src, int scomp, int ncomp, const amrex::Box &bx, bool to_all)
Definition: ERF_SampleData.H:606
amrex::Vector< std::unique_ptr< std::fstream > > m_logstream
Definition: ERF_SampleData.H:1712
amrex::Vector< amrex::Real > interp_heights(int iline) const
Definition: ERF_SampleData.H:579
amrex::Vector< amrex::Real > m_ground
Definition: ERF_SampleData.H:1716
void open_ascii_streams()
Definition: ERF_SampleData.H:448
amrex::Vector< amrex::MultiFab > m_ls_mf
Definition: ERF_SampleData.H:1696
void resolve_latlon(const LatLonMap &latlon_map, const amrex::Geometry &geom0)
Definition: ERF_SampleData.H:483
LineSampler(const amrex::Vector< amrex::IntVect > &cells, const amrex::Vector< std::string > &log_names, const amrex::Box &domain)
Definition: ERF_SampleData.H:327
amrex::Box interp_sample_box(int iline, const amrex::Box &cols) const
The samples of line iline, along its direction: its heights (vertical) or its columns (horizontal)
Definition: ERF_SampleData.H:593
amrex::Vector< amrex::Real > m_latlon_lo
Definition: ERF_SampleData.H:1701
amrex::Real line_ground(int iline, const amrex::Geometry &geom0, const amrex::MultiFab *ground_nd)
The ground under line iline, found once: level 0 and the terrain do not change.
Definition: ERF_SampleData.H:965
amrex::Vector< amrex::Vector< int > > m_box_cache_found
Definition: ERF_SampleData.H:1718
void write_coords(amrex::Vector< std::unique_ptr< amrex::MultiFab > > &z_phys_cc, amrex::Vector< std::unique_ptr< amrex::MultiFab > > &z_phys_nd, amrex::Vector< amrex::Geometry > &geom, const amrex::MultiFab *ground_nd=nullptr, const amrex::Vector< const amrex::MultiFab * > &ground_lev={}, const amrex::Vector< const amrex::MultiFab * > &volfrac_lev={})
Definition: ERF_SampleData.H:1082
bool needs_ground() const
Definition: ERF_SampleData.H:549
int m_max_level
Definition: ERF_SampleData.H:1708
LineSampler(const amrex::Vector< amrex::IntVect > &cells, const amrex::Vector< amrex::RealVect > &points, const amrex::Vector< std::string > &log_names, bool z_agl)
Definition: ERF_SampleData.H:366
bool m_interp
Definition: ERF_SampleData.H:1705
void write_sample_data(amrex::Vector< double > &time, amrex::Vector< int > &level_steps, amrex::Vector< amrex::IntVect > &ref_ratio, amrex::Vector< amrex::Geometry > &geom)
Definition: ERF_SampleData.H:1548
amrex::Vector< std::unique_ptr< std::fstream > > m_datastream
Definition: ERF_SampleData.H:1711
const InterpPlan & cachedInterpPlan(int iline, int lev, const amrex::Geometry &geom, const amrex::Geometry &geom0, const amrex::MultiFab &z_cc, const amrex::MultiFab *z_nd, const amrex::MultiFab *volfrac, const amrex::MultiFab *ground, const amrex::MultiFab *ground0)
make_interp_plan, kept until the grids of the level change
Definition: ERF_SampleData.H:833
static amrex::Real column_ground(const amrex::MultiFab &ground_nd, const amrex::Geometry &geom, const amrex::IntVect &cell)
Definition: ERF_SampleData.H:940
bool uses_latlon() const
Whether the lines are given by latitude and longitude (erf.sample_line_lo/hi_latlon)
Definition: ERF_SampleData.H:470
void get_sample_data(amrex::Vector< amrex::Geometry > &geom, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_new, const amrex::Vector< amrex::Vector< amrex::MultiFab * >> &tau_lev={}, const amrex::Vector< amrex::MultiFab * > &hfx3_lev={}, const amrex::Vector< int > &prognostic_tke_available={}, const amrex::Vector< amrex::MultiFab * > &z_cc_lev={}, const amrex::Vector< amrex::MultiFab * > &z_nd_lev={}, const amrex::MultiFab *ground_nd=nullptr, const amrex::Vector< const amrex::MultiFab * > &volfrac_lev={}, const amrex::Vector< const amrex::MultiFab * > &ground_lev={})
Definition: ERF_SampleData.H:1177
bool m_use_real_bx
Definition: ERF_SampleData.H:1699
amrex::Vector< amrex::Vector< InterpPlan > > m_interp_plan
Definition: ERF_SampleData.H:1723
static amrex::Vector< amrex::Gpu::HostVector< amrex::Real > > column_face_heights(const amrex::MultiFab &z_nd, const amrex::Geometry &geom, const amrex::IntVect &cell)
Definition: ERF_SampleData.H:908
void write_line_log(amrex::Vector< double > &time)
One row per line in its log: the time, then each variable along the line.
Definition: ERF_SampleData.H:1564
void write_line_ascii(amrex::Vector< double > &time)
Definition: ERF_SampleData.H:1598
amrex::Vector< amrex::BoxArray > m_zcc_ba
Definition: ERF_SampleData.H:1721
const amrex::Box & cachedLineIndexBox(int iline, int lev, const amrex::Geometry &geom, const amrex::MultiFab &z_nd, amrex::Real ground)
Definition: ERF_SampleData.H:986
amrex::Vector< int > m_dir
Definition: ERF_SampleData.H:1692
bool use_column_heights() const
Definition: ERF_SampleData.H:897
static const amrex::MultiFab * level_ptr(const amrex::Vector< const amrex::MultiFab * > &v, int lev)
The entry of level lev, or nullptr past the end.
Definition: ERF_SampleData.H:826
bool m_z_agl
Definition: ERF_SampleData.H:1704
amrex::Vector< int > m_npts
Definition: ERF_SampleData.H:1706
int m_ncons
Definition: ERF_SampleData.H:1709
amrex::Vector< amrex::BoxArray > m_box_cache_ba
Definition: ERF_SampleData.H:1719
InterpPlan make_interp_plan(int iline, int lev, const amrex::Geometry &geom, const amrex::Geometry &geom0, const amrex::MultiFab &z_cc, const amrex::MultiFab *z_nd, const amrex::MultiFab *volfrac, const amrex::MultiFab *ground, const amrex::MultiFab *ground0) const
Definition: ERF_SampleData.H:659
amrex::Vector< std::string > m_name
Definition: ERF_SampleData.H:1697
void check_interp_heights(int iline) const
Abort unless the number of heights of line iline suits its ends (erf.sample_line_interp)
Definition: ERF_SampleData.H:556
static amrex::Box lineIndexBoxXY(const amrex::RealBox &rb, const amrex::Geometry &geom)
The cells of a real box in x and y on a level, with k = 0: its k comes from heights.
Definition: ERF_SampleData.H:1052
amrex::Vector< amrex::Real > m_latlon_hi
Definition: ERF_SampleData.H:1701
LineSampler()
Definition: ERF_SampleData.H:57
bool m_write_log
Definition: ERF_SampleData.H:1703
static const amrex::Vector< std::string > & line_sampling_var_order()
The variables that may be sampled, in the order they are stored and written.
Definition: ERF_SampleData.H:425
amrex::Real m_covered_value
Definition: ERF_SampleData.H:1707
amrex::Vector< amrex::Box > m_bnd_bx
Definition: ERF_SampleData.H:1694
bool m_use_latlon
Definition: ERF_SampleData.H:1700
void open_logs(const amrex::Vector< std::string > &log_names)
Open one log per line, appended to, on the I/O rank.
Definition: ERF_SampleData.H:406
amrex::Box getIndexBox(const amrex::RealBox &real_box, const amrex::Geometry &geom)
Definition: ERF_SampleData.H:1066
amrex::Vector< amrex::Vector< amrex::Box > > m_box_cache
Definition: ERF_SampleData.H:1717
bool m_write_ascii
Definition: ERF_SampleData.H:1702
amrex::Vector< amrex::RealBox > m_bnd_rbx
Definition: ERF_SampleData.H:1695
amrex::Vector< int > m_lev
Definition: ERF_SampleData.H:1693
amrex::Box lineIndexBox(int iline, const amrex::Geometry &geom, const amrex::MultiFab &z_nd, amrex::Real ground)
Definition: ERF_SampleData.H:1016
Definition: ERF_SampleData.H:1728
amrex::Vector< int > m_dir
Definition: ERF_SampleData.H:2245
amrex::Box getIndexBox(const amrex::RealBox &real_box, const amrex::Geometry &geom)
Definition: ERF_SampleData.H:1862
amrex::Real m_covered_value
Definition: ERF_SampleData.H:2251
void write_sample_data(amrex::Vector< double > &time, amrex::Vector< int > &level_steps, amrex::Vector< amrex::IntVect > &ref_ratio, amrex::Vector< amrex::Geometry > &geom)
Definition: ERF_SampleData.H:2174
int m_max_level
Definition: ERF_SampleData.H:2244
amrex::Vector< std::string > m_name
Definition: ERF_SampleData.H:2248
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > m_ps_mf
Definition: ERF_SampleData.H:2247
amrex::Vector< amrex::RealBox > m_bnd_rbx
Definition: ERF_SampleData.H:2246
PlaneSampler()
Definition: ERF_SampleData.H:1729
amrex::Vector< std::string > m_varnames
Definition: ERF_SampleData.H:2250
void get_sample_data(amrex::Vector< amrex::Geometry > &geom, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_new, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &z_phys_cc, const amrex::Vector< const amrex::MultiFab * > &volfrac_lev={})
Definition: ERF_SampleData.H:1878
static MeshType mesh_type
Vertical mesh representation.
Definition: ERF_DataStruct.H:3004
static TerrainType terrain_type
Terrain or immersed-boundary representation.
Definition: ERF_DataStruct.H:2989