ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_FloodCoupler.H
Go to the documentation of this file.
1 #ifndef ERF_FLOOD_COUPLER_H
2 #define ERF_FLOOD_COUPLER_H
3 
4 #include <memory>
5 #include <string>
6 #include <vector>
7 #include <AMReX_MultiFab.H>
8 #include <AMReX_Geometry.H>
9 #include <AMReX_BoxArray.H>
11 #include <ERF_FloodEngine.H>
12 #include <ERF_FloodGrid.H>
14 
15 class ERF;
16 
17 /**
18  * @file ERF_FloodCoupler.H
19  * @brief The atmosphere side of the flood coupling, shared by every flood engine
20  *
21  * ERF holds one FloodCoupler (std::unique_ptr<FloodCoupler> m_flood_coupler),
22  * which owns the flood engine selected by erf.flood.engine. The coupler:
23  * - checks the ERF configuration a flood grid needs and builds that grid on
24  * the level it refines (erf.flood.anchor_level);
25  * - keeps the microphysics surface precipitation accumulation it saw last, and
26  * each step of that level hands the engine the change since then (liquid and
27  * frozen), with the wind after the dycore (FloodForcing);
28  * - writes the engine's fields into the ERF plotfile on request;
29  * - records the flood grid in the checkpoint and refuses a restart onto
30  * another one.
31  *
32  * The coupling is one-way: nothing returns to the atmosphere.
33  */
35 {
36 public:
37  /**
38  * @param[in] params The coupling inputs (FloodCouplingParams::read())
39  * @param[in] engine The flood engine erf.flood.engine selects
40  *
41  * Aborts when the engine lists a plot field whose name does not start with
42  * "flood_" or lists a name twice.
43  */
44  FloodCoupler (const FloodCouplingParams& params, std::unique_ptr<FloodEngine> engine);
45 
46  /**
47  * @brief Check the configuration, build the flood grid and set the engine up
48  *
49  * Called once from ERF::InitData_post(), on a fresh start and on a restart
50  * alike. Runs the coupling's checks (verify_flood_prerequisites), then the
51  * engine's, builds the flood grid on level lev and initializes the engine.
52  *
53  * @param[in] erf ERF instance (public AmrCore accessors only)
54  * @param[in] lev The level the flood grid refines (flood_anchor_level)
55  * @param[in] z_phys_nd_atm Nodal height of level lev [m]
56  */
57  void initialize (const ERF& erf, int lev, const amrex::MultiFab* z_phys_nd_atm);
58 
59  /**
60  * @brief Lay the coupling out on a flood grid: what initialize() does after
61  * its start-up checks and before it sets the engine up
62  *
63  * Takes the flood grid and the k = 0 slabs of ba_atm. Public so that a test
64  * can drive the coupling with an engine of its own without an ERF object.
65  *
66  * @param[in] lev The level the flood grid refines
67  * @param[in] fg The flood grid (create_flood_grid)
68  * @param[in] ba_atm The level's boxes, each spanning the level's height
69  * @param[in] dm_atm Their owners
70  * @param[in] geom_atm The level's geometry
71  */
72  void set_flood_grid (int lev, const FloodGrid& fg,
73  const amrex::BoxArray& ba_atm,
74  const amrex::DistributionMapping& dm_atm,
75  const amrex::Geometry& geom_atm);
76 
77  /**
78  * @brief Take the current surface precipitation accumulation as the
79  * reference the next step's precipitation is measured from
80  *
81  * Called once at start-up, after initialize(), when the engine reads the
82  * precipitation (FloodEngine::wants_surface_precip). On a fresh start the
83  * accumulators are zero; on a restart they hold the checkpoint's values, so
84  * the first restarted step hands the engine the same precipitation as the
85  * straight run.
86  *
87  * @param[in] precip_sources The microphysics surface precipitation accumulators of the level
88  */
89  void set_precip_reference (const SurfacePrecipAccumulationSources& precip_sources);
90 
91  /**
92  * @brief Advance the flood one step of its level
93  *
94  * Called from ERF::Advance() once per step of the flood grid's level, after
95  * that level's dycore and microphysics.
96  *
97  * @param[in] time Start of the step [s]
98  * @param[in] dt Step of the level [s], ERF's double dt[lev]: fields_time()
99  * becomes time + dt, the time ERF assigns to the level
100  * @param[in] xvel, yvel Face velocities after the dycore [m/s]
101  * @param[in] z_phys_cc Cell-center height of the level [m]
102  * @param[in] precip_sources The microphysics surface precipitation accumulators of the level
103  */
104  void advance (double time, double dt,
105  const amrex::MultiFab& xvel, const amrex::MultiFab& yvel,
106  const amrex::MultiFab& z_phys_cc,
107  const SurfacePrecipAccumulationSources& precip_sources);
108 
109  /**
110  * @brief Write the flood to a checkpoint: the engine's state
111  * (FloodEngine::write_checkpoint), then the flood grid's level,
112  * ratio and region in checkpointname/Level_<lev>/FloodGrid, and the
113  * precipitation reference in Level_<lev>/FloodPrecipRef, or in
114  * Level_<lev>/FloodPrecipRefIce for a scheme whose cloud ice counts as
115  * frozen (SurfacePrecipAccumulationSources::rest_is_frozen)
116  */
117  void write_checkpoint (const std::string& checkpointname) const;
118 
119  /**
120  * @brief Why a checkpoint's flood state does not belong to this run's flood
121  * grid, or an empty string when it does
122  *
123  * @param[in] chk_lev The level the checkpoint's flood grid refines
124  * @param[in] chk_ratio Its grid ratio
125  * @param[in] chk_region The atmospheric columns it covers
126  */
127  std::string checkpoint_mismatch (int chk_lev, int chk_ratio, const amrex::Box& chk_region) const;
128 
129  /**
130  * @brief Restore the engine's state on a restart
131  *
132  * A checkpoint written with a flood engine holds Level_<lev>/FloodGrid: the
133  * coupler aborts when it names another level, ratio or region than this run,
134  * and otherwise calls FloodEngine::read_checkpoint. A checkpoint without it
135  * (an atmosphere-only spin-up) leaves the engine in its initial state, said
136  * once.
137  *
138  * @param[in] restart_chkfile The checkpoint directory
139  * @param[in] step The step count of the flood grid's level at the checkpoint
140  */
141  void read_checkpoint (const std::string& restart_chkfile, int step);
142 
143  /// Flood fields the ERF plotfile can carry (FloodEngine::plot_var_names(), read at construction).
144  const std::vector<std::string>& plot_var_names () const { return m_plot_var_names; }
145 
146  /**
147  * @brief Fill component dcomp of an ERF plotfile MultiFab of level lev
148  * with the flood field name
149  *
150  * On the flood grid's level, the k = 0 cell of each column holds the mean
151  * of the engine's field over the flood cells under it; every other cell,
152  * every other level and a field the engine returns as nullptr hold zero.
153  */
154  void fill_plot_var (const std::string& name, amrex::MultiFab& dst, int dcomp, int lev) const;
155 
156  /// The level the flood grid refines (erf.flood.anchor_level, resolved at initialize).
157  int level () const { return m_lev; }
158 
159  /// The coupling inputs.
160  const FloodCouplingParams& params () const { return m_params; }
161 
162  /// The flood engine.
163  FloodEngine& engine () { return *m_engine; }
164  const FloodEngine& engine () const { return *m_engine; }
165 
166  /// The flood grid.
167  const FloodGrid& flood_grid () const { return m_fg; }
168 
169  /**
170  * @brief The time the engine's fields belong to [s], in ERF's double time
171  *
172  * ERF sets it at start-up and on a restart, advance() to the end of the
173  * step, and ERF again to the coarser level's time once the finer levels'
174  * substeps have reached it.
175  */
176  double fields_time () const { return m_fields_time; }
177  /// Set fields_time() to t [s] (ERF only: start-up, restart, end of a coarser step).
178  void set_fields_time (double t) { m_fields_time = t; }
179 
180 private:
182  std::unique_ptr<FloodEngine> m_engine;
184  int m_lev = 0;
185  bool m_grid_set = false;
186  amrex::Geometry m_geom_atm; ///< geometry of the flood grid's level
187  amrex::BoxArray m_ba_cols; ///< the k = 0 slab of each box of the level
188  amrex::DistributionMapping m_dm_cols; ///< their owners (the level's)
189  double m_fields_time = 0.0;
190 
191  /// Liquid (0) and frozen (1) accumulation [kg/m2] at the end of the last
192  /// step, on m_ba_cols; null until set_precip_reference()
193  std::unique_ptr<amrex::MultiFab> m_precip_prev;
194  /// The sources' rest_is_frozen at set_precip_reference(): which split the reference holds
195  bool m_precip_rest_frozen = false;
196 
197  /// The checkpoint name of the reference. Before the cloud ice of WSM6 and WDM6
198  /// counted as frozen, their reference had it in the liquid part, under FloodPrecipRef.
199  std::string precip_ref_name () const { return m_precip_rest_frozen ? "FloodPrecipRefIce" : "FloodPrecipRef"; }
200 
201  std::vector<std::string> m_plot_var_names;
202 };
203 
204 #endif
The inputs of the atmosphere side of the flood coupling.
The interface between the flood coupling and a flood engine.
Flood grid data structure, construction and the maps between the flood grid and the atmospheric colum...
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
Definition: ERF_FloodCoupler.H:35
std::vector< std::string > m_plot_var_names
Definition: ERF_FloodCoupler.H:201
const std::vector< std::string > & plot_var_names() const
Flood fields the ERF plotfile can carry (FloodEngine::plot_var_names(), read at construction).
Definition: ERF_FloodCoupler.H:144
void initialize(const ERF &erf, int lev, const amrex::MultiFab *z_phys_nd_atm)
Check the configuration, build the flood grid and set the engine up.
Definition: ERF_FloodCoupler.cpp:71
void advance(double time, double dt, const amrex::MultiFab &xvel, const amrex::MultiFab &yvel, const amrex::MultiFab &z_phys_cc, const SurfacePrecipAccumulationSources &precip_sources)
Advance the flood one step of its level.
Definition: ERF_FloodCoupler.cpp:119
const FloodCouplingParams & params() const
The coupling inputs.
Definition: ERF_FloodCoupler.H:160
amrex::Geometry m_geom_atm
geometry of the flood grid's level
Definition: ERF_FloodCoupler.H:186
void fill_plot_var(const std::string &name, amrex::MultiFab &dst, int dcomp, int lev) const
Fill component dcomp of an ERF plotfile MultiFab of level lev with the flood field name.
Definition: ERF_FloodCoupler.cpp:157
double fields_time() const
The time the engine's fields belong to [s], in ERF's double time.
Definition: ERF_FloodCoupler.H:176
amrex::BoxArray m_ba_cols
the k = 0 slab of each box of the level
Definition: ERF_FloodCoupler.H:187
amrex::DistributionMapping m_dm_cols
their owners (the level's)
Definition: ERF_FloodCoupler.H:188
int level() const
The level the flood grid refines (erf.flood.anchor_level, resolved at initialize).
Definition: ERF_FloodCoupler.H:157
double m_fields_time
Definition: ERF_FloodCoupler.H:189
void set_flood_grid(int lev, const FloodGrid &fg, const amrex::BoxArray &ba_atm, const amrex::DistributionMapping &dm_atm, const amrex::Geometry &geom_atm)
Lay the coupling out on a flood grid: what initialize() does after its start-up checks and before it ...
Definition: ERF_FloodCoupler.cpp:87
std::unique_ptr< FloodEngine > m_engine
Definition: ERF_FloodCoupler.H:182
FloodEngine & engine()
The flood engine.
Definition: ERF_FloodCoupler.H:163
FloodCoupler(const FloodCouplingParams &params, std::unique_ptr< FloodEngine > engine)
Definition: ERF_FloodCoupler.cpp:47
std::string precip_ref_name() const
Definition: ERF_FloodCoupler.H:199
const FloodGrid & flood_grid() const
The flood grid.
Definition: ERF_FloodCoupler.H:167
void write_checkpoint(const std::string &checkpointname) const
Write the flood to a checkpoint: the engine's state (FloodEngine::write_checkpoint),...
Definition: ERF_FloodCoupler.cpp:186
bool m_grid_set
Definition: ERF_FloodCoupler.H:185
FloodGrid m_fg
Definition: ERF_FloodCoupler.H:183
const FloodEngine & engine() const
Definition: ERF_FloodCoupler.H:164
FloodCouplingParams m_params
Definition: ERF_FloodCoupler.H:181
std::string checkpoint_mismatch(int chk_lev, int chk_ratio, const amrex::Box &chk_region) const
Why a checkpoint's flood state does not belong to this run's flood grid, or an empty string when it d...
Definition: ERF_FloodCoupler.cpp:215
int m_lev
Definition: ERF_FloodCoupler.H:184
std::unique_ptr< amrex::MultiFab > m_precip_prev
Definition: ERF_FloodCoupler.H:193
void read_checkpoint(const std::string &restart_chkfile, int step)
Restore the engine's state on a restart.
Definition: ERF_FloodCoupler.cpp:233
void set_fields_time(double t)
Set fields_time() to t [s] (ERF only: start-up, restart, end of a coarser step).
Definition: ERF_FloodCoupler.H:178
void set_precip_reference(const SurfacePrecipAccumulationSources &precip_sources)
Take the current surface precipitation accumulation as the reference the next step's precipitation is...
Definition: ERF_FloodCoupler.cpp:110
bool m_precip_rest_frozen
The sources' rest_is_frozen at set_precip_reference(): which split the reference holds.
Definition: ERF_FloodCoupler.H:195
A flood engine behind the shared coupling layer.
Definition: ERF_FloodEngine.H:109
Definition: ERF_InterpolationUtils.H:15
@ fg
Definition: ERF_AdvanceMorrison.cpp:111
@ xvel
Definition: ERF_IndexDefines.H:232
@ yvel
Definition: ERF_IndexDefines.H:233
@ t
Definition: ERF_WSM6.H:295
Definition: ERF_FloodCouplingParams.H:20
2D horizontal flood grid
Definition: ERF_FloodGrid.H:42
Definition: ERF_SurfacePrecipitation.H:40