| 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 |
#pragma once |
| 12 |
|
| 13 |
#include <memory> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "atlas/array.h" |
| 17 |
#include "atlas/functionspace/Spectral.h" |
| 18 |
#include "atlas/grid/Grid.h" |
| 19 |
#include "atlas/linalg/dense/Backend.h" |
| 20 |
#include "atlas/trans/detail/TransImpl.h" |
| 21 |
|
| 22 |
//----------------------------------------------------------------------------- |
| 23 |
// Forward declarations |
| 24 |
|
| 25 |
namespace atlas { |
| 26 |
class Field; |
| 27 |
class FieldSet; |
| 28 |
class StructuredGrid; |
| 29 |
} // namespace atlas |
| 30 |
|
| 31 |
namespace atlas::linalg { |
| 32 |
class FFT; |
| 33 |
} |
| 34 |
|
| 35 |
//----------------------------------------------------------------------------- |
| 36 |
|
| 37 |
namespace atlas { |
| 38 |
namespace trans { |
| 39 |
|
| 40 |
namespace detail { |
| 41 |
struct FFT_Data; |
| 42 |
} |
| 43 |
|
| 44 |
class LegendreCacheCreatorLocal; |
| 45 |
int fourier_truncation(const int truncation, // truncation |
| 46 |
const int nx, // number of longitudes |
| 47 |
const int nxmax, // maximum nx |
| 48 |
const int ndgl, // number of latitudes |
| 49 |
const double lat, // latitude in radian |
| 50 |
const bool regular); // regular grid |
| 51 |
|
| 52 |
//----------------------------------------------------------------------------- |
| 53 |
|
| 54 |
/// @class TransLocal |
| 55 |
/// |
| 56 |
/// Local spherical harmonics transformations to any grid |
| 57 |
/// Optimisations are present for structured grids |
| 58 |
/// For global grids, please consider using TransIFS instead. |
| 59 |
/// |
| 60 |
/// @todo: |
| 61 |
/// - support multiple fields |
| 62 |
/// - support atlas::Field and atlas::FieldSet based on function spaces |
| 63 |
/// |
| 64 |
/// @note: Direct transforms are not implemented and cannot be unless |
| 65 |
/// the grid is global. There are no plans to support this at the moment. |
| 66 |
/// |
| 67 |
/// @note: The matrix_multiply (GEMM) implementation can be configured within the Configuration argument in the constructor |
| 68 |
/// using "matrix_multiply" key or if not given, it will use the atlas::linalg::dense::current_backend(), |
| 69 |
/// evaluated at invocation time. To reset the current_backend at any time: |
| 70 |
/// atlas::linalg::dense::current_backend( type ); |
| 71 |
/// Possible values for type or "matrix_multiply" are e.g. |
| 72 |
/// - "eckit_linalg" which uses the configured default backend of eckit::linalg::LinearAlgebra |
| 73 |
/// - "generic" : "generic" backend for eckit::linalg::LinearAlgebra |
| 74 |
/// - "lapack" : "lapack" backend for eckit::linalg::LinearAlgebra |
| 75 |
/// - "openmp" : "openmp" backend for eckit::linalg::LinearAlgebra, or "generic" if "openmp" is not available. |
| 76 |
/// - "eigen" : "eigen" backend for eckit::linalg::LinearAlgebra |
| 77 |
|
| 78 |
class TransLocal : public trans::TransImpl { |
| 79 |
public: |
| 80 |
TransLocal(const Grid&, const long truncation, const eckit::Configuration& = util::NoConfig()); |
| 81 |
TransLocal(const Grid&, const Domain&, const long truncation, const eckit::Configuration& = util::NoConfig()); |
| 82 |
TransLocal(const Cache&, const Grid&, const long truncation, const eckit::Configuration& = util::NoConfig()); |
| 83 |
TransLocal(const Cache&, const Grid&, const Domain&, const long truncation, |
| 84 |
const eckit::Configuration& = util::NoConfig()); |
| 85 |
|
| 86 |
virtual ~TransLocal() override; |
| 87 |
|
| 88 |
virtual int truncation() const override { return truncation_; } |
| 89 |
|
| 90 |
virtual size_t nb_spectral_coefficients() const override { return (truncation_ + 1) * (truncation_ + 2); } |
| 91 |
virtual size_t nb_spectral_coefficients_global() const override { return (truncation_ + 1) * (truncation_ + 2); } |
| 92 |
|
| 93 |
virtual const Grid& grid() const override { return grid_; } |
| 94 |
virtual const functionspace::Spectral& spectral() const override; |
| 95 |
|
| 96 |
virtual void invtrans(const Field& spfield, Field& gpfield, |
| 97 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 98 |
|
| 99 |
virtual void invtrans(const FieldSet& spfields, FieldSet& gpfields, |
| 100 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 101 |
|
| 102 |
virtual void invtrans_grad(const Field& spfield, Field& gradfield, |
| 103 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 104 |
|
| 105 |
virtual void invtrans_grad(const FieldSet& spfields, FieldSet& gradfields, |
| 106 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 107 |
|
| 108 |
virtual void invtrans_vordiv2wind(const Field& spvor, const Field& spdiv, Field& gpwind, |
| 109 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 110 |
|
| 111 |
virtual void invtrans_adj(const Field& gpfield, Field& spfield, |
| 112 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 113 |
|
| 114 |
virtual void invtrans_adj(const FieldSet& gpfields, FieldSet& spfields, |
| 115 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 116 |
|
| 117 |
virtual void invtrans_grad_adj(const Field& gradfield, Field& spfield, |
| 118 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 119 |
|
| 120 |
virtual void invtrans_grad_adj(const FieldSet& spfields, FieldSet& gradfields, |
| 121 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 122 |
|
| 123 |
virtual void invtrans_vordiv2wind_adj(const Field& gpwind, Field& spvor, Field& spdiv, |
| 124 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 125 |
|
| 126 |
// -- IFS style API -- |
| 127 |
|
| 128 |
virtual void invtrans(const int nb_scalar_fields, const double scalar_spectra[], const int nb_vordiv_fields, |
| 129 |
const double vorticity_spectra[], const double divergence_spectra[], double gp_fields[], |
| 130 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 131 |
|
| 132 |
virtual void invtrans(const int nb_scalar_fields, const double scalar_spectra[], double gp_fields[], |
| 133 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 134 |
|
| 135 |
virtual void invtrans(const int nb_vordiv_fields, const double vorticity_spectra[], |
| 136 |
const double divergence_spectra[], double gp_fields[], |
| 137 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 138 |
|
| 139 |
virtual void invtrans_adj(const int nb_scalar_fields, const double gp_fields[], const int nb_vordiv_fields, |
| 140 |
double vorticity_spectra[], double divergence_spectra[], double scalar_spectra[], |
| 141 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 142 |
|
| 143 |
virtual void invtrans_adj(const int nb_scalar_fields, const double gp_fields[], double scalar_spectra[], |
| 144 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 145 |
|
| 146 |
virtual void invtrans_adj(const int nb_vordiv_fields, const double gp_fields[], double vorticity_spectra[], |
| 147 |
double divergence_spectra[], |
| 148 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 149 |
|
| 150 |
// -- NOT SUPPORTED -- // |
| 151 |
|
| 152 |
virtual void dirtrans(const Field& gpfield, Field& spfield, |
| 153 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 154 |
|
| 155 |
virtual void dirtrans(const FieldSet& gpfields, FieldSet& spfields, |
| 156 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 157 |
|
| 158 |
virtual void dirtrans_wind2vordiv(const Field& gpwind, Field& spvor, Field& spdiv, |
| 159 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 160 |
|
| 161 |
virtual void dirtrans_adj(const Field& spfield, Field& gpfield, |
| 162 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 163 |
|
| 164 |
virtual void dirtrans_adj(const FieldSet& spfields, FieldSet& gpfields, |
| 165 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 166 |
|
| 167 |
virtual void dirtrans_wind2vordiv_adj(const Field& spvor, const Field& spdiv, Field& gpwind, |
| 168 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 169 |
|
| 170 |
virtual void dirtrans(const int nb_fields, const double scalar_fields[], double scalar_spectra[], |
| 171 |
const eckit::Configuration& = util::NoConfig()) const override; |
| 172 |
|
| 173 |
virtual void dirtrans(const int nb_fields, const double wind_fields[], double vorticity_spectra[], |
| 174 |
double divergence_spectra[], const eckit::Configuration& = util::NoConfig()) const override; |
| 175 |
|
| 176 |
private: |
| 177 |
inline constexpr int posMethod(const int jfld, const int imag, const int jlat, const int jm, const int nb_fields, |
| 178 |
const int nlats) const { |
| 179 |
return imag + 2 * (jm + (truncation_ + 1) * (jlat + nlats * jfld)); |
| 180 |
} |
| 181 |
|
| 182 |
void invtrans_legendre(const int truncation, const int nlats, const int nb_fields, const int nb_vordiv_fields, |
| 183 |
const double scalar_spectra[], double scl_fourier[], |
| 184 |
const eckit::Configuration& config) const; |
| 185 |
|
| 186 |
void invtrans_fourier_regular(const int nlats, const int nlons, const int nb_fields, double scl_fourier[], |
| 187 |
double gp_fields[], const eckit::Configuration& config) const; |
| 188 |
|
| 189 |
void invtrans_fourier_reduced(const int nlats, const StructuredGrid& g, const int nb_fields, double scl_fourier[], |
| 190 |
double gp_fields[], const eckit::Configuration& config) const; |
| 191 |
|
| 192 |
void invtrans_unstructured_precomp(const int truncation, const int nb_scalar_fields, const int nb_vordiv_fields, |
| 193 |
const double scalar_spectra[], double gp_fields[], |
| 194 |
const eckit::Configuration& = util::NoConfig()) const; |
| 195 |
|
| 196 |
void invtrans_unstructured(const int truncation, const int nb_fields, const int nb_vordiv_fields, |
| 197 |
const double scalar_spectra[], double gp_fields[], |
| 198 |
const eckit::Configuration& config) const; |
| 199 |
|
| 200 |
void invtrans_uv(const int truncation, const int nb_scalar_fields, const int nb_vordiv_fields, |
| 201 |
const double scalar_spectra[], double gp_fields[], |
| 202 |
const eckit::Configuration& = util::NoConfig()) const; |
| 203 |
|
| 204 |
bool warning(const eckit::Configuration& = util::NoConfig()) const; |
| 205 |
|
| 206 |
friend class LegendreCacheCreatorLocal; |
| 207 |
|
| 208 |
private: |
| 209 |
mutable functionspace::Spectral spectral_; |
| 210 |
Grid grid_; |
| 211 |
Grid gridGlobal_; |
| 212 |
bool useFFT_; |
| 213 |
bool dgemmMethod1_; |
| 214 |
bool unstruct_precomp_; |
| 215 |
bool no_symmetry_; |
| 216 |
int truncation_; |
| 217 |
idx_t nlatsNH_; |
| 218 |
idx_t nlatsSH_; |
| 219 |
idx_t nlatsLeg_; |
| 220 |
idx_t nlatsLegReduced_; |
| 221 |
idx_t nlatsLegDomain_; |
| 222 |
std::vector<idx_t> jlonMin_; |
| 223 |
idx_t jlatMin_; |
| 224 |
idx_t jlatMinLeg_; |
| 225 |
int nlonsMaxGlobal_; |
| 226 |
std::vector<idx_t> nlonsGlobal_; |
| 227 |
std::vector<idx_t> nlat0_; |
| 228 |
idx_t nlatsGlobal_; |
| 229 |
bool precompute_; |
| 230 |
double* legendre_; |
| 231 |
double* legendre_sym_; |
| 232 |
double* legendre_asym_; |
| 233 |
double* fourier_; |
| 234 |
double* fouriertp_; |
| 235 |
std::vector<size_t> legendre_sym_begin_; |
| 236 |
std::vector<size_t> legendre_asym_begin_; |
| 237 |
|
| 238 |
Cache cache_; |
| 239 |
Cache export_legendre_; |
| 240 |
const void* legendre_cache_{nullptr}; |
| 241 |
size_t legendre_cachesize_{0}; |
| 242 |
const void* fft_cache_{nullptr}; |
| 243 |
size_t fft_cachesize_{0}; |
| 244 |
|
| 245 |
std::unique_ptr<detail::FFT_Data> fft_data_; |
| 246 |
std::unique_ptr<linalg::FFT> fft_; |
| 247 |
|
| 248 |
std::string linalg_backend_; |
| 249 |
std::string fft_backend_; |
| 250 |
int warning_ = 0; |
| 251 |
}; |
| 252 |
|
| 253 |
//----------------------------------------------------------------------------- |
| 254 |
|
| 255 |
} // namespace trans |
| 256 |
} // namespace atlas |