41 throw std::runtime_error(
42 "InactiveScreening: no inactive density; run the projection-based "
48 if (DB.rows() != C.rows()) {
49 throw std::runtime_error(
50 "InactiveScreening: inactive density and MOs have different AO "
51 "dimensions (truncated basis is not supported)");
56 const Eigen::MatrixXd SCocc = overlap.
Matrix() * C.leftCols(nocc);
60 const Eigen::MatrixXd Q = 0.5 * SCocc.transpose() * DB * SCocc;
63 const Eigen::MatrixXd UB = q.
vectors.rightCols(nB);
69 const Eigen::MatrixXd FB = UB.transpose() *
e.head(nocc).asDiagonal() * UB;
71 if (ncore < 0 || ncore > nB) {
72 throw std::runtime_error(
"InactiveScreening: " + std::to_string(ncore) +
73 " core levels requested, but the inactive "
75 std::to_string(nB) +
" occupied orbitals");
77 const Index nkeep = nB - ncore;
80 const Index nvirt = C.cols() - nocc;
88 const Eigen::MatrixXd& occupied,
const Eigen::VectorXd& occupied_energies,
89 const Eigen::MatrixXd& virtuals,
const Eigen::VectorXd& virtual_energies,
90 double chunk_bytes,
const std::vector<double>& imaginary_frequencies,
91 const Eigen::MatrixXd& inv_sqrt) {
93 const Index nocc = occupied.cols();
94 const Index nvirt = virtuals.cols();
95 std::vector<Eigen::MatrixXd> X(imaginary_frequencies.size(),
96 Eigen::MatrixXd::Zero(naux, naux));
97 if (nocc == 0 || nvirt == 0) {
100 const double bytes_per_level = 8.0 * double(nvirt) * double(naux);
101 const Index chunk = std::clamp<Index>(
102 static_cast<Index>(chunk_bytes / bytes_per_level), 1, nocc);
105 Eigen::MatrixXd metric = inv_sqrt;
106 for (
Index first = 0; first < nocc; first += chunk) {
107 const Index nc = std::min(chunk, nocc - first);
108 Eigen::MatrixXd orbitals(occupied.rows(), nc + nvirt);
109 orbitals << occupied.middleCols(first, nc), virtuals;
112 Mmn.
Initialize(naux, 0, nc - 1, nc, nc + nvirt - 1);
113 if (metric.size() == 0) {
114 Mmn.
Fill(auxbasis, dftbasis, orbitals);
117 Mmn.
Fill(auxbasis, dftbasis, orbitals, metric);
119 for (std::size_t f = 0; f < imaginary_frequencies.size(); ++f) {
120 const double w2 = imaginary_frequencies[f] * imaginary_frequencies[f];
123 [&](
Index m, Eigen::VectorXd& w) {
124 const Eigen::ArrayXd d =
125 virtual_energies.array() - occupied_energies(first + m);
126 w = 4.0 * d / (d.square() + w2);
136 const Eigen::MatrixXd& occupied,
const Eigen::VectorXd& occupied_energies,
137 const Eigen::MatrixXd& virtuals,
const Eigen::VectorXd& virtual_energies,
138 double chunk_bytes,
double imaginary_frequency) {
139 return Responses(auxbasis, dftbasis, occupied, occupied_energies, virtuals,
140 virtual_energies, chunk_bytes, {imaginary_frequency})
145 const std::string& name) {
146 if (name ==
"none") {
148 }
else if (name ==
"block") {
150 }
else if (name ==
"exact") {
153 throw std::runtime_error(
"inactive_screening: unknown mode '" + name +
154 "' (none, block, exact)");
170 const Eigen::MatrixXd&
L = full.
getLMOs();
173 throw std::runtime_error(
174 "inactive_screening_radius needs the localized occupied orbitals of "
175 "the full calculation (localize step)");
179 const Eigen::MatrixXd&
S = overlap.
Matrix();
180 const Eigen::MatrixXd SL =
S *
L;
182 const Eigen::VectorXd q =
187 Eigen::MatrixXd R(3, atoms.
size());
188 for (
Index a = 0; a < atoms.
size(); ++a) {
189 R.col(a) = atoms[a].
getPos();
191 const Eigen::MatrixXd centroid =
192 R * pop * pop.colwise().sum().cwiseInverse().asDiagonal();
193 std::vector<Index> active;
194 std::vector<Index> inactive;
195 for (
Index k = 0; k < nocc; ++k) {
196 (q(k) > 0.5 ? inactive : active).push_back(k);
198 std::vector<Index> near;
199 for (
Index k : inactive) {
200 double dmin = std::numeric_limits<double>::max();
201 for (
Index j : active) {
202 dmin = std::min(dmin, (centroid.col(k) - centroid.col(j)).norm());
204 if (dmin <= radius) {
208 n_far =
Index(inactive.size() - near.size());
209 Eigen::MatrixXd Ln(
L.rows(),
Index(near.size()));
210 for (
Index i = 0; i <
Index(near.size()); ++i) {
211 Ln.col(i) =
L.col(near[std::size_t(i)]);
216 const Eigen::MatrixXd CL = SC.leftCols(nocc).transpose() * Ln;
217 const Eigen::MatrixXd FL =
218 CL.transpose() * full.
MOs().
eigenvalues().head(nocc).asDiagonal() * CL;
238 throw std::runtime_error(
239 "inactive_screening is not implemented for truncated active "
240 "regions (dft_in_dft.truncate_basis).");
262 throw std::runtime_error(
263 "inactive_screening_radius is only available for "
264 "inactive_screening block");
280 const Index nA = n_active_occupied;
282 throw std::runtime_error(
283 "CompleteGOrbitals: the embedded MOs have fewer levels than the "
284 "active occupied plus virtual ones");
300 Index homo,
double chunk_bytes,
301 double imaginary_frequency,
302 Index first_active) {
307 const Index nA = homo + 1 - first_active;
309 if (homo + 1 + nvirt > embedded.
eigenvectors().cols()) {
310 throw std::runtime_error(
311 "inactive_screening: the embedded MOs have fewer levels than the "
312 "full calculation's occupied and virtual ones");
318 embedded.
eigenvalues().segment(homo + 1, nvirt), chunk_bytes,
319 imaginary_frequency);
321 return 0.5 * (Y + Y.transpose());
327 const Eigen::VectorXd& rpa_energies,
Index homo,
Index rpamin,
Index rpamax,
328 double chunk_bytes,
const std::vector<double>& imaginary_frequencies,
329 double virtual_cutoff) {
333 Index nvirt = std::min(rpamax - homo, input.
virtuals.cols());
334 if (virtual_cutoff > 0.0 && nvirt > 0) {
341 std::vector<Eigen::MatrixXd> Y =
Responses(
344 chunk_bytes, imaginary_frequencies, active.
InvSqrt());
349 throw std::runtime_error(
350 "inactive_screening: X_A must be built before the inactive "
351 "response is attached to the integrals");
356 for (std::size_t f = 0; f < Y.size(); ++f) {
357 const double w2 = imaginary_frequencies[f] * imaginary_frequencies[f];
359 homo + 1 - first, active.
auxsize(),
360 [&](
Index m, Eigen::VectorXd& w) {
361 const Eigen::ArrayXd d =
362 rpa_energies.segment(homo + 1 - rpamin, nvirt).array() -
363 rpa_energies(first + m - rpamin);
364 w = 4.0 * d / (d.square() + w2);
367 use(active[first + m - mmin].middleRows(homo + 1 - nmin, nvirt));
371 for (Eigen::MatrixXd& y : Y) {
372 y = 0.5 * (y + y.transpose());
378 const Eigen::MatrixXd& Y0,
const Eigen::MatrixXd& Yi,
double w_i,
382 const double cut = tolerance * std::max(y.values.maxCoeff(), 0.0);
383 const Index keep = (y.values.array() > cut).count();
384 fit.
dropped = y.values.size() - keep;
389 const Eigen::VectorXd s = y.values.tail(keep);
390 const Eigen::MatrixXd U = y.vectors.rightCols(keep);
391 const Eigen::MatrixXd
P = U * s.cwiseSqrt().cwiseInverse().asDiagonal();
392 const Eigen::MatrixXd Yp =
P.transpose() * Yi *
P;
395 const double l_min = 1
e-6 / (1
e-6 + w_i * w_i);
396 const double l_max = 1e6 / (1e6 + w_i * w_i);
397 Eigen::VectorXd l = q.
values;
398 for (
Index k = 0; k < l.size(); ++k) {
399 if (l(k) <= 0.0 || l(k) >= 1.0) {
406 l(k) = std::clamp(l(k), l_min, l_max);
409 fit.
energies = w_i * (l.array() / (1.0 - l.array())).sqrt();
410 const Eigen::MatrixXd
G = U * s.cwiseSqrt().asDiagonal() * q.
vectors;
411 fit.
vectors =
G * (0.25 * fit.
energies.array()).sqrt().matrix().asDiagonal();
416 const Eigen::MatrixXd& R_env) {
417 const Index n = Y.rows();
418 const Eigen::MatrixXd one = Eigen::MatrixXd::Identity(n, n);
419 Eigen::MatrixXd u_inv = one + Y;
420 if (R_env.size() > 0) {
421 if (R_env.rows() != n) {
422 throw std::runtime_error(
423 "InactiveScreening: environment reaction field and inactive "
424 "response have different auxiliary dimensions");
430 return 0.5 * (R + R.transpose());
Container to hold Basisfunctions for all atoms.
Index AOBasisSize() const
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
static Eigen::MatrixXd AtomPopulations(const Orbitals &orb, const Eigen::MatrixXd &mos)
const Eigen::Vector3d & getPos() const
static Eigen::MatrixXd ReactionField(const Eigen::MatrixXd &Y, const Eigen::MatrixXd &R_env=Eigen::MatrixXd())
The reaction field R with 1 + R = ((1 + R_env)^-1 + Y)^-1; without an environment (R_env empty) R = (...
static PoleFit FitPoles(const Eigen::MatrixXd &Y0, const Eigen::MatrixXd &Yi, double w_i, double tolerance=1e-10)
static tools::EigenSystem CompleteGOrbitals(const tools::EigenSystem &embedded, Index n_active_occupied, const InactiveScreeningInput &input)
The MOs GW-BSE works with for complete G: the inactive occupied orbitals (semicanonical,...
static Eigen::MatrixXd Kernel(const AOBasis &auxbasis, const AOBasis &dftbasis, const InactiveScreeningInput &input, const tools::EigenSystem &embedded, Index homo, double chunk_bytes, double imaginary_frequency=0.0, Index first_active=0)
Y, the response of the inactive region: X_B (block) or X_full - X_A (exact), static or at an imaginar...
static std::vector< Eigen::MatrixXd > Kernels(const AOBasis &auxbasis, const AOBasis &dftbasis, const InactiveScreeningInput &input, const TCMatrix_gwbse &active, const Eigen::VectorXd &rpa_energies, Index homo, Index rpamin, Index rpamax, double chunk_bytes, const std::vector< double > &imaginary_frequencies, double virtual_cutoff=0.0)
Y at several imaginary frequencies, as GWBSE uses it: one integral pass for the full (exact) or inact...
static std::vector< Eigen::MatrixXd > Responses(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &occupied, const Eigen::VectorXd &occupied_energies, const Eigen::MatrixXd &virtuals, const Eigen::VectorXd &virtual_energies, double chunk_bytes, const std::vector< double > &imaginary_frequencies, const Eigen::MatrixXd &inv_sqrt=Eigen::MatrixXd())
static InactiveSpace BuildSpace(const Orbitals &full, Index ncore=0)
The inactive space from the full reference orbitals and the inactive density stored by DFTEngine::Eva...
static Eigen::MatrixXd Response(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &occupied, const Eigen::VectorXd &occupied_energies, const Eigen::MatrixXd &virtuals, const Eigen::VectorXd &virtual_energies, double chunk_bytes=4e9, double imaginary_frequency=0.0)
X = sum_ia 4/(e_a - e_i) M_ia M_ia^T over the given occupied and virtual orbitals,...
static InactiveScreeningInput Prepare(const Orbitals &full, InactiveScreeningInput::Mode mode, bool complete_g=false, double radius=0.0)
The input for GWBSE, from the full reference orbitals (before they are replaced by the embedded ones)...
static InactiveSpace NearSpace(const Orbitals &full, double radius, Index &n_far)
Block mode with a spatial cutoff: the inactive LMOs whose charge centroid lies within radius (bohr) o...
Container for molecular orbitals and derived one-particle data.
Index getNumberOfAlphaElectrons() const
Return the stored number of alpha electrons.
std::string getCalculationType() const
Return the stored calculation-type tag.
const Eigen::MatrixXd & getLMOs() const
Return localized molecular orbitals, if available.
const Eigen::MatrixXd & getInactiveDensity() const
Return the inactive-region density matrix used in embedding workflows.
const tools::EigenSystem & MOs() const
Return read-only access to alpha/restricted molecular orbitals.
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
const AOBasis & getDftBasis() const
Return the DFT AO basis, throwing if it has not been initialized.
void Fill(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
Index ExcludedHoles() const
Index AdditionalPoleCount() const
void Initialize(Index basissize, Index mmin, Index mmax, Index nmin, Index nmax)
bool HasAdditionalResponse() const
const Eigen::MatrixXd & InvSqrt() const
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)
Charge transport classes.
SymmetricEigenSystem SymmetricEigen(const Eigen::MatrixXd &A, bool vectors=true)
With vectors = false only the eigenvalues are computed.
Eigen::MatrixXd InverseSPD(const Eigen::MatrixXd &A)
Provides a means for comparing floating point numbers.
Poles in the form of transitions that reproduce a response at zero and at one imaginary frequency: Y(...
Occupied orbitals of the inactive region of a projection-based embedding, with the virtual space they...
Eigen::VectorXd occupied_energies
Eigen::VectorXd virtual_energies