ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_FloodEngine.H
Go to the documentation of this file.
1 #ifndef ERF_FLOOD_ENGINE_H
2 #define ERF_FLOOD_ENGINE_H
3 
4 #include <AMReX.H>
5 #include <AMReX_MultiFab.H>
6 #include <AMReX_REAL.H>
7 #include <string>
8 #include <vector>
9 #include <ERF_FloodGrid.H>
10 
11 class ERF;
12 
13 /**
14  * @file ERF_FloodEngine.H
15  * @brief The interface between the flood coupling and a flood engine
16  *
17  * A flood engine is a surface-water model (shallow water, a runoff or routing
18  * model) that runs on the flood grid and takes the atmosphere's surface
19  * precipitation as its source. The shared coupling layer (FloodCoupler) owns
20  * everything that touches the atmosphere: the flood grid's placement on one
21  * level, the precipitation of each step taken from the microphysics
22  * accumulators, the wind handed to the engine, the engine's fields in the ERF
23  * plotfile and the check that a restart uses the same flood grid. An engine
24  * sees only this interface, FloodForcing and the helpers of ERF_FloodGrid.H.
25  *
26  * The flood coupling is one-way in this version: the engine reads the
27  * atmosphere and returns nothing to it.
28  *
29  * ERF itself contains no engine. An engine lives in its own directory outside
30  * ERF (FLOOD_HOME in GNU make, ERF_FLOOD_ENGINE_DIRS in CMake) and registers
31  * itself with ERF_REGISTER_FLOOD_ENGINE (ERF_FloodEngineRegistry.H).
32  * ERF_FloodEngineTemplate.H declares every method below for new engines: a
33  * method added to FloodEngine is declared there too (a pure virtual one that
34  * is missing fails the build; an optional one does not).
35  *
36  * Memory contract. Every MultiFab the coupling hands an engine (FloodForcing,
37  * the FloodGrid layout) and every one the engine hands back (plot_var) is an
38  * ordinary AMReX MultiFab in the default arena, so on a GPU build its data is
39  * on the device. An engine that runs on the host:
40  * - copies what it reads from FloodForcing to the host inside advance(),
41  * because those fields are temporaries of that call, and synchronizes the
42  * GPU stream (amrex::Gpu::streamSynchronize()) before it reads them there;
43  * - owns the MultiFabs it returns, allocated on fg.ba and fg.dm, keeps them
44  * alive until its next advance(), and copies them back to device memory
45  * before returning them.
46  * The coupling never frees, resizes or keeps across steps a pointer an engine
47  * returns.
48  */
49 
50 /**
51  * @brief The atmospheric state a flood engine reads for one step
52  *
53  * Every field lives on the level the flood grid refines
54  * (erf.flood.anchor_level), in that level's index space; the engine maps them
55  * onto its own grid (FloodGrid::atm_lo gives the corner of the flood region,
56  * fill_flood_from_atm_k0 copies a column field onto the flood grid). The k = 0
57  * slabs hold one cell in z per box of the level. The slabs are temporaries of
58  * FloodCoupler::advance(): an engine reads them during its advance() and keeps
59  * no pointer to them.
60  */
62 {
63  double time; ///< Start of the step [s], in double as ERF's own clock
64  amrex::Real dt; ///< Step of the flood grid's level [s]
65  const amrex::MultiFab& xvel; ///< x-velocity on x-faces [m/s], after this step's dycore
66  const amrex::MultiFab& yvel; ///< y-velocity on y-faces [m/s], after this step's dycore
67  const amrex::MultiFab& z_phys_cc; ///< Cell-center height [m] of the terrain-following mesh
68  /**
69  * Surface precipitation of this step on the k = 0 slabs [kg/m2, equal to
70  * mm of liquid water]: component 0 the liquid part, component 1 the frozen
71  * part (snow, graupel and hail as liquid-water equivalent; WSM6 and WDM6
72  * also count their cloud-ice fallout as frozen, while Morrison counts its
73  * cloud-water and cloud-ice fallout as liquid). It is the change of the
74  * microphysics accumulators over the step, so the sum over the steps of a
75  * run is the change of ERF's own accumulation over the run. Not bounded at
76  * zero: the part that is a difference of two accumulators (the liquid part
77  * for Morrison, the frozen part for WSM6 and WDM6) can be slightly negative
78  * from round-off; an engine that bounds it should carry the negative part
79  * to the next step. Where a finer level covers the
80  * flood grid's level, ERF averages the finer accumulators down onto it
81  * after the flood's step (and, with super-droplets, at plotfiles and
82  * regrids); the next step carries that correction, of either sign, in the
83  * columns under the finer level. In single precision the per-step value is
84  * resolved to the round-off of the accumulator. nullptr unless the engine
85  * asks for it (FloodEngine::wants_surface_precip).
86  */
87  const amrex::MultiFab* precip_k0;
88 };
89 
90 /**
91  * @brief A flood engine behind the shared coupling layer
92  *
93  * The coupling calls, in this order:
94  * - plot_var_names() once, when the coupler is constructed (before
95  * verify_prerequisites and before ERF reads its plot variable lists);
96  * - verify_prerequisites() once at start-up, after its own checks of the
97  * ERF configuration and before it builds the flood grid;
98  * - initialize() once, with the flood grid built, then start_run();
99  * - read_checkpoint() on a restart from a checkpoint that holds flood state,
100  * right after start_run();
101  * - advance() once per step of the flood grid's level, after that level's
102  * dycore and microphysics;
103  * - write_plotfile() on the flood plotfile schedule (erf.flood.plot_int,
104  * erf.flood.plot_per), write_checkpoint() when ERF writes a checkpoint, and
105  * plot_var() each time ERF writes its own plotfile, for the flood fields
106  * that file carries.
107  */
109 {
110 public:
111  virtual ~FloodEngine() = default;
112 
113  /// Name of the engine, as erf.flood.engine selects it.
114  virtual const char* name () const = 0;
115 
116  /**
117  * @brief Check the engine's own inputs against the ERF configuration
118  *
119  * Called once at start-up, after the coupling's checks of the grid
120  * (verify_flood_prerequisites) and before the flood grid is built. Aborts,
121  * naming the input, on a configuration the engine cannot run.
122  *
123  * @param[in] erf ERF instance (public AmrCore accessors only)
124  * @param[in] lev The level the flood grid refines
125  */
126  virtual void verify_prerequisites (const ERF& erf, int lev) = 0;
127 
128  /**
129  * @brief Set the engine up on the flood grid
130  *
131  * @param[in] erf ERF instance (public AmrCore accessors only)
132  * @param[in] lev The level the flood grid refines
133  * @param[in] fg The flood grid the coupling built
134  * @param[in] z_phys_nd_atm Nodal height of level lev [m]: the terrain
135  * the atmosphere sees, at k = 0
136  */
137  virtual void initialize (const ERF& erf,
138  int lev,
139  const FloodGrid& fg,
140  const amrex::MultiFab* z_phys_nd_atm) = 0;
141 
142  /**
143  * @brief Start-up after initialize(), once the run's end is known
144  *
145  * @param[in] stop_time Elapsed time at which the run stops [s], on the clock the
146  * engine's step times use (ERF's stop_time less start_time);
147  * numeric_limits<amrex::Real>::max() when no stop time is set
148  * @param[in] max_step ERF's max_step, counting level-0 steps;
149  * numeric_limits<int>::max() when unset (never negative)
150  * @param[in] steps_per_coarse_step Steps of the flood grid's level per level-0 step
151  */
152  virtual void start_run (amrex::Real stop_time, int max_step, int steps_per_coarse_step)
153  {
154  amrex::ignore_unused(stop_time, max_step, steps_per_coarse_step);
155  }
156 
157  /// Advance the flood over one step of its level.
158  virtual void advance (const FloodForcing& forcing) = 0;
159 
160  /**
161  * @brief The engine takes the surface precipitation in FloodForcing
162  *
163  * True by default: the coupling then stops at start-up unless the
164  * microphysics scheme accumulates surface precipitation. An engine driven
165  * by other sources only (an inflow hydrograph) returns false.
166  */
167  virtual bool wants_surface_precip () const { return true; }
168 
169  /**
170  * @brief Write the engine's own state to the checkpoint directory
171  *
172  * Everything the engine carries from one step to the next, under
173  * checkpointname/Level_<lev>/ of the flood grid's level. The default
174  * writes nothing; such an engine cannot restart (read_checkpoint).
175  */
176  virtual void write_checkpoint (const std::string& checkpointname) const
177  {
178  amrex::ignore_unused(checkpointname);
179  }
180 
181  /**
182  * @brief Restore the engine's state from a checkpoint on a restart
183  *
184  * Called only when the checkpoint holds flood state (it was written with
185  * erf.flood.enable = true) and the coupling has checked that it belongs to
186  * this run's flood grid. A restart from a checkpoint without flood state
187  * (an atmosphere-only spin-up) skips this call, and the engine continues
188  * from its initial state.
189  *
190  * @param[in] restart_chkfile The checkpoint directory
191  * @param[in] step The step count of the flood grid's level at the checkpoint
192  *
193  * The default stops the run: an engine that does not implement restart
194  * says so instead of continuing from its initial state.
195  */
196  virtual void read_checkpoint (const std::string& restart_chkfile, int step)
197  {
198  amrex::ignore_unused(restart_chkfile, step);
199  amrex::Abort(std::string("[FLOOD] flood engine \"") + name()
200  + "\" does not support restart from a checkpoint");
201  }
202 
203  /**
204  * @brief Write the engine's plotfile of the flood grid
205  *
206  * @param[in] plotfile_prefix erf.flood.plot_file (the step is appended)
207  * @param[in] time Simulation time [s]
208  * @param[in] step Level-0 step number
209  *
210  * The default writes nothing (the engine's fields may still reach the ERF
211  * plotfile through plot_var_names()).
212  */
213  virtual void write_plotfile (const std::string& plotfile_prefix, amrex::Real time, int step) const
214  {
215  amrex::ignore_unused(plotfile_prefix, time, step);
216  }
217 
218  /**
219  * @brief Flood fields the ERF plotfile can carry, by name
220  *
221  * The names the deck may list in erf.plot_vars_*. Each must start with
222  * "flood_" (the coupler stops otherwise), so they never collide with an
223  * ERF variable. The coupler asks once, when it is constructed, so the
224  * answer depends on the engine's inputs only. The default lists none.
225  */
226  virtual std::vector<std::string> plot_var_names () const { return {}; }
227 
228  /**
229  * @brief The field plot_var_names() lists as name, on the flood grid
230  *
231  * One component, on FloodGrid::ba and FloodGrid::dm, valid until the next
232  * advance(). The coupler averages it over each atmospheric column (the
233  * grid_ratio^2 flood cells under it) and writes it into the k = 0 layer of
234  * the ERF plotfile, zero elsewhere. nullptr writes zeros.
235  */
236  virtual const amrex::MultiFab* plot_var (const std::string& name) const
237  {
238  amrex::ignore_unused(name);
239  return nullptr;
240  }
241 };
242 
243 #endif
Flood grid data structure, construction and the maps between the flood grid and the atmospheric colum...
A flood engine behind the shared coupling layer.
Definition: ERF_FloodEngine.H:109
virtual const amrex::MultiFab * plot_var(const std::string &name) const
The field plot_var_names() lists as name, on the flood grid.
Definition: ERF_FloodEngine.H:236
virtual void start_run(amrex::Real stop_time, int max_step, int steps_per_coarse_step)
Start-up after initialize(), once the run's end is known.
Definition: ERF_FloodEngine.H:152
virtual void advance(const FloodForcing &forcing)=0
Advance the flood over one step of its level.
virtual void initialize(const ERF &erf, int lev, const FloodGrid &fg, const amrex::MultiFab *z_phys_nd_atm)=0
Set the engine up on the flood grid.
virtual std::vector< std::string > plot_var_names() const
Flood fields the ERF plotfile can carry, by name.
Definition: ERF_FloodEngine.H:226
virtual void write_plotfile(const std::string &plotfile_prefix, amrex::Real time, int step) const
Write the engine's plotfile of the flood grid.
Definition: ERF_FloodEngine.H:213
virtual void read_checkpoint(const std::string &restart_chkfile, int step)
Restore the engine's state from a checkpoint on a restart.
Definition: ERF_FloodEngine.H:196
virtual void verify_prerequisites(const ERF &erf, int lev)=0
Check the engine's own inputs against the ERF configuration.
virtual bool wants_surface_precip() const
The engine takes the surface precipitation in FloodForcing.
Definition: ERF_FloodEngine.H:167
virtual ~FloodEngine()=default
virtual const char * name() const =0
Name of the engine, as erf.flood.engine selects it.
virtual void write_checkpoint(const std::string &checkpointname) const
Write the engine's own state to the checkpoint directory.
Definition: ERF_FloodEngine.H:176
Definition: ERF_InterpolationUtils.H:15
@ fg
Definition: ERF_AdvanceMorrison.cpp:111
The atmospheric state a flood engine reads for one step.
Definition: ERF_FloodEngine.H:62
const amrex::MultiFab & z_phys_cc
Cell-center height [m] of the terrain-following mesh.
Definition: ERF_FloodEngine.H:67
double time
Start of the step [s], in double as ERF's own clock.
Definition: ERF_FloodEngine.H:63
amrex::Real dt
Step of the flood grid's level [s].
Definition: ERF_FloodEngine.H:64
const amrex::MultiFab & yvel
y-velocity on y-faces [m/s], after this step's dycore
Definition: ERF_FloodEngine.H:66
const amrex::MultiFab * precip_k0
Definition: ERF_FloodEngine.H:87
const amrex::MultiFab & xvel
x-velocity on x-faces [m/s], after this step's dycore
Definition: ERF_FloodEngine.H:65
2D horizontal flood grid
Definition: ERF_FloodGrid.H:42