Debian Codegraph @

src/atlas/mesh/actions/ExtendNodesGlobal.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 "atlas/mesh/actions/ExtendNodesGlobal.h"
12
13 #include "atlas/array.h"
14 #include "atlas/field/Field.h"
15 #include "atlas/grid/Grid.h"
16 #include "atlas/grid/Iterator.h"
17 #include "atlas/mesh/Mesh.h"
18 #include "atlas/mesh/Nodes.h"
19 #include "atlas/runtime/Exception.h"
20 #include "atlas/util/CoordinateEnums.h"
21 #include "atlas/util/Earth.h"
22
23 namespace atlas {
24 namespace mesh {
25 namespace actions {
26
27 const std::string ExtendNodesGlobal::DEFAULT_GRIDNAME = "O16";
28
29 ExtendNodesGlobal::ExtendNodesGlobal(const std::string& gridname):
30 gridname_(gridname.empty() ? DEFAULT_GRIDNAME : gridname) {
31 ATLAS_ASSERT(!gridname_.empty());
32 }
33
34 void ExtendNodesGlobal::operator()(const Grid& grid, Mesh& mesh) const {
35 if (grid.domain().global()) {
36 return; // don't add virtual points to global domains
37 }
38
39 Grid extension_grid(gridname_);
40
41 // virtual points
42 std::vector<PointXY> extended_pts;
43 extended_pts.reserve(grid.size());
44
45 // loop over the point and keep the ones that *don't* fall in the domain
46
47 for (const PointLonLat& lonlat : extension_grid.lonlat()) {
48 PointXY xy = grid.projection().xy(lonlat);
49 if (not grid.domain().contains(xy)) {
50 extended_pts.push_back(xy);
51 }
52 }
53
54 mesh::Nodes& nodes = mesh.nodes();
55
56 const idx_t nb_real_pts = nodes.size();
57 const idx_t nb_extension_pts = extended_pts.size();
58
59 idx_t new_size = nodes.size() + extended_pts.size();
60
61 nodes.resize(new_size); // resizes the fields
62
63 const idx_t nb_total_pts = nodes.size();
64
65 ATLAS_ASSERT(nb_total_pts == nb_real_pts + nb_extension_pts);
66
67 nodes.metadata().set<idx_t>("NbRealPts", nb_real_pts);
68 nodes.metadata().set<idx_t>("NbVirtualPts", nb_extension_pts);
69
70 array::ArrayView<double, 2> xyz = array::make_view<double, 2>(nodes.field("xyz"));
71 array::ArrayView<double, 2> xy = array::make_view<double, 2>(nodes.xy());
72 array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(nodes.lonlat());
73 array::ArrayView<gidx_t, 1> gidx = array::make_view<gidx_t, 1>(nodes.global_index());
74 array::ArrayView<int, 1> ghost = array::make_view<int, 1>(nodes.ghost());
75 array::ArrayView<int, 1> partition = array::make_view<int, 1>(nodes.partition());
76 array::ArrayView<int, 1> flags = array::make_view<int, 1>(nodes.flags());
77
78 for (idx_t i = 0; i < nb_extension_pts; ++i) {
79 const idx_t n = nb_real_pts + i;
80 const PointLonLat pLL = grid.projection().lonlat(extended_pts[i]);
81
82 PointXYZ pXYZ;
83 util::Earth::convertSphericalToCartesian(pLL, pXYZ);
84
85 xyz(n, XX) = pXYZ.x();
86 xyz(n, YY) = pXYZ.y();
87 xyz(n, ZZ) = pXYZ.z();
88 xy(n, XX) = extended_pts[i].x();
89 xy(n, YY) = extended_pts[i].y();
90 lonlat(n, LON) = pLL.lon();
91 lonlat(n, LAT) = pLL.lat();
92 gidx(n) = n + 1;
93 ghost(n) = false;
94 partition(n) = 0;
95 util::Topology::view(flags(n)).reset(util::Topology::NONE);
96 // util::Topology::view(flags(n)).set(util::Topology::GHOST);
97 }
98 }
99
100 } // namespace actions
101 } // namespace mesh
102 } // namespace atlas