votca 2026-dev
Loading...
Searching...
No Matches
rpa.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 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// Local VOTCA includes
21#include "votca/xtp/rpa.h"
22#include "votca/xtp/aomatrix.h"
25#include "votca/xtp/vc2index.h"
26#include <algorithm>
27#include <iomanip>
28#include <sstream>
29#include <stdexcept>
30
31namespace votca {
32namespace xtp {
33
34void RPA::UpdateRPAInputEnergies(const Eigen::VectorXd& dftenergies,
35 const Eigen::VectorXd& gwaenergies,
36 Index qpmin) {
37 Index rpatotal = rpamax_ - rpamin_ + 1;
38 energies_ = dftenergies.segment(rpamin_, rpatotal);
39 Index gwsize = Index(gwaenergies.size());
40
41 energies_.segment(qpmin - rpamin_, gwsize) = gwaenergies;
42
43 ShiftUncorrectedEnergies(dftenergies, qpmin, gwsize);
44}
45
46// Shifts the energies of levels in the RPA but outside the QP window, see
47// OutOfWindowShift.
48void RPA::ShiftUncorrectedEnergies(const Eigen::VectorXd& dftenergies,
49 Index qpmin, Index gwsize) {
50 out_of_window_.Apply(energies_, dftenergies, rpamin_, rpamax_, homo_, qpmin,
51 qpmin + gwsize - 1);
52}
53
55 const WeightedGram::RowsVisitor& use) const {
56 const Index n_unocc = rpamax_ - homo_;
57 use(Mmn_[m_level].bottomRows(n_unocc));
58}
59
60Eigen::MatrixXd RPA::ResponseSum(const WeightedGram::WeightsFn& weights) const {
61 const Index size = Mmn_.auxsize();
62 const Index n_occ = homo_ + 1 - rpamin_;
63 // holes left out of the response (TCMatrix_gwbse::ExcludedHoles)
64 const Index skip = std::min(Mmn_.ExcludedHoles(), n_occ);
65 OptionalTiming timing(timings_, "screening: epsilon (RPA)");
67 n_occ - skip, size,
68 [&weights, skip](Index m, Eigen::VectorXd& w) { weights(m + skip, w); },
69 [this, skip](Index m, const WeightedGram::RowsVisitor& use) {
70 VisitHoleVirtualRows(m + skip, use);
71 });
72}
73
74template <bool imag>
75Eigen::MatrixXd RPA::calculate_epsilon(double frequency) const {
76 const Index n_unocc = rpamax_ - homo_;
77 const double freq2 = frequency * frequency;
78 const double eta2 = eta_ * eta_;
79 Eigen::MatrixXd result =
80 ResponseSum([&](Index m_level, Eigen::VectorXd& denom) {
81 const Eigen::ArrayXd deltaE =
82 energies_.tail(n_unocc).array() - energies_(m_level);
83 if (imag) {
84 denom = 4 * deltaE / (deltaE.square() + freq2);
85 } else {
86 Eigen::ArrayXd deltEf = deltaE - frequency;
87 Eigen::ArrayXd sum = deltEf / (deltEf.square() + eta2);
88 deltEf = deltaE + frequency;
89 sum += deltEf / (deltEf.square() + eta2);
90 denom = 2 * sum;
91 }
92 });
93 result.diagonal().array() += 1.0;
94 if (Mmn_.HasAdditionalResponse()) {
95 result += Mmn_.AdditionalResponse(imag, frequency);
96 }
97 if (Mmn_.AdditionalPoleCount() > 0) {
98 // the poles as transitions, with the weights above
99 const Eigen::ArrayXd& W = Mmn_.AdditionalPoleEnergies().array();
100 Eigen::ArrayXd w;
101 if (imag) {
102 w = 4 * W / (W.square() + freq2);
103 } else {
104 w = 2 * ((W - frequency) / ((W - frequency).square() + eta2) +
105 (W + frequency) / ((W + frequency).square() + eta2));
106 }
107 const Eigen::MatrixXd C = Mmn_.AdditionalPoleVectors();
108 result.noalias() += C * w.matrix().asDiagonal() * C.transpose();
109 }
110 return result;
111}
112
113template Eigen::MatrixXd RPA::calculate_epsilon<true>(double frequency) const;
114template Eigen::MatrixXd RPA::calculate_epsilon<false>(double frequency) const;
115
116Eigen::MatrixXd RPA::calculate_epsilon_r(std::complex<double> frequency) const {
117 if (Mmn_.HasAdditionalResponse()) {
118 throw std::runtime_error(
119 "RPA: an additional response (inactive_screening with the "
120 "plasmon-pole model) is only available at the plasmon-pole fitting "
121 "points, not for complex frequencies (sigma_integrator cda).");
122 }
123 const Index n_unocc = rpamax_ - homo_;
124 const double sigma_1 = std::pow(frequency.imag() + eta_, 2);
125 const double sigma_2 = std::pow(frequency.imag() - eta_, 2);
126 Eigen::MatrixXd result =
127 ResponseSum([&](Index m_level, Eigen::VectorXd& chi) {
128 const Eigen::ArrayXd deltaE =
129 energies_.tail(n_unocc).array() - energies_(m_level);
130 const Eigen::ArrayXd deltaEm = frequency.real() - deltaE;
131 const Eigen::ArrayXd deltaEp = frequency.real() + deltaE;
132 // the factor -2 of the response is folded into the weights
133 chi = -2.0 * (deltaEm * (deltaEm.abs2() + sigma_1).inverse() -
134 deltaEp * (deltaEp.abs2() + sigma_2).inverse());
135 });
136 if (Mmn_.AdditionalPoleCount() > 0) {
137 const Eigen::ArrayXd W = Mmn_.AdditionalPoleEnergies().array();
138 const Eigen::ArrayXd deltaEm = frequency.real() - W;
139 const Eigen::ArrayXd deltaEp = frequency.real() + W;
140 const Eigen::VectorXd chi =
141 -2.0 * (deltaEm * (deltaEm.square() + sigma_1).inverse() -
142 deltaEp * (deltaEp.square() + sigma_2).inverse());
143 const Eigen::MatrixXd C = Mmn_.AdditionalPoleVectors();
144 result.noalias() += C * chi.asDiagonal() * C.transpose();
145 }
146 result.diagonal().array() += 1.0;
147 return result;
148}
149
151 // transitions of M, then the additional poles (TCMatrix_gwbse)
152 Eigen::VectorXd AmB = Calculate_H2p_AmB();
153 Eigen::MatrixXd ApB = Calculate_H2p_ApB();
154 const Index rpasize = AmB.size();
155
157 sol.ERPA_correlation = -0.25 * (ApB.trace() + AmB.sum());
158
159 // C = AmB^1/2 * ApB * AmB^1/2
160 Eigen::MatrixXd& C = ApB;
161 C.applyOnTheLeft(AmB.cwiseSqrt().asDiagonal());
162 C.applyOnTheRight(AmB.cwiseSqrt().asDiagonal());
163
165
166 // Do not remove this line! It has to be there for MKL to not crash
167 sol.omega = Eigen::VectorXd::Zero(es.values.size());
168 sol.omega = es.values.cwiseSqrt();
169 sol.ERPA_correlation += 0.5 * sol.omega.sum();
170
171 {
172 std::ostringstream oss;
173 oss << TimeStamp() << " RKS H2p poles (first 20, eV): ";
174 const Index nprint = std::min<Index>(20, sol.omega.size());
175 for (Index i = 0; i < nprint; ++i) {
176 oss << std::fixed << std::setprecision(6)
177 << tools::conv::hrt2ev * sol.omega(i);
178 if (i + 1 < nprint) {
179 oss << ", ";
180 }
181 }
182 XTP_LOG(Log::error, log_) << oss.str() << std::flush;
183 }
184
186 << " Lowest neutral excitation energy (eV): "
187 << tools::conv::hrt2ev * sol.omega.minCoeff()
188 << std::flush;
189
190 // RPA correlation energy calculated from Eq.9 of J. Chem. Phys. 132, 234114
191 // (2010)
193 << TimeStamp()
194 << " RPA correlation energy (Hartree): " << sol.ERPA_correlation
195 << std::flush;
196
197 sol.XpY = Eigen::MatrixXd(rpasize, rpasize);
198
199 Eigen::VectorXd AmB_sqrt = AmB.cwiseSqrt();
200 Eigen::VectorXd Omega_sqrt_inv = sol.omega.cwiseSqrt().cwiseInverse();
201 for (int s = 0; s < rpasize; s++) {
202 sol.XpY.col(s) =
203 Omega_sqrt_inv(s) * AmB_sqrt.cwiseProduct(es.vectors.col(s));
204 }
205
206 return sol;
207}
208
209Eigen::VectorXd RPA::Calculate_H2p_AmB() const {
210 const Index lumo = homo_ + 1;
211 const Index n_occ = lumo - rpamin_;
212 const Index n_unocc = rpamax_ - lumo + 1;
213 // excluded holes (TCMatrix_gwbse::ExcludedHoles) have no transitions
214 const Index skip = std::min(Mmn_.ExcludedHoles(), n_occ);
215 const Index rpasize = (n_occ - skip) * n_unocc;
216 const Index npoles = Mmn_.AdditionalPoleCount();
217 vc2index vc = vc2index(skip, 0, n_unocc);
218 Eigen::VectorXd AmB = Eigen::VectorXd::Zero(rpasize + npoles);
219 for (Index v = skip; v < n_occ; v++) {
220 Index i = vc.I(v, 0);
221 AmB.segment(i, n_unocc) =
222 energies_.segment(n_occ, n_unocc).array() - energies_(v);
223 }
224 AmB.tail(npoles) = Mmn_.AdditionalPoleEnergies();
225 return AmB;
226}
227
228Eigen::MatrixXd RPA::Calculate_H2p_ApB() const {
229 const Index lumo = homo_ + 1;
230 const Index n_occ = lumo - rpamin_;
231 const Index n_unocc = rpamax_ - lumo + 1;
232 const Index skip = std::min(Mmn_.ExcludedHoles(), n_occ);
233 const Index rpasize = (n_occ - skip) * n_unocc;
234 const Index npoles = Mmn_.AdditionalPoleCount();
235 vc2index vc = vc2index(skip, 0, n_unocc);
236 Eigen::MatrixXd ApB =
237 Eigen::MatrixXd::Zero(rpasize + npoles, rpasize + npoles);
238#pragma omp parallel for schedule(guided)
239 for (Index v2 = skip; v2 < n_occ; v2++) {
240 Index i2 = vc.I(v2, 0);
241 const Eigen::MatrixXd Mmn_v2T =
242 Mmn_[v2].middleRows(n_occ, n_unocc).transpose();
243 for (Index v1 = v2; v1 < n_occ; v1++) {
244 Index i1 = vc.I(v1, 0);
245 // Multiply with factor 2 to sum over both (identical) spin states
246 ApB.block(i1, i2, n_unocc, n_unocc) =
247 2 * 2 * Mmn_[v1].middleRows(n_occ, n_unocc) * Mmn_v2T;
248 }
249 }
250 if (npoles > 0) {
251 // the poles couple like transitions with integrals c_k (same factor 4)
252 const Eigen::MatrixXd C = Mmn_.AdditionalPoleVectors();
253 ApB.bottomRightCorner(npoles, npoles) = 4 * C.transpose() * C;
254#pragma omp parallel for schedule(dynamic)
255 for (Index v = skip; v < n_occ; v++) {
256 ApB.block(rpasize, vc.I(v, 0), npoles, n_unocc) =
257 4 * C.transpose() * Mmn_[v].middleRows(n_occ, n_unocc).transpose();
258 }
259 }
260 ApB.diagonal() += Calculate_H2p_AmB();
261 return ApB;
262}
263
264SymmetricEigenSystem RPA::Diagonalize_H2p_C(const Eigen::MatrixXd& C) const {
266 << TimeStamp() << " Diagonalizing two-particle Hamiltonian "
267 << std::flush;
270 << TimeStamp() << " Diagonalization done " << std::flush;
271 double minCoeff = es.values.minCoeff();
272 if (minCoeff <= 0.0) {
274 << TimeStamp() << " Detected non-positive eigenvalue: " << minCoeff
275 << std::flush;
276 throw std::runtime_error("Detected non-positive eigenvalue.");
277 }
278 return es;
279}
280
281} // namespace xtp
282} // namespace votca
Eigen::MatrixXd Calculate_H2p_ApB() const
Definition rpa.cc:228
void ShiftUncorrectedEnergies(const Eigen::VectorXd &dftenergies, Index qpmin, Index gwsize)
Definition rpa.cc:48
Eigen::MatrixXd ResponseSum(const WeightedGram::WeightsFn &weights) const
Definition rpa.cc:60
Index rpamax_
Definition rpa.h:90
OutOfWindowShift out_of_window_
Definition rpa.h:115
Eigen::VectorXd energies_
Definition rpa.h:93
void VisitHoleVirtualRows(Index m_level, const WeightedGram::RowsVisitor &use) const
Definition rpa.cc:54
Eigen::MatrixXd calculate_epsilon(double frequency) const
Definition rpa.cc:75
Eigen::MatrixXd calculate_epsilon_r(double frequency) const
Definition rpa.h:61
const TCMatrix_gwbse & Mmn_
Definition rpa.h:96
const double eta_
Definition rpa.h:91
Index rpamin_
Definition rpa.h:89
SymmetricEigenSystem Diagonalize_H2p_C(const Eigen::MatrixXd &C) const
Definition rpa.cc:264
Eigen::VectorXd Calculate_H2p_AmB() const
Definition rpa.cc:209
Logger & log_
Definition rpa.h:95
rpa_eigensolution Diagonalize_H2p() const
Definition rpa.cc:150
void UpdateRPAInputEnergies(const Eigen::VectorXd &dftenergies, const Eigen::VectorXd &gwaenergies, Index qpmin)
Definition rpa.cc:34
DFTTimings * timings_
Definition rpa.h:97
Index homo_
Definition rpa.h:88
Timestamp returns the current time as a string Example: cout << TimeStamp().
Definition logger.h:224
std::function< void(Index m, Eigen::VectorXd &w)> WeightsFn
Writes the weights of block m; their number is the number of rows.
std::function< void(const Eigen::Ref< const Eigen::MatrixXd > &)> RowsVisitor
Calls use(R_m); R_m may be a block of stored data or a temporary.
static Eigen::MatrixXd Compute(Index nblocks, Index ncols, const WeightsFn &weights, const RowsFn &rows)
This class transforms a pair of indices v,c into a compound index I, via I=ctotal*v+c the fast dimens...
Definition vc2index.h:36
Index I(Index v, Index c) const
Definition vc2index.h:42
#define XTP_LOG(level, log)
Definition logger.h:40
const double hrt2ev
Definition constants.h:53
Charge transport classes.
Definition ERIs.h:28
SymmetricEigenSystem SymmetricEigen(const Eigen::MatrixXd &A, bool vectors=true)
With vectors = false only the eigenvalues are computed.
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26