ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_LatLonMap.H
Go to the documentation of this file.
1 /**
2  * \file ERF_LatLonMap.H
3  */
4 #ifndef ERF_LATLONMAP_H_
5 #define ERF_LATLONMAP_H_
6 
7 #include <AMReX_Array.H>
8 #include <AMReX_Array4.H>
9 #include <AMReX_Box.H>
10 #include <AMReX_BoxArray.H>
11 #include <AMReX_DistributionMapping.H>
12 #include <AMReX_Geometry.H>
13 #include <AMReX_MultiFab.H>
14 #include <AMReX_REAL.H>
15 
16 #include <algorithm>
17 #include <cmath>
18 #include <limits>
19 #include <string>
20 
21 //
22 // Placing a point given by latitude and longitude on the model grid.
23 //
24 // The model has no map projection of its own: a run initialized from a WRF or
25 // metgrid file carries the latitude and longitude of every level-0 mass point
26 // (lat_m / lon_m), whatever projection produced them. A requested point is
27 // found by searching those arrays for the nearest mass point and then inverting
28 // the map locally with one linear solve, which is exact for a map that is
29 // linear over a cell and well under a cell width in error for any smooth one.
30 // Longitude differences are taken the short way round, so a grid that crosses
31 // the antimeridian is handled like any other.
32 //
33 // The same arrays give the orientation of the grid: the direction in which the
34 // i index grows, measured counterclockwise from true east, is the angle alpha
35 // by which an earth-relative (east, north) wind has to be rotated to become a
36 // grid-relative (u, v) wind.
37 //
38 
39 //
40 // Invert the latitude/longitude map locally. Given the difference between the
41 // requested lat/lon and the lat/lon of a grid point, and the gradients of
42 // latitude and longitude with respect to the two horizontal index directions at
43 // that point, return the offset in cells from the grid point to the request.
44 //
45 // Returns false when the two gradients are parallel and the map is therefore not
46 // locally invertible. "Parallel" is judged relative to the gradients' size and
47 // to the precision: in single precision the determinant of an exactly
48 // degenerate map comes out of rounding at about 1e-8 of its scale squared, far
49 // above a fixed 1e-12, so the threshold is at least 64 machine epsilons (which
50 // leaves double precision at 1e-12).
51 //
52 inline bool
53 invert_latlon_offset (amrex::Real dlat, amrex::Real dlon,
54  amrex::Real dlat_di, amrex::Real dlon_di,
55  amrex::Real dlat_dj, amrex::Real dlon_dj,
56  amrex::Real& di, amrex::Real& dj)
57 {
58  const amrex::Real scale = std::max(std::max(std::abs(dlat_di), std::abs(dlon_di)),
59  std::max(std::abs(dlat_dj), std::abs(dlon_dj)));
60  const amrex::Real det = dlat_di*dlon_dj - dlat_dj*dlon_di;
61  const amrex::Real rel = std::max(amrex::Real(1.0e-12),
62  amrex::Real(64) * std::numeric_limits<amrex::Real>::epsilon());
63  if (scale <= amrex::Real(0.0) || std::abs(det) <= rel*scale*scale) {
64  return false;
65  }
66  di = ( dlat*dlon_dj - dlon*dlat_dj) / det;
67  dj = (-dlat*dlon_di + dlon*dlat_di) / det;
68  return true;
69 }
70 
71 //
72 // A longitude difference in degrees, brought into [-180, 180] so that two
73 // points either side of the antimeridian are close rather than 360 degrees apart.
74 //
75 inline amrex::Real
76 wrap_longitude_difference (amrex::Real dlon)
77 {
78  if (dlon > amrex::Real(180.0)) { return dlon - amrex::Real(360.0); }
79  if (dlon < -amrex::Real(180.0)) { return dlon + amrex::Real(360.0); }
80  return dlon;
81 }
82 
83 //
84 // The result of placing a latitude/longitude on the grid.
85 //
87 {
88  // Domain coordinates of the point
89  amrex::Real x = amrex::Real(0.0);
90  amrex::Real y = amrex::Real(0.0);
91 
92  // The lat/lon actually reached by (x,y): equals the request to within the
93  // accuracy of the local inverse
94  amrex::Real lat = amrex::Real(0.0);
95  amrex::Real lon = amrex::Real(0.0);
96 
97  // The nearest mass point, reported when the request cannot be placed
98  amrex::Real near_lat = amrex::Real(0.0);
99  amrex::Real near_lon = amrex::Real(0.0);
100 
101  // Orientation of the grid x axis, counterclockwise from true east
102  amrex::Real cos_alpha = amrex::Real(1.0);
103  amrex::Real sin_alpha = amrex::Real(0.0);
104 };
105 
106 enum class LatLonStatus {
107  Ok, // placed
108  Degenerate, // the lat/lon gradients are parallel near the point
109  TooFar // more than one cell from the nearest mass point: outside the domain
110 };
111 
112 //
113 // The orientation of the grid at mass point (i,j) of a lat/lon array ll
114 // (component 0 latitude, component 1 longitude, both in degrees) over the 2D
115 // domain dom: the direction of increasing i, counterclockwise from true east.
116 //
117 void grid_rotation_from_latlon (const amrex::Array4<const amrex::Real>& ll,
118  const amrex::Box& dom, int i, int j,
119  amrex::Real& cos_alpha, amrex::Real& sin_alpha);
120 
121 //
122 // Place (req_lat, req_lon) on the grid described by the lat/lon array ll over
123 // the 2D domain dom, whose cell (i,j) is centered at problo + (i+1/2) dx. Host
124 // code; the whole array must be addressable.
125 //
126 LatLonStatus locate_latlon_on_grid (const amrex::Array4<const amrex::Real>& ll,
127  const amrex::Box& dom,
128  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& problo,
129  const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dx,
130  amrex::Real req_lat, amrex::Real req_lon,
131  LatLonLocation& loc);
132 
133 //
134 // The grid orientation at every mass point of one level: cos_alpha and sin_alpha
135 // (2D, one component, on the level's ba2d/dmap) from that level's lat and lon,
136 // which may be on any layout of the same 2D domain. Collective.
137 //
138 void fill_grid_rotation (amrex::MultiFab& cos_alpha, amrex::MultiFab& sin_alpha,
139  const amrex::MultiFab& lat, const amrex::MultiFab& lon,
140  const amrex::Geometry& geom);
141 
142 // Copy lat and lon (2D, one component each) into the two components of latlon,
143 // all three on the same BoxArray and DistributionMapping
144 void pack_latlon (amrex::MultiFab& latlon, const amrex::MultiFab& lat, const amrex::MultiFab& lon);
145 
146 //
147 // The level-0 lat/lon arrays gathered onto the I/O rank, with the two
148 // questions asked of them answered there and broadcast. Every member function
149 // is collective: call it on every rank with the same arguments.
150 //
152 {
153 public:
154  // lat and lon are the level-0 mass-point arrays (lat_m[0], lon_m[0]),
155  // defined on ba2d/dm; geom0 is the level-0 geometry.
156  LatLonMap (const amrex::MultiFab& lat, const amrex::MultiFab& lon,
157  const amrex::BoxArray& ba2d, const amrex::DistributionMapping& dm,
158  const amrex::Geometry& geom0);
159 
160  // Place a latitude/longitude on the grid
161  LatLonStatus locate (amrex::Real req_lat, amrex::Real req_lon, LatLonLocation& loc) const;
162 
163  // Place a latitude/longitude on the grid, and abort, naming it as what, when it cannot
164  // be inverted or is outside the domain. Collective, like locate.
165  LatLonLocation locate_or_abort (amrex::Real req_lat, amrex::Real req_lon,
166  const std::string& what) const;
167 
168  // Orientation of the grid at domain coordinates (x, y), from the cell that contains it
169  void rotation_at (amrex::Real x, amrex::Real y,
170  amrex::Real& cos_alpha, amrex::Real& sin_alpha) const;
171 
172 private:
173  amrex::MultiFab m_ll; // one box on the I/O rank, lat and lon
174  amrex::Box m_dom; // the 2D level-0 domain
175  amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> m_problo;
176  amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> m_dx;
177 };
178 
179 #endif
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
auto problo
Definition: ERF_InitCustomPertVels_ABL.H:36
LatLonStatus
Definition: ERF_LatLonMap.H:106
LatLonStatus locate_latlon_on_grid(const amrex::Array4< const amrex::Real > &ll, const amrex::Box &dom, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &problo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dx, amrex::Real req_lat, amrex::Real req_lon, LatLonLocation &loc)
amrex::Real wrap_longitude_difference(amrex::Real dlon)
Definition: ERF_LatLonMap.H:76
bool invert_latlon_offset(amrex::Real dlat, amrex::Real dlon, amrex::Real dlat_di, amrex::Real dlon_di, amrex::Real dlat_dj, amrex::Real dlon_dj, amrex::Real &di, amrex::Real &dj)
Definition: ERF_LatLonMap.H:53
void pack_latlon(amrex::MultiFab &latlon, const amrex::MultiFab &lat, const amrex::MultiFab &lon)
void grid_rotation_from_latlon(const amrex::Array4< const amrex::Real > &ll, const amrex::Box &dom, int i, int j, amrex::Real &cos_alpha, amrex::Real &sin_alpha)
void fill_grid_rotation(amrex::MultiFab &cos_alpha, amrex::MultiFab &sin_alpha, const amrex::MultiFab &lat, const amrex::MultiFab &lon, const amrex::Geometry &geom)
Definition: ERF_LatLonMap.H:152
void rotation_at(amrex::Real x, amrex::Real y, amrex::Real &cos_alpha, amrex::Real &sin_alpha) const
Definition: ERF_LatLonMap.cpp:253
LatLonStatus locate(amrex::Real req_lat, amrex::Real req_lon, LatLonLocation &loc) const
Definition: ERF_LatLonMap.cpp:207
amrex::Box m_dom
Definition: ERF_LatLonMap.H:174
LatLonLocation locate_or_abort(amrex::Real req_lat, amrex::Real req_lon, const std::string &what) const
Definition: ERF_LatLonMap.cpp:233
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > m_dx
Definition: ERF_LatLonMap.H:176
amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > m_problo
Definition: ERF_LatLonMap.H:175
LatLonMap(const amrex::MultiFab &lat, const amrex::MultiFab &lon, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, const amrex::Geometry &geom0)
Definition: ERF_LatLonMap.cpp:177
amrex::MultiFab m_ll
Definition: ERF_LatLonMap.H:173
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
real(c_double), private di
Definition: ERF_module_mp_morr_two_moment.F90:203
Definition: ERF_LatLonMap.H:87
amrex::Real cos_alpha
Definition: ERF_LatLonMap.H:102
amrex::Real lat
Definition: ERF_LatLonMap.H:94
amrex::Real x
Definition: ERF_LatLonMap.H:89
amrex::Real near_lat
Definition: ERF_LatLonMap.H:98
amrex::Real y
Definition: ERF_LatLonMap.H:90
amrex::Real sin_alpha
Definition: ERF_LatLonMap.H:103
amrex::Real near_lon
Definition: ERF_LatLonMap.H:99
amrex::Real lon
Definition: ERF_LatLonMap.H:95