votca 2026-dev
Loading...
Searching...
No Matches
threecenter.h
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#pragma once
21#ifndef VOTCA_XTP_THREECENTER_H
22#define VOTCA_XTP_THREECENTER_H
23
24// Local VOTCA includes
25#include <utility>
26#include <vector>
27
28#include "aobasis.h"
29#include "eigen.h"
30#include "symmetric_matrix.h"
31
38
39namespace votca {
40namespace xtp {
41
42// due to different requirements for the data format for DFT and GW we have two
43// different classes TCMatrix_gwbse and TCMatrix_dft which inherit from TCMatrix
44class TCMatrix {
45
46 public:
47 TCMatrix() = default;
48 virtual ~TCMatrix() = default;
49 // the virtual destructor would otherwise suppress moving: moving the DFT
50 // tensor (tens of GB) must not fall back to a copy
51 TCMatrix(const TCMatrix&) = default;
52 TCMatrix(TCMatrix&&) = default;
53 TCMatrix& operator=(const TCMatrix&) = default;
56 enum class SpinChannel { Alpha, Beta };
57
58 protected:
60 Eigen::MatrixXd inv_sqrt_;
61};
62
77class TCMatrix_dft final : public TCMatrix {
78 public:
81 void Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
82 double pair_threshold = 0.0, Index build_columns = kBuildColumns);
83
85 Index size() const { return Index(data_.cols()); }
87 Index basissize() const { return basissize_; }
88
90 Eigen::MatrixXd FullMatrix(Index P) const;
91
95 void FillFullMatrix(Index P, Eigen::MatrixXd& full) const;
96
101 Eigen::VectorXd PackWeighted(const Eigen::MatrixXd& full) const;
102
105 Eigen::MatrixXd Unpack(const Eigen::VectorXd& packed) const;
106
108 const Eigen::MatrixXd& Data() const { return data_; }
109
111 Index StoredPairs() const { return Index(data_.rows()); }
112 Index AllPairs() const { return basissize_ * (basissize_ + 1) / 2; }
113
114 double MetricSeconds() const { return metric_seconds_; }
115
116 private:
125
128 std::vector<std::vector<Index>> SignificantPairs(
129 const AOBasis& dftbasis, const Eigen::MatrixXd& metric,
130 double pair_threshold) const;
131 void SetupLayout(const AOBasis& dftbasis,
132 const std::vector<std::vector<Index>>& kept);
133
134 Eigen::MatrixXd data_;
135 std::vector<Run> runs_;
136 // column mu starts at col_offset_[mu] in a data column
137 std::vector<Index> col_offset_;
139 double metric_seconds_ = 0.0;
140
143 static constexpr Index kBuildColumns = 512;
144};
145
146class TCMatrix_gwbse final : public TCMatrix {
147 public:
148 // Eigenvalue tolerance of the metric (Pseudo_InvSqrt_GWBSE) folded into
149 // the stored integrals. Shared with EnvironmentScreening::Metric, which
150 // must build the same T.
151 static constexpr double metric_tolerance = 5e-7;
152
153 // returns one level as a constant reference
154 const Eigen::MatrixXd& operator[](Index i) const { return matrix_[i]; }
155
156 // returns one level as a reference
157 Eigen::MatrixXd& operator[](Index i) { return matrix_[i]; }
158 // returns auxbasissize
159 Index auxsize() const { return auxbasissize_; }
160
161 Index get_mmin() const { return mmin_; }
162
163 Index get_mmax() const { return mmax_; }
164
165 Index get_nmin() const { return nmin_; }
166
167 Index get_nmax() const { return nmax_; }
168
169 Index msize() const { return mtotal_; }
170
171 Index nsize() const { return ntotal_; }
172
178 static Eigen::MatrixXd CoreExchange(const AOBasis& auxbasis,
179 const AOBasis& dftbasis,
180 const Eigen::MatrixXd& mos, Index ncore,
181 Index qpmin, Index qpmax);
182
183 void Initialize(Index basissize, Index mmin, Index mmax, Index nmin,
184 Index nmax);
185
186 void Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
187 const Eigen::MatrixXd& dft_orbitals);
190 void Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
191 const Eigen::MatrixXd& dft_orbitals,
192 const Eigen::MatrixXd& inv_sqrt);
193 // Rebuilds ThreeCenterIntegrals, only works if the original basisobjects
194 // still exist
196
197 void MultiplyRightWithAuxMatrix(const Eigen::MatrixXd& matrix);
198
212 void MultiplyRightWithAuxMatrixLeading(const Eigen::MatrixXd& U,
213 Index full_slices, Index lead);
215 bool PartiallyRotated() const { return partially_rotated_; }
216
217 // The metric that was folded into the stored integrals at Fill time:
218 // T = Pseudo_InvSqrt_GWBSE, with T T^T = V^-1 (pseudo-inverse) and
219 // T^T V T the projector onto the retained auxiliary functions. After
220 // Fill, operator[] holds M = (mn|Q) T, so the bare Coulomb operator
221 // exists only implicitly, as M M^T.
222 //
223 // Kept because anything that must act on the Coulomb interaction in
224 // the same space as M needs it. The environment reaction field is the
225 // case in point: a kernel B between auxiliary functions, taken as charge
226 // densities, enters as M (T^T B T) M^T -- see EnvironmentScreening.
227 //
228 // IN THE FILL-TIME AUXILIARY BASIS. MultiplyRightWithAuxMatrix, which the
229 // PPM and the BSE both call with an eigenvector matrix U, rotates the
230 // auxiliary index of M. This matrix is not rotated with it: a kernel
231 // built from it has to be brought into the current frame with
232 // ToCurrentAuxFrame before it meets operator[]. Refilled, and so reset,
233 // by every Fill and Rebuild.
234 const Eigen::MatrixXd& InvSqrt() const { return inv_sqrt_; }
235
236 // The auxiliary frame operator[] is currently in. Every
237 // MultiplyRightWithAuxMatrix since the last Fill or Rebuild is recorded
238 // here, so that operator[] holds M_fill * AuxFrame() with M_fill the
239 // integrals as Fill left them. Empty means the identity: nothing has
240 // rotated them yet.
241 //
242 // The bare Coulomb interaction does not care -- it is the identity in
243 // every orthonormal frame, which is why nothing tracked this before. A
244 // kernel K between the columns of M_fill does care: M_fill K M_fill^T is
245 // M K' M^T only with K' = ToCurrentAuxFrame(K).
246 const Eigen::MatrixXd& AuxFrame() const { return aux_frame_; }
247
248 // For code that snapshots slices through operator[] and later writes
249 // them back, which bypasses the bookkeeping above: restore the frame
250 // that was current when the snapshot was taken, alongside the slices.
251 void RestoreAuxFrame(const Eigen::MatrixXd& frame) {
252 aux_frame_ = frame;
253 aux_frame_known_ = true;
254 }
255
256 // K, a kernel between the columns of M_fill, as it must be applied to
257 // operator[] now: F^-1 K F^-T with F = AuxFrame(), which is F^T K F for
258 // the orthogonal eigenvector matrices the PPM and the BSE rotate with.
259 // Throws if the frame is not known -- after a non-square
260 // MultiplyRightWithAuxMatrix, which is not a change of frame.
261 Eigen::MatrixXd ToCurrentAuxFrame(const Eigen::MatrixXd& K) const;
262
263 // Whether AuxFrame() is orthogonal, so that the bare interaction M M^T is
264 // what it was at Fill time. False once the index has been dressed.
265 bool AuxFrameIsOrthogonal() const;
266
285 void DressAuxIndex(const Eigen::MatrixXd& S);
286 // Back to M_fill (times whatever rotations happened since), bare v.
287 void UndressAuxIndex();
288 bool Dressed() const { return dressing_.size() > 0; }
290 const Eigen::MatrixXd& Dressing() const { return dressing_; }
291
302 bool imaginary = false;
303 double frequency = 0.0;
304 Eigen::MatrixXd Y;
305 };
306 void setAdditionalResponse(std::vector<ResponsePoint> points) {
307 additional_response_ = std::move(points);
308 }
309 bool HasAdditionalResponse() const { return !additional_response_.empty(); }
311 Eigen::MatrixXd AdditionalResponse(bool imaginary, double frequency) const;
312
323 void setAdditionalPoles(Eigen::VectorXd energies, Eigen::MatrixXd vectors) {
324 pole_energies_ = std::move(energies);
325 pole_vectors_ = std::move(vectors);
326 }
327 Index AdditionalPoleCount() const { return pole_energies_.size(); }
328 const Eigen::VectorXd& AdditionalPoleEnergies() const {
329 return pole_energies_;
330 }
331
332 Eigen::MatrixXd AdditionalPoleVectors() const;
333
344 void setExcludedHoles(Index k);
346
362 void RotateOrbitals(const Eigen::MatrixXd& U, Index first, Index last);
363
364 private:
365 // store vector of matrices
366 std::vector<Eigen::MatrixXd> matrix_;
367
368 // band summation indices
376
377 Eigen::MatrixXd aux_frame_; // empty: identity
378 bool aux_frame_known_ = true;
379 bool partially_rotated_ = false;
380 Eigen::MatrixXd dressing_; // S of DressAuxIndex; empty: bare
381 std::vector<ResponsePoint> additional_response_;
382 Eigen::VectorXd pole_energies_;
384 Eigen::MatrixXd pole_vectors_;
385
386 // M_fill -> M_fill A, whatever frame the integrals are in.
387 void MultiplyInFillFrame(const Eigen::MatrixXd& A);
388
389 const AOBasis* auxbasis_ = nullptr;
390 const AOBasis* dftbasis_ = nullptr;
391 const Eigen::MatrixXd* dft_orbitals_ = nullptr;
392
393 void Fill3cMO(const AOBasis& auxbasis, const AOBasis& dftbasis,
394 const Eigen::MatrixXd& dft_orbitals);
395};
396
409
410} // namespace xtp
411} // namespace votca
412
413#endif // VOTCA_XTP_THREECENTER_H
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
RI three-centre tensor of the DFT code, B_P,mu nu = sum_Q V^-1/2_PQ (Q|mu nu).
Definition threecenter.h:77
std::vector< Index > col_offset_
std::vector< Run > runs_
std::vector< std::vector< Index > > SignificantPairs(const AOBasis &dftbasis, const Eigen::MatrixXd &metric, double pair_threshold) const
Index size() const
Number of aux functions.
Definition threecenter.h:85
const Eigen::MatrixXd & Data() const
The stored tensor, one column per aux function.
Eigen::VectorXd PackWeighted(const Eigen::MatrixXd &full) const
void Fill(const AOBasis &auxbasis, const AOBasis &dftbasis, double pair_threshold=0.0, Index build_columns=kBuildColumns)
double MetricSeconds() 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
Index StoredPairs() const
Stored pairs (nu <= mu) and all pairs N(N+1)/2.
Eigen::MatrixXd Unpack(const Eigen::VectorXd &packed) const
Eigen::MatrixXd data_
Index basissize() const
Number of basis functions.
Definition threecenter.h:87
static constexpr Index kBuildColumns
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_
Index AdditionalPoleCount() const
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_
void RestoreAuxFrame(const Eigen::MatrixXd &frame)
bool AuxFrameIsOrthogonal() const
void Fill3cMO(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
void setAdditionalPoles(Eigen::VectorXd energies, Eigen::MatrixXd vectors)
A response given as poles, in the form of transitions: Y(i w) = sum_k 4 W_k/(W_k^2 + w^2) c_k c_k^T,...
void Initialize(Index basissize, Index mmin, Index mmax, Index nmin, Index nmax)
void MultiplyRightWithAuxMatrix(const Eigen::MatrixXd &matrix)
bool PartiallyRotated() const
Whether only a leading part was rotated since the last Fill.
const Eigen::VectorXd & AdditionalPoleEnergies() const
void setAdditionalResponse(std::vector< ResponsePoint > points)
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)
const Eigen::MatrixXd & Dressing() const
S of the current dressing (empty if bare).
Eigen::VectorXd pole_energies_
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_
const Eigen::MatrixXd & operator[](Index i) const
Eigen::MatrixXd & operator[](Index i)
void MultiplyInFillFrame(const Eigen::MatrixXd &A)
bool HasAdditionalResponse() const
const Eigen::MatrixXd & InvSqrt() const
const Eigen::MatrixXd & AuxFrame() const
Eigen::MatrixXd ToCurrentAuxFrame(const Eigen::MatrixXd &K) const
TCMatrix & operator=(const TCMatrix &)=default
Eigen::MatrixXd inv_sqrt_
Definition threecenter.h:60
TCMatrix(TCMatrix &&)=default
virtual ~TCMatrix()=default
TCMatrix(const TCMatrix &)=default
Index Removedfunctions() const
Definition threecenter.h:55
TCMatrix & operator=(TCMatrix &&)=default
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,...
TCMatrix_gwbse & operator[](TCMatrix::SpinChannel spin)
const TCMatrix_gwbse & operator[](TCMatrix::SpinChannel spin) const