| 1 |
/* |
| 2 |
* (C) Copyright 1996- 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. and Interpolation |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "atlas/interpolation/method/knn/GridBoxMethod.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include "eckit/log/Plural.h" |
| 18 |
#include "eckit/log/ProgressTimer.h" |
| 19 |
#include "eckit/types/FloatCompare.h" |
| 20 |
|
| 21 |
#include "atlas/functionspace.h" |
| 22 |
#include "atlas/grid.h" |
| 23 |
#include "atlas/parallel/mpi/mpi.h" |
| 24 |
#include "atlas/runtime/Exception.h" |
| 25 |
#include "atlas/runtime/Log.h" |
| 26 |
#include "atlas/runtime/Trace.h" |
| 27 |
|
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
namespace interpolation { |
| 31 |
namespace method { |
| 32 |
|
| 33 |
|
| 34 |
GridBoxMethod::GridBoxMethod(const Method::Config& config): KNearestNeighboursBase(config) { |
| 35 |
config.get("matrix_free", matrixFree_ = false); |
| 36 |
config.get("fail_early", failEarly_ = true); |
| 37 |
config.get("gaussian_weighted_latitudes", gaussianWeightedLatitudes_ = true); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
GridBoxMethod::~GridBoxMethod() = default; |
| 42 |
|
| 43 |
|
| 44 |
void GridBoxMethod::print(std::ostream& out) const { |
| 45 |
out << "GridBoxMethod[]"; |
| 46 |
} |
| 47 |
|
| 48 |
namespace { |
| 49 |
StructuredGrid extract_structured_grid(const FunctionSpace& fs) { |
| 50 |
if (functionspace::StructuredColumns{fs}) { |
| 51 |
return functionspace::StructuredColumns(fs).grid(); |
| 52 |
} |
| 53 |
if (functionspace::NodeColumns{fs}) { |
| 54 |
return functionspace::NodeColumns(fs).mesh().grid(); |
| 55 |
} |
| 56 |
return Grid(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void GridBoxMethod::do_setup(const FunctionSpace& source, const FunctionSpace& target) { |
| 61 |
if( mpi::size() > 1 ) { |
| 62 |
ATLAS_THROW_EXCEPTION("Cannot use GridBoxMethod in parallel yet."); |
| 63 |
} |
| 64 |
auto src_grid = extract_structured_grid(source); |
| 65 |
auto tgt_grid = extract_structured_grid(target); |
| 66 |
if( not src_grid ) { |
| 67 |
ATLAS_THROW_EXCEPTION("Could not extract StructuredGrid from source function space " << source.type() ); |
| 68 |
} |
| 69 |
if( not tgt_grid ) { |
| 70 |
ATLAS_THROW_EXCEPTION("Could not extract StructuredGrid from target function space " << target.type() ); |
| 71 |
} |
| 72 |
do_setup(src_grid,tgt_grid,Cache()); |
| 73 |
} |
| 74 |
|
| 75 |
bool GridBoxMethod::intersect(size_t i, const GridBox& box, const util::IndexKDTree::ValueList& closest, |
| 76 |
std::vector<eckit::linalg::Triplet>& triplets) const { |
| 77 |
ASSERT(!closest.empty()); |
| 78 |
|
| 79 |
triplets.clear(); |
| 80 |
triplets.reserve(closest.size()); |
| 81 |
|
| 82 |
double area = box.area(); |
| 83 |
ASSERT(area > 0.); |
| 84 |
|
| 85 |
double sumSmallAreas = 0.; |
| 86 |
for (auto& c : closest) { |
| 87 |
auto j = c.payload(); |
| 88 |
ASSERT(j >= 0); |
| 89 |
|
| 90 |
auto smallBox = sourceBoxes_.at(size_t(j)); |
| 91 |
if (box.intersects(smallBox)) { |
| 92 |
double smallArea = smallBox.area(); |
| 93 |
ASSERT(smallArea > 0.); |
| 94 |
|
| 95 |
triplets.emplace_back(i, j, smallArea / area); |
| 96 |
sumSmallAreas += smallArea; |
| 97 |
|
| 98 |
if (eckit::types::is_approximately_equal(area, sumSmallAreas, 1. /*m^2*/)) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (failEarly_) { |
| 105 |
Log::error() << "Failed to intersect grid box " << i << ", " << box << std::endl; |
| 106 |
throw_Exception("Failed to intersect grid box"); |
| 107 |
} |
| 108 |
|
| 109 |
failures_.push_front(i); |
| 110 |
triplets.clear(); |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void GridBoxMethod::do_setup(const Grid& source, const Grid& target, const Cache& cache) { |
| 116 |
ATLAS_TRACE("GridBoxMethod::setup()"); |
| 117 |
|
| 118 |
if (mpi::size() > 1) { |
| 119 |
ATLAS_THROW_EXCEPTION("Not implemented for MPI-parallel runs"); |
| 120 |
} |
| 121 |
|
| 122 |
ATLAS_ASSERT(source); |
| 123 |
ATLAS_ASSERT(target); |
| 124 |
|
| 125 |
functionspace::PointCloud src(source); |
| 126 |
functionspace::PointCloud tgt(target); |
| 127 |
ATLAS_ASSERT(src); |
| 128 |
ATLAS_ASSERT(tgt); |
| 129 |
source_ = src; |
| 130 |
target_ = tgt; |
| 131 |
|
| 132 |
if (not matrixFree_ && interpolation::MatrixCache(cache)) { |
| 133 |
setMatrix(cache); |
| 134 |
ATLAS_ASSERT(matrix().rows() == target.size()); |
| 135 |
ATLAS_ASSERT(matrix().cols() == source.size()); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if (not extractTreeFromCache(cache)) { |
| 140 |
buildPointSearchTree(src); |
| 141 |
} |
| 142 |
|
| 143 |
sourceBoxes_ = GridBoxes(source, gaussianWeightedLatitudes_); |
| 144 |
targetBoxes_ = GridBoxes(target, gaussianWeightedLatitudes_); |
| 145 |
|
| 146 |
searchRadius_ = sourceBoxes_.getLongestGridBoxDiagonal() + targetBoxes_.getLongestGridBoxDiagonal(); |
| 147 |
failures_.clear(); |
| 148 |
|
| 149 |
if (matrixFree_) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
std::vector<Triplet> allTriplets; |
| 154 |
|
| 155 |
{ |
| 156 |
ATLAS_TRACE("GridBoxMethod::setup: intersecting grid boxes"); |
| 157 |
|
| 158 |
constexpr double TIMED = 5.; |
| 159 |
eckit::ProgressTimer progress("Intersecting", targetBoxes_.size(), "grid box", TIMED); |
| 160 |
|
| 161 |
std::vector<Triplet> triplets; |
| 162 |
size_t i = 0; |
| 163 |
for (auto p : tgt.iterate().lonlat()) { |
| 164 |
++progress; |
| 165 |
|
| 166 |
if (intersect(i, targetBoxes_.at(i), pTree_.closestPointsWithinRadius(p, searchRadius_), triplets)) { |
| 167 |
std::copy(triplets.begin(), triplets.end(), std::back_inserter(allTriplets)); |
| 168 |
} |
| 169 |
|
| 170 |
++i; |
| 171 |
} |
| 172 |
|
| 173 |
if (!failures_.empty()) { |
| 174 |
giveUp(failures_); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
{ |
| 179 |
ATLAS_TRACE("GridBoxMethod::setup: build interpolant matrix"); |
| 180 |
setMatrix(targetBoxes_.size(), sourceBoxes_.size(), allTriplets); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void GridBoxMethod::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { |
| 185 |
ATLAS_TRACE("GridBoxMethod::setup()"); |
| 186 |
|
| 187 |
if (not matrixFree_ && interpolation::MatrixCache(cache)) { |
| 188 |
setMatrix(cache); |
| 189 |
source_ = source; |
| 190 |
target_ = target; |
| 191 |
ATLAS_ASSERT(matrix().rows() == target.size()); |
| 192 |
ATLAS_ASSERT(matrix().cols() == source.size()); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
Log::warning() << "Can not create GridBoxMethod from (FunctionSpace, FunctionSpace, Cache). Use (Grid, Grid, Cache)"; |
| 197 |
ATLAS_NOTIMPLEMENTED; |
| 198 |
} |
| 199 |
|
| 200 |
void GridBoxMethod::giveUp(const std::forward_list<size_t>& failures) { |
| 201 |
Log::warning() << "Failed to intersect grid boxes: "; |
| 202 |
|
| 203 |
constexpr int COUNTED = 10; |
| 204 |
|
| 205 |
int count = 0; |
| 206 |
auto sep = ""; |
| 207 |
for (const auto& f : failures) { |
| 208 |
if (count++ < COUNTED) { |
| 209 |
Log::warning() << sep << f; |
| 210 |
sep = ", "; |
| 211 |
} |
| 212 |
} |
| 213 |
Log::warning() << "... (" << eckit::Plural(count, "total failure") << std::endl; |
| 214 |
|
| 215 |
throw_Exception("Failed to intersect grid boxes"); |
| 216 |
} |
| 217 |
|
| 218 |
Cache GridBoxMethod::createCache() const { |
| 219 |
Cache cache; |
| 220 |
cache.add(interpolation::IndexKDTreeCache(pTree_)); |
| 221 |
if (not matrix().empty()) { |
| 222 |
cache.add(Method::createCache()); |
| 223 |
} |
| 224 |
return cache; |
| 225 |
} |
| 226 |
|
| 227 |
} // namespace method |
| 228 |
} // namespace interpolation |
| 229 |
} // namespace atlas |