1 |
gezelter |
1042 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
|
|
* 1. Acknowledgement of the program authors must be made in any |
10 |
|
|
* publication of scientific results based in part on use of the |
11 |
|
|
* program. An acceptable form of acknowledgement is citation of |
12 |
|
|
* the article in which the program was described (Matthew |
13 |
|
|
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
|
|
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
|
|
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
|
|
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
|
|
* |
18 |
|
|
* 2. Redistributions of source code must retain the above copyright |
19 |
|
|
* notice, this list of conditions and the following disclaimer. |
20 |
|
|
* |
21 |
|
|
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
|
|
* notice, this list of conditions and the following disclaimer in the |
23 |
|
|
* documentation and/or other materials provided with the |
24 |
|
|
* distribution. |
25 |
|
|
* |
26 |
|
|
* This software is provided "AS IS," without a warranty of any |
27 |
|
|
* kind. All express or implied conditions, representations and |
28 |
|
|
* warranties, including any implied warranty of merchantability, |
29 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
30 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
31 |
|
|
* be liable for any damages suffered by licensee as a result of |
32 |
|
|
* using, modifying or distributing the software or its |
33 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
34 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
35 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
36 |
|
|
* damages, however caused and regardless of the theory of liability, |
37 |
|
|
* arising out of the use of or inability to use software, even if the |
38 |
|
|
* University of Notre Dame has been advised of the possibility of |
39 |
|
|
* such damages. |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
#include <stdio.h> |
43 |
|
|
#include <limits> |
44 |
|
|
#include "math/SphericalHarmonic.hpp" |
45 |
|
|
#include "utils/simError.h" |
46 |
|
|
|
47 |
|
|
using namespace oopse; |
48 |
|
|
|
49 |
|
|
SphericalHarmonic::SphericalHarmonic() { |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
ComplexType SphericalHarmonic::getValueAt(RealType costheta, RealType phi) { |
53 |
|
|
|
54 |
|
|
RealType p; |
55 |
|
|
ComplexType phase; |
56 |
|
|
ComplexType I(0.0, 1.0); |
57 |
|
|
|
58 |
|
|
// associated Legendre polynomial |
59 |
|
|
p = Legendre(L, M, costheta); |
60 |
|
|
|
61 |
|
|
phase = exp(I * (ComplexType)M * (ComplexType)phi); |
62 |
|
|
|
63 |
|
|
return coefficient * phase * (ComplexType)p; |
64 |
|
|
|
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
//---------------------------------------------------------------------------// |
68 |
|
|
// |
69 |
|
|
// RealType LegendreP (int l, int m, RealType x); |
70 |
|
|
// |
71 |
|
|
// Computes the value of the associated Legendre polynomial P_lm (x) |
72 |
|
|
// of order l at a given point. |
73 |
|
|
// |
74 |
|
|
// Input: |
75 |
|
|
// l = degree of the polynomial >= 0 |
76 |
|
|
// m = parameter satisfying 0 <= m <= l, |
77 |
|
|
// x = point in which the computation is performed, range -1 <= x <= 1. |
78 |
|
|
// Returns: |
79 |
|
|
// value of the polynomial in x |
80 |
|
|
// |
81 |
|
|
//---------------------------------------------------------------------------// |
82 |
|
|
RealType SphericalHarmonic::LegendreP (int l, int m, RealType x) { |
83 |
|
|
// check parameters |
84 |
|
|
if (m < 0 || m > l || fabs(x) > 1.0) { |
85 |
|
|
printf("LegendreP got a bad argument: l = %d\tm = %d\tx = %lf\n", l, m, x); |
86 |
|
|
return std::numeric_limits <RealType>:: quiet_NaN(); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
RealType pmm = 1.0; |
90 |
|
|
if (m > 0) { |
91 |
|
|
RealType h = sqrt((1.0-x)*(1.0+x)), |
92 |
|
|
f = 1.0; |
93 |
|
|
for (int i = 1; i <= m; i++) { |
94 |
|
|
pmm *= -f * h; |
95 |
|
|
f += 2.0; |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
if (l == m) |
99 |
|
|
return pmm; |
100 |
|
|
else { |
101 |
|
|
RealType pmmp1 = x * (2 * m + 1) * pmm; |
102 |
|
|
if (l == (m+1)) |
103 |
|
|
return pmmp1; |
104 |
|
|
else { |
105 |
|
|
RealType pll = 0.0; |
106 |
|
|
for (int ll = m+2; ll <= l; ll++) { |
107 |
|
|
pll = (x * (2 * ll - 1) * pmmp1 - (ll + m - 1) * pmm) / (ll - m); |
108 |
|
|
pmm = pmmp1; |
109 |
|
|
pmmp1 = pll; |
110 |
|
|
} |
111 |
|
|
return pll; |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
// |
117 |
|
|
// Routine to calculate the associated Legendre polynomials for all m... |
118 |
|
|
// |
119 |
|
|
RealType SphericalHarmonic::Legendre(int l, int m, RealType x) { |
120 |
|
|
RealType result; |
121 |
|
|
if ( m>l || m <-l ) { |
122 |
|
|
printf("Legendre got a bad argument: l = %d\tm = %d\tx = %lf\n", l, m, x); |
123 |
|
|
return std::numeric_limits <RealType>:: quiet_NaN(); |
124 |
|
|
} else if (m >= 0) { |
125 |
|
|
result = LegendreP(l,m,x); |
126 |
|
|
} else { |
127 |
|
|
result = mpow(-m)*Fact(l+m)/Fact(l-m)*LegendreP(l, -m, x); |
128 |
|
|
} |
129 |
|
|
result *=mpow(m); |
130 |
|
|
return result; |
131 |
|
|
} |
132 |
|
|
// |
133 |
|
|
// mpow returns (-1)**m |
134 |
|
|
// |
135 |
|
|
RealType SphericalHarmonic::mpow(int m) { |
136 |
|
|
int result; |
137 |
|
|
if (m<0) m=-m; |
138 |
|
|
if (m & 0x1) result = -1; |
139 |
|
|
else result = 1; |
140 |
|
|
return result; |
141 |
|
|
} |
142 |
|
|
// |
143 |
|
|
// factorial_list is a lookup table for n! |
144 |
|
|
// |
145 |
|
|
static RealType factorial_list[171]= |
146 |
|
|
{1.,1.,2.,6.,24.,120.,720.,5040.,40320.,362880.,3.6288e6,3.99168e7,4.790016e8,6.2270208e9, |
147 |
|
|
8.71782912e10,1.307674368e12,2.0922789888e13,3.55687428096e14,6.402373705728e15, |
148 |
|
|
1.21645100408832e17, |
149 |
|
|
2.43290200817664e18,5.109094217170944e19,1.1240007277776077e21,2.585201673888498e22, |
150 |
|
|
6.204484017332394e23,1.5511210043330986e25,4.0329146112660565e26,1.0888869450418352e28, |
151 |
|
|
3.0488834461171387e29,8.841761993739702e30,2.6525285981219107e32,8.222838654177922e33, |
152 |
|
|
2.631308369336935e35,8.683317618811886e36,2.9523279903960416e38,1.0333147966386145e40, |
153 |
|
|
3.7199332678990125e41,1.3763753091226346e43,5.230226174666011e44,2.0397882081197444e46, |
154 |
|
|
8.159152832478977e47,3.345252661316381e49,1.40500611775288e51,6.041526306337383e52, |
155 |
|
|
2.658271574788449e54,1.1962222086548019e56,5.502622159812089e57,2.5862324151116818e59, |
156 |
|
|
1.2413915592536073e61,6.082818640342675e62,3.0414093201713376e64,1.5511187532873822e66, |
157 |
|
|
8.065817517094388e67,4.2748832840600255e69,2.308436973392414e71,1.2696403353658276e73, |
158 |
|
|
7.109985878048635e74,4.0526919504877214e76,2.3505613312828785e78,1.3868311854568984e80, |
159 |
|
|
8.32098711274139e81,5.075802138772248e83,3.146997326038794e85,1.98260831540444e87, |
160 |
|
|
1.2688693218588417e89,8.247650592082472e90,5.443449390774431e92,3.647111091818868e94, |
161 |
|
|
2.4800355424368305e96,1.711224524281413e98,1.1978571669969892e100,8.504785885678623e101, |
162 |
|
|
6.1234458376886085e103,4.4701154615126844e105,3.307885441519386e107,2.48091408113954e109, |
163 |
|
|
1.8854947016660504e111,1.4518309202828587e113,1.1324281178206297e115,8.946182130782976e116, |
164 |
|
|
7.156945704626381e118,5.797126020747368e120,4.753643337012842e122,3.945523969720659e124, |
165 |
|
|
3.314240134565353e126,2.81710411438055e128,2.4227095383672734e130,2.107757298379528e132, |
166 |
|
|
1.8548264225739844e134,1.650795516090846e136,1.4857159644817615e138,1.352001527678403e140, |
167 |
|
|
1.2438414054641308e142,1.1567725070816416e144,1.087366156656743e146,1.032997848823906e148, |
168 |
|
|
9.916779348709496e149,9.619275968248212e151,9.426890448883248e153,9.332621544394415e155, |
169 |
|
|
9.332621544394415e157,9.42594775983836e159,9.614466715035127e161,9.90290071648618e163, |
170 |
|
|
1.0299016745145628e166,1.081396758240291e168,1.1462805637347084e170,1.226520203196138e172, |
171 |
|
|
1.324641819451829e174,1.4438595832024937e176,1.588245541522743e178,1.7629525510902446e180, |
172 |
|
|
1.974506857221074e182,2.2311927486598138e184,2.5435597334721877e186,2.925093693493016e188, |
173 |
|
|
3.393108684451898e190,3.969937160808721e192,4.684525849754291e194,5.574585761207606e196, |
174 |
|
|
6.689502913449127e198,8.094298525273444e200,9.875044200833601e202,1.214630436702533e205, |
175 |
|
|
1.506141741511141e207,1.882677176888926e209,2.372173242880047e211,3.0126600184576594e213, |
176 |
|
|
3.856204823625804e215,4.974504222477287e217,6.466855489220474e219,8.47158069087882e221, |
177 |
|
|
1.1182486511960043e224,1.4872707060906857e226,1.9929427461615188e228,2.6904727073180504e230, |
178 |
|
|
3.659042881952549e232,5.012888748274992e234,6.917786472619489e236,9.615723196941089e238, |
179 |
|
|
1.3462012475717526e241,1.898143759076171e243,2.695364137888163e245,3.854370717180073e247, |
180 |
|
|
5.5502938327393044e249,8.047926057471992e251,1.1749972043909107e254,1.727245890454639e256, |
181 |
|
|
2.5563239178728654e258,3.80892263763057e260,5.713383956445855e262,8.62720977423324e264, |
182 |
|
|
1.3113358856834524e267,2.0063439050956823e269,3.0897696138473508e271,4.789142901463394e273, |
183 |
|
|
7.471062926282894e275,1.1729568794264145e278,1.853271869493735e280,2.9467022724950384e282, |
184 |
|
|
4.7147236359920616e284,7.590705053947219e286,1.2296942187394494e289,2.0044015765453026e291, |
185 |
|
|
3.287218585534296e293,5.423910666131589e295,9.003691705778438e297,1.503616514864999e300, |
186 |
|
|
2.5260757449731984e302,4.269068009004705e304,7.257415615307999e306}; |
187 |
|
|
|
188 |
|
|
// |
189 |
|
|
// Routine to return the factorial of j |
190 |
|
|
// |
191 |
|
|
RealType SphericalHarmonic::Fact(int j) { |
192 |
|
|
if (j <= 170 && j>=0) return factorial_list[j]; |
193 |
|
|
|
194 |
|
|
sprintf( painCave.errMsg, |
195 |
|
|
"Fact(j) for j >= 171\n"); |
196 |
|
|
painCave.isFatal = 0; |
197 |
|
|
simError(); |
198 |
|
|
return 0.; |
199 |
|
|
} |