Debian Codegraph @

src/atlas/meshgenerator/detail/StructuredMeshGenerator.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 #include <algorithm>
11 #include <cmath>
12 #include <iomanip>
13 #include <limits>
14 #include <memory>
15 #include <numeric>
16 #include <vector>
17
18 #include "eckit/log/ProgressTimer.h"
19 #include "eckit/types/FloatCompare.h"
20 #include "eckit/utils/Hash.h"
21
22 #include "atlas/array.h"
23 #include "atlas/array/ArrayView.h"
24 #include "atlas/array/MakeView.h"
25 #include "atlas/field/Field.h"
26 #include "atlas/grid/Distribution.h"
27 #include "atlas/grid/Partitioner.h"
28 #include "atlas/grid/StructuredGrid.h"
29 #include "atlas/library/config.h"
30 #include "atlas/mesh/ElementType.h"
31 #include "atlas/mesh/Elements.h"
32 #include "atlas/mesh/HybridElements.h"
33 #include "atlas/mesh/Mesh.h"
34 #include "atlas/mesh/Nodes.h"
35 #include "atlas/meshgenerator/detail/MeshGeneratorFactory.h"
36 #include "atlas/meshgenerator/detail/StructuredMeshGenerator.h"
37 #include "atlas/parallel/mpi/mpi.h"
38 #include "atlas/runtime/Log.h"
39 #include "atlas/runtime/Trace.h"
40 #include "atlas/util/CoordinateEnums.h"
41
42 #define DEBUG_OUTPUT 0
43
44 using namespace atlas::array;
45 using atlas::Mesh;
46 using Topology = atlas::mesh::Nodes::Topology;
47
48 namespace atlas {
49 namespace meshgenerator {
50
51 namespace {
52 static double to_rad = M_PI / 180.;
53 static double to_deg = 180. * M_1_PI;
54 } // namespace
55
56 struct Region {
57 int north {-1};
58 int south {-1};
59 std::unique_ptr<array::Array> elems;
60 int ntriags {0};
61 int nquads {0};
62 int nnodes {0};
63 std::vector<idx_t> lat_begin;
64 std::vector<idx_t> lat_end;
65 std::vector<idx_t> nb_lat_elems;
66 };
67
68 StructuredMeshGenerator::StructuredMeshGenerator(const eckit::Parametrisation& p) {
69 configure_defaults();
70
71 bool include_pole;
72 if (p.get("include_pole", include_pole)) {
73 options.set("include_pole", include_pole);
74 }
75
76 bool patch_pole;
77 if (p.get("patch_pole", patch_pole)) {
78 options.set("patch_pole", patch_pole);
79 }
80
81 bool patch_quads;
82 if (p.get("patch_quads", patch_quads)) {
83 options.set("patch_quads", patch_quads);
84 }
85
86 bool unique_pole;
87 if (p.get("unique_pole", unique_pole)) {
88 options.set("unique_pole", unique_pole);
89 }
90
91 bool force_include_pole;
92 if (p.get("force_include_north_pole", force_include_pole)) {
93 options.set("force_include_north_pole", force_include_pole);
94 }
95 if (p.get("force_include_south_pole", force_include_pole)) {
96 options.set("force_include_south_pole", force_include_pole);
97 }
98
99 bool three_dimensional;
100 if (p.get("three_dimensional", three_dimensional) || p.get("3d", three_dimensional)) {
101 options.set("three_dimensional", three_dimensional);
102 }
103
104 std::string mpi_comm_name = mpi::comm().name();
105 p.get("mpi_comm", mpi_comm_name);
106 options.set("mpi_comm",mpi_comm_name);
107 auto& comm = mpi::comm(mpi_comm_name);
108
109 // This option sets number of parts the mesh will be split in
110 options.set("nb_parts", comm.size());
111
112 // This option sets the part that will be generated
113 options.set("part", comm.rank());
114
115 size_t nb_parts;
116 if (p.get("nb_parts", nb_parts)) {
117 options.set("nb_parts", nb_parts);
118 }
119
120 size_t part;
121 if (p.get("part", part)) {
122 options.set("part", part);
123 }
124
125 double angle;
126 if (p.get("angle", angle)) {
127 options.set("angle", angle);
128 }
129
130 bool triangulate;
131 if (p.get("triangulate", triangulate)) {
132 options.set("triangulate", triangulate);
133 }
134
135 bool ghost_at_end;
136 if (p.get("ghost_at_end", ghost_at_end)) {
137 options.set("ghost_at_end", ghost_at_end);
138 }
139
140 std::string partitioner;
141 if (p.get("partitioner", partitioner)) {
142 if (not grid::Partitioner::exists(partitioner)) {
143 Log::warning() << "Atlas does not have support for partitioner " << partitioner << ". "
144 << "Defaulting to use partitioner EqualRegions" << std::endl;
145 partitioner = "equal_regions";
146 }
147 }
148 else {
149 partitioner = "equal_regions";
150 }
151 options.set("partitioner", partitioner);
152 }
153
154 void StructuredMeshGenerator::configure_defaults() {
155 // This option creates a point at the pole when true
156 options.set("include_pole", false);
157
158 // This option sets the part that will be generated
159 options.set("patch_pole", true);
160
161 // This option disregards multiple poles in grid (e.g. lonlat up to poles) and
162 // connects elements
163 // to the first node only. Note this option will only be looked at in case
164 // other option
165 // "three_dimensional"==true
166 options.set("unique_pole", true);
167
168 // This option creates elements that connect east to west at greenwich
169 // meridian
170 // when true, instead of creating periodic ghost-points at east boundary when
171 // false
172 options.set("three_dimensional", false);
173
174 // Experimental option. This is what makes tripolar grid patched with quads
175 options.set("patch_quads", false);
176
177 // This option sets the maximum angle deviation for a quadrilateral element
178 // angle = 30 --> minimises number of triangles
179 // angle = 0 --> maximises number of triangles
180 options.set("angle", 0.);
181
182 options.set("triangulate", false);
183
184 options.set("ghost_at_end", true);
185 }
186
187 void StructuredMeshGenerator::generate(const Grid& grid, Mesh& mesh) const {
188 ATLAS_TRACE();
189 ATLAS_ASSERT(!mesh.generated());
190
191 const StructuredGrid rg = StructuredGrid(grid);
192 if (!rg) {
193 throw_Exception("Structured can only work with a Structured grid", Here());
194 }
195
196 idx_t nb_parts = options.getInt("nb_parts");
197
198 std::string partitioner_type = "equal_regions";
199 options.get("partitioner", partitioner_type);
200
201 if (partitioner_type == "ectrans") {
202 if (rg.ny() % 2 == 1) {
203 partitioner_type = "equal_regions"; // Odd number of latitudes
204 }
205 if (nb_parts != mpi::size()) {
206 partitioner_type = "equal_regions";
207 }
208 }
209 if (nb_parts == 1) {
210 partitioner_type = "serial";
211 }
212 mpi::scope::push(options.getString("mpi_comm"));
213 grid::Partitioner partitioner(partitioner_type, nb_parts);
214 grid::Distribution distribution(partitioner.partition(grid));
215 mpi::scope::pop();
216 generate(grid, distribution, mesh);
217 }
218
219 void StructuredMeshGenerator::hash(eckit::Hash& h) const {
220 h.add("Structured");
221 options.hash(h);
222 }
223
224 void StructuredMeshGenerator::generate(const Grid& grid, const grid::Distribution& distribution, Mesh& mesh) const {
225 ATLAS_TRACE("structuredmeshgenerator(grid,dist,mesh)");
226 Log::debug() << "StructuredMeshGenerator generating mesh from " << grid.name() << std::endl;
227
228 const StructuredGrid rg = StructuredGrid(grid);
229 if (!rg) {
230 throw_Exception("Grid could not be cast to a Structured", Here());
231 }
232
233 ATLAS_ASSERT(!mesh.generated());
234
235 if (grid.size() != idx_t(distribution.size())) {
236 std::stringstream msg;
237 msg << "Number of points in grid (" << grid.size()
238 << ") different from "
239 "number of points in grid distribution ("
240 << distribution.size() << ")";
241 throw_AssertionFailed(msg.str(), Here());
242 }
243
244 idx_t mypart = options.getInt("part");
245
246 // show distribution
247 #if DEBUG_OUTPUT
248 int inode = 0;
249 Log::info() << "Partition : " << std::endl;
250 for (size_t ilat = 0; ilat < rg.ny(); ilat++) {
251 for (size_t ilon = 0; ilon < rg.nx(ilat); ilon++) {
252 Log::info() << std::setw(3) << distribution.partition(inode);
253 inode++;
254 }
255 Log::info() << std::endl;
256 }
257 #endif
258
259 // clone some grid properties
260 setGrid(mesh, rg, distribution);
261
262 Region region;
263 generate_region(rg, distribution, mypart, region);
264
265 if (not rg.projection() && rg.domain().global()) {
266 double max_dy = 0;
267 for (size_t ilat = 0; ilat < rg.ny()-1; ilat++) {
268 max_dy = std::max(max_dy, std::abs(rg.y(ilat)-rg.y(ilat+1)));
269 }
270 double cell_maximum_diagonal_on_unit_sphere = std::sqrt(max_dy*max_dy + max_dy*max_dy) * M_PI / 180.;
271 mesh.metadata().set("cell_maximum_diagonal_on_unit_sphere",cell_maximum_diagonal_on_unit_sphere);
272 }
273
274 mesh.metadata().set("nb_parts",options.getInt("nb_parts"));
275 mesh.metadata().set("part",options.getInt("part"));
276 mesh.metadata().set("mpi_comm",options.getString("mpi_comm"));
277 generate_mesh(rg, distribution, region, mesh);
278 }
279
280 void StructuredMeshGenerator::generate_region(const StructuredGrid& rg, const grid::Distribution& distribution,
281 int mypart, Region& region) const {
282 ATLAS_TRACE();
283
284 double max_angle = options.getDouble("angle");
285 bool triangulate_quads = options.getBool("triangulate");
286 bool three_dimensional = options.getBool("three_dimensional");
287 bool has_north_pole = eckit::types::is_approximately_equal(rg.y().front(), 90.);
288 bool has_south_pole = eckit::types::is_approximately_equal(rg.y().back(), -90.);
289 bool unique_pole = options.getBool("unique_pole") && three_dimensional && has_north_pole && has_south_pole;
290 bool periodic_east_west = rg.periodic();
291
292 region.lat_begin.resize(rg.ny(), -1);
293 region.lat_end.resize(rg.ny(), -1);
294 region.nb_lat_elems.resize(rg.ny(), 0);
295
296 idx_t lat_north = -1;
297 idx_t lat_south = -1;
298
299 if (distribution.nb_partitions() == 1) {
300 lat_north = 0;
301 lat_south = rg.ny() - 1;
302 }
303 else {
304 ATLAS_TRACE_SCOPE("compute bounds") {
305 // This part does not scale
306 /*
307 Find min and max latitudes used by this part.
308 */
309
310 size_t num_threads = atlas_omp_get_max_threads();
311 if (num_threads == 1) {
312 int n = 0;
313 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
314 for (idx_t jlon = 0; jlon < rg.nx(jlat); ++jlon) {
315 if (distribution.partition(n) == mypart) {
316 lat_north = jlat;
317 goto end_north;
318 }
319 ++n;
320 }
321 }
322 end_north:;
323
324 n = rg.size() - 1;
325 for (idx_t jlat = rg.ny() - 1; jlat >= 0; --jlat) {
326 for (idx_t jlon = rg.nx(jlat) - 1; jlon >= 0; --jlon) {
327 if (distribution.partition(n) == mypart) {
328 lat_south = jlat;
329 goto end_south;
330 }
331 --n;
332 }
333 }
334 end_south:;
335 }
336 else {
337 std::vector<idx_t> thread_reduce_lat_north(num_threads, std::numeric_limits<idx_t>::max());
338 std::vector<idx_t> thread_reduce_lat_south(num_threads, std::numeric_limits<idx_t>::min());
339
340 atlas_omp_parallel {
341 const idx_t thread_num = atlas_omp_get_thread_num();
342 const idx_t jbegin = static_cast<int>(size_t(thread_num) * size_t(rg.ny()) / size_t(num_threads));
343 const idx_t jend = static_cast<int>(size_t(thread_num + 1) * size_t(rg.ny()) / size_t(num_threads));
344
345 auto& thread_lat_north = thread_reduce_lat_north[thread_num];
346 auto& thread_lat_south = thread_reduce_lat_south[thread_num];
347
348 for (idx_t jlat = jbegin; jlat < jend && thread_lat_north > jend; ++jlat) {
349 int n = rg.index(0, jlat);
350 for (idx_t jlon = 0; jlon < rg.nx(jlat); ++jlon, ++n) {
351 if (distribution.partition(n) == mypart) {
352 thread_lat_north = jlat;
353 break;
354 }
355 }
356 }
357
358 for (idx_t jlat = jend - 1; jlat >= jbegin && thread_lat_south < jbegin; --jlat) {
359 int n = rg.index(0, jlat);
360 for (idx_t jlon = 0; jlon < rg.nx(jlat); ++jlon, ++n) {
361 if (distribution.partition(n) == mypart) {
362 thread_lat_south = jlat;
363 break;
364 }
365 }
366 }
367 }
368 lat_north = *std::min_element(thread_reduce_lat_north.begin(), thread_reduce_lat_north.end());
369 lat_south = *std::max_element(thread_reduce_lat_south.begin(), thread_reduce_lat_south.end());
370 if (lat_north == std::numeric_limits<idx_t>::max()) {
371 lat_north = -1;
372 }
373 if (lat_south == std::numeric_limits<idx_t>::min()) {
374 lat_south = -1;
375 }
376 }
377 }
378 }
379
380 if (lat_north == -1 && lat_south == -1 ) {
381 return;
382 }
383
384 ATLAS_ASSERT(lat_north >= 0);
385 ATLAS_ASSERT(lat_south < rg.ny() );
386
387 std::vector<idx_t> offset(rg.ny(), 0);
388
389 int n = 0;
390 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
391 offset.at(jlat) = n;
392 n += rg.nx(jlat);
393 }
394
395 /*
396 We need to connect to next region
397 */
398 if (lat_north - 1 >= 0 && rg.nx(lat_north - 1) > 0) {
399 --lat_north;
400 }
401 if (idx_t(lat_south + 1) <= rg.ny() - 1 && rg.nx(lat_south + 1) > 0) {
402 ++lat_south;
403 }
404
405 region.north = lat_north;
406 region.south = lat_south;
407
408 array::ArrayShape shape = array::make_shape(region.south - region.north, 4 * rg.nxmax(), 4);
409
410 region.elems.reset(array::Array::create<int>(shape));
411
412 [[maybe_unused]] int nelems = 0;
413 region.nquads = 0;
414 region.ntriags = 0;
415
416 array::ArrayView<int, 3> elemview = array::make_view<int, 3>(*region.elems);
417 elemview.assign(-1);
418
419 ATLAS_TRACE_SCOPE("generate elements") {
420 eckit::Channel blackhole;
421
422 eckit::ProgressTimer progress("Generating elements for " + std::to_string(lat_south - lat_north) + " rows",
423 lat_south - lat_north, " row", 5.,
424 lat_south - lat_north > 1280 ? Log::trace() : blackhole);
425
426 for (idx_t jlat = lat_north; jlat < lat_south; ++jlat, ++progress) {
427 // std::stringstream filename; filename << "/tmp/debug/"<<jlat;
428
429 idx_t ilat, latN, latS;
430 idx_t ipN1, ipN2, ipS1, ipS2;
431 double xN1, xN2, yN, xS1, xS2, yS;
432 double dN1S2, dS1N2; // dN2S2;
433 bool try_make_triangle_up, try_make_triangle_down, try_make_quad;
434 bool add_triag, add_quad;
435
436 ilat = jlat - region.north;
437
438 auto lat_elems_view = elemview.slice(ilat, Range::all(), Range::all());
439
440 latN = jlat;
441 latS = jlat + 1;
442 yN = rg.y(latN);
443 yS = rg.y(latS);
444
445 idx_t beginN, beginS, endN, endS;
446
447 beginN = 0;
448 endN = rg.nx(latN) - (periodic_east_west ? 0 : 1);
449 if (eckit::types::is_approximately_equal(yN, 90.) && unique_pole) {
450 endN = beginN;
451 }
452
453 beginS = 0;
454 endS = rg.nx(latS) - (periodic_east_west ? 0 : 1);
455 if (eckit::types::is_approximately_equal(yS, -90.) && unique_pole) {
456 endS = beginS;
457 }
458
459 ipN1 = beginN;
460 ipS1 = beginS;
461 ipN2 = std::min(ipN1 + 1, endN);
462 ipS2 = std::min(ipS1 + 1, endS);
463
464 idx_t jelem = 0;
465 int pE = distribution.partition(offset.at(latN));
466
467 #if DEBUG_OUTPUT
468 Log::info() << "=================\n";
469 Log::info() << "latN, latS : " << latN << ", " << latS << '\n';
470 #endif
471
472 while (true) {
473 if (ipN1 == endN && ipS1 == endS) {
474 // ATLAS_DEBUG("ipN1 == endN && ipS1 == endS");
475 break;
476 }
477
478 #if DEBUG_OUTPUT
479 Log::info() << "-------\n";
480 #endif
481
482 // ATLAS_ASSERT(offset.at(latN)+ipN1 < parts.size());
483 // ATLAS_ASSERT(offset.at(latS)+ipS1 < parts.size());
484
485 int pN1, pS1, pN2, pS2;
486 if (ipN1 != rg.nx(latN)) {
487 pN1 = distribution.partition(offset.at(latN) + ipN1);
488 }
489 else {
490 pN1 = distribution.partition(offset.at(latN));
491 }
492 if (ipS1 != rg.nx(latS)) {
493 pS1 = distribution.partition(offset.at(latS) + ipS1);
494 }
495 else {
496 pS1 = distribution.partition(offset.at(latS));
497 }
498
499 if (ipN2 == rg.nx(latN)) {
500 pN2 = distribution.partition(offset.at(latN));
501 }
502 else {
503 pN2 = distribution.partition(offset.at(latN) + ipN2);
504 }
505 if (ipS2 == rg.nx(latS)) {
506 pS2 = distribution.partition(offset.at(latS));
507 }
508 else {
509 pS2 = distribution.partition(offset.at(latS) + ipS2);
510 }
511
512 // Log::info() << ipN1 << "("<<pN1<<") " << ipN2 <<"("<<pN2<<")" << std::endl;
513 // Log::info() << ipS1 << "("<<pS2<<") " << ipS2 <<"("<<pS2<<")" << std::endl;
514
515 #if DEBUG_OUTPUT
516 Log::info() << ipN1 << "(" << pN1 << ") " << ipN2 << "(" << pN2 << ")" << std::endl;
517 Log::info() << ipS1 << "(" << pS2 << ") " << ipS2 << "(" << pS2 << ")" << std::endl;
518 #endif
519
520 xN1 = rg.x(ipN1, latN) * to_rad;
521 xN2 = rg.x(ipN2, latN) * to_rad;
522 xS1 = rg.x(ipS1, latS) * to_rad;
523 xS2 = rg.x(ipS2, latS) * to_rad;
524
525 #if DEBUG_OUTPUT
526 Log::info() << "-------\n";
527 #endif
528 // Log::info() << " access " <<
529 // region.elems.stride(0)*(jlat-region.north) +
530 // region.elems.stride(1)*jelem + 5 << std::endl;
531 // Log::info() << ipN1 << "("<< xN1 << ") " << ipN2 << "("<< xN2
532 // << ") " << std::endl;
533 // Log::info() << ipS1 << "("<< xS1 << ") " << ipS2 << "("<< xS2
534 // << ") " << std::endl;
535 try_make_triangle_up = false;
536 try_make_triangle_down = false;
537 try_make_quad = false;
538
539 // ------------------------------------------------
540 // START RULES
541 // ------------------------------------------------
542
543 const double dxN = std::abs(xN2 - xN1);
544 const double dxS = std::abs(xS2 - xS1);
545 const double dx = std::min(dxN, dxS);
546 const double alpha1 = (dx == 0. ? 0. : std::atan2((xN1 - xS1), dx) * to_deg);
547 const double alpha2 = (dx == 0. ? 0. : std::atan2((xN2 - xS2), dx) * to_deg);
548 if (std::abs(alpha1) <= max_angle && std::abs(alpha2) <= max_angle) {
549 if (triangulate_quads) {
550 if (false) // std::abs(alpha1) < 1 && std::abs(alpha2) < 1)
551 {
552 try_make_triangle_up = (jlat + ipN1) % 2;
553 try_make_triangle_down = (jlat + ipN1 + 1) % 2;
554 }
555 else {
556 dN1S2 = std::abs(xN1 - xS2);
557 dS1N2 = std::abs(xS1 - xN2);
558 // dN2S2 = std::abs(xN2-xS2);
559 // Log::info() << " dN1S2 " << dN1S2 << " dS1N2 " << dS1N2 << "
560 // dN2S2 " << dN2S2 << std::endl;
561 if (dN1S2 == dS1N2) {
562 try_make_triangle_up = (jlat + ipN1) % 2;
563 try_make_triangle_down = (jlat + ipN1 + 1) % 2;
564 }
565 else if (dN1S2 < dS1N2) {
566 if (ipS1 != ipS2) {
567 try_make_triangle_up = true;
568 }
569 else {
570 try_make_triangle_down = true;
571 }
572 }
573 else if (dN1S2 > dS1N2) {
574 if (ipN1 != ipN2) {
575 try_make_triangle_down = true;
576 }
577 else {
578 try_make_triangle_up = true;
579 }
580 }
581 else {
582 throw_Exception("Should not be here", Here());
583 }
584 }
585 }
586 else {
587 if (ipN1 == ipN2) {
588 try_make_triangle_up = true;
589 }
590 else if (ipS1 == ipS2) {
591 try_make_triangle_down = true;
592 }
593 else {
594 try_make_quad = true;
595 }
596
597 // try_make_quad = true;
598 }
599 }
600 else {
601 dN1S2 = std::abs(xN1 - xS2);
602 dS1N2 = std::abs(xS1 - xN2);
603 // dN2S2 = std::abs(xN2-xS2);
604 // Log::info() << " dN1S2 " << dN1S2 << " dS1N2 " << dS1N2 << "
605 // dN2S2 " << dN2S2 << std::endl;
606 if ((dN1S2 <= dS1N2) && (ipS1 != ipS2)) {
607 try_make_triangle_up = true;
608 }
609 else if ((dN1S2 >= dS1N2) && (ipN1 != ipN2)) {
610 try_make_triangle_down = true;
611 }
612 else {
613 if (ipN1 == ipN2) {
614 try_make_triangle_up = true;
615 }
616 else if (ipS1 == ipS2) {
617 try_make_triangle_down = true;
618 }
619 else {
620 ATLAS_DEBUG_VAR(dN1S2);
621 ATLAS_DEBUG_VAR(dS1N2);
622 ATLAS_DEBUG_VAR(jlat);
623 Log::info() << ipN1 << "(" << xN1 << ") " << ipN2 << "(" << xN2 << ") " << std::endl;
624 Log::info() << ipS1 << "(" << xS1 << ") " << ipS2 << "(" << xS2 << ") " << std::endl;
625 throw_Exception("Should not try to make a quadrilateral!", Here());
626 }
627 }
628 }
629
630 ATLAS_ASSERT( (ipN1!=ipN2) || (ipS1 != ipS2) );
631 if( ipN1 == ipN2 ) {
632 try_make_triangle_up = true;
633 try_make_triangle_down = false;
634 try_make_quad = false;
635 }
636 if( ipS1 == ipS2 ) {
637 try_make_triangle_up = false;
638 try_make_triangle_down = true;
639 try_make_quad = false;
640 }
641
642 // ------------------------------------------------
643 // END RULES
644 // ------------------------------------------------
645
646 #if DEBUG_OUTPUT
647 ATLAS_DEBUG_VAR(jelem);
648 #endif
649
650 auto elem = lat_elems_view.slice(jelem, Range::all());
651
652 if (try_make_quad) {
653 // add quadrilateral
654 #if DEBUG_OUTPUT
655 Log::info() << " " << ipN1 << " " << ipN2 << '\n';
656 Log::info() << " " << ipS1 << " " << ipS2 << '\n';
657 #endif
658 elem(0) = ipN1;
659 elem(1) = ipS1;
660 elem(2) = ipS2;
661 elem(3) = ipN2;
662 std::array<int, 4> np{pN1, pN2, pS1, pS2};
663 std::array<int, 4> pcnts;
664 for (int j = 0; j < 4; ++j) {
665 pcnts[j] = static_cast<int>(std::count(np.begin(), np.end(), np[j]));
666 }
667 if (pcnts[0] > 2) { // 3 or more of pN1
668 pE = pN1;
669 if (latS == rg.ny() - 1) {
670 pE = pS1;
671 }
672 }
673 else if (pcnts[2] > 2) { // 3 or more of pS1
674 pE = pS1;
675 if (latN == 0) {
676 pE = pN1;
677 }
678 }
679 else {
680 std::array<int, 4>::iterator p_max = std::max_element(pcnts.begin(), pcnts.end());
681 if (*p_max > 2) { // 3 or 4 points belong to same part
682 pE = np[std::distance(np.begin(), p_max)];
683 }
684 else { // 3 or 4 points don't belong to mypart
685 pE = pN1;
686 if (latS == rg.ny() - 1) {
687 pE = pS1;
688 }
689 }
690 }
691 add_quad = (pE == mypart);
692 if (add_quad) {
693 ++region.nquads;
694 ++jelem;
695 ++nelems;
696
697 if (region.lat_begin.at(latN) == -1) {
698 region.lat_begin.at(latN) = ipN1;
699 }
700 if (region.lat_begin.at(latS) == -1) {
701 region.lat_begin.at(latS) = ipS1;
702 }
703 region.lat_begin.at(latN) = std::min<int>(region.lat_begin.at(latN), ipN1);
704 region.lat_begin.at(latS) = std::min<int>(region.lat_begin.at(latS), ipS1);
705 region.lat_end.at(latN) = std::max<int>(region.lat_end.at(latN), ipN2);
706 region.lat_end.at(latS) = std::max<int>(region.lat_end.at(latS), ipS2);
707 }
708 else {
709 #if DEBUG_OUTPUT
710 Log::info() << "Quad belongs to other partition" << std::endl;
711 #endif
712 }
713 ipN1 = ipN2;
714 ipS1 = ipS2;
715 }
716 else if (try_make_triangle_down) // make triangle down
717 {
718 // triangle without ip3
719 #if DEBUG_OUTPUT
720 Log::info() << "v " << ipN1 << " " << ipN2 << '\n';
721 Log::info() << " " << ipS1 << '\n';
722 #endif
723 elem(0) = ipN1;
724 elem(1) = ipS1;
725 elem(2) = -1;
726 elem(3) = ipN2;
727
728 pE = pN1;
729 if (latS == rg.ny() - 1) {
730 pE = pS1;
731 }
732 add_triag = (mypart == pE);
733
734 if (add_triag) {
735 ATLAS_ASSERT(ipN1 != ipN2, "Faulty triangle with latN = "+std::to_string(latN)+"("+std::to_string(rg.y(latN))+")");
736 ++region.ntriags;
737 ++jelem;
738 ++nelems;
739
740 if (region.lat_begin.at(latN) == -1) {
741 region.lat_begin.at(latN) = ipN1;
742 }
743 if (region.lat_begin.at(latS) == -1) {
744 region.lat_begin.at(latS) = ipS1;
745 }
746 region.lat_begin.at(latN) = std::min<int>(region.lat_begin.at(latN), ipN1);
747 region.lat_begin.at(latS) = std::min<int>(region.lat_begin.at(latS), ipS1);
748 region.lat_end.at(latN) = std::max<int>(region.lat_end.at(latN), ipN2);
749 region.lat_end.at(latS) = std::max<int>(region.lat_end.at(latS), ipS1);
750 }
751 else {
752 #if DEBUG_OUTPUT
753 Log::info() << "Downward Triag belongs to other partition" << std::endl;
754 #endif
755 }
756 ipN1 = ipN2;
757 // and ipS1=ipS1;
758 }
759 else if (try_make_triangle_up) // make triangle up
760 {
761 // triangle without ip4
762 #if DEBUG_OUTPUT
763 Log::info() << "^ " << ipN1 << " (" << pN1 << ")" << '\n';
764 Log::info() << " " << ipS1 << " (" << pS1 << ")"
765 << " " << ipS2 << " (" << pS2 << ")" << '\n';
766 #endif
767 elem(0) = ipN1;
768 elem(1) = ipS1;
769 elem(2) = ipS2;
770 elem(3) = -1;
771
772 if (pS1 == pE && pN1 != pE) {
773 if (xN1 < 0.5 * (xS1 + xS2)) {
774 pE = pN1;
775 } // else pE of previous element
776 }
777 else {
778 pE = pN1;
779 }
780 if (ipN1 == rg.nx(latN)) {
781 pE = pS1;
782 }
783 if (latS == rg.ny() - 1) {
784 pE = pS1;
785 }
786
787 add_triag = (mypart == pE);
788
789 if (add_triag) {
790 ++region.ntriags;
791 ++jelem;
792 ++nelems;
793
794 if (region.lat_begin.at(latN) == -1) {
795 region.lat_begin.at(latN) = ipN1;
796 }
797 if (region.lat_begin.at(latS) == -1) {
798 region.lat_begin.at(latS) = ipS1;
799 }
800 region.lat_begin.at(latN) = std::min(region.lat_begin.at(latN), ipN1);
801 region.lat_begin.at(latS) = std::min(region.lat_begin.at(latS), ipS1);
802 region.lat_end.at(latN) = std::max(region.lat_end.at(latN), ipN1);
803 region.lat_end.at(latS) = std::max(region.lat_end.at(latS), ipS2);
804 }
805 else {
806 #if DEBUG_OUTPUT
807 Log::info() << "Upward Triag belongs to other partition" << std::endl;
808 #endif
809 }
810 ipS1 = ipS2;
811 // and ipN1=ipN1;
812 }
813 else {
814 throw_Exception("Could not detect which element to create", Here());
815 }
816 ipN2 = std::min(endN, ipN1 + 1);
817 ipS2 = std::min(endS, ipS1 + 1);
818 }
819 region.nb_lat_elems.at(jlat) = jelem;
820 #if DEBUG_OUTPUT
821 ATLAS_DEBUG_VAR(region.nb_lat_elems.at(jlat));
822 #endif
823 if (region.nb_lat_elems.at(jlat) == 0 && latN == region.north) {
824 ++region.north;
825 }
826 if (region.nb_lat_elems.at(jlat) == 0 && latS == region.south) {
827 --region.south;
828 }
829 // region.lat_end.at(latN) = std::min(region.lat_end.at(latN),
830 // int(rg.nx(latN)-1));
831 // region.lat_end.at(latS) = std::min(region.lat_end.at(latS),
832 // int(rg.nx(latS)-1));
833 if (yN == 90 && unique_pole) {
834 region.lat_end.at(latN) = rg.nx(latN) - 1;
835 }
836 if (yS == -90 && unique_pole) {
837 region.lat_end.at(latS) = rg.nx(latS) - 1;
838 }
839
840 if (region.nb_lat_elems.at(jlat) > 0) {
841 region.lat_end.at(latN) = std::max(region.lat_end.at(latN), region.lat_begin.at(latN));
842 region.lat_end.at(latS) = std::max(region.lat_end.at(latS), region.lat_begin.at(latS));
843 }
844 } // for jlat
845 }
846
847 // Log::info() << "nb_triags = " << region.ntriags << std::endl;
848 // Log::info() << "nb_quads = " << region.nquads << std::endl;
849 // Log::info() << "nb_elems = " << nelems << std::endl;
850
851 int nb_region_nodes = 0;
852 for (int jlat = region.north; jlat <= region.south; ++jlat) {
853 n = offset.at(jlat);
854 region.lat_begin.at(jlat) = std::max<idx_t>(0, region.lat_begin.at(jlat));
855 for (idx_t jlon = 0; jlon < rg.nx(jlat); ++jlon) {
856 if (distribution.partition(n) == mypart) {
857 region.lat_begin.at(jlat) = std::min(region.lat_begin.at(jlat), jlon);
858 region.lat_end.at(jlat) = std::max(region.lat_end.at(jlat), jlon);
859 }
860 ++n;
861 }
862 nb_region_nodes += region.lat_end.at(jlat) - region.lat_begin.at(jlat) + 1;
863
864 // Count extra periodic node
865 // if( periodic_east_west && size_t(region.lat_end.at(jlat)) == rg.nx(jlat)
866 // - 1) ++nb_region_nodes;
867 }
868
869 region.nnodes = nb_region_nodes;
870 if (region.nnodes == 0) {
871 throw_Exception(
872 "Trying to generate mesh with too many partitions. Reduce "
873 "the number of partitions.",
874 Here());
875 }
876 #if DEBUG_OUTPUT
877 ATLAS_DEBUG("End of generate_region()");
878 #endif
879 }
880
881 namespace {
882 struct GhostNode {
883 GhostNode(int _jlat, int _jlon, int _jnode) {
884 jlat = _jlat;
885 jlon = _jlon;
886 jnode = _jnode;
887 }
888 int jlat;
889 int jlon;
890 int jnode;
891 };
892 } // namespace
893
894 void StructuredMeshGenerator::generate_mesh(const StructuredGrid& rg, const grid::Distribution& distribution,
895 const Region& region, Mesh& mesh) const {
896 ATLAS_TRACE();
897
898 ATLAS_ASSERT(!mesh.generated());
899
900 int mypart = options.getInt("part");
901 int nparts = options.getInt("nb_parts");
902 int n, l;
903 const int y_numbering = (rg.y().front() < rg.y().back()) ? +1 : -1;
904 double y_north = y_numbering < 0 ? rg.y().front() : rg.y().back();
905 double y_south = y_numbering < 0 ? rg.y().back() : rg.y().front();
906 idx_t nx_north = y_numbering < 0 ? rg.nx().front() : rg.nx().back();
907 idx_t nx_south = y_numbering < 0 ? rg.nx().back() : rg.nx().front();
908
909 std::vector<int> part_north(nx_north);
910 std::vector<int> part_south(nx_south);
911 if (y_numbering < 0) {
912 distribution.partition(0, nx_north, part_north);
913 distribution.partition(rg.size() - nx_south, rg.size(), part_south);
914 }
915 else {
916 distribution.partition(0, nx_south, part_south);
917 distribution.partition(rg.size() - nx_north, rg.size(), part_north);
918 }
919 bool mypart_at_north = std::any_of(part_north.begin(), part_north.end(), [&](int p) { return p == mypart; });
920 bool mypart_at_south = std::any_of(part_south.begin(), part_south.end(), [&](int p) { return p == mypart; });
921
922 bool three_dimensional = options.getBool("three_dimensional");
923 bool periodic_east_west = rg.periodic();
924 bool include_periodic_ghost_points = periodic_east_west && !three_dimensional;
925 bool remove_periodic_ghost_points = periodic_east_west && three_dimensional;
926
927 bool has_point_at_north_pole = y_north == 90 && nx_north > 0;
928 bool has_point_at_south_pole = y_south == -90 && nx_south > 0;
929 bool possible_north_pole = !has_point_at_north_pole && rg.domain().containsNorthPole() && mypart_at_north;
930 bool possible_south_pole = !has_point_at_south_pole && rg.domain().containsSouthPole() && mypart_at_south;
931
932 bool force_include_north_pole(options.has("force_include_north_pole") &&
933 options.getBool("force_include_north_pole"));
934 bool force_include_south_pole(options.has("force_include_south_pole") &&
935 options.getBool("force_include_south_pole"));
936
937 bool include_north_pole = (possible_north_pole && options.getBool("include_pole")) || force_include_north_pole;
938 bool include_south_pole = (possible_south_pole && options.getBool("include_pole")) || force_include_south_pole;
939 bool patch_north_pole = possible_north_pole && options.getBool("patch_pole") && nx_north > 0;
940 bool patch_south_pole = possible_south_pole && options.getBool("patch_pole") && nx_south > 0;
941 bool patch_quads = options.getBool("patch_quads");
942
943 int nnewnodes = (!has_point_at_north_pole && include_north_pole ? 1 : 0) +
944 (!has_point_at_south_pole && include_south_pole ? 1 : 0);
945
946 if (three_dimensional && nparts != 1) {
947 throw_Exception("Cannot generate three_dimensional mesh in parallel", Here());
948 }
949 int nnodes = region.nnodes;
950 int ntriags = region.ntriags;
951 int nquads = region.nquads;
952
953 if (include_north_pole) {
954 ++nnodes;
955 ntriags += nx_north - (periodic_east_west ? 0 : 1);
956 }
957 else if (patch_north_pole) {
958 if (patch_quads) {
959 nquads += (nx_north + 1) / 2;
960 }
961 else {
962 ntriags += nx_north - 2;
963 }
964 }
965 if (include_south_pole) {
966 ++nnodes;
967 ntriags += nx_south - (periodic_east_west ? 0 : 1);
968 }
969 else if (patch_south_pole) {
970 if (patch_quads) {
971 nquads += (nx_south + 1) / 2;
972 }
973 else {
974 ntriags += nx_south - 2;
975 }
976 }
977
978 size_t node_numbering_size = nnodes;
979 if (remove_periodic_ghost_points) {
980 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
981 if (region.lat_end[jlat] >= rg.nx(jlat)) {
982 --nnodes;
983 }
984 }
985 }
986 ATLAS_ASSERT(nnodes >= nnewnodes);
987
988 #if DEBUG_OUTPUT
989 ATLAS_DEBUG_VAR(include_periodic_ghost_points);
990 ATLAS_DEBUG_VAR(include_north_pole);
991 ATLAS_DEBUG_VAR(include_south_pole);
992 ATLAS_DEBUG_VAR(three_dimensional);
993 ATLAS_DEBUG_VAR(patch_north_pole);
994 ATLAS_DEBUG_VAR(patch_south_pole);
995 ATLAS_DEBUG_VAR(rg.size());
996 ATLAS_DEBUG_VAR(nnodes);
997 ATLAS_DEBUG_VAR(ntriags);
998 ATLAS_DEBUG_VAR(nquads);
999 ATLAS_DEBUG_VAR(options.getBool("ghost_at_end"));
1000 #endif
1001
1002 std::vector<int> offset_glb(rg.ny());
1003 std::vector<int> offset_loc(region.south - region.north + 1, 0);
1004
1005 n = 0;
1006 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
1007 offset_glb.at(jlat) = n;
1008 n += rg.nx(jlat);
1009 }
1010
1011 std::vector<int> periodic_glb(rg.ny());
1012
1013 if (include_periodic_ghost_points) {
1014 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
1015 if (rg.nx(jlat) > 0) {
1016 periodic_glb.at(jlat) = n;
1017 ++n;
1018 }
1019 }
1020 }
1021 else {
1022 for (idx_t jlat = 0; jlat < rg.ny(); ++jlat) {
1023 if (rg.nx(jlat) > 0) {
1024 periodic_glb.at(jlat) = offset_glb.at(jlat) + rg.nx(jlat) - 1;
1025 }
1026 }
1027 }
1028 int max_glb_idx = n;
1029
1030 mesh.nodes().resize(nnodes);
1031 mesh::Nodes& nodes = mesh.nodes();
1032
1033 auto xy = array::make_view<double, 2>(nodes.xy());
1034 auto lonlat = array::make_view<double, 2>(nodes.lonlat());
1035 auto glb_idx = array::make_view<gidx_t, 1>(nodes.global_index());
1036 auto part = array::make_view<int, 1>(nodes.partition());
1037 auto ghost = array::make_view<int, 1>(nodes.ghost());
1038 auto flags = array::make_view<int, 1>(nodes.flags());
1039 auto halo = array::make_view<int, 1>(nodes.halo());
1040
1041 idx_t jnorth = -1;
1042 idx_t jsouth = -1;
1043
1044 std::vector<idx_t> node_numbering(node_numbering_size, -1);
1045 if (nnodes > 0) {
1046 if (options.getBool("ghost_at_end")) {
1047 std::vector<GhostNode> ghost_nodes;
1048 ghost_nodes.reserve(nnodes);
1049 idx_t node_number = 0;
1050 idx_t jnode = 0;
1051 l = 0;
1052 ATLAS_ASSERT(region.south >= region.north);
1053 for (int jlat = region.north; jlat <= region.south; ++jlat) {
1054 int ilat = jlat - region.north;
1055 offset_loc.at(ilat) = l;
1056 l += region.lat_end.at(jlat) - region.lat_begin.at(jlat) + 1;
1057
1058 if (region.lat_end.at(jlat) < region.lat_begin.at(jlat)) {
1059 ATLAS_DEBUG_VAR(jlat);
1060 ATLAS_DEBUG_VAR(region.lat_begin[jlat]);
1061 ATLAS_DEBUG_VAR(region.lat_end[jlat]);
1062 }
1063 for (idx_t jlon = region.lat_begin.at(jlat); jlon <= region.lat_end.at(jlat); ++jlon) {
1064 if (jlon < rg.nx(jlat)) {
1065 n = offset_glb.at(jlat) + jlon;
1066 if (distribution.partition(n) == mypart) {
1067 node_numbering.at(jnode) = node_number;
1068 ++node_number;
1069 }
1070 else {
1071 ghost_nodes.emplace_back(jlat, jlon, jnode);
1072 }
1073 ++jnode;
1074 }
1075 else if (include_periodic_ghost_points) // add periodic point
1076 {
1077 //#warning TODO: use commented approach
1078 part(jnode) = mypart;
1079 // part(jnode) = parts.at( offset_glb.at(jlat) );
1080 ghost(jnode) = 1;
1081 halo(jnode) = 0;
1082 ghost_nodes.emplace_back(jlat, rg.nx(jlat), jnode);
1083 ++jnode;
1084 }
1085 else {
1086 --l;
1087 }
1088 }
1089 }
1090 for (size_t jghost = 0; jghost < ghost_nodes.size(); ++jghost) {
1091 node_numbering.at(ghost_nodes.at(jghost).jnode) = node_number;
1092 ++node_number;
1093 }
1094 if (include_north_pole) {
1095 node_numbering.at(jnode) = jnode;
1096 ++jnode;
1097 }
1098 if (include_south_pole) {
1099 node_numbering.at(jnode) = jnode;
1100 ++jnode;
1101 }
1102 ATLAS_ASSERT(jnode == nnodes);
1103 }
1104 else // No renumbering
1105 {
1106 for (idx_t jnode = 0; jnode < nnodes; ++jnode) {
1107 node_numbering.at(jnode) = jnode;
1108 }
1109 }
1110
1111 idx_t jnode = 0;
1112 l = 0;
1113 for (idx_t jlat = region.north; jlat <= region.south; ++jlat) {
1114 idx_t ilat = jlat - region.north;
1115 offset_loc.at(ilat) = l;
1116 l += region.lat_end.at(jlat) - region.lat_begin.at(jlat) + 1;
1117
1118 double y = rg.y(jlat);
1119 for (idx_t jlon = region.lat_begin.at(jlat); jlon <= region.lat_end.at(jlat); ++jlon) {
1120 if (jlon < rg.nx(jlat)) {
1121 idx_t inode = node_numbering.at(jnode);
1122 n = offset_glb.at(jlat) + jlon;
1123
1124 double x = rg.x(jlon, jlat);
1125 // std::cout << "jlat = " << jlat << "; jlon = " << jlon << "; x = " <<
1126 // x << std::endl;
1127
1128 xy(inode, XX) = x;
1129 xy(inode, YY) = y;
1130
1131 // geographic coordinates by using projection
1132 double crd[] = {x, y};
1133 rg.projection().xy2lonlat(crd);
1134 lonlat(inode, LON) = crd[LON];
1135 lonlat(inode, LAT) = crd[LAT];
1136
1137 glb_idx(inode) = n + 1;
1138 part(inode) = distribution.partition(n);
1139 ghost(inode) = 0;
1140 halo(inode) = 0;
1141 Topology::reset(flags(inode));
1142 Topology::set(flags(inode), Topology::BC);
1143 if (jlat == 0) {
1144 if (y_numbering < 0 && !include_north_pole) {
1145 Topology::set(flags(inode), Topology::NORTH);
1146 }
1147 if (y_numbering > 0 && !include_south_pole) {
1148 Topology::set(flags(inode), Topology::SOUTH);
1149 }
1150 }
1151 if (jlat == rg.ny() - 1) {
1152 if (y_numbering < 0 && !include_south_pole) {
1153 Topology::set(flags(inode), Topology::SOUTH);
1154 }
1155 if (y_numbering > 0 && !include_north_pole) {
1156 Topology::set(flags(inode), Topology::NORTH);
1157 }
1158 }
1159 if (jlon == 0 && include_periodic_ghost_points) {
1160 Topology::set(flags(inode), Topology::WEST);
1161 }
1162 if (part(inode) != mypart) {
1163 Topology::set(flags(inode), Topology::GHOST);
1164 ghost(inode) = 1;
1165 }
1166 ++jnode;
1167 }
1168 else if (include_periodic_ghost_points) // add periodic point
1169 {
1170 idx_t inode = node_numbering.at(jnode);
1171 // int inode_left = node_numbering.at(jnode-1);
1172 double x = rg.x(rg.nx(jlat), jlat);
1173
1174 xy(inode, XX) = x;
1175 xy(inode, YY) = y;
1176
1177 // geographic coordinates by using projection
1178 double crd[] = {x, y};
1179 rg.projection().xy2lonlat(crd);
1180 lonlat(inode, LON) = crd[LON];
1181 lonlat(inode, LAT) = crd[LAT];
1182
1183 glb_idx(inode) = periodic_glb.at(jlat) + 1;
1184 //#warning TODO: use commented approach
1185 // part(inode) = parts.at( offset_glb.at(jlat) );
1186 part(inode) = mypart; // The actual part will be fixed later
1187 ghost(inode) = 1;
1188 halo(inode) = 0;
1189 Topology::reset(flags(inode));
1190 Topology::set(flags(inode), Topology::BC | Topology::EAST);
1191 Topology::set(flags(inode), Topology::GHOST);
1192 if (jlat == 0) {
1193 if (y_numbering < 0 && !include_north_pole) {
1194 Topology::set(flags(inode), Topology::NORTH);
1195 }
1196 if (y_numbering > 0 && !include_south_pole) {
1197 Topology::set(flags(inode), Topology::SOUTH);
1198 }
1199 }
1200 if (jlat == rg.ny() - 1) {
1201 if (y_numbering < 0 && !include_south_pole) {
1202 Topology::set(flags(inode), Topology::SOUTH);
1203 }
1204 if (y_numbering > 0 && !include_north_pole) {
1205 Topology::set(flags(inode), Topology::NORTH);
1206 }
1207 }
1208 ++jnode;
1209 }
1210 else {
1211 --l;
1212 }
1213 }
1214 }
1215
1216 if (include_north_pole) {
1217 idx_t inode = node_numbering.at(jnode);
1218 jnorth = jnode;
1219 double y = 90.;
1220 double x = 180.;
1221 xy(inode, XX) = x;
1222 xy(inode, YY) = y;
1223
1224 // geographic coordinates by using projection
1225 double crd[] = {x, y};
1226 rg.projection().xy2lonlat(crd);
1227 lonlat(inode, LON) = crd[LON];
1228 lonlat(inode, LAT) = crd[LAT];
1229
1230 glb_idx(inode) = periodic_glb.at(rg.ny() - 1) + 2;
1231 part(inode) = mypart;
1232 ghost(inode) = 0;
1233 halo(inode) = 0;
1234 Topology::reset(flags(inode));
1235 Topology::set(flags(inode), Topology::NORTH);
1236 ++jnode;
1237 }
1238
1239 if (include_south_pole) {
1240 idx_t inode = node_numbering.at(jnode);
1241 jsouth = jnode;
1242 double y = -90.;
1243 double x = 180.;
1244 xy(inode, XX) = x;
1245 xy(inode, YY) = y;
1246
1247 // geographic coordinates by using projection
1248 double crd[] = {x, y};
1249 rg.projection().xy2lonlat(crd);
1250 lonlat(inode, LON) = crd[LON];
1251 lonlat(inode, LAT) = crd[LAT];
1252
1253 glb_idx(inode) = periodic_glb.at(rg.ny() - 1) + 3;
1254 part(inode) = mypart;
1255 ghost(inode) = 0;
1256 halo(inode) = 0;
1257 Topology::reset(flags(inode));
1258 Topology::set(flags(inode), Topology::SOUTH);
1259 ++jnode;
1260 }
1261 }
1262
1263 mesh.metadata().set<size_t>("nb_nodes_including_halo[0]", nodes.size());
1264 nodes.metadata().set<size_t>("NbRealPts", size_t(nnodes - nnewnodes));
1265 nodes.metadata().set<size_t>("NbVirtualPts", size_t(nnewnodes));
1266 nodes.global_index().metadata().set("human_readable", true);
1267 nodes.global_index().metadata().set("min", 1);
1268 nodes.global_index().metadata().set("max", max_glb_idx);
1269
1270 // Now handle elements
1271 // -------------------
1272
1273 mesh.cells().add(mesh::ElementType::create("Quadrilateral"), nquads);
1274 mesh.cells().add(mesh::ElementType::create("Triangle"), ntriags);
1275
1276 mesh::HybridElements::Connectivity& node_connectivity = mesh.cells().node_connectivity();
1277 array::ArrayView<gidx_t, 1> cells_glb_idx = array::make_view<gidx_t, 1>(mesh.cells().global_index());
1278 array::ArrayView<int, 1> cells_part = array::make_view<int, 1>(mesh.cells().partition());
1279 array::ArrayView<int, 1> cells_flags = array::make_view<int, 1>(mesh.cells().flags());
1280
1281 /*
1282 * label all patch cells a non-patch
1283 */
1284 cells_flags.assign(0);
1285
1286 /*
1287 * Fill in connectivity tables with global node indices first
1288 */
1289 idx_t jcell;
1290 idx_t jquad = 0;
1291 idx_t jtriag = 0;
1292 idx_t quad_begin = mesh.cells().elements(0).begin();
1293 idx_t triag_begin = mesh.cells().elements(1).begin();
1294 idx_t quad_nodes[4];
1295 idx_t triag_nodes[3];
1296
1297 auto fix_quad_orientation = [](idx_t nodes[]) {
1298 idx_t tmp;
1299 tmp = nodes[0];
1300 nodes[0] = nodes[1];
1301 nodes[1] = tmp;
1302 tmp = nodes[2];
1303 nodes[2] = nodes[3];
1304 nodes[3] = tmp;
1305 };
1306 auto fix_triag_orientation = [](idx_t nodes[]) {
1307 idx_t tmp;
1308 tmp = nodes[0];
1309 nodes[0] = nodes[1];
1310 nodes[1] = tmp;
1311 };
1312
1313 bool regular_cells_glb_idx = rg.regular();
1314 if (options.getBool("triangulate") || y_numbering > 0 || rg.y().front() != 90. || rg.y().back() != -90.) {
1315 regular_cells_glb_idx = false;
1316 }
1317
1318 if ((region.nquads + region.ntriags) > 0) {
1319 for (idx_t jlat = region.north; jlat < region.south; ++jlat) {
1320 idx_t ilat = jlat - region.north;
1321 idx_t jlatN = jlat;
1322 idx_t jlatS = jlat + 1;
1323 idx_t ilatN = ilat;
1324 idx_t ilatS = ilat + 1;
1325 for (idx_t jelem = 0; jelem < region.nb_lat_elems.at(jlat); ++jelem) {
1326 const auto elem = array::make_view<int, 3>(*region.elems).slice(ilat, jelem, Range::all());
1327
1328 if (elem(2) >= 0 && elem(3) >= 0) // This is a quad
1329 {
1330 quad_nodes[0] = node_numbering.at(offset_loc.at(ilatN) + elem(0) - region.lat_begin.at(jlatN));
1331 quad_nodes[1] = node_numbering.at(offset_loc.at(ilatS) + elem(1) - region.lat_begin.at(jlatS));
1332 quad_nodes[2] = node_numbering.at(offset_loc.at(ilatS) + elem(2) - region.lat_begin.at(jlatS));
1333 quad_nodes[3] = node_numbering.at(offset_loc.at(ilatN) + elem(3) - region.lat_begin.at(jlatN));
1334
1335 if (three_dimensional && periodic_east_west) {
1336 if (elem(2) == rg.nx(jlatS)) {
1337 quad_nodes[2] = node_numbering.at(offset_loc.at(ilatS));
1338 }
1339 if (elem(3) == rg.nx(jlatN)) {
1340 quad_nodes[3] = node_numbering.at(offset_loc.at(ilatN));
1341 }
1342 }
1343
1344 if (y_numbering > 0) {
1345 fix_quad_orientation(quad_nodes);
1346 }
1347
1348 jcell = quad_begin + jquad++;
1349 node_connectivity.set(jcell, quad_nodes);
1350 cells_glb_idx(jcell) = jcell + 1;
1351 cells_part(jcell) = mypart;
1352 if( regular_cells_glb_idx ) {
1353 cells_glb_idx(jcell) = glb_idx( quad_nodes[0] );
1354 }
1355 }
1356 else // This is a triag
1357 {
1358 if (elem(3) < 0) // This is a triangle pointing up
1359 {
1360 triag_nodes[0] = node_numbering.at(offset_loc.at(ilatN) + elem(0) - region.lat_begin.at(jlatN));
1361 triag_nodes[1] = node_numbering.at(offset_loc.at(ilatS) + elem(1) - region.lat_begin.at(jlatS));
1362 triag_nodes[2] = node_numbering.at(offset_loc.at(ilatS) + elem(2) - region.lat_begin.at(jlatS));
1363 if (three_dimensional && periodic_east_west) {
1364 if (elem(0) == rg.nx(jlatN)) {
1365 triag_nodes[0] = node_numbering.at(offset_loc.at(ilatN));
1366 }
1367 if (elem(2) == rg.nx(jlatS)) {
1368 triag_nodes[2] = node_numbering.at(offset_loc.at(ilatS));
1369 }
1370 }
1371 if (y_numbering > 0) {
1372 fix_triag_orientation(triag_nodes);
1373 }
1374 }
1375 else // This is a triangle pointing down
1376 {
1377 triag_nodes[0] = node_numbering.at(offset_loc.at(ilatN) + elem(0) - region.lat_begin.at(jlatN));
1378 triag_nodes[1] = node_numbering.at(offset_loc.at(ilatS) + elem(1) - region.lat_begin.at(jlatS));
1379 triag_nodes[2] = node_numbering.at(offset_loc.at(ilatN) + elem(3) - region.lat_begin.at(jlatN));
1380 if (three_dimensional && periodic_east_west) {
1381 if (elem(1) == rg.nx(jlatS)) {
1382 triag_nodes[1] = node_numbering.at(offset_loc.at(ilatS));
1383 }
1384 if (elem(3) == rg.nx(jlatN)) {
1385 triag_nodes[2] = node_numbering.at(offset_loc.at(ilatN));
1386 }
1387 }
1388 if (y_numbering > 0) {
1389 fix_triag_orientation(triag_nodes);
1390 }
1391 }
1392 jcell = triag_begin + jtriag++;
1393 node_connectivity.set(jcell, triag_nodes);
1394 cells_glb_idx(jcell) = jcell + 1;
1395 cells_part(jcell) = mypart;
1396 }
1397 }
1398 }
1399
1400 if (include_north_pole) {
1401 idx_t ilat = 0;
1402 idx_t ip1 = 0;
1403 idx_t nlon = rg.nx(0) - (periodic_east_west ? 0 : 1);
1404 for (idx_t ip2 = 0; ip2 < nlon; ++ip2) {
1405 jcell = triag_begin + jtriag++;
1406 idx_t ip3 = ip2 + 1;
1407 if (three_dimensional && ip3 == rg.nx(0)) {
1408 ip3 = 0;
1409 }
1410 triag_nodes[0] = node_numbering.at(jnorth + ip1);
1411 triag_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1412 triag_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1413 if (y_numbering > 0) {
1414 fix_triag_orientation(triag_nodes);
1415 }
1416 node_connectivity.set(jcell, triag_nodes);
1417 cells_glb_idx(jcell) = jcell + 1;
1418 cells_part(jcell) = mypart;
1419 }
1420 }
1421 else if (patch_north_pole) {
1422 idx_t jlat = y_numbering < 0 ? 0 : rg.ny() - 1;
1423 idx_t ilat = y_numbering < 0 ? 0 : std::abs(region.south - region.north);
1424
1425 if (patch_quads) {
1426 idx_t ip1, ip2, ip3, ip4;
1427
1428 idx_t jforward = 0;
1429 idx_t jbackward = rg.nx(jlat) - 1;
1430
1431 while (true) {
1432 ip1 = jforward;
1433 ip2 = jforward + 1;
1434 ip3 = jbackward - 1;
1435 ip4 = jbackward;
1436
1437 quad_nodes[0] = node_numbering.at(offset_loc.at(ilat) + ip1);
1438 quad_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1439 quad_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1440 quad_nodes[3] = node_numbering.at(offset_loc.at(ilat) + ip4);
1441
1442 jcell = quad_begin + jquad++;
1443 node_connectivity.set(jcell, quad_nodes);
1444
1445 cells_glb_idx(jcell) = jcell + 1;
1446 cells_part(jcell) = mypart;
1447 Topology::set(cells_flags(jcell), Topology::PATCH);
1448
1449 if (jcell == cells_glb_idx.size() - 1) {
1450 }
1451 if (jforward + 1 == jbackward) {
1452 break;
1453 }
1454 ++jforward;
1455 --jbackward;
1456 }
1457 }
1458 else {
1459 idx_t ip1, ip2, ip3;
1460
1461 idx_t jforward = 0;
1462 idx_t jbackward = rg.nx(jlat) - 1;
1463 bool forward = true;
1464
1465 while (true) {
1466 if (forward) {
1467 ip1 = jforward;
1468 ip2 = jforward + 1;
1469 ip3 = jbackward;
1470 }
1471 else {
1472 ip1 = jforward;
1473 ip2 = jbackward - 1;
1474 ip3 = jbackward;
1475 }
1476
1477 triag_nodes[0] = node_numbering.at(offset_loc.at(ilat) + ip1);
1478 triag_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1479 triag_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1480 // if ( y_numbering > 0 ) {
1481 // fix_triag_orientation( triag_nodes );
1482 // }
1483
1484 jcell = triag_begin + jtriag++;
1485 node_connectivity.set(jcell, triag_nodes);
1486
1487 cells_glb_idx(jcell) = jcell + 1;
1488 cells_part(jcell) = mypart;
1489 Topology::set(cells_flags(jcell), Topology::PATCH);
1490
1491 if (jbackward == jforward + 2) {
1492 break;
1493 }
1494
1495 if (forward) {
1496 ++jforward;
1497 forward = false;
1498 }
1499 else {
1500 --jbackward;
1501 forward = true;
1502 }
1503 }
1504 }
1505 }
1506
1507 if (include_south_pole) {
1508 idx_t jlat = y_numbering > 0 ? 0 : rg.ny() - 1;
1509 idx_t ilat = y_numbering > 0 ? 0 : std::abs(region.south - region.north);
1510 idx_t ip1 = 0;
1511 idx_t nlon = rg.nx(jlat) + 1 - (periodic_east_west ? 0 : 1);
1512 for (idx_t ip2 = 1; ip2 < nlon; ++ip2) {
1513 jcell = triag_begin + jtriag++;
1514 int ip3 = ip2 - 1;
1515 triag_nodes[0] = node_numbering.at(jsouth + ip1);
1516 triag_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1517 triag_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1518 if (three_dimensional && ip2 == rg.nx(jlat)) {
1519 triag_nodes[1] = node_numbering.at(offset_loc.at(ilat) + 0);
1520 }
1521 if (y_numbering > 0) {
1522 fix_triag_orientation(triag_nodes);
1523 }
1524
1525 node_connectivity.set(jcell, triag_nodes);
1526 cells_glb_idx(jcell) = jcell + 1;
1527 cells_part(jcell) = mypart;
1528 }
1529 }
1530 else if (patch_south_pole) {
1531 idx_t jlat = y_numbering > 0 ? 0 : rg.ny() - 1;
1532 idx_t ilat = y_numbering > 0 ? 0 : std::abs(region.south - region.north);
1533
1534 if (patch_quads) {
1535 idx_t ip1, ip2, ip3, ip4;
1536
1537 idx_t jforward = 0;
1538 idx_t jbackward = rg.nx(jlat) - 1;
1539
1540 while (true) {
1541 ip1 = jbackward;
1542 ip2 = jbackward - 1;
1543 ip3 = jforward + 1;
1544 ip4 = jforward;
1545
1546 quad_nodes[0] = node_numbering.at(offset_loc.at(ilat) + ip1);
1547 quad_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1548 quad_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1549 quad_nodes[3] = node_numbering.at(offset_loc.at(ilat) + ip4);
1550
1551 jcell = quad_begin + jquad++;
1552 node_connectivity.set(jcell, quad_nodes);
1553
1554 cells_glb_idx(jcell) = jcell + 1;
1555 cells_part(jcell) = mypart;
1556 Topology::set(cells_flags(jcell), Topology::PATCH);
1557
1558 if (jcell == cells_glb_idx.size() - 1) {
1559 }
1560 if (jforward + 1 == jbackward) {
1561 break;
1562 }
1563 ++jforward;
1564 --jbackward;
1565 }
1566 }
1567 else {
1568 idx_t ip1, ip2, ip3;
1569
1570 idx_t jforward = 0;
1571 idx_t jbackward = rg.nx(jlat) - 1;
1572 bool forward = true;
1573
1574 while (true) {
1575 if (forward) {
1576 ip1 = jbackward;
1577 ip2 = jforward + 1;
1578 ip3 = jforward;
1579 }
1580 else {
1581 ip1 = jbackward;
1582 ip2 = jbackward - 1;
1583 ip3 = jforward;
1584 }
1585
1586 triag_nodes[0] = node_numbering.at(offset_loc.at(ilat) + ip1);
1587 triag_nodes[1] = node_numbering.at(offset_loc.at(ilat) + ip2);
1588 triag_nodes[2] = node_numbering.at(offset_loc.at(ilat) + ip3);
1589 // if ( y_numbering > 0 ) {
1590 // fix_triag_orientation( triag_nodes );
1591 // }
1592
1593 jcell = triag_begin + jtriag++;
1594 node_connectivity.set(jcell, triag_nodes);
1595
1596 cells_glb_idx(jcell) = jcell + 1;
1597 cells_part(jcell) = mypart;
1598 Topology::set(cells_flags(jcell), Topology::PATCH);
1599
1600 if (jbackward == jforward + 2) {
1601 break;
1602 }
1603
1604 if (forward) {
1605 ++jforward;
1606 forward = false;
1607 }
1608 else {
1609 --jbackward;
1610 forward = true;
1611 }
1612 }
1613 }
1614 }
1615 }
1616 if (regular_cells_glb_idx) {
1617 mesh.cells().global_index().metadata().set("min", 1);
1618 mesh.cells().global_index().metadata().set("max", rg.nx(0) * (rg.ny()-1) );
1619 }
1620 else {
1621 generateGlobalElementNumbering(mesh);
1622 }
1623 }
1624
1625 namespace {
1626 static MeshGeneratorBuilder<StructuredMeshGenerator> __Structured("structured");
1627 } // namespace
1628
1629 } // namespace meshgenerator
1630 } // namespace atlas