Debian Codegraph @

src/atlas/mesh/PartitionPolygon.cc

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 #include <fstream>
12 #include <sstream>
13
14 #include "eckit/exception/Exceptions.h"
15
16 #include "atlas/array/MakeView.h"
17 #include "atlas/field/Field.h"
18 #include "atlas/mesh.h"
19 #include "atlas/mesh/PartitionPolygon.h"
20 #include "atlas/parallel/mpi/mpi.h"
21 #include "atlas/runtime/Trace.h"
22 #include "atlas/util/CoordinateEnums.h"
23
24 namespace atlas {
25 namespace mesh {
26
27 namespace {
28 util::Polygon::edge_set_t compute_edges(const detail::MeshImpl& mesh, idx_t halo) {
29 ATLAS_TRACE("PartitionPolygon [halo=" + std::to_string(halo) + "]");
30 // extract partition boundary edges by always attempting first to`
31 // remove a reversed edge from a neighbouring element, if any
32
33 util::Polygon::edge_set_t edges;
34 for (idx_t t = 0; t < mesh.cells().nb_types(); ++t) {
35 const Elements& elements = mesh.cells().elements(t);
36
37 const BlockConnectivity& conn = elements.node_connectivity();
38 auto field_flags = elements.view<int, 1>(elements.flags());
39 auto field_halo = elements.view<int, 1>(elements.halo());
40
41 constexpr bool include_patch = false;
42 auto include = [&](idx_t e) {
43 using util::Topology;
44 auto topology = Topology::view(field_flags(e));
45 if (field_halo(e) > halo) {
46 return false;
47 }
48 if (not include_patch) {
49 if (topology.check(Topology::PATCH)) {
50 return false;
51 }
52 }
53 return true;
54 };
55
56
57 const idx_t nb_nodes = elements.nb_nodes();
58
59 for (idx_t j = 0; j < elements.size(); ++j) {
60 if (include(j)) {
61 for (idx_t k = 0; k < nb_nodes; ++k) {
62 util::Polygon::edge_t edge(conn(j, k), conn(j, (k + 1) % nb_nodes));
63 if (!edges.erase(edge.reverse())) {
64 edges.insert(edge);
65 }
66 }
67 }
68 }
69 }
70 return edges;
71 }
72 } // namespace
73
74 PartitionPolygon::PartitionPolygon(const detail::MeshImpl& mesh, idx_t halo):
75 util::PartitionPolygon(compute_edges(mesh, halo)), mesh_(mesh), halo_(halo) {}
76
77 size_t PartitionPolygon::footprint() const {
78 size_t size = sizeof(*this);
79 size += capacity() * sizeof(idx_t);
80 return size;
81 }
82
83 void PartitionPolygon::outputPythonScript(const eckit::PathName& filepath, const eckit::Configuration& config) const {
84 const auto& comm = mpi::comm(mesh_.mpi_comm());
85 int mpi_rank = int(comm.rank());
86 int mpi_size = int(comm.size());
87
88 std::string coordinates = config.getString("coordinates", "xy");
89 if (coordinates == "ij") {
90 coordinates = "xy";
91 }
92 auto points =
93 coordinates == "xy" ? this->xy() :
94 // coordinates == "ij" ? this->ij() : /* no member named `ij` in PartitionPolygon */
95 this->lonlat();
96
97 ATLAS_ASSERT(points.size() == size());
98 const auto nodes_xy = array::make_view<double, 2>(mesh_.nodes().field(coordinates));
99 const auto nodes_g = array::make_view<gidx_t, 1>(mesh_.nodes().global_index());
100 double xmin = std::numeric_limits<double>::max();
101 double xmax = -std::numeric_limits<double>::max();
102 for (const auto& p : points) {
103 xmin = std::min(xmin, p[XX]);
104 xmax = std::max(xmax, p[XX]);
105 }
106 comm.allReduceInPlace(xmin, eckit::mpi::min());
107 comm.allReduceInPlace(xmax, eckit::mpi::max());
108
109 idx_t count = mesh_.nodes().size();
110 idx_t count_all = count;
111 comm.allReduceInPlace(count_all, eckit::mpi::sum());
112
113 bool plot_nodes = config.getBool("nodes", false);
114 for (int r = 0; r < mpi_size; ++r) {
115 if (mpi_rank == r) {
116 std::ofstream f(filepath.asString().c_str(), mpi_rank == 0 ? std::ios::trunc : std::ios::app);
117 if (!f.is_open()) {
118 throw eckit::CantOpenFile(filepath);
119 }
120 //clang-format off
121 if (mpi_rank == 0) {
122 f << "\n"
123 "# Configuration option to plot nodes"
124 "\n"
125 "plot_nodes = " +
126 std::string(plot_nodes ? "True" : "False")
127 << "\n"
128 "plot_node_numbers = False"
129 "\n"
130 "\n"
131 "import matplotlib.pyplot as plt"
132 "\n"
133 "from matplotlib.path import Path"
134 "\n"
135 "import matplotlib.patches as patches"
136 "\n"
137 "\n"
138 "from itertools import cycle"
139 "\n"
140 "import matplotlib.cm as cm"
141 "\n"
142 "import numpy as np"
143 "\n"
144 "colours = cycle([cm.Paired(i) for i in np.linspace(0,1,12,endpoint=True)])"
145 "\n"
146 "\n"
147 "fig = plt.figure()"
148 "\n"
149 "ax = fig.add_subplot(111,aspect='equal')"
150 "\n";
151 }
152 f << "\n"
153 "verts_"
154 << r << " = [";
155 for (const auto& p : points) {
156 f << "\n (" << p[XX] << ", " << p[YY] << "), ";
157 }
158 f << "\n]"
159 "\n"
160 "\n"
161 "codes_"
162 << r
163 << " = [Path.MOVETO]"
164 "\n"
165 "codes_"
166 << r << ".extend([Path.LINETO] * " << (size() - 2)
167 << ")"
168 "\n"
169 "codes_"
170 << r
171 << ".extend([Path.CLOSEPOLY])"
172 "\n"
173 "\n"
174 "count_"
175 << r << " = " << count
176 << "\n"
177 "count_all_"
178 << r << " = " << count_all << "\n";
179 if (plot_nodes) {
180 f << "\n"
181 "g_"
182 << r << " = [";
183 for (idx_t i = 0; i < count; ++i) {
184 f << nodes_g(i) << ", ";
185 }
186 f << "]"
187 "\n"
188 "x_"
189 << r << " = [";
190 for (idx_t i = 0; i < count; ++i) {
191 f << nodes_xy(i, XX) << ", ";
192 }
193 f << "]"
194 "\n"
195 "y_"
196 << r << " = [";
197 for (idx_t i = 0; i < count; ++i) {
198 f << nodes_xy(i, YY) << ", ";
199 }
200 f << "]";
201 }
202 f << "\n"
203 "\n"
204 "c = next(colours)"
205 "\n"
206 "ax.add_patch(patches.PathPatch(Path(verts_"
207 << r << ", codes_" << r << "), color=c, alpha=0.3, lw=1))";
208 if (plot_nodes) {
209 f << "\n"
210 "if plot_nodes:"
211 "\n"
212 " ax.scatter(x_"
213 << r << ", y_" << r
214 << ", color=c, marker='o')"
215 "\n"
216 " if plot_node_numbers:"
217 "\n"
218 " for (g,x,y) in zip(g_"
219 << r << ", x_" << r << ", y_" << r
220 << "):"
221 "\n"
222 " ax.text(x,y, str(g), color=c, fontsize=12)";
223 }
224 f << "\n";
225 // clang-format on
226 if (mpi_rank == mpi_size - 1) {
227 auto xticks = [&]() -> std::string {
228 std::vector<int> xticks;
229 xticks.push_back(std::floor(xmin - int(xmin) % 45));
230 while (xticks.back() < int(std::round(xmax))) {
231 xticks.push_back(xticks.back() + 45);
232 }
233 std::stringstream xticks_ss;
234 for (int i = 0; i < xticks.size(); ++i) {
235 xticks_ss << (i == 0 ? "[" : ",") << xticks[i];
236 }
237 xticks_ss << "]";
238 return xticks_ss.str();
239 };
240 // clang-format off
241 if (mesh_.projection().units() == "degrees") {
242 f << "\n" "ax.set_xlim( " << xmin << "-5, " << xmax << "+5)"
243 "\n" "ax.set_ylim(-90-5, 90+5)"
244 "\n" "ax.set_xticks(" << xticks() << ")"
245 "\n" "ax.set_yticks([-90,-45,0,45,90])";
246 }
247 else {
248 f << "\n" "ax.autoscale()";
249 }
250 f << "\n" "plt.grid()"
251 "\n" "plt.show()";
252 // clang-format on
253 }
254 }
255 comm.barrier();
256 }
257 }
258
259 void PartitionPolygon::allGather(util::PartitionPolygons& polygons) const {
260 ATLAS_TRACE();
261
262 const auto& comm = mpi::comm(mesh_.mpi_comm());
263 const int mpi_size = int(comm.size());
264
265 polygons.clear();
266 polygons.reserve(mpi_size);
267
268 auto& poly = *this;
269
270 std::vector<double> mypolygon;
271 mypolygon.reserve(poly.size() * 2);
272
273 auto points_xy = poly.xy();
274 ATLAS_ASSERT(points_xy.front() == points_xy.back());
275 for (auto& p : points_xy) {
276 mypolygon.push_back(p[XX]);
277 mypolygon.push_back(p[YY]);
278 }
279 ATLAS_ASSERT(mypolygon.size() >= 4);
280
281 eckit::mpi::Buffer<double> recv_polygons(mpi_size);
282
283 comm.allGatherv(mypolygon.begin(), mypolygon.end(), recv_polygons);
284
285 for (idx_t p = 0; p < mpi_size; ++p) {
286 PointsXY recv_points;
287 recv_points.reserve(recv_polygons.counts[p]);
288 for (idx_t j = 0; j < recv_polygons.counts[p] / 2; ++j) {
289 PointXY pxy(*(recv_polygons.begin() + recv_polygons.displs[p] + 2 * j + XX),
290 *(recv_polygons.begin() + recv_polygons.displs[p] + 2 * j + YY));
291 recv_points.push_back(pxy);
292 }
293 polygons.emplace_back(new util::ExplicitPartitionPolygon(std::move(recv_points)));
294 }
295 }
296
297 void PartitionPolygon::print(std::ostream& out) const {
298 out << "polygon:{"
299 << "halo:" << halo_ << ",size:" << size() << ",nodes:" << static_cast<const util::Polygon&>(*this) << "}";
300 }
301
302 namespace {
303 PartitionPolygon::PointsXY points(const Mesh::Implementation& mesh_, const Field& coords,
304 const std::vector<idx_t>& polygon) {
305 PartitionPolygon::PointsXY points_xy;
306 points_xy.reserve(polygon.size());
307
308 auto xy_view = array::make_view<double, 2>(coords);
309 auto flags = array::make_view<int, 1>(mesh_.nodes().flags());
310
311 bool domain_includes_north_pole = false;
312 bool domain_includes_south_pole = false;
313 if (mesh_.grid()) {
314 if (mesh_.grid().domain()) {
315 domain_includes_north_pole = mesh_.grid().domain().containsNorthPole();
316 domain_includes_south_pole = mesh_.grid().domain().containsSouthPole();
317 }
318 }
319 auto bc_north = [&flags, &domain_includes_north_pole](idx_t n) {
320 if (domain_includes_north_pole) {
321 using Topology = atlas::mesh::Nodes::Topology;
322 return Topology::check(flags(n), Topology::BC | Topology::NORTH);
323 }
324 return false;
325 };
326 auto bc_south = [&flags, &domain_includes_south_pole](idx_t n) {
327 if (domain_includes_south_pole) {
328 using Topology = atlas::mesh::Nodes::Topology;
329 return Topology::check(flags(n), Topology::BC | Topology::SOUTH);
330 }
331 return false;
332 };
333
334 if (coords.name() == "xy") {
335 for (idx_t i : polygon) {
336 double y = bc_north(i) ? 90. : bc_south(i) ? -90. : xy_view(i, idx_t(YY));
337 points_xy.emplace_back(xy_view(i, idx_t(XX)), y);
338 }
339 }
340 else {
341 for (idx_t i : polygon) {
342 points_xy.emplace_back(xy_view(i, idx_t(XX)), xy_view(i, idx_t(YY)));
343 }
344 }
345 return points_xy;
346 }
347 } // namespace
348
349 PartitionPolygon::PointsXY PartitionPolygon::xy() const {
350 return points(mesh_, mesh_.nodes().xy(), *this);
351 }
352
353 PartitionPolygon::PointsLonLat PartitionPolygon::lonlat() const {
354 return points(mesh_, mesh_.nodes().lonlat(), *this);
355 }
356
357 } // namespace mesh
358 } // namespace atlas