Debian Codegraph @

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

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 #pragma once
9
10 #include "atlas/library/config.h"
11 #include "atlas/projection/detail/ProjectionImpl.h"
12 #include "atlas/runtime/Exception.h"
13 #include "atlas/util/Point.h"
14
15 namespace atlas {
16 class CubedSphereGrid;
17 }
18
19 namespace atlas {}
20
21 namespace atlas {
22 namespace projection {
23 namespace detail {
24 class CubedSphereProjectionBase;
25 } // namespace detail
26 } // namespace projection
27 } // namespace atlas
28
29 namespace atlas {
30 namespace meshgenerator {
31 namespace detail {
32 namespace cubedsphere {
33
34 /// Enum for (i, j, t) coordinate fields.
35 struct Coordinates {
36 enum k : idx_t
37 {
38 T,
39 I,
40 J
41 };
42 };
43
44 /// Enum for tile edges.
45 struct TileEdge {
46 enum k : size_t
47 {
48 LEFT,
49 BOTTOM,
50 RIGHT,
51 TOP,
52 UNDEFINED
53 };
54 };
55
56 /// Class to store (i, j) indices as a Point2 coordinate.
57 class PointIJ : public Point2 {
58 public:
59 using Point2::Point2;
60 PointIJ(): Point2() {}
61
62 /// Index constructor.
63 template <typename IndexI, typename IndexJ>
64 inline PointIJ(IndexI i, IndexJ j): Point2(static_cast<double>(i), static_cast<double>(j)) {}
65
66 /// @{
67 /// Return i or j by value.
68 inline double i() const { return x_[0]; }
69 inline double j() const { return x_[1]; }
70 /// @}
71
72 /// @{
73 /// Return i or j by reference
74 inline double& i() { return x_[0]; }
75 inline double& j() { return x_[1]; }
76 /// @}
77
78 /// @{
79 /// Round i or j to node index.
80 inline idx_t iNode() const { return static_cast<idx_t>(std::round(i())); }
81 inline idx_t jNode() const { return static_cast<idx_t>(std::round(j())); }
82 /// @}
83
84 /// @{
85 /// Round i or j to cell index.
86 inline idx_t iCell() const { return static_cast<idx_t>(std::floor(i())); }
87 inline idx_t jCell() const { return static_cast<idx_t>(std::floor(j())); }
88 /// @}
89 };
90
91 /// (t, PointXY) tuple.
92 class PointTXY : public std::pair<idx_t, PointXY> {
93 using std::pair<idx_t, PointXY>::pair;
94
95 public:
96 idx_t& t() { return first; }
97 PointXY& xy() { return second; }
98 const idx_t& t() const { return first; }
99 const PointXY& xy() const { return second; }
100 };
101
102 /// (t, PointIJ) tuple.
103 class PointTIJ : public std::pair<idx_t, PointIJ> {
104 using std::pair<idx_t, PointIJ>::pair;
105
106 public:
107 idx_t& t() { return first; }
108 PointIJ& ij() { return second; }
109 const idx_t& t() const { return first; }
110 const PointIJ& ij() const { return second; }
111 };
112
113 /// \brief Class to convert between ij and xy on a tile and its four
114 /// surrounding neighbours.
115 ///
116 /// Helper class to deal with the coordinate system roations and
117 /// displacements between a tile and its neighbours. This class
118 /// is specifcially written to comupute the (x, y) and (t, i, j)
119 /// coordinates of halos that extend across tile boundaries.
120 class NeighbourJacobian {
121 private:
122 using Jacobian = projection::Jacobian;
123
124 public:
125 /// Default constructor.
126 NeighbourJacobian() = default;
127
128 /// Grid-data constructor.
129 NeighbourJacobian(const CubedSphereGrid& csGrid);
130
131 /// Convert ij on local tile t to xy.
132 PointXY xy(const PointIJ& ij, idx_t t) const;
133
134 /// Convert xy on local tile t to ij.
135 PointIJ ij(const PointXY& xy, idx_t t) const;
136
137 /// Convert extrapolated xy on tile t to global xy and t (needed for halos).
138 PointTXY xyLocalToGlobal(const PointXY& xyLocal, idx_t tLocal) const;
139
140 /// Convert extrapolated ij on tile t to global ij and t (needed for halos).
141 PointTIJ ijLocalToGlobal(const PointIJ& ijLocal, idx_t tLocal) const;
142
143 /// Return true if ij is interior or on the edge of a tile.
144 bool ijInterior(const PointIJ& ij) const;
145
146 /// Return true if ij is on the edge of a tile.
147 bool ijEdge(const PointIJ& ij) const;
148
149 /// Return true if ij is in the valid "+" halo extension of at tile.
150 bool ijCross(const PointIJ& ij) const;
151
152 /// Makes sure points near tile edges are *exactly* on the edge.
153 PointXY snapToEdge(const PointXY& xy, idx_t t) const;
154
155 private:
156 // Pointer to grid projection.
157 const projection::detail::CubedSphereProjectionBase* csProjection_{};
158
159 // Grid size.
160 idx_t N_{};
161
162 // Jacobian of xy with respect to ij for each tile.
163 std::array<Jacobian, 6> dxy_by_dij_{};
164
165 // Jacobian of ij with respect to xy for each tile.
166 std::array<Jacobian, 6> dij_by_dxy_{};
167
168 // Lower-left xy position on each tile.
169 std::array<PointXY, 6> xy00_{};
170
171 // Min xy on each tile.
172 std::array<PointXY, 6> xyMin_{};
173
174 // Max xy on each tile.
175 std::array<PointXY, 6> xyMax_{};
176
177 // Properties of four neighbours of a tile.
178 struct Neighbours {
179 // Tile ID.
180 std::array<idx_t, 4> t_{};
181
182 // Jacobian of remote xy with respect to local xy.
183 std::array<Jacobian, 4> dxyGlobal_by_dxyLocal_{};
184
185 // Lower left most local xy position on neighbour tiles.
186 std::array<PointXY, 4> xy00Local_;
187 std::array<PointXY, 4> xy00Global_;
188 };
189
190 // Set of neighbours for each tile.
191 std::array<Neighbours, 6> neighbours_{};
192 };
193
194 } // namespace cubedsphere
195 } // namespace detail
196 } // namespace meshgenerator
197 } // namespace atlas