Debian Codegraph @

src/atlas/interpolation/method/cubedsphere/CubedSphereBilinear.cc

raw
1 /*
2 * (C) Crown Copyright 2022 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 "atlas/interpolation/method/cubedsphere/CubedSphereBilinear.h"
9 #include "atlas/functionspace/NodeColumns.h"
10 #include "atlas/grid/CubedSphereGrid.h"
11 #include "atlas/interpolation/method/MethodFactory.h"
12 #include "atlas/interpolation/method/cubedsphere/CellFinder.h"
13 #include "atlas/parallel/omp/omp.h"
14 #include "atlas/util/CoordinateEnums.h"
15
16 namespace atlas {
17 namespace interpolation {
18 namespace method {
19
20 namespace {
21 MethodBuilder<CubedSphereBilinear> __builder("cubedsphere-bilinear");
22 } // namespace
23
24 void CubedSphereBilinear::do_setup(const Grid& source, const Grid& target, const Cache&) {
25 ATLAS_NOTIMPLEMENTED;
26 }
27
28 void CubedSphereBilinear::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache&) {
29 ATLAS_NOTIMPLEMENTED;
30 }
31
32 void CubedSphereBilinear::do_setup(const FunctionSpace& source, const FunctionSpace& target) {
33 source_ = source;
34 target_ = target;
35
36 const auto ncSource = functionspace::NodeColumns(source);
37
38 ATLAS_ASSERT(ncSource);
39 ATLAS_ASSERT(target_);
40
41
42 // return early if no output points on this partition reserve is called on
43 // the triplets but also during the sparseMatrix constructor. This won't
44 // work for empty matrices
45 if (target_.size() == 0) {
46 return;
47 }
48
49 const auto finder = cubedsphere::CellFinder(ncSource.mesh(), util::Config("halo", halo_));
50
51 // Numeric tolerance should scale with N.
52 const auto N = CubedSphereGrid(ncSource.mesh().grid()).N();
53 const auto tolerance = 2. * std::numeric_limits<double>::epsilon() * N;
54
55 // Weights vector is oversized. Empty triplets are ignored by Matrix constructor.
56 auto weights = Triplets(4 * target_.size());
57 const auto ghostView = array::make_view<int, 1>(target_.ghost());
58 const auto lonlatView = array::make_view<double, 2>(target_.lonlat());
59
60 atlas_omp_parallel_for(idx_t i = 0; i < target_.size(); ++i) {
61 if (!ghostView(i)) {
62 const auto cell =
63 finder.getCell(PointLonLat(lonlatView(i, LON), lonlatView(i, LAT)), listSize_, tolerance, tolerance);
64
65 if (!cell.isect) {
66 ATLAS_THROW_EXCEPTION(
67 "Cannot find a cell surrounding target"
68 "point " +
69 std::to_string(i) + ".");
70 }
71
72 const auto& isect = cell.isect;
73 const auto& j = cell.nodes;
74
75 switch (cell.nodes.size()) {
76 case (3): {
77 // Cell is a triangle.
78 weights[4 * i] = Triplet(i, j[0], 1. - isect.u - isect.v);
79 weights[4 * i + 1] = Triplet(i, j[1], isect.u);
80 weights[4 * i + 2] = Triplet(i, j[2], isect.v);
81 break;
82 }
83 case (4): {
84 // Cell is quad.
85 weights[4 * i] = Triplet(i, j[0], (1. - isect.u) * (1. - isect.v));
86 weights[4 * i + 1] = Triplet(i, j[1], isect.u * (1. - isect.v));
87 weights[4 * i + 2] = Triplet(i, j[2], isect.u * isect.v);
88 weights[4 * i + 3] = Triplet(i, j[3], (1. - isect.u) * isect.v);
89 break;
90 }
91 default: {
92 ATLAS_THROW_EXCEPTION("Unknown cell type with " + std::to_string(cell.nodes.size()) + " nodes.");
93 }
94 }
95 }
96 }
97
98 // fill sparse matrix and return.
99 setMatrix(target_.size(), source_.size(), weights);
100 }
101
102 void CubedSphereBilinear::print(std::ostream&) const {
103 ATLAS_NOTIMPLEMENTED;
104 }
105
106 } // namespace method
107 } // namespace interpolation
108 } // namespace atlas