ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_EarthRelativeWind.H
Go to the documentation of this file.
1 /**
2  * \file ERF_EarthRelativeWind.H
3  */
4 #ifndef ERF_EARTH_RELATIVE_WIND_H_
5 #define ERF_EARTH_RELATIVE_WIND_H_
6 
7 #include <AMReX_Array4.H>
8 #include <AMReX_GpuQualifiers.H>
9 #include <AMReX_REAL.H>
10 
11 #include "ERF_NumericalConstants.H"
12 
13 #include <cmath>
14 
15 //
16 // Earth-relative winds from grid-relative ones, shared by the 2D sampled-level
17 // fields and the station time series so that the two cannot disagree.
18 //
19 // (cos_alpha, sin_alpha) is the direction of the grid x axis (increasing i),
20 // counterclockwise from true east, as LatLonMap and grid_rotation_from_latlon
21 // return it. A grid-relative (u, v) wind is then
22 //
23 // u_east = u cos_alpha - v sin_alpha
24 // v_north = u sin_alpha + v cos_alpha
25 //
26 namespace erf_earth_wind {
27 
28 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
29 void grid_to_earth (amrex::Real u, amrex::Real v,
30  amrex::Real cos_alpha, amrex::Real sin_alpha,
31  amrex::Real& u_east, amrex::Real& v_north) noexcept
32 {
33  u_east = u * cos_alpha - v * sin_alpha;
34  v_north = u * sin_alpha + v * cos_alpha;
35 }
36 
37 // The inverse: grid-relative (u, v) from earth-relative components
38 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
39 void earth_to_grid (amrex::Real u_east, amrex::Real v_north,
40  amrex::Real cos_alpha, amrex::Real sin_alpha,
41  amrex::Real& u, amrex::Real& v) noexcept
42 {
43  u = u_east * cos_alpha + v_north * sin_alpha;
44  v = -u_east * sin_alpha + v_north * cos_alpha;
45 }
46 
47 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
48 amrex::Real horizontal_speed (amrex::Real u_east, amrex::Real v_north) noexcept
49 {
50  return std::sqrt(u_east * u_east + v_north * v_north);
51 }
52 
53 //
54 // Meteorological wind direction in degrees, clockwise from north, of where the
55 // wind comes from, in [0, 360). A calm wind (speed <= 1e-12 m/s) has no
56 // direction, and missing_value is returned; the threshold catches an exactly
57 // zero wind, not a light one.
58 //
59 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
60 amrex::Real meteorological_direction (amrex::Real u_east, amrex::Real v_north,
61  amrex::Real missing_value) noexcept
62 {
63  if (horizontal_speed(u_east, v_north) <= amrex::Real(1.0e-12)) {
64  return missing_value;
65  }
66  amrex::Real direction = amrex::Real(270.0)
67  - std::atan2(v_north, u_east) * amrex::Real(180.0) / PI;
68  while (direction < amrex::Real(0.0)) {
69  direction += amrex::Real(360.0);
70  }
71  while (direction >= amrex::Real(360.0)) {
72  direction -= amrex::Real(360.0);
73  }
74  return direction;
75 }
76 
77 //
78 // The orientation of the grid at mass point (i,j) of a lat/lon array ll
79 // (component 0 latitude, component 1 longitude, both in degrees): the
80 // direction of increasing i, counterclockwise from true east. The centered
81 // difference in i collapses to one side at ilo and ihi, the x extent of the
82 // domain; a domain one cell wide has no i direction, and gives (1, 0).
83 //
84 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
85 void grid_rotation_at (const amrex::Array4<const amrex::Real>& ll, int ilo, int ihi,
86  int i, int j, amrex::Real& cos_alpha, amrex::Real& sin_alpha) noexcept
87 {
88  const int im = (i - 1 > ilo) ? i - 1 : ilo;
89  const int ip = (i + 1 < ihi) ? i + 1 : ihi;
90  const amrex::Real inv_di = (ip > im) ? amrex::Real(1.0) / amrex::Real(ip - im)
91  : amrex::Real(0.0);
92 
93  // Displacement on the sphere per cell in i: east and north components, in
94  // degrees of latitude (the common factor, the Earth's radius, cancels).
95  // The longitude difference is taken the short way round the antimeridian.
96  amrex::Real dlon = ll(ip,j,0,1) - ll(im,j,0,1);
97  if (dlon > amrex::Real(180.0)) { dlon -= amrex::Real(360.0); }
98  if (dlon < -amrex::Real(180.0)) { dlon += amrex::Real(360.0); }
99  const amrex::Real lat = ll(i,j,0,0);
100  const amrex::Real north = (ll(ip,j,0,0) - ll(im,j,0,0)) * inv_di;
101  const amrex::Real east = dlon * inv_di * std::cos(lat * PI / amrex::Real(180.0));
102  const amrex::Real norm = std::sqrt(east*east + north*north);
103 
104  if (norm > amrex::Real(0.0)) {
105  cos_alpha = east / norm;
106  sin_alpha = north / norm;
107  } else {
108  cos_alpha = amrex::Real(1.0);
109  sin_alpha = amrex::Real(0.0);
110  }
111 }
112 
113 } // namespace erf_earth_wind
114 
115 #endif
Dimensionless numeric literals and pure mathematical constants.
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:46
Definition: ERF_EarthRelativeWind.H:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void earth_to_grid(amrex::Real u_east, amrex::Real v_north, amrex::Real cos_alpha, amrex::Real sin_alpha, amrex::Real &u, amrex::Real &v) noexcept
Definition: ERF_EarthRelativeWind.H:39
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void grid_to_earth(amrex::Real u, amrex::Real v, amrex::Real cos_alpha, amrex::Real sin_alpha, amrex::Real &u_east, amrex::Real &v_north) noexcept
Definition: ERF_EarthRelativeWind.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real horizontal_speed(amrex::Real u_east, amrex::Real v_north) noexcept
Definition: ERF_EarthRelativeWind.H:48
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real meteorological_direction(amrex::Real u_east, amrex::Real v_north, amrex::Real missing_value) noexcept
Definition: ERF_EarthRelativeWind.H:60
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void grid_rotation_at(const amrex::Array4< const amrex::Real > &ll, int ilo, int ihi, int i, int j, amrex::Real &cos_alpha, amrex::Real &sin_alpha) noexcept
Definition: ERF_EarthRelativeWind.H:85