21#ifndef _libint2_include_solidharmonics_h_
22#define _libint2_include_solidharmonics_h_
24#include <libint2/util/cxxstd.h>
25#if LIBINT2_CPLUSPLUS_STD < 2011
26#error "The simple Libint API requires C++11 support"
35#ifndef LIBINT2_REALTYPE
36#define LIBINT2_REALTYPE double
38#include <libint2/cgshell_ordering.h>
39#include <libint2/initialize.h>
40#include <libint2/shell.h>
43template <
typename Int>
44signed char parity(Int i) {
45 return i % 2 ? -1 : 1;
51namespace solidharmonics {
58template <
typename Real>
61 typedef ::libint2::value_type real_t;
65 assert(l <= std::numeric_limits<signed char>::max());
70 : values_(std::move(other.values_)),
71 row_offset_(std::move(other.row_offset_)),
72 colidx_(std::move(other.colidx_)),
77 void init(
unsigned char l) {
78 assert(l <= std::numeric_limits<signed char>::max());
84 static std::vector<SolidHarmonicsCoefficients> shg_coefs(
85 SolidHarmonicsCoefficients::CtorHelperIter(0),
86 SolidHarmonicsCoefficients::CtorHelperIter(LIBINT_HARD_MAX_AM + 1));
87 assert(l < shg_coefs.size());
97 return &values_[0] + row_offset_[r];
101 return &colidx_[0] + row_offset_[r];
104 unsigned int nnz(
size_t r)
const {
105 return row_offset_[r + 1] - row_offset_[r];
114 static Real
coeff(
int l,
int m,
int lx,
int ly,
int lz) {
115 using libint2::math::bc_real;
116 using libint2::math::df_Kminus1_real;
117 using libint2::math::fac_real;
119 auto abs_m = std::abs(m);
120 if ((lx + ly - abs_m) % 2)
return 0.0;
122 auto j = (lx + ly - abs_m) / 2;
123 if (j < 0)
return 0.0;
129 auto comp = (m >= 0) ? 1 : -1;
132 if (comp != parity(abs(i)))
return 0.0;
135 pfac = sqrt(((fac_real<Real>(2 * lx) * fac_real<Real>(2 * ly) *
136 fac_real<Real>(2 * lz)) /
137 fac_real<Real>(2 * l)) *
138 ((fac_real<Real>(l - abs_m)) / (fac_real<Real>(l))) *
139 (Real(1) / fac_real<Real>(l + abs_m)) *
140 (Real(1) / (fac_real<Real>(lx) * fac_real<Real>(ly) *
141 fac_real<Real>(lz))));
147 pfac *= parity((i - 1) / 2);
149 pfac *= parity(i / 2);
152 auto i_max = (l - abs_m) / 2;
154 for (
auto i = i_min; i <= i_max; i++) {
155 Real pfac1 = bc_real<Real>(l, i) * bc_real<Real>(i, j);
156 pfac1 *= (Real(parity(i) * fac_real<Real>(2 * (l - i))) /
157 fac_real<Real>(l - abs_m - 2 * i));
159 const int k_min = std::max((lx - abs_m) / 2, 0);
160 const int k_max = std::min(j, lx / 2);
161 for (
int k = k_min; k <= k_max; k++) {
162 if (lx - 2 * k <= abs_m)
163 sum1 += bc_real<Real>(j, k) * bc_real<Real>(abs_m, lx - 2 * k) *
168 sum *= sqrt(df_Kminus1_real<Real>(2 * l) /
169 (df_Kminus1_real<Real>(2 * lx) * df_Kminus1_real<Real>(2 * ly) *
170 df_Kminus1_real<Real>(2 * lz)));
172 Real result = (m == 0) ? pfac * sum : M_SQRT2 * pfac * sum;
177 std::vector<Real> values_;
178 std::vector<unsigned int>
180 std::vector<unsigned int> colidx_;
184 const unsigned int npure = 2 * l_ + 1;
185 const unsigned int ncart = (l_ + 1) * (l_ + 2) / 2;
186 std::vector<Real> full_coeff(npure * ncart);
188 std::vector<int> shg_indices;
190 libint2::SHGShellOrdering_Standard) {
191 for (
signed int pure_idx = 0, m = -l_; pure_idx != npure; ++pure_idx, ++m)
192 shg_indices.push_back(m);
194 libint2::SHGShellOrdering_Gaussian) {
195 for (
signed int pure_idx = 0, m = 0; pure_idx != npure;
196 ++pure_idx, m = (m > 0 ? -m : 1 - m))
197 shg_indices.push_back(m);
199 throw std::invalid_argument(std::string(
200 "libint2::solid_harmonics_ordering() value not recognized."));
203 for (
signed int pure_idx = 0; pure_idx != npure; ++pure_idx) {
204 int m = shg_indices[pure_idx];
207 FOR_CART(lx, ly, lz, l_)
208 full_coeff[pure_idx * ncart + cart_idx] =
coeff(l_, m, lx, ly, lz);
219 for (
size_t i = 0; i != full_coeff.size(); ++i)
220 nnz += full_coeff[i] == 0.0 ? 0 : 1;
224 row_offset_.resize(npure + 1);
228 unsigned int cnt = 0;
229 for (
unsigned int p = 0; p != npure; ++p) {
230 row_offset_[p] = cnt;
231 for (
unsigned int c = 0; c != ncart; ++c, ++pc) {
232 if (full_coeff[pc] != 0.0) {
233 values_[cnt] = full_coeff[pc];
239 row_offset_[npure] = cnt;
244 struct CtorHelperIter {
245 using iterator_category = std::input_iterator_tag;
246 using value_type = SolidHarmonicsCoefficients;
247 using difference_type = std::ptrdiff_t;
248 using pointer = value_type*;
249 using reference = value_type&;
253 CtorHelperIter() =
default;
254 CtorHelperIter(
unsigned int l) : l_(l) {}
255 CtorHelperIter(
const CtorHelperIter&) =
default;
256 CtorHelperIter& operator=(
const CtorHelperIter& rhs) {
261 CtorHelperIter& operator++() {
265 CtorHelperIter& operator--() {
271 value_type operator*()
const {
return value_type(l_); }
272 bool operator==(
const CtorHelperIter& rhs)
const {
return l_ == rhs.l_; }
273 bool operator!=(
const CtorHelperIter& rhs)
const {
274 return not(*
this == rhs);
281template <
typename Real>
282void transform_first(
size_t l,
size_t n2,
const Real* src, Real* tgt) {
283 const auto& coefs = SolidHarmonicsCoefficients<Real>::instance(l);
285 const auto n = 2 * l + 1;
286 std::fill(tgt, tgt + n * n2, 0);
289 for (
size_t s = 0; s != n; ++s) {
290 const auto nc_s = coefs.nnz(s);
293 const auto* c_vals = coefs.row_values(
296 const auto tgt_blk_s_offset = s * n2;
298 for (
size_t ic = 0; ic != nc_s;
300 const auto c = c_idxs[ic];
301 const auto s_c_coeff = c_vals[ic];
303 auto src_blk_s = src + c * n2;
304 auto tgt_blk_s = tgt + tgt_blk_s_offset;
307 for (
size_t i2 = 0; i2 != n2; ++i2, ++src_blk_s, ++tgt_blk_s) {
308 *tgt_blk_s += s_c_coeff * *src_blk_s;
316template <
typename Real>
317void transform_first2(
int l1,
int l2,
size_t inner_dim,
const Real* source_blk,
319 const auto& coefs1 = SolidHarmonicsCoefficients<Real>::instance(l1);
320 const auto& coefs2 = SolidHarmonicsCoefficients<Real>::instance(l2);
322 const auto ncart2 = (l2 + 1) * (l2 + 2) / 2;
323 const auto npure1 = 2 * l1 + 1;
324 const auto npure2 = 2 * l2 + 1;
325 const auto ncart2inner = ncart2 * inner_dim;
326 const auto npure2inner = npure2 * inner_dim;
327 std::fill(target_blk, target_blk + npure1 * npure2inner, 0);
330 const size_t inner_blk_size = 8;
331 const size_t nblks = (inner_dim + inner_blk_size - 1) / inner_blk_size;
332 for (
size_t blk = 0; blk != nblks; ++blk) {
333 const auto blk_begin = blk * inner_blk_size;
334 const auto blk_end = std::min(blk_begin + inner_blk_size, inner_dim);
335 const auto blk_size = blk_end - blk_begin;
338 for (
size_t s1 = 0; s1 != npure1; ++s1) {
341 const auto* c1_idxs =
343 const auto* c1_vals = coefs1.row_values(
346 auto target_blk_s1 = target_blk + s1 * npure2inner + blk_begin;
349 for (
size_t s2 = 0; s2 != npure2; ++s2) {
352 const auto* c2_idxs =
354 const auto* c2_vals = coefs2.row_values(
356 const auto s2inner = s2 * inner_dim;
357 const auto target_blk_s1_blk_begin = target_blk_s1 + s2inner;
359 for (
size_t ic1 = 0; ic1 != nc1;
361 auto c1 = c1_idxs[ic1];
362 auto s1_c1_coeff = c1_vals[ic1];
364 auto source_blk_c1 = source_blk + c1 * ncart2inner + blk_begin;
366 for (
size_t ic2 = 0; ic2 != nc2;
368 auto c2 = c2_idxs[ic2];
369 auto s2_c2_coeff = c2_vals[ic2];
370 const auto c2inner = c2 * inner_dim;
372 const auto coeff = s1_c1_coeff * s2_c2_coeff;
373 const auto source_blk_c1_blk_begin = source_blk_c1 + c2inner;
374 for (
auto b = 0; b < blk_size; ++b)
375 target_blk_s1_blk_begin[b] += source_blk_c1_blk_begin[b] * coeff;
389template <
typename Real>
390void transform_inner(
size_t n1,
size_t l,
size_t n2,
const Real* src,
392 const auto& coefs = SolidHarmonicsCoefficients<Real>::instance(l);
394 const auto nc = (l + 1) * (l + 2) / 2;
395 const auto n = 2 * l + 1;
396 const auto nc_n2 = nc * n2;
397 const auto n_n2 = n * n2;
398 std::fill(tgt, tgt + n1 * n_n2, 0);
401 for (
size_t s = 0; s != n; ++s) {
402 const auto nc_s = coefs.nnz(s);
405 const auto* c_vals = coefs.row_values(
408 const auto tgt_blk_s_offset = s * n2;
410 for (
size_t ic = 0; ic != nc_s;
412 const auto c = c_idxs[ic];
413 const auto s_c_coeff = c_vals[ic];
415 auto src_blk_s = src + c * n2;
416 auto tgt_blk_s = tgt + tgt_blk_s_offset;
419 for (
size_t i1 = 0; i1 != n1;
420 ++i1, src_blk_s += nc_n2, tgt_blk_s += n_n2) {
421 for (
size_t i2 = 0; i2 != n2; ++i2) {
422 tgt_blk_s[i2] += s_c_coeff * src_blk_s[i2];
431template <
typename Real>
432void transform_last(
size_t n1,
size_t l,
const Real* src, Real* tgt) {
433 const auto& coefs = SolidHarmonicsCoefficients<Real>::instance(l);
435 const auto nc = (l + 1) * (l + 2) / 2;
436 const auto n = 2 * l + 1;
437 std::fill(tgt, tgt + n1 * n, 0);
440 for (
size_t s = 0; s != n; ++s) {
441 const auto nc_s = coefs.nnz(s);
444 const auto* c_vals = coefs.row_values(
447 const auto tgt_blk_s_offset = s;
449 for (
size_t ic = 0; ic != nc_s;
451 const auto c = c_idxs[ic];
452 const auto s_c_coeff = c_vals[ic];
454 auto src_blk_s = src + c;
455 auto tgt_blk_s = tgt + tgt_blk_s_offset;
458 for (
size_t i1 = 0; i1 != n1; ++i1, src_blk_s += nc, tgt_blk_s += n) {
459 *tgt_blk_s += s_c_coeff * *src_blk_s;
467template <
typename Real>
468void tform_last2(
size_t n1,
int l_row,
int l_col,
const Real* source_blk,
470 const auto& coefs_row = SolidHarmonicsCoefficients<Real>::instance(l_row);
471 const auto& coefs_col = SolidHarmonicsCoefficients<Real>::instance(l_col);
473 const auto ncart_row = (l_row + 1) * (l_row + 2) / 2;
474 const auto ncart_col = (l_col + 1) * (l_col + 2) / 2;
475 const auto ncart = ncart_row * ncart_col;
476 const auto npure_row = 2 * l_row + 1;
477 const auto npure_col = 2 * l_col + 1;
478 const auto npure = npure_row * npure_col;
479 std::fill(target_blk, target_blk + n1 * npure, 0);
481 for (
size_t i1 = 0; i1 != n1;
482 ++i1, source_blk += ncart, target_blk += npure) {
484 for (
size_t s1 = 0; s1 != npure_row; ++s1) {
487 const auto* c1_idxs = coefs_row.row_idx(
489 const auto* c1_vals = coefs_row.row_values(
492 auto target_blk_s1 = target_blk + s1 * npure_col;
495 for (
size_t s2 = 0; s2 != npure_col; ++s2) {
498 const auto* c2_idxs = coefs_col.row_idx(
500 const auto* c2_vals = coefs_col.row_values(
503 for (
size_t ic1 = 0; ic1 != nc1;
505 auto c1 = c1_idxs[ic1];
506 auto s1_c1_coeff = c1_vals[ic1];
508 auto source_blk_c1 = source_blk + c1 * ncart_col;
510 for (
size_t ic2 = 0; ic2 != nc2;
512 auto c2 = c2_idxs[ic2];
513 auto s2_c2_coeff = c2_vals[ic2];
515 target_blk_s1[s2] += source_blk_c1[c2] * s1_c1_coeff * s2_c2_coeff;
528template <
typename Real>
529void tform(
int l_row,
int l_col,
const Real* source_blk, Real* target_blk) {
530 const auto& coefs_row = SolidHarmonicsCoefficients<Real>::instance(l_row);
531 const auto& coefs_col = SolidHarmonicsCoefficients<Real>::instance(l_col);
533 const auto ncart_col = (l_col + 1) * (l_col + 2) / 2;
534 const auto npure_row = 2 * l_row + 1;
535 const auto npure_col = 2 * l_col + 1;
536 std::fill(target_blk, target_blk + npure_row * npure_col, 0);
539 for (
auto s1 = 0; s1 != npure_row; ++s1) {
542 const auto* c1_idxs =
543 coefs_row.row_idx(s1);
544 const auto* c1_vals = coefs_row.row_values(
547 auto target_blk_s1 = target_blk + s1 * npure_col;
550 for (
auto s2 = 0; s2 != npure_col; ++s2) {
553 const auto* c2_idxs = coefs_col.row_idx(
555 const auto* c2_vals = coefs_col.row_values(
558 for (
size_t ic1 = 0; ic1 != nc1;
560 auto c1 = c1_idxs[ic1];
561 auto s1_c1_coeff = c1_vals[ic1];
563 auto source_blk_c1 = source_blk + c1 * ncart_col;
565 for (
size_t ic2 = 0; ic2 != nc2;
567 auto c2 = c2_idxs[ic2];
568 auto s2_c2_coeff = c2_vals[ic2];
570 target_blk_s1[s2] += source_blk_c1[c2] * s1_c1_coeff * s2_c2_coeff;
581template <
typename Real>
582void tform_cols(
size_t nrow,
int l_col,
const Real* source_blk,
584 return transform_last(nrow, l_col, source_blk, target_blk);
585 const auto& coefs_col = SolidHarmonicsCoefficients<Real>::instance(l_col);
587 const auto ncart_col = (l_col + 1) * (l_col + 2) / 2;
588 const auto npure_col = 2 * l_col + 1;
591 for (
auto r1 = 0ul; r1 != nrow; ++r1) {
592 auto source_blk_r1 = source_blk + r1 * ncart_col;
593 auto target_blk_r1 = target_blk + r1 * npure_col;
596 for (
auto s2 = 0; s2 != npure_col; ++s2) {
599 const auto* c2_idxs = coefs_col.row_idx(
601 const auto* c2_vals = coefs_col.row_values(
604 Real r1_s2_value = 0.0;
606 for (
size_t ic2 = 0; ic2 != nc2;
608 auto c2 = c2_idxs[ic2];
609 auto s2_c2_coeff = c2_vals[ic2];
611 r1_s2_value += source_blk_r1[c2] * s2_c2_coeff;
615 target_blk_r1[s2] = r1_s2_value;
624template <
typename Real>
625void tform_rows(
int l_row,
size_t ncol,
const Real* source_blk,
627 return transform_first(l_row, ncol, source_blk, target_blk);
628 const auto& coefs_row = SolidHarmonicsCoefficients<Real>::instance(l_row);
630 const auto npure_row = 2 * l_row + 1;
633 for (
auto s1 = 0; s1 != npure_row; ++s1) {
636 const auto* c1_idxs =
637 coefs_row.row_idx(s1);
638 const auto* c1_vals = coefs_row.row_values(
641 auto target_blk_s1 = target_blk + s1 * ncol;
644 for (
decltype(ncol) c2 = 0; c2 != ncol; ++c2) {
645 Real s1_c2_value = 0.0;
646 auto source_blk_c2_offset = source_blk + c2;
648 for (std::size_t ic1 = 0; ic1 != nc1;
650 auto c1 = c1_idxs[ic1];
651 auto s1_c1_coeff = c1_vals[ic1];
653 s1_c2_value += source_blk_c2_offset[c1 * ncol] * s1_c1_coeff;
657 target_blk_s1[c2] = s1_c2_value;
665template <
typename Real,
typename Shell>
666void tform(
const Shell& shell_row,
const Shell& shell_col,
667 const Real* source_blk, Real* target_blk) {
668 const auto trow = shell_row.pure;
669 const auto tcol = shell_col.pure;
673 Real localscratch[500];
674 tform_cols(shell_row.cartesian_size(), shell_col.l, source_blk,
676 tform_rows(shell_row.l, shell_col.size(), &localscratch[0], target_blk);
678 tform_rows(shell_row.l, shell_col.cartesian_size(), source_blk,
681 tform_cols(shell_row.cartesian_size(), shell_col.l, source_blk, target_blk);
Transformation coefficients from unnormalized Cartesian Gaussians (rows) to unit-normalized real Soli...
Definition solidharmonics.h:59
const Real * row_values(size_t r) const
returns ptr to row values
Definition solidharmonics.h:96
const unsigned int * row_idx(size_t r) const
returns ptr to row indices
Definition solidharmonics.h:100
unsigned int nnz(size_t r) const
number of nonzero elements in row r
Definition solidharmonics.h:104
static Real coeff(int l, int m, int lx, int ly, int lz)
Definition solidharmonics.h:114
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
SHGShellOrdering solid_harmonics_ordering()
Accessor for the SHGShellOrdering.
Definition initialize.h:122