| 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/VorDivToUVLocal.h" |
| 12 |
#include <cmath> // for std::sqrt |
| 13 |
#include "atlas/functionspace/Spectral.h" |
| 14 |
#include "atlas/runtime/Log.h" |
| 15 |
#include "atlas/runtime/Trace.h" |
| 16 |
#include "atlas/util/Earth.h" |
| 17 |
|
| 18 |
using atlas::FunctionSpace; |
| 19 |
using atlas::functionspace::Spectral; |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace trans { |
| 23 |
|
| 24 |
namespace { |
| 25 |
static VorDivToUVBuilder<VorDivToUVLocal> builder("local"); |
| 26 |
} |
| 27 |
|
| 28 |
// -------------------------------------------------------------------------------------------------------------------- |
| 29 |
// Routine to copy spectral data into internal storage form of IFS trans |
| 30 |
// Ported to C++ by: Andreas Mueller *ECMWF* |
| 31 |
void prfi1b(const int truncation, |
| 32 |
const int km, // zonal wavenumber |
| 33 |
const int nb_fields, // number of fields |
| 34 |
const double rspec[], // spectral data |
| 35 |
double pia[]) // spectral components in data layout of trans library |
| 36 |
{ |
| 37 |
int ilcm = truncation + 1 - km, ioff = (2 * truncation - km + 3) * km, |
| 38 |
nlei1 = truncation + 4 + (truncation + 4 + 1) % 2; |
| 39 |
for (int j = 1; j <= ilcm; j++) { |
| 40 |
int inm = ioff + (ilcm - j) * 2; |
| 41 |
for (int jfld = 0; jfld < nb_fields; jfld++) { |
| 42 |
int ir = 2 * jfld, ii = ir + 1; |
| 43 |
pia[ir * nlei1 + j + 1] = rspec[inm * nb_fields + jfld]; |
| 44 |
pia[ii * nlei1 + j + 1] = rspec[(inm + 1) * nb_fields + jfld]; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
for (int jfld = 0; jfld < 2 * nb_fields; jfld++) { |
| 49 |
pia[jfld * nlei1] = 0.; |
| 50 |
pia[jfld * nlei1 + 1] = 0.; |
| 51 |
pia[jfld * nlei1 + ilcm + 2] = 0.; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// -------------------------------------------------------------------------------------------------------------------- |
| 56 |
// Routine to compute spectral velocities (*cos(latitude)) out of spectral |
| 57 |
// vorticity and divergence |
| 58 |
// Reference: |
| 59 |
// ECMWF Research Department documentation of the IFS |
| 60 |
// Temperton, 1991, MWR 119 p1303 |
| 61 |
// Ported to C++ by: Andreas Mueller *ECMWF* |
| 62 |
void vd2uv(const int truncation, // truncation |
| 63 |
const int nb_vordiv_fields, // number of vorticity and divergence fields |
| 64 |
const double vorticity_spectra[], // spectral data of vorticity |
| 65 |
const double divergence_spectra[], // spectral data of divergence |
| 66 |
double U[], // spectral data of U |
| 67 |
double V[], // spectral data of V |
| 68 |
const eckit::Configuration& config) { |
| 69 |
std::vector<double> repsnm((truncation + 1) * (truncation + 6) / 2); |
| 70 |
int idx = 0; |
| 71 |
int nlei1 = truncation + 4 + (truncation + 4 + 1) % 2; |
| 72 |
double ra = util::Earth::radius(); |
| 73 |
std::vector<double> rlapin(truncation + 3); |
| 74 |
|
| 75 |
{ |
| 76 |
//ATLAS_TRACE( "general setup" ); |
| 77 |
// repsnm: epsilon from eq.(2.12) and (2.13) in [Temperton 1991] |
| 78 |
for (int jm = 0; jm <= truncation; ++jm) { |
| 79 |
for (int jn = jm; jn <= truncation + 2; ++jn, ++idx) { |
| 80 |
repsnm[idx] = std::sqrt((jn * jn - jm * jm) / (4. * jn * jn - 1.)); |
| 81 |
} |
| 82 |
} |
| 83 |
repsnm[0] = 0.; |
| 84 |
|
| 85 |
// rlapin: constant factor from eq.(2.2) and (2.3) in [Temperton 1991] |
| 86 |
for (int jn = 1; jn <= truncation + 2; ++jn) { |
| 87 |
rlapin[jn] = -ra * ra / (jn * (jn + 1.)); |
| 88 |
} |
| 89 |
rlapin[0] = 0.; |
| 90 |
} |
| 91 |
|
| 92 |
// inverse the order of repsnm and rlapin for improved accuracy |
| 93 |
std::vector<double> zepsnm(truncation + 6); |
| 94 |
std::vector<double> zlapin(truncation + 6); |
| 95 |
std::vector<double> zn(truncation + 6); |
| 96 |
for (int km = 0; km <= truncation; ++km) { |
| 97 |
{ |
| 98 |
//ATLAS_TRACE( "current wavenumber setup" ); |
| 99 |
for (int jn = km - 1; jn <= truncation + 2; ++jn) { |
| 100 |
int ij = truncation + 3 - jn; |
| 101 |
if (jn >= 0) { |
| 102 |
zlapin[ij] = rlapin[jn]; |
| 103 |
if (jn < km) { |
| 104 |
zepsnm[ij] = 0.; |
| 105 |
} |
| 106 |
else { |
| 107 |
zepsnm[ij] = repsnm[jn + (2 * truncation - km + 5) * km / 2]; |
| 108 |
} |
| 109 |
} |
| 110 |
else { |
| 111 |
zlapin[ij] = 0.; |
| 112 |
zepsnm[ij] = 0.; |
| 113 |
} |
| 114 |
zn[ij] = jn; |
| 115 |
} |
| 116 |
zn[0] = truncation + 3; |
| 117 |
} |
| 118 |
|
| 119 |
// copy spectral data into internal trans storage: |
| 120 |
std::vector<double> rvor(2 * nb_vordiv_fields * nlei1); |
| 121 |
std::vector<double> rdiv(2 * nb_vordiv_fields * nlei1); |
| 122 |
std::vector<double> ru(2 * nb_vordiv_fields * nlei1); |
| 123 |
std::vector<double> rv(2 * nb_vordiv_fields * nlei1); |
| 124 |
{ |
| 125 |
//ATLAS_TRACE( "copy data to internal storage" ); |
| 126 |
prfi1b(truncation, km, nb_vordiv_fields, vorticity_spectra, rvor.data()); |
| 127 |
prfi1b(truncation, km, nb_vordiv_fields, divergence_spectra, rdiv.data()); |
| 128 |
} |
| 129 |
|
| 130 |
{ |
| 131 |
//ATLAS_TRACE( "actual computation" ); |
| 132 |
// compute eq.(2.12) and (2.13) in [Temperton 1991]: |
| 133 |
if (km == 0) { |
| 134 |
for (int jfld = 0; jfld < nb_vordiv_fields; ++jfld) { |
| 135 |
int ir = 2 * jfld * nlei1 - 1; |
| 136 |
for (int ji = 2; ji < truncation + 4 - km; ++ji) { |
| 137 |
double psiM1 = zn[ji + 1] * zepsnm[ji] * zlapin[ji + 1]; |
| 138 |
double psiP1 = zn[ji - 2] * zepsnm[ji - 1] * zlapin[ji - 1]; |
| 139 |
ru[ir + ji] = +psiM1 * rvor[ir + ji + 1] - psiP1 * rvor[ir + ji - 1]; |
| 140 |
rv[ir + ji] = -psiM1 * rdiv[ir + ji + 1] + psiP1 * rdiv[ir + ji - 1]; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
else { |
| 145 |
for (int jfld = 0; jfld < nb_vordiv_fields; ++jfld) { |
| 146 |
int ir = 2 * jfld * nlei1 - 1, ii = ir + nlei1; |
| 147 |
for (int ji = 2; ji < truncation + 4 - km; ++ji) { |
| 148 |
double chiIm = km * zlapin[ji]; |
| 149 |
double psiM1 = zn[ji + 1] * zepsnm[ji] * zlapin[ji + 1]; |
| 150 |
double psiP1 = zn[ji - 2] * zepsnm[ji - 1] * zlapin[ji - 1]; |
| 151 |
ru[ir + ji] = -chiIm * rdiv[ii + ji] + psiM1 * rvor[ir + ji + 1] - psiP1 * rvor[ir + ji - 1]; |
| 152 |
ru[ii + ji] = +chiIm * rdiv[ir + ji] + psiM1 * rvor[ii + ji + 1] - psiP1 * rvor[ii + ji - 1]; |
| 153 |
rv[ir + ji] = -chiIm * rvor[ii + ji] - psiM1 * rdiv[ir + ji + 1] + psiP1 * rdiv[ir + ji - 1]; |
| 154 |
rv[ii + ji] = +chiIm * rvor[ir + ji] - psiM1 * rdiv[ii + ji + 1] + psiP1 * rdiv[ii + ji - 1]; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
{ |
| 161 |
//ATLAS_TRACE( "copy data back to external storage" ); |
| 162 |
// copy data from internal storage back to external spectral data: |
| 163 |
int ilcm = truncation - km; |
| 164 |
int ioff = (2 * truncation - km + 3) * km; |
| 165 |
// ioff: start index of zonal wavenumber km in spectral data |
| 166 |
double za_r = 1. / util::Earth::radius(); |
| 167 |
for (int j = 0; j <= ilcm; ++j) { |
| 168 |
// ilcm-j = total wavenumber |
| 169 |
int inm = ioff + (ilcm - j) * 2; |
| 170 |
for (int jfld = 0; jfld < nb_vordiv_fields; ++jfld) { |
| 171 |
int ir = 2 * jfld * nlei1, ii = ir + nlei1; |
| 172 |
int idx = inm * nb_vordiv_fields + jfld; |
| 173 |
// real part: |
| 174 |
U[idx] = ru[ir + j + 2] * za_r; |
| 175 |
V[idx] = rv[ir + j + 2] * za_r; |
| 176 |
idx += nb_vordiv_fields; |
| 177 |
// imaginary part: |
| 178 |
U[idx] = ru[ii + j + 2] * za_r; |
| 179 |
V[idx] = rv[ii + j + 2] * za_r; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void VorDivToUVLocal::execute(const int nb_coeff, const int nb_fields, const double vorticity[], |
| 187 |
const double divergence[], double U[], double V[], |
| 188 |
const eckit::Configuration& config) const { |
| 189 |
vd2uv(truncation_, nb_fields, vorticity, divergence, U, V, config); |
| 190 |
} |
| 191 |
|
| 192 |
VorDivToUVLocal::VorDivToUVLocal(const int truncation, const eckit::Configuration& config): truncation_(truncation) {} |
| 193 |
|
| 194 |
VorDivToUVLocal::VorDivToUVLocal(const FunctionSpace& fs, const eckit::Configuration& config): |
| 195 |
truncation_(Spectral(fs).truncation()) {} |
| 196 |
|
| 197 |
VorDivToUVLocal::~VorDivToUVLocal() = default; |
| 198 |
|
| 199 |
} // namespace trans |
| 200 |
} // namespace atlas |