1#ifndef SEAMS_STEINHARDT_DEVICE_H_
2#define SEAMS_STEINHARDT_DEVICE_H_
14#ifdef SEAMS_HAS_OFFLOAD
15#pragma omp declare target
23inline void minImage(
double &dx,
double &dy,
double &dz,
double bx,
double by,
45inline double normLM(
int orderL,
int absM) {
46 constexpr double pi = 3.14159265358979323846;
50 return 0.25 * std::sqrt(7.0 / pi);
52 return 0.125 * std::sqrt(21.0 / pi);
54 return 0.25 * std::sqrt(105.0 / (2.0 * pi));
56 return 0.125 * std::sqrt(35.0 / pi);
64 return 0.1875 * std::sqrt(1.0 / pi);
66 return 0.375 * std::sqrt(5.0 / pi);
68 return 0.375 * std::sqrt(5.0 / (2.0 * pi));
70 return 0.375 * std::sqrt(35.0 / pi);
72 return 0.1875 * std::sqrt(35.0 / (2.0 * pi));
80 return 0.03125 * std::sqrt(13.0 / pi);
82 return 0.0625 * std::sqrt(273.0 / (2.0 * pi));
84 return 0.015625 * std::sqrt(1365.0 / pi);
86 return 0.03125 * std::sqrt(1365.0 / pi);
88 return 0.09375 * std::sqrt(91.0 / (2.0 * pi));
90 return 0.09375 * std::sqrt(1001.0 / pi);
92 return 0.015625 * std::sqrt(3003.0 / pi);
100 return std::sqrt(17.0 / (4.0 * pi));
102 return std::sqrt(17.0 / (288.0 * pi));
104 return std::sqrt(17.0 / (20160.0 * pi));
106 return std::sqrt(17.0 / (1330560.0 * pi));
108 return std::sqrt(17.0 / (79833600.0 * pi));
110 return std::sqrt(17.0 / (4151347200.0 * pi));
112 return std::sqrt(17.0 / (174356582400.0 * pi));
114 return std::sqrt(17.0 / (5230697472000.0 * pi));
116 return std::sqrt(17.0 / (83691159552000.0 * pi));
129 return 5.0 * c[3] - 3.0 * c[1];
131 return s[1] * (5.0 * c[2] - 1.0);
143 return 35.0 * c[4] - 30.0 * c[2] + 3.0;
145 return s[1] * (7.0 * c[3] - 3.0 * c[1]);
147 return s[2] * (7.0 * c[2] - 1.0);
159 return 231.0 * c[6] - 315.0 * c[4] + 105.0 * c[2] - 5.0;
161 return s[1] * (33.0 * c[5] - 30.0 * c[3] + 5.0 * c[1]);
163 return s[2] * (33.0 * c[4] - 18.0 * c[2] + 1.0);
165 return s[3] * (11.0 * c[3] - 3.0 * c[1]);
167 return s[4] * (11.0 * c[2] - 1.0);
179 return (35.0 + c[2] * (-1260.0 + c[2] * (6930.0 + c[2] * (-12012.0 + c[2] * 6435.0)))) / 128.0;
181 return s[1] * c[1] * (-315.0 + c[2] * (3465.0 + c[2] * (-9009.0 + c[2] * 6435.0))) / 16.0;
183 return s[2] * (-315.0 + c[2] * (10395.0 + c[2] * (-45045.0 + c[2] * 45045.0))) / 16.0;
185 return s[3] * c[1] * (10395.0 + c[2] * (-90090.0 + c[2] * 135135.0)) / 8.0;
187 return s[4] * (10395.0 + c[2] * (-270270.0 + c[2] * 675675.0)) / 8.0;
189 return s[5] * c[1] * (-135135.0 + c[2] * 675675.0) / 2.0;
191 return s[6] * (-135135.0 + c[2] * 2027025.0) / 2.0;
193 return s[7] * c[1] * 2027025.0;
195 return s[8] * 2027025.0;
207inline void ylmAllTrig(
int orderL,
double sinT,
double cosT,
double cphi,
208 double sphi,
double *out) {
209 const int nComp = 2 * orderL + 1;
210 for (
int k = 0; k < nComp; k++) {
212 out[2 * k + 1] = 0.0;
223 for (
int k = 1; k <= orderL; k++) {
224 s[k] = s[k - 1] * sinT;
225 c[k] = c[k - 1] * cosT;
226 pr[k] = pr[k - 1] * cphi - pi[k - 1] * sphi;
227 pi[k] = pr[k - 1] * sphi + pi[k - 1] * cphi;
230 for (
int absM = 0; absM <= orderL; absM++) {
233 const double nre = amp * pr[absM];
234 const double nim = -amp * pi[absM];
235 const int ineg = orderL - absM;
237 out[2 * ineg + 1] = nim;
239 const double sign = (absM % 2 == 0) ? 1.0 : -1.0;
240 const int ipos = orderL + absM;
241 out[2 * ipos] = sign * amp * pr[absM];
242 out[2 * ipos + 1] = sign * amp * pi[absM];
246inline void ylmAll(
int orderL,
double theta,
double phi,
double *out) {
247 ylmAllTrig(orderL, std::sin(theta), std::cos(theta), std::cos(phi),
254inline void qlmAddBond(
int orderL,
double dx,
double dy,
double dz,
255 double *qlmInterleaved,
int row,
int nComp,
257 if (orderL == 3 || orderL == 4 || orderL == 6) {
263 for (
int m = 0; m < nComp; m++) {
264 qlmInterleaved[2 * (row + m)] += re[m];
265 qlmInterleaved[2 * (row + m) + 1] += im[m];
270 const double r2 = dx * dx + dy * dy + dz * dz;
274 const double r = std::sqrt(r2);
275 const double invr = 1.0 / r;
276 double cosT = dz * invr;
279 }
else if (cosT < -1.0) {
282 const double rho2 = dx * dx + dy * dy;
287 const double rho = std::sqrt(rho2);
289 const double invrho = 1.0 / rho;
295 for (
int m = 0; m < nComp; m++) {
296 qlmInterleaved[2 * (row + m)] +=
ylm[2 * m];
297 qlmInterleaved[2 * (row + m) + 1] +=
ylm[2 * m + 1];
304inline void qlmOneAtom(
int iatom,
int orderL,
const double *xyz,
305 const int *offsets,
const int *cols,
double bx,
306 double by,
double bz,
double *qlmInterleaved) {
310 const int nComp = 2 * orderL + 1;
311 const int iOff = 3 * iatom;
312 const int row = iatom * nComp;
313 for (
int m = 0; m < nComp; m++) {
314 qlmInterleaved[2 * (row + m)] = 0.0;
315 qlmInterleaved[2 * (row + m) + 1] = 0.0;
317 const int j0 = offsets[iatom];
318 const int j1 = offsets[iatom + 1];
320 for (
int p = j0; p < j1; p++) {
321 const int jatom = cols[p];
322 const double dx = xyz[iOff] - xyz[3 * jatom];
323 const double dy = xyz[iOff + 1] - xyz[3 * jatom + 1];
324 const double dz = xyz[iOff + 2] - xyz[3 * jatom + 2];
325 qlmAddBond(orderL, dx, dy, dz, qlmInterleaved, row, nComp, nUsed);
330 const double inv = 1.0 /
static_cast<double>(nUsed);
331 for (
int m = 0; m < nComp; m++) {
332 qlmInterleaved[2 * (row + m)] *= inv;
333 qlmInterleaved[2 * (row + m) + 1] *= inv;
340 const int *offsets,
const int *cols,
341 double *qlmInterleaved) {
342 const int nComp = 2 * orderL + 1;
343 const int row = iatom * nComp;
344 for (
int m = 0; m < nComp; m++) {
345 qlmInterleaved[2 * (row + m)] = 0.0;
346 qlmInterleaved[2 * (row + m) + 1] = 0.0;
348 const int j0 = offsets[iatom];
349 const int j1 = offsets[iatom + 1];
351 for (
int p = j0; p < j1; p++) {
352 qlmAddBond(orderL, dr[3 * p], dr[3 * p + 1], dr[3 * p + 2],
353 qlmInterleaved, row, nComp, nUsed);
358 const double inv = 1.0 /
static_cast<double>(nUsed);
359 for (
int m = 0; m < nComp; m++) {
360 qlmInterleaved[2 * (row + m)] *= inv;
361 qlmInterleaved[2 * (row + m) + 1] *= inv;
365inline void qlOneAtom(
int iatom,
int orderL,
const double *qlmInterleaved,
366 const int *offsets,
const int *cols,
double *ql,
368 constexpr double pi = 3.14159265358979323846;
369 const int nComp = 2 * orderL + 1;
370 const double prefactor = 4.0 * pi / (2.0 * orderL + 1.0);
371 const int row = iatom * nComp;
372 double sumLocal = 0.0;
373 for (
int m = 0; m < nComp; m++) {
374 const double re = qlmInterleaved[2 * (row + m)];
375 const double im = qlmInterleaved[2 * (row + m) + 1];
376 sumLocal += re * re + im * im;
378 ql[iatom] = std::sqrt(prefactor * sumLocal);
380 const int j0 = offsets[iatom];
381 const int j1 = offsets[iatom + 1];
383 qlBar[iatom] = ql[iatom];
388 for (
int m = 0; m < nComp; m++) {
389 barRe[m] = qlmInterleaved[2 * (row + m)];
390 barIm[m] = qlmInterleaved[2 * (row + m) + 1];
393 for (
int p = j0; p < j1; p++) {
394 const int jatom = cols[p];
395 const int jRow = jatom * nComp;
396 for (
int m = 0; m < nComp; m++) {
397 barRe[m] += qlmInterleaved[2 * (jRow + m)];
398 barIm[m] += qlmInterleaved[2 * (jRow + m) + 1];
402 const double inv = 1.0 /
static_cast<double>(nContrib);
404 for (
int m = 0; m < nComp; m++) {
405 const double re = barRe[m] * inv;
406 const double im = barIm[m] * inv;
407 sumBar += re * re + im * im;
409 qlBar[iatom] = std::sqrt(prefactor * sumBar);
415#ifdef SEAMS_HAS_OFFLOAD
416#pragma omp end declare target
void qlmOneAtom(int iatom, int orderL, const double *xyz, const int *offsets, const int *cols, double bx, double by, double bz, double *qlmInterleaved)
double legendreAmp(int orderL, int absM, const double *s, const double *c)
void minImage(double &dx, double &dy, double &dz, double bx, double by, double bz)
void qlmOneAtomDr(int iatom, int orderL, const double *dr, const int *offsets, const int *cols, double *qlmInterleaved)
void qlOneAtom(int iatom, int orderL, const double *qlmInterleaved, const int *offsets, const int *cols, double *ql, double *qlBar)
void ylmAll(int orderL, double theta, double phi, double *out)
double normLM(int orderL, int absM)
void qlmAddBond(int orderL, double dx, double dy, double dz, double *qlmInterleaved, int row, int nComp, int &nUsed)
void ylmAllTrig(int orderL, double sinT, double cosT, double cphi, double sphi, double *out)
bool ylmReal(int orderL, double dx, double dy, double dz, double *re, double *im)