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