51#define LIBINT2_CONSTEXPR_STATICS 0
53#pragma clang diagnostic push
54#pragma clang diagnostic ignored "-W#warnings"
55#elif defined(__GNUC__)
56#pragma GCC diagnostic push
57#pragma GCC diagnostic ignored "-Warray-bounds"
58#pragma GCC diagnostic ignored "-Wcpp"
64#pragma clang diagnostic pop
65#elif defined(__GNUC__)
66#pragma GCC diagnostic pop
74#if !defined(LIBINT_INCLUDE_ONEBODY) && defined(INCLUDE_ONEBODY)
75#define LIBINT_INCLUDE_ONEBODY INCLUDE_ONEBODY
77#if !defined(LIBINT_INCLUDE_ERI2) && defined(INCLUDE_ERI2)
78#define LIBINT_INCLUDE_ERI2 INCLUDE_ERI2
80#if !defined(LIBINT_INCLUDE_ERI) && defined(INCLUDE_ERI)
81#define LIBINT_INCLUDE_ERI INCLUDE_ERI
83#if !defined(LIBINT_INCLUDE_ERI3) && defined(INCLUDE_ERI3)
84#define LIBINT_INCLUDE_ERI3 INCLUDE_ERI3
95#if (LIBINT_MAJOR_VERSION < 2 || \
96 (LIBINT_MAJOR_VERSION == 2 && LIBINT_MINOR_VERSION < 9)) && \
97 defined(LIBINT2_CENTER_DEPENDENT_MAX_AM_3eri1) && \
98 defined(LIBINT2_MAX_AM_3eri1) && defined(LIBINT2_MAX_AM_default1)
99#if LIBINT2_CENTER_DEPENDENT_MAX_AM_3eri1 == 1 && \
100 LIBINT2_MAX_AM_3eri1 != LIBINT2_MAX_AM_default1
101#define VOTCA_LIBINT_ERI3_DERIV_BROKEN 1
104#ifndef VOTCA_LIBINT_ERI3_DERIV_BROKEN
105#define VOTCA_LIBINT_ERI3_DERIV_BROKEN 0
113#ifdef VOTCA_LIBINT_EXPECT_MAJOR
114#ifdef LIBINT_INCLUDE_ONEBODY
115#define VOTCA_LIBINT_HAVE_ONEBODY LIBINT_INCLUDE_ONEBODY
117#define VOTCA_LIBINT_HAVE_ONEBODY -1
119#ifdef LIBINT_INCLUDE_ERI
120#define VOTCA_LIBINT_HAVE_ERI LIBINT_INCLUDE_ERI
122#define VOTCA_LIBINT_HAVE_ERI -1
124#ifdef LIBINT_INCLUDE_ERI2
125#define VOTCA_LIBINT_HAVE_ERI2 LIBINT_INCLUDE_ERI2
127#define VOTCA_LIBINT_HAVE_ERI2 -1
129#ifdef LIBINT_INCLUDE_ERI3
130#define VOTCA_LIBINT_HAVE_ERI3 LIBINT_INCLUDE_ERI3
132#define VOTCA_LIBINT_HAVE_ERI3 -1
134#if LIBINT_MAJOR_VERSION != VOTCA_LIBINT_EXPECT_MAJOR || \
135 LIBINT_MINOR_VERSION != VOTCA_LIBINT_EXPECT_MINOR || \
136 LIBINT_MICRO_VERSION != VOTCA_LIBINT_EXPECT_MICRO || \
137 VOTCA_LIBINT_HAVE_ONEBODY != VOTCA_LIBINT_EXPECT_ONEBODY || \
138 VOTCA_LIBINT_HAVE_ERI != VOTCA_LIBINT_EXPECT_ERI || \
139 VOTCA_LIBINT_HAVE_ERI2 != VOTCA_LIBINT_EXPECT_ERI2 || \
140 VOTCA_LIBINT_HAVE_ERI3 != VOTCA_LIBINT_EXPECT_ERI3
142 "The libint2 headers found by the compiler are not those of the libint2 CMake configured (VOTCA_LIBINT_CONFIG_H in CMakeCache.txt). Another libint2 is earlier in the include path; remove it from there or uninstall it."
152std::vector<Eigen::MatrixXd>
ComputeAO3cBlock(
const libint2::Shell& auxshell,
153 const AOBasis& dftbasis,
154 libint2::Engine& engine);
167 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
174[[noreturn, maybe_unused]]
void ThrowNoDerivativeSupport(
175 const char* function_name,
const char* configure_flag) {
176 throw std::runtime_error(
177 std::string(function_name) +
178 ": this libint2 build was compiled without derivative integral "
179 "support for this operator category, so analytic nuclear forces "
180 "are unavailable. Rebuild libint2 with " +
182 " to use this feature. Many packaged libint2 builds (Homebrew, "
183 "Ubuntu apt, etc.) do not enable it; one-body, two-center Coulomb "
184 "metric and three-center RI integrals each have their own "
185 "configure option.");
197template <
class Fn, std::
size_t N1, std::
size_t N2>
198bool BuildTableCovers(Fn (&table)[N1][N2],
Index lmax1,
Index lmax2) {
199 if (lmax1 >=
Index(N1) || lmax2 >=
Index(N2)) {
202 for (
Index l1 = 0; l1 <= lmax1; ++l1) {
203 for (
Index l2 = 0; l2 <= lmax2; ++l2) {
204 if (table[l1][l2] ==
nullptr) {
212template <
class Fn, std::
size_t NP, std::
size_t N1, std::
size_t N2>
213bool BuildTableCovers(Fn (&table)[NP][N1][N2],
Index lmaxP,
Index lmax) {
217 for (
Index lP = 0; lP <= lmaxP; ++lP) {
218 for (
Index l1 = 0; l1 <= lmax; ++l1) {
219 for (
Index l2 = 0; l2 <= l1; ++l2) {
220 if (table[lP][l1][l2] ==
nullptr && table[lP][l2][l1] ==
nullptr) {
229[[maybe_unused]]
void CheckDerivativeTable(
bool covered,
230 const char* function_name,
233 throw std::runtime_error(
234 std::string(function_name) +
235 ": this libint2 build has no first-derivative integrals for the "
236 "angular momenta of this basis (maximum l = " +
237 std::to_string(lmax) +
238 "), so analytic nuclear forces are unavailable. Rebuild libint2 "
239 "with a higher derivative angular-momentum limit (the second "
240 "entry of --with-max-am / --with-eri2-max-am / --with-eri3-max-am).");
261#if defined(LIBINT2_MAX_DERIV_ORDER) && LIBINT2_MAX_DERIV_ORDER >= 1 && \
262 (LIBINT_INCLUDE_ONEBODY >= 1)
263template <lib
int2::Operator obtype>
264std::vector<AOMatrixDerivative> computeOneBodyIntegralDerivatives(
265 const AOBasis& aobasis) {
268 libint2::operator_traits<obtype>::nopers == 1,
269 "computeOneBodyIntegralDerivatives only supports single-operator "
270 "types (overlap, kinetic); multipole-type operators with multiple "
271 "result matrices are not handled.");
273 Index lmax = aobasis.getMaxL();
274 if constexpr (obtype == libint2::Operator::overlap) {
275 CheckDerivativeTable(
276 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_overlap1), lmax,
278 "ComputeOverlapDerivatives", lmax);
280 CheckDerivativeTable(
281 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_kinetic1), lmax,
283 "ComputeKineticDerivatives", lmax);
288 Index natoms =
static_cast<Index>(aobasis.getFuncPerAtom().size());
290 std::vector<libint2::Shell> shells = aobasis.GenerateLibintBasis();
291 std::vector<std::vector<Index>> shellpair_list = aobasis.ComputeShellPairs();
292 std::vector<Index> shell2bf = aobasis.getMapToBasisFunctions();
295 std::vector<Index> shell2atom;
296 shell2atom.reserve(shells.size());
297 for (
Index s = 0; s < aobasis.getNumofShells(); ++s) {
298 shell2atom.push_back(aobasis.getShell(s).getAtomIndex());
301 Index nbf = aobasis.AOBasisSize();
302 std::vector<AOMatrixDerivative> result(natoms);
303 for (
Index a = 0; a < natoms; ++a) {
304 for (
Index xyz = 0; xyz < 3; ++xyz) {
305 result[a][xyz] = Eigen::MatrixXd::Zero(nbf, nbf);
309 std::vector<libint2::Engine> engines(nthreads);
311 engines[0] = libint2::Engine(obtype, aobasis.getMaxNprim(),
312 static_cast<int>(aobasis.getMaxL()), 1);
313 for (
Index i = 1; i < nthreads; ++i) {
314 engines[i] = engines[0];
317 std::exception_ptr eptr =
nullptr;
318 std::atomic<bool> any_nonnull_buffer{
false};
319#pragma omp parallel for schedule(dynamic)
320 for (
Index s1 = 0; s1 < aobasis.getNumofShells(); ++s1) {
323 libint2::Engine& engine = engines[thread_id];
324 const libint2::Engine::target_ptr_vec& buf = engine.results();
326 Index bf1 = shell2bf[s1];
327 Index n1 = shells[s1].size();
328 Index atom1 = shell2atom[s1];
330 for (
Index s2 : shellpair_list[s1]) {
332 engine.compute(shells[s1], shells[s2]);
336 if (buf[0] ==
nullptr || buf[3] ==
nullptr) {
340 any_nonnull_buffer.store(
true, std::memory_order_relaxed);
342 Index bf2 = shell2bf[s2];
343 Index n2 = shells[s2].size();
344 Index atom2 = shell2atom[s2];
346 for (
Index xyz = 0; xyz < 3; ++xyz) {
347 Eigen::Map<const MatrixLibInt> buf_mat1(buf[xyz], n1, n2);
348 result[atom1][xyz].block(bf1, bf2, n1, n2) += buf_mat1;
350 result[atom1][xyz].block(bf2, bf1, n2, n1) += buf_mat1.transpose();
353 Eigen::Map<const MatrixLibInt> buf_mat2(buf[3 + xyz], n1, n2);
354 result[atom2][xyz].block(bf1, bf2, n1, n2) += buf_mat2;
356 result[atom2][xyz].block(bf2, bf1, n2, n1) += buf_mat2.transpose();
366 eptr = std::current_exception();
372 std::rethrow_exception(eptr);
374 if (!any_nonnull_buffer.load() && aobasis.getNumofShells() > 0) {
377 throw std::runtime_error(
378 "computeOneBodyIntegralDerivatives: engine.results() returned a "
379 "null buffer for every shell pair; this libint2 build does not "
380 "support this operator's derivative integrals at runtime, even "
381 "though it may report LIBINT2_MAX_DERIV_ORDER >= 1 for other "
382 "operators. Rebuild libint2 with --enable-1body=1 to use this "
390 const AOBasis& aobasis) {
391 return computeOneBodyIntegralDerivatives<libint2::Operator::overlap>(aobasis);
395 const AOBasis& aobasis) {
396 return computeOneBodyIntegralDerivatives<libint2::Operator::kinetic>(aobasis);
402 ThrowNoDerivativeSupport(
"ComputeOverlapDerivatives",
"--enable-1body=1");
405 ThrowNoDerivativeSupport(
"ComputeKineticDerivatives",
"--enable-1body=1");
419#if (LIBINT_INCLUDE_ERI2 >= 1)
421 const AOBasis& aobasis) {
422 CheckDerivativeTable(
423 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_2eri1),
424 aobasis.getMaxL(), aobasis.getMaxL()),
425 "ComputeCoulombMetricDerivatives", aobasis.getMaxL());
426 Index natoms =
static_cast<Index>(aobasis.getFuncPerAtom().size());
428 std::vector<libint2::Shell> shells = aobasis.GenerateLibintBasis();
429 std::vector<Index> shell2bf = aobasis.getMapToBasisFunctions();
431 std::vector<Index> shell2atom;
432 shell2atom.reserve(aobasis.getNumofShells());
433 for (
Index s = 0; s < aobasis.getNumofShells(); ++s) {
434 shell2atom.push_back(aobasis.getShell(s).getAtomIndex());
437 Index nbf = aobasis.AOBasisSize();
438 std::vector<AOMatrixDerivative> result(natoms);
439 for (
Index a = 0; a < natoms; ++a) {
440 for (
Index xyz = 0; xyz < 3; ++xyz) {
441 result[a][xyz] = Eigen::MatrixXd::Zero(nbf, nbf);
445 std::vector<libint2::Engine> engines(nthreads);
447 libint2::Engine(libint2::Operator::coulomb, aobasis.getMaxNprim(),
448 static_cast<int>(aobasis.getMaxL()), 1);
449 engines[0].set(libint2::BraKet::xs_xs);
450 for (
Index i = 1; i < nthreads; ++i) {
451 engines[i] = engines[0];
454 std::exception_ptr eptr_coulmetric =
nullptr;
455 std::atomic<bool> any_nonnull_buffer_coulmetric{
false};
456#pragma omp parallel for schedule(dynamic)
457 for (
Index s1 = 0; s1 < aobasis.getNumofShells(); ++s1) {
460 const libint2::Engine::target_ptr_vec& buf = engine.results();
462 Index bf1 = shell2bf[s1];
463 Index n1 = shells[s1].size();
464 Index atom1 = shell2atom[s1];
469 for (
Index s2 = 0; s2 <= s1; ++s2) {
470 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xs, 1>(
471 shells[s1], libint2::Shell::unit(), shells[s2],
472 libint2::Shell::unit());
475 if (buf[0] ==
nullptr || buf[3] ==
nullptr) {
478 any_nonnull_buffer_coulmetric.store(
true, std::memory_order_relaxed);
480 Index bf2 = shell2bf[s2];
481 Index n2 = shells[s2].size();
482 Index atom2 = shell2atom[s2];
485 Eigen::Map<const MatrixLibInt> buf_mat1(buf[xyz], n1, n2);
486 result[atom1][
xyz].block(bf1, bf2, n1, n2) += buf_mat1;
488 result[atom1][
xyz].block(bf2, bf1, n2, n1) += buf_mat1.transpose();
491 Eigen::Map<const MatrixLibInt> buf_mat2(buf[3 + xyz], n1, n2);
492 result[atom2][
xyz].block(bf1, bf2, n1, n2) += buf_mat2;
494 result[atom2][
xyz].block(bf2, bf1, n2, n1) += buf_mat2.transpose();
501 if (!eptr_coulmetric) {
502 eptr_coulmetric = std::current_exception();
507 if (eptr_coulmetric) {
508 std::rethrow_exception(eptr_coulmetric);
510 if (!any_nonnull_buffer_coulmetric.load() && aobasis.getNumofShells() > 0) {
511 throw std::runtime_error(
512 "ComputeCoulombMetricDerivatives: engine.results() returned a "
513 "null buffer for every shell pair; this libint2 build does not "
514 "support this operator's derivative integrals at runtime, even "
515 "though it may report LIBINT2_MAX_DERIV_ORDER >= 1 for other "
516 "operators. Rebuild libint2 with --enable-eri2=1 to use this "
524 ThrowNoDerivativeSupport(
"ComputeCoulombMetricDerivatives",
533#if (LIBINT_INCLUDE_ERI2 >= 1)
535 const Eigen::MatrixXd&
G) {
536 CheckDerivativeTable(
537 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_2eri1),
538 auxbasis.getMaxL(), auxbasis.getMaxL()),
539 "ContractCoulombMetricDerivatives", auxbasis.getMaxL());
540 Index natoms =
static_cast<Index>(auxbasis.getFuncPerAtom().size());
542 std::vector<libint2::Shell> shells = auxbasis.GenerateLibintBasis();
543 std::vector<Index> shell2bf = auxbasis.getMapToBasisFunctions();
545 std::vector<Index> shell2atom;
546 shell2atom.reserve(auxbasis.getNumofShells());
547 for (
Index s = 0; s < auxbasis.getNumofShells(); ++s) {
548 shell2atom.push_back(auxbasis.getShell(s).getAtomIndex());
551 std::vector<Eigen::MatrixXd> partial(nthreads,
552 Eigen::MatrixXd::Zero(natoms, 3));
554 std::vector<libint2::Engine> engines(nthreads);
556 libint2::Engine(libint2::Operator::coulomb, auxbasis.getMaxNprim(),
557 static_cast<int>(auxbasis.getMaxL()), 1);
558 engines[0].set(libint2::BraKet::xs_xs);
559 for (
Index i = 1; i < nthreads; ++i) {
560 engines[i] = engines[0];
563 std::exception_ptr eptr_coulmetric =
nullptr;
564 std::atomic<bool> any_nonnull_buffer_coulmetric{
false};
565#pragma omp parallel for schedule(dynamic)
566 for (
Index s1 = 0; s1 < auxbasis.getNumofShells(); ++s1) {
569 libint2::Engine& engine = engines[thread];
570 Eigen::MatrixXd& acc = partial[thread];
571 const libint2::Engine::target_ptr_vec& buf = engine.results();
573 Index bf1 = shell2bf[s1];
574 Index n1 = shells[s1].size();
575 Index atom1 = shell2atom[s1];
577 for (
Index s2 = 0; s2 <= s1; ++s2) {
578 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xs, 1>(
579 shells[s1], libint2::Shell::unit(), shells[s2],
580 libint2::Shell::unit());
582 if (buf[0] ==
nullptr || buf[3] ==
nullptr) {
585 any_nonnull_buffer_coulmetric.store(
true, std::memory_order_relaxed);
587 Index bf2 = shell2bf[s2];
588 Index n2 = shells[s2].size();
589 Index atom2 = shell2atom[s2];
590 const double factor = (s1 == s2) ? 1.0 : 2.0;
591 const auto Gblock =
G.block(bf1, bf2, n1, n2);
594 Eigen::Map<const MatrixLibInt> buf_mat1(buf[xyz], n1, n2);
595 Eigen::Map<const MatrixLibInt> buf_mat2(buf[3 + xyz], n1, n2);
596 acc(atom1, xyz) += factor * (Gblock.array() * buf_mat1.array()).sum();
597 acc(atom2, xyz) += factor * (Gblock.array() * buf_mat2.array()).sum();
603 if (!eptr_coulmetric) {
604 eptr_coulmetric = std::current_exception();
609 if (eptr_coulmetric) {
610 std::rethrow_exception(eptr_coulmetric);
612 if (!any_nonnull_buffer_coulmetric.load() && auxbasis.getNumofShells() > 0) {
613 throw std::runtime_error(
614 "ContractCoulombMetricDerivatives: engine.results() returned a "
615 "null buffer for every shell pair; this libint2 build does not "
616 "support this operator's derivative integrals at runtime. Rebuild "
617 "libint2 with --enable-eri2=1 to use this feature.");
619 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
620 for (
const Eigen::MatrixXd& p : partial) {
627 const Eigen::MatrixXd&) {
628 ThrowNoDerivativeSupport(
"ContractCoulombMetricDerivatives",
652#if (LIBINT_INCLUDE_ERI3 >= 1) && !VOTCA_LIBINT_ERI3_DERIV_BROKEN
655 CheckDerivativeTable(
656 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_3eri1),
658 "ComputeThreeCenterDerivatives",
669 std::vector<Index> dftshell2atom;
674 std::vector<Index> auxshell2atom;
683 std::vector<ThreeCenterDerivative> result(natoms);
684 for (
Index a = 0; a < natoms; ++a) {
685 for (
Index xyz = 0; xyz < 3; ++xyz) {
686 result[a][xyz] = std::vector<Eigen::MatrixXd>(
687 n_aux_bf, Eigen::MatrixXd::Zero(n_dft_bf, n_dft_bf));
691 std::vector<libint2::Engine> engines(nthreads);
692 engines[0] = libint2::Engine(
693 libint2::Operator::coulomb,
695 static_cast<int>(std::max(dftbasis.
getMaxL(), auxbasis.
getMaxL())), 1);
696 engines[0].set(libint2::BraKet::xs_xx);
697 for (
Index i = 1; i < nthreads; ++i) {
698 engines[i] = engines[0];
701 std::exception_ptr eptr_3c =
nullptr;
702 std::atomic<bool> any_nonnull_buffer_3c{
false};
703#pragma omp parallel for schedule(dynamic)
707 const libint2::Engine::target_ptr_vec& buf = engine.results();
709 const libint2::Shell& auxshell = auxshells[aux];
710 Index aux_start = auxshell2bf[aux];
711 Index atom_aux = auxshell2atom[aux];
714 const libint2::Shell& shell_row = dftshells[row];
715 Index row_start = shell2bf[row];
716 Index atom_row = dftshell2atom[row];
721 const libint2::Shell& shell_col = dftshells[col];
722 Index col_start = shell2bf[col];
723 Index atom_col = dftshell2atom[col];
726 .compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xx, 1>(
727 auxshell, libint2::Shell::unit(), shell_col, shell_row);
730 if (buf[0] ==
nullptr || buf[3] ==
nullptr || buf[6] ==
nullptr) {
733 any_nonnull_buffer_3c.store(
true, std::memory_order_relaxed);
737 for (
Index xyz = 0; xyz < 3; ++xyz) {
739 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
740 result_aux(buf[xyz], auxshell.size(), shell_col.size(),
743 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
744 result_col(buf[3 + xyz], auxshell.size(), shell_col.size(),
747 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
748 result_row(buf[6 + xyz], auxshell.size(), shell_col.size(),
751 for (
size_t aux_c = 0; aux_c < auxshell.size(); ++aux_c) {
752 Index global_aux = aux_start +
static_cast<Index>(aux_c);
753 for (
size_t col_c = 0; col_c < shell_col.size(); ++col_c) {
754 for (
size_t row_c = 0; row_c < shell_row.size(); ++row_c) {
755 double val_aux = result_aux(aux_c, col_c, row_c);
756 double val_col = result_col(aux_c, col_c, row_c);
757 double val_row = result_row(aux_c, col_c, row_c);
758 Index r = row_start +
static_cast<Index>(row_c);
759 Index c = col_start +
static_cast<Index>(col_c);
760 result[atom_aux][xyz][global_aux](r, c) += val_aux;
761 result[atom_col][xyz][global_aux](r, c) += val_col;
762 result[atom_row][xyz][global_aux](r, c) += val_row;
773 eptr_3c = std::current_exception();
779 std::rethrow_exception(eptr_3c);
781 if (!any_nonnull_buffer_3c.load() && auxbasis.
getNumofShells() > 0 &&
783 throw std::runtime_error(
784 "ComputeThreeCenterDerivatives: engine.results() returned a null "
785 "buffer for every shell triple; this libint2 build does not "
786 "support this operator's derivative integrals at runtime, even "
787 "though it may report LIBINT2_MAX_DERIV_ORDER >= 1 for other "
788 "operators. Rebuild libint2 with --enable-eri3=1 to use this "
796 ThrowNoDerivativeSupport(
797 "ComputeThreeCenterDerivatives",
799 ?
"--enable-eri3=1 (libint2 >= 2.9.0: older releases index "
800 "centre-dependent 3-centre derivative code incorrectly)"
801 :
"--enable-eri3=1");
805#if (LIBINT_INCLUDE_ERI3 >= 1) && !VOTCA_LIBINT_ERI3_DERIV_BROKEN
814 const AOBasis& dftbasis,
816 CheckDerivativeTable(
817 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_3eri1),
818 auxbasis.getMaxL(), dftbasis.getMaxL()),
819 "ContractThreeCenterDerivatives",
820 std::max(auxbasis.getMaxL(), dftbasis.getMaxL()));
821 Index natoms =
static_cast<Index>(dftbasis.getFuncPerAtom().size());
823 std::vector<libint2::Shell> dftshells = dftbasis.GenerateLibintBasis();
824 std::vector<libint2::Shell> auxshells = auxbasis.GenerateLibintBasis();
825 std::vector<Index> shell2bf = dftbasis.getMapToBasisFunctions();
826 std::vector<Index> auxshell2bf = auxbasis.getMapToBasisFunctions();
828 std::vector<Index> dftshell2atom;
829 dftshell2atom.reserve(dftbasis.getNumofShells());
830 for (
Index s = 0; s < dftbasis.getNumofShells(); ++s) {
831 dftshell2atom.push_back(dftbasis.getShell(s).getAtomIndex());
833 std::vector<Index> auxshell2atom;
834 auxshell2atom.reserve(auxbasis.getNumofShells());
835 for (
Index s = 0; s < auxbasis.getNumofShells(); ++s) {
836 auxshell2atom.push_back(auxbasis.getShell(s).getAtomIndex());
839 std::vector<Eigen::MatrixXd> partial(nthreads,
840 Eigen::MatrixXd::Zero(natoms, 3));
842 std::vector<libint2::Engine> engines(nthreads);
843 engines[0] = libint2::Engine(
844 libint2::Operator::coulomb,
845 std::max(dftbasis.getMaxNprim(), auxbasis.getMaxNprim()),
846 static_cast<int>(std::max(dftbasis.getMaxL(), auxbasis.getMaxL())), 1);
847 engines[0].set(libint2::BraKet::xs_xx);
848 for (
Index i = 1; i < nthreads; ++i) {
849 engines[i] = engines[0];
852 std::exception_ptr eptr_3c =
nullptr;
853 std::atomic<bool> any_nonnull_buffer_3c{
false};
854#pragma omp parallel for schedule(dynamic)
855 for (
Index aux = 0; aux < auxbasis.getNumofShells(); ++aux) {
858 libint2::Engine& engine = engines[thread];
859 Eigen::MatrixXd& acc = partial[thread];
860 const libint2::Engine::target_ptr_vec& buf = engine.results();
862 const libint2::Shell& auxshell = auxshells[aux];
863 Index aux_start = auxshell2bf[aux];
864 Index atom_aux = auxshell2atom[aux];
865 Index naux_c =
static_cast<Index>(auxshell.size());
867 std::vector<Eigen::MatrixXd> gam(naux_c);
868 for (
Index f = 0; f < naux_c; ++f) {
869 gamma(aux_start + f, gam[f]);
870 gam[f] = 0.5 * (gam[f] + gam[f].transpose()).eval();
873 for (
Index row = 0; row < dftbasis.getNumofShells(); ++row) {
874 const libint2::Shell& shell_row = dftshells[row];
875 Index row_start = shell2bf[row];
876 Index atom_row = dftshell2atom[row];
878 for (
Index col = 0; col <= row; ++col) {
879 const libint2::Shell& shell_col = dftshells[col];
880 Index col_start = shell2bf[col];
881 Index atom_col = dftshell2atom[col];
884 .compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xx, 1>(
885 auxshell, libint2::Shell::unit(), shell_col, shell_row);
887 if (buf[0] ==
nullptr || buf[3] ==
nullptr || buf[6] ==
nullptr) {
890 any_nonnull_buffer_3c.store(
true, std::memory_order_relaxed);
892 const double factor = (row == col) ? 1.0 : 2.0;
895 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
896 result_aux(buf[xyz], auxshell.size(), shell_col.size(),
899 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
900 result_col(buf[3 + xyz], auxshell.size(), shell_col.size(),
903 Eigen::Tensor<const double, 3, Eigen::RowMajor>
const>
904 result_row(buf[6 + xyz], auxshell.size(), shell_col.size(),
910 for (
size_t aux_c = 0; aux_c < auxshell.size(); ++aux_c) {
911 const Eigen::MatrixXd& g = gam[aux_c];
912 for (
size_t col_c = 0; col_c < shell_col.size(); ++col_c) {
913 Index c = col_start +
static_cast<Index>(col_c);
914 for (
size_t row_c = 0; row_c < shell_row.size(); ++row_c) {
915 Index r = row_start +
static_cast<Index>(row_c);
916 double g_rc = g(r, c);
917 s_aux += g_rc * result_aux(aux_c, col_c, row_c);
918 s_col += g_rc * result_col(aux_c, col_c, row_c);
919 s_row += g_rc * result_row(aux_c, col_c, row_c);
923 acc(atom_aux, xyz) += factor * s_aux;
924 acc(atom_col, xyz) += factor * s_col;
925 acc(atom_row, xyz) += factor * s_row;
933 eptr_3c = std::current_exception();
939 std::rethrow_exception(eptr_3c);
941 if (!any_nonnull_buffer_3c.load() && auxbasis.getNumofShells() > 0 &&
942 dftbasis.getNumofShells() > 0) {
943 throw std::runtime_error(
944 "ContractThreeCenterDerivatives: engine.results() returned a null "
945 "buffer for every shell triple; this libint2 build does not "
946 "support this operator's derivative integrals at runtime, even "
947 "though it may report LIBINT2_MAX_DERIV_ORDER >= 1 for other "
948 "operators. Rebuild libint2 with --enable-eri3=1 to use this "
951 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
952 for (
const Eigen::MatrixXd& p : partial) {
960 ThrowNoDerivativeSupport(
961 "ContractThreeCenterDerivatives",
963 ?
"--enable-eri3=1 (libint2 >= 2.9.0: older releases index "
964 "centre-dependent 3-centre derivative code incorrectly)"
965 :
"--enable-eri3=1");
978 std::vector<Eigen::MatrixXd> tensor(
982 libint2::Engine engine(
983 libint2::Operator::coulomb,
985 static_cast<int>(std::max(dftbasis.
getMaxL(), auxbasis.
getMaxL())), 0);
986 engine.set(libint2::BraKet::xs_xx);
989 std::vector<Eigen::MatrixXd> block =
991 Index aux_start = auxshell2bf[aux];
992 for (
size_t i = 0; i < block.size(); ++i) {
993 tensor[aux_start +
static_cast<Index>(i)] = block[i];
1010 std::vector<libint2::Engine> engines(nthreads);
1011 engines[0] = libint2::Engine(
1012 libint2::Operator::coulomb,
1014 static_cast<int>(std::max(dftbasis.
getMaxL(), auxbasis.
getMaxL())), 0);
1015 engines[0].set(libint2::BraKet::xs_xx);
1016 for (
Index i = 1; i < nthreads; ++i) {
1017 engines[i] = engines[0];
1020 std::exception_ptr eptr =
nullptr;
1021#pragma omp parallel for schedule(dynamic)
1025 std::vector<Eigen::MatrixXd> block =
1027 Index aux_start = auxshell2bf[aux];
1028 for (
size_t i = 0; i < block.size(); ++i) {
1029 use(aux_start +
static_cast<Index>(i), block[i]);
1035 eptr = std::current_exception();
1041 std::rethrow_exception(eptr);
1058#if defined(LIBINT2_MAX_DERIV_ORDER) && LIBINT2_MAX_DERIV_ORDER >= 1 && \
1059 (LIBINT_INCLUDE_ONEBODY >= 1)
1061 const AOBasis& aobasis,
const QMMolecule& mol) {
1062 CheckDerivativeTable(
1063 BuildTableCovers(LIBINT2_PREFIXED_NAME(libint2_build_elecpot1),
1064 aobasis.getMaxL(), aobasis.getMaxL()),
1065 "ComputeNuclearAttractionDerivatives", aobasis.getMaxL());
1066 Index natoms = mol.size();
1068 std::vector<libint2::Shell> shells = aobasis.GenerateLibintBasis();
1069 std::vector<Index> shell2bf = aobasis.getMapToBasisFunctions();
1071 std::vector<Index> shell2atom;
1072 shell2atom.reserve(aobasis.getNumofShells());
1073 for (
Index s = 0; s < aobasis.getNumofShells(); ++s) {
1074 shell2atom.push_back(aobasis.getShell(s).getAtomIndex());
1077 Index nbf = aobasis.AOBasisSize();
1078 std::vector<std::vector<AOMatrixDerivative>> result_thread(nthreads);
1079 for (
Index t = 0; t < nthreads; ++t) {
1080 result_thread[t].resize(natoms);
1081 for (
Index a = 0; a < natoms; ++a) {
1082 for (
Index xyz = 0; xyz < 3; ++xyz) {
1083 result_thread[t][a][xyz] = Eigen::MatrixXd::Zero(nbf, nbf);
1088 std::vector<libint2::Engine> engines(nthreads);
1089 for (
Index i = 0; i < nthreads; ++i) {
1091 libint2::Engine(libint2::Operator::nuclear, aobasis.getMaxNprim(),
1092 static_cast<int>(aobasis.getMaxL()), 1);
1097 std::exception_ptr eptr_nucattr =
nullptr;
1098 std::atomic<bool> any_nonnull_buffer_nucattr{
false};
1099#pragma omp parallel for schedule(dynamic)
1100 for (
Index A = 0; A < natoms; ++A) {
1103 libint2::Engine& engine = engines[thread_id];
1105 std::vector<libint2::Atom> single_atom(1);
1106 single_atom[0].atomic_number =
static_cast<int>(mol[A].getNuccharge());
1107 single_atom[0].x = mol[A].getPos().x();
1108 single_atom[0].y = mol[A].getPos().y();
1109 single_atom[0].z = mol[A].getPos().z();
1110 engine.set_params(libint2::make_point_charges(single_atom));
1112 const libint2::Engine::target_ptr_vec& buf = engine.results();
1114 for (
Index s1 = 0; s1 < aobasis.getNumofShells(); ++s1) {
1115 Index bf1 = shell2bf[s1];
1116 Index n1 = shells[s1].size();
1117 Index atom1 = shell2atom[s1];
1119 for (
Index s2 = 0; s2 <= s1; ++s2) {
1120 engine.compute(shells[s1], shells[s2]);
1125 if (buf[0] ==
nullptr || buf[3] ==
nullptr || buf[6] ==
nullptr) {
1128 any_nonnull_buffer_nucattr.store(
true, std::memory_order_relaxed);
1130 Index bf2 = shell2bf[s2];
1131 Index n2 = shells[s2].size();
1132 Index atom2 = shell2atom[s2];
1136 Eigen::Map<const MatrixLibInt> buf_mat1(buf[xyz], n1, n2);
1137 result_thread[thread_id][atom1][
xyz].block(bf1, bf2, n1, n2) +=
1140 result_thread[thread_id][atom1][
xyz].block(bf2, bf1, n2, n1) +=
1141 buf_mat1.transpose();
1144 Eigen::Map<const MatrixLibInt> buf_mat2(buf[3 + xyz], n1, n2);
1145 result_thread[thread_id][atom2][
xyz].block(bf1, bf2, n1, n2) +=
1148 result_thread[thread_id][atom2][
xyz].block(bf2, bf1, n2, n1) +=
1149 buf_mat2.transpose();
1152 Eigen::Map<const MatrixLibInt> buf_mat3(buf[6 + xyz], n1, n2);
1153 result_thread[thread_id][A][
xyz].block(bf1, bf2, n1, n2) +=
1156 result_thread[thread_id][A][
xyz].block(bf2, bf1, n2, n1) +=
1157 buf_mat3.transpose();
1165 if (!eptr_nucattr) {
1166 eptr_nucattr = std::current_exception();
1172 std::rethrow_exception(eptr_nucattr);
1174 if (!any_nonnull_buffer_nucattr.load() && natoms > 0 &&
1175 aobasis.getNumofShells() > 0) {
1178 throw std::runtime_error(
1179 "ComputeNuclearAttractionDerivatives: engine.results() returned "
1180 "a null buffer for every shell pair and point charge; this "
1181 "libint2 build does not support nuclear-attraction derivative "
1182 "integrals at runtime, even though it may report "
1183 "LIBINT2_MAX_DERIV_ORDER >= 1 for other operators. Rebuild "
1184 "libint2 with --enable-1body=1 to use this feature.");
1190 double total_norm_sq_nucattr = 0.0;
1191 for (
Index a = 0; a < natoms; ++a) {
1193 for (
Index t = 0; t < nthreads; ++t) {
1194 total_norm_sq_nucattr += result_thread[t][a][
xyz].squaredNorm();
1198 if (total_norm_sq_nucattr < 1.e-20 && natoms > 0 &&
1199 aobasis.getNumofShells() > 0) {
1200 throw std::runtime_error(
1201 "ComputeNuclearAttractionDerivatives: the assembled result is "
1202 "exactly zero everywhere, which is impossible for a real "
1203 "molecule; this libint2 build likely returns zero-filled buffers "
1204 "for this operator. Rebuild libint2 with --enable-1body=1 to use "
1208 std::vector<AOMatrixDerivative> result(natoms);
1209 for (
Index a = 0; a < natoms; ++a) {
1211 result[a][
xyz] = Eigen::MatrixXd::Zero(nbf, nbf);
1212 for (
Index t = 0; t < nthreads; ++t) {
1213 result[a][
xyz] += result_thread[t][a][
xyz];
1222 ThrowNoDerivativeSupport(
"ComputeNuclearAttractionDerivatives",
1223 "--enable-1body=1");
1232#if defined(LIBINT2_MAX_DERIV_ORDER) && LIBINT2_MAX_DERIV_ORDER >= 1 && \
1233 (LIBINT_INCLUDE_ONEBODY >= 1) && (LIBINT_INCLUDE_ERI2 >= 1) && \
1234 (LIBINT_INCLUDE_ERI3 >= 1) && !VOTCA_LIBINT_ERI3_DERIV_BROKEN
Container to hold Basisfunctions for all atoms.
Index AOBasisSize() const
std::vector< Index > getMapToBasisFunctions() const
const AOShell & getShell(Index idx) const
const std::vector< Index > & getFuncPerAtom() const
Index getNumofShells() const
Index getMaxNprim() const
std::vector< libint2::Shell > GenerateLibintBasis() const
Index getAtomIndex() const
#define VOTCA_LIBINT_ERI3_DERIV_BROKEN
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixLibInt
void ForEachThreeCenterIntegral(const AOBasis &auxbasis, const AOBasis &dftbasis, const ThreeCenterVisitor &use)
Eigen::MatrixXd ContractCoulombMetricDerivatives(const AOBasis &auxbasis, const Eigen::MatrixXd &G)
std::vector< AOMatrixDerivative > ComputeNuclearAttractionDerivatives(const AOBasis &aobasis, const QMMolecule &mol)
std::array< std::vector< Eigen::MatrixXd >, 3 > ThreeCenterDerivative
std::vector< AOMatrixDerivative > ComputeKineticDerivatives(const AOBasis &aobasis)
std::vector< ThreeCenterDerivative > ComputeThreeCenterDerivatives(const AOBasis &, const AOBasis &)
std::vector< AOMatrixDerivative > ComputeOverlapDerivatives(const AOBasis &aobasis)
std::function< void(Index, const Eigen::MatrixXd &)> ThreeCenterVisitor
std::vector< Eigen::MatrixXd > ComputeThreeCenterIntegrals(const AOBasis &auxbasis, const AOBasis &dftbasis)
std::vector< Eigen::MatrixXd > ComputeAO3cBlock(const libint2::Shell &auxshell, const AOBasis &dftbasis, libint2::Engine &engine)
std::function< void(Index, Eigen::MatrixXd &)> AuxDensityFn
Eigen::MatrixXd ContractThreeCenterDerivatives(const AOBasis &auxbasis, const AOBasis &dftbasis, const AuxDensityFn &gamma)
std::vector< AOMatrixDerivative > ComputeCoulombMetricDerivatives(const AOBasis &)
std::array< Eigen::MatrixXd, 3 > AOMatrixDerivative
bool HasLibint2DerivativeSupport()
Provides a means for comparing floating point numbers.