22#include <boost/format.hpp>
49void RejectRangeSeparated(
const xc_func_type& func,
const std::string& name) {
50 if (func.cam_beta != 0.0 || func.cam_omega != 0.0) {
51 throw std::runtime_error(
52 "Functional " + name +
53 " is a range-separated hybrid; range-separated exact exchange is not "
54 "implemented in xtp (DFT and GW). Use a global hybrid or a semilocal "
74 double exactexchange = 0.0;
77 std::vector<std::string> functional_names =
80 if (functional_names.size() > 2) {
81 throw std::runtime_error(
"Too many functional names");
82 }
else if (functional_names.empty()) {
83 throw std::runtime_error(
"Specify at least one functional");
86 for (
const std::string& functional_name : functional_names) {
87 int func_id = map.getID(functional_name);
94 if (xc_func_init(&func, func_id, XC_UNPOLARIZED) != 0) {
95 throw std::runtime_error(
96 (boost::format(
"Functional %s not found\n") % functional_name).str());
100 RejectRangeSeparated(func, functional_name);
106 if (exactexchange > 0 && func.cam_alpha > 0) {
108 throw std::runtime_error(
109 "You have specified two functionals with exact exchange");
112 exactexchange += func.cam_alpha;
116 return exactexchange;
122 std::vector<std::string> strs =
129 if (strs.size() == 1) {
131 }
else if (strs.size() == 2) {
136 throw std::runtime_error(
137 "LIBXC. Please specify one combined or an exchange and a correlation "
145 throw std::runtime_error(
146 (boost::format(
"Functional %s not found\n") % strs[0]).str());
150 RejectRangeSeparated(
xfunc, strs[0]);
157 throw std::runtime_error(
158 "Your functional misses either correlation or exchange, please specify "
159 "another functional, separated by whitespace");
165 throw std::runtime_error(
166 (boost::format(
"Functional %s not found\n") % strs[1]).str());
170 RejectRangeSeparated(
cfunc, strs[1]);
177 if ((
xfunc.info->kind +
cfunc.info->kind) != 1) {
180 throw std::runtime_error(
181 "Your functionals are not one exchange and one correlation");
187 throw std::runtime_error(
188 "Failed to initialize polarized exchange XC "
193 throw std::runtime_error(
194 "Failed to initialize polarized correlation XC "
198 auto is_gga = [](
const xc_func_type& f) {
199 return f.info->family == XC_FAMILY_GGA ||
200 f.info->family == XC_FAMILY_HYB_GGA;
210void CallLibxc(
const xc_func_type& f,
Index n,
const double* rho,
211 const double* sigma,
double* exc,
double* vrho,
double* vsigma) {
212 switch (f.info->family) {
214 xc_lda_exc_vxc(&f, std::size_t(n), rho, exc, vrho);
217 case XC_FAMILY_HYB_GGA:
218 xc_gga_exc_vxc(&f, std::size_t(n), rho, sigma, exc, vrho, vsigma);
221 throw std::runtime_error(
"Unsupported XC functional family.");
228 const double* sigma,
double* exc,
229 double* vrho,
double* vsigma)
const {
230 std::fill(vsigma, vsigma + n, 0.0);
231 CallLibxc(
xfunc, n, rho, sigma, exc, vrho, vsigma);
233 std::vector<double>
e(n), vr(n), vs(n, 0.0);
234 CallLibxc(
cfunc, n, rho, sigma,
e.data(), vr.data(), vs.data());
235 for (
Index i = 0; i < n; ++i) {
245 const double* sigma,
double* exc,
247 double* vsigma)
const {
248 std::fill(vsigma, vsigma + 3 * n, 0.0);
249 CallLibxc(
xfunc_pol_, n, rho, sigma, exc, vrho, vsigma);
251 std::vector<double>
e(n), vr(2 * n), vs(3 * n, 0.0);
252 CallLibxc(
cfunc_pol_, n, rho, sigma,
e.data(), vr.data(), vs.data());
253 for (
Index i = 0; i < n; ++i) {
256 for (
Index i = 0; i < 2 * n; ++i) {
259 for (
Index i = 0; i < 3 * n; ++i) {
267 double rho,
double sigma)
const {
276 double rho_a,
double rho_b,
double sigma_aa,
double sigma_ab,
277 double sigma_bb)
const {
279 const double rho[2] = {rho_a, rho_b};
280 const double sigma[3] = {sigma_aa, sigma_ab, sigma_bb};
301 const Eigen::MatrixXd& density_matrix)
const {
302 assert(density_matrix.isApprox(density_matrix.transpose()) &&
303 "Density matrix has to be symmetric!");
307#pragma omp parallel for schedule(guided) reduction(+ : vxc)
308 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
318 1.e-40 / double(density_matrix.rows()) / double(density_matrix.rows());
319 if (DMAT_here.cwiseAbs2().maxCoeff() < cutoff) {
325 Eigen::MatrixXd Vxc_here = Eigen::MatrixXd::Zero(nf, nf);
326 Eigen::MatrixXd phi(nf, block);
327 Eigen::MatrixXd dphi(nf, 3 * block);
328 Eigen::MatrixXd y(nf, block);
329 Eigen::MatrixXd a(nf, block);
330 std::vector<Index> kept;
331 std::vector<double> rho, sigma, exc, vrho, vsigma;
332 std::vector<Eigen::Vector3d> grad;
333 double EXC_box = 0.0;
335 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
338 for (
Index start = 0; start < box.
size(); start += block) {
339 const Index nb = std::min(block, box.
size() - start);
340 for (
Index p = 0; p < nb; ++p) {
343 y.leftCols(nb).noalias() = DMAT_here * phi.leftCols(nb);
349 for (
Index p = 0; p < nb; ++p) {
350 const double r = 0.5 * phi.col(p).dot(y.col(p));
351 if (r * weights[start + p] < 1.e-20) {
354 const Eigen::Vector3d g =
355 dphi.middleCols(3 * p, 3).transpose() * y.col(p);
358 sigma.push_back(g.squaredNorm());
373 for (
Index k = 0; k < nk; ++k) {
374 const Index p = kept[k];
375 const double w = weights[start + p];
376 EXC_box += w * rho[k] * exc[k];
377 a.col(k) = (w * 0.5 * vrho[k]) * phi.col(p);
379 a.col(k).noalias() +=
380 (w * 2.0 * vsigma[k]) * (dphi.middleCols(3 * p, 3) * grad[k]);
383 phi.col(k) = phi.col(p);
386 Vxc_here.noalias() += a.leftCols(nk) * phi.leftCols(nk).transpose();
398void CallLibxcFXC(
const xc_func_type& f,
Index n,
const double* rho,
399 const double* sigma,
double* vrho,
double* vsigma,
400 double* v2rho2,
double* v2rhosigma,
double* v2sigma2) {
401 if (!(f.info->flags & XC_FLAGS_HAVE_FXC)) {
402 throw std::runtime_error(std::string(
"Functional ") + f.info->name +
403 " has no second derivatives in libxc");
405 switch (f.info->family) {
407 xc_lda_vxc_fxc(&f, std::size_t(n), rho, vrho, v2rho2);
410 case XC_FAMILY_HYB_GGA:
411 xc_gga_vxc_fxc(&f, std::size_t(n), rho, sigma, vrho, vsigma, v2rho2,
412 v2rhosigma, v2sigma2);
415 throw std::runtime_error(
"Unsupported XC functional family.");
422 const double* sigma,
double* vsigma,
423 double* v2rho2,
double* v2rhosigma,
424 double* v2sigma2)
const {
425 std::vector<double> vrho(n);
432 const double* sigma,
double* vrho,
433 double* vsigma,
double* v2rho2,
435 double* v2sigma2)
const {
436 std::fill(vsigma, vsigma + n, 0.0);
437 std::fill(v2rhosigma, v2rhosigma + n, 0.0);
438 std::fill(v2sigma2, v2sigma2 + n, 0.0);
439 CallLibxcFXC(
xfunc, n, rho, sigma, vrho, vsigma, v2rho2, v2rhosigma,
442 std::vector<double> vr(n), vs(n, 0.0), rr(n), rs(n, 0.0), ss(n, 0.0);
443 CallLibxcFXC(
cfunc, n, rho, sigma, vr.data(), vs.data(), rr.data(),
444 rs.data(), ss.data());
445 for (
Index i = 0; i < n; ++i) {
449 v2rhosigma[i] += rs[i];
450 v2sigma2[i] += ss[i];
458 double* vsigma,
double* v2rho2,
460 double* v2sigma2)
const {
461 std::vector<double> vrho(2 * n);
462 std::fill(vsigma, vsigma + 3 * n, 0.0);
463 std::fill(v2rhosigma, v2rhosigma + 6 * n, 0.0);
464 std::fill(v2sigma2, v2sigma2 + 6 * n, 0.0);
465 CallLibxcFXC(
xfunc_pol_, n, rho, sigma, vrho.data(), vsigma, v2rho2,
466 v2rhosigma, v2sigma2);
468 std::vector<double> vr(2 * n), vs(3 * n, 0.0), rr(3 * n), rs(6 * n, 0.0),
470 CallLibxcFXC(
cfunc_pol_, n, rho, sigma, vr.data(), vs.data(), rr.data(),
471 rs.data(), ss.data());
472 for (
Index i = 0; i < 3 * n; ++i) {
476 for (
Index i = 0; i < 6 * n; ++i) {
477 v2rhosigma[i] += rs[i];
478 v2sigma2[i] += ss[i];
493std::vector<std::array<Eigen::MatrixXd, 2>>
495 const Eigen::MatrixXd& dmat_alpha,
const Eigen::MatrixXd& dmat_beta,
496 const std::vector<std::array<Eigen::MatrixXd, 2>>& response)
const {
497 const Index n_ao = dmat_alpha.rows();
499 std::vector<std::array<Eigen::MatrixXd, 2>> result(
500 static_cast<std::size_t
>(n_resp),
501 {Eigen::MatrixXd::Zero(n_ao, n_ao), Eigen::MatrixXd::Zero(n_ao, n_ao)});
505 auto wrong_size = [n_ao](
const std::array<Eigen::MatrixXd, 2>& r) {
506 return r[0].rows() != n_ao || r[0].cols() != n_ao || r[1].rows() != n_ao ||
509 if (std::any_of(response.begin(), response.end(), wrong_size)) {
510 throw std::runtime_error(
511 "IntegrateFXCSpin: response matrix does not match the density "
516 const double rho[2] = {0.1, 0.05};
517 const double sigma[3] = {0.01, 0.005, 0.004};
518 double vs[3], rr[3], rs[6], ss[6];
523 std::vector<Eigen::MatrixXd*> targets;
524 for (std::array<Eigen::MatrixXd, 2>& m : result) {
525 targets.push_back(&m[0]);
526 targets.push_back(&m[1]);
529#pragma omp parallel for schedule(guided)
530 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
537 std::vector<Eigen::MatrixXd> dDa(
static_cast<std::size_t
>(n_resp));
538 std::vector<Eigen::MatrixXd> dDb(
static_cast<std::size_t
>(n_resp));
539 for (
Index k = 0; k < n_resp; ++k) {
545 std::vector<Eigen::MatrixXd> fxc_a(
static_cast<std::size_t
>(n_resp),
546 Eigen::MatrixXd::Zero(nf, nf));
547 std::vector<Eigen::MatrixXd> fxc_b(
static_cast<std::size_t
>(n_resp),
548 Eigen::MatrixXd::Zero(nf, nf));
549 Eigen::MatrixXd phi(nf, block);
550 Eigen::MatrixXd dphi(nf, 3 * block);
551 Eigen::MatrixXd ya(nf, block);
552 Eigen::MatrixXd yb(nf, block);
553 Eigen::MatrixXd yka(nf, block);
554 Eigen::MatrixXd ykb(nf, block);
555 Eigen::MatrixXd aa(nf, block);
556 Eigen::MatrixXd ab(nf, block);
557 std::vector<Index> kept;
558 std::vector<double> rho, sigma, vsigma, v2rho2, v2rhosigma, v2sigma2;
559 std::vector<Eigen::Vector3d> grad_a, grad_b;
560 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
563 for (
Index start = 0; start < box.
size(); start += block) {
564 const Index nb = std::min(block, box.
size() - start);
565 for (
Index p = 0; p < nb; ++p) {
568 ya.leftCols(nb).noalias() = DMa * phi.leftCols(nb);
569 yb.leftCols(nb).noalias() = DMb * phi.leftCols(nb);
575 for (
Index p = 0; p < nb; ++p) {
576 const double rho_a = phi.col(p).dot(ya.col(p));
577 const double rho_b = phi.col(p).dot(yb.col(p));
578 if ((rho_a + rho_b) * weights[start + p] < 1.e-20) {
581 const Eigen::Vector3d ga =
582 2.0 * (dphi.middleCols(3 * p, 3).transpose() * ya.col(p));
583 const Eigen::Vector3d gb =
584 2.0 * (dphi.middleCols(3 * p, 3).transpose() * yb.col(p));
586 rho.push_back(rho_a);
587 rho.push_back(rho_b);
588 sigma.push_back(ga.dot(ga));
589 sigma.push_back(ga.dot(gb));
590 sigma.push_back(gb.dot(gb));
591 grad_a.push_back(ga);
592 grad_b.push_back(gb);
598 vsigma.resize(3 * nk);
599 v2rho2.resize(3 * nk);
600 v2rhosigma.resize(6 * nk);
601 v2sigma2.resize(6 * nk);
603 v2rho2.data(), v2rhosigma.data(), v2sigma2.data());
604 Eigen::MatrixXd phik(nf, nk);
605 for (
Index q = 0; q < nk; ++q) {
606 phik.col(q) = phi.col(kept[q]);
609 for (
Index k = 0; k < n_resp; ++k) {
610 yka.leftCols(nb).noalias() = dDa[std::size_t(k)] * phi.leftCols(nb);
611 ykb.leftCols(nb).noalias() = dDb[std::size_t(k)] * phi.leftCols(nb);
612 for (
Index q = 0; q < nk; ++q) {
613 const Index p = kept[q];
614 const double w = weights[start + p];
615 const double dra = phi.col(p).dot(yka.col(p));
616 const double drb = phi.col(p).dot(ykb.col(p));
617 const double* r2 = &v2rho2[3 * q];
619 const double dva = r2[0] * dra + r2[1] * drb;
620 const double dvb = r2[1] * dra + r2[2] * drb;
621 aa.col(q) = (w * 0.5 * dva) * phi.col(p);
622 ab.col(q) = (w * 0.5 * dvb) * phi.col(p);
625 const auto d = dphi.middleCols(3 * p, 3);
626 const Eigen::Vector3d dga = 2.0 * (d.transpose() * yka.col(p));
627 const Eigen::Vector3d dgb = 2.0 * (d.transpose() * ykb.col(p));
628 const Eigen::Vector3d& ga = grad_a[q];
629 const Eigen::Vector3d& gb = grad_b[q];
630 const double ds[3] = {2.0 * ga.dot(dga), ga.dot(dgb) + dga.dot(gb),
632 const double* rs = &v2rhosigma[6 * q];
633 const double* s2 = &v2sigma2[6 * q];
634 const double* vs = &vsigma[3 * q];
636 const double S[3][3] = {{s2[0], s2[1], s2[2]},
637 {s2[1], s2[3], s2[4]},
638 {s2[2], s2[4], s2[5]}};
639 const double dva = r2[0] * dra + r2[1] * drb + rs[0] * ds[0] +
640 rs[1] * ds[1] + rs[2] * ds[2];
641 const double dvb = r2[1] * dra + r2[2] * drb + rs[3] * ds[0] +
642 rs[4] * ds[1] + rs[5] * ds[2];
644 for (
int c = 0; c < 3; ++c) {
645 dvs[c] = rs[c] * dra + rs[3 + c] * drb +
S[c][0] * ds[0] +
646 S[c][1] * ds[1] +
S[c][2] * ds[2];
648 const Eigen::Vector3d vec_a =
649 2.0 * dvs[0] * ga + 2.0 * vs[0] * dga + dvs[1] * gb + vs[1] * dgb;
650 const Eigen::Vector3d vec_b =
651 2.0 * dvs[2] * gb + 2.0 * vs[2] * dgb + dvs[1] * ga + vs[1] * dga;
652 aa.col(q) = (w * 0.5 * dva) * phi.col(p) + w * (d * vec_a);
653 ab.col(q) = (w * 0.5 * dvb) * phi.col(p) + w * (d * vec_b);
655 fxc_a[std::size_t(k)].noalias() += aa.leftCols(nk) * phik.transpose();
656 fxc_b[std::size_t(k)].noalias() += ab.leftCols(nk) * phik.transpose();
659 std::vector<const Eigen::MatrixXd*> blocks;
660 for (
Index k = 0; k < n_resp; ++k) {
661 blocks.push_back(&fxc_a[std::size_t(k)]);
662 blocks.push_back(&fxc_b[std::size_t(k)]);
664 acc.
Add(box, blocks);
667 for (std::array<Eigen::MatrixXd, 2>& m : result) {
668 m[0] = (m[0] + m[0].transpose()).eval();
669 m[1] = (m[1] + m[1].transpose()).eval();
683 const Eigen::MatrixXd& density_matrix)
const {
686 const double rho = 0.1;
687 const double sigma = 0.01;
688 double vs, rr, rs, ss;
692 kernel.
n_ao = density_matrix.rows();
693 kernel.
boxes.resize(std::size_t(
grid_.getBoxesSize()));
694#pragma omp parallel for schedule(guided)
695 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
704 Eigen::MatrixXd phi(nf, block);
705 Eigen::MatrixXd dphi(nf, 3 * block);
706 Eigen::MatrixXd y(nf, block);
707 std::vector<double> rho, sigma, w;
708 std::vector<Eigen::Vector3d> grad;
709 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
711 for (
Index start = 0; start < box.
size(); start += block) {
712 const Index nb = std::min(block, box.
size() - start);
713 for (
Index p = 0; p < nb; ++p) {
716 y.leftCols(nb).noalias() = DMAT_here * phi.leftCols(nb);
717 for (
Index p = 0; p < nb; ++p) {
718 const double r = 0.5 * phi.col(p).dot(y.col(p));
719 if (r * weights[start + p] < 1.e-20) {
722 const Eigen::Vector3d g =
723 dphi.middleCols(3 * p, 3).transpose() * y.col(p);
724 kb.
points.push_back(start + p);
726 sigma.push_back(g.squaredNorm());
728 w.push_back(weights[start + p]);
732 kb.
w = Eigen::Map<const Eigen::VectorXd>(w.data(), nk);
742 kb.
grad.resize(3, nk);
743 for (
Index q = 0; q < nk; ++q) {
744 kb.
grad.col(q) = grad[std::size_t(q)];
753 const std::vector<Eigen::MatrixXd>& response_matrices)
const {
755 const Index n_resp =
Index(response_matrices.size());
756 std::vector<Eigen::MatrixXd> result(
static_cast<std::size_t
>(n_resp),
757 Eigen::MatrixXd::Zero(n_ao, n_ao));
761 if (std::any_of(response_matrices.begin(), response_matrices.end(),
762 [n_ao](
const Eigen::MatrixXd& m) {
763 return m.rows() != n_ao || m.cols() != n_ao;
765 throw std::runtime_error(
766 "IntegrateFXC: response matrix does not match the density matrix");
768 std::vector<Eigen::MatrixXd*> targets;
769 for (Eigen::MatrixXd& m : result) {
770 targets.push_back(&m);
773#pragma omp parallel for schedule(guided)
774 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
782 std::vector<Eigen::MatrixXd> dmat_k(
static_cast<std::size_t
>(n_resp));
783 for (
Index k = 0; k < n_resp; ++k) {
784 dmat_k[std::size_t(k)] =
787 std::vector<Eigen::MatrixXd> fxc_here(
static_cast<std::size_t
>(n_resp),
788 Eigen::MatrixXd::Zero(nf, nf));
790 Eigen::MatrixXd phi_buf(nf, block);
791 Eigen::MatrixXd dphi_buf(nf, 3 * block);
792 Eigen::MatrixXd yk(nf, block);
793 Eigen::MatrixXd a(nf, block);
794 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
795 for (
Index start = 0; start < nk_box; start += block) {
796 const Index nb = std::min(block, nk_box - start);
797 for (
Index q = 0; q < nb; ++q) {
801 const auto phi = phi_buf.leftCols(nb);
802 const auto dphi = dphi_buf.leftCols(3 * nb);
803 for (
Index k = 0; k < n_resp; ++k) {
804 yk.leftCols(nb).noalias() = dmat_k[std::size_t(k)] * phi;
805 for (
Index q = 0; q < nb; ++q) {
806 const Index kq = start + q;
807 const double w = kb.
w(kq);
808 const double drho = 0.5 * phi.col(q).dot(yk.col(q));
810 const Eigen::Vector3d dg =
811 dphi.middleCols(3 * q, 3).transpose() * yk.col(q);
812 const Eigen::Vector3d g = kb.
grad.col(kq);
813 const double dsigma = 2.0 * g.dot(dg);
814 const double dv_rho =
816 const double dv_sigma =
818 const Eigen::Vector3d vec =
819 2.0 * (dv_sigma * g + kb.
vsigma(kq) * dg);
820 a.col(q) = (w * 0.5 * dv_rho) * phi.col(q) +
821 w * (dphi.middleCols(3 * q, 3) * vec);
823 a.col(q) = (w * 0.5 * kb.
v2rho2(kq) * drho) * phi.col(q);
826 fxc_here[std::size_t(k)].noalias() += a.leftCols(nb) * phi.transpose();
829 std::vector<const Eigen::MatrixXd*> blocks;
830 for (
const Eigen::MatrixXd& m : fxc_here) {
831 blocks.push_back(&m);
833 acc.
Add(box, blocks);
836 for (Eigen::MatrixXd& m : result) {
837 m = (m + m.transpose()).eval();
844 const Eigen::MatrixXd& density_matrix,
845 const std::vector<Eigen::MatrixXd>& response_matrices)
const {
846 if (std::any_of(response_matrices.begin(), response_matrices.end(),
847 [&density_matrix](
const Eigen::MatrixXd& m) {
848 return m.rows() != density_matrix.rows() ||
849 m.cols() != density_matrix.cols();
851 throw std::runtime_error(
852 "IntegrateFXC: response matrix does not match the density matrix");
854 if (response_matrices.empty()) {
879 const Eigen::MatrixXd& density_matrix,
const AOBasis& dftbasis)
const {
880 assert(density_matrix.isApprox(density_matrix.transpose()) &&
881 "Density matrix has to be symmetric!");
885 std::vector<Eigen::MatrixXd> grad_thread(nthreads,
886 Eigen::MatrixXd::Zero(natoms, 3));
888 std::exception_ptr eptr_pulay =
nullptr;
889#pragma omp parallel for schedule(guided)
890 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
898 const Eigen::MatrixXd DMAT_here =
901 double cutoff = 1.e-40 / double(density_matrix.rows()) /
902 double(density_matrix.rows());
903 if (DMAT_here.cwiseAbs2().maxCoeff() < cutoff) {
908 std::vector<Index> local_idx_to_atom(box.
Matrixsize());
909 const std::vector<const AOShell*>& shells = box.
getShells();
910 const std::vector<GridboxRange>& ao_ranges = box.
getAOranges();
911 for (
size_t s = 0; s < shells.size(); ++s) {
912 Index atom = shells[s]->getAtomIndex();
913 for (
Index k = 0; k < ao_ranges[s].size; ++k) {
914 local_idx_to_atom[ao_ranges[s].start + k] = atom;
918 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
926 Eigen::VectorXd temp = ao.
values.transpose() * DMAT_here;
927 double rho = 0.5 * temp.dot(ao.
values);
928 const double weight = weights[p];
930 if (rho * weight < 1.e-20) {
934 const Eigen::Vector3d rho_grad = temp.transpose() * ao.
derivatives;
957 Eigen::MatrixX3d Gmat = DMAT_here * ao.
derivatives;
958 Eigen::VectorXd s_vec = Gmat * rho_grad;
960 Eigen::Matrix3d Hessian_rho = Eigen::Matrix3d::Zero();
962 Hessian_rho += temp(mu) * ao.
hessians[mu];
964 Eigen::Matrix3d M = ao.
derivatives.transpose() * Gmat;
965 Hessian_rho += 0.5 * (M + M.transpose());
967 Index owner_of_point_sigma = owner_atoms[p];
968 Eigen::Vector3d dsigma_translation = 2.0 * (Hessian_rho * rho_grad);
969 grad_thread[thread_id].row(owner_of_point_sigma) +=
970 (weight * xc.
df_dsigma * dsigma_translation).transpose();
980 Index owner_of_point = owner_atoms[p];
981 grad_thread[thread_id].row(owner_of_point) +=
982 (weight * xc.
df_drho * rho_grad).transpose();
988 Index atom = local_idx_to_atom[mu];
989 Eigen::Vector3d contribution = -weight * xc.
df_drho * temp(mu) *
992 Eigen::Vector3d dsigma_basis_mu =
993 -2.0 * (ao.
derivatives.row(mu).transpose() * s_vec(mu) +
994 temp(mu) * (ao.
hessians[mu] * rho_grad));
995 contribution += weight * xc.
df_dsigma * dsigma_basis_mu;
996 grad_thread[thread_id].row(atom) += contribution.transpose();
1003 eptr_pulay = std::current_exception();
1009 std::rethrow_exception(eptr_pulay);
1012 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
1013 for (
Index t = 0; t < nthreads; ++t) {
1014 grad += grad_thread[t];
1021template <
class Gr
id>
1023 const Eigen::MatrixXd& dmat_alpha,
const Eigen::MatrixXd& dmat_beta)
const {
1024 assert(dmat_alpha.isApprox(dmat_alpha.transpose()) &&
1025 "Alpha density matrix has to be symmetric!");
1026 assert(dmat_beta.isApprox(dmat_beta.transpose()) &&
1027 "Beta density matrix has to be symmetric!");
1031 Eigen::MatrixXd::Zero(dmat_alpha.rows(), dmat_alpha.cols());
1032 result.
vxc_beta = Eigen::MatrixXd::Zero(dmat_beta.rows(), dmat_beta.cols());
1036 Eigen::MatrixXd vxc_alpha_private =
1037 Eigen::MatrixXd::Zero(dmat_alpha.rows(), dmat_alpha.cols());
1038 Eigen::MatrixXd vxc_beta_private =
1039 Eigen::MatrixXd::Zero(dmat_beta.rows(), dmat_beta.cols());
1040 double exc_private = 0.0;
1042 std::vector<Index> kept;
1043 std::vector<double> rho, sigma, exc, vrho, vsigma;
1044 std::vector<Eigen::Vector3d> grad_a, grad_b;
1046#pragma omp for schedule(guided)
1047 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
1057 1.e-40 / double(dmat_alpha.rows()) / double(dmat_alpha.rows());
1058 if (std::max(DMa.cwiseAbs2().maxCoeff(), DMb.cwiseAbs2().maxCoeff()) <
1065 Eigen::MatrixXd Vxc_a_here = Eigen::MatrixXd::Zero(nf, nf);
1066 Eigen::MatrixXd Vxc_b_here = Eigen::MatrixXd::Zero(nf, nf);
1067 Eigen::MatrixXd phi(nf, block);
1068 Eigen::MatrixXd dphi(nf, 3 * block);
1069 Eigen::MatrixXd ya(nf, block);
1070 Eigen::MatrixXd yb(nf, block);
1071 Eigen::MatrixXd aa(nf, block);
1072 Eigen::MatrixXd ab(nf, block);
1074 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
1077 for (
Index start = 0; start < box.
size(); start += block) {
1078 const Index nb = std::min(block, box.
size() - start);
1079 for (
Index p = 0; p < nb; ++p) {
1082 ya.leftCols(nb).noalias() = DMa * phi.leftCols(nb);
1083 yb.leftCols(nb).noalias() = DMb * phi.leftCols(nb);
1090 for (
Index p = 0; p < nb; ++p) {
1091 const double rho_a = phi.col(p).dot(ya.col(p));
1092 const double rho_b = phi.col(p).dot(yb.col(p));
1093 if ((rho_a + rho_b) * weights[start + p] < 1.e-20) {
1096 const Eigen::Vector3d ga =
1097 2.0 * (dphi.middleCols(3 * p, 3).transpose() * ya.col(p));
1098 const Eigen::Vector3d gb =
1099 2.0 * (dphi.middleCols(3 * p, 3).transpose() * yb.col(p));
1101 rho.push_back(rho_a);
1102 rho.push_back(rho_b);
1103 sigma.push_back(ga.dot(ga));
1104 sigma.push_back(ga.dot(gb));
1105 sigma.push_back(gb.dot(gb));
1106 grad_a.push_back(ga);
1107 grad_b.push_back(gb);
1114 vrho.resize(2 * nk);
1115 vsigma.resize(3 * nk);
1117 vrho.data(), vsigma.data());
1119 for (
Index k = 0; k < nk; ++k) {
1120 const Index p = kept[k];
1121 const double w = weights[start + p];
1122 exc_private += w * (rho[2 * k] + rho[2 * k + 1]) * exc[k];
1124 aa.col(k) = (w * 0.5 * vrho[2 * k]) * phi.col(p);
1125 ab.col(k) = (w * 0.5 * vrho[2 * k + 1]) * phi.col(p);
1127 const auto d = dphi.middleCols(3 * p, 3);
1128 const Eigen::VectorXd g_a = d * grad_a[k];
1129 const Eigen::VectorXd g_b = d * grad_b[k];
1130 const double vs_aa = vsigma[3 * k];
1131 const double vs_ab = vsigma[3 * k + 1];
1132 const double vs_bb = vsigma[3 * k + 2];
1133 aa.col(k).noalias() += w * (2.0 * vs_aa * g_a + vs_ab * g_b);
1134 ab.col(k).noalias() += w * (vs_ab * g_a + 2.0 * vs_bb * g_b);
1137 phi.col(k) = phi.col(p);
1140 Vxc_a_here.noalias() += aa.leftCols(nk) * phi.leftCols(nk).transpose();
1141 Vxc_b_here.noalias() += ab.leftCols(nk) * phi.leftCols(nk).transpose();
1150 result.
vxc_alpha += vxc_alpha_private + vxc_alpha_private.transpose();
1151 result.
vxc_beta += vxc_beta_private + vxc_beta_private.transpose();
1152 result.
energy += exc_private;
1163constexpr double kSSWAlpha = 1.0 / 0.30;
1164constexpr double kSSWCutoff = 0.725;
1166constexpr double kSqrtPi = 1.7724538509055160273;
1168double SSWValue(
double mu) {
1169 double val = 0.5 * std::erfc(std::abs(mu / (1.0 - mu * mu)) * kSSWAlpha);
1177double SSWDerivative(
double mu) {
1178 double h = std::abs(mu) / (1.0 - mu * mu);
1179 double sign_mu = (mu > 0.0) ? 1.0 : ((mu < 0.0) ? -1.0 : 0.0);
1180 double one_minus_mu2 = 1.0 - mu * mu;
1181 double hprime = sign_mu * (1.0 + mu * mu) / (one_minus_mu2 * one_minus_mu2);
1182 double d_erf1c = -(kSSWAlpha / kSqrtPi) *
1183 std::exp(-(kSSWAlpha * h) * (kSSWAlpha * h)) * hprime;
1184 return (mu > 0.0) ? -d_erf1c : d_erf1c;
1193void AddSSWWeightDerivative(
const Eigen::Vector3d& point,
Index owner,
1195 const Eigen::MatrixXd& Rij, Eigen::MatrixXd& grad) {
1196 const Index natoms = atoms.size();
1197 Eigen::VectorXd rq(natoms);
1198 for (
Index k = 0; k < natoms; ++k) {
1199 rq(k) = (point - atoms[k].getPos()).norm();
1205 Eigen::VectorXd p = Eigen::VectorXd::Ones(natoms);
1206 Eigen::MatrixXd mu_table = Eigen::MatrixXd::Zero(natoms, natoms);
1207 Eigen::MatrixXd sk_table = Eigen::MatrixXd::Zero(natoms, natoms);
1208 Eigen::MatrixXi hard = Eigen::MatrixXi::Zero(natoms, natoms);
1209 for (
Index ii = 1; ii < natoms; ++ii) {
1210 for (
Index jj = 0; jj < ii; ++jj) {
1211 const double mu = (rq(ii) - rq(jj)) * Rij(jj, ii);
1212 mu_table(jj, ii) = mu;
1213 if (mu > kSSWCutoff) {
1216 }
else if (mu < -kSSWCutoff) {
1220 const double sk = SSWValue(mu);
1221 sk_table(jj, ii) = sk;
1223 p(ii) *= (1.0 - sk);
1227 const double wsum = p.sum();
1228 const double w_owner = p(owner) / wsum;
1230 constexpr double kNegligibleWOwner = 1.e-8;
1231 if (w_owner < kNegligibleWOwner) {
1235 auto d_rq_dR = [&](
Index k,
Index A) -> Eigen::Vector3d {
1236 if (A == owner && A == k) {
1237 return Eigen::Vector3d::Zero();
1238 }
else if (A == owner) {
1239 return (point - atoms[k].getPos()) / rq(k);
1240 }
else if (A == k) {
1241 return -(point - atoms[k].getPos()) / rq(k);
1243 return Eigen::Vector3d::Zero();
1246 auto add_dmu = [&](
Index a,
Index b,
double coef, Eigen::MatrixXd& out) {
1247 const double Rab = 1.0 / Rij(a, b);
1248 const Eigen::Vector3d rvec = atoms[a].getPos() - atoms[b].getPos();
1249 const double mu = mu_table(a, b);
1250 const Index cand[3] = {owner, a, b};
1251 for (
Index c = 0; c < 3; ++c) {
1252 const Index A = cand[c];
1253 if ((c > 0 && A == cand[0]) || (c > 1 && A == cand[1])) {
1256 Eigen::Vector3d dRab = Eigen::Vector3d::Zero();
1259 }
else if (A == b) {
1262 const Eigen::Vector3d dmu =
1263 (d_rq_dR(b, A) - d_rq_dR(a, A)) / Rab - (mu / Rab) * dRab;
1264 out.row(A) += coef * dmu.transpose();
1270 constexpr double kNegligibleP = 1.e-8;
1271 Eigen::MatrixXd dp_owner = Eigen::MatrixXd::Zero(natoms, 3);
1272 Eigen::MatrixXd dp_sum = Eigen::MatrixXd::Zero(natoms, 3);
1273 Eigen::MatrixXd dp_k(natoms, 3);
1274 for (
Index k = 0; k < natoms; ++k) {
1275 if (p(k) < kNegligibleP) {
1279 for (
Index b = k + 1; b < natoms; ++b) {
1280 if (hard(k, b) != 0) {
1283 const double skv = sk_table(k, b);
1284 if (skv < kNegligibleP) {
1287 add_dmu(k, b, SSWDerivative(mu_table(k, b)) / skv, dp_k);
1289 for (
Index a = 0; a < k; ++a) {
1290 if (hard(a, k) != 0) {
1293 const double one_minus_skv = 1.0 - sk_table(a, k);
1294 if (one_minus_skv < kNegligibleP) {
1297 add_dmu(a, k, -SSWDerivative(mu_table(a, k)) / one_minus_skv, dp_k);
1299 dp_sum += p(k) * dp_k;
1301 dp_owner = p(k) * dp_k;
1304 const double C_p = weight / w_owner;
1305 grad += (C_p *
e / wsum) * (dp_owner - w_owner * dp_sum);
1309template <
class Gr
id>
1311 const Eigen::MatrixXd& density_matrix,
const QMMolecule& atoms)
const {
1313 Eigen::MatrixXd Rij =
grid_.CalcInverseAtomDist(atoms);
1316 std::vector<Eigen::MatrixXd> grad_thread(nthreads,
1317 Eigen::MatrixXd::Zero(natoms, 3));
1319 std::exception_ptr eptr_weight =
nullptr;
1320#pragma omp parallel for schedule(guided)
1321 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
1329 const Eigen::MatrixXd DMAT_here =
1331 double cutoff = 1.e-40 / double(density_matrix.rows()) /
1332 double(density_matrix.rows());
1333 if (DMAT_here.cwiseAbs2().maxCoeff() < cutoff) {
1337 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
1339 const std::vector<Index>& owner_atoms = box.
getOwnerAtoms();
1341 for (
Index pidx = 0; pidx < box.
size(); ++pidx) {
1343 Eigen::VectorXd temp = ao.
values.transpose() * DMAT_here;
1344 double rho = 0.5 * temp.dot(ao.
values);
1345 double weight = weights[pidx];
1346 if (rho * weight < 1.e-20) {
1349 const Eigen::Vector3d rho_grad = temp.transpose() * ao.
derivatives;
1353 Index owner = owner_atoms[pidx];
1357 throw std::runtime_error(
1358 "GridWeightGradient: grid point has no owner atom set; the "
1359 "grid must be built with Vxc_Grid::GridSetup.");
1362 AddSSWWeightDerivative(points[pidx], owner, weight, rho * xc.
f_xc,
1363 atoms, Rij, grad_thread[thread_id]);
1369 eptr_weight = std::current_exception();
1375 std::rethrow_exception(eptr_weight);
1378 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
1379 for (
Index t = 0; t < nthreads; ++t) {
1380 grad += grad_thread[t];
1404template <
class Gr
id>
1406 const Eigen::MatrixXd& density_matrix,
const Eigen::MatrixXd& direction,
1409 const Eigen::MatrixXd Rij =
grid_.CalcInverseAtomDist(atoms);
1412 const double rho = 0.1;
1413 const double sigma = 0.01;
1414 double vr, vs, rr, rs, ss;
1418 std::vector<Eigen::MatrixXd> grad_thread(nthreads,
1419 Eigen::MatrixXd::Zero(natoms, 3));
1420 std::exception_ptr eptr =
nullptr;
1421#pragma omp parallel for schedule(guided)
1422 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
1425 Eigen::MatrixXd& gt = grad_thread[thread_id];
1432 const double cutoff = 1.e-40 / double(density_matrix.rows()) /
1433 double(density_matrix.rows());
1434 if (T.cwiseAbs2().maxCoeff() < cutoff) {
1438 std::vector<Index> local_idx_to_atom(nf);
1439 const std::vector<const AOShell*>& shells = box.
getShells();
1440 const std::vector<GridboxRange>& ao_ranges = box.
getAOranges();
1441 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
1442 const Index atom = shells[sh]->getAtomIndex();
1443 for (
Index k = 0; k < ao_ranges[sh].size; ++k) {
1444 local_idx_to_atom[ao_ranges[sh].start + k] = atom;
1447 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
1449 const std::vector<Index>& owner_atoms = box.
getOwnerAtoms();
1451 for (
Index pt = 0; pt < box.
size(); ++pt) {
1452 const double w = weights[pt];
1453 const Index owner = owner_atoms[pt];
1455 throw std::runtime_error(
1456 "GradientResponse: grid point has no owner atom set; the grid "
1457 "must be built with Vxc_Grid::GridSetup.");
1467 const Eigen::MatrixX3d& dphi =
1470 const Eigen::VectorXd temp = T * phi;
1471 const double rho = 0.5 * temp.dot(phi);
1472 if (rho * w < 1.e-20) {
1475 const Eigen::VectorXd tempP = Tp * phi;
1476 const double drho = 0.5 * tempP.dot(phi);
1477 const Eigen::Vector3d g = dphi.transpose() * temp;
1478 const Eigen::Vector3d gp = dphi.transpose() * tempP;
1479 const double sigma = g.squaredNorm();
1480 const double dsigma = 2.0 * g.dot(gp);
1481 double vr, vs, rr, rs, ss;
1483 const double dvr = rr * drho + rs * dsigma;
1484 const double dvs = rs * drho + ss * dsigma;
1487 gt.row(owner) += (w * (dvr * g + vr * gp)).transpose();
1488 for (
Index mu = 0; mu < nf; ++mu) {
1489 gt.row(local_idx_to_atom[mu]) -=
1490 w * (dvr * temp(mu) + vr * tempP(mu)) * dphi.row(mu);
1493 const Eigen::MatrixX3d Gm = T * dphi;
1494 const Eigen::MatrixX3d Gp = Tp * dphi;
1495 const Eigen::VectorXd sv = Gm * g;
1496 const Eigen::VectorXd svp = Gp * g + Gm * gp;
1497 Eigen::Matrix3d Hrho = Eigen::Matrix3d::Zero();
1498 Eigen::Matrix3d Hp = Eigen::Matrix3d::Zero();
1499 for (
Index mu = 0; mu < nf; ++mu) {
1500 Hrho += temp(mu) * ao.
hessians[mu];
1503 const Eigen::Matrix3d M = dphi.transpose() * Gm;
1504 const Eigen::Matrix3d Mp = dphi.transpose() * Gp;
1505 Hrho += 0.5 * (M + M.transpose());
1506 Hp += 0.5 * (Mp + Mp.transpose());
1508 (2.0 * w * (dvs * (Hrho * g) + vs * (Hp * g + Hrho * gp)))
1510 for (
Index mu = 0; mu < nf; ++mu) {
1511 const Eigen::Matrix3d&
H = ao.
hessians[mu];
1512 const Eigen::Vector3d d = dphi.row(mu).transpose();
1513 const Eigen::Vector3d c =
1514 dvs * (d * sv(mu) + temp(mu) * (
H * g)) +
1515 vs * (d * svp(mu) + tempP(mu) * (
H * g) + temp(mu) * (
H * gp));
1516 gt.row(local_idx_to_atom[mu]) -= (2.0 * w * c).transpose();
1519 AddSSWWeightDerivative(points[pt], owner, w, vr * drho + vs * dsigma,
1526 eptr = std::current_exception();
1532 std::rethrow_exception(eptr);
1534 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
1535 for (
const Eigen::MatrixXd& gtt : grad_thread) {
1541template <
class Gr
id>
1543 const Eigen::MatrixXd& dmat_alpha,
const Eigen::MatrixXd& dmat_beta,
1544 const AOBasis& dftbasis)
const {
1553 std::vector<Eigen::MatrixXd> grad_thread(nthreads,
1554 Eigen::MatrixXd::Zero(natoms, 3));
1556 std::exception_ptr eptr_pulay_uks =
nullptr;
1557#pragma omp parallel for schedule(guided)
1558 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
1574 std::vector<Index> local_idx_to_atom(box.
Matrixsize());
1575 const std::vector<const AOShell*>& shells = box.
getShells();
1576 const std::vector<GridboxRange>& ao_ranges = box.
getAOranges();
1577 for (
size_t s = 0; s < shells.size(); ++s) {
1578 Index atom = shells[s]->getAtomIndex();
1579 for (
Index k = 0; k < ao_ranges[s].size; ++k) {
1580 local_idx_to_atom[ao_ranges[s].start + k] = atom;
1583 const std::vector<Index>& owner_atoms = box.
getOwnerAtoms();
1585 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
1588 for (
Index p = 0; p < box.
size(); ++p) {
1591 Eigen::VectorXd temp_a = DMa * ao.
values;
1592 Eigen::VectorXd temp_b = DMb * ao.
values;
1593 const double rho_a = ao.
values.dot(temp_a);
1594 const double rho_b = ao.
values.dot(temp_b);
1595 const double rho = rho_a + rho_b;
1596 const double weight = weights[p];
1598 if (rho * weight < 1.e-20) {
1602 const Eigen::Vector3d rho_a_grad =
1604 const Eigen::Vector3d rho_b_grad =
1606 const double sigma_aa = rho_a_grad.dot(rho_a_grad);
1607 const double sigma_ab = rho_a_grad.dot(rho_b_grad);
1608 const double sigma_bb = rho_b_grad.dot(rho_b_grad);
1614 Index owner_of_point = owner_atoms[p];
1615 grad_thread[thread_id].row(owner_of_point) +=
1616 (weight * (xc.
vrho_a * rho_a_grad + xc.
vrho_b * rho_b_grad))
1622 Eigen::MatrixX3d Gmat_a = 2.0 * (DMa * ao.
derivatives);
1623 Eigen::MatrixX3d Gmat_b = 2.0 * (DMb * ao.
derivatives);
1624 Eigen::Vector3d V_a =
1626 Eigen::Vector3d V_b =
1631 Eigen::Matrix3d Hessian_rho_a = Eigen::Matrix3d::Zero();
1632 Eigen::Matrix3d Hessian_rho_b = Eigen::Matrix3d::Zero();
1634 Hessian_rho_a += 2.0 * temp_a(mu) * ao.
hessians[mu];
1635 Hessian_rho_b += 2.0 * temp_b(mu) * ao.
hessians[mu];
1637 Eigen::Matrix3d Ma = ao.
derivatives.transpose() * Gmat_a;
1638 Eigen::Matrix3d Mb = ao.
derivatives.transpose() * Gmat_b;
1639 Hessian_rho_a += 0.5 * (Ma + Ma.transpose());
1640 Hessian_rho_b += 0.5 * (Mb + Mb.transpose());
1642 grad_thread[thread_id].row(owner_of_point) +=
1643 (weight * (Hessian_rho_a * V_a + Hessian_rho_b * V_b)).transpose();
1647 Eigen::VectorXd Gmat_a_dot_Va = Gmat_a * V_a;
1648 Eigen::VectorXd Gmat_b_dot_Vb = Gmat_b * V_b;
1650 Index atom = local_idx_to_atom[mu];
1651 Eigen::Vector3d lda_contribution =
1655 Eigen::Vector3d sigma_contribution =
1657 (ao.
derivatives.row(mu).transpose() * Gmat_a_dot_Va(mu) +
1658 2.0 * temp_a(mu) * (ao.
hessians[mu] * V_a) +
1659 ao.
derivatives.row(mu).transpose() * Gmat_b_dot_Vb(mu) +
1660 2.0 * temp_b(mu) * (ao.
hessians[mu] * V_b));
1661 grad_thread[thread_id].row(atom) +=
1662 (lda_contribution + sigma_contribution).transpose();
1668 if (!eptr_pulay_uks) {
1669 eptr_pulay_uks = std::current_exception();
1674 if (eptr_pulay_uks) {
1675 std::rethrow_exception(eptr_pulay_uks);
1678 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
1679 for (
Index t = 0; t < nthreads; ++t) {
1680 grad += grad_thread[t];
1685template <
class Gr
id>
1687 const Eigen::MatrixXd& dmat_alpha,
const Eigen::MatrixXd& dmat_beta,
1694 Eigen::MatrixXd Rij =
grid_.CalcInverseAtomDist(atoms);
1697 std::vector<Eigen::MatrixXd> grad_thread(nthreads,
1698 Eigen::MatrixXd::Zero(natoms, 3));
1700 std::exception_ptr eptr_weight_uks =
nullptr;
1701#pragma omp parallel for schedule(guided)
1702 for (
Index i = 0; i <
grid_.getBoxesSize(); ++i) {
1713 1.e-40 / double(dmat_alpha.rows()) / double(dmat_alpha.rows());
1714 if (std::max(DMa.cwiseAbs2().maxCoeff(), DMb.cwiseAbs2().maxCoeff()) <
1719 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
1721 const std::vector<Index>& owner_atoms = box.
getOwnerAtoms();
1723 for (
Index pidx = 0; pidx < box.
size(); ++pidx) {
1725 Eigen::VectorXd temp_a = DMa * ao.
values;
1726 Eigen::VectorXd temp_b = DMb * ao.
values;
1727 const double rho_a = ao.
values.dot(temp_a);
1728 const double rho_b = ao.
values.dot(temp_b);
1729 const double rho = rho_a + rho_b;
1730 double weight = weights[pidx];
1731 if (rho * weight < 1.e-20) {
1734 const Eigen::Vector3d rho_a_grad =
1736 const Eigen::Vector3d rho_b_grad =
1738 const double sigma_aa = rho_a_grad.dot(rho_a_grad);
1739 const double sigma_ab = rho_a_grad.dot(rho_b_grad);
1740 const double sigma_bb = rho_b_grad.dot(rho_b_grad);
1744 Index owner = owner_atoms[pidx];
1746 throw std::runtime_error(
1747 "GridWeightGradientUKS: grid point has no owner atom set; the "
1748 "grid must be built with Vxc_Grid::GridSetup.");
1751 AddSSWWeightDerivative(points[pidx], owner, weight, rho * xc.
f_xc,
1752 atoms, Rij, grad_thread[thread_id]);
1757 if (!eptr_weight_uks) {
1758 eptr_weight_uks = std::current_exception();
1763 if (eptr_weight_uks) {
1764 std::rethrow_exception(eptr_weight_uks);
1767 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(natoms, 3);
1768 for (
Index t = 0; t < nthreads; ++t) {
1769 grad += grad_thread[t];
Container to hold Basisfunctions for all atoms.
const std::vector< Index > & getFuncPerAtom() const
Adds per-grid-box blocks into a set of AO matrices from inside an OpenMP loop over grid boxes.
void Finish()
sums the thread-local copies into the targets (call outside the loop)
void Add(const GridBox &box, const std::vector< const Eigen::MatrixXd * > &small)
const std::vector< Eigen::Vector3d > & getGridPoints() const
Eigen::MatrixXd ReadFromBigMatrix(const Eigen::MatrixXd &bigmatrix) const
void AddtoBigMatrix(Eigen::MatrixXd &bigmatrix, const Eigen::MatrixXd &smallmatrix) const
const std::vector< const AOShell * > & getShells() const
const std::vector< GridboxRange > & getAOranges() const
const std::vector< double > & getGridWeights() const
const std::vector< Index > & getOwnerAtoms() const
AOShell::AOValuesHessian CalcAOValuesHessian(const Eigen::Vector3d &point) const
AOShell::AOValues CalcAOValues(const Eigen::Vector3d &point) const
Eigen::MatrixXd & matrix()
conversion of functional string into integer
static constexpr Index kPointBlock
void EvaluateXCSpinBatch(Index n, const double *rho, const double *sigma, double *exc, double *vrho, double *vsigma) const
Eigen::MatrixXd GridWeightGradient(const Eigen::MatrixXd &density_matrix, const QMMolecule &atoms) const
void EvaluateVXCFXCBatch(Index n, const double *rho, const double *sigma, double *vrho, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
Eigen::MatrixXd PulayGradient(const Eigen::MatrixXd &density_matrix, const AOBasis &dftbasis) const
void EvaluateFXCSpinBatch(Index n, const double *rho, const double *sigma, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
XC_entry_spin EvaluateXCSpin(double rho_a, double rho_b, double sigma_aa, double sigma_ab, double sigma_bb) const
void EvaluateXCBatch(Index n, const double *rho, const double *sigma, double *exc, double *vrho, double *vsigma) const
FXCKernel PrepareFXC(const Eigen::MatrixXd &density_matrix) const
std::vector< Eigen::MatrixXd > ApplyFXC(const FXCKernel &kernel, const std::vector< Eigen::MatrixXd > &response_matrices) const
IntegrateFXC with a prepared kernel (the same results).
Eigen::MatrixXd PulayGradientUKS(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const AOBasis &dftbasis) const
XC_entry EvaluateXC(double rho, double sigma) const
Eigen::MatrixXd GradientResponse(const Eigen::MatrixXd &density_matrix, const Eigen::MatrixXd &direction, const QMMolecule &atoms) const
void EvaluateFXCBatch(Index n, const double *rho, const double *sigma, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
Mat_p_Energy IntegrateVXC(const Eigen::MatrixXd &density_matrix) const
void setXCfunctional(const std::string &functional)
std::vector< std::array< Eigen::MatrixXd, 2 > > IntegrateFXCSpin(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const std::vector< std::array< Eigen::MatrixXd, 2 > > &response) const
static double getExactExchange(const std::string &functional)
std::vector< Eigen::MatrixXd > IntegrateFXC(const Eigen::MatrixXd &density_matrix, const std::vector< Eigen::MatrixXd > &response_matrices) const
Eigen::MatrixXd GridWeightGradientUKS(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const QMMolecule &atoms) const
SpinResult IntegrateVXCSpin(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta) const
Charge transport classes.
Provides a means for comparing floating point numbers.
std::vector< Eigen::Matrix3d > hessians
Eigen::MatrixX3d derivatives
Eigen::MatrixX3d derivatives
Eigen::VectorXd v2rhosigma
std::vector< Index > points
Eigen::MatrixXd vxc_alpha