ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceLayer.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACELAYER_H
2 #define ERF_SURFACELAYER_H
3 
4 #include <cmath>
5 #include <fstream>
6 #include <sstream>
7 #include <string>
8 
9 #include "AMReX_Geometry.H"
10 #include "AMReX_ParmParse.H"
11 #include "AMReX_FArrayBox.H"
12 #include "AMReX_MultiFab.H"
13 #include "AMReX_iMultiFab.H"
14 #include "AMReX_MFInterpolater.H"
15 #include "AMReX_GpuMemory.H"
16 
17 #include "ERF_IndexDefines.H"
18 #include "ERF_Constants.H"
19 #include "ERF_MOSTAverage.H"
20 #include "ERF_PlanarBoundary.H"
21 #include "ERF_MOSTStress.H"
22 #include "ERF_MOSTUtils.H"
23 #include "ERF_EBMOSTStress.H"
25 #include "ERF_TerrainMetrics.H"
26 #include "ERF_PBLHeight.H"
27 #include "ERF_SurfaceModel.H"
28 #include "ERF_SurfaceTemperature.H"
29 #include "ERF_MicrophysicsUtils.H"
30 #include "ERF_EB.H"
31 
32 namespace erf_surface_layer {
33 
34 inline bool
35 planar_sources_supported_for_terrain (TerrainType terrain_type,
36  bool use_sst,
37  bool use_tsk,
38  bool use_coupled_sst,
39  bool has_lsm_tsurf,
40  bool has_lsm_fluxes)
41 {
42  if (terrain_type != TerrainType::EB) { return true; }
43 
44  // EB supports this configuration only when all planar providers are absent.
45  return !use_sst && !use_tsk && !use_coupled_sst &&
46  !has_lsm_tsurf && !has_lsm_fluxes;
47 }
48 
49 } // namespace erf_surface_layer
50 
51 
52 /** Abstraction layer for different surface layer schemes (e.g. MOST, Cd)
53  *
54  * van der Laan, P., Kelly, M. C., & Sørensen, N. N. (2017). A new k-epsilon
55  * model consistent with Monin-Obukhov similarity theory. Wind Energy,
56  * 20(3), 479–amrex::Real(489.) https://doi.org/amrex::Real(10.1002)/we.2017
57  *
58  * Consistent with Dyer (1974) formulation from page 57, Chapter 2, Modeling
59  * the vertical ABL structure in Modeling of Atmospheric Flow Fields,
60  * Demetri P Lalas and Corrado F Ratto, January 1996,
61  * https://doi.org/amrex::Real(10.1142)/amrex::Real(2975.)
62  */
64 {
65 
66 public:
67  /**
68  * Construct the surface-layer interface.
69  *
70  * @param[in] face orientation of face (for wall geometries)
71  * @param[in] geom geometry for all AMR levels
72  * @param[in,out] use_rot_surface_flux whether to use rotated surface fluxes
73  * @param[in] a_pp_prefix ParmParse prefix used by MOST averages
74  * @param[in] Qv_prim primitive water-vapor fields by level
75  * @param[in] z_phys_nd nodal physical-height fields by level
76  * @param[in] zlevels_stag nominal staggered z levels by level
77  * @param[in] a_mesh_type mesh type
78  * @param[in] a_terrain_type terrain representation
79  * @param[in] a_turb_choice turbulence-model options
80  * @param[in] a_rdOcp configured Rd/cp exponent for T-theta conversions
81  * @param[in] start_low_time first available low-boundary-data time
82  * @param[in] final_low_time final available low-boundary-data time
83  * @param[in] low_time_interval low-boundary-data time interval
84  * @param[in] eb_vec optional embedded-boundary geometry data
85  * @param[in] surf_model optional weighted surface-model interface
86  * @param[in] a_has_immersed_buildings whether building immersed forcing is configured
87  */
88  explicit SurfaceLayer (amrex::Orientation face,
89  const amrex::Vector<amrex::Geometry>& geom,
90  bool& use_rot_surface_flux,
91  std::string a_pp_prefix,
92  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qv_prim,
93  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& z_phys_nd,
94  const amrex::Vector<amrex::Vector<amrex::Real>>& zlevels_stag,
95  const MeshType& a_mesh_type,
96  const TerrainType& a_terrain_type,
97  const TurbChoice& a_turb_choice,
98  amrex::Real a_rdOcp,
99  double start_low_time,
100  double final_low_time,
101  double low_time_interval = 0.0,
102  const amrex::Vector<const eb_*>& eb_vec = {},
103  SurfaceModel* surf_model = nullptr,
104  bool a_has_immersed_buildings = false)
105  : m_face(face),
106  m_pp_prefix(a_pp_prefix),
107  m_geom(geom),
108  m_rotate(use_rot_surface_flux),
109  m_start_low_time(start_low_time),
110  m_final_low_time(final_low_time),
111  m_low_time_interval(low_time_interval),
112  m_eb_vec(eb_vec),
113  m_terrain_type(a_terrain_type),
114  m_has_immersed_buildings(a_has_immersed_buildings),
115  m_rdOcp(a_rdOcp),
116  m_ma(face, geom, (z_phys_nd[0] != nullptr), a_pp_prefix, a_mesh_type, a_terrain_type,
117  zlevels_stag, eb_vec),
118  m_surf_model(surf_model)
119  {
120  // We have a moisture model if Qv_prim is a valid pointer
121  use_moisture = (Qv_prim[0].get());
122 
123  // Keep standalone SurfaceLayer instances compatible with the historical
124  // single-face behavior. ERF replaces this with the complete active-face
125  // set after constructing all SurfaceLayer objects.
126  m_surface_layer_faces[static_cast<int>(face)] = 1;
127 
128  // Get roughness
129  amrex::ParmParse pp(a_pp_prefix);
130  m_z0_given = pp.contains("most.z0");
131  pp.queryAdd("most.z0", z0_const);
132 
133  // Specify how to compute the flux
134  if (use_rot_surface_flux) {
136  } else {
137  std::string flux_string_in;
138  std::string flux_string{"moeng"};
139  auto read_flux = pp.queryAdd("surface_layer.flux_type", flux_string_in);
140  if (read_flux) {
141  flux_string = amrex::toLower(flux_string_in);
142  }
143  if (flux_string == "moeng") {
145  } else if (flux_string == "rico") {
147  } else if (flux_string == "bulk_coeff") {
149  } else if (flux_string == "custom") {
151  } else {
152  amrex::Abort("Undefined MOST flux type!");
153  }
154  }
155 
160  m_face.coordDir() == 2 && m_face.isLow(),
161  "BULK_COEFF, CUSTOM, and RICO surface-layer fluxes are supported only on the z-low face.");
162  }
163 
164  // Include w* to handle free convection (Beljaars 1995, QJRMS)
165  pp.queryAdd("most.include_wstar", m_include_wstar);
166 
167  std::string pblh_string_in;
168  std::string pblh_string{"none"};
169  auto read_pblh = pp.queryAdd("most.pblh_calc", pblh_string_in);
170  if (read_pblh) {
171  pblh_string = amrex::toLower(pblh_string_in);
172  }
173  if (pblh_string == "none") {
175  } else if (pblh_string == "mynn25") {
177  } else if (pblh_string == "mynnedmf") {
179  } else if (pblh_string == "ysu") {
181  } else if (pblh_string == "mrf") {
183  } else if (pblh_string == "ysunew") {
184  // The former YSUNew is now the only YSU; the name is still accepted
185  static bool warned_ysunew = false;
186  if (!warned_ysunew && amrex::ParallelDescriptor::IOProcessor()) {
187  amrex::Warning(m_pp_prefix + ".most.pblh_calc = YSUNew is deprecated; it is now YSU");
188  }
189  warned_ysunew = true;
191  } else {
192  amrex::Abort("Undefined PBLH calc type!");
193  }
194 
197  m_face.coordDir() == 2 && m_face.isLow(),
198  "MOST PBL-height calculation and wstar correction are supported only on the z-low face.");
199  }
200 
201  // The w* correction is computed from the PBL height, so it needs a scheme that
202  // actually diagnoses one. With pblh_calc = "none" the pblh MultiFab keeps the
203  // bogus_large_value it was initialized with and calc_wstar turns that into a
204  // convective velocity scale of ~1e50, which destroys the surface fluxes.
206  amrex::Abort("erf.most.include_wstar requires a PBL height: set "
207  "erf.most.pblh_calc to MYNN25 (or MYNNEDMF), or to YSU or MRF "
208  "with the same erf.pbl_type");
209  }
210 
211  if (m_surf_model) {
212  use_surface_model = true;
213  amrex::Print() << " Using fluxes from surface model!" << std::endl;
215  }
216 
217  // Get surface temperature, as a potential temperature (most.pot_surf_temp) or an
218  // absolute temperature (most.abs_surf_temp); most.surf_temp is the deprecated
219  // spelling of most.pot_surf_temp. These are read with query, not queryAdd, so
220  // that whether a key was given is the query return value: queryAdd would add a
221  // key that is absent, and these keys are also read by ERF_InputSoundingData.H and
222  // by the SurfaceLayer of every face, so the first reader would make the key look
223  // given to the rest. A key that is given must be positive; it is never ignored.
224  {
225  amrex::Real pot_st = amrex::Real(-1.);
226  amrex::Real abs_st = amrex::Real(-1.);
227  amrex::Real old_st = amrex::Real(-1.);
228  const bool has_pot = pp.query("most.pot_surf_temp", pot_st);
229  const bool has_abs = pp.query("most.abs_surf_temp", abs_st);
230  const bool has_old = pp.query("most.surf_temp", old_st);
231  if (int(has_pot) + int(has_abs) + int(has_old) > 1) {
232  amrex::Abort("Specify only one of most.pot_surf_temp, most.abs_surf_temp "
233  "and the deprecated most.surf_temp");
234  }
235  if ((has_pot && !(pot_st > amrex::Real(0))) ||
236  (has_abs && !(abs_st > amrex::Real(0))) ||
237  (has_old && !(old_st > amrex::Real(0)))) {
238  amrex::Abort("most.pot_surf_temp, most.abs_surf_temp and most.surf_temp "
239  "must be positive temperatures [K]");
240  }
241  if (has_old) {
242  amrex::Print() << "WARNING: most.surf_temp is deprecated and is read as a surface "
243  << "potential temperature; use most.pot_surf_temp (potential) or "
244  << "most.abs_surf_temp (absolute) instead" << std::endl;
245  pot_st = old_st;
246  }
247  if (has_abs) {
248  surf_temp = abs_st;
249  surf_temp_is_absolute = true;
250  } else {
251  surf_temp = pot_st;
252  }
253  }
254  const bool erf_st = (surf_temp > amrex::Real(0));
255  // With an absolute temperature this is only the placeholder until update_fluxes
256  // converts it with the surface pressure.
257  if (erf_st) { default_land_surf_temp = surf_temp; }
258 
259  if (surf_temp_is_absolute) {
261  m_face.coordDir() == 2 && m_face.isLow() && m_terrain_type != TerrainType::EB,
262  "most.abs_surf_temp is supported only on the z-low face without EB terrain");
265  "most.abs_surf_temp is not supported with custom or rico fluxes; "
266  "use most.pot_surf_temp");
267  }
268 
269  // Get surface moisture
270  bool erf_sq = false;
271  if (use_moisture) {
272  pp.queryAdd("most.surf_moist", surf_moist);
273  erf_sq = (surf_moist >= amrex::Real(0));
274  }
275  if (erf_sq) { default_land_surf_moist = surf_moist; }
276 
277  // Custom type user must specify the fluxes
282  pp.get("most.ustar", custom_ustar);
283  pp.get("most.tstar", custom_tstar);
284  pp.get("most.qstar", custom_qstar);
285  pp.queryAdd("most.rhosurf", custom_rhosurf);
286  if (custom_qstar != 0) {
288  "Specified custom MOST qv flux without moisture model!");
289  }
290  amrex::Print() << "Using specified ustar, tstar, qstar for MOST = "
291  << custom_ustar << " " << custom_tstar << " "
292  << custom_qstar << std::endl;
293 
294  // Bulk transfer coefficient (must specify coeffs and surface values)
295  } else if (flux_type == FluxCalcType::BULK_COEFF) {
296  // Optional imposed kinematic heat flux <theta'w'> [K m/s], positive upward (the
297  // sign of most.surf_temp_flux for the other flux types). It replaces Ch * |U| *
298  // (theta_surf - theta): a constant drag with a fixed heat flux, as WRF's
299  // isfflx = 0 with tke_drag_coefficient and tke_heat_flux.
300  m_bulk_use_fixed_tflux = (pp.query("most.surf_temp_flux", m_bulk_fixed_tflux) != 0);
301  pp.get("most.Cd", m_Cd);
303  if (!std::isfinite(m_bulk_fixed_tflux)) {
304  amrex::Abort("erf.most.surf_temp_flux must be a finite number");
305  }
306  // the bulk_coeff branch never reads a surface heating rate: stop rather than
307  // run with it silently unused (the doc row of surf_temp_flux forbids the pair)
308  if (pp.contains("most.surf_heating_rate")) {
309  amrex::Abort("erf.most.surf_temp_flux with surface_layer.flux_type = bulk_coeff "
310  "imposes the surface heat flux; it may not be combined with "
311  "erf.most.surf_heating_rate");
312  }
313  // Ch is optional here: the heat flux does not use it
314  if (pp.query("most.Ch", m_Ch)) {
315  amrex::Print() << "WARNING: erf.most.Ch is not used: erf.most.surf_temp_flux "
316  << "imposes the bulk_coeff heat flux" << std::endl;
317  }
318  } else {
319  pp.get("most.Ch", m_Ch);
320  }
321  pp.get("most.Cq", m_Cq);
323  "bulk_coeff fluxes need most.pot_surf_temp or most.abs_surf_temp");
325  pp.get("most.surf_moist", default_land_surf_moist);
327  amrex::Print() << "Using specified Cd, Cq for MOST = " << m_Cd << " " << m_Cq
328  << " with an imposed surface heat flux most.surf_temp_flux = "
329  << m_bulk_fixed_tflux << " K m/s" << std::endl;
330  } else {
331  amrex::Print() << "Using specified Cd, Ch, Cq for MOST = "
332  << m_Cd << " " << m_Ch << " "
333  << m_Cq << std::endl;
334  }
335 
336  // Specify surface temperature/moisture or surface flux
337  } else {
338  if (erf_st) {
340  pp.queryAdd("most.surf_heating_rate", surf_heating_rate); // [K/h]
341 
342  // Modify rate to be in units of K / s rather than K / hr
343  surf_heating_rate /= amrex::Real(3600.0); // [K/s]
344 
345  if (pp.query("most.surf_temp_flux", surf_temp_flux)) {
346  amrex::Abort("Can only specify one of surf_temp_flux or surf_heating_rate");
347  }
348  } else {
349  pp.queryAdd("most.surf_temp_flux", surf_temp_flux);
350 
351  if (pp.query("most.surf_heating_rate", surf_heating_rate)) {
352  amrex::Abort("Can only specify one of surf_temp_flux or surf_heating_rate");
353  }
354  if (std::abs(surf_temp_flux) >
357  } else {
359  }
360  }
361 
362  if (erf_sq) {
364  } else {
365  pp.queryAdd("most.surf_moist_flux", surf_moist_flux);
366  if (std::abs(surf_moist_flux) >
369  } else {
371  }
372  }
373  }
374 
379  m_face.coordDir() == 2 && m_face.isLow(),
380  "HEAT_FLUX and ADIABATIC surface-layer fluxes are supported only on the z-low face.");
381  }
382 
384  {
385  pp.queryAdd("most.rico.theta_z0", rico_theta_z0);
386  pp.queryAdd("most.rico.qsat_z0", rico_qsat_z0);
387  }
388 
389  // Make sure the inputs file doesn't try to use most.roughness_type
390  std::string bogus_input;
391  if (pp.queryAdd("most.roughness_type", bogus_input) > 0) {
392  amrex::Abort("most.roughness_type is deprecated; use "
393  "most.roughness_type_land and/or most.roughness_type_sea");
394  }
395 
396  // Specify how to compute the surface flux over land (if there is any)
397  std::string rough_land_string_in;
398  std::string rough_land_string{"constant"};
399  auto read_rough_land =
400  pp.queryAdd("most.roughness_type_land", rough_land_string_in);
401  if (read_rough_land) {
402  rough_land_string = amrex::toLower(rough_land_string_in);
403  }
404  if (rough_land_string == "constant") {
406  } else {
407  amrex::Abort("Undefined MOST roughness type for land!");
408  }
409 
410  // Allow for smooth-flow limit?
411  pp.queryAdd("most.smooth_flow_viscosity", smooth_flow_visc);
412  amrex::Print() << "The smooth-flow limit will be included for variable roughness models over sea: " << smooth_flow_visc << "\n";
413 
414  // Specify how to compute the surface flux over sea (if there is any)
415  std::string rough_sea_string_in;
416  std::string rough_sea_string{"charnock"};
417  auto read_rough_sea = pp.queryAdd("most.roughness_type_sea", rough_sea_string_in);
418  if (read_rough_sea) {
419  rough_sea_string = amrex::toLower(rough_sea_string_in);
420  }
421  if (rough_sea_string == "charnock") {
423  pp.queryAdd("most.charnock_constant", cnk_a);
424  if (cnk_a > 0) {
425  amrex::Print() << "If there is water, Charnock relation with C_a="
426  << cnk_a << " will be used" << std::endl;
427  } else {
428  amrex::Print() << "If there is water, Charnock relation with variable "
429  "Charnock parameter (COARE3.0) will be used" << std::endl;
430  }
431  } else if (rough_sea_string == "coare3.0") {
433  amrex::Print() << "If there is water, Charnock relation with variable "
434  "Charnock parameter (COARE3.0) will be used" << std::endl;
435  cnk_a = -1;
436  } else if (rough_sea_string == "donelan") {
438  } else if (rough_sea_string == "modified_charnock") {
440  pp.queryAdd("most.modified_charnock_depth", depth);
441  if (depth < amrex::Real(10.0) || depth > amrex::Real(100.0) ) {
442  amrex::Print() << "Specified depth of " << depth
443  << " is outside the valid range of [10 100], resetting to bounds now."
444  << std::endl;
445  }
446  // Limiter based upon the fit range in Jiménez & Dudhia
447  depth = amrex::min(amrex::max(depth,amrex::Real(10.0)),amrex::Real(100.0));
448  } else if (rough_sea_string == "wave_coupled") {
450  } else if (rough_sea_string == "constant") {
452  } else {
453  amrex::Abort("Undefined MOST roughness type for sea!");
454  }
455 
456  // use skin temperature instead of sea-surface temperature
457  // (wrfinput data may have lower resolution SST data)
458  pp.queryAdd("most.ignore_sst", m_ignore_sst);
459 
460  // A uniform current of the water surface [m/s, along grid x and y]. The surface
461  // layer then takes the wind relative to the surface, as over an ocean current.
462  if (m_face.coordDir() == 2 && m_face.isLow()) {
463  amrex::Vector<amrex::Real> current;
464  if (pp.queryarr("most.surface_current", current)) {
465  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(current.size() == 2,
466  "most.surface_current takes two components, along x and y.");
468  "most.surface_current is not supported with EB terrain.");
469  m_use_surface_current = true;
470  m_surface_current_const[0] = current[0];
471  m_surface_current_const[1] = current[1];
472  }
473  }
474 
475  // If we're using the RANS k model, then we need to update the dirichlet
476  // wall value of k (written into the first cell above the wall and held
477  // there through the step) based on the instantaneous u* and θ*; the turbulence modeling
478  // choices can vary per level but for now, assume that if specified then
479  // all levels are using the same RANS model.
480  m_update_k_rans = (a_turb_choice.rans_type == RANSType::kEqn &&
481  a_turb_choice.dirichlet_k == true);
482  if (m_update_k_rans) {
484  m_face.coordDir() == 2 && m_face.isLow(),
485  "RANS surface-layer k updates are supported only on the z-low face.");
486  }
487  if (m_update_k_rans) {
488  inv_Cmu2 = one / (a_turb_choice.Cmu0 * a_turb_choice.Cmu0);
489  theta_ref = a_turb_choice.theta_ref;
490  // with the moist TKE buoyancy (erf.rans_moist_buoyancy) the wall value's buoyancy flux
491  // is the virtual one, -u* theta_v* with theta_v* = theta* (1 + epsv qv) + epsv theta q*
493  }
494 
495  // Stable-side similarity functions (most.stable_functions): Businger-Dyer, or QNSE for
496  // the prescribed-flux kernels and their companions; see check_similarity_funs_use
499 
500  } // constructor
501 
502  /**
503  * Stop when most.stable_functions = qnse would be read by nothing on this face.
504  *
505  * The choice selects the stable arm of the Businger-Dyer pair, which the prescribed-flux
506  * (moeng) kernels use, with their companions: the PBL surface gradients, the 2 m and 10 m
507  * diagnostics and the MYNN surface TKE production. The prescribed-temperature kernels use the Jimenez
508  * (2012) pair on both sides, the adiabatic kernels no stability function, and the custom,
509  * rico and bulk_coeff fluxes none at all. Called at construction and again where a surface
510  * file, SST/TSK data or a land model switch the flux or temperature mode.
511  */
513  {
514  if (!m_sfuns.uses_qnse()) { return; }
515  const std::string key = m_pp_prefix + ".most.stable_functions = qnse";
517  amrex::Abort(key + " needs the moeng flux type: the custom, rico and bulk_coeff fluxes "
518  "use no similarity functions");
519  }
521  amrex::Abort(key + " needs a prescribed heat flux (most.surf_temp_flux): the "
522  "prescribed-temperature kernels use the Jimenez (2012) functions on both "
523  "sides and the adiabatic kernels no stability function");
524  }
525  }
526 
527  /**
528  * Allocate and initialize surface-layer data for one AMR level.
529  *
530  * @param[in] lev level index
531  * @param[in] nlevs number of AMR levels
532  * @param[in] mfv conserved and velocity MultiFabs for this level
533  * @param[in] Theta_prim primitive potential-temperature field
534  * @param[in] Qv_prim primitive water-vapor field
535  * @param[in] Qr_prim primitive rain-water field
536  * @param[in] z_phys_nd nodal physical-height field
537  * @param[in] Hwave wave-height field
538  * @param[in] Lwave wavelength field
539  * @param[in] eddyDiffs eddy-diffusivity field
540  * @param[in] lsm_data land-surface-model data fields
541  * @param[in] lsm_data_name names for lsm_data entries
542  * @param[in] lsm_flux land-surface-model flux fields
543  * @param[in] lsm_flux_name names for lsm_flux entries
544  * @param[in] sst_lev sea-surface-temperature data by time
545  * @param[in] tsk_lev skin-temperature data by time
546  * @param[in] lmask_lev land-mask data by time
547  * @param[in] mapfac map-factor fields at the level (MapFacType); none means m = 1
548  */
549  void make_SurfaceLayer_at_level (const int& lev,
550  int nlevs,
551  const amrex::Vector<amrex::MultiFab*>& mfv,
552  std::unique_ptr<amrex::MultiFab>& Theta_prim,
553  std::unique_ptr<amrex::MultiFab>& Qv_prim,
554  std::unique_ptr<amrex::MultiFab>& Qr_prim,
555  std::unique_ptr<amrex::MultiFab>& z_phys_nd,
556  amrex::MultiFab* Hwave,
557  amrex::MultiFab* Lwave,
558  amrex::MultiFab* eddyDiffs,
559  amrex::Vector<amrex::MultiFab*> lsm_data,
560  amrex::Vector<std::string> lsm_data_name,
561  amrex::Vector<amrex::MultiFab*> lsm_flux,
562  amrex::Vector<std::string> lsm_flux_name,
563  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& sst_lev,
564  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& tsk_lev,
565  amrex::Vector<std::unique_ptr<amrex::iMultiFab>>& lmask_lev,
566  const amrex::Vector<std::unique_ptr<amrex::MultiFab>>* mapfac = nullptr)
567  {
568  // Update MOST Average
570  Theta_prim, Qv_prim, Qr_prim,
571  z_phys_nd, mapfac);
572 
573  // Get CC vars
574  amrex::MultiFab& mf = *(mfv[0]);
575 
576  amrex::ParmParse pp(m_pp_prefix);
577 
578  // Do we have a time-varying surface roughness that needs to be saved?
579  if (lev == 0) {
580  const int nghost = 0; // ghost cells not included
581  int lmask_min = lmask_min_reduce(*lmask_lev[0].get(), nghost);
582  amrex::ParallelDescriptor::ReduceIntMin(lmask_min);
583 
584  m_var_z0 = (lmask_min < 1) & (rough_type_sea != RoughCalcType::CONSTANT);
585  if (m_var_z0) {
587  m_face.coordDir() == 2 && m_face.isLow(),
588  "Variable sea roughness surface-layer fluxes are supported only on the z-low face.");
589 
590  std::string rough_sea_string{"charnock"};
591  pp.queryAdd("most.roughness_type_sea", rough_sea_string);
592  amrex::Print() << "Variable sea roughness (type " << rough_sea_string
593  << ")" << std::endl;
594  }
595 
596  // The surface current moves only the water, so without water it would do nothing
598  "The surface current (most.surface_current or erf.use_coupled_currents) moves "
599  "only water, and the surface has none: set erf.is_land = 0 or supply a land "
600  "mask with water.");
601  }
602 
603  if (m_eddyDiffs_lev.size() < lev+1) {
604  m_Hwave_lev.resize(nlevs);
605  m_Lwave_lev.resize(nlevs);
606  m_eddyDiffs_lev.resize(nlevs);
607 
608  m_lsm_data_lev.resize(nlevs);
609  m_lsm_flux_lev.resize(nlevs);
610 
611  m_sst_lev.resize(nlevs);
612  m_tsk_lev.resize(nlevs);
613  m_lmask_lev.resize(nlevs);
614 
615  m_coupled_sst_lev.resize(nlevs, nullptr);
616  m_coupled_sst_valid_lev.resize(nlevs, nullptr);
617 
618  m_surface_current_x.resize(nlevs);
619  m_surface_current_y.resize(nlevs);
620  m_rel_xvel.resize(nlevs);
621  m_rel_yvel.resize(nlevs);
622  m_coupled_current_east_lev.resize(nlevs, nullptr);
623  m_coupled_current_north_lev.resize(nlevs, nullptr);
624  m_coupled_current_covered_lev.resize(nlevs, nullptr);
625  m_coupled_current_cos_lev.resize(nlevs, nullptr);
626  m_coupled_current_sin_lev.resize(nlevs, nullptr);
627  m_coupled_rotation_lev.resize(nlevs);
628  m_coupled_rotation_src_lev.resize(nlevs, nullptr);
629  m_coupled_rotation_src_ba_lev.resize(nlevs);
630  m_coupled_rotation_src_dm_lev.resize(nlevs);
631  m_rel_k_top.resize(nlevs, 0);
632  m_rel_src_ba.resize(nlevs);
633 
634  // Size the MOST params for all levels
635  z_0.resize(nlevs);
636  u_star.resize(nlevs);
637  w_star.resize(nlevs);
638  t_star.resize(nlevs);
639  q_star.resize(nlevs);
640  t_surf.resize(nlevs);
641  q_surf.resize(nlevs);
642  surface_diagnostic_source.resize(nlevs);
643  olen.resize(nlevs);
644  pblh.resize(nlevs);
645  m_planar_bndry.resize(nlevs);
646  m_lsm_held_columns.resize(nlevs);
647  }
648 
649  // Get pointers to SST,TSK and LANDMASK data
650  int nt_tot_sst = sst_lev.size();
651  m_sst_lev[lev].resize(nt_tot_sst);
652  for (int nt(0); nt < nt_tot_sst; ++nt) {
653  m_sst_lev[lev][nt] = sst_lev[nt].get();
654  }
655  int nt_tot_tsk = static_cast<int>(tsk_lev.size());
656  m_tsk_lev[lev].resize(nt_tot_tsk);
657  for (int nt(0); nt < nt_tot_tsk; ++nt) {
658  m_tsk_lev[lev][nt] = tsk_lev[nt].get();
659  }
660  int nt_tot_lmask = static_cast<int>(lmask_lev.size());
661  m_lmask_lev[lev].resize(nt_tot_lmask);
662  for (int nt(0); nt < nt_tot_lmask; ++nt) {
663  m_lmask_lev[lev][nt] = lmask_lev[nt].get();
664  }
665 
666  // The surface current over this level's water, and room for the velocities relative to it
667  if (m_use_surface_current) {
668  define_surface_current(lev, *mfv[Vars::xvel], *mfv[Vars::yvel]);
669  }
670 
671  // Get pointers to wave data
672  m_Hwave_lev[lev] = Hwave;
673  m_Lwave_lev[lev] = Lwave;
674  m_eddyDiffs_lev[lev] = eddyDiffs;
675 
676  // Text-file driven surface forcing modes. The file always contains at
677  // least time(day) and absolute sst(K); the value is normalized to the
678  // canonical SurfaceLayer theta field at the z-low surface.
679  pp.queryAdd("most.use_sfc_fluxes", m_use_sfc_fluxes);
680  pp.queryAdd("most.use_sfc_sst", m_use_sfc_sst);
682  amrex::Abort("Only one of most.use_sfc_fluxes and most.use_sfc_sst may be enabled");
683  }
685  amrex::Abort("erf.most.surf_temp_flux with surface_layer.flux_type = bulk_coeff "
686  "imposes the surface heat flux; it cannot be combined with "
687  "most.use_sfc_fluxes or most.use_sfc_sst");
688  }
690  if (m_use_sfc_fluxes) {
691  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!use_surface_model, "Cannot use surface flux forcing with LSM/Urban model!");
692  // The file's H and LE already carry the density, and the stress
693  // takes the local density, so a surface density would count it
694  // twice in H and LE (see set_sfc_params_from_file_fluxes).
695  // The value is tested, not the key: flux_type = custom has already
696  // queryAdd-ed most.rhosurf with its default of 0 (use the first cell).
697  amrex::Real rhosurf_in = zero;
698  pp.query("most.rhosurf", rhosurf_in);
700  "most.rhosurf cannot be combined with most.use_sfc_fluxes");
701  }
702  if (m_terrain_type == TerrainType::EB) {
703  amrex::Abort("Text-file surface forcing is not supported with EB terrain");
704  }
706  m_face.coordDir() == 2 && m_face.isLow(),
707  "most.use_sfc_fluxes and most.use_sfc_sst are supported only on the z-low face.");
708 
709  // load sensible and latent heat fluxes from sfc to prescribe
710  std::string sfc_file = "";
711  pp.queryAdd("most.sfc_file", sfc_file);
712  if (sfc_file.empty()) {
713  amrex::Abort("most.sfc_file must be set when using text-file surface forcing");
714  }
715 
716  // sfc contains: time(day) sst(K) H(W/m2) LE(W/m2) TAU(m2/s2)
717  sfc = read_cols(sfc_file, 1);
718 
719  const int min_cols = m_use_sfc_fluxes ? 5 : 2;
720  if (static_cast<int>(sfc.size()) < min_cols) {
721  amrex::Abort("Surface forcing file does not contain the required number of columns");
722  }
723 
724  // shift time column in days to be relative to current elapsed time
725  const amrex::Real start_day = sfc[0][0];
726  for (int i = 0; i < static_cast<int>(sfc[0].size()); ++i) {
727  sfc[0][i] = 86400.0 * (sfc[0][i] - start_day);
728  }
729 
730  if (m_use_sfc_sst) {
732  amrex::Abort("most.use_sfc_sst cannot be combined with prescribed heat flux or surf_heating_rate");
733  }
735  amrex::Abort("most.use_sfc_sst cannot be combined with prescribed moisture flux");
736  }
738  amrex::Print() << "Using MOST with prescribed SST from most.sfc_file '" << sfc_file << "' over sea" << std::endl;
739  }
740 
741  if (m_use_sfc_fluxes) {
743  amrex::Print() << "Using MOST with prescribed time-varying surface fluxes from '" << sfc_file << "'" << std::endl;
744  }
745  }
747 
748  // The constructor's check of most.abs_surf_temp sees only most.flux_type; the
749  // modes known here replace the prescribed temperature too. The surface-flux file
750  // makes the fluxes custom, and the SST/TSK and sfc-file SST fills use the
751  // prescribed value as their land temperature, which is a potential temperature there.
752  if (surf_temp_is_absolute) {
754  "most.abs_surf_temp cannot be combined with most.use_sfc_fluxes");
756  (m_sst_lev[lev].empty() || !m_sst_lev[lev][0]) && !m_use_sfc_sst,
757  "most.abs_surf_temp cannot be combined with SST/TSK or sfc-file SST input");
758  }
759 
760  // By default an LSM field named "tsurf", "theta" or "t_surf" is copied into
761  // t_surf unchanged. With this option the LSM skin temperature ("tsurf" from
762  // SLM, "t_sfc" from Noah-MP) is taken as an absolute temperature and
763  // converted to potential temperature at the surface pressure.
764  pp.queryAdd("most.use_lsm_skin_temperature", m_use_lsm_skin_temperature);
765 
766  // Get pointers to LSM data and Fluxes
767  int ndata = static_cast<int>(lsm_data.size());
768  int nflux = static_cast<int>(lsm_flux.size());
769  m_lsm_data_name.resize(ndata);
770  m_lsm_data_lev[lev].resize(ndata);
771  m_lsm_flux_name.resize(nflux);
772  m_lsm_flux_lev[lev].resize(nflux);
773  for (int n(0); n < ndata; ++n) {
774  m_lsm_data_name[n] = lsm_data_name[n];
775  m_lsm_data_lev[lev][n] = lsm_data[n];
776  const std::string lc_name = amrex::toLower(lsm_data_name[n]);
777  // With most.use_lsm_skin_temperature, "tsurf" (SLM) and "t_sfc" (Noah-MP)
778  // are absolute skin temperatures and are converted to potential
779  // temperature in get_lsm_tsurf; "theta" and "t_surf" are taken to be
780  // potential temperature already.
781  if (lc_name == "tsurf" || (m_use_lsm_skin_temperature && lc_name == "t_sfc") ||
782  lc_name == "theta" || lc_name == "t_surf") {
783  m_has_lsm_tsurf = true;
784  m_lsm_tsurf_indx = n;
785  m_lsm_tsurf_is_theta = (lc_name == "theta" || lc_name == "t_surf");
786  }
787  }
789  amrex::Abort("most.use_sfc_sst cannot be combined with an ocean LSM t_surf input");
790  }
791  int n_valid_lsm_flux = 0;
792  bool has_soil_t_flux = false;
793  for (int n(0); n < nflux; ++n) {
794  m_lsm_flux_name[n] = lsm_flux_name[n];
795  m_lsm_flux_lev[lev][n] = lsm_flux[n];
796  if (m_lsm_flux_lev[lev][n]) { ++n_valid_lsm_flux; }
797  if (amrex::toLower(m_lsm_flux_name[n]) == "soil_t_flux") {
798  has_soil_t_flux = true;
799  }
800  }
801  AMREX_ALWAYS_ASSERT((n_valid_lsm_flux==0 || n_valid_lsm_flux>=4 ||
802  (n_valid_lsm_flux==1 && has_soil_t_flux)));
803  if (n_valid_lsm_flux>=4) { m_has_lsm_fluxes = true; }
804 
805  const bool use_sst = (!m_sst_lev[lev].empty() && m_sst_lev[lev][0]);
806  const bool use_tsk = (!m_tsk_lev[lev].empty() && m_tsk_lev[lev][0]);
807 
808  // An imposed bulk_coeff heat flux and a surface temperature or flux supplied from
809  // elsewhere contradict each other: one of the two would be dropped without a word.
811  (use_sst || use_tsk || m_has_lsm_tsurf || m_has_lsm_fluxes ||
813  amrex::Abort("erf.most.surf_temp_flux with surface_layer.flux_type = bulk_coeff "
814  "imposes the surface heat flux; it cannot be combined with SST/TSK data, "
815  "a land-surface or surface model, or a coupled ocean");
816  }
817 
818  // Check if there is a user-specified roughness file to be read
819  std::string fname;
820  bool read_z0 = false;
821  if ( (flux_type == FluxCalcType::MOENG) ||
823  int count = pp.countval("most.roughness_file_name");
824  if (count > 1) {
825  AMREX_ALWAYS_ASSERT(count >= lev+1);
826  pp.query("most.roughness_file_name", fname, lev);
827  read_z0 = true;
828  } else if (count == 1) {
829  if (lev == 0) {
830  pp.queryAdd("most.roughness_file_name", fname);
831  } else {
832  // we will interpolate from the coarsest level
833  fname = "";
834  }
835  read_z0 = true;
836  }
837  // else use z0_const
838  }
839  if (read_z0) {
841  m_face.coordDir() == 2 && m_face.isLow(),
842  "Custom MOST roughness is supported only on the z-low face.");
843  }
844 
845  // LSM flux arrays are planar and compute_sfc_params_from_lsm_fluxes only
846  // writes the k=0 slab, while EB MOST consumes surface parameters at
847  // arbitrary cut-cell k; no valid planar-to-cut-cell mapping exists yet.
848  // Custom roughness is a horizontal material property, handled separately.
850  m_terrain_type, use_sst, use_tsk, m_use_coupled_sst,
852  amrex::Abort(
853  "EB SurfaceLayer does not support planar SST/TSK, coupled SST, LSM surface "
854  "temperature or fluxes; no mapping exists "
855  "from those planar inputs to arbitrary EB cut cells.");
856  }
857 
858  // Attributes for MFs and FABs
859  //--------------------------------------------------------
860  // Create a 2D ba for planar terrain, 3D for EB terrain
861  const int dir = m_face.coordDir();
862  int sm_index;
863  if (m_face.isLow()) {
864  sm_index = m_geom[lev].Domain().smallEnd(dir);
865  } else {
866  sm_index = m_geom[lev].Domain().bigEnd(dir);
867  }
868 
869  amrex::BoxArray ba = mf.boxArray();
870  amrex::BoxArray ba_flux;
871  amrex::IntVect ng{1,1,0};
872 
873  // The lateral-wall implementation uses the two-dimensional land-mask
874  // layout to identify grids on the requested face. Lateral walls are
875  // intentionally restricted to grids spanning the complete z domain;
876  // z-decomposed and partial-height grids are not supported.
877  if (dir != 2) {
878  const int dom_lo_z = m_geom[lev].Domain().smallEnd(2);
879  const int dom_hi_z = m_geom[lev].Domain().bigEnd(2);
880  for (int ibox = 0; ibox < ba.size(); ++ibox) {
882  ba[ibox].smallEnd(2) == dom_lo_z && ba[ibox].bigEnd(2) == dom_hi_z,
883  "Surface layer boundaries on x/y faces require Cartesian grids that "
884  "span the full level z domain; partial-height refined grids and grids "
885  "decomposed in z are not supported. Set amr.max_grid_size_z accordingly.");
886  }
887  }
888 
889  if (m_terrain_type == TerrainType::EB) {
890  // Use full 3D BoxArray for EB terrain
891  ba_flux = ba;
892  ng = amrex::IntVect{1,1,1}; // Include z ghost cells
893  } else {
894  // Collapse to 2D for planar terrain
895  amrex::BoxList bl2d = ba.boxList();
896  for (auto& b : bl2d) {
897  b.setRange(dir,sm_index);
898  }
899  ba_flux = amrex::BoxArray(std::move(bl2d));
900  // Lateral faces need state-width ghosts in the tangential z
901  // direction. A z face only needs the original one-cell x/y halo;
902  // kernels using the wider state/mask boxes clip to the FAB bounds.
903  if (dir == 2) {
904  ng = amrex::IntVect{1,1,0};
905  } else {
906  ng = mf.nGrowVect();
907  ng[dir] = 0;
908  }
909  }
910 
911  const amrex::DistributionMapping& dm = mf.DistributionMap();
912  const int ncomp = 1;
913 
914  // Surface copies of the planar boxes (see fill_planar_boundary)
915  // PlanarBoundary handles duplicate boxes created by a z-split. The x/y
916  // layouts use selective lateral exchange instead.
917  if (m_terrain_type != TerrainType::EB && dir == 2) {
918  const int ksurface = m_face.isLow()
919  ? m_geom[lev].Domain().smallEnd(2)
920  : m_geom[lev].Domain().bigEnd(2);
921  m_planar_bndry[lev].define(ba, ba_flux, dm, ksurface, m_face.isLow());
922  }
923 
924  // Z0 heights FAB
925  //--------------------------------------------------------
926  z_0[lev].define(ba_flux, dm, ncomp, ng);
927  z_0[lev].setVal(z0_const);
928  if (read_z0) {
929  read_custom_roughness(lev, fname);
930  }
931 
932  // The surface layer normalizes with ln(zref / z0), so the reference height
933  // must lie above the roughness length in every column. With the default
934  // (the first cell center) a fine mesh over a rough surface can fail this.
935  {
936  const amrex::MultiFab* zref = m_ma.get_zref(lev);
937  if (zref && zref->boxArray() == z_0[lev].boxArray()) {
938  amrex::MultiFab margin(ba_flux, dm, ncomp, 0);
939  amrex::MultiFab::Copy(margin, *zref, 0, 0, ncomp, 0);
940  amrex::MultiFab::Subtract(margin, z_0[lev], 0, 0, ncomp, 0);
941  if (margin.min(0) <= zero) {
942  amrex::Abort(m_pp_prefix + ".most.zref (or the first cell center), "
943  + std::to_string(zref->min(0)) + " m at its lowest, must exceed "
944  + m_pp_prefix + ".most.z0, " + std::to_string(z_0[lev].max(0))
945  + " m at its largest, in every column");
946  }
947  }
948  }
949 
950  // 2D MFs for U*, T*, T_surf
951  //--------------------------------------------------------
952  u_star[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
953  u_star[lev]->setVal(bogus_large_value);
954 
955  w_star[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
956  w_star[lev]->setVal(bogus_large_value);
957 
958  t_star[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
959  t_star[lev]->setVal(zero); // default to neutral
960 
961  q_star[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
962  q_star[lev]->setVal(zero); // default to dry
963 
964  olen[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
965  olen[lev]->setVal(bogus_large_value);
966 
967  pblh[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
968  pblh[lev]->setVal(bogus_large_value);
969 
970  t_surf[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
971  t_surf[lev]->setVal(default_land_surf_temp);
972 
973  q_surf[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
974  q_surf[lev]->setVal(default_land_surf_moist);
975 
976  // Columns of a refined level that a grid touching the surface holds, one cell beyond
977  // each grid (see LsmFluxColumn). A grid that starts above the ground collapses onto
978  // the same columns but holds no surface fluxes there, so it does not count. Level 0
979  // has no coarse/fine boundary and needs no mask.
980  m_lsm_held_columns[lev].clear();
981  if (lev > 0 && dir == 2 && m_face.isLow() && m_terrain_type != TerrainType::EB) {
982  amrex::iMultiFab& held = m_lsm_held_columns[lev];
983  held.define(ba_flux, dm, 1, amrex::IntVect(1,1,0));
984  held.setVal(0);
985  for (amrex::MFIter mfi(held); mfi.isValid(); ++mfi) {
986  if (m_planar_bndry[lev].is_surface_copy(mfi.index())) {
987  held[mfi].setVal<amrex::RunOn::Device>(1, mfi.validbox());
988  }
989  }
990  held.FillBoundary(m_geom[lev].periodicity());
991  }
992 
993  const amrex::iMultiFab& surface_mask = *m_lmask_lev[lev][0];
995  surface_mask.boxArray().size() == mf.boxArray().size(),
996  "Surface-layer mask and state must have the same number of boxes.");
998  surface_mask.DistributionMap() == mf.DistributionMap(),
999  "Surface-layer mask and state must have identical ownership.");
1001  surface_mask.boxArray().size() == u_star[lev]->boxArray().size(),
1002  "Surface-layer mask and parameters must have the same number of boxes.");
1004  surface_mask.DistributionMap() == u_star[lev]->DistributionMap(),
1005  "Surface-layer mask and parameters must have identical ownership.");
1006 
1008 
1009  surface_diagnostic_source[lev] = std::make_unique<amrex::MultiFab>(ba_flux, dm, ncomp, ng);
1010  surface_diagnostic_source[lev]->setVal(
1012 
1013  // TODO: Do we want an enum struct for indexing?
1014 
1015  if (use_sst || use_tsk || m_has_lsm_tsurf || m_use_coupled_sst || use_surface_model) {
1016  // Valid SST, TSK, LSM or coupled-ocean data; t_surf set before computing
1017  // fluxes (avoids extended lambda capture) Note that land temp will be set
1018  // from m_tsk_lev while sea temp will be set from m_sst_lev
1021 
1022  // Pathways in fill_tsurf_with_sst_and_tsk
1023  amrex::Print() << "Using MOST with specified surface temperature ";
1024  if (m_has_lsm_tsurf && !use_sst && !use_tsk) {
1025  amrex::Print() << "(LSM: " << m_lsm_data_name[m_lsm_tsurf_indx] << ")";
1026  } else if (!use_sst && !use_tsk) {
1027  amrex::Print() << "(land: T0, sea: none)";
1028  } else {
1029  // NOTE: SST from the LOW file populates TSK in update_sst_tsk.
1030  // So if we have TSK, it contains everything and has been
1031  // sanity checked for valid SST values.
1033  if (use_tsk) {
1034  amrex::Print() << "(land: TSK, ";
1035  } else {
1036  amrex::Print() << "(land: T0, ";
1037  }
1038  if (use_tsk && !use_sst) {
1039  amrex::Print() << "sea: TSK)";
1040  } else {
1041  amrex::Print() << "sea: SST)";
1042  AMREX_ALWAYS_ASSERT(m_sst_lev[lev][0]);
1043  }
1044  }
1045  // The coupler is layered on top of whatever the above selected: it
1046  // overwrites only the water cells it actually covers, so the pathway
1047  // named above remains the value for land and for uncovered water.
1048  if (m_use_coupled_sst) {
1049  amrex::Print() << " + coupled ocean SST where covered";
1050  }
1051  amrex::Print() << std::endl;
1052  }
1053  }
1054 
1055  /**
1056  * Set benign surface scales in immersed columns (land mask 2), including ghosts.
1057  * Called after allocation, checkpoint restoration, and surface-scale updates.
1058  * Preserve PBL height, roughness, surface temperature, and surface moisture.
1059  *
1060  * @param[in] lev level index
1061  */
1062  void set_immersed_surface_params (const int& lev);
1063 
1064  /**
1065  * Update surface fluxes and related surface-layer state.
1066  *
1067  * @param[in] lev level index
1068  * @param[in] elapsed_time current elapsed simulation time
1069  * @param[in] elapsed_time_since_start_low elapsed time relative to low-data start
1070  * @param[in,out] cons_in conserved state used by the flux update
1071  * @param[in] z_phys_nd nodal physical-height field
1072  * @param[in] walldist wall-distance field
1073  * @param[in] max_iters maximum MOST iteration count
1074  */
1075  void
1076  update_fluxes (const int& lev,
1077  const double& elapsed_time,
1078  const double& elapsed_time_since_start_low,
1079  amrex::MultiFab& cons_in,
1080  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1081  const std::unique_ptr<amrex::MultiFab>& walldist,
1082  int max_iters = 100);
1083 
1084  /**
1085  * Compute MOST fluxes with a selected flux-iteration functor.
1086  *
1087  * @param[in] lev level index
1088  * @param[in] max_iters maximum MOST iteration count
1089  * @param[in,out] cons_in conserved state used by the flux computation
1090  * @param[in] most_flux flux-iteration functor
1091  * @param[in] is_land whether the land-surface branch is active
1092  */
1093  template <typename FluxIter>
1094  void compute_fluxes (const int& lev,
1095  const int& max_iters,
1096  amrex::MultiFab& cons_in,
1097  const FluxIter& most_flux,
1098  bool is_land);
1099 
1100  /**
1101  * Initialize TKE from the current surface friction velocity.
1102  *
1103  * @param[in] lev level index
1104  * @param[in,out] cons conserved state whose TKE component is initialized
1105  * @param[in] z_phys_nd nodal physical-height field
1106  * @param[in] tkefac scale factor applied to the initialized TKE
1107  * @param[in] zscale vertical decay scale
1108  */
1109  void init_tke_from_ustar (const int& lev,
1110  amrex::MultiFab& cons,
1111  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1112  const amrex::Real tkefac = one,
1113  const amrex::Real zscale = amrex::Real(700.0));
1114 
1115  /**
1116  * Fill the ghost cells of a planar surface-layer MultiFab, and the valid region of
1117  * its uncomputed copies when the 3D BoxArray is split in z (see PlanarBoundary).
1118  *
1119  * @param[in] lev level index
1120  * @param[in,out] mf planar MultiFab to fill
1121  */
1122  void fill_planar_boundary (const int& lev, amrex::MultiFab& mf);
1123 
1124  /**
1125  * Sum a planar field over the valid cells of the surface, counting each surface cell once.
1126  *
1127  * On a level whose grids are split in the surface-normal direction the planar BoxArray holds
1128  * one duplicate box per stacked 3D box (see PlanarBoundary), so a plain sum over the planar
1129  * MultiFab counts every surface cell once per stacked box. Only the computed surface copies
1130  * are read here, so the duplicates need not have been filled by fill_planar_boundary, and the
1131  * gather needs no communication. On EB terrain the fields are 3D without duplicates, and the
1132  * lowest plane of the domain is summed. Only for a surface layer on a z face.
1133  *
1134  * @param[in] lev level index
1135  * @param[in] mf cell-centered planar MultiFab on this surface's planar BoxArray
1136  * @param[in] comp component to sum
1137  */
1138  amrex::Real surface_sum (const int& lev, const amrex::MultiFab& mf, int comp = 0) const;
1139 
1140  /**
1141  * Impose surface-layer boundary conditions for planar terrain.
1142  *
1143  * @param[in] lev level index
1144  * @param[in] mfs state and velocity fields used by the BC computation
1145  * @param[in,out] Tau_lev stress fields to fill
1146  * @param[in,out] xheat_flux x-face heat flux field
1147  * @param[in,out] yheat_flux y-face heat flux field
1148  * @param[in,out] zheat_flux z-face heat flux field
1149  * @param[in,out] xqv_flux x-face moisture flux field
1150  * @param[in,out] yqv_flux y-face moisture flux field
1151  * @param[in,out] zqv_flux z-face moisture flux field
1152  * @param[in] z_phys physical-height field
1153  */
1154  void impose_SurfaceLayer_bcs (const int& lev,
1155  amrex::Vector<const amrex::MultiFab*> mfs,
1156  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Tau_lev,
1157  amrex::MultiFab* xheat_flux,
1158  amrex::MultiFab* yheat_flux,
1159  amrex::MultiFab* zheat_flux,
1160  amrex::MultiFab* xqv_flux,
1161  amrex::MultiFab* yqv_flux,
1162  amrex::MultiFab* zqv_flux,
1163  const amrex::MultiFab* z_phys,
1164  amrex::MultiFab* mf_surface_fluxes=nullptr);
1165 
1166  /**
1167  * Impose surface-layer boundary conditions for embedded-boundary terrain.
1168  *
1169  * @param[in] lev level index
1170  * @param[in] mfs state and velocity fields used by the BC computation
1171  * @param[in,out] Tau_lev EB stress fields to fill
1172  * @param[in,out] xheat_flux x-face heat flux field
1173  * @param[in,out] yheat_flux y-face heat flux field
1174  * @param[in,out] zheat_flux z-face heat flux field
1175  * @param[in,out] xqv_flux x-face moisture flux field
1176  * @param[in,out] yqv_flux y-face moisture flux field
1177  * @param[in,out] zqv_flux z-face moisture flux field
1178  */
1179  void impose_SurfaceLayer_bcs_EB (const int& lev,
1180  amrex::Vector<const amrex::MultiFab*> mfs,
1181  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>>& Tau_lev,
1182  amrex::MultiFab* xheat_flux,
1183  amrex::MultiFab* yheat_flux,
1184  amrex::MultiFab* zheat_flux,
1185  amrex::MultiFab* xqv_flux,
1186  amrex::MultiFab* yqv_flux,
1187  amrex::MultiFab* zqv_flux);
1188 
1189  /**
1190  * Compute planar-terrain surface-layer flux boundary conditions.
1191  *
1192  * @param[in] lev level index
1193  * @param[in] mfs state and velocity fields used by the BC computation
1194  * @param[in,out] Tau_lev stress fields to fill
1195  * @param[in,out] xheat_flux x-face heat flux field
1196  * @param[in,out] yheat_flux y-face heat flux field
1197  * @param[in,out] zheat_flux z-face heat flux field
1198  * @param[in,out] xqv_flux x-face moisture flux field
1199  * @param[in,out] yqv_flux y-face moisture flux field
1200  * @param[in,out] zqv_flux z-face moisture flux field
1201  * @param[in] z_phys physical-height field
1202  * @param[in] flux_comp flux-computation functor
1203  */
1204  template <typename FluxCalc>
1205  void compute_SurfaceLayer_bcs (const int& lev,
1206  amrex::Vector<const amrex::MultiFab*> mfs,
1207  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Tau_lev,
1208  amrex::MultiFab* xheat_flux,
1209  amrex::MultiFab* yheat_flux,
1210  amrex::MultiFab* zheat_flux,
1211  amrex::MultiFab* xqv_flux,
1212  amrex::MultiFab* yqv_flux,
1213  amrex::MultiFab* zqv_flux,
1214  const amrex::MultiFab* z_phys,
1215  const FluxCalc& flux_comp,
1216  amrex::MultiFab* mf_surface_fluxes=nullptr);
1217 
1218  /**
1219  * Compute embedded-boundary surface-layer flux boundary conditions.
1220  *
1221  * @param[in] lev level index
1222  * @param[in] mfs state and velocity fields used by the BC computation
1223  * @param[in,out] Tau_lev EB stress fields to fill
1224  * @param[in,out] xheat_flux x-face heat flux field
1225  * @param[in,out] yheat_flux y-face heat flux field
1226  * @param[in,out] zheat_flux z-face heat flux field
1227  * @param[in,out] xqv_flux x-face moisture flux field
1228  * @param[in,out] yqv_flux y-face moisture flux field
1229  * @param[in,out] zqv_flux z-face moisture flux field
1230  * @param[in] flux_comp flux-computation functor
1231  */
1232  template <typename FluxCalc>
1233  void compute_SurfaceLayer_bcs_EB (const int& lev,
1234  amrex::Vector<const amrex::MultiFab*> mfs,
1235  amrex::Vector<amrex::Vector<std::unique_ptr<amrex::MultiFab>>>& Tau_lev,
1236  amrex::MultiFab* xheat_flux,
1237  amrex::MultiFab* yheat_flux,
1238  amrex::MultiFab* zheat_flux,
1239  amrex::MultiFab* xqv_flux,
1240  amrex::MultiFab* yqv_flux,
1241  amrex::MultiFab* zqv_flux,
1242  const FluxCalc& flux_comp);
1243 
1244  /**
1245  * Derive MOST surface parameters from LSM fluxes.
1246  *
1247  * @param[in] lev level index
1248  * @param[in,out] cons_in conserved state used by the surface-parameter computation
1249  */
1250  void compute_sfc_params_from_lsm_fluxes (const int& lev,
1251  amrex::MultiFab& cons_in);
1252 
1253  /**
1254  * The four LSM fluxes on one grid: from the surface model when it supplies fluxes,
1255  * otherwise from the registered LSM fluxes. A flux that is not available is left null,
1256  * and SurfaceFluxView::valid then rejects every cell. compute_fluxes,
1257  * compute_sfc_params_from_lsm_fluxes and get_lsm_tsurf all take their fluxes from here,
1258  * so they agree on which land columns use the LSM flux. Each decides a ghost column
1259  * outside a non-periodic domain from the nearest column inside it; compute_SurfaceLayer_bcs
1260  * also reads the fluxes one ghost cell beyond the grid, so they must carry one filled
1261  * lateral ghost cell.
1262  *
1263  * @param[in] lev level index
1264  * @param[in] mfi iterator over a MultiFab with the cons BoxArray and DistributionMapping
1265  */
1266  [[nodiscard]] SurfaceFluxView get_lsm_flux_view (const int& lev,
1267  const amrex::MFIter& mfi) const;
1268 
1269  /**
1270  * Set kinematic u*, t*, and q* from the fluxes interpolated from most.sfc_file.
1271  *
1272  * @param[in] lev level index
1273  * @param[in] cons_in conserved state supplying the first-cell density
1274  * @param[in] tflux_in sensible heat flux H [W/m2]
1275  * @param[in] qflux_in latent heat flux LE [W/m2]
1276  * @param[in] sfc_tau kinematic surface stress TAU [m2/s2]
1277  */
1278  void set_sfc_params_from_file_fluxes (const int& lev,
1279  const amrex::MultiFab& cons_in,
1280  const amrex::Real& tflux_in,
1281  const amrex::Real& qflux_in,
1282  const amrex::Real& sfc_tau);
1283 
1284  /**
1285  * Fill surface temperature from available SST and TSK data.
1286  *
1287  * The SST and TSK slices hold absolute temperature [K]. They are interpolated in
1288  * time and converted to potential temperature with the surface pressure diagnosed
1289  * from the lowest cell of the current state, so the effective surface temperature
1290  * does not drift as the surface pressure evolves. Land cells without TSK take the
1291  * prescribed land surface potential temperature unchanged.
1292  *
1293  * @param[in] lev level index
1294  * @param[in] time interpolation time
1295  * @param[in] cons_in conserved state (surface pressure from the lowest cell)
1296  * @param[in] z_phys_nd nodal heights, or nullptr on a flat mesh
1297  */
1298  void fill_tsurf_with_sst_and_tsk (const int& lev,
1299  const double& time,
1300  const amrex::MultiFab& cons_in,
1301  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1303 
1304  /**
1305  * Fill surface temperature interpolated from time varying SST file
1306  *
1307  * @param[in] lev level index
1308  * @param[in] time interpolation time
1309  */
1310  void fill_tsurf_with_sfc_sst (const int& lev,
1311  const double& time,
1312  const amrex::MultiFab& cons_in,
1313  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1315 
1316  /**
1317  * Overwrite surface temperature with coupled ocean SST where the coupler
1318  * covers the cell.
1319  *
1320  * Runs after fill_tsurf_with_sst_and_tsk so that the lower-boundary data is
1321  * the base layer: land cells, and water cells with no ocean donor, keep the
1322  * value written there.
1323  *
1324  * @param[in] lev level index
1325  */
1326  void fill_tsurf_with_coupled_sst (const int& lev,
1327  const amrex::MultiFab& cons_in,
1328  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1330 
1331  /**
1332  * Set t_surf from the prescribed absolute surface temperature (most.abs_surf_temp, plus
1333  * most.surf_heating_rate times the time), converted to the potential temperature the
1334  * surface layer works in with the pressure at the surface diagnosed from the lowest
1335  * cell, as for coupled SST. Like update_surf_temp, which it replaces in that case, it
1336  * writes every cell of the surface.
1337  *
1338  * @param[in] lev level index
1339  * @param[in] time elapsed simulation time
1340  * @param[in] cons_in conserved state used to evaluate surface pressure
1341  * @param[in] z_phys_nd nodal physical-height field
1342  */
1343  void fill_tsurf_with_abs_surf_temp (const int& lev,
1344  const double& time,
1345  const amrex::MultiFab& cons_in,
1346  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1348 
1349  /**
1350  * Overwrite the land surface temperature with an external skin temperature.
1351  *
1352  * The skin is an absolute temperature [K]; it is converted to the potential
1353  * temperature the surface layer works in with the pressure at the surface
1354  * diagnosed from the lowest cell, as for coupled SST. Runs last among the
1355  * surface-temperature writers, so on land it replaces what they wrote.
1356  *
1357  * @param[in] lev level index
1358  * @param[in] cons_in conserved state used to evaluate surface pressure
1359  * @param[in] z_phys_nd nodal physical-height field
1360  */
1361  void fill_tsurf_with_skin_temperature (const int& lev,
1362  const amrex::MultiFab& cons_in,
1363  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1365 
1366  /**
1367  * Overwrite the land surface mixing ratio from the external skin temperature and the
1368  * moisture field of set_skin_moisture, with q_air the reference-height mixing ratio the
1369  * flux uses. Without resistances: beta * q_sat(T_skin, p_s) + (1 - beta) * q_air
1370  * (erf_surface_moisture::surface_mixing_ratio) with beta the soil-water factor, so the
1371  * moisture flux is beta times the potential one. With resistances:
1372  * erf_surface_moisture::two_source_surface_mixing_ratio of the aerodynamic resistance
1373  * (from the last u* and Obukhov length; neutral before the first flux), the canopy
1374  * resistance with its vapor-deficit factor, the soil resistance and the soil pore air's
1375  * relative humidity. Needs set_skin_temperature and the averages of this step.
1376  *
1377  * @param[in] lev level index
1378  * @param[in] cons_in conserved state (surface pressure and q_air from the lowest cell)
1379  * @param[in] z_phys_nd nodal heights, or nullptr on a flat mesh
1380  */
1381  void fill_qsurf_with_skin_moisture (const int& lev,
1382  const amrex::MultiFab& cons_in,
1383  const std::unique_ptr<amrex::MultiFab>& z_phys_nd);
1384 
1385  void fill_qsurf_with_skin_moisture (const int& lev,
1386  const amrex::MultiFab& cons_in,
1387  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1389 
1390  /**
1391  * Fill surface moisture from saturation specific humidity.
1392  *
1393  * @param[in] lev level index
1394  * @param[in] cons_in conserved state used to evaluate surface pressure
1395  * @param[in] z_phys_nd nodal physical-height field
1396  */
1397  void fill_qsurf_with_qsat (const int& lev,
1398  const amrex::MultiFab& cons_in,
1399  const std::unique_ptr<amrex::MultiFab>& z_phys_nd);
1400 
1401  void fill_qsurf_with_qsat (const int& lev,
1402  const amrex::MultiFab& cons_in,
1403  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1405 
1406  void set_pblh (const int& lev, const amrex::MultiFab& pblh_in);
1407 
1408  /**
1409  * Updates current time index for interpolating data from SFC/SST file.
1410  *
1411  * @param[in] time elapsed time
1412  */
1413  void update_sfc_time_index (const amrex::Real& time);
1414 
1415  /**
1416  * Interpolates the SFC/SST data at the given column and time
1417  *
1418  * @param[in] time elapsed time
1419  * @param[in] col column index of file data
1420  */
1421  amrex::Real interpolate_sfc_column (const amrex::Real& time,
1422  int col) const;
1423 
1424  /**
1425  * Fill surface temperature from the LSM surface-temperature field, converted to
1426  * potential temperature at the surface pressure when the field is an absolute
1427  * temperature.
1428  *
1429  * @param[in] lev level index
1430  * @param[in] cons_in conserved state (surface pressure from the lowest cell)
1431  * @param[in] z_phys_nd nodal heights, or nullptr on a flat mesh
1432  */
1433  void get_lsm_tsurf (const int& lev,
1434  const amrex::MultiFab& cons_in,
1435  const std::unique_ptr<amrex::MultiFab>& z_phys_nd,
1437 
1438  /**
1439  * Wrapper around compute_pblh.
1440  *
1441  * @param[in] lev level index
1442  * @param[in,out] vars state variables used by the PBL-height calculation
1443  * @param[in] z_phys_cc cell-centered physical-height field
1444  * @param[in] moisture_indices indices for moisture components
1445  */
1446  void update_pblh (const int& lev,
1447  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars,
1448  amrex::MultiFab* z_phys_cc,
1449  const MoistureComponentIndices& moisture_indices);
1450 
1451  /**
1452  * Set the PBL height of a refined level whose grids do not run from the ground to the top
1453  * of the domain in every column from the next coarser level, column by column (see
1454  * compute_pblh). Public because it launches a device lambda.
1455  *
1456  * @param[in] lev Current level (> 0)
1457  */
1458  void fill_pblh_from_coarser (const int& lev);
1459 
1460  /**
1461  * The PBL height of the next coarser level, injected onto the grids of this level over all
1462  * of their cells, ghost cells included. Public because it launches a device lambda.
1463  *
1464  * @param[in] lev Current level (> 0)
1465  * @param[out] fine MultiFab on the grids of pblh[lev]
1466  */
1467  void inject_pblh_from_coarser (const int& lev, amrex::MultiFab& fine) const;
1468 
1469  /**
1470  * Compute planetary-boundary-layer height with the selected estimator.
1471  *
1472  * @param[in] lev level index
1473  * @param[in,out] vars state variables used by the PBL-height calculation
1474  * @param[in] z_phys_cc cell-centered physical-height field
1475  * @param[in] est PBL-height estimator functor
1476  * @param[in] moisture_indice indices for moisture components
1477  */
1478  template <typename PBLHeightEstimator>
1479  void compute_pblh (const int& lev,
1480  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars,
1481  amrex::MultiFab* z_phys_cc,
1482  const PBLHeightEstimator& est,
1483  const MoistureComponentIndices& moisture_indice);
1484 
1485  /**
1486  * Read custom roughness data for one level.
1487  *
1488  * @param[in] lev level index
1489  * @param[in] fname roughness-data file name
1490  */
1491  void read_custom_roughness (const int& lev,
1492  const std::string& fname);
1493 
1494  /**
1495  * Update prescribed surface temperature from the configured heating rate.
1496  *
1497  * @param[in] time elapsed simulation time
1498  */
1499  void update_surf_temp (const double& time)
1500  {
1501  // NOTE: this is a whole-domain setVal, so it overwrites the SST/TSK fill
1502  // done earlier in update_fluxes. Coupled SST is applied after this
1503  // call and therefore still wins on the water cells it covers.
1504  // An absolute surface temperature is written, converted, by fill_tsurf_with_abs_surf_temp
1505  if (surf_temp_is_absolute) { return; }
1506  if (surf_heating_rate != 0) {
1507  // Use the actual size of t_surf, not m_geom.size(), which is always
1508  // max_level+1 and so runs past the levels that exist. t_surf is sized for
1509  // all levels up front but filled one level at a time, so we also have to
1510  // skip the entries that have not been allocated yet.
1511  int nlevs = static_cast<int>(t_surf.size());
1512  for (int lev = 0; lev < nlevs; lev++) {
1513  if (!t_surf[lev]) { continue; }
1514  t_surf[lev]->setVal(surf_temp + surf_heating_rate * static_cast<amrex::Real>(time));
1515  amrex::Print() << "Surface temp at t=" << time << ": "
1516  << surf_temp + surf_heating_rate * time << std::endl;
1517  }
1518  }
1519  }
1520 
1521  /**
1522  * Update MOST-average field pointers.
1523  *
1524  * @param[in] lev level index
1525  * @param[in] vars_old old-time state variables
1526  * @param[in] Theta_prim primitive potential-temperature fields by level
1527  * @param[in] Qv_prim primitive water-vapor fields by level
1528  * @param[in] Qr_prim primitive rain-water fields by level
1529  */
1530  void update_mac_ptrs (const int& lev,
1531  amrex::Vector<amrex::Vector<amrex::MultiFab>>& vars_old,
1532  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Theta_prim,
1533  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qv_prim,
1534  amrex::Vector<std::unique_ptr<amrex::MultiFab>>& Qr_prim)
1535  {
1536  m_ma.update_field_ptrs(lev, vars_old, Theta_prim, Qv_prim, Qr_prim);
1537  if (m_use_surface_current) {
1538  // Average the wind relative to the surface. The averages read a copy of the
1539  // velocities taken here, not the velocities themselves, so they must be computed
1540  // before the velocities change, as update_fluxes does right after this.
1541  build_relative_velocities(lev, vars_old[lev][Vars::xvel], vars_old[lev][Vars::yvel]);
1543  }
1544  }
1545 
1546  /**
1547  * Allocate the surface current for this level's velocities and set it over the water,
1548  * and allocate the velocities relative to it over the planes the averages read.
1549  *
1550  * The current is held on the x- and y-faces of the plane k = klo, one 2D box for each
1551  * box of the velocities, with their lateral ghost cells. Only water moves: a face takes
1552  * the current where the cells on both sides of it are water (land mask 0), and is at
1553  * rest elsewhere.
1554  *
1555  * @param[in] lev level index
1556  * @param[in] xvel x-velocity on the level
1557  * @param[in] yvel y-velocity on the level
1558  */
1559  void define_surface_current (const int& lev, const amrex::MultiFab& xvel,
1560  const amrex::MultiFab& yvel);
1561 
1562  /**
1563  * Set the surface current on this level's faces: the uniform most.surface_current, or the
1564  * coupler's last current (update_coupled_surface_current), with water on both sides of a
1565  * face and, with a coupler, the ocean covering both cells; at rest elsewhere.
1566  *
1567  * @param[in] lev level index
1568  */
1569  void fill_surface_current (const int& lev);
1570 
1571  /**
1572  * Allocate the velocities relative to the surface current, over the planes the
1573  * averages read: each box of the velocities from its lower end to k_top.
1574  *
1575  * @param[in] lev level index
1576  * @param[in] xvel x-velocity on the level
1577  * @param[in] yvel y-velocity on the level
1578  * @param[in] k_top highest k the averages read (MOSTAverage::max_k_read)
1579  */
1580  void define_relative_velocities (const int& lev, const amrex::MultiFab& xvel,
1581  const amrex::MultiFab& yvel, int k_top);
1582 
1583  /**
1584  * Fill the velocities relative to the surface current.
1585  *
1586  * @param[in] lev level index
1587  * @param[in] xvel x-velocity on the level
1588  * @param[in] yvel y-velocity on the level
1589  */
1590  void build_relative_velocities (const int& lev, const amrex::MultiFab& xvel,
1591  const amrex::MultiFab& yvel);
1592 
1593  /**
1594  * Return whether the surface layer takes the wind relative to a current of the surface.
1595  */
1596  [[nodiscard]] bool use_surface_current () const { return m_use_surface_current; }
1597 
1598  /**
1599  * Return a component of the surface current on its faces of the plane k = klo, or
1600  * null when there is none on this level.
1601  *
1602  * @param[in] lev level index
1603  * @param[in] comp 0 for the x-component on x-faces, 1 for the y-component on y-faces
1604  */
1605  [[nodiscard]] const amrex::MultiFab* get_surface_current (const int& lev, int comp) const
1606  {
1607  const auto& current = (comp == 0) ? m_surface_current_x : m_surface_current_y;
1608  return (m_use_surface_current && lev < static_cast<int>(current.size()))
1609  ? current[lev].get() : nullptr;
1610  }
1611 
1612  /**
1613  * Return the friction-velocity field.
1614  *
1615  * @param[in] lev level index
1616  */
1617  amrex::MultiFab* get_u_star (const int& lev) { return u_star[lev].get(); }
1618 
1619  /**
1620  * Return the convective velocity scale field.
1621  *
1622  * @param[in] lev level index
1623  */
1624  amrex::MultiFab* get_w_star (const int& lev) { return w_star[lev].get(); }
1625 
1626  /**
1627  * Do we actually compute w*? If not then the field returned by get_w_star holds
1628  * only the value it was initialized with, and must not be reported as a diagnostic.
1629  */
1630  [[nodiscard]] bool computes_w_star () const { return m_include_wstar; }
1631 
1632  /**
1633  * Do we actually compute the PBL height? If not then the field returned by get_pblh
1634  * holds only the value it was initialized with, and must not be reported as a diagnostic.
1635  */
1636  [[nodiscard]] bool computes_pblh () const { return (pblh_type != PBLHeightCalcType::None); }
1637 
1638  /**
1639  * Return the temperature scale field.
1640  *
1641  * @param[in] lev level index
1642  */
1643  amrex::MultiFab* get_t_star (const int& lev) { return t_star[lev].get(); }
1644 
1645  /**
1646  * Return the moisture scale field.
1647  *
1648  * @param[in] lev level index
1649  */
1650  amrex::MultiFab* get_q_star (const int& lev) { return q_star[lev].get(); }
1651 
1652  /**
1653  * Return the Obukhov length field.
1654  *
1655  * @param[in] lev level index
1656  */
1657  amrex::MultiFab* get_olen (const int& lev) { return olen[lev].get(); }
1658 
1659  /** Similarity functions of this surface layer (most.stable_functions). */
1660  const similarity_funs& similarity_functions () const { return m_sfuns; }
1661 
1662  /**
1663  * Return the planetary-boundary-layer-height field.
1664  *
1665  * @param[in] lev level index
1666  */
1667  amrex::MultiFab* get_pblh (const int& lev) { return pblh[lev].get(); }
1668 
1669  /**
1670  * Return a MOST-average field.
1671  *
1672  * @param[in] lev level index
1673  * @param[in] comp component index
1674  */
1675  const amrex::MultiFab* get_mac_avg (const int& lev, int comp)
1676  {
1677  return m_ma.get_average(lev, comp);
1678  }
1679 
1680  /**
1681  * Return whether the MOST averages are filtered in time.
1682  */
1684 
1685  /**
1686  * Return the number of MOST-average components.
1687  */
1688  int get_num_mac_avg () const { return m_ma.get_navg(); }
1689 
1690  /**
1691  * Return whether the time filter at this level holds meaningful history.
1692  *
1693  * @param[in] lev level index
1694  */
1695  bool mac_avg_is_initialized (const int& lev) const { return m_ma.time_avg_is_initialized(lev); }
1696 
1697  /**
1698  * Declare the time filter at this level to hold meaningful history (restart).
1699  *
1700  * @param[in] lev level index
1701  */
1703 
1704  /**
1705  * Return a MOST-average field for modification (restart).
1706  *
1707  * @param[in] lev level index
1708  * @param[in] comp component index
1709  */
1710  amrex::MultiFab* get_mac_avg_ptr (const int& lev, int comp) { return m_ma.get_average(lev, comp); }
1711 
1712  /**
1713  * Return the filtered plane averages, which hold the filter state for the
1714  * plane and EB averaging policies.
1715  *
1716  * @param[in] lev level index
1717  */
1718  amrex::Vector<amrex::Real> get_mac_plane_avg (const int& lev) const { return m_ma.get_plane_average(lev); }
1719 
1720  /**
1721  * Restore the filtered plane averages from a checkpoint; returns false if the
1722  * checkpoint does not hold what this run expects.
1723  *
1724  * @param[in] lev level index
1725  * @param[in] pavg filtered plane averages, one per average component
1726  */
1727  bool set_mac_plane_avg (const int& lev, const amrex::Vector<amrex::Real>& pavg) { return m_ma.set_plane_average(lev, pavg); }
1728 
1729  /**
1730  * Return the surface-temperature field.
1731  *
1732  * @param[in] lev level index
1733  */
1734  amrex::MultiFab* get_t_surf (const int& lev) { return t_surf[lev].get(); }
1735 
1736  /**
1737  * Take the land surface temperature of level lev from an external absolute
1738  * skin temperature [K] on the surface-layer layout, or stop doing so (nullptr).
1739  * update_fluxes reads it; the owner keeps it alive and resets the pointer
1740  * whenever it reallocates the field.
1741  *
1742  * @param[in] lev level index
1743  * @param[in] t_skin absolute skin temperature, or nullptr
1744  */
1745  void set_skin_temperature (const int& lev, const amrex::MultiFab* t_skin)
1746  {
1747  if (lev >= static_cast<int>(m_skin_tsurf_lev.size())) {
1748  m_skin_tsurf_lev.resize(lev + 1, nullptr);
1749  }
1750  m_skin_tsurf_lev[lev] = t_skin;
1751  }
1752 
1753  /**
1754  * Why this surface layer cannot take its surface temperature from an external
1755  * skin temperature, or an empty string when it can: the skin sets t_surf, so the
1756  * surface layer must compute its heat flux from t_surf (a surface temperature given,
1757  * no heating rate) on a non-EB lower surface without rotated surface fluxes, and no
1758  * land model may own t_surf.
1759  */
1760  [[nodiscard]] std::string skin_temperature_conflict () const
1761  {
1762  if (m_terrain_type == TerrainType::EB) {
1763  return "the surface layer is on EB terrain";
1764  }
1765  // Rotated, the flux is split over hfx1/hfx2/hfx3 and the balance, which removes
1766  // the vertical-face flux hfx3 only, would lose cos(slope) of what the air gains.
1767  if (m_rotate) {
1768  return "erf.use_rotate_surface_flux is set";
1769  }
1771  return "the surface layer does not compute its heat flux from a surface "
1772  "temperature (set erf.most.pot_surf_temp or erf.most.abs_surf_temp, not a "
1773  "flux or custom/bulk fluxes)";
1774  }
1775  if (surf_heating_rate != amrex::Real(0)) {
1776  return "erf.most.surf_heating_rate is set";
1777  }
1779  return "a land-surface or surface model supplies the surface temperature";
1780  }
1781  return "";
1782  }
1783 
1784  /**
1785  * Take the land surface mixing ratio of level lev from the skin set by
1786  * set_skin_temperature (fill_qsurf_with_skin_moisture), or stop doing so (nullptr).
1787  * The owner keeps the field alive and resets the pointer whenever it reallocates it.
1788  *
1789  * @param[in] lev level index
1790  * @param[in] moisture per-column field in the components of
1791  * erf_surface_moisture::Component, on the surface-layer layout
1792  * @param[in] use_resistances form q_surf from the soil resistance and pore humidity and,
1793  * where the field's VegetationFraction is positive, the
1794  * canopy's (two_source_surface_mixing_ratio), not from the
1795  * soil-water factor alone:
1796  * the two-stream balance passes true whenever it has a soil type
1797  * @param[in] hs the canopy's vapor-deficit parameter [1/(kg/kg)]; 0 without
1798  * vegetation
1799  */
1800  void set_skin_moisture (const int& lev, const amrex::MultiFab* moisture,
1801  bool use_resistances = false, amrex::Real hs = amrex::Real(0))
1802  {
1803  if (lev >= static_cast<int>(m_skin_moisture_lev.size())) {
1804  m_skin_moisture_lev.resize(lev + 1, nullptr);
1805  m_skin_moisture_resistances_lev.resize(lev + 1, 0);
1806  m_skin_moisture_hs_lev.resize(lev + 1, amrex::Real(0));
1807  }
1808  m_skin_moisture_lev[lev] = moisture;
1809  m_skin_moisture_resistances_lev[lev] = use_resistances ? 1 : 0;
1810  m_skin_moisture_hs_lev[lev] = hs;
1811  }
1812 
1813  /**
1814  * Why this surface layer cannot take its surface mixing ratio from the skin
1815  * (set_skin_moisture), or an empty string when it can: everything
1816  * skin_temperature_conflict asks, plus a moisture model and a moisture flux computed
1817  * from a surface mixing ratio (erf.most.surf_moist given).
1818  */
1819  [[nodiscard]] std::string skin_moisture_conflict () const
1820  {
1821  const std::string temperature = skin_temperature_conflict();
1822  if (!temperature.empty()) { return temperature; }
1823  if (!use_moisture) {
1824  return "the run has no moisture model";
1825  }
1827  return "the surface layer does not compute its moisture flux from a surface "
1828  "mixing ratio (set erf.most.surf_moist, not a flux)";
1829  }
1830  return "";
1831  }
1832 
1833  /**
1834  * The land roughness length [m] to use when erf.most.z0 is not given (the two-stream
1835  * balance's, from Noah-MP's tables). Call before the levels are made, which set z0 from
1836  * it.
1837  *
1838  * Taking it also replaces the erf.most.z0 entry that the constructor's queryAdd put in
1839  * the ParmParse table. job_info (ParmParse::dumpTable) then records the roughness the
1840  * run used.
1841  *
1842  * @return whether the roughness was taken: an erf.most.z0 given in the inputs stands.
1843  */
1844  bool set_default_roughness (amrex::Real z0)
1845  {
1846  if (m_z0_given) { return false; }
1847  z0_const = z0;
1848  amrex::ParmParse pp(m_pp_prefix);
1849  pp.remove("most.z0");
1850  pp.add("most.z0", z0_const);
1851  return true;
1852  }
1853 
1854  /** The constant roughness length [m] the levels take. */
1855  [[nodiscard]] amrex::Real roughness () const { return z0_const; }
1856 
1857  /**
1858  * Return whether prescribed surface fluxes are active.
1859  */
1860  bool use_sfc_fluxes () const { return m_use_sfc_fluxes; }
1861 
1862  /**
1863  * Return whether the surface fluxes are rotated with the terrain slope
1864  * (erf.use_rotate_surface_flux), splitting them over the x, y and z faces.
1865  */
1866  bool rotates_surface_fluxes () const { return m_rotate; }
1867 
1868  /**
1869  * Set the surface-temperature field to a constant value.
1870  *
1871  * @param[in] lev level index
1872  * @param[in] tsurf surface temperature
1873  */
1874  void set_t_surf (const int& lev, const amrex::Real tsurf) { t_surf[lev]->setVal(tsurf); }
1875 
1876  /**
1877  * Return the surface-moisture field.
1878  *
1879  * @param[in] lev level index
1880  */
1881  amrex::MultiFab* get_q_surf (const int& lev) { return q_surf[lev].get(); }
1882 
1883  /**
1884  * Set the surface-moisture field to a constant value.
1885  *
1886  * @param[in] lev level index
1887  * @param[in] qsurf surface moisture
1888  */
1889  void set_q_surf (const int& lev, const amrex::Real qsurf) { q_surf[lev]->setVal(qsurf); }
1890 
1891  /**
1892  * Return the surface-diagnostic provenance field.
1893  *
1894  * @param[in] lev level index
1895  */
1896  amrex::MultiFab* get_surface_diagnostic_source (const int& lev) { return surface_diagnostic_source[lev].get(); }
1897 
1898  /**
1899  * Return the 2D MF of zref heights.
1900  *
1901  * @param[in] lev level index
1902  */
1903  amrex::MultiFab* get_zref (const int& lev) { return m_ma.get_zref(lev); }
1904 
1905  /**
1906  * Return the minimum reference height for one level.
1907  *
1908  * @param[in] lev level index
1909  */
1910  amrex::Real get_zref_min (const int& lev) { return (m_ma.get_zref(lev))->min(0); }
1911 
1912  /**
1913  * Return the roughness-height field.
1914  *
1915  * @param[in] lev level index
1916  */
1917  amrex::MultiFab* get_z0 (const int& lev) { return &z_0[lev]; }
1918 
1919  /**
1920  * Return the roughness-height field, read-only (for a caller that holds a
1921  * const SurfaceLayer, such as a fire engine).
1922  *
1923  * @param[in] lev level index
1924  */
1925  const amrex::MultiFab* get_z0 (const int& lev) const { return &z_0[lev]; }
1926 
1927  /**
1928  * Return whether variable sea roughness is active.
1929  */
1930  bool have_variable_sea_roughness () const { return m_var_z0; }
1931 
1932  /**
1933  * Return the land-mask field.
1934  *
1935  * @param[in] lev level index
1936  */
1937  amrex::iMultiFab* get_lmask (const int& lev) { return m_lmask_lev[lev][0]; }
1938 
1939  /**
1940  * Compute the minimum land-mask value over valid and optional ghost cells.
1941  *
1942  * @param[in] lmask land-mask field
1943  * @param[in] nghost number of ghost cells included in the reduction
1944  */
1945  int lmask_min_reduce (amrex::iMultiFab& lmask,
1946  const int& nghost)
1947  {
1948  int lmask_min = amrex::ReduceMin(lmask, nghost, [=] AMREX_GPU_HOST_DEVICE(
1949  amrex::Box const& bx, amrex::Array4<int const> const& lm_arr) -> int
1950  {
1951  int locmin = std::numeric_limits<int>::max();
1952  const auto lo = lbound(bx);
1953  const auto hi = ubound(bx);
1954  for (int j = lo.y; j <= hi.y; ++j) {
1955  for (int i = lo.x; i <= hi.x; ++i) {
1956  locmin = std::min(locmin, lm_arr(i, j, 0));
1957  }
1958  }
1959  return locmin;
1960  });
1961 
1962  return lmask_min;
1963  }
1964 
1965  /**
1966  * Update one stored sea-surface-temperature pointer.
1967  *
1968  * @param[in] lev level index
1969  * @param[in] itime time-slice index
1970  * @param[in] sst_ptr sea-surface-temperature field pointer
1971  */
1972  void update_sst_ptr (const int lev, const int itime, amrex::MultiFab* sst_ptr) {
1973  AMREX_ALWAYS_ASSERT(itime >= 0 && itime < static_cast<int>(m_sst_lev[lev].size()));
1974  m_sst_lev[lev][itime] = sst_ptr;
1975  }
1976 
1977  /**
1978  * Update one stored skin-temperature pointer.
1979  *
1980  * @param[in] lev level index
1981  * @param[in] itime time-slice index
1982  * @param[in] tsk_ptr skin-temperature field pointer
1983  */
1984  void update_tsk_ptr (const int lev, const int itime, amrex::MultiFab* tsk_ptr) {
1985  AMREX_ALWAYS_ASSERT(itime >= 0 && itime < static_cast<int>(m_tsk_lev[lev].size()));
1986  m_tsk_lev[lev][itime] = tsk_ptr;
1987  }
1988 
1989  /**
1990  * Hand over the coupled sea-surface temperature and its per-cell coverage flag.
1991  *
1992  * Both pointers stay owned by the caller and must remain valid, and on the same
1993  * layout, until replaced. Passing a null sst_ptr retracts the coupled lane, so
1994  * the lower-boundary SST/TSK value stands everywhere again.
1995  *
1996  * @param[in] lev level index
1997  * @param[in] sst_ptr coupled sea-surface temperature [K]
1998  * @param[in] valid_ptr per-cell coverage flag; nonzero where the coupler
1999  * supplied a value. Null means no cell is covered.
2000  */
2001  void update_coupled_sst_ptr (const int lev,
2002  amrex::MultiFab* sst_ptr,
2003  amrex::iMultiFab* valid_ptr) {
2004  m_coupled_sst_lev[lev] = sst_ptr;
2005  m_coupled_sst_valid_lev[lev] = valid_ptr;
2006  }
2007 
2008  /**
2009  * Declare that an ocean coupler will supply SST for this run.
2010  *
2011  * Must be called before make_SurfaceLayer_at_level, which uses it to select
2012  * ThetaCalcType::SURFACE_TEMPERATURE.
2013  *
2014  * @param[in] active whether coupled SST is configured
2015  */
2016  void set_coupled_sst_active (const bool active) { m_use_coupled_sst = active; }
2017 
2018  /**
2019  * Declare that an ocean coupler will supply the surface current for this run.
2020  *
2021  * Must be called before make_SurfaceLayer_at_level, which allocates the current. The
2022  * surface is at rest until update_coupled_surface_current first sets it.
2023  *
2024  * @param[in] active whether a coupled surface current is configured
2025  */
2026  void set_coupled_current_active (const bool active)
2027  {
2028  // Idempotent: the set-up that calls it may run again on the same layer
2029  if (!active || m_coupled_current_active) { return; }
2031  "most.surface_current cannot be combined with a coupled surface current "
2032  "(erf.use_coupled_currents).");
2033  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_face.coordDir() == 2 && m_face.isLow(),
2034  "A coupled surface current is supported only on the z-low face.");
2036  "A coupled surface current is not supported with EB terrain.");
2037  m_coupled_current_active = true;
2038  m_use_surface_current = true;
2039  }
2040 
2041  /**
2042  * Set the surface current on this level from an ocean coupler's current at the cell
2043  * centers of the surface.
2044  *
2045  * The current is rotated from east and north to the grid axes and averaged to the faces.
2046  * A face takes it where the cells on both sides of it are water (land mask 0) and covered
2047  * by the ocean, and is at rest elsewhere. Outside a non-periodic domain, the cells beside
2048  * a face are those of the nearest column inside it.
2049  *
2050  * The fields are borrowed, like the coupled SST's: the surface layer keeps them and sets
2051  * the current from them again whenever the level is remade, so they must outlive it or
2052  * be replaced by a later call.
2053  *
2054  * @param[in] lev level index
2055  * @param[in] east eastward current [m/s], cell-centered on the 2D surface layout
2056  * @param[in] north northward current [m/s], on the layout of east
2057  * @param[in] covered nonzero where the ocean covers the cell, on the layout of east;
2058  * null when it covers none, as for the coupled SST
2059  * @param[in] cos_alpha cosine of the angle of the grid x axis from east, cell-centered on
2060  * the 2D surface layout; null when the grid is aligned with east
2061  * @param[in] sin_alpha sine of that angle, on the layout of cos_alpha
2062  */
2063  void update_coupled_surface_current (const int& lev,
2064  const amrex::MultiFab& east,
2065  const amrex::MultiFab& north,
2066  const amrex::iMultiFab* covered,
2067  const amrex::MultiFab* cos_alpha,
2068  const amrex::MultiFab* sin_alpha);
2069 
2070  /**
2071  * Set the domain faces that use the surface-layer boundary condition.
2072  *
2073  * The face set is used to suppress transpose stress writes at shared edges;
2074  * the face-normal component remains owned by its corresponding face path.
2075  *
2076  * @param[in] active_faces whether a specific x/y/z lo/hi face is enabled
2077  */
2078  void set_surface_layer_faces (const amrex::GpuArray<int, AMREX_SPACEDIM*2>& active_faces)
2079  {
2080  m_surface_layer_faces = active_faces;
2081  }
2082 
2083  /**
2084  * Reads columns of data from a text file, returning each column in a vector.
2085  *
2086  * @param[in] fname path to text file
2087  * @param[in] skip_nlines number of lines to skip before reading data (e.g, header lines)
2088  * @return Vector containing each column in the file as a vector
2089  */
2090  static amrex::Vector<amrex::Vector<amrex::Real>>
2091  read_cols (const std::string& fname, const int skip_nlines = 1);
2092 
2093  /**
2094  * Fills surface layer data for lateral faces. A normal FillBoundary on
2095  * lateral faces would bring in data from other ranks or interior grids
2096  * that do not own data on the lateral face.
2097  *
2098  * @param[in] lev level index
2099  * @param[in,out] selected_field if non-null, fill only this field;
2100  * otherwise fill all lateral surface fields
2101  */
2102  void fill_lateral_surface_parameter_ghosts (const int& lev,
2103  amrex::MultiFab* selected_field = nullptr);
2104 
2105  enum struct FluxCalcType {
2106  MOENG = 0, ///< Moeng functional form
2107  CUSTOM, ///< Custom constant flux functional form
2108  BULK_COEFF, ///< Bulk transfer coefficient functional form
2109  ROTATE, ///< Terrain rotation flux functional form
2110  RICO
2111  };
2112 
2113  enum struct ThetaCalcType {
2114  ADIABATIC = 0,
2115  HEAT_FLUX, ///< Heat-flux specified
2116  SURFACE_TEMPERATURE ///< Surface temperature specified
2117  };
2118 
2119  enum struct MoistCalcType {
2120  ADIABATIC = 0,
2121  MOISTURE_FLUX, ///< Qv-flux specified
2122  SURFACE_MOISTURE ///< Surface Qv specified
2123  };
2124 
2125  enum struct RoughCalcType {
2126  CONSTANT = 0, ///< Constant z0
2127  CHARNOCK,
2129  DONELAN,
2130  WAVE_COUPLED
2131  };
2132 
2133  enum struct PBLHeightCalcType { None, MYNN25, YSU, MRF };
2134 
2141  similarity_funs m_sfuns; ///< stable-side similarity functions (most.stable_functions)
2142 
2143 private:
2144  // Set in constructor
2145  amrex::Orientation m_face;
2146  std::string m_pp_prefix;
2147  amrex::Vector<amrex::Geometry> m_geom;
2148  // Reused for the two collective validation points in update_fluxes; all GPU writes
2149  // complete before it is read back and before their outputs reach a flux kernel.
2150  amrex::Gpu::DeviceScalar<erf_surface_temperature::ConversionFailure>
2152  bool m_rotate = false;
2153  amrex::GpuArray<int, AMREX_SPACEDIM*2> m_surface_layer_faces{};
2157 
2158  bool m_include_wstar = false;
2159  amrex::Real z0_const{amrex::Real(0.1)};
2160  // erf.most.z0 was given (set_default_roughness then leaves z0_const alone).
2161  bool m_z0_given{false};
2162  amrex::Real default_land_surf_temp{amrex::Real(300.)};
2163  // Negative sentinels: a surface temperature is in kelvin and a surface moisture is a
2164  // mixing ratio, so neither can legitimately be negative. The constructor probes
2165  // most.pot_surf_temp / most.abs_surf_temp / most.surf_moist with these and tests the
2166  // resulting value to decide whether the user supplied them. They were previously left
2167  // uninitialized, which was safe only because a plain query() leaves them untouched on
2168  // a miss.
2169  amrex::Real surf_temp{amrex::Real(-1.)};
2170  // surf_temp (and surf_heating_rate) is an absolute temperature (most.abs_surf_temp),
2171  // converted to t_surf's potential temperature with the surface pressure each step.
2173  amrex::Real surf_heating_rate{0};
2174  amrex::Real surf_temp_flux{0};
2176  amrex::Real surf_moist{amrex::Real(-1.)};
2177  amrex::Real surf_moist_flux{0};
2178  amrex::Real custom_ustar{0};
2179  amrex::Real custom_tstar{0};
2180  amrex::Real custom_qstar{0};
2181  amrex::Real custom_rhosurf{0}; // use specified value instead of rho from first cell
2182  bool specified_rho_surf{false};
2183  amrex::Real cnk_a{amrex::Real(0.0185)};
2184  bool smooth_flow_visc{true};
2185  amrex::Real depth{amrex::Real(30.0)};
2186  amrex::Vector<amrex::MultiFab> z_0;
2187  bool m_var_z0{false};
2188 
2189  amrex::Real rico_theta_z0{amrex::Real(298.0)};
2190  amrex::Real rico_qsat_z0{amrex::Real(0.001)};
2191 
2193  bool m_has_lsm_fluxes = false;
2194  bool m_has_lsm_tsurf = false;
2196  bool m_lsm_tsurf_is_theta = false;
2198 
2199  // variables for sfc/sst forcing from file
2200  bool m_use_sfc_fluxes = false;
2201  bool m_use_sfc_sst = false;
2202  int sfc_time_ind = 0;
2203  amrex::Vector<amrex::Vector<amrex::Real>> sfc;
2204  amrex::Real sfc_qflux = zero;
2205  amrex::Real sfc_tflux = zero;
2206  amrex::Real sfc_ustar = zero;
2207 
2208  // Set from solverChoice.use_coupled_sst before the per-level Define runs.
2209  // Configuration only: true means an ocean coupler will supply SST on some
2210  // subset of the water cells, not that it has supplied any yet.
2211  bool m_use_coupled_sst = false;
2212 
2213  amrex::Real m_Cd = zero;
2214  amrex::Real m_Ch = zero;
2215  amrex::Real m_Cq = zero;
2216  // bulk_coeff only: most.surf_temp_flux given, imposed kinematic heat flux [K m/s]
2218  amrex::Real m_bulk_fixed_tflux = zero;
2219  bool m_ignore_sst = false;
2220 
2221  amrex::Vector<const eb_*> m_eb_vec;
2222  TerrainType m_terrain_type;
2224  amrex::Real m_rdOcp = RdoCp;
2227  bool use_surface_model = false;
2228  bool surf_model_fluxes = false;
2229  amrex::Vector<std::unique_ptr<amrex::MultiFab>> u_star;
2230  amrex::Vector<std::unique_ptr<amrex::MultiFab>> w_star;
2231  amrex::Vector<std::unique_ptr<amrex::MultiFab>> t_star;
2232  amrex::Vector<std::unique_ptr<amrex::MultiFab>> q_star;
2233  amrex::Vector<std::unique_ptr<amrex::MultiFab>> olen;
2234  amrex::Vector<std::unique_ptr<amrex::MultiFab>> pblh;
2235  amrex::Vector<std::unique_ptr<amrex::MultiFab>> t_surf;
2236  amrex::Vector<std::unique_ptr<amrex::MultiFab>> q_surf;
2237  // The planar BoxArray is the z-collapse of the 3D BoxArray, so a 3D BoxArray split in z
2238  // gives duplicate planar boxes (see PlanarBoundary). fill_planar_boundary goes through
2239  // this for u*, w* (only when it is computed), t*, q*, L, t_surf, q_surf, pblh and the
2240  // diagnostic source, after each is computed on the surface copies.
2241  amrex::Vector<PlanarBoundary> m_planar_bndry;
2242  // 1 in the columns of a refined level that a grid touching the surface holds, within one
2243  // cell of each grid; empty on level 0. Built by make_SurfaceLayer_at_level.
2244  amrex::Vector<amrex::iMultiFab> m_lsm_held_columns;
2245  // Columns for the PBL-height estimator. The estimator scans each box it is given from its
2246  // lowest cell to its highest, so the box must start at the ground. When some box of a
2247  // level does not (grids stacked in z, or a refined region aloft), compute_pblh runs the
2248  // estimator on these boxes instead: the runs of cells that start at the ground, each joined
2249  // into one box that ends where the grids above that ground end. Built by
2250  // define_pblh_columns when the grids of the level change.
2252  {
2253  amrex::BoxArray ba; // Grids the columns were built for ...
2254  amrex::DistributionMapping dm; // ... and their ranks
2255  bool needed = false; // Whether some box does not start at the ground
2256  bool full_height = true; // Whether every column runs from the ground to the top
2257  amrex::BoxArray ba_col; // The columns; empty on a level entirely aloft
2258  amrex::BoxArray ba_col2d; // Their ground plane
2259  amrex::DistributionMapping dm_col;
2260  amrex::MultiFab hold_col; // No data; keep the copy metadata alive
2261  amrex::MultiFab hold_col2d;
2262  };
2263  amrex::Vector<PBLHColumns> m_pblh_columns;
2264  void define_pblh_columns (const int& lev,
2265  const amrex::BoxArray& ba,
2266  const amrex::DistributionMapping& dm);
2267  // Diagnostic-only provenance mask. This records the cell-centered source
2268  // path used by the SurfaceLayer scalar diagnostic path. It must not feed
2269  // back into flux calculations, boundary conditions, or model state.
2270  amrex::Vector<std::unique_ptr<amrex::MultiFab>> surface_diagnostic_source;
2271 
2272  // Lower-boundary SST and TSK time slices, absolute temperature [K]
2273  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_sst_lev;
2274  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_tsk_lev;
2275  amrex::Vector<amrex::Vector<amrex::iMultiFab*>> m_lmask_lev;
2276 
2277  // Coupled SST from an external ocean model, plus the per-cell coverage flag
2278  // that says whether the coupler actually supplied a value for that cell.
2279  // Both are owned by ERF and borrowed here, exactly like m_sst_lev. A null
2280  // m_coupled_sst_lev[lev] means "no coupled SST has arrived yet"; a zero entry in
2281  // m_coupled_sst_valid_lev means "this cell has no ocean donor", and in both cases the
2282  // lower-boundary SST/TSK value filled earlier in update_fluxes stands.
2283  amrex::Vector<amrex::MultiFab*> m_coupled_sst_lev;
2284  amrex::Vector<amrex::iMultiFab*> m_coupled_sst_valid_lev;
2285 
2286  // Current of the lower surface (most.surface_current, or from an ocean coupler): the
2287  // uniform value read from the inputs (zero with a coupler), and per level its x-component
2288  // on the x-faces and y-component on the y-faces of the plane k = klo. The surface layer
2289  // averages and applies the wind relative to it, held in m_rel_xvel and m_rel_yvel on the
2290  // velocities' boxes cut at m_rel_k_top, the highest plane the averages read; m_rel_src_ba
2291  // is the x-velocity layout they were made for.
2293  // Whether the current comes from an ocean coupler (set_coupled_current_active)
2295  amrex::Real m_surface_current_const[2] = {zero, zero};
2296  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_surface_current_x;
2297  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_surface_current_y;
2298  // The coupler's last current per level (update_coupled_surface_current), borrowed; null
2299  // until it first calls
2300  amrex::Vector<const amrex::MultiFab*> m_coupled_current_east_lev;
2301  amrex::Vector<const amrex::MultiFab*> m_coupled_current_north_lev;
2302  amrex::Vector<const amrex::iMultiFab*> m_coupled_current_covered_lev;
2303  amrex::Vector<const amrex::MultiFab*> m_coupled_current_cos_lev;
2304  amrex::Vector<const amrex::MultiFab*> m_coupled_current_sin_lev;
2305  // The coupler's grid orientation on the layout fill_surface_current reads, ghost cells
2306  // filled, per level; remade only when the orientation or that layout changes, since the
2307  // coupler passes the same orientation on every call
2308  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_coupled_rotation_lev;
2309  amrex::Vector<const amrex::MultiFab*> m_coupled_rotation_src_lev;
2310  amrex::Vector<amrex::BoxArray> m_coupled_rotation_src_ba_lev;
2311  amrex::Vector<amrex::DistributionMapping> m_coupled_rotation_src_dm_lev;
2312  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_rel_xvel;
2313  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_rel_yvel;
2314  amrex::Vector<int> m_rel_k_top;
2315  amrex::Vector<amrex::BoxArray> m_rel_src_ba;
2316  // External absolute skin temperature per level (set_skin_temperature), owned
2317  // by the caller; nullptr where none is supplied.
2318  amrex::Vector<const amrex::MultiFab*> m_skin_tsurf_lev;
2319  // What that skin hands over for its surface mixing ratio, with how to form it from that
2320  // field, per level (set_skin_moisture), so setting one level never changes another's.
2321  amrex::Vector<const amrex::MultiFab*> m_skin_moisture_lev;
2323  amrex::Vector<amrex::Real> m_skin_moisture_hs_lev;
2324  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_lsm_data_lev;
2325  amrex::Vector<amrex::Vector<amrex::MultiFab*>> m_lsm_flux_lev;
2326  amrex::Vector<std::string> m_lsm_data_name;
2327  amrex::Vector<std::string> m_lsm_flux_name;
2328  amrex::Vector<amrex::MultiFab*> m_Hwave_lev;
2329  amrex::Vector<amrex::MultiFab*> m_Lwave_lev;
2330  amrex::Vector<amrex::MultiFab*> m_eddyDiffs_lev;
2331 
2332  bool m_update_k_rans = false;
2333  bool m_k_moist_buoyancy = false; // erf.rans_moist_buoyancy: the wall value of k from theta_v*
2334  amrex::Real inv_Cmu2 = zero;
2335  amrex::Real theta_ref = zero;
2336 };
2337 
2338 #endif /* SURFACELAYER_H */
int count
Definition: ERF_ActuatorSpreading.cpp:264
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
constexpr amrex::Real RdoCp
Definition: ERF_Constants.H:57
Defines EB Monin-Obukhov surface-layer flux functors.
Declares the embedded-boundary factory manager used by ERF levels.
pp_for_pert_vels get("prob_type", prob_type)
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
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)
similarity_funs read_most_similarity_funs(amrex::ParmParse &pp, const std::string &who="Surface layer")
Definition: ERF_MOSTUtils.H:379
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
AMREX_ASSERT_WITH_MESSAGE(wbar_cutoff_min > wbar_cutoff_max, "ERROR: wbar_cutoff_min < wbar_cutoff_max")
Definition: ERF_MOSTAverage.H:18
bool set_plane_average(const int &lev, const amrex::Vector< amrex::Real > &pavg)
Definition: ERF_MOSTAverage.H:336
void make_MOSTAverage_at_level(const int &lev, const amrex::Vector< amrex::MultiFab * > &vars_old, std::unique_ptr< amrex::MultiFab > &Theta_prim, std::unique_ptr< amrex::MultiFab > &Qv_prim, std::unique_ptr< amrex::MultiFab > &Qr_prim, std::unique_ptr< amrex::MultiFab > &z_phys_nd, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> *mapfac=nullptr)
Definition: ERF_MOSTAverage.cpp:174
amrex::Vector< amrex::Real > get_plane_average(const int &lev) const
Definition: ERF_MOSTAverage.H:322
bool do_time_averaging() const
Definition: ERF_MOSTAverage.H:286
amrex::MultiFab * get_zref(const int &lev) const
Definition: ERF_MOSTAverage.H:350
void set_time_avg_initialized(const int &lev)
Definition: ERF_MOSTAverage.H:310
int get_navg() const
Definition: ERF_MOSTAverage.H:291
const amrex::MultiFab * get_average(const int &lev, const int &comp) const
Definition: ERF_MOSTAverage.H:272
void update_field_ptrs(const int &lev, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_old, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Theta_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qv_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qr_prim)
Definition: ERF_MOSTAverage.cpp:474
bool time_avg_is_initialized(const int &lev) const
Definition: ERF_MOSTAverage.H:299
void set_velocity_field_ptrs(const int &lev, amrex::MultiFab *xvel, amrex::MultiFab *yvel)
Definition: ERF_MOSTAverage.H:120
Definition: ERF_SurfaceLayer.H:64
bool m_has_immersed_buildings
Definition: ERF_SurfaceLayer.H:2223
void build_relative_velocities(const int &lev, const amrex::MultiFab &xvel, const amrex::MultiFab &yvel)
Definition: ERF_SurfaceLayer.cpp:434
ThetaCalcType theta_type
Definition: ERF_SurfaceLayer.H:2136
int lmask_min_reduce(amrex::iMultiFab &lmask, const int &nghost)
Definition: ERF_SurfaceLayer.H:1945
amrex::Vector< std::string > m_lsm_data_name
Definition: ERF_SurfaceLayer.H:2326
bool m_include_wstar
Definition: ERF_SurfaceLayer.H:2158
amrex::Real interpolate_sfc_column(const amrex::Real &time, int col) const
Definition: ERF_SurfaceLayer.cpp:1054
bool specified_rho_surf
Definition: ERF_SurfaceLayer.H:2182
void set_q_surf(const int &lev, const amrex::Real qsurf)
Definition: ERF_SurfaceLayer.H:1889
void update_fluxes(const int &lev, const double &elapsed_time, const double &elapsed_time_since_start_low, amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, const std::unique_ptr< amrex::MultiFab > &walldist, int max_iters=100)
Definition: ERF_SurfaceLayer.cpp:505
bool m_rotate
Definition: ERF_SurfaceLayer.H:2152
amrex::Real sfc_tflux
Definition: ERF_SurfaceLayer.H:2205
amrex::Vector< const amrex::MultiFab * > m_coupled_current_east_lev
Definition: ERF_SurfaceLayer.H:2300
PBLHeightCalcType pblh_type
Definition: ERF_SurfaceLayer.H:2140
void fill_tsurf_with_coupled_sst(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:3139
amrex::Vector< amrex::Vector< amrex::iMultiFab * > > m_lmask_lev
Definition: ERF_SurfaceLayer.H:2275
double m_final_low_time
Definition: ERF_SurfaceLayer.H:2155
amrex::Vector< PlanarBoundary > m_planar_bndry
Definition: ERF_SurfaceLayer.H:2241
amrex::iMultiFab * get_lmask(const int &lev)
Definition: ERF_SurfaceLayer.H:1937
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_surface_current_x
Definition: ERF_SurfaceLayer.H:2296
bool use_moisture
Definition: ERF_SurfaceLayer.H:2192
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_surface_current_y
Definition: ERF_SurfaceLayer.H:2297
amrex::Vector< PBLHColumns > m_pblh_columns
Definition: ERF_SurfaceLayer.H:2263
amrex::MultiFab * get_q_surf(const int &lev)
Definition: ERF_SurfaceLayer.H:1881
bool m_has_lsm_tsurf
Definition: ERF_SurfaceLayer.H:2194
amrex::MultiFab * get_w_star(const int &lev)
Definition: ERF_SurfaceLayer.H:1624
amrex::Real m_Cq
Definition: ERF_SurfaceLayer.H:2215
amrex::Vector< const eb_ * > m_eb_vec
Definition: ERF_SurfaceLayer.H:2221
const amrex::MultiFab * get_surface_current(const int &lev, int comp) const
Definition: ERF_SurfaceLayer.H:1605
amrex::Gpu::DeviceScalar< erf_surface_temperature::ConversionFailure > m_surface_conversion_failure
Definition: ERF_SurfaceLayer.H:2151
RoughCalcType rough_type_land
Definition: ERF_SurfaceLayer.H:2138
bool have_variable_sea_roughness() const
Definition: ERF_SurfaceLayer.H:1930
void update_pblh(const int &lev, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars, amrex::MultiFab *z_phys_cc, const MoistureComponentIndices &moisture_indices)
Definition: ERF_SurfaceLayer.cpp:3590
void fill_pblh_from_coarser(const int &lev)
Definition: ERF_SurfaceLayer.cpp:3912
SurfaceModel * m_surf_model
Definition: ERF_SurfaceLayer.H:2226
amrex::Real sfc_qflux
Definition: ERF_SurfaceLayer.H:2204
amrex::Vector< std::unique_ptr< amrex::MultiFab > > t_surf
Definition: ERF_SurfaceLayer.H:2235
bool m_use_sfc_fluxes
Definition: ERF_SurfaceLayer.H:2200
amrex::Real z0_const
Definition: ERF_SurfaceLayer.H:2159
bool use_sfc_fluxes() const
Definition: ERF_SurfaceLayer.H:1860
amrex::Vector< std::unique_ptr< amrex::MultiFab > > surface_diagnostic_source
Definition: ERF_SurfaceLayer.H:2270
amrex::Real cnk_a
Definition: ERF_SurfaceLayer.H:2183
amrex::Real m_Ch
Definition: ERF_SurfaceLayer.H:2214
void fill_qsurf_with_skin_moisture(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
void fill_tsurf_with_skin_temperature(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:3350
amrex::Real surf_temp
Definition: ERF_SurfaceLayer.H:2169
void update_mac_ptrs(const int &lev, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars_old, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Theta_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qv_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qr_prim)
Definition: ERF_SurfaceLayer.H:1530
bool m_lsm_tsurf_is_theta
Definition: ERF_SurfaceLayer.H:2196
void set_sfc_params_from_file_fluxes(const int &lev, const amrex::MultiFab &cons_in, const amrex::Real &tflux_in, const amrex::Real &qflux_in, const amrex::Real &sfc_tau)
Definition: ERF_SurfaceLayer.cpp:2401
void compute_pblh(const int &lev, amrex::Vector< amrex::Vector< amrex::MultiFab >> &vars, amrex::MultiFab *z_phys_cc, const PBLHeightEstimator &est, const MoistureComponentIndices &moisture_indice)
amrex::Vector< int > m_skin_moisture_resistances_lev
Definition: ERF_SurfaceLayer.H:2322
amrex::Vector< std::unique_ptr< amrex::MultiFab > > q_star
Definition: ERF_SurfaceLayer.H:2232
int m_lsm_tsurf_indx
Definition: ERF_SurfaceLayer.H:2195
double m_start_low_time
Definition: ERF_SurfaceLayer.H:2154
void impose_SurfaceLayer_bcs(const int &lev, amrex::Vector< const amrex::MultiFab * > mfs, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Tau_lev, amrex::MultiFab *xheat_flux, amrex::MultiFab *yheat_flux, amrex::MultiFab *zheat_flux, amrex::MultiFab *xqv_flux, amrex::MultiFab *yqv_flux, amrex::MultiFab *zqv_flux, const amrex::MultiFab *z_phys, amrex::MultiFab *mf_surface_fluxes=nullptr)
Definition: ERF_SurfaceLayer.cpp:1408
const similarity_funs & similarity_functions() const
Definition: ERF_SurfaceLayer.H:1660
amrex::Real m_surface_current_const[2]
Definition: ERF_SurfaceLayer.H:2295
bool mac_avg_is_initialized(const int &lev) const
Definition: ERF_SurfaceLayer.H:1695
bool mac_avg_is_time_averaged() const
Definition: ERF_SurfaceLayer.H:1683
amrex::Real rico_qsat_z0
Definition: ERF_SurfaceLayer.H:2190
bool m_has_lsm_fluxes
Definition: ERF_SurfaceLayer.H:2193
bool surf_temp_is_absolute
Definition: ERF_SurfaceLayer.H:2172
bool m_update_k_rans
Definition: ERF_SurfaceLayer.H:2332
void check_similarity_funs_use() const
Definition: ERF_SurfaceLayer.H:512
amrex::Vector< amrex::MultiFab * > m_Lwave_lev
Definition: ERF_SurfaceLayer.H:2329
void fill_qsurf_with_qsat(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd)
const amrex::MultiFab * get_z0(const int &lev) const
Definition: ERF_SurfaceLayer.H:1925
amrex::MultiFab * get_olen(const int &lev)
Definition: ERF_SurfaceLayer.H:1657
bool computes_w_star() const
Definition: ERF_SurfaceLayer.H:1630
amrex::Vector< amrex::MultiFab > z_0
Definition: ERF_SurfaceLayer.H:2186
amrex::Real surf_moist_flux
Definition: ERF_SurfaceLayer.H:2177
void fill_qsurf_with_qsat(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
bool smooth_flow_visc
Definition: ERF_SurfaceLayer.H:2184
amrex::Vector< const amrex::MultiFab * > m_skin_moisture_lev
Definition: ERF_SurfaceLayer.H:2321
amrex::Vector< const amrex::MultiFab * > m_coupled_current_north_lev
Definition: ERF_SurfaceLayer.H:2301
bool m_bulk_use_fixed_tflux
Definition: ERF_SurfaceLayer.H:2217
void get_lsm_tsurf(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:2995
RoughCalcType rough_type_sea
Definition: ERF_SurfaceLayer.H:2139
void update_coupled_sst_ptr(const int lev, amrex::MultiFab *sst_ptr, amrex::iMultiFab *valid_ptr)
Definition: ERF_SurfaceLayer.H:2001
std::string skin_temperature_conflict() const
Definition: ERF_SurfaceLayer.H:1760
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_rel_yvel
Definition: ERF_SurfaceLayer.H:2313
void init_tke_from_ustar(const int &lev, amrex::MultiFab &cons, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, const amrex::Real tkefac=one, const amrex::Real zscale=amrex::Real(700.0))
Definition: ERF_SurfaceLayer.cpp:3986
std::string m_pp_prefix
Definition: ERF_SurfaceLayer.H:2146
amrex::Real surface_sum(const int &lev, const amrex::MultiFab &mf, int comp=0) const
Definition: ERF_SurfaceLayer.cpp:1017
amrex::Vector< const amrex::iMultiFab * > m_coupled_current_covered_lev
Definition: ERF_SurfaceLayer.H:2302
amrex::Real surf_moist
Definition: ERF_SurfaceLayer.H:2176
similarity_funs m_sfuns
stable-side similarity functions (most.stable_functions)
Definition: ERF_SurfaceLayer.H:2141
amrex::Vector< std::unique_ptr< amrex::MultiFab > > w_star
Definition: ERF_SurfaceLayer.H:2230
bool m_ignore_sst
Definition: ERF_SurfaceLayer.H:2219
int get_num_mac_avg() const
Definition: ERF_SurfaceLayer.H:1688
amrex::Real roughness() const
Definition: ERF_SurfaceLayer.H:1855
amrex::Vector< amrex::Vector< amrex::Real > > sfc
Definition: ERF_SurfaceLayer.H:2203
std::string skin_moisture_conflict() const
Definition: ERF_SurfaceLayer.H:1819
amrex::MultiFab * get_u_star(const int &lev)
Definition: ERF_SurfaceLayer.H:1617
double m_low_time_interval
Definition: ERF_SurfaceLayer.H:2156
amrex::Vector< const amrex::MultiFab * > m_coupled_current_sin_lev
Definition: ERF_SurfaceLayer.H:2304
bool m_use_lsm_skin_temperature
Definition: ERF_SurfaceLayer.H:2197
SurfaceFluxView get_lsm_flux_view(const int &lev, const amrex::MFIter &mfi) const
Definition: ERF_SurfaceLayer.cpp:2368
amrex::Vector< const amrex::MultiFab * > m_coupled_rotation_src_lev
Definition: ERF_SurfaceLayer.H:2309
amrex::GpuArray< int, AMREX_SPACEDIM *2 > m_surface_layer_faces
Definition: ERF_SurfaceLayer.H:2153
void compute_fluxes(const int &lev, const int &max_iters, amrex::MultiFab &cons_in, const FluxIter &most_flux, bool is_land)
bool set_mac_plane_avg(const int &lev, const amrex::Vector< amrex::Real > &pavg)
Definition: ERF_SurfaceLayer.H:1727
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_lsm_data_lev
Definition: ERF_SurfaceLayer.H:2324
void set_t_surf(const int &lev, const amrex::Real tsurf)
Definition: ERF_SurfaceLayer.H:1874
amrex::Real custom_qstar
Definition: ERF_SurfaceLayer.H:2180
amrex::Vector< std::unique_ptr< amrex::MultiFab > > u_star
Definition: ERF_SurfaceLayer.H:2229
void update_tsk_ptr(const int lev, const int itime, amrex::MultiFab *tsk_ptr)
Definition: ERF_SurfaceLayer.H:1984
bool surf_model_fluxes
Definition: ERF_SurfaceLayer.H:2228
amrex::Real custom_rhosurf
Definition: ERF_SurfaceLayer.H:2181
amrex::Vector< std::unique_ptr< amrex::MultiFab > > q_surf
Definition: ERF_SurfaceLayer.H:2236
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_sst_lev
Definition: ERF_SurfaceLayer.H:2273
bool m_use_surface_current
Definition: ERF_SurfaceLayer.H:2292
FluxCalcType
Definition: ERF_SurfaceLayer.H:2105
@ MOENG
Moeng functional form.
@ BULK_COEFF
Bulk transfer coefficient functional form.
@ CUSTOM
Custom constant flux functional form.
@ ROTATE
Terrain rotation flux functional form.
void fill_tsurf_with_sst_and_tsk(const int &lev, const double &time, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:2572
amrex::Vector< amrex::BoxArray > m_rel_src_ba
Definition: ERF_SurfaceLayer.H:2315
amrex::Real m_rdOcp
Definition: ERF_SurfaceLayer.H:2224
amrex::Vector< amrex::DistributionMapping > m_coupled_rotation_src_dm_lev
Definition: ERF_SurfaceLayer.H:2311
MoistCalcType
Definition: ERF_SurfaceLayer.H:2119
@ SURFACE_MOISTURE
Surface Qv specified.
@ MOISTURE_FLUX
Qv-flux specified.
amrex::Real depth
Definition: ERF_SurfaceLayer.H:2185
amrex::Vector< amrex::MultiFab * > m_coupled_sst_lev
Definition: ERF_SurfaceLayer.H:2283
amrex::MultiFab * get_zref(const int &lev)
Definition: ERF_SurfaceLayer.H:1903
void fill_lateral_surface_parameter_ghosts(const int &lev, amrex::MultiFab *selected_field=nullptr)
Definition: ERF_SurfaceLayer.cpp:1289
amrex::Vector< const amrex::MultiFab * > m_skin_tsurf_lev
Definition: ERF_SurfaceLayer.H:2318
amrex::Vector< amrex::MultiFab * > m_Hwave_lev
Definition: ERF_SurfaceLayer.H:2328
void define_relative_velocities(const int &lev, const amrex::MultiFab &xvel, const amrex::MultiFab &yvel, int k_top)
Definition: ERF_SurfaceLayer.cpp:415
amrex::Real default_land_surf_moist
Definition: ERF_SurfaceLayer.H:2175
amrex::Vector< amrex::iMultiFab > m_lsm_held_columns
Definition: ERF_SurfaceLayer.H:2244
bool m_coupled_current_active
Definition: ERF_SurfaceLayer.H:2294
void update_sfc_time_index(const amrex::Real &time)
Definition: ERF_SurfaceLayer.cpp:1037
bool m_var_z0
Definition: ERF_SurfaceLayer.H:2187
amrex::Vector< amrex::iMultiFab * > m_coupled_sst_valid_lev
Definition: ERF_SurfaceLayer.H:2284
amrex::MultiFab * get_surface_diagnostic_source(const int &lev)
Definition: ERF_SurfaceLayer.H:1896
amrex::MultiFab * get_t_star(const int &lev)
Definition: ERF_SurfaceLayer.H:1643
bool rotates_surface_fluxes() const
Definition: ERF_SurfaceLayer.H:1866
void impose_SurfaceLayer_bcs_EB(const int &lev, amrex::Vector< const amrex::MultiFab * > mfs, amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab >>> &Tau_lev, amrex::MultiFab *xheat_flux, amrex::MultiFab *yheat_flux, amrex::MultiFab *zheat_flux, amrex::MultiFab *xqv_flux, amrex::MultiFab *yqv_flux, amrex::MultiFab *zqv_flux)
Definition: ERF_SurfaceLayer.cpp:1472
amrex::MultiFab * get_q_star(const int &lev)
Definition: ERF_SurfaceLayer.H:1650
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_lsm_flux_lev
Definition: ERF_SurfaceLayer.H:2325
amrex::Vector< amrex::Real > m_skin_moisture_hs_lev
Definition: ERF_SurfaceLayer.H:2323
amrex::Vector< int > m_rel_k_top
Definition: ERF_SurfaceLayer.H:2314
amrex::MultiFab * get_mac_avg_ptr(const int &lev, int comp)
Definition: ERF_SurfaceLayer.H:1710
amrex::Vector< amrex::Real > get_mac_plane_avg(const int &lev) const
Definition: ERF_SurfaceLayer.H:1718
void set_pblh(const int &lev, const amrex::MultiFab &pblh_in)
Definition: ERF_SurfaceLayer.cpp:3622
amrex::Vector< amrex::BoxArray > m_coupled_rotation_src_ba_lev
Definition: ERF_SurfaceLayer.H:2310
void fill_planar_boundary(const int &lev, amrex::MultiFab &mf)
Definition: ERF_SurfaceLayer.cpp:1000
PBLHeightCalcType
Definition: ERF_SurfaceLayer.H:2133
bool m_z0_given
Definition: ERF_SurfaceLayer.H:2161
amrex::MultiFab * get_pblh(const int &lev)
Definition: ERF_SurfaceLayer.H:1667
bool use_surface_model
Definition: ERF_SurfaceLayer.H:2227
void compute_SurfaceLayer_bcs_EB(const int &lev, amrex::Vector< const amrex::MultiFab * > mfs, amrex::Vector< amrex::Vector< std::unique_ptr< amrex::MultiFab >>> &Tau_lev, amrex::MultiFab *xheat_flux, amrex::MultiFab *yheat_flux, amrex::MultiFab *zheat_flux, amrex::MultiFab *xqv_flux, amrex::MultiFab *yqv_flux, amrex::MultiFab *zqv_flux, const FluxCalc &flux_comp)
amrex::Real rico_theta_z0
Definition: ERF_SurfaceLayer.H:2189
void fill_tsurf_with_sfc_sst(const int &lev, const double &time, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:2725
amrex::Real sfc_ustar
Definition: ERF_SurfaceLayer.H:2206
amrex::Real surf_temp_flux
Definition: ERF_SurfaceLayer.H:2174
amrex::Vector< amrex::Geometry > m_geom
Definition: ERF_SurfaceLayer.H:2147
amrex::Vector< std::unique_ptr< amrex::MultiFab > > t_star
Definition: ERF_SurfaceLayer.H:2231
int sfc_time_ind
Definition: ERF_SurfaceLayer.H:2202
void set_immersed_surface_params(const int &lev)
Definition: ERF_SurfaceLayer.cpp:288
void compute_SurfaceLayer_bcs(const int &lev, amrex::Vector< const amrex::MultiFab * > mfs, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Tau_lev, amrex::MultiFab *xheat_flux, amrex::MultiFab *yheat_flux, amrex::MultiFab *zheat_flux, amrex::MultiFab *xqv_flux, amrex::MultiFab *yqv_flux, amrex::MultiFab *zqv_flux, const amrex::MultiFab *z_phys, const FluxCalc &flux_comp, amrex::MultiFab *mf_surface_fluxes=nullptr)
bool computes_pblh() const
Definition: ERF_SurfaceLayer.H:1636
amrex::Real theta_ref
Definition: ERF_SurfaceLayer.H:2335
void set_skin_moisture(const int &lev, const amrex::MultiFab *moisture, bool use_resistances=false, amrex::Real hs=amrex::Real(0))
Definition: ERF_SurfaceLayer.H:1800
amrex::MultiFab * get_z0(const int &lev)
Definition: ERF_SurfaceLayer.H:1917
amrex::Vector< amrex::Vector< amrex::MultiFab * > > m_tsk_lev
Definition: ERF_SurfaceLayer.H:2274
void set_surface_layer_faces(const amrex::GpuArray< int, AMREX_SPACEDIM *2 > &active_faces)
Definition: ERF_SurfaceLayer.H:2078
void update_surf_temp(const double &time)
Definition: ERF_SurfaceLayer.H:1499
void set_coupled_current_active(const bool active)
Definition: ERF_SurfaceLayer.H:2026
void set_coupled_sst_active(const bool active)
Definition: ERF_SurfaceLayer.H:2016
amrex::Real custom_tstar
Definition: ERF_SurfaceLayer.H:2179
SurfaceLayer(amrex::Orientation face, const amrex::Vector< amrex::Geometry > &geom, bool &use_rot_surface_flux, std::string a_pp_prefix, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &Qv_prim, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &z_phys_nd, const amrex::Vector< amrex::Vector< amrex::Real >> &zlevels_stag, const MeshType &a_mesh_type, const TerrainType &a_terrain_type, const TurbChoice &a_turb_choice, amrex::Real a_rdOcp, double start_low_time, double final_low_time, double low_time_interval=0.0, const amrex::Vector< const eb_ * > &eb_vec={}, SurfaceModel *surf_model=nullptr, bool a_has_immersed_buildings=false)
Definition: ERF_SurfaceLayer.H:88
void inject_pblh_from_coarser(const int &lev, amrex::MultiFab &fine) const
Definition: ERF_SurfaceLayer.cpp:3927
static amrex::Vector< amrex::Vector< amrex::Real > > read_cols(const std::string &fname, const int skip_nlines=1)
Definition: ERF_SurfaceLayer.cpp:4255
void update_coupled_surface_current(const int &lev, const amrex::MultiFab &east, const amrex::MultiFab &north, const amrex::iMultiFab *covered, const amrex::MultiFab *cos_alpha, const amrex::MultiFab *sin_alpha)
Definition: ERF_SurfaceLayer.cpp:472
amrex::Real surf_heating_rate
Definition: ERF_SurfaceLayer.H:2173
RoughCalcType
Definition: ERF_SurfaceLayer.H:2125
FluxCalcType flux_type
Definition: ERF_SurfaceLayer.H:2135
MoistCalcType moist_type
Definition: ERF_SurfaceLayer.H:2137
bool use_surface_current() const
Definition: ERF_SurfaceLayer.H:1596
void fill_surface_current(const int &lev)
Definition: ERF_SurfaceLayer.cpp:347
amrex::Real m_bulk_fixed_tflux
Definition: ERF_SurfaceLayer.H:2218
bool m_use_sfc_sst
Definition: ERF_SurfaceLayer.H:2201
void compute_sfc_params_from_lsm_fluxes(const int &lev, amrex::MultiFab &cons_in)
Definition: ERF_SurfaceLayer.cpp:2446
void define_surface_current(const int &lev, const amrex::MultiFab &xvel, const amrex::MultiFab &yvel)
Definition: ERF_SurfaceLayer.cpp:325
amrex::Real inv_Cmu2
Definition: ERF_SurfaceLayer.H:2334
amrex::Vector< const amrex::MultiFab * > m_coupled_current_cos_lev
Definition: ERF_SurfaceLayer.H:2303
amrex::Vector< amrex::MultiFab * > m_eddyDiffs_lev
Definition: ERF_SurfaceLayer.H:2330
amrex::Real get_zref_min(const int &lev)
Definition: ERF_SurfaceLayer.H:1910
const amrex::MultiFab * get_mac_avg(const int &lev, int comp)
Definition: ERF_SurfaceLayer.H:1675
void make_SurfaceLayer_at_level(const int &lev, int nlevs, const amrex::Vector< amrex::MultiFab * > &mfv, std::unique_ptr< amrex::MultiFab > &Theta_prim, std::unique_ptr< amrex::MultiFab > &Qv_prim, std::unique_ptr< amrex::MultiFab > &Qr_prim, std::unique_ptr< amrex::MultiFab > &z_phys_nd, amrex::MultiFab *Hwave, amrex::MultiFab *Lwave, amrex::MultiFab *eddyDiffs, amrex::Vector< amrex::MultiFab * > lsm_data, amrex::Vector< std::string > lsm_data_name, amrex::Vector< amrex::MultiFab * > lsm_flux, amrex::Vector< std::string > lsm_flux_name, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &sst_lev, amrex::Vector< std::unique_ptr< amrex::MultiFab >> &tsk_lev, amrex::Vector< std::unique_ptr< amrex::iMultiFab >> &lmask_lev, const amrex::Vector< std::unique_ptr< amrex::MultiFab >> *mapfac=nullptr)
Definition: ERF_SurfaceLayer.H:549
void set_skin_temperature(const int &lev, const amrex::MultiFab *t_skin)
Definition: ERF_SurfaceLayer.H:1745
void fill_qsurf_with_skin_moisture(const int &lev, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd)
void fill_tsurf_with_abs_surf_temp(const int &lev, const double &time, const amrex::MultiFab &cons_in, const std::unique_ptr< amrex::MultiFab > &z_phys_nd, erf_surface_temperature::ConversionFailure *fail)
Definition: ERF_SurfaceLayer.cpp:3267
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_coupled_rotation_lev
Definition: ERF_SurfaceLayer.H:2308
void update_sst_ptr(const int lev, const int itime, amrex::MultiFab *sst_ptr)
Definition: ERF_SurfaceLayer.H:1972
amrex::Real custom_ustar
Definition: ERF_SurfaceLayer.H:2178
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_rel_xvel
Definition: ERF_SurfaceLayer.H:2312
amrex::Vector< std::unique_ptr< amrex::MultiFab > > olen
Definition: ERF_SurfaceLayer.H:2233
bool m_use_coupled_sst
Definition: ERF_SurfaceLayer.H:2211
amrex::Orientation m_face
Definition: ERF_SurfaceLayer.H:2145
amrex::MultiFab * get_t_surf(const int &lev)
Definition: ERF_SurfaceLayer.H:1734
amrex::Real m_Cd
Definition: ERF_SurfaceLayer.H:2213
void set_mac_avg_initialized(const int &lev)
Definition: ERF_SurfaceLayer.H:1702
amrex::Vector< std::unique_ptr< amrex::MultiFab > > pblh
Definition: ERF_SurfaceLayer.H:2234
amrex::Real default_land_surf_temp
Definition: ERF_SurfaceLayer.H:2162
bool m_k_moist_buoyancy
Definition: ERF_SurfaceLayer.H:2333
bool set_default_roughness(amrex::Real z0)
Definition: ERF_SurfaceLayer.H:1844
ThetaCalcType
Definition: ERF_SurfaceLayer.H:2113
@ SURFACE_TEMPERATURE
Surface temperature specified.
@ HEAT_FLUX
Heat-flux specified.
void define_pblh_columns(const int &lev, const amrex::BoxArray &ba, const amrex::DistributionMapping &dm)
Definition: ERF_SurfaceLayer.cpp:3843
void read_custom_roughness(const int &lev, const std::string &fname)
Definition: ERF_SurfaceLayer.cpp:4071
TerrainType m_terrain_type
Definition: ERF_SurfaceLayer.H:2222
amrex::Vector< std::string > m_lsm_flux_name
Definition: ERF_SurfaceLayer.H:2327
MOSTAverage m_ma
Definition: ERF_SurfaceLayer.H:2225
Interface between ERF and land-surface and urban models.
Definition: ERF_SurfaceModel.H:60
bool are_fluxes()
Returns whether the interface exports fluxes rather than MOST variables.
Definition: ERF_SurfaceModel.H:391
@ tsurf
Definition: ERF_SLM.H:29
@ ng
Definition: ERF_Morrison.H:51
@ xvel
Definition: ERF_IndexDefines.H:232
@ cons
Definition: ERF_IndexDefines.H:231
@ yvel
Definition: ERF_IndexDefines.H:233
Definition: ERF_SurfaceLayer.H:32
bool planar_sources_supported_for_terrain(TerrainType terrain_type, bool use_sst, bool use_tsk, bool use_coupled_sst, bool has_lsm_tsurf, bool has_lsm_fluxes)
Definition: ERF_SurfaceLayer.H:35
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool sst_ignored(const bool ignore_sst_requested, const bool have_tsk)
Definition: ERF_SurfaceTemperature.H:189
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real to_plot_value(SurfaceDiagnosticSource source) noexcept
Definition: ERF_SurfaceDiagnosticSource.H:45
The moisture data carried by the active microphysics scheme.
Definition: ERF_DataStruct.H:279
Non-owning views of surface fluxes for one tile.
Definition: ERF_SurfaceModel.H:24
Definition: ERF_SurfaceLayer.H:2252
bool needed
Definition: ERF_SurfaceLayer.H:2255
amrex::BoxArray ba
Definition: ERF_SurfaceLayer.H:2253
amrex::BoxArray ba_col2d
Definition: ERF_SurfaceLayer.H:2258
amrex::MultiFab hold_col2d
Definition: ERF_SurfaceLayer.H:2261
bool full_height
Definition: ERF_SurfaceLayer.H:2256
amrex::MultiFab hold_col
Definition: ERF_SurfaceLayer.H:2260
amrex::DistributionMapping dm_col
Definition: ERF_SurfaceLayer.H:2259
amrex::DistributionMapping dm
Definition: ERF_SurfaceLayer.H:2254
amrex::BoxArray ba_col
Definition: ERF_SurfaceLayer.H:2257
Definition: ERF_TurbStruct.H:129
RANSType rans_type
Selected RANS closure.
Definition: ERF_TurbStruct.H:960
amrex::Real theta_ref
Reference potential temperature for stable stratification.
Definition: ERF_TurbStruct.H:947
bool rans_moist_buoyancy
Definition: ERF_TurbStruct.H:1000
bool dirichlet_k
Whether TKE uses Dirichlet boundary treatment.
Definition: ERF_TurbStruct.H:962
amrex::Real Cmu0
One-equation RANS Cmu0 coefficient.
Definition: ERF_TurbStruct.H:936
Definition: ERF_SurfaceTemperature.H:268
Definition: ERF_MOSTUtils.H:84
AMREX_GPU_HOST_DEVICE bool uses_qnse() const
Definition: ERF_MOSTUtils.H:334