ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_CloudChamber.H
Go to the documentation of this file.
1 #ifndef ERF_CLOUD_CHAMBER_H_
2 #define ERF_CLOUD_CHAMBER_H_
3 
4 #include <AMReX_Array.H>
5 #include <AMReX_Geometry.H>
6 #include <AMReX_ParmParse.H>
7 #include <AMReX_REAL.H>
8 
9 #include "ERF_Constants.H"
11 #include "ERF_DataStruct.H"
12 #include "ERF_EOS.H"
13 #include "ERF_MicrophysicsUtils.H"
16 
17 #include <array>
18 #include <cmath>
19 #include <limits>
20 #include <optional>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace erf_cloud_chamber {
26 
27 /**
28  * Cloud Chamber wall metadata. This is deliberately a small, trivially
29  * copyable description: it can be captured by an AMReX device kernel without
30  * virtual dispatch. The per-channel contract supports resolved molecular,
31  * fixed bulk-aerodynamic, and neutral roughness-log transfer, with MOST
32  * coefficient generation on the supported horizontal faces.
33  */
34 enum class WallFace : unsigned char { XLo, XHi, YLo, YHi, ZLo, ZHi };
35 
36 /**
37  * @brief Profile data used by the device initializer; no wall transport is exposed here.
38  */
40  bool active = false;
41  bool has_moisture = false;
43  amrex::Real initial_temperature_bottom = amrex::Real(0.0);
44  amrex::Real initial_temperature_top = amrex::Real(0.0);
45  amrex::Real initial_relative_humidity = amrex::Real(0.0);
46  amrex::Real temperature_perturbation_amplitude = amrex::Real(0.0);
47  amrex::Real theta_bottom = amrex::Real(0.0); // legacy_theta_qv only
48  amrex::Real theta_top = amrex::Real(0.0); // legacy_theta_qv only
49  amrex::Real theta_perturbation_amplitude = amrex::Real(0.0); // legacy
50  amrex::Real qv_bottom = amrex::Real(0.0); // legacy_theta_qv only
51  amrex::Real qv_top = amrex::Real(0.0); // legacy_theta_qv only
52  amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> prob_lo = {};
53  amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> prob_hi = {};
54 };
55 
57  // Only the full solver-side parser can publish walls. The default (inactive)
58  // config has no boundary, rather than a plausible-looking array of zero speeds.
59  [[nodiscard]] const erf_wall_thermodynamics::Boundary& transport_boundary () const &
60  {
61  return m_boundary.value();
62  }
64 
66  {
67  return {m_bands[0].view(), m_bands[1].view(), m_bands[2].view(), m_bands[3].view()};
68  }
70 
71  static bool has_bulk_scalar_wall (const erf_wall_thermodynamics::Boundary& walls) noexcept
72  {
74  for (const auto& wall : walls) {
75  if ((wall.heat_is_conductive() && wall.heat.model == ScalarModel::BulkAero) ||
76  (wall.vapor_is_wet() && wall.vapor.model == ScalarModel::BulkAero)) {
77  return true;
78  }
79  }
80  return false;
81  }
82 
83  bool has_wall_rate_channel () const
84  {
85  for (const auto& wall : transport_boundary()) {
86  if (wall.has_wall_rate_channel()) { return true; }
87  }
88  return false;
89  }
90 
91 private:
92  friend Config parse_config (const amrex::Geometry&, MoistureType, const DiffChoice&);
93  std::optional<erf_wall_thermodynamics::Boundary> m_boundary;
94  std::array<SidewallTemperatureBands, 4> m_bands;
95 };
96 
97 enum class InitializationMode : unsigned char {
100 };
101 
102 /**
103  * @brief Contract used to validate initialization parameter consistency.
104  */
107  // True for any active moisture model, including noncondensing transport.
108  bool has_moisture = false;
112 };
113 
114 /**
115  * @brief Validate the initialization contract and return an error message if violated.
116  * @param[in] contract The contract to validate.
117  * @return Error message string, empty if valid.
118  */
119 inline std::string
121 {
122  switch (contract.mode) {
124  if (contract.has_moisture) {
125  if (!contract.has_initial_relative_humidity) {
126  return "Cloud Chamber: physical moist initialization requires prob.initial_relative_humidity";
127  }
128  } else if (contract.has_initial_relative_humidity) {
129  return "Cloud Chamber: prob.initial_relative_humidity requires an active moisture model";
130  }
131  if (contract.has_legacy_profile_keys) {
132  return "Cloud Chamber: physical_temperature_rh cannot be combined with legacy theta/qv profile keys";
133  }
134  break;
136  if (contract.has_physical_profile_keys) {
137  return "Cloud Chamber: legacy_theta_qv cannot be combined with physical temperature/RH profile keys";
138  }
139  break;
140  }
141 
142  return {};
143 }
144 
145 /**
146  * @brief Return whether a moisture type is supported by the Cloud Chamber.
147  *
148  * This is intentionally an allowlist. In particular, spectral SBM remains
149  * outside the chamber's supported moisture configurations even though it is
150  * a valid ERF moisture type.
151  */
152 inline bool
153 supports_moisture_model (const MoistureType moisture_type) noexcept
154 {
155  switch (moisture_type) {
156  case MoistureType::None:
157  case MoistureType::SatAdj:
158  case MoistureType::MoistNoCondensation:
159  case MoistureType::Kessler_NoRain:
160  case MoistureType::Kessler:
161  case MoistureType::SAM_NoPrecip_NoIce:
162  case MoistureType::SAM_NoIce:
163  case MoistureType::SAM:
164  case MoistureType::WSM6:
165  case MoistureType::WDM6:
166  case MoistureType::Morrison_NoIce:
167  case MoistureType::Morrison:
168  case MoistureType::SuperDroplets:
169  return true;
170  case MoistureType::SBM:
171  return false;
172  }
173  return false;
174 }
175 
176 /**
177  * @brief Compute a linear profile between two values.
178  * @param[in] bottom Value at the lower boundary.
179  * @param[in] top Value at the upper boundary.
180  * @param[in] coordinate Current coordinate.
181  * @param[in] lower Lower boundary coordinate.
182  * @param[in] length Length of the domain.
183  * @return Interpolated value.
184  */
185 AMREX_FORCE_INLINE
186 AMREX_GPU_HOST_DEVICE
187 amrex::Real
188 linear_profile (amrex::Real bottom, amrex::Real top,
189  amrex::Real coordinate, amrex::Real lower,
190  amrex::Real length) noexcept
191 {
192  return bottom + (top - bottom) * (coordinate - lower) / length;
193 }
194 
195 /**
196  * @brief Compute a deterministic 3D sine perturbation.
197  * @param[in] x x-coordinate.
198  * @param[in] y y-coordinate.
199  * @param[in] z z-coordinate.
200  * @param[in] lo Lower boundary coordinates.
201  * @param[in] length Domain lengths.
202  * @param[in] amplitude Perturbation amplitude.
203  * @return Perturbation value at the given coordinates.
204  */
205 AMREX_FORCE_INLINE
206 AMREX_GPU_HOST_DEVICE
207 amrex::Real
208 deterministic_perturbation (amrex::Real x, amrex::Real y, amrex::Real z,
209  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& lo,
210  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& length,
211  amrex::Real amplitude) noexcept
212 {
213  constexpr amrex::Real pi = amrex::Real(3.1415926535897932384626433832795);
214  const amrex::Real sx = std::sin(amrex::Real(2.0) * pi * (x - lo[0]) / length[0]);
215  const amrex::Real sy = std::sin(amrex::Real(2.0) * pi * (y - lo[1]) / length[1]);
216  const amrex::Real sz = std::sin(pi * (z - lo[2]) / length[2]);
217  return amplitude * sx * sy * sz;
218 }
219 
220 /**
221  * @brief Discrete derivative ratio for the staggered initial velocity field.
222  *
223  * The ratio makes the finite-volume x/y divergence cancel on rectangular
224  * grids. A one-cell horizontal direction has an identically zero sine
225  * pattern, so the perturbation is zero rather than dividing by a zero
226  * discrete derivative factor.
227  */
228 AMREX_FORCE_INLINE
229 AMREX_GPU_HOST_DEVICE
230 amrex::Real
231 staggered_velocity_v_scale (amrex::Real lx, amrex::Real ly,
232  amrex::Real dx, amrex::Real dy,
233  int nx, int ny) noexcept
234 {
235  if (nx <= 1 || ny <= 1) { return amrex::Real(0.0); }
236  if (lx == ly && dx == dy) { return amrex::Real(1.0); }
237  constexpr amrex::Real pi = amrex::Real(3.1415926535897932384626433832795);
238  const amrex::Real kx_d = std::sin(pi * dx / lx) / dx;
239  const amrex::Real ky_d = std::sin(pi * dy / ly) / dy;
240  return ky_d == amrex::Real(0.0) ? amrex::Real(0.0) : kx_d / ky_d;
241 }
242 
243 /**
244  * @brief Evaluate the x-face component of the Cloud Chamber initial velocity.
245  */
246 AMREX_FORCE_INLINE
247 AMREX_GPU_HOST_DEVICE
248 amrex::Real
250  amrex::Real x, amrex::Real y, amrex::Real z,
251  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& lo,
252  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& length,
253  amrex::Real U_0, int nx, int ny) noexcept
254 {
255  if (nx <= 1 || ny <= 1) { return amrex::Real(0.0); }
256  constexpr amrex::Real pi = amrex::Real(3.1415926535897932384626433832795);
257  const amrex::Real sx = std::sin(pi * (x - lo[0]) / length[0]);
258  const amrex::Real sz = std::sin(pi * (z - lo[2]) / length[2]);
259  return U_0 * sx * sx *
260  std::sin(amrex::Real(2.0) * pi * (y - lo[1]) / length[1]) * sz * sz;
261 }
262 
263 /**
264  * @brief Evaluate the y-face component of the Cloud Chamber initial velocity.
265  */
266 AMREX_FORCE_INLINE
267 AMREX_GPU_HOST_DEVICE
268 amrex::Real
270  amrex::Real x, amrex::Real y, amrex::Real z,
271  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& lo,
272  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& length,
273  amrex::Real U_0, amrex::Real v_scale, int nx, int ny) noexcept
274 {
275  if (nx <= 1 || ny <= 1) { return amrex::Real(0.0); }
276  constexpr amrex::Real pi = amrex::Real(3.1415926535897932384626433832795);
277  const amrex::Real sy = std::sin(pi * (y - lo[1]) / length[1]);
278  const amrex::Real sz = std::sin(pi * (z - lo[2]) / length[2]);
279  return -U_0 * v_scale *
280  std::sin(amrex::Real(2.0) * pi * (x - lo[0]) / length[0]) *
281  sy * sy * sz * sz;
282 }
283 
284 /**
285  * @brief The Cloud Chamber initial vertical velocity perturbation.
286  */
287 AMREX_FORCE_INLINE
288 AMREX_GPU_HOST_DEVICE
289 amrex::Real
291 {
292  return amrex::Real(0.0);
293 }
294 
295 /**
296  * @brief Compute the potential temperature at a given location.
297  * @param[in] config Configuration parameters.
298  * @param[in] x x-coordinate.
299  * @param[in] y y-coordinate.
300  * @param[in] z z-coordinate.
301  * @return Potential temperature at the specified location.
302  */
303 AMREX_FORCE_INLINE
304 AMREX_GPU_HOST_DEVICE
305 amrex::Real
306 theta_at (const Initialization& config, amrex::Real x, amrex::Real y,
307  amrex::Real z) noexcept
308 {
309  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> length = {
310  config.prob_hi[0] - config.prob_lo[0],
311  config.prob_hi[1] - config.prob_lo[1],
312  config.prob_hi[2] - config.prob_lo[2]};
313  const amrex::Real bottom = config.physical_initialization ?
314  config.initial_temperature_bottom : config.theta_bottom;
315  const amrex::Real top = config.physical_initialization ?
316  config.initial_temperature_top : config.theta_top;
317  const amrex::Real amplitude = config.physical_initialization ?
318  config.temperature_perturbation_amplitude : config.theta_perturbation_amplitude;
319  return linear_profile(bottom, top, z,
320  config.prob_lo[2], length[2]) +
322  amplitude);
323 }
324 
325 /**
326  * @brief Compute the water vapor mixing ratio at a given height.
327  * @param[in] config Configuration parameters.
328  * @param[in] z Vertical coordinate.
329  * @return Vapor mixing ratio at the specified height.
330  */
331 AMREX_FORCE_INLINE
332 AMREX_GPU_HOST_DEVICE
333 amrex::Real
334 qv_at (const Initialization& config, amrex::Real z) noexcept
335 {
336  return linear_profile(config.qv_bottom, config.qv_top, z,
337  config.prob_lo[2],
338  config.prob_hi[2] - config.prob_lo[2]);
339 }
340 
341 /**
342  * @brief Convert relative humidity to water vapor mixing ratio.
343  * @param[in] temperature_K Temperature in Kelvin.
344  * @param[in] pressure_Pa Pressure in Pascals.
345  * @param[in] relative_humidity Relative humidity [0,1].
346  * @return Water vapor mixing ratio.
347  */
348 AMREX_FORCE_INLINE
349 AMREX_GPU_HOST_DEVICE
350 amrex::Real
351 vapor_mixing_ratio_from_relative_humidity (amrex::Real temperature_K,
352  amrex::Real pressure_Pa,
353  amrex::Real relative_humidity) noexcept
354 {
355  // erf_esatw returns hPa; convert saturation and vapor pressure to Pa before
356  // applying the dry-air vapor mixing-ratio definition.
357  const amrex::Real es_Pa = erf_esatw(temperature_K) * amrex::Real(100.0);
358  const amrex::Real ev_Pa = relative_humidity * es_Pa;
359  return RdoRv * ev_Pa / (pressure_Pa - ev_Pa);
360 }
361 
362 inline bool finite (amrex::Real value) noexcept
363 {
364  return std::isfinite(static_cast<double>(value));
365 }
366 
367 inline void require_finite (const std::string& key, amrex::Real value)
368 {
369  if (!finite(value)) {
370  amrex::Error("Cloud Chamber: " + key + " must be finite");
371  }
372 }
373 
374 inline std::string face_name (WallFace face)
375 {
376  switch (face) {
377  case WallFace::XLo: return "xlo";
378  case WallFace::XHi: return "xhi";
379  case WallFace::YLo: return "ylo";
380  case WallFace::YHi: return "yhi";
381  case WallFace::ZLo: return "zlo";
382  case WallFace::ZHi: return "zhi";
383  }
384  return "unknown";
385 }
386 
387 inline bool valid_wall_wetness (amrex::Real rh) noexcept
388 {
389  return finite(rh) && rh >= amrex::Real(0.0) && rh <= amrex::Real(1.0);
390 }
391 
392 inline std::string
393 parse_wall_wetness (amrex::ParmParse& pp, const std::string& face,
395 {
396  if (!pp.contains("wetness")) { return {}; }
398  return "Cloud Chamber: " + face + ".wetness is only valid on physical walls with moisture = wet";
399  }
400  amrex::Real rh;
401  pp.get("wetness", rh);
402  if (!valid_wall_wetness(rh)) {
403  return "Cloud Chamber: " + face + ".wetness must be a finite relative humidity fraction in [0,1]";
404  }
405  wall.wetness = rh;
406  return {};
407 }
408 
412  bool heat_model_specified = false;
413  bool vapor_model_specified = false;
418  bool z0_m_specified = false;
419  bool z0_h_specified = false;
420  bool z0_q_specified = false;
421  std::string momentum_model;
422  std::string heat_model;
423  std::string vapor_model;
424  std::string coefficient_source;
425  amrex::Real momentum_coefficient = amrex::Real(0.0);
426  amrex::Real heat_coefficient = amrex::Real(0.0);
427  amrex::Real vapor_coefficient = amrex::Real(0.0);
428  amrex::Real z0_m = amrex::Real(0.0);
429  amrex::Real z0_h = amrex::Real(0.0);
430  amrex::Real z0_q = amrex::Real(0.0);
431 };
432 
433 inline WallTransferContract read_wall_transfer_contract (const amrex::ParmParse& pp,
434  const std::string& face)
435 {
436  WallTransferContract contract;
437  auto selector = [&](const char* key, std::string& value) {
438  if (!pp.query(key, value)) { return false; }
439  value = amrex::toLower(value);
440  return true;
441  };
442  std::string aggregate;
443  contract.legacy_aggregate_specified = selector("wall_transfer_model", aggregate);
444  if (contract.legacy_aggregate_specified && aggregate != "resolved_molecular") {
445  amrex::Error("Cloud Chamber: " + face + ".wall_transfer_model only accepts "
446  "resolved_molecular; select other models with the per-channel keys");
447  }
448  contract.momentum_model_specified = selector("momentum_transfer_model", contract.momentum_model);
449  contract.heat_model_specified = selector("heat_transfer_model", contract.heat_model);
450  contract.vapor_model_specified = selector("vapor_transfer_model", contract.vapor_model);
451  contract.coefficient_source_specified = selector("coefficient_source", contract.coefficient_source);
452  auto coefficient = [&](const char* key, amrex::Real& value) {
453  if (!pp.query(key, value)) { return false; }
454  require_finite(face + "." + key, value);
455  return true;
456  };
457  contract.momentum_coefficient_specified = coefficient("C_D", contract.momentum_coefficient);
458  contract.heat_coefficient_specified = coefficient("C_H", contract.heat_coefficient);
459  contract.vapor_coefficient_specified = coefficient("C_E", contract.vapor_coefficient);
460  contract.z0_m_specified = coefficient("z0_m", contract.z0_m);
461  contract.z0_h_specified = coefficient("z0_h", contract.z0_h);
462  contract.z0_q_specified = coefficient("z0_q", contract.z0_q);
463  return contract;
464 }
465 
466 inline bool is_bulk_model (const std::string& model)
467 {
468  return model == "bulk_aero" || model == "bulk_aero_plus_molecular";
469 }
470 
471 inline std::string
473  const std::string& face)
474 {
475  const bool momentum_neutral = contract.momentum_model_specified &&
476  (contract.momentum_model == "neutral_roughness_log" ||
477  contract.momentum_model == "law_of_wall_momentum");
478  const bool momentum_bulk = contract.momentum_model_specified &&
479  is_bulk_model(contract.momentum_model);
480  const bool heat_neutral = contract.heat_model_specified &&
481  contract.heat_model == "neutral_roughness_log";
482  const bool vapor_neutral = contract.vapor_model_specified &&
483  contract.vapor_model == "neutral_roughness_log";
484  const bool any_neutral =
485  momentum_neutral || heat_neutral || vapor_neutral;
486 
487  const bool any_channel_key =
488  contract.momentum_model_specified ||
489  contract.heat_model_specified ||
490  contract.vapor_model_specified ||
491  contract.coefficient_source_specified ||
492  contract.heat_coefficient_specified ||
493  contract.vapor_coefficient_specified ||
495  contract.z0_m_specified ||
496  contract.z0_h_specified ||
497  contract.z0_q_specified;
498  if (contract.legacy_aggregate_specified && any_channel_key) {
499  return "Cloud Chamber: " + face +
500  " cannot combine wall_transfer_model with per-channel wall model keys";
501  }
502 
503  if (contract.momentum_model_specified &&
504  contract.momentum_model != "resolved_noslip" &&
505  !momentum_bulk && !momentum_neutral) {
506  return "Cloud Chamber: " + face +
507  ".momentum_transfer_model must be resolved_noslip, bulk_aero, bulk_aero_plus_molecular, "
508  "neutral_roughness_log, or law_of_wall_momentum";
509  }
510 
511  const bool heat_bulk = contract.heat_model_specified &&
512  is_bulk_model(contract.heat_model);
513  const bool vapor_bulk = contract.vapor_model_specified &&
514  is_bulk_model(contract.vapor_model);
515  if (contract.heat_model_specified &&
516  contract.heat_model != "resolved_molecular" && !heat_bulk && !heat_neutral) {
517  return "Cloud Chamber: " + face +
518  ".heat_transfer_model must be resolved_molecular, bulk_aero, bulk_aero_plus_molecular, or neutral_roughness_log";
519  }
520  if (contract.vapor_model_specified &&
521  contract.vapor_model != "resolved_molecular" && !vapor_bulk && !vapor_neutral) {
522  return "Cloud Chamber: " + face +
523  ".vapor_transfer_model must be resolved_molecular, bulk_aero, bulk_aero_plus_molecular, or neutral_roughness_log";
524  }
525 
526  const bool bulk_active = heat_bulk || vapor_bulk || momentum_bulk;
527  if ((contract.momentum_model == "bulk_aero_plus_molecular" ||
528  contract.heat_model == "bulk_aero_plus_molecular" ||
529  contract.vapor_model == "bulk_aero_plus_molecular") && contract.coefficient_source != "fixed") {
530  return "Cloud Chamber: " + face + " bulk_aero_plus_molecular requires coefficient_source = fixed";
531  }
532  const bool most_provider = contract.coefficient_source_specified &&
533  contract.coefficient_source == "most";
534  if (most_provider && !(face == "zlo" || face == "zhi")) {
535  return "Cloud Chamber: " + face +
536  " supports coefficient_source = most only on zlo or zhi";
537  }
538  if ((any_neutral || (bulk_active && most_provider)) &&
539  !contract.z0_m_specified) {
540  return "Cloud Chamber: " + face +
541  " requires z0_m for the active neutral/MOST wall-transfer channels";
542  }
543  if ((heat_neutral || (heat_bulk && most_provider)) &&
544  !contract.z0_h_specified) {
545  return "Cloud Chamber: " + face +
546  " requires z0_h for neutral roughness-log heat or MOST heat";
547  }
548  if ((vapor_neutral || (vapor_bulk && most_provider)) &&
549  !contract.z0_q_specified) {
550  return "Cloud Chamber: " + face +
551  " requires z0_q for neutral roughness-log vapor or MOST vapor";
552  }
553  if (contract.z0_m_specified &&
554  !(any_neutral || (bulk_active && most_provider))) {
555  return "Cloud Chamber: " + face +
556  ".z0_m requires a neutral_roughness_log or MOST channel";
557  }
558  if (contract.z0_h_specified &&
559  !(heat_neutral || (heat_bulk && most_provider))) {
560  return "Cloud Chamber: " + face +
561  ".z0_h requires neutral_roughness_log heat or MOST heat";
562  }
563  if (contract.z0_q_specified &&
564  !(vapor_neutral || (vapor_bulk && most_provider))) {
565  return "Cloud Chamber: " + face +
566  ".z0_q requires neutral_roughness_log vapor or MOST vapor";
567  }
568  if ((contract.z0_m_specified &&
569  (!finite(contract.z0_m) || contract.z0_m <= amrex::Real(0.0))) ||
570  (contract.z0_h_specified &&
571  (!finite(contract.z0_h) || contract.z0_h <= amrex::Real(0.0))) ||
572  (contract.z0_q_specified &&
573  (!finite(contract.z0_q) || contract.z0_q <= amrex::Real(0.0)))) {
574  return "Cloud Chamber: " + face +
575  " active roughness lengths z0_m, z0_h, and z0_q must be finite and positive";
576  }
577 
578  if (bulk_active) {
579  if (!contract.coefficient_source_specified ||
580  (contract.coefficient_source != "fixed" &&
581  contract.coefficient_source != "most")) {
582  return "Cloud Chamber: " + face +
583  " active bulk_aero channels require coefficient_source = fixed or most";
584  }
585  } else if (contract.coefficient_source_specified) {
586  return "Cloud Chamber: " + face +
587  ".coefficient_source requires a bulk_aero or bulk_aero_plus_molecular channel";
588  }
589 
590  if (momentum_bulk && !most_provider &&
591  !contract.momentum_coefficient_specified) {
592  return "Cloud Chamber: " + face +
593  " requires C_D when momentum_transfer_model = " + contract.momentum_model +
594  " with coefficient_source = fixed";
595  }
596  if (momentum_bulk && most_provider && contract.momentum_coefficient_specified) {
597  return "Cloud Chamber: " + face +
598  ".C_D is not valid with coefficient_source = most";
599  }
600  if (heat_bulk && !most_provider && !contract.heat_coefficient_specified) {
601  return "Cloud Chamber: " + face +
602  " requires C_H when heat_transfer_model = " + contract.heat_model +
603  " with coefficient_source = fixed";
604  }
605  if (heat_bulk && most_provider && contract.heat_coefficient_specified) {
606  return "Cloud Chamber: " + face +
607  ".C_H is not valid with coefficient_source = most";
608  }
609  if (vapor_bulk && !most_provider && !contract.vapor_coefficient_specified) {
610  return "Cloud Chamber: " + face +
611  " requires C_E when vapor_transfer_model = " + contract.vapor_model +
612  " with coefficient_source = fixed";
613  }
614  if (vapor_bulk && most_provider && contract.vapor_coefficient_specified) {
615  return "Cloud Chamber: " + face +
616  ".C_E is not valid with coefficient_source = most";
617  }
618  if (contract.momentum_coefficient_specified && !momentum_bulk) {
619  return "Cloud Chamber: " + face +
620  ".C_D requires momentum_transfer_model = bulk_aero or bulk_aero_plus_molecular";
621  }
622  if (contract.heat_coefficient_specified && !heat_bulk) {
623  return "Cloud Chamber: " + face +
624  ".C_H requires heat_transfer_model = bulk_aero or bulk_aero_plus_molecular";
625  }
626  if (contract.vapor_coefficient_specified && !vapor_bulk) {
627  return "Cloud Chamber: " + face +
628  ".C_E requires vapor_transfer_model = bulk_aero or bulk_aero_plus_molecular";
629  }
630  if ((contract.momentum_coefficient_specified &&
631  (!finite(contract.momentum_coefficient) ||
632  contract.momentum_coefficient < amrex::Real(0.0))) ||
633  (contract.heat_coefficient_specified &&
634  (!finite(contract.heat_coefficient) ||
635  contract.heat_coefficient < amrex::Real(0.0))) ||
636  (contract.vapor_coefficient_specified &&
637  (!finite(contract.vapor_coefficient) ||
638  contract.vapor_coefficient < amrex::Real(0.0)))) {
639  return "Cloud Chamber: " + face +
640  " bulk coefficients C_D, C_H, and C_E must be finite and nonnegative";
641  }
642  return {};
643 }
644 
645 // Wet-wall saturation needs T_wall independently of sensible heat transfer.
646 // A dry adiabatic wall does not need T_wall, even for MOST drag.
647 inline std::string
648 parse_wall_heat (amrex::ParmParse& pp, const std::string& face, bool physical,
649  erf_wall_thermodynamics::FaceWall& wall, const amrex::Geometry& geom,
650  SidewallTemperatureBands* sidewall_bands)
651 {
652  using namespace erf_wall_thermodynamics;
653  const bool edges = pp.contains("temperature_height_edges"), values = pp.contains("temperature_values");
654  if (edges || values) {
655  const auto prefix = "Cloud Chamber: " + face + ": ";
656  if (!physical || !sidewall_bands) { return prefix + "temperature bands require a physical x or y sidewall"; }
657  if (!(edges && values)) { return prefix + "temperature_height_edges and temperature_values must be supplied together"; }
658  if (pp.contains("temperature")) { return prefix + "temperature and temperature bands are mutually exclusive"; }
659  }
660  if (!physical) {
661  if (pp.contains("heat") || pp.contains("thermal")) {
662  return "Cloud Chamber: " + face + ".heat and its .thermal alias require physical_temperature_rh";
663  }
664  return {};
665  }
666 
667  std::string heat = "conductive";
668  const bool explicit_heat = pp.query("heat", heat);
669  heat = amrex::toLower(heat);
670  if (heat != "conductive" && heat != "adiabatic") {
671  return "Cloud Chamber: " + face + ".heat must be conductive or adiabatic";
672  }
673  std::string alias;
674  if (pp.query("thermal", alias)) {
675  alias = amrex::toLower(alias);
676  if (alias == "fixed_temperature") { alias = "conductive"; }
677  else if (alias != "adiabatic") {
678  return "Cloud Chamber: " + face + ".thermal must be fixed_temperature or adiabatic";
679  }
680  if (explicit_heat && heat != alias) {
681  return "Cloud Chamber: conflicting " + face + ".heat and " + face + ".thermal";
682  }
683  heat = alias;
684  }
685 
686  const bool conductive = heat == "conductive";
687  ThermalBoundary thermal;
688  thermal.mode = conductive ? ThermalMode::FixedPhysicalTemperature : ThermalMode::Adiabatic;
689  if (edges) {
690  amrex::Vector<amrex::Real> z, temperatures;
691  pp.getarr("temperature_height_edges", z);
692  pp.getarr("temperature_values", temperatures);
693  const auto error = sidewall_bands->define(std::move(z), temperatures, geom);
694  if (!error.empty()) { return "Cloud Chamber: " + face + ": " + error; }
695  // Uniform bands use the scalar path. For nonuniform bands each active
696  // scalar flux uses its panel temperature; MOST cannot run on this face.
697  thermal.temperature_K = temperatures.front();
698  } else if (pp.query("temperature", thermal.temperature_K)) {
699  if (!finite(thermal.temperature_K) || thermal.temperature_K <= 0) {
700  return "Cloud Chamber: " + face + ".temperature must be finite and positive Kelvin";
701  }
702  } else if (conductive || wall.vapor_is_wet()) {
703  return "Cloud Chamber: missing required physical " + face +
704  ".temperature (needed for conductive heat or wet-wall saturation)";
705  }
706  wall.thermal = thermal;
707  return {};
708 }
709 
710 inline std::string
712  const std::string& face)
713 {
714  if (wall.heat_uses_most() && wall.vapor_uses_most() && wall.heat.z0 != wall.vapor.z0) {
715  return "Cloud Chamber: " + face +
716  " coupled heat/vapor MOST requires z0_h == z0_q because the "
717  "prescribed-temperature bulk-Richardson closure uses one scalar "
718  "similarity resistance; z0_h=" +
719  std::to_string(static_cast<double>(wall.heat.z0)) +
720  ", z0_q=" +
721  std::to_string(static_cast<double>(wall.vapor.z0));
722  }
723  return {};
724 }
725 
727 {
728  amrex::ParmParse pp_geom("geometry");
729  std::vector<int> periodic;
730  pp_geom.getarr("is_periodic", periodic);
731  if (periodic.size() != AMREX_SPACEDIM) {
732  amrex::Error("Cloud Chamber: geometry.is_periodic must provide three values");
733  }
734  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
735  if (periodic[d] != 0) {
736  amrex::Error("Cloud Chamber: geometry.is_periodic must be 0 0 0");
737  }
738  }
739 }
740 
742 {
743  amrex::ParmParse pp_erf("erf");
744  std::string value;
745  if (pp_erf.query("terrain_type", value) &&
746  amrex::toLower(value) != "none") {
747  amrex::Error("Cloud Chamber: erf.terrain_type must be None; terrain and embedded boundaries are out of scope");
748  }
749  value.clear();
750  if (pp_erf.query("buildings_type", value) &&
751  amrex::toLower(value) != "none") {
752  amrex::Error("Cloud Chamber: erf.buildings_type must be None; immersed buildings are out of scope");
753  }
754  value.clear();
755  if (pp_erf.query("mesh_type", value) &&
756  amrex::toLower(value) != "constantdz") {
757  amrex::Error("Cloud Chamber: erf.mesh_type must be ConstantDz");
758  }
759  // A stretched or listed vertical grid makes the mesh StretchedDz without erf.mesh_type
760  if (SolverChoice::mesh_type != MeshType::ConstantDz) {
761  amrex::Error("Cloud Chamber: the vertical grid must be uniform (no erf.grid_stretching_ratio, "
762  "erf.initial_dz or erf.terrain_z_levels)");
763  }
764 }
765 
766 inline void reject_unsupported_face_keys (amrex::ParmParse& pp,
767  const std::string& face)
768 {
769  if (pp.contains("physical_temperature") || pp.contains("relative_humidity") ||
770  pp.contains("rh") || pp.contains("roughness") ||
771  pp.contains("roughness_length") || pp.contains("z0") ||
772  pp.contains("most") || pp.contains("MOST")) {
773  amrex::Error("Cloud Chamber: " + face +
774  " must use temperature and moisture for physical walls; "
775  "roughness aliases and legacy aliases are unsupported");
776  }
777 }
778 
779 inline std::string
782  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dx)
783 {
784  for (int n = 0; n < 2 * AMREX_SPACEDIM; ++n) {
785  const auto& wall = walls[n];
786  const int dir = n / 2;
787  const amrex::Real z_ref = amrex::Real(0.5) * dx[dir];
788  const std::string face = face_name(static_cast<WallFace>(n));
789  const auto check = [&](bool active, const char* key,
790  amrex::Real z0) -> std::string {
791  if (!active) { return {}; }
792  if (!finite(z_ref) || !finite(z0) ||
793  !(z0 > amrex::Real(0.0)) || !(z_ref > z0)) {
794  return "Cloud Chamber: " + face + " " + key +
795  " requires 0 < " + key + " < z_ref; " + key + "=" +
796  std::to_string(static_cast<double>(z0)) +
797  ", z_ref=" +
798  std::to_string(static_cast<double>(z_ref));
799  }
800  return {};
801  };
802  // An adiabatic face never evaluates its heat resistance. Keep the
803  // existing geometry contract for configured vapor models, even if dry.
804  using namespace erf_wall_thermodynamics;
805  const bool heat_rough = wall.heat_uses_most() ||
806  (wall.heat_is_conductive() && wall.heat.model == ScalarModel::NeutralRoughnessLog);
807  const bool vapor_rough = wall.vapor.model == ScalarModel::NeutralRoughnessLog ||
808  (wall.vapor.model == ScalarModel::BulkAero &&
809  wall.vapor.provider == CoefficientProvider::MOST);
810  const auto momentum_error = check(
811  wall.momentum.model == MomentumModel::NeutralRoughnessLog ||
812  wall.momentum_uses_most() || heat_rough || vapor_rough,
813  "z0_m", wall.momentum.z0_m);
814  if (!momentum_error.empty()) { return momentum_error; }
815  const auto heat_error = check(
816  heat_rough,
817  "z0_h", wall.heat.z0);
818  if (!heat_error.empty()) { return heat_error; }
819  const auto vapor_error = check(
820  vapor_rough,
821  "z0_q", wall.vapor.z0);
822  if (!vapor_error.empty()) { return vapor_error; }
823  }
824  return {};
825 }
826 
827 inline void
830  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dx)
831 {
832  const auto error = wall_roughness_geometry_error(walls, dx);
833  if (!error.empty()) { amrex::Error(error); }
834 }
835 
836 inline Initialization parse_initialization (const amrex::Real* problo,
837  const amrex::Real* probhi,
838  const MoistureType selected_moisture_model)
839 {
841  config.active = true;
842  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
843  config.prob_lo[d] = problo[d];
844  config.prob_hi[d] = probhi[d];
845  require_finite("geometry extent", config.prob_lo[d]);
846  require_finite("geometry extent", config.prob_hi[d]);
847  if (!(config.prob_hi[d] > config.prob_lo[d])) {
848  amrex::Error("Cloud Chamber: geometry.prob_hi must exceed geometry.prob_lo in every direction");
849  }
850  }
851 
852  amrex::ParmParse pp_erf("erf");
853  CompressibilityType compressibility_type = CompressibilityType::FullyCompressible;
854  int anelastic = 0;
855  if (pp_erf.query("anelastic", anelastic)) {
856  if (anelastic == 1) { compressibility_type = CompressibilityType::Anelastic; }
857  } else {
858  pp_erf.query_enum_case_insensitive("compressibility_type", compressibility_type);
859  }
860  // The chamber initializer corrects only rho theta and the moisture at the fixed base-state
861  // density, so it needs the anelastic equations; PseudoIncompressible is not supported
862  if (compressibility_type != CompressibilityType::Anelastic) {
863  amrex::Error("Cloud Chamber: erf.compressibility_type = Anelastic is required "
864  "(FullyCompressible and PseudoIncompressible are not supported)");
865  }
866  bool use_gravity = false;
867  if (!pp_erf.query("use_gravity", use_gravity) || !use_gravity) {
868  amrex::Error("Cloud Chamber: erf.use_gravity = true is required");
869  }
870 
871  amrex::ParmParse pp_amr("amr");
872  int max_level = 0;
873  pp_amr.query("max_level", max_level);
874  if (max_level != 0) {
875  amrex::Error("Cloud Chamber: amr.max_level must be 0");
876  }
879 
880  amrex::ParmParse pp_prob("prob");
881  amrex::Real p_inf = p_0;
882  if (pp_prob.query("p_inf", p_inf)) {
883  require_finite("prob.p_inf", p_inf);
884  const amrex::Real pressure_tolerance = amrex::Real(1.0e-6) * p_0;
885  if (std::abs(p_inf - p_0) > pressure_tolerance) {
886  amrex::Error("Cloud Chamber: prob.p_inf must match ERF's p_0 = 100000 Pa within 1e-6 relative tolerance");
887  }
888  }
889 
890  if (!supports_moisture_model(selected_moisture_model)) {
891  amrex::Error("Cloud Chamber: selected moisture model is unsupported; use an Eulerian model from the supported moisture allowlist");
892  }
893  config.has_moisture = selected_moisture_model != MoistureType::None;
894  if (!config.has_moisture && pp_erf.contains("moisture_model")) {
895  amrex::Error("Cloud Chamber: omit erf.moisture_model for dry mode instead of explicitly selecting None");
896  }
897  if (!config.has_moisture &&
898  (pp_prob.contains("qv_bottom") || pp_prob.contains("qv_top"))) {
899  amrex::Error("Cloud Chamber: qv_bottom/qv_top require an active moisture model");
900  }
901 
902  std::string init_mode;
903  pp_prob.get("thermodynamic_initialization", init_mode);
904  init_mode = amrex::toLower(init_mode);
905  if (init_mode == "physical_temperature_rh") {
906  config.physical_initialization = true;
907  std::string init_type;
908  pp_erf.get("init_type", init_type);
909  if (amrex::toLower(init_type) != "constantdensity") {
910  amrex::Error("Cloud Chamber: physical initialization requires the existing HSE-backed erf.init_type = ConstantDensity path");
911  }
912  bool vert_implicit = false;
913  pp_erf.query("vert_implicit", vert_implicit);
914  if (vert_implicit) {
915  amrex::Error("Cloud Chamber: physical wall transfer requires erf.vert_implicit = false");
916  }
917  pp_prob.get("initial_temperature_bottom", config.initial_temperature_bottom);
918  pp_prob.get("initial_temperature_top", config.initial_temperature_top);
919  const bool has_initial_relative_humidity = pp_prob.contains("initial_relative_humidity");
920  auto physical_contract_error = [&](bool has_legacy_profile_keys) {
921  const InitializationContract contract {
923  config.has_moisture,
924  has_initial_relative_humidity,
925  has_legacy_profile_keys,
926  false};
927  return initialization_contract_error(contract);
928  };
929  std::string contract_error = physical_contract_error(false);
930  if (!contract_error.empty()) {
931  amrex::Error(contract_error);
932  }
933  if (config.has_moisture) {
934  pp_prob.get("initial_relative_humidity", config.initial_relative_humidity);
935  }
936  pp_prob.query("temperature_perturbation_amplitude",
937  config.temperature_perturbation_amplitude);
938  const bool has_legacy_profile_keys = pp_prob.contains("theta_bottom") ||
939  pp_prob.contains("theta_top") ||
940  pp_prob.contains("theta_perturbation_amplitude") ||
941  pp_prob.contains("qv_bottom") ||
942  pp_prob.contains("qv_top");
943  contract_error = physical_contract_error(has_legacy_profile_keys);
944  if (!contract_error.empty()) {
945  amrex::Error(contract_error);
946  }
947  require_finite("prob.initial_temperature_bottom", config.initial_temperature_bottom);
948  require_finite("prob.initial_temperature_top", config.initial_temperature_top);
949  require_finite("prob.temperature_perturbation_amplitude",
950  config.temperature_perturbation_amplitude);
951  if (config.initial_temperature_bottom <= amrex::Real(0.0) ||
952  config.initial_temperature_top <= amrex::Real(0.0)) {
953  amrex::Error("Cloud Chamber: physical initial temperatures must be positive");
954  }
955  if (config.has_moisture) {
956  require_finite("prob.initial_relative_humidity", config.initial_relative_humidity);
957  if (config.initial_relative_humidity < amrex::Real(0.0) ||
958  config.initial_relative_humidity > amrex::Real(1.0)) {
959  amrex::Error("Cloud Chamber: initial_relative_humidity must lie in [0,1]");
960  }
961  }
962  } else if (init_mode == "legacy_theta_qv") {
963  config.physical_initialization = false;
964  const InitializationContract contract {
966  false,
967  false,
968  false,
969  pp_prob.contains("initial_temperature_bottom") ||
970  pp_prob.contains("initial_temperature_top") ||
971  pp_prob.contains("initial_relative_humidity") ||
972  pp_prob.contains("temperature_perturbation_amplitude")};
973  const std::string contract_error = initialization_contract_error(contract);
974  if (!contract_error.empty()) {
975  amrex::Error(contract_error);
976  }
977  pp_prob.get("theta_bottom", config.theta_bottom);
978  pp_prob.get("theta_top", config.theta_top);
979  pp_prob.query("theta_perturbation_amplitude",
980  config.theta_perturbation_amplitude);
981  } else {
982  amrex::Error("Cloud Chamber: prob.thermodynamic_initialization must be physical_temperature_rh or legacy_theta_qv");
983  }
984  std::string perturbation_mode = "deterministic_sine";
985  pp_prob.query("perturbation_mode", perturbation_mode);
986  if (amrex::toLower(perturbation_mode) != "deterministic_sine") {
987  amrex::Error("Cloud Chamber: prob.perturbation_mode must be deterministic_sine");
988  }
989  if (!config.physical_initialization) {
990  require_finite("prob.theta_bottom", config.theta_bottom);
991  require_finite("prob.theta_top", config.theta_top);
992  require_finite("prob.theta_perturbation_amplitude",
993  config.theta_perturbation_amplitude);
994  if (config.theta_bottom <= amrex::Real(0.0) ||
995  config.theta_top <= amrex::Real(0.0)) {
996  amrex::Error("Cloud Chamber: theta profile values must be positive");
997  }
998  }
999 
1000  if (config.has_moisture) {
1001  if (!config.physical_initialization) {
1002  pp_prob.get("qv_bottom", config.qv_bottom);
1003  pp_prob.get("qv_top", config.qv_top);
1004  require_finite("prob.qv_bottom", config.qv_bottom);
1005  require_finite("prob.qv_top", config.qv_top);
1006  if (config.qv_bottom < amrex::Real(0.0) ||
1007  config.qv_top < amrex::Real(0.0)) {
1008  amrex::Error("Cloud Chamber: qv profile values must be nonnegative");
1009  }
1010  }
1011  }
1012 
1013  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
1014  const amrex::Real length = config.prob_hi[d] - config.prob_lo[d];
1015  if (!(length > amrex::Real(0.0)) || !finite(length)) {
1016  amrex::Error("Cloud Chamber: every rectangular dimension must be finite and positive");
1017  }
1018  }
1019  return config;
1020 }
1021 
1022 // Set up one physical face using the already parsed ConstantAlpha solver choice (no SGS).
1023 // Validate a selected combined channel even on an impermeable wall, just as for C_E;
1024 // only an active channel gets a speed. Ordinary bulk/resolved inputs retain their defaults.
1025 inline std::string set_wall_transport_speeds (
1027  const DiffChoice& dc, amrex::Real dx_inv, const std::string& face)
1028 {
1029  using namespace erf_wall_thermodynamics;
1030  const auto positive = [](amrex::Real x) { return finite(x) && x > 0; };
1031  const auto error = [&](const char* channel, const char* key) {
1032  return "Cloud Chamber: " + face + "." + channel + "_transfer_model = "
1033  "bulk_aero_plus_molecular requires finite positive " + key;
1034  };
1035  const auto inv_distance = amrex::Real(2) * dx_inv;
1036  if (contract.momentum_model == "bulk_aero_plus_molecular") {
1037  if (!positive(dc.dynamic_viscosity)) { return error("momentum", "dynamic_viscosity"); }
1038  if (!positive(dc.rho0_trans)) { return error("momentum", "rho0_trans"); }
1039  const auto speed = (dc.dynamic_viscosity / dc.rho0_trans) * inv_distance;
1040  if (!positive(speed)) {
1041  return error("momentum", "(dynamic_viscosity/rho0_trans) / half-cell distance");
1042  }
1043  wall.momentum.molecular_speed = speed;
1044  } else if (wall.momentum.model == MomentumModel::ResolvedNoSlip &&
1045  positive(dc.dynamic_viscosity) && positive(dc.rho0_trans)) {
1046  // ComputeStrain_N uses a prescribed wall value and two interior velocities.
1047  // Only the latter vary in the wall-stress row; the normal velocity is prescribed too.
1048  const auto w = WallGradientCoeffsUniform();
1049  wall.momentum.resolved_row_rate = (std::abs(w.c_0) + std::abs(w.c_1)) *
1050  (dc.dynamic_viscosity / dc.rho0_trans) * dx_inv * dx_inv;
1051  }
1052  const auto scalar = [&](ScalarTransfer& transfer, const std::string& model, bool active,
1053  amrex::Real alpha, const char* channel, const char* key) -> std::string {
1054  if (model == "bulk_aero_plus_molecular") {
1055  if (!positive(alpha)) { return error(channel, key); }
1056  if (active) {
1057  const auto speed = alpha * inv_distance;
1058  if (!positive(speed)) { return error(channel, "molecular transfer speed"); }
1059  transfer.molecular_speed = speed;
1060  }
1061  } else if (active && transfer.model == ScalarModel::ResolvedMolecular) {
1062  // Guard metadata only: resolved flux already includes this molecular exchange.
1063  transfer.resolved_speed = alpha * inv_distance;
1064  }
1065  return {};
1066  };
1067  const auto heat_error = scalar(wall.heat, contract.heat_model,
1068  wall.heat_is_conductive(), dc.alpha_T, "heat", "alpha_T");
1069  if (!heat_error.empty()) { return heat_error; }
1070  return scalar(wall.vapor, contract.vapor_model,
1071  wall.vapor_is_wet(), dc.alpha_C, "vapor", "alpha_C");
1072 }
1073 
1074 inline Config parse_config (const amrex::Geometry& geom, MoistureType selected_moisture_model,
1075  const DiffChoice& dc)
1076 {
1077  Config config;
1078  static_cast<Initialization&>(config) = parse_initialization(
1079  geom.ProbLo(), geom.ProbHi(), selected_moisture_model);
1080  if (config.physical_initialization && dc.molec_diff_type != MolecDiffType::ConstantAlpha) {
1081  amrex::Error("Cloud Chamber: physical wall transfer requires erf.molec_diff_type = ConstantAlpha");
1082  }
1083  // max_level == 0 and ConstantDz were checked above. This is the only geometry
1084  // from which the RHS, timestep and roughness validation get their face metadata.
1086  const auto dx_inv = geom.InvCellSizeArray();
1089  for (int n = 0; n < 2 * AMREX_SPACEDIM; ++n) {
1090  const std::string face = face_name(faces[n]);
1091  amrex::ParmParse pp_face(face.c_str());
1092  reject_unsupported_face_keys(pp_face, face);
1093  std::string type;
1094  if (!pp_face.query("type", type) ||
1095  amrex::toLower(type) != "noslipwall") {
1096  amrex::Error("Cloud Chamber: " + face + ".type must be NoSlipWall");
1097  }
1098  auto& wall = walls[n];
1099  auto transfer_contract = read_wall_transfer_contract(pp_face, face);
1100  const std::string transfer_error =
1101  wall_transfer_contract_error(transfer_contract, face);
1102  if (!transfer_error.empty()) {
1103  amrex::Error(transfer_error);
1104  }
1105  const bool any_channel_key =
1106  transfer_contract.momentum_model_specified ||
1107  transfer_contract.heat_model_specified ||
1108  transfer_contract.vapor_model_specified ||
1109  transfer_contract.coefficient_source_specified ||
1110  transfer_contract.momentum_coefficient_specified ||
1111  transfer_contract.heat_coefficient_specified ||
1112  transfer_contract.vapor_coefficient_specified ||
1113  transfer_contract.z0_m_specified ||
1114  transfer_contract.z0_h_specified ||
1115  transfer_contract.z0_q_specified;
1116  if (!config.physical_initialization && any_channel_key) {
1117  amrex::Error("Cloud Chamber: per-channel wall transfer keys require physical temperature initialization");
1118  }
1119  if (config.physical_initialization) {
1120  if (pp_face.contains("theta") || pp_face.contains("qv") ||
1121  pp_face.contains("theta_grad") || pp_face.contains("qv_grad")) {
1122  amrex::Error("Cloud Chamber: physical walls cannot combine temperature/moisture with legacy theta/qv wall keys");
1123  }
1124  std::string moisture;
1125  pp_face.get("moisture", moisture);
1126  moisture = amrex::toLower(moisture);
1127  if (moisture == "dry") {
1129  } else if (moisture == "wet") {
1131  if (!config.has_moisture) {
1132  amrex::Error("Cloud Chamber: wet walls require an active moisture model");
1133  }
1134  } else {
1135  amrex::Error("Cloud Chamber: " + face + ".moisture must be dry or wet");
1136  }
1137  if (transfer_contract.momentum_model == "law_of_wall_momentum") {
1138  transfer_contract.momentum_model = "neutral_roughness_log";
1139  }
1140  wall.momentum.model =
1141  transfer_contract.momentum_model == "neutral_roughness_log" ?
1143  (is_bulk_model(transfer_contract.momentum_model) ?
1146  wall.heat.model =
1147  is_bulk_model(transfer_contract.heat_model) ?
1149  (transfer_contract.heat_model == "neutral_roughness_log" ?
1152  wall.vapor.model =
1153  is_bulk_model(transfer_contract.vapor_model) ?
1155  (transfer_contract.vapor_model == "neutral_roughness_log" ?
1158  wall.momentum.z0_m = transfer_contract.z0_m;
1159  wall.momentum.C_D = transfer_contract.momentum_coefficient;
1160  wall.heat.z0 = transfer_contract.z0_h;
1161  wall.vapor.z0 = transfer_contract.z0_q;
1162  if (wall.momentum.model == erf_wall_thermodynamics::MomentumModel::BulkAero) {
1163  wall.momentum.provider = transfer_contract.coefficient_source == "most" ?
1166  }
1167  if (wall.heat.model == erf_wall_thermodynamics::ScalarModel::BulkAero) {
1168  wall.heat.provider = transfer_contract.coefficient_source == "most" ?
1171  wall.heat.coefficient = transfer_contract.heat_coefficient;
1172  }
1173  if (wall.vapor.model == erf_wall_thermodynamics::ScalarModel::BulkAero) {
1174  wall.vapor.provider = transfer_contract.coefficient_source == "most" ?
1177  wall.vapor.coefficient = transfer_contract.vapor_coefficient;
1178  }
1179  } else {
1180  if (!pp_face.query("theta", wall.thermal.temperature_K)) {
1181  amrex::Error("Cloud Chamber: missing required " + face + ".theta");
1182  }
1184  wall.moisture = config.has_moisture ?
1187  if (config.has_moisture && !pp_face.contains("qv")) {
1188  amrex::Error("Cloud Chamber: legacy cloudy walls require " + face + ".qv");
1189  }
1190  }
1191  const auto heat_error = parse_wall_heat(pp_face, face, config.physical_initialization, wall,
1192  geom, n < 4 ? &config.m_bands[n] : nullptr);
1193  if (!heat_error.empty()) { amrex::Error(heat_error); }
1194  const auto coupled_error = wall_coupled_most_roughness_error(wall, face);
1195  if (!coupled_error.empty()) { amrex::Error(coupled_error); }
1196  if (config.physical_initialization) {
1197  // After parse_wall_heat: only conductive heat gets a molecular speed.
1198  const auto speed_error = set_wall_transport_speeds(
1199  wall, transfer_contract, dc, dx_inv[n/2], face);
1200  if (!speed_error.empty()) { amrex::Error(speed_error); }
1201  }
1202  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
1203  if (pp_face.contains("velocity")) {
1204  amrex::Error("Cloud Chamber: wall velocity metadata is not active; use NoSlipWall with zero velocity");
1205  }
1206  wall.wall_velocity[d] = amrex::Real(0.0);
1207  }
1208  const auto wetness_error = parse_wall_wetness(pp_face, face, wall);
1209  if (!wetness_error.empty()) { amrex::Error(wetness_error); }
1210  }
1211  validate_wall_roughness_geometry(walls, geom.CellSizeArray());
1212  config.m_boundary = walls;
1213  return config;
1214 }
1215 
1216 } // namespace erf_cloud_chamber
1217 
1218 #endif
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
constexpr amrex::Real p_0
Definition: ERF_Constants.H:69
constexpr amrex::Real RdoRv
Definition: ERF_Constants.H:60
auto probhi
Definition: ERF_InitCustomPertVels_ABL.H:37
auto problo
Definition: ERF_InitCustomPertVels_ABL.H:36
const Real lx
Definition: ERF_InitCustomPertVels_CloudChamber.H:10
const int nx
Definition: ERF_InitCustomPertVels_CloudChamber.H:14
const int ny
Definition: ERF_InitCustomPertVels_CloudChamber.H:15
const Real v_scale
Definition: ERF_InitCustomPertVels_CloudChamber.H:16
const Real ly
Definition: ERF_InitCustomPertVels_CloudChamber.H:11
InitType init_type
Definition: ERF_InitCustomPertVels_EkmanSpiral.H:8
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
const Real dy
Definition: ERF_InitCustomPert_ABL.H:101
ParmParse pp_prob("prob")
const Real length
Definition: ERF_InitCustomPert_AnelasticWallDiffusion.H:16
ParmParse pp("prob")
const auto config
Definition: ERF_InitCustomPert_CloudChamber.H:38
amrex::Real p_inf
Definition: ERF_InitCustomPert_DataAssimilation_ISV.H:5
Real U_0
Definition: ERF_InitCustomPert_TurbulentInflow.H:18
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_esatw(amrex::Real t, bool use_empirical=false)
Definition: ERF_MicrophysicsUtils.H:160
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE WallGradCoeffs WallGradientCoeffsUniform() noexcept
Definition: ERF_WallGradientCoeffs.H:46
Definition: ERF_CloudChamberTemperatureBands.H:18
std::string define(amrex::Vector< amrex::Real > edges, const amrex::Vector< amrex::Real > &values, const amrex::Geometry &geom)
Definition: ERF_CloudChamberTemperatureBands.H:20
@ None
Definition: ERF_LSM_Types.H:6
@ alpha
Definition: ERF_SLM.H:153
Definition: ERF_CloudChamber.H:25
void require_zero_periodicity()
Definition: ERF_CloudChamber.H:726
WallFace
Definition: ERF_CloudChamber.H:34
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real initial_u_velocity_perturbation(amrex::Real x, amrex::Real y, amrex::Real z, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &lo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &length, amrex::Real U_0, int nx, int ny) noexcept
Evaluate the x-face component of the Cloud Chamber initial velocity.
Definition: ERF_CloudChamber.H:249
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real theta_at(const Initialization &config, amrex::Real x, amrex::Real y, amrex::Real z) noexcept
Compute the potential temperature at a given location.
Definition: ERF_CloudChamber.H:306
std::string parse_wall_heat(amrex::ParmParse &pp, const std::string &face, bool physical, erf_wall_thermodynamics::FaceWall &wall, const amrex::Geometry &geom, SidewallTemperatureBands *sidewall_bands)
Definition: ERF_CloudChamber.H:648
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real deterministic_perturbation(amrex::Real x, amrex::Real y, amrex::Real z, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &lo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &length, amrex::Real amplitude) noexcept
Compute a deterministic 3D sine perturbation.
Definition: ERF_CloudChamber.H:208
std::string wall_transfer_contract_error(const WallTransferContract &contract, const std::string &face)
Definition: ERF_CloudChamber.H:472
std::string wall_roughness_geometry_error(const erf_wall_thermodynamics::Boundary &walls, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dx)
Definition: ERF_CloudChamber.H:780
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real staggered_velocity_v_scale(amrex::Real lx, amrex::Real ly, amrex::Real dx, amrex::Real dy, int nx, int ny) noexcept
Discrete derivative ratio for the staggered initial velocity field.
Definition: ERF_CloudChamber.H:231
bool valid_wall_wetness(amrex::Real rh) noexcept
Definition: ERF_CloudChamber.H:387
std::string wall_coupled_most_roughness_error(const erf_wall_thermodynamics::FaceWall &wall, const std::string &face)
Definition: ERF_CloudChamber.H:711
bool supports_moisture_model(const MoistureType moisture_type) noexcept
Return whether a moisture type is supported by the Cloud Chamber.
Definition: ERF_CloudChamber.H:153
WallTransferContract read_wall_transfer_contract(const amrex::ParmParse &pp, const std::string &face)
Definition: ERF_CloudChamber.H:433
std::string face_name(WallFace face)
Definition: ERF_CloudChamber.H:374
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real vapor_mixing_ratio_from_relative_humidity(amrex::Real temperature_K, amrex::Real pressure_Pa, amrex::Real relative_humidity) noexcept
Convert relative humidity to water vapor mixing ratio.
Definition: ERF_CloudChamber.H:351
void require_finite(const std::string &key, amrex::Real value)
Definition: ERF_CloudChamber.H:367
std::string parse_wall_wetness(amrex::ParmParse &pp, const std::string &face, erf_wall_thermodynamics::FaceWall &wall)
Definition: ERF_CloudChamber.H:393
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real qv_at(const Initialization &config, amrex::Real z) noexcept
Compute the water vapor mixing ratio at a given height.
Definition: ERF_CloudChamber.H:334
void validate_wall_roughness_geometry(const erf_wall_thermodynamics::Boundary &walls, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dx)
Definition: ERF_CloudChamber.H:828
Config parse_config(const amrex::Geometry &geom, MoistureType selected_moisture_model, const DiffChoice &dc)
Definition: ERF_CloudChamber.H:1074
InitializationMode
Definition: ERF_CloudChamber.H:97
void reject_unsupported_face_keys(amrex::ParmParse &pp, const std::string &face)
Definition: ERF_CloudChamber.H:766
void require_no_unsupported_geometry()
Definition: ERF_CloudChamber.H:741
bool is_bulk_model(const std::string &model)
Definition: ERF_CloudChamber.H:466
std::string initialization_contract_error(const InitializationContract &contract)
Validate the initialization contract and return an error message if violated.
Definition: ERF_CloudChamber.H:120
bool finite(amrex::Real value) noexcept
Definition: ERF_CloudChamber.H:362
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real linear_profile(amrex::Real bottom, amrex::Real top, amrex::Real coordinate, amrex::Real lower, amrex::Real length) noexcept
Compute a linear profile between two values.
Definition: ERF_CloudChamber.H:188
Initialization parse_initialization(const amrex::Real *problo, const amrex::Real *probhi, const MoistureType selected_moisture_model)
Definition: ERF_CloudChamber.H:836
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real initial_w_velocity_perturbation() noexcept
The Cloud Chamber initial vertical velocity perturbation.
Definition: ERF_CloudChamber.H:290
std::string set_wall_transport_speeds(erf_wall_thermodynamics::FaceWall &wall, const WallTransferContract &contract, const DiffChoice &dc, amrex::Real dx_inv, const std::string &face)
Definition: ERF_CloudChamber.H:1025
AMREX_FORCE_INLINE AMREX_GPU_HOST_DEVICE amrex::Real initial_v_velocity_perturbation(amrex::Real x, amrex::Real y, amrex::Real z, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &lo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &length, amrex::Real U_0, amrex::Real v_scale, int nx, int ny) noexcept
Evaluate the y-face component of the Cloud Chamber initial velocity.
Definition: ERF_CloudChamber.H:269
Definition: ERF_WallThermodynamics.H:10
amrex::GpuArray< TemperatureBandView, 4 > SidewallBandViews
Definition: ERF_WallThermodynamics.H:63
ScalarModel
Definition: ERF_WallThermodynamics.H:32
amrex::GpuArray< FaceWall, 2 *AMREX_SPACEDIM > Boundary
Definition: ERF_WallThermodynamics.H:133
real(c_double), parameter, private pi
Definition: ERF_module_mp_morr_two_moment.F90:100
Definition: ERF_DiffStruct.H:25
MolecDiffType molec_diff_type
Selected molecular transport model.
Definition: ERF_DiffStruct.H:97
amrex::Real alpha_C
Kinematic scalar diffusivity [m2/s].
Definition: ERF_DiffStruct.H:101
amrex::Real dynamic_viscosity
Dynamic viscosity for momentum diffusion [kg/(m-s)].
Definition: ERF_DiffStruct.H:109
amrex::Real rho0_trans
Reference density used to compute dynamic diffusion coefficients [kg/m3].
Definition: ERF_DiffStruct.H:104
amrex::Real alpha_T
Kinematic temperature diffusivity [m2/s].
Definition: ERF_DiffStruct.H:100
static MeshType mesh_type
Vertical mesh representation.
Definition: ERF_DataStruct.H:3004
Definition: ERF_CloudChamber.H:56
std::array< SidewallTemperatureBands, 4 > m_bands
Definition: ERF_CloudChamber.H:94
bool has_wall_rate_channel() const
Definition: ERF_CloudChamber.H:83
const erf_wall_thermodynamics::Boundary & transport_boundary() const &&=delete
erf_wall_thermodynamics::SidewallBandViews temperature_band_views() const &noexcept
Definition: ERF_CloudChamber.H:65
std::optional< erf_wall_thermodynamics::Boundary > m_boundary
Definition: ERF_CloudChamber.H:93
static bool has_bulk_scalar_wall(const erf_wall_thermodynamics::Boundary &walls) noexcept
Definition: ERF_CloudChamber.H:71
friend Config parse_config(const amrex::Geometry &, MoistureType, const DiffChoice &)
Definition: ERF_CloudChamber.H:1074
const erf_wall_thermodynamics::Boundary & transport_boundary() const &
Definition: ERF_CloudChamber.H:59
erf_wall_thermodynamics::SidewallBandViews temperature_band_views() const &&=delete
Contract used to validate initialization parameter consistency.
Definition: ERF_CloudChamber.H:105
InitializationMode mode
Definition: ERF_CloudChamber.H:106
bool has_initial_relative_humidity
Definition: ERF_CloudChamber.H:109
bool has_physical_profile_keys
Definition: ERF_CloudChamber.H:111
bool has_moisture
Definition: ERF_CloudChamber.H:108
bool has_legacy_profile_keys
Definition: ERF_CloudChamber.H:110
Profile data used by the device initializer; no wall transport is exposed here.
Definition: ERF_CloudChamber.H:39
amrex::Real temperature_perturbation_amplitude
Definition: ERF_CloudChamber.H:46
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > prob_hi
Definition: ERF_CloudChamber.H:53
bool has_moisture
Definition: ERF_CloudChamber.H:41
bool physical_initialization
Definition: ERF_CloudChamber.H:42
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > prob_lo
Definition: ERF_CloudChamber.H:52
amrex::Real initial_temperature_top
Definition: ERF_CloudChamber.H:44
bool active
Definition: ERF_CloudChamber.H:40
amrex::Real theta_perturbation_amplitude
Definition: ERF_CloudChamber.H:49
amrex::Real qv_bottom
Definition: ERF_CloudChamber.H:50
amrex::Real qv_top
Definition: ERF_CloudChamber.H:51
amrex::Real initial_relative_humidity
Definition: ERF_CloudChamber.H:45
amrex::Real theta_top
Definition: ERF_CloudChamber.H:48
amrex::Real theta_bottom
Definition: ERF_CloudChamber.H:47
amrex::Real initial_temperature_bottom
Definition: ERF_CloudChamber.H:43
Definition: ERF_CloudChamber.H:409
amrex::Real z0_m
Definition: ERF_CloudChamber.H:428
std::string heat_model
Definition: ERF_CloudChamber.H:422
bool momentum_model_specified
Definition: ERF_CloudChamber.H:411
amrex::Real z0_q
Definition: ERF_CloudChamber.H:430
bool z0_h_specified
Definition: ERF_CloudChamber.H:419
bool coefficient_source_specified
Definition: ERF_CloudChamber.H:414
std::string coefficient_source
Definition: ERF_CloudChamber.H:424
std::string vapor_model
Definition: ERF_CloudChamber.H:423
bool z0_q_specified
Definition: ERF_CloudChamber.H:420
std::string momentum_model
Definition: ERF_CloudChamber.H:421
amrex::Real z0_h
Definition: ERF_CloudChamber.H:429
bool legacy_aggregate_specified
Definition: ERF_CloudChamber.H:410
bool vapor_model_specified
Definition: ERF_CloudChamber.H:413
amrex::Real momentum_coefficient
Definition: ERF_CloudChamber.H:425
bool heat_coefficient_specified
Definition: ERF_CloudChamber.H:416
amrex::Real heat_coefficient
Definition: ERF_CloudChamber.H:426
bool heat_model_specified
Definition: ERF_CloudChamber.H:412
bool momentum_coefficient_specified
Definition: ERF_CloudChamber.H:415
bool z0_m_specified
Definition: ERF_CloudChamber.H:418
amrex::Real vapor_coefficient
Definition: ERF_CloudChamber.H:427
bool vapor_coefficient_specified
Definition: ERF_CloudChamber.H:417
Definition: ERF_WallThermodynamics.H:86
constexpr AMREX_GPU_HOST_DEVICE bool heat_is_conductive() const noexcept
Definition: ERF_WallThermodynamics.H:96
ScalarTransfer vapor
Definition: ERF_WallThermodynamics.H:92
ScalarTransfer heat
Definition: ERF_WallThermodynamics.H:91
constexpr AMREX_GPU_HOST_DEVICE bool heat_uses_most() const noexcept
Definition: ERF_WallThermodynamics.H:108
constexpr AMREX_GPU_HOST_DEVICE bool vapor_is_wet() const noexcept
Definition: ERF_WallThermodynamics.H:99
MoistureMode moisture
Definition: ERF_WallThermodynamics.H:88
MomentumTransfer momentum
Definition: ERF_WallThermodynamics.H:90
amrex::Real wetness
Definition: ERF_WallThermodynamics.H:89
constexpr AMREX_GPU_HOST_DEVICE bool vapor_uses_most() const noexcept
Definition: ERF_WallThermodynamics.H:114
ThermalBoundary thermal
Definition: ERF_WallThermodynamics.H:87
amrex::Real resolved_row_rate
Definition: ERF_WallThermodynamics.H:72
MomentumModel model
Definition: ERF_WallThermodynamics.H:67
amrex::Real molecular_speed
Definition: ERF_WallThermodynamics.H:71
Definition: ERF_WallThermodynamics.H:75
amrex::Real z0
Definition: ERF_WallThermodynamics.H:79
amrex::Real resolved_speed
Definition: ERF_WallThermodynamics.H:83
amrex::Real molecular_speed
Definition: ERF_WallThermodynamics.H:81
ScalarModel model
Definition: ERF_WallThermodynamics.H:76
Definition: ERF_WallThermodynamics.H:44
amrex::Real temperature_K
Definition: ERF_WallThermodynamics.H:46
ThermalMode mode
Definition: ERF_WallThermodynamics.H:45