/* * (C) Copyright 2013 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #include "atlas/interpolation/method/knn/NearestNeighbour.h" #include #include "eckit/log/Plural.h" #include "eckit/types/FloatCompare.h" #include "atlas/array.h" #include "atlas/functionspace/NodeColumns.h" #include "atlas/grid.h" #include "atlas/interpolation/method/MethodFactory.h" #include "atlas/mesh/Nodes.h" #include "atlas/mesh/actions/BuildXYZField.h" #include "atlas/meshgenerator.h" #include "atlas/parallel/mpi/mpi.h" #include "atlas/runtime/Exception.h" #include "atlas/runtime/Log.h" #include "atlas/runtime/Trace.h" #include "atlas/util/CoordinateEnums.h" namespace atlas { namespace interpolation { namespace method { namespace { MethodBuilder __builder("nearest-neighbour"); } // namespace void NearestNeighbour::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { if (interpolation::MatrixCache(cache)) { setMatrix(cache); source_ = source; target_ = target; } else { do_setup(source, target); } } void NearestNeighbour::do_setup(const Grid& source, const Grid& target, const Cache&) { if (mpi::size() > 1) { ATLAS_NOTIMPLEMENTED; } auto functionspace = [](const Grid& grid) -> FunctionSpace { Mesh mesh; if (StructuredGrid(grid)) { mesh = MeshGenerator("structured", util::Config("3d", true)).generate(grid); } else { mesh = MeshGenerator("delaunay").generate(grid); } return functionspace::NodeColumns(mesh); }; do_setup(functionspace(source), functionspace(target)); } void NearestNeighbour::do_setup(const FunctionSpace& source, const FunctionSpace& target) { source_ = source; target_ = target; // build point-search tree buildPointSearchTree(source); array::ArrayView lonlat = array::make_view(target.lonlat()); size_t inp_npts = source.size(); size_t out_npts = target.size(); // return early if no output points on this partition reserve is called on // the triplets but also during the sparseMatrix constructor. This won't // work for empty matrices if (out_npts == 0) { return; } // fill the sparse matrix std::vector weights_triplets; weights_triplets.reserve(out_npts); { Trace timer(Here(), "atlas::interpolation::method::NearestNeighbour::do_setup()"); for (size_t ip = 0; ip < out_npts; ++ip) { if (ip && (ip % 1000 == 0)) { timer.pause(); auto elapsed = timer.elapsed(); timer.resume(); auto rate = eckit::types::is_approximately_equal(elapsed, 0.) ? std::numeric_limits::infinity() : (ip / elapsed); Log::debug() << eckit::BigNum(ip) << " (at " << rate << " points/s)... after " << elapsed << " s" << std::endl; } // find the closest input point to the output point auto nn = pTree_.closestPoint(PointLonLat{lonlat(ip, size_t(LON)), lonlat(ip, size_t(LAT))}); size_t jp = nn.payload(); // insert the weights into the interpolant matrix ATLAS_ASSERT(jp < inp_npts, "point found which is not covered within the halo of the source function space"); weights_triplets.emplace_back(ip, jp, 1); } } // fill sparse matrix and return setMatrix( out_npts, inp_npts, weights_triplets ); } } // namespace method } // namespace interpolation } // namespace atlas