133 throw std::runtime_error(
134 "BSEExcitationGradient: needs the RI ground state (auxbasisset)");
137 throw std::runtime_error(
"BSEExcitationGradient: ECPs are not supported");
140 throw std::runtime_error(
141 "BSEExcitationGradient: libint2 lacks derivative integrals");
146 throw std::runtime_error(
147 "BSEExcitationGradient: closed-shell ground states only");
152 std::vector<std::pair<std::string, double>> phase_times;
153 auto phase_start = std::chrono::steady_clock::now();
154 auto phase = [&](
const std::string& name) {
155 const auto now = std::chrono::steady_clock::now();
156 phase_times.emplace_back(
157 name, std::chrono::duration<double>(now - phase_start).count());
163 const Index nmo = C.cols();
165 const Index nvirt = nmo - nocc;
166 const Index homo = nocc - 1;
167 const Eigen::MatrixXd C_occ = C.leftCols(nocc);
168 const Eigen::MatrixXd C_virt = C.rightCols(nvirt);
169 const Eigen::MatrixXd D0 = 2.0 * C_occ * C_occ.transpose();
175 throw std::runtime_error(
"BSEExcitationGradient: invalid windows");
180 const bool has_bse = in.
qp_levels.empty();
181 if ((has_bse && in.
X.size() != vt * ct) ||
182 (has_bse && in.
Y.size() != 0 && in.
Y.size() != vt * ct) ||
185 throw std::runtime_error(
"BSEExcitationGradient: input sizes");
190 throw std::runtime_error(
191 "BSEExcitationGradient: QP window must lie in the RPA window");
194 if (level < in.vmin || level > in.
cmax) {
195 throw std::runtime_error(
196 "BSEExcitationGradient: QP level outside the window");
201 const Index nw = hi - lo + 1;
202 const Eigen::MatrixXd Cw = C.middleCols(lo, nw);
204 const Index c0 = homo + 1 - lo;
208 const double kappa = in.
singlet ? 1.0 : 0.0;
211 Eigen::VectorXd eA = Eigen::VectorXd::Zero(nw);
213 Eigen::VectorXd eP = Eigen::VectorXd::Zero(nw);
223 if (es.
values.minCoeff() <= 0.0) {
224 throw std::runtime_error(
225 "BSEExcitationGradient: aux Coulomb metric not positive definite");
227 L = es.
vectors * es.
values.cwiseInverse().cwiseSqrt().asDiagonal() *
232 Eigen::MatrixXd Bmat(nw * nw, naux);
235 [&Cw, &Bmat, nw](
Index R,
const Eigen::MatrixXd&
I) {
236 Eigen::MatrixXd m = Cw.transpose() * (
I * Cw);
237 Bmat.col(R) = Eigen::Map<const Eigen::VectorXd>(m.data(), nw * nw);
239 RightMultiplyInPlace(Bmat,
L);
240 auto B = [&Bmat, nw](
Index P) {
241 return Eigen::Map<const Eigen::MatrixXd>(Bmat.col(
P).data(), nw, nw);
247 const bool full = has_bse && in.
Y.size() > 0;
248 const Eigen::MatrixXd Xvc =
249 has_bse ? Eigen::MatrixXd(
250 Eigen::Map<const Eigen::MatrixXd>(in.
X.data(), ct, vt)
252 : Eigen::MatrixXd::Zero(vt, ct);
253 const Eigen::MatrixXd Yvc =
254 full ? Eigen::MatrixXd(
255 Eigen::Map<const Eigen::MatrixXd>(in.
Y.data(), ct, vt)
257 : Eigen::MatrixXd::Zero(vt, ct);
258 const double norm = has_bse ? Xvc.squaredNorm() - Yvc.squaredNorm() : 1.0;
260 throw std::runtime_error(
"BSEExcitationGradient: X X - Y Y <= 0");
263 phase(
"three-index integrals B");
265 Eigen::MatrixXd gmat = Eigen::MatrixXd::Zero(nw * nw, naux);
266 auto g = [&gmat, nw](
Index P) {
267 return Eigen::Map<Eigen::MatrixXd>(gmat.col(
P).data(), nw, nw);
269 Eigen::VectorXd w = Eigen::VectorXd::Zero(nw);
272 Eigen::VectorXd wA = Eigen::VectorXd::Zero(nw);
273 double omega_qp = 0.0;
274 for (
Index v = 0; v < vt; ++v) {
275 for (
Index c = 0; c < ct; ++c) {
276 const double x2 = Xvc(v, c) * Xvc(v, c) + Yvc(v, c) * Yvc(v, c);
277 omega_qp += x2 * (eA(c0 + c) - eA(v0 + v));
284 const Eigen::MatrixXd XpY = Xvc + Yvc;
285 Eigen::VectorXd x(naux);
287 x(
P) = XpY.cwiseProduct(B(
P).block(v0, c0, vt, ct)).sum();
289 const double omega_x = 2.0 * kappa * x.squaredNorm();
291 g(
P).block(v0, c0, vt, ct) += 4.0 * kappa * x(
P) * XpY;
295 Eigen::MatrixXd Bkd(nk * nd, naux);
296 Eigen::VectorXd f(nk * nd);
297 Eigen::VectorXd fprime(nk * nd);
298 const double eta2 = in.
eta * in.
eta;
299 for (
Index d = 0; d < nd; ++d) {
300 for (
Index k = 0; k < nk; ++k) {
301 const double Delta = eP(c0 + d) - eP(k0 + k);
302 const double den = Delta * Delta + eta2;
303 f(k + nk * d) = 4.0 * Delta / den;
304 fprime(k + nk * d) = 4.0 * (eta2 - Delta * Delta) / (den * den);
309 for (
Index d = 0; d < nd; ++d) {
310 Bkd.col(
P).segment(nk * d, nk) = b.block(k0, c0 + d, nk, 1);
313 Eigen::MatrixXd epsilon = Bkd.transpose() * f.asDiagonal() * Bkd;
314 epsilon.diagonal().array() += 1.0;
324 double omega_d = 0.0;
328 Eigen::MatrixXd Bvv(vt * vt, naux);
329 Eigen::MatrixXd Ymat(vt * vt, naux);
330 Eigen::MatrixXd Zmat(ct * ct, naux);
333 const Eigen::MatrixXd bvv = b.block(v0, v0, vt, vt);
334 const Eigen::MatrixXd bcc = b.block(c0, c0, ct, ct);
335 Eigen::MatrixXd Yq = Xvc * bcc * Xvc.transpose();
336 Eigen::MatrixXd Zc = Xvc.transpose() * bvv * Xvc;
338 Yq += Yvc * bcc * Yvc.transpose();
339 Zc += Yvc.transpose() * bvv * Yvc;
341 Bvv.col(
P) = Eigen::Map<const Eigen::VectorXd>(bvv.data(), vt * vt);
342 Ymat.col(
P) = Eigen::Map<const Eigen::VectorXd>(Yq.data(), vt * vt);
343 Zmat.col(
P) = Eigen::Map<const Eigen::VectorXd>(Zc.data(), ct * ct);
345 Eigen::MatrixXd GammaW = Bvv.transpose() * Ymat;
347 const Eigen::MatrixXd MY = Ymat * M;
348 const Eigen::MatrixXd MZ = Zmat * M;
350 g(
P).block(v0, v0, vt, vt) -=
351 Eigen::Map<const Eigen::MatrixXd>(MY.col(
P).data(), vt, vt);
352 g(
P).block(c0, c0, ct, ct) -=
353 Eigen::Map<const Eigen::MatrixXd>(MZ.col(
P).data(), ct, ct);
361 Eigen::MatrixXd Bvc(vt * ct, naux);
362 Eigen::MatrixXd Tmat(vt * ct, naux);
363 Eigen::MatrixXd Tpmat(vt * ct, naux);
365 const Eigen::MatrixXd bvc = B(
P).block(v0, c0, vt, ct);
366 const Eigen::MatrixXd T = Xvc * bvc.transpose() * Yvc;
367 const Eigen::MatrixXd Tp = Yvc * bvc.transpose() * Xvc;
368 Bvc.col(
P) = Eigen::Map<const Eigen::VectorXd>(bvc.data(), vt * ct);
369 Tmat.col(
P) = Eigen::Map<const Eigen::VectorXd>(T.data(), vt * ct);
370 Tpmat.col(
P) = Eigen::Map<const Eigen::VectorXd>(Tp.data(), vt * ct);
372 GammaW += 2.0 * Bvc.transpose() * Tmat;
373 const Eigen::MatrixXd MT = (Tmat + Tpmat) * M;
375 g(
P).block(v0, c0, vt, ct) -=
376 2.0 * Eigen::Map<const Eigen::MatrixXd>(MT.col(
P).data(), vt, ct);
379 omega_d = -(M.cwiseProduct(GammaW)).sum();
380 GammaW = 0.5 * (GammaW + GammaW.transpose()).eval();
383 const Eigen::MatrixXd Gbar = M * GammaW * M;
384 const Eigen::MatrixXd GB = Bkd * Gbar;
386 for (
Index d = 0; d < nd; ++d) {
387 g(
P).block(k0, c0 + d, nk, 1) +=
389 f.segment(nk * d, nk).cwiseProduct(GB.col(
P).segment(nk * d, nk));
392 const Eigen::VectorXd bGb = Bkd.cwiseProduct(GB).rowwise().sum();
393 for (
Index d = 0; d < nd; ++d) {
394 for (
Index k = 0; k < nk; ++k) {
395 const double t = fprime(k + nk * d) * bGb(k + nk * d);
403 phase(
"BSE energy and partial derivatives");
409 result.
omega = (omega_qp + omega_x + omega_d) / norm;
412 Eigen::VectorXd u = Eigen::VectorXd::Zero(nmo);
415 auto downstream = [&](
bool last) -> Eigen::MatrixXd {
421 Eigen::MatrixXd grad;
423 Eigen::MatrixXd
G = gmat.transpose() * Bmat;
427 G = (0.5 * (
G +
G.transpose())).eval();
430 RightMultiplyInPlace(gmat,
L);
431 const Eigen::MatrixXd& gL = gmat;
435 (Eigen::Map<const Eigen::MatrixXd>(gL.col(R).data(), nw, nw) *
439 phase(
"integral derivatives of B");
442 Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(nmo, nmo);
444 Eigen::MatrixXd Qw = Eigen::MatrixXd::Zero(nmo, nw);
447 const Eigen::Map<const Eigen::MatrixXd> gLR(gL.col(R).data(), nw,
449 const Eigen::MatrixXd sLR = gLR + gLR.transpose();
450 const Eigen::MatrixXd t = C.transpose() * (
I * (Cw * sLR));
451#pragma omp critical(bsegradient_Q)
454 Q.middleCols(lo, nw) = Qw;
458 const bool has_u = u.cwiseAbs().maxCoeff() > 0.0;
460 const Eigen::MatrixXd Vxc_mo =
462 Q += 2.0 * Vxc_mo * u.asDiagonal();
464 Eigen::VectorXd wfull = Eigen::VectorXd::Zero(nmo);
465 wfull.segment(lo, nw) = w;
467 phase(
"orbital part Q");
469 const double kDegenerate = 1
e-8;
470 Eigen::MatrixXd Dmo = Eigen::MatrixXd::Zero(nmo, nmo);
471 Eigen::MatrixXd Wmo = Eigen::MatrixXd::Zero(nmo, nmo);
473 for (
Index r = first; r < first + n; ++r) {
474 for (
Index p = first; p < first + n; ++p) {
476 Dmo(p, p) = wfull(p);
477 Wmo(p, p) = 0.5 * Q(p, p) + wfull(p) * eps(p);
480 const double de = eps(p) - eps(r);
481 if (std::abs(de) < kDegenerate) {
482 Wmo(r, p) = 0.25 * (Q(r, p) + Q(p, r));
485 Dmo(r, p) = (Q(r, p) - Q(p, r)) / (2.0 * de);
486 Wmo(r, p) = (Q(r, p) * eps(p) - Q(p, r) * eps(r)) / (2.0 * de);
491 same_space(nocc, nvirt);
495 auto fock_response = [&](
const Eigen::MatrixXd& Xmo) {
496 const Eigen::MatrixXd Xao = C * Xmo * C.transpose();
498 G +=
ERIs_.CalculateERIs_3c(Xao);
500 G += 0.5 *
ScaHFX_ *
ERIs_.CalculateEXX_3c(Eigen::MatrixXd(0, 0), Xao);
502 return Eigen::MatrixXd(C.transpose() *
G * C);
508 const Eigen::MatrixXd Uao = C * u.asDiagonal() * C.transpose();
509 Gu = C.transpose() * vxc.
IntegrateFXC(D0, {Uao})[0] * C;
512 phase(
"Fock responses");
516 const Eigen::MatrixXd GD = fock_response(Dmo);
517 Eigen::MatrixXd Lvo = Q.block(nocc, 0, nvirt, nocc) -
518 Q.block(0, nocc, nocc, nvirt).transpose() +
519 4.0 * GD.block(nocc, 0, nvirt, nocc);
521 Lvo += 4.0 * Gu.block(nocc, 0, nvirt, nocc);
526 const Eigen::MatrixXd&
S = overlap.
Matrix();
527 Eigen::MatrixXd h = -(
S * C_virt * Lvo * C_occ.transpose() *
S);
528 h = (h + h.transpose()).eval();
533 Zvo = 0.5 * C_virt.transpose() *
S * dP[0] *
S * C_occ;
538 Eigen::MatrixXd Pmo = Dmo;
539 Pmo.block(nocc, 0, nvirt, nocc) = -0.5 * Zvo;
540 Pmo.block(0, nocc, nocc, nvirt) = -0.5 * Zvo.transpose();
542 const Eigen::MatrixXd GP = fock_response(Pmo);
543 Wmo.topLeftCorner(nocc, nocc) += 2.0 * GP.topLeftCorner(nocc, nocc);
545 Wmo.topLeftCorner(nocc, nocc) += 2.0 * Gu.topLeftCorner(nocc, nocc);
548 for (
Index i = 0; i < nocc; ++i) {
549 for (
Index a = 0; a < nvirt; ++a) {
550 const double wai = 0.5 * (Q(i, nocc + a) - Zvo(a, i) * eps(i));
551 Wmo(nocc + a, i) = wai;
552 Wmo(i, nocc + a) = wai;
555 const Eigen::MatrixXd Pao = C * Pmo * C.transpose();
556 const Eigen::MatrixXd Wao = C * Wmo * C.transpose();
558 phase(
"energy-weighted density");
561 grad += ContractAODerivatives(
564 phase(
"one-electron and overlap derivatives");
570 const double cutoff = 1
e-12 * es.
values.cwiseAbs().maxCoeff();
571 std::vector<Index> keep;
573 if (std::abs(es.
values(k)) > cutoff) {
577 Eigen::MatrixXd U(es.
vectors.rows(),
Index(keep.size()));
578 Eigen::VectorXd lambda(
Index(keep.size()));
579 for (
Index k = 0; k <
Index(keep.size()); ++k) {
580 U.col(k) = es.
vectors.col(keep[std::size_t(k)]);
581 lambda(k) = es.
values(keep[std::size_t(k)]);
586 phase(
"RI-J and RI-K");
591 const Eigen::MatrixXd Pxc = Pao + C * u.asDiagonal() * C.transpose();
602 std::vector<Eigen::MatrixXd> gradients;
603 auto run_targets = [&](
const std::function<void()>& qp_response) {
606 phase(
"QP-correction response");
607 gradients.push_back(downstream(
true));
610 for (std::size_t t = 0; t < in.
qp_levels.size(); ++t) {
612 gmat = Eigen::MatrixXd::Zero(nw * nw, naux);
616 std::vector<Index> partners;
617 const Index first = (level <= homo) ? in.
vmin : homo + 1;
618 const Index last = (level <= homo) ? homo : in.
cmax;
619 for (
Index j = first; j <= last; ++j) {
620 if (std::abs(eps(j) - eps(level)) < 1
e-6) {
621 partners.push_back(j);
624 for (
Index j : partners) {
625 wA(j - lo) = 1.0 / double(partners.size());
627 if (partners.size() > 1) {
629 <<
TimeStamp() <<
" QP level " << level <<
" is one of "
631 <<
" degenerate KS levels: gradient of their mean energy"
635 phase(
"QP-correction response");
636 gradients.push_back(downstream(t + 1 == in.
qp_levels.size()));
642 run_targets([&] { w += wA; });
671 std::function<Eigen::VectorXd(
const Eigen::VectorXd&,
bool)>;
678 auto finish = [&](
const Eigen::VectorXd& dsigma,
const ReversePass& reverse,
679 const Eigen::MatrixXd& poles) {
682 Eigen::VectorXd wR = w.segment(r0, nr);
684 Eigen::VectorXd wE = wA.segment(q0, nq);
685 for (
Index j = 0; j < nw; ++j) {
686 if (j >= q0 && j < q0 + nq) {
689 if (j < r0 || j >= r0 + nr) {
691 throw std::runtime_error(
692 "BSEExcitationGradient: BSE level outside the RPA window");
701 const Eigen::VectorXd v =
702 wE.cwiseQuotient((1.0 - dsigma.array()).matrix());
703 const Eigen::VectorXd rho = reverse(v,
true);
704 w.segment(r0, nr) += wR + rho;
713 auto rpa_energies_eps = [&](
const Eigen::VectorXd& c,
714 const Eigen::VectorXd&
e) {
715 Eigen::VectorXd R =
e.segment(in.
rpamin, nr);
716 R.segment(qr, nq) += c;
720 auto rpa_energies = [&](
const Eigen::VectorXd& c) {
721 return rpa_energies_eps(c, eps);
723 const Eigen::VectorXd c_ref =
725 const Eigen::VectorXd R_ref = rpa_energies(c_ref);
728 Eigen::MatrixXd Js = Eigen::MatrixXd::Zero(nr, nq);
729 const double dc = 1
e-7;
730 for (
Index q = 0; q < nq; ++q) {
731 Eigen::VectorXd cp = c_ref;
733 Eigen::VectorXd cm = c_ref;
735 Js.col(q) = (rpa_energies(cp) - rpa_energies(cm)) / (2.0 * dc);
737 Js.middleRows(qr, nq).setZero();
739 const Eigen::VectorXd Wbar =
740 wE + wR.segment(qr, nq) + Js.transpose() * wR;
747 const Eigen::VectorXd diag1 = (1.0 - dsigma.array()).matrix();
748 auto apply = [&](
const Eigen::VectorXd& vin) {
749 const Eigen::VectorXd rho = reverse(vin,
false);
750 return Eigen::VectorXd(vin.cwiseProduct(diag1) - rho.segment(qr, nq) -
751 Js.transpose() * rho);
753 Eigen::MatrixXd Mpre =
754 -poles.middleRows(qr, nq) - Js.transpose() * poles;
755 Mpre.diagonal() += diag1;
756 const Eigen::PartialPivLU<Eigen::MatrixXd> luM(Mpre);
757 const double bnorm = Wbar.norm();
758 const double tol = 1
e-11 * std::max(1.0, bnorm);
759 Eigen::VectorXd v = Eigen::VectorXd::Zero(nq);
760 Index iterations = 0;
761 bool converged = (bnorm == 0.0);
763 const Index max_krylov = std::min<Index>(nq, 60);
764 Eigen::MatrixXd Vk(nq, max_krylov + 1);
766 Eigen::MatrixXd::Zero(max_krylov + 1, max_krylov);
767 Vk.col(0) = Wbar / bnorm;
768 for (
Index j = 0; j < max_krylov; ++j) {
769 Eigen::VectorXd z = apply(luM.solve(Vk.col(j)));
770 for (
Index pass = 0; pass < 2; ++pass) {
771 for (
Index i = 0; i <= j; ++i) {
772 const double hij = Vk.col(i).dot(z);
774 z -= hij * Vk.col(i);
777 Hk(j + 1, j) = z.norm();
780 Eigen::VectorXd rhs = Eigen::VectorXd::Zero(j + 2);
782 const Eigen::MatrixXd Hj = Hk.topLeftCorner(j + 2, j + 1);
783 const Eigen::VectorXd y = Hj.colPivHouseholderQr().solve(rhs);
784 const double res = (rhs - Hj * y).norm();
785 if (res < tol || Hk(j + 1, j) < 1
e-14 * bnorm) {
786 v = luM.solve(Vk.leftCols(j + 1) * y);
790 Vk.col(j + 1) = z / Hk(j + 1, j);
794 (apply(v) - Wbar).norm() > 1
e-8 * std::max(1.0, bnorm)) {
799 <<
TimeStamp() <<
" evGW adjoint: GMRES converged in "
800 << iterations <<
" iterations" << std::flush;
803 <<
TimeStamp() <<
" evGW adjoint: GMRES did not converge in "
804 << iterations <<
" iterations, building the matrix" << std::flush;
805 Eigen::MatrixXd A(nq, nq);
806 for (
Index q = 0; q < nq; ++q) {
807 Eigen::VectorXd
e = Eigen::VectorXd::Zero(nq);
811 const Eigen::PartialPivLU<Eigen::MatrixXd> lu(A);
813 if ((A * v - Wbar).cwiseAbs().maxCoeff() >
814 1
e-8 * std::max(1.0, Wbar.cwiseAbs().maxCoeff())) {
815 throw std::runtime_error(
816 "BSEExcitationGradient: the evGW adjoint equation is "
820 const Eigen::VectorXd rho = reverse(v,
true);
823 Eigen::VectorXd wRo = wR + rho;
824 wRo.segment(qr, nq).setZero();
825 Eigen::MatrixXd K = Eigen::MatrixXd::Zero(nr, nr);
826 const double de = 1
e-7;
827 for (
Index k = 0; k < nr; ++k) {
828 Eigen::VectorXd ep = eps;
830 Eigen::VectorXd em = eps;
834 K.col(k) = (rpa_energies_eps(cp, ep) - rpa_energies_eps(cm, em)) /
837 w.segment(r0, nr) += K.transpose() * wRo;
844 const Eigen::MatrixXd& Phi = es0.
vectors;
845 const Eigen::VectorXd& lam = es0.
values;
846 Eigen::VectorXd fi(nk * nd);
847 Eigen::VectorXd fiprime(nk * nd);
848 for (
Index d = 0; d < nd; ++d) {
849 for (
Index k = 0; k < nk; ++k) {
850 const double Delta = eP(c0 + d) - eP(k0 + k);
851 const double den = Delta * Delta + nu2;
852 fi(k + nk * d) = 4.0 * Delta / den;
853 fiprime(k + nk * d) = 4.0 * (nu2 - Delta * Delta) / (den * den);
856 Eigen::MatrixXd epsi = Bkd.transpose() * fi.asDiagonal() * Bkd;
857 epsi.diagonal().array() += 1.0;
858 const Eigen::MatrixXd EiPhi =
InverseSPD(epsi) * Phi;
860 Eigen::VectorXd wppm = Eigen::VectorXd::Zero(naux);
861 Eigen::VectorXd Om = Eigen::VectorXd::Zero(naux);
862 Eigen::VectorXd a = Eigen::VectorXd::Zero(naux);
863 Eigen::VectorXd nom = Eigen::VectorXd::Zero(naux);
864 Eigen::VectorXd sgn = Eigen::VectorXd::Ones(naux);
865 for (
Index i = 0; i < naux; ++i) {
866 const double wi = 1.0 - 1.0 / lam(i);
871 nom(i) = Phi.col(i).dot(EiPhi.col(i)) - 1.0;
872 const double frac = -nom(i) / (nom(i) + wi) * nu2;
873 sgn(i) = (frac < 0.0) ? -1.0 : 1.0;
874 Om(i) = std::sqrt(std::abs(frac));
875 a(i) = 0.5 * wi * Om(i);
877 const Eigen::VectorXd epole = eP.segment(r0, nr);
878 Eigen::VectorXd s_n = -Eigen::VectorXd::Ones(nr);
879 s_n.head(nk).setOnes();
881 const Eigen::MatrixXd Vxc_mo =
886 auto gatherB = [&](
Index p) {
887 Eigen::MatrixXd Bp(nr, naux);
888 for (
Index n = 0; n < nr; ++n) {
889 Bp.row(n) = Bmat.row((q0 + p) + nw * (r0 + n));
893 auto poleFactors = [&](
Index p, Eigen::ArrayXXd&
H, Eigen::ArrayXXd& Hp) {
897 for (
Index i = 0; i < naux; ++i) {
898 for (
Index n = 0; n < nr; ++n) {
899 const double t = omega_p - epole(n) + s_n(n) * Om(i);
900 const double den = t * t + seta2;
902 Hp(n, i) = (seta2 - t * t) / (den * den);
906 std::vector<Eigen::MatrixXd> Bts(
static_cast<std::size_t
>(nq));
907 Eigen::VectorXd dsigma(nq);
908 Eigen::MatrixXd poles(nr, nq);
909 for (
Index p = 0; p < nq; ++p) {
912 const Eigen::MatrixXd Bp = gatherB(p);
913 Bts[std::size_t(p)] = Bp * Phi;
914 Eigen::ArrayXXd
H, Hp;
915 poleFactors(p,
H, Hp);
916 const Eigen::ArrayXXd aBt2 =
917 Bts[std::size_t(p)].array().square().rowwise() *
918 a.transpose().array();
919 const double sigma_c = (aBt2 *
H).sum();
920 dsigma(p) = (aBt2 * Hp).sum();
921 poles.col(p) = -(aBt2 * Hp).rowwise().sum().matrix();
922 const double sigma_x = -(1.0 - ax) * Bp.topRows(nk).squaredNorm();
923 const double e_rebuilt =
924 eps(pabs) + sigma_x - Vxc_mo(pabs, pabs) + sigma_c;
932 auto reverse = [&](
const Eigen::VectorXd& vq,
bool accumulate) {
933 Eigen::VectorXd rho = Eigen::VectorXd::Zero(nr);
934 Eigen::VectorXd gOm = Eigen::VectorXd::Zero(naux);
935 Eigen::VectorXd gw = Eigen::VectorXd::Zero(naux);
936 Eigen::MatrixXd GPhi = Eigen::MatrixXd::Zero(naux, naux);
937 for (
Index p = 0; p < nq; ++p) {
938 const double v = vq(p);
942 const Eigen::MatrixXd Bp = gatherB(p);
943 const Eigen::MatrixXd& Bt = Bts[std::size_t(p)];
944 Eigen::ArrayXXd
H, Hp;
945 poleFactors(p,
H, Hp);
946 const Eigen::ArrayXXd Bt2 = Bt.array().square();
947 const Eigen::ArrayXXd aBt2 = Bt2.rowwise() * a.transpose().array();
948 const Eigen::ArrayXXd BtH = Bt.array() *
H;
949 const Eigen::MatrixXd GBt =
950 (2.0 * v) * (BtH.rowwise() * a.transpose().array()).matrix();
953 u(in.
qpmin + p) -= v;
954 const Eigen::MatrixXd gBp = GBt * Phi.transpose();
955 for (
Index n = 0; n < nr; ++n) {
957 double gpn = gBp(n,
P);
959 gpn += -2.0 * (1.0 - ax) * v * Bp(n,
P);
961 g(
P)(q0 + p, r0 + n) += gpn;
965 GPhi.noalias() += Bp.transpose() * GBt;
966 rho += v * poles.col(p);
967 for (
Index i = 0; i < naux; ++i) {
968 if (wppm(i) == 0.0) {
973 for (
Index n = 0; n < nr; ++n) {
974 sH += Bt2(n, i) *
H(n, i);
975 sHp += Bt2(n, i) * s_n(n) * Hp(n, i);
977 gOm(i) += v * (0.5 * wppm(i) * sH + a(i) * sHp);
978 gw(i) += v * 0.5 * Om(i) * sH;
982 Eigen::VectorXd gy = Eigen::VectorXd::Zero(naux);
983 Eigen::VectorXd glam = Eigen::VectorXd::Zero(naux);
984 for (
Index i = 0; i < naux; ++i) {
985 if (wppm(i) == 0.0) {
988 const double d2 = (nom(i) + wppm(i)) * (nom(i) + wppm(i));
989 const double dOm_dfrac = sgn(i) / (2.0 * Om(i));
990 gy(i) = gOm(i) * dOm_dfrac * (-nu2 * wppm(i) / d2);
991 const double gwi = gw(i) + gOm(i) * dOm_dfrac * (nu2 * nom(i) / d2);
992 glam(i) = gwi / (lam(i) * lam(i));
993 GPhi.col(i) += 2.0 * gy(i) * EiPhi.col(i);
995 const Eigen::MatrixXd Gepsi =
996 -(EiPhi * gy.asDiagonal() * EiPhi.transpose());
997 Eigen::MatrixXd Mx = Phi.transpose() * GPhi;
998 for (
Index j = 0; j < naux; ++j) {
999 for (
Index i = 0; i < naux; ++i) {
1004 const double dl = lam(j) - lam(i);
1005 Mx(i, j) = (std::abs(dl) > 1
e-12) ? Mx(i, j) / dl : 0.0;
1008 Eigen::MatrixXd Geps0 = Phi * Mx * Phi.transpose();
1009 Geps0 = (0.5 * (Geps0 + Geps0.transpose())).eval();
1011 const Eigen::MatrixXd GB0 = Bkd * Geps0;
1012 const Eigen::MatrixXd GBi = Bkd * Gepsi;
1015 for (
Index d = 0; d < nd; ++d) {
1016 g(
P).block(k0, c0 + d, nk, 1) +=
1017 2.0 * (f.segment(nk * d, nk)
1018 .cwiseProduct(GB0.col(
P).segment(nk * d, nk)) +
1019 fi.segment(nk * d, nk)
1020 .cwiseProduct(GBi.col(
P).segment(nk * d, nk)));
1024 const Eigen::VectorXd bGb0 = Bkd.cwiseProduct(GB0).rowwise().sum();
1025 const Eigen::VectorXd bGbi = Bkd.cwiseProduct(GBi).rowwise().sum();
1026 for (
Index d = 0; d < nd; ++d) {
1027 for (
Index k = 0; k < nk; ++k) {
1028 const Index kd = k + nk * d;
1029 const double t = fprime(kd) * bGb0(kd) + fiprime(kd) * bGbi(kd);
1037 run_targets([&] { finish(dsigma, reverse, poles); });
1050 Eigen::VectorXd Delta(
S);
1051 for (
Index d = 0; d < nd; ++d) {
1052 for (
Index k = 0; k < nk; ++k) {
1053 Delta(k + nk * d) = eP(c0 + d) - eP(k0 + k);
1056 const Eigen::VectorXd sD = Delta.cwiseSqrt();
1057 Eigen::MatrixXd Cm = 4.0 * Bkd * Bkd.transpose();
1058 Cm.diagonal() += Delta;
1059 Cm = (sD.asDiagonal() * Cm * sD.asDiagonal()).eval();
1061 const Eigen::MatrixXd&
F = esC.
vectors;
1062 const Eigen::VectorXd& lamC = esC.
values;
1063 if (lamC.minCoeff() <= 0.0) {
1064 throw std::runtime_error(
1065 "BSEExcitationGradient: non-positive RPA eigenvalue");
1067 const Eigen::ArrayXd OmC = lamC.array().sqrt();
1070 Eigen::MatrixXd invdl(
S,
S);
1071 std::vector<std::pair<Index, Index>> degenerate;
1072 for (
Index t = 0; t <
S; ++t) {
1073 for (
Index s2 = 0; s2 <
S; ++s2) {
1074 const double dl = lamC(s2) - lamC(t);
1075 if (s2 != t && std::abs(dl) > 1
e-10 * std::max(1.0, lamC(s2))) {
1076 invdl(s2, t) = 1.0 / dl;
1080 degenerate.emplace_back(s2, t);
1085 const Eigen::VectorXd epole = eP.segment(r0, nr);
1086 Eigen::ArrayXd s_n = -Eigen::ArrayXd::Ones(nr);
1087 s_n.head(nk).setOnes();
1088 const Eigen::MatrixXd Vxc_mo =
1094 auto gatherB = [&](
Index p) {
1095 Eigen::MatrixXd Bp(nr, naux);
1096 for (
Index n = 0; n < nr; ++n) {
1097 Bp.row(n) = Bmat.row((q0 + p) + nw * (r0 + n));
1102 Eigen::ArrayXXd phi, dphi, hp;
1104 auto poleTerms = [&](
Index p) {
1107 pt.phi.resize(nr,
S);
1108 pt.dphi.resize(nr,
S);
1109 pt.hp.resize(nr,
S);
1110 for (
Index s2 = 0; s2 <
S; ++s2) {
1111 const double om = OmC(s2);
1112 for (
Index n = 0; n < nr; ++n) {
1113 const double t = omega_p - epole(n) + s_n(n) * om;
1114 const double den = t * t + seta2;
1115 const double h = t / den;
1116 const double hp = (seta2 - t * t) / (den * den);
1117 pt.phi(n, s2) = h / om;
1118 pt.hp(n, s2) = hp / om;
1120 -h / (2.0 * om * om * om) + s_n(n) * hp / (2.0 * om * om);
1125 std::vector<Eigen::MatrixXd> Aps(
static_cast<std::size_t
>(nq));
1126 Eigen::VectorXd dsigma(nq);
1127 Eigen::MatrixXd poles(nr, nq);
1128 for (
Index p = 0; p < nq; ++p) {
1131 const Eigen::MatrixXd Bp = gatherB(p);
1132 Aps[std::size_t(p)] =
1133 ((Bp * Bkd.transpose()) * sD.asDiagonal()) *
F;
1134 const PoleTerms pt = poleTerms(p);
1135 const Eigen::ArrayXXd A2 = Aps[std::size_t(p)].array().square();
1136 const double sigma_c = 2.0 * (A2 * pt.phi).sum();
1137 dsigma(p) = 2.0 * (A2 * pt.hp).sum();
1138 poles.col(p) = -2.0 * (A2 * pt.hp).rowwise().sum().matrix();
1139 const double sigma_x = -(1.0 - ax) * Bp.topRows(nk).squaredNorm();
1140 const double e_rebuilt =
1141 eps(pabs) + sigma_x - Vxc_mo(pabs, pabs) + sigma_c;
1146 auto reverse = [&](
const Eigen::VectorXd& vq,
bool accumulate) {
1147 Eigen::VectorXd rho = Eigen::VectorXd::Zero(nr);
1148 Eigen::MatrixXd Gt = Eigen::MatrixXd::Zero(
S,
S);
1149 Eigen::MatrixXd gB = Eigen::MatrixXd::Zero(
S, naux);
1150 Eigen::VectorXd gDelta = Eigen::VectorXd::Zero(
S);
1151 for (
Index p = 0; p < nq; ++p) {
1152 const double v = vq(p);
1156 const Eigen::MatrixXd Bp = gatherB(p);
1157 const Eigen::MatrixXd Kraw = Bp * Bkd.transpose();
1158 const Eigen::MatrixXd& A = Aps[std::size_t(p)];
1159 const PoleTerms pt = poleTerms(p);
1160 const Eigen::ArrayXXd A2 = A.array().square();
1162 const Eigen::MatrixXd Aphi = (A.array() * pt.phi).matrix();
1164 (2.0 * v) * ((Aphi.transpose() * A - A.transpose() * Aphi)
1165 .cwiseProduct(invdl));
1166 Gt.diagonal() += (2.0 * v) * (A2 * pt.dphi).colwise().sum().matrix();
1167 for (
const auto& st : degenerate) {
1168 Gt(st.first, st.second) +=
1170 (A.col(st.first).array() * A.col(st.second).array() *
1171 pt.dphi.col(st.first))
1175 rho += v * poles.col(p);
1177 const Eigen::MatrixXd gK =
1178 ((4.0 * v) * (A.array() * pt.phi)).matrix() *
F.transpose();
1179 gDelta += (gK.array() * Kraw.array())
1184 .cwiseQuotient(2.0 * sD);
1185 const Eigen::MatrixXd gKraw = gK * sD.asDiagonal();
1186 gB.noalias() += gKraw.transpose() * Bp;
1189 u(in.
qpmin + p) -= v;
1190 const Eigen::MatrixXd gBp = gKraw * Bkd;
1191 for (
Index n = 0; n < nr; ++n) {
1193 double gpn = gBp(n,
P);
1195 gpn += -2.0 * (1.0 - ax) * v * Bp(n,
P);
1197 g(
P)(q0 + p, r0 + n) += gpn;
1203 Eigen::MatrixXd GC =
F * Gt *
F.transpose();
1204 GC = (0.5 * (GC + GC.transpose())).eval();
1205 gDelta += (GC.cwiseProduct(Cm)).rowwise().sum().cwiseQuotient(Delta) +
1206 GC.diagonal().cwiseProduct(Delta);
1207 gB.noalias() += 8.0 * (sD.asDiagonal() * GC * sD.asDiagonal()) * Bkd;
1210 for (
Index d = 0; d < nd; ++d) {
1211 g(
P).block(k0, c0 + d, nk, 1) += gB.col(
P).segment(nk * d, nk);
1215 for (
Index d = 0; d < nd; ++d) {
1216 for (
Index k = 0; k < nk; ++k) {
1217 const double t = gDelta(k + nk * d);
1225 run_targets([&] { finish(dsigma, reverse, poles); });
1227 throw std::runtime_error(
1228 "BSEExcitationGradient: QP response only for the ppm and exact "
1235 std::vector<std::pair<std::string, double>> summed;
1237 for (
const auto& pt : phase_times) {
1239 auto it = std::find_if(
1240 summed.begin(), summed.end(),
1241 [&pt](
const auto& entry) { return entry.first == pt.first; });
1242 if (it == summed.end()) {
1243 summed.push_back(pt);
1245 it->second += pt.second;
1248 phase_times = summed;
1250 <<
TimeStamp() <<
" BSE gradient timing (s), total " << total
1252 for (
const auto& pt : phase_times) {
1254 <<
" " << pt.first <<
": " << pt.second << std::flush;