Debian Codegraph @

src/atlas/mesh/actions/BuildStatistics.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 // file deepcode ignore MissingOpenCheckOnFile: False positive
12
13 #include <algorithm>
14 #include <cmath>
15 #include <fstream>
16 #include <iomanip>
17 #include <iostream>
18 #include <limits>
19 #include <set>
20 #include <stdexcept>
21
22 #include "eckit/exception/Exceptions.h"
23 #include "eckit/filesystem/PathName.h"
24
25 #include "atlas/array/ArrayView.h"
26 #include "atlas/array/MakeView.h"
27 #include "atlas/field/Field.h"
28 #include "atlas/library/config.h"
29 #include "atlas/mesh/ElementType.h"
30 #include "atlas/mesh/Elements.h"
31 #include "atlas/mesh/HybridElements.h"
32 #include "atlas/mesh/Mesh.h"
33 #include "atlas/mesh/Nodes.h"
34 #include "atlas/mesh/actions/BuildDualMesh.h"
35 #include "atlas/parallel/Checksum.h"
36 #include "atlas/runtime/Exception.h"
37 #include "atlas/util/Constants.h"
38 #include "atlas/util/CoordinateEnums.h"
39 #include "atlas/util/Earth.h"
40 #include "atlas/util/Point.h"
41 #include "atlas/util/UnitSphere.h"
42
43 namespace atlas {
44 namespace mesh {
45 namespace actions {
46
47 namespace {
48
49 void tri_quality(double& eta, double& rho, const PointLonLat& p1, const PointLonLat& p2, const PointLonLat& p3) {
50 // see
51 // http://www.gidhome.com/component/manual/referencemanual/preprocessing/mesh_menu/mesh_quality
52
53 double l12 = util::Constants::radiansToDegrees() * util::Earth::centralAngle(p1, p2);
54 double l23 = util::Constants::radiansToDegrees() * util::Earth::centralAngle(p2, p3);
55 double l31 = util::Constants::radiansToDegrees() * util::Earth::centralAngle(p3, p1);
56
57 double s = 0.5 * (l12 + l23 + l31);
58 double area = std::sqrt(s * (s - l12) * (s - l23) * (s - l31));
59
60 eta = (4 * area * std::sqrt(3.)) / (std::pow(l12, 2) + std::pow(l23, 2) + std::pow(l31, 2));
61
62 double min_length = std::min(std::min(l12, l23), l31);
63 double max_length = std::max(std::max(l12, l23), l31);
64
65 rho = min_length / max_length;
66 }
67
68 void quad_quality(double& eta, double& rho, const PointLonLat& p1, const PointLonLat& p2, const PointLonLat& p3,
69 const PointLonLat& p4) {
70 // see http://geuz.org/gmsh/doc/preprints/gmsh_quad_preprint.pdf
71
72 PointXYZ xyz[4];
73 util::UnitSphere::convertSphericalToCartesian(p1, xyz[0]);
74 util::UnitSphere::convertSphericalToCartesian(p2, xyz[1]);
75 util::UnitSphere::convertSphericalToCartesian(p3, xyz[2]);
76 util::UnitSphere::convertSphericalToCartesian(p4, xyz[3]);
77
78 PointXYZ l2m1(PointXYZ::sub(xyz[1], xyz[0]));
79 PointXYZ l3m2(PointXYZ::sub(xyz[2], xyz[1]));
80 PointXYZ l4m3(PointXYZ::sub(xyz[3], xyz[2]));
81 PointXYZ l1m4(PointXYZ::sub(xyz[0], xyz[3]));
82
83 double norm_l2m1 = PointXYZ::norm(l2m1);
84 double norm_l3m2 = PointXYZ::norm(l3m2);
85 double norm_l4m3 = PointXYZ::norm(l4m3);
86 double norm_l1m4 = PointXYZ::norm(l1m4);
87
88 double dot_l4m1_l2m1 = -PointXYZ::dot(l1m4, l2m1);
89 double dot_l1m2_l3m2 = -PointXYZ::dot(l2m1, l3m2);
90 double dot_l2m3_l4m3 = -PointXYZ::dot(l3m2, l4m3);
91 double dot_l3m4_l1m4 = -PointXYZ::dot(l4m3, l1m4);
92
93 // Angles at each quad corner
94 double a1 = std::acos(dot_l4m1_l2m1 / (norm_l1m4 * norm_l2m1));
95 double a2 = std::acos(dot_l1m2_l3m2 / (norm_l2m1 * norm_l3m2));
96 double a3 = std::acos(dot_l2m3_l4m3 / (norm_l3m2 * norm_l4m3));
97 double a4 = std::acos(dot_l3m4_l1m4 / (norm_l4m3 * norm_l1m4));
98
99 double max_inner = std::max(std::max(std::max(std::abs(M_PI_2 - a1), std::abs(M_PI_2 - a2)), std::abs(M_PI_2 - a3)),
100 std::abs(M_PI_2 - a4));
101
102 eta = std::max(1. - M_2_PI * max_inner, 0.);
103
104 double l12 = util::Earth::centralAngle(p1, p2);
105 double l23 = util::Earth::centralAngle(p2, p3);
106 double l34 = util::Earth::centralAngle(p3, p4);
107 double l41 = util::Earth::centralAngle(p4, p1);
108
109 double min_length = std::min(std::min(std::min(l12, l23), l34), l41);
110 double max_length = std::max(std::max(std::max(l12, l23), l34), l41);
111
112 rho = min_length / max_length;
113 }
114
115 } // namespace
116
117 void build_statistics(Mesh& mesh) {
118 mesh::Nodes& nodes = mesh.nodes();
119 array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(nodes.lonlat());
120
121 if (mesh.edges().size()) {
122 if (!mesh.edges().has_field("arc_length")) {
123 mesh.edges().add(
124 Field("arc_length", array::make_datatype<double>(), array::make_shape(mesh.edges().size())));
125 }
126 array::ArrayView<double, 1> dist = array::make_view<double, 1>(mesh.edges().field("arc_length"));
127 const mesh::HybridElements::Connectivity& edge_nodes = mesh.edges().node_connectivity();
128
129 const int nb_edges = mesh.edges().size();
130 for (int jedge = 0; jedge < nb_edges; ++jedge) {
131 int ip1 = edge_nodes(jedge, 0);
132 int ip2 = edge_nodes(jedge, 1);
133 PointLonLat p1(lonlat(ip1, LON), lonlat(ip1, LAT));
134 PointLonLat p2(lonlat(ip2, LON), lonlat(ip2, LAT));
135 dist(jedge) = util::Earth::distance(p1, p2) * 1e-3;
136 }
137 }
138
139 std::ofstream ofs;
140 eckit::PathName stats_path("stats.txt");
141 int idt = 10;
142 if (mpi::size() == 1) {
143 ofs.open(stats_path.localPath(), std::ofstream::out);
144 if (!ofs.is_open()) {
145 throw eckit::CantOpenFile(stats_path);
146 }
147 ofs << "# STATISTICS rho (min_length/max_length), eta (quality) \n";
148 ofs << std::setw(idt) << "# rho";
149 ofs << std::setw(idt) << "eta";
150 ofs << "\n";
151 }
152
153 // Cell statistics
154 {
155 array::ArrayView<double, 1> rho = array::make_view<double, 1>(mesh.cells().add(
156 Field("stats_rho", array::make_datatype<double>(), array::make_shape(mesh.cells().size()))));
157 array::ArrayView<double, 1> eta = array::make_view<double, 1>(mesh.cells().add(
158 Field("stats_eta", array::make_datatype<double>(), array::make_shape(mesh.cells().size()))));
159
160 for (idx_t jtype = 0; jtype < mesh.cells().nb_types(); ++jtype) {
161 const mesh::Elements& elements = mesh.cells().elements(jtype);
162 const BlockConnectivity& elem_nodes = elements.node_connectivity();
163 const idx_t nb_elems = elements.size();
164
165 if (elements.element_type().name() == "Triangle") {
166 for (idx_t jelem = 0; jelem < nb_elems; ++jelem) {
167 idx_t ielem = elements.begin() + jelem;
168 idx_t ip1 = elem_nodes(jelem, 0);
169 idx_t ip2 = elem_nodes(jelem, 1);
170 idx_t ip3 = elem_nodes(jelem, 2);
171 PointLonLat p1(lonlat(ip1, LON), lonlat(ip1, LAT));
172 PointLonLat p2(lonlat(ip2, LON), lonlat(ip2, LAT));
173 PointLonLat p3(lonlat(ip3, LON), lonlat(ip3, LAT));
174
175 tri_quality(eta(ielem), rho(ielem), p1, p2, p3);
176
177 if (mpi::size() == 1) {
178 ofs << std::setw(idt) << rho(ielem) << std::setw(idt) << eta(ielem) << "\n";
179 }
180 }
181 }
182 if (elements.element_type().name() == "Quadrilateral") {
183 for (idx_t jelem = 0; jelem < nb_elems; ++jelem) {
184 idx_t ielem = elements.begin() + jelem;
185 idx_t ip1 = elem_nodes(jelem, 0);
186 idx_t ip2 = elem_nodes(jelem, 1);
187 idx_t ip3 = elem_nodes(jelem, 2);
188 idx_t ip4 = elem_nodes(jelem, 3);
189
190 PointLonLat p1(lonlat(ip1, LON), lonlat(ip1, LAT));
191 PointLonLat p2(lonlat(ip2, LON), lonlat(ip2, LAT));
192 PointLonLat p3(lonlat(ip3, LON), lonlat(ip3, LAT));
193 PointLonLat p4(lonlat(ip4, LON), lonlat(ip4, LAT));
194
195 quad_quality(eta(ielem), rho(ielem), p1, p2, p3, p4);
196
197 if (mpi::size() == 1) {
198 ofs << std::setw(idt) << rho(ielem) << std::setw(idt) << eta(ielem) << "\n";
199 }
200 }
201 }
202 }
203 }
204
205 eckit::PathName dual_stats_path("dual_stats.txt");
206 if (mpi::size() == 1) {
207 ofs << "# STATISTICS dual_area \n";
208 ofs << std::setw(idt) << "# area";
209 ofs << "\n";
210 }
211
212 if (nodes.has_field("dual_volumes")) {
213 array::ArrayView<double, 1> dual_volumes = array::make_view<double, 1>(nodes.field("dual_volumes"));
214 array::ArrayView<double, 1> dual_delta_sph = array::make_view<double, 1>(
215 nodes.add(Field("dual_delta_sph", array::make_datatype<double>(), array::make_shape(nodes.size(), 1))));
216
217 for (idx_t jnode = 0; jnode < nodes.size(); ++jnode) {
218 const double lat = util::Constants::degreesToRadians() * lonlat(jnode, LAT);
219 const double hx = util::Constants::degreesToRadians() * util::Earth::radius() * std::cos(lat);
220 const double hy = util::Constants::degreesToRadians() * util::Earth::radius();
221 dual_delta_sph(jnode) = std::sqrt(dual_volumes(jnode) * hx * hy);
222 }
223
224 if (mpi::size() == 1) {
225 for (idx_t jnode = 0; jnode < nodes.size(); ++jnode) {
226 ofs << std::setw(idt) << dual_delta_sph(jnode) << "\n";
227 }
228 }
229 }
230 if (mpi::size() == 1) {
231 ofs.close();
232 }
233 }
234
235 // ------------------------------------------------------------------
236 // C wrapper interfaces to C++ routines
237
238 void atlas__build_statistics(Mesh::Implementation* mesh) {
239 ATLAS_ASSERT(mesh != nullptr, "Cannot access uninitialised atlas_Mesh");
240 Mesh m(mesh);
241 build_statistics(m);
242 }
243
244 // ------------------------------------------------------------------
245
246 } // namespace actions
247 } // namespace mesh
248 } // namespace atlas