Debian Codegraph @

src/atlas/util/function/SphericalHarmonic.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 #include <cmath>
12 #include <map>
13
14 #include "atlas/runtime/Exception.h"
15 #include "atlas/util/Constants.h"
16 #include "atlas/util/function/SphericalHarmonic.h"
17
18 namespace atlas {
19
20 namespace util {
21
22 namespace function {
23
24
25 namespace {
26 static double factorial(double v) {
27 if (v == 0) {
28 return 1;
29 }
30 double result = v;
31 while (--v > 0) {
32 result *= v;
33 }
34 return result;
35 }
36
37 static double double_factorial(double x) {
38 if (x == 0 || x == -1) {
39 return 1;
40 }
41
42 double result = x;
43 while ((x -= 2) > 0) {
44 result *= x;
45 }
46 return result;
47 }
48
49 // Associated Legendre Polynomial
50 static double P(const int n, const int m, const double x) {
51 // No recursive calculation needed
52 if (n == m) {
53 return (std::pow(-1.0, m) * double_factorial(2 * m - 1) * std::pow(std::sqrt(1. - x * x), m));
54 }
55
56 if (n == m + 1) {
57 return x * (2 * m + 1) * P(m, m, x);
58 }
59
60 // Formula 1
61 return (x * (2 * n - 1) * P(n - 1, m, x) - (n + m - 1) * P(n - 2, m, x)) / (n - m);
62 }
63
64 static double K(const int n, const int m) {
65 //When m is less than 0, multiply - 1 to pass in
66 return std::sqrt(((2 * n + 1) * factorial(n - m)) / (4 * M_PI * factorial(n + m)));
67 }
68 } // namespace
69
70 double spherical_harmonic(int n, int m, double lon, double lat) {
71 const int abs_m = std::abs(m);
72
73 ATLAS_ASSERT(n >= abs_m);
74
75 double colat = (90. - lat) * Constants::degreesToRadians();
76 lon *= Constants::degreesToRadians();
77
78 if (m == 0) {
79 return (K(n, 0) * P(n, 0, std::cos(colat)));
80 }
81
82 if (m > 0) {
83 return (M_SQRT2 * K(n, m) * std::cos(m * lon) * P(n, m, std::cos(colat)));
84 }
85
86 // When m is less than 0, multiply - 1 in advance and send it to K
87 return (M_SQRT2 * K(n, abs_m) * std::sin(abs_m * lon) * P(n, abs_m, std::cos(colat)));
88 }
89
90 SphericalHarmonic::SphericalHarmonic(int n, int m, bool caching) {
91 const int abs_m = std::abs(m);
92
93 ATLAS_ASSERT(n >= abs_m);
94
95 const double Knm = K(n, abs_m);
96 std::function<double(double)> Pnm = [n, abs_m](double x) { return P(n, abs_m, x); };
97
98 if (caching) {
99 // Computation of associated legendre polynomials is
100 // very expensive. With this trick (off by default),
101 // we can cache repeated values, useful for structured
102 // grids with constant latitude.
103 Pnm = [Pnm](double x) {
104 static std::map<double, double> memo;
105
106 auto it = memo.find(x);
107 if (it != memo.end()) {
108 return it->second;
109 }
110 auto result = Pnm(x);
111 memo[x] = result;
112 return result;
113 };
114 }
115
116 if (m == 0) {
117 Y_ = [Knm, Pnm](double lon, double colat) { return Knm * Pnm(std::cos(colat)); };
118 }
119 else if (m > 0) {
120 Y_ = [m, Knm, Pnm](double lon, double colat) {
121 return M_SQRT2 * Knm * std::cos(m * lon) * Pnm(std::cos(colat));
122 };
123 }
124 else {
125 Y_ = [m, Knm, Pnm](double lon, double colat) {
126 return M_SQRT2 * Knm * std::sin(-m * lon) * Pnm(std::cos(colat));
127 };
128 }
129 }
130
131 double SphericalHarmonic::operator()(double lon, double lat) const {
132 double colat = (90. - lat) * Constants::degreesToRadians();
133 lon *= Constants::degreesToRadians();
134 return Y_(lon, colat);
135 }
136
137
138 } // namespace function
139
140 } // namespace util
141
142 } // namespace atlas