| 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/interpolation/method/knn/KNearestNeighbours.h" |
| 12 |
|
| 13 |
#include <limits> |
| 14 |
|
| 15 |
#include "eckit/log/Plural.h" |
| 16 |
#include "eckit/types/FloatCompare.h" |
| 17 |
|
| 18 |
#include "atlas/array.h" |
| 19 |
#include "atlas/functionspace/NodeColumns.h" |
| 20 |
#include "atlas/grid.h" |
| 21 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 22 |
#include "atlas/mesh/Nodes.h" |
| 23 |
#include "atlas/mesh/actions/BuildXYZField.h" |
| 24 |
#include "atlas/meshgenerator.h" |
| 25 |
#include "atlas/parallel/mpi/mpi.h" |
| 26 |
#include "atlas/runtime/Exception.h" |
| 27 |
#include "atlas/runtime/Log.h" |
| 28 |
#include "atlas/runtime/Trace.h" |
| 29 |
#include "atlas/util/CoordinateEnums.h" |
| 30 |
|
| 31 |
namespace atlas { |
| 32 |
namespace interpolation { |
| 33 |
namespace method { |
| 34 |
|
| 35 |
namespace { |
| 36 |
|
| 37 |
MethodBuilder<KNearestNeighbours> __builder("k-nearest-neighbours"); |
| 38 |
|
| 39 |
} // namespace |
| 40 |
|
| 41 |
KNearestNeighbours::KNearestNeighbours(const Method::Config& config): KNearestNeighboursBase(config) { |
| 42 |
k_ = 1; |
| 43 |
config.get("k-nearest-neighbours", k_); |
| 44 |
ATLAS_ASSERT(k_); |
| 45 |
} |
| 46 |
|
| 47 |
void KNearestNeighbours::do_setup(const Grid& source, const Grid& target, const Cache&) { |
| 48 |
if (mpi::size() > 1) { |
| 49 |
ATLAS_NOTIMPLEMENTED; |
| 50 |
} |
| 51 |
auto functionspace = [](const Grid& grid) -> FunctionSpace { |
| 52 |
Mesh mesh; |
| 53 |
if (StructuredGrid(grid)) { |
| 54 |
mesh = MeshGenerator("structured", util::Config("3d", true)).generate(grid); |
| 55 |
} |
| 56 |
else { |
| 57 |
mesh = MeshGenerator("delaunay").generate(grid); |
| 58 |
} |
| 59 |
return functionspace::NodeColumns(mesh); |
| 60 |
}; |
| 61 |
|
| 62 |
do_setup(functionspace(source), functionspace(target)); |
| 63 |
} |
| 64 |
|
| 65 |
void KNearestNeighbours::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { |
| 66 |
if (interpolation::MatrixCache(cache)) { |
| 67 |
setMatrix(cache); |
| 68 |
source_ = source; |
| 69 |
target_ = target; |
| 70 |
} |
| 71 |
else { |
| 72 |
do_setup(source, target); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void KNearestNeighbours::do_setup(const FunctionSpace& source, const FunctionSpace& target) { |
| 77 |
source_ = source; |
| 78 |
target_ = target; |
| 79 |
|
| 80 |
buildPointSearchTree(source); |
| 81 |
|
| 82 |
array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(target.lonlat()); |
| 83 |
|
| 84 |
size_t inp_npts = source.size(); |
| 85 |
size_t out_npts = target.size(); |
| 86 |
|
| 87 |
// return early if no output points on this partition reserve is called on |
| 88 |
// the triplets but also during the sparseMatrix constructor. This won't |
| 89 |
// work for empty matrices |
| 90 |
if (out_npts == 0) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// fill the sparse matrix |
| 95 |
std::vector<Triplet> weights_triplets; |
| 96 |
weights_triplets.reserve(out_npts * k_); |
| 97 |
{ |
| 98 |
Trace timer(Here(), "atlas::interpolation::method::KNearestNeighbour::do_setup()"); |
| 99 |
|
| 100 |
std::vector<double> weights; |
| 101 |
|
| 102 |
Log::debug() << "Computing interpolation weights for " << out_npts << " points." << std::endl; |
| 103 |
for (size_t ip = 0; ip < out_npts; ++ip) { |
| 104 |
if (ip && (ip % 1000 == 0)) { |
| 105 |
timer.pause(); |
| 106 |
auto elapsed = timer.elapsed(); |
| 107 |
timer.resume(); |
| 108 |
auto rate = eckit::types::is_approximately_equal(elapsed, 0.) ? std::numeric_limits<double>::infinity() |
| 109 |
: (ip / elapsed); |
| 110 |
Log::debug() << eckit::BigNum(ip) << " (at " << size_t(rate) << " points/s)... after " << elapsed << " s" << std::endl; |
| 111 |
} |
| 112 |
|
| 113 |
// find the closest input points to the output point |
| 114 |
auto nn = pTree_.closestPoints(PointLonLat{lonlat(ip, size_t(LON)), lonlat(ip, size_t(LAT))}, k_); |
| 115 |
|
| 116 |
// calculate weights (individual and total, to normalise) using distance |
| 117 |
// squared |
| 118 |
const size_t npts = nn.size(); |
| 119 |
ATLAS_ASSERT(npts); |
| 120 |
weights.resize(npts, 0); |
| 121 |
|
| 122 |
double sum = 0; |
| 123 |
for (size_t j = 0; j < npts; ++j) { |
| 124 |
const double d = nn[j].distance(); |
| 125 |
const double d2 = d * d; |
| 126 |
|
| 127 |
weights[j] = 1. / (1. + d2); |
| 128 |
sum += weights[j]; |
| 129 |
} |
| 130 |
ATLAS_ASSERT(sum > 0); |
| 131 |
|
| 132 |
// insert weights into the matrix |
| 133 |
for (size_t j = 0; j < npts; ++j) { |
| 134 |
size_t jp = nn[j].payload(); |
| 135 |
ATLAS_ASSERT(jp < inp_npts, |
| 136 |
"point found which is not covered within the halo of the source function space"); |
| 137 |
weights_triplets.emplace_back(ip, jp, weights[j] / sum); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// fill sparse matrix and return |
| 143 |
setMatrix(out_npts, inp_npts, weights_triplets); |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace method |
| 147 |
} // namespace interpolation |
| 148 |
} // namespace atlas |