ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TwoStreamCanopyForcing.H
Go to the documentation of this file.
1 #ifndef ERF_TWO_STREAM_CANOPY_FORCING_H_
2 #define ERF_TWO_STREAM_CANOPY_FORCING_H_
3 
4 #include <AMReX_Array4.H>
5 #include <AMReX_Extension.H>
6 #include <AMReX_GpuQualifiers.H>
7 #include <AMReX_MultiFab.H>
8 #include <AMReX_REAL.H>
9 #include <limits>
10 
11 /**
12  * \file ERF_TwoStreamCanopyForcing.H
13  * \brief What the two-stream sweep hands the building faces of the
14  * immersed-boundary surface energy balance (erf.ibseb.radiation = two_stream),
15  * and how a face reads it.
16  *
17  * The sweep already writes the interface fluxes of every column into ERF's
18  * ``rad_fluxes`` (RRTMGP's level layout: index k the lower interface of layer k,
19  * component 0 SW up, 1 SW down, 2 LW up, 3 LW down, after the clear/cloudy
20  * blend). What it does not keep is the direct beam, which only its scratch holds,
21  * and the cosine of the zenith it used. On the levels where ERF::init_ibseb()
22  * asks for them (TwoStreamRadiation::supply_canopy_forcing()) every sweep also
23  * writes:
24  * - the direct beam on a horizontal surface at the interfaces m = 0 .. m_top of
25  * every column, counted from the surface, m_top the highest interface a face
26  * of the level reads (a field on the level's grids flattened in z and then
27  * given those interfaces, index k = m), blended clear/cloudy like the fluxes;
28  * - the cosine of the zenith angle, floored at zero (a 2D field).
29  * A face reads the column of its fluid cell at its own height
30  * (two_stream_canopy_sample()): the downwelling fluxes there are the sun and
31  * the sky that reach it (for height-map buildings no building stands above a
32  * face in its own column; a face under the overhang of an STL building sees only
33  * building in IBFaceSet's view fractions, which take each column as solid up to
34  * its top, so its column's fluxes carry no weight), and the
35  * upwelling fluxes there are the ground as seen from that height, through the air
36  * in between.
37  */
38 
39 /**
40  * @brief The fields a level's faces read for one step: the beam and the cosine
41  * of the zenith the sweep of that step wrote, and that sweep's interface fluxes.
42  * All null when the level has not swept this step (the initial report).
43  */
45 {
46  const amrex::MultiFab* beam = nullptr; ///< Direct beam on a horizontal surface, interfaces 0 .. m_top [W/m^2]
47  const amrex::MultiFab* cos_zenith = nullptr; ///< Cosine of the zenith of the sweep, floored at zero (2D) [1]
48  const amrex::MultiFab* fluxes = nullptr; ///< The level's rad_fluxes (SW up, SW down, LW up, LW down) [W/m^2]
49  /// The direct-beam albedo the sweep used at each column's lower boundary (2D) [1], and, as a
50  /// second component, the interface that boundary is at, counted from the ground (0, or a
51  /// roof's with erf.ibseb.two_stream_roofs; two_stream_column_base()). Read only with
52  /// erf.ibseb.ground_view_shadow, which checks for it; valid() does not ask for it.
53  const amrex::MultiFab* ground_albedo_dir = nullptr;
54  [[nodiscard]] bool valid () const noexcept { return beam != nullptr && cos_zenith != nullptr && fluxes != nullptr; }
55 };
56 
57 /**
58  * @brief The interface a column starts on, counted from the ground: 0, or the roof's
59  * with erf.ibseb.two_stream_roofs. It is the second component of the field of
60  * TwoStreamCanopyView::ground_albedo_dir, which the sweep writes with the albedo of the
61  * column's lower boundary.
62  */
63 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
64 int two_stream_column_base (int i, int j, const amrex::Array4<const amrex::Real>& alb_dir) noexcept
65 {
66  return static_cast<int>(alb_dir(i, j, 0, 1) + amrex::Real(0.5));
67 }
68 
69 /**
70  * @brief Write the direct beam on a horizontal surface at the interfaces
71  * 0 .. m_top of column (i,j), from the scratch of the evaluation that has just
72  * run there, into @p beam: the clear-sky evaluation's beam as it is, and the
73  * cloudy evaluation's blended in with the cloud fraction, by the same expression
74  * as the fluxes ((1 - f) clear + f cloudy), so the two round alike in single
75  * precision and the shortwave down less the beam is the diffuse light.
76  *
77  * vertical_two_stream_sweep() keeps the Beer-Lambert beam of every interface in
78  * its scratch (TwoStreamScratch::F_DIR), but writes it only when the shortwave is
79  * on and the sun is up; otherwise the scratch holds the other evaluation's beam or
80  * nothing at all, and the beam is zero. Call it right after each evaluation, before
81  * the next one overwrites the scratch.
82  *
83  * @param scratch the sweep's scratch, as vertical_two_stream_sweep() takes it
84  * @param f_dir_comp the scratch component of the beam (TwoStreamScratch::F_DIR)
85  * @param kmin lowest cell of the column (its surface interface)
86  * @param m_top highest interface of the beam field, at most the column's top
87  * @param sw_on whether the sweep ran its shortwave
88  * @param cos_zenith cosine of the zenith angle the sweep used (unfloored)
89  * @param cloud_fraction the cloud fraction (used only when @p blend)
90  * @param blend false for the clear-sky evaluation (overwrite), true for the cloudy one
91  * @param beam the beam field (component 0, index k = m)
92  */
93 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
94 void two_stream_canopy_beam (int i, int j, int kmin, int m_top,
95  const amrex::Array4<const amrex::Real>& scratch, int f_dir_comp,
96  bool sw_on, amrex::Real cos_zenith, amrex::Real cloud_fraction, bool blend,
97  const amrex::Array4<amrex::Real>& beam)
98 {
99  const bool lit = sw_on && (cos_zenith > amrex::Real(0.0));
100  for (int m = 0; m <= m_top; ++m) {
101  const amrex::Real b = lit ? scratch(i, j, kmin + m, f_dir_comp) : amrex::Real(0.0);
102  beam(i, j, m) = blend ? (1.0 - cloud_fraction) * beam(i, j, m) + cloud_fraction * b : b;
103  }
104 }
105 
106 /**
107  * @brief Sample interface of a face and the weight of the one above. A z face
108  * takes the interface it lies on: a roof (solid below, side -1) the bottom of its
109  * fluid cell (m = k - k0), a ceiling (solid above, side +1, as under an overhang
110  * of a building from an STL file) the top of it (m = k - k0 + 1). A wall takes the
111  * mean of the two interfaces of its cell (m and m + 1), its mean height.
112  *
113  * @param k fluid cell of the face (the level's index)
114  * @param k0 the level's domain bottom
115  * @param dir face direction (0 x, 1 y, 2 z)
116  * @param side side of the fluid cell the solid is on (-1 or +1)
117  * @param w_up weight of interface m + 1
118  * @return the lower sample interface m
119  */
120 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
121 int two_stream_canopy_sample (int k, int k0, int dir, int side, amrex::Real& w_up)
122 {
123  w_up = (dir == 2) ? amrex::Real(0.0) : amrex::Real(0.5);
124  return k - k0 + ((dir == 2 && side > 0) ? 1 : 0);
125 }
126 
127 /** @brief What one face takes from its column (all W/m^2). */
129 {
130  amrex::Real dni = 0.0; ///< Direct-normal irradiance at the face's height
131  amrex::Real diffuse = 0.0; ///< Diffuse shortwave down at the face's height
132  amrex::Real lw_down = 0.0; ///< Longwave down at the face's height
133  amrex::Real sw_up = 0.0; ///< Shortwave up at the face's height (the ground's reflection, seen from there)
134  amrex::Real lw_up = 0.0; ///< Longwave up at the face's height (the ground's emission and the air's below)
135 };
136 
137 /**
138  * @brief The column radiation a face at fluid cell (i,j,k) of direction @p dir,
139  * with its solid on side @p side, takes: at its sample (two_stream_canopy_sample()), the direct-normal
140  * irradiance (the beam over the column's cosine of the zenith), the diffuse
141  * light (the shortwave down less the beam, floored at zero against round-off on
142  * a transparent column), the longwave down, and the shortwave and longwave up,
143  * which the face takes through its view of the ground.
144  *
145  * @param beam beam field (index k = m)
146  * @param cosz cosine of the zenith, floored at zero (2D)
147  * @param fluxes interface fluxes in RRTMGP's level layout (absolute k)
148  */
149 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
150 TwoStreamFaceSky two_stream_face_sky (int i, int j, int k, int k0, int dir, int side,
151  const amrex::Array4<const amrex::Real>& beam,
152  const amrex::Array4<const amrex::Real>& cosz,
153  const amrex::Array4<const amrex::Real>& fluxes)
154 {
155  amrex::Real w_up = 0.0;
156  const int ms = two_stream_canopy_sample(k, k0, dir, side, w_up);
157  const int mu = ms + ((w_up > amrex::Real(0.0)) ? 1 : 0);
158  const amrex::Real w_lo = amrex::Real(1.0) - w_up;
159  const amrex::Real beam_h = w_lo * beam(i, j, ms) + w_up * beam(i, j, mu);
160  const amrex::Real sw_dn = w_lo * fluxes(i, j, k0 + ms, 1) + w_up * fluxes(i, j, k0 + mu, 1);
162  const amrex::Real cz = cosz(i, j, 0);
163  // Both operands valid in both arms: an optimizer may evaluate the division
164  // before the selection, and 0 / 0 at night would raise the invalid flag.
165  s.dni = (cz > amrex::Real(0.0)) ? beam_h / (cz + std::numeric_limits<amrex::Real>::min()) : amrex::Real(0.0);
166  s.diffuse = (sw_dn - beam_h > amrex::Real(0.0)) ? sw_dn - beam_h : amrex::Real(0.0);
167  s.lw_down = w_lo * fluxes(i, j, k0 + ms, 3) + w_up * fluxes(i, j, k0 + mu, 3);
168  s.sw_up = w_lo * fluxes(i, j, k0 + ms, 0) + w_up * fluxes(i, j, k0 + mu, 0);
169  s.lw_up = w_lo * fluxes(i, j, k0 + ms, 2) + w_up * fluxes(i, j, k0 + mu, 2);
170  return s;
171 }
172 
173 /**
174  * @brief The share of column (i,j)'s shortwave up that is its ground's reflection of
175  * the direct beam: (direct-beam albedo x beam at the surface) over the shortwave up at
176  * the surface, which is that reflection plus the ground's reflection of the diffuse
177  * light; in [0, 1], and 0 when nothing comes up. The building faces take this share of
178  * the shortwave up at their own height as the reflected beam (erf.ibseb.ground_view_shadow):
179  * exact where the air between the ground and the face only absorbs, as both parts are
180  * then dimmed alike on the way up; with scattering the light the air sends up from the
181  * beam and from the sky's diffuse light is split in the surface's proportion too.
182  *
183  * @param k0 the level's domain bottom (the surface interface of fluxes)
184  * @param beam beam field (index k = m; m = 0 the surface)
185  * @param fluxes interface fluxes in RRTMGP's level layout (absolute k; component 0 SW up)
186  * @param alb_dir the ground's direct-beam albedo (2D)
187  */
188 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
189 amrex::Real two_stream_ground_beam_share (int i, int j, int k0,
190  const amrex::Array4<const amrex::Real>& beam,
191  const amrex::Array4<const amrex::Real>& fluxes,
192  const amrex::Array4<const amrex::Real>& alb_dir)
193 {
194  // The column's lower boundary: the ground, or the roof it starts on.
195  const int m_base = two_stream_column_base(i, j, alb_dir);
196  const amrex::Real up0 = fluxes(i, j, k0 + m_base, 0);
197  const amrex::Real refl = alb_dir(i, j, 0) * beam(i, j, m_base);
198  // Both operands valid in both arms (see two_stream_face_sky()).
199  const amrex::Real share = (up0 > amrex::Real(0.0) && refl > amrex::Real(0.0))
200  ? refl / (up0 + std::numeric_limits<amrex::Real>::min()) : amrex::Real(0.0);
201  return (share < amrex::Real(1.0)) ? share : amrex::Real(1.0);
202 }
203 
204 /// What two_stream_ground_wall_terms() adds to the ground balance's net shortwave and longwave [W/m^2].
206 {
207  amrex::Real d_sw = 0.0;
208  amrex::Real d_lw = 0.0;
209 };
210 
211 /**
212  * @brief The change of the open ground's net radiation beside walls
213  * (erf.ibseb.ground_sky_view). The walls take the share @p wall_view of the ground's
214  * view of the sky. Through it the ground loses the sky's diffuse light and longwave and
215  * gains the walls':
216  * d_sw = c [(1 - alb_dif) sw_walls - wall_view (sw_net - sw_beam)],
217  * d_lw = c emis (lw_walls - wall_view lw_down).
218  * The share comes from a count of rays and can exceed 1 in a column closed in by walls;
219  * a ground cannot see more than its whole view, so there c = 1 / wall_view scales the
220  * loss and the gain alike (the ground then sees no sky and the walls' mean); c = 1
221  * otherwise. The direct beam is not touched (the shadow takes it).
222  *
223  * @param sw_net the sweep's absorbed shortwave at the ground, beam and diffuse [W/m^2]
224  * @param sw_beam its absorbed direct beam [W/m^2]
225  * @param wall_view the walls' share of the ground's view [1]
226  * @param sw_walls the walls' reflected shortwave arriving on the ground [W/m^2]
227  * @param lw_walls the walls' longwave arriving on the ground [W/m^2]
228  * @param lw_down the sky's longwave down at the ground [W/m^2]
229  * @param absorb_dif the ground's diffuse absorptance, 1 - alb_dif [1]
230  * @param emis the ground's longwave emissivity [1]
231  */
232 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
233 GroundWallTerms two_stream_ground_wall_terms (amrex::Real sw_net, amrex::Real sw_beam, amrex::Real wall_view,
234  amrex::Real sw_walls, amrex::Real lw_walls, amrex::Real lw_down,
235  amrex::Real absorb_dif, amrex::Real emis) noexcept
236 {
237  // The divisor is at least 1 whatever the share: an optimizer that forms both sides of
238  // a choice would otherwise divide by a share of zero, which traps where traps are on.
239  const amrex::Real whole = (wall_view > amrex::Real(1.0)) ? wall_view : amrex::Real(1.0);
240  const amrex::Real c = amrex::Real(1.0) / whole;
242  t.d_sw = c * (absorb_dif * sw_walls - wall_view * (sw_net - sw_beam));
243  t.d_lw = c * emis * (lw_walls - wall_view * lw_down);
244  return t;
245 }
246 
247 #endif // ERF_TWO_STREAM_CANOPY_FORCING_H_
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int two_stream_column_base(int i, int j, const amrex::Array4< const amrex::Real > &alb_dir) noexcept
The interface a column starts on, counted from the ground: 0, or the roof's with erf....
Definition: ERF_TwoStreamCanopyForcing.H:64
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void two_stream_canopy_beam(int i, int j, int kmin, int m_top, const amrex::Array4< const amrex::Real > &scratch, int f_dir_comp, bool sw_on, amrex::Real cos_zenith, amrex::Real cloud_fraction, bool blend, const amrex::Array4< amrex::Real > &beam)
Write the direct beam on a horizontal surface at the interfaces 0 .. m_top of column (i,...
Definition: ERF_TwoStreamCanopyForcing.H:94
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real two_stream_ground_beam_share(int i, int j, int k0, const amrex::Array4< const amrex::Real > &beam, const amrex::Array4< const amrex::Real > &fluxes, const amrex::Array4< const amrex::Real > &alb_dir)
The share of column (i,j)'s shortwave up that is its ground's reflection of the direct beam: (direct-...
Definition: ERF_TwoStreamCanopyForcing.H:189
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GroundWallTerms two_stream_ground_wall_terms(amrex::Real sw_net, amrex::Real sw_beam, amrex::Real wall_view, amrex::Real sw_walls, amrex::Real lw_walls, amrex::Real lw_down, amrex::Real absorb_dif, amrex::Real emis) noexcept
The change of the open ground's net radiation beside walls (erf.ibseb.ground_sky_view)....
Definition: ERF_TwoStreamCanopyForcing.H:233
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TwoStreamFaceSky two_stream_face_sky(int i, int j, int k, int k0, int dir, int side, const amrex::Array4< const amrex::Real > &beam, const amrex::Array4< const amrex::Real > &cosz, const amrex::Array4< const amrex::Real > &fluxes)
The column radiation a face at fluid cell (i,j,k) of direction dir, with its solid on side side,...
Definition: ERF_TwoStreamCanopyForcing.H:150
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int two_stream_canopy_sample(int k, int k0, int dir, int side, amrex::Real &w_up)
Sample interface of a face and the weight of the one above. A z face takes the interface it lies on: ...
Definition: ERF_TwoStreamCanopyForcing.H:121
@ mu
Definition: ERF_AdvanceMorrison.cpp:94
@ t
Definition: ERF_WSM6.H:295
What two_stream_ground_wall_terms() adds to the ground balance's net shortwave and longwave [W/m^2].
Definition: ERF_TwoStreamCanopyForcing.H:206
amrex::Real d_lw
Definition: ERF_TwoStreamCanopyForcing.H:208
amrex::Real d_sw
Definition: ERF_TwoStreamCanopyForcing.H:207
The fields a level's faces read for one step: the beam and the cosine of the zenith the sweep of that...
Definition: ERF_TwoStreamCanopyForcing.H:45
const amrex::MultiFab * fluxes
Definition: ERF_TwoStreamCanopyForcing.H:48
const amrex::MultiFab * beam
Direct beam on a horizontal surface, interfaces 0 .. m_top [W/m^2].
Definition: ERF_TwoStreamCanopyForcing.H:46
const amrex::MultiFab * cos_zenith
Cosine of the zenith of the sweep, floored at zero (2D) [1].
Definition: ERF_TwoStreamCanopyForcing.H:47
bool valid() const noexcept
Definition: ERF_TwoStreamCanopyForcing.H:54
const amrex::MultiFab * ground_albedo_dir
Definition: ERF_TwoStreamCanopyForcing.H:53
What one face takes from its column (all W/m^2).
Definition: ERF_TwoStreamCanopyForcing.H:129
amrex::Real sw_up
Shortwave up at the face's height (the ground's reflection, seen from there)
Definition: ERF_TwoStreamCanopyForcing.H:133
amrex::Real diffuse
Diffuse shortwave down at the face's height.
Definition: ERF_TwoStreamCanopyForcing.H:131
amrex::Real lw_up
Longwave up at the face's height (the ground's emission and the air's below)
Definition: ERF_TwoStreamCanopyForcing.H:134
amrex::Real lw_down
Longwave down at the face's height.
Definition: ERF_TwoStreamCanopyForcing.H:132
amrex::Real dni
Direct-normal irradiance at the face's height.
Definition: ERF_TwoStreamCanopyForcing.H:130