ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_IBSEBSolar.H
Go to the documentation of this file.
1 #ifndef ERF_IBSEB_SOLAR_H
2 #define ERF_IBSEB_SOLAR_H
3 
4 #include <AMReX_REAL.H>
5 #include <AMReX_GpuQualifiers.H>
6 #include <AMReX_Algorithm.H>
7 #include <AMReX_Array4.H>
8 #include <limits>
10 #include <cmath>
11 
12 /**
13  * \file ERF_IBSEBSolar.H
14  * \brief Sun position, clear-sky shortwave, and the ray cast that decides
15  * whether a building face is in shadow.
16  *
17  * The solar geometry (Spencer's declination and equation of time, the hour
18  * angle from UTC and the site longitude, the zenith, the clockwise-from-north
19  * azimuth, the Earth-Sun distance) is kept here: the radiation models use
20  * the CESM orbital formulation of ERF_OrbCosZenith.H, which gives no
21  * azimuth and has no equation of time; orbital_hour_angle() turns its
22  * calendar day into the hour angle these functions take, for
23  * erf.ibseb.sun_mode = two_stream. The clear-sky formulas follow Bird (1984) as used by the SLUCM
24  * branch. The ray cast is new here.
25  *
26  * **Sun vector convention.** ``ibseb_sun_vector()`` returns the unit vector
27  * from a surface toward the sun, with azimuth measured clockwise from north:
28  * east is +x, north is +y, so ``s = (sin z sin a, sin z cos a, cos z)`` with
29  * z the zenith angle and a the azimuth. A face with outward normal n receives
30  * direct beam only when ``n . s > 0``.
31  *
32  * References: Spencer (1971); Duffie and Beckman (1991); Iqbal (1983);
33  * Bird and Hulstrom (1981).
34  */
35 namespace ibseb {
36 
37 /**
38  * Day angle [rad] of Spencer's (1971) series, 2 pi (day - 1) / 365. The
39  * series' coefficients were fitted with this day angle; the declination, the
40  * equation of time and the Earth-Sun distance below all use it, so day 366
41  * gives the same values as day 1.
42  */
43 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
44 amrex::Real spencer_day_angle (amrex::Real day_of_year)
45 {
46  return 2.0 * PI * (day_of_year - 1.0) / 365.0;
47 }
48 
49 /**
50  * Day of year at a UTC time: the start day plus the whole days elapsed since
51  * 00:00 UTC of that day (t_utc_s may exceed 86400 s in a multi-day run). The
52  * Spencer series repeat every 365 days, so the result needs no wrapping.
53  */
54 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
55 amrex::Real day_of_year_at (amrex::Real day0, amrex::Real t_utc_s)
56 {
57  return day0 + std::floor(t_utc_s / 86400.0);
58 }
59 
60 /** Solar declination [rad] from the day of year (Spencer 1971). */
61 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
62 amrex::Real solar_declination (amrex::Real day_of_year)
63 {
64  const amrex::Real gamma = spencer_day_angle(day_of_year);
65  return 0.006918
66  - 0.399912 * std::cos(gamma) + 0.070257 * std::sin(gamma)
67  - 0.006758 * std::cos(2.0 * gamma) + 0.000907 * std::sin(2.0 * gamma)
68  - 0.002697 * std::cos(3.0 * gamma) + 0.00148 * std::sin(3.0 * gamma);
69 }
70 
71 /** Equation of time [minutes] from the day of year (Spencer 1971). */
72 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
73 amrex::Real equation_of_time (amrex::Real day_of_year)
74 {
75  const amrex::Real gamma = spencer_day_angle(day_of_year);
76  return 229.18 * (0.000075 + 0.001868 * std::cos(gamma) - 0.032077 * std::sin(gamma)
77  - 0.014615 * std::cos(2.0 * gamma) - 0.040849 * std::sin(2.0 * gamma));
78 }
79 
80 /**
81  * Hour angle [rad] from the UTC time of day and the site: solar time is UTC
82  * plus one hour per 15 degrees of longitude (east positive) plus the equation
83  * of time, and the hour angle is 15 degrees per hour from solar noon,
84  * negative in the morning. The time zone plays no part (it would cancel).
85  */
86 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
87 amrex::Real solar_hour_angle (amrex::Real time_utc_s, amrex::Real longitude_deg, amrex::Real day_of_year)
88 {
89  amrex::Real t_utc_h = std::fmod(time_utc_s / 3600.0, 24.0);
90  if (t_utc_h < 0.0) { t_utc_h += 24.0; }
91  const amrex::Real t_solar_h = t_utc_h + longitude_deg / 15.0 + equation_of_time(day_of_year) / 60.0;
92  return 15.0 * (t_solar_h - 12.0) * PI / 180.0;
93 }
94 
95 /**
96  * Hour angle [rad] of the orbital formula the radiation models use
97  * (orbital_cos_zenith_instant() of ERF_OrbCosZenith.H): its
98  * ``cos z = sin(lat) sin(decl) - cos(lat) cos(decl) cos(2 pi frac(calday) + lon)``
99  * is the usual form with the hour angle ``2 pi frac(calday) + lon - pi``. The
100  * fraction of the calendar day is the UTC time of day and there is no equation
101  * of time, so local solar time is UTC plus one hour per 15 degrees of
102  * longitude. Wrapped to [-pi, pi), negative in the morning.
103  */
104 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
105 amrex::Real orbital_hour_angle (double calday, amrex::Real longitude_rad)
106 {
107  const double two_pi = 2.0 * double(PI);
108  double h = two_pi * (calday - std::floor(calday)) + double(longitude_rad) - double(PI);
109  h = std::fmod(h + double(PI), two_pi);
110  if (h < 0.0) { h += two_pi; }
111  return static_cast<amrex::Real>(h - double(PI));
112 }
113 
114 /** Zenith angle [rad] from the latitude, declination and hour angle. */
115 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
116 amrex::Real solar_zenith (amrex::Real latitude_deg, amrex::Real decl, amrex::Real hour_angle)
117 {
118  const amrex::Real lat = latitude_deg * PI / 180.0;
119  amrex::Real cz = std::sin(lat) * std::sin(decl) + std::cos(lat) * std::cos(decl) * std::cos(hour_angle);
120  if (cz < -1.0) { cz = -1.0; }
121  if (cz > 1.0) { cz = 1.0; }
122  return std::acos(cz);
123 }
124 
125 /**
126  * Azimuth [rad] clockwise from north (east is pi/2) from the latitude,
127  * declination, hour angle and zenith; north when the sun is at the zenith or
128  * the site is at a pole, where the azimuth is undefined.
129  */
130 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
131 amrex::Real solar_azimuth (amrex::Real latitude_deg, amrex::Real decl,
132  amrex::Real hour_angle, amrex::Real zenith)
133 {
134  const amrex::Real lat = latitude_deg * PI / 180.0;
135  const amrex::Real sz = std::sin(zenith);
136  const amrex::Real cz = std::cos(zenith);
137  if (sz < 1.0e-6) { return 0.0; }
138  const amrex::Real cos_lat = std::cos(lat);
139  if (std::abs(cos_lat) < 1.0e-6) { return 0.0; }
140  // The hour angle is negative before solar noon, when the sun is east of
141  // south, hence the sign for the clockwise-from-north convention.
142  const amrex::Real sin_az = -std::sin(hour_angle) * std::cos(decl) / sz;
143  const amrex::Real cos_az = (std::sin(decl) - cz * std::sin(lat)) / (sz * cos_lat);
144  amrex::Real az = std::atan2(sin_az, cos_az);
145  if (az < 0.0) { az += 2.0 * PI; }
146  return az;
147 }
148 
149 /**
150  * Earth-Sun distance factor (d0/d)^2 that scales the solar constant for the
151  * day of year (Spencer 1971): about 1.034 at perihelion in early January,
152  * 0.967 at aphelion in early July, annual mean 1.
153  */
154 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
155 amrex::Real earth_sun_distance_factor (amrex::Real day_of_year)
156 {
157  const amrex::Real G = spencer_day_angle(day_of_year);
158  return 1.000110 + 0.034221 * std::cos(G) + 0.001280 * std::sin(G)
159  + 0.000719 * std::cos(2.0 * G) + 0.000077 * std::sin(2.0 * G);
160 }
161 
162 /** Unit vector toward the sun from zenith and azimuth (east +x, north +y). */
163 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
164 void sun_vector (amrex::Real zenith, amrex::Real azimuth,
165  amrex::Real& sx, amrex::Real& sy, amrex::Real& sz)
166 {
167  const amrex::Real s = std::sin(zenith);
168  sx = s * std::sin(azimuth);
169  sy = s * std::cos(azimuth);
170  sz = std::cos(zenith);
171 }
172 
173 /**
174  * Clear-sky direct-normal irradiance [W/m2] (Bird form): the solar constant,
175  * corrected for the Earth-Sun distance, attenuated by ``tau^(1/cos z)``.
176  * Zero when the sun is below the horizon.
177  */
178 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
179 amrex::Real clear_sky_dni (amrex::Real cos_zenith, amrex::Real S0, amrex::Real tau,
180  amrex::Real distance_factor)
181 {
182  if (cos_zenith <= 1.0e-3) return 0.0;
183  return S0 * distance_factor * std::pow(tau, 1.0 / cos_zenith);
184 }
185 
186 /**
187  * Clear-sky diffuse irradiance on a horizontal surface [W/m2]: a fraction
188  * ``k_d`` of what the direct beam lost to the atmosphere reaches the ground
189  * as diffuse light (Liu and Jordan form).
190  */
191 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
192 amrex::Real clear_sky_diffuse_h (amrex::Real cos_zenith, amrex::Real S0, amrex::Real tau,
193  amrex::Real distance_factor, amrex::Real k_d)
194 {
195  if (cos_zenith <= 1.0e-3) return 0.0;
196  const amrex::Real tr = std::pow(tau, 1.0 / cos_zenith);
197  return k_d * S0 * distance_factor * cos_zenith * (1.0 - tr);
198 }
199 
200 /**
201  * The column tops a ray cast on one rank sees: a k-collapsed map over this
202  * rank's patch of the level (its boxes grown by the halo of
203  * IBFaceSet::build_ray_map()), with the domain and the grid it is drawn on.
204  * An entry is the domain-relative k of the highest solid cell of its column,
205  * -1 where the column is fluid; column_top() turns it into a height. The
206  * patch holds the columns of other ranks and the periodic images within the
207  * halo, so the walk needs no communication. Where ``wrap_x`` (``wrap_y``) is
208  * set the patch spans the whole periodic domain in x (y) and a ray that
209  * leaves it comes back in on the other side; otherwise a ray that leaves the
210  * patch has left the map: the far field is open ground (see locate()). A
211  * ray is followed for as many columns as the halo's reach holds
212  * (``max_steps``), which is every column a ray no shallower than the map's
213  * floor can cross before it clears the tallest column or reaches the
214  * ground; a shallower ray stops there too, so a building beyond the halo's
215  * reach never shades, whether the map wraps or not. A level ray is followed
216  * for two rounds of the map and then counts as sky.
217  */
218 struct RayMap
219 {
220  amrex::Array4<const int> top; ///< Column tops over the patch (any k index; use ``top.begin[2]``)
221  int ilo = 0, jlo = 0; ///< Low corner of the domain, in cells
222  int nx = 0, ny = 0; ///< Columns of the domain in x and y
223  bool wrap_x = false, wrap_y = false;///< The patch spans the periodic domain in x / y
224  amrex::Real x_lo = 0.0, y_lo = 0.0; ///< Physical position of the domain's low corner [m]
225  amrex::Real dx = 0.0, dy = 0.0, dz = 0.0; ///< Cell sizes [m]
226  amrex::Real z_ground = 0.0; ///< Ground height [m]
227  amrex::Real z_max = 0.0; ///< Tallest column the map holds [m]; a ray above it meets nothing
228  int max_steps = 0; ///< Columns a ray at the map's floor elevation crosses before it clears z_max (the halo's reach in x plus y)
229  int reach_x = 0, reach_y = 0; ///< The halo's reach in x and in y, in columns, before the domain cap: every patch holds that many columns around its rank's boxes (or stops at a closed domain edge, or wraps)
230 
231  /** Whether the map holds column ``(ci, cj)`` (wrapped into the patch where the patch spans the domain). */
232  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
233  bool locate (int& ci, int& cj) const noexcept
234  {
235  if (ci < top.begin[0] || ci >= top.end[0]) {
236  if (!wrap_x) { return false; }
237  ci = top.begin[0] + ((ci - top.begin[0]) % nx + nx) % nx;
238  }
239  if (cj < top.begin[1] || cj >= top.end[1]) {
240  if (!wrap_y) { return false; }
241  cj = top.begin[1] + ((cj - top.begin[1]) % ny + ny) % ny;
242  }
243  return true;
244  }
245 
246  /** Top height of column ``(ci, cj)`` of the map (the ground where it is fluid). */
247  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
248  amrex::Real column_top (int ci, int cj) const noexcept
249  {
250  const int kt = top(ci, cj, top.begin[2]);
251  return (kt < 0) ? z_ground : z_ground + (kt + 1) * dz;
252  }
253 
254  /** Center of column ``ci`` in x (``cj`` in y) [m]; the column is counted from the domain's low corner and may lie outside it. */
255  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
256  amrex::Real centre_x (int ci) const noexcept { return x_lo + (static_cast<amrex::Real>(ci - ilo) + amrex::Real(0.5)) * dx; }
257  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
258  amrex::Real centre_y (int cj) const noexcept { return y_lo + (static_cast<amrex::Real>(cj - jlo) + amrex::Real(0.5)) * dy; }
259 };
260 
261 /**
262  * Halo of a ray map, in cells: how far a ray no shallower than ``sz_min``
263  * (the sine of its elevation) can travel horizontally before it rises above
264  * ``height`` or falls that far, ``height / tan(elevation)``, rounded up and
265  * with one cell for the column it starts in. At least one cell, so a face
266  * always finds the column of its solid neighbor; a level ray would reach
267  * for ever, which the caller caps at the domain.
268  */
269 inline int ray_halo_cells (amrex::Real height, amrex::Real dx, amrex::Real sz_min)
270 {
271  if (height <= 0.0) { return 1; }
272  const amrex::Real cz = std::sqrt(amrex::max(amrex::Real(0.0), amrex::Real(1.0) - sz_min * sz_min));
273  if (sz_min <= 0.0 || cz <= 0.0) { return std::numeric_limits<int>::max() / 4; }
274  const amrex::Real reach = height * cz / sz_min / dx;
275  if (reach >= amrex::Real(std::numeric_limits<int>::max() / 8)) { return std::numeric_limits<int>::max() / 4; }
276  return amrex::max(1, static_cast<int>(std::ceil(reach)) + 1);
277 }
278 
279 /**
280  * Sine of the elevation of the shallowest ray of the hemisphere sampling
281  * (hemisphere_direction()) with ``n_az`` azimuths and ``n_el`` elevations:
282  * on a roof the ray furthest from the normal, ``cos(theta_max)``; on a wall,
283  * where a ray's sine of elevation is ``sin(theta) sin(phi)``, the ray
284  * nearest the normal at the azimuth nearest the horizontal,
285  * ``sin(theta_min) sin(pi / n_az)``. With an odd ``n_az`` a wall has level
286  * rays (``phi = pi``), which the walk follows for two rounds of the map.
287  */
288 inline amrex::Real hemisphere_min_sz (int n_az, int n_el)
289 {
290  const amrex::Real u_min = 0.5 / n_el, u_max = (n_el - 0.5) / n_el;
291  const amrex::Real roof = std::sqrt(1.0 - u_max);
292  const amrex::Real wall = std::sqrt(u_min) * std::sin(PI / n_az);
293  return amrex::min(roof, wall);
294 }
295 
296 /**
297  * Weight of one ray of the hemisphere sampling, ``1 / (n_az n_el)``: a count of rays
298  * times it is a view fraction. The one expression for the view fractions and for the
299  * sunlit ground view (ground_view_lit()), so that equal counts give equal fractions
300  * to the last bit in either precision.
301  */
302 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
303 amrex::Real hemisphere_ray_weight (int n_az, int n_el) noexcept
304 {
305  const amrex::Real w = 1.0 / static_cast<amrex::Real>(n_az * n_el);
306  return w;
307 }
308 
309 /**
310  * Whether the ray from a point toward the sun hits a building: the one walk of
311  * ray_blocked() (a face, or the center of a column's ground) and of ray_blocked_near()
312  * (the ground a face sees).
313  *
314  * Buildings are the column tops of the rank's ray map (RayMap), so a ray is
315  * blocked wherever its height on entering a column is below that column's
316  * top: buildings stand on the ground, so the lowest point of the ray inside
317  * a column is at its entry, and the height only grows along a ray toward a
318  * sun above the horizon. The walk over the columns is a 2D digital
319  * differential analyser. It stops when the ray rises above the tallest
320  * column or leaves the map (the far field is open ground; in a periodic
321  * direction the map spans, the ray wraps round instead).
322  *
323  * The start point is nudged a little along the ray so a face does not
324  * test its own column at its own height: a wall starts in its fluid cell's
325  * column, whose top is below the face (no overhangs), and a roof starts a
326  * hair above its own top.
327  *
328  * With ``windowed`` the walk also stops, unblocked, at the first column farther than the
329  * map's reach (``reach_x``, ``reach_y``) from a reference column; ``(oi, oj)`` is the
330  * offset of the start's column from that reference, in columns, and is carried along
331  * the walk without wrapping.
332  *
333  * @return true when blocked (the start is in shadow).
334  */
335 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
336 bool ray_blocked_walk (amrex::Real x0, amrex::Real y0, amrex::Real z0,
337  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m,
338  bool windowed, int oi, int oj)
339 {
340  if (sz <= 0.0) return true; // sun at or below the horizon
341  if (z0 >= m.z_max) return false; // nothing stands this high
342  const amrex::Real dx = m.dx, dy = m.dy;
343  const amrex::Real eps = 1.0e-6 * amrex::min(dx, dy);
344  const amrex::Real t0 = 1.0e-3 * amrex::min(dx, dy);
345  amrex::Real x = x0 + t0 * sx, y = y0 + t0 * sy, z = z0 + t0 * sz;
346  int ci = m.ilo + static_cast<int>(std::floor((x - m.x_lo) / dx));
347  int cj = m.jlo + static_cast<int>(std::floor((y - m.y_lo) / dy));
348  const int stepx = (sx > 0.0) ? 1 : ((sx < 0.0) ? -1 : 0);
349  const int stepy = (sy > 0.0) ? 1 : ((sy < 0.0) ? -1 : 0);
350  const amrex::Real big = 1.0e30;
351  const amrex::Real tdx = (stepx != 0) ? dx / std::abs(sx) : big;
352  const amrex::Real tdy = (stepy != 0) ? dy / std::abs(sy) : big;
353  // Distance along the ray to the next column boundary in x and in y.
354  amrex::Real tmx = big, tmy = big;
355  if (stepx > 0) tmx = ((m.x_lo + (ci - m.ilo + 1) * dx) - x) / sx;
356  else if (stepx < 0) tmx = ((m.x_lo + (ci - m.ilo) * dx) - x) / sx;
357  if (stepy > 0) tmy = ((m.y_lo + (cj - m.jlo + 1) * dy) - y) / sy;
358  else if (stepy < 0) tmy = ((m.y_lo + (cj - m.jlo) * dy) - y) / sy;
359  // Columns crossed before the ray passes the tallest one, plus a margin,
360  // and never more than the halo's reach (see RayMap).
361  const amrex::Real t_max = (m.z_max - z) / sz;
362  const amrex::Real steps = amrex::min(t_max * (std::abs(sx) / dx + std::abs(sy) / dy), amrex::Real(m.max_steps));
363  const int max_iter = static_cast<int>(amrex::min(amrex::Real(1.0e9), steps)) + 8;
364  for (int it = 0; it < max_iter; ++it) {
365  // Beyond the reach of the reference column: open, sunlit ground.
366  if (windowed && (std::abs(oi) > m.reach_x || std::abs(oj) > m.reach_y)) return false;
367  // Column (ci, cj) with the ray entering at height z.
368  if (!m.locate(ci, cj)) return false;
369  if (z + eps < m.column_top(ci, cj)) return true;
370  // Step to the next column.
371  amrex::Real t;
372  if (tmx < tmy) { t = tmx; tmx += tdx; ci += stepx; oi += stepx; }
373  else { t = tmy; tmy += tdy; cj += stepy; oj += stepy; }
374  z = z0 + t0 * sz + t * sz;
375  if (z >= m.z_max) return false;
376  }
377  return false;
378 }
379 
380 /**
381  * Whether the ray from a face, or from the center of a column's ground, toward the sun
382  * hits a building (ray_blocked_walk() over all the map holds).
383  *
384  * @return true when blocked (the face, or the ground, is in shadow).
385  */
386 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
387 bool ray_blocked (amrex::Real x0, amrex::Real y0, amrex::Real z0,
388  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m)
389 {
390  return ray_blocked_walk(x0, y0, z0, sx, sy, sz, m, false, 0, 0);
391 }
392 
393 /**
394  * ray_blocked() for the ground a face sees (the sunlit part of a face's ground view,
395  * ground_view_lit()): the same walk (ray_blocked_walk()) from the same start as the
396  * ground's own shadow (IBFaceSet::compute_ground_shadow()), the center of the ground of
397  * column ``(ci0, cj0)``, kept within the map's reach (``reach_x``, ``reach_y``) of the
398  * face's own column ``(ci_ref, cj_ref)``; the columns are counted without wrapping.
399  * Every rank's patch holds that window around each of its faces (its boxes grown by
400  * the reach, a closed domain edge or the wrap-round apart), so the answer is the same
401  * however the level is cut into boxes and ranks: a building farther from the face than
402  * the reach casts no shadow in that face's view, and ground farther than that is
403  * sunlit, open ground. For a ray that stays in the window the result is the ground
404  * shadow of that column.
405  *
406  * @return true when blocked (the column's ground is in shadow).
407  */
408 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
409 bool ray_blocked_near (int ci0, int cj0,
410  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m,
411  int ci_ref, int cj_ref)
412 {
413  return ray_blocked_walk(m.centre_x(ci0), m.centre_y(cj0), m.z_ground, sx, sy, sz, m,
414  true, ci0 - ci_ref, cj0 - cj_ref);
415 }
416 
417 /// What a ray from a face ends on.
418 enum RayHit : int { RAY_SKY = 0, RAY_GROUND = 1, RAY_BUILDING = 2 };
419 
420 /**
421  * Where a ray from a face ends: sky, ground or a building, for any
422  * direction (the hemisphere sampling of the view fractions). The same column walk as
423  * ray_blocked(), with the height test made direction-aware:
424  *
425  * - rising or level rays (``sz >= 0``) are blocked by a column whose top is
426  * above the entry height; a level ray that leaves the map, or has crossed
427  * the domain twice where the map wraps, ends at the horizon, which counts as sky;
428  * - falling rays (``sz < 0``) are blocked by a solid column whose top is
429  * above the exit height (the ray descends inside the column) and reach
430  * the ground where the exit height is at or below ``z_ground``.
431  *
432  * Solid columns are those with a top above the ground. Leaving the map
433  * counts as sky for a rising ray and as ground for a falling one: the far
434  * field is open ground. Where the map spans a periodic direction the ray
435  * wraps round instead.
436  *
437  * For a ray that reaches the ground inside the map, ``landed`` is set and
438  * ``(li, lj)`` is the column it lands in, counted without wrapping (so the
439  * distance from the start is kept where the map wraps). A ray that ends on the
440  * ground by leaving the map, or by running out of steps, has not landed: it
441  * went to the open ground of the far field.
442  */
443 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
444 int ray_hit_landing (amrex::Real x0, amrex::Real y0, amrex::Real z0,
445  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m,
446  bool& landed, int& li, int& lj)
447 {
448  landed = false; li = 0; lj = 0;
449  const amrex::Real dx = m.dx, dy = m.dy;
450  const amrex::Real eps = 1.0e-6 * amrex::min(dx, dy);
451  if (sz > 0.0 && z0 >= m.z_max) return RAY_SKY;
452  const amrex::Real t0 = 1.0e-3 * amrex::min(dx, dy);
453  amrex::Real x = x0 + t0 * sx, y = y0 + t0 * sy, z = z0 + t0 * sz;
454  // The column the ray is in, counted without wrapping.
455  int ui = m.ilo + static_cast<int>(std::floor((x - m.x_lo) / dx));
456  int uj = m.jlo + static_cast<int>(std::floor((y - m.y_lo) / dy));
457  const int stepx = (sx > 0.0) ? 1 : ((sx < 0.0) ? -1 : 0);
458  const int stepy = (sy > 0.0) ? 1 : ((sy < 0.0) ? -1 : 0);
459  const amrex::Real big = 1.0e30;
460  const amrex::Real tdx = (stepx != 0) ? dx / std::abs(sx) : big;
461  const amrex::Real tdy = (stepy != 0) ? dy / std::abs(sy) : big;
462  amrex::Real tmx = big, tmy = big;
463  if (stepx > 0) tmx = ((m.x_lo + (ui - m.ilo + 1) * dx) - x) / sx;
464  else if (stepx < 0) tmx = ((m.x_lo + (ui - m.ilo) * dx) - x) / sx;
465  if (stepy > 0) tmy = ((m.y_lo + (uj - m.jlo + 1) * dy) - y) / sy;
466  else if (stepy < 0) tmy = ((m.y_lo + (uj - m.jlo) * dy) - y) / sy;
467  if (stepx == 0 && stepy == 0) { // vertical ray
468  if (sz > 0.0) return RAY_SKY;
469  // straight down from a wall face never happens (the face is vertical);
470  // from a roof the hemisphere points up. Treat as ground for safety.
471  return RAY_GROUND;
472  }
473  // Columns crossed before the ray passes the tallest column or reaches
474  // the ground, plus a margin, and never more than the halo's reach (see
475  // RayMap). A ray within 1e-9 of level (phi = pi of an odd n_az gives sz
476  // of 1e-16) is a level ray and gets two rounds of the map.
477  const bool level = (std::abs(sz) < 1.0e-9);
478  const amrex::Real t_max = (sz > 0.0) ? (m.z_max - z) / sz : ((sz < 0.0) ? (z - m.z_ground) / (-sz) : big);
479  const amrex::Real rounds = amrex::Real(2 * ((m.top.end[0] - m.top.begin[0]) + (m.top.end[1] - m.top.begin[1])));
480  const amrex::Real steps = level ? rounds
481  : amrex::min(t_max * (std::abs(sx) / dx + std::abs(sy) / dy), amrex::Real(m.max_steps));
482  const int max_iter = static_cast<int>(amrex::min(amrex::Real(1.0e9), steps)) + 8;
483  for (int it = 0; it < max_iter; ++it) {
484  int ci = ui, cj = uj;
485  if (!m.locate(ci, cj)) return (sz >= 0.0) ? RAY_SKY : RAY_GROUND;
486  const amrex::Real top = m.column_top(ci, cj);
487  const bool solid = (top > m.z_ground + eps);
488  const amrex::Real t_next = amrex::min(tmx, tmy);
489  const amrex::Real z_out = z0 + t0 * sz + t_next * sz;
490  if (sz >= 0.0) {
491  if (solid && z + eps < top) return RAY_BUILDING;
492  } else {
493  if (solid && z + eps < top) return RAY_BUILDING; // entered below the top
494  if (solid && z_out < top - eps) return RAY_BUILDING; // descends into it
495  if (z_out <= m.z_ground + eps) { landed = true; li = ui; lj = uj; return RAY_GROUND; }
496  }
497  if (tmx < tmy) { tmx += tdx; ui += stepx; }
498  else { tmy += tdy; uj += stepy; }
499  z = z_out;
500  if (sz > 0.0 && z >= m.z_max) return RAY_SKY;
501  }
502  return (sz >= 0.0) ? RAY_SKY : RAY_GROUND;
503 }
504 
505 /** Where a ray from a face ends (ray_hit_landing() without the landing column). */
506 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
507 int ray_hit (amrex::Real x0, amrex::Real y0, amrex::Real z0,
508  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m)
509 {
510  bool landed = false;
511  int li = 0, lj = 0;
512  return ray_hit_landing(x0, y0, z0, sx, sy, sz, m, landed, li, lj);
513 }
514 
515 /**
516  * Direction of the ``(ia, ie)`` sample of a cosine-weighted hemisphere
517  * around the outward normal of a face with direction ``dir`` and normal
518  * sign ``nsign`` (+1 along the axis, -1 against it). Stratified in the
519  * azimuth ``phi = 2 pi (ia + 1/2) / n_az`` and in ``u = (ie + 1/2) / n_el``
520  * with ``theta = asin(sqrt(u))`` from the normal, so every sample carries
521  * the same weight and the counts are view factors.
522  */
523 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
524 void hemisphere_direction (int dir, int nsign, int ia, int ie, int n_az, int n_el,
525  amrex::Real& sx, amrex::Real& sy, amrex::Real& sz)
526 {
527  const amrex::Real u = (ie + 0.5) / n_el;
528  const amrex::Real theta = std::asin(std::sqrt(u));
529  const amrex::Real phi = 2.0 * PI * (ia + 0.5) / n_az;
530  const amrex::Real cn = std::cos(theta), ct = std::sin(theta) * std::cos(phi), st = std::sin(theta) * std::sin(phi);
531  // Normal and two tangents for each axis.
532  amrex::Real n[3] = {0.0, 0.0, 0.0}, t1[3] = {0.0, 0.0, 0.0}, t2[3] = {0.0, 0.0, 0.0};
533  n[dir] = nsign;
534  if (dir == 0) { t1[1] = 1.0; t2[2] = 1.0; }
535  else if (dir == 1) { t1[0] = 1.0; t2[2] = 1.0; }
536  else { t1[0] = 1.0; t2[1] = 1.0; }
537  sx = cn * n[0] + ct * t1[0] + st * t2[0];
538  sy = cn * n[1] + ct * t1[1] + st * t2[1];
539  sz = cn * n[2] + ct * t1[2] + st * t2[2];
540 }
541 
542 /**
543  * Where a ground ray of a face lands, as kept for erf.ibseb.ground_view_shadow: the
544  * landing column's offset from the column of the face's fluid cell, in x and in y,
545  * 16 bits each. An offset beyond the map's reach, or beyond what 16 bits hold, is not
546  * kept: that ground is the open, sunlit ground of the far field (landing_kept()).
547  * A kept landing's word is never below 65536, which leaves the two words below for the
548  * rays that are not kept (ray_landing_word()).
549  */
550 constexpr int landing_offset_max = 32767;
551 constexpr unsigned int landing_none = 0U; ///< The ray does not end on the ground
552 constexpr unsigned int landing_far = 1U; ///< The ray ends on the far field's open ground
553 
554 /** Whether a landing ``(di, dj)`` columns from the face is kept (see pack_landing()). */
555 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
556 bool landing_kept (int di, int dj, const RayMap& m) noexcept
557 {
558  const int rx = (m.reach_x < landing_offset_max) ? m.reach_x : landing_offset_max;
559  const int ry = (m.reach_y < landing_offset_max) ? m.reach_y : landing_offset_max;
560  return std::abs(di) <= rx && std::abs(dj) <= ry;
561 }
562 
563 /** A kept landing's offsets ``(di, dj)`` in one word. */
564 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
565 unsigned int pack_landing (int di, int dj) noexcept
566 {
567  return (static_cast<unsigned int>(di + 32768) << 16) | static_cast<unsigned int>(dj + 32768);
568 }
569 
570 /** The offsets of a word of pack_landing(). */
571 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
572 void unpack_landing (unsigned int w, int& di, int& dj) noexcept
573 {
574  di = static_cast<int>(w >> 16) - 32768;
575  dj = static_cast<int>(w & 0xFFFFu) - 32768;
576 }
577 
578 /**
579  * Where one hemisphere ray of a face ends, as one word: a kept landing (pack_landing()),
580  * ``landing_far`` for a ray that ends on the ground of the far field, ``landing_none``
581  * for a ray that ends on the sky or a building.
582  *
583  * @param ci_f, cj_f the column of the face's fluid cell
584  */
585 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
586 unsigned int ray_landing_word (amrex::Real x0, amrex::Real y0, amrex::Real z0,
587  amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m,
588  int ci_f, int cj_f)
589 {
590  bool landed = false;
591  int li = 0, lj = 0;
592  if (ray_hit_landing(x0, y0, z0, sx, sy, sz, m, landed, li, lj) != RAY_GROUND) { return landing_none; }
593  if (landed && landing_kept(li - ci_f, lj - cj_f, m)) { return pack_landing(li - ci_f, lj - cj_f); }
594  return landing_far;
595 }
596 
597 /**
598  * Whether the ground reflects a beam that buildings can shade (erf.ibseb.ground_view_shadow
599  * with the two-stream columns): the sun that casts the shadows (the faces', ``sz`` the
600  * sine of its elevation) is above the horizon and the column brings a direct beam
601  * ``beam_ground`` to its ground. The one condition for the sunlit ground view and for
602  * the beam's share of the reflection, so neither is taken without the other.
603  */
604 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
605 bool ground_view_beam_up (amrex::Real sz, amrex::Real beam_ground) noexcept
606 {
607  return sz > 0.0 && beam_ground > 0.0;
608 }
609 
610 /**
611  * The sunlit part of a face's view of the ground (erf.ibseb.ground_view_shadow): of
612  * the hemisphere rays of the view fractions that end on the ground, the share that
613  * ends on ground the sun reaches, over all the rays (so the result never exceeds the
614  * ground view fraction). The landings ``land[0 .. n_land)`` are the distinct columns
615  * the face's ground rays land in and ``rays[0 .. n_land)`` the number of rays that land
616  * in each (IBFaceSet::build_ground_landings(), kept once: the geometry is static). Each
617  * column takes the shadow at the center of its ground, cast toward the sun
618  * ``(sx, sy, sz)`` by the walk of the ground's own shadow
619  * (IBFaceSet::compute_ground_shadow()), within the map's reach of the face's column
620  * ``(ci_f, cj_f)`` (ray_blocked_near()): one walk per column, whatever the number of
621  * rays that land in it. The ``n_far`` rays that reach the ground beyond that reach, or
622  * by leaving the map, land on the open ground of the far field, which is sunlit. With
623  * the sun at or below the horizon nothing is sunlit.
624  *
625  * @param ci_f, cj_f the column of the face's fluid cell
626  * @param inv the weight of one ray (hemisphere_ray_weight())
627  */
628 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
629 amrex::Real ground_view_lit (int ci_f, int cj_f, int n_far, const unsigned int* land, const int* rays, int n_land,
630  amrex::Real inv, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap& m)
631 {
632  if (sz <= 0.0) { return 0.0; }
633  int n_lit = n_far;
634  for (int n = 0; n < n_land; ++n) {
635  int di = 0, dj = 0;
636  unpack_landing(land[n], di, dj);
637  if (!ray_blocked_near(ci_f + di, cj_f + dj, sx, sy, sz, m, ci_f, cj_f)) { n_lit += rays[n]; }
638  }
639  // The count times the weight of a ray, as the view fractions are formed
640  // (IBFaceSet::compute_view_fractions()): a ground view that is all sunlit then
641  // equals the ground view fraction to the last bit.
642  return n_lit * inv;
643 }
644 
645 } // namespace ibseb
646 
647 #endif
std::vector< std::array< Box, 3 > > reach
Definition: ERF_ActuatorSpreading.cpp:222
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
const Real dy
Definition: ERF_InitCustomPert_ABL.H:101
amrex::Real gamma
Definition: ERF_InitCustomPert_DataAssimilation_ISV.H:9
Real height
Definition: ERF_InitCustomPert_SquallLine.H:37
Dimensionless numeric literals and pure mathematical constants.
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:46
@ theta
Definition: ERF_Kessler.H:26
@ U
Definition: ERF_IndexDefines.H:178
@ t
Definition: ERF_WSM6.H:295
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real eps()
Definition: ERF_ShocMoments.cpp:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real phi(amrex::Real d, amrex::Real z0, amrex::Real psi) noexcept
Definition: ERF_ImmersedWallCell.H:176
Definition: ERF_IBSEBBalance.H:51
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ray_hit(amrex::Real x0, amrex::Real y0, amrex::Real z0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m)
Definition: ERF_IBSEBSolar.H:507
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE unsigned int pack_landing(int di, int dj) noexcept
Definition: ERF_IBSEBSolar.H:565
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool ray_blocked_near(int ci0, int cj0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m, int ci_ref, int cj_ref)
Definition: ERF_IBSEBSolar.H:409
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE unsigned int ray_landing_word(amrex::Real x0, amrex::Real y0, amrex::Real z0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m, int ci_f, int cj_f)
Definition: ERF_IBSEBSolar.H:586
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void hemisphere_direction(int dir, int nsign, int ia, int ie, int n_az, int n_el, amrex::Real &sx, amrex::Real &sy, amrex::Real &sz)
Definition: ERF_IBSEBSolar.H:524
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool ray_blocked_walk(amrex::Real x0, amrex::Real y0, amrex::Real z0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m, bool windowed, int oi, int oj)
Definition: ERF_IBSEBSolar.H:336
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int ray_hit_landing(amrex::Real x0, amrex::Real y0, amrex::Real z0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m, bool &landed, int &li, int &lj)
Definition: ERF_IBSEBSolar.H:444
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real solar_zenith(amrex::Real latitude_deg, amrex::Real decl, amrex::Real hour_angle)
Definition: ERF_IBSEBSolar.H:116
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void unpack_landing(unsigned int w, int &di, int &dj) noexcept
Definition: ERF_IBSEBSolar.H:572
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real clear_sky_dni(amrex::Real cos_zenith, amrex::Real S0, amrex::Real tau, amrex::Real distance_factor)
Definition: ERF_IBSEBSolar.H:179
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real orbital_hour_angle(double calday, amrex::Real longitude_rad)
Definition: ERF_IBSEBSolar.H:105
amrex::Real hemisphere_min_sz(int n_az, int n_el)
Definition: ERF_IBSEBSolar.H:288
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real equation_of_time(amrex::Real day_of_year)
Definition: ERF_IBSEBSolar.H:73
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real earth_sun_distance_factor(amrex::Real day_of_year)
Definition: ERF_IBSEBSolar.H:155
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool ground_view_beam_up(amrex::Real sz, amrex::Real beam_ground) noexcept
Definition: ERF_IBSEBSolar.H:605
constexpr unsigned int landing_none
The ray does not end on the ground.
Definition: ERF_IBSEBSolar.H:551
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real hemisphere_ray_weight(int n_az, int n_el) noexcept
Definition: ERF_IBSEBSolar.H:303
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real ground_view_lit(int ci_f, int cj_f, int n_far, const unsigned int *land, const int *rays, int n_land, amrex::Real inv, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m)
Definition: ERF_IBSEBSolar.H:629
RayHit
What a ray from a face ends on.
Definition: ERF_IBSEBSolar.H:418
@ RAY_GROUND
Definition: ERF_IBSEBSolar.H:418
@ RAY_SKY
Definition: ERF_IBSEBSolar.H:418
@ RAY_BUILDING
Definition: ERF_IBSEBSolar.H:418
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void sun_vector(amrex::Real zenith, amrex::Real azimuth, amrex::Real &sx, amrex::Real &sy, amrex::Real &sz)
Definition: ERF_IBSEBSolar.H:164
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool ray_blocked(amrex::Real x0, amrex::Real y0, amrex::Real z0, amrex::Real sx, amrex::Real sy, amrex::Real sz, const RayMap &m)
Definition: ERF_IBSEBSolar.H:387
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real solar_azimuth(amrex::Real latitude_deg, amrex::Real decl, amrex::Real hour_angle, amrex::Real zenith)
Definition: ERF_IBSEBSolar.H:131
int ray_halo_cells(amrex::Real height, amrex::Real dx, amrex::Real sz_min)
Definition: ERF_IBSEBSolar.H:269
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real day_of_year_at(amrex::Real day0, amrex::Real t_utc_s)
Definition: ERF_IBSEBSolar.H:55
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real solar_declination(amrex::Real day_of_year)
Definition: ERF_IBSEBSolar.H:62
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real spencer_day_angle(amrex::Real day_of_year)
Definition: ERF_IBSEBSolar.H:44
constexpr unsigned int landing_far
The ray ends on the far field's open ground.
Definition: ERF_IBSEBSolar.H:552
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real solar_hour_angle(amrex::Real time_utc_s, amrex::Real longitude_deg, amrex::Real day_of_year)
Definition: ERF_IBSEBSolar.H:87
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool landing_kept(int di, int dj, const RayMap &m) noexcept
Definition: ERF_IBSEBSolar.H:556
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real clear_sky_diffuse_h(amrex::Real cos_zenith, amrex::Real S0, amrex::Real tau, amrex::Real distance_factor, amrex::Real k_d)
Definition: ERF_IBSEBSolar.H:192
constexpr int landing_offset_max
Definition: ERF_IBSEBSolar.H:550
real(c_double), parameter t0
Definition: ERF_module_model_constants.F90:39
real(c_double), private ci
Definition: ERF_module_mp_morr_two_moment.F90:203
real(c_double), private di
Definition: ERF_module_mp_morr_two_moment.F90:203
Definition: ERF_IBSEBSolar.H:219
amrex::Real x_lo
Definition: ERF_IBSEBSolar.H:224
amrex::Array4< const int > top
Column tops over the patch (any k index; use top.begin[2])
Definition: ERF_IBSEBSolar.H:220
int reach_x
Definition: ERF_IBSEBSolar.H:229
int ilo
Definition: ERF_IBSEBSolar.H:221
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real column_top(int ci, int cj) const noexcept
Definition: ERF_IBSEBSolar.H:248
amrex::Real y_lo
Physical position of the domain's low corner [m].
Definition: ERF_IBSEBSolar.H:224
amrex::Real z_max
Tallest column the map holds [m]; a ray above it meets nothing.
Definition: ERF_IBSEBSolar.H:227
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real centre_y(int cj) const noexcept
Definition: ERF_IBSEBSolar.H:258
bool wrap_x
Definition: ERF_IBSEBSolar.H:223
int ny
Columns of the domain in x and y.
Definition: ERF_IBSEBSolar.H:222
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real centre_x(int ci) const noexcept
Definition: ERF_IBSEBSolar.H:256
amrex::Real dx
Definition: ERF_IBSEBSolar.H:225
amrex::Real z_ground
Ground height [m].
Definition: ERF_IBSEBSolar.H:226
bool wrap_y
The patch spans the periodic domain in x / y.
Definition: ERF_IBSEBSolar.H:223
int max_steps
Columns a ray at the map's floor elevation crosses before it clears z_max (the halo's reach in x plus...
Definition: ERF_IBSEBSolar.H:228
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool locate(int &ci, int &cj) const noexcept
Definition: ERF_IBSEBSolar.H:233
amrex::Real dy
Definition: ERF_IBSEBSolar.H:225
amrex::Real dz
Cell sizes [m].
Definition: ERF_IBSEBSolar.H:225
int jlo
Low corner of the domain, in cells.
Definition: ERF_IBSEBSolar.H:221
int reach_y
The halo's reach in x and in y, in columns, before the domain cap: every patch holds that many column...
Definition: ERF_IBSEBSolar.H:229
int nx
Definition: ERF_IBSEBSolar.H:222