Debian Codegraph @

src/atlas/functionspace/PointCloud.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF
3 * (C) Crown Copyright 2023 Met Office
4 *
5 * This software is licensed under the terms of the Apache Licence Version 2.0
6 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation
9 * nor does it submit to any jurisdiction.
10 */
11
12
13 #pragma once
14
15 #include <memory>
16
17 #include "atlas/array/ArrayView.h"
18 #include "atlas/field/Field.h"
19 #include "atlas/field/FieldSet.h"
20 #include "atlas/functionspace/FunctionSpace.h"
21 #include "atlas/functionspace/detail/FunctionSpaceImpl.h"
22 #include "atlas/grid/Grid.h"
23 #include "atlas/parallel/HaloExchange.h"
24 #include "atlas/parallel/GatherScatter.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/util/Config.h"
27 #include "atlas/util/Point.h"
28 #include "atlas/grid/Partitioner.h"
29
30 namespace atlas {
31 namespace parallel {
32 class HaloExchange;
33 class GatherScatter;
34 } // namespace parallel
35 } // namespace atlas
36
37 namespace atlas {
38 class Grid;
39
40 namespace functionspace {
41
42 //------------------------------------------------------------------------------------------------------
43
44 namespace detail {
45
46 class PointCloud : public functionspace::FunctionSpaceImpl {
47 public:
48 template <typename Point>
49 PointCloud(const std::vector<Point>&, const eckit::Configuration& = util::NoConfig());
50 PointCloud(const Field& lonlat, const eckit::Configuration& = util::NoConfig());
51 PointCloud(const Field& lonlat, const Field& ghost, const eckit::Configuration& = util::NoConfig());
52 PointCloud(const FieldSet&, const eckit::Configuration& = util::NoConfig()); // assuming lonlat ghost ridx and partition present
53 PointCloud(const Grid&, const eckit::Configuration& = util::NoConfig());
54 PointCloud(const Grid&, const grid::Distribution&, const eckit::Configuration& = util::NoConfig());
55 PointCloud(const Grid&, const grid::Partitioner&, const eckit::Configuration& = util::NoConfig());
56 ~PointCloud() override {}
57 std::string type() const override { return "PointCloud"; }
58 operator bool() const override { return true; }
59 size_t footprint() const override { return sizeof(*this); }
60 std::string distribution() const override;
61 const Grid& grid() const override;
62 Field lonlat() const override { return lonlat_; }
63 const Field& vertical() const { return vertical_; }
64 Field ghost() const override;
65 Field remote_index() const override { return remote_index_; }
66 Field global_index() const override { return global_index_; }
67 Field partition() const override { return partition_; }
68 idx_t size() const override { return lonlat_.shape(0); }
69 idx_t part() const override { return part_; }
70 idx_t nb_parts() const override { return nb_partitions_; }
71 std::string mpi_comm() const override { return mpi_comm_; }
72
73 using FunctionSpaceImpl::createField;
74 Field createField(const eckit::Configuration&) const override;
75 Field createField(const Field&, const eckit::Configuration&) const override;
76
77 void haloExchange(const FieldSet&, bool on_device = false) const override;
78 void haloExchange(const Field&, bool on_device = false) const override;
79
80 void adjointHaloExchange(const FieldSet&, bool on_device = false) const override;
81 void adjointHaloExchange(const Field&, bool on_device = false) const override;
82
83 const parallel::HaloExchange& halo_exchange() const;
84
85 void gather(const FieldSet&, FieldSet&) const override;
86 void gather(const Field&, Field&) const override;
87 const parallel::GatherScatter& gather() const override;
88
89 void scatter(const FieldSet&, FieldSet&) const override;
90 void scatter(const Field&, Field&) const override;
91 const parallel::GatherScatter& scatter() const override;
92
93
94 template <typename Point>
95 class IteratorT {
96 public:
97 using difference_type = std::ptrdiff_t;
98 using value_type = Point;
99 using pointer = Point*;
100 using reference = Point&;
101 using iterator_category = std::output_iterator_tag;
102
103 IteratorT(const PointCloud& fs, bool begin = true);
104
105 bool next(Point&);
106
107 const Point operator*() const;
108
109 const IteratorT& operator++() {
110 ++n_;
111 return *this;
112 }
113
114 bool operator==(const IteratorT& other) const { return n_ == other.n_; }
115 bool operator!=(const IteratorT& other) const { return n_ != other.n_; }
116
117 private:
118 const PointCloud& fs_;
119 const array::ArrayView<const double, 2> xy_;
120 const array::ArrayView<const double, 1> z_;
121 idx_t n_;
122 idx_t size_;
123 };
124
125 template <typename Point>
126 class IterateT {
127 public:
128 using value_type = Point;
129 using iterator = IteratorT<Point>;
130 using const_iterator = iterator;
131
132 public:
133 IterateT(const PointCloud& fs): fs_(fs) {}
134 iterator begin() const { return IteratorT<Point>(fs_); }
135 iterator end() const { return IteratorT<Point>(fs_, false); }
136 idx_t size() const { return fs_.size(); }
137
138 private:
139 const PointCloud& fs_;
140 };
141
142
143 class Iterate {
144 public:
145 Iterate(const PointCloud& fs): fs_(fs) {}
146 IterateT<PointXYZ> xyz() const {
147 ATLAS_ASSERT(fs_.vertical());
148 return IterateT<PointXYZ>{fs_};
149 }
150 IterateT<PointXY> xy() const { return IterateT<PointXY>{fs_}; }
151 IterateT<PointLonLat> lonlat() const { return IterateT<PointLonLat>{fs_}; }
152
153 private:
154 const PointCloud& fs_;
155 };
156
157 Iterate iterate() const { return Iterate(*this); }
158
159 private:
160 array::ArrayShape config_shape(const eckit::Configuration& config) const;
161
162 array::ArrayAlignment config_alignment(const eckit::Configuration& config) const;
163
164 array::ArraySpec config_spec(const eckit::Configuration& config) const;
165
166 array::DataType config_datatype(const eckit::Configuration& config) const;
167
168 std::string config_name(const eckit::Configuration& config) const;
169
170 void set_field_metadata(const eckit::Configuration& config, Field& field) const;
171
172 void create_remote_index() const;
173
174 idx_t size_global() const;
175
176 private:
177 mutable Grid grid_;
178 Field lonlat_;
179 Field vertical_;
180 mutable Field ghost_;
181 mutable Field remote_index_;
182 Field global_index_;
183 Field partition_;
184 idx_t size_owned_;
185 mutable idx_t size_global_{-1};
186 idx_t max_glb_idx_{0};
187
188 idx_t levels_{0};
189 idx_t part_{0};
190 idx_t nb_partitions_{1};
191 std::string mpi_comm_;
192 bool parallel_{false};
193
194 mutable std::unique_ptr<parallel::HaloExchange> halo_exchange_;
195 mutable std::unique_ptr<parallel::GatherScatter> gather_scatter_;
196
197 void setupParallel();
198 void setupHaloExchange();
199 void setupGatherScatter();
200
201 };
202
203 //------------------------------------------------------------------------------------------------------
204
205 } // namespace detail
206
207 //------------------------------------------------------------------------------------------------------
208
209 class PointCloud : public FunctionSpace {
210 public:
211 PointCloud(const FunctionSpace&);
212 PointCloud(const Field& points, const eckit::Configuration& = util::NoConfig());
213 PointCloud(const Field&, const Field&, const eckit::Configuration& = util::NoConfig());
214 PointCloud(const FieldSet& flds, const eckit::Configuration& = util::NoConfig());
215 PointCloud(const std::vector<PointXY>&, const eckit::Configuration& = util::NoConfig());
216 PointCloud(const std::vector<PointXYZ>&, const eckit::Configuration& = util::NoConfig());
217 PointCloud(const std::initializer_list<std::initializer_list<double>>&, const eckit::Configuration& = util::NoConfig());
218 PointCloud(const Grid&, const eckit::Configuration& = util::NoConfig());
219 PointCloud(const Grid&, const grid::Distribution&, const eckit::Configuration& = util::NoConfig());
220 PointCloud(const Grid&, const grid::Partitioner&, const eckit::Configuration& = util::NoConfig());
221
222 operator bool() const { return valid(); }
223 bool valid() const { return functionspace_; }
224
225 const Field& vertical() const { return functionspace_->vertical(); }
226
227 detail::PointCloud::Iterate iterate() const { return functionspace_->iterate(); }
228
229
230 private:
231 const detail::PointCloud* functionspace_;
232 };
233
234 } // namespace functionspace
235 } // namespace atlas