ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_PlotfileSelection.H
Go to the documentation of this file.
1 /**
2  * \file ERF_PlotfileSelection.H
3  */
4 #ifndef ERF_PLOTFILE_SELECTION_H_
5 #define ERF_PLOTFILE_SELECTION_H_
6 
7 #include <algorithm>
8 #include <string>
9 
10 #include <AMReX_Vector.H>
11 
12 #include "ERF_DataStruct.H"
13 
14 namespace erf_plotfile
15 {
16 
17 /**
18  * @brief Whether the SW/LW heating-rate storage (qsrc_sw, qsrc_lw) exists:
19  * every erf.radiation_model other than None (RRTMGP, Simple, TwoStream)
20  * allocates the same qheating_rates array.
21  */
22 inline bool radiation_heating_storage_available (RadiationType rad_model)
23 {
24  return rad_model != RadiationType::None;
25 }
26 
27 /**
28  * @brief What the active run can supply to a 3D plotfile.
29  *
30  * Availability of the *moist* components is not described here: it comes from
31  * the active scheme's MoistureComponentIndices, which is the single source of
32  * truth for which moisture variables exist (see the class comment on that
33  * struct). What remains in this struct is the conserved-state layout -- needed
34  * to know where the moist window ends and the non-water species begin -- and the
35  * optional non-moisture storage, which a plot variable can only be selected
36  * against if it was actually allocated.
37  */
39 {
40  //! Width of the allocated conserved state, i.e. NDRY + NSCALARS plus every
41  //! component the microphysics interface adds. This bounds the dry
42  //! components and the non-water species some schemes carry above the moist
43  //! window; it is deliberately *not* what gates the moist components.
45 
46  //! Number of conserved components in the moist window that starts at
47  //! RhoQ1_comp. Inside that window a component is gated by moisture_indices
48  //! rather than by allocation, because several schemes allocate moist slots
49  //! they never integrate. Above the window sit the non-water species a
50  //! scheme may add (SuperDroplets), which are allocated *and* integrated and
51  //! so are gated by conserved_state_size like the dry components.
52  //!
53  //! The default is the full RhoQ1..RhoQ11 window, i.e. "let the moisture map
54  //! decide about every rhoQn slot", which is the conservative answer for a
55  //! caller that does not know the scheme's width.
57 
58  //! The active moisture scheme's component/diagnostic map.
60 
61  bool time_average_storage = false;
62  bool interval_mean_storage = false;
65  bool dissipation_storage = false;
66  bool wall_distance_storage = false;
67 };
68 
69 /**
70  * @brief Record the conserved-state layout of a run on its capabilities.
71  *
72  * Every output path that selects raw state names must describe the state the
73  * same way, so the three fields are filled here rather than at each call site.
74  *
75  * @param caps Capabilities to fill in.
76  * @param mi The active scheme's component/diagnostic map.
77  * @param qstate_moist_size Conserved components in the moist window
78  * (Microphysics::Get_Qstate_Moist_Size()).
79  * @param qstate_size All conserved components the microphysics adds,
80  * moist window plus non-water species
81  * (Microphysics::Get_Qstate_Size()).
82  */
83 inline void
85  const MoistureComponentIndices& mi,
86  const int qstate_moist_size,
87  const int qstate_size)
88 {
89  caps.moisture_indices = mi;
90  caps.moist_state_size = qstate_moist_size;
91  caps.conserved_state_size = NDRY + NSCALARS + qstate_size;
92 }
93 
94 inline bool
95 plot3d_batch_resets_interval_means (int plotfiles_written,
96  bool interval_diagnostic_consumed,
97  bool compute_mean_vars,
98  const std::string& reset_mode)
99 {
100  return plotfiles_written > 0 && interval_diagnostic_consumed &&
101  compute_mean_vars && reset_mode == "plotfile";
102 }
103 
104 /**
105  * @brief Conserved-state component holding the q-th moist variable, or -1 if
106  * q is outside the fixed RhoQ1..RhoQ11 layout.
107  */
108 inline int
110 {
111  if (q < 1 || q > 11) return -1;
112  return RhoQ1_comp + q - 1;
113 }
114 
115 /**
116  * @brief Conserved-state component behind a raw state variable name
117  * ("density", "rhotheta", "rhoQ3", ...), or -1 if the name is not one.
118  */
119 inline int
121 {
122  if (name == "density") return Rho_comp;
123  if (name == "rhotheta") return RhoTheta_comp;
124  if (name == "rhoKE") return RhoKE_comp;
125  if (name == "rhoadv_0") return RhoScalar_comp;
126 
127  if (name.size() > 4 && name.compare(0, 4, "rhoQ") == 0) {
128  const std::string suffix = name.substr(4);
129  if (!suffix.empty() && std::all_of(suffix.cbegin(), suffix.cend(),
130  [] (const char c) { return c >= '0' && c <= '9'; })) {
131  const int q = std::stoi(suffix);
133  }
134  }
135 
136  return -1;
137 }
138 
139 /**
140  * @brief Diagnostics that every run can write, moist or dry.
141  */
142 inline bool
144 {
145  static constexpr const char* names[] = {
146  "temp", "theta", "KE", "scalar", "soundspeed", "reflectivity",
147  "max_reflectivity", "mucape", "vorticity_x", "vorticity_y", "vorticity_z",
148  "helicity", "local_helicity", "vort_stretching",
149  "magvel", "divU", "pres_hse", "dens_hse",
150  "theta_hse", "pi_hse", "qv_hse", "pressure", "pert_pres", "pert_dens", "buoyancy",
151  "eq_pot_temp", "VPD", "dpdx", "dpdy", "dpdz", "pres_hse_x", "pres_hse_y",
152  "pres_hse_z", "z_phys", "detJ", "h_xi", "h_eta", "h_zeta",
153  "mapfac", "lat_m", "lon_m", "pblh",
154  "shoc_cldfrac", "shoc_ql", "shoc_ql2", "shoc_cond", "wqls_sec", "wthv_sec",
155  "w_sec", "thl_sec", "qw_sec", "qwthl_sec", "wthl_sec", "wqw_sec", "w3",
156  "brunt", "isotropy", "shear_prod", "buoy_prod", "diss_tke",
157  "terrain_IB_mask", "volfrac",
158  "ibseb_nfaces", "ibseb_tskin", "ibseb_sw_abs", "ibseb_shadow",
159  "ibseb_lw_net", "ibseb_f_sky", "ibseb_H", "ibseb_G",
160  "Tau11", "Tau12", "Tau13", "Tau21", "Tau22", "Tau23",
161  "Tau31", "Tau32", "Tau33", "hfx1", "hfx2", "hfx3",
162  "q1fx1", "q1fx2", "q1fx3", "q2fx3"
163  };
164 
165  for (const char* candidate : names) {
166  if (name == candidate) return true;
167  }
168 
169 #ifdef ERF_COMPUTE_ERROR
170  if (name == "xvel_err" || name == "yvel_err" || name == "zvel_err" ||
171  name == "pp_err") return true;
172 #endif
173 
174  return false;
175 }
176 
177 /**
178  * @brief Whether a named 3D plot variable can be written by this run.
179  *
180  * The order of the tests below is the order of authority: raw state components
181  * answer from the state layout, moisture variables answer from the active
182  * scheme's index map, optional diagnostics answer from their storage flag, and
183  * anything left is an always-available diagnostic.
184  */
185 inline bool
187 {
188  static constexpr const char* names[] = {
189  "u_mean", "v_mean", "w_mean", "theta_mean",
190  "uu_mean", "vv_mean", "ww_mean", "uw_mean", "vw_mean", "wtheta_mean",
191  "uu_fluct", "vv_fluct", "ww_fluct", "uw_fluct", "vw_fluct", "wtheta_fluct", "tke_resolved"
192  };
193 
194  for (const char* candidate : names) {
195  if (name == candidate) return true;
196  }
197  return false;
198 }
199 
200 inline bool
202  const amrex::Vector<std::string>& names)
203 {
204  return std::any_of(names.begin(), names.end(),
205  [] (const std::string& name) {
206  return plot3d_interval_mean_diagnostic(name);
207  });
208 }
209 
210 inline bool
212  const Plot3DSelectionCapabilities& caps)
213 {
215 
216  // Raw conserved-state components. Inside the moist window the moisture map
217  // decides, since allocated-but-never-integrated slots must not be published
218  // as data. Below the window the state is always allocated dry-or-moist, and
219  // above it are the non-water species a scheme may add -- both of those are
220  // real data whenever they were allocated, so a bounds check is the right
221  // test for them.
222  const int cons_comp = plot3d_conserved_component_index(name);
223  if (cons_comp >= 0) {
224  if ( (cons_comp >= RhoQ1_comp) &&
225  (cons_comp < RhoQ1_comp + caps.moist_state_size) ) {
226  return mi.has_comp(cons_comp);
227  }
228  return (cons_comp < caps.conserved_state_size);
229  }
230 
231  // Every moisture variable -- species, aggregates and moist diagnostics --
232  // is decided by the scheme's index map.
233  const auto moist_status = mi.query_var(name);
234  if (moist_status.governed) return moist_status.available;
235 
236  if (name == "u_t_avg" || name == "v_t_avg" || name == "w_t_avg" ||
237  name == "umag_t_avg") {
238  return caps.time_average_storage;
239  }
241  return caps.interval_mean_storage;
242  }
243  if (name == "qsrc_sw" || name == "qsrc_lw") {
244  return caps.radiation_heating_storage;
245  }
246  if (name == "nut" || name == "Kmv" || name == "Kmh" ||
247  name == "Khv" || name == "Khh" || name == "Lturb") {
248  return caps.eddy_diffusivity_storage;
249  }
250  if (name == "Rt" || name == "cmu" || name == "cmu_prime") {
251  return caps.eddy_diffusivity_storage;
252  }
253  if (name == "diss") return caps.dissipation_storage;
254  if (name == "walldist") return caps.wall_distance_storage;
255 
257 }
258 
259 /**
260  * @brief Whether any requested variable needs the pressure field computed.
261  */
262 inline bool
263 plot3d_needs_pressure (const amrex::Vector<std::string>& names)
264 {
265  for (const auto& name : names) {
266  if (name == "pressure" || name == "pert_pres" ||
267  name == "dpdx" || name == "dpdy" || name == "dpdz" ||
268  name == "eq_pot_temp" || name == "VPD" || name == "qsat" ||
269  name == "rel_humidity") {
270  return true;
271  }
272 #ifdef ERF_COMPUTE_ERROR
273  if (name == "pp_err") return true;
274 #endif
275  }
276  return false;
277 }
278 
279 inline amrex::Vector<std::string>
280 plot3d_selected_particle_count_names (const amrex::Vector<std::string>& requested,
281  const amrex::Vector<std::string>& configured)
282 {
283  amrex::Vector<std::string> selected;
284  for (const auto& count_name : requested) {
285  constexpr std::size_t suffix_size = 6; // "_count"
286  if (count_name.size() > suffix_size &&
287  count_name.compare(count_name.size() - suffix_size, suffix_size, "_count") == 0) {
288  const std::string container = count_name.substr(0, count_name.size() - suffix_size);
289  if (std::find(configured.cbegin(), configured.cend(), container) == configured.cend() ||
290  std::find(selected.cbegin(), selected.cend(), container) != selected.cend()) {
291  continue;
292  }
293  selected.push_back(container);
294  }
295  }
296  return selected;
297 }
298 
299 } // namespace erf_plotfile
300 
301 #endif
#define RhoScalar_comp
Definition: ERF_IndexDefines.H:43
#define Rho_comp
Definition: ERF_IndexDefines.H:39
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:40
#define NDRY
Definition: ERF_IndexDefines.H:13
#define NSCALARS
Definition: ERF_IndexDefines.H:16
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:45
#define NMOIST_max
Definition: ERF_IndexDefines.H:19
#define RhoKE_comp
Definition: ERF_IndexDefines.H:41
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:101
@ q
Definition: ERF_WSM6.H:273
Definition: ERF_PlotfileSelection.H:15
int plot3d_q_conserved_component_index(const int q)
Conserved-state component holding the q-th moist variable, or -1 if q is outside the fixed RhoQ1....
Definition: ERF_PlotfileSelection.H:109
int plot3d_conserved_component_index(const std::string &name)
Conserved-state component behind a raw state variable name ("density", "rhotheta",...
Definition: ERF_PlotfileSelection.H:120
void plot3d_set_state_capabilities(Plot3DSelectionCapabilities &caps, const MoistureComponentIndices &mi, const int qstate_moist_size, const int qstate_size)
Record the conserved-state layout of a run on its capabilities.
Definition: ERF_PlotfileSelection.H:84
bool plot3d_batch_resets_interval_means(int plotfiles_written, bool interval_diagnostic_consumed, bool compute_mean_vars, const std::string &reset_mode)
Definition: ERF_PlotfileSelection.H:95
bool plot3d_interval_mean_diagnostic(const std::string &name)
Whether a named 3D plot variable can be written by this run.
Definition: ERF_PlotfileSelection.H:186
bool plot3d_selection_has_interval_mean_diagnostic(const amrex::Vector< std::string > &names)
Definition: ERF_PlotfileSelection.H:201
bool plot3d_needs_pressure(const amrex::Vector< std::string > &names)
Whether any requested variable needs the pressure field computed.
Definition: ERF_PlotfileSelection.H:263
bool plot3d_fixed_variable_available(const std::string &name, const Plot3DSelectionCapabilities &caps)
Definition: ERF_PlotfileSelection.H:211
amrex::Vector< std::string > plot3d_selected_particle_count_names(const amrex::Vector< std::string > &requested, const amrex::Vector< std::string > &configured)
Definition: ERF_PlotfileSelection.H:280
bool plot3d_fixed_unconditional_diagnostic(const std::string &name)
Diagnostics that every run can write, moist or dry.
Definition: ERF_PlotfileSelection.H:143
bool radiation_heating_storage_available(RadiationType rad_model)
Whether the SW/LW heating-rate storage (qsrc_sw, qsrc_lw) exists: every erf.radiation_model other tha...
Definition: ERF_PlotfileSelection.H:22
bool available
the active scheme carries the data behind it
Definition: ERF_DataStruct.H:593
The moisture data carried by the active microphysics scheme.
Definition: ERF_DataStruct.H:223
bool has_comp(int comp) const
Test whether a conserved-state component holds one of the moisture variables this scheme carries.
Definition: ERF_DataStruct.H:515
VarAvailability query_var(const std::string &name) const
Classify an output variable name against this index map.
Definition: ERF_DataStruct.H:607
What the active run can supply to a 3D plotfile.
Definition: ERF_PlotfileSelection.H:39
bool eddy_diffusivity_storage
Definition: ERF_PlotfileSelection.H:64
MoistureComponentIndices moisture_indices
The active moisture scheme's component/diagnostic map.
Definition: ERF_PlotfileSelection.H:59
int moist_state_size
Definition: ERF_PlotfileSelection.H:56
bool dissipation_storage
Definition: ERF_PlotfileSelection.H:65
bool radiation_heating_storage
Definition: ERF_PlotfileSelection.H:63
bool wall_distance_storage
Definition: ERF_PlotfileSelection.H:66
bool time_average_storage
Definition: ERF_PlotfileSelection.H:61
int conserved_state_size
Definition: ERF_PlotfileSelection.H:44
bool interval_mean_storage
Definition: ERF_PlotfileSelection.H:62