Debian Codegraph @

src/atlas/mesh/actions/Build2DCellCentres.cc

raw
1 /*
2 * (C) Crown Copyright 2021 Met Office
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 <limits>
9
10 #include "eckit/types/FloatCompare.h"
11
12 #include "atlas/array/MakeView.h"
13 #include "atlas/field/Field.h"
14 #include "atlas/mesh/HybridElements.h"
15 #include "atlas/mesh/Mesh.h"
16 #include "atlas/mesh/Nodes.h"
17 #include "atlas/mesh/actions/Build2DCellCentres.h"
18 #include "atlas/runtime/Trace.h"
19 #include "atlas/util/CoordinateEnums.h"
20
21 namespace atlas {
22 namespace mesh {
23 namespace actions {
24
25 //----------------------------------------------------------------------------------------------------------------------
26
27 Build2DCellCentres::Build2DCellCentres(const std::string& field_name, bool force_recompute):
28 field_name_(field_name), force_recompute_(force_recompute), flatten_virtual_elements_(true) {}
29
30 Build2DCellCentres::Build2DCellCentres(eckit::Configuration& config):
31 field_name_(config.getString("name", "centre")),
32 force_recompute_(config.getBool("force_recompute", false)),
33 flatten_virtual_elements_(config.getBool("flatten_virtual_elements", true)) {}
34
35 Field& Build2DCellCentres::operator()(Mesh& mesh) const {
36 bool recompute = force_recompute_;
37 if (!mesh.cells().has_field(field_name_)) {
38 mesh.cells().add(Field(field_name_, array::make_datatype<double>(), array::make_shape(mesh.cells().size(), 2)));
39 recompute = true;
40 }
41 if (recompute) {
42 ATLAS_TRACE("Build2DCellCentres");
43 mesh::Nodes& nodes = mesh.nodes();
44 array::ArrayView<double, 2> coords = array::make_view<double, 2>(nodes.field("lonlat"));
45
46 idx_t firstVirtualPoint = std::numeric_limits<idx_t>::max();
47 if (nodes.metadata().has("NbRealPts")) {
48 firstVirtualPoint = nodes.metadata().get<idx_t>("NbRealPts");
49 }
50
51 idx_t nb_cells = mesh.cells().size();
52 std::vector<double> lons(nodes.size());
53 for (idx_t e = 0; e < nodes.size(); ++e) {
54 lons[e] = coords(e, LON);
55 //while (lons[e] > 360.0) { lons[e] -= 360.0; }
56 //while (lons[e] < 0.0) { lons[e] += 360.0; }
57 }
58 auto centroids = array::make_view<double, 2>(mesh.cells().field(field_name_));
59 const mesh::HybridElements::Connectivity& cell_node_connectivity = mesh.cells().node_connectivity();
60
61 for (idx_t e = 0; e < nb_cells; ++e) {
62 centroids(e, LON) = 0.;
63 centroids(e, LAT) = 0.;
64
65 const idx_t nb_cell_nodes = cell_node_connectivity.cols(e);
66
67 // check for degenerate elements (less than three unique nodes)
68 // NOTE: this is not a proper check but it is very robust
69 eckit::types::CompareApproximatelyEqual<double> approx(1.e-9);
70
71 idx_t nb_equal_nodes = 0;
72 for (idx_t ni = 0; ni < nb_cell_nodes - 1; ++ni) {
73 idx_t i = cell_node_connectivity(e, ni);
74 Point2 Pi(lons[i], coords(i, LAT));
75 for (idx_t nj = ni + 1; nj < nb_cell_nodes; ++nj) {
76 idx_t j = cell_node_connectivity(e, nj);
77 Point2 Pj(lons[j], coords(j, LAT));
78 if (approx(Pi[LON], Pj[LON]) && approx(Pi[LAT], Pj[LAT])) {
79 ++nb_equal_nodes;
80 }
81 }
82 }
83
84 idx_t nb_unique_nodes = idx_t(nb_cell_nodes) - nb_equal_nodes;
85 if (nb_unique_nodes < 3) {
86 continue;
87 }
88
89 if (flatten_virtual_elements_) {
90 // calculate centroid by averaging coordinates (uses only "real" nodes)
91 idx_t nb_real_nodes = 0;
92 for (idx_t n = 0; n < nb_cell_nodes; ++n) {
93 const idx_t i = cell_node_connectivity(e, n);
94 if (i < firstVirtualPoint) {
95 ++nb_real_nodes;
96 centroids(e, LON) += lons[i];
97 centroids(e, LAT) += coords(i, LAT);
98 }
99 }
100
101 if (nb_real_nodes > 1) {
102 const double average_coefficient = 1. / static_cast<double>(nb_real_nodes);
103 centroids(e, LON) *= average_coefficient;
104 centroids(e, LAT) *= average_coefficient;
105 }
106 }
107 else {
108 const double average_coefficient = 1. / static_cast<double>(nb_cell_nodes);
109 for (idx_t n = 0; n < nb_cell_nodes; ++n) {
110 const idx_t i = cell_node_connectivity(e, n);
111 centroids(e, LON) += lons[i] * average_coefficient;
112 centroids(e, LAT) += coords(i, LAT) * average_coefficient;
113 }
114 }
115 }
116 }
117 return mesh.cells().field(field_name_);
118 }
119
120 } // namespace actions
121 } // namespace mesh
122 } // namespace atlas