Debian Codegraph @

src/atlas/trans/local/LegendrePolynomials.cc

raw
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
9 * does it submit to any jurisdiction.
10 */
11
12 #include <cmath>
13 #include <limits>
14
15 #include "atlas/array.h"
16 #include "atlas/parallel/mpi/mpi.h"
17 #include "atlas/trans/local/LegendrePolynomials.h"
18
19 namespace atlas {
20 namespace trans {
21
22 //-----------------------------------------------------------------------------
23
24 void compute_zfn(const int trc, double zfn[]) {
25 auto idxzfn = [&](int jn, int jk) { return jk + (trc + 1) * jn; };
26 int iodd = 0;
27 // Compute coefficients for Taylor series in Belousov (19) and (21)
28 // Belousov, Swarztrauber use zfn[0]=std::sqrt(2.)
29 // IFS normalisation chosen to be 0.5*Integral(Pnm**2) = 1
30 zfn[idxzfn(0, 0)] = 2.;
31 for (int jn = 1; jn <= trc; ++jn) {
32 double zfnn = zfn[idxzfn(0, 0)];
33 for (int jgl = 1; jgl <= jn; ++jgl) {
34 zfnn *= std::sqrt(1. - 0.25 / (jgl * jgl));
35 }
36 iodd = jn % 2;
37 zfn[idxzfn(jn, jn)] = zfnn;
38 for (int jgl = 2; jgl <= jn - iodd; jgl += 2) {
39 double zfjn = ((jgl - 1.) * (2. * jn - jgl + 2.)); // new factor numerator
40 double zfjd = (jgl * (2. * jn - jgl + 1.)); // new factor denominator
41
42 zfn[idxzfn(jn, jn - jgl)] = zfn[idxzfn(jn, jn - jgl + 2)] * zfjn / zfjd;
43 }
44 }
45 }
46
47 void compute_legendre_polynomials_lat(const int trc, // truncation (in)
48 const double lat, // latitude in radians (in)
49 double legpol[], // legendre polynomials
50 double zfn[],
51 LegendrePolynomialsWorkspace& w // workspace to avoid allocations
52 ) {
53 auto idxmn = [&](int jm, int jn) { return (2 * trc + 3 - jm) * jm / 2 + jn - jm; };
54 auto idxzfn = [&](int jn, int jk) { return jk + (trc + 1) * jn; };
55 { //ATLAS_TRACE( "compute Legendre polynomials" );
56 // --------------------
57 // 1. First two columns
58 // --------------------
59 double zdlx1 = (M_PI_2 - lat); // theta
60 double zdlx = std::cos(zdlx1); // cos(theta)
61 volatile double zdlsita = std::sqrt(1. - zdlx * zdlx); // sin(theta) (this is how trans library does it)
62
63 legpol[idxmn(0, 0)] = 1.;
64 w.vsin.resize(trc+1);
65 w.vcos.resize(trc+1);
66 for (int j = 1; j <= trc; j++) {
67 w.vsin[j] = std::sin(j * zdlx1);
68 w.vcos[j] = std::cos(j * zdlx1);
69 }
70
71 double zdl1sita = 0.;
72 // if we are less than 1 meter from the pole,
73 if (std::abs(zdlsita) <= std::sqrt(std::numeric_limits<double>::epsilon())) {
74 zdlx = 1.;
75 zdlsita = 0.;
76 }
77 else {
78 zdl1sita = 1. / zdlsita;
79 }
80
81 // ordinary Legendre polynomials from series expansion
82 // ---------------------------------------------------
83
84 // even N
85 for (int jn = 2; jn <= trc; jn += 2) {
86 double zdlk = 0.5 * zfn[idxzfn(jn, 0)];
87 double zdlldn = 0.0;
88 double zdsq = 1. / std::sqrt(jn * (jn + 1.));
89 // represented by only even k
90 for (int jk = 2; jk <= jn; jk += 2) {
91 // normalised ordinary Legendre polynomial == \overbar{P_n}^0
92 zdlk = zdlk + zfn[idxzfn(jn, jk)] * w.vcos[jk];
93 // normalised associated Legendre polynomial == \overbar{P_n}^1
94 zdlldn = zdlldn + zdsq * zfn[idxzfn(jn, jk)] * jk * w.vsin[jk];
95 }
96 legpol[idxmn(0, jn)] = zdlk;
97 legpol[idxmn(1, jn)] = zdlldn;
98 }
99
100 // odd N
101 for (int jn = 1; jn <= trc; jn += 2) {
102 zfn[idxzfn(jn, 0)] = 0.;
103 double zdlk = 0.;
104 double zdlldn = 0.0;
105 double zdsq = 1. / std::sqrt(jn * (jn + 1.));
106 // represented by only even k
107 for (int jk = 1; jk <= jn; jk += 2) {
108 // normalised ordinary Legendre polynomial == \overbar{P_n}^0
109 zdlk = zdlk + zfn[idxzfn(jn, jk)] * w.vcos[jk];
110 // normalised associated Legendre polynomial == \overbar{P_n}^1
111 zdlldn = zdlldn + zdsq * zfn[idxzfn(jn, jk)] * jk * w.vsin[jk];
112 }
113 legpol[idxmn(0, jn)] = zdlk;
114 legpol[idxmn(1, jn)] = zdlldn;
115 }
116
117 // --------------------------------------------------------------
118 // 2. Diagonal (the terms 0,0 and 1,1 have already been computed)
119 // Belousov, equation (23)
120 // --------------------------------------------------------------
121
122 double zdls = zdl1sita * std::numeric_limits<double>::min();
123 for (int jn = 2; jn <= trc; ++jn) {
124 double sq = std::sqrt((2. * jn + 1.) / (2. * jn));
125
126 legpol[idxmn(jn, jn)] = legpol[idxmn(jn - 1, jn - 1)] * zdlsita * sq;
127 if (std::abs(legpol[idxmn(jn, jn)]) < zdls) {
128 legpol[idxmn(jn, jn)] = 0.0;
129 }
130 }
131
132 // ---------------------------------------------
133 // 3. General recurrence (Belousov, equation 17)
134 // ---------------------------------------------
135
136 for (int jn = 3; jn <= trc; ++jn) {
137 for (int jm = 2; jm < jn; ++jm) {
138 double cn = ((2. * jn + 1.) * (jn + jm - 3.) * (jn + jm - 1.)); // numerator of c in Belousov
139 double cd = ((2. * jn - 3.) * (jn + jm - 2.) * (jn + jm)); // denominator of c in Belousov
140 double dn = ((2. * jn + 1.) * (jn - jm + 1.) * (jn + jm - 1.)); // numerator of d in Belousov
141 double dd = ((2. * jn - 1.) * (jn + jm - 2.) * (jn + jm)); // denominator of d in Belousov
142 double en = ((2. * jn + 1.) * (jn - jm)); // numerator of e in Belousov
143 double ed = ((2. * jn - 1.) * (jn + jm)); // denominator of e in Belousov
144
145 legpol[idxmn(jm, jn)] = std::sqrt(cn / cd) * legpol[idxmn(jm - 2, jn - 2)] -
146 std::sqrt(dn / dd) * legpol[idxmn(jm - 2, jn - 1)] * zdlx +
147 std::sqrt(en / ed) * legpol[idxmn(jm, jn - 1)] * zdlx;
148 }
149 }
150 }
151 }
152
153
154 void compute_legendre_polynomials(
155 const int truncation, // truncation (in)
156 const int nlats, // number of latitudes
157 const double lats[], // latitudes in radians (in)
158 double leg_sym[], // values of associated Legendre functions, symmetric part
159 double leg_asym[], // values of associated Legendre functions, asymmetric part
160 size_t leg_start_sym[], // start indices for different zonal wave numbers, symmetric part
161 size_t leg_start_asym[]) // start indices for different zonal wave numbers, asymmetric part
162 {
163 size_t trc = static_cast<size_t>(truncation);
164 size_t legendre_size = (trc + 2) * (trc + 1) / 2;
165 std::vector<double> legpol(legendre_size);
166 std::vector<double> zfn((trc + 1) * (trc + 1));
167 auto idxmn = [&](size_t jm, size_t jn) { return (2 * trc + 3 - jm) * jm / 2 + jn - jm; };
168 compute_zfn(truncation, zfn.data());
169
170 LegendrePolynomialsWorkspace w{truncation};
171
172 // Loop over latitudes:
173 for (size_t jlat = 0; jlat < size_t(nlats); ++jlat) {
174 // compute legendre polynomials for current latitude:
175 compute_legendre_polynomials_lat(truncation, lats[jlat], legpol.data(), zfn.data(), w);
176
177 // split polynomials into symmetric and antisymmetric parts:
178 {
179 //ATLAS_TRACE( "add to global arrays" );
180
181 for (size_t jm = 0; jm <= trc; jm++) {
182 size_t is1 = 0, ia1 = 0;
183 for (size_t jn = jm; jn <= trc; jn++) {
184 (jn - jm) % 2 ? ia1++ : is1++;
185 }
186
187 size_t is2 = 0, ia2 = 0;
188 // the choice between the following two code lines determines whether
189 // total wavenumbers are summed in an ascending or descending order.
190 // The trans library in IFS uses descending order because it should
191 // be more accurate (higher wavenumbers have smaller contributions).
192 // This also needs to be changed when splitting the spectral data in
193 // TransLocal::invtrans_uv!
194 //for ( int jn = jm; jn <= trc; jn++ ) {
195 for (long ljn = long(trc), ljm = long(jm); ljn >= ljm; ljn--) {
196 size_t jn = size_t(ljn);
197 if ((jn - jm) % 2 == 0) {
198 size_t is = leg_start_sym[jm] + is1 * jlat + is2++;
199 leg_sym[is] = legpol[idxmn(jm, jn)];
200 }
201 else {
202 size_t ia = leg_start_asym[jm] + ia1 * jlat + ia2++;
203 leg_asym[ia] = legpol[idxmn(jm, jn)];
204 }
205 }
206 }
207 }
208 }
209 }
210
211 void compute_legendre_polynomials_all(const int truncation, // truncation (in)
212 const int nlats, // number of latitudes
213 const double lats[], // latitudes in radians (in)
214 double legendre[]) // legendre polynomials for all latitudes
215 {
216 size_t trc = static_cast<size_t>(truncation);
217 size_t legendre_size = (trc + 2) * (trc + 1) / 2;
218 std::vector<double> legpol(legendre_size);
219 std::vector<double> zfn((trc + 1) * (trc + 1));
220 auto idxmn = [&](size_t jm, size_t jn) { return (2 * trc + 3 - jm) * jm / 2 + jn - jm; };
221 auto idxmnl = [&](size_t jm, size_t jn, size_t jlat) {
222 return (2 * trc + 3 - jm) * jm / 2 * nlats + jlat * (trc - jm + 1) + jn - jm;
223 };
224 compute_zfn(truncation, zfn.data());
225
226 LegendrePolynomialsWorkspace w{truncation};
227
228 // Loop over latitudes:
229 for (size_t jlat = 0; jlat < nlats; ++jlat) {
230 // compute legendre polynomials for current latitude:
231 compute_legendre_polynomials_lat(truncation, lats[jlat], legpol.data(), zfn.data(), w);
232
233 for (size_t jm = 0; jm <= trc; ++jm) {
234 for (size_t jn = jm; jn <= trc; ++jn) {
235 legendre[idxmnl(jm, jn, jlat)] = legpol[idxmn(jm, jn)];
236 }
237 }
238 }
239 } // namespace trans
240
241 // --------------------------------------------------------------------------------------------------------------------
242
243 } // namespace trans
244 } // namespace atlas