Debian Codegraph @

src/atlas/interpolation/method/unstructured/ConservativeSphericalPolygonInterpolation.h

raw
1 /*
2 * (C) Copyright 2021- 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
12 #pragma once
13
14 #include "atlas/functionspace.h"
15 #include "atlas/interpolation/method/Method.h"
16 #include "atlas/util/ConvexSphericalPolygon.h"
17
18 namespace atlas {
19 namespace interpolation {
20 namespace method {
21
22 using Indices = std::vector<idx_t>;
23
24
25 class ConservativeSphericalPolygonInterpolation : public Method {
26 public:
27 struct InterpolationParameters { // one polygon intersection
28 Indices csp_ids; // target cells used for intersection
29 std::vector<PointXYZ> centroids; // intersection cell centroids
30 std::vector<double> weights; // intersection cell areas
31 };
32
33 private:
34 class Data : public InterpolationCacheEntry {
35 public:
36 ~Data() override = default;
37 size_t footprint() const override;
38 static std::string static_type() { return "ConservativeSphericalPolygonInterpolation"; }
39 std::string type() const override { return static_type(); }
40 void print(std::ostream& out) const;
41
42 private:
43 friend class ConservativeSphericalPolygonInterpolation;
44
45 struct PolygonsData {
46 enum class Context { SOURCE, TARGET } context;
47
48 // position and effective area of data points
49 std::vector<PointXYZ> points;
50 std::vector<double> areas;
51
52 // indexing of subpolygons
53 Indices csp2node;
54 std::vector<Indices> node2csp;
55 gidx_t csp_size;
56 Indices csp_cell_index;
57 Indices csp_index;
58 bool cell_data;
59 PolygonsData(PolygonsData::Context ctx) : context(ctx) {}
60 } src_{PolygonsData::Context::SOURCE}, tgt_{PolygonsData::Context::TARGET};
61
62 // Timings
63 struct Timings {
64 double source_polygons_assembly{0};
65 double target_polygons_assembly{0};
66 double src_already_in{0};
67 double source_kdtree_assembly{0};
68 double source_kdtree_search{0};
69 double source_polygons_filter{0};
70 double polygon_intersections{0};
71 double matrix_assembly{0};
72 double interpolation{0};
73 } timings;
74
75 std::vector<InterpolationParameters> tgt_iparam_;
76
77 // Reconstructible if need be
78 FunctionSpace src_fs_;
79 FunctionSpace tgt_fs_;
80 };
81
82
83 struct TargetTriplets {
84 bool normalise_;
85 int t;
86 std::map<idx_t, double> tpoint_subweights;
87 void add (idx_t s, double w) {
88 auto pair = tpoint_subweights.emplace(s, w);
89 bool inserted = pair.second;
90 if (! inserted) {
91 auto it = pair.first;
92 it->second += w;
93 }
94 }
95 void emplace_in(Triplets& triplets) {
96 if (tpoint_subweights.size()) {
97 double wfactor = 1.;
98 if (normalise_) {
99 volatile double sum_of_weights{0.};
100 for (auto& p : tpoint_subweights) {
101 sum_of_weights += p.second;
102 }
103 ATLAS_ASSERT(sum_of_weights > 0.);
104 wfactor = 1./sum_of_weights;
105 }
106 for (auto& p : tpoint_subweights) {
107 triplets.emplace_back(t, p.first, p.second * wfactor);
108 }
109 }
110 }
111 void reset(int _t) {
112 tpoint_subweights.clear();
113 t = _t;
114 }
115 size_t size() const { return tpoint_subweights.size(); }
116 TargetTriplets(bool normalise) : normalise_(normalise) {}
117 };
118
119 public:
120 class Cache final : public interpolation::Cache {
121 public:
122 Cache() = default;
123 Cache(const interpolation::Cache& c);
124 Cache(const Interpolation&);
125
126 operator bool() const { return entry_; }
127 const Data* get() const { return entry_; }
128
129 private:
130 friend class ConservativeSphericalPolygonInterpolation;
131 Cache(std::shared_ptr<InterpolationCacheEntry> entry);
132 const Data* entry_{nullptr};
133 };
134
135 struct Statistics {
136 enum Counts {
137 NUM_SRC_PLG = 0, // index, number of source polygons
138 NUM_TGT_PLG, // index, number of target polygons
139 NUM_INT_PLG, // index, number of intersection polygons
140 NUM_UNCVR_FULL_TGT, // index, number of completely non covered target polygons
141 NUM_UNCVR_PART_TGT, // index, number of partially non covered target polygons
142 NUM_ENUM_SIZE
143 };
144 enum Errors {
145 ERR_TGT_INTERSECTPLG_L1 = 0, // see above
146 ERR_TGT_INTERSECTPLG_LINF, // see above
147 ERR_SRCTGT_INTERSECTPLG_DIFF, // index, 1/(unit_sphere.area) ( \sum_{scell} scell.area - \sum{tcell} tcell.area )
148 ERR_REMAP_CONS, // index, error in mass conservation
149 ERR_REMAP_L2, // index, error accuracy for given analytical function
150 ERR_REMAP_LINF, // index, like REMAP_L2 but in L_infinity norm
151 ERR_ENUM_SIZE
152 };
153 enum Timings {
154 TIME_SRC_PLG = 0, // index, max time in second per task to build source polygons
155 TIME_TGT_PLG, // index, max time in second per task to build target polygons
156 TIME_KDTREE_BUILD, // index, max time in second per task to build kdtree of source polygons
157 TIME_KDTREE_SEARCH, // index, max time in second per task to compute kdtree searches for all target polygons
158 TIME_MATRIX, // index, max time in second per task to assemble the interpolation matrix from the weights
159 TIME_INTERS, // index, max time in second per task to compute intersection polygons
160 TIME_INTERP, // index, max time in second per task to interpolate a source to a target field
161 TIME_ENUM_SIZE
162 };
163 enum Memory {
164 MEM_MATRIX = 0, // index, max memory size per task of the interpolation matrix
165 MEM_SRC, // index, max memory size per task of source point values
166 MEM_TGT,
167 MEM_SRC_AREAS, // index, max memory size per task of source-area array
168 MEM_TGT_AREAS,
169 MEM_SRC_CSP2N, // index, max memory size per task of source polygon-to-node index-array
170 MEM_SRC_N2CSP, // index, max memory size per task of source node to polygon index-array
171 MEM_SRC_CSP2CI, // index, max memory size per task of source polygon-to-cell-index lookup-array
172 MEM_SRC_CSP2C, // index, max memory size per task of source polygon-to-cell lookup-array
173 MEM_SRC_PLG, // index, max memory size per task of source polygon array
174 MEM_TGT_CSP2N,
175 MEM_TGT_N2CSP,
176 MEM_TGT_CSP2CI,
177 MEM_TGT_CSP2C,
178 MEM_IPARAM, // index, max memory size per task of stored intersection parameters
179 MEM_ENUM_SIZE
180 };
181 std::array<int, NUM_ENUM_SIZE> counts;
182 std::array<double, ERR_ENUM_SIZE> errors;
183 std::array<size_t, MEM_ENUM_SIZE> memory;
184 std::array<double, TIME_ENUM_SIZE> time;
185
186 double tgt_area_sum;
187 double src_area_sum;
188 bool all;
189 bool accuracy;
190 bool conservation;
191 bool intersection;
192 bool timings;
193 Metadata metadata;
194
195 Statistics();
196 Statistics(const Metadata&);
197
198 void compute_accuracy(const Interpolation& interpolation, const Field target, std::function<double(const PointLonLat&)> func, Metadata* metadata = nullptr);
199 void fillMetadata(Metadata&);
200 };
201
202
203 public:
204 ConservativeSphericalPolygonInterpolation(const Config& = util::NoConfig());
205
206 using Method::do_setup;
207 void do_setup(const FunctionSpace& src_fs, const FunctionSpace& tgt_fs) override;
208 void do_setup(const FunctionSpace& source, const FunctionSpace& target, const interpolation::Cache&) override;
209 void do_setup(const Grid& src_grid, const Grid& tgt_grid, const interpolation::Cache&) override;
210
211 void do_execute(const Field& src_field, Field& tgt_field, Metadata&) const override;
212 void do_execute(const FieldSet& src_fields, FieldSet& tgt_fields, Metadata&) const override;
213
214 void print(std::ostream& out) const override;
215
216 const FunctionSpace& source() const override { return src_fs_; }
217 const FunctionSpace& target() const override { return tgt_fs_; }
218 Statistics& statistics() { return remap_stat_; }
219
220 inline const PointXYZ& src_points(size_t id) const { return data_->src_.points[id]; }
221 inline const PointXYZ& tgt_points(size_t id) const { return data_->tgt_.points[id]; }
222
223 interpolation::Cache createCache() const override;
224
225 private:
226 using Polygon = util::ConvexSphericalPolygon;
227 using PolygonArray = std::vector<util::ConvexSphericalPolygon>;
228
229 void do_setup_impl(const Grid& src_grid, const Grid& tgt_grid);
230
231 void build_source_kdtree(util::KDTree<idx_t>&, double& max_srccell_rad, const PolygonArray&) const;
232 void build_source_kdtree_centroid(util::KDTree<idx_t>&, double& max_srccell_rad, const PolygonArray&) const;
233 void intersect_polygons(const PolygonArray& src_csp);
234 Triplets compute_1st_order_triplets();
235 Triplets compute_2nd_order_triplets();
236 void dump_intersection(const std::string, const Polygon& plg_1, const PolygonArray& plg_2_array,
237 const Indices& plg_2_idx_array) const;
238
239 struct Workspace_get_cell_neighbours;
240 std::vector<idx_t> get_cell_neighbours(Mesh&, idx_t jcell, Workspace_get_cell_neighbours&) const;
241
242 struct Workspace_get_node_neighbours;
243 std::vector<idx_t> get_node_neighbours(Mesh&, idx_t jcell, Workspace_get_node_neighbours&) const;
244
245 void init_polygons_data(FunctionSpace fs, Data::PolygonsData& md);
246
247
248 Polygon get_csp_celldata(idx_t csp_id, const Mesh& mesh, const Data::PolygonsData& md);
249 Polygon get_csp_nodedata(idx_t csp_id, const Mesh& mesh, Data::PolygonsData& md);
250
251 idx_t csp_to_cell(idx_t csp_id, const Data::PolygonsData& md) const {
252 if (md.cell_data) {
253 return md.csp_index[csp_id];
254 }
255 else {
256 auto iterator_upper_bound = std::upper_bound(md.csp_index.begin(), md.csp_index.end(), csp_id);
257 idx_t idx = iterator_upper_bound-1 - md.csp_index.begin();
258 return md.csp_cell_index[idx];
259 }
260 }
261
262 std::pair<idx_t, idx_t> csp_to_cell_and_subcell(idx_t csp_id, const Data::PolygonsData& md) const {
263 auto iterator_upper_bound = std::upper_bound(md.csp_index.begin(), md.csp_index.end(), csp_id);
264 idx_t idx = iterator_upper_bound-1 - md.csp_index.begin();
265 idx_t cell = md.csp_cell_index[idx];
266 idx_t subcell = csp_id - md.csp_index[idx];
267 return std::make_pair(cell, subcell);
268 }
269
270 // Polygon get_src_csp(idx_t csp_id) {
271 // if (sharable_data_->src_.cell_data) {
272 // return get_csp_celldata(csp_id, src_mesh_, sharable_data_->src_);
273 // }
274 // else {
275 // return get_csp_nodedata(csp_id, src_mesh_, sharable_data_->src_);
276 // }
277 // }
278 Polygon get_tgt_csp(idx_t csp_id) {
279 if (sharable_data_->tgt_.cell_data) {
280 return get_csp_celldata(csp_id, tgt_mesh_, sharable_data_->tgt_);
281 }
282 else {
283 return get_csp_nodedata(csp_id, tgt_mesh_, sharable_data_->tgt_);
284 }
285 }
286 PolygonArray get_polygons_celldata(FunctionSpace, Data::PolygonsData&);
287 PolygonArray get_polygons_nodedata(FunctionSpace, Data::PolygonsData&);
288 PolygonArray get_polygons(FunctionSpace fs, Data::PolygonsData& md) {
289 return md.cell_data ? get_polygons_celldata(fs, md) : get_polygons_nodedata(fs, md);
290 }
291
292
293 int next_index(int current_index, int size) const;
294 int prev_index(int current_index, int size) const;
295
296 void setup_stat() const;
297
298 private:
299 bool validate_;
300 bool src_cell_data_;
301 bool tgt_cell_data_;
302 FunctionSpace src_fs_;
303 FunctionSpace tgt_fs_;
304 mutable Mesh src_mesh_;
305 mutable Mesh tgt_mesh_;
306 bool normalise_;
307 int order_;
308 bool matrix_free_;
309
310 mutable Statistics remap_stat_;
311
312 Cache cache_; // Storage of cache if any was passed to constructor
313 std::shared_ptr<Data> sharable_data_; // Storage of new data_, only allocated if cache is empty
314 const Data* data_; // Read-only access to data, pointing either to cache_ or sharable_data_
315
316 // position and effective area of data points
317 idx_t n_spoints_;
318 idx_t n_tpoints_;
319 };
320
321
322 } // namespace method
323 } // namespace interpolation
324 } // namespace atlas