Debian Codegraph @

src/atlas/grid/detail/spacing/gaussian/Latitudes.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 does it submit to any jurisdiction.
9 */
10
11 /// @file Latitidus.cc
12 /// @author Willem Deconinck
13 /// @date Jan 2014
14
15 #include <limits>
16 #include <memory>
17
18 #include "eckit/log/Bytes.h"
19
20 #include "atlas/grid/detail/spacing/gaussian/Latitudes.h"
21 #include "atlas/grid/detail/spacing/gaussian/N.h"
22 #include "atlas/library/config.h"
23 #include "atlas/runtime/Exception.h"
24 #include "atlas/runtime/Log.h"
25 #include "atlas/runtime/Trace.h"
26 #include "atlas/util/Constants.h"
27 #include "atlas/util/CoordinateEnums.h"
28
29
30 namespace atlas {
31 namespace grid {
32 namespace spacing {
33 namespace gaussian {
34
35 //-----------------------------------------------------------------------------
36
37 void compute_gaussian_quadrature_npole_equator(const size_t N, double lat[], double weights[]);
38
39 //-----------------------------------------------------------------------------
40
41 void gaussian_latitudes_npole_equator(const size_t N, double lats[]) {
42 std::stringstream Nstream;
43 Nstream << N;
44 std::string Nstr = Nstream.str();
45 if (GaussianLatitudesFactory::has(Nstr)) {
46 std::unique_ptr<const GaussianLatitudes> gl(GaussianLatitudesFactory::build(Nstr));
47 gl->assign(lats, N);
48 }
49 // if ( Factory<GaussianLatitudes>::instance().exists( Nstr ) ) {
50 // std::unique_ptr<GaussianLatitudes> gl( Factory<GaussianLatitudes>::instance().get( Nstr ).create() );
51 // gl->assign( lats, N );
52 // }
53 else {
54 std::vector<double> weights(N);
55 compute_gaussian_quadrature_npole_equator(N, lats, weights.data());
56 }
57 }
58
59 //-----------------------------------------------------------------------------
60
61 void gaussian_latitudes_npole_spole(const size_t N, double lats[]) {
62 gaussian_latitudes_npole_equator(N, lats);
63 size_t end = 2 * N - 1;
64 for (size_t jlat = 0; jlat < N; ++jlat) {
65 lats[end--] = -lats[jlat];
66 }
67 }
68
69 //-----------------------------------------------------------------------------
70
71 void gaussian_quadrature_npole_equator(const size_t N, double lats[], double weights[]) {
72 compute_gaussian_quadrature_npole_equator(N, lats, weights);
73 }
74
75 //-----------------------------------------------------------------------------
76
77 void gaussian_quadrature_npole_spole(const size_t N, double lats[], double weights[]) {
78 gaussian_quadrature_npole_equator(N, lats, weights);
79 size_t end = 2 * N - 1;
80 for (size_t jlat = 0; jlat < N; ++jlat) {
81 lats[end] = -lats[jlat];
82 weights[end] = weights[jlat];
83 end--;
84 }
85 }
86
87 //-----------------------------------------------------------------------------
88
89 namespace { // Anonymous namespace
90
91 //-----------------------------------------------------------------------------
92
93 void legpol_newton_iteration(size_t kn, const double pfn[], double px, double& pxn, double& pxmod) {
94 // Routine to perform a single Newton iteration step to find
95 // the zero of the ordinary Legendre polynomial of degree N
96
97 // Explicit arguments :
98 // --------------------
99 // KN : Degree of the Legendre polynomial (in)
100 // KODD : odd or even number of latitudes (in)
101 // PFN : Fourier coefficients of series expansion (in)
102 // for the ordinary Legendre polynomials
103 // PX : abcissa where the computations are performed (in)
104 // PXN : new abscissa (Newton iteration) (out)
105 // PXMOD : PXN-PX (out)
106
107 double zdlx, zdlk, zdlldn;
108 double zdlmod = 0;
109 size_t ik;
110 size_t kodd = kn % 2; // mod(kn,2)
111
112 zdlx = px;
113 zdlk = 0.;
114 if (kodd == 0) {
115 zdlk = 0.5 * pfn[0];
116 }
117 zdlldn = 0.;
118 ik = 1;
119
120 // Newton interation step
121 // ----------------------
122 for (size_t jn = 2 - kodd; jn <= kn; jn += 2) {
123 // normalised ordinary Legendre polynomial == \overbar{P_n}^0
124 zdlk += pfn[ik] * std::cos(static_cast<double>(jn) * zdlx);
125 // normalised derivative == d/d\theta(\overbar{P_n}^0)
126 zdlldn -= pfn[ik] * static_cast<double>(jn) * std::sin(static_cast<double>(jn) * zdlx);
127 ++ik;
128 }
129 // Newton method
130 if( zdlldn != 0 ) {
131 zdlmod = -zdlk / zdlldn;
132 }
133 pxn = zdlx + zdlmod;
134 pxmod = zdlmod;
135 }
136
137 void legpol_weight(const size_t kn, const double pfn[], const double px, double& pw) {
138 // Routine to compute the quadrature weight of the legendre polynomial
139
140 // Explicit arguments :
141 // --------------------
142 // KN : Degree of the Legendre polynomial (in)
143 // KODD : odd or even number of latitudes (in)
144 // PFN : Fourier coefficients of series expansion (in)
145 // for the ordinary Legendre polynomials
146 // PX : abcissa where the computations are performed (in)
147 // KFLAG : When KFLAG.EQ.1 computes the weights (in)
148 // PXN : new abscissa (Newton iteration) (out)
149 // PXMOD : PXN-PX (out)
150
151 double zdlx, zdlldn;
152 size_t ik;
153 size_t kodd = kn % 2;
154
155 zdlx = px;
156 zdlldn = 0.;
157 ik = 1;
158
159 // Compute weights
160 // ---------------
161 for (size_t jn = 2 - kodd; jn <= kn; jn += 2) {
162 zdlldn -= pfn[ik] * static_cast<double>(jn) * std::sin(static_cast<double>(jn) * zdlx);
163 ++ik;
164 }
165 pw = static_cast<double>(2 * kn + 1) / (zdlldn * zdlldn);
166 }
167
168 //-----------------------------------------------------------------------------
169
170 void legpol_quadrature(const size_t kn, const double pfn[], double& pl, double& pw, size_t& kiter, double& pmod) {
171 //**** *GAWL * - Routine to perform the Newton loop
172
173 // Purpose.
174 // --------
175 // Find 0 of Legendre polynomial with Newton loop
176 //** Interface.
177 // ----------
178 // *CALL* *GAWL(PFN,PL,PW,PEPS,KN,KITER,PMOD)
179
180 // Explicit arguments :
181 // --------------------
182 // PFN Fourier coefficients of series expansion
183 // for the ordinary Legendre polynomials (in)
184 // PL Gaussian latitude (inout)
185 // PEPS 0 of the machine (in)
186 // KN Truncation (in)
187 // KITER Number of iterations (out)
188 // PMOD Last modification (inout)
189
190 constexpr size_t itemax = 20;
191 constexpr double ztol = std::numeric_limits<double>::epsilon() * 1000.;
192
193 double zx = pl;
194 double zxn;
195 double zw = 0;
196 bool tol_reached = false;
197
198 for (size_t jter = 1; jter <= itemax + 1; ++jter) {
199 kiter = jter;
200 legpol_newton_iteration(kn, pfn, zx, zxn, pmod);
201 zx = zxn;
202 if (tol_reached) {
203 legpol_weight(kn, pfn, zx, zw);
204 break;
205 }
206 if (std::abs(pmod) <= ztol) {
207 tol_reached = true;
208 }
209 }
210 if (not tol_reached) {
211 std::stringstream s;
212 s << "Could not converge gaussian latitude to accuracy [" << ztol << "]\n";
213 s << "after " << itemax << " iterations. Consequently also failed to compute quadrature weight.";
214 throw_Exception(s.str(), Here());
215 }
216
217 pl = zxn;
218 pw = zw;
219 }
220
221 //-----------------------------------------------------------------------------
222
223 } // anonymous namespace
224
225 //-----------------------------------------------------------------------------
226
227 void compute_gaussian_quadrature_npole_equator(const size_t N, double lats[], double weights[]) {
228 Log::debug() << "Atlas computing Gaussian latitudes for N " << N << std::endl;
229 ATLAS_TRACE();
230
231 size_t kdgl = 2 * N;
232
233 // Computation of Fourier coefficients of series expansion for the ordinary Legendre polynomials
234 std::vector<double> zzfn(N + 1);
235 {
236 // Belousov, Swarztrauber use zfn(0)=std::sqrt(2.)
237 // IFS normalisation chosen to be 0.5*Integral(Pnm**2) = 1
238 constexpr double zfn_0 = 2.;
239 std::vector<double> zfn(kdgl+1);
240 zfn[0] = zfn_0;
241 for (size_t jn = 1; jn <= kdgl; ++jn) {
242 zfn[jn] = zfn_0;
243 for (size_t jgl = 1; jgl <= jn; ++jgl) {
244 zfn[jn] *= std::sqrt(1. - 0.25 / (static_cast<double>(jgl * jgl)));
245 }
246 size_t iodd = jn % 2;
247 for (size_t jgl = 2; jgl <= jn - iodd; jgl += 2) {
248 zfn[jn - jgl] = zfn[jn - jgl + 2] * static_cast<double>((jgl - 1) * (2 * jn - jgl + 2)) /
249 static_cast<double>(jgl * (2 * jn - jgl + 1));
250 }
251 }
252
253 size_t iodd = kdgl % 2;
254 for (size_t jgl = iodd, ik = iodd; jgl <= kdgl; jgl += 2, ++ik) {
255 zzfn[ik] = zfn[jgl];
256 }
257 }
258
259 for (size_t jgl = 0; jgl < N; ++jgl) {
260 // Compute first guess for colatitudes in radians
261 double z = (4. * (jgl + 1.) - 1.) * M_PI / (4. * 2. * N + 2.);
262 lats[jgl] = (z + 1. / (std::tan(z) * (8. * (2. * N) * (2. * N))));
263
264 // refine colat first guess here via Newton's method
265 size_t iter;
266 double zmod;
267 legpol_quadrature(kdgl, zzfn.data(), lats[jgl], weights[jgl], iter, zmod);
268
269 // Convert colat to lat, in degrees
270 constexpr double pole = 90.;
271 lats[jgl] = pole - lats[jgl] * util::Constants::radiansToDegrees();
272 }
273 }
274
275 //-----------------------------------------------------------------------------
276
277 } // namespace gaussian
278 } // namespace spacing
279 } // namespace grid
280 } // namespace atlas