ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceModel.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACEMODEL_H
2 #define ERF_SURFACEMODEL_H
3 
4 #include "AMReX_Geometry.H"
5 #include "AMReX_ParmParse.H"
6 #include "AMReX_FArrayBox.H"
7 #include "AMReX_IntVect.H"
8 #include "AMReX_MFIter.H"
9 #include "AMReX_MultiFab.H"
10 #include "AMReX_FillPatchUtil.H"
11 #include "AMReX_GpuLaunch.H"
12 
13 #include <ERF_Constants.H>
14 #include <ERF_DataStruct.H>
15 #include <ERF_TypedInterp.H>
16 
17 enum SurfaceModelType { LAND = 0, URBAN = 1};
18 
20 
21 /**
22  * @brief Non-owning views of surface fluxes for one tile.
23  */
25  amrex::Array4<const amrex::Real> tau13;
26  amrex::Array4<const amrex::Real> tau23;
27  amrex::Array4<const amrex::Real> t_flux;
28  amrex::Array4<const amrex::Real> q_flux;
29 
30  /** Whether all four fluxes exist and hold a valid LSM value (is_valid_lsm_value) at (i,j). */
31  [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
32  bool valid (int i, int j) const noexcept
33  {
34  return t_flux && q_flux && tau13 && tau23 &&
35  is_valid_lsm_value(t_flux(i,j,0)) &&
36  is_valid_lsm_value(q_flux(i,j,0)) &&
37  is_valid_lsm_value(tau13 (i,j,0)) &&
38  is_valid_lsm_value(tau23 (i,j,0));
39  }
40 };
41 
42 /**
43  * @brief Interface between ERF and land-surface and urban models.
44  *
45  * SurfaceModel collects data and fluxes from the LSM and urban models,
46  * performs the land-urban weight averaging and exposes the resulting fields
47  * for use by ERF boundary-condition and physics components.
48  *
49  * Models may register data and mappings independently, without requiring a
50  * fixed ordering or one-to-one correspondence between their fields.
51  *
52  * Model-specific field names can also be registered under a common name for
53  * consumers such as RRTMGP and MOST.
54  *
55  * This class supports its own checkpoint state, ensuring the mappings are
56  * restored correctly on restart. This class also provides a surface plotfile
57  * for weight-averaged model data for the fields registered.
58  */
60 {
61 public:
62 
63  /**
64  * @brief Constructs a surface-model interface for the supplied AMR levels.
65  *
66  * @param nlevs Number of AMR levels initially available.
67  * @param ba Cell-centered box arrays for each level.
68  * @param geom Geometry for each level.
69  * @param dm Distribution mappings for each level.
70  * @param solverChoice ERF solver configuration.
71  * @param lmask_lev Land-mask data for each level.
72  */
73  explicit SurfaceModel (int nlevs,
74  const amrex::Vector<amrex::BoxArray> &ba,
75  const amrex::Vector<amrex::Geometry> &geom,
76  const amrex::Vector<amrex::DistributionMapping> &dm,
77  const SolverChoice &/*solverChoice*/,
78  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::iMultiFab>>>& /*lmask_lev*/)
79  : m_nlevs(nlevs), m_ba(ba), m_geom(geom), m_dmap(dm) {
80 
82  m_output_fields_registered = {false, false};
83 
84  // set initial fields for each model to none
85  lsm_fields = amrex::Vector<int>(5, -1);
86  urban_fields = amrex::Vector<int>(5, -1);
87  }
88 
89  /**
90  * @brief Initializes storage and boundary data for an AMR level.
91  *
92  * Existing coarser-level data are interpolated when a finer level is
93  * initialized.
94  *
95  * @param lev AMR level to initialize.
96  * @param ba Cell-centered box array for the level.
97  * @param geom Geometry for the level.
98  * @param dm Distribution mapping for the level.
99  * @param lmask_lev Land-mask data used by the level.
100  * @param domain_bcs_type Boundary conditions for interpolation.
101  * @param refRatio Refinement ratios between adjacent levels.
102  */
103  void initialize_for_level (int lev, const amrex::BoxArray &ba, const amrex::Geometry &geom, const amrex::DistributionMapping &dm, const amrex::Vector<std::unique_ptr<amrex::iMultiFab>>& lmask_lev, const amrex::Vector <amrex::BCRec> &domain_bcs_type, const amrex::Vector<amrex::IntVect> &refRatio)
104  {
105  if (lev >= m_nlevs) { resize_levels(lev+1); }
106 
107  m_ba[lev] = ba;
108  m_geom[lev] = geom;
109  m_dmap[lev] = dm;
110 
111  // Create a 2D ba, dm, & ghost cells
112  amrex::BoxList bl2d = ba.boxList();
113  for (auto& b : bl2d) {
114  b.setRange(2, 0);
115  }
116  m_ba2d[lev] = amrex::BoxArray(std::move(bl2d));
117 
118  amrex::RealBox dom2d = geom.ProbDomain();
119  dom2d.setHi(2, geom.CellSize(2));
120  m_geom2d[lev].define(makeSlab(m_geom[lev].Domain(), 2, 0), dom2d, geom.Coord(), geom.isPeriodic());
121 
122  m_lmask[lev] = lmask_lev[0].get();
123 
124  weighted_lsm_data_lev[lev].clear();
125  weighted_urban_data_lev[lev].clear();
126 
127  u_star[lev].reset();
128  t_star[lev].reset();
129  q_star[lev].reset();
130  t_surf[lev].reset();
131  wavg[lev].reset();
132  if (m_surface_outputs_requested[lev]) {
134  }
136 
137  // Recreate registered fields on the current level layout. This is also
138  // needed when an existing AMR level is remade with a new grid layout.
139  for (int field = 0; field < static_cast<int>(fields.size()); ++field) {
140  fields[field][lev].reset();
141  }
142  // Keep each map's consumer kind: a map active only for one output must not
143  // become persistent because a regrid happened while it was active
144  for (const auto& entry : fieldmap) {
145  if (entry.second.active) {
146  activate_field_map(entry.first, entry.second.persistent_consumer);
147  }
148  }
149  for (auto& entry : radiation_input_map) {
150  entry.second.weighted[lev].reset();
151  }
152 
153  // Cached radiation fields point into the registered storage above.
154  // Rebuild the cache when this level's fields have been replaced.
155  rad_fields[lev].clear();
156  rad_output_fields[lev].clear();
157 
158  // Interpolate the new finer level using coarse data. These fields hold lsm_undefined
159  // where no provider has a value, and their consumers fall back to MOST there. A valid
160  // and an invalid value are two types (interp_by_type), so no valid fine cell takes a
161  // slope toward an invalid coarse cell, and a fine cell under an invalid one is
162  // lsm_undefined. Every field moves in one pack: one ghost exchange and one
163  // coarse-to-fine copy.
164  if (lev > 0) {
165  amrex::Vector<InterpTransfer> transfers;
166  if (u_star[lev] != nullptr && u_star[lev-1] != nullptr) {
167  transfers.push_back({u_star[lev].get(), u_star[lev-1].get(), 2});
168  transfers.push_back({t_star[lev].get(), t_star[lev-1].get(), 1});
169  transfers.push_back({q_star[lev].get(), q_star[lev-1].get(), 1});
170  // t_surf starts at 300 but is lsm_undefined where its provider has no field
171  transfers.push_back({t_surf[lev].get(), t_surf[lev-1].get(), 1});
172  }
173  for (int field = 0; field < static_cast<int>(fields.size()); ++field) {
174  if (fields[field][lev] == nullptr || fields[field][lev-1] == nullptr) { continue; }
175  transfers.push_back({fields[field][lev].get(), fields[field][lev-1].get(), 1});
176  }
177  if (!transfers.empty()) {
178  interp_surface_fields_from_coarse(lev, transfers, domain_bcs_type[BCVars::cons_bc],
179  refRatio[lev-1]);
180  }
181  }
182  }
183 
184  /**
185  * @brief Removes an AMR level, and every level above it, from the surface model.
186  *
187  * Called when ERF destroys a level during regridding so that m_nlevs tracks the
188  * levels that actually exist. Otherwise the checkpoint and other level loops
189  * would still visit the destroyed levels and their stale provider pointers.
190  *
191  * @param lev AMR level being destroyed.
192  */
193  void clear_level (int lev)
194  {
195  if (lev < 0 || lev >= m_nlevs) { return; }
196  resize_levels(lev);
197  }
198 
199  /**
200  * @brief Registers model variables used in weighted averaging.
201  *
202  * @param lev AMR level associated with the data.
203  * @param model_data Pointers to the model variables.
204  * @param data_names Names corresponding to @p model_data.
205  * @param type Whether the data belong to the land or urban model.
206  */
207  void set_model_data (const int lev, const amrex::Vector<amrex::MultiFab*> model_data, const amrex::Vector<std::string>& data_names, SurfaceModelType type)
208  {
209  if (type == SurfaceModelType::LAND) {
210  lsm_data_lev[lev].resize(model_data.size());
211  weighted_lsm_data_lev[lev].clear();
212  weighted_lsm_data_lev[lev].resize(model_data.size());
213  if (lev == 0) {
214  // only set names on first level, since they are the same for all levels
215  m_lsm_names.resize(model_data.size());
216  AMREX_ALWAYS_ASSERT(data_names.size() == model_data.size());
217  for (int i = 0; i < static_cast<int>(data_names.size()); ++i) {
218  m_lsm_names[i] = data_names[i];
219  }
220  }
221 
222  for (int i = 0; i < static_cast<int>(lsm_data_lev[lev].size()); ++i) {
223  AMREX_ALWAYS_ASSERT(model_data[i]);
224  lsm_data_lev[lev][i] = model_data[i];
225  }
226  m_use_land = true;
228  for (auto& fields_at_level : rad_fields) { fields_at_level.clear(); }
229  for (auto& fields_at_level : rad_output_fields) { fields_at_level.clear(); }
230  for (auto& entry : radiation_input_map) {
231  if (lev < static_cast<int>(entry.second.weighted.size())) {
232  entry.second.weighted[lev].reset();
233  }
234  }
235  } else if (type == SurfaceModelType::URBAN) {
236  urban_data_lev[lev].resize(model_data.size());
237  weighted_urban_data_lev[lev].clear();
238  weighted_urban_data_lev[lev].resize(model_data.size());
239  if (lev == 0) {
240  // only set names on first level, since they are the same for all levels
241  m_urban_names.resize(model_data.size());
242  AMREX_ALWAYS_ASSERT(data_names.size() == model_data.size());
243  for (int i = 0; i < static_cast<int>(data_names.size()); ++i) {
244  m_urban_names[i] = data_names[i];
245  }
246  }
247  for (int i = 0; i < static_cast<int>(urban_data_lev[lev].size()); ++i) {
248  AMREX_ALWAYS_ASSERT(model_data[i]);
249  urban_data_lev[lev][i] = model_data[i];
250  }
251  m_use_urban = true;
253  for (auto& fields_at_level : rad_fields) { fields_at_level.clear(); }
254  for (auto& fields_at_level : rad_output_fields) { fields_at_level.clear(); }
255  for (auto& entry : radiation_input_map) {
256  if (lev < static_cast<int>(entry.second.weighted.size())) {
257  entry.second.weighted[lev].reset();
258  }
259  }
260  }
261  }
262 
263  /**
264  * @brief Registers model fluxes as additional weighted-average inputs.
265  *
266  * @param lev AMR level associated with the fluxes.
267  * @param model_fluxes Pointers to the model flux fields.
268  * @param flux_names Names corresponding to @p model_fluxes.
269  * @param type Whether the fluxes belong to the land or urban model.
270  */
271  void set_model_fluxes (const int lev, const amrex::Vector<amrex::MultiFab*> model_fluxes,
272  const amrex::Vector<std::string>& flux_names, SurfaceModelType type)
273  {
274  amrex::Vector<amrex::MultiFab*>* model_data = nullptr;
275  amrex::Vector<std::string>* model_names = nullptr;
276 
277  if (type == SurfaceModelType::LAND) {
278  model_data = &lsm_data_lev[lev];
279  model_names = &m_lsm_names;
280  } else if (type == SurfaceModelType::URBAN) {
281  model_data = &urban_data_lev[lev];
282  model_names = &m_urban_names;
283  }
284 
285  AMREX_ALWAYS_ASSERT(model_data != nullptr);
286  AMREX_ALWAYS_ASSERT(model_names != nullptr);
287  AMREX_ALWAYS_ASSERT(flux_names.size() == model_fluxes.size());
288 
289  const int data_size = static_cast<int>(model_data->size());
290  model_data->resize(data_size + static_cast<int>(model_fluxes.size()));
291  if (lev == 0) {
292  model_names->resize(data_size + static_cast<int>(model_fluxes.size()));
293  }
294 
295  for (int i = 0; i < static_cast<int>(model_fluxes.size()); ++i) {
296  AMREX_ALWAYS_ASSERT(model_fluxes[i]);
297  (*model_data)[data_size + i] = model_fluxes[i];
298  if (lev == 0) {
299  (*model_names)[data_size + i] = flux_names[i];
300  }
301  }
302  }
303 
304  /**
305  * @brief Selects the model fields that participate in weighted averaging.
306  *
307  * When using fluxes, the first five elements map to u-stress, v-stress,
308  * heat flux, vapor flux, and surface temperature. Otherwise, the first
309  * four elements map to ustar, tstar, qstar, and surface temperature. A
310  * field index of -1 skips that field.
311  *
312  * @param type Whether the fields belong to the land or urban model.
313  * @param field_indices Indices of the fields to average.
314  * @param use_fluxes Whether the selected fields are fluxes.
315  */
316  void set_model_fields (SurfaceModelType type, const amrex::Vector<int> &field_indices, const bool use_fluxes = true)
317  {
318  const int nfields = static_cast<int>(field_indices.size());
319  if (!use_fluxes) {
320  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nfields >= 4, "Must have at least four fields to average (ustar, tstar, qstar, tsurf)");
321  } else {
322  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nfields >= 5, "Must have at least five fields to average (u-stress, v-stress, hfx, qfx, tsurf)");
323  }
324 
325  // Latch the export mode off the first model that registers, then make sure every
326  // subsequent model agrees: either all of them supply fluxes, or all of them supply
327  // u*, t*, q* directly. Taking the mode from the caller rather than assuming the
328  // default is what makes the non-flux path reachable at all.
329  if (!m_export_fluxes_set) {
330  m_export_fluxes = use_fluxes;
331  m_export_fluxes_set = true;
332  }
334  "All registered surface models must export either fluxes or u*/t*/q*, not a mix");
335 
336  const int nregistered = (type == SurfaceModelType::LAND)
337  ? static_cast<int>(m_lsm_names.size())
338  : static_cast<int>(m_urban_names.size());
339  for (const int field : field_indices) {
341  field == -1 || (field >= 0 && field < nregistered),
342  "Model field index must be -1 or within the registered provider data range");
343  }
344 
345  if (type == SurfaceModelType::LAND) {
346  lsm_fields = amrex::Vector<int>(field_indices);
348  for (auto& fields_at_level : weighted_lsm_data_lev) {
349  fields_at_level.clear();
350  }
351  } else if (type == SurfaceModelType::URBAN) {
352  urban_fields = amrex::Vector<int>(field_indices);
354  for (auto& fields_at_level : weighted_urban_data_lev) {
355  fields_at_level.clear();
356  }
357  }
358  }
359 
360  /**
361  * @brief Applies land and urban weights into separate output fields.
362  *
363  * A null land or urban source is skipped, but both sources cannot be
364  * null. The source fields are not modified and the fields may use
365  * different underlying grids.
366  *
367  * @param lev AMR level whose weights are used.
368  * @param lsm_data Land-model source data to weight.
369  * @param lsm_weighted Destination for weighted land data.
370  * @param urban_data Urban-model source data to weight.
371  * @param urban_weighted Destination for weighted urban data.
372  */
373  void apply_weight_average (int lev, const amrex::MultiFab *lsm_data,
374  amrex::MultiFab *lsm_weighted,
375  const amrex::MultiFab *urban_data,
376  amrex::MultiFab *urban_weighted);
377 
378  /**
379  * @brief Computes land and urban weights and applies them to registered fields.
380  *
381  * @param lev AMR level to update.
382  * @param urban_frac Urban fraction used to compute the weights.
383  */
384  void calculate_weight_average (int lev, amrex::MultiFab* const urban_frac,
385  bool update_derived = true);
386 
387  /**
388  * @brief Returns whether the interface exports fluxes rather than MOST variables.
389  * @return True when weighted outputs are fluxes.
390  */
391  bool are_fluxes () { return m_export_fluxes; }
392 
393  /**
394  * @brief Enables and materializes the surface outputs consumed by ERF.
395  */
396  void request_surface_outputs (const bool persistent = true)
397  {
399  for (int lev = 0; lev < m_nlevs; ++lev) {
400  m_surface_outputs_requested[lev] = true;
402  }
403  }
404 
405  /**
406  * @brief Enables blended surface outputs needed by the SurfaceLayer.
407  */
409  {
411  for (int lev = 0; lev < m_nlevs; ++lev) {
413  m_surface_outputs_requested[lev] = true;
415  }
416  }
417  }
418 
419  /**
420  * @brief Marks the registered surface fields as filled by an actual model advance.
421  */
423 
424  /**
425  * @brief Returns whether the surface models have advanced at least once.
426  * @return False until the first advance, while the registered fields still hold their
427  * initial values rather than anything the models computed.
428  */
429  bool fields_are_valid () const { return m_fields_are_valid; }
430 
431  /**
432  * @brief Returns the weighted horizontal momentum output for a level.
433  * @param lev AMR level to query.
434  * @return Pointer to the weighted u-star or momentum-flux field.
435  */
436  amrex::MultiFab* get_ustar (const int& lev) {
437  return u_star[lev].get();
438  }
439 
440  /**
441  * @brief Returns the weighted thermal output for a level.
442  * @param lev AMR level to query.
443  * @return Pointer to the weighted t-star or heat-flux field.
444  */
445  amrex::MultiFab* get_tstar (const int& lev) {
446  return t_star[lev].get();
447  }
448 
449  /**
450  * @brief Returns the weighted moisture output for a level.
451  * @param lev AMR level to query.
452  * @return Pointer to the weighted q-star or vapor-flux field.
453  */
454  amrex::MultiFab* get_qstar (const int& lev) {
455  return q_star[lev].get();
456  }
457 
458  /**
459  * @brief Returns the weighted surface-temperature output for a level.
460  * @param lev AMR level to query.
461  * @return Pointer to the weighted surface-temperature field.
462  */
463  amrex::MultiFab* get_tsurf (const int& lev) {
464  return t_surf[lev].get();
465  }
466 
467  /**
468  * @brief Returns provider or blended surface-flux views for one tile.
469  * @param lev AMR level to query.
470  * @param mfi Tile whose flux arrays are requested.
471  * @return Read-only flux views selected for the level's provider mode.
472  */
473  SurfaceFluxView get_surface_flux_view (const int lev, const amrex::MFIter& mfi) const
474  {
476 
477  const SurfaceProviderMode mode = m_provider_mode[lev];
478  if (mode == SurfaceProviderMode::Both) {
479  AMREX_ALWAYS_ASSERT(u_star[lev] != nullptr);
480  AMREX_ALWAYS_ASSERT(t_star[lev] != nullptr);
481  AMREX_ALWAYS_ASSERT(q_star[lev] != nullptr);
482  return {u_star[lev]->const_array(mfi, 0),
483  u_star[lev]->const_array(mfi, 1),
484  t_star[lev]->const_array(mfi),
485  q_star[lev]->const_array(mfi)};
486  }
487 
488  const auto& provider_fields = (mode == SurfaceProviderMode::LandOnly)
490  const auto& provider_data = (mode == SurfaceProviderMode::LandOnly)
491  ? lsm_data_lev[lev] : urban_data_lev[lev];
492  auto get_provider_field = [&] (const int field) -> amrex::Array4<const amrex::Real> {
493  if (field >= static_cast<int>(provider_fields.size())) {
494  return {};
495  }
496  const int index = provider_fields[field];
497  if (index < 0 || index >= static_cast<int>(provider_data.size()) ||
498  provider_data[index] == nullptr) {
499  return {};
500  }
501  return provider_data[index]->const_array(mfi);
502  };
503 
504  return {get_provider_field(0), get_provider_field(1),
505  get_provider_field(2), get_provider_field(3)};
506  }
507 
508  /**
509  * @brief Returns weighted data for a selected model field.
510  * @param lev AMR level to query.
511  * @param type Provider type owning the field.
512  * @param field_idx Provider field index.
513  * @return Weighted field or the source field for a single provider, or nullptr if it has not been selected.
514  */
515  const amrex::MultiFab* get_weighted_model_data (const int lev,
516  SurfaceModelType type,
517  const int field_idx) const {
518  const SurfaceProviderMode mode = m_provider_mode[lev];
519  const bool single_provider =
522  if (single_provider) {
523  const auto& selected = (type == SurfaceModelType::LAND)
525  const int first_output_field = m_export_fluxes ? 5 : 4;
526  bool is_weighted_field = false;
527  for (int field = first_output_field;
528  field < static_cast<int>(selected.size()); ++field) {
529  if (selected[field] == field_idx) {
530  is_weighted_field = true;
531  break;
532  }
533  }
534  if (!is_weighted_field) {
535  return nullptr;
536  }
537 
538  const auto& source = (type == SurfaceModelType::LAND)
539  ? lsm_data_lev[lev] : urban_data_lev[lev];
540  if (field_idx < 0 || field_idx >= static_cast<int>(source.size())) {
541  return nullptr;
542  }
543  return source[field_idx];
544  }
545 
546  const auto& weighted = (type == SurfaceModelType::LAND)
548  if (field_idx < 0 || field_idx >= static_cast<int>(weighted.size())) {
549  return nullptr;
550  }
551  return weighted[field_idx].get();
552  }
553 
554  /**
555  * @brief Returns the land and urban weighting factors for a level.
556  * @param lev AMR level to query.
557  * @return Pointer to the weighting-factor field.
558  */
559  amrex::MultiFab* get_wavg_factors (const int &lev) {
561  return wavg[lev].get();
562  }
563 
564  /**
565  * @brief Activates all registered mapped fields for an output consumer.
566  */
567  void activate_all_field_maps (const bool persistent = true);
568 
569  /**
570  * @brief Writes registered surface-model fields to plotfile output.
571  * @param finest_lev Finest AMR level to write.
572  * @param time Simulation time associated with the output.
573  * @param plot_prefix Output plotfile prefix.
574  * @param level_steps Number of steps represented at each level.
575  * @param ref_ratio Refinement ratios between adjacent levels.
576  */
577  void write_output (const int &finest_lev, const amrex::Real &time, const std::string &plot_prefix, const amrex::Vector<int> &level_steps, const amrex::Vector<amrex::IntVect> &ref_ratio);
578 
579  /**
580  * @brief Registers a field map using land and urban field indices.
581  * @param name Common name used to retrieve the mapped field.
582  * @param lsm_urb_map Pair of land and urban field indices.
583  * @param fill_boundary Whether to fill mapped-field boundaries.
584  */
585  void register_field_map (std::string name, const std::pair<int, int> &lsm_urb_map, bool fill_boundary=false);
586 
587  /**
588  * @brief Registers a field map using per-level land and urban pointers.
589  * @param name Common name used to retrieve the mapped field.
590  * @param lsm_lev_mf Land-model fields, indexed by AMR level.
591  * @param urb_lev_mf Urban-model fields, indexed by AMR level.
592  * @param fill_boundary Whether to fill mapped-field boundaries.
593  */
594  void register_field_map (std::string name, amrex::Vector<amrex::MultiFab*> &lsm_lev_mf, amrex::Vector<amrex::MultiFab*> &urb_lev_mf, bool fill_boundary=false);
595 
596  /**
597  * @brief Updates a pointer-based field mapping for one level.
598  * @param name Common name of the mapped field.
599  * @param lev AMR level to update.
600  * @param lsm_mf Land-model field pointer, or nullptr.
601  * @param urban_mf Urban-model field pointer, or nullptr.
602  */
603  void set_field_map_pointers (const std::string& name, int lev,
604  amrex::MultiFab* lsm_mf, amrex::MultiFab* urban_mf);
605 
606  /**
607  * @brief Registers a canonical radiation input mapping.
608  * @param name Canonical radiation input name.
609  * @param lsm_urb_map Pair of land and urban model field indices.
610  */
611  void register_radiation_input (const std::string& name,
612  const std::pair<int, int>& lsm_urb_map);
613 
614  /**
615  * @brief Registers canonical radiation input mappings.
616  * @param input_map Canonical names mapped to land and urban indices.
617  */
619  const std::unordered_map<std::string, std::pair<int, int>>& input_map);
620 
621  /**
622  * @brief Registers a canonical radiation output mapping.
623  * @param name Canonical radiation output name.
624  * @param lsm_urb_map Pair of land and urban model field indices.
625  */
626  void register_radiation_output (const std::string& name,
627  const std::pair<int, int>& lsm_urb_map);
628 
629  /**
630  * @brief Registers canonical radiation output mappings.
631  * @param output_map Canonical names mapped to land and urban indices.
632  */
634  const std::unordered_map<std::string, std::pair<int, int>>& output_map);
635 
636  /**
637  * @brief Retrieves a registered field by its common name.
638  * @param name Registered field name.
639  * @param lev AMR level to query.
640  * @return Pointer to the registered field, or nullptr if it is unknown.
641  */
642  amrex::MultiFab* get_field (const std::string &name, int lev = 0) {
643  if (auto field = fieldmap.find(name); field != fieldmap.end()) {
645  return fields[field->second.mf_ind][lev].get();
646  } else {
647  return nullptr;
648  }
649  }
650 
651  /**
652  * @brief Returns fields registered under the RRTMGP radiation names.
653  *
654  * The resulting map is cached after the first call for reuse on subsequent timesteps.
655  * When an expected field is missing, it is marked nullptr for the RRTMGP implementation
656  * to apply its fallback values.
657  *
658  * @param lev AMR level to query.
659  * @return Radiation fields in the order expected by RRTMGP.
660  */
661  const amrex::Vector<const amrex::MultiFab*> get_radiation_fields (int lev = 0) {
662  // check if we need to build the radiation list (for first time step,)
663  if (rad_fields[lev].size() == 0) {
665  }
666  return rad_fields[lev];
667  }
668 
669  /**
670  * @brief Returns canonical radiation output destinations for one level.
671  * @param lev AMR level to query.
672  * @return One destination per canonical radiation output.
673  */
674  const amrex::Vector<amrex::MultiFab*> get_radiation_output_fields (int lev = 0);
675 
676  /**
677  * @brief Returns the destination of one canonical radiation output by name.
678  * @param lev AMR level to query.
679  * @param name Canonical radiation output name (aborts on an unknown name).
680  * @return The registered destination, or nullptr when none is registered.
681  */
682  amrex::MultiFab* get_radiation_output_field (int lev, const std::string& name);
683 
684  /**
685  * @brief Distributes updated canonical radiation outputs to other providers.
686  * @param lev AMR level to update.
687  */
688  void distribute_radiation_outputs (int lev);
689 
690  /**
691  * @brief Distributes one updated canonical radiation output to other providers.
692  * @param lev AMR level to update.
693  * @param output_index Index in the canonical radiation output list.
694  */
695  void distribute_radiation_output (int lev, int output_index);
696 
697  /**
698  * @brief Advances an input stream to the next line.
699  *
700  * taken from ERF_Checkpoint, reuse?
701  *
702  * @param is Input stream to advance.
703  */
704  static void GotoNextLine (std::istream& is);
705 
706  /**
707  * @brief Writes surface-model state to a checkpoint file.
708  * @param checkpointname Checkpoint file name.
709  */
710  void WriteCheckpoint (const std::string &checkpointname);
711 
712  /**
713  * @brief Restores surface-model state from a checkpoint file.
714  * @param checkpointname Checkpoint file name.
715  */
716  void ReadCheckpoint (const std::string &checkpointname);
717 
718  /**
719  * @brief Validates the layout contract for a canonical radiation output.
720  * @param lev AMR level associated with the destination.
721  * @param mf Radiation output destination to validate.
722  */
723  void validate_radiation_output_layout (int lev, const amrex::MultiFab* mf) const;
724 
725  /**
726  * @brief Computes simple land and urban weights from the urban fraction.
727  * @param lev AMR level to update.
728  * @param urban_frac Urban fraction used to compute the weights.
729  */
730  void calculate_simple_average (int lev, amrex::MultiFab* const urban_frac);
731 
732  /**
733  * @brief Builds the ordered radiation-field list for one AMR level.
734  * @param lev AMR level to update.
735  */
736  void build_radiation_fieldlist (int lev) {
737  amrex::Print() << " --- building radiation field list" << std::endl;
738 
739  // loop over fields and check against names of radiation variables
740  for (int li = 0; li < static_cast<int>(radnames.size()); ++li) {
741  amrex::Print() << " - Checking for radiation variable '" << radnames[li] << "'" << std::endl;
742 
743  amrex::MultiFab* mf = nullptr;
744  auto rad_it = radiation_input_map.find(radnames[li]);
745  if (rad_it != radiation_input_map.end()) {
746  const int land_idx = rad_it->second.map.first;
747  const int urban_idx = rad_it->second.map.second;
748  const SurfaceProviderMode mode = m_provider_mode[lev];
749  const bool use_land = mode == SurfaceProviderMode::LandOnly ||
751  const bool use_urban = mode == SurfaceProviderMode::UrbanOnly ||
753  const bool valid_land = use_land && land_idx >= 0 &&
754  lev < static_cast<int>(lsm_data_lev.size()) &&
755  land_idx < static_cast<int>(lsm_data_lev[lev].size()) &&
756  lsm_data_lev[lev][land_idx] != nullptr;
757  const bool valid_urban = use_urban && urban_idx >= 0 &&
758  lev < static_cast<int>(urban_data_lev.size()) &&
759  urban_idx < static_cast<int>(urban_data_lev[lev].size()) &&
760  urban_data_lev[lev][urban_idx] != nullptr;
761  if (valid_land && valid_urban) {
762  if (!rad_it->second.weighted[lev]) {
763  rad_it->second.weighted[lev] = std::make_unique<amrex::MultiFab>(
764  m_ba2d[lev], m_dmap[lev], 1, amrex::IntVect(1,1,0));
765  }
766  auto& weighted = *rad_it->second.weighted[lev];
767  const auto& land = *lsm_data_lev[lev][land_idx];
768  const auto& urban = *urban_data_lev[lev][urban_idx];
769  for (amrex::MFIter mfi(weighted); mfi.isValid(); ++mfi) {
770  const amrex::Box bx = mfi.tilebox();
771  const auto weights = wavg[lev]->const_array(mfi);
772  const auto land_arr = land.const_array(mfi);
773  const auto urban_arr = urban.const_array(mfi);
774  // LSM surface fields may be stored at k <= 0; use the top valid LSM
775  // plane, which matches k=0 when the LSM keeps those fields synchronized
776  // at the surface (such as SLM). Urban surface fields are stored at k=0.
777  const int land_k = land.box(mfi.index()).bigEnd(2);
778  const int urban_k = urban.box(mfi.index()).smallEnd(2);
779  auto output = weighted.array(mfi);
780  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
781  output(i,j,k) = land_arr(i,j,land_k) *
782  weights(i,j,0,SurfaceModelType::LAND) +
783  urban_arr(i,j,urban_k) *
784  weights(i,j,0,SurfaceModelType::URBAN);
785  });
786  }
787  weighted.FillBoundary(m_geom[lev].periodicity());
788  mf = rad_it->second.weighted[lev].get();
789  } else if (valid_land) {
790  mf = lsm_data_lev[lev][land_idx];
791  } else if (valid_urban) {
792  mf = urban_data_lev[lev][urban_idx];
793  }
794  }
795  if (mf) {
796  rad_fields[lev].push_back(mf);
797  } else {
798  amrex::Print() << " - WARNING: did not find radiation variable, setting to null to use default RRTMGP value!" << std::endl;
799  rad_fields[lev].push_back(nullptr);
800  }
801  }
803  static_cast<int>(rad_fields[lev].size()) == static_cast<int>(radnames.size()),
804  "Did not find all radiation fields");
805  }
806 
807  /**
808  * @brief Applies the current land and urban weights to selected model fields.
809  * @param lev AMR level to update.
810  * @param urban_frac Urban fraction used to compute the weights.
811  */
812  void weight_average_fields (int lev, amrex::MultiFab* const /*urban_frac*/);
813 
814  /**
815  * @brief Activates a mapped field and allocates its storage on all levels.
816  * @param name Common mapped field name.
817  */
818  void activate_field_map (const std::string& name, const bool persistent = true);
819 
820  /**
821  * @brief Disables and releases mapped fields used only by the last output.
822  */
824 
825  /**
826  * @brief Checks whether a model field participates in a registered mapping.
827  * @param lev AMR level to query.
828  * @param type Land or urban model owning the field.
829  * @param field_idx Index of the model field.
830  * @param mf Field pointer to test.
831  * @return True when the field is part of a registered mapping.
832  */
833  bool is_field_mapped (int lev, SurfaceModelType type, int field_idx,
834  const amrex::MultiFab* mf) const;
835 
836  /**
837  * @brief Writes a weighted copy of a model field.
838  * @param lev AMR level whose weights are used.
839  * @param source Model-owned source field.
840  * @param weighted SurfaceModel-owned destination field.
841  * @param type Provider type owning the source field.
842  */
843  void weight_model_field (int lev, const amrex::MultiFab* source,
844  amrex::MultiFab* weighted, SurfaceModelType type);
845 
846  /** A field to interpolate to a new fine level: its fine and coarse data and components. */
848  {
849  amrex::MultiFab* fine;
850  const amrex::MultiFab* crse;
851  int ncomp;
852  };
853 
854  /**
855  * @brief Fills fine-level surface fields from the next coarser level with interp_by_type.
856  *
857  * Each component is typed valid or invalid (is_valid_lsm_value) per coarse cell, so a
858  * valid fine cell takes no slope toward an invalid coarse cell; where every neighbor is
859  * valid this is cell_cons_interp. A fine cell whose parent is invalid is set to
860  * lsm_undefined. Fine ghost cells outside the domain keep their values.
861  *
862  * @param lev Fine AMR level.
863  * @param transfers Fields to fill; all share one fine and one coarse layout.
864  * @param bc Boundary condition every field takes.
865  * @param ratio Refinement ratio from level lev-1 to lev.
866  */
868  const amrex::Vector<InterpTransfer>& transfers,
869  const amrex::BCRec& bc, const amrex::IntVect& ratio)
870  {
871  constexpr amrex::Real valid_type = 0.0;
872  constexpr amrex::Real undefined_type = 1.0;
873 
874  int nfield = 0;
875  for (const auto& t : transfers) { nfield += t.ncomp; }
876 
877  // Each pack holds the nfield components and then their nfield types
878  const amrex::MultiFab& f0 = *transfers[0].fine;
879  const amrex::MultiFab& c0 = *transfers[0].crse;
880  amrex::MultiFab fine_pack(f0.boxArray(), f0.DistributionMap(), 2*nfield, f0.nGrowVect());
881  amrex::MultiFab crse_pack(c0.boxArray(), c0.DistributionMap(), 2*nfield, c0.nGrowVect());
882  int n0 = 0;
883  for (const auto& t : transfers) {
884  AMREX_ALWAYS_ASSERT(t.fine->boxArray() == f0.boxArray() &&
885  t.fine->DistributionMap() == f0.DistributionMap() &&
886  t.fine->nGrowVect() == f0.nGrowVect());
887  AMREX_ALWAYS_ASSERT(t.crse->boxArray() == c0.boxArray() &&
888  t.crse->DistributionMap() == c0.DistributionMap() &&
889  t.crse->nGrowVect() == c0.nGrowVect());
890  amrex::MultiFab::Copy(fine_pack, *t.fine, 0, n0, t.ncomp, f0.nGrowVect());
891  amrex::MultiFab::Copy(crse_pack, *t.crse, 0, n0, t.ncomp, c0.nGrowVect());
892  n0 += t.ncomp;
893  }
894 
895  // The interpolation reads the coarse ghost cells; fill them in the pack only, so the
896  // coarse fields themselves are not touched
897  crse_pack.FillBoundary(0, nfield, m_geom2d[lev-1].periodicity());
898  for (amrex::MFIter mfi(crse_pack, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
899  const auto p = crse_pack.array(mfi);
900  amrex::ParallelFor(mfi.growntilebox(), nfield,
901  [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
902  {
903  p(i,j,k,nfield+n) = is_valid_lsm_value(p(i,j,k,n)) ? valid_type : undefined_type;
904  });
905  }
906  // The fine types come out as their parents'; a cell the interpolation does not fill
907  // keeps valid_type and so its value
908  fine_pack.setVal(valid_type, nfield, nfield, f0.nGrowVect());
909 
910  // The fields are planar
911  amrex::IntVect rr2d = ratio;
912  rr2d[2] = 1;
913  interp_by_type_packed(fine_pack, crse_pack, nfield, m_geom2d[lev-1], m_geom2d[lev], rr2d,
914  bc);
915 
916  for (amrex::MFIter mfi(fine_pack, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
917  const auto p = fine_pack.array(mfi);
918  amrex::ParallelFor(mfi.growntilebox(), nfield,
919  [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
920  {
921  if (p(i,j,k,nfield+n) != valid_type) { p(i,j,k,n) = lsm_undefined; }
922  });
923  }
924 
925  n0 = 0;
926  for (const auto& t : transfers) {
927  amrex::MultiFab::Copy(*t.fine, fine_pack, n0, 0, t.ncomp, f0.nGrowVect());
928  n0 += t.ncomp;
929  }
930  }
931 
932 private:
933 
934  /**
935  * @brief Sets the number of AMR levels and resizes all per-level storage to match.
936  *
937  * Shared by the constructor, initialize_for_level and clear_level so that every
938  * per-level member is grown and shrunk together. New levels start empty.
939  *
940  * @param nlevs New number of AMR levels.
941  */
942  void resize_levels (int nlevs)
943  {
944  m_nlevs = nlevs;
945 
946  u_star.resize(m_nlevs);
947  t_star.resize(m_nlevs);
948  q_star.resize(m_nlevs);
949  t_surf.resize(m_nlevs);
950 
951  wavg.resize(m_nlevs);
953 
954  m_dmap.resize(m_nlevs);
955  m_ba.resize(m_nlevs);
956  m_ba2d.resize(m_nlevs);
957  m_geom.resize(m_nlevs);
958  m_geom2d.resize(m_nlevs);
959  m_lmask.resize(m_nlevs);
960 
961  urban_frac_lev.resize(m_nlevs);
962  lsm_data_lev.resize(m_nlevs);
963  urban_data_lev.resize(m_nlevs);
966 
967  rad_fields.resize(m_nlevs);
968  rad_output_fields.resize(m_nlevs);
970  m_last_urban_frac.resize(m_nlevs, nullptr);
971 
972  for (auto& field_lev : fields) { field_lev.resize(m_nlevs); }
973  for (auto& entry : fieldmap) {
974  entry.second.lsm_ptr.resize(m_nlevs, nullptr);
975  entry.second.urb_ptr.resize(m_nlevs, nullptr);
976  }
977  for (auto& entry : radiation_input_map) { entry.second.weighted.resize(m_nlevs); }
978  }
979 
980  /**
981  * @brief Updates the provider mode after model data are registered.
982  * @param lev AMR level whose provider mode should be updated.
983  */
984  void update_provider_mode (const int lev)
985  {
986  const bool has_land = !lsm_data_lev[lev].empty();
987  const bool has_urban = !urban_data_lev[lev].empty();
988  if (has_land && has_urban) {
992  m_surface_outputs_requested[lev] = true;
994  }
995  } else if (has_land) {
997  wavg[lev].reset();
998  } else if (has_urban) {
1000  wavg[lev].reset();
1001  } else {
1003  }
1004  }
1005 
1006  /**
1007  * @brief Creates weighting factors when an external caller requests them.
1008  * @param lev AMR level whose weighting factors should be materialized.
1009  */
1010  void ensure_weight_factors (const int lev)
1011  {
1012  if (wavg[lev] != nullptr) { return; }
1013 
1014  const amrex::IntVect ng(1,1,0);
1015  wavg[lev] = std::make_unique<amrex::MultiFab>(m_ba2d[lev], m_dmap[lev], 2, ng);
1016  const SurfaceProviderMode mode = m_provider_mode[lev];
1017  wavg[lev]->setVal(mode != SurfaceProviderMode::UrbanOnly ? 1.0 : 0.0,
1018  SurfaceModelType::LAND, 1, 0);
1019  wavg[lev]->setVal(mode == SurfaceProviderMode::UrbanOnly ? 1.0 : 0.0,
1020  SurfaceModelType::URBAN, 1, 0);
1021  }
1022 
1023  /**
1024  * @brief Allocates surface-output fields for one level.
1025  * @param lev AMR level whose outputs should be materialized.
1026  */
1027  void ensure_surface_outputs (const int lev)
1028  {
1029  if (u_star[lev] != nullptr) { return; }
1030 
1031  const amrex::IntVect ng(1,1,0);
1032  u_star[lev] = std::make_unique<amrex::MultiFab>(m_ba2d[lev], m_dmap[lev], 2, ng);
1033  t_star[lev] = std::make_unique<amrex::MultiFab>(m_ba2d[lev], m_dmap[lev], 1, ng);
1034  q_star[lev] = std::make_unique<amrex::MultiFab>(m_ba2d[lev], m_dmap[lev], 1, ng);
1035  t_surf[lev] = std::make_unique<amrex::MultiFab>(m_ba2d[lev], m_dmap[lev], 1, ng);
1036  u_star[lev]->setVal(lsm_undefined);
1037  t_star[lev]->setVal(lsm_undefined);
1038  q_star[lev]->setVal(lsm_undefined);
1039  t_surf[lev]->setVal(300.0);
1040  }
1041 
1042  /**
1043  * @brief Releases surface outputs that have no persistent consumer.
1044  */
1046  {
1047  if (m_surface_outputs_enabled) { return; }
1048  for (int lev = 0; lev < m_nlevs; ++lev) {
1051  continue;
1052  }
1053  m_surface_outputs_requested[lev] = false;
1054  u_star[lev].reset();
1055  t_star[lev].reset();
1056  q_star[lev].reset();
1057  t_surf[lev].reset();
1058  }
1059  }
1060 
1061  int m_nlevs;
1062 
1063  bool m_use_urban = false; // whether we have an urban model
1064  bool m_use_land = false; // whether we have a LSM
1065 
1066  bool m_export_fluxes = true;
1067  bool m_export_fluxes_set = false; // whether a model has registered and latched the mode above
1068  amrex::GpuArray<bool, 2> m_output_fields_registered{{false, false}};
1069 
1070  // Set once the surface models have actually integrated at least one step, so consumers can
1071  // tell registered-but-unfilled fields from real ones.
1072  bool m_fields_are_valid = false;
1073 
1074  // internal flag to indicate if the weight average has been calculated this time step
1075  bool m_weights_updated = false;
1076 
1077  amrex::Vector<SurfaceProviderMode> m_provider_mode;
1078 
1079  amrex::Vector<amrex::BoxArray> m_ba;
1080  amrex::Vector<amrex::BoxArray> m_ba2d;
1081  amrex::Vector<amrex::Geometry> m_geom;
1082  amrex::Vector<amrex::DistributionMapping> m_dmap;
1083 
1084  amrex::Vector<amrex::Geometry> m_geom2d;
1085  amrex::Vector<amrex::iMultiFab*> m_lmask;
1086 
1087  // Pointers to LSM and Urban model data
1088  amrex::Vector<amrex::MultiFab*> urban_frac_lev; // Urban fraction for each level [0-1.0]
1089  amrex::Vector<amrex::Vector<amrex::MultiFab*>> lsm_data_lev; // LSM data for each level
1090  amrex::Vector<amrex::Vector<amrex::MultiFab*>> urban_data_lev; // Urban data for each level
1091  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> weighted_lsm_data_lev;
1092  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> weighted_urban_data_lev;
1093 
1094  // Output weighted averages passed to MOST
1095  // These can either be the u*, t*, q* for MOST, or fluxes computed directly from the model
1096  amrex::Vector<std::unique_ptr<amrex::MultiFab>> u_star; // up to 2 components: X-stress, Y-stress
1097  amrex::Vector<std::unique_ptr<amrex::MultiFab>> t_star;
1098  amrex::Vector<std::unique_ptr<amrex::MultiFab>> q_star;
1099  amrex::Vector<std::unique_ptr<amrex::MultiFab>> t_surf;
1100 
1101  amrex::Vector<int> m_surface_outputs_requested;
1104  amrex::Vector<amrex::MultiFab*> m_last_urban_frac;
1105 
1106  // Current weight average factors for each level (2 components: land and urban)
1107  amrex::Vector<std::unique_ptr<amrex::MultiFab>> wavg;
1108 
1109  // Model variables to weight average
1110  // The flux interface uses u-stress, v-stress, heat flux, vapor flux, tsurf.
1111  // TODO: make this more flexible for different types of models
1112  amrex::Vector<int> lsm_fields;
1113  amrex::Vector<int> urban_fields;
1114 
1115  // These are the names to be consumed in RRTMGP, but the mapped LSM/Urban names can be different
1116  inline static const std::vector<std::string> radnames = {"tskin", "emiss", "albedo_vis", "albedo_nir", "albedo_vis_diff", "albedo_nir_diff"};
1117 
1118  /**
1119  * @brief Describes a registered land/urban field mapping.
1120  *
1121  * A Field associates the common name used by ERF consumers with either
1122  * model-specific field indices or per-level field pointers. The mapped
1123  * fields are stored in the SurfaceModel-owned field array and the source
1124  * pointers remain owned by their respective surface models.
1125  */
1126  struct Field {
1127  /// Land and urban model field indices, respectively.
1128  std::pair<int, int> map;
1129  /// Per-level land-model field pointers.
1130  amrex::Vector<amrex::MultiFab*> lsm_ptr;
1131  /// Per-level urban-model field pointers.
1132  amrex::Vector<amrex::MultiFab*> urb_ptr;
1133  /// Index of the mapped field in the SurfaceModel field array.
1134  int mf_ind;
1135  /// Whether boundaries are filled after mapping the field.
1137  /// Whether a consumer currently requires this mapped field.
1138  bool active = false;
1139  /// Whether the field must be updated on every timestep.
1140  bool persistent_consumer = false;
1141  };
1142  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>> fields; // NOTE: this is stored in inverse order: (field, nlev)
1143  std::unordered_map<std::string, Field> fieldmap;
1144 
1145  amrex::Vector<std::string> m_lsm_names;
1146  amrex::Vector<std::string> m_urban_names;
1147 
1148  amrex::Vector<amrex::Vector<const amrex::MultiFab*>> rad_fields; // pointers to radiation MFs at each level
1149  amrex::Vector<amrex::Vector<amrex::MultiFab*>> rad_output_fields;
1150 
1151  inline static const std::vector<std::string> rad_output_names = {
1152  "cos_zenith_angle", "sw_flux_dn", "sw_flux_dn_dir_vis",
1153  "sw_flux_dn_dir_nir", "sw_flux_dn_dif_vis", "sw_flux_dn_dif_nir",
1154  "lw_flux_dn"};
1155 
1157  std::pair<int, int> map;
1158  amrex::Vector<std::unique_ptr<amrex::MultiFab>> weighted;
1159  };
1160  std::unordered_map<std::string, RadiationField> radiation_input_map;
1161  std::unordered_map<std::string, RadiationField> radiation_output_map;
1162 
1163  // plotfile names for outputs
1164  inline static const std::vector<std::string> field_names = {
1165  "uflux",
1166  "vflux",
1167  "hfx",
1168  "qfx",
1169  "tskin",
1170  };
1171 };
1172 
1173 #endif
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool is_valid_lsm_value(amrex::Real v)
Definition: ERF_Constants.H:38
constexpr amrex::Real lsm_undefined
Definition: ERF_Constants.H:28
pp_for_pert_vels get("prob_type", prob_type)
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
SurfaceProviderMode
Definition: ERF_SurfaceModel.H:19
SurfaceModelType
Definition: ERF_SurfaceModel.H:17
@ LAND
Definition: ERF_SurfaceModel.H:17
@ URBAN
Definition: ERF_SurfaceModel.H:17
void interp_by_type_packed(amrex::MultiFab &fdst, amrex::MultiFab &csrc, int nind, const amrex::Geometry &cgeom, const amrex::Geometry &fgeom, const amrex::IntVect &ratio, const amrex::BCRec &bc)
Definition: ERF_TypedInterp.H:200
Interface between ERF and land-surface and urban models.
Definition: ERF_SurfaceModel.H:60
amrex::MultiFab * get_tsurf(const int &lev)
Returns the weighted surface-temperature output for a level.
Definition: ERF_SurfaceModel.H:463
void clear_level(int lev)
Removes an AMR level, and every level above it, from the surface model.
Definition: ERF_SurfaceModel.H:193
amrex::Vector< amrex::MultiFab * > urban_frac_lev
Definition: ERF_SurfaceModel.H:1088
std::unordered_map< std::string, RadiationField > radiation_input_map
Definition: ERF_SurfaceModel.H:1160
void resize_levels(int nlevs)
Sets the number of AMR levels and resizes all per-level storage to match.
Definition: ERF_SurfaceModel.H:942
void apply_weight_average(int lev, const amrex::MultiFab *lsm_data, amrex::MultiFab *lsm_weighted, const amrex::MultiFab *urban_data, amrex::MultiFab *urban_weighted)
Applies land and urban weights into separate output fields.
Definition: ERF_SurfaceModel.cpp:12
void interp_surface_fields_from_coarse(const int lev, const amrex::Vector< InterpTransfer > &transfers, const amrex::BCRec &bc, const amrex::IntVect &ratio)
Fills fine-level surface fields from the next coarser level with interp_by_type.
Definition: ERF_SurfaceModel.H:867
std::unordered_map< std::string, RadiationField > radiation_output_map
Definition: ERF_SurfaceModel.H:1161
void register_radiation_output(const std::string &name, const std::pair< int, int > &lsm_urb_map)
Registers a canonical radiation output mapping.
Definition: ERF_SurfaceModel.cpp:350
amrex::Vector< std::string > m_lsm_names
Definition: ERF_SurfaceModel.H:1145
int m_nlevs
Definition: ERF_SurfaceModel.H:1061
amrex::MultiFab * get_radiation_output_field(int lev, const std::string &name)
Returns the destination of one canonical radiation output by name.
Definition: ERF_SurfaceModel.cpp:421
amrex::Vector< amrex::BoxArray > m_ba2d
Definition: ERF_SurfaceModel.H:1080
bool m_weights_updated
Definition: ERF_SurfaceModel.H:1075
void set_field_map_pointers(const std::string &name, int lev, amrex::MultiFab *lsm_mf, amrex::MultiFab *urban_mf)
Updates a pointer-based field mapping for one level.
Definition: ERF_SurfaceModel.cpp:629
bool is_field_mapped(int lev, SurfaceModelType type, int field_idx, const amrex::MultiFab *mf) const
Checks whether a model field participates in a registered mapping.
Definition: ERF_SurfaceModel.cpp:502
void write_output(const int &finest_lev, const amrex::Real &time, const std::string &plot_prefix, const amrex::Vector< int > &level_steps, const amrex::Vector< amrex::IntVect > &ref_ratio)
Writes registered surface-model fields to plotfile output.
Definition: ERF_SurfaceModel.cpp:768
amrex::Vector< amrex::Geometry > m_geom
Definition: ERF_SurfaceModel.H:1081
bool m_export_fluxes
Definition: ERF_SurfaceModel.H:1066
static const std::vector< std::string > rad_output_names
Definition: ERF_SurfaceModel.H:1151
void activate_field_map(const std::string &name, const bool persistent=true)
Activates a mapped field and allocates its storage on all levels.
Definition: ERF_SurfaceModel.cpp:647
amrex::Vector< amrex::Vector< amrex::MultiFab * > > rad_output_fields
Definition: ERF_SurfaceModel.H:1149
void weight_model_field(int lev, const amrex::MultiFab *source, amrex::MultiFab *weighted, SurfaceModelType type)
Writes a weighted copy of a model field.
Definition: ERF_SurfaceModel.cpp:43
amrex::Vector< amrex::MultiFab * > m_last_urban_frac
Definition: ERF_SurfaceModel.H:1104
void register_radiation_outputs(const std::unordered_map< std::string, std::pair< int, int >> &output_map)
Registers canonical radiation output mappings.
Definition: ERF_SurfaceModel.cpp:380
const amrex::Vector< const amrex::MultiFab * > get_radiation_fields(int lev=0)
Returns fields registered under the RRTMGP radiation names.
Definition: ERF_SurfaceModel.H:661
void distribute_radiation_outputs(int lev)
Distributes updated canonical radiation outputs to other providers.
Definition: ERF_SurfaceModel.cpp:449
amrex::MultiFab * get_ustar(const int &lev)
Returns the weighted horizontal momentum output for a level.
Definition: ERF_SurfaceModel.H:436
void register_field_map(std::string name, const std::pair< int, int > &lsm_urb_map, bool fill_boundary=false)
Registers a field map using land and urban field indices.
Definition: ERF_SurfaceModel.cpp:575
bool m_surface_layer_outputs_enabled
Definition: ERF_SurfaceModel.H:1103
amrex::MultiFab * get_tstar(const int &lev)
Returns the weighted thermal output for a level.
Definition: ERF_SurfaceModel.H:445
static void GotoNextLine(std::istream &is)
Advances an input stream to the next line.
Definition: ERF_SurfaceModel.cpp:846
amrex::Vector< std::unique_ptr< amrex::MultiFab > > q_star
Definition: ERF_SurfaceModel.H:1098
amrex::Vector< std::string > m_urban_names
Definition: ERF_SurfaceModel.H:1146
amrex::Vector< amrex::BoxArray > m_ba
Definition: ERF_SurfaceModel.H:1079
bool m_fields_are_valid
Definition: ERF_SurfaceModel.H:1072
amrex::Vector< int > lsm_fields
Definition: ERF_SurfaceModel.H:1112
void release_transient_surface_outputs()
Releases surface outputs that have no persistent consumer.
Definition: ERF_SurfaceModel.H:1045
std::unordered_map< std::string, Field > fieldmap
Definition: ERF_SurfaceModel.H:1143
void ensure_surface_outputs(const int lev)
Allocates surface-output fields for one level.
Definition: ERF_SurfaceModel.H:1027
amrex::Vector< std::unique_ptr< amrex::MultiFab > > t_surf
Definition: ERF_SurfaceModel.H:1099
void ReadCheckpoint(const std::string &checkpointname)
Restores surface-model state from a checkpoint file.
Definition: ERF_SurfaceModel.cpp:969
SurfaceFluxView get_surface_flux_view(const int lev, const amrex::MFIter &mfi) const
Returns provider or blended surface-flux views for one tile.
Definition: ERF_SurfaceModel.H:473
SurfaceModel(int nlevs, const amrex::Vector< amrex::BoxArray > &ba, const amrex::Vector< amrex::Geometry > &geom, const amrex::Vector< amrex::DistributionMapping > &dm, const SolverChoice &, amrex::Vector< amrex::Vector< std::unique_ptr< amrex::iMultiFab >>> &)
Constructs a surface-model interface for the supplied AMR levels.
Definition: ERF_SurfaceModel.H:73
amrex::Vector< SurfaceProviderMode > m_provider_mode
Definition: ERF_SurfaceModel.H:1077
bool m_use_urban
Definition: ERF_SurfaceModel.H:1063
void set_model_fields(SurfaceModelType type, const amrex::Vector< int > &field_indices, const bool use_fluxes=true)
Selects the model fields that participate in weighted averaging.
Definition: ERF_SurfaceModel.H:316
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > weighted_urban_data_lev
Definition: ERF_SurfaceModel.H:1092
void distribute_radiation_output(int lev, int output_index)
Distributes one updated canonical radiation output to other providers.
Definition: ERF_SurfaceModel.cpp:456
static const std::vector< std::string > radnames
Definition: ERF_SurfaceModel.H:1116
const amrex::Vector< amrex::MultiFab * > get_radiation_output_fields(int lev=0)
Returns canonical radiation output destinations for one level.
Definition: ERF_SurfaceModel.cpp:388
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > weighted_lsm_data_lev
Definition: ERF_SurfaceModel.H:1091
amrex::GpuArray< bool, 2 > m_output_fields_registered
Definition: ERF_SurfaceModel.H:1068
void calculate_simple_average(int lev, amrex::MultiFab *const urban_frac)
Computes simple land and urban weights from the urban fraction.
Definition: ERF_SurfaceModel.cpp:537
amrex::MultiFab * get_wavg_factors(const int &lev)
Returns the land and urban weighting factors for a level.
Definition: ERF_SurfaceModel.H:559
amrex::Vector< std::unique_ptr< amrex::MultiFab > > u_star
Definition: ERF_SurfaceModel.H:1096
const amrex::MultiFab * get_weighted_model_data(const int lev, SurfaceModelType type, const int field_idx) const
Returns weighted data for a selected model field.
Definition: ERF_SurfaceModel.H:515
void register_radiation_input(const std::string &name, const std::pair< int, int > &lsm_urb_map)
Registers a canonical radiation input mapping.
Definition: ERF_SurfaceModel.cpp:319
void validate_radiation_output_layout(int lev, const amrex::MultiFab *mf) const
Validates the layout contract for a canonical radiation output.
Definition: ERF_SurfaceModel.cpp:429
void calculate_weight_average(int lev, amrex::MultiFab *const urban_frac, bool update_derived=true)
Computes land and urban weights and applies them to registered fields.
Definition: ERF_SurfaceModel.cpp:71
amrex::Vector< std::unique_ptr< amrex::MultiFab > > t_star
Definition: ERF_SurfaceModel.H:1097
void build_radiation_fieldlist(int lev)
Builds the ordered radiation-field list for one AMR level.
Definition: ERF_SurfaceModel.H:736
void update_provider_mode(const int lev)
Updates the provider mode after model data are registered.
Definition: ERF_SurfaceModel.H:984
void register_radiation_inputs(const std::unordered_map< std::string, std::pair< int, int >> &input_map)
Registers canonical radiation input mappings.
Definition: ERF_SurfaceModel.cpp:342
void weight_average_fields(int lev, amrex::MultiFab *const)
Applies the current land and urban weights to selected model fields.
Definition: ERF_SurfaceModel.cpp:689
void set_model_data(const int lev, const amrex::Vector< amrex::MultiFab * > model_data, const amrex::Vector< std::string > &data_names, SurfaceModelType type)
Registers model variables used in weighted averaging.
Definition: ERF_SurfaceModel.H:207
static const std::vector< std::string > field_names
Definition: ERF_SurfaceModel.H:1164
amrex::Vector< amrex::Geometry > m_geom2d
Definition: ERF_SurfaceModel.H:1084
void mark_fields_valid()
Marks the registered surface fields as filled by an actual model advance.
Definition: ERF_SurfaceModel.H:422
amrex::Vector< int > urban_fields
Definition: ERF_SurfaceModel.H:1113
void ensure_weight_factors(const int lev)
Creates weighting factors when an external caller requests them.
Definition: ERF_SurfaceModel.H:1010
amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab > > > fields
Definition: ERF_SurfaceModel.H:1142
amrex::Vector< amrex::Vector< amrex::MultiFab * > > lsm_data_lev
Definition: ERF_SurfaceModel.H:1089
void activate_all_field_maps(const bool persistent=true)
Activates all registered mapped fields for an output consumer.
Definition: ERF_SurfaceModel.cpp:669
bool are_fluxes()
Returns whether the interface exports fluxes rather than MOST variables.
Definition: ERF_SurfaceModel.H:391
void WriteCheckpoint(const std::string &checkpointname)
Writes surface-model state to a checkpoint file.
Definition: ERF_SurfaceModel.cpp:852
amrex::Vector< int > m_surface_outputs_requested
Definition: ERF_SurfaceModel.H:1101
amrex::Vector< amrex::Vector< const amrex::MultiFab * > > rad_fields
Definition: ERF_SurfaceModel.H:1148
amrex::Vector< std::unique_ptr< amrex::MultiFab > > wavg
Definition: ERF_SurfaceModel.H:1107
bool fields_are_valid() const
Returns whether the surface models have advanced at least once.
Definition: ERF_SurfaceModel.H:429
void request_surface_outputs(const bool persistent=true)
Enables and materializes the surface outputs consumed by ERF.
Definition: ERF_SurfaceModel.H:396
amrex::Vector< amrex::iMultiFab * > m_lmask
Definition: ERF_SurfaceModel.H:1085
amrex::Vector< amrex::Vector< amrex::MultiFab * > > urban_data_lev
Definition: ERF_SurfaceModel.H:1090
amrex::MultiFab * get_qstar(const int &lev)
Returns the weighted moisture output for a level.
Definition: ERF_SurfaceModel.H:454
void set_model_fluxes(const int lev, const amrex::Vector< amrex::MultiFab * > model_fluxes, const amrex::Vector< std::string > &flux_names, SurfaceModelType type)
Registers model fluxes as additional weighted-average inputs.
Definition: ERF_SurfaceModel.H:271
bool m_export_fluxes_set
Definition: ERF_SurfaceModel.H:1067
amrex::MultiFab * get_field(const std::string &name, int lev=0)
Retrieves a registered field by its common name.
Definition: ERF_SurfaceModel.H:642
void initialize_for_level(int lev, const amrex::BoxArray &ba, const amrex::Geometry &geom, const amrex::DistributionMapping &dm, const amrex::Vector< std::unique_ptr< amrex::iMultiFab >> &lmask_lev, const amrex::Vector< amrex::BCRec > &domain_bcs_type, const amrex::Vector< amrex::IntVect > &refRatio)
Initializes storage and boundary data for an AMR level.
Definition: ERF_SurfaceModel.H:103
bool m_surface_outputs_enabled
Definition: ERF_SurfaceModel.H:1102
void deactivate_transient_field_maps()
Disables and releases mapped fields used only by the last output.
Definition: ERF_SurfaceModel.cpp:676
amrex::Vector< amrex::DistributionMapping > m_dmap
Definition: ERF_SurfaceModel.H:1082
bool m_use_land
Definition: ERF_SurfaceModel.H:1064
void request_surface_layer_outputs()
Enables blended surface outputs needed by the SurfaceLayer.
Definition: ERF_SurfaceModel.H:408
@ cons_bc
Definition: ERF_IndexDefines.H:122
@ ng
Definition: ERF_Morrison.H:51
@ t
Definition: ERF_WSM6.H:295
@ p
Definition: ERF_WSM6.H:303
real(c_double), private bc
Definition: ERF_module_mp_morr_two_moment.F90:182
Definition: ERF_DataStruct.H:743
Non-owning views of surface fluxes for one tile.
Definition: ERF_SurfaceModel.H:24
amrex::Array4< const amrex::Real > q_flux
Definition: ERF_SurfaceModel.H:28
amrex::Array4< const amrex::Real > tau23
Definition: ERF_SurfaceModel.H:26
amrex::Array4< const amrex::Real > tau13
Definition: ERF_SurfaceModel.H:25
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid(int i, int j) const noexcept
Definition: ERF_SurfaceModel.H:32
amrex::Array4< const amrex::Real > t_flux
Definition: ERF_SurfaceModel.H:27
Describes a registered land/urban field mapping.
Definition: ERF_SurfaceModel.H:1126
bool fill_bound
Whether boundaries are filled after mapping the field.
Definition: ERF_SurfaceModel.H:1136
bool persistent_consumer
Whether the field must be updated on every timestep.
Definition: ERF_SurfaceModel.H:1140
amrex::Vector< amrex::MultiFab * > urb_ptr
Per-level urban-model field pointers.
Definition: ERF_SurfaceModel.H:1132
std::pair< int, int > map
Land and urban model field indices, respectively.
Definition: ERF_SurfaceModel.H:1128
int mf_ind
Index of the mapped field in the SurfaceModel field array.
Definition: ERF_SurfaceModel.H:1134
bool active
Whether a consumer currently requires this mapped field.
Definition: ERF_SurfaceModel.H:1138
amrex::Vector< amrex::MultiFab * > lsm_ptr
Per-level land-model field pointers.
Definition: ERF_SurfaceModel.H:1130
Definition: ERF_SurfaceModel.H:848
const amrex::MultiFab * crse
Definition: ERF_SurfaceModel.H:850
amrex::MultiFab * fine
Definition: ERF_SurfaceModel.H:849
int ncomp
Definition: ERF_SurfaceModel.H:851
Definition: ERF_SurfaceModel.H:1156
std::pair< int, int > map
Definition: ERF_SurfaceModel.H:1157
amrex::Vector< std::unique_ptr< amrex::MultiFab > > weighted
Definition: ERF_SurfaceModel.H:1158