| 1 |
/* |
| 2 |
* (C) Copyright 2024 Meteorologisk Institutt |
| 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 |
*/ |
| 7 |
|
| 8 |
#include "atlas/interpolation/method/structured/RegionalLinear2D.h" |
| 9 |
|
| 10 |
#include <limits> |
| 11 |
|
| 12 |
#include "atlas/array.h" |
| 13 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 14 |
#include "atlas/util/KDTree.h" |
| 15 |
#include "atlas/util/Point.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace interpolation { |
| 19 |
namespace method { |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
using util::FactoryDeprecated; |
| 24 |
|
| 25 |
MethodBuilder<RegionalLinear2D> __builder0("regional-bilinear"); |
| 26 |
MethodBuilder<RegionalLinear2D> __builder1("regional-linear-2d", FactoryDeprecated("Please use regional-bilinear")); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
void RegionalLinear2D::print(std::ostream&) const { ATLAS_NOTIMPLEMENTED; } |
| 31 |
|
| 32 |
void RegionalLinear2D::do_setup(const Grid& source, const Grid& target, |
| 33 |
const Cache&) { |
| 34 |
ATLAS_NOTIMPLEMENTED; |
| 35 |
} |
| 36 |
|
| 37 |
void RegionalLinear2D::do_setup(const FunctionSpace& source, const FunctionSpace& target, |
| 38 |
const Cache&) { |
| 39 |
ATLAS_NOTIMPLEMENTED; |
| 40 |
} |
| 41 |
|
| 42 |
void RegionalLinear2D::do_setup(const FunctionSpace& source, |
| 43 |
const FunctionSpace& target) { |
| 44 |
ATLAS_TRACE("interpolation::method::RegionalLinear2D::do_setup"); |
| 45 |
source_ = source; |
| 46 |
target_ = target; |
| 47 |
|
| 48 |
ASSERT(source_.type() == "StructuredColumns"); |
| 49 |
|
| 50 |
// Get grid parameters |
| 51 |
const functionspace::StructuredColumns sourceFs(source_); |
| 52 |
const RegularGrid sourceGrid(sourceFs.grid()); |
| 53 |
const Projection & sourceProj = sourceGrid.projection(); |
| 54 |
const size_t sourceNx = sourceGrid.nx(); |
| 55 |
const size_t sourceNy = sourceGrid.ny(); |
| 56 |
const double sourceDx = sourceGrid.dx(); |
| 57 |
const double sourceDy = std::abs(sourceGrid.y(1)-sourceGrid.y(0)); |
| 58 |
const bool reversedY = sourceGrid.y(1) < sourceGrid.y(0); |
| 59 |
|
| 60 |
// Check grid regularity in y direction |
| 61 |
for (size_t sourceJ = 0; sourceJ < sourceNy-1; ++sourceJ) { |
| 62 |
if (reversedY) { |
| 63 |
ASSERT(std::abs(sourceGrid.y(sourceJ)-sourceGrid.y(sourceJ+1)-sourceDy) < 1.0e-12*sourceDy); |
| 64 |
} else { |
| 65 |
ASSERT(std::abs(sourceGrid.y(sourceJ+1)-sourceGrid.y(sourceJ)-sourceDy) < 1.0e-12*sourceDy); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Source grid indices |
| 70 |
const auto sourceIndexIView = array::make_indexview<idx_t, 1>(sourceFs.index_i()); |
| 71 |
const auto sourceIndexJView = array::make_indexview<idx_t, 1>(sourceFs.index_j()); |
| 72 |
sourceSize_ = sourceFs.size(); |
| 73 |
|
| 74 |
// Destination grid size |
| 75 |
targetSize_ = target_.size(); |
| 76 |
|
| 77 |
// Ghost points |
| 78 |
const auto sourceGhostView = array::make_view<int, 1>(sourceFs.ghost()); |
| 79 |
const auto targetGhostView = array::make_view<int, 1>(target_.ghost()); |
| 80 |
|
| 81 |
// Define reduced grid horizontal distribution |
| 82 |
std::vector<int> mpiTask(sourceNx*sourceNy, 0); |
| 83 |
for (size_t sourceJnode = 0; sourceJnode < sourceSize_; ++sourceJnode) { |
| 84 |
if (sourceGhostView(sourceJnode) == 0) { |
| 85 |
idx_t idx = sourceIndexIView(sourceJnode)*static_cast<idx_t>(sourceNy)+sourceIndexJView(sourceJnode); |
| 86 |
if (idx < 0 || idx >= mpiTask.size()) { |
| 87 |
throw_OutOfRange("mpiTask", idx, mpiTask.size(), Here()); |
| 88 |
} |
| 89 |
mpiTask[idx] = comm_.rank(); |
| 90 |
} |
| 91 |
} |
| 92 |
comm_.allReduceInPlace(mpiTask.begin(), mpiTask.end(), eckit::mpi::sum()); |
| 93 |
|
| 94 |
// Define local tree on destination grid |
| 95 |
std::vector<Point3> targetPoints; |
| 96 |
std::vector<size_t> targetIndices; |
| 97 |
targetPoints.reserve(targetSize_); |
| 98 |
targetIndices.reserve(targetSize_); |
| 99 |
const auto targetLonLatView = array::make_view<double, 2>(target_.lonlat()); |
| 100 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 101 |
PointLonLat p({targetLonLatView(targetJnode, 0), targetLonLatView(targetJnode, 1)}); |
| 102 |
sourceProj.lonlat2xy(p); |
| 103 |
targetPoints.emplace_back(p[0], p[1], 0.0); |
| 104 |
targetIndices.emplace_back(targetJnode); |
| 105 |
} |
| 106 |
util::IndexKDTree targetTree; |
| 107 |
if (targetSize_ > 0) { |
| 108 |
targetTree.build(targetPoints, targetIndices); |
| 109 |
} |
| 110 |
const double radius = std::sqrt(sourceDx*sourceDx+sourceDy*sourceDy); |
| 111 |
|
| 112 |
// Delta for colocation |
| 113 |
const double eps = 1.0e-8; |
| 114 |
|
| 115 |
// RecvCounts and received points list |
| 116 |
targetRecvCounts_.resize(comm_.size()); |
| 117 |
std::fill(targetRecvCounts_.begin(), targetRecvCounts_.end(), 0); |
| 118 |
std::vector<int> targetRecvPointsList; |
| 119 |
for (size_t sourceJ = 0; sourceJ < sourceNy; ++sourceJ) { |
| 120 |
double yMin, yMax; |
| 121 |
if (reversedY) { |
| 122 |
yMin = sourceJ < sourceNy-1 ? sourceGrid.y(sourceJ+1)-eps : -std::numeric_limits<double>::max(); |
| 123 |
yMax = sourceJ > 0 ? sourceGrid.y(sourceJ-1)+eps : std::numeric_limits<double>::max(); |
| 124 |
} else { |
| 125 |
yMin = sourceJ > 0 ? sourceGrid.y(sourceJ-1)-eps : -std::numeric_limits<double>::max(); |
| 126 |
yMax = sourceJ < sourceNy-1 ? sourceGrid.y(sourceJ+1)+eps : std::numeric_limits<double>::max(); |
| 127 |
} |
| 128 |
for (size_t sourceI = 0; sourceI < sourceNx; ++sourceI) { |
| 129 |
const double xMin = sourceI > 0 ? sourceGrid.x(sourceI-1)-eps : -std::numeric_limits<double>::max(); |
| 130 |
const double xMax = sourceI < sourceNx-1 ? sourceGrid.x(sourceI+1)+eps : |
| 131 |
std::numeric_limits<double>::max(); |
| 132 |
|
| 133 |
bool pointsNeeded = false; |
| 134 |
if (targetSize_ > 0) { |
| 135 |
const Point3 p(sourceGrid.x(sourceI), sourceGrid.y(sourceJ), 0.0); |
| 136 |
const auto list = targetTree.closestPointsWithinRadius(p, radius); |
| 137 |
for (const auto & item : list) { |
| 138 |
const PointXYZ targetPoint = item.point(); |
| 139 |
const size_t targetJnode = item.payload(); |
| 140 |
if (targetGhostView(targetJnode) == 0) { |
| 141 |
const bool inX = (xMin <= targetPoint[0] && targetPoint[0] <= xMax); |
| 142 |
const bool inY = (yMin <= targetPoint[1] && targetPoint[1] <= yMax); |
| 143 |
if (inX && inY) { |
| 144 |
pointsNeeded = true; |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
if (pointsNeeded) { |
| 151 |
++targetRecvCounts_[mpiTask[sourceI*sourceNy+sourceJ]]; |
| 152 |
targetRecvPointsList.push_back(sourceI*sourceNy+sourceJ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// Buffer size |
| 158 |
targetRecvSize_ = targetRecvPointsList.size(); |
| 159 |
|
| 160 |
{ |
| 161 |
// RecvDispls |
| 162 |
targetRecvDispls_.push_back(0); |
| 163 |
for (size_t jt = 0; jt < comm_.size()-1; ++jt) { |
| 164 |
targetRecvDispls_.push_back(targetRecvDispls_[jt]+targetRecvCounts_[jt]); |
| 165 |
} |
| 166 |
|
| 167 |
// Allgather RecvCounts |
| 168 |
eckit::mpi::Buffer<int> targetRecvCountsBuffer(comm_.size()); |
| 169 |
comm_.allGatherv(targetRecvCounts_.begin(), targetRecvCounts_.end(), targetRecvCountsBuffer); |
| 170 |
std::vector<int> targetRecvCountsGlb_ = std::move(targetRecvCountsBuffer.buffer); |
| 171 |
|
| 172 |
// SendCounts |
| 173 |
for (size_t jt = 0; jt < comm_.size(); ++jt) { |
| 174 |
sourceSendCounts_.push_back(targetRecvCountsGlb_[jt*comm_.size()+comm_.rank()]); |
| 175 |
} |
| 176 |
|
| 177 |
// Buffer size |
| 178 |
sourceSendSize_ = 0; |
| 179 |
for (const auto & n : sourceSendCounts_) { |
| 180 |
sourceSendSize_ += n; |
| 181 |
} |
| 182 |
|
| 183 |
// SendDispls |
| 184 |
sourceSendDispls_.push_back(0); |
| 185 |
for (size_t jt = 0; jt < comm_.size()-1; ++jt) { |
| 186 |
sourceSendDispls_.push_back(sourceSendDispls_[jt]+sourceSendCounts_[jt]); |
| 187 |
} |
| 188 |
|
| 189 |
// Ordered received points list |
| 190 |
std::vector<size_t> targetRecvOffset(comm_.size(), 0); |
| 191 |
std::vector<int> targetRecvPointsListOrdered(targetRecvSize_); |
| 192 |
for (size_t jr = 0; jr < targetRecvSize_; ++jr) { |
| 193 |
const size_t sourceI = targetRecvPointsList[jr]/sourceNy; |
| 194 |
const size_t sourceJ = targetRecvPointsList[jr]-sourceI*sourceNy; |
| 195 |
size_t jt = mpiTask[sourceI*sourceNy+sourceJ]; |
| 196 |
size_t jro = targetRecvDispls_[jt]+targetRecvOffset[jt]; |
| 197 |
targetRecvPointsListOrdered[jro] = targetRecvPointsList[jr]; |
| 198 |
++targetRecvOffset[jt]; |
| 199 |
} |
| 200 |
std::vector<int> sourceSentPointsList(sourceSendSize_); |
| 201 |
comm_.allToAllv(targetRecvPointsListOrdered.data(), targetRecvCounts_.data(), targetRecvDispls_.data(), |
| 202 |
sourceSentPointsList.data(), sourceSendCounts_.data(), sourceSendDispls_.data()); |
| 203 |
|
| 204 |
// Sort indices |
| 205 |
std::vector<int> gij; |
| 206 |
for (size_t sourceJnode = 0; sourceJnode < sourceSize_; ++sourceJnode) { |
| 207 |
if (sourceGhostView(sourceJnode) == 0) { |
| 208 |
gij.push_back(sourceIndexIView(sourceJnode)*sourceNy+sourceIndexJView(sourceJnode)); |
| 209 |
} else { |
| 210 |
gij.push_back(-1); |
| 211 |
} |
| 212 |
} |
| 213 |
std::vector<size_t> gidx(sourceSize_); |
| 214 |
std::iota(gidx.begin(), gidx.end(), 0); |
| 215 |
std::stable_sort(gidx.begin(), gidx.end(), [&gij](size_t i1, size_t i2) |
| 216 |
{return gij[i1] < gij[i2];}); |
| 217 |
std::vector<size_t> ridx(sourceSendSize_); |
| 218 |
std::iota(ridx.begin(), ridx.end(), 0); |
| 219 |
std::stable_sort(ridx.begin(), ridx.end(), [&sourceSentPointsList](size_t i1, size_t i2) |
| 220 |
{return sourceSentPointsList[i1] < sourceSentPointsList[i2];}); |
| 221 |
|
| 222 |
// Mapping for sent points |
| 223 |
sourceSendMapping_.resize(sourceSendSize_); |
| 224 |
size_t sourceJnode = 0; |
| 225 |
for (size_t js = 0; js < sourceSendSize_; ++js) { |
| 226 |
while (gij[gidx[sourceJnode]] < sourceSentPointsList[ridx[js]]) { |
| 227 |
++sourceJnode; |
| 228 |
ASSERT(sourceJnode < sourceSize_); |
| 229 |
} |
| 230 |
sourceSendMapping_[ridx[js]] = gidx[sourceJnode]; |
| 231 |
} |
| 232 |
|
| 233 |
// Sort indices |
| 234 |
std::vector<size_t> idx(targetRecvPointsListOrdered.size()); |
| 235 |
std::iota(idx.begin(), idx.end(), 0); |
| 236 |
std::stable_sort(idx.begin(), idx.end(), [&targetRecvPointsListOrdered](size_t i1, size_t i2) |
| 237 |
{return targetRecvPointsListOrdered[i1] < targetRecvPointsListOrdered[i2];}); |
| 238 |
|
| 239 |
// Compute horizontal interpolation |
| 240 |
stencil_.resize(targetSize_); |
| 241 |
weights_.resize(targetSize_); |
| 242 |
stencilSize_.resize(targetSize_); |
| 243 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 244 |
// Interpolation element default values |
| 245 |
if (targetGhostView(targetJnode) == 0) { |
| 246 |
// Destination grid indices |
| 247 |
const double targetX = targetPoints[targetJnode][0]; |
| 248 |
bool colocatedX = false; |
| 249 |
int indexI = -1; |
| 250 |
for (size_t sourceI = 0; sourceI < sourceNx-1; ++sourceI) { |
| 251 |
if (std::abs(targetX-sourceGrid.x(sourceI)) < eps) { |
| 252 |
indexI = sourceI; |
| 253 |
colocatedX = true; |
| 254 |
} |
| 255 |
if (sourceGrid.x(sourceI)+eps < targetX && targetX < sourceGrid.x(sourceI+1)-eps) { |
| 256 |
indexI = sourceI; |
| 257 |
colocatedX = false; |
| 258 |
} |
| 259 |
} |
| 260 |
if (std::abs(targetX-sourceGrid.x(sourceNx-1)) < eps) { |
| 261 |
indexI = sourceNx-1; |
| 262 |
colocatedX = true; |
| 263 |
} |
| 264 |
const double targetY = targetPoints[targetJnode][1]; |
| 265 |
bool colocatedY = false; |
| 266 |
int indexJ = -1; |
| 267 |
for (size_t sourceJ = 0; sourceJ < sourceNy-1; ++sourceJ) { |
| 268 |
if (std::abs(targetY-sourceGrid.y(sourceJ)) < eps) { |
| 269 |
indexJ = sourceJ; |
| 270 |
colocatedY = true; |
| 271 |
} |
| 272 |
if (reversedY) { |
| 273 |
if (sourceGrid.y(sourceJ+1)+eps < targetY && targetY < sourceGrid.y(sourceJ)-eps) { |
| 274 |
indexJ = sourceJ; |
| 275 |
colocatedY = false; |
| 276 |
} |
| 277 |
} else { |
| 278 |
if (sourceGrid.y(sourceJ)+eps < targetY && targetY < sourceGrid.y(sourceJ+1)-eps) { |
| 279 |
indexJ = sourceJ; |
| 280 |
colocatedY = false; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
if (std::abs(targetY-sourceGrid.y(sourceNy-1)) < eps) { |
| 285 |
indexJ = sourceNy-1; |
| 286 |
colocatedY = true; |
| 287 |
} |
| 288 |
|
| 289 |
if (indexI == -1 || indexJ == -1) { |
| 290 |
// Point outside of the domain, using nearest neighbor |
| 291 |
if (indexI > -1) { |
| 292 |
if (!colocatedX && |
| 293 |
(std::abs(targetX-sourceGrid.x(indexI+1)) < std::abs(targetX-sourceGrid.x(indexI)))) { |
| 294 |
indexI += 1; |
| 295 |
} |
| 296 |
} else { |
| 297 |
if (std::abs(targetX-sourceGrid.x(0)) < std::abs(targetX-sourceGrid.x(sourceNx-1))) { |
| 298 |
indexI = 0; |
| 299 |
} else { |
| 300 |
indexI = sourceNx-1; |
| 301 |
} |
| 302 |
} |
| 303 |
if (indexJ > -1) { |
| 304 |
if (!colocatedY && |
| 305 |
(std::abs(targetY-sourceGrid.y(indexJ+1)) < std::abs(targetY-sourceGrid.y(indexJ)))) { |
| 306 |
indexJ += 1; |
| 307 |
} |
| 308 |
} else { |
| 309 |
if (std::abs(targetY-sourceGrid.y(0)) < std::abs(targetY-sourceGrid.y(sourceNy-1))) { |
| 310 |
indexJ = 0; |
| 311 |
} else { |
| 312 |
indexJ = sourceNy-1; |
| 313 |
} |
| 314 |
Log::info() << "WARNING: point outside of the domain" << std::endl; |
| 315 |
} |
| 316 |
|
| 317 |
// Colocated point (actually nearest neighbor) |
| 318 |
colocatedX = true; |
| 319 |
colocatedY = true; |
| 320 |
} |
| 321 |
|
| 322 |
// Bilinear interpolation factor |
| 323 |
const double alphaX = 1.0-(sourceGrid.x(indexI)+sourceDx-targetX)/sourceDx; |
| 324 |
const double alphaY = reversedY ? (sourceGrid.y(indexJ)-targetY)/sourceDy |
| 325 |
: 1.0-(sourceGrid.y(indexJ)+sourceDy-targetY)/sourceDy; |
| 326 |
|
| 327 |
// Points to find |
| 328 |
std::vector<bool> toFind = {true, !colocatedX, !colocatedY, !colocatedX && !colocatedY}; |
| 329 |
std::vector<size_t> valueToFind = {indexI*sourceNy+indexJ, (indexI+1)*sourceNy+indexJ, |
| 330 |
indexI*sourceNy+(indexJ+1), (indexI+1)*sourceNy+(indexJ+1)}; |
| 331 |
std::array<int,4> foundIndex; |
| 332 |
foundIndex.fill(-1); |
| 333 |
|
| 334 |
// Binary search for each point |
| 335 |
for (size_t jj = 0; jj < 4; ++jj) { |
| 336 |
if (toFind[jj]) { |
| 337 |
size_t low = 0; |
| 338 |
size_t high = targetRecvPointsListOrdered.size()-1; |
| 339 |
while (low <= high) { |
| 340 |
size_t mid = low+(high-low)/2; |
| 341 |
if (valueToFind[jj] == static_cast<size_t>(targetRecvPointsListOrdered[idx[mid]])) { |
| 342 |
foundIndex[jj] = idx[mid]; |
| 343 |
break; |
| 344 |
} |
| 345 |
if (valueToFind[jj] > static_cast<size_t>(targetRecvPointsListOrdered[idx[mid]])) { |
| 346 |
low = mid+1; |
| 347 |
} |
| 348 |
if (valueToFind[jj] < static_cast<size_t>(targetRecvPointsListOrdered[idx[mid]])) { |
| 349 |
high = mid-1; |
| 350 |
} |
| 351 |
} |
| 352 |
ASSERT(foundIndex[jj] > -1); |
| 353 |
ASSERT(static_cast<size_t>(targetRecvPointsListOrdered[foundIndex[jj]]) == |
| 354 |
valueToFind[jj]); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// Create interpolation operations |
| 359 |
if (colocatedX && colocatedY) { |
| 360 |
// Colocated point |
| 361 |
stencil_[targetJnode][0] = foundIndex[0]; |
| 362 |
weights_[targetJnode][0] = 1.0; |
| 363 |
stencilSize_[targetJnode] = 1; |
| 364 |
} else if (colocatedY) { |
| 365 |
// Linear interpolation along x |
| 366 |
stencil_[targetJnode][0] = foundIndex[0]; |
| 367 |
weights_[targetJnode][0] = 1.0-alphaX; |
| 368 |
stencil_[targetJnode][1] = foundIndex[1]; |
| 369 |
weights_[targetJnode][1] = alphaX; |
| 370 |
stencilSize_[targetJnode] = 2; |
| 371 |
} else if (colocatedX) { |
| 372 |
// Linear interpolation along y |
| 373 |
stencil_[targetJnode][0] = foundIndex[0]; |
| 374 |
weights_[targetJnode][0] = 1.0-alphaY; |
| 375 |
stencil_[targetJnode][1] = foundIndex[2]; |
| 376 |
weights_[targetJnode][1] = alphaY; |
| 377 |
stencilSize_[targetJnode] = 2; |
| 378 |
} else { |
| 379 |
// Bilinear interpolation |
| 380 |
stencil_[targetJnode][0] = foundIndex[0]; |
| 381 |
weights_[targetJnode][0] = (1.0-alphaX)*(1.0-alphaY); |
| 382 |
stencil_[targetJnode][1] = foundIndex[1]; |
| 383 |
weights_[targetJnode][1] = alphaX*(1.0-alphaY); |
| 384 |
stencil_[targetJnode][2] = foundIndex[2]; |
| 385 |
weights_[targetJnode][2] = (1.0-alphaX)*alphaY; |
| 386 |
stencil_[targetJnode][3] = foundIndex[3]; |
| 387 |
weights_[targetJnode][3] = alphaX*alphaY; |
| 388 |
stencilSize_[targetJnode] = 4; |
| 389 |
} |
| 390 |
} else { |
| 391 |
// Ghost point |
| 392 |
stencilSize_[targetJnode] = 0; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
void RegionalLinear2D::do_execute(const FieldSet& sourceFieldSet, |
| 399 |
FieldSet& targetFieldSet, |
| 400 |
Metadata& metadata) const { |
| 401 |
ATLAS_TRACE("atlas::interpolation::method::RegionalLinear2D::do_execute()"); |
| 402 |
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size()); |
| 403 |
|
| 404 |
for (auto i = 0; i < sourceFieldSet.size(); ++i) { |
| 405 |
do_execute(sourceFieldSet[i], targetFieldSet[i], metadata); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void RegionalLinear2D::do_execute(const Field& sourceField, Field& targetField, |
| 410 |
Metadata&) const { |
| 411 |
ATLAS_TRACE("atlas::interpolation::method::RegionalLinear2D::do_execute()"); |
| 412 |
|
| 413 |
// Check number of levels |
| 414 |
ASSERT(sourceField.levels() == targetField.levels()); |
| 415 |
const size_t nz = sourceField.levels() > 0 ? sourceField.levels() : 1; |
| 416 |
const size_t ndim = sourceField.levels() > 0 ? 2 : 1; |
| 417 |
|
| 418 |
// Scale counts and displs for all levels |
| 419 |
std::vector<int> sourceSendCounts3D(comm_.size()); |
| 420 |
std::vector<int> sourceSendDispls3D(comm_.size()); |
| 421 |
std::vector<int> targetRecvCounts3D(comm_.size()); |
| 422 |
std::vector<int> targetRecvDispls3D(comm_.size()); |
| 423 |
for (size_t jt = 0; jt < comm_.size(); ++jt) { |
| 424 |
sourceSendCounts3D[jt] = sourceSendCounts_[jt]*nz; |
| 425 |
sourceSendDispls3D[jt] = sourceSendDispls_[jt]*nz; |
| 426 |
targetRecvCounts3D[jt] = targetRecvCounts_[jt]*nz; |
| 427 |
targetRecvDispls3D[jt] = targetRecvDispls_[jt]*nz; |
| 428 |
} |
| 429 |
|
| 430 |
// Halo exchange |
| 431 |
haloExchange(sourceField); |
| 432 |
|
| 433 |
// Serialize |
| 434 |
std::vector<double> sourceSendVec(sourceSendSize_*nz); |
| 435 |
if (ndim == 1) { |
| 436 |
const auto sourceView = array::make_view<double, 1>(sourceField); |
| 437 |
for (size_t js = 0; js < sourceSendSize_; ++js) { |
| 438 |
size_t sourceJnode = sourceSendMapping_[js]; |
| 439 |
sourceSendVec[js] = sourceView(sourceJnode); |
| 440 |
} |
| 441 |
} else if (ndim == 2) { |
| 442 |
const auto sourceView = array::make_view<double, 2>(sourceField); |
| 443 |
for (size_t js = 0; js < sourceSendSize_; ++js) { |
| 444 |
for (size_t k = 0; k < nz; ++k) { |
| 445 |
size_t sourceJnode = sourceSendMapping_[js]; |
| 446 |
sourceSendVec[js*nz+k] = sourceView(sourceJnode, k); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
// Communication |
| 452 |
std::vector<double> targetRecvVec(targetRecvSize_*nz); |
| 453 |
comm_.allToAllv(sourceSendVec.data(), sourceSendCounts3D.data(), sourceSendDispls3D.data(), |
| 454 |
targetRecvVec.data(), targetRecvCounts3D.data(), targetRecvDispls3D.data()); |
| 455 |
|
| 456 |
// Interpolation |
| 457 |
if (ndim == 1) { |
| 458 |
auto targetView = array::make_view<double, 1>(targetField); |
| 459 |
targetView.assign(0.0); |
| 460 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 461 |
for (size_t jj = 0; jj < stencilSize_[targetJnode]; ++jj) { |
| 462 |
targetView(targetJnode) += weights_[targetJnode][jj] |
| 463 |
*targetRecvVec[stencil_[targetJnode][jj]]; |
| 464 |
} |
| 465 |
} |
| 466 |
} else if (ndim == 2) { |
| 467 |
auto targetView = array::make_view<double, 2>(targetField); |
| 468 |
targetView.assign(0.0); |
| 469 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 470 |
for (size_t jj = 0; jj < stencilSize_[targetJnode]; ++jj) { |
| 471 |
for (size_t k = 0; k < nz; ++k) { |
| 472 |
targetView(targetJnode, k) += weights_[targetJnode][jj] |
| 473 |
*targetRecvVec[stencil_[targetJnode][jj]*nz+k]; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
// Set target field dirty |
| 480 |
targetField.set_dirty(); |
| 481 |
} |
| 482 |
|
| 483 |
void RegionalLinear2D::do_execute_adjoint(FieldSet& sourceFieldSet, |
| 484 |
const FieldSet& targetFieldSet, |
| 485 |
Metadata& metadata) const { |
| 486 |
ATLAS_TRACE( |
| 487 |
"atlas::interpolation::method::RegionalLinear2D::do_execute_adjoint()"); |
| 488 |
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size()); |
| 489 |
|
| 490 |
for (auto i = 0; i < sourceFieldSet.size(); ++i) { |
| 491 |
do_execute_adjoint(sourceFieldSet[i], targetFieldSet[i], metadata); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
void RegionalLinear2D::do_execute_adjoint(Field& sourceField, |
| 496 |
const Field& targetField, |
| 497 |
Metadata& metadata) const { |
| 498 |
ATLAS_TRACE( |
| 499 |
"atlas::interpolation::method::RegionalLinear2D::do_execute_adjoint()"); |
| 500 |
|
| 501 |
if (targetField.size() == 0) { |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
// Check number of levels |
| 506 |
ASSERT(sourceField.levels() == targetField.levels()); |
| 507 |
const size_t nz = sourceField.levels() > 0 ? sourceField.levels() : 1; |
| 508 |
const size_t ndim = sourceField.levels() > 0 ? 2 : 1; |
| 509 |
|
| 510 |
// Scale counts and displs for all levels |
| 511 |
std::vector<int> sourceSendCounts3D(comm_.size()); |
| 512 |
std::vector<int> sourceSendDispls3D(comm_.size()); |
| 513 |
std::vector<int> targetRecvCounts3D(comm_.size()); |
| 514 |
std::vector<int> targetRecvDispls3D(comm_.size()); |
| 515 |
for (size_t jt = 0; jt < comm_.size(); ++jt) { |
| 516 |
sourceSendCounts3D[jt] = sourceSendCounts_[jt]*nz; |
| 517 |
sourceSendDispls3D[jt] = sourceSendDispls_[jt]*nz; |
| 518 |
targetRecvCounts3D[jt] = targetRecvCounts_[jt]*nz; |
| 519 |
targetRecvDispls3D[jt] = targetRecvDispls_[jt]*nz; |
| 520 |
} |
| 521 |
|
| 522 |
// Copy destination field |
| 523 |
Field targetTmpField = targetField.clone(); |
| 524 |
|
| 525 |
// Interpolation adjoint |
| 526 |
std::vector<double> targetRecvVec(targetRecvSize_*nz, 0.0); |
| 527 |
if (ndim == 1) { |
| 528 |
const auto targetView = array::make_view<double, 1>(targetTmpField); |
| 529 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 530 |
for (size_t jj = 0; jj < stencilSize_[targetJnode]; ++jj) { |
| 531 |
targetRecvVec[stencil_[targetJnode][jj]] += weights_[targetJnode][jj] |
| 532 |
*targetView(targetJnode); |
| 533 |
} |
| 534 |
} |
| 535 |
} else if (ndim == 2) { |
| 536 |
const auto targetView = array::make_view<double, 2>(targetTmpField); |
| 537 |
for (size_t targetJnode = 0; targetJnode < targetSize_; ++targetJnode) { |
| 538 |
for (size_t jj = 0; jj < stencilSize_[targetJnode]; ++jj) { |
| 539 |
for (size_t k = 0; k < nz; ++k) { |
| 540 |
targetRecvVec[stencil_[targetJnode][jj]*nz+k] += weights_[targetJnode][jj] |
| 541 |
*targetView(targetJnode, k); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
// Communication |
| 548 |
std::vector<double> sourceSendVec(sourceSendSize_*nz); |
| 549 |
comm_.allToAllv(targetRecvVec.data(), targetRecvCounts3D.data(), targetRecvDispls3D.data(), |
| 550 |
sourceSendVec.data(), sourceSendCounts3D.data(), sourceSendDispls3D.data()); |
| 551 |
|
| 552 |
// Deserialize |
| 553 |
if (ndim == 1) { |
| 554 |
auto sourceView = array::make_view<double, 1>(sourceField); |
| 555 |
sourceView.assign(0.0); |
| 556 |
for (size_t js = 0; js < sourceSendSize_; ++js) { |
| 557 |
size_t sourceJnode = sourceSendMapping_[js]; |
| 558 |
sourceView(sourceJnode) += sourceSendVec[js]; |
| 559 |
} |
| 560 |
} else if (ndim == 2) { |
| 561 |
auto sourceView = array::make_view<double, 2>(sourceField); |
| 562 |
sourceView.assign(0.0); |
| 563 |
for (size_t js = 0; js < sourceSendSize_; ++js) { |
| 564 |
size_t sourceJnode = sourceSendMapping_[js]; |
| 565 |
for (size_t k = 0; k < nz; ++k) { |
| 566 |
sourceView(sourceJnode, k) += sourceSendVec[js*nz+k]; |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
// Adjoint halo exchange |
| 572 |
adjointHaloExchange(sourceField); |
| 573 |
} |
| 574 |
|
| 575 |
} // namespace method |
| 576 |
} // namespace interpolation |
| 577 |
} // namespace atlas |