| 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/NearestNeighbour.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<NearestNeighbour> __builder("nearest-neighbour"); |
| 38 |
|
| 39 |
} // namespace |
| 40 |
|
| 41 |
void NearestNeighbour::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { |
| 42 |
if (interpolation::MatrixCache(cache)) { |
| 43 |
setMatrix(cache); |
| 44 |
source_ = source; |
| 45 |
target_ = target; |
| 46 |
} |
| 47 |
else { |
| 48 |
do_setup(source, target); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void NearestNeighbour::do_setup(const Grid& source, const Grid& target, const Cache&) { |
| 53 |
if (mpi::size() > 1) { |
| 54 |
ATLAS_NOTIMPLEMENTED; |
| 55 |
} |
| 56 |
auto functionspace = [](const Grid& grid) -> FunctionSpace { |
| 57 |
Mesh mesh; |
| 58 |
if (StructuredGrid(grid)) { |
| 59 |
mesh = MeshGenerator("structured", util::Config("3d", true)).generate(grid); |
| 60 |
} |
| 61 |
else { |
| 62 |
mesh = MeshGenerator("delaunay").generate(grid); |
| 63 |
} |
| 64 |
return functionspace::NodeColumns(mesh); |
| 65 |
}; |
| 66 |
|
| 67 |
do_setup(functionspace(source), functionspace(target)); |
| 68 |
} |
| 69 |
|
| 70 |
void NearestNeighbour::do_setup(const FunctionSpace& source, const FunctionSpace& target) { |
| 71 |
source_ = source; |
| 72 |
target_ = target; |
| 73 |
|
| 74 |
// build point-search tree |
| 75 |
buildPointSearchTree(source); |
| 76 |
|
| 77 |
array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(target.lonlat()); |
| 78 |
|
| 79 |
size_t inp_npts = source.size(); |
| 80 |
size_t out_npts = target.size(); |
| 81 |
|
| 82 |
// return early if no output points on this partition reserve is called on |
| 83 |
// the triplets but also during the sparseMatrix constructor. This won't |
| 84 |
// work for empty matrices |
| 85 |
if (out_npts == 0) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// fill the sparse matrix |
| 90 |
std::vector<Triplet> weights_triplets; |
| 91 |
weights_triplets.reserve(out_npts); |
| 92 |
{ |
| 93 |
Trace timer(Here(), "atlas::interpolation::method::NearestNeighbour::do_setup()"); |
| 94 |
for (size_t ip = 0; ip < out_npts; ++ip) { |
| 95 |
if (ip && (ip % 1000 == 0)) { |
| 96 |
timer.pause(); |
| 97 |
auto elapsed = timer.elapsed(); |
| 98 |
timer.resume(); |
| 99 |
auto rate = eckit::types::is_approximately_equal(elapsed, 0.) ? std::numeric_limits<double>::infinity() |
| 100 |
: (ip / elapsed); |
| 101 |
Log::debug() << eckit::BigNum(ip) << " (at " << rate << " points/s)... after " << elapsed << " s" << std::endl; |
| 102 |
} |
| 103 |
|
| 104 |
// find the closest input point to the output point |
| 105 |
auto nn = pTree_.closestPoint(PointLonLat{lonlat(ip, size_t(LON)), lonlat(ip, size_t(LAT))}); |
| 106 |
size_t jp = nn.payload(); |
| 107 |
|
| 108 |
// insert the weights into the interpolant matrix |
| 109 |
ATLAS_ASSERT(jp < inp_npts, |
| 110 |
"point found which is not covered within the halo of the source function space"); |
| 111 |
weights_triplets.emplace_back(ip, jp, 1); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// fill sparse matrix and return |
| 116 |
setMatrix( out_npts, inp_npts, weights_triplets ); |
| 117 |
} |
| 118 |
|
| 119 |
} // namespace method |
| 120 |
} // namespace interpolation |
| 121 |
} // namespace atlas |