ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_StructureModel.H
Go to the documentation of this file.
1 #ifndef ERF_STRUCTURE_MODEL_H
2 #define ERF_STRUCTURE_MODEL_H
3 
4 #include <AMReX.H>
5 #include <AMReX_FArrayBox.H>
6 #include <AMReX_Geometry.H>
7 #include <AMReX_MultiFab.H>
8 #include <AMReX_REAL.H>
9 #include <AMReX_RealBox.H>
10 
11 #include <functional>
12 #include <string>
13 #include <vector>
14 
15 /**
16  * @file ERF_StructureModel.H
17  * @brief The interface between ERF and a model of structures in the flow
18  *
19  * A structure model represents bodies that the air loads and that push back on
20  * the air: power-line conductors and their towers, for example. It samples the
21  * wind at its own points, advances its own dynamics, and returns a momentum
22  * source that ERF adds to the air at every Runge-Kutta stage. The velocity
23  * sampler and the force spreading of the actuator core (Source/MovingBodies/Core,
24  * ERF_ActuatorSampling.H and ERF_ActuatorSpreading.H) are the tools a model is
25  * expected to use for both.
26  *
27  * ERF itself contains no structure model. A model lives in its own directory
28  * outside ERF (STRUCTURE_HOME in GNU make, ERF_STRUCTURE_MODEL_DIRS in CMake)
29  * and registers itself with ERF_REGISTER_STRUCTURE_MODEL
30  * (ERF_StructureModelRegistry.H); erf.structures.model selects it by name.
31  * Tests/StructureModelExample is a complete model directory.
32  *
33  * ERF calls, in this order:
34  * - the registered creator once, in the ERF constructor (the model reads its
35  * own inputs there, under its own prefix);
36  * - level() each time ERF needs the level the model couples to;
37  * - initialize() once, after the initial data (or the checkpoint) exist and
38  * before the step-0 plotfile; on a restart the model restores its own state
39  * there, from StructureInit::restart_chkfile. It is not called again after a
40  * regrid: what the model needs of the mesh it reads from StructureStep each step;
41  * - advance() once per step of level(), with the state at the start of the step,
42  * before that step's Runge-Kutta stages;
43  * - support() right after each advance(): when a finer level than level() exists,
44  * ERF stops the run if that level's boxes overlap the support (the force would
45  * be lost there, see level());
46  * - add_momentum_sources() at every Runge-Kutta stage of level(), and, with
47  * erf.coupling_type = TwoWay, once at the start of each step of every coarser
48  * level, into zeroed MultiFabs on the grids of level(), which ERF restricts
49  * onto that level (see add_momentum_sources());
50  * - write_checkpoint() each time ERF writes a checkpoint;
51  * - finalize() once, when the run ends normally, after the last plotfile and checkpoint;
52  * - plot_var_names() each time ERF builds its plotfile variable list, and
53  * fill_plot_var() for each listed name on every level of each plotfile.
54  *
55  * Memory contract. Every MultiFab ERF hands a model is an ordinary AMReX
56  * MultiFab in the default arena (device memory on a GPU build) and is valid
57  * only during the call. A model keeps no pointer to it after the call returns.
58  */
59 
60 /**
61  * @brief What a structure model reads once, at start-up
62  *
63  * All fields live on the level the model couples to (StructureModel::level()).
64  */
66 {
67  int lev; ///< The level the model couples to
68  double time; ///< Simulation time [s]: 0, or the checkpoint's time on a restart
69  const amrex::Geometry& geom; ///< Geometry of level lev
70  const amrex::MultiFab& xvel; ///< x-velocity on x-faces [m/s]
71  const amrex::MultiFab* z_phys_nd; ///< Nodal height [m] of a stretched or terrain-following mesh; nullptr on a uniform-dz mesh (flat ground at prob_lo z)
72  const amrex::MultiFab* detJ_cc; ///< Cell Jacobian of a stretched or terrain-following mesh; nullptr on a uniform-dz mesh
73  /// With erf.terrain_type = ImmersedForcing: the terrain height [m] at the nodes of level lev
74  /// (a k = 0 slab over the domain grown by 3 nodes), from the surface the immersed boundary
75  /// was built from, in host-readable memory and valid only during initialize(); nullptr
76  /// otherwise, or when wants_immersed_terrain_surface() is false
77  const amrex::FArrayBox* immersed_terrain_surface;
78  bool unit_map_factors; ///< Every map factor of level lev is 1 (the actuator core assumes so)
79  bool keqn_rans; ///< Level lev runs the k-equation RANS closure, so cons holds rho k
80  amrex::Real Cmu0; ///< The closure's C_mu^0 on level lev
81  std::string restart_chkfile; ///< The checkpoint ERF restarts from; empty on a fresh start
82  /// The nominal step of level lev [s]: erf.fixed_dt divided by the sub-cycling ratios down to lev,
83  /// after the CFL limit of erf.fixed_dt_cfl_limit; ERF calls fixed_dt_changed() if it lowers it later.
84  /// Not positive when the run has no fixed step (ERF then chooses each step from erf.cfl).
85  /// StructureStep::dt is the step taken: it differs on the first steps with erf.init_shrink
86  /// below 1 or after a restart (growth limited by erf.change_max), and on a last step that
87  /// ERF fits to the stop time
88  double fixed_dt;
89  /// The time [s], on the clock of time, at which the run stops: the input stop_time, or
90  /// stop_datetime less start_datetime; the largest double when neither is given. It is ERF's
91  /// own stop, which can differ from the input by about 1e-7 s when start_datetime is given,
92  /// so compare with a tolerance. max_step may end the run earlier
93  double stop_time;
94  /// The run stops after this many level-0 steps, counted from the start of the first run (a
95  /// restart continues the count); the largest int when max_step is not given or negative
96  int max_step;
97  /// A level of the run, not only lev, is FullyCompressible (erf.compressibility_type); false
98  /// when every level is Anelastic or PseudoIncompressible. It names the solver, not the time
99  /// scheme: a FullyCompressible level with erf.timestepping_type = RK3_NoSubstepping counts
101 };
102 
103 /**
104  * @brief What a structure model may read from the fire, when the run has one
105  *
106  * The fire engine's fields (the getters of ERF_FireEngine.H) on the fire grid: a
107  * horizontal grid, one cell in z, that refines one atmospheric level
108  * (erf.fire.anchor_level). That level need not be the one the model couples to.
109  * Fire cell (i, j) has its center at geom.ProbLo() + ((i + 1/2) dx, (j + 1/2) dy),
110  * in the coordinates of the model's own points.
111  *
112  * The fields are the engine's state at time: the time ERF assigns to the fire's
113  * level at the end of the fire's last step, or the time ERF started or restarted
114  * from before that step. The fire steps after the dycore of its own level. A
115  * model on the fire's level or a coarser one therefore sees the fire at its own
116  * step's time, exactly; a model
117  * on a finer level sees it up to one step of the fire's level ahead of its own
118  * step. On a restart, until the fire's first step, they are what the engine
119  * restored from the checkpoint (an engine that does not restore a field holds
120  * its initial value there). Each field is spread over the ranks as the fire grid is: a model that
121  * needs the fire at its own points reduces over the ranks itself (every rank
122  * calls advance()). Like the flow fields, they are temporaries of the call.
123  */
125 {
126  const amrex::Geometry& geom; ///< Geometry of the fire grid [m] (one cell in z)
127  int lev; ///< The atmospheric level the fire grid refines
128  double time; ///< The time the fields belong to [s] (see above)
129  const amrex::MultiFab* heat_release; ///< Heat released [W/m2] on the fire grid (FireEngine::heat_release); nullptr if the engine has none
130  const amrex::MultiFab* level_set; ///< Level set on the fire grid, negative where burned (FireEngine::level_set); nullptr if the engine has none
131  /// Area burned so far [m2] (FireEngine::burned_area), negative when the engine does not
132  /// track it. It may reduce over the ranks: every rank calls it, or none.
133  std::function<amrex::Real()> burned_area;
134 };
135 
136 /**
137  * @brief The flow a structure model reads at the start of each step of its level
138  *
139  * The state at the start of the step, before its Runge-Kutta stages, on level lev
140  * with the level's ghost cells filled. Temporaries of the call: the model samples
141  * them during advance() and keeps no pointer to them.
142  */
144 {
145  int lev; ///< The level the model couples to
146  int finest_level; ///< ERF's finest level now
147  double time; ///< Start of the step [s]
148  double dt; ///< Step of level lev [s], in double as ERF keeps it, also in a single-precision build
149  const amrex::Geometry& geom; ///< Geometry of level lev
150  const amrex::MultiFab& xvel; ///< x-velocity on x-faces [m/s]
151  const amrex::MultiFab& yvel; ///< y-velocity on y-faces [m/s]
152  const amrex::MultiFab& zvel; ///< z-velocity on z-faces [m/s]
153  const amrex::MultiFab& cons; ///< The conserved state (rho, rho theta, the scalars; rho k with the k-equation RANS)
154  const amrex::MultiFab* z_phys_nd; ///< Nodal height [m]; nullptr on a uniform-dz mesh, as in StructureInit
155  const amrex::MultiFab* detJ_cc; ///< Cell Jacobian; nullptr on a uniform-dz mesh, as in StructureInit
156  bool unit_map_factors; ///< Every map factor of level lev is 1 now (checked each step, so also after a regrid)
157  const StructureFire* fire; ///< The fire (see StructureFire) when the run has one; nullptr otherwise
158 };
159 
160 /**
161  * @brief A model of structures in the flow, behind ERF's structure coupling
162  */
164 {
165 public:
166  virtual ~StructureModel () = default;
167 
168  /// Name of the model, as erf.structures.model selects it.
169  virtual const char* name () const = 0;
170 
171  /**
172  * @brief The level the model couples to
173  *
174  * ERF calls advance() on this level only, and add_momentum_sources() for
175  * this level only (see add_momentum_sources() for the coarser levels).
176  * Aborts, naming the model's input, when the model cannot couple to a level
177  * of this run (for example a level above finest_level).
178  *
179  * The level must be the finest level that covers the model's support. On a
180  * refined level the model's points and their kernels must lie inside the
181  * level's boxes, also after a regrid (erf_actuator::points_covered_by checks
182  * it). The force reaches the coarser levels through ERF's averaging down and,
183  * with TwoWay coupling, as a source restricted from this level.
184  * It never reaches a finer level: averaging down would overwrite the forced
185  * cells with that level's unforced data, so ERF stops the run when a finer
186  * level's boxes overlap support().
187  *
188  * @param[in] finest_level ERF's finest level now
189  */
190  virtual int level (int finest_level) const = 0;
191 
192  /**
193  * @brief Where the model's force can be nonzero this step
194  *
195  * Boxes in physical coordinates [m] (z as the height of z_phys_nd on a
196  * terrain-following mesh) that hold every point together with its kernel's
197  * reach, as of the last advance(). The same on every rank. ERF checks them
198  * against the next finer level's boxes after each advance() (see level()).
199  * The boxes may extend past the domain; a periodic direction wraps.
200  * Every box needs its low end at or below its high end in each direction and no
201  * NaN (an infinite end is allowed); ERF stops the run otherwise, naming the model.
202  */
203  virtual std::vector<amrex::RealBox> support () const = 0;
204 
205  /// The model wants StructureInit::immersed_terrain_surface (ERF builds it only on request).
206  virtual bool wants_immersed_terrain_surface () const { return false; }
207 
208  /**
209  * @brief Set the model up once the mesh and the initial state exist
210  *
211  * On a restart (StructureInit::restart_chkfile not empty) the model restores
212  * what write_checkpoint() wrote and continues from it. Aborts, naming the
213  * input, on a configuration the model cannot run.
214  */
215  virtual void initialize (const StructureInit& init) = 0;
216 
217  /**
218  * @brief ERF lowered the fixed step after initialize()
219  *
220  * With erf.fixed_dt_cfl_limit, ERF lowers the fixed step on all levels when a regrid makes
221  * grids on which it exceeds erf.cfl. fixed_dt is the new StructureInit::fixed_dt of level
222  * lev (the level the model couples to); a step under way finishes at the old step. The
223  * default does nothing.
224  */
225  virtual void fixed_dt_changed (int lev, double fixed_dt) { amrex::ignore_unused(lev, fixed_dt); }
226 
227  /**
228  * @brief Advance the structures over one step of level()
229  *
230  * Samples the flow, advances the structures from step.time to step.time +
231  * step.dt, and sets the momentum source add_momentum_sources() returns at the
232  * stages of this step.
233  */
234  virtual void advance (const StructureStep& step) = 0;
235 
236  /**
237  * @brief Add the structures' force on the air to the momentum sources
238  *
239  * Face-centered sources on level lev [N/m^3, i.e. kg/(m^2 s^2)], added (not set),
240  * held fixed over the step. Called at every Runge-Kutta stage, after ERF's
241  * own sources. ERF applies no mask to them: the model keeps its kernels clear
242  * of embedded boundaries, immersed terrain and buildings.
243  *
244  * With erf.coupling_type = TwoWay, ERF also calls it at the start of each step
245  * of every coarser level, with lev = level() and zeroed MultiFabs on the grids
246  * of level() as of the last advance() (they may have ghost faces, which the
247  * model leaves alone). It adds the source of its last advance(), as at a
248  * stage. ERF restricts that source onto the coarser level, keeping the force
249  * it puts on the air (its integral over the faces' control volumes, with the
250  * mesh Jacobian on a stretched or terrain-following mesh), and holds it over
251  * that level's step: the air under the finer level then feels the force while
252  * the coarser level steps, and the coarse/fine faces, which the finer level
253  * takes from the coarser one, get the force once.
254  * Before the first advance() and after a regrid of level(), until the next
255  * advance(), ERF does not call it for the coarser levels. ERF writes the
256  * source to its checkpoint, so after a restart, until the first advance(), it
257  * restricts the checkpointed source instead: the model need not hold one.
258  * ERF also calls it when it writes a checkpoint; being const, it leaves the
259  * model as it was.
260  */
261  virtual void add_momentum_sources (int lev,
262  amrex::MultiFab& xmom_src,
263  amrex::MultiFab& ymom_src,
264  amrex::MultiFab& zmom_src) const = 0;
265 
266  /**
267  * @brief Write the model's state under the checkpoint directory
268  *
269  * Everything the model carries from one step to the next, under
270  * checkpointname/<name()>/ (the model creates that directory on the I/O rank).
271  * The default writes nothing, and supports_restart() must then stay false.
272  */
273  virtual void write_checkpoint (const std::string& checkpointname) const
274  {
275  amrex::ignore_unused(checkpointname);
276  }
277 
278  /**
279  * @brief The model restores its state in initialize() on a restart
280  *
281  * False by default: ERF then stops a restart instead of letting the model
282  * continue from its initial state.
283  */
284  virtual bool supports_restart () const { return false; }
285 
286  /**
287  * @brief The run has ended normally
288  *
289  * Called once, on every rank, after the last step, plotfile and checkpoint: the place to
290  * flush and close the model's own files or to shut down a solver it drives. Not called
291  * when the run aborts. The default does nothing.
292  */
293  virtual void finalize () {}
294 
295  /**
296  * @brief Fields the ERF plotfile can carry, by name
297  *
298  * The names the deck may list in erf.plot_vars_*; they start with the model's
299  * own prefix (for example "conductor_") so they never collide with an ERF
300  * variable. ERF stops the run on an empty name, a name listed twice, or a
301  * name ERF or another model already uses. The answer depends on the model's
302  * inputs only. The default lists none.
303  */
304  virtual std::vector<std::string> plot_var_names () const { return {}; }
305 
306  /**
307  * @brief Fill the field plot_var_names() lists as name on level lev
308  *
309  * One cell-centered component written into dst at component comp, on dst's
310  * boxes, with no ghost cells; zero where the model has nothing (for example
311  * on levels other than level()).
312  */
313  virtual void fill_plot_var (const std::string& name, int lev,
314  amrex::MultiFab& dst, int comp) const
315  {
316  amrex::ignore_unused(name, lev);
317  dst.setVal(amrex::Real(0.0), comp, 1, 0);
318  }
319 };
320 
321 #endif
A model of structures in the flow, behind ERF's structure coupling.
Definition: ERF_StructureModel.H:164
virtual void write_checkpoint(const std::string &checkpointname) const
Write the model's state under the checkpoint directory.
Definition: ERF_StructureModel.H:273
virtual bool wants_immersed_terrain_surface() const
The model wants StructureInit::immersed_terrain_surface (ERF builds it only on request).
Definition: ERF_StructureModel.H:206
virtual void finalize()
The run has ended normally.
Definition: ERF_StructureModel.H:293
virtual void fixed_dt_changed(int lev, double fixed_dt)
ERF lowered the fixed step after initialize()
Definition: ERF_StructureModel.H:225
virtual void fill_plot_var(const std::string &name, int lev, amrex::MultiFab &dst, int comp) const
Fill the field plot_var_names() lists as name on level lev.
Definition: ERF_StructureModel.H:313
virtual bool supports_restart() const
The model restores its state in initialize() on a restart.
Definition: ERF_StructureModel.H:284
virtual void add_momentum_sources(int lev, amrex::MultiFab &xmom_src, amrex::MultiFab &ymom_src, amrex::MultiFab &zmom_src) const =0
Add the structures' force on the air to the momentum sources.
virtual std::vector< std::string > plot_var_names() const
Fields the ERF plotfile can carry, by name.
Definition: ERF_StructureModel.H:304
virtual void initialize(const StructureInit &init)=0
Set the model up once the mesh and the initial state exist.
virtual ~StructureModel()=default
virtual const char * name() const =0
Name of the model, as erf.structures.model selects it.
virtual void advance(const StructureStep &step)=0
Advance the structures over one step of level()
virtual int level(int finest_level) const =0
The level the model couples to.
virtual std::vector< amrex::RealBox > support() const =0
Where the model's force can be nonzero this step.
What a structure model may read from the fire, when the run has one.
Definition: ERF_StructureModel.H:125
std::function< amrex::Real()> burned_area
Definition: ERF_StructureModel.H:133
int lev
The atmospheric level the fire grid refines.
Definition: ERF_StructureModel.H:127
const amrex::Geometry & geom
Geometry of the fire grid [m] (one cell in z)
Definition: ERF_StructureModel.H:126
const amrex::MultiFab * level_set
Definition: ERF_StructureModel.H:130
double time
The time the fields belong to [s] (see above)
Definition: ERF_StructureModel.H:128
const amrex::MultiFab * heat_release
Heat released [W/m2] on the fire grid (FireEngine::heat_release); nullptr if the engine has none.
Definition: ERF_StructureModel.H:129
What a structure model reads once, at start-up.
Definition: ERF_StructureModel.H:66
bool keqn_rans
Level lev runs the k-equation RANS closure, so cons holds rho k.
Definition: ERF_StructureModel.H:79
const amrex::Geometry & geom
Geometry of level lev.
Definition: ERF_StructureModel.H:69
double fixed_dt
Definition: ERF_StructureModel.H:88
const amrex::MultiFab & xvel
x-velocity on x-faces [m/s]
Definition: ERF_StructureModel.H:70
int lev
The level the model couples to.
Definition: ERF_StructureModel.H:67
const amrex::FArrayBox * immersed_terrain_surface
Definition: ERF_StructureModel.H:77
double stop_time
Definition: ERF_StructureModel.H:93
int max_step
Definition: ERF_StructureModel.H:96
bool fully_compressible
Definition: ERF_StructureModel.H:100
amrex::Real Cmu0
The closure's C_mu^0 on level lev.
Definition: ERF_StructureModel.H:80
const amrex::MultiFab * z_phys_nd
Nodal height [m] of a stretched or terrain-following mesh; nullptr on a uniform-dz mesh (flat ground ...
Definition: ERF_StructureModel.H:71
std::string restart_chkfile
Definition: ERF_StructureModel.H:81
bool unit_map_factors
Every map factor of level lev is 1 (the actuator core assumes so)
Definition: ERF_StructureModel.H:78
double time
Simulation time [s]: 0, or the checkpoint's time on a restart.
Definition: ERF_StructureModel.H:68
const amrex::MultiFab * detJ_cc
Definition: ERF_StructureModel.H:72
The flow a structure model reads at the start of each step of its level.
Definition: ERF_StructureModel.H:144
const amrex::MultiFab & cons
The conserved state (rho, rho theta, the scalars; rho k with the k-equation RANS)
Definition: ERF_StructureModel.H:153
const amrex::Geometry & geom
Geometry of level lev.
Definition: ERF_StructureModel.H:149
const amrex::MultiFab & zvel
z-velocity on z-faces [m/s]
Definition: ERF_StructureModel.H:152
double time
Start of the step [s].
Definition: ERF_StructureModel.H:147
const amrex::MultiFab * detJ_cc
Cell Jacobian; nullptr on a uniform-dz mesh, as in StructureInit.
Definition: ERF_StructureModel.H:155
const amrex::MultiFab & xvel
x-velocity on x-faces [m/s]
Definition: ERF_StructureModel.H:150
const amrex::MultiFab * z_phys_nd
Nodal height [m]; nullptr on a uniform-dz mesh, as in StructureInit.
Definition: ERF_StructureModel.H:154
double dt
Step of level lev [s], in double as ERF keeps it, also in a single-precision build.
Definition: ERF_StructureModel.H:148
int finest_level
ERF's finest level now.
Definition: ERF_StructureModel.H:146
const StructureFire * fire
The fire (see StructureFire) when the run has one; nullptr otherwise.
Definition: ERF_StructureModel.H:157
int lev
The level the model couples to.
Definition: ERF_StructureModel.H:145
bool unit_map_factors
Every map factor of level lev is 1 now (checked each step, so also after a regrid)
Definition: ERF_StructureModel.H:156
const amrex::MultiFab & yvel
y-velocity on y-faces [m/s]
Definition: ERF_StructureModel.H:151