Debian Codegraph @

src/atlas/mesh/MeshBuilder.cc

raw
1 /*
2 * (C) Copyright 2023 UCAR.
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/mesh/MeshBuilder.h"
9
10 #include <algorithm>
11 #include <numeric>
12
13 #include "atlas/array/MakeView.h"
14 #include "atlas/grid/Grid.h"
15 #include "atlas/grid/Iterator.h"
16 #include "atlas/grid/StructuredGrid.h"
17 #include "atlas/grid/UnstructuredGrid.h"
18 #include "atlas/mesh/ElementType.h"
19 #include "atlas/mesh/HybridElements.h"
20 #include "atlas/mesh/Mesh.h"
21 #include "atlas/mesh/Nodes.h"
22 #include "atlas/parallel/mpi/mpi.h"
23 #include "atlas/util/CoordinateEnums.h"
24
25 namespace atlas {
26
27 namespace detail {
28 atlas::UnstructuredGrid assemble_unstructured_grid(size_t nb_nodes, const double lon[], const double lat[],
29 const int ghost[], const eckit::mpi::Comm& comm) {
30
31 // First serialize owned lons and lats into single vector
32 const size_t nb_owned_nodes = std::count(ghost, ghost + nb_nodes, 0);
33 std::vector<double> owned_lonlat(2 * nb_owned_nodes);
34 int counter = 0;
35 for (size_t n = 0; n < nb_nodes; ++n) {
36 if (ghost[n] == 0) {
37 owned_lonlat[counter] = lon[n];
38 counter++;
39 owned_lonlat[counter] = lat[n];
40 counter++;
41 }
42 }
43 ATLAS_ASSERT(counter == 2 * nb_owned_nodes);
44
45 // Gather points across MPI ranks
46 size_t nb_nodes_global = 0;
47 comm.allReduce(nb_owned_nodes, nb_nodes_global, eckit::mpi::sum());
48
49 std::vector<double> global_lonlat(2 * nb_nodes_global);
50 eckit::mpi::Buffer<double> buffer(comm.size());
51 comm.allGatherv(owned_lonlat.begin(), owned_lonlat.end(), buffer);
52 global_lonlat = std::move(buffer.buffer);
53
54 std::vector<atlas::PointXY> points(nb_nodes_global);
55 for (size_t n = 0; n < nb_nodes_global; ++n) {
56 points[n] = atlas::PointXY({global_lonlat[2*n], global_lonlat[2*n + 1]});
57 }
58
59 return atlas::UnstructuredGrid(new std::vector<atlas::PointXY>(points.begin(), points.end()));
60 }
61
62 // Check if grid points match mesh points -- not obviously true as the MeshBuilder sets the Grid
63 // independently from the Mesh. This check can give confidence that the grid and mesh are specified
64 // consistently with each other.
65 //
66 // This check makes a few assumptions
67 // - the global_index passed to the MeshBuilder are a global_index_base-based contiguous index
68 // - the Grid is one of StructuredGrid or UnstructedGrid. This restriction is because a
69 // CubedSphereGrid doesn't present a simple interface for the coordinates at the N'th point.
70 // - the Mesh grid points are the grid nodes, and not the grid cell-centers (e.g., HEALPix or CubedSphere meshes)
71 void validate_grid_vs_mesh(const eckit::mpi::Comm& comm, const atlas::Grid& grid,
72 size_t nb_nodes, const double lon[], const double lat[], size_t lonstride, size_t latstride,
73 const int ghost[], const gidx_t global_index[], gidx_t global_index_base) {
74 // Check assumption that global_index look like a global_index_based contiguous index
75 const size_t nb_owned_nodes = std::count(ghost, ghost + nb_nodes, 0);
76 size_t nb_nodes_global = 0;
77 comm.allReduce(nb_owned_nodes, nb_nodes_global, eckit::mpi::sum());
78 for (size_t n = 0; n < nb_nodes; ++n) {
79 if (ghost[n] == 0) {
80 // Check global_index is consistent with a global_index_base contiguous index over nodes
81 auto g = global_index[n] - global_index_base;
82 ATLAS_ASSERT(g >= 0);
83 ATLAS_ASSERT(g < nb_nodes_global);
84 }
85 }
86
87 double lonlat[2];
88 const auto equal_within_roundoff = [](const double a, const double b) -> bool {
89 return std::abs(a - b) <= 360.0 * 1.0e-16;
90 };
91
92 // Check lonlats for each supported grid type
93 if (grid.type() == "unstructured") {
94 const atlas::UnstructuredGrid ugrid(grid);
95 for (size_t n = 0; n < nb_nodes; ++n) {
96 if (ghost[n] == 0) {
97 ugrid.lonlat(global_index[n] - global_index_base, lonlat);
98 if (!equal_within_roundoff(lonlat[0], lon[n]) || !equal_within_roundoff(lonlat[1], lat[n])) {
99 throw_Exception("In MeshBuilder: UnstructuredGrid from config does not match mesh coordinates", Here());
100 }
101 }
102 }
103 } else if (grid.type() == "structured") {
104 const atlas::StructuredGrid sgrid(grid);
105 for (size_t n = 0; n < nb_nodes; ++n) {
106 if (ghost[n] == 0) {
107 idx_t i, j;
108 sgrid.index2ij(global_index[n] - global_index_base, i, j);
109 sgrid.lonlat(i, j, lonlat);
110 if (!equal_within_roundoff(lonlat[0], lon[n]) || !equal_within_roundoff(lonlat[1], lat[n])) {
111 throw_Exception("In MeshBuilder: StructuredGrid from config does not match mesh coordinates", Here());
112 }
113 }
114 }
115 } else {
116 for (size_t n = 0; n < nb_nodes; ++n) {
117 if (ghost[n] == 0) {
118 auto point = *(grid.lonlat().begin() + static_cast<size_t>(global_index[n] - global_index_base));
119 if (!equal_within_roundoff(point.lon(), lon[n]) || !equal_within_roundoff(point.lat(), lat[n])) {
120 throw_Exception("In MeshBuilder: Grid from config does not match mesh coordinates", Here());
121 }
122 }
123 }
124 }
125 }
126 } // namespace detail
127
128 namespace mesh {
129
130 //----------------------------------------------------------------------------------------------------------------------
131
132 Mesh MeshBuilder::operator()(const std::vector<double>& lon, const std::vector<double>& lat,
133 const std::vector<int>& ghost, const std::vector<gidx_t>& global_index,
134 const std::vector<idx_t>& remote_index, const idx_t remote_index_base,
135 const std::vector<int>& partition,
136 const std::vector<std::array<gidx_t, 3>>& triag_nodes_global,
137 const std::vector<gidx_t>& triag_global_index,
138 const std::vector<std::array<gidx_t, 4>>& quad_nodes_global,
139 const std::vector<gidx_t>& quad_global_index,
140 const eckit::Configuration& config) const {
141 constexpr gidx_t global_index_base = 1;
142
143 return operator()(
144 span<const gidx_t>{global_index.data(),global_index.size()},
145 span<const double>{lon.data(), lon.size()}, span<const double>{lat.data(), lat.size()},
146 span<const double>{lon.data(), lon.size()}, span<const double>{lat.data(), lat.size()},
147 span<const int>{ghost.data(), ghost.size()}, span<const int>{partition.data(), partition.size()},
148 span<const idx_t>{remote_index.data(), remote_index.size()}, remote_index_base,
149 span<const gidx_t>{triag_global_index.data(), triag_global_index.size()},
150 mdspan<const gidx_t,extents<size_t,dynamic_extent,3>>{reinterpret_cast<const gidx_t*>(triag_nodes_global.data()),triag_nodes_global.size()},
151 span<const gidx_t>{quad_global_index.data(), quad_global_index.size()},
152 mdspan<const gidx_t,extents<size_t,dynamic_extent,4>>{reinterpret_cast<const gidx_t*>(quad_nodes_global.data()),quad_nodes_global.size()},
153 global_index_base,
154 config);
155 }
156
157 Mesh MeshBuilder::operator()(size_t nb_nodes, const double lon[], const double lat[], const int ghost[],
158 const gidx_t global_index[], const idx_t remote_index[], const idx_t remote_index_base,
159 const int partition[], size_t nb_triags, const gidx_t triag_nodes_global[],
160 const gidx_t triag_global_index[], size_t nb_quads, const gidx_t quad_nodes_global[],
161 const gidx_t quad_global_index[],
162 const eckit::Configuration& config) const {
163 constexpr gidx_t global_index_base = 1;
164 return this->operator()(
165 nb_nodes, global_index,
166 lon, lat,
167 ghost, partition, remote_index, remote_index_base,
168 nb_triags, triag_global_index, triag_nodes_global,
169 nb_quads, quad_global_index, quad_nodes_global,
170 global_index_base,
171 config
172 );
173 }
174
175 Mesh MeshBuilder::operator()(
176 size_t nb_nodes, const gidx_t global_index[],
177 const double lon[], const double lat[],
178 const int ghost[], const int partition[], const idx_t remote_index[], const idx_t remote_index_base,
179 size_t nb_triags, const gidx_t triag_nodes_global[], const gidx_t triag_global_index[],
180 size_t nb_quads, const gidx_t quad_nodes_global[], const gidx_t quad_global_index[],
181 gidx_t global_index_base,
182 const eckit::Configuration& config) const {
183 return this->operator()(
184 nb_nodes, global_index,
185 lon, lat, 1, 1,
186 lon, lat, 1, 1,
187 ghost, partition, remote_index, remote_index_base,
188 nb_triags, triag_global_index, triag_nodes_global,
189 nb_quads, quad_global_index, quad_nodes_global,
190 global_index_base,
191 config
192 );
193 }
194
195
196 Mesh MeshBuilder::operator()(size_t nb_nodes, const gidx_t global_index[],
197 const double x[], const double y[], size_t xstride, size_t ystride,
198 const double lon[], const double lat[], size_t lonstride, size_t latstride,
199 const int ghost[], const int partitions[], const idx_t remote_index[], const idx_t remote_index_base,
200 size_t nb_triags, const gidx_t triag_global_index[], const gidx_t triag_nodes_global[],
201 size_t nb_quads, const gidx_t quad_global_index[], const gidx_t quad_nodes_global[],
202 const eckit::Configuration& config) const {
203 constexpr gidx_t global_index_base = 1;
204 return this->operator()(
205 nb_nodes, global_index,
206 x, y, xstride, ystride,
207 lon, lat, lonstride, latstride,
208 ghost, partitions, remote_index, remote_index_base,
209 nb_triags, triag_global_index, triag_nodes_global,
210 nb_quads, quad_global_index, quad_nodes_global,
211 global_index_base,
212 config);
213 }
214
215
216 Mesh MeshBuilder::operator()(size_t nb_nodes, const gidx_t global_index[],
217 const double x[], const double y[], size_t xstride, size_t ystride,
218 const double lon[], const double lat[], size_t lonstride, size_t latstride,
219 const int ghost[], const int partitions[], const idx_t remote_index[], const idx_t remote_index_base,
220 size_t nb_triags, const gidx_t triag_global_index[], const gidx_t triag_nodes_global[],
221 size_t nb_quads, const gidx_t quad_global_index[], const gidx_t quad_nodes_global[],
222 gidx_t global_index_base,
223 const eckit::Configuration& config) const {
224 // Get MPI comm from config name or fall back to atlas default comm
225 auto mpi_comm_name = [](const auto& config) {
226 return config.getString("mpi_comm", atlas::mpi::comm().name());
227 };
228
229 Mesh mesh{};
230 mesh.metadata().set("mpi_comm", mpi_comm_name(config));
231 auto& comm = mpi::comm(mesh.mpi_comm());
232
233 // Setup a grid, if requested via config argument
234 if (config.has("grid")) {
235 atlas::Grid grid;
236 if (config.has("grid.type") && (config.getString("grid.type") == "unstructured")
237 && !config.has("grid.xy")) {
238 // Assemble the unstructured grid by gathering input lons,lats across ranks
239 grid = ::atlas::detail::assemble_unstructured_grid(nb_nodes, lon, lat, ghost, comm);
240 } else {
241 // Build grid directly from config
242 grid = atlas::Grid(config.getSubConfiguration("grid"));
243 const bool validate = config.getBool("validate", false);
244 if (validate) {
245 ::atlas::detail::validate_grid_vs_mesh(comm, grid, nb_nodes, lon, lat, lonstride, latstride, ghost, global_index, global_index_base);
246 }
247 }
248 mesh.setGrid(grid);
249 }
250
251 // Populate node data
252
253 mesh.nodes().resize(nb_nodes);
254 auto xy = array::make_view<double, 2>(mesh.nodes().xy());
255 auto lonlat = array::make_view<double, 2>(mesh.nodes().lonlat());
256 auto ghostv = array::make_view<int, 1>(mesh.nodes().ghost());
257 auto gidx = array::make_view<gidx_t, 1>(mesh.nodes().global_index());
258 auto ridx = array::make_indexview<idx_t, 1>(mesh.nodes().remote_index());
259 auto partition = array::make_view<int, 1>(mesh.nodes().partition());
260 auto halo = array::make_view<int, 1>(mesh.nodes().halo());
261
262 for (size_t i = 0; i < nb_nodes; ++i) {
263 xy(i, size_t(XX)) = x[i*xstride];
264 xy(i, size_t(YY)) = y[i*ystride];
265 lonlat(i, size_t(LON)) = lon[i*lonstride];
266 lonlat(i, size_t(LAT)) = lat[i*latstride];
267 ghostv(i) = ghost[i];
268 gidx(i) = global_index[i] - global_index_base + 1; // Make 1-based!
269 ridx(i) = remote_index[i] - remote_index_base;
270 partition(i) = partitions[i];
271 }
272 halo.assign(0);
273
274 // Populate cell/element data
275
276 // First, count how many cells of each type are on this processor
277 // Then optimize away the element type if globally nb_triags or nb_quads is zero
278 size_t sum_nb_triags = 0;
279 comm.allReduce(nb_triags, sum_nb_triags, eckit::mpi::sum());
280 const bool add_triags = (sum_nb_triags > 0);
281
282 size_t sum_nb_quads = 0;
283 comm.allReduce(nb_quads, sum_nb_quads, eckit::mpi::sum());
284 const bool add_quads = (sum_nb_quads > 0);
285
286 if (add_triags) {
287 mesh.cells().add(mesh::ElementType::create("Triangle"), nb_triags);
288 }
289 if (add_quads) {
290 mesh.cells().add(mesh::ElementType::create("Quadrilateral"), nb_quads);
291 }
292
293 atlas::mesh::HybridElements::Connectivity& node_connectivity = mesh.cells().node_connectivity();
294 auto cells_part = array::make_view<int, 1>(mesh.cells().partition());
295 auto cells_gidx = array::make_view<gidx_t, 1>(mesh.cells().global_index());
296
297 std::unordered_map<gidx_t,idx_t> global_to_local_index;
298 global_to_local_index.reserve(nb_nodes);
299 for (gidx_t j=0; j<nb_nodes; ++j) {
300 global_to_local_index.insert({global_index[j],j});
301 }
302
303 size_t idx = 0;
304 if (add_triags) {
305 idx_t buffer[3];
306 for (size_t tri = 0; tri < nb_triags; ++tri) {
307 for (size_t i = 0; i < 3; ++i) {
308 buffer[i] = global_to_local_index.at(triag_nodes_global[3 * tri + i]);
309 }
310 node_connectivity.set(idx, buffer);
311 cells_gidx(idx) = triag_global_index[tri] - global_index_base + 1; // make 1-based
312 idx++;
313 }
314 }
315 if (add_quads) {
316 idx_t buffer[4];
317 for (size_t quad = 0; quad < nb_quads; ++quad) {
318 for (size_t i = 0; i < 4; ++i) {
319 buffer[i] = global_to_local_index.at(quad_nodes_global[4 * quad + i]);
320 }
321 node_connectivity.set(idx, buffer);
322 cells_gidx(idx) = quad_global_index[quad] - global_index_base + 1; // make 1-based
323 idx++;
324 }
325 }
326
327 ATLAS_ASSERT(idx == nb_triags + nb_quads);
328
329 cells_part.assign(comm.rank());
330
331 return mesh;
332 }
333
334 Mesh MeshBuilder::operator()(
335 span<const gidx_t> global_index,
336 strided_span<const double> x, strided_span<const double> y,
337 strided_span<const double> lon, strided_span<const double> lat,
338 span<const int> ghost, span<const int> partition,
339 span<const idx_t> remote_index, const idx_t remote_index_base,
340 span<const gidx_t> triag_global_index, mdspan<const gidx_t, extents<size_t,dynamic_extent,3>> triag_nodes_global,
341 span<const gidx_t> quad_global_index, mdspan<const gidx_t, extents<size_t,dynamic_extent,4>> quad_nodes_global,
342 const gidx_t global_index_base,
343 const eckit::Configuration& config) const {
344
345 const size_t nb_nodes = global_index.size();
346 const size_t nb_triags = triag_global_index.size();
347 const size_t nb_quads = quad_global_index.size();
348
349 ATLAS_ASSERT(nb_nodes == lon.size());
350 ATLAS_ASSERT(nb_nodes == lat.size());
351 ATLAS_ASSERT(nb_nodes == ghost.size());
352 ATLAS_ASSERT(nb_nodes == remote_index.size());
353 ATLAS_ASSERT(nb_nodes == partition.size());
354 ATLAS_ASSERT(nb_triags == triag_nodes_global.extent(0));
355 ATLAS_ASSERT(nb_quads == quad_nodes_global.extent(0));
356
357 return operator()(
358 nb_nodes, global_index.data_handle(),
359 x.data_handle(), y.data_handle(), x.stride(0), y.stride(0),
360 lon.data_handle(), lat.data_handle(), lon.stride(0), lat.stride(0),
361 ghost.data_handle(), partition.data_handle(), remote_index.data_handle(), remote_index_base,
362 nb_triags, triag_global_index.data_handle(), triag_nodes_global.data_handle(),
363 nb_quads, quad_global_index.data_handle(), quad_nodes_global.data_handle(),
364 global_index_base,
365 config);
366 }
367
368 //----------------------------------------------------------------------------------------------------------------------
369
370 Mesh TriangularMeshBuilder::operator()(size_t nb_nodes, const gidx_t nodes_global_index[], const double x[], const double y[], const double lon[], const double lat[],
371 size_t nb_triags, const gidx_t triangle_global_index[], const gidx_t triangle_nodes_global_index[]) const {
372 constexpr gidx_t global_index_base = 1;
373 constexpr size_t stride_1 = 1;
374 return this->operator()(
375 nb_nodes, nodes_global_index, x, y, stride_1, stride_1, lon, lat, stride_1, stride_1,
376 nb_triags, triangle_global_index, triangle_nodes_global_index,
377 global_index_base
378 );
379 }
380
381 //----------------------------------------------------------------------------------------------------------------------
382
383 Mesh TriangularMeshBuilder::operator()(size_t nb_nodes, const gidx_t nodes_global_index[],
384 const double x[], const double y[], size_t xstride, size_t ystride,
385 const double lon[], const double lat[], size_t lonstride, size_t latstride,
386 size_t nb_triags, const gidx_t triangle_global_index[], const gidx_t triangle_nodes_global_index[],
387 gidx_t global_index_base) const {
388 std::vector<int> ghost(nb_nodes,0);
389 std::vector<int> partition(nb_nodes,0);
390 std::vector<idx_t> remote_index(nb_nodes);
391 idx_t remote_index_base = 0;
392 std::iota(remote_index.begin(), remote_index.end(), remote_index_base);
393
394 size_t nb_quads = 0;
395 gidx_t quad_global_index[] = {};
396 gidx_t quad_nodes_global_index[] = {};
397
398 return meshbuilder_(
399 nb_nodes, nodes_global_index,
400 x, y, xstride, ystride, lon, lat, lonstride, latstride,
401 ghost.data(), partition.data(), remote_index.data(), remote_index_base,
402 nb_triags, triangle_global_index, triangle_nodes_global_index,
403 nb_quads, quad_global_index, quad_nodes_global_index,
404 global_index_base);
405 }
406
407 //----------------------------------------------------------------------------------------------------------------------
408
409 } // namespace mesh
410 } // namespace atlas