votca 2026-dev
Loading...
Searching...
No Matches
threecenter.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 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// Standard includes
22#include <algorithm>
23#include <cassert>
24#include <cmath>
25#include <stdexcept>
26#include <string>
27
28#include "votca/xtp/aomatrix.h"
31
32namespace votca {
33namespace xtp {
34
35Eigen::MatrixXd TCMatrix_gwbse::CoreExchange(const AOBasis& auxbasis,
36 const AOBasis& dftbasis,
37 const Eigen::MatrixXd& mos,
38 Index ncore, Index qpmin,
39 Index qpmax) {
40 const Index qptotal = qpmax - qpmin + 1;
41 Eigen::MatrixXd X = Eigen::MatrixXd::Zero(qptotal, qptotal);
42 if (ncore <= 0 || qptotal <= 0) {
43 return X;
44 }
45 // (m c | P) for m in the GW window, c in the core: freshly filled, so in
46 // the orthogonal auxiliary frame where the bare v is M M^T
48 Mc.Initialize(auxbasis.AOBasisSize(), qpmin, qpmax, 0, ncore - 1);
49 Mc.Fill(auxbasis, dftbasis, mos);
50 Eigen::MatrixXd Z(qptotal, Mc.auxsize());
51 for (Index c = 0; c < ncore; ++c) {
52 for (Index m = 0; m < qptotal; ++m) {
53 Z.row(m) = Mc[m].row(c);
54 }
55 X.selfadjointView<Eigen::Upper>().rankUpdate(Z, -1.0);
56 }
57 return X.selfadjointView<Eigen::Upper>();
58}
59
60void TCMatrix_gwbse::Initialize(Index basissize, Index mmin, Index mmax,
61 Index nmin, Index nmax) {
62
63 // here as storage indices starting from zero
64 nmin_ = nmin;
65 nmax_ = nmax;
66 ntotal_ = nmax - nmin + 1;
67 mmin_ = mmin;
68 mmax_ = mmax;
69 mtotal_ = mmax - mmin + 1;
70 auxbasissize_ = basissize;
71
72 // vector has mtotal elements
73 // largest object should be allocated in multithread fashion
74 matrix_ = std::vector<Eigen::MatrixXd>(mtotal_);
75 const Index skip = std::min(excluded_holes_, mtotal_);
76#pragma omp parallel for schedule(dynamic, 4)
77 for (Index i = 0; i < mtotal_; i++) {
78 matrix_[i] = (i < skip) ? Eigen::MatrixXd(0, auxbasissize_)
79 : Eigen::MatrixXd::Zero(ntotal_, auxbasissize_);
80 }
81}
82
85 // the excluded slices are not stored
86 for (Index i = 0; i < std::min(k, Index(matrix_.size())); i++) {
87 matrix_[i].resize(0, auxbasissize_);
88 }
89}
90
91/*
92 * Modify 3-center matrix elements consistent with use of symmetrized
93 * Coulomb interaction.
94 */
95void TCMatrix_gwbse::MultiplyRightWithAuxMatrix(const Eigen::MatrixXd& matrix) {
96#pragma omp parallel for schedule(dynamic)
97 for (Index i = 0; i < msize(); i++) {
98 matrix_[i] *= matrix;
99 }
100 if (matrix.rows() != matrix.cols()) {
101 aux_frame_known_ = false; // a projection, not a change of frame
102 aux_frame_.resize(0, 0);
103 } else if (aux_frame_known_) {
104 aux_frame_ = (aux_frame_.size() == 0) ? matrix : aux_frame_ * matrix;
105 }
106}
107
109 Index full_slices,
110 Index lead) {
111 if (U.rows() != U.cols()) {
113 return;
114 }
115 const Index lead_rows = std::clamp<Index>(lead, 0, nsize());
116 const Index lead_slices = std::clamp<Index>(lead, 0, msize());
117 full_slices = std::clamp<Index>(full_slices, 0, lead_slices);
118#pragma omp parallel for schedule(dynamic)
119 for (Index i = 0; i < lead_slices; i++) {
120 if (matrix_[i].rows() == 0) {
121 continue; // excluded hole, not stored
122 }
123 const Index rows = (i < full_slices) ? nsize() : lead_rows;
124 Eigen::MatrixXd rotated = matrix_[i].topRows(rows) * U;
125 matrix_[i].topRows(rows) = rotated;
126 }
127 if (aux_frame_known_) {
128 aux_frame_ = (aux_frame_.size() == 0) ? U : aux_frame_ * U;
129 }
130 if (full_slices < msize() && (lead_slices < msize() || lead_rows < nsize())) {
131 partially_rotated_ = true;
132 }
133}
134
136 if (!aux_frame_known_) {
137 return false;
138 }
139 if (aux_frame_.size() == 0) {
140 return true;
141 }
142 const Index n = aux_frame_.rows();
143 return (aux_frame_.transpose() * aux_frame_ - Eigen::MatrixXd::Identity(n, n))
144 .cwiseAbs()
145 .maxCoeff() < 1e-10;
146}
147
148void TCMatrix_gwbse::MultiplyInFillFrame(const Eigen::MatrixXd& A) {
149 if (partially_rotated_) {
150 throw std::runtime_error(
151 "TCMatrix_gwbse: cannot dress the auxiliary index after only part of "
152 "the tensor was rotated (MultiplyRightWithAuxMatrixLeading). Rebuild "
153 "first.");
154 }
155 if (!aux_frame_known_) {
156 throw std::runtime_error(
157 "TCMatrix_gwbse: cannot dress the auxiliary index after a non-square "
158 "MultiplyRightWithAuxMatrix. Rebuild first.");
159 }
160 if (aux_frame_.size() == 0) {
162 return;
163 }
164 // M_fill F X = M_fill A F => X = F^-1 A F. The frame becomes A F.
165 const Eigen::PartialPivLU<Eigen::MatrixXd> lu(aux_frame_);
167}
168
169void TCMatrix_gwbse::DressAuxIndex(const Eigen::MatrixXd& S) {
170 if (Dressed()) {
171 throw std::runtime_error(
172 "TCMatrix_gwbse::DressAuxIndex: already dressed. Undress first.");
173 }
174 if (S.rows() != auxbasissize_ || S.cols() != auxbasissize_) {
175 throw std::runtime_error(
176 "TCMatrix_gwbse::DressAuxIndex: S is " + std::to_string(S.rows()) +
177 "x" + std::to_string(S.cols()) + ", auxiliary basis has " +
178 std::to_string(auxbasissize_) + " functions.");
179 }
181 dressing_ = S;
182}
183
185 if (!Dressed()) {
186 return;
187 }
189 dressing_.resize(0, 0);
190}
191
192Eigen::MatrixXd TCMatrix_gwbse::AdditionalResponse(bool imaginary,
193 double frequency) const {
194 for (const ResponsePoint& p : additional_response_) {
195 if (p.imaginary == imaginary && std::abs(p.frequency - frequency) <=
196 1e-12 * (1.0 + std::abs(frequency))) {
197 if (!aux_frame_known_) {
198 throw std::runtime_error(
199 "TCMatrix_gwbse::AdditionalResponse: the auxiliary frame is not "
200 "known (non-square MultiplyRightWithAuxMatrix).");
201 }
202 if (aux_frame_.size() == 0) {
203 return p.Y;
204 }
205 return aux_frame_.transpose() * p.Y * aux_frame_;
206 }
207 }
208 throw std::runtime_error(
209 "TCMatrix_gwbse: an additional response (e.g. inactive_screening with "
210 "the plasmon-pole model) is set, but not at " +
211 std::string(imaginary ? "imaginary" : "real") + " frequency " +
212 std::to_string(frequency) +
213 " Ha; only the plasmon-pole fitting points are supported.");
214}
215
217 if (!aux_frame_known_) {
218 throw std::runtime_error(
219 "TCMatrix_gwbse::AdditionalPoleVectors: the auxiliary frame is not "
220 "known (non-square MultiplyRightWithAuxMatrix).");
221 }
222 if (aux_frame_.size() == 0) {
223 return pole_vectors_;
224 }
225 return aux_frame_.transpose() * pole_vectors_;
226}
227
229 const Eigen::MatrixXd& K) const {
230 if (!aux_frame_known_) {
231 throw std::runtime_error(
232 "TCMatrix_gwbse::ToCurrentAuxFrame: the auxiliary index of the "
233 "three-centre integrals was multiplied by a non-square matrix, so "
234 "there is no frame to bring the kernel into. Apply it before that, "
235 "or Rebuild.");
236 }
237 if (aux_frame_.size() == 0) {
238 return K;
239 }
240 if (K.rows() != aux_frame_.rows() || K.cols() != aux_frame_.rows()) {
241 throw std::runtime_error(
242 "TCMatrix_gwbse::ToCurrentAuxFrame: kernel is " +
243 std::to_string(K.rows()) + "x" + std::to_string(K.cols()) +
244 ", auxiliary frame is " + std::to_string(aux_frame_.rows()) + "x" +
245 std::to_string(aux_frame_.cols()) + ".");
246 }
247 const Index n = aux_frame_.rows();
248 const double off_orthogonal =
249 (aux_frame_.transpose() * aux_frame_ - Eigen::MatrixXd::Identity(n, n))
250 .cwiseAbs()
251 .maxCoeff();
252 Eigen::MatrixXd result;
253 if (off_orthogonal < 1e-10) {
254 result = aux_frame_.transpose() * K * aux_frame_;
255 } else {
256 const Eigen::PartialPivLU<Eigen::MatrixXd> lu(aux_frame_);
257 const Eigen::MatrixXd X = lu.solve(K); // F^-1 K
258 result = lu.solve(Eigen::MatrixXd(X.transpose())).transpose();
259 }
260 return result;
261}
262/*
263 * Fill the 3-center object by looping over shells of GW basis set and
264 * calling FillBlock, which calculates all 3-center overlap integrals
265 * associated to a particular shell, convoluted with the DFT orbital
266 * coefficients
267 */
268void TCMatrix_gwbse::Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
269 const Eigen::MatrixXd& dft_orbitals) {
270 // needed for Rebuild())
271 auxbasis_ = &auxbasis;
272 dftbasis_ = &dftbasis;
273 dft_orbitals_ = &dft_orbitals;
274
275 Fill3cMO(auxbasis, dftbasis, dft_orbitals);
276
277 AOOverlap auxoverlap;
278 auxoverlap.Fill(auxbasis);
279 AOCoulomb auxcoulomb;
280 auxcoulomb.Fill(auxbasis);
281 inv_sqrt_ = auxcoulomb.Pseudo_InvSqrt_GWBSE(auxoverlap, metric_tolerance);
282 removedfunctions_ = auxcoulomb.Removedfunctions();
284 // What Fill leaves behind is the reference frame by definition.
285 aux_frame_.resize(0, 0);
286 aux_frame_known_ = true;
287 partially_rotated_ = false;
288 dressing_.resize(0, 0);
289
290 return;
291}
292
293void TCMatrix_gwbse::Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
294 const Eigen::MatrixXd& dft_orbitals,
295 const Eigen::MatrixXd& inv_sqrt) {
296 auxbasis_ = &auxbasis;
297 dftbasis_ = &dftbasis;
298 dft_orbitals_ = &dft_orbitals;
299 Fill3cMO(auxbasis, dftbasis, dft_orbitals);
300 inv_sqrt_ = inv_sqrt;
302 aux_frame_.resize(0, 0);
303 aux_frame_known_ = true;
304 partially_rotated_ = false;
305 dressing_.resize(0, 0);
306}
307
308void TCMatrix_gwbse::RotateOrbitals(const Eigen::MatrixXd& U, Index first,
309 Index last) {
310 const Index window = last - first + 1;
311 assert(first >= nmin_ && last <= nmax_);
312 assert(first >= mmin_ && last <= mmax_);
313 assert(U.rows() == window && U.cols() == window);
314 const Index row0 = first - nmin_;
315 const Index slice0 = first - mmin_;
316 const Index rows = ntotal_;
317 const Index aux = auxbasissize_;
318 assert(slice0 >= std::min(excluded_holes_, mtotal_));
319
320 // second index, every slice
321 const Eigen::MatrixXd Ut = U.transpose();
322#pragma omp parallel for schedule(dynamic)
323 for (Index m = 0; m < Index(matrix_.size()); m++) {
324 if (matrix_[m].rows() == 0) {
325 continue; // excluded hole, not stored
326 }
327 auto block = matrix_[m].middleRows(row0, window);
328 block = Ut * block;
329 }
330
331 // first index: the slices of the window as columns of one matrix, in
332 // chunks of auxiliary columns (contiguous in each slice) of about 64 MB
333 const Index chunk = std::max<Index>(
334 1, std::min<Index>(aux, Index(8e6 / double(rows * window + 1))));
335 Eigen::MatrixXd X;
336 Eigen::MatrixXd Y;
337 for (Index c0 = 0; c0 < aux; c0 += chunk) {
338 const Index nc = std::min(chunk, aux - c0);
339 const Index len = rows * nc;
340 X.resize(len, window);
341#pragma omp parallel for
342 for (Index k = 0; k < window; k++) {
343 X.col(k) = Eigen::Map<const Eigen::VectorXd>(
344 matrix_[slice0 + k].col(c0).data(), len);
345 }
346 Y.noalias() = X * U;
347#pragma omp parallel for
348 for (Index i = 0; i < window; i++) {
349 Eigen::Map<Eigen::VectorXd>(matrix_[slice0 + i].col(c0).data(), len) =
350 Y.col(i);
351 }
352 }
353}
354
356 const std::vector<std::vector<Index>>& kept) {
357 basissize_ = dftbasis.AOBasisSize();
358 const std::vector<Index> shell2bf = dftbasis.getMapToBasisFunctions();
359 runs_.clear();
360 col_offset_.assign(basissize_, 0);
361 Index offset = 0;
362 for (Index a = 0; a < Index(kept.size()); ++a) {
363 const Index start = shell2bf[a];
364 const Index size = dftbasis.getShell(a).getNumFunc();
365 for (Index i = 0; i < size; ++i) {
366 const Index mu = start + i;
367 col_offset_[mu] = offset;
368 // merge neighbouring kept shells into one run of rows
369 Index k = 0;
370 while (k < Index(kept[a].size())) {
371 Index b_first = kept[a][k];
372 Index b_last = b_first;
373 while (k + 1 < Index(kept[a].size()) && kept[a][k + 1] == b_last + 1) {
374 ++k;
375 b_last = kept[a][k];
376 }
377 ++k;
378 const Index row = shell2bf[b_first];
379 const Index end = std::min(
380 shell2bf[b_last] + dftbasis.getShell(b_last).getNumFunc(), mu + 1);
381 runs_.push_back({mu, row, end - row, offset});
382 offset += end - row;
383 }
384 }
385 }
386}
387
388void TCMatrix_dft::FillFullMatrix(Index P, Eigen::MatrixXd& full) const {
389 assert(full.rows() == basissize_ && full.cols() == basissize_);
390 const double* src = data_.col(P).data();
391 for (const Run& r : runs_) {
392 std::copy(src + r.offset, src + r.offset + r.length,
393 full.data() + r.col * basissize_ + r.row);
394 }
395 full.triangularView<Eigen::StrictlyLower>() = full.transpose();
396}
397
398Eigen::MatrixXd TCMatrix_dft::FullMatrix(Index P) const {
399 Eigen::MatrixXd full = Eigen::MatrixXd::Zero(basissize_, basissize_);
400 FillFullMatrix(P, full);
401 return full;
402}
403
404Eigen::VectorXd TCMatrix_dft::PackWeighted(const Eigen::MatrixXd& full) const {
405 assert(full.rows() == basissize_ && full.cols() == basissize_);
406 Eigen::VectorXd packed(data_.rows());
407 for (const Run& r : runs_) {
408 for (Index k = 0; k < r.length; ++k) {
409 const Index row = r.row + k;
410 packed(r.offset + k) =
411 (row == r.col) ? full(r.col, row) : 2.0 * full(r.col, row);
412 }
413 }
414 return packed;
415}
416
417Eigen::MatrixXd TCMatrix_dft::Unpack(const Eigen::VectorXd& packed) const {
418 assert(packed.size() == data_.rows());
419 Eigen::MatrixXd full = Eigen::MatrixXd::Zero(basissize_, basissize_);
420 for (const Run& r : runs_) {
421 full.block(r.row, r.col, r.length, 1) = packed.segment(r.offset, r.length);
422 }
423 full.triangularView<Eigen::StrictlyLower>() = full.transpose();
424 return full;
425}
426
427} // namespace xtp
428} // namespace votca
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Index AOBasisSize() const
Definition aobasis.h:46
std::vector< Index > getMapToBasisFunctions() const
Definition aobasis.cc:45
const AOShell & getShell(Index idx) const
Definition aobasis.h:52
void Fill(const AOBasis &aobasis) final
Index Removedfunctions() const
Definition aomatrix.h:81
Eigen::MatrixXd Pseudo_InvSqrt_GWBSE(const AOOverlap &auxoverlap, double etol)
Definition aomatrix.cc:54
void Fill(const AOBasis &aobasis) final
Index getNumFunc() const
Definition aoshell.h:103
std::vector< Index > col_offset_
std::vector< Run > runs_
Index size() const
Number of aux functions.
Definition threecenter.h:85
Eigen::VectorXd PackWeighted(const Eigen::MatrixXd &full) const
Eigen::MatrixXd FullMatrix(Index P) const
B_P as a full symmetric matrix, zero for the dropped pairs.
void SetupLayout(const AOBasis &dftbasis, const std::vector< std::vector< Index > > &kept)
void FillFullMatrix(Index P, Eigen::MatrixXd &full) const
Eigen::MatrixXd Unpack(const Eigen::VectorXd &packed) const
Eigen::MatrixXd data_
void Fill(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
const AOBasis * auxbasis_
static constexpr double metric_tolerance
void DressAuxIndex(const Eigen::MatrixXd &S)
Dress the auxiliary index with S: M_fill -> M_fill S.
std::vector< ResponsePoint > additional_response_
Eigen::MatrixXd AdditionalResponse(bool imaginary, double frequency) const
F^T Y F at this frequency, in the frame operator[] is in now.
void MultiplyRightWithAuxMatrixLeading(const Eigen::MatrixXd &U, Index full_slices, Index lead)
Rotate the auxiliary index of only the part of the tensor a consumer reads: all rows of slices [0,...
Eigen::MatrixXd pole_vectors_
Eigen::MatrixXd aux_frame_
Eigen::MatrixXd dressing_
const Eigen::MatrixXd * dft_orbitals_
bool AuxFrameIsOrthogonal() const
void Fill3cMO(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
void Initialize(Index basissize, Index mmin, Index mmax, Index nmin, Index nmax)
void MultiplyRightWithAuxMatrix(const Eigen::MatrixXd &matrix)
Eigen::MatrixXd AdditionalPoleVectors() const
The pole vectors in the frame operator[] is in now: F^T c_k.
std::vector< Eigen::MatrixXd > matrix_
void setExcludedHoles(Index k)
The first k occupied slices (levels rpamin .. rpamin+k-1) are left out of the RPA response sums,...
static Eigen::MatrixXd CoreExchange(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &mos, Index ncore, Index qpmin, Index qpmax)
void RotateOrbitals(const Eigen::MatrixXd &U, Index first, Index last)
Change the orbital basis of levels [first, last] for QSGW: phi_i = sum_k U_ki psi_k,...
const AOBasis * dftbasis_
void MultiplyInFillFrame(const Eigen::MatrixXd &A)
Eigen::MatrixXd ToCurrentAuxFrame(const Eigen::MatrixXd &K) const
Eigen::MatrixXd inv_sqrt_
Definition threecenter.h:60
Charge transport classes.
Definition ERIs.h:28
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
A response the RPA adds to its own sum over these integrals, epsilon(w) = 1 + X[M](w) + F^T Y(w) F,...