OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SphericalHarmonic.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
46
47#include <config.h>
48
49#include <cmath>
50#include <cstdio>
51#include <limits>
52
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
55
56using namespace OpenMD;
57
58SphericalHarmonic::SphericalHarmonic() {}
59
60ComplexType SphericalHarmonic::getValueAt(RealType costheta, RealType phi) {
61 RealType p;
62
63 // associated Legendre polynomial
64 p = Ptilde(L, M, costheta);
65 ComplexType phase(0.0, (RealType)M * phi);
66
67 return exp(phase) * (ComplexType)p;
68}
69//
70// Routine to calculate the associated Legendre polynomials for m>=0
71//
72RealType SphericalHarmonic::LegendreP(int l, int m, RealType x) {
73 RealType result;
74
75 if (fabs(x) > 1.0) {
76 printf("LegendreP: x out of range: l = %d\tm = %d\tx = %lf\n", l, m, x);
77 return std::numeric_limits<RealType>::quiet_NaN();
78 }
79
80 if (m > l) {
81 printf("LegendreP: m > l: l = %d\tm = %d\tx = %lf\n", l, m, x);
82 return std::numeric_limits<RealType>::quiet_NaN();
83 }
84
85 if (m < 0) {
86 printf("LegendreP: m < 0: l = %d\tm = %d\tx = %lf\n", l, m, x);
87 return std::numeric_limits<RealType>::quiet_NaN();
88 } else {
89 RealType temp1, temp2(0.0), temp3, temp4, temp5;
90 temp3 = 1.0;
91
92 if (m > 0) {
93 temp1 = sqrt(1.0 - pow(x, 2));
94 temp5 = 1.0;
95 for (int i = 1; i <= m; ++i) {
96 temp3 *= -temp5 * temp1;
97 temp5 += 2.0;
98 }
99 }
100 if (l == m) {
101 result = temp3;
102 } else {
103 temp4 = x * (2. * m + 1.) * temp3;
104 if (l == (m + 1)) {
105 result = temp4;
106 } else {
107 for (int ll = (m + 2); ll <= l; ++ll) {
108 temp2 = (x * (2. * ll - 1.) * temp4 - (ll + m - 1.) * temp3) /
109 (RealType)(ll - m);
110 temp3 = temp4;
111 temp4 = temp2;
112 }
113 result = temp2;
114 }
115 }
116 }
117 return result;
118}
119
120//
121// Routine to calculate the associated Legendre polynomials for all m...
122//
123RealType SphericalHarmonic::Legendre(int l, int m, RealType x) {
124 RealType result;
125 if (m > l || m < -l) {
126 printf("Legendre got a bad argument: l = %d\tm = %d\tx = %lf\n", l, m, x);
127 return std::numeric_limits<RealType>::quiet_NaN();
128 } else if (m >= 0) {
129 result = LegendreP(l, m, x);
130 } else {
131 // result = mpow(-m)*LegendreP(l,-m,x);
132 result = mpow(-m) * Fact(l + m) / Fact(l - m) * LegendreP(l, -m, x);
133 }
134 result *= mpow(m);
135 return result;
136}
137//
138// Routine to calculate the normalized associated Legendre polynomials...
139//
140RealType SphericalHarmonic::Ptilde(int l, int m, RealType x) {
141 RealType result;
142 if (m > l || m < -l) {
143 result = 0.;
144 } else {
145 RealType y = (RealType)(2. * l + 1.) * Fact(l - m) / Fact(l + m);
146 result = mpow(m) * sqrt(y) * Legendre(l, m, x) / sqrt(4.0 * Constants::PI);
147 }
148 return result;
149}
150//
151// mpow returns (-1)**m
152//
153RealType SphericalHarmonic::mpow(int m) {
154 int result;
155 if (m < 0) m = -m;
156 if (m & 0x1)
157 result = -1;
158 else
159 result = 1;
160 return result;
161}
162//
163// factorial_list is a lookup table for n!
164//
165static RealType factorial_list[171] = {1.,
166 1.,
167 2.,
168 6.,
169 24.,
170 120.,
171 720.,
172 5040.,
173 40320.,
174 362880.,
175 3.6288e6,
176 3.99168e7,
177 4.790016e8,
178 6.2270208e9,
179 8.71782912e10,
180 1.307674368e12,
181 2.0922789888e13,
182 3.55687428096e14,
183 6.402373705728e15,
184 1.21645100408832e17,
185 2.43290200817664e18,
186 5.109094217170944e19,
187 1.1240007277776077e21,
188 2.585201673888498e22,
189 6.204484017332394e23,
190 1.5511210043330986e25,
191 4.0329146112660565e26,
192 1.0888869450418352e28,
193 3.0488834461171387e29,
194 8.841761993739702e30,
195 2.6525285981219107e32,
196 8.222838654177922e33,
197 2.631308369336935e35,
198 8.683317618811886e36,
199 2.9523279903960416e38,
200 1.0333147966386145e40,
201 3.7199332678990125e41,
202 1.3763753091226346e43,
203 5.230226174666011e44,
204 2.0397882081197444e46,
205 8.159152832478977e47,
206 3.345252661316381e49,
207 1.40500611775288e51,
208 6.041526306337383e52,
209 2.658271574788449e54,
210 1.1962222086548019e56,
211 5.502622159812089e57,
212 2.5862324151116818e59,
213 1.2413915592536073e61,
214 6.082818640342675e62,
215 3.0414093201713376e64,
216 1.5511187532873822e66,
217 8.065817517094388e67,
218 4.2748832840600255e69,
219 2.308436973392414e71,
220 1.2696403353658276e73,
221 7.109985878048635e74,
222 4.0526919504877214e76,
223 2.3505613312828785e78,
224 1.3868311854568984e80,
225 8.32098711274139e81,
226 5.075802138772248e83,
227 3.146997326038794e85,
228 1.98260831540444e87,
229 1.2688693218588417e89,
230 8.247650592082472e90,
231 5.443449390774431e92,
232 3.647111091818868e94,
233 2.4800355424368305e96,
234 1.711224524281413e98,
235 1.1978571669969892e100,
236 8.504785885678623e101,
237 6.1234458376886085e103,
238 4.4701154615126844e105,
239 3.307885441519386e107,
240 2.48091408113954e109,
241 1.8854947016660504e111,
242 1.4518309202828587e113,
243 1.1324281178206297e115,
244 8.946182130782976e116,
245 7.156945704626381e118,
246 5.797126020747368e120,
247 4.753643337012842e122,
248 3.945523969720659e124,
249 3.314240134565353e126,
250 2.81710411438055e128,
251 2.4227095383672734e130,
252 2.107757298379528e132,
253 1.8548264225739844e134,
254 1.650795516090846e136,
255 1.4857159644817615e138,
256 1.352001527678403e140,
257 1.2438414054641308e142,
258 1.1567725070816416e144,
259 1.087366156656743e146,
260 1.032997848823906e148,
261 9.916779348709496e149,
262 9.619275968248212e151,
263 9.426890448883248e153,
264 9.332621544394415e155,
265 9.332621544394415e157,
266 9.42594775983836e159,
267 9.614466715035127e161,
268 9.90290071648618e163,
269 1.0299016745145628e166,
270 1.081396758240291e168,
271 1.1462805637347084e170,
272 1.226520203196138e172,
273 1.324641819451829e174,
274 1.4438595832024937e176,
275 1.588245541522743e178,
276 1.7629525510902446e180,
277 1.974506857221074e182,
278 2.2311927486598138e184,
279 2.5435597334721877e186,
280 2.925093693493016e188,
281 3.393108684451898e190,
282 3.969937160808721e192,
283 4.684525849754291e194,
284 5.574585761207606e196,
285 6.689502913449127e198,
286 8.094298525273444e200,
287 9.875044200833601e202,
288 1.214630436702533e205,
289 1.506141741511141e207,
290 1.882677176888926e209,
291 2.372173242880047e211,
292 3.0126600184576594e213,
293 3.856204823625804e215,
294 4.974504222477287e217,
295 6.466855489220474e219,
296 8.47158069087882e221,
297 1.1182486511960043e224,
298 1.4872707060906857e226,
299 1.9929427461615188e228,
300 2.6904727073180504e230,
301 3.659042881952549e232,
302 5.012888748274992e234,
303 6.917786472619489e236,
304 9.615723196941089e238,
305 1.3462012475717526e241,
306 1.898143759076171e243,
307 2.695364137888163e245,
308 3.854370717180073e247,
309 5.5502938327393044e249,
310 8.047926057471992e251,
311 1.1749972043909107e254,
312 1.727245890454639e256,
313 2.5563239178728654e258,
314 3.80892263763057e260,
315 5.713383956445855e262,
316 8.62720977423324e264,
317 1.3113358856834524e267,
318 2.0063439050956823e269,
319 3.0897696138473508e271,
320 4.789142901463394e273,
321 7.471062926282894e275,
322 1.1729568794264145e278,
323 1.853271869493735e280,
324 2.9467022724950384e282,
325 4.7147236359920616e284,
326 7.590705053947219e286,
327 1.2296942187394494e289,
328 2.0044015765453026e291,
329 3.287218585534296e293,
330 5.423910666131589e295,
331 9.003691705778438e297,
332 1.503616514864999e300,
333 2.5260757449731984e302,
334 4.269068009004705e304,
335 7.257415615307999e306};
336
337//
338// Routine to return the factorial of j
339//
340RealType SphericalHarmonic::Fact(int j) {
341 if (j <= 170 && j >= 0) return factorial_list[j];
342
343 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH, "Fact(j) for j >= 171\n");
344 painCave.isFatal = 0;
345 simError();
346 return 0.;
347}
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.