-
Notifications
You must be signed in to change notification settings - Fork 15
/
wignerSymbols-cpp.cpp
506 lines (413 loc) · 14.5 KB
/
wignerSymbols-cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*******************************************************-/
* This source code is subject to the terms of the GNU -/
* Lesser Public License. If a copy of the LGPL was not -/
* distributed with this file, you can obtain one at -/
* https://www.gnu.org/licenses/lgpl.html. -/
********************************************************/
#include "../include/wignerSymbols/commonFunctions.h"
#include "../include/wignerSymbols/wignerSymbols-cpp.h"
namespace WignerSymbols {
std::vector<double> wigner3j(double l2, double l3,
double m1, double m2, double m3)
{
// We compute the numeric limits of double precision.
double huge = sqrt(std::numeric_limits<double>::max()/20.0);
double srhuge = sqrt(huge);
double tiny = std::numeric_limits<double>::min();
double srtiny = sqrt(tiny);
double eps = std::numeric_limits<double>::epsilon();
// We enforce the selection rules.
bool select(true);
select = (
std::fabs(m1+m2+m3)<eps
&& std::fabs(m2) <= l2+eps
&& std::fabs(m3) <= l3+eps
);
if (!select) return std::vector<double>(1,0.0);
// We compute the limits of l1.
double l1min = std::max(std::fabs(l2-l3),std::fabs(m1));
double l1max = l2+l3;
// We compute the size of the resulting array.
int size = (int)std::floor(l1max-l1min+1.0+eps);
std::vector<double> thrcof(size,0.0);
// If l1min=l1max, we have an analytical formula.
if (size==1)
{
thrcof[0] = pow(-1.0,std::floor(std::fabs(l2+m2-l3+m3)))/sqrt(l1min+l2+l3+1.0);
}
// Another special case where the recursion relation fails.
else
{
// We start with an arbitrary value.
thrcof[0] = srtiny;
// From now on, we check the variation of |alpha(l1)|.
double alphaNew, l1(l1min);
if (l1min==0.0)
alphaNew = -(m3-m2+2.0*wigner3j_auxB(l1,l2,l3,m1,m2,m3))/wigner3j_auxA(1.0,l2,l3,m1,m2,m3);
else
alphaNew = -wigner3j_auxB(l1min,l2,l3,m1,m2,m3)
/(l1min*wigner3j_auxA(l1min+1.0,l2,l3,m1,m2,m3));
// We compute the two-term recursion.
thrcof[1] = alphaNew*thrcof[0];
// If size > 2, we start the forward recursion.
if (size>2)
{
// We start with an arbitrary value.
thrcof[0] = srtiny;
// From now on, we check the variation of |alpha(l1)|.
double alphaOld, alphaNew, beta, l1(l1min);
if (l1min==0.0)
alphaNew = -(m3-m2+2.0*wigner3j_auxB(l1,l2,l3,m1,m2,m3))/wigner3j_auxA(1.0,l2,l3,m1,m2,m3);
else
alphaNew = -wigner3j_auxB(l1min,l2,l3,m1,m2,m3)
/(l1min*wigner3j_auxA(l1min+1.0,l2,l3,m1,m2,m3));
// We compute the two-term recursion.
thrcof[1] = alphaNew*thrcof[0];
// We compute the rest of the recursion.
int i = 1;
bool alphaVar = false;
do
{
// Bookkeeping:
i++; // Next term in recursion
alphaOld = alphaNew; // Monitoring of |alpha(l1)|.
l1 += 1.0; // l1 = l1+1
// New coefficients in recursion.
alphaNew = -wigner3j_auxB(l1,l2,l3,m1,m2,m3)
/(l1*wigner3j_auxA(l1+1.0,l2,l3,m1,m2,m3));
beta = -(l1+1.0)*wigner3j_auxA(l1,l2,l3,m1,m2,m3)
/(l1*wigner3j_auxA(l1+1.0,l2,l3,m1,m2,m3));
// Application of the recursion.
thrcof[i] = alphaNew*thrcof[i-1]+beta*thrcof[i-2];
// We check if we are overflowing.
if (std::fabs(thrcof[i])>srhuge)
{
std::cout << "We renormalized the forward recursion." << std::endl;
for (std::vector<double>::iterator it = thrcof.begin(); it != thrcof.begin()+i; ++it)
{
//if (std::fabs(*it) < srtiny) *it = 0;
//else
*it /= srhuge;
}
}
// This piece of code checks whether we have reached
// the classical region. If we have, the second if
// sets alphaVar to true and we break this loop at the
// next iteration because we need thrcof(l1mid+1) to
// compute the scalar lambda.
if (alphaVar) break;
if (std::fabs(alphaNew)-std::fabs(alphaOld)>0.0)
alphaVar=true;
} while (i<(size-1)); // Loop stops when we have computed all values.
// If this is the case, we have stumbled upon a classical region.
// We start the backwards recursion.
if (i!=size-1)
{
// We keep the two terms around l1mid to compute the factor later.
double l1midm1(thrcof[i-2]),l1mid(thrcof[i-1]),l1midp1(thrcof[i]);
// We compute the backward recursion by providing an arbitrary
// startint value.
thrcof[size-1] = srtiny;
// We compute the two-term recursion.
l1 = l1max;
alphaNew = -wigner3j_auxB(l1,l2,l3,m1,m2,m3)
/((l1+1.0)*wigner3j_auxA(l1,l2,l3,m1,m2,m3));
thrcof[size-2] = alphaNew*thrcof[size-1];
// We compute the rest of the backward recursion.
int j = size-2;
do
{
// Bookkeeping
j--; // Previous term in recursion.
l1 -= 1.0; // l1 = l1-1
// New coefficients in recursion.
alphaNew = -wigner3j_auxB(l1,l2,l3,m1,m2,m3)
/((l1+1.0)*wigner3j_auxA(l1,l2,l3,m1,m2,m3));
beta = -l1*wigner3j_auxA(l1+1.0,l2,l3,m1,m2,m3)
/((l1+1.0)*wigner3j_auxA(l1,l2,l3,m1,m2,m3));
// Application of the recursion.
thrcof[j] = alphaNew*thrcof[j+1]+beta*thrcof[j+2];
// We check if we are overflowing.
if (std::fabs(thrcof[j]>srhuge))
{
std::cout << "We renormalized the backward recursion." << std::endl;
for (std::vector<double>::iterator it = thrcof.begin()+j; it != thrcof.end(); ++it)
{
//if (std::fabs(*it) < srtiny) *it = 0;
//else
*it /= srhuge;
}
}
} while (j>(i-2)); // Loop stops when we are at l1=l1mid-1.
// We now compute the scaling factor for the forward recursion.
double lambda = (l1midp1*thrcof[j+2]+l1mid*thrcof[j+1]+l1midm1*thrcof[j])
/(l1midp1*l1midp1+l1mid*l1mid+l1midm1*l1midm1);
// We scale the forward recursion.
for (std::vector<double>::iterator it = thrcof.begin(); it != thrcof.begin()+j; ++it)
{
*it *= lambda;
}
}
}
}
// We compute the overall factor.
double sum = 0.0;
for (int k=0;k<size;k++)
{
sum += (2.0*(l1min+k)+1.0)*thrcof[k]*thrcof[k];
}
//std::cout << sum << std::endl;
//std::cout << "(-1)^(l2-l3-m1): " << pow(-1.0,l2-l3-m1) << " sgn:" << sgn(thrcof[size-1]) << std::endl;
double c1 = pow(-1.0,l2-l3-m1)*sgn(thrcof[size-1]);
//std::cout << "c1: " << c1 << std::endl;
for (std::vector<double>::iterator it = thrcof.begin(); it != thrcof.end(); ++it)
{
//std::cout << *it << ", " << c1 << ", ";
*it *= c1/sqrt(sum);
//std::cout << *it << std::endl;
}
return thrcof;
}
double wigner3j(double l1, double l2, double l3,
double m1, double m2, double m3)
{
// We enforce the selection rules.
bool select(true);
select = (
std::fabs(m1+m2+m3)<1.0e-10
&& std::floor(l1+l2+l3)==(l1+l2+l3)
&& l3 >= std::fabs(l1-l2)
&& l3 <= l1+l2
&& std::fabs(m1) <= l1
&& std::fabs(m2) <= l2
&& std::fabs(m3) <= l3
);
if (!select) return 0.0;
// We compute l1min and the position of the array we will want.
double l1min = std::max(std::fabs(l2-l3),std::fabs(m1));
// We fetch the proper value in the array.
int index = (int)(l1-l1min);
return wigner3j(l2,l3,m1,m2,m3)[index];
}
std::vector<double> wigner6j(double l2, double l3,
double l4, double l5, double l6)
{
// We compute the numeric limits of double precision.
double huge = std::numeric_limits<double>::max();
double srhuge = sqrt(huge);
double tiny = std::numeric_limits<double>::min();
double srtiny = sqrt(tiny);
double eps = std::numeric_limits<double>::epsilon();
// We enforce the selection rules.
bool select(true);
// Triangle relations for the four tryads
select = (
std::fabs(l4-l2) <= l6 && l6 <= l4+l2
&& std::fabs(l4-l5) <= l3 && l3 <= l4+l5
);
// Sum rule of the tryads
select = (
std::floor(l4+l2+l6)==(l4+l2+l6)
&& std::floor(l4+l5+l3)==(l4+l5+l3)
);
if (!select) return std::vector<double>(1,0.0);
// We compute the limits of l1.
double l1min = std::max(std::fabs(l2-l3),std::fabs(l5-l6));
double l1max = std::min(l2+l3,l5+l6);
// We compute the size of the resulting array.
unsigned int size = (int)std::floor(l1max-l1min+1.0+eps);
std::vector<double> sixcof(size,0.0);
// If l1min=l1max, we have an analytical formula.
if (size==1)
{
sixcof[0] = 1.0/sqrt((l1min+l1min+1.0)*(l4+l4+1.0));
sixcof[0] *= ((int)std::floor(l2+l3+l5+l6+eps) & 1 ? -1.0 : 1.0);
}
// Otherwise, we start the forward recursion.
else
{
// We start with an arbitrary value.
sixcof[0] = srtiny;
// From now on, we check the variation of |alpha(l1)|.
double alphaNew, l1(l1min);
if (l1min==0)
alphaNew = -(l2*(l2+1.0)+l3*(l3+1.0)+l5*(l5+1.0)+l6*(l6+1.0)-2.0*l4*(l4+1.0))/wigner6j_auxA(1.0,l2,l3,l4,l5,l6);
else
alphaNew = -wigner6j_auxB(l1,l2,l3,l4,l5,l6)
/(l1min*wigner6j_auxA(l1+1.0,l2,l3,l4,l5,l6));
// We compute the two-term recursion.
sixcof[1] = alphaNew*sixcof[0];
if (size>2)
{
// We start with an arbitrary value.
sixcof[0] = srtiny;
// From now on, we check the variation of |alpha(l1)|.
double alphaOld, alphaNew, beta, l1(l1min);
if (l1min==0)
alphaNew = -(l2*(l2+1.0)+l3*(l3+1.0)+l5*(l5+1.0)+l6*(l6+1.0)-2.0*l4*(l4+1.0))/wigner6j_auxA(1.0,l2,l3,l4,l5,l6);
else
alphaNew = -wigner6j_auxB(l1,l2,l3,l4,l5,l6)
/(l1min*wigner6j_auxA(l1+1.0,l2,l3,l4,l5,l6));
// We compute the two-term recursion.
sixcof[1] = alphaNew*sixcof[0];
// We compute the rest of the recursion.
unsigned int i = 1;
bool alphaVar = false;
do
{
// Bookkeeping:
i++; // Next term in recursion
alphaOld = alphaNew; // Monitoring of |alpha(l1)|.
l1 += 1.0; // l1 = l1+1
// New coefficients in recursion.
alphaNew = -wigner6j_auxB(l1,l2,l3,l4,l5,l6)
/(l1*wigner6j_auxA(l1+1.0,l2,l3,l4,l5,l6));
beta = -(l1+1.0)*wigner6j_auxA(l1,l2,l3,l4,l5,l6)
/(l1*wigner6j_auxA(l1+1.0,l2,l3,l4,l5,l6));
// Application of the recursion.
sixcof[i] = alphaNew*sixcof[i-1]+beta*sixcof[i-2];
// We check if we are overflowing.
if (std::fabs(sixcof[i]>srhuge))
{
std::cout << "We renormalized the forward recursion." << std::endl;
for (std::vector<double>::iterator it = sixcof.begin(); it != sixcof.begin()+i; ++it)
{
*it /= srhuge;
}
}
// This piece of code checks whether we have reached
// the classical region. If we have, the second if
// sets alphaVar to true and we break this loop at the
// next iteration because we need sixcof(l1mid+1) to
// compute the scalar.
if (alphaVar) break;
if (std::fabs(alphaNew)-std::fabs(alphaOld)>0.0)
alphaVar=true;
} while (i<(size-1)); // Loop stops when we have computed all values.
// If this is the case, we have stumbled upon a classical region.
// We start the backwards recursion.
if (i!=size-1)
{
// We keep the two terms around l1mid to compute the factor later.
double l1midm1(sixcof[i-2]),l1mid(sixcof[i-1]),l1midp1(sixcof[i]);
// We compute the backward recursion by providing an arbitrary
// startint value.
sixcof[size-1] = srtiny;
// We compute the two-term recursion.
l1 = l1max;
alphaNew = -wigner6j_auxB(l1,l2,l3,l4,l5,l6)
/((l1+1.0)*wigner6j_auxA(l1,l2,l3,l4,l5,l6));
sixcof[size-2] = alphaNew*sixcof[size-1];
// We compute the rest of the backward recursion.
unsigned int j = size-2;
do
{
// Bookkeeping
j--; // Previous term in recursion.
l1 -= 1.0; // l1 = l1-1
// New coefficients in recursion.
alphaNew = -wigner6j_auxB(l1,l2,l3,l4,l5,l6)
/((l1+1.0)*wigner6j_auxA(l1,l2,l3,l4,l5,l6));
beta = -l1*wigner6j_auxA(l1+1.0,l2,l3,l4,l5,l6)
/((l1+1.0)*wigner6j_auxA(l1,l2,l3,l4,l5,l6));
// Application of the recursion.
sixcof[j] = alphaNew*sixcof[j+1]+beta*sixcof[j+2];
// We check if we are overflowing.
if (std::fabs(sixcof[j]>srhuge))
{
std::cout << "We renormalized the backward recursion." << std::endl;
for (std::vector<double>::iterator it = sixcof.begin()+j; it != sixcof.end(); ++it)
{
*it /= srhuge;
}
}
} while (j>(i-2)); // Loop stops when we are at l1=l1mid-1.
// We now compute the scaling factor for the forward recursion.
double lambda = (l1midp1*sixcof[j+2]+l1mid*sixcof[j+1]+l1midm1*sixcof[j])
/(l1midp1*l1midp1+l1mid*l1mid+l1midm1*l1midm1);
// We scale the forward recursion.
for (std::vector<double>::iterator it = sixcof.begin(); it != sixcof.begin()+j; ++it)
{
*it *= lambda;
}
}
}
}
// We compute the overall factor.
double sum = 0.0;
for (unsigned int k=0;k<size;k++)
{
sum += (2.0*(l1min+k)+1.0)*(2.0*l4+1.0)*sixcof[k]*sixcof[k];
}
double c1 = pow(-1.0,std::floor(l2+l3+l5+l6+eps))*sgn(sixcof[size-1])/sqrt(sum);
for (std::vector<double>::iterator it = sixcof.begin(); it != sixcof.end(); ++it)
{
*it *= c1;
}
return sixcof;
}
double wigner6j(double l1, double l2, double l3,
double l4, double l5, double l6)
{
// We enforce the selection rules.
bool select(true);
// Triangle relations for the four tryads
select = (
std::fabs(l1-l2) <= l3 && l3 <= l1+l2
&& std::fabs(l1-l5) <= l6 && l6 <= l1+l5
&& std::fabs(l4-l2) <= l6 && l6 <= l4+l2
&& std::fabs(l4-l5) <= l3 && l3 <= l4+l5
);
// Sum rule of the tryads
select = (
std::floor(l1+l2+l3)==(l1+l2+l3)
&& std::floor(l1+l5+l6)==(l1+l5+l6)
&& std::floor(l4+l2+l6)==(l4+l2+l6)
&& std::floor(l4+l5+l3)==(l4+l5+l3)
);
if (!select) return 0.0;
// We compute l1min and the position of the array we will want.
double l1min = std::max(std::fabs(l2-l3),std::fabs(l5-l6));
int index = (int)(l1-l1min);
return wigner6j(l2,l3,l4,l5,l6)[index];
}
double wigner3j_auxA(double l1, double l2, double l3,
double m1, double m2, double m3)
{
double T1 = l1*l1-pow(l2-l3,2.0);
double T2 = pow(l2+l3+1.0,2.0)-l1*l1;
double T3 = l1*l1-m1*m1;
return sqrt(T1*T2*T3);
}
double wigner3j_auxB(double l1, double l2, double l3,
double m1, double m2, double m3)
{
double T1 = -(2.0*l1+1.0);
double T2 = l2*(l2+1.0)*m1;
double T3 = l3*(l3+1.0)*m1;
double T4 = l1*(l1+1.0)*(m3-m2);
return T1*(T2-T3-T4);
}
double wigner6j_auxA(double l1, double l2, double l3,
double l4, double l5, double l6)
{
double T1 = l1*l1-pow(l2-l3,2.0);
double T2 = pow(l2+l3+1.0,2.0)-l1*l1;
double T3 = l1*l1-pow(l5-l6,2.0);
double T4 = pow(l5+l6+1.0,2.0)-l1*l1;
return sqrt(T1*T2*T3*T4);
}
double wigner6j_auxB(double l1, double l2, double l3,
double l4, double l5, double l6)
{
double T0 = 2.0*l1+1.0;
double T1 = l1*(l1+1.0);
double T2 = -l1*(l1+1.0)+l2*(l2+1.0)+l3*(l3+1.0);
double T3 = l5*(l5+1.0);
double T4 = l1*(l1+1.0)+l2*(l2+1.0)-l3*(l3+1.0);
double T5 = l6*(l6+1.0);
double T6 = l1*(l1+1.0)-l2*(l2+1.0)+l3*(l3+1.0);
double T7 = 2.0*l1*(l1+1.0)*l4*(l4+1.0);
return (T0*(T1*T2+T3*T4+T5*T6-T7));
}
}