ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_FireCoupler.H
Go to the documentation of this file.
1 #ifndef ERF_FIRE_COUPLER_H
2 #define ERF_FIRE_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>
10 #include <ERF_FireCouplingParams.H>
11 #include <ERF_FireEngine.H>
12 #include <ERF_FireGrid.H>
14 
15 class ERF;
16 class SurfaceLayer;
17 
18 /**
19  * @file ERF_FireCoupler.H
20  * @brief The atmosphere side of the fire coupling, shared by every fire engine
21  *
22  * ERF holds one FireCoupler (std::unique_ptr<FireCoupler> m_fire_coupler),
23  * which owns the fire engine selected by erf.fire.engine. The coupler:
24  * - checks the ERF configuration a fire grid needs and builds that grid on
25  * the level it refines (erf.fire.anchor_level);
26  * - each step of that level, hands the engine the atmospheric state
27  * (FireForcing) and advances it; with lagged or synchronous coupling, or
28  * with the smoke tracer, it averages the fluxes the engine reports
29  * (FireFluxes) onto the atmospheric columns, where they wait for the next
30  * step (the one-step explicit lag of WRF-Fire);
31  * - at every Runge-Kutta stage of that next step, injects the lagged heat and
32  * vapor into the cell sources with the WRF-Fire exponential profile
33  * (ERF_FireAtmCoupling.H; lagged and synchronous coupling only) and the
34  * smoke tracer (ERF_FireSmokeEmission.H; erf.fire.smoke_enable);
35  * - holds the lagged fluxes for the boundary-layer scheme and the checkpoint.
36  *
37  * Coarser levels see the fire through average-down, finer levels not at all.
38  */
40 {
41 public:
42  /**
43  * @param[in] params The coupling inputs (FireCouplingParams::read())
44  * @param[in] engine The fire engine erf.fire.engine selects
45  */
46  FireCoupler (const FireCouplingParams& params, std::unique_ptr<FireEngine> engine);
47 
48  /**
49  * @brief Check the configuration, build the fire grid and set the engine up
50  *
51  * Called once from ERF::InitData_post() after every surface layer exists.
52  * Runs the coupling's checks (verify_fire_prerequisites), then the
53  * engine's, builds the fire grid on level lev, allocates the lagged flux
54  * buffers (zero) and initializes the engine.
55  *
56  * @param[in] erf ERF instance (public AmrCore accessors only)
57  * @param[in] lev The level the fire grid refines (fire_anchor_level)
58  * @param[in] surface_layer The bottom surface layer, or nullptr (aborts when
59  * the engine needs one, FireEngine::needs_surface_layer)
60  * @param[in] z_phys_nd_atm Nodal height of level lev [m] (ERF always passes it)
61  */
62  void initialize (const ERF& erf,
63  int lev,
65  const amrex::MultiFab* z_phys_nd_atm);
66 
67  /**
68  * @brief Lay the coupling out on a fire grid: what initialize() does after
69  * its start-up checks and before it sets the engine up
70  *
71  * Takes the fire grid and allocates the lagged flux buffers (zero) on the
72  * k = 0 slabs of ba_atm, with 5 ghost columns. Public so that a test can
73  * drive the coupling with an engine of its own without an ERF object
74  * (erf-model/FLARE: Tests/Unit/Fire/ERF_GTestFireCoupler.cpp).
75  *
76  * @param[in] lev The level the fire grid refines
77  * @param[in] fg The fire grid (create_fire_grid)
78  * @param[in] ba_atm The level's boxes
79  * @param[in] dm_atm Their owners
80  * @param[in] geom_atm The level's geometry
81  */
82  void set_fire_grid (int lev, const FireGrid& fg,
83  const amrex::BoxArray& ba_atm,
84  const amrex::DistributionMapping& dm_atm,
85  const amrex::Geometry& geom_atm);
86 
87  /**
88  * @brief Advance the fire one step of its level and refill the lagged fluxes
89  *
90  * Called from ERF::Advance() once per step of the fire grid's level, after
91  * that level's dycore. The air temperature and humidity at k = 0 come from
92  * the state before the dycore; the wind from before the dycore (passive and
93  * lagged coupling) or after it (synchronous).
94  *
95  * @param[in] time Start of the step [s]
96  * @param[in] dt Step of the level [s], ERF's double dt[lev]: fields_time()
97  * becomes time + dt, the time ERF assigns to the level
98  * @param[in] S_old Conserved state before the dycore
99  * @param[in] xvel_old, yvel_old Face velocities before the dycore [m/s]
100  * @param[in] xvel_new, yvel_new Face velocities after the dycore [m/s]
101  * @param[in] z_phys_cc Cell-center height of the level [m]
102  * @param[in] ba2d The level's boxes coarsened to one cell in z
103  * @param[in] geom Geometry of the level
104  * @param[in] precip_sources The microphysics surface precipitation accumulators of the level
105  */
106  void advance (double time, double dt,
107  const amrex::MultiFab& S_old,
108  const amrex::MultiFab& xvel_old, const amrex::MultiFab& yvel_old,
109  const amrex::MultiFab& xvel_new, const amrex::MultiFab& yvel_new,
110  const amrex::MultiFab& z_phys_cc,
111  const amrex::BoxArray& ba2d,
112  const amrex::Geometry& geom,
113  const SurfacePrecipAccumulationSources& precip_sources);
114 
115  /**
116  * @brief Inject the lagged fire heat, vapor and smoke into the cell sources
117  *
118  * Called from the slow right-hand side at every Runge-Kutta stage on the
119  * fire grid's level, after make_sources has rebuilt cc_source. The lagged
120  * fluxes are fixed over the step, so every stage receives the same
121  * tendency (erf.fire.source_mode decides whether it replaces or adds to the
122  * theta and vapor sources). Heat and vapor only with lagged or
123  * synchronous coupling; smoke with erf.fire.smoke_enable.
124  *
125  * @param[in,out] cc_source Cell-centered sources of the level
126  * @param[in] S_old Conserved state at the start of the step (density)
127  * @param[in] z_phys_cc Cell-center height [m]
128  * @param[in] z_phys_nd Nodal height [m] (ERF always passes it): the cell
129  * thickness and roof datum of the heat, and the
130  * smoke cell's thickness
131  * @param[in] geom Geometry of the level
132  * @param[in] has_moisture The vapor source exists
133  * @param[in] step Step of the level, for the debug output
134  */
135  void add_sources (amrex::MultiFab& cc_source,
136  const amrex::MultiFab& S_old,
137  const amrex::MultiFab& z_phys_cc,
138  const amrex::MultiFab* z_phys_nd,
139  const amrex::Geometry& geom,
140  bool has_moisture,
141  int step);
142 
143  /**
144  * @brief Write the fire to a checkpoint: the engine's state
145  * (FireEngine::write_checkpoint), then the coupling's lagged fluxes
146  * (FireQAtmPrev and FireQLatAtmPrev) under checkpointname/Level_<lev>/
147  */
148  void write_checkpoint (const std::string& checkpointname) const;
149 
150  /**
151  * @brief Why a checkpoint's lagged fluxes do not belong to this run's fire
152  * grid, or an empty string when they do
153  *
154  * @param[in] chk_lev The level the checkpoint holds them on
155  * @param[in] chk_region The columns they cover (minimal box of their boxes)
156  */
157  std::string checkpoint_mismatch (int chk_lev, const amrex::Box& chk_region) const;
158 
159  /**
160  * @brief Restore the engine's state, then the lagged fluxes, on a restart
161  *
162  * Aborts when the checkpoint holds the lagged fluxes on another level or
163  * over another region than this run's fire grid. A checkpoint without them
164  * (no fire state, or written before they were checkpointed) leaves them at
165  * zero: the first restarted step injects no fire heat.
166  *
167  * @param[in] restart_chkfile The checkpoint directory
168  * @param[in] max_level ERF's max_level
169  * @param[in] step The step count of the fire grid's level at the checkpoint
170  */
171  void read_checkpoint (const std::string& restart_chkfile, int max_level, int step);
172 
173  /// Fire fields the ERF plotfile can carry (FireEngine::plot_var_names(), read at construction,
174  /// before ERF reads erf.plot_vars_*).
175  const std::vector<std::string>& plot_var_names () const { return m_plot_var_names; }
176 
177  /**
178  * @brief Fill component dcomp of an ERF plotfile MultiFab of level lev
179  * with the fire field name
180  *
181  * On the fire grid's level, the k = 0 cell of each column holds the mean
182  * of the engine's field (FireEngine::plot_var) over the fire cells under
183  * it; every other cell, every other level and a field the engine returns
184  * as nullptr hold zero.
185  *
186  * @param[in] name One of plot_var_names()
187  * @param[out] dst Cell-centered MultiFab on the boxes of level lev
188  * @param[in] dcomp Component of dst to fill
189  * @param[in] lev The level dst belongs to
190  */
191  void fill_plot_var (const std::string& name, amrex::MultiFab& dst, int dcomp, int lev) const;
192 
193  /// The level the fire grid refines (erf.fire.anchor_level, resolved at initialize).
194  int level () const { return m_lev; }
195 
196  /// The coupling inputs.
197  const FireCouplingParams& params () const { return m_params; }
198 
199  /// The fire engine.
200  FireEngine& engine () { return *m_engine; }
201  const FireEngine& engine () const { return *m_engine; }
202 
203  /// The fire grid.
204  const FireGrid& fire_grid () const { return m_fg; }
205 
206  /**
207  * @brief The time the engine's fields belong to [s], in ERF's double time
208  *
209  * ERF sets it to the fire level's time at start-up and on a restart
210  * (set_fields_time), advance() to the end of the step, and ERF again to
211  * the coarser level's time once the finer levels' substeps have reached
212  * it, so that it equals the time ERF assigns to the level and carries no
213  * round-off from summing substeps.
214  */
215  double fields_time () const { return m_fields_time; }
216  /// Set fields_time() to t [s] (ERF only: start-up, restart, end of a coarser step).
217  void set_fields_time (double t) { m_fields_time = t; }
218 
219  /**
220  * @brief Sensible heat flux [W/m2] of the previous fire step on the
221  * atmospheric columns of the fire grid's level, with 5 ghost columns
222  * (for a reader that samples a halo around each tile, such as a
223  * boundary-layer scheme taking the fire's heat into its mixing); zero
224  * outside the fire region. Allocated at initialize, so never null afterwards.
225  */
226  const amrex::MultiFab* get_Q_atm_prev () const { return m_Q_atm_prev.get(); }
227  /// Latent heat flux [W/m2] of the previous fire step, laid out as get_Q_atm_prev().
228  const amrex::MultiFab* get_Q_lat_atm_prev () const { return m_Q_lat_atm_prev.get(); }
229 
230 private:
231  /// Average the engine's fluxes onto the atmospheric columns for the next step.
232  void update_flux_buffer (const amrex::Geometry& geom_atm);
233 
235  std::unique_ptr<FireEngine> m_engine;
237  int m_lev = 0;
238  amrex::Geometry m_geom_atm; ///< geometry of the fire grid's level
239  double m_fields_time = 0.0; ///< the time the engine's fields belong to (fields_time())
240 
241  // The engine's fluxes averaged onto the atmospheric columns of the fire
242  // grid's level (one slab per box of that level), held from the previous
243  // step for injection at every stage of the next (one-step explicit lag).
244  // Their ghost columns hold the neighboring box's value for a reader that
245  // samples a halo around every tile; outside the refined region of a finer
246  // level they hold zero.
247  std::unique_ptr<amrex::MultiFab> m_Q_atm_prev; ///< sensible [W/m2]
248  std::unique_ptr<amrex::MultiFab> m_Q_lat_atm_prev; ///< latent [W/m2]
249 
250  std::vector<std::string> m_plot_var_names; ///< FireEngine::plot_var_names(), read at construction
251 };
252 
253 #endif
The inputs of the atmosphere side of the fire coupling.
The interface between the fire coupling and a fire engine.
Fire grid data structure and construction.
@ surface_layer
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
Definition: ERF_FireCoupler.H:40
FireGrid m_fg
Definition: ERF_FireCoupler.H:236
const FireCouplingParams & params() const
The coupling inputs.
Definition: ERF_FireCoupler.H:197
const amrex::MultiFab * get_Q_atm_prev() const
Sensible heat flux [W/m2] of the previous fire step on the atmospheric columns of the fire grid's lev...
Definition: ERF_FireCoupler.H:226
void add_sources(amrex::MultiFab &cc_source, const amrex::MultiFab &S_old, const amrex::MultiFab &z_phys_cc, const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, bool has_moisture, int step)
Inject the lagged fire heat, vapor and smoke into the cell sources.
Definition: ERF_FireCoupler.cpp:284
std::unique_ptr< FireEngine > m_engine
Definition: ERF_FireCoupler.H:235
void set_fire_grid(int lev, const FireGrid &fg, const amrex::BoxArray &ba_atm, const amrex::DistributionMapping &dm_atm, const amrex::Geometry &geom_atm)
Lay the coupling out on a fire grid: what initialize() does after its start-up checks and before it s...
Definition: ERF_FireCoupler.cpp:113
void initialize(const ERF &erf, int lev, const SurfaceLayer *surface_layer, const amrex::MultiFab *z_phys_nd_atm)
Check the configuration, build the fire grid and set the engine up.
Definition: ERF_FireCoupler.cpp:84
void write_checkpoint(const std::string &checkpointname) const
Write the fire to a checkpoint: the engine's state (FireEngine::write_checkpoint),...
Definition: ERF_FireCoupler.cpp:345
double fields_time() const
The time the engine's fields belong to [s], in ERF's double time.
Definition: ERF_FireCoupler.H:215
std::unique_ptr< amrex::MultiFab > m_Q_lat_atm_prev
latent [W/m2]
Definition: ERF_FireCoupler.H:248
void advance(double time, double dt, const amrex::MultiFab &S_old, const amrex::MultiFab &xvel_old, const amrex::MultiFab &yvel_old, const amrex::MultiFab &xvel_new, const amrex::MultiFab &yvel_new, const amrex::MultiFab &z_phys_cc, const amrex::BoxArray &ba2d, const amrex::Geometry &geom, const SurfacePrecipAccumulationSources &precip_sources)
Advance the fire one step of its level and refill the lagged fluxes.
Definition: ERF_FireCoupler.cpp:137
FireCouplingParams m_params
Definition: ERF_FireCoupler.H:234
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 fire field name.
Definition: ERF_FireCoupler.cpp:209
FireCoupler(const FireCouplingParams &params, std::unique_ptr< FireEngine > engine)
Definition: ERF_FireCoupler.cpp:71
FireEngine & engine()
The fire engine.
Definition: ERF_FireCoupler.H:200
int m_lev
Definition: ERF_FireCoupler.H:237
void update_flux_buffer(const amrex::Geometry &geom_atm)
Average the engine's fluxes onto the atmospheric columns for the next step.
Definition: ERF_FireCoupler.cpp:241
double m_fields_time
the time the engine's fields belong to (fields_time())
Definition: ERF_FireCoupler.H:239
void set_fields_time(double t)
Set fields_time() to t [s] (ERF only: start-up, restart, end of a coarser step).
Definition: ERF_FireCoupler.H:217
std::vector< std::string > m_plot_var_names
FireEngine::plot_var_names(), read at construction.
Definition: ERF_FireCoupler.H:250
const FireEngine & engine() const
Definition: ERF_FireCoupler.H:201
const amrex::MultiFab * get_Q_lat_atm_prev() const
Latent heat flux [W/m2] of the previous fire step, laid out as get_Q_atm_prev().
Definition: ERF_FireCoupler.H:228
const std::vector< std::string > & plot_var_names() const
Definition: ERF_FireCoupler.H:175
amrex::Geometry m_geom_atm
geometry of the fire grid's level
Definition: ERF_FireCoupler.H:238
int level() const
The level the fire grid refines (erf.fire.anchor_level, resolved at initialize).
Definition: ERF_FireCoupler.H:194
std::unique_ptr< amrex::MultiFab > m_Q_atm_prev
sensible [W/m2]
Definition: ERF_FireCoupler.H:247
void read_checkpoint(const std::string &restart_chkfile, int max_level, int step)
Restore the engine's state, then the lagged fluxes, on a restart.
Definition: ERF_FireCoupler.cpp:377
std::string checkpoint_mismatch(int chk_lev, const amrex::Box &chk_region) const
Why a checkpoint's lagged fluxes do not belong to this run's fire grid, or an empty string when they ...
Definition: ERF_FireCoupler.cpp:362
const FireGrid & fire_grid() const
The fire grid.
Definition: ERF_FireCoupler.H:204
A fire engine behind the shared coupling layer.
Definition: ERF_FireEngine.H:132
Definition: ERF_SurfaceLayer.H:64
Definition: ERF_InterpolationUtils.H:15
@ fg
Definition: ERF_AdvanceMorrison.cpp:111
@ t
Definition: ERF_WSM6.H:295
Definition: ERF_FireCouplingParams.H:18
2D horizontal fire computational grid
Definition: ERF_FireGrid.H:42
Definition: ERF_SurfacePrecipitation.H:40