| 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. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "atlas/trans/local/TransLocal.h" |
| 12 |
|
| 13 |
#include <cmath> |
| 14 |
#include <cstdlib> |
| 15 |
#include <fstream> |
| 16 |
|
| 17 |
#include "eckit/config/YAMLConfiguration.h" |
| 18 |
#include "eckit/eckit.h" |
| 19 |
#include "eckit/io/DataHandle.h" |
| 20 |
#include "eckit/linalg/LinearAlgebra.h" |
| 21 |
#include "eckit/log/Bytes.h" |
| 22 |
#include "eckit/log/JSON.h" |
| 23 |
#include "eckit/types/FloatCompare.h" |
| 24 |
|
| 25 |
#include "atlas/array.h" |
| 26 |
#include "atlas/field.h" |
| 27 |
#include "atlas/grid/Iterator.h" |
| 28 |
#include "atlas/grid/StructuredGrid.h" |
| 29 |
#include "atlas/library.h" |
| 30 |
#include "atlas/linalg/dense.h" |
| 31 |
#include "atlas/linalg/fft.h" |
| 32 |
#include "atlas/option.h" |
| 33 |
#include "atlas/parallel/mpi/mpi.h" |
| 34 |
#include "atlas/runtime/Exception.h" |
| 35 |
#include "atlas/runtime/Log.h" |
| 36 |
#include "atlas/trans/Trans.h" |
| 37 |
#include "atlas/trans/VorDivToUV.h" |
| 38 |
#include "atlas/trans/detail/TransFactory.h" |
| 39 |
#include "atlas/trans/local/LegendrePolynomials.h" |
| 40 |
#include "atlas/util/Constants.h" |
| 41 |
|
| 42 |
#include "atlas/library/defines.h" |
| 43 |
#if ATLAS_HAVE_FFTW |
| 44 |
#include "fftw3.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
// move latitudes at the poles to the following latitude: |
| 48 |
// (otherwise we would divide by zero when computing u,v from U,V) |
| 49 |
static constexpr double latPole = 89.9999999; |
| 50 |
// (latPole=89.9999999 seems to produce the best accuracy. Moving it further away |
| 51 |
// or closer to the pole both increase the errors!) |
| 52 |
|
| 53 |
namespace atlas { |
| 54 |
namespace trans { |
| 55 |
|
| 56 |
namespace { |
| 57 |
static TransBuilderGrid<TransLocal> builder("local", "local"); |
| 58 |
} // namespace |
| 59 |
|
| 60 |
namespace { |
| 61 |
class TransParameters { |
| 62 |
public: |
| 63 |
TransParameters(const eckit::Configuration& config): config_(config) {} |
| 64 |
~TransParameters() = default; |
| 65 |
|
| 66 |
/* |
| 67 |
* For the future |
| 68 |
*/ |
| 69 |
// bool scalar_derivatives() const { return config_.getBool( "scalar_derivatives", false ); } |
| 70 |
|
| 71 |
// bool wind_EW_derivatives() const { return config_.getBool( "wind_EW_derivatives", false ); } |
| 72 |
|
| 73 |
// bool vorticity_divergence_fields() const { return config_.getBool( "vorticity_divergence_fields", false ); } |
| 74 |
|
| 75 |
// std::string read_legendre() const { return config_.getString( "read_legendre", "" ); } |
| 76 |
// bool global() const { return config_.getBool( "global", false ); } |
| 77 |
|
| 78 |
// std::string read_fft() const { return config_.getString( "read_fft", "" ); } |
| 79 |
|
| 80 |
|
| 81 |
std::string write_legendre() const { return config_.getString("write_legendre", ""); } |
| 82 |
|
| 83 |
std::string write_fft() const { return config_.getString("write_fft", ""); } |
| 84 |
|
| 85 |
|
| 86 |
bool export_legendre() const { return config_.getBool("export_legendre", false); } |
| 87 |
|
| 88 |
int warning() const { return config_.getInt("warning", 1); } |
| 89 |
|
| 90 |
std::string fft() const { |
| 91 |
static const std::set<std::string> supported = {"OFF","FFTW","pocketfft"}; |
| 92 |
std::string defaultLinalgFFTBackend = atlas::Library::instance().linalgFFTBackend(); |
| 93 |
if (defaultLinalgFFTBackend.empty()) { |
| 94 |
defaultLinalgFFTBackend = "FFTW"; |
| 95 |
} |
| 96 |
std::string value = config_.getString("fft", defaultLinalgFFTBackend); |
| 97 |
if (supported.find(value) == supported.end()) { |
| 98 |
ATLAS_THROW_EXCEPTION("FFT backend \"" << value << "\" is not one of the supported : OFF, FFTW, pocketfft"); |
| 99 |
} |
| 100 |
if (not ATLAS_HAVE_FFTW && value == "FFTW") { |
| 101 |
value = "pocketfft"; |
| 102 |
} |
| 103 |
return value; |
| 104 |
} |
| 105 |
|
| 106 |
std::string matrix_multiply() const { return config_.getString("matrix_multiply", ""); } |
| 107 |
|
| 108 |
|
| 109 |
private: |
| 110 |
const eckit::Configuration& config_; |
| 111 |
}; |
| 112 |
|
| 113 |
struct ReadCache { |
| 114 |
ReadCache(const void* cache) { |
| 115 |
begin = reinterpret_cast<const char*>(cache); |
| 116 |
pos = 0; |
| 117 |
} |
| 118 |
template <typename T> |
| 119 |
T* read(size_t size) { |
| 120 |
const T* v = reinterpret_cast<const T*>(begin + pos); |
| 121 |
pos += size * sizeof(T); |
| 122 |
return const_cast<T*>(v); |
| 123 |
} |
| 124 |
|
| 125 |
const char* begin; |
| 126 |
size_t pos; |
| 127 |
}; |
| 128 |
|
| 129 |
struct WriteCache { |
| 130 |
WriteCache(const eckit::PathName& file_path): dh_(file_path.fileHandle(/*overwrite = */ true)) { |
| 131 |
if (file_path.exists()) { |
| 132 |
std::stringstream err; |
| 133 |
err << "Cannot open cache file " << file_path << " for writing as it already exists. Remove first."; |
| 134 |
throw_Exception(err.str(), Here()); |
| 135 |
} |
| 136 |
dh_->openForWrite(0); |
| 137 |
pos = 0; |
| 138 |
} |
| 139 |
~WriteCache() { dh_->close(); } |
| 140 |
template <typename T> |
| 141 |
void write(const T* v, long size) { |
| 142 |
dh_->write(v, size * sizeof(T)); |
| 143 |
pos += size * sizeof(T); |
| 144 |
} |
| 145 |
|
| 146 |
//void write( long v ) { |
| 147 |
// dh_->write( &v , sizeof(long) ); |
| 148 |
// pos += sizeof(long); |
| 149 |
//} |
| 150 |
|
| 151 |
//void write( const Grid& grid ) { |
| 152 |
// std::stringstream s; |
| 153 |
// eckit::JSON json(s); |
| 154 |
// json << grid.spec(); |
| 155 |
// std::string grid_spec( s.str() ); |
| 156 |
// long size = grid_spec.size(); |
| 157 |
// write( size ); |
| 158 |
// dh_->write( grid_spec.c_str(), grid_spec.size() ); |
| 159 |
// pos += grid_spec.size(); |
| 160 |
//} |
| 161 |
|
| 162 |
|
| 163 |
std::unique_ptr<eckit::DataHandle> dh_; |
| 164 |
size_t pos; |
| 165 |
}; |
| 166 |
|
| 167 |
} // namespace |
| 168 |
|
| 169 |
// -------------------------------------------------------------------------------------------------------------------- |
| 170 |
// Helper functions |
| 171 |
// -------------------------------------------------------------------------------------------------------------------- |
| 172 |
namespace { // anonymous |
| 173 |
|
| 174 |
size_t legendre_size(const size_t truncation) { |
| 175 |
return (truncation + 2) * (truncation + 1) / 2; |
| 176 |
} |
| 177 |
|
| 178 |
//int nlats_northernHemisphere( const int nlats ) { |
| 179 |
// return ceil( nlats / 2. ); |
| 180 |
// // using ceil here should make it possible to have odd number of latitudes (with the centre latitude being the equator) |
| 181 |
//} |
| 182 |
|
| 183 |
size_t num_n(const int truncation, const int m, const bool symmetric) { |
| 184 |
int len = (truncation - m + (symmetric ? 2 : 1)) / 2; |
| 185 |
ATLAS_ASSERT(len >= 0); |
| 186 |
return size_t(len); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
[[noreturn]] void throw_AllocationFailed(size_t bytes, const eckit::CodeLocation& loc) { |
| 191 |
std::stringstream ss; |
| 192 |
ss << "AllocationFailed: Could not allocate " << eckit::Bytes(bytes); |
| 193 |
throw_Exception(ss.str(), loc); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
void alloc_aligned(double*& ptr, size_t n) { |
| 198 |
const size_t alignment = 256; |
| 199 |
size_t bytes = sizeof(double) * n; |
| 200 |
int err = posix_memalign((void**)&ptr, alignment, bytes); |
| 201 |
if (err) { |
| 202 |
throw_AllocationFailed(bytes, Here()); |
| 203 |
} |
| 204 |
} |
| 205 |
void free_aligned(double*& ptr) { |
| 206 |
free(ptr); |
| 207 |
ptr = nullptr; |
| 208 |
} |
| 209 |
|
| 210 |
void alloc_aligned(double*& ptr, size_t n, const char* msg) { |
| 211 |
ATLAS_ASSERT(msg); |
| 212 |
Log::debug() << "TransLocal: allocating '" << msg << "': " << eckit::Bytes(sizeof(double) * n) << std::endl; |
| 213 |
alloc_aligned(ptr, n); |
| 214 |
} |
| 215 |
|
| 216 |
void free_aligned(double*& ptr, const char* msg) { |
| 217 |
ATLAS_ASSERT(msg); |
| 218 |
Log::debug() << "TransLocal: deallocating '" << msg << "'" << std::endl; |
| 219 |
free_aligned(ptr); |
| 220 |
} |
| 221 |
|
| 222 |
void alloc_aligned(std::complex<double>*& ptr, size_t n) { |
| 223 |
const size_t alignment = 256; |
| 224 |
size_t bytes = sizeof(std::complex<double>) * n; |
| 225 |
int err = posix_memalign((void**)&ptr, alignment, bytes); |
| 226 |
if (err) { |
| 227 |
throw_AllocationFailed(bytes, Here()); |
| 228 |
} |
| 229 |
} |
| 230 |
void free_aligned(std::complex<double>*& ptr) { |
| 231 |
free(ptr); |
| 232 |
ptr = nullptr; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
size_t add_padding(size_t n) { |
| 237 |
return size_t(std::ceil(n / 8.)) * 8; |
| 238 |
} |
| 239 |
|
| 240 |
std::string detect_linalg_backend(const std::string& linalg_backend_) { |
| 241 |
linalg::dense::Backend linalg_backend = linalg::dense::Backend{linalg_backend_}; |
| 242 |
if (linalg_backend.type() == linalg::dense::backend::eckit_linalg::type()) { |
| 243 |
std::string backend; |
| 244 |
linalg_backend.get("backend", backend); |
| 245 |
if (backend.empty() || backend == "default") { |
| 246 |
#if ATLAS_ECKIT_HAVE_ECKIT_585 |
| 247 |
return eckit::linalg::LinearAlgebraDense::backend().name(); |
| 248 |
#else |
| 249 |
return eckit::linalg::LinearAlgebra::backend().name(); |
| 250 |
#endif |
| 251 |
} |
| 252 |
return backend; |
| 253 |
} |
| 254 |
return linalg_backend.type(); |
| 255 |
}; |
| 256 |
|
| 257 |
bool using_eckit_default_backend(const std::string& linalg_backend_) { |
| 258 |
linalg::dense::Backend linalg_backend = linalg::dense::Backend{linalg_backend_}; |
| 259 |
if (linalg_backend.type() == linalg::dense::backend::eckit_linalg::type()) { |
| 260 |
std::string backend; |
| 261 |
linalg_backend.get("backend", backend); |
| 262 |
if (backend.empty() || backend == "default") { |
| 263 |
return true; |
| 264 |
} |
| 265 |
} |
| 266 |
return false; |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
} // namespace |
| 271 |
|
| 272 |
int fourier_truncation(const int truncation, // truncation |
| 273 |
const int nx, // number of longitudes |
| 274 |
const int /*nxmax*/, // maximum nx |
| 275 |
const int ndgl, // number of latitudes |
| 276 |
const double lat, // latitude in radian |
| 277 |
const bool fullgrid) { // regular grid |
| 278 |
int trc = truncation; |
| 279 |
int trclin = ndgl - 1; |
| 280 |
int trcquad = ndgl * 2 / 3 - 1; |
| 281 |
if (truncation >= trclin || fullgrid) { |
| 282 |
// linear |
| 283 |
trc = (nx - 1) / 2; |
| 284 |
} |
| 285 |
else if (truncation >= trcquad) { |
| 286 |
// quadratic |
| 287 |
double weight = 3 * (trclin - truncation) / ndgl; |
| 288 |
double sqcos = std::pow(std::cos(lat), 2); |
| 289 |
|
| 290 |
trc = static_cast<int>((nx - 1) / (2 + weight * sqcos)); |
| 291 |
} |
| 292 |
else { |
| 293 |
// cubic |
| 294 |
double sqcos = std::pow(std::cos(lat), 2); |
| 295 |
|
| 296 |
trc = static_cast<int>((nx - 1) / (2 + sqcos) - 1); |
| 297 |
} |
| 298 |
trc = std::min(truncation, trc); |
| 299 |
return trc; |
| 300 |
} |
| 301 |
|
| 302 |
namespace detail { |
| 303 |
|
| 304 |
struct FFT_Data { |
| 305 |
std::complex<double>* in{nullptr}; |
| 306 |
double* out{nullptr}; |
| 307 |
}; |
| 308 |
|
| 309 |
} // namespace detail |
| 310 |
|
| 311 |
|
| 312 |
// -------------------------------------------------------------------------------------------------------------------- |
| 313 |
// Class TransLocal |
| 314 |
// -------------------------------------------------------------------------------------------------------------------- |
| 315 |
|
| 316 |
bool TransLocal::warning(const eckit::Configuration& config) const { |
| 317 |
int warning = warning_; |
| 318 |
config.get("warning", warning); |
| 319 |
return (warning > 0 && grid_.size() >= warning); |
| 320 |
} |
| 321 |
|
| 322 |
TransLocal::TransLocal(const Cache& cache, const Grid& grid, const Domain& domain, const long truncation, |
| 323 |
const eckit::Configuration& config): |
| 324 |
grid_(grid, domain), |
| 325 |
truncation_(static_cast<int>(truncation)), |
| 326 |
precompute_(config.getBool("precompute", true)), |
| 327 |
cache_(cache), |
| 328 |
legendre_cache_(cache.legendre().data()), |
| 329 |
legendre_cachesize_(cache.legendre().size()), |
| 330 |
fft_cache_(cache.fft().data()), |
| 331 |
fft_cachesize_(cache.fft().size()), |
| 332 |
fft_data_(new detail::FFT_Data), |
| 333 |
linalg_backend_(TransParameters{config}.matrix_multiply()), |
| 334 |
fft_backend_(TransParameters{config}.fft()), |
| 335 |
warning_(TransParameters{config}.warning()) { |
| 336 |
ATLAS_TRACE("TransLocal constructor"); |
| 337 |
|
| 338 |
if (mpi::size() > 1) { |
| 339 |
ATLAS_THROW_EXCEPTION("TransLocal is not implemented for more than 1 MPI task."); |
| 340 |
} |
| 341 |
|
| 342 |
double fft_threshold = 0.0; // fraction of latitudes of the full grid down to which FFT is used. |
| 343 |
// This threshold needs to be adjusted depending on the dgemm and FFT performance of the machine |
| 344 |
// on which this code is running! |
| 345 |
idx_t nlats = 0; |
| 346 |
idx_t nlonsMax = 0; |
| 347 |
idx_t neqtr = 0; |
| 348 |
useFFT_ = (fft_backend_ != "OFF"); |
| 349 |
if (useFFT_) { |
| 350 |
if (fft_backend_ == "FFTW") { |
| 351 |
fft_.reset(new linalg::FFTW()); |
| 352 |
} |
| 353 |
else if(fft_backend_ == "pocketfft") { |
| 354 |
fft_.reset(new linalg::pocketfft()); |
| 355 |
} |
| 356 |
else { |
| 357 |
ATLAS_THROW_EXCEPTION("Unrecognised FFT configuration"); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
unstruct_precomp_ = (config.has("precompute") ? precompute_ : false); |
| 362 |
no_symmetry_ = false; |
| 363 |
nlatsNH_ = 0; |
| 364 |
nlatsSH_ = 0; |
| 365 |
nlatsLeg_ = 0; |
| 366 |
nlatsLegDomain_ = 0; |
| 367 |
nlatsLegReduced_ = 0; |
| 368 |
bool useGlobalLeg = true; |
| 369 |
bool no_nest = false; |
| 370 |
|
| 371 |
if (StructuredGrid(grid_) && not grid_.projection()) { |
| 372 |
StructuredGrid g(grid_); |
| 373 |
nlats = g.ny(); |
| 374 |
nlonsMax = g.nxmax(); |
| 375 |
|
| 376 |
// check location of domain relative to the equator: |
| 377 |
for (idx_t j = 0; j < nlats; ++j) { |
| 378 |
// assumptions: latitudes in g.y(j) are monotone and decreasing |
| 379 |
// no assumption on whether we have 0, 1 or 2 latitudes at the equator |
| 380 |
double lat = g.y(j); |
| 381 |
(eckit::types::is_approximately_equal(lat, 0.) ? neqtr : (lat < 0 ? nlatsSH_ : nlatsNH_))++; |
| 382 |
} |
| 383 |
if (neqtr > 0) { |
| 384 |
nlatsNH_++; |
| 385 |
nlatsSH_++; |
| 386 |
} |
| 387 |
if (nlatsNH_ >= nlatsSH_) { |
| 388 |
nlatsLegDomain_ = nlatsNH_; |
| 389 |
} |
| 390 |
else { |
| 391 |
nlatsLegDomain_ = nlatsSH_; |
| 392 |
} |
| 393 |
|
| 394 |
gridGlobal_ = grid; |
| 395 |
if (not gridGlobal_.domain().global()) { |
| 396 |
// The grid is not a nest of a global grid |
| 397 |
if (RegularGrid(grid_)) { |
| 398 |
no_nest = true; |
| 399 |
no_symmetry_ = true; |
| 400 |
useFFT_ = false; |
| 401 |
fft_backend_ = "OFF"; |
| 402 |
nlatsNH_ = nlats; |
| 403 |
nlatsSH_ = 0; |
| 404 |
nlatsLegDomain_ = nlatsNH_; |
| 405 |
gridGlobal_ = grid_; |
| 406 |
useGlobalLeg = false; |
| 407 |
} |
| 408 |
else { // non-nested reduced grids are not supported |
| 409 |
std::ostringstream log; |
| 410 |
log << "Transforms to non-regular regional grids is not supported, unless it defined as a cropping of " |
| 411 |
"a global grid." |
| 412 |
<< std::endl; |
| 413 |
log << " Input grid: " << grid.spec() << std::endl; |
| 414 |
log << " Applied domain: " << domain << std::endl; |
| 415 |
log << " Regional grid after Domain applied (the output grid): " << grid_.spec() << std::endl; |
| 416 |
throw_NotImplemented(log.str(), Here()); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
StructuredGrid gs_global(gridGlobal_); |
| 421 |
ATLAS_ASSERT(gs_global); // assert structured grid |
| 422 |
StructuredGrid gsLeg = (useGlobalLeg ? gs_global : g); |
| 423 |
nlonsMaxGlobal_ = gs_global.nxmax(); |
| 424 |
jlonMin_.resize(1); |
| 425 |
jlonMin_[0] = 0; |
| 426 |
jlatMin_ = 0; |
| 427 |
nlatsGlobal_ = gs_global.ny(); |
| 428 |
if (grid_.domain().global()) { |
| 429 |
Log::debug() << "Global grid with " << nlats << " latitudes." << std::endl; |
| 430 |
} |
| 431 |
else { |
| 432 |
Log::debug() << "Grid has " << nlats << " latitudes. Global grid has " << nlatsGlobal_ << std::endl; |
| 433 |
} |
| 434 |
if (useGlobalLeg) { |
| 435 |
nlatsLeg_ = (nlatsGlobal_ + 1) / 2; |
| 436 |
} |
| 437 |
else { |
| 438 |
nlatsLeg_ = nlatsLegDomain_; |
| 439 |
nlatsLegReduced_ = nlatsLeg_; |
| 440 |
} |
| 441 |
for (int jlat = 0; jlat < nlatsGlobal_; jlat++) { |
| 442 |
if (gs_global.y(jlat) > g.y(0)) { |
| 443 |
//Log::info() << gs_global.y( jlat ) << ">" << g.y( 0 ) << " "; |
| 444 |
jlatMin_++; |
| 445 |
}; |
| 446 |
} |
| 447 |
//Log::info() << std::endl; |
| 448 |
int jlatMinLeg_ = jlatMin_; |
| 449 |
if (nlatsNH_ < nlatsSH_) { |
| 450 |
jlatMinLeg_ += nlatsNH_ - nlatsSH_; |
| 451 |
}; |
| 452 |
if (jlatMin_ >= (nlatsGlobal_ + 1) / 2) { |
| 453 |
jlatMinLeg_ -= 2 * (jlatMin_ - (nlatsGlobal_ + 1) / 2); |
| 454 |
if (nlatsGlobal_ % 2 == 1) { |
| 455 |
jlatMinLeg_--; |
| 456 |
} |
| 457 |
}; |
| 458 |
if (useGlobalLeg) { |
| 459 |
nlatsLegReduced_ = jlatMinLeg_ + nlatsLegDomain_; |
| 460 |
} |
| 461 |
|
| 462 |
// reduce truncation towards the pole for reduced meshes: |
| 463 |
nlat0_.resize(truncation_ + 1); |
| 464 |
if (no_nest) { |
| 465 |
for (int j = 0; j <= truncation_; j++) { |
| 466 |
nlat0_[j] = 0; |
| 467 |
} |
| 468 |
} |
| 469 |
else { |
| 470 |
int nmen0 = -1; |
| 471 |
for (int jlat = 0; jlat < nlatsGlobal_ / 2; jlat++) { |
| 472 |
double lat = gs_global.y(jlat) * util::Constants::degreesToRadians(); |
| 473 |
int nmen = fourier_truncation(truncation_, gs_global.nx(jlat), gs_global.nxmax(), nlatsGlobal_, lat, |
| 474 |
RegularGrid(gs_global)); |
| 475 |
nmen = std::max(nmen0, nmen); |
| 476 |
int ndgluj = nlatsLeg_ - std::min(nlatsLeg_, nlatsLeg_ + jlatMinLeg_ - jlat); |
| 477 |
if (useGlobalLeg) { |
| 478 |
ndgluj = std::max(jlatMinLeg_, jlat); |
| 479 |
} |
| 480 |
for (int j = nmen0 + 1; j <= nmen; j++) { |
| 481 |
nlat0_[j] = ndgluj; |
| 482 |
} |
| 483 |
nmen0 = nmen; |
| 484 |
} |
| 485 |
for (int j = nmen0 + 1; j <= truncation_; j++) { |
| 486 |
nlat0_[j] = nlatsLeg_; |
| 487 |
} |
| 488 |
} |
| 489 |
/*Log::info() << "nlats=" << g.ny() << " nlatsGlobal=" << gs_global.ny() << " jlatMin=" << jlatMin_ |
| 490 |
<< " jlatMinLeg=" << jlatMinLeg_ << " nlatsGlobal/2-nlatsLeg=" << nlatsGlobal_ / 2 - nlatsLeg_ |
| 491 |
<< " nlatsLeg_=" << nlatsLeg_ << " nlatsLegDomain_=" << nlatsLegDomain_ << std::endl;*/ |
| 492 |
|
| 493 |
// compute longitudinal location of domain within global grid for using FFT: |
| 494 |
auto wrapAngle = [&](double angle) { |
| 495 |
double result = std::fmod(angle, 360.); |
| 496 |
if (result < 0.) { |
| 497 |
result += 360.; |
| 498 |
} |
| 499 |
return result; |
| 500 |
}; |
| 501 |
if (useFFT_) { |
| 502 |
double lonmin = wrapAngle(g.x(0, 0)); |
| 503 |
if (nlonsMax < fft_threshold * nlonsMaxGlobal_) { |
| 504 |
useFFT_ = false; |
| 505 |
fft_backend_ = "OFF"; |
| 506 |
} |
| 507 |
else { |
| 508 |
// need to use FFT with cropped grid |
| 509 |
if (RegularGrid(gridGlobal_)) { |
| 510 |
for (idx_t jlon = 0; jlon < nlonsMaxGlobal_; ++jlon) { |
| 511 |
if (gs_global.x(jlon, 0) < lonmin) { |
| 512 |
jlonMin_[0]++; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
else { |
| 517 |
nlonsGlobal_.resize(nlats); |
| 518 |
jlonMin_.resize(nlats); |
| 519 |
for (idx_t jlat = 0; jlat < nlats; jlat++) { |
| 520 |
double lonmin = wrapAngle(g.x(0, jlat)); |
| 521 |
nlonsGlobal_[jlat] = gs_global.nx(jlat + jlatMin_); |
| 522 |
jlonMin_[jlat] = 0; |
| 523 |
for (idx_t jlon = 0; jlon < nlonsGlobal_[jlat]; ++jlon) { |
| 524 |
if (gs_global.x(jlon, jlat + jlatMin_) < lonmin) { |
| 525 |
jlonMin_[jlat]++; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
//Log::info() << "nlats=" << g.ny() << " nlatsGlobal=" << gs_global.ny() << std::endl; |
| 533 |
std::vector<double> lats(nlatsLeg_); |
| 534 |
std::vector<double> lons(nlonsMax); |
| 535 |
if (nlatsNH_ >= nlatsSH_ || useGlobalLeg) { |
| 536 |
for (idx_t j = 0; j < nlatsLeg_; ++j) { |
| 537 |
double lat = gsLeg.y(j); |
| 538 |
if (lat > latPole) { |
| 539 |
lat = latPole; |
| 540 |
} |
| 541 |
if (lat < -latPole) { |
| 542 |
lat = -latPole; |
| 543 |
} |
| 544 |
lats[j] = lat * util::Constants::degreesToRadians(); |
| 545 |
} |
| 546 |
} |
| 547 |
else { |
| 548 |
for (idx_t j = nlats - 1, idx = 0; idx < nlatsLeg_; --j, ++idx) { |
| 549 |
double lat = gsLeg.y(j); |
| 550 |
if (lat > latPole) { |
| 551 |
lat = latPole; |
| 552 |
} |
| 553 |
if (lat < -latPole) { |
| 554 |
lat = -latPole; |
| 555 |
} |
| 556 |
lats[idx] = -lat * util::Constants::degreesToRadians(); |
| 557 |
} |
| 558 |
} |
| 559 |
for (idx_t j = 0; j < nlonsMax; ++j) { |
| 560 |
lons[j] = g.x(j, 0) * util::Constants::degreesToRadians(); |
| 561 |
} |
| 562 |
/*Log::info() << "lats: "; |
| 563 |
for ( int j = 0; j < nlatsLeg_; j++ ) { |
| 564 |
Log::info() << lats[j] << " "; |
| 565 |
} |
| 566 |
Log::info() << std::endl;*/ |
| 567 |
|
| 568 |
|
| 569 |
Log::debug() << "TransLocal set up with:\n" |
| 570 |
<< " - grid: " << grid.name() << '\n' |
| 571 |
<< " - truncation: " << truncation << '\n'; |
| 572 |
if (not domain.global()) { |
| 573 |
Log::debug() << " - domain: " << domain << '\n'; |
| 574 |
} |
| 575 |
if (GlobalDomain(domain)) { |
| 576 |
if (GlobalDomain(domain).west() != 0.) { |
| 577 |
Log::debug() << " - global domain with modified west: " << GlobalDomain(domain).west() << '\n'; |
| 578 |
} |
| 579 |
} |
| 580 |
Log::debug() << " - fft: " << fft_backend_ << '\n'; |
| 581 |
Log::debug() << " - linalg_backend: "; |
| 582 |
if (using_eckit_default_backend(linalg_backend_)) { |
| 583 |
Log::debug() << "eckit_linalg default (currently \"" << detect_linalg_backend(linalg_backend_) |
| 584 |
<< "\" but could be changed after setup, check invtrans debug output)" << '\n'; |
| 585 |
} |
| 586 |
else { |
| 587 |
Log::debug() << detect_linalg_backend(linalg_backend_) << '\n'; |
| 588 |
} |
| 589 |
Log::debug() << " - legendre_cache: " << std::boolalpha << bool(legendre_cache_) << std::endl; |
| 590 |
|
| 591 |
|
| 592 |
// precomputations for Legendre polynomials: |
| 593 |
{ |
| 594 |
const auto nlatsLeg = size_t(nlatsLeg_); |
| 595 |
size_t size_sym = 0; |
| 596 |
size_t size_asym = 0; |
| 597 |
legendre_sym_begin_.resize(truncation_ + 3); |
| 598 |
legendre_asym_begin_.resize(truncation_ + 3); |
| 599 |
legendre_sym_begin_[0] = 0; |
| 600 |
legendre_asym_begin_[0] = 0; |
| 601 |
for (idx_t jm = 0; jm <= truncation_ + 1; jm++) { |
| 602 |
size_sym += add_padding(num_n(truncation_ + 1, jm, /*symmetric*/ true ) * nlatsLeg); |
| 603 |
size_asym += add_padding(num_n(truncation_ + 1, jm, /*antisymmetric*/ false) * nlatsLeg); |
| 604 |
legendre_sym_begin_[jm + 1] = size_sym; |
| 605 |
legendre_asym_begin_[jm + 1] = size_asym; |
| 606 |
} |
| 607 |
|
| 608 |
if (legendre_cache_) { |
| 609 |
ReadCache legendre(legendre_cache_); |
| 610 |
legendre_sym_ = legendre.read<double>(size_sym); |
| 611 |
legendre_asym_ = legendre.read<double>(size_asym); |
| 612 |
ATLAS_ASSERT(legendre.pos == legendre_cachesize_); |
| 613 |
// TODO: check this is all aligned... |
| 614 |
} |
| 615 |
else { |
| 616 |
if (TransParameters(config).export_legendre()) { |
| 617 |
ATLAS_ASSERT(not cache_.legendre()); |
| 618 |
|
| 619 |
size_t bytes = sizeof(double) * (size_sym + size_asym); |
| 620 |
Log::debug() << "TransLocal: allocating LegendreCache: " << eckit::Bytes(bytes) << std::endl; |
| 621 |
export_legendre_ = LegendreCache(bytes); |
| 622 |
|
| 623 |
legendre_cachesize_ = export_legendre_.legendre().size(); |
| 624 |
legendre_cache_ = export_legendre_.legendre().data(); |
| 625 |
ReadCache legendre(legendre_cache_); |
| 626 |
legendre_sym_ = legendre.read<double>(size_sym); |
| 627 |
legendre_asym_ = legendre.read<double>(size_asym); |
| 628 |
} |
| 629 |
else { |
| 630 |
alloc_aligned(legendre_sym_, size_sym, "Legendre coeffs symmetric"); |
| 631 |
alloc_aligned(legendre_asym_, size_asym, "Legendre coeffs asymmetric"); |
| 632 |
} |
| 633 |
|
| 634 |
ATLAS_TRACE_SCOPE("Legendre precomputations (structured)") { |
| 635 |
compute_legendre_polynomials(truncation_ + 1, nlatsLeg_, lats.data(), legendre_sym_, legendre_asym_, |
| 636 |
legendre_sym_begin_.data(), legendre_asym_begin_.data()); |
| 637 |
} |
| 638 |
std::string file_path = TransParameters(config).write_legendre(); |
| 639 |
if (file_path.size()) { |
| 640 |
ATLAS_TRACE("Write LegendreCache to file"); |
| 641 |
Log::debug() << "Writing Legendre cache file ..." << std::endl; |
| 642 |
Log::debug() << " path: " << file_path << std::endl; |
| 643 |
WriteCache legendre(file_path); |
| 644 |
legendre.write(legendre_sym_, size_sym); |
| 645 |
legendre.write(legendre_asym_, size_asym); |
| 646 |
Log::debug() << " size: " << eckit::Bytes(legendre.pos) << std::endl; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
// precomputations for Fourier transformations: |
| 652 |
if (useFFT_) { |
| 653 |
ATLAS_TRACE("Fourier precomputations ("+fft_->type()+")"); |
| 654 |
int num_complex = (nlonsMaxGlobal_ / 2) + 1; |
| 655 |
int howmany = RegularGrid(gridGlobal_) ? nlats : 1; |
| 656 |
alloc_aligned(fft_data_->in, howmany * num_complex); |
| 657 |
alloc_aligned(fft_data_->out, howmany * nlonsMaxGlobal_); |
| 658 |
|
| 659 |
#if ATLAS_HAVE_FFTW |
| 660 |
if (fft_cache_) { |
| 661 |
Log::debug() << "Import FFTW wisdom from cache" << std::endl; |
| 662 |
fftw_import_wisdom_from_string(static_cast<const char*>(fft_cache_)); |
| 663 |
} |
| 664 |
// std::string wisdomString( "" ); |
| 665 |
// std::ifstream read( "wisdom.bin" ); |
| 666 |
// if ( read.is_open() ) { |
| 667 |
// std::getline( read, wisdomString ); |
| 668 |
// while ( read ) { |
| 669 |
// std::string line; |
| 670 |
// std::getline( read, line ); |
| 671 |
// wisdomString += line; |
| 672 |
// } |
| 673 |
// } |
| 674 |
// read.close(); |
| 675 |
// if ( wisdomString.length() > 0 ) { fftw_import_wisdom_from_string( &wisdomString[0u] ); } |
| 676 |
#endif |
| 677 |
|
| 678 |
if (RegularGrid(gridGlobal_)) { |
| 679 |
fft_->plan_inverse_c2r_many(nlats, nlonsMaxGlobal_, fft_data_->in, fft_data_->out); |
| 680 |
} |
| 681 |
else { |
| 682 |
for (int j = 0; j < nlatsLegDomain_; j++) { |
| 683 |
int nlonsGlobalj = gs_global.nx(jlatMinLeg_ + j); |
| 684 |
fft_->plan_inverse_c2r(nlonsGlobalj, fft_data_->in, fft_data_->out); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
#if ATLAS_HAVE_FFTW |
| 689 |
std::string file_path = TransParameters(config).write_fft(); |
| 690 |
if (file_path.size()) { |
| 691 |
Log::debug() << "Write FFTW wisdom to file " << file_path << std::endl; |
| 692 |
//bool success = fftw_export_wisdom_to_filename( "wisdom.bin" ); |
| 693 |
//ASSERT( success ); |
| 694 |
//std::ofstream write( file_path ); |
| 695 |
//write << FFTW_Wisdom(); |
| 696 |
|
| 697 |
FILE* file_fftw = fopen(file_path.c_str(), "wb"); |
| 698 |
fftw_export_wisdom_to_file(file_fftw); |
| 699 |
fclose(file_fftw); |
| 700 |
} |
| 701 |
// std::string newWisdom( fftw_export_wisdom_to_string() ); |
| 702 |
// if ( 1.1 * wisdomString.length() < newWisdom.length() ) { |
| 703 |
// std::ofstream write( "wisdom.bin" ); |
| 704 |
// write << newWisdom; |
| 705 |
// write.close(); |
| 706 |
// } |
| 707 |
#else |
| 708 |
// std::string file_path = TransParameters(config).write_fft(); |
| 709 |
// if (file_path.size()) { |
| 710 |
// std::ofstream write(file_path); |
| 711 |
// write << "No cache available, as FFTW is not enabled" << std::endl; |
| 712 |
// write.close(); |
| 713 |
// } |
| 714 |
#endif |
| 715 |
} |
| 716 |
else { // not using FFT |
| 717 |
Log::warning() |
| 718 |
<< "WARNING: Spectral transform results may contain aliasing errors. This will be addressed soon." |
| 719 |
<< std::endl; |
| 720 |
|
| 721 |
alloc_aligned(fourier_, 2 * (truncation_ + 1) * nlonsMax, "Fourier coeffs"); |
| 722 |
{ |
| 723 |
ATLAS_TRACE("Fourier precomputations (NoFFT)"); |
| 724 |
int idx = 0; |
| 725 |
for (int jm = 0; jm < truncation_ + 1; jm++) { |
| 726 |
double factor = 1.; |
| 727 |
if (jm > 0) { |
| 728 |
factor = 2.; |
| 729 |
} |
| 730 |
for (int jlon = 0; jlon < nlonsMax; jlon++) { |
| 731 |
fourier_[idx++] = +std::cos(jm * lons[jlon]) * factor; // real part |
| 732 |
} |
| 733 |
for (int jlon = 0; jlon < nlonsMax; jlon++) { |
| 734 |
fourier_[idx++] = -std::sin(jm * lons[jlon]) * factor; // imaginary part |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
else { |
| 741 |
// unstructured grid |
| 742 |
if (unstruct_precomp_) { |
| 743 |
ATLAS_TRACE("Legendre precomputations (unstructured)"); |
| 744 |
|
| 745 |
if (warning()) { |
| 746 |
Log::warning() |
| 747 |
<< "WARNING: Precomputations for spectral transforms could take a long time as there's no structure" |
| 748 |
" to take advantage of!!!" |
| 749 |
<< std::endl |
| 750 |
<< "The precomputed values consume at least " |
| 751 |
<< eckit::Bytes(sizeof(double) * legendre_size(truncation_) * grid_.size()) << " (" |
| 752 |
<< eckit::Bytes(sizeof(double) * legendre_size(truncation_)) << " for each of " << grid_.size() |
| 753 |
<< " grid points )" << std::endl |
| 754 |
<< "Furthermore, results may contain aliasing errors." << std::endl; |
| 755 |
} |
| 756 |
|
| 757 |
std::vector<double> lats(grid_.size()); |
| 758 |
alloc_aligned(legendre_, legendre_size(truncation_) * grid_.size(), "Legendre coeffs."); |
| 759 |
int j(0); |
| 760 |
for (PointLonLat p : grid_.lonlat()) { |
| 761 |
lats[j++] = p.lat() * util::Constants::degreesToRadians(); |
| 762 |
} |
| 763 |
compute_legendre_polynomials_all(truncation_, grid_.size(), lats.data(), legendre_); |
| 764 |
} |
| 765 |
if (TransParameters(config).write_legendre().size()) { |
| 766 |
throw_NotImplemented( |
| 767 |
"Caching for unstructured grids or structured grids with projections not yet implemented", Here()); |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
// -------------------------------------------------------------------------------------------------------------------- |
| 773 |
|
| 774 |
TransLocal::TransLocal(const Grid& grid, const long truncation, const eckit::Configuration& config): |
| 775 |
TransLocal(Cache(), grid, grid.domain(), truncation, config) {} |
| 776 |
|
| 777 |
TransLocal::TransLocal(const Grid& grid, const Domain& domain, const long truncation, |
| 778 |
const eckit::Configuration& config): |
| 779 |
TransLocal(Cache(), grid, domain, truncation, config) {} |
| 780 |
|
| 781 |
TransLocal::TransLocal(const Cache& cache, const Grid& grid, const long truncation, const eckit::Configuration& config): |
| 782 |
TransLocal(cache, grid, grid.domain(), truncation, config) {} |
| 783 |
|
| 784 |
// -------------------------------------------------------------------------------------------------------------------- |
| 785 |
|
| 786 |
TransLocal::~TransLocal() { |
| 787 |
if (StructuredGrid(grid_) && not grid_.projection()) { |
| 788 |
if (not legendre_cache_) { |
| 789 |
free_aligned(legendre_sym_, "symmetric"); |
| 790 |
free_aligned(legendre_asym_, "asymmetric"); |
| 791 |
} |
| 792 |
if (useFFT_) { |
| 793 |
free_aligned(fft_data_->in); |
| 794 |
free_aligned(fft_data_->out); |
| 795 |
} |
| 796 |
else { |
| 797 |
free_aligned(fourier_, "Fourier coeffs."); |
| 798 |
} |
| 799 |
} |
| 800 |
else { |
| 801 |
if (unstruct_precomp_) { |
| 802 |
free_aligned(legendre_, "Legendre coeffs."); |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
// -------------------------------------------------------------------------------------------------------------------- |
| 808 |
|
| 809 |
const functionspace::Spectral& TransLocal::spectral() const { |
| 810 |
if (not spectral_) { |
| 811 |
spectral_ = functionspace::Spectral( truncation() ); |
| 812 |
} |
| 813 |
return spectral_; |
| 814 |
} |
| 815 |
|
| 816 |
// -------------------------------------------------------------------------------------------------------------------- |
| 817 |
|
| 818 |
void TransLocal::invtrans(const Field& spfield, Field& gpfield, const eckit::Configuration& config) const { |
| 819 |
// VERY PRELIMINARY IMPLEMENTATION WITHOUT ANY GUARANTEES |
| 820 |
int nb_scalar_fields = 1; |
| 821 |
ATLAS_ASSERT(spfield.rank() == 1, "Only rank-1 fields supported at the moment"); |
| 822 |
ATLAS_ASSERT(gpfield.rank() == 1, "Only rank-1 fields supported at the moment"); |
| 823 |
const auto scalar_spectra = array::make_view<double, 1>(spfield); |
| 824 |
auto gp_fields = array::make_view<double, 1>(gpfield); |
| 825 |
|
| 826 |
if (gp_fields.shape(0) < grid().size()) { |
| 827 |
// Hopefully the halo (if present) is appended |
| 828 |
ATLAS_DEBUG_VAR(gp_fields.shape(0)); |
| 829 |
ATLAS_DEBUG_VAR(grid().size()); |
| 830 |
ATLAS_ASSERT(gp_fields.shape(0) < grid().size()); |
| 831 |
} |
| 832 |
|
| 833 |
invtrans(nb_scalar_fields, scalar_spectra.data(), gp_fields.data(), config); |
| 834 |
} |
| 835 |
|
| 836 |
// -------------------------------------------------------------------------------------------------------------------- |
| 837 |
|
| 838 |
void TransLocal::invtrans(const FieldSet& spfields, FieldSet& gpfields, const eckit::Configuration& config) const { |
| 839 |
// VERY PRELIMINARY IMPLEMENTATION WITHOUT ANY GUARANTEES |
| 840 |
ATLAS_ASSERT(spfields.size() == gpfields.size()); |
| 841 |
for (idx_t f = 0; f < spfields.size(); ++f) { |
| 842 |
invtrans(spfields[f], gpfields[f], config); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
// -------------------------------------------------------------------------------------------------------------------- |
| 847 |
|
| 848 |
void TransLocal::invtrans_grad(const Field& /*spfield*/, Field& /*gradfield*/, const eckit::Configuration&) const { |
| 849 |
ATLAS_NOTIMPLEMENTED; |
| 850 |
} |
| 851 |
|
| 852 |
// -------------------------------------------------------------------------------------------------------------------- |
| 853 |
|
| 854 |
void TransLocal::invtrans_grad(const FieldSet& /*spfields*/, FieldSet& /*gradfields*/, |
| 855 |
const eckit::Configuration&) const { |
| 856 |
ATLAS_NOTIMPLEMENTED; |
| 857 |
} |
| 858 |
|
| 859 |
// -------------------------------------------------------------------------------------------------------------------- |
| 860 |
|
| 861 |
void gp_transpose(const int nb_size, const int nb_fields, const double gp_tmp[], double gp_fields[]) { |
| 862 |
for (int jgp = 0; jgp < nb_size; jgp++) { |
| 863 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 864 |
gp_fields[jfld * nb_size + jgp] = gp_tmp[jgp * nb_fields + jfld]; |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
// -------------------------------------------------------------------------------------------------------------------- |
| 870 |
|
| 871 |
void TransLocal::invtrans_vordiv2wind(const Field& spvor, const Field& spdiv, Field& gpwind, |
| 872 |
const eckit::Configuration& config) const { |
| 873 |
// VERY PRELIMINARY IMPLEMENTATION WITHOUT ANY GUARANTEES |
| 874 |
ATLAS_ASSERT(spvor.rank() == 1, "Only rank-1 fields supported at the moment"); |
| 875 |
ATLAS_ASSERT(spdiv.rank() == 1, "Only rank-1 fields supported at the moment"); |
| 876 |
int nb_vordiv_fields = 1; |
| 877 |
const auto vorticity_spectra = array::make_view<double, 1>(spvor); |
| 878 |
const auto divergence_spectra = array::make_view<double, 1>(spdiv); |
| 879 |
auto gp_fields = array::make_view<double, 2>(gpwind); |
| 880 |
|
| 881 |
size_t spectral_data_size = 2 * legendre_size(truncation_) * nb_vordiv_fields; |
| 882 |
ATLAS_ASSERT(vorticity_spectra.size() == spectral_data_size); |
| 883 |
ATLAS_ASSERT(divergence_spectra.size() == spectral_data_size); |
| 884 |
|
| 885 |
if (gp_fields.shape(1) == grid().size() && gp_fields.shape(0) == 2) { |
| 886 |
invtrans(nb_vordiv_fields, vorticity_spectra.data(), divergence_spectra.data(), gp_fields.data(), config); |
| 887 |
} |
| 888 |
else if (gp_fields.shape(0) == grid().size() && gp_fields.shape(1) == 2) { |
| 889 |
array::ArrayT<double> gpwind_t(gp_fields.shape(1), gp_fields.shape(0)); |
| 890 |
auto gp_fields_t = array::make_view<double, 2>(gpwind_t); |
| 891 |
invtrans(nb_vordiv_fields, vorticity_spectra.data(), divergence_spectra.data(), gp_fields_t.data(), config); |
| 892 |
gp_transpose(grid().size(), 2, gp_fields_t.data(), gp_fields.data()); |
| 893 |
} |
| 894 |
else { |
| 895 |
ATLAS_NOTIMPLEMENTED; |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
void TransLocal::invtrans_adj(const Field& gpfield, Field& spfield, const eckit::Configuration& config) const { |
| 900 |
ATLAS_NOTIMPLEMENTED; |
| 901 |
} |
| 902 |
|
| 903 |
// -------------------------------------------------------------------------------------------------------------------- |
| 904 |
|
| 905 |
void TransLocal::invtrans_adj(const FieldSet& gpfields, FieldSet& spfields, const eckit::Configuration& config) const { |
| 906 |
ATLAS_NOTIMPLEMENTED; |
| 907 |
} |
| 908 |
|
| 909 |
// -------------------------------------------------------------------------------------------------------------------- |
| 910 |
|
| 911 |
void TransLocal::invtrans_grad_adj(const Field& /*gradfield*/, Field& /*spfield*/, const eckit::Configuration&) const { |
| 912 |
ATLAS_NOTIMPLEMENTED; |
| 913 |
} |
| 914 |
|
| 915 |
// -------------------------------------------------------------------------------------------------------------------- |
| 916 |
|
| 917 |
void TransLocal::invtrans_grad_adj(const FieldSet& /*gradfields*/, FieldSet& /*spfields*/, |
| 918 |
const eckit::Configuration&) const { |
| 919 |
ATLAS_NOTIMPLEMENTED; |
| 920 |
} |
| 921 |
|
| 922 |
// -------------------------------------------------------------------------------------------------------------------- |
| 923 |
|
| 924 |
void TransLocal::invtrans_vordiv2wind_adj(const Field& gpwind, Field& spvor, Field& spdiv, |
| 925 |
const eckit::Configuration& config) const { |
| 926 |
ATLAS_NOTIMPLEMENTED; |
| 927 |
} |
| 928 |
|
| 929 |
// -------------------------------------------------------------------------------------------------------------------- |
| 930 |
|
| 931 |
void TransLocal::invtrans(const int nb_scalar_fields, const double scalar_spectra[], double gp_fields[], |
| 932 |
const eckit::Configuration& config) const { |
| 933 |
invtrans_uv(truncation_, nb_scalar_fields, 0, scalar_spectra, gp_fields, config); |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
// -------------------------------------------------------------------------------------------------------------------- |
| 938 |
|
| 939 |
void TransLocal::invtrans_legendre(const int truncation, const int nlats, const int nb_fields, |
| 940 |
const int /*nb_vordiv_fields*/, const double scalar_spectra[], double scl_fourier[], |
| 941 |
const eckit::Configuration&) const { |
| 942 |
// Legendre transform: |
| 943 |
{ |
| 944 |
Log::debug() << "TransLocal::invtrans_legendre: Legendre GEMM with \"" << detect_linalg_backend(linalg_backend_) |
| 945 |
<< "\" using " << nlatsLegReduced_ - nlat0_[0] << " latitudes out of " << nlatsGlobal_ / 2 |
| 946 |
<< std::endl; |
| 947 |
linalg::dense::Backend linalg_backend{linalg_backend_}; |
| 948 |
ATLAS_TRACE("Inverse Legendre Transform (GEMM)"); |
| 949 |
for (int jm = 0; jm <= truncation_; jm++) { |
| 950 |
size_t size_sym = num_n(truncation_ + 1, jm, true); |
| 951 |
size_t size_asym = num_n(truncation_ + 1, jm, false); |
| 952 |
const int n_imag = (jm ? 2 : 1); |
| 953 |
int size_fourier = nb_fields * n_imag * (nlatsLegReduced_ - nlat0_[jm]); |
| 954 |
if (size_fourier > 0) { |
| 955 |
auto posFourier = [&](int jfld, int imag, int jlat, int jm, int nlatsH) { |
| 956 |
return jfld + nb_fields * (imag + n_imag * (nlatsLegReduced_ - nlat0_[jm] - nlatsH + jlat)); |
| 957 |
}; |
| 958 |
// THESE ARE REALLOCATED FOR EACH jm ??? |
| 959 |
// THEY COULD ALLOCATED ONCE BEFORE jm LOOP WITH A MAXIMUM SIZE? |
| 960 |
double* scalar_sym; |
| 961 |
double* scalar_asym; |
| 962 |
double* scl_fourier_sym; |
| 963 |
double* scl_fourier_asym; |
| 964 |
alloc_aligned(scalar_sym, n_imag * nb_fields * size_sym); |
| 965 |
alloc_aligned(scalar_asym, n_imag * nb_fields * size_asym); |
| 966 |
alloc_aligned(scl_fourier_sym, size_fourier); |
| 967 |
alloc_aligned(scl_fourier_asym, size_fourier); |
| 968 |
{ |
| 969 |
//ATLAS_TRACE( "Legendre split" ); |
| 970 |
idx_t idx = 0, is = 0, ia = 0, ioff = (2 * truncation + 3 - jm) * jm / 2 * nb_fields * 2; |
| 971 |
// the choice between the following two code lines determines whether |
| 972 |
// total wavenumbers are summed in an ascending or descending order. |
| 973 |
// The trans library in IFS uses descending order because it should |
| 974 |
// be more accurate (higher wavenumbers have smaller contributions). |
| 975 |
// This also needs to be changed when splitting the spectral data in |
| 976 |
// compute_legendre_polynomials! |
| 977 |
//for ( int jn = jm; jn <= truncation_ + 1; jn++ ) { |
| 978 |
for (int jn = truncation_ + 1; jn >= jm; jn--) { |
| 979 |
for (int imag = 0; imag < n_imag; imag++) { |
| 980 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 981 |
idx = jfld + nb_fields * (imag + 2 * (jn - jm)); |
| 982 |
if (jn <= truncation && jm < truncation) { |
| 983 |
if ((jn - jm) % 2 == 0) { |
| 984 |
scalar_sym[is++] = scalar_spectra[idx + ioff]; |
| 985 |
} |
| 986 |
else { |
| 987 |
scalar_asym[ia++] = scalar_spectra[idx + ioff]; |
| 988 |
} |
| 989 |
} |
| 990 |
else { |
| 991 |
if ((jn - jm) % 2 == 0) { |
| 992 |
scalar_sym[is++] = 0.; |
| 993 |
} |
| 994 |
else { |
| 995 |
scalar_asym[ia++] = 0.; |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
ATLAS_ASSERT(size_t(ia) == n_imag * nb_fields * size_asym && |
| 1002 |
size_t(is) == n_imag * nb_fields * size_sym); |
| 1003 |
} |
| 1004 |
if (nlatsLegReduced_ - nlat0_[jm] > 0) { |
| 1005 |
ATLAS_TRACE("matrix_multiply (" + std::string(linalg_backend) + ")"); |
| 1006 |
{ |
| 1007 |
linalg::Matrix A(scalar_sym, nb_fields * n_imag, size_sym); |
| 1008 |
linalg::Matrix B(legendre_sym_ + legendre_sym_begin_[jm] + nlat0_[jm] * size_sym, size_sym, |
| 1009 |
nlatsLegReduced_ - nlat0_[jm]); |
| 1010 |
linalg::Matrix C(scl_fourier_sym, nb_fields * n_imag, nlatsLegReduced_ - nlat0_[jm]); |
| 1011 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1012 |
/*Log::info() << "sym: "; |
| 1013 |
for ( int j = 0; j < size_sym * ( nlatsLegReduced_ - nlat0_[jm] ); j++ ) { |
| 1014 |
Log::info() << legendre_sym_[j + legendre_sym_begin_[jm] + nlat0_[jm] * size_sym] << " "; |
| 1015 |
} |
| 1016 |
Log::info() << std::endl;*/ |
| 1017 |
} |
| 1018 |
if (size_asym > 0) { |
| 1019 |
linalg::Matrix A(scalar_asym, nb_fields * n_imag, size_asym); |
| 1020 |
linalg::Matrix B(legendre_asym_ + legendre_asym_begin_[jm] + nlat0_[jm] * size_asym, size_asym, |
| 1021 |
nlatsLegReduced_ - nlat0_[jm]); |
| 1022 |
linalg::Matrix C(scl_fourier_asym, nb_fields * n_imag, nlatsLegReduced_ - nlat0_[jm]); |
| 1023 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1024 |
/*Log::info() << "asym: "; |
| 1025 |
for ( int j = 0; j < size_asym * ( nlatsLegReduced_ - nlat0_[jm] ); j++ ) { |
| 1026 |
Log::info() << legendre_asym_[j + legendre_asym_begin_[jm] + nlat0_[jm] * size_asym] << " "; |
| 1027 |
} |
| 1028 |
Log::info() << std::endl;*/ |
| 1029 |
} |
| 1030 |
} |
| 1031 |
{ |
| 1032 |
//ATLAS_TRACE( "merge spheres" ); |
| 1033 |
// northern hemisphere: |
| 1034 |
for (int jlat = 0; jlat < nlatsNH_; jlat++) { |
| 1035 |
if (nlatsLegReduced_ - nlat0_[jm] - nlatsNH_ + jlat >= 0) { |
| 1036 |
for (int imag = 0; imag < n_imag; imag++) { |
| 1037 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1038 |
int idx = posFourier(jfld, imag, jlat, jm, nlatsNH_); |
| 1039 |
scl_fourier[posMethod(jfld, imag, jlat, jm, nb_fields, nlats)] = |
| 1040 |
scl_fourier_sym[idx] + scl_fourier_asym[idx]; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
else { |
| 1045 |
for (int imag = 0; imag < n_imag; imag++) { |
| 1046 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1047 |
scl_fourier[posMethod(jfld, imag, jlat, jm, nb_fields, nlats)] = 0.; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
/*for ( int imag = 0; imag < n_imag; imag++ ) { |
| 1052 |
for ( int jfld = 0; jfld < nb_fields; jfld++ ) { |
| 1053 |
if ( scl_fourier[posMethod( jfld, imag, jlat, jm, nb_fields, nlats )] > 0. ) { |
| 1054 |
Log::info() << "jm=" << jm << " jlat=" << jlat << " nlatsLeg_=" << nlatsLeg_ |
| 1055 |
<< " nlat0=" << nlat0_[jm] << " nlatsNH=" << nlatsNH_ << std::endl; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
}*/ |
| 1059 |
} |
| 1060 |
// southern hemisphere: |
| 1061 |
for (int jlat = 0; jlat < nlatsSH_; jlat++) { |
| 1062 |
int jslat = nlats - jlat - 1; |
| 1063 |
if (nlatsLegReduced_ - nlat0_[jm] - nlatsSH_ + jlat >= 0) { |
| 1064 |
for (int imag = 0; imag < n_imag; imag++) { |
| 1065 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1066 |
int idx = posFourier(jfld, imag, jlat, jm, nlatsSH_); |
| 1067 |
scl_fourier[posMethod(jfld, imag, jslat, jm, nb_fields, nlats)] = |
| 1068 |
scl_fourier_sym[idx] - scl_fourier_asym[idx]; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else { |
| 1073 |
for (int imag = 0; imag < n_imag; imag++) { |
| 1074 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1075 |
scl_fourier[posMethod(jfld, imag, jslat, jm, nb_fields, nlats)] = 0.; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
free_aligned(scalar_sym); |
| 1082 |
free_aligned(scalar_asym); |
| 1083 |
free_aligned(scl_fourier_sym); |
| 1084 |
free_aligned(scl_fourier_asym); |
| 1085 |
} |
| 1086 |
else { |
| 1087 |
for (int jlat = 0; jlat < nlats; jlat++) { |
| 1088 |
for (int imag = 0; imag < n_imag; imag++) { |
| 1089 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1090 |
scl_fourier[posMethod(jfld, imag, jlat, jm, nb_fields, nlats)] = 0.; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1100 |
|
| 1101 |
void TransLocal::invtrans_fourier_regular(const int nlats, const int nlons, const int nb_fields, double scl_fourier[], |
| 1102 |
double gp_fields[], const eckit::Configuration&) const { |
| 1103 |
// Fourier transformation: |
| 1104 |
if (useFFT_) { |
| 1105 |
int num_complex = (nlonsMaxGlobal_ / 2) + 1; |
| 1106 |
{ |
| 1107 |
ATLAS_TRACE("Inverse Fourier Transform ("+fft_->type()+", RegularGrid)"); |
| 1108 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1109 |
int idx = 0; |
| 1110 |
for (int jlat = 0; jlat < nlats; jlat++) { |
| 1111 |
fft_data_->in[idx++] = std::complex<double>{scl_fourier[posMethod(jfld, 0, jlat, 0, nb_fields, nlats)], 0.}; |
| 1112 |
for (int jm = 1; jm < num_complex; jm++, idx++) { |
| 1113 |
auto& complex_in = reinterpret_cast<std::array<double,2>&>(fft_data_->in[idx]); |
| 1114 |
for (int imag = 0; imag < 2; imag++) { |
| 1115 |
if (jm <= truncation_) { |
| 1116 |
complex_in[imag] = |
| 1117 |
scl_fourier[posMethod(jfld, imag, jlat, jm, nb_fields, nlats)]; |
| 1118 |
} |
| 1119 |
else { |
| 1120 |
complex_in[imag] = 0.; |
| 1121 |
} |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
fft_->inverse_c2r_many(nlats, nlonsMaxGlobal_, fft_data_->in, fft_data_->out); |
| 1126 |
for (int jlat = 0; jlat < nlats; jlat++) { |
| 1127 |
for (int jlon = 0; jlon < nlons; jlon++) { |
| 1128 |
int j = jlon + jlonMin_[0]; |
| 1129 |
if (j >= nlonsMaxGlobal_) { |
| 1130 |
j -= nlonsMaxGlobal_; |
| 1131 |
} |
| 1132 |
gp_fields[jlon + nlons * (jlat + nlats * jfld)] = fft_data_->out[j + nlonsMaxGlobal_ * jlat]; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
} |
| 1137 |
} |
| 1138 |
else { |
| 1139 |
linalg::dense::Backend linalg_backend{linalg_backend_}; |
| 1140 |
// dgemm-method 1 |
| 1141 |
{ |
| 1142 |
ATLAS_TRACE("Inverse Fourier Transform (NoFFT,matrix_multiply=" + detect_linalg_backend(linalg_backend_) + |
| 1143 |
")"); |
| 1144 |
linalg::Matrix A(fourier_, nlons, (truncation_ + 1) * 2); |
| 1145 |
linalg::Matrix B(scl_fourier, (truncation_ + 1) * 2, nb_fields * nlats); |
| 1146 |
linalg::Matrix C(gp_fields, nlons, nb_fields * nlats); |
| 1147 |
|
| 1148 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1154 |
|
| 1155 |
void TransLocal::invtrans_fourier_reduced(const int nlats, const StructuredGrid& g, const int nb_fields, |
| 1156 |
double scl_fourier[], double gp_fields[], const eckit::Configuration&) const { |
| 1157 |
// Fourier transformation: |
| 1158 |
if (useFFT_) { |
| 1159 |
ATLAS_TRACE("Inverse Fourier Transform ("+fft_->type()+", ReducedGrid)"); |
| 1160 |
int jgp = 0; |
| 1161 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1162 |
for (int jlat = 0; jlat < nlats; jlat++) { |
| 1163 |
int idx = 0; |
| 1164 |
int num_complex = (nlonsGlobal_[jlat] / 2) + 1; |
| 1165 |
fft_data_->in[idx++] = std::complex<double>{scl_fourier[posMethod(jfld, 0, jlat, 0, nb_fields, nlats)], 0.}; |
| 1166 |
|
| 1167 |
for (int jm = 1; jm < num_complex; jm++, idx++) { |
| 1168 |
auto& complex_in = reinterpret_cast<std::array<double,2>&>(fft_data_->in[idx]); |
| 1169 |
for (int imag = 0; imag < 2; imag++) { |
| 1170 |
if (jm <= truncation_) { |
| 1171 |
complex_in[imag] = |
| 1172 |
scl_fourier[posMethod(jfld, imag, jlat, jm, nb_fields, nlats)]; |
| 1173 |
} |
| 1174 |
else { |
| 1175 |
complex_in[imag] = 0.; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
fft_->inverse_c2r(nlonsGlobal_[jlat], fft_data_->in, fft_data_->out); |
| 1180 |
for (int jlon = 0; jlon < g.nx(jlat); jlon++) { |
| 1181 |
int j = jlon + jlonMin_[jlat]; |
| 1182 |
if (j >= nlonsGlobal_[jlat]) { |
| 1183 |
j -= nlonsGlobal_[jlat]; |
| 1184 |
} |
| 1185 |
ATLAS_ASSERT(j < nlonsMaxGlobal_); |
| 1186 |
gp_fields[jgp++] = fft_data_->out[j]; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
} |
| 1190 |
} |
| 1191 |
else { |
| 1192 |
throw_NotImplemented( |
| 1193 |
"Using dgemm in Fourier transform for reduced grids is extremely slow.", |
| 1194 |
Here()); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1199 |
|
| 1200 |
void TransLocal::invtrans_unstructured_precomp(const int truncation, const int nb_fields, const int nb_vordiv_fields, |
| 1201 |
const double scalar_spectra[], double gp_fields[], |
| 1202 |
const eckit::Configuration&) const { |
| 1203 |
ATLAS_TRACE("invtrans_uv unstructured"); |
| 1204 |
|
| 1205 |
const int nlats = grid_.size(); |
| 1206 |
const int size_fourier = nb_fields * 2; |
| 1207 |
double* scl_fourier; |
| 1208 |
double* scl_fourier_tp; |
| 1209 |
double* fouriertp; |
| 1210 |
double* gp_opt; |
| 1211 |
alloc_aligned(scl_fourier, size_fourier * (truncation)*nlats); |
| 1212 |
alloc_aligned(scl_fourier_tp, size_fourier * (truncation)); |
| 1213 |
alloc_aligned(fouriertp, 2 * (truncation)); |
| 1214 |
alloc_aligned(gp_opt, nb_fields); |
| 1215 |
|
| 1216 |
linalg::dense::Backend linalg_backend{linalg_backend_}; |
| 1217 |
|
| 1218 |
{ |
| 1219 |
ATLAS_TRACE("Inverse Legendre Transform (GEMM," + std::string(linalg_backend) + ")"); |
| 1220 |
for (int jm = 0; jm < truncation; jm++) { |
| 1221 |
const int noff = (2 * truncation + 3 - jm) * jm / 2, ns = truncation - jm + 1; |
| 1222 |
linalg::Matrix A( |
| 1223 |
eckit::linalg::Matrix(const_cast<double*>(scalar_spectra) + nb_fields * 2 * noff, nb_fields * 2, ns)); |
| 1224 |
linalg::Matrix B(legendre_ + noff * nlats, ns, nlats); |
| 1225 |
linalg::Matrix C(scl_fourier + jm * size_fourier * nlats, nb_fields * 2, nlats); |
| 1226 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
// loop over all points: |
| 1231 |
{ |
| 1232 |
ATLAS_TRACE("Inverse Fourier Transform (NoFFT," + std::string(linalg_backend) + ")"); |
| 1233 |
int ip = 0; |
| 1234 |
for (const PointLonLat p : grid_.lonlat()) { |
| 1235 |
const double lon = p.lon() * util::Constants::degreesToRadians(); |
| 1236 |
const double lat = p.lat() * util::Constants::degreesToRadians(); |
| 1237 |
{ |
| 1238 |
//ATLAS_TRACE( "opt transposition in Fourier" ); |
| 1239 |
for (int jm = 0; jm < truncation; jm++) { |
| 1240 |
int idx = nb_fields * 2 * (ip + nlats * jm); |
| 1241 |
for (int imag = 0; imag < 2; imag++) { |
| 1242 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1243 |
const int pos_tp = imag + 2 * (jm + (truncation) * (jfld)); |
| 1244 |
//int pos = jfld + nb_fields * ( imag + 2 * ( jm ) ); |
| 1245 |
scl_fourier_tp[pos_tp] = scl_fourier[idx++]; // = scl_fourier[pos] |
| 1246 |
} |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
// Fourier transformation: |
| 1252 |
{ |
| 1253 |
//ATLAS_TRACE( "opt compute fouriertp" ); |
| 1254 |
int idx = 0; |
| 1255 |
fouriertp[idx++] = 1.; // real part |
| 1256 |
fouriertp[idx++] = 0.; // imaginary part |
| 1257 |
for (int jm = 1; jm < truncation; jm++) { |
| 1258 |
fouriertp[idx++] = +2. * std::cos(jm * lon); // real part |
| 1259 |
fouriertp[idx++] = -2. * std::sin(jm * lon); // imaginary part |
| 1260 |
} |
| 1261 |
} |
| 1262 |
{ |
| 1263 |
//ATLAS_TRACE( "opt Fourier dgemm" ); |
| 1264 |
linalg::Matrix A(fouriertp, 1, (truncation)*2); |
| 1265 |
linalg::Matrix B(scl_fourier_tp, (truncation)*2, nb_fields); |
| 1266 |
linalg::Matrix C(gp_opt, 1, nb_fields); |
| 1267 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1268 |
for (int j = 0; j < nb_fields; j++) { |
| 1269 |
gp_fields[ip + j * grid_.size()] = gp_opt[j]; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
// Computing u,v from U,V: |
| 1273 |
{ |
| 1274 |
if (nb_vordiv_fields > 0) { |
| 1275 |
//ATLAS_TRACE( " u,v from U,V" ); |
| 1276 |
double coslat = std::cos(lat); |
| 1277 |
for (int j = 0; j < 2 * nb_vordiv_fields && j < nb_fields; j++) { |
| 1278 |
gp_fields[ip + j * grid_.size()] /= coslat; |
| 1279 |
} |
| 1280 |
} |
| 1281 |
} |
| 1282 |
++ip; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
free_aligned(scl_fourier); |
| 1286 |
free_aligned(scl_fourier_tp); |
| 1287 |
free_aligned(fouriertp); |
| 1288 |
free_aligned(gp_opt); |
| 1289 |
} |
| 1290 |
|
| 1291 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1292 |
|
| 1293 |
void TransLocal::invtrans_unstructured(const int truncation, const int nb_fields, const int nb_vordiv_fields, |
| 1294 |
const double scalar_spectra[], double gp_fields[], |
| 1295 |
const eckit::Configuration& config) const { |
| 1296 |
ATLAS_TRACE("invtrans_unstructured"); |
| 1297 |
|
| 1298 |
if (warning(config)) { |
| 1299 |
Log::warning() << "WARNING: Spectral transforms could take a long time (unstructured grid approach). Results " |
| 1300 |
"may contain aliasing errors." |
| 1301 |
<< std::endl; |
| 1302 |
} |
| 1303 |
|
| 1304 |
linalg::dense::Backend linalg_backend{linalg_backend_}; |
| 1305 |
|
| 1306 |
double* zfn; |
| 1307 |
alloc_aligned(zfn, (truncation + 1) * (truncation + 1)); |
| 1308 |
compute_zfn(truncation, zfn); |
| 1309 |
int size_fourier = nb_fields * 2; |
| 1310 |
double* legendre; |
| 1311 |
double* scl_fourier; |
| 1312 |
double* scl_fourier_tp; |
| 1313 |
double* fouriertp; |
| 1314 |
double* gp_opt; |
| 1315 |
alloc_aligned(legendre, legendre_size(truncation + 1)); |
| 1316 |
alloc_aligned(scl_fourier, size_fourier * (truncation + 1)); |
| 1317 |
alloc_aligned(scl_fourier_tp, size_fourier * (truncation + 1)); |
| 1318 |
alloc_aligned(fouriertp, 2 * (truncation + 1)); |
| 1319 |
alloc_aligned(gp_opt, nb_fields); |
| 1320 |
|
| 1321 |
|
| 1322 |
// loop over all points: |
| 1323 |
int ip = 0; |
| 1324 |
LegendrePolynomialsWorkspace w{truncation}; |
| 1325 |
for (const PointLonLat p : grid_.lonlat()) { |
| 1326 |
const double lon = p.lon() * util::Constants::degreesToRadians(); |
| 1327 |
const double lat = p.lat() * util::Constants::degreesToRadians(); |
| 1328 |
compute_legendre_polynomials_lat(truncation, lat, legendre, zfn, w); |
| 1329 |
// Legendre transform: |
| 1330 |
{ |
| 1331 |
//ATLAS_TRACE( "opt Legendre dgemm" ); |
| 1332 |
ATLAS_TRACE("Legendre matrix_multiply (" + std::string(linalg_backend) + ")"); |
| 1333 |
for (int jm = 0; jm <= truncation; jm++) { |
| 1334 |
const int noff = (2 * truncation + 3 - jm) * jm / 2, ns = truncation - jm + 1; |
| 1335 |
linalg::Matrix A(eckit::linalg::Matrix(const_cast<double*>(scalar_spectra) + nb_fields * 2 * noff, |
| 1336 |
nb_fields * 2, ns)); |
| 1337 |
linalg::Matrix B(legendre + noff, ns, 1); |
| 1338 |
linalg::Matrix C(scl_fourier + jm * size_fourier, nb_fields * 2, 1); |
| 1339 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1340 |
} |
| 1341 |
} |
| 1342 |
{ |
| 1343 |
//ATLAS_TRACE( "opt transposition in Fourier" ); |
| 1344 |
int idx = 0; |
| 1345 |
for (int jm = 0; jm < truncation + 1; jm++) { |
| 1346 |
for (int imag = 0; imag < 2; imag++) { |
| 1347 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 1348 |
const int pos_tp = imag + 2 * (jm + (truncation + 1) * (jfld)); |
| 1349 |
//int pos = jfld + nb_fields * ( imag + 2 * ( jm ) ); |
| 1350 |
scl_fourier_tp[pos_tp] = scl_fourier[idx++]; // = scl_fourier[pos] |
| 1351 |
} |
| 1352 |
} |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
// Fourier transformation: |
| 1357 |
int idx = 0; |
| 1358 |
fouriertp[idx++] = 1.; // real part |
| 1359 |
fouriertp[idx++] = 0.; // imaginary part |
| 1360 |
for (int jm = 1; jm < truncation + 1; jm++) { |
| 1361 |
fouriertp[idx++] = +2. * std::cos(jm * lon); // real part |
| 1362 |
fouriertp[idx++] = -2. * std::sin(jm * lon); // imaginary part |
| 1363 |
} |
| 1364 |
{ |
| 1365 |
ATLAS_TRACE("Fourier matrix_multiply (" + std::string(linalg_backend) + ")"); |
| 1366 |
linalg::Matrix A(fouriertp, 1, (truncation + 1) * 2); |
| 1367 |
linalg::Matrix B(scl_fourier_tp, (truncation + 1) * 2, nb_fields); |
| 1368 |
linalg::Matrix C(gp_opt, 1, nb_fields); |
| 1369 |
linalg::matrix_multiply(A, B, C, linalg_backend); |
| 1370 |
for (int j = 0; j < nb_fields; j++) { |
| 1371 |
gp_fields[ip + j * grid_.size()] = gp_opt[j]; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
// Computing u,v from U,V: |
| 1375 |
{ |
| 1376 |
if (nb_vordiv_fields > 0) { |
| 1377 |
//ATLAS_TRACE( "u,v from U,V" ); |
| 1378 |
const double coslat = std::cos(lat); |
| 1379 |
for (int j = 0; j < 2 * nb_vordiv_fields && j < nb_fields; j++) { |
| 1380 |
gp_fields[ip + j * grid_.size()] /= coslat; |
| 1381 |
} |
| 1382 |
} |
| 1383 |
} |
| 1384 |
++ip; |
| 1385 |
} |
| 1386 |
free_aligned(legendre); |
| 1387 |
free_aligned(scl_fourier); |
| 1388 |
free_aligned(scl_fourier_tp); |
| 1389 |
free_aligned(fouriertp); |
| 1390 |
free_aligned(gp_opt); |
| 1391 |
free_aligned(zfn); |
| 1392 |
} |
| 1393 |
|
| 1394 |
//----------------------------------------------------------------------------- |
| 1395 |
// Routine to compute the spectral transform by using a Local Fourier transformation |
| 1396 |
// for a grid (same latitude for all longitudes, allows to compute Legendre functions |
| 1397 |
// once for all longitudes). U and v components are divided by cos(latitude) for |
| 1398 |
// nb_vordiv_fields > 0. |
| 1399 |
// |
| 1400 |
// Legendre polynomials are computed up to truncation_+1 to be accurate for vorticity and |
| 1401 |
// divergence computation. The parameter truncation is the truncation used in storing the |
| 1402 |
// spectral data scalar_spectra and can be different from truncation_. If truncation is |
| 1403 |
// larger than truncation_+1 the transform will behave as if the spectral data was truncated |
| 1404 |
// to truncation_+1. |
| 1405 |
// |
| 1406 |
// Author: |
| 1407 |
// Andreas Mueller *ECMWF* |
| 1408 |
// |
| 1409 |
void TransLocal::invtrans_uv(const int truncation, const int nb_scalar_fields, const int nb_vordiv_fields, |
| 1410 |
const double scalar_spectra[], double gp_fields[], |
| 1411 |
const eckit::Configuration& config) const { |
| 1412 |
if (nb_scalar_fields > 0) { |
| 1413 |
int nb_fields = nb_scalar_fields; |
| 1414 |
|
| 1415 |
// Transform |
| 1416 |
if (StructuredGrid(grid_) && not grid_.projection()) { |
| 1417 |
auto g = StructuredGrid(grid_); |
| 1418 |
ATLAS_TRACE("invtrans_uv structured"); |
| 1419 |
int nlats = g.ny(); |
| 1420 |
int nlons = g.nxmax(); |
| 1421 |
int size_fourier_max = nb_fields * 2 * nlats; |
| 1422 |
double* scl_fourier; |
| 1423 |
alloc_aligned(scl_fourier, size_fourier_max * (truncation_ + 1)); |
| 1424 |
|
| 1425 |
// ATLAS-159 workaround begin |
| 1426 |
for (int i = 0; i < size_fourier_max * (truncation_ + 1); ++i) { |
| 1427 |
scl_fourier[i] = 0.; |
| 1428 |
} |
| 1429 |
// ATLAS-159 workaround end |
| 1430 |
|
| 1431 |
// Legendre transformation: |
| 1432 |
invtrans_legendre(truncation, nlats, nb_scalar_fields, nb_vordiv_fields, scalar_spectra, scl_fourier, |
| 1433 |
config); |
| 1434 |
|
| 1435 |
// Fourier transformation: |
| 1436 |
if (RegularGrid(gridGlobal_)) { |
| 1437 |
invtrans_fourier_regular(nlats, nlons, nb_fields, scl_fourier, gp_fields, config); |
| 1438 |
} |
| 1439 |
else { |
| 1440 |
invtrans_fourier_reduced(nlats, g, nb_fields, scl_fourier, gp_fields, config); |
| 1441 |
} |
| 1442 |
|
| 1443 |
// Computing u,v from U,V: |
| 1444 |
{ |
| 1445 |
if (nb_vordiv_fields > 0) { |
| 1446 |
ATLAS_TRACE("compute u,v from U,V"); |
| 1447 |
std::vector<double> coslatinvs(nlats); |
| 1448 |
for (idx_t j = 0; j < nlats; ++j) { |
| 1449 |
double lat = g.y(j); |
| 1450 |
if (lat > latPole) { |
| 1451 |
lat = latPole; |
| 1452 |
} |
| 1453 |
if (lat < -latPole) { |
| 1454 |
lat = -latPole; |
| 1455 |
} |
| 1456 |
double coslat = std::cos(lat * util::Constants::degreesToRadians()); |
| 1457 |
coslatinvs[j] = 1. / coslat; |
| 1458 |
//Log::info() << "lat=" << g.y( j ) << " coslat=" << coslat << std::endl; |
| 1459 |
} |
| 1460 |
int idx = 0; |
| 1461 |
for (idx_t jfld = 0; jfld < 2 * nb_vordiv_fields && jfld < nb_fields; jfld++) { |
| 1462 |
for (idx_t jlat = 0; jlat < g.ny(); jlat++) { |
| 1463 |
for (idx_t jlon = 0; jlon < g.nx(jlat); jlon++) { |
| 1464 |
gp_fields[idx] *= coslatinvs[jlat]; |
| 1465 |
idx++; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
} |
| 1469 |
} |
| 1470 |
} |
| 1471 |
free_aligned(scl_fourier); |
| 1472 |
} |
| 1473 |
else { |
| 1474 |
if (unstruct_precomp_) { |
| 1475 |
invtrans_unstructured_precomp(truncation, nb_scalar_fields, nb_vordiv_fields, scalar_spectra, gp_fields, |
| 1476 |
config); |
| 1477 |
} |
| 1478 |
else { |
| 1479 |
invtrans_unstructured(truncation, nb_scalar_fields, nb_vordiv_fields, scalar_spectra, gp_fields, |
| 1480 |
config); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1487 |
|
| 1488 |
void TransLocal::invtrans(const int nb_vordiv_fields, const double vorticity_spectra[], |
| 1489 |
const double divergence_spectra[], double gp_fields[], |
| 1490 |
const eckit::Configuration& config) const { |
| 1491 |
invtrans(0, nullptr, nb_vordiv_fields, vorticity_spectra, divergence_spectra, gp_fields, config); |
| 1492 |
} |
| 1493 |
|
| 1494 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1495 |
|
| 1496 |
void extend_truncation(const int old_truncation, const int nb_fields, const double old_spectra[], double new_spectra[]) { |
| 1497 |
|
| 1498 |
int new_truncation = old_truncation + 1; |
| 1499 |
int old_size = 2 * legendre_size(old_truncation) * nb_fields; |
| 1500 |
int new_size = 2 * legendre_size(new_truncation) * nb_fields; |
| 1501 |
|
| 1502 |
int k = 0, k_old = 0; |
| 1503 |
for (int m = 0; m <= new_truncation; m++) { // zonal wavenumber |
| 1504 |
for (int n = m; n <= new_truncation; n++) { // total wavenumber |
| 1505 |
for (int imag = 0; imag < 2; imag++) { // imaginary/real part |
| 1506 |
for (int jfld = 0; jfld < nb_fields; jfld++) { // field |
| 1507 |
if (m == new_truncation || n == new_truncation) { |
| 1508 |
new_spectra[k++] = 0.; |
| 1509 |
} |
| 1510 |
else { |
| 1511 |
new_spectra[k++] = old_spectra[k_old++]; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
} |
| 1515 |
} |
| 1516 |
} |
| 1517 |
ATLAS_ASSERT(k==new_size); |
| 1518 |
ATLAS_ASSERT(k_old==old_size); |
| 1519 |
} |
| 1520 |
|
| 1521 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1522 |
|
| 1523 |
void TransLocal::invtrans(const int nb_scalar_fields, const double scalar_spectra[], const int nb_vordiv_fields, |
| 1524 |
const double vorticity_spectra[], const double divergence_spectra[], double gp_fields[], |
| 1525 |
const eckit::Configuration& config) const { |
| 1526 |
int nb_gp = grid_.size(); |
| 1527 |
if (nb_vordiv_fields > 0) { |
| 1528 |
// collect all spectral data into one array "all_spectra": |
| 1529 |
ATLAS_TRACE("TransLocal::invtrans"); |
| 1530 |
int nb_vordiv_spec_ext = 2 * legendre_size(truncation_ + 1) * nb_vordiv_fields; |
| 1531 |
std::vector<double> U_ext; |
| 1532 |
std::vector<double> V_ext; |
| 1533 |
std::vector<double> scalar_ext; |
| 1534 |
if (nb_vordiv_fields > 0) { |
| 1535 |
std::vector<double> vorticity_spectra_extended(nb_vordiv_spec_ext); |
| 1536 |
std::vector<double> divergence_spectra_extended(nb_vordiv_spec_ext); |
| 1537 |
U_ext.resize(nb_vordiv_spec_ext); |
| 1538 |
V_ext.resize(nb_vordiv_spec_ext); |
| 1539 |
|
| 1540 |
{ |
| 1541 |
ATLAS_TRACE("extend vordiv"); |
| 1542 |
// increase truncation in vorticity_spectra and divergence_spectra: |
| 1543 |
extend_truncation(truncation_, nb_vordiv_fields, vorticity_spectra, vorticity_spectra_extended.data()); |
| 1544 |
extend_truncation(truncation_, nb_vordiv_fields, divergence_spectra, |
| 1545 |
divergence_spectra_extended.data()); |
| 1546 |
} |
| 1547 |
|
| 1548 |
{ |
| 1549 |
ATLAS_TRACE("vordiv to UV"); |
| 1550 |
// call vd2uv to compute u and v in spectral space |
| 1551 |
trans::VorDivToUV vordiv_to_UV_ext(truncation_ + 1, option::type("local")); |
| 1552 |
vordiv_to_UV_ext.execute(nb_vordiv_spec_ext, nb_vordiv_fields, vorticity_spectra_extended.data(), |
| 1553 |
divergence_spectra_extended.data(), U_ext.data(), V_ext.data()); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
if (nb_scalar_fields > 0) { |
| 1557 |
int nb_scalar_ext = 2 * legendre_size(truncation_ + 1) * nb_scalar_fields; |
| 1558 |
scalar_ext.resize(nb_scalar_ext); |
| 1559 |
extend_truncation(truncation_, nb_scalar_fields, scalar_spectra, scalar_ext.data()); |
| 1560 |
} |
| 1561 |
int nb_all_fields = 2 * nb_vordiv_fields + nb_scalar_fields; |
| 1562 |
int nb_all_size = 2 * legendre_size(truncation_ + 1) * nb_all_fields; |
| 1563 |
std::vector<double> all_spectra(nb_all_size); |
| 1564 |
int k = 0, i = 0, j = 0, l = 0; |
| 1565 |
{ |
| 1566 |
ATLAS_TRACE("merge all spectra"); |
| 1567 |
for (int m = 0; m <= truncation_ + 1; m++) { // zonal wavenumber |
| 1568 |
for (int n = m; n <= truncation_ + 1; n++) { // total wavenumber |
| 1569 |
for (int imag = 0; imag < 2; imag++) { // imaginary/real part |
| 1570 |
for (int jfld = 0; jfld < nb_vordiv_fields; jfld++) { // vorticity fields |
| 1571 |
all_spectra[k++] = U_ext[i++]; |
| 1572 |
} |
| 1573 |
for (int jfld = 0; jfld < nb_vordiv_fields; jfld++) { // divergence fields |
| 1574 |
all_spectra[k++] = V_ext[j++]; |
| 1575 |
} |
| 1576 |
for (int jfld = 0; jfld < nb_scalar_fields; jfld++) { // scalar fields |
| 1577 |
all_spectra[k++] = scalar_ext[l++]; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
} |
| 1581 |
} |
| 1582 |
} |
| 1583 |
int nb_vordiv_size = 2 * legendre_size(truncation_ + 1) * nb_vordiv_fields; |
| 1584 |
int nb_scalar_size = 2 * legendre_size(truncation_ + 1) * nb_scalar_fields; |
| 1585 |
ATLAS_ASSERT(k == nb_all_size); |
| 1586 |
ATLAS_ASSERT(i == nb_vordiv_size); |
| 1587 |
ATLAS_ASSERT(j == nb_vordiv_size); |
| 1588 |
ATLAS_ASSERT(l == nb_scalar_size); |
| 1589 |
invtrans_uv(truncation_ + 1, nb_all_fields, nb_vordiv_fields, all_spectra.data(), gp_fields, config); |
| 1590 |
} |
| 1591 |
else { |
| 1592 |
if (nb_scalar_fields > 0) { |
| 1593 |
invtrans_uv(truncation_, nb_scalar_fields, 0, scalar_spectra, gp_fields + 2 * nb_gp * nb_vordiv_fields, |
| 1594 |
config); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
} |
| 1598 |
|
| 1599 |
void TransLocal::invtrans_adj(const int nb_scalar_fields, const double gp_fields[], double scalar_spectra[], |
| 1600 |
const eckit::Configuration& config) const { |
| 1601 |
ATLAS_NOTIMPLEMENTED; |
| 1602 |
// Not implemented and not planned. |
| 1603 |
// Use the TransIFS implementation instead. |
| 1604 |
} |
| 1605 |
|
| 1606 |
void TransLocal::invtrans_adj(const int nb_vordiv_fields, const double gp_fields[], double vorticity_spectra[], |
| 1607 |
double divergence_spectra[], const eckit::Configuration& config) const { |
| 1608 |
ATLAS_NOTIMPLEMENTED; |
| 1609 |
// Not implemented and not planned. |
| 1610 |
// Use the TransIFS implementation instead. |
| 1611 |
} |
| 1612 |
|
| 1613 |
void TransLocal::invtrans_adj(const int nb_scalar_fields, const double gp_fields[], const int nb_vordiv_fields, |
| 1614 |
double vorticity_spectra[], double divergence_spectra[], double scalar_spectra[], |
| 1615 |
const eckit::Configuration& config) const { |
| 1616 |
ATLAS_NOTIMPLEMENTED; |
| 1617 |
// Not implemented and not planned. |
| 1618 |
// Use the TransIFS implementation instead. |
| 1619 |
} |
| 1620 |
|
| 1621 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1622 |
|
| 1623 |
void TransLocal::dirtrans(const Field& gpfield, Field& spfield, const eckit::Configuration& config) const { |
| 1624 |
ATLAS_NOTIMPLEMENTED; |
| 1625 |
// Not implemented and not planned. |
| 1626 |
// Use the TransIFS implementation instead. |
| 1627 |
} |
| 1628 |
|
| 1629 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1630 |
|
| 1631 |
void TransLocal::dirtrans(const FieldSet& gpfields, FieldSet& spfields, const eckit::Configuration& config) const { |
| 1632 |
ATLAS_NOTIMPLEMENTED; |
| 1633 |
// Not implemented and not planned. |
| 1634 |
// Use the TransIFS implementation instead. |
| 1635 |
} |
| 1636 |
|
| 1637 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1638 |
|
| 1639 |
void TransLocal::dirtrans_wind2vordiv(const Field& gpwind, Field& spvor, Field& spdiv, |
| 1640 |
const eckit::Configuration& config) const { |
| 1641 |
ATLAS_NOTIMPLEMENTED; |
| 1642 |
// Not implemented and not planned. |
| 1643 |
// Use the TransIFS implementation instead. |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
void TransLocal::dirtrans_adj(const Field& spfield, Field& gpfield, |
| 1648 |
const eckit::Configuration& config) const { |
| 1649 |
ATLAS_NOTIMPLEMENTED; |
| 1650 |
// Not implemented and not planned. |
| 1651 |
// Use the TransIFS implementation instead. |
| 1652 |
} |
| 1653 |
|
| 1654 |
void TransLocal::dirtrans_adj(const FieldSet& spfields, FieldSet& gpfields, |
| 1655 |
const eckit::Configuration& config) const { |
| 1656 |
ATLAS_NOTIMPLEMENTED; |
| 1657 |
// Not implemented and not planned. |
| 1658 |
// Use the TransIFS implementation instead. |
| 1659 |
} |
| 1660 |
|
| 1661 |
void TransLocal::dirtrans_wind2vordiv_adj(const Field& spvor, const Field& spdiv, Field& gpwind, |
| 1662 |
const eckit::Configuration& config) const { |
| 1663 |
ATLAS_NOTIMPLEMENTED; |
| 1664 |
// Not implemented and not planned. |
| 1665 |
// Use the TransIFS implementation instead. |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1670 |
|
| 1671 |
void TransLocal::dirtrans(const int nb_fields, const double scalar_fields[], double scalar_spectra[], |
| 1672 |
const eckit::Configuration&) const { |
| 1673 |
ATLAS_NOTIMPLEMENTED; |
| 1674 |
// Not implemented and not planned. |
| 1675 |
// Use the TransIFS implementation instead. |
| 1676 |
} |
| 1677 |
|
| 1678 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1679 |
|
| 1680 |
void TransLocal::dirtrans(const int nb_fields, const double wind_fields[], double vorticity_spectra[], |
| 1681 |
double divergence_spectra[], const eckit::Configuration&) const { |
| 1682 |
ATLAS_NOTIMPLEMENTED; |
| 1683 |
// Not implemented and not planned. |
| 1684 |
// Use the TransIFS implementation instead. |
| 1685 |
} |
| 1686 |
|
| 1687 |
// -------------------------------------------------------------------------------------------------------------------- |
| 1688 |
|
| 1689 |
} // namespace trans |
| 1690 |
} // namespace atlas |