Debian Codegraph @

src/atlas/mesh/actions/BuildNode2CellConnectivity.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 <cmath>
12 #include <iostream>
13 #include <limits>
14 #include <memory>
15 #include <set>
16 #include <stdexcept>
17
18 #include "BuildNode2CellConnectivity.h"
19
20 #include "atlas/array.h"
21 #include "atlas/array/ArrayView.h"
22 #include "atlas/array/IndexView.h"
23 #include "atlas/domain.h"
24 #include "atlas/field/Field.h"
25 #include "atlas/grid/StructuredGrid.h"
26 #include "atlas/library/config.h"
27 #include "atlas/mesh/ElementType.h"
28 #include "atlas/mesh/Elements.h"
29 #include "atlas/mesh/HybridElements.h"
30 #include "atlas/mesh/Mesh.h"
31 #include "atlas/mesh/Nodes.h"
32 #include "atlas/mesh/actions/BuildEdges.h"
33 #include "atlas/mesh/detail/AccumulateFacets.h"
34 #include "atlas/parallel/mpi/mpi.h"
35 #include "atlas/runtime/Exception.h"
36 #include "atlas/runtime/Log.h"
37 #include "atlas/util/CoordinateEnums.h"
38 #include "atlas/util/LonLatMicroDeg.h"
39 #include "atlas/util/MicroDeg.h"
40 #include "atlas/util/Unique.h"
41
42 using atlas::mesh::detail::accumulate_facets_ordered_by_halo;
43 using Topology = atlas::mesh::Nodes::Topology;
44 using atlas::util::microdeg;
45 using atlas::util::UniqueLonLat;
46
47 namespace atlas {
48 namespace mesh {
49 namespace actions {
50
51 //----------------------------------------------------------------------------------------------------------------------
52
53 namespace { // anonymous
54 struct Sort {
55 Sort() = default;
56 Sort(gidx_t gid, idx_t idx) {
57 g = gid;
58 i = idx;
59 }
60 gidx_t g;
61 idx_t i;
62 bool operator<(const Sort& other) const { return (g < other.g); }
63 };
64 } // anonymous namespace
65
66 void BuildNode2CellConnectivity::operator()() {
67 mesh::Nodes& nodes = mesh_.nodes();
68 const idx_t nb_cells = mesh_.cells().size();
69
70 mesh::Nodes::Connectivity& node_to_cell = nodes.cell_connectivity();
71 node_to_cell.clear();
72
73 const mesh::HybridElements::Connectivity& cell_node_connectivity = mesh_.cells().node_connectivity();
74
75 std::vector<idx_t> to_cell_size(nodes.size(), 0);
76 for (idx_t jcell = 0; jcell < nb_cells; ++jcell) {
77 for (idx_t j = 0; j < cell_node_connectivity.cols(jcell); ++j) {
78 ++to_cell_size[cell_node_connectivity(jcell, j)];
79 }
80 }
81
82 node_to_cell.add(nodes.size(), to_cell_size.data());
83
84 for (idx_t jnode = 0; jnode < nodes.size(); ++jnode) {
85 to_cell_size[jnode] = 0;
86 }
87
88 UniqueLonLat compute_uid(mesh_);
89 std::vector<Sort> cell_sort(nb_cells);
90 for (idx_t jcell = 0; jcell < nb_cells; ++jcell) {
91 cell_sort[jcell] = Sort(compute_uid(cell_node_connectivity.row(jcell)), jcell);
92 }
93
94
95 std::stable_sort(cell_sort.data(), cell_sort.data() + nb_cells);
96
97 for (idx_t jcell = 0; jcell < nb_cells; ++jcell) {
98 idx_t icell = cell_sort[jcell].i;
99 ATLAS_ASSERT(icell < nb_cells);
100 for (idx_t j = 0; j < cell_node_connectivity.cols(icell); ++j) {
101 idx_t node = cell_node_connectivity(icell, j);
102 node_to_cell.set(node, to_cell_size[node]++, icell);
103 }
104 }
105 }
106
107 BuildNode2CellConnectivity::BuildNode2CellConnectivity(Mesh& mesh): mesh_(mesh) {}
108
109
110 //----------------------------------------------------------------------------------------------------------------------
111 // C wrapper interfaces to C++ routines
112
113 extern "C" {
114 void atlas__build_node_to_cell_connectivity(Mesh::Implementation* mesh) {
115 ATLAS_ASSERT(mesh != nullptr, "Cannot access uninitialised atlas_Mesh");
116 auto m = Mesh(mesh);
117 auto build_node_to_cell_connectivity = BuildNode2CellConnectivity(m);
118 build_node_to_cell_connectivity();
119 }
120 }
121
122
123 //----------------------------------------------------------------------------------------------------------------------
124
125 } // namespace actions
126 } // namespace mesh
127 } // namespace atlas