Debian Codegraph @

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