Debian Codegraph @

src/atlas/meshgenerator/detail/cubedsphere/CubedSphereUtility.cc

raw
1 /*
2 * (C) Crown Copyright 2021 Met Office
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 */
7
8 #include "atlas/meshgenerator/detail/cubedsphere/CubedSphereUtility.h"
9 #include "atlas/grid/CubedSphereGrid.h"
10 #include "atlas/grid/Iterator.h"
11 #include "atlas/projection/Jacobian.h"
12 #include "atlas/projection/detail/CubedSphereProjectionBase.h"
13
14
15 namespace atlas {
16 namespace meshgenerator {
17 namespace detail {
18 namespace cubedsphere {
19
20 // -----------------------------------------------------------------------------
21 // NeighbourJacobian class
22 // -----------------------------------------------------------------------------
23
24 NeighbourJacobian::NeighbourJacobian(const CubedSphereGrid& csGrid) {
25 // N must be greater than 2.
26 if (csGrid.N() < 2) {
27 throw_Exception("Jacobians can only be calculated for N > 1 .", Here());
28 }
29
30 // Assumes cell centre staggering.
31 if (csGrid.stagger() != "C") {
32 throw_Exception("NeighbourJacobian class only works for cell-centre grid", Here());
33 }
34
35 // Get projection.
36 csProjection_ = &csGrid.cubedSphereProjection();
37
38 // Get tiles.
39 const auto& csTiles = csProjection_->getCubedSphereTiles();
40
41 // Get grid size.
42 N_ = csGrid.N();
43
44
45 // Get cell width.
46 const double cellWidth = 90. / N_;
47
48
49 for (size_t t = 0; t < 6; ++t) {
50 // Calculate tile Jacobians.
51 dxy_by_dij_[t] = csTiles.tileJacobian(t) * cellWidth;
52
53 // Get inverse.
54 dij_by_dxy_[t] = dxy_by_dij_[t].inverse();
55
56 // Set xy00. Grid point needs moving to (i = 0, j = 0).
57 xy00_[t] = csTiles.tileCentre(t) + dxy_by_dij_[t] * PointIJ(-0.5 * N_, -0.5 * N_);
58
59 // Get xy min/max.
60 xyMin_[t].x() = csTiles.tileCentre(t).x() + 45.;
61 xyMax_[t].x() = csTiles.tileCentre(t).x() - 45.;
62 xyMin_[t].y() = csTiles.tileCentre(t).y() + 45.;
63 xyMax_[t].y() = csTiles.tileCentre(t).y() - 45.;
64
65 // Neighbour assignment lambda.
66 const auto neighbourAssignment = [&](TileEdge::k k) -> void {
67 // Shift points in to neighbouring tiles.
68 PointIJ Dij;
69 switch (k) {
70 case TileEdge::LEFT: {
71 Dij = PointIJ(-2, 0);
72 break;
73 }
74 case TileEdge::BOTTOM: {
75 Dij = PointIJ(0, -2);
76 break;
77 }
78 case TileEdge::RIGHT: {
79 Dij = PointIJ(N_, 0);
80 break;
81 }
82 case TileEdge::TOP: {
83 Dij = PointIJ(0, N_);
84 break;
85 }
86 case TileEdge::UNDEFINED: {
87 throw_Exception("Undefined tile edge.", Here());
88 }
89 }
90
91 // half-index displacements.
92 const auto dij00 = PointIJ(0.5, 0.5);
93 const auto dij10 = PointIJ(1.5, 0.5);
94 const auto dij01 = PointIJ(0.5, 1.5);
95
96 // Get neighbour xy points in xy space local to tile.
97 const PointXY xy00Local = xy00_[t] + dxy_by_dij_[t] * (Dij + dij00);
98 const PointXY xy10Local = xy00_[t] + dxy_by_dij_[t] * (Dij + dij10);
99 const PointXY xy01Local = xy00_[t] + dxy_by_dij_[t] * (Dij + dij01);
100
101 // Convert from local xy to global xy.
102 const PointXY xy00Global = csTiles.tileCubePeriodicity(xy00Local, static_cast<idx_t>(t));
103 const PointXY xy10Global = csTiles.tileCubePeriodicity(xy10Local, static_cast<idx_t>(t));
104 const PointXY xy01Global = csTiles.tileCubePeriodicity(xy01Local, static_cast<idx_t>(t));
105
106 // Get neighbour tile ID.
107 neighbours_[t].t_[k] = csTiles.indexFromXY(xy00Global.data());
108
109 // Set Jacobian of global xy with respect to local ij.
110 auto dxyGlobal_by_dij = Jacobian{{xy10Global[0] - xy00Global[0], xy01Global[0] - xy00Global[0]},
111 {xy10Global[1] - xy00Global[1], xy01Global[1] - xy00Global[1]}};
112
113 // Rescale by cell width (gains an extra couple of decimal places of precision).
114 const auto sign = [](double num, double tol = 360. * std::numeric_limits<double>::epsilon()) {
115 return std::abs(num) < tol ? 0 : num > 0 ? 1. : -1.;
116 };
117 dxyGlobal_by_dij = Jacobian{sign(dxyGlobal_by_dij[0][0]), sign(dxyGlobal_by_dij[0][1]),
118 sign(dxyGlobal_by_dij[1][0]), sign(dxyGlobal_by_dij[1][1])} *
119 cellWidth;
120
121 // Chain rule to get Jacobian with respect to local xy.
122 neighbours_[t].dxyGlobal_by_dxyLocal_[k] = dxyGlobal_by_dij * dij_by_dxy_[t];
123
124 // Set local xy00
125 neighbours_[t].xy00Local_[k] = xy00Local;
126
127 // Set global xy00
128 neighbours_[t].xy00Global_[k] = xy00Global;
129 };
130
131 // Assign neighbours (good job we put it all in a lambda!).
132 neighbourAssignment(TileEdge::LEFT);
133 neighbourAssignment(TileEdge::BOTTOM);
134 neighbourAssignment(TileEdge::RIGHT);
135 neighbourAssignment(TileEdge::TOP);
136 }
137 }
138
139 PointXY NeighbourJacobian::xy(const PointIJ& ij, idx_t t) const {
140 // Get jacobian.
141 const Jacobian& jac = dxy_by_dij_[static_cast<size_t>(t)];
142 const PointXY& xy00 = xy00_[static_cast<size_t>(t)];
143
144 // Return ij
145 return xy00 + jac * ij;
146 }
147
148 PointIJ NeighbourJacobian::ij(const PointXY& xy, idx_t t) const {
149 // Get jacobian.
150 const Jacobian& jac = dij_by_dxy_[static_cast<size_t>(t)];
151 const PointXY& xy00 = xy00_[static_cast<size_t>(t)];
152
153 // Return ij
154 return jac * (xy - xy00);
155 }
156
157 PointTXY NeighbourJacobian::xyLocalToGlobal(const PointXY& xyLocal, idx_t tLocal) const {
158 // The tileCubePeriodicity method fails when extrapolating along an unowned
159 // tile edge. This method explicitly places an xy point on to a neighbouring
160 // tile to avoid this. Once the correct xy position has been found,
161 // tileCubePeriodicity will correcty find the "owned" xy position of a point
162 // on an unowned tile edge.
163
164 // Declare result.
165 PointXY xyGlobal;
166 idx_t tGlobal;
167
168 // Get ij.
169 const PointIJ ijLocal = ij(xyLocal, tLocal);
170
171 // Get tiles.
172 const auto& csTiles = csProjection_->getCubedSphereTiles();
173
174 if (ijInterior(ijLocal)) {
175 // We're within the tile boundary (possibly on an edge).
176
177 // Return local values if not on edge.
178 if (!ijEdge(ijLocal)) {
179 return PointTXY(tLocal, xyLocal);
180 }
181
182 // We're on an edge. Will need to check with Tiles class.
183 xyGlobal = xyLocal;
184 tGlobal = tLocal;
185 }
186 else {
187 // We're outside the tile boundary.
188 // Figure out which tile xy is on.
189 TileEdge::k k;
190 if (ijLocal.iNode() < 0) {
191 k = TileEdge::LEFT;
192 }
193 else if (ijLocal.jNode() < 0) {
194 k = TileEdge::BOTTOM;
195 }
196 else if (ijLocal.iNode() > N_) {
197 k = TileEdge::RIGHT;
198 }
199 else if (ijLocal.jNode() > N_) {
200 k = TileEdge::TOP;
201 }
202 else {
203 throw_Exception("Cannot determine neighbour tile.", Here());
204 }
205
206 // Get reference points and jacobian.
207 const PointXY& xy00Local_ = neighbours_[static_cast<size_t>(tLocal)].xy00Local_[k];
208 const PointXY& xy00Global_ = neighbours_[static_cast<size_t>(tLocal)].xy00Global_[k];
209 const Jacobian& jac = neighbours_[static_cast<size_t>(tLocal)].dxyGlobal_by_dxyLocal_[k];
210
211 // Get t.
212 tGlobal = neighbours_[static_cast<size_t>(tLocal)].t_[k];
213
214 // Calculate global xy.
215 xyGlobal = xy00Global_ + jac * (xyLocal - xy00Local_);
216 }
217
218 // Need to be very careful with floating point comparisons used in projection
219 // class. Move point on to edge if it is very close.
220 xyGlobal = snapToEdge(xyGlobal, tGlobal);
221
222 // Correct for edge-ownership rules.
223 xyGlobal = csTiles.tileCubePeriodicity(xyGlobal, tGlobal);
224 tGlobal = csTiles.indexFromXY(xyGlobal.data());
225
226 return PointTXY(tGlobal, xyGlobal);
227 }
228
229 PointTIJ NeighbourJacobian::ijLocalToGlobal(const PointIJ& ijLocal, idx_t tLocal) const {
230 // Use xyLocalToGlobal method to take care of this.
231
232 // Get global xyt.
233 PointTXY txyGlobal = xyLocalToGlobal(xy(ijLocal, tLocal), tLocal);
234
235 // convert to ijt
236 return PointTIJ(txyGlobal.t(), ij(txyGlobal.xy(), txyGlobal.t()));
237 }
238
239 bool NeighbourJacobian::ijInterior(const PointIJ& ij) const {
240 return ij.iNode() >= 0 && ij.iNode() <= N_ && ij.jNode() >= 0 && ij.jNode() <= N_;
241 }
242
243 bool NeighbourJacobian::ijEdge(const PointIJ& ij) const {
244 return ijInterior(ij) && (ij.iNode() == 0 || ij.iNode() == N_ || ij.jNode() == 0 || ij.jNode() == N_);
245 }
246
247 bool NeighbourJacobian::ijCross(const PointIJ& ij) const {
248 const bool inCorner = (ij.iNode() < 0 && ij.jNode() < 0) || // bottom-left corner.
249 (ij.iNode() > N_ && ij.jNode() < 0) || // bottom-right corner.
250 (ij.iNode() > N_ && ij.jNode() > N_) || // top-right corner.
251 (ij.iNode() < 0 && ij.jNode() > N_); // top-left corner.
252 return !inCorner;
253 }
254
255 PointXY NeighbourJacobian::snapToEdge(const PointXY& xy, idx_t t) const {
256 const auto nudgeValue = [](double a, double b) -> double {
257 // Set tolerance to machine epsilon * 360 degrees.
258 constexpr double tol = 360. * std::numeric_limits<double>::epsilon();
259
260 // If a is nearly equal to b, return b. Otherwise return a.
261 return std::abs(a - b) <= tol ? b : a;
262 };
263
264 // If point is near edge, place it exactly on edge.
265 const PointXY& xyMin = xyMin_[static_cast<size_t>(t)];
266 const PointXY& xyMax = xyMax_[static_cast<size_t>(t)];
267 return PointXY(nudgeValue(nudgeValue(xy.x(), xyMin.x()), xyMax.x()),
268 nudgeValue(nudgeValue(xy.y(), xyMin.y()), xyMax.y()));
269 }
270
271 } // namespace cubedsphere
272 } // namespace detail
273 } // namespace meshgenerator
274 } // namespace atlas