| 1 |
/* |
| 2 |
* (C) Crown Copyright 2024 Met Office |
| 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 <cmath> |
| 9 |
#include <variant> |
| 10 |
|
| 11 |
#include "atlas/interpolation/method/sphericalvector/SphericalVector.h" |
| 12 |
|
| 13 |
#include "atlas/array/ArrayView.h" |
| 14 |
#include "atlas/field/Field.h" |
| 15 |
#include "atlas/field/FieldSet.h" |
| 16 |
#include "atlas/interpolation/Cache.h" |
| 17 |
#include "atlas/interpolation/Interpolation.h" |
| 18 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 19 |
#include "atlas/interpolation/method/sphericalvector/ComplexMatrixMultiply.h" |
| 20 |
#include "atlas/interpolation/method/sphericalvector/Types.h" |
| 21 |
#include "atlas/linalg/sparse/MakeEckitSparseMatrix.h" |
| 22 |
#include "atlas/option/Options.h" |
| 23 |
#include "atlas/parallel/omp/omp.h" |
| 24 |
#include "atlas/runtime/Exception.h" |
| 25 |
#include "atlas/runtime/Trace.h" |
| 26 |
#include "atlas/util/Constants.h" |
| 27 |
#include "atlas/util/Geometry.h" |
| 28 |
#include "atlas/util/PackVectorFields.h" |
| 29 |
#include "eckit/config/LocalConfiguration.h" |
| 30 |
|
| 31 |
namespace atlas { |
| 32 |
namespace interpolation { |
| 33 |
namespace method { |
| 34 |
|
| 35 |
namespace { |
| 36 |
MethodBuilder<SphericalVector> __builder("spherical-vector"); |
| 37 |
} |
| 38 |
|
| 39 |
using namespace detail; |
| 40 |
|
| 41 |
using WeightsMatMul = detail::ComplexMatrixMultiply<true>; |
| 42 |
using WeightsMatMulAdjoint = detail::ComplexMatrixMultiply<false>; |
| 43 |
|
| 44 |
SphericalVector::SphericalVector(const Config& config) : Method(config) { |
| 45 |
const auto* conf = dynamic_cast<const eckit::LocalConfiguration*>(&config); |
| 46 |
ATLAS_ASSERT(conf, "config must be derived from eckit::LocalConfiguration"); |
| 47 |
interpolationScheme_ = conf->getSubConfiguration("scheme"); |
| 48 |
adjoint_ = conf->getBool("adjoint", false); |
| 49 |
} |
| 50 |
|
| 51 |
void SphericalVector::do_setup(const Grid& source, const Grid& target, |
| 52 |
const Cache&) { |
| 53 |
ATLAS_NOTIMPLEMENTED; |
| 54 |
} |
| 55 |
|
| 56 |
void SphericalVector::do_setup(const FunctionSpace& source, const FunctionSpace& target, |
| 57 |
const Cache&) { |
| 58 |
ATLAS_NOTIMPLEMENTED; |
| 59 |
} |
| 60 |
|
| 61 |
void SphericalVector::do_setup(const FunctionSpace& source, |
| 62 |
const FunctionSpace& target) { |
| 63 |
ATLAS_TRACE("interpolation::method::SphericalVector::do_setup"); |
| 64 |
source_ = source; |
| 65 |
target_ = target; |
| 66 |
|
| 67 |
if (target_.size() == 0) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
interpolationScheme_.set("adjoint", false); // The temporary interpolation object should not compute the adjoint |
| 72 |
auto matrix_cache = MatrixCache(Interpolation(interpolationScheme_, source_, target_)); |
| 73 |
setMatrix(matrix_cache); |
| 74 |
if (adjoint_) { |
| 75 |
adjoint_matrix(); |
| 76 |
} |
| 77 |
|
| 78 |
// Get matrix data. |
| 79 |
const auto m = atlas::linalg::make_host_view<eckit::linalg::Scalar>(matrix()); |
| 80 |
const auto nRows = static_cast<Index>(m.rows()); |
| 81 |
const auto nCols = static_cast<Index>(m.cols()); |
| 82 |
const auto nNonZeros = static_cast<std::size_t>(m.nnz()); |
| 83 |
const auto* outerIndices = m.outer(); |
| 84 |
const auto* innerIndices = m.inner(); |
| 85 |
const auto* baseWeights = m.value(); |
| 86 |
|
| 87 |
// Note: need to store copy of weights as Eigen3 sorts compressed rows by j |
| 88 |
// whereas eckit does not. |
| 89 |
auto complexTriplets = ComplexTriplets(nNonZeros); |
| 90 |
auto realTriplets = RealTriplets(nNonZeros); |
| 91 |
|
| 92 |
const auto sourceLonLatsView = array::make_view<double, 2>(source_.lonlat()); |
| 93 |
const auto targetLonLatsView = array::make_view<double, 2>(target_.lonlat()); |
| 94 |
const auto unitSphere = geometry::UnitSphere{}; |
| 95 |
|
| 96 |
atlas_omp_parallel_for(auto rowIndex = Index{0}; rowIndex < nRows; |
| 97 |
++rowIndex) { |
| 98 |
for (auto dataIndex = outerIndices[rowIndex]; |
| 99 |
dataIndex < outerIndices[rowIndex + 1]; ++dataIndex) { |
| 100 |
const auto colIndex = static_cast<Index>(innerIndices[dataIndex]); |
| 101 |
const auto baseWeight = baseWeights[dataIndex]; |
| 102 |
|
| 103 |
const auto sourceLonLat = PointLonLat(sourceLonLatsView(colIndex, 0), |
| 104 |
sourceLonLatsView(colIndex, 1)); |
| 105 |
const auto targetLonLat = PointLonLat(targetLonLatsView(rowIndex, 0), |
| 106 |
targetLonLatsView(rowIndex, 1)); |
| 107 |
|
| 108 |
const auto alpha = |
| 109 |
unitSphere.greatCircleCourse(sourceLonLat, targetLonLat); |
| 110 |
|
| 111 |
const auto deltaAlpha = |
| 112 |
(alpha.first - alpha.second) * util::Constants::degreesToRadians(); |
| 113 |
|
| 114 |
complexTriplets[dataIndex] = ComplexTriplet{ |
| 115 |
rowIndex, colIndex, |
| 116 |
baseWeight * Complex{std::cos(deltaAlpha), std::sin(deltaAlpha)}}; |
| 117 |
realTriplets[dataIndex] = RealTriplet{rowIndex, colIndex, baseWeight}; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
complexWeights_ = ComplexMatrix(nRows, nCols, complexTriplets); |
| 122 |
realWeights_ = RealMatrix(nRows, nCols, realTriplets); |
| 123 |
|
| 124 |
if (adjoint_) { |
| 125 |
complexWeightsAdjoint_ = complexWeights_.adjoint(); |
| 126 |
realWeightsAdjoint_ = realWeights_.adjoint(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void SphericalVector::print(std::ostream&) const { ATLAS_NOTIMPLEMENTED; } |
| 131 |
|
| 132 |
void SphericalVector::do_execute(const FieldSet& sourceFieldSet, |
| 133 |
FieldSet& targetFieldSet, |
| 134 |
Metadata& metadata) const { |
| 135 |
ATLAS_TRACE("atlas::interpolation::method::SphericalVector::do_execute()"); |
| 136 |
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size()); |
| 137 |
|
| 138 |
const auto packedSourceFieldSet = util::pack_vector_fields(sourceFieldSet); |
| 139 |
auto packedTargetFieldSet = util::pack_vector_fields(targetFieldSet); |
| 140 |
|
| 141 |
for (auto i = 0; i < packedSourceFieldSet.size(); ++i) { |
| 142 |
do_execute(packedSourceFieldSet[i], packedTargetFieldSet[i], metadata); |
| 143 |
} |
| 144 |
|
| 145 |
util::unpack_vector_fields(packedTargetFieldSet, targetFieldSet); |
| 146 |
} |
| 147 |
|
| 148 |
void SphericalVector::do_execute(const Field& sourceField, Field& targetField, |
| 149 |
Metadata&) const { |
| 150 |
ATLAS_TRACE("atlas::interpolation::method::SphericalVector::do_execute()"); |
| 151 |
|
| 152 |
const auto fieldType = sourceField.metadata().getString("type", ""); |
| 153 |
if (fieldType != "vector") { |
| 154 |
auto metadata = Metadata(); |
| 155 |
Method::do_execute(sourceField, targetField, metadata); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
if (targetField.size() == 0) { |
| 160 |
haloExchange(sourceField); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
Method::check_compatibility(sourceField, targetField, matrix()); |
| 165 |
|
| 166 |
haloExchange(sourceField); |
| 167 |
interpolate_vector_field(sourceField, targetField, |
| 168 |
WeightsMatMul(complexWeights_, realWeights_)); |
| 169 |
targetField.set_dirty(); |
| 170 |
} |
| 171 |
|
| 172 |
void SphericalVector::do_execute_adjoint(FieldSet& sourceFieldSet, |
| 173 |
const FieldSet& targetFieldSet, |
| 174 |
Metadata& metadata) const { |
| 175 |
ATLAS_TRACE( |
| 176 |
"atlas::interpolation::method::SphericalVector::do_execute_adjoint()"); |
| 177 |
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size()); |
| 178 |
|
| 179 |
auto packedSourceFieldSet = util::pack_vector_fields(sourceFieldSet); |
| 180 |
const auto packedTargetFieldSet = util::pack_vector_fields(targetFieldSet); |
| 181 |
|
| 182 |
for (auto i = 0; i < packedSourceFieldSet.size(); ++i) { |
| 183 |
do_execute_adjoint(packedSourceFieldSet[i], packedTargetFieldSet[i], |
| 184 |
metadata); |
| 185 |
} |
| 186 |
|
| 187 |
util::unpack_vector_fields(packedSourceFieldSet, sourceFieldSet); |
| 188 |
} |
| 189 |
|
| 190 |
void SphericalVector::do_execute_adjoint(Field& sourceField, |
| 191 |
const Field& targetField, |
| 192 |
Metadata& metadata) const { |
| 193 |
ATLAS_TRACE( |
| 194 |
"atlas::interpolation::method::SphericalVector::do_execute_adjoint()"); |
| 195 |
|
| 196 |
const auto fieldType = sourceField.metadata().getString("type", ""); |
| 197 |
if (fieldType != "vector") { |
| 198 |
auto metadata = Metadata(); |
| 199 |
Method::do_execute_adjoint(sourceField, targetField, metadata); |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if (targetField.size() == 0) { |
| 204 |
adjointHaloExchange(sourceField); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
Method::check_compatibility(sourceField, targetField, matrix()); |
| 209 |
|
| 210 |
ATLAS_ASSERT(adjoint_, "\"adjoint\" needs to be set to \"true\" in Config."); |
| 211 |
interpolate_vector_field( |
| 212 |
targetField, sourceField, |
| 213 |
WeightsMatMulAdjoint(complexWeightsAdjoint_, realWeightsAdjoint_)); |
| 214 |
adjointHaloExchange(sourceField); |
| 215 |
} |
| 216 |
|
| 217 |
template <typename MatMul> |
| 218 |
void SphericalVector::interpolate_vector_field(const Field& sourceField, |
| 219 |
Field& targetField, |
| 220 |
const MatMul& matMul) { |
| 221 |
const auto sourceViewVariant = array::make_view_variant(sourceField); |
| 222 |
|
| 223 |
const auto sourceViewVisitor = [&](auto sourceView) { |
| 224 |
if constexpr (array::is_rank<2, 3>(sourceView) && |
| 225 |
array::is_non_const_value_type<float, double>(sourceView)) { |
| 226 |
using SourceView = std::decay_t<decltype(sourceView)>; |
| 227 |
using Value = typename SourceView::non_const_value_type; |
| 228 |
constexpr auto Rank = SourceView::rank(); |
| 229 |
auto targetView = array::make_view<Value, Rank>(targetField); |
| 230 |
|
| 231 |
switch (sourceField.variables()) { |
| 232 |
case 2: |
| 233 |
return matMul.apply(sourceView, targetView, twoVector); |
| 234 |
case 3: |
| 235 |
return matMul.apply(sourceView, targetView, threeVector); |
| 236 |
default: |
| 237 |
ATLAS_THROW_EXCEPTION("Error: no support for " + |
| 238 |
std::to_string(sourceField.variables()) + |
| 239 |
" variable vector fields.\n" + |
| 240 |
" Number of variables must be 2 or 3."); |
| 241 |
} |
| 242 |
|
| 243 |
} else { |
| 244 |
ATLAS_THROW_EXCEPTION( |
| 245 |
"Error: no support for rank = " + std::to_string(sourceField.rank()) + |
| 246 |
" and value type = " + sourceField.datatype().str() + ".\n" + |
| 247 |
"Vector field must have rank 2 or 3 with value type " |
| 248 |
"float or double"); |
| 249 |
} |
| 250 |
}; |
| 251 |
|
| 252 |
std::visit(sourceViewVisitor, sourceViewVariant); |
| 253 |
}; |
| 254 |
|
| 255 |
} // namespace method |
| 256 |
} // namespace interpolation |
| 257 |
} // namespace atlas |