phono3py-gpu 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,801 @@
1
+ /* SPDX-License-Identifier: BSD-3-Clause
2
+ * Phonopy tetrahedron formulas, ported to CUDA; see LICENSE.phonopy. */
3
+ typedef long long int64_t;
4
+ #define THM_EPSILON 1e-10
5
+ #define warning_print(...)
6
+ static __device__ int64_t sort_omegas(double v[4]);
7
+ static __device__ double norm_squared_d3(const double a[3]);
8
+ static __device__ void multiply_matrix_vector_dl3(double v[3], const double a[3][3],
9
+ const int64_t b[3]);
10
+ static __device__ double _f(const int64_t n, const int64_t m, const double omega,
11
+ const double vertices_omegas[4]);
12
+ static __device__ double _J(const int64_t i, const int64_t ci, const double omega,
13
+ const double vertices_omegas[4]);
14
+ static __device__ double _I(const int64_t i, const int64_t ci, const double omega,
15
+ const double vertices_omegas[4]);
16
+ static __device__ double _n(const int64_t i, const double omega,
17
+ const double vertices_omegas[4]);
18
+ static __device__ double _g(const int64_t i, const double omega,
19
+ const double vertices_omegas[4]);
20
+ static __device__ double _n_0(void);
21
+ static __device__ double _n_1(const double omega, const double vertices_omegas[4]);
22
+ static __device__ double _n_2(const double omega, const double vertices_omegas[4]);
23
+ static __device__ double _n_3(const double omega, const double vertices_omegas[4]);
24
+ static __device__ double _n_4(void);
25
+ static __device__ double _g_0(void);
26
+ static __device__ double _g_1(const double omega, const double vertices_omegas[4]);
27
+ static __device__ double _g_2(const double omega, const double vertices_omegas[4]);
28
+ static __device__ double _g_3(const double omega, const double vertices_omegas[4]);
29
+ static __device__ double _g_4(void);
30
+ static __device__ double _J_0(void);
31
+ static __device__ double _J_10(const double omega, const double vertices_omegas[4]);
32
+ static __device__ double _J_11(const double omega, const double vertices_omegas[4]);
33
+ static __device__ double _J_12(const double omega, const double vertices_omegas[4]);
34
+ static __device__ double _J_13(const double omega, const double vertices_omegas[4]);
35
+ static __device__ double _J_20(const double omega, const double vertices_omegas[4]);
36
+ static __device__ double _J_21(const double omega, const double vertices_omegas[4]);
37
+ static __device__ double _J_22(const double omega, const double vertices_omegas[4]);
38
+ static __device__ double _J_23(const double omega, const double vertices_omegas[4]);
39
+ static __device__ double _J_30(const double omega, const double vertices_omegas[4]);
40
+ static __device__ double _J_31(const double omega, const double vertices_omegas[4]);
41
+ static __device__ double _J_32(const double omega, const double vertices_omegas[4]);
42
+ static __device__ double _J_33(const double omega, const double vertices_omegas[4]);
43
+ static __device__ double _J_4(void);
44
+ static __device__ double _I_0(void);
45
+ static __device__ double _I_10(const double omega, const double vertices_omegas[4]);
46
+ static __device__ double _I_11(const double omega, const double vertices_omegas[4]);
47
+ static __device__ double _I_12(const double omega, const double vertices_omegas[4]);
48
+ static __device__ double _I_13(const double omega, const double vertices_omegas[4]);
49
+ static __device__ double _I_20(const double omega, const double vertices_omegas[4]);
50
+ static __device__ double _I_21(const double omega, const double vertices_omegas[4]);
51
+ static __device__ double _I_22(const double omega, const double vertices_omegas[4]);
52
+ static __device__ double _I_23(const double omega, const double vertices_omegas[4]);
53
+ static __device__ double _I_30(const double omega, const double vertices_omegas[4]);
54
+ static __device__ double _I_31(const double omega, const double vertices_omegas[4]);
55
+ static __device__ double _I_32(const double omega, const double vertices_omegas[4]);
56
+ static __device__ double _I_33(const double omega, const double vertices_omegas[4]);
57
+ static __device__ double _I_4(void);
58
+
59
+ static __device__ int64_t sort_omegas(double v[4]) {
60
+ int64_t i;
61
+ double w[4];
62
+
63
+ i = 0;
64
+
65
+ if (v[0] > v[1]) {
66
+ w[0] = v[1];
67
+ w[1] = v[0];
68
+ i = 1;
69
+ } else {
70
+ w[0] = v[0];
71
+ w[1] = v[1];
72
+ }
73
+
74
+ if (v[2] > v[3]) {
75
+ w[2] = v[3];
76
+ w[3] = v[2];
77
+ } else {
78
+ w[2] = v[2];
79
+ w[3] = v[3];
80
+ }
81
+
82
+ if (w[0] > w[2]) {
83
+ v[0] = w[2];
84
+ v[1] = w[0];
85
+ if (i == 0) {
86
+ i = 4;
87
+ }
88
+ } else {
89
+ v[0] = w[0];
90
+ v[1] = w[2];
91
+ }
92
+
93
+ if (w[1] > w[3]) {
94
+ v[3] = w[1];
95
+ v[2] = w[3];
96
+ if (i == 1) {
97
+ i = 3;
98
+ }
99
+ } else {
100
+ v[3] = w[3];
101
+ v[2] = w[1];
102
+ if (i == 1) {
103
+ i = 5;
104
+ }
105
+ }
106
+
107
+ if (v[1] > v[2]) {
108
+ w[1] = v[1];
109
+ v[1] = v[2];
110
+ v[2] = w[1];
111
+ if (i == 4) {
112
+ i = 2;
113
+ }
114
+ if (i == 5) {
115
+ i = 1;
116
+ }
117
+ } else {
118
+ if (i == 4) {
119
+ i = 1;
120
+ }
121
+ if (i == 5) {
122
+ i = 2;
123
+ }
124
+ }
125
+ return i;
126
+ }
127
+
128
+ static __device__ double _f(const int64_t n, const int64_t m, const double omega,
129
+ const double vertices_omegas[4]) {
130
+ double delta;
131
+ delta = vertices_omegas[n] - vertices_omegas[m];
132
+
133
+ #ifdef THM_EPSILON
134
+ if (fabs(delta) < THM_EPSILON) {
135
+ return 0;
136
+ }
137
+ #endif
138
+
139
+ return ((omega - vertices_omegas[m]) / delta);
140
+ }
141
+
142
+ static __device__ double _J(const int64_t i, const int64_t ci, const double omega,
143
+ const double vertices_omegas[4]) {
144
+ switch (i) {
145
+ case 0:
146
+ return _J_0();
147
+ case 1:
148
+ switch (ci) {
149
+ case 0:
150
+ return _J_10(omega, vertices_omegas);
151
+ case 1:
152
+ return _J_11(omega, vertices_omegas);
153
+ case 2:
154
+ return _J_12(omega, vertices_omegas);
155
+ case 3:
156
+ return _J_13(omega, vertices_omegas);
157
+ }
158
+ case 2:
159
+ switch (ci) {
160
+ case 0:
161
+ return _J_20(omega, vertices_omegas);
162
+ case 1:
163
+ return _J_21(omega, vertices_omegas);
164
+ case 2:
165
+ return _J_22(omega, vertices_omegas);
166
+ case 3:
167
+ return _J_23(omega, vertices_omegas);
168
+ }
169
+ case 3:
170
+ switch (ci) {
171
+ case 0:
172
+ return _J_30(omega, vertices_omegas);
173
+ case 1:
174
+ return _J_31(omega, vertices_omegas);
175
+ case 2:
176
+ return _J_32(omega, vertices_omegas);
177
+ case 3:
178
+ return _J_33(omega, vertices_omegas);
179
+ }
180
+ case 4:
181
+ return _J_4();
182
+ }
183
+
184
+ warning_print("******* Warning *******\n");
185
+ warning_print(" J is something wrong. \n");
186
+ warning_print("******* Warning *******\n");
187
+ warning_print("(line %d, %s).\n", __LINE__, __FILE__);
188
+
189
+ return 0;
190
+ }
191
+
192
+ static __device__ double _I(const int64_t i, const int64_t ci, const double omega,
193
+ const double vertices_omegas[4]) {
194
+ switch (i) {
195
+ case 0:
196
+ return _I_0();
197
+ case 1:
198
+ switch (ci) {
199
+ case 0:
200
+ return _I_10(omega, vertices_omegas);
201
+ case 1:
202
+ return _I_11(omega, vertices_omegas);
203
+ case 2:
204
+ return _I_12(omega, vertices_omegas);
205
+ case 3:
206
+ return _I_13(omega, vertices_omegas);
207
+ }
208
+ case 2:
209
+ switch (ci) {
210
+ case 0:
211
+ return _I_20(omega, vertices_omegas);
212
+ case 1:
213
+ return _I_21(omega, vertices_omegas);
214
+ case 2:
215
+ return _I_22(omega, vertices_omegas);
216
+ case 3:
217
+ return _I_23(omega, vertices_omegas);
218
+ }
219
+ case 3:
220
+ switch (ci) {
221
+ case 0:
222
+ return _I_30(omega, vertices_omegas);
223
+ case 1:
224
+ return _I_31(omega, vertices_omegas);
225
+ case 2:
226
+ return _I_32(omega, vertices_omegas);
227
+ case 3:
228
+ return _I_33(omega, vertices_omegas);
229
+ }
230
+ case 4:
231
+ return _I_4();
232
+ }
233
+
234
+ warning_print("******* Warning *******\n");
235
+ warning_print(" I is something wrong. \n");
236
+ warning_print("******* Warning *******\n");
237
+ warning_print("(line %d, %s).\n", __LINE__, __FILE__);
238
+
239
+ return 0;
240
+ }
241
+
242
+ static __device__ double _n(const int64_t i, const double omega,
243
+ const double vertices_omegas[4]) {
244
+ switch (i) {
245
+ case 0:
246
+ return _n_0();
247
+ case 1:
248
+ return _n_1(omega, vertices_omegas);
249
+ case 2:
250
+ return _n_2(omega, vertices_omegas);
251
+ case 3:
252
+ return _n_3(omega, vertices_omegas);
253
+ case 4:
254
+ return _n_4();
255
+ }
256
+
257
+ warning_print("******* Warning *******\n");
258
+ warning_print(" n is something wrong. \n");
259
+ warning_print("******* Warning *******\n");
260
+ warning_print("(line %d, %s).\n", __LINE__, __FILE__);
261
+
262
+ return 0;
263
+ }
264
+
265
+ static __device__ double _g(const int64_t i, const double omega,
266
+ const double vertices_omegas[4]) {
267
+ switch (i) {
268
+ case 0:
269
+ return _g_0();
270
+ case 1:
271
+ return _g_1(omega, vertices_omegas);
272
+ case 2:
273
+ return _g_2(omega, vertices_omegas);
274
+ case 3:
275
+ return _g_3(omega, vertices_omegas);
276
+ case 4:
277
+ return _g_4();
278
+ }
279
+
280
+ warning_print("******* Warning *******\n");
281
+ warning_print(" g is something wrong. \n");
282
+ warning_print("******* Warning *******\n");
283
+ warning_print("(line %d, %s).\n", __LINE__, __FILE__);
284
+
285
+ return 0;
286
+ }
287
+
288
+ /* omega < omega1 */
289
+ static __device__ double _n_0(void) { return 0.0; }
290
+
291
+ /* omega1 < omega < omega2 */
292
+ static __device__ double _n_1(const double omega, const double vertices_omegas[4]) {
293
+ return (_f(1, 0, omega, vertices_omegas) *
294
+ _f(2, 0, omega, vertices_omegas) *
295
+ _f(3, 0, omega, vertices_omegas));
296
+ }
297
+
298
+ /* omega2 < omega < omega3 */
299
+ static __device__ double _n_2(const double omega, const double vertices_omegas[4]) {
300
+ return (
301
+ _f(3, 1, omega, vertices_omegas) * _f(2, 1, omega, vertices_omegas) +
302
+ _f(3, 0, omega, vertices_omegas) * _f(1, 3, omega, vertices_omegas) *
303
+ _f(2, 1, omega, vertices_omegas) +
304
+ _f(3, 0, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas) *
305
+ _f(1, 2, omega, vertices_omegas));
306
+ }
307
+
308
+ /* omega2 < omega < omega3 */
309
+ static __device__ double _n_3(const double omega, const double vertices_omegas[4]) {
310
+ return (1.0 - _f(0, 3, omega, vertices_omegas) *
311
+ _f(1, 3, omega, vertices_omegas) *
312
+ _f(2, 3, omega, vertices_omegas));
313
+ }
314
+
315
+ /* omega4 < omega */
316
+ static __device__ double _n_4(void) { return 1.0; }
317
+
318
+ /* omega < omega1 */
319
+ static __device__ double _g_0(void) { return 0.0; }
320
+
321
+ /* omega1 < omega < omega2 */
322
+ static __device__ double _g_1(const double omega, const double vertices_omegas[4]) {
323
+ return (3 * _f(1, 0, omega, vertices_omegas) *
324
+ _f(2, 0, omega, vertices_omegas) /
325
+ (vertices_omegas[3] - vertices_omegas[0]));
326
+ }
327
+
328
+ /* omega2 < omega < omega3 */
329
+ static __device__ double _g_2(const double omega, const double vertices_omegas[4]) {
330
+ return (
331
+ 3 / (vertices_omegas[3] - vertices_omegas[0]) *
332
+ (_f(1, 2, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas) +
333
+ _f(2, 1, omega, vertices_omegas) * _f(1, 3, omega, vertices_omegas)));
334
+ }
335
+
336
+ /* omega3 < omega < omega4 */
337
+ static __device__ double _g_3(const double omega, const double vertices_omegas[4]) {
338
+ return (3 * _f(1, 3, omega, vertices_omegas) *
339
+ _f(2, 3, omega, vertices_omegas) /
340
+ (vertices_omegas[3] - vertices_omegas[0]));
341
+ }
342
+
343
+ /* omega4 < omega */
344
+ static __device__ double _g_4(void) { return 0.0; }
345
+
346
+ static __device__ double _J_0(void) { return 0.0; }
347
+
348
+ static __device__ double _J_10(const double omega, const double vertices_omegas[4]) {
349
+ return (1.0 + _f(0, 1, omega, vertices_omegas) +
350
+ _f(0, 2, omega, vertices_omegas) +
351
+ _f(0, 3, omega, vertices_omegas)) /
352
+ 4;
353
+ }
354
+
355
+ static __device__ double _J_11(const double omega, const double vertices_omegas[4]) {
356
+ return _f(1, 0, omega, vertices_omegas) / 4;
357
+ }
358
+
359
+ static __device__ double _J_12(const double omega, const double vertices_omegas[4]) {
360
+ return _f(2, 0, omega, vertices_omegas) / 4;
361
+ }
362
+
363
+ static __device__ double _J_13(const double omega, const double vertices_omegas[4]) {
364
+ return _f(3, 0, omega, vertices_omegas) / 4;
365
+ }
366
+
367
+ static __device__ double _J_20(const double omega, const double vertices_omegas[4]) {
368
+ double n;
369
+ n = _n_2(omega, vertices_omegas);
370
+
371
+ #ifdef THM_EPSILON
372
+ if (n < THM_EPSILON) {
373
+ return 0;
374
+ }
375
+ #endif
376
+
377
+ return (_f(3, 1, omega, vertices_omegas) *
378
+ _f(2, 1, omega, vertices_omegas) +
379
+ _f(3, 0, omega, vertices_omegas) *
380
+ _f(1, 3, omega, vertices_omegas) *
381
+ _f(2, 1, omega, vertices_omegas) *
382
+ (1.0 + _f(0, 3, omega, vertices_omegas)) +
383
+ _f(3, 0, omega, vertices_omegas) *
384
+ _f(2, 0, omega, vertices_omegas) *
385
+ _f(1, 2, omega, vertices_omegas) *
386
+ (1.0 + _f(0, 3, omega, vertices_omegas) +
387
+ _f(0, 2, omega, vertices_omegas))) /
388
+ 4 / n;
389
+ }
390
+
391
+ static __device__ double _J_21(const double omega, const double vertices_omegas[4]) {
392
+ double n;
393
+ n = _n_2(omega, vertices_omegas);
394
+
395
+ #ifdef THM_EPSILON
396
+ if (n < THM_EPSILON) {
397
+ return 0;
398
+ }
399
+ #endif
400
+
401
+ return (_f(3, 1, omega, vertices_omegas) *
402
+ _f(2, 1, omega, vertices_omegas) *
403
+ (1.0 + _f(1, 3, omega, vertices_omegas) +
404
+ _f(1, 2, omega, vertices_omegas)) +
405
+ _f(3, 0, omega, vertices_omegas) *
406
+ _f(1, 3, omega, vertices_omegas) *
407
+ _f(2, 1, omega, vertices_omegas) *
408
+ (_f(1, 3, omega, vertices_omegas) +
409
+ _f(1, 2, omega, vertices_omegas)) +
410
+ _f(3, 0, omega, vertices_omegas) *
411
+ _f(2, 0, omega, vertices_omegas) *
412
+ _f(1, 2, omega, vertices_omegas) *
413
+ _f(1, 2, omega, vertices_omegas)) /
414
+ 4 / n;
415
+ }
416
+
417
+ static __device__ double _J_22(const double omega, const double vertices_omegas[4]) {
418
+ double n;
419
+ n = _n_2(omega, vertices_omegas);
420
+
421
+ #ifdef THM_EPSILON
422
+ if (n < THM_EPSILON) {
423
+ return 0;
424
+ }
425
+ #endif
426
+
427
+ return (_f(3, 1, omega, vertices_omegas) *
428
+ _f(2, 1, omega, vertices_omegas) *
429
+ _f(2, 1, omega, vertices_omegas) +
430
+ _f(3, 0, omega, vertices_omegas) *
431
+ _f(1, 3, omega, vertices_omegas) *
432
+ _f(2, 1, omega, vertices_omegas) *
433
+ _f(2, 1, omega, vertices_omegas) +
434
+ _f(3, 0, omega, vertices_omegas) *
435
+ _f(2, 0, omega, vertices_omegas) *
436
+ _f(1, 2, omega, vertices_omegas) *
437
+ (_f(2, 1, omega, vertices_omegas) +
438
+ _f(2, 0, omega, vertices_omegas))) /
439
+ 4 / n;
440
+ }
441
+
442
+ static __device__ double _J_23(const double omega, const double vertices_omegas[4]) {
443
+ double n;
444
+ n = _n_2(omega, vertices_omegas);
445
+
446
+ #ifdef THM_EPSILON
447
+ if (n < THM_EPSILON) {
448
+ return 0;
449
+ }
450
+ #endif
451
+
452
+ return (_f(3, 1, omega, vertices_omegas) *
453
+ _f(2, 1, omega, vertices_omegas) *
454
+ _f(3, 1, omega, vertices_omegas) +
455
+ _f(3, 0, omega, vertices_omegas) *
456
+ _f(1, 3, omega, vertices_omegas) *
457
+ _f(2, 1, omega, vertices_omegas) *
458
+ (_f(3, 1, omega, vertices_omegas) +
459
+ _f(3, 0, omega, vertices_omegas)) +
460
+ _f(3, 0, omega, vertices_omegas) *
461
+ _f(2, 0, omega, vertices_omegas) *
462
+ _f(1, 2, omega, vertices_omegas) *
463
+ _f(3, 0, omega, vertices_omegas)) /
464
+ 4 / n;
465
+ }
466
+
467
+ static __device__ double _J_30(const double omega, const double vertices_omegas[4]) {
468
+ double n;
469
+ n = _n_3(omega, vertices_omegas);
470
+
471
+ #ifdef THM_EPSILON
472
+ if (n < THM_EPSILON) {
473
+ return 0;
474
+ }
475
+ #endif
476
+
477
+ return (1.0 - _f(0, 3, omega, vertices_omegas) *
478
+ _f(0, 3, omega, vertices_omegas) *
479
+ _f(1, 3, omega, vertices_omegas) *
480
+ _f(2, 3, omega, vertices_omegas)) /
481
+ 4 / n;
482
+ }
483
+
484
+ static __device__ double _J_31(const double omega, const double vertices_omegas[4]) {
485
+ double n;
486
+ n = _n_3(omega, vertices_omegas);
487
+
488
+ #ifdef THM_EPSILON
489
+ if (n < THM_EPSILON) {
490
+ return 0;
491
+ }
492
+ #endif
493
+
494
+ return (1.0 - _f(0, 3, omega, vertices_omegas) *
495
+ _f(1, 3, omega, vertices_omegas) *
496
+ _f(1, 3, omega, vertices_omegas) *
497
+ _f(2, 3, omega, vertices_omegas)) /
498
+ 4 / n;
499
+ }
500
+
501
+ static __device__ double _J_32(const double omega, const double vertices_omegas[4]) {
502
+ double n;
503
+ n = _n_3(omega, vertices_omegas);
504
+
505
+ #ifdef THM_EPSILON
506
+ if (n < THM_EPSILON) {
507
+ return 0;
508
+ }
509
+ #endif
510
+
511
+ return (1.0 - _f(0, 3, omega, vertices_omegas) *
512
+ _f(1, 3, omega, vertices_omegas) *
513
+ _f(2, 3, omega, vertices_omegas) *
514
+ _f(2, 3, omega, vertices_omegas)) /
515
+ 4 / n;
516
+ }
517
+
518
+ static __device__ double _J_33(const double omega, const double vertices_omegas[4]) {
519
+ double n;
520
+ n = _n_3(omega, vertices_omegas);
521
+
522
+ #ifdef THM_EPSILON
523
+ if (n < THM_EPSILON) {
524
+ return 0;
525
+ }
526
+ #endif
527
+
528
+ return (1.0 - _f(0, 3, omega, vertices_omegas) *
529
+ _f(1, 3, omega, vertices_omegas) *
530
+ _f(2, 3, omega, vertices_omegas) *
531
+ (1.0 + _f(3, 0, omega, vertices_omegas) +
532
+ _f(3, 1, omega, vertices_omegas) +
533
+ _f(3, 2, omega, vertices_omegas))) /
534
+ 4 / n;
535
+ }
536
+
537
+ static __device__ double _J_4(void) { return 0.25; }
538
+
539
+ static __device__ double _I_0(void) { return 0.0; }
540
+
541
+ static __device__ double _I_10(const double omega, const double vertices_omegas[4]) {
542
+ return (_f(0, 1, omega, vertices_omegas) +
543
+ _f(0, 2, omega, vertices_omegas) +
544
+ _f(0, 3, omega, vertices_omegas)) /
545
+ 3;
546
+ }
547
+
548
+ static __device__ double _I_11(const double omega, const double vertices_omegas[4]) {
549
+ return _f(1, 0, omega, vertices_omegas) / 3;
550
+ }
551
+
552
+ static __device__ double _I_12(const double omega, const double vertices_omegas[4]) {
553
+ return _f(2, 0, omega, vertices_omegas) / 3;
554
+ }
555
+
556
+ static __device__ double _I_13(const double omega, const double vertices_omegas[4]) {
557
+ return _f(3, 0, omega, vertices_omegas) / 3;
558
+ }
559
+
560
+ static __device__ double _I_20(const double omega, const double vertices_omegas[4]) {
561
+ double f12_20, g;
562
+ f12_20 =
563
+ _f(1, 2, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas);
564
+ g = f12_20 +
565
+ _f(2, 1, omega, vertices_omegas) * _f(1, 3, omega, vertices_omegas);
566
+
567
+ #ifdef THM_EPSILON
568
+ if (g < THM_EPSILON) {
569
+ return 0;
570
+ }
571
+ #endif
572
+
573
+ return (_f(0, 3, omega, vertices_omegas) +
574
+ _f(0, 2, omega, vertices_omegas) * f12_20 / g) /
575
+ 3;
576
+ }
577
+
578
+ static __device__ double _I_21(const double omega, const double vertices_omegas[4]) {
579
+ double f13_21, g;
580
+ f13_21 =
581
+ _f(1, 3, omega, vertices_omegas) * _f(2, 1, omega, vertices_omegas);
582
+ g = _f(1, 2, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas) +
583
+ f13_21;
584
+
585
+ #ifdef THM_EPSILON
586
+ if (g < THM_EPSILON) {
587
+ return 0;
588
+ }
589
+ #endif
590
+
591
+ return (_f(1, 2, omega, vertices_omegas) +
592
+ _f(1, 3, omega, vertices_omegas) * f13_21 / g) /
593
+ 3;
594
+ }
595
+
596
+ static __device__ double _I_22(const double omega, const double vertices_omegas[4]) {
597
+ double f12_20, g;
598
+ f12_20 =
599
+ _f(1, 2, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas);
600
+ g = f12_20 +
601
+ _f(2, 1, omega, vertices_omegas) * _f(1, 3, omega, vertices_omegas);
602
+
603
+ #ifdef THM_EPSILON
604
+ if (g < THM_EPSILON) {
605
+ return 0;
606
+ }
607
+ #endif
608
+
609
+ return (_f(2, 1, omega, vertices_omegas) +
610
+ _f(2, 0, omega, vertices_omegas) * f12_20 / g) /
611
+ 3;
612
+ }
613
+
614
+ static __device__ double _I_23(const double omega, const double vertices_omegas[4]) {
615
+ double f13_21, g;
616
+ f13_21 =
617
+ _f(1, 3, omega, vertices_omegas) * _f(2, 1, omega, vertices_omegas);
618
+ g = _f(1, 2, omega, vertices_omegas) * _f(2, 0, omega, vertices_omegas) +
619
+ f13_21;
620
+
621
+ #ifdef THM_EPSILON
622
+ if (g < THM_EPSILON) {
623
+ return 0;
624
+ }
625
+ #endif
626
+
627
+ return (_f(3, 0, omega, vertices_omegas) +
628
+ _f(3, 1, omega, vertices_omegas) * f13_21 / g) /
629
+ 3;
630
+ }
631
+
632
+ static __device__ double _I_30(const double omega, const double vertices_omegas[4]) {
633
+ return _f(0, 3, omega, vertices_omegas) / 3;
634
+ }
635
+
636
+ static __device__ double _I_31(const double omega, const double vertices_omegas[4]) {
637
+ return _f(1, 3, omega, vertices_omegas) / 3;
638
+ }
639
+
640
+ static __device__ double _I_32(const double omega, const double vertices_omegas[4]) {
641
+ return _f(2, 3, omega, vertices_omegas) / 3;
642
+ }
643
+
644
+ static __device__ double _I_33(const double omega, const double vertices_omegas[4]) {
645
+ return (_f(3, 0, omega, vertices_omegas) +
646
+ _f(3, 1, omega, vertices_omegas) +
647
+ _f(3, 2, omega, vertices_omegas)) /
648
+ 3;
649
+ }
650
+
651
+ static __device__ double _I_4(void) { return 0.0; }
652
+
653
+ // For a band pair, every initial frequency outside these extrema has
654
+ // exactly zero support in the corresponding tetrahedron channel.
655
+ extern "C" __global__ void channel_bounds(
656
+ double* bounds, const double* freq, const long long* vertices,
657
+ int nt, int nb, int nth) {
658
+ long long idx = (long long)blockIdx.x*blockDim.x + threadIdx.x;
659
+ long long pairs = (long long)nt*nb*nb;
660
+ if (idx >= pairs) return;
661
+ int t=idx/(nb*nb), j=(idx/nb)%nb, k=idx%nb;
662
+ const long long* first = vertices + (long long)t*2*nth*4;
663
+ double x0 = fmax(freq[first[0]*nb+j], 0.);
664
+ double y0 = fmax(freq[first[nth*4]*nb+k], 0.);
665
+ double lo=x0+y0, hi=lo, dlo=y0-x0, dhi=dlo;
666
+ for (int v=1; v<nth*4; ++v) {
667
+ double x = fmax(freq[first[v]*nb+j], 0.);
668
+ double y = fmax(freq[first[nth*4+v]*nb+k], 0.);
669
+ lo=fmin(lo,x+y); hi=fmax(hi,x+y);
670
+ dlo=fmin(dlo,y-x); dhi=fmax(dhi,y-x);
671
+ }
672
+ bounds[idx]=lo; bounds[pairs+idx]=hi;
673
+ bounds[2*pairs+idx]=dlo; bounds[3*pairs+idx]=dhi;
674
+ }
675
+ // vertices: (triplet, 2, tetrahedron, 4); each work item is one band triple.
676
+ extern "C" __global__ void weights(
677
+ double* out, signed char* zero, const double* freq, const double* f0,
678
+ const long long* vertices, int nt, int nb0, int nb, int nth, int nc,
679
+ int frequency_stride, const double* bounds) {
680
+ long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;
681
+ long long size = (long long)nt*nb0*nb*nb;
682
+ if (idx >= size) return;
683
+ int k = idx % nb, j = (idx/nb)%nb, b = (idx/(nb*nb))%nb0;
684
+ int t = idx/(nb0*nb*nb);
685
+ double total[3] = {0., 0., 0.};
686
+ bool active = false;
687
+ double w = f0[t*frequency_stride+b];
688
+ long long pairs = (long long)nt*nb*nb;
689
+ long long pair = ((long long)t*nb+j)*nb+k;
690
+ for (int ch=0; ch<3; ++ch) {
691
+ if (bounds) {
692
+ double lo, hi;
693
+ if (ch==0) { lo=bounds[pair]; hi=bounds[pairs+pair]; }
694
+ else if (ch==1) { lo=bounds[2*pairs+pair]; hi=bounds[3*pairs+pair]; }
695
+ else { lo=-bounds[3*pairs+pair]; hi=-bounds[2*pairs+pair]; }
696
+ if (w<lo || w>hi) continue;
697
+ }
698
+ for (int h=0; h<nth; ++h) {
699
+ double v[4];
700
+ for (int l=0; l<4; ++l) {
701
+ long long a = vertices[((t*2)*nth+h)*4+l];
702
+ long long c = vertices[((t*2+1)*nth+h)*4+l];
703
+ double x = fmax(freq[a*nb+j], 0.);
704
+ double y = fmax(freq[c*nb+k], 0.);
705
+ v[l] = ch==0 ? x+y : (ch==1 ? -x+y : x-y);
706
+ }
707
+ int center = sort_omegas(v);
708
+ if (w>=v[0] && w<=v[3]) active=true;
709
+ int region = w<v[0] ? 0 : w<v[1] ? 1 : w<v[2] ? 2 : w<v[3] ? 3 : 4;
710
+ total[ch] += _I(region, center, w, v)*_g(region, w, v);
711
+ }
712
+ total[ch] *= 4.0/nth;
713
+ }
714
+ out[idx] = total[0];
715
+ out[size+idx] = total[1]-total[2];
716
+ if (nc==3) out[2*size+idx] = total[0]+total[1]+total[2];
717
+ zero[idx] = !active;
718
+ }
719
+
720
+ extern "C" __global__ void spectral_weights(
721
+ double *out, const double *freq, const double *f0,
722
+ const long long *vertices, int nq, int nb0, int nb, int nth) {
723
+ long long index = blockIdx.x*(long long)blockDim.x+threadIdx.x;
724
+ if (index >= (long long)nq*nb0*nb) return;
725
+ int b = index%nb;
726
+ int a = (index/nb)%nb0;
727
+ int q = index/(nb*nb0);
728
+ double sum = 0;
729
+ for (int t=0;t<nth;t++) {
730
+ double v[4];
731
+ for (int j=0;j<4;j++) v[j]=freq[vertices[(q*nth+t)*4+j]*nb+b];
732
+ int center = sort_omegas(v);
733
+ double w=f0[a];
734
+ int region = w<v[0]?0:w<v[1]?1:w<v[2]?2:w<v[3]?3:4;
735
+ sum += _I(region,center,w,v)*_g(region,w,v);
736
+ }
737
+ out[index]=sum*4.0/nth;
738
+ }
739
+
740
+ // One warp shares vertex sorting for a final-band pair across initial bands.
741
+ extern "C" __global__ void weights_shared(
742
+ double* out, signed char* zero, const double* freq, const double* f0,
743
+ const long long* vertices, int nt, int nb0, int nb, int nth, int nc,
744
+ int frequency_stride, const double* bounds) {
745
+ int lane = threadIdx.x % 32;
746
+ long long pair = (long long)blockIdx.x * (blockDim.x / 32) + threadIdx.x / 32;
747
+ long long pairs = (long long)nt * nb * nb;
748
+ if (pair >= pairs) return;
749
+ int t = pair / (nb * nb), j = (pair / nb) % nb, k = pair % nb;
750
+ long long size = (long long)nt * nb0 * nb * nb;
751
+ for (int first = 0; first < nb0; first += 32) {
752
+ int b = first + lane;
753
+ double w = b < nb0 ? f0[t * frequency_stride + b] : 0.;
754
+ double total[3] = {0., 0., 0.};
755
+ bool active = false;
756
+ for (int ch = 0; ch < 3; ++ch) {
757
+ bool valid = b < nb0;
758
+ if (bounds) {
759
+ double lo, hi;
760
+ if (ch == 0) {
761
+ lo = bounds[pair]; hi = bounds[pairs + pair];
762
+ } else if (ch == 1) {
763
+ lo = bounds[2 * pairs + pair]; hi = bounds[3 * pairs + pair];
764
+ } else {
765
+ lo = -bounds[3 * pairs + pair]; hi = -bounds[2 * pairs + pair];
766
+ }
767
+ valid = valid && w >= lo && w <= hi;
768
+ }
769
+ if (!__any_sync(0xffffffff, valid)) continue;
770
+ for (int h = 0; h < nth; ++h) {
771
+ double v[4] = {0., 0., 0., 0.};
772
+ int center = 0;
773
+ if (lane == 0) {
774
+ for (int l = 0; l < 4; ++l) {
775
+ long long a = vertices[((t * 2) * nth + h) * 4 + l];
776
+ long long c = vertices[((t * 2 + 1) * nth + h) * 4 + l];
777
+ double x = fmax(freq[a * nb + j], 0.);
778
+ double y = fmax(freq[c * nb + k], 0.);
779
+ v[l] = ch == 0 ? x + y : ch == 1 ? -x + y : x - y;
780
+ }
781
+ center = sort_omegas(v);
782
+ }
783
+ for (int l = 0; l < 4; ++l) v[l] = __shfl_sync(0xffffffff, v[l], 0);
784
+ center = __shfl_sync(0xffffffff, center, 0);
785
+ if (valid) {
786
+ if (w >= v[0] && w <= v[3]) active = true;
787
+ int region = w < v[0] ? 0 : w < v[1] ? 1 : w < v[2] ? 2 : w < v[3] ? 3 : 4;
788
+ total[ch] += _I(region, center, w, v) * _g(region, w, v);
789
+ }
790
+ }
791
+ total[ch] *= 4.0 / nth;
792
+ }
793
+ if (b < nb0) {
794
+ long long idx = ((long long)t * nb0 + b) * nb * nb + j * nb + k;
795
+ out[idx] = total[0];
796
+ out[size + idx] = total[1] - total[2];
797
+ if (nc == 3) out[2 * size + idx] = total[0] + total[1] + total[2];
798
+ zero[idx] = !active;
799
+ }
800
+ }
801
+ }