votca 2026-dev
Loading...
Searching...
No Matches
sigma_base.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// Standard includes
21#include <cmath>
22
23// Third party includes
24#include <boost/math/constants/constants.hpp>
25
26#include <algorithm>
27#include <vector>
28
29// VOTCA includes
31
32// Local VOTCA includes
35
36namespace votca {
37namespace xtp {
38
40 Index nrows,
41 const std::function<void(Index, Eigen::MatrixXd&)>& makeX) const {
42 const Index qpmin = opt_.qpmin - opt_.rpamin;
43 const Index auxsize = Mmn_.auxsize();
44 // X of a block of levels kept at once, and the chunk of all M_n gathered
45 // for one product; the block size sets how often the integrals are read.
46 const double kBlockBytes = block_bytes_;
47 const double kChunkBytes = chunk_bytes_;
48 const Index block = std::max<Index>(
49 1, std::min<Index>(qptotal_, Index(kBlockBytes / (8.0 * double(nrows) *
50 double(auxsize)))));
51 const Index chunk = std::max<Index>(
52 1, std::min<Index>(auxsize, Index(kChunkBytes / (8.0 * double(nrows) *
53 double(qptotal_)))));
54 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(qptotal_, qptotal_);
55 std::vector<Eigen::MatrixXd> X(static_cast<std::size_t>(block));
56 // chunk buffers kept across chunks (no reallocation per chunk)
57 Eigen::MatrixXd Mc;
58 Eigen::MatrixXd Xc;
59 for (Index m0 = 0; m0 < qptotal_; m0 += block) {
60 const Index nb = std::min(block, qptotal_ - m0);
61#pragma omp parallel for schedule(dynamic)
62 for (Index j = 0; j < nb; ++j) {
63 makeX(m0 + j, X[std::size_t(j)]);
64 }
65 for (Index s0 = 0; s0 < auxsize; s0 += chunk) {
66 const Index ns = std::min(chunk, auxsize - s0);
67 Mc.resize(nrows * ns, qptotal_);
68 Xc.resize(nrows * ns, nb);
69#pragma omp parallel for schedule(static)
70 for (Index n = 0; n < qptotal_; ++n) {
71 Eigen::Map<Eigen::MatrixXd>(Mc.col(n).data(), nrows, ns) =
72 Mmn_[n + qpmin].block(0, s0, nrows, ns);
73 }
74 for (Index j = 0; j < nb; ++j) {
75 Eigen::Map<Eigen::MatrixXd>(Xc.col(j).data(), nrows, ns) =
76 X[std::size_t(j)].middleCols(s0, ns);
77 }
78 A.middleRows(m0, nb).noalias() += Xc.transpose() * Mc;
79 }
80 }
81 return A;
82}
83
84Eigen::MatrixXd Sigma_base::CalcExchangeMatrix() const {
85 const Index occlevel = opt_.homo - opt_.rpamin + 1;
86 const Index qpmin = opt_.qpmin - opt_.rpamin;
87 // Exchange is with the bare v, which is M M^T only as long as the
88 // auxiliary frame is orthogonal. Once the index is dressed for an
89 // environment (TCMatrix_gwbse::DressAuxIndex) it takes the bare
90 // interaction in the current frame as an explicit kernel.
91 const bool bare = Mmn_.AuxFrameIsOrthogonal();
92 Eigen::MatrixXd v;
93 if (!bare) {
94 v = Mmn_.ToCurrentAuxFrame(
95 Eigen::MatrixXd::Identity(Mmn_.auxsize(), Mmn_.auxsize()));
96 }
97 // Sigma_x(m,n) = - sum_{i occ} sum_P M_m(i,P) [v] M_n(i,P)
98 const Eigen::MatrixXd A =
99 PairContraction(occlevel, [&](Index m, Eigen::MatrixXd& X) {
100 if (bare) {
101 X = Mmn_[m + qpmin].topRows(occlevel);
102 } else {
103 X = Mmn_[m + qpmin].topRows(occlevel) * v;
104 }
105 });
106 return -0.5 * (A + A.transpose());
107}
108
110 const Eigen::MatrixXd& R) const {
111 const Eigen::MatrixXd Rc = Mmn_.ToCurrentAuxFrame(R);
112 const Index occlevel = opt_.homo - opt_.rpamin + 1;
113 const Index qpmin = opt_.qpmin - opt_.rpamin;
114 // Row l of X_m is s_l M_m(l,:) R: the occupation sign folded in once.
115 const Eigen::MatrixXd A =
116 PairContraction(Mmn_.nsize(), [&](Index m, Eigen::MatrixXd& X) {
117 X = Mmn_[m + qpmin] * Rc;
118 X.topRows(occlevel) *= -1.0;
119 });
120 return 0.25 * (A + A.transpose());
121}
122
124 const Eigen::VectorXd& frequencies) const {
125 Eigen::VectorXd result = Eigen::VectorXd::Zero(qptotal_);
126#pragma omp parallel for schedule(dynamic)
127 for (Index gw_level = 0; gw_level < qptotal_; gw_level++) {
128 result(gw_level) =
129 CalcCorrelationDiagElement(gw_level, frequencies[gw_level]);
130 }
131 return result;
132}
133
135 const Eigen::VectorXd& frequencies) const {
136 Eigen::MatrixXd result = Eigen::MatrixXd::Zero(qptotal_, qptotal_);
137#pragma omp parallel for schedule(dynamic)
138 for (Index gw_level1 = 0; gw_level1 < qptotal_; gw_level1++) {
139 for (Index gw_level2 = gw_level1 + 1; gw_level2 < qptotal_; gw_level2++) {
140 double sigma_c = CalcCorrelationOffDiagElement(
141 gw_level1, gw_level2, frequencies[gw_level1], frequencies[gw_level2]);
142 result(gw_level2, gw_level1) = sigma_c;
143 }
144 }
145 result = result.selfadjointView<Eigen::Lower>();
146 return result;
147}
148
149} // namespace xtp
150} // namespace votca
virtual Eigen::MatrixXd CalcCorrelationOffDiag(const Eigen::VectorXd &frequencies) const
virtual double CalcCorrelationOffDiagElement(Index gw_level1, Index gw_level2, double frequency1, double frequency2) const =0
virtual double CalcCorrelationDiagElement(Index gw_level, double frequency) const =0
Eigen::MatrixXd PairContraction(Index nrows, const std::function< void(Index, Eigen::MatrixXd &)> &makeX) const
Definition sigma_base.cc:39
Eigen::VectorXd CalcCorrelationDiag(const Eigen::VectorXd &frequencies) const
TCMatrix_gwbse & Mmn_
Definition sigma_base.h:142
Eigen::MatrixXd CalcReactionFieldMatrix(const Eigen::MatrixXd &R) const
Static self-energy of an environment reaction field: the COH+SEX pair for v_reac, in the QP window.
Eigen::MatrixXd CalcExchangeMatrix() const
Definition sigma_base.cc:84
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