ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_FireAtmCoupling.H
Go to the documentation of this file.
1 // One-way fire-to-atmosphere coupling: distribute fire heat flux through
2 // the atmospheric column and inject into cc_source.
3 //
4 // This mirrors the WRF-Fire approach in module_fr_sfire_atm.F
5 // (subroutine fire_tendency), which:
6 // 1. Builds a vertical flux profile with exponential decay:
7 // hfx(z) = (Q_sfc / Cp) * exp(-z / alfg)
8 // where z is the height above the lowest cell center of the column (half
9 // a cell above the terrain: the profile's datum is that center, so the
10 // first cell takes the share 1 - e^{-dz/a}, 0.105 for dz = 5 m and
11 // a = 45 m, and nothing is lost below it) and alfg is the e-folding
12 // scale [m].
13 // 2. Converts the flux profile to a theta tendency via flux divergence:
14 // d(RhoTheta)/dt = -d(hfx)/dz
15 // with dz the thickness of the cell (face to face), so the column
16 // receives the flux whatever the vertical spacing; erf.fire.
17 // heat_tendency_density multiplies by rho and erf.fire.heat_tendency_exner
18 // divides by the Exner function (see below).
19 // 3. Does the same for latent heat (grnqfx -> rqvfrten):
20 // qfx(z) = (Q_lat / Lv) * exp(-z / alfg)
21 // d(RhoQ1)/dt = -d(qfx)/dz
22 //
23 // The e-folding scale alfg controls how deep into the atmosphere the fire
24 // heating penetrates. With alfg = 45 m, the flux has decayed to 37% at 45 m,
25 // 5% at 135 m, and 1% at 225 m above the surface.
26 //
27 // Reference:
28 // Mandel, J., Beezley, J. D., Coen, J. L. and Kim, M. (2009). Data assimilation
29 // for wildland fires. IEEE Control Systems Magazine, 29(3), 47-65; and
30 // Mandel, J., Beezley, J. D. and Kochanski, A. K. (2011). Coupled
31 // atmosphere-wildland fire modeling with WRF 3.3 and SFIRE 2011. Geoscientific
32 // Model Development, 4, 591-610.
33 // WRF-SFIRE source: phys/module_fr_sfire_atm.F, subroutine fire_tendency.
34 //
35 // Coupling time lag:
36 // ERF uses one-step explicit lag: the fire heat flux computed at the end of
37 // step n is held in Q_atm_prev and injected into cc_source during step n+1,
38 // from the slow right-hand side of every RK stage (ERF_TI_slow_rhs_pre.H),
39 // after make_sources has rebuilt cc_source for that stage.
40 // This is consistent with WRF-Fire's explicit coupling approach.
41 //
42 // RK-stage safety:
43 // apply_fire_tendency_to_cc_source is invoked once per RK stage of the
44 // slow-RHS integrator. Because the lagged Q_atm_prev is fixed for the whole
45 // timestep, the fire tendency it produces must also be identical at every
46 // RK stage. With erf.fire.source_mode = overwrite (the default) we OVERWRITE
47 // cc_source[RhoTheta_comp] (and RhoQ1_comp when latent heat is on): the
48 // values are cleared to zero at the top of the routine and then set via '='
49 // inside the kernel, which discards every other source make_sources put
50 // there (radiative heating, Rayleigh damping, nudging). source_mode = add
51 // adds to the rebuilt source instead. Accumulating with '+=' across RK
52 // stages without the per-stage rebuild would triple the injected heat.
53 //
54 // Latent heat:
55 // WRF-Fire computes grnqfx (ground latent heat flux) alongside grnhfx and
56 // passes both through fire_tendency. Here the fire engine computes the
57 // latent flux (from the fuel moisture and the water of combustion of the fuel
58 // it burns) and hands it over in FireFluxes::latent [W/m2]; the coupling
59 // distributes it with the same exponential profile.
60 //
61 // Assumptions:
62 // - The tendency goes into the source of the level the fire grid refines
63 // (erf.fire.anchor_level, the finest level by default); coarser levels see
64 // the result through average-down with erf.coupling_type = TwoWay (the
65 // default), not with OneWay; finer levels not at all.
66 // - The engine's sensible and latent fluxes (FireFluxes) are on the fire grid
67 // (C*nx x C*ny x 1 over the fire region).
68 // - The coarsening from fire grid to atmospheric grid is the area average of
69 // surface_coarsen_to_atm() (ERF_SurfaceGrid.H), with the region's lower
70 // corner added (FireGrid::atm_lo).
71 // - z_phys_cc is the cell-center terrain-following height MultiFab [m].
72 // - The atmospheric BoxArray is not split in z (checked at start-up by
73 // verify_surface_grid_level(), ERF_SurfacePrerequisites.H).
74 // - Latent heat injection into RhoQ1_comp is skipped when moisture is inactive.
75 // - fire_atm_feedback multiplier (equivalent to WRF-Fire's fire_atm_feedback)
76 // scales both sensible and latent flux. Set to 0 for one-way coupling only
77 // (fire responds to wind but atmosphere does not respond to fire heat).
78 // - The RhoTheta / RhoQ1 slots of cc_source are OWNED by the fire coupling
79 // for the duration of a timestep. If any other physics writes into those
80 // same slots per stage, this routine must be moved out of the per-stage
81 // slow-RHS path and called once before mri_integrator.advance() instead.
82 
83 #ifndef ERF_FIRE_ATM_COUPLING_H
84 #define ERF_FIRE_ATM_COUPLING_H
85 
86 #include <AMReX_MultiFab.H>
87 #include <AMReX_Geometry.H>
88 #include <AMReX_Box.H>
89 #include <AMReX_MFIter.H>
90 #include <AMReX_MultiFabUtil.H>
91 #include <cmath>
92 #include <limits>
93 #include <ERF_Constants.H>
94 #include <ERF_IndexDefines.H>
95 #include <ERF_EOS.H>
96 #include <ERF_FireGrid.H>
97 
98 // Default e-folding height for ground fire heat distribution [m].
99 // WRF-Fire default: alfg ~ 45-75 m. At z = alfg: flux decays to 1/e ~ 37%.
100 // The crown-fire release is added to the same surface flux and injected with
101 // this one profile; there is no separate canopy e-folding height.
102 constexpr amrex::Real FIRE_ATM_ALFG_DEFAULT = amrex::Real(45.0);
103 
104 // Coarsen a fire-grid field (C*nx x C*ny x 1) onto the atmospheric columns of
105 // the level the fire grid refines (nx x ny x 1) by area averaging.
106 //
107 // Q_atm_out lives on the k = 0 slabs of that level's boxes, in their order and
108 // with their owners; Q_fire on the fire grid, whose index space starts at the
109 // region's corner: fire cells (i - lo_x) C + r, (j - lo_y) C + q for r, q in
110 // [0, C) average into atmospheric cell (i, j). This is surface_coarsen_to_atm()
111 // (ERF_SurfaceGrid.H), which sums in the order of amrex::average_down() with
112 // ratio (C, C, 1), so on level 0 it gives the same bits as average_down().
113 // Unlike average_down(), which accepts any layout of the output, it stops the
114 // run unless Q_fire and Q_atm_out pair box for box with the same owners: an
115 // engine must return its fluxes on fg.ba / fg.dm (ERF_FireEngine.H).
116 // geom_atm is not used; it stays in the signature for the engines that call
117 // this function.
119  amrex::MultiFab& Q_atm_out,
120  const amrex::MultiFab& Q_fire,
121  const amrex::Geometry& /*geom_atm*/,
122  const FireGrid& fg)
123 {
124  surface_coarsen_to_atm(Q_atm_out, Q_fire, fg, fire_coupling_names());
125 }
126 
127 // Height of the lower (upper = 0) or upper (upper = 1) face of cell (i, j, k):
128 // the mean of its four nodes where the nodal heights exist, half a cell below
129 // (above) the center on a uniform grid without them.
130 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
131 amrex::Real fire_cell_face (amrex::Array4<amrex::Real const> const& z_nd,
132  amrex::Array4<amrex::Real const> const& z_cc,
133  bool has_nd, amrex::Real dz_avg,
134  int i, int j, int k, int upper) noexcept
135 {
136  if (has_nd) {
137  const int kf = k + upper;
138  return amrex::Real(0.25) * (z_nd(i, j, kf) + z_nd(i+1, j, kf)
139  + z_nd(i, j+1, kf) + z_nd(i+1, j+1, kf));
140  }
141  return z_cc(i, j, k) + (upper ? amrex::Real(0.5) : amrex::Real(-0.5)) * dz_avg;
142 }
143 
144 // Thickness of a cell from its faces. Only a degenerate (zero or inverted)
145 // cell is floored, at 1e-6 of the mean spacing, so that the division stays
146 // finite; a thin first cell on a stretched grid keeps its own thickness (a
147 // floor of 0.1 dz_avg put 59 % of the heat into a column with a 1 m first
148 // cell stretched by 1.1 over 64 cells, at the default alfg of 45 m).
149 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
150 amrex::Real fire_cell_thickness (amrex::Real z_lo, amrex::Real z_hi, amrex::Real dz_avg) noexcept
151 {
152  return amrex::max(z_hi - z_lo, amrex::Real(1.0e-6) * dz_avg);
153 }
154 
155 // Distribute fire surface heat flux through the atmospheric column and write
156 // the resulting tendency into cc_source.
157 //
158 // Behavior: OVERWRITES cc_source[RhoTheta_comp] and (when latent heat is on)
159 // cc_source[RhoQ1_comp]. See file-level "RK-stage safety" note above.
160 //
161 // Optional fire_debug argument prints per-call tendency diagnostics to stdout:
162 // [FIRE COUPLING] RhoTheta tendency sum = ... max = ... [kg K m^-3 s^-1]
163 // expected_surface_max = feedback * Q_max / Cp * (1 - exp(-dz/alfg)) / dz
164 // (the default heat_tendency_density = false form, d(rho theta)/dt =
165 // -(1/Cp) dQ/dz, for the lowest cell on a uniform grid; with the density
166 // factor the tendency carries an extra rho, with the Exner factor 1/Pi)
167 //
168 // With Q_max=14330 W/m2, Cp=Cp_d (1004.5 J/(kg K)), alfg=45 m, dz=5 m:
169 // expected_surface_max ~ 0.30 kg K m^-3 s^-1
170 //
171 // z_phys_nd, the nodal heights of the level, gives each cell's faces: its
172 // thickness divides the tendency, and the open-fraction placement measures the
173 // roof height from the ground. ERF always passes it; with nullptr (callers
174 // without nodal heights, such as the unit tests) the faces are half a uniform
175 // cell from the centers, which is right only on a uniform grid.
177  amrex::MultiFab& cc_source,
178  const amrex::MultiFab& Q_atm,
179  const amrex::MultiFab* Q_lat_atm,
180  const amrex::MultiFab& z_phys_cc,
181  const amrex::MultiFab* z_phys_nd,
182  const amrex::MultiFab& S_old,
183  const amrex::Geometry& geom_atm,
184  amrex::Real alfg,
185  amrex::Real feedback,
186  bool has_moisture,
187  bool fire_debug = false,
188  bool additive = false,
189  const amrex::MultiFab* open_frac_atm = nullptr,
190  const amrex::MultiFab* roof_h_atm = nullptr,
191  bool use_open_weights = false,
192  bool density_factor = false,
193  bool exner_factor = false,
194  bool bottom_cell = false)
195 {
196  using namespace amrex;
197 
198  if (alfg <= Real(0.0)) { return; }
199  if (feedback <= Real(0.0)) { return; }
200 
201  // Source conversion must use the same dry-air heat capacity as ERF's EOS.
202  constexpr Real Cp_fire = Cp_d;
203  // and the latent heat ERF uses (an engine converting a vapor mass flux to
204  // its latent flux multiplies by this same L_v, so the water round-trips exactly)
205  constexpr Real L_V = L_v;
206  // Exner function of the cell from its rho theta (erf.fire.heat_tendency_exner):
207  // heating q [W/m3] gives d(theta)/dt = q / (rho cp Pi), so the rho theta
208  // tendency carries 1/Pi; without it the enthalpy injected is Pi times the
209  // flux, 4.5 % short at 850 hPa. WRF-SFIRE omits the factor; it is on by
210  // default here.
211  const Real rdOcp = R_d / Cp_d;
212 
213  const Box& domain = geom_atm.Domain();
214  int klo = domain.smallEnd(2);
215  int khi = domain.bigEnd(2);
216  const auto& dx = geom_atm.CellSize();
217  Real dz_avg = dx[2];
218 
219  const bool has_latent_global = (Q_lat_atm != nullptr) && has_moisture;
220  // The open fraction and roof height are handed in whenever the engine
221  // reports them; the weights only use them when asked.
222  const bool have_open = (open_frac_atm != nullptr) && (roof_h_atm != nullptr);
223  const bool use_open = have_open && use_open_weights;
224 
225  // Debug only: the fire tendency alone, times the cell thickness (in additive mode
226  // cc_source also holds the other sources), for the energy check below.
227  MultiFab tend_dbg;
228  if (fire_debug) {
229  tend_dbg.define(cc_source.boxArray(), cc_source.DistributionMap(), 1, 0);
230  tend_dbg.setVal(0.0);
231  }
232 
233  // Legacy (overwrite) mode: zero the components we own before writing them,
234  // so any state left over from a previous stage does not leak through. In
235  // additive mode make_sources has just rebuilt the source for this stage,
236  // so the fire tendency is accumulated once per stage on top of it.
237  if (!additive) {
238  cc_source.setVal(0.0, RhoTheta_comp, 1, cc_source.nGrowVect());
239  if (has_latent_global) {
240  cc_source.setVal(0.0, RhoQ1_comp, 1, cc_source.nGrowVect());
241  }
242  }
243 
244  // Column normalization of the open-fraction weights: one k = klo slab per
245  // box, filled once per column (the boxes hold whole columns, which the
246  // fire prerequisites assert), before the tiled loop that reads it.
247  // The engine's open fraction and roof height are read with the level's
248  // MFIter below, so they must hold one k = klo slab per box of the level,
249  // in its order and on its DistributionMapping (only a debug build checks
250  // that inside AMReX).
251  if (have_open) {
252  const BoxArray& cba = cc_source.boxArray();
253  bool same_layout = open_frac_atm->DistributionMap() == cc_source.DistributionMap() &&
254  roof_h_atm->DistributionMap() == cc_source.DistributionMap() &&
255  open_frac_atm->boxArray().size() == cba.size() &&
256  roof_h_atm->boxArray().size() == cba.size();
257  for (int ib = 0; same_layout && ib < cba.size(); ++ib) {
258  const Box slab = makeSlab(cba[ib], 2, klo);
259  same_layout = (open_frac_atm->boxArray()[ib] == slab) && (roof_h_atm->boxArray()[ib] == slab);
260  }
262  "[FIRE] the engine's heat_open_fraction_atm and roof_height_atm must hold one k = 0 slab "
263  "per box of the fire grid's level, in its box order and on its DistributionMapping");
264  }
265  MultiFab norm_mf;
266  if (use_open) {
267  norm_mf.define(open_frac_atm->boxArray(), cc_source.DistributionMap(), 1, 0);
268  for (MFIter mfi(norm_mf); mfi.isValid(); ++mfi) {
269  auto const& norm_a = norm_mf.array(mfi);
270  auto const& open_arr = open_frac_atm->const_array(mfi);
271  auto const& roof_arr = roof_h_atm->const_array(mfi);
272  auto const& z_cc_a = z_phys_cc.const_array(mfi);
273  const bool has_nd = (z_phys_nd != nullptr);
274  Array4<Real const> z_nd_a;
275  if (has_nd) { z_nd_a = z_phys_nd->const_array(mfi); }
276  amrex::ParallelFor(mfi.validbox(), [=] AMREX_GPU_DEVICE (int i, int j, int /*kslab*/) noexcept {
277  const Real f_open = open_arr(i, j, 0);
278  const Real h_roof = roof_arr(i, j, 0); // above the ground
279  const Real z_sfc = z_cc_a(i, j, klo);
280  const Real ground = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, klo, 0);
281  Real norm = Real(0.0);
282  for (int kk = klo; kk <= khi; ++kk) {
283  const Real zz = z_cc_a(i, j, kk) - z_sfc;
284  const Real zzp1 = (kk < khi) ? (z_cc_a(i, j, kk + 1) - z_sfc) : (zz + dz_avg);
285  Real s = Real(1.0);
286  if (h_roof > Real(0.0)) {
287  const Real zlo2 = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, kk, 0) - ground;
288  const Real zhi2 = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, kk, 1) - ground;
289  const Real dzz = fire_cell_thickness(zlo2, zhi2, dz_avg);
290  if (zhi2 <= h_roof) { s = f_open; }
291  else if (zlo2 < h_roof) { s = f_open + (Real(1.0) - f_open) * (zhi2 - h_roof) / dzz; }
292  }
293  norm += (std::exp(-zz / alfg) - std::exp(-zzp1 / alfg)) * s;
294  }
295  norm_a(i, j, klo) = norm;
296  });
297  }
298  }
299 
300  for (MFIter mfi(cc_source, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
301  auto const& cc_src_a = cc_source.array(mfi);
302  auto const& q_atm_a = Q_atm.array(mfi);
303  auto const& z_cc_a = z_phys_cc.array(mfi);
304  auto const& s_old_a = S_old.array(mfi);
305  // Faces of cell k: the mean of its four lower (upper) nodes where the
306  // nodal heights exist, half a cell below (above) the center otherwise
307  const bool has_nd = (z_phys_nd != nullptr);
308  Array4<Real const> z_nd_a;
309  if (has_nd) { z_nd_a = z_phys_nd->const_array(mfi); }
310 
311  const Box& bx = mfi.tilebox();
312 
313  const bool has_latent = has_latent_global;
314  Array4<Real const> q_lat_arr;
315  if (has_latent) {
316  q_lat_arr = Q_lat_atm->const_array(mfi);
317  }
318  Array4<Real const> open_arr, roof_arr;
319  if (use_open) {
320  open_arr = open_frac_atm->const_array(mfi);
321  roof_arr = roof_h_atm->const_array(mfi);
322  }
323  Array4<Real> dbg_arr;
324  if (fire_debug) { dbg_arr = tend_dbg.array(mfi); }
325 
326  // Column normalization of the open-fraction weights, computed once per
327  // column above the tile loop.
328  Array4<Real const> norm_arr;
329  if (use_open) { norm_arr = norm_mf.const_array(mfi); }
330 
331  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(const IntVect& iv) {
332  int i = iv[0];
333  int j = iv[1];
334  int k = iv[2];
335 
336  Real q_sfc_sensible = feedback * q_atm_a(i, j, 0);
337  Real q_sfc_latent = Real(0.0);
338  if (has_latent) {
339  q_sfc_latent = feedback * q_lat_arr(i, j, 0);
340  }
341 
342  Real z_sfc = z_cc_a(i, j, klo);
343  Real z_k = z_cc_a(i, j, k) - z_sfc;
344  Real exp_k = std::exp(-z_k / alfg);
345 
346  Real z_kp1 = (k < khi) ? (z_cc_a(i, j, k + 1) - z_sfc) : (z_k + dz_avg);
347  Real exp_kp1 = std::exp(-z_kp1 / alfg);
348 
349  // The cell's own thickness, face to face: the column then receives
350  // sum_k w_k of the flux, 1 - exp(-z_top/alfg), on any vertical
351  // spacing (the center-to-center spacing used before put a share
352  // dz_cell / dz_cc into each cell, 2 / (1 + r) of the flux on a grid
353  // stretched by r)
354  const Real z_lo = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, k, 0);
355  const Real z_hi = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, k, 1);
356  const Real dz = fire_cell_thickness(z_lo, z_hi, dz_avg);
357 
358  Real rho = s_old_a(i, j, k, Rho_comp);
359  if (rho <= Real(0.0)) { rho = Real(1.2); }
360  // Heating gives d(rho theta)/dt = -(1/Cp) dQ/dz, with no density
361  // factor. The legacy form carries an extra rho, so its injected
362  // energy is rho times the flux; density_factor = false drops it.
363  // The Exner factor divides the tendency by Pi(p) so that the
364  // enthalpy, not rho theta, integrates to the flux.
365  // Preserve the explicit legacy density option, but apply Exner
366  // only to heat. Q_lat / L_V is a water mass flux: converting it
367  // to d(rho_d q_v)/dt must not multiply that mass by 1/Pi.
368  const Real mass_fac = density_factor ? rho : Real(1.0);
369  Real heat_fac = mass_fac;
370  if (exner_factor) {
371  const Real rhotheta = amrex::max(s_old_a(i, j, k, RhoTheta_comp), Real(1.0e-10));
372  // the moist pressure where there is vapor (about 1 % in Pi at 15 g/kg)
373  const Real qv = has_moisture ? amrex::max(s_old_a(i, j, k, RhoQ1_comp), Real(0.0)) / rho : Real(0.0);
374  const Real p = getPgivenRTh(rhotheta, qv);
375  const Real exner = std::pow(p / p_0, rdOcp);
376  heat_fac /= amrex::max(exner, Real(1.0e-6));
377  }
378 
379  // Layer weight: the fraction of the column's exponential profile
380  // that falls in this cell. Without an open fraction this is the flux
381  // difference form used since the coupling was written. With the
382  // open-fraction option, cells below the sampled roof height carry
383  // only their open share and the column is renormalized over the
384  // cells that remain, so the injected energy per column is unchanged
385  // and none of it lands inside a building.
386  Real w_k = exp_k - exp_kp1;
387  // erf.fire.heat_profile = bottom_cell: the whole flux enters the
388  // lowest cell of the column (the LSFIRE coupling)
389  if (bottom_cell) { w_k = (k == klo) ? Real(1.0) : Real(0.0); }
390  if (use_open) {
391  const Real f_open = open_arr(i, j, 0);
392  const Real h_roof = roof_arr(i, j, 0);
393  // Open share of this cell: fully open above the roof, f_open
394  // below it, linear across the cell that holds the roof.
395  // The roof height is above the ground, so are the cell's faces.
396  Real share = Real(1.0);
397  if (h_roof > Real(0.0)) {
398  const Real ground = fire_cell_face(z_nd_a, z_cc_a, has_nd, dz_avg, i, j, klo, 0);
399  const Real zlo_g = z_lo - ground;
400  const Real zhi_g = z_hi - ground;
401  if (zhi_g <= h_roof) { share = f_open; }
402  else if (zlo_g < h_roof) { share = f_open + (Real(1.0) - f_open) * (zhi_g - h_roof) / dz; }
403  }
404  // Column normalization over the open shares of the same
405  // profile, precomputed per column above.
406  const Real norm = norm_arr(i, j, klo);
407  // The legacy profile integrates to (1 - exp(-z_top/alfg)); keep
408  // that total so the two modes inject the same energy per column.
409  const Real z_top = (z_cc_a(i, j, khi) - z_sfc) + dz_avg;
410  const Real legacy = Real(1.0) - std::exp(-z_top / alfg);
411  w_k = (norm > Real(1.0e-30)) ? w_k * share * legacy / amrex::max(norm, Real(1.0e-30)) : Real(0.0);
412  }
413 
414  // Without the open-fraction option the flux-difference expression
415  // is kept (exp_k - exp_kp1 times the flux, over the cell thickness).
416  Real theta_tend, q_tend = Real(0.0);
417  if (use_open || bottom_cell) {
418  theta_tend = heat_fac * (q_sfc_sensible / Cp_fire) * w_k / dz;
419  if (has_latent) { q_tend = mass_fac * (q_sfc_latent / L_V) * w_k / dz; }
420  } else {
421  Real hfx_k = (q_sfc_sensible / Cp_fire) * exp_k;
422  Real hfx_kp1 = (q_sfc_sensible / Cp_fire) * exp_kp1;
423  theta_tend = -heat_fac * (hfx_kp1 - hfx_k) / dz;
424  if (has_latent) {
425  Real qfx_k = (q_sfc_latent / L_V) * exp_k;
426  Real qfx_kp1 = (q_sfc_latent / L_V) * exp_kp1;
427  q_tend = -mass_fac * (qfx_kp1 - qfx_k) / dz;
428  }
429  }
430  if (additive) {
431  cc_src_a(i, j, k, RhoTheta_comp) += theta_tend;
432  } else {
433  // OVERWRITE (see file-level RK-stage safety note).
434  cc_src_a(i, j, k, RhoTheta_comp) = theta_tend;
435  }
436  if (has_latent) {
437  if (additive) {
438  cc_src_a(i, j, k, RhoQ1_comp) += q_tend;
439  } else {
440  cc_src_a(i, j, k, RhoQ1_comp) = q_tend;
441  }
442  }
443  // the cell's true thickness (not the floored one), so a degenerate
444  // cell's lost energy shows in the ratio
445  if (dbg_arr) { dbg_arr(i, j, k) = theta_tend * (z_hi - z_lo); }
446  });
447  }
448 
449  // Tendency diagnostics: runs on host after GPU kernel completes.
450  // Prints once per apply call (3x per timestep for RK3).
451  // Expected rt_tend_max ~ feedback * Q_max / Cp * (1 - exp(-dz/alfg)) / dz
452  // (times rho with the density factor) on a uniform grid
453  if (fire_debug) {
454  Real Q_max = Q_atm.max(0);
455  Real rt_tend_sum = cc_source.sum(RhoTheta_comp);
456  Real rt_tend_max = cc_source.max(RhoTheta_comp);
457  Real expected_max = feedback * Q_max / Cp_fire
458  * (Real(1.0) - std::exp(-dz_avg / alfg)) / dz_avg;
459  amrex::Print() << "[FIRE COUPLING] Q_atm_max=" << Q_max
460  << " W/m2 alfg=" << alfg << " m\n"
461  << "[FIRE COUPLING] RhoTheta tendency"
462  << " sum=" << rt_tend_sum
463  << " max=" << rt_tend_max
464  << " expected_surface_max=" << expected_max
465  << " [kg K m^-3 s^-1]\n";
466 
467  // Energy check: Cp times the sum over cells of (rho theta tendency) dz
468  // per unit area, against the flux handed in. The profile carries
469  // 1 - exp(-z_top/alfg) of the flux, essentially all of it for any
470  // realistic domain height, so with neither the density nor the Exner
471  // factor the ratio is that (the density factor multiplies it by rho,
472  // the Exner factor by 1/Pi, both profile-weighted means, essentially
473  // their values near the surface). The largest fire tendency inside a fully blocked cell below the roof must be
474  // zero with the open-fraction option.
475  const Real area = dx[0] * dx[1];
476  const Real e_out = Cp_fire * tend_dbg.sum(0) * area;
477  const Real e_in = feedback * Q_atm.sum(0) * area;
478  // Columns that straddle a footprint (0 < open fraction < 1) are where
479  // the placement matters: fully covered columns carry no flux once their
480  // fire cells are masked. For those partial columns report the largest
481  // share of the column's heating that lands below the roof H (above the
482  // ground). With B the plain profile's share below the roof (measured
483  // from the first cell center) and L = 1 - exp(-z_top/alfg), the
484  // open-fraction weights give f B L / (L - (1 - f) B), more than f B
485  // because the column is renormalized; and the largest potential temperature of the
486  // state in cells below a roof, which shows heat that earlier steps left
487  // inside buildings.
488  Real below_roof_max = -Real(1.0), theta_blocked_max = -Real(1.0);
489  if (have_open) {
490  MultiFab col(open_frac_atm->boxArray(), open_frac_atm->DistributionMap(), 1, 0);
491  MultiFab thb(tend_dbg.boxArray(), tend_dbg.DistributionMap(), 1, 0);
492  col.setVal(0.0); thb.setVal(0.0);
493  for (MFIter mfi(col, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
494  const Box& bx2 = mfi.tilebox();
495  auto const& c = col.array(mfi);
496  auto const& t = tend_dbg.const_array(mfi);
497  auto const& z = z_phys_cc.const_array(mfi);
498  auto const& ofrac = open_frac_atm->const_array(mfi);
499  auto const& hr = roof_h_atm->const_array(mfi);
500  const bool has_nd = (z_phys_nd != nullptr);
501  Array4<Real const> znd;
502  if (has_nd) { znd = z_phys_nd->const_array(mfi); }
503  amrex::ParallelFor(bx2, [=] AMREX_GPU_DEVICE (int i, int j, int /*k*/) noexcept {
504  const Real f = ofrac(i, j, 0);
505  if (f <= Real(1.0e-6) || f >= Real(1.0) - Real(1.0e-6)) { return; }
506  const Real ground = fire_cell_face(znd, z, has_nd, dz_avg, i, j, klo, 0);
507  Real below = Real(0.0), total = Real(0.0);
508  for (int kk = klo; kk <= khi; ++kk) {
509  const Real zk = z(i, j, kk) - ground;
510  total += t(i, j, kk);
511  if (zk < hr(i, j, 0)) { below += t(i, j, kk); }
512  }
513  c(i, j, 0) = (total > Real(1.0e-30)) ? below / amrex::max(total, Real(1.0e-30)) : Real(0.0);
514  });
515  }
516  for (MFIter mfi(thb, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
517  const Box& bx = mfi.tilebox();
518  auto const& b = thb.array(mfi);
519  auto const& z = z_phys_cc.const_array(mfi);
520  auto const& so = S_old.const_array(mfi);
521  auto const& ofrac = open_frac_atm->const_array(mfi);
522  auto const& hr = roof_h_atm->const_array(mfi);
523  const bool has_nd = (z_phys_nd != nullptr);
524  Array4<Real const> znd;
525  if (has_nd) { znd = z_phys_nd->const_array(mfi); }
526  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept {
527  const Real zk = z(i, j, k) - fire_cell_face(znd, z, has_nd, dz_avg, i, j, klo, 0);
528  if (ofrac(i, j, 0) < Real(1.0) - Real(1.0e-6) && zk < hr(i, j, 0)) {
529  b(i, j, k) = so(i, j, k, RhoTheta_comp) / so(i, j, k, Rho_comp);
530  }
531  });
532  }
533  below_roof_max = col.max(0);
534  theta_blocked_max = thb.max(0);
535  }
536  // The ratio divides only when e_in > 0, but the optimizer evaluates the division
537  // speculatively for both arms of the selection, and 0/0 on the first lagged step
538  // raised the invalid flag that aborts the run under amrex.fpe_trap_invalid; the
539  // floored denominator (the smallest normal Real, so it holds in single
540  // precision too) keeps the discarded value finite and the printed one unchanged.
541  amrex::Print() << "[FIRE COUPLING] energy_in=" << e_in << " W energy_out=" << e_out
542  << " W ratio=" << ((e_in > 0.0) ? e_out / amrex::max(e_in, std::numeric_limits<Real>::min()) : 0.0)
543  << " below_roof_share_max=" << below_roof_max
544  << " theta_blocked_max=" << theta_blocked_max << "\n";
545  }
546 }
547 
548 #endif // ERF_FIRE_ATM_COUPLING_H
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
constexpr amrex::Real Cp_d
Definition: ERF_Constants.H:51
constexpr amrex::Real p_0
Definition: ERF_Constants.H:69
constexpr amrex::Real R_d
Definition: ERF_Constants.H:49
constexpr amrex::Real L_v
Definition: ERF_Constants.H:67
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getPgivenRTh(const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:81
void apply_fire_tendency_to_cc_source(amrex::MultiFab &cc_source, const amrex::MultiFab &Q_atm, const amrex::MultiFab *Q_lat_atm, const amrex::MultiFab &z_phys_cc, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab &S_old, const amrex::Geometry &geom_atm, amrex::Real alfg, amrex::Real feedback, bool has_moisture, bool fire_debug=false, bool additive=false, const amrex::MultiFab *open_frac_atm=nullptr, const amrex::MultiFab *roof_h_atm=nullptr, bool use_open_weights=false, bool density_factor=false, bool exner_factor=false, bool bottom_cell=false)
Definition: ERF_FireAtmCoupling.H:176
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real fire_cell_thickness(amrex::Real z_lo, amrex::Real z_hi, amrex::Real dz_avg) noexcept
Definition: ERF_FireAtmCoupling.H:150
void coarsen_fire_flux_to_atm(amrex::MultiFab &Q_atm_out, const amrex::MultiFab &Q_fire, const amrex::Geometry &, const FireGrid &fg)
Definition: ERF_FireAtmCoupling.H:118
constexpr amrex::Real FIRE_ATM_ALFG_DEFAULT
Definition: ERF_FireAtmCoupling.H:102
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real fire_cell_face(amrex::Array4< amrex::Real const > const &z_nd, amrex::Array4< amrex::Real const > const &z_cc, bool has_nd, amrex::Real dz_avg, int i, int j, int k, int upper) noexcept
Definition: ERF_FireAtmCoupling.H:131
Fire grid data structure and construction.
const SurfaceCouplingNames & fire_coupling_names()
The fire coupling's names: erf.fire.<key>, "[FIRE]", "fire grid".
Definition: ERF_FireGrid.H:25
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:78
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
const int khi
Definition: ERF_InitCustomPert_Bubble.H:25
const int klo
Definition: ERF_InitCustomPert_KE.H:9
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
void surface_coarsen_to_atm(amrex::MultiFab &atm_out, const amrex::MultiFab &surf, const SurfaceGrid &sg, const SurfaceCouplingNames &names)
Definition: ERF_SurfaceGrid.H:201
@ fg
Definition: ERF_AdvanceMorrison.cpp:111
@ rho
Definition: ERF_Kessler.H:25
@ qv
Definition: ERF_Kessler.H:31
@ total
Definition: ERF_NOAHMP_Fields.H:137
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
@ t
Definition: ERF_WSM6.H:295
@ p
Definition: ERF_WSM6.H:303
Definition: ERF_ConsoleIO.cpp:17
2D horizontal fire computational grid
Definition: ERF_FireGrid.H:42