| 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. and Interpolation |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "StructuredInterpolation2D.h" |
| 14 |
|
| 15 |
#include <fstream> |
| 16 |
#include <iostream> |
| 17 |
#include <string> |
| 18 |
#include <algorithm> |
| 19 |
#include <sstream> |
| 20 |
#include <iomanip> |
| 21 |
#include <chrono> |
| 22 |
#include <thread> |
| 23 |
|
| 24 |
#include "eckit/filesystem/LocalPathName.h" |
| 25 |
|
| 26 |
#include "atlas/array/ArrayView.h" |
| 27 |
#include "atlas/field/Field.h" |
| 28 |
#include "atlas/field/FieldSet.h" |
| 29 |
#include "atlas/functionspace/NodeColumns.h" |
| 30 |
#include "atlas/functionspace/PointCloud.h" |
| 31 |
#include "atlas/functionspace/StructuredColumns.h" |
| 32 |
#include "atlas/grid/Grid.h" |
| 33 |
#include "atlas/grid/StructuredGrid.h" |
| 34 |
#include "atlas/mesh/Nodes.h" |
| 35 |
#include "atlas/parallel/mpi/mpi.h" |
| 36 |
#include "atlas/parallel/omp/omp.h" |
| 37 |
#include "atlas/runtime/Exception.h" |
| 38 |
#include "atlas/runtime/Log.h" |
| 39 |
#include "atlas/runtime/Trace.h" |
| 40 |
#include "atlas/util/CoordinateEnums.h" |
| 41 |
#include "atlas/util/NormaliseLongitude.h" |
| 42 |
#include "atlas/util/Point.h" |
| 43 |
|
| 44 |
// With nvidia/22.1, following error is encountered during interpolation. |
| 45 |
// NVC++-S-0000-Internal compiler error. BAD sptr in var_refsym 0 |
| 46 |
// It is observed to have been fixed with nvidia/22.11 |
| 47 |
// Disabling OpenMP in this routine for nvidia < 22.11 seems to fix things |
| 48 |
#if defined(__NVCOMPILER) |
| 49 |
#if (__NVCOMPILER_MAJOR__*100) + __NVCOMPILER_MINOR__ < 2211 |
| 50 |
#warning "Disabled OpenMP for StructuredInterpolation2D due to internal compiler error" |
| 51 |
#undef atlas_omp_parallel |
| 52 |
#define atlas_omp_parallel |
| 53 |
#undef atlas_omp_for |
| 54 |
#define atlas_omp_for for |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
namespace atlas { |
| 61 |
namespace interpolation { |
| 62 |
namespace method { |
| 63 |
|
| 64 |
namespace structured2d { |
| 65 |
|
| 66 |
namespace { |
| 67 |
|
| 68 |
inline std::string search_replace(const std::string& in, const std::string& search, const std::string& replace) { |
| 69 |
std::string out = in; |
| 70 |
int pos = out.find(search); |
| 71 |
while (pos != std::string::npos) { |
| 72 |
out.erase(pos, search.length()); |
| 73 |
out.insert(pos, replace); |
| 74 |
pos = out.find(search, pos + replace.length()); |
| 75 |
} |
| 76 |
return out; |
| 77 |
} |
| 78 |
|
| 79 |
inline std::string left_padded_str(int x, int max = 0) { |
| 80 |
auto digits = [](long x) -> long { return std::floor(std::log10(std::max(1l, x))) + 1l; }; |
| 81 |
if (max) { |
| 82 |
std::ostringstream ss; |
| 83 |
ss << std::setw(digits(max)) << std::setfill('0') << x; |
| 84 |
return ss.str(); |
| 85 |
} |
| 86 |
return std::to_string(x); |
| 87 |
} |
| 88 |
|
| 89 |
inline std::string filename( const std::string& path ){ |
| 90 |
return search_replace(path, "%p", left_padded_str(mpi::rank(),mpi::size())); |
| 91 |
} |
| 92 |
|
| 93 |
template <typename LonLat> |
| 94 |
inline std::string to_str (const std::vector<idx_t>& points, LonLat lonlat) { |
| 95 |
std::ostringstream out; |
| 96 |
out << "[\n"; |
| 97 |
for( size_t j=0; j<points.size(); ++j ) { |
| 98 |
PointLonLat p = lonlat(points[j]); |
| 99 |
out << " [" << p.lon() << "," << p.lat() << "]"; |
| 100 |
if( j < points.size() - 1 ) { |
| 101 |
out << ",\n"; |
| 102 |
} |
| 103 |
} |
| 104 |
out << "\n]"; |
| 105 |
return out.str(); |
| 106 |
}; |
| 107 |
|
| 108 |
inline void output_source_partition_polygon(const std::string& path, functionspace::StructuredColumns src_fs, idx_t halo) { |
| 109 |
auto& polygon = src_fs.polygon(halo); |
| 110 |
std::ofstream f(filename(path)); |
| 111 |
f << "[\n " << polygon.json() << "\n]"; |
| 112 |
} |
| 113 |
|
| 114 |
inline void output_all_source_partition_polygons(const std::string& path, functionspace::StructuredColumns src_fs, idx_t halo) { |
| 115 |
auto& polygon = src_fs.polygon(halo); |
| 116 |
util::PartitionPolygons polygons; |
| 117 |
polygon.allGather(polygons); |
| 118 |
if( mpi::rank() == 0 ) { |
| 119 |
std::ofstream f(path); |
| 120 |
f << polygons.json(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
inline void output_source_grid_points(const std::string& path, functionspace::StructuredColumns src_fs, idx_t halo) { |
| 125 |
std::ofstream f(filename(path)); |
| 126 |
f << "[\n"; |
| 127 |
if( halo == 0 ) { |
| 128 |
idx_t size = src_fs.sizeOwned(); |
| 129 |
idx_t n = 0; |
| 130 |
for( idx_t j=src_fs.j_begin(); j<src_fs.j_end(); ++j ) { |
| 131 |
for( idx_t i=src_fs.i_begin(j); i<src_fs.i_end(j); ++i ) { |
| 132 |
auto p = src_fs.compute_xy(i,j); |
| 133 |
f << " [" << p.x() << "," << p.y() << "]"; |
| 134 |
if( (++n) != size ) { |
| 135 |
f << ",\n"; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
else { |
| 141 |
idx_t size = src_fs.sizeHalo(); |
| 142 |
idx_t n = 0; |
| 143 |
for( idx_t j=src_fs.j_begin_halo(); j<src_fs.j_end_halo(); ++j ) { |
| 144 |
for( idx_t i=src_fs.i_begin_halo(j); i<src_fs.i_end_halo(j); ++i ) { |
| 145 |
auto p = src_fs.compute_xy(i,j); |
| 146 |
f << " [" << p.x() << "," << p.y() << "]"; |
| 147 |
if( (++n) != size ) { |
| 148 |
f << ",\n"; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
f << "\n]"; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
template <typename LonLat> |
| 158 |
inline void output_target_points(const std::string& path, const std::vector<idx_t>& points, LonLat lonlat) { |
| 159 |
std::ofstream f(filename(path)); |
| 160 |
f << to_str(points,lonlat); |
| 161 |
} |
| 162 |
|
| 163 |
template <typename LonLat, typename Interpolation> |
| 164 |
inline void handle_failed_points(const Interpolation& interpolation, const std::vector<idx_t>& failed_points, LonLat lonlat ) { |
| 165 |
const auto src_fs = functionspace::StructuredColumns( interpolation.source() ); |
| 166 |
size_t num_failed_points{0}; |
| 167 |
mpi::comm().allReduce(failed_points.size(), num_failed_points, eckit::mpi::sum()); |
| 168 |
if( num_failed_points > 0 ) { |
| 169 |
std::string halo_str = std::to_string(src_fs.halo()); |
| 170 |
output_source_partition_polygon("atlas_source_partition_polygons_halo_0_p%p.json",src_fs,0); |
| 171 |
output_source_partition_polygon("atlas_source_partition_polygons_halo_" + halo_str + "_p%p.json",src_fs,src_fs.halo()); |
| 172 |
output_target_points("atlas_target_failed_points_p%p.json", failed_points, lonlat); |
| 173 |
idx_t my_rank = mpi::rank(); |
| 174 |
for( idx_t p=0; p<mpi::size(); ++p ) { |
| 175 |
if( p == my_rank && failed_points.size() ) { |
| 176 |
Log::error() << "Failed to interpolate " << failed_points.size() << " points on rank " << p |
| 177 |
<< ".\nSee " << filename("atlas_target_failed_points_p%p.json") ; |
| 178 |
if( failed_points.size() <= 20 ) { |
| 179 |
Log::error() << " :\n" << to_str(failed_points, lonlat); |
| 180 |
} |
| 181 |
Log::error() << std::endl; |
| 182 |
} |
| 183 |
mpi::comm().barrier(); |
| 184 |
} |
| 185 |
output_all_source_partition_polygons("atlas_source_partition_polygons_halo_0.json",src_fs,0); |
| 186 |
output_all_source_partition_polygons("atlas_source_partition_polygons_halo_"+halo_str+".json",src_fs,src_fs.halo()); |
| 187 |
// output_source_grid_points("atlas_source_grid_points_halo_0_p%p.json",src_fs,0); |
| 188 |
// output_source_grid_points("atlas_source_grid_points_halo_"+halo_str+"_p%p.json",src_fs,src_fs.halo()); |
| 189 |
{ |
| 190 |
std::ofstream f("atlas_interpolation_info.json"); |
| 191 |
util::Config src_fs_config; |
| 192 |
src_fs_config.set("source.functionspace","StructuredColumns"); |
| 193 |
src_fs_config.set("source.grid",src_fs.grid().spec()); |
| 194 |
src_fs_config.set("source.halo",src_fs.halo()); |
| 195 |
src_fs_config.set("source.distribution",src_fs.distribution()); |
| 196 |
src_fs_config.set("source.partitions",src_fs.nb_parts()); |
| 197 |
src_fs_config.set("interpolation","StructuredInterpolation2D<"+interpolation.kernel().className()+">"); |
| 198 |
f << src_fs_config.json(); |
| 199 |
} |
| 200 |
std::string p_range = "{"+left_padded_str(0,mpi::size()-1)+".."+left_padded_str(mpi::size()-1,mpi::size()-1)+"}"; |
| 201 |
Log::info() << "Dumped files to " << eckit::LocalPathName::cwd() << ": \n" |
| 202 |
<< " atlas_target_failed_points_p" << p_range << ".json\n" |
| 203 |
<< " atlas_source_partition_polygons_halo_0_p" << p_range << ".json\n" |
| 204 |
<< " atlas_source_partition_polygons_halo_0.json\n" |
| 205 |
<< " atlas_source_partition_polygons_halo_" << src_fs.halo() << "_p" << p_range << ".json\n" |
| 206 |
<< " atlas_source_partition_polygons_halo_" << src_fs.halo() << ".json\n" |
| 207 |
<< " atlas_interpolation_info.json\n" |
| 208 |
<< std::endl; |
| 209 |
|
| 210 |
std::ostringstream err; |
| 211 |
err << "StructuredInterpolation2D<" << interpolation.kernel().className() << "> failed for " |
| 212 |
<< num_failed_points << " points with source halo="<<src_fs.halo() |
| 213 |
<< ". Try increasing the source halo. Files have been written for debugging purpose to [" |
| 214 |
<< eckit::LocalPathName::cwd() << "]."; |
| 215 |
ATLAS_THROW_EXCEPTION(err.str()); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
template <typename Kernel> |
| 223 |
double StructuredInterpolation2D<Kernel>::convert_units_multiplier( const Field& field ) { |
| 224 |
std::string units = field.metadata().getString( "units", "degrees" ); |
| 225 |
if ( units == "degrees" ) { |
| 226 |
return 1.; |
| 227 |
} |
| 228 |
if ( units == "radians" ) { |
| 229 |
return 180. / M_PI; |
| 230 |
} |
| 231 |
ATLAS_NOTIMPLEMENTED; |
| 232 |
} |
| 233 |
|
| 234 |
template <typename Kernel> |
| 235 |
StructuredInterpolation2D<Kernel>::StructuredInterpolation2D( const Method::Config& config ) : |
| 236 |
Method( config ), |
| 237 |
verbose_{false}, |
| 238 |
limiter_{false}, |
| 239 |
matrix_free_{false} { |
| 240 |
config.get( "verbose", verbose_ ); |
| 241 |
config.get( "limiter", limiter_ ); |
| 242 |
config.get( "matrix_free", matrix_free_ ); |
| 243 |
if (limiter_ && not matrix_free_) { |
| 244 |
ATLAS_THROW_EXCEPTION("Cannot apply configuration 'limiter=true' and 'matrix_free=false' together"); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
template <typename Kernel> |
| 250 |
void StructuredInterpolation2D<Kernel>::do_setup( const Grid& source, const Grid& target, const Cache& ) { |
| 251 |
ATLAS_TRACE( "StructuredInterpolation2D<" + Kernel::className() + ">::do_setup(Grid source, Grid target)" ); |
| 252 |
if ( mpi::size() > 1 ) { |
| 253 |
ATLAS_NOTIMPLEMENTED; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
ATLAS_ASSERT( StructuredGrid( source ) ); |
| 258 |
FunctionSpace source_fs = |
| 259 |
functionspace::StructuredColumns( source, option::halo( std::max<idx_t>( kernel_->stencil_halo(), 1 ) ) ); |
| 260 |
// guarantee "1" halo for pole treatment! |
| 261 |
FunctionSpace target_fs = functionspace::PointCloud( target ); |
| 262 |
|
| 263 |
do_setup( source_fs, target_fs ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
template <typename Kernel> |
| 268 |
void StructuredInterpolation2D<Kernel>::do_setup( const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { |
| 269 |
ATLAS_TRACE( "StructuredInterpolation2D<" + Kernel::className() + ">::do_setup(FunctionSpace source, FunctionSpace target)" ); |
| 270 |
if (! matrix_free_ && interpolation::MatrixCache(cache)) { |
| 271 |
setMatrix(cache); |
| 272 |
source_ = source; |
| 273 |
target_ = target; |
| 274 |
ATLAS_ASSERT(matrix().rows() == target.size()); |
| 275 |
ATLAS_ASSERT(matrix().cols() == source.size()); |
| 276 |
return; |
| 277 |
} |
| 278 |
else { |
| 279 |
do_setup(source, target); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
template <typename Kernel> |
| 284 |
void StructuredInterpolation2D<Kernel>::do_setup( const FunctionSpace& source, const FunctionSpace& target ) { |
| 285 |
ATLAS_TRACE( "StructuredInterpolation2D<" + Kernel::className() + ">::do_setup(FS source, FS target)" ); |
| 286 |
|
| 287 |
source_ = source; |
| 288 |
target_ = target; |
| 289 |
|
| 290 |
if ( functionspace::NodeColumns tgt = target ) { |
| 291 |
target_lonlat_ = tgt.mesh().nodes().lonlat(); |
| 292 |
target_ghost_ = tgt.mesh().nodes().ghost(); |
| 293 |
} |
| 294 |
else if ( functionspace::PointCloud tgt = target ) { |
| 295 |
target_lonlat_ = tgt.lonlat(); |
| 296 |
target_ghost_ = tgt.ghost(); |
| 297 |
} |
| 298 |
else if ( functionspace::StructuredColumns tgt = target ) { |
| 299 |
target_lonlat_ = tgt.xy(); |
| 300 |
target_ghost_ = tgt.ghost(); |
| 301 |
} |
| 302 |
else { |
| 303 |
throw_NotImplemented( |
| 304 |
"Only interpolation to functionspaces NodeColumns, PointCloud or StructuredColumns are implemented", |
| 305 |
Here() ); |
| 306 |
} |
| 307 |
|
| 308 |
setup( source ); |
| 309 |
} |
| 310 |
|
| 311 |
template <typename Kernel> |
| 312 |
void StructuredInterpolation2D<Kernel>::do_setup( const FunctionSpace& source, const Field& target ) { |
| 313 |
ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_setup(FunctionSpace source, Field target)" ); |
| 314 |
|
| 315 |
source_ = source; |
| 316 |
|
| 317 |
if ( target.functionspace() ) { |
| 318 |
target_ = target.functionspace(); |
| 319 |
} |
| 320 |
|
| 321 |
target_lonlat_ = target; |
| 322 |
|
| 323 |
setup( source ); |
| 324 |
} |
| 325 |
|
| 326 |
template <typename Kernel> |
| 327 |
void StructuredInterpolation2D<Kernel>::do_setup( const FunctionSpace& source, const FieldSet& target ) { |
| 328 |
ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_setup(FunctionSpace source,FieldSet target)" ); |
| 329 |
|
| 330 |
source_ = source; |
| 331 |
|
| 332 |
ATLAS_ASSERT( target.size() >= 2 ); |
| 333 |
if ( target[0].functionspace() ) { |
| 334 |
target_ = target[0].functionspace(); |
| 335 |
} |
| 336 |
|
| 337 |
target_lonlat_fields_ = target; |
| 338 |
|
| 339 |
setup( source ); |
| 340 |
} |
| 341 |
|
| 342 |
template <typename Kernel> |
| 343 |
void StructuredInterpolation2D<Kernel>::print( std::ostream& ) const { |
| 344 |
ATLAS_NOTIMPLEMENTED; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
template <typename Kernel> |
| 349 |
void StructuredInterpolation2D<Kernel>::setup( const FunctionSpace& source ) { |
| 350 |
using namespace structured2d; |
| 351 |
|
| 352 |
kernel_.reset( new Kernel( source, util::Config( "limiter", limiter_ ) ) ); |
| 353 |
|
| 354 |
if ( functionspace::StructuredColumns( source ).halo() < 1 ) { |
| 355 |
throw_Exception( "The source functionspace must have (halo >= 1) for pole treatment" ); |
| 356 |
} |
| 357 |
|
| 358 |
out_npts_ = 0; |
| 359 |
if( target_lonlat_ ) { |
| 360 |
convert_units_ = convert_units_multiplier(target_lonlat_); |
| 361 |
out_npts_ = target_lonlat_.shape( 0 ); |
| 362 |
} |
| 363 |
else if( target_lonlat_fields_.empty() ) { |
| 364 |
convert_units_ = convert_units_multiplier(target_lonlat_fields_[LON]); |
| 365 |
out_npts_ = target_lonlat_fields_[0].shape(0); |
| 366 |
} |
| 367 |
|
| 368 |
if ( not matrix_free_ ) { |
| 369 |
ATLAS_TRACE( "Precomputing interpolation matrix" ); |
| 370 |
|
| 371 |
std::vector<idx_t> failed_points; |
| 372 |
|
| 373 |
auto triplets = kernel_->allocate_triplets( out_npts_ ); |
| 374 |
|
| 375 |
using WorkSpace = typename Kernel::WorkSpace; |
| 376 |
auto interpolate_point = [&]( idx_t n, PointLonLat&& p, WorkSpace& workspace ) -> int { |
| 377 |
try { |
| 378 |
kernel_->insert_triplets( n, p, triplets, workspace ); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
catch(const eckit::Exception& e) {} |
| 382 |
if (verbose_) { |
| 383 |
Log::error() << "Could not interpolate point " << n << " :\t" << p << std::endl; |
| 384 |
} |
| 385 |
return 1; |
| 386 |
}; |
| 387 |
|
| 388 |
auto interpolate_omp = [&failed_points,interpolate_point]( idx_t out_npts, auto lonlat, auto ghost) { |
| 389 |
atlas_omp_parallel { |
| 390 |
WorkSpace workspace; |
| 391 |
atlas_omp_for( idx_t n = 0; n < out_npts; ++n ) { |
| 392 |
if( not ghost(n) ) { |
| 393 |
if (interpolate_point(n, lonlat(n), workspace) != 0) { |
| 394 |
atlas_omp_critical { |
| 395 |
failed_points.emplace_back(n); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
}; |
| 402 |
|
| 403 |
if ( target_lonlat_ ) { |
| 404 |
auto lonlat_view = array::make_view<double, 2>( target_lonlat_ ); |
| 405 |
auto lonlat = [lonlat_view, convert_units = convert_units_] (idx_t n) { |
| 406 |
return PointLonLat{lonlat_view(n,LON) * convert_units, lonlat_view(n,LAT) * convert_units}; |
| 407 |
}; |
| 408 |
|
| 409 |
if( out_npts_ != 0 ) { |
| 410 |
if ( target_ghost_ ) { |
| 411 |
auto ghost = array::make_view<int, 1>( target_ghost_ ); |
| 412 |
interpolate_omp(out_npts_, lonlat, ghost); |
| 413 |
} |
| 414 |
else { |
| 415 |
auto no_ghost = [](idx_t n) { return false; }; |
| 416 |
interpolate_omp(out_npts_, lonlat, no_ghost); |
| 417 |
} |
| 418 |
} |
| 419 |
handle_failed_points(*this, failed_points, lonlat); |
| 420 |
} |
| 421 |
else if ( not target_lonlat_fields_.empty() ) { |
| 422 |
const auto lon = array::make_view<double, 1>( target_lonlat_fields_[LON] ); |
| 423 |
const auto lat = array::make_view<double, 1>( target_lonlat_fields_[LAT] ); |
| 424 |
auto lonlat = [lon, lat, convert_units = convert_units_] (idx_t n) { |
| 425 |
return PointLonLat{lon(n) * convert_units, lat(n) * convert_units}; |
| 426 |
}; |
| 427 |
|
| 428 |
if( out_npts_ != 0) { |
| 429 |
if ( target_ghost_ ) { |
| 430 |
auto ghost = array::make_view<int, 1>( target_ghost_ ); |
| 431 |
interpolate_omp(out_npts_, lonlat, ghost); |
| 432 |
} |
| 433 |
else { |
| 434 |
auto no_ghost = [](idx_t n) { return false; }; |
| 435 |
interpolate_omp(out_npts_, lonlat, no_ghost); |
| 436 |
} |
| 437 |
} |
| 438 |
handle_failed_points(*this, failed_points, lonlat); |
| 439 |
} |
| 440 |
else { |
| 441 |
ATLAS_NOTIMPLEMENTED; |
| 442 |
} |
| 443 |
|
| 444 |
// fill sparse matrix |
| 445 |
if( failed_points.empty() && out_npts_) { |
| 446 |
idx_t inp_npts = source.size(); |
| 447 |
setMatrix(out_npts_, inp_npts, triplets); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
template <typename Kernel> |
| 454 |
void StructuredInterpolation2D<Kernel>::do_execute( const Field& src_field, Field& tgt_field, Metadata& metadata ) const { |
| 455 |
FieldSet tgt( tgt_field ); |
| 456 |
do_execute( FieldSet( src_field ), tgt, metadata ); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
template <typename Kernel> |
| 461 |
void StructuredInterpolation2D<Kernel>::do_execute( const FieldSet& src_fields, FieldSet& tgt_fields, Metadata& metadata ) const { |
| 462 |
if ( not matrix_free_ ) { |
| 463 |
Method::do_execute( src_fields, tgt_fields, metadata ); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
ATLAS_TRACE( "StructuredInterpolation<" + Kernel::className() + ">::do_execute()" ); |
| 468 |
|
| 469 |
const idx_t N = src_fields.size(); |
| 470 |
ATLAS_ASSERT( N == tgt_fields.size() ); |
| 471 |
|
| 472 |
if ( N == 0 ) |
| 473 |
return; |
| 474 |
|
| 475 |
haloExchange( src_fields ); |
| 476 |
|
| 477 |
array::DataType datatype = src_fields[0].datatype(); |
| 478 |
int rank = src_fields[0].rank(); |
| 479 |
|
| 480 |
for ( idx_t i = 0; i < N; ++i ) { |
| 481 |
ATLAS_ASSERT( src_fields[i].datatype() == datatype ); |
| 482 |
ATLAS_ASSERT( src_fields[i].rank() == rank ); |
| 483 |
ATLAS_ASSERT( tgt_fields[i].datatype() == datatype ); |
| 484 |
ATLAS_ASSERT( tgt_fields[i].rank() == rank ); |
| 485 |
} |
| 486 |
|
| 487 |
if ( datatype.kind() == array::DataType::KIND_REAL64 && rank == 1 ) { |
| 488 |
execute_impl<double, 1>( *kernel_, src_fields, tgt_fields ); |
| 489 |
} |
| 490 |
else if ( datatype.kind() == array::DataType::KIND_REAL32 && rank == 1 ) { |
| 491 |
execute_impl<float, 1>( *kernel_, src_fields, tgt_fields ); |
| 492 |
} |
| 493 |
else if ( datatype.kind() == array::DataType::KIND_REAL64 && rank == 2 ) { |
| 494 |
execute_impl<double, 2>( *kernel_, src_fields, tgt_fields ); |
| 495 |
} |
| 496 |
else if ( datatype.kind() == array::DataType::KIND_REAL32 && rank == 2 ) { |
| 497 |
execute_impl<float, 2>( *kernel_, src_fields, tgt_fields ); |
| 498 |
} |
| 499 |
else if ( datatype.kind() == array::DataType::KIND_REAL64 && rank == 3 ) { |
| 500 |
execute_impl<double, 3>( *kernel_, src_fields, tgt_fields ); |
| 501 |
} |
| 502 |
else if ( datatype.kind() == array::DataType::KIND_REAL32 && rank == 3 ) { |
| 503 |
execute_impl<float, 3>( *kernel_, src_fields, tgt_fields ); |
| 504 |
} |
| 505 |
else { |
| 506 |
ATLAS_NOTIMPLEMENTED; |
| 507 |
} |
| 508 |
|
| 509 |
tgt_fields.set_dirty(); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
template <typename Kernel> |
| 514 |
template <typename Value, int Rank> |
| 515 |
void StructuredInterpolation2D<Kernel>::execute_impl( const Kernel& kernel, const FieldSet& src_fields, |
| 516 |
FieldSet& tgt_fields ) const { |
| 517 |
using namespace structured2d; |
| 518 |
|
| 519 |
const idx_t N = src_fields.size(); |
| 520 |
|
| 521 |
std::vector<array::ArrayView<const Value, Rank> > src_view; |
| 522 |
std::vector<array::ArrayView<Value, Rank> > tgt_view; |
| 523 |
src_view.reserve( N ); |
| 524 |
tgt_view.reserve( N ); |
| 525 |
|
| 526 |
for ( idx_t i = 0; i < N; ++i ) { |
| 527 |
src_view.emplace_back( array::make_view<Value, Rank>( src_fields[i] ) ); |
| 528 |
tgt_view.emplace_back( array::make_view<Value, Rank>( tgt_fields[i] ) ); |
| 529 |
} |
| 530 |
|
| 531 |
using WorkSpace = typename Kernel::WorkSpace; |
| 532 |
|
| 533 |
auto interpolate_point = [&]( idx_t n, PointLonLat&& p, WorkSpace& workspace ) -> int { |
| 534 |
try { |
| 535 |
kernel.compute_stencil( p.lon(), p.lat(), workspace.stencil ); |
| 536 |
kernel.compute_weights( p.lon(), p.lat(), workspace.stencil, workspace.weights ); |
| 537 |
kernel.make_valid_stencil( p.lon(), p.lat(), workspace.stencil ); |
| 538 |
for ( idx_t i = 0; i < N; ++i ) { |
| 539 |
kernel.interpolate( workspace.stencil, workspace.weights, src_view[i], tgt_view[i], n ); |
| 540 |
} |
| 541 |
return 0; |
| 542 |
} |
| 543 |
catch(const eckit::Exception& e) {} |
| 544 |
if (verbose_) { |
| 545 |
Log::error() << "Could not interpolate point " << n << " :\t" << p << std::endl; |
| 546 |
} |
| 547 |
return 1; |
| 548 |
}; |
| 549 |
|
| 550 |
std::vector<idx_t> failed_points; |
| 551 |
|
| 552 |
auto interpolate_omp = [&failed_points,interpolate_point]( idx_t out_npts, auto lonlat, auto ghost) { |
| 553 |
atlas_omp_parallel { |
| 554 |
WorkSpace workspace; |
| 555 |
atlas_omp_for( idx_t n = 0; n < out_npts; ++n ) { |
| 556 |
if( not ghost(n) ) { |
| 557 |
if (interpolate_point(n, lonlat(n), workspace) != 0) { |
| 558 |
atlas_omp_critical { |
| 559 |
failed_points.emplace_back(n); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
}; |
| 566 |
|
| 567 |
if ( target_lonlat_ ) { |
| 568 |
auto lonlat_view = array::make_view<double, 2>( target_lonlat_ ); |
| 569 |
auto lonlat = [lonlat_view, convert_units = convert_units_] (idx_t n) { |
| 570 |
return PointLonLat{lonlat_view(n,LON) * convert_units, lonlat_view(n,LAT) * convert_units}; |
| 571 |
}; |
| 572 |
|
| 573 |
if( out_npts_ != 0 ) { |
| 574 |
if ( target_ghost_ ) { |
| 575 |
auto ghost = array::make_view<int, 1>( target_ghost_ ); |
| 576 |
interpolate_omp(out_npts_, lonlat, ghost); |
| 577 |
} |
| 578 |
else { |
| 579 |
auto no_ghost = [](idx_t n) { return false; }; |
| 580 |
interpolate_omp(out_npts_, lonlat, no_ghost); |
| 581 |
} |
| 582 |
} |
| 583 |
handle_failed_points(*this, failed_points, lonlat); |
| 584 |
} |
| 585 |
else if ( not target_lonlat_fields_.empty() ) { |
| 586 |
const auto lon = array::make_view<double, 1>( target_lonlat_fields_[LON] ); |
| 587 |
const auto lat = array::make_view<double, 1>( target_lonlat_fields_[LAT] ); |
| 588 |
auto lonlat = [lon, lat, convert_units = convert_units_] (idx_t n) { |
| 589 |
return PointLonLat{lon(n) * convert_units, lat(n) * convert_units}; |
| 590 |
}; |
| 591 |
|
| 592 |
if( out_npts_ != 0) { |
| 593 |
if ( target_ghost_ ) { |
| 594 |
auto ghost = array::make_view<int, 1>( target_ghost_ ); |
| 595 |
interpolate_omp(out_npts_, lonlat, ghost); |
| 596 |
} |
| 597 |
else { |
| 598 |
auto no_ghost = [](idx_t n) { return false; }; |
| 599 |
interpolate_omp(out_npts_, lonlat, no_ghost); |
| 600 |
} |
| 601 |
} |
| 602 |
handle_failed_points(*this, failed_points, lonlat); |
| 603 |
} |
| 604 |
else { |
| 605 |
ATLAS_NOTIMPLEMENTED; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
} // namespace method |
| 610 |
} // namespace interpolation |
| 611 |
} // namespace atlas |