votca 2026-dev
Loading...
Searching...
No Matches
bsegradient.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Analytic nuclear gradient of a BSE excitation energy (TDA or full) with
21// static RPA screening and fixed (geometry-independent) QP corrections.
22//
23// Notation. p, q, r: MOs; i, j, k: occupied, a, b: virtual (all MOs of the
24// closed-shell KS reference); v, c: occupied/virtual levels of the BSE window
25// [vmin, cmax]; k, d: occupied/virtual levels of the RPA window
26// [rpamin, rpamax]. B^P_pq = sum_R L_RP (R|pq) with L L^T = V^-1 (here
27// L = V^-1/2; the energy is invariant under orthogonal rotations of the aux
28// index, so VOTCA's Pseudo_InvSqrt_GWBSE frame gives the same Omega).
29//
30// Energy, written here for the TDA (X normalized, kappa = 1 for singlets, 0
31// for triplets; the full BSE adds Y, see the amplitude section below):
32// Omega = sum_vc X_vc^2 (e_c - e_v) + 2 kappa |x|^2
33// - sum_{vc,v'c'} X_vc X_v'c' B_vv'^T M B_cc',
34// x^P = sum_vc X_vc B^P_vc, M = eps^-1,
35// eps = 1 + sum_kd f(Delta_kd) b_kd b_kd^T, f(D) = 4 D / (D^2 + eta^2),
36// Delta_kd = e'_d - e'_k,
37// with e_p = eps_p + a_p (BSE) and e'_p = eps_p + pi_p (RPA), a and pi fixed.
38// By Hellmann-Feynman dOmega = X^T dH X, so Omega depends on the geometry
39// through B (integrals and MO coefficients) and the KS energies eps_p:
40// dOmega = sum_P g^P . dB^P + sum_p w_p deps_p,
41// g^P = dOmega/dB^P (exchange, direct, and screening response through M),
42// w_p = dOmega/deps_p (diagonal of the BSE difference density and of the
43// screening response density, cf. Holzer, arXiv:2611.12617).
44//
45// dB^P has an integral part (three-centre and metric derivatives at fixed
46// MO coefficients, ContractThreeCenterDerivatives and
47// ContractCoulombMetricDerivatives with
48// Gamma^R = C (sum_P L_RP g^P) C^T and G = -1/2 L (g . B)_sym L^T) and an
49// orbital part sum_rp U_rp Q_rp with Q_rp = sum_{P,q} B^P_rq (g^P_pq +
50// g^P_qp) for the MO response C -> C (1 + U). The canonical KS orbitals
51// respond as
52// same space, r != p: U_rp = (F'_rp - eps_p S_rp) / (eps_p - eps_r)
53// diagonal: U_pp = -S_pp / 2, deps_p = F'_pp - eps_p S_pp
54// occupied-virtual: U_ia = -S_ia - U_ai, U_ai from coupled-perturbed KS
55// with S_pq = C_p^T dS C_q, F' = F^(R) + G[dD], F^(R) the derivative of the
56// Fock matrix at fixed AO density and G the closed-shell Fock response
57// (J + 1/2 a_x K + f_xc) to the density change dD (dD_ai = dD_ia = 2 U_ai,
58// dD_jk = -2 S_jk). Collecting terms:
59// D_rp = (Q_rp - Q_pr) / (2 (eps_p - eps_r)) same space, r != p
60// D_pp = w_p
61// L_ai = Q_ai - Q_ia + 4 G_ai[D], (A + B) Z = L (Z-vector)
62// Ptilde = D (occ-occ, virt-virt), Ptilde_ai = Ptilde_ia = -Z_ai / 2
63// W_rp = (Q_rp eps_p - Q_pr eps_r) / (2 (eps_p - eps_r)) same space
64// W_pp = Q_pp / 2 + w_p eps_p, W_jk += 2 G_jk[Ptilde],
65// W_ai = W_ia = (Q_ia - Z_ai eps_i) / 2
66// and
67// dOmega/dR = (integral part) + sum Ptilde_AO dF/dR|_D - sum W_AO dS/dR.
68// The Fock derivative at fixed density contracted with Ptilde is split into
69// the one-electron part, RI-J and RI-K (exact, both are bilinear) and XC
70// (d/dt of the ground-state XC gradient at D + t Ptilde, analytic in
71// Vxc_Potential::GradientResponse), which contains the f_xc term of the moving
72// basis functions and grid. No g_xc is needed: the BSE kernel has no XC.
73//
74// Degenerate KS levels within a space get D_rp = 0 (the antisymmetric part
75// of Q vanishes for symmetry-related partners, not for accidental
76// degeneracies).
77
78// Local VOTCA includes
79#include "votca/xtp/aomatrix.h"
80#include "votca/xtp/dftengine.h"
82#include "votca/xtp/eigen.h"
83#include "votca/xtp/orbitals.h"
87#include <algorithm>
88#include <chrono>
89#include <functional>
90
91namespace votca {
92namespace xtp {
93
94// Defined in libint2_derivative_calls.cc.
95using AOMatrixDerivative = std::array<Eigen::MatrixXd, 3>;
96std::vector<AOMatrixDerivative> ComputeOverlapDerivatives(
97 const AOBasis& aobasis);
98std::vector<AOMatrixDerivative> ComputeKineticDerivatives(
99 const AOBasis& aobasis);
100std::vector<AOMatrixDerivative> ComputeNuclearAttractionDerivatives(
101 const AOBasis& aobasis, const QMMolecule& mol);
103
104namespace {
105
106Eigen::MatrixXd ContractAODerivatives(const std::vector<AOMatrixDerivative>& d,
107 const Eigen::MatrixXd& P) {
108 Eigen::MatrixXd grad = Eigen::MatrixXd::Zero(Index(d.size()), 3);
109 for (Index a = 0; a < Index(d.size()); ++a) {
110 for (Index xyz = 0; xyz < 3; ++xyz) {
111 grad(a, xyz) = P.cwiseProduct(d[a][xyz]).sum();
112 }
113 }
114 return grad;
115}
116
117// A <- A B for a square B, by blocks of rows (scratch of one block)
118void RightMultiplyInPlace(Eigen::MatrixXd& A, const Eigen::MatrixXd& B) {
119 const Index rows = A.rows();
120 const Index block = std::max<Index>(1, std::min<Index>(rows, 4096));
121 for (Index r0 = 0; r0 < rows; r0 += block) {
122 const Index n = std::min(block, rows - r0);
123 Eigen::MatrixXd tmp = A.middleRows(r0, n) * B;
124 A.middleRows(r0, n) = tmp;
125 }
126}
127
128} // namespace
129
131 const Orbitals& orb, const BSEGradientInput& in) {
132 if (auxbasis_name_.empty()) {
133 throw std::runtime_error(
134 "BSEExcitationGradient: needs the RI ground state (auxbasisset)");
135 }
136 if (!ecp_name_.empty()) {
137 throw std::runtime_error("BSEExcitationGradient: ECPs are not supported");
138 }
140 throw std::runtime_error(
141 "BSEExcitationGradient: libint2 lacks derivative integrals");
142 }
143 Orbitals work = orb;
144 Prepare(work);
146 throw std::runtime_error(
147 "BSEExcitationGradient: closed-shell ground states only");
148 }
149 const QMMolecule& mol = work.QMAtoms();
150 const Vxc_Potential<Vxc_Grid> vxc = SetupVxc(mol);
151 // wall time of the phases, reported at the end
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());
158 phase_start = now;
159 };
160
161 const Eigen::MatrixXd& C = orb.MOs().eigenvectors();
162 const Eigen::VectorXd& eps = orb.MOs().eigenvalues();
163 const Index nmo = C.cols();
164 const Index nocc = num_docc_;
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();
170
171 // windows
172 if (in.vmin < 0 || in.vmin > homo || in.cmax <= homo || in.cmax >= nmo ||
173 in.rpamin < 0 || in.rpamin > homo || in.rpamax <= homo ||
174 in.rpamax >= nmo) {
175 throw std::runtime_error("BSEExcitationGradient: invalid windows");
176 }
177 const Index vt = homo - in.vmin + 1;
178 const Index ct = in.cmax - homo;
179 // targets: the BSE excitation energy, or the energies of QP levels
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) ||
183 in.energies_bse.size() != in.cmax - in.vmin + 1 ||
184 in.energies_rpa.size() != in.rpamax - in.rpamin + 1) {
185 throw std::runtime_error("BSEExcitationGradient: input sizes");
186 }
187 if (in.qp_response &&
188 (in.qpmin < in.rpamin || in.qpmax > in.rpamax || in.qpmin > in.qpmax ||
189 in.qp_energies.size() != in.qpmax - in.qpmin + 1)) {
190 throw std::runtime_error(
191 "BSEExcitationGradient: QP window must lie in the RPA window");
192 }
193 for (Index level : in.qp_levels) {
194 if (level < in.vmin || level > in.cmax) {
195 throw std::runtime_error(
196 "BSEExcitationGradient: QP level outside the window");
197 }
198 }
199 const Index lo = std::min(in.vmin, in.rpamin);
200 const Index hi = std::max(in.cmax, in.rpamax);
201 const Index nw = hi - lo + 1;
202 const Eigen::MatrixXd Cw = C.middleCols(lo, nw);
203 const Index v0 = in.vmin - lo; // window offsets
204 const Index c0 = homo + 1 - lo; // first virtual
205 const Index k0 = in.rpamin - lo;
206 const Index nk = homo - in.rpamin + 1;
207 const Index nd = in.rpamax - homo;
208 const double kappa = in.singlet ? 1.0 : 0.0;
209
210 // energies used in the Hamiltonian, by window position
211 Eigen::VectorXd eA = Eigen::VectorXd::Zero(nw);
212 eA.segment(v0, in.cmax - in.vmin + 1) = in.energies_bse;
213 Eigen::VectorXd eP = Eigen::VectorXd::Zero(nw);
214 eP.segment(k0, in.rpamax - in.rpamin + 1) = in.energies_rpa;
215
216 // ---- B^P_pq over the window, L = V^-1/2
217 AOCoulomb aocoulomb;
218 aocoulomb.Fill(auxbasis_);
219 const Index naux = auxbasis_.AOBasisSize();
220 Eigen::MatrixXd L;
221 {
222 const SymmetricEigenSystem es = SymmetricEigen(aocoulomb.Matrix());
223 if (es.values.minCoeff() <= 0.0) {
224 throw std::runtime_error(
225 "BSEExcitationGradient: aux Coulomb metric not positive definite");
226 }
227 L = es.vectors * es.values.cwiseInverse().cwiseSqrt().asDiagonal() *
228 es.vectors.transpose();
229 }
230 // (R|pq) first, then B = J L in place by blocks of rows: one array of
231 // nw^2 x Naux (the size of the GW-BSE integrals) instead of two
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);
238 });
239 RightMultiplyInPlace(Bmat, L);
240 auto B = [&Bmat, nw](Index P) {
241 return Eigen::Map<const Eigen::MatrixXd>(Bmat.col(P).data(), nw, nw);
242 };
243
244 // X (and Y for the full BSE) as (v, c); the energy functional
245 // Omega = (X A X + Y A Y + 2 X B Y) / (X X - Y Y)
246 // is stationary in X, Y, so dOmega = (X dA X + Y dA Y + 2 X dB Y) / N
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)
251 .transpose())
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)
256 .transpose())
257 : Eigen::MatrixXd::Zero(vt, ct);
258 const double norm = has_bse ? Xvc.squaredNorm() - Yvc.squaredNorm() : 1.0;
259 if (norm <= 0.0) {
260 throw std::runtime_error("BSEExcitationGradient: X X - Y Y <= 0");
261 }
262
263 phase("three-index integrals B");
264 // ---- energy and its partial derivatives g^P (nw x nw per P), w
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);
268 };
269 Eigen::VectorXd w = Eigen::VectorXd::Zero(nw); // dOmega/deps, window
270
271 // dOmega/de of the BSE energies (wA) and of the RPA energies (w)
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));
278 wA(c0 + c) += x2;
279 wA(v0 + v) -= x2;
280 }
281 }
282
283 // exchange, A and B: 2 kappa |x + y|^2
284 const Eigen::MatrixXd XpY = Xvc + Yvc;
285 Eigen::VectorXd x(naux);
286 for (Index P = 0; P < naux; ++P) {
287 x(P) = XpY.cwiseProduct(B(P).block(v0, c0, vt, ct)).sum();
288 }
289 const double omega_x = 2.0 * kappa * x.squaredNorm();
290 for (Index P = 0; P < naux; ++P) {
291 g(P).block(v0, c0, vt, ct) += 4.0 * kappa * x(P) * XpY;
292 }
293
294 // static RPA screening
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);
305 }
306 }
307 for (Index P = 0; P < naux; ++P) {
308 const auto b = B(P);
309 for (Index d = 0; d < nd; ++d) {
310 Bkd.col(P).segment(nk * d, nk) = b.block(k0, c0 + d, nk, 1);
311 }
312 }
313 Eigen::MatrixXd epsilon = Bkd.transpose() * f.asDiagonal() * Bkd;
314 epsilon.diagonal().array() += 1.0;
315 Eigen::MatrixXd M;
316 {
318 for (Index i = 0; i < es.values.size(); ++i) {
319 es.values(i) = (es.values(i) > 1e-8) ? 1.0 / es.values(i) : 0.0;
320 }
321 M = es.vectors * es.values.asDiagonal() * es.vectors.transpose();
322 }
323
324 double omega_d = 0.0;
325 if (has_bse) {
326 // direct term of A: Y^Q = X B^Q_cc X^T + Y B^Q_cc Y^T (v x v),
327 // Z^P = X^T B^P_vv X + Y^T B^P_vv Y (c x c)
328 Eigen::MatrixXd Bvv(vt * vt, naux);
329 Eigen::MatrixXd Ymat(vt * vt, naux);
330 Eigen::MatrixXd Zmat(ct * ct, naux);
331 for (Index P = 0; P < naux; ++P) {
332 const auto b = B(P);
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;
337 if (full) {
338 Yq += Yvc * bcc * Yvc.transpose();
339 Zc += Yvc.transpose() * bvv * Yvc;
340 }
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);
344 }
345 Eigen::MatrixXd GammaW = Bvv.transpose() * Ymat; // G_PQ = B^P . Y^Q
346 {
347 const Eigen::MatrixXd MY = Ymat * M;
348 const Eigen::MatrixXd MZ = Zmat * M;
349 for (Index P = 0; P < naux; ++P) {
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);
354 }
355 }
356 // direct term of B (full BSE): 2 X Hd2 Y with
357 // Hd2(v1c1, v2c2) = -B_{v1c2}^T M B_{v2c1}, i.e.
358 // -2 sum_PQ M_PQ B^P_vc . T^Q, T^Q = X (B^Q_vc)^T Y (v x c)
359 // and dT^Q/dB^Q gives T'^P = Y (B^P_vc)^T X.
360 if (full) {
361 Eigen::MatrixXd Bvc(vt * ct, naux);
362 Eigen::MatrixXd Tmat(vt * ct, naux);
363 Eigen::MatrixXd Tpmat(vt * ct, naux);
364 for (Index P = 0; P < naux; ++P) {
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);
371 }
372 GammaW += 2.0 * Bvc.transpose() * Tmat;
373 const Eigen::MatrixXd MT = (Tmat + Tpmat) * M;
374 for (Index P = 0; P < naux; ++P) {
375 g(P).block(v0, c0, vt, ct) -=
376 2.0 * Eigen::Map<const Eigen::MatrixXd>(MT.col(P).data(), vt, ct);
377 }
378 }
379 omega_d = -(M.cwiseProduct(GammaW)).sum();
380 GammaW = 0.5 * (GammaW + GammaW.transpose()).eval();
381 // screening response: dOmega = Tr(Gbar d eps), Gbar = M GammaW M
382 {
383 const Eigen::MatrixXd Gbar = M * GammaW * M;
384 const Eigen::MatrixXd GB = Bkd * Gbar; // (kd x P)
385 for (Index P = 0; P < naux; ++P) {
386 for (Index d = 0; d < nd; ++d) {
387 g(P).block(k0, c0 + d, nk, 1) +=
388 2.0 *
389 f.segment(nk * d, nk).cwiseProduct(GB.col(P).segment(nk * d, nk));
390 }
391 }
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);
396 w(c0 + d) += t;
397 w(k0 + k) -= t;
398 }
399 }
400 }
401 }
402
403 phase("BSE energy and partial derivatives");
404 // normalization of the full BSE (1 for TDA and normalized vectors)
405 gmat /= norm;
406 w /= norm;
407 wA /= norm;
408 BSEGradientResult result;
409 result.omega = (omega_qp + omega_x + omega_d) / norm;
410
411 // u_p: coefficients of sum_p u_p <p|V_xc|p> in the energy (QP response)
412 Eigen::VectorXd u = Eigen::VectorXd::Zero(nmo);
413
414 // ---- from the partial derivatives g, w, u of one target to its gradient
415 auto downstream = [&](bool last) -> Eigen::MatrixXd {
416 // ---- integral part of sum g . dB
417 // metric term first (needs g and B), then B is released (after the last
418 // target) and g becomes
419 // gL (column R: sum_P L_RP g^P) in place; the integral derivatives and
420 // the orbital part Q (with s^R = gL^R + gL^R^T) both use gL
421 Eigen::MatrixXd grad;
422 {
423 Eigen::MatrixXd G = gmat.transpose() * Bmat;
424 if (last) {
425 Bmat.resize(0, 0);
426 }
427 G = (0.5 * (G + G.transpose())).eval();
428 grad = -0.5 * ContractCoulombMetricDerivatives(auxbasis_, L * G * L);
429 }
430 RightMultiplyInPlace(gmat, L);
431 const Eigen::MatrixXd& gL = gmat;
433 auxbasis_, dftbasis_, [&gL, &Cw, nw](Index R, Eigen::MatrixXd& Gamma) {
434 Gamma = Cw *
435 (Eigen::Map<const Eigen::MatrixXd>(gL.col(R).data(), nw, nw) *
436 Cw.transpose());
437 });
438
439 phase("integral derivatives of B");
440 // ---- orbital part: Q_rp = sum_{P,q} B^P_rq s^P_qp, s^P = g^P + g^P^T,
441 // for all r (all MOs), p in the window
442 Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(nmo, nmo);
443 {
444 Eigen::MatrixXd Qw = Eigen::MatrixXd::Zero(nmo, nw);
446 auxbasis_, dftbasis_, [&](Index R, const Eigen::MatrixXd& I) {
447 const Eigen::Map<const Eigen::MatrixXd> gLR(gL.col(R).data(), nw,
448 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)
452 { Qw += t; }
453 });
454 Q.middleCols(lo, nw) = Qw;
455 }
456 gmat.resize(0, 0);
457 // v_xc expectation values (QP response): orbital part 2 u_p Vxc_rp
458 const bool has_u = u.cwiseAbs().maxCoeff() > 0.0;
459 if (has_u) {
460 const Eigen::MatrixXd Vxc_mo =
461 C.transpose() * vxc.IntegrateVXC(D0).matrix() * C;
462 Q += 2.0 * Vxc_mo * u.asDiagonal();
463 }
464 Eigen::VectorXd wfull = Eigen::VectorXd::Zero(nmo);
465 wfull.segment(lo, nw) = w;
466
467 phase("orbital part Q");
468 // ---- D, Theta (same-space blocks and diagonal)
469 const double kDegenerate = 1e-8;
470 Eigen::MatrixXd Dmo = Eigen::MatrixXd::Zero(nmo, nmo);
471 Eigen::MatrixXd Wmo = Eigen::MatrixXd::Zero(nmo, nmo);
472 auto same_space = [&](Index first, Index n) {
473 for (Index r = first; r < first + n; ++r) {
474 for (Index p = first; p < first + n; ++p) {
475 if (r == p) {
476 Dmo(p, p) = wfull(p);
477 Wmo(p, p) = 0.5 * Q(p, p) + wfull(p) * eps(p);
478 continue;
479 }
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));
483 continue;
484 }
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);
487 }
488 }
489 };
490 same_space(0, nocc);
491 same_space(nocc, nvirt);
492
493 // closed-shell Fock response G[X] (MO in, MO out) to a symmetric MO
494 // density change X
495 auto fock_response = [&](const Eigen::MatrixXd& Xmo) {
496 const Eigen::MatrixXd Xao = C * Xmo * C.transpose();
497 Eigen::MatrixXd G = vxc.IntegrateFXC(D0, {Xao})[0];
498 G += ERIs_.CalculateERIs_3c(Xao);
499 if (ScaHFX_ > 0) {
500 G += 0.5 * ScaHFX_ * ERIs_.CalculateEXX_3c(Eigen::MatrixXd(0, 0), Xao);
501 }
502 return Eigen::MatrixXd(C.transpose() * G * C);
503 };
504
505 // XC-kernel response alone (the v_xc expectation values), MO in and out
506 Eigen::MatrixXd Gu;
507 if (has_u) {
508 const Eigen::MatrixXd Uao = C * u.asDiagonal() * C.transpose();
509 Gu = C.transpose() * vxc.IntegrateFXC(D0, {Uao})[0] * C;
510 }
511
512 phase("Fock responses");
513 // ---- Z-vector: (A + B) Z = L
514 Eigen::MatrixXd Zvo;
515 {
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);
520 if (has_u) {
521 Lvo += 4.0 * Gu.block(nocc, 0, nvirt, nocc);
522 }
523 // StaticResponse solves (A + B) U = -C_v^T h C_o
524 AOOverlap overlap;
525 overlap.Fill(dftbasis_);
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();
529 // the gradient error is linear in the Z residual: 1e-6 relative changed
530 // the forces of pyridine + 6 H2O by 1e-8 Ha/Bohr against 1e-8 (three
531 // iterations fewer), far below the FD agreement and optimizer thresholds
532 std::vector<Eigen::MatrixXd> dP = StaticResponse(orb, {h}, 1e-6, 200);
533 Zvo = 0.5 * C_virt.transpose() * S * dP[0] * S * C_occ;
534 }
535
536 phase("Z-vector");
537 // ---- relaxed density Ptilde and energy-weighted density W
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();
541 {
542 const Eigen::MatrixXd GP = fock_response(Pmo);
543 Wmo.topLeftCorner(nocc, nocc) += 2.0 * GP.topLeftCorner(nocc, nocc);
544 if (has_u) {
545 Wmo.topLeftCorner(nocc, nocc) += 2.0 * Gu.topLeftCorner(nocc, nocc);
546 }
547 }
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;
553 }
554 }
555 const Eigen::MatrixXd Pao = C * Pmo * C.transpose();
556 const Eigen::MatrixXd Wao = C * Wmo * C.transpose();
557
558 phase("energy-weighted density");
559 // ---- sum Ptilde dF/dR|_D - sum W dS/dR
560 grad += ContractAODerivatives(ComputeKineticDerivatives(dftbasis_), Pao);
561 grad += ContractAODerivatives(
563 grad -= ContractAODerivatives(ComputeOverlapDerivatives(dftbasis_), Wao);
564 phase("one-electron and overlap derivatives");
565 // RI-J, exact: the Hartree energy is quadratic in the density
566 grad += 0.5 * (DFTGradient::RIJGradient(D0 + Pao, auxbasis_, dftbasis_) -
568 if (ScaHFX_ > 0) {
569 const SymmetricEigenSystem es = SymmetricEigen(Pao);
570 const double cutoff = 1e-12 * es.values.cwiseAbs().maxCoeff();
571 std::vector<Index> keep;
572 for (Index k = 0; k < es.values.size(); ++k) {
573 if (std::abs(es.values(k)) > cutoff) {
574 keep.push_back(k);
575 }
576 }
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)]);
582 }
583 grad -= ScaHFX_ * DFTGradient::RIPairGradient(U, lambda, C_occ, auxbasis_,
584 dftbasis_);
585 }
586 phase("RI-J and RI-K");
587 // XC: d/dt of the ground-state XC gradient at D0 + t (Ptilde + U), with
588 // U = sum_p u_p |p><p| from the v_xc expectation values (analytic, one
589 // grid pass)
590 {
591 const Eigen::MatrixXd Pxc = Pao + C * u.asDiagonal() * C.transpose();
592 grad += vxc.GradientResponse(D0, Pxc, mol);
593 }
594 phase("XC");
595 return grad;
596 };
597
598 // ---- targets: the BSE excitation energy, or one QP level after the
599 // other (g = 0, weight 1 on the level; KS-degenerate levels share the
600 // weight, since only the gradient of their mean is defined). The
601 // integrals B and the self-energy model are shared.
602 std::vector<Eigen::MatrixXd> gradients;
603 auto run_targets = [&](const std::function<void()>& qp_response) {
604 if (has_bse) {
605 qp_response();
606 phase("QP-correction response");
607 gradients.push_back(downstream(true));
608 return;
609 }
610 for (std::size_t t = 0; t < in.qp_levels.size(); ++t) {
611 const Index level = in.qp_levels[t];
612 gmat = Eigen::MatrixXd::Zero(nw * nw, naux);
613 w.setZero();
614 wA.setZero();
615 u.setZero();
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)) < 1e-6) {
621 partners.push_back(j);
622 }
623 }
624 for (Index j : partners) {
625 wA(j - lo) = 1.0 / double(partners.size());
626 }
627 if (partners.size() > 1) {
629 << TimeStamp() << " QP level " << level << " is one of "
630 << partners.size()
631 << " degenerate KS levels: gradient of their mean energy"
632 << std::flush;
633 }
634 qp_response();
635 phase("QP-correction response");
636 gradients.push_back(downstream(t + 1 == in.qp_levels.size()));
637 }
638 };
639
640 if (!in.qp_response) {
641 // all energies are KS energies plus fixed shifts
642 run_targets([&] { w += wA; });
643 } else {
644 // ---- response of the QP corrections, plasmon-pole model of VOTCA
645 // (Sigma_PPM, PPM::PPM_construct_parameters). The QP energies E of
646 // qpmin..qpmax solve E_p = eps_p + sigma_p(E_p; R), sigma = (1 - a_x)
647 // Sigma_x - v_xc + Sigma_c, with the screening and the poles of G at
648 // the RPA energies R: G0W0: R = KS energies plus fixed shifts; evGW:
649 // R = E in the QP window and, outside, KS energies plus the
650 // out-of-window shift of the corrections E - eps (OutOfWindowShift).
651 // With the weights W of dOmega on E, the adjoint (1 - J)^T v = W,
652 // J = dsigma/dE (through omega_p = E_p and, for evGW, R), turns
653 // W . dE into sum_p v_p (deps_p + dsigma_p|_{omega, R}) plus the
654 // explicit dependence of R on eps; G0W0: v_p = Z_p W_p.
655 // Sigma_c(omega) = sum_i a_i sum_n (Bt^i_pn)^2 h(omega - e_n +- Omega_i),
656 // h(t) = t / (t^2 + eta^2), a_i = w_i Omega_i / 2, Bt = B Phi with
657 // eps(0) = Phi diag(lambda) Phi^T, w_i = 1 - 1/lambda_i,
658 // Omega_i^2 = -nu^2 (y_i - 1) / (y_i - 1 + w_i), y_i = phi_i^T eps(i nu)^-1
659 // phi_i; + for occupied, - for virtual n. The reverse pass goes through
660 // all of these down to B and R.
661 const Index q0 = in.qpmin - lo;
662 const Index nq = in.qpmax - in.qpmin + 1;
663 const Index r0 = k0;
664 const Index nr = in.rpamax - in.rpamin + 1;
665 const Index qr = in.qpmin - in.rpamin; // QP level p is RPA level qr + p
666 const double nu2 = in.ppm_imag * in.ppm_imag;
667 const double seta2 = in.sigma_eta * in.sigma_eta;
668 const double ax = ScaHFX_;
669
670 using ReversePass =
671 std::function<Eigen::VectorXd(const Eigen::VectorXd&, bool)>;
672 // the adjoint and the bookkeeping of the energy weights, the same for
673 // all self-energy models: dsigma = Sigma_c'(E_p), reverse(v, accumulate)
674 // the reverse pass of sum_p v_p sigma_p(omega_p; R) at fixed omega
675 // (returns d/dR; with accumulate also adds d/dB, d/deps_p and v_xc),
676 // poles (nr x nq): column p the part of d sigma_p/dR through the pole
677 // energies of G alone (cheap, preconditioner of the evGW adjoint)
678 auto finish = [&](const Eigen::VectorXd& dsigma, const ReversePass& reverse,
679 const Eigen::MatrixXd& poles) {
680 // weights of dOmega: wE on the QP energies, wR on the RPA energies
681 // (screening of the BSE; BSE levels outside the QP window use R)
682 Eigen::VectorXd wR = w.segment(r0, nr);
683 w.setZero();
684 Eigen::VectorXd wE = wA.segment(q0, nq);
685 for (Index j = 0; j < nw; ++j) {
686 if (j >= q0 && j < q0 + nq) {
687 continue;
688 }
689 if (j < r0 || j >= r0 + nr) {
690 if (wA(j) != 0.0) {
691 throw std::runtime_error(
692 "BSEExcitationGradient: BSE level outside the RPA window");
693 }
694 continue;
695 }
696 wR(j - r0) += wA(j);
697 }
698
699 if (!in.qp_selfconsistent) {
700 // G0W0: R = eps + fixed shifts
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;
705 } else {
706 // evGW: Jacobian of the out-of-window RPA energies with respect to
707 // the corrections c = E - eps of the QP window (piecewise linear in c)
708 OutOfWindowShift shift;
711 // R(c, eps): the linear shift also depends on the KS energies (the
712 // abscissae of its fit), so both Jacobians are taken
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;
717 shift.Apply(R, e, in.rpamin, in.rpamax, homo, in.qpmin, in.qpmax);
718 return R;
719 };
720 auto rpa_energies = [&](const Eigen::VectorXd& c) {
721 return rpa_energies_eps(c, eps);
722 };
723 const Eigen::VectorXd c_ref =
724 in.qp_energies - eps.segment(in.qpmin, nq);
725 const Eigen::VectorXd R_ref = rpa_energies(c_ref);
726 result.rpa_rebuild_error =
727 (R_ref - in.energies_rpa).cwiseAbs().maxCoeff();
728 Eigen::MatrixXd Js = Eigen::MatrixXd::Zero(nr, nq); // dR/dc
729 const double dc = 1e-7;
730 for (Index q = 0; q < nq; ++q) {
731 Eigen::VectorXd cp = c_ref;
732 cp(q) += dc;
733 Eigen::VectorXd cm = c_ref;
734 cm(q) -= dc;
735 Js.col(q) = (rpa_energies(cp) - rpa_energies(cm)) / (2.0 * dc);
736 }
737 Js.middleRows(qr, nq).setZero(); // inside the window R = E itself
738 // W . dE with W_q = wE_q + wR_{window q} + sum_o wR_o Js_oq
739 const Eigen::VectorXd Wbar =
740 wE + wR.segment(qr, nq) + Js.transpose() * wR;
741 // adjoint: v_q (1 - Sigma_c'_q) - rho_q(v) - sum_o rho_o(v) Js_oq =
742 // Wbar_q, linear in v. A fixed-point iteration need not converge (it
743 // mirrors the evGW iteration, which VOTCA stabilizes by mixing), so
744 // GMRES, right-preconditioned with the same operator restricted to
745 // the pole energies of G (screening frozen); one reverse pass per
746 // iteration. Fallback: the matrix column by column (nq passes).
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);
752 };
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 = 1e-11 * std::max(1.0, bnorm);
759 Eigen::VectorXd v = Eigen::VectorXd::Zero(nq);
760 Index iterations = 0;
761 bool converged = (bnorm == 0.0);
762 if (!converged) {
763 const Index max_krylov = std::min<Index>(nq, 60);
764 Eigen::MatrixXd Vk(nq, max_krylov + 1);
765 Eigen::MatrixXd Hk =
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) { // Gram-Schmidt, twice
771 for (Index i = 0; i <= j; ++i) {
772 const double hij = Vk.col(i).dot(z);
773 Hk(i, j) += hij;
774 z -= hij * Vk.col(i);
775 }
776 }
777 Hk(j + 1, j) = z.norm();
778 iterations = j + 1;
779 // least squares min |bnorm e1 - H y|
780 Eigen::VectorXd rhs = Eigen::VectorXd::Zero(j + 2);
781 rhs(0) = bnorm;
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) < 1e-14 * bnorm) {
786 v = luM.solve(Vk.leftCols(j + 1) * y);
787 converged = true;
788 break;
789 }
790 Vk.col(j + 1) = z / Hk(j + 1, j);
791 }
792 }
793 if (converged &&
794 (apply(v) - Wbar).norm() > 1e-8 * std::max(1.0, bnorm)) {
795 converged = false;
796 }
797 if (converged) {
799 << TimeStamp() << " evGW adjoint: GMRES converged in "
800 << iterations << " iterations" << std::flush;
801 } else {
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);
808 e(q) = 1.0;
809 A.col(q) = apply(e);
810 }
811 const Eigen::PartialPivLU<Eigen::MatrixXd> lu(A);
812 v = lu.solve(Wbar);
813 if ((A * v - Wbar).cwiseAbs().maxCoeff() >
814 1e-8 * std::max(1.0, Wbar.cwiseAbs().maxCoeff())) {
815 throw std::runtime_error(
816 "BSEExcitationGradient: the evGW adjoint equation is "
817 "singular");
818 }
819 }
820 const Eigen::VectorXd rho = reverse(v, true);
821 // explicit eps dependence of the out-of-window R at fixed E (the dE
822 // part is in the adjoint): K_ok = dR_o/deps_k with c = E - eps
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 = 1e-7;
827 for (Index k = 0; k < nr; ++k) {
828 Eigen::VectorXd ep = eps;
829 ep(in.rpamin + k) += de;
830 Eigen::VectorXd em = eps;
831 em(in.rpamin + k) -= de;
832 const Eigen::VectorXd cp = in.qp_energies - ep.segment(in.qpmin, nq);
833 const Eigen::VectorXd cm = in.qp_energies - em.segment(in.qpmin, nq);
834 K.col(k) = (rpa_energies_eps(cp, ep) - rpa_energies_eps(cm, em)) /
835 (2.0 * de);
836 }
837 w.segment(r0, nr) += K.transpose() * wRo;
838 }
839 };
840
841 if (in.sigma_integrator == "ppm") {
842
843 const SymmetricEigenSystem es0 = SymmetricEigen(epsilon);
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);
854 }
855 }
856 Eigen::MatrixXd epsi = Bkd.transpose() * fi.asDiagonal() * Bkd;
857 epsi.diagonal().array() += 1.0;
858 const Eigen::MatrixXd EiPhi = InverseSPD(epsi) * Phi;
859
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);
867 if (wi < 1e-5) {
868 continue; // mode dropped, as in PPM_construct_parameters
869 }
870 wppm(i) = wi;
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);
876 }
877 const Eigen::VectorXd epole = eP.segment(r0, nr);
878 Eigen::VectorXd s_n = -Eigen::VectorXd::Ones(nr);
879 s_n.head(nk).setOnes();
880
881 const Eigen::MatrixXd Vxc_mo =
882 C.transpose() * vxc.IntegrateVXC(D0).matrix() * C;
883
884 // per QP level: B_pn (rows of B), Bt = B_p Phi (stored, nr x Naux per
885 // level), pole factors h, h' (recomputed when needed), Sigma_c'(E_p)
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));
890 }
891 return Bp;
892 };
893 auto poleFactors = [&](Index p, Eigen::ArrayXXd& H, Eigen::ArrayXXd& Hp) {
894 const double omega_p = in.qp_energies(p);
895 H.resize(nr, naux);
896 Hp.resize(nr, naux);
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;
901 H(n, i) = t / den;
902 Hp(n, i) = (seta2 - t * t) / (den * den);
903 }
904 }
905 };
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) {
910 const Index pabs = in.qpmin + p;
911 const double omega_p = in.qp_energies(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;
925 result.qp_rebuild_error =
926 std::max(result.qp_rebuild_error, std::abs(e_rebuilt - omega_p));
927 }
928
929 // reverse pass of sum_p v_p sigma_p(omega_p; R) at fixed omega: returns
930 // its derivative with respect to R (rho, RPA window); with accumulate,
931 // also adds the derivatives with respect to B (g), eps_p and v_xc
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);
939 if (v == 0.0) {
940 continue;
941 }
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();
951 if (accumulate) {
952 w(q0 + p) += v; // eps_p
953 u(in.qpmin + p) -= v; // -v_xc
954 const Eigen::MatrixXd gBp = GBt * Phi.transpose();
955 for (Index n = 0; n < nr; ++n) {
956 for (Index P = 0; P < naux; ++P) {
957 double gpn = gBp(n, P);
958 if (n < nk) { // Sigma_x: -(1 - a_x) sum_{i occ} (B_pi)^2
959 gpn += -2.0 * (1.0 - ax) * v * Bp(n, P);
960 }
961 g(P)(q0 + p, r0 + n) += gpn;
962 }
963 }
964 }
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) {
969 continue;
970 }
971 double sH = 0.0;
972 double sHp = 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);
976 }
977 gOm(i) += v * (0.5 * wppm(i) * sH + a(i) * sHp);
978 gw(i) += v * 0.5 * Om(i) * sH;
979 }
980 }
981 // PPM parameters -> eps(0), eps(i nu)
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) {
986 continue;
987 }
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);
994 }
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) {
1000 if (i == j) {
1001 Mx(i, i) = glam(i);
1002 continue;
1003 }
1004 const double dl = lam(j) - lam(i);
1005 Mx(i, j) = (std::abs(dl) > 1e-12) ? Mx(i, j) / dl : 0.0;
1006 }
1007 }
1008 Eigen::MatrixXd Geps0 = Phi * Mx * Phi.transpose();
1009 Geps0 = (0.5 * (Geps0 + Geps0.transpose())).eval();
1010 // eps = 1 + sum_kd f(Delta_kd) b b^T for both frequencies
1011 const Eigen::MatrixXd GB0 = Bkd * Geps0;
1012 const Eigen::MatrixXd GBi = Bkd * Gepsi;
1013 if (accumulate) {
1014 for (Index P = 0; P < naux; ++P) {
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)));
1021 }
1022 }
1023 }
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);
1030 rho(nk + d) += t;
1031 rho(k) -= t;
1032 }
1033 }
1034 return rho;
1035 };
1036
1037 run_targets([&] { finish(dsigma, reverse, poles); });
1038 } else if (in.sigma_integrator == "exact") {
1039 // Sigma_c of Sigma_Exact: the RPA pair eigenproblem
1040 // C = Delta^1/2 (Delta + 4 b b^T) Delta^1/2 = F diag(Omega^2) F^T over
1041 // the RPA transitions (k, d), modes Z_s = sum_kd b_kd (X+Y)_kd,s with
1042 // (X+Y)_s = Delta^1/2 F_s Omega_s^-1/2, and
1043 // Sigma_c(omega) = 2 sum_s sum_n (B_pn . Z_s)^2 h(omega - e_n +-
1044 // Omega_s)
1045 // = 2 sum_n k_n^T phi_n(C) k_n, k_n = (B_pn . b_kd) Delta_kd^1/2,
1046 // phi_n(lambda) = lambda^-1/2 h(omega - e_n +- lambda^1/2): a matrix
1047 // function of C, differentiated with the divided differences of
1048 // phi_n in the eigenbasis of C (Daleckii-Krein).
1049 const Index S = nk * nd;
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);
1054 }
1055 }
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();
1060 const SymmetricEigenSystem esC = SymmetricEigen(Cm);
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");
1066 }
1067 const Eigen::ArrayXd OmC = lamC.array().sqrt();
1068 // 1/(lambda_s - lambda_t), zero for (near) degenerate pairs, which
1069 // are handled with the derivative
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) > 1e-10 * std::max(1.0, lamC(s2))) {
1076 invdl(s2, t) = 1.0 / dl;
1077 } else {
1078 invdl(s2, t) = 0.0;
1079 if (s2 != t) {
1080 degenerate.emplace_back(s2, t);
1081 }
1082 }
1083 }
1084 }
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 =
1089 C.transpose() * vxc.IntegrateVXC(D0).matrix() * C;
1090
1091 // per QP level: a_n = F^T k_n stored (nr x S); B_pn, k_n and the
1092 // pole terms h, h', phi_n(lambda_s), phi_n'(lambda_s) at omega_p are
1093 // recomputed when needed
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));
1098 }
1099 return Bp;
1100 };
1101 struct PoleTerms {
1102 Eigen::ArrayXXd phi, dphi, hp;
1103 };
1104 auto poleTerms = [&](Index p) {
1105 const double omega_p = in.qp_energies(p);
1106 PoleTerms pt;
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; // h' / Omega
1119 pt.dphi(n, s2) =
1120 -h / (2.0 * om * om * om) + s_n(n) * hp / (2.0 * om * om);
1121 }
1122 }
1123 return pt;
1124 };
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) {
1129 const Index pabs = in.qpmin + p;
1130 const double omega_p = in.qp_energies(p);
1131 const Eigen::MatrixXd Bp = gatherB(p);
1132 Aps[std::size_t(p)] =
1133 ((Bp * Bkd.transpose()) * sD.asDiagonal()) * F; // a_n = F^T k_n
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;
1142 result.qp_rebuild_error =
1143 std::max(result.qp_rebuild_error, std::abs(e_rebuilt - omega_p));
1144 }
1145
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); // eigenbasis of C
1149 Eigen::MatrixXd gB = Eigen::MatrixXd::Zero(S, naux); // d/db_kd
1150 Eigen::VectorXd gDelta = Eigen::VectorXd::Zero(S);
1151 for (Index p = 0; p < nq; ++p) {
1152 const double v = vq(p);
1153 if (v == 0.0) {
1154 continue;
1155 }
1156 const Eigen::MatrixXd Bp = gatherB(p);
1157 const Eigen::MatrixXd Kraw = Bp * Bkd.transpose(); // (n, kd)
1158 const Eigen::MatrixXd& A = Aps[std::size_t(p)];
1159 const PoleTerms pt = poleTerms(p);
1160 const Eigen::ArrayXXd A2 = A.array().square();
1161 // d/dC through phi_n(C): divided differences in the eigenbasis
1162 const Eigen::MatrixXd Aphi = (A.array() * pt.phi).matrix();
1163 Gt.noalias() +=
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) +=
1169 2.0 * v *
1170 (A.col(st.first).array() * A.col(st.second).array() *
1171 pt.dphi.col(st.first))
1172 .sum();
1173 }
1174 // pole energies e_n
1175 rho += v * poles.col(p);
1176 // k_n = Kraw_n * Delta^1/2, a_n = F^T k_n
1177 const Eigen::MatrixXd gK =
1178 ((4.0 * v) * (A.array() * pt.phi)).matrix() * F.transpose();
1179 gDelta += (gK.array() * Kraw.array())
1180 .colwise()
1181 .sum()
1182 .matrix()
1183 .transpose()
1184 .cwiseQuotient(2.0 * sD);
1185 const Eigen::MatrixXd gKraw = gK * sD.asDiagonal(); // d/d(B_pn.b)
1186 gB.noalias() += gKraw.transpose() * Bp;
1187 if (accumulate) {
1188 w(q0 + p) += v; // eps_p
1189 u(in.qpmin + p) -= v; // -v_xc
1190 const Eigen::MatrixXd gBp = gKraw * Bkd; // d/dB_pn
1191 for (Index n = 0; n < nr; ++n) {
1192 for (Index P = 0; P < naux; ++P) {
1193 double gpn = gBp(n, P);
1194 if (n < nk) { // Sigma_x
1195 gpn += -2.0 * (1.0 - ax) * v * Bp(n, P);
1196 }
1197 g(P)(q0 + p, r0 + n) += gpn;
1198 }
1199 }
1200 }
1201 }
1202 // C = Delta^1/2 (Delta + 4 b b^T) Delta^1/2
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;
1208 if (accumulate) {
1209 for (Index P = 0; P < naux; ++P) {
1210 for (Index d = 0; d < nd; ++d) {
1211 g(P).block(k0, c0 + d, nk, 1) += gB.col(P).segment(nk * d, nk);
1212 }
1213 }
1214 }
1215 for (Index d = 0; d < nd; ++d) {
1216 for (Index k = 0; k < nk; ++k) {
1217 const double t = gDelta(k + nk * d);
1218 rho(nk + d) += t;
1219 rho(k) -= t;
1220 }
1221 }
1222 return rho;
1223 };
1224
1225 run_targets([&] { finish(dsigma, reverse, poles); });
1226 } else {
1227 throw std::runtime_error(
1228 "BSEExcitationGradient: QP response only for the ppm and exact "
1229 "self-energies");
1230 }
1231 }
1232
1233 {
1234 // phases of several targets are added up
1235 std::vector<std::pair<std::string, double>> summed;
1236 double total = 0.0;
1237 for (const auto& pt : phase_times) {
1238 total += pt.second;
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);
1244 } else {
1245 it->second += pt.second;
1246 }
1247 }
1248 phase_times = summed;
1250 << TimeStamp() << " BSE gradient timing (s), total " << total
1251 << std::flush;
1252 for (const auto& pt : phase_times) {
1254 << " " << pt.first << ": " << pt.second << std::flush;
1255 }
1256 }
1257 if (has_bse) {
1258 result.gradient = gradients[0];
1259 } else {
1260 result.qp_gradients = gradients;
1261 }
1262 return result;
1263}
1264
1266 const Orbitals& orb, bool singlet, Index index) {
1267 if (orb.hasBetaMOs()) {
1268 throw std::runtime_error(
1269 "BSE gradient: closed-shell (RKS) GW-BSE runs only");
1270 }
1271 if (orb.GetFlagUseHqpOffdiag()) {
1272 throw std::runtime_error(
1273 "BSE gradient: needs the diagonal QP Hamiltonian "
1274 "(bse.use_Hqp_offdiag false)");
1275 }
1276 if (orb.isQSGW()) {
1277 throw std::runtime_error("BSE gradient: QSGW is not supported");
1278 }
1279 const tools::EigenSystem& es =
1280 singlet ? orb.BSESinglets() : orb.BSETriplets();
1281 if (index < 0 || index >= es.eigenvalues().size()) {
1282 throw std::runtime_error("BSE gradient: state " + std::to_string(index) +
1283 " was not computed");
1284 }
1286 in.singlet = singlet;
1287 in.vmin = orb.getBSEvmin();
1288 in.cmax = orb.getBSEcmax();
1289 in.rpamin = orb.getRPAmin();
1290 in.rpamax = orb.getRPAmax();
1291 in.X = es.eigenvectors().col(index);
1292 if (!orb.getTDAApprox()) {
1293 in.Y = es.eigenvectors2().col(index);
1294 }
1295
1296 const Eigen::VectorXd& rpa = orb.RPAInputEnergies();
1297 if (rpa.size() != in.rpamax - in.rpamin + 1) {
1298 throw std::runtime_error("BSE gradient: no RPA input energies in orb");
1299 }
1300 in.energies_rpa = rpa;
1301 // diagonal of Hqp = V diag(e) V^T (QPdiag), as GWBSE builds it
1302 const Eigen::MatrixXd& V = orb.QPdiag().eigenvectors();
1303 const Eigen::VectorXd hqp_diag =
1304 V.cwiseProduct(V) * orb.QPdiag().eigenvalues();
1305 const Index qpmin = orb.getGWAmin();
1306 const Index qpmax = orb.getGWAmax();
1307 if (hqp_diag.size() != qpmax - qpmin + 1) {
1308 throw std::runtime_error("BSE gradient: no QP Hamiltonian in orb");
1309 }
1310 in.energies_bse.resize(in.cmax - in.vmin + 1);
1311 for (Index p = in.vmin; p <= in.cmax; ++p) {
1312 in.energies_bse(p - in.vmin) =
1313 (p >= qpmin && p <= qpmax) ? hqp_diag(p - qpmin) : rpa(p - in.rpamin);
1314 }
1315 return in;
1316}
1317
1319 const Orbitals& orb, const std::vector<Index>& levels) {
1320 if (orb.hasBetaMOs()) {
1321 throw std::runtime_error("QP gradient: closed-shell (RKS) GW runs only");
1322 }
1323 if (orb.isQSGW()) {
1324 throw std::runtime_error("QP gradient: QSGW is not supported");
1325 }
1327 in.rpamin = orb.getRPAmin();
1328 in.rpamax = orb.getRPAmax();
1329 // no BSE: the window is that of the screening
1330 in.vmin = in.rpamin;
1331 in.cmax = in.rpamax;
1332 const Eigen::VectorXd& rpa = orb.RPAInputEnergies();
1333 if (rpa.size() != in.rpamax - in.rpamin + 1) {
1334 throw std::runtime_error("QP gradient: no RPA input energies in orb");
1335 }
1336 in.energies_rpa = rpa;
1337 const Index qpmin = orb.getGWAmin();
1338 const Index qpmax = orb.getGWAmax();
1339 const Eigen::VectorXd& qp = orb.QPpertEnergies();
1340 if (qp.size() != qpmax - qpmin + 1) {
1341 throw std::runtime_error("QP gradient: no QP energies in orb");
1342 }
1343 in.energies_bse = rpa;
1344 in.energies_bse.segment(qpmin - in.rpamin, qp.size()) = qp;
1345 if (levels.empty()) {
1346 throw std::runtime_error("QP gradient: no levels");
1347 }
1348 for (Index level : levels) {
1349 if (level < qpmin || level > qpmax) {
1350 throw std::runtime_error("QP gradient: level " + std::to_string(level) +
1351 " is outside the QP window " +
1352 std::to_string(qpmin) + ":" +
1353 std::to_string(qpmax));
1354 }
1355 }
1356 in.qp_levels = levels;
1357 return in;
1358}
1359
1360} // namespace xtp
1361} // namespace votca
const Eigen::MatrixXd & eigenvectors2() const
Definition eigensystem.h:36
const Eigen::VectorXd & eigenvalues() const
Definition eigensystem.h:30
const Eigen::MatrixXd & eigenvectors() const
Definition eigensystem.h:33
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
Definition aomatrix.h:76
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
Definition aomatrix.h:52
std::string auxbasis_name_
Definition dftengine.h:591
std::string ecp_name_
Definition dftengine.h:593
void Prepare(Orbitals &orb, Index numofelectrons=-1)
static BSEGradientInput QPGradientInputFromOrbitals(const Orbitals &orb, const std::vector< Index > &levels)
BSEGradientResult BSEExcitationGradient(const Orbitals &orb, const BSEGradientInput &input)
static BSEGradientInput BSEGradientInputFromOrbitals(const Orbitals &orb, bool singlet, Index index)
std::vector< Eigen::MatrixXd > StaticResponse(const Orbitals &orb, const std::vector< Eigen::MatrixXd > &perturbations, double tolerance=1e-7, Index max_iterations=100)
Vxc_Potential< Vxc_Grid > SetupVxc(const QMMolecule &mol)
static Eigen::MatrixXd RIJGradient(const Eigen::MatrixXd &density, const AOBasis &auxbasis, const AOBasis &dftbasis)
static Eigen::MatrixXd RIPairGradient(const Eigen::MatrixXd &U, const Eigen::VectorXd &w, const Eigen::MatrixXd &C, const AOBasis &auxbasis, const AOBasis &dftbasis)
Eigen::MatrixXd & matrix()
Definition eigen.h:97
Container for molecular orbitals and derived one-particle data.
Definition orbitals.h:47
const tools::EigenSystem & BSETriplets() const
Return read-only access to triplet BSE eigenpairs.
Definition orbitals.h:527
bool GetFlagUseHqpOffdiag() const
Definition orbitals.h:707
const tools::EigenSystem & QPdiag() const
Return read-only access to the diagonalized quasiparticle representation.
Definition orbitals.h:512
const tools::EigenSystem & BSESinglets() const
Return read-only access to singlet BSE eigenpairs.
Definition orbitals.h:540
Index getBSEcmax() const
Return the highest conduction orbital included in BSE.
Definition orbitals.h:474
const Eigen::VectorXd & QPpertEnergies() const
Return read-only access to perturbative quasiparticle energies.
Definition orbitals.h:499
bool getTDAApprox() const
Return whether the Tamm-Dancoff approximation is enabled.
Definition orbitals.h:446
Index getBSEvmin() const
Return the lowest valence orbital included in BSE.
Definition orbitals.h:465
bool isQSGW() const
Definition orbitals.h:715
Index getRPAmax() const
Return the upper RPA orbital index.
Definition orbitals.h:439
const Eigen::VectorXd & RPAInputEnergies() const
Return read-only access to the RPA input energies.
Definition orbitals.h:487
Index getGWAmin() const
Return the lower GW orbital index.
Definition orbitals.h:419
const tools::EigenSystem & MOs() const
Return read-only access to alpha/restricted molecular orbitals.
Definition orbitals.h:192
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
Definition orbitals.h:262
Index getGWAmax() const
Return the upper GW orbital index.
Definition orbitals.h:422
bool hasBetaMOs() const
Report whether beta-spin molecular orbitals are available.
Definition orbitals.h:187
Index getRPAmin() const
Return the lower RPA orbital index.
Definition orbitals.h:436
Timestamp returns the current time as a string Example: cout << TimeStamp().
Definition logger.h:224
Eigen::MatrixXd GradientResponse(const Eigen::MatrixXd &density_matrix, const Eigen::MatrixXd &direction, const QMMolecule &atoms) const
Mat_p_Energy IntegrateVXC(const Eigen::MatrixXd &density_matrix) const
std::vector< Eigen::MatrixXd > IntegrateFXC(const Eigen::MatrixXd &density_matrix, const std::vector< Eigen::MatrixXd > &response_matrices) const
#define XTP_LOG(level, log)
Definition logger.h:40
Charge transport classes.
Definition ERIs.h:28
void ForEachThreeCenterIntegral(const AOBasis &auxbasis, const AOBasis &dftbasis, const ThreeCenterVisitor &use)
Eigen::MatrixXd ContractCoulombMetricDerivatives(const AOBasis &auxbasis, const Eigen::MatrixXd &G)
SymmetricEigenSystem SymmetricEigen(const Eigen::MatrixXd &A, bool vectors=true)
With vectors = false only the eigenvalues are computed.
std::vector< AOMatrixDerivative > ComputeNuclearAttractionDerivatives(const AOBasis &aobasis, const QMMolecule &mol)
std::vector< AOMatrixDerivative > ComputeKineticDerivatives(const AOBasis &aobasis)
std::vector< AOMatrixDerivative > ComputeOverlapDerivatives(const AOBasis &aobasis)
Eigen::MatrixXd ContractThreeCenterDerivatives(const AOBasis &auxbasis, const AOBasis &dftbasis, const AuxDensityFn &gamma)
std::array< Eigen::MatrixXd, 3 > AOMatrixDerivative
Eigen::MatrixXd InverseSPD(const Eigen::MatrixXd &A)
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
Eigen::VectorXd energies_rpa
energies of the levels rpamin..rpamax in the static RPA screening
Definition dftengine.h:202
Eigen::VectorXd qp_energies
QP energies of qpmin..qpmax (must equal energies_bse there).
Definition dftengine.h:215
double qp_rebuild_error
with qp_response: max |E - rebuilt QP energy| over the QP window
Definition dftengine.h:235
double omega
X^T H X from the rebuilt BSE Hamiltonian (equals the BSE eigenvalue).
Definition dftengine.h:228
double rpa_rebuild_error
with qp_selfconsistent: max |R - rebuilt RPA energy|
Definition dftengine.h:237
std::vector< Eigen::MatrixXd > qp_gradients
for qp_levels: dE_p/dR per level (natoms x 3 each), Hartree/Bohr
Definition dftengine.h:233
How the RPA energies of levels outside the GW window are shifted.
void Apply(Eigen::VectorXd &energies, const Eigen::VectorXd &dft, Index rpamin, Index rpamax, Index homo, Index qpmin, Index qpmax) const
static Mode Parse(const std::string &name)