Debian Codegraph @

src/atlas/meshgenerator/detail/MeshGeneratorImpl.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 <numeric>
12
13 #include "eckit/utils/Hash.h"
14
15 #include "atlas/array/ArrayView.h"
16 #include "atlas/field/Field.h"
17 #include "atlas/grid/Distribution.h"
18 #include "atlas/grid/Grid.h"
19 #include "atlas/mesh/HybridElements.h"
20 #include "atlas/mesh/Mesh.h"
21 #include "atlas/meshgenerator/MeshGenerator.h"
22 #include "atlas/meshgenerator/detail/MeshGeneratorImpl.h"
23 #include "atlas/parallel/mpi/mpi.h"
24
25 using atlas::Mesh;
26
27 namespace atlas {
28 namespace meshgenerator {
29
30 //----------------------------------------------------------------------------------------------------------------------
31
32 MeshGeneratorImpl::MeshGeneratorImpl() = default;
33
34 MeshGeneratorImpl::~MeshGeneratorImpl() = default;
35
36 Mesh MeshGeneratorImpl::operator()(const Grid& grid) const {
37 Mesh mesh;
38 generate(grid, mesh);
39 return mesh;
40 }
41
42 Mesh MeshGeneratorImpl::operator()(const Grid& grid, const grid::Partitioner& partitioner) const {
43 Mesh mesh;
44 generate(grid, partitioner, mesh);
45 return mesh;
46 }
47
48 Mesh MeshGeneratorImpl::operator()(const Grid& grid, const grid::Distribution& distribution) const {
49 Mesh mesh;
50 generate(grid, distribution, mesh);
51 return mesh;
52 }
53
54 Mesh MeshGeneratorImpl::generate(const Grid& grid) const {
55 Mesh mesh;
56 generate(grid, mesh);
57 return mesh;
58 }
59
60 void MeshGeneratorImpl::generate(const Grid& grid, const grid::Partitioner& partitioner, Mesh& mesh) const {
61 generate(grid, grid::Distribution(grid, partitioner), mesh);
62 }
63
64 Mesh MeshGeneratorImpl::generate(const Grid& grid, const grid::Partitioner& partitioner) const {
65 Mesh mesh;
66 generate(grid, partitioner, mesh);
67 return mesh;
68 }
69
70 Mesh MeshGeneratorImpl::generate(const Grid& grid, const grid::Distribution& distribution) const {
71 Mesh mesh;
72 generate(grid, distribution, mesh);
73 return mesh;
74 }
75
76 //----------------------------------------------------------------------------------------------------------------------
77
78 void MeshGeneratorImpl::generateGlobalElementNumbering(Mesh& mesh) const {
79 const auto& comm = mpi::comm(mesh.mpi_comm());
80 idx_t mpi_size = static_cast<idx_t>(comm.size());
81
82 gidx_t loc_nb_elems = mesh.cells().size();
83 std::vector<gidx_t> elem_counts(mpi_size);
84 std::vector<gidx_t> elem_displs(mpi_size);
85
86 ATLAS_TRACE_MPI(ALLGATHER) { comm.allGather(loc_nb_elems, elem_counts.begin(), elem_counts.end()); }
87
88 elem_displs.at(0) = 0;
89 for (idx_t jpart = 1; jpart < mpi_size; ++jpart) {
90 elem_displs.at(jpart) = elem_displs.at(jpart - 1) + elem_counts.at(jpart - 1);
91 }
92
93 gidx_t gid = 1 + elem_displs.at(comm.rank());
94
95 array::ArrayView<gidx_t, 1> glb_idx = array::make_view<gidx_t, 1>(mesh.cells().global_index());
96
97 for (idx_t jelem = 0; jelem < mesh.cells().size(); ++jelem) {
98 glb_idx(jelem) = gid++;
99 }
100
101 gidx_t max_glb_idx = std::accumulate(elem_counts.begin(), elem_counts.end(), gidx_t(0));
102
103 mesh.cells().global_index().metadata().set("human_readable", true);
104 mesh.cells().global_index().metadata().set("min", 1);
105 mesh.cells().global_index().metadata().set("max", max_glb_idx);
106 }
107
108 void MeshGeneratorImpl::setProjection(Mesh& mesh, const Projection& p) const {
109 mesh.setProjection(p);
110 }
111
112 void MeshGeneratorImpl::setGrid(Mesh& mesh, const Grid& g, const grid::Distribution& d) const {
113 mesh.setGrid(g);
114 mesh.metadata().set("distribution", d.type());
115 }
116 void MeshGeneratorImpl::setGrid(Mesh& mesh, const Grid& g, const std::string& d) const {
117 mesh.setGrid(g);
118 mesh.metadata().set("distribution", d);
119 }
120
121 //----------------------------------------------------------------------------------------------------------------------
122
123 } // namespace meshgenerator
124
125 //----------------------------------------------------------------------------------------------------------------------
126
127 } // namespace atlas