ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_WRFSurfaceFields.H
Go to the documentation of this file.
1 #ifndef ERF_WRF_SURFACE_FIELDS_H_
2 #define ERF_WRF_SURFACE_FIELDS_H_
3 
4 /**
5  * \file ERF_WRFSurfaceFields.H
6  *
7  * Land and soil fields of a wrfinput file that no land model takes: leaf area, vegetation
8  * fraction, deep soil temperature, roughness, albedo, background albedo, sea ice, snow,
9  * and the soil temperature and moisture profiles with the depths of their layers.
10  *
11  * ERF::init_from_wrfinput fills this holder when the two-stream force-restore balance
12  * (erf.radiation.seb_prognostic_enable) runs without a land model. Every field is
13  * optional: a file without it leaves get() null. The holder keeps the values as the file
14  * gives them, in the file's units; it applies no physics.
15  *
16  * A field that holds a value outside its catalog range, or one that is not finite, or
17  * that the level's files do not cover, is reported and not held: get() is null, as for a
18  * field the file does not have. The code that takes a field decides whether its absence
19  * stops the run.
20  *
21  * The data are those of the initial time. They are written to the checkpoint and read
22  * back on a restart. A regrid carries them onto the new grids: a column the old grids
23  * of the level held keeps its values, and any other column takes those of the column of
24  * the coarser level it lies in (piecewise constant, so a flag stays a flag, a soil
25  * profile stays the profile of one column, and no value leaves the catalog range). A
26  * level without a file of its own (made by tagging, at the start or by a regrid) takes
27  * every field of the coarser level in the same way.
28  *
29  * The class has no NetCDF dependency: fill() takes the region a rank has read as an
30  * FArrayBox, so the unit tests can drive it without a file.
31  */
32 
33 #include <array>
34 #include <iosfwd>
35 #include <limits>
36 #include <memory>
37 #include <string>
38 
39 #include <AMReX_BoxArray.H>
40 #include <AMReX_DistributionMapping.H>
41 #include <AMReX_FArrayBox.H>
42 #include <AMReX_MultiFab.H>
43 #include <AMReX_Periodicity.H>
44 #include <AMReX_Vector.H>
45 
47 {
48 public:
49  /** The variables, in the order they are requested from the file. */
50  enum class Field : int {
51  LAI = 0, ///< leaf area index [m2/m2]
52  VEGFRA, ///< vegetation fraction [percent]
53  TMN, ///< soil temperature at the lower boundary [K]
54  ZNT, ///< roughness length [m]; in wrfout files, not in a real.exe wrfinput
55  ALBEDO, ///< surface albedo [1]; in wrfout files, not in a real.exe wrfinput
56  ALBBCK, ///< background (snow-free) albedo [1]; what a real.exe wrfinput holds
57  SEAICE, ///< sea ice flag [1]
58  SNOW, ///< snow water equivalent [kg/m2]
59  SNOWH, ///< snow depth [m]
60  TSLB, ///< soil temperature, one component per soil layer, top layer first [K]
61  SMOIS, ///< soil moisture, one component per soil layer, top layer first [m3/m3]
62  ZS, ///< depth of the center of each soil layer, one column for the level [m]
63  DZS, ///< thickness of each soil layer, one column for the level [m]
64  NumFields
65  };
66 
67  static constexpr int num_fields = static_cast<int>(Field::NumFields);
68 
69  /** How a variable is laid out in the file. */
70  enum class Kind : int {
71  Surface, ///< one value per column
72  SoilField, ///< one value per column and soil layer
73  LayerColumn ///< one value per soil layer, the same for every column (ZS, DZS)
74  };
75 
76  /** A variable's name in the file, its layout, its units, and the range a file may hold. */
77  struct Descriptor {
79  const char* nc_name;
81  const char* units;
82  amrex::Real lo; ///< smallest value a file may hold
83  amrex::Real hi; ///< largest value a file may hold
84  };
85 
86  /**
87  * The one list of the variables. The reader asks the file for every name in it, and
88  * takes the layout of a name from its kind.
89  *
90  * The bounds leave out only values that cannot be the named quantity (a negative
91  * temperature, a fraction above one). A field that a hand-built file declares and
92  * leaves at zero passes; whether zero is usable is for the code that takes the field.
93  * The upper bounds of LAI, TMN, ZNT, SNOW, SNOWH, TSLB, ZS and DZS are not physical
94  * limits: they are set far above any value of the quantity so that a NetCDF fill
95  * value (9.97e36), which is finite, is left out.
96  */
97  [[nodiscard]] static const std::array<Descriptor, num_fields>& catalog ();
98 
99  /** The descriptor of the variable with this name in the file, or null when it is not one of these. */
100  [[nodiscard]] static const Descriptor* find (const std::string& nc_name);
101 
102  /** True when the third index of this variable in the file is the soil layer. */
103  [[nodiscard]] static bool has_soil_layers (const std::string& nc_name)
104  {
105  const Descriptor* d = find(nc_name);
106  return d != nullptr && d->kind != Kind::Surface;
107  }
108 
109  void resize (int nlevs);
110 
111  [[nodiscard]] int num_levels () const { return static_cast<int>(m_fields.size()); }
112 
113  /** True when the level holds a field or a soil layer column. */
114  [[nodiscard]] bool holds_any (int lev) const;
115 
116  /** Drop everything held at this level. Prints one line when something was held. */
117  void clear (int lev);
118 
119  /** The field at this level, or null when it is not held. Always null for ZS and DZS. */
120  [[nodiscard]] const amrex::MultiFab* get (int lev, Field id) const
121  {
122  if (lev < 0 || lev >= num_levels()) { return nullptr; }
123  return m_fields[lev][static_cast<int>(id)].get();
124  }
125 
126  [[nodiscard]] bool has (int lev, Field id) const { return get(lev, id) != nullptr; }
127 
128  /** Depth of the center of each soil layer [m], top layer first (ZS); empty when not held. */
129  [[nodiscard]] const amrex::Vector<amrex::Real>& soil_depth (int lev) const
130  {
131  AMREX_ALWAYS_ASSERT(lev >= 0 && lev < num_levels());
132  return m_soil_depth[lev];
133  }
134 
135  /** Thickness of each soil layer [m], top layer first (DZS); empty when not held. */
136  [[nodiscard]] const amrex::Vector<amrex::Real>& soil_thickness (int lev) const
137  {
138  AMREX_ALWAYS_ASSERT(lev >= 0 && lev < num_levels());
139  return m_soil_thickness[lev];
140  }
141 
142  /**
143  * Take the soil layer depths (ZS) or thicknesses (DZS) from the column a rank has
144  * read. Local: every rank reads the same column.
145  *
146  * A column that is zero in every layer is taken as not given. Otherwise the depths
147  * must be zero or more and increase with the layer (a land model may put its first
148  * level at the surface), and the thicknesses must be positive. A column that breaks
149  * these rules, or holds a value that is not finite or above the catalog bound, is
150  * reported and not held.
151  *
152  * @param lev level
153  * @param id Field::ZS or Field::DZS
154  * @param src the column as read; host-readable memory; its z range is the soil layers
155  */
156  void set_soil_layers (int lev, Field id, const amrex::FArrayBox& src);
157 
158  /**
159  * Copy the part of one field that one file covers. Every rank calls it, once per file
160  * of the level; no MPI call is made here.
161  *
162  * The valid cells of the level inside file_box take the file's values. A soil field
163  * gets one component per layer, in the file's order. The values are counted against
164  * the catalog range here; the report, the decision and the ghost cells are left to
165  * finish_level().
166  *
167  * @param lev level
168  * @param id field (not ZS or DZS)
169  * @param ba2d the level's grids, one cell thick in z
170  * @param dm the level's distribution
171  * @param ng ghost cells of the stored field (none in z)
172  * @param nlayers number of soil layers of the variable in the file (1 for a surface
173  * field); the same on every rank
174  * @param src this rank's part of the field as read; memory a device kernel can read
175  * (the reader's fab); its low z index is the first layer
176  * @param file_box the columns this file covers at this level (z is ignored)
177  */
178  void fill (int lev, Field id,
179  const amrex::BoxArray& ba2d,
180  const amrex::DistributionMapping& dm,
181  const amrex::IntVect& ng,
182  int nlayers,
183  const amrex::FArrayBox& src,
184  const amrex::Box& file_box);
185 
186  /**
187  * Close the read of a level, after the last file. Collective: every rank calls it.
188  *
189  * Reduces the counts and ranges of fill() over the ranks (two reductions for the
190  * level, whatever the number of fields), then for every field read: reports it and
191  * fills its ghost cells; or, when a valid cell was not covered by a file, a value
192  * lies outside the catalog range, a value is not finite, or two files disagree on
193  * the number of layers, reports that with the counts and drops the field. Then drops
194  * TSLB, SMOIS, ZS and DZS together when they disagree on the number of soil layers.
195  *
196  * @param lev level
197  * @param period the level's periodicity
198  */
199  void finish_level (int lev, const amrex::Periodicity& period);
200 
201  /**
202  * Put the level's fields on new grids (a new level, a regrid or a rebalance).
203  * Collective: every rank calls it. Level lev-1 must already be on its new grids.
204  *
205  * A field the level holds keeps its values on the columns its old grids covered. The
206  * other columns take the values of the column of level lev-1 they lie in. When level
207  * lev-1 does not hold the field with the same number of components and the new grids
208  * reach beyond the old ones, that field is dropped and one line says so.
209  *
210  * With take_coarser_fields, a field the level does not hold and level lev-1 does is
211  * taken from level lev-1 on every column, and so are the soil layer columns when the
212  * level has none. This is for a level without a file of its own.
213  *
214  * The soil layer columns do not depend on the grids and are kept.
215  *
216  * @param lev level
217  * @param new_ba2d,new_dm the new grids, one cell thick in z, and their distribution
218  * @param period the level's periodicity
219  * @param ratio refinement ratio between level lev-1 and this level (z is ignored;
220  * not used at level 0)
221  * @param take_coarser_fields take the fields the level lacks from level lev-1
222  * @param verbose above zero: one line per field put on new grids, with the number of
223  * columns taken from level lev-1 and the range
224  */
225  void remake (int lev,
226  const amrex::BoxArray& new_ba2d,
227  const amrex::DistributionMapping& new_dm,
228  const amrex::Periodicity& period,
229  const amrex::IntVect& ratio,
230  bool take_coarser_fields,
231  int verbose = 0);
232 
233  /** Name of the checkpoint's text file that lists what each level holds. */
234  static constexpr const char* checkpoint_header_name = "WRFSurfaceFieldsHeader";
235 
236  /**
237  * Write the held fields into a checkpoint directory: one MultiFab file per field
238  * under the level's directory, and one text file that lists what each level holds.
239  * The text file is written by the I/O rank, also when nothing is held.
240  */
241  void write_checkpoint (const std::string& checkpoint_dir, int nlevels) const;
242 
243  /**
244  * Read what write_checkpoint() wrote, onto the grids of this run (they need not be
245  * those of the checkpoint). A checkpoint without the text file (written before this
246  * class) leaves the holder empty, prints one line and returns false.
247  *
248  * Where this run's grids reach beyond those of the checkpoint, the rule of remake()
249  * applies: the columns the checkpoint does not give come from level lev-1, or the
250  * field is not restored and one line says so. No cell is left without a value.
251  *
252  * @param ba2d,dm the grids and distribution of each level of this run
253  * @param ng ghost cells of the stored fields
254  * @param period per level
255  * @param ratio refinement ratio between level lev and lev+1, per level (z is ignored)
256  */
257  bool read_checkpoint (const std::string& checkpoint_dir, int nlevels,
258  const amrex::Vector<amrex::BoxArray>& ba2d,
259  const amrex::Vector<amrex::DistributionMapping>& dm,
260  const amrex::IntVect& ng,
261  const amrex::Vector<amrex::Periodicity>& period,
262  const amrex::Vector<amrex::IntVect>& ratio);
263 
264  /** Write and parse the text file; public for the unit tests. */
265  void write_header (std::ostream& os, int nlevels) const;
266  struct HeaderLevel {
267  std::array<int, num_fields> ncomp{}; ///< 0: not held
268  amrex::Vector<amrex::Real> soil_depth;
269  amrex::Vector<amrex::Real> soil_thickness;
270  };
271  /** Returns false, leaving out empty, when the text is not a header of this class. */
272  [[nodiscard]] static bool parse_header (std::istream& is, amrex::Vector<HeaderLevel>& out);
273 
274  /**
275  * Fill the ghost cells of a field from its valid cells: a ghost cell that the
276  * level's grids cover (also through a periodic boundary) takes the value there; one
277  * beyond the bounding box of the level's grids takes the nearest column inside it, as
278  * the reader's other surface fields do beyond the file; any other takes the nearest
279  * column of its own box. Public for the unit tests.
280  */
281  static void fill_ghost_cells (amrex::MultiFab& mf, const amrex::Periodicity& period);
282 
283 private:
284  // What fill() has found on this rank for one field, until finish_level() reduces it
285  struct Pending {
286  amrex::Long n_filled = 0; // valid cell values taken from a file
287  amrex::Long n_outside = 0; // of those, outside the catalog range
288  amrex::Long n_not_finite = 0; // of those, not finite
289  amrex::Long n_uncovered = 0; // boxes whose part of a file this rank did not read
290  bool read = false; // a file of the level held the variable
291  bool conflict = false; // two files disagree (layer count, ZS or DZS)
292  amrex::Real vmin = std::numeric_limits<amrex::Real>::max();
293  amrex::Real vmax = -std::numeric_limits<amrex::Real>::max();
294  };
295 
296  void drop (int lev, Field id);
297 
298  // The field on the grids ba2d: the valid cells of src (the level's own values on
299  // other grids; may be null) where they reach, and the column of level lev-1 it lies
300  // in elsewhere. Null when the two together do not give every column.
301  [[nodiscard]] std::unique_ptr<amrex::MultiFab>
302  on_grids (int lev, Field id, const amrex::MultiFab* src,
303  const amrex::BoxArray& ba2d, const amrex::DistributionMapping& dm,
304  const amrex::IntVect& ng, const amrex::Periodicity& period,
305  const amrex::IntVect& ratio) const;
306 
307  // (lev)(field); null when the field is not held
308  amrex::Vector<std::array<std::unique_ptr<amrex::MultiFab>, num_fields>> m_fields;
309  amrex::Vector<std::array<Pending, num_fields>> m_pending;
310  // (lev)(layer)
311  amrex::Vector<amrex::Vector<amrex::Real>> m_soil_depth;
312  amrex::Vector<amrex::Vector<amrex::Real>> m_soil_thickness;
313 };
314 
315 #endif
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
DiagnosticID id
Definition: ERF_Plotfile2DWaterPath.cpp:28
Definition: ERF_WRFSurfaceFields.H:47
static const std::array< Descriptor, num_fields > & catalog()
Definition: ERF_WRFSurfaceFields.cpp:57
bool holds_any(int lev) const
Definition: ERF_WRFSurfaceFields.cpp:99
void write_header(std::ostream &os, int nlevels) const
Definition: ERF_WRFSurfaceFields.cpp:517
const amrex::Vector< amrex::Real > & soil_thickness(int lev) const
Definition: ERF_WRFSurfaceFields.H:136
std::unique_ptr< amrex::MultiFab > on_grids(int lev, Field id, const amrex::MultiFab *src, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, const amrex::IntVect &ng, const amrex::Periodicity &period, const amrex::IntVect &ratio) const
Definition: ERF_WRFSurfaceFields.cpp:425
int num_levels() const
Definition: ERF_WRFSurfaceFields.H:111
static bool has_soil_layers(const std::string &nc_name)
Definition: ERF_WRFSurfaceFields.H:103
Kind
Definition: ERF_WRFSurfaceFields.H:70
@ LayerColumn
one value per soil layer, the same for every column (ZS, DZS)
@ Surface
one value per column
@ SoilField
one value per column and soil layer
Field
Definition: ERF_WRFSurfaceFields.H:50
@ TSLB
soil temperature, one component per soil layer, top layer first [K]
@ ALBBCK
background (snow-free) albedo [1]; what a real.exe wrfinput holds
@ SMOIS
soil moisture, one component per soil layer, top layer first [m3/m3]
@ SNOWH
snow depth [m]
@ SNOW
snow water equivalent [kg/m2]
@ ALBEDO
surface albedo [1]; in wrfout files, not in a real.exe wrfinput
@ ZS
depth of the center of each soil layer, one column for the level [m]
@ VEGFRA
vegetation fraction [percent]
@ LAI
leaf area index [m2/m2]
@ TMN
soil temperature at the lower boundary [K]
@ SEAICE
sea ice flag [1]
@ ZNT
roughness length [m]; in wrfout files, not in a real.exe wrfinput
@ DZS
thickness of each soil layer, one column for the level [m]
void clear(int lev)
Definition: ERF_WRFSurfaceFields.cpp:109
amrex::Vector< amrex::Vector< amrex::Real > > m_soil_thickness
Definition: ERF_WRFSurfaceFields.H:312
static void fill_ghost_cells(amrex::MultiFab &mf, const amrex::Periodicity &period)
Definition: ERF_WRFSurfaceFields.cpp:383
const amrex::MultiFab * get(int lev, Field id) const
Definition: ERF_WRFSurfaceFields.H:120
bool read_checkpoint(const std::string &checkpoint_dir, int nlevels, const amrex::Vector< amrex::BoxArray > &ba2d, const amrex::Vector< amrex::DistributionMapping > &dm, const amrex::IntVect &ng, const amrex::Vector< amrex::Periodicity > &period, const amrex::Vector< amrex::IntVect > &ratio)
Definition: ERF_WRFSurfaceFields.cpp:601
static bool parse_header(std::istream &is, amrex::Vector< HeaderLevel > &out)
Definition: ERF_WRFSurfaceFields.cpp:545
static constexpr int num_fields
Definition: ERF_WRFSurfaceFields.H:67
static constexpr const char * checkpoint_header_name
Definition: ERF_WRFSurfaceFields.H:234
void set_soil_layers(int lev, Field id, const amrex::FArrayBox &src)
Definition: ERF_WRFSurfaceFields.cpp:134
static const Descriptor * find(const std::string &nc_name)
Definition: ERF_WRFSurfaceFields.cpp:81
amrex::Vector< amrex::Vector< amrex::Real > > m_soil_depth
Definition: ERF_WRFSurfaceFields.H:311
void remake(int lev, const amrex::BoxArray &new_ba2d, const amrex::DistributionMapping &new_dm, const amrex::Periodicity &period, const amrex::IntVect &ratio, bool take_coarser_fields, int verbose=0)
Definition: ERF_WRFSurfaceFields.cpp:455
amrex::Vector< std::array< Pending, num_fields > > m_pending
Definition: ERF_WRFSurfaceFields.H:309
void drop(int lev, Field id)
Definition: ERF_WRFSurfaceFields.cpp:122
bool has(int lev, Field id) const
Definition: ERF_WRFSurfaceFields.H:126
void fill(int lev, Field id, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, const amrex::IntVect &ng, int nlayers, const amrex::FArrayBox &src, const amrex::Box &file_box)
Definition: ERF_WRFSurfaceFields.cpp:205
void finish_level(int lev, const amrex::Periodicity &period)
Definition: ERF_WRFSurfaceFields.cpp:292
const amrex::Vector< amrex::Real > & soil_depth(int lev) const
Definition: ERF_WRFSurfaceFields.H:129
void resize(int nlevs)
Definition: ERF_WRFSurfaceFields.cpp:90
amrex::Vector< std::array< std::unique_ptr< amrex::MultiFab >, num_fields > > m_fields
Definition: ERF_WRFSurfaceFields.H:308
void write_checkpoint(const std::string &checkpoint_dir, int nlevels) const
Definition: ERF_WRFSurfaceFields.cpp:582
@ ng
Definition: ERF_Morrison.H:51
Definition: ERF_WRFSurfaceFields.H:77
Field id
Definition: ERF_WRFSurfaceFields.H:78
amrex::Real lo
smallest value a file may hold
Definition: ERF_WRFSurfaceFields.H:82
Kind kind
Definition: ERF_WRFSurfaceFields.H:80
amrex::Real hi
largest value a file may hold
Definition: ERF_WRFSurfaceFields.H:83
const char * units
Definition: ERF_WRFSurfaceFields.H:81
const char * nc_name
Definition: ERF_WRFSurfaceFields.H:79
Definition: ERF_WRFSurfaceFields.H:266
amrex::Vector< amrex::Real > soil_thickness
Definition: ERF_WRFSurfaceFields.H:269
std::array< int, num_fields > ncomp
0: not held
Definition: ERF_WRFSurfaceFields.H:267
amrex::Vector< amrex::Real > soil_depth
Definition: ERF_WRFSurfaceFields.H:268
Definition: ERF_WRFSurfaceFields.H:285
amrex::Long n_filled
Definition: ERF_WRFSurfaceFields.H:286
bool conflict
Definition: ERF_WRFSurfaceFields.H:291
amrex::Long n_uncovered
Definition: ERF_WRFSurfaceFields.H:289
amrex::Real vmax
Definition: ERF_WRFSurfaceFields.H:293
bool read
Definition: ERF_WRFSurfaceFields.H:290
amrex::Long n_outside
Definition: ERF_WRFSurfaceFields.H:287
amrex::Long n_not_finite
Definition: ERF_WRFSurfaceFields.H:288
amrex::Real vmin
Definition: ERF_WRFSurfaceFields.H:292