Debian Codegraph @

src/atlas/util/Unique.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <cmath>
14 #include "atlas/array/ArrayView.h"
15 #include "atlas/array/IndexView.h"
16 #include "atlas/array/LocalView.h"
17 #include "atlas/array/MakeView.h"
18 #include "atlas/field/Field.h"
19 #include "atlas/library/config.h"
20 #include "atlas/mesh/Connectivity.h"
21 #include "atlas/mesh/Mesh.h"
22 #include "atlas/mesh/Nodes.h"
23 #include "atlas/util/CoordinateEnums.h"
24 #include "atlas/util/LonLatMicroDeg.h"
25 #include "atlas/util/MicroDeg.h"
26 #include "atlas/util/PeriodicTransform.h"
27
28 namespace atlas {
29 namespace util {
30
31 class PeriodicTransform;
32
33 // ----------------------------------------------------------------------------
34
35 /// @brief Compute unique positive index from lon-lat coordinates in
36 /// microdegrees
37 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
38 uidx_t unique_lonlat_microdeg(const int lon, const int lat);
39
40 /// @brief Compute unique positive index from lon-lat coordinates in
41 /// microdegrees
42 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
43 uidx_t unique_lonlat_microdeg(const int lonlat[]);
44
45 /// @brief Compute unique positive index from lon-lat coordinates in
46 /// microdegrees
47 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
48 uidx_t unique_lonlat(const LonLatMicroDeg&);
49
50 /// @brief Compute unique positive index from lon-lat coordinates in degrees
51 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
52 uidx_t unique_lonlat(const double& lon, const double& lat);
53 uidx_t unique_lonlat(const double lonlat[]);
54 uidx_t unique_lonlat(const array::LocalView<double, 1>& lonlat);
55 uidx_t unique_lonlat(const std::array<double, 2>& lonlat);
56
57 /// @brief Compute unique positive index from lon-lat coordinates in degrees
58 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
59 uidx_t unique_lonlat(const double& lon, const double& lat, const PeriodicTransform&);
60
61 /// @brief Compute unique positive index from lon-lat coordinates in degrees.
62 /// coordinates are stored in order:
63 /// [ x1, y1, x2, y2, ... , xn, yn ]
64 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
65 uidx_t unique_lonlat(const double elem_lonlat[], size_t npts);
66
67 /// @brief Compute unique positive index for a element
68 /// This class is a functor initialised with the nodes
69 class UniqueLonLat {
70 public:
71 // UniqueLonLat() {}
72
73 /// @brief Constructor, needs nodes functionspace to cache the lonlat field
74 UniqueLonLat(const mesh::Nodes&);
75
76 /// @brief Constructor
77 UniqueLonLat(const Mesh&);
78
79 /// @brief Compute unique positive index of a node defined by node index.
80 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
81 uidx_t operator()(int node) const;
82
83 uidx_t operator()(int node, const PeriodicTransform& transform) const;
84
85 /// @brief Compute unique positive index of element defined by node indices.
86 /// The assumption is that the elements exist in a lon-lat domain and don't
87 // degenerate to a line.
88 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
89 uidx_t operator()(const mesh::Connectivity::Row& elem_nodes) const;
90
91 /// @brief Compute unique positive index of element defined by node indices.
92 /// The assumption is that the elements exist in a lon-lat domain and don't
93 // degenerate to a line.
94 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
95 uidx_t operator()(const mesh::Connectivity::Row& elem_nodes, const PeriodicTransform& transform) const;
96
97 /// @brief Compute unique positive index of element defined by node indices.
98 /// The assumption is that the elements exist in a lon-lat domain and don't
99 // degenerate to a line.
100 /// @return uidx_t Return type depends on ATLAS_BITS_GLOBAL [32/64] bits
101 uidx_t operator()(const int elem_nodes[], size_t npts) const;
102
103 /// @brief update the internally cached lonlat view if the field has changed
104 void update();
105
106 private:
107 const mesh::Nodes* nodes;
108 array::ArrayView<const double, 2> lonlat;
109 };
110
111 // ----------------------------------------------------------------------------
112
113 /* Inline implementation for maximum performance */
114
115 namespace detail {
116
117 /// lon and lat arguments in microdegrees
118 template <typename T>
119 inline T uniqueT(const int lon, const int lat) {
120 return uniqueT<T>(lon, lat);
121 }
122 template <>
123 inline int uniqueT<int>(const int lon, const int lat);
124 template <>
125 inline long uniqueT<long>(const int lon, const int lat);
126
127 /// @brief unique32 computes 32bit positive unique id
128 /// max precision is 0.02 degrees
129 /// Numbering follows ECMWF standard grib ordering (from N-S and W-E)
130 inline int unique32(const int lon_microdeg, const int lat_microdeg) {
131 // Truncate microdegrees to order of degrees (16 bits), and add bits together
132 // to 32 bit int.
133 int iy = static_cast<int>((180000000 - lat_microdeg) * 5e-5); // (2*microdeg(90.)-lat)*5e-5
134 int ix = static_cast<int>((lon_microdeg + 720000000) * 5e-5); // (lon+2*microdeg(360.))*5e-5
135 iy <<= 17;
136 int id = iy | ix;
137 return id;
138 }
139 template <>
140 inline int uniqueT<int>(const int lon, const int lat) {
141 return unique32(lon, lat);
142 }
143
144 /// @brief unique64 computes 64bit positive unique id
145 /// max precision is 1 microdegree
146 /// Numbering follows ECMWF standard grib ordering (from N-S and W-E)
147 inline long unique64(const int lon_microdeg, const int lat_microdeg) {
148 // Truncate microdegrees to (32 bits), and add bits together to 64 bit long.
149 long iy = 360000000l - long(lat_microdeg); // (4*microdeg(90.)-lat)
150 long ix = long(lon_microdeg) + 1440000000l; // (lon+4*microdeg(360.))
151
152 iy <<= 31;
153 long id = iy | ix;
154 return id;
155 }
156 template <>
157 inline long uniqueT<long>(const int lon, const int lat) {
158 return unique64(lon, lat);
159 }
160 } // namespace detail
161
162 inline uidx_t unique_lonlat_microdeg(const int lon, const int lat) {
163 return detail::uniqueT<uidx_t>(lon, lat);
164 }
165
166 inline uidx_t unique_lonlat_microdeg(const int lonlat[]) {
167 return detail::uniqueT<uidx_t>(lonlat[LON], lonlat[LAT]);
168 }
169
170 inline uidx_t unique_lonlat(const LonLatMicroDeg& p) {
171 return unique_lonlat_microdeg(p.data());
172 }
173
174 inline uidx_t unique_lonlat(const double& lon, const double& lat) {
175 return detail::uniqueT<uidx_t>(microdeg(lon), microdeg(lat));
176 }
177
178 inline uidx_t unique_lonlat(const double lonlat[]) {
179 return detail::uniqueT<uidx_t>(microdeg(lonlat[LON]), microdeg(lonlat[LAT]));
180 }
181
182 inline uidx_t unique_lonlat(const std::array<double, 2>& lonlat) {
183 return detail::uniqueT<uidx_t>(microdeg(lonlat[LON]), microdeg(lonlat[LAT]));
184 }
185
186 inline uidx_t unique_lonlat(const array::LocalView<double, 1>& lonlat) {
187 return unique_lonlat(lonlat.data());
188 }
189
190 inline uidx_t unique_lonlat(const double elem_lonlat[], size_t npts) {
191 std::array<double, 2> centroid{0., 0.};
192 for (size_t jnode = 0; jnode < npts; ++jnode) {
193 centroid[LON] += elem_lonlat[jnode * 2 + LON];
194 centroid[LAT] += elem_lonlat[jnode * 2 + LAT];
195 }
196 centroid[LON] /= static_cast<double>(npts);
197 centroid[LAT] /= static_cast<double>(npts);
198
199 return unique_lonlat(centroid);
200 }
201
202 inline UniqueLonLat::UniqueLonLat(const Mesh& mesh):
203 nodes(&mesh.nodes()), lonlat(array::make_view<double, 2>(nodes->lonlat())) {
204 update();
205 }
206
207 inline UniqueLonLat::UniqueLonLat(const mesh::Nodes& _nodes):
208 nodes(&_nodes), lonlat(array::make_view<double, 2>(nodes->lonlat())) {
209 update();
210 }
211
212 inline uidx_t UniqueLonLat::operator()(int node) const {
213 return unique_lonlat(lonlat(node, LON), lonlat(node, LAT));
214 }
215
216 inline uidx_t UniqueLonLat::operator()(const mesh::Connectivity::Row& elem_nodes) const {
217 std::array<double, 2> centroid{0., 0.};
218 size_t npts = elem_nodes.size();
219 for (size_t jnode = 0; jnode < npts; ++jnode) {
220 centroid[LON] += lonlat(elem_nodes(jnode), LON);
221 centroid[LAT] += lonlat(elem_nodes(jnode), LAT);
222 }
223 centroid[LON] /= static_cast<double>(npts);
224 centroid[LAT] /= static_cast<double>(npts);
225
226 return unique_lonlat(centroid);
227 }
228
229 inline uidx_t UniqueLonLat::operator()(const int elem_nodes[], size_t npts) const {
230 std::array<double, 2> centroid{0., 0.};
231
232 for (size_t jnode = 0; jnode < npts; ++jnode) {
233 centroid[LON] += lonlat(elem_nodes[jnode], LON);
234 centroid[LAT] += lonlat(elem_nodes[jnode], LAT);
235 }
236 centroid[LON] /= static_cast<double>(npts);
237 centroid[LAT] /= static_cast<double>(npts);
238
239 return unique_lonlat(centroid);
240 }
241
242 inline void UniqueLonLat::update() {
243 lonlat = array::make_view<double, 2>(nodes->lonlat());
244 }
245
246 // ----------------------------------------------------------------------------
247
248 } // namespace util
249 } // namespace atlas