votca 2026-dev
Loading...
Searching...
No Matches
gwbse.cc
Go to the documentation of this file.
1
2
3/*
4 * Copyright 2009-2023 The VOTCA Development Team
5 * (http://www.votca.org)
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License")
8 *
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22// Standard includes
23#include <algorithm>
24#include <cmath>
25
26// Third party includes
27#include <boost/algorithm/string.hpp>
28#include <boost/format.hpp>
29
30// VOTCA includes
31#include <stdexcept>
33#include <votca/tools/version.h>
34
35// Local VOTCA includes
37#include "votca/xtp/basisset.h"
38#include "votca/xtp/bse.h"
39#include "votca/xtp/citations.h"
41#include "votca/xtp/gwbse.h"
42#include "votca/xtp/logger.h"
44#include "votca/xtp/orbitals.h"
45#include "votca/xtp/ppm.h"
46#include "votca/xtp/rpa_uks.h"
49#include "votca/xtp/vxc_grid.h"
51
52using std::flush;
53namespace votca {
54namespace xtp {
55
57 Index ignored_corelevels = 0;
58 // projection-based embedding: the active space holds only the cores of
59 // the active atoms, counted when the active region was set up
60 if (orbitals_.getActiveCoreLevels() >= 0 &&
61 2 * orbitals_.getNumberOfAlphaElectrons() ==
62 orbitals_.getNumOfActiveElectrons()) {
63 return orbitals_.getActiveCoreLevels();
64 }
65 if (!orbitals_.hasECPName()) {
66 ECPBasisSet basis;
67 basis.Load("corelevels");
68 Index coreElectrons = 0;
69 for (const auto& atom : orbitals_.QMAtoms()) {
70 coreElectrons += basis.getElement(atom.getElement()).getNcore();
71 }
72 ignored_corelevels = coreElectrons / 2;
73 }
74 return ignored_corelevels;
75}
76
78
79 // getting level ranges
80 Index rpamax = 0;
81 Index rpamin = 0; // never changes
82 Index qpmin = 0;
83 Index qpmax = 0;
84 Index bse_vmin = 0;
85 Index bse_cmax = 0;
86
87 const bool is_uks = orbitals_.hasUnrestrictedOrbitals();
88 Index homo = orbitals_.getHomo(); // indexed from 0
89 if (is_uks) {
90 homo = std::max(orbitals_.getHomoAlpha(), orbitals_.getHomoBeta());
91 }
92 Index num_of_levels = orbitals_.getBasisSetSize();
93 Index num_of_occlevels = orbitals_.getNumberOfAlphaElectrons();
94 if (is_uks) {
95 num_of_occlevels = std::max(orbitals_.getNumberOfAlphaElectrons(),
96 orbitals_.getNumberOfBetaElectrons());
97 }
98
99 std::string ranges = options.get(".ranges").as<std::string>();
100
101 // now check validity, and get rpa, qp, and bse level ranges accordingly
102
103 if (ranges == "factor") {
104
105 double rpamaxfactor = options.get(".rpamax").as<double>();
106 rpamax = Index(rpamaxfactor * double(num_of_levels)) -
107 1; // total number of levels
108
109 double qpminfactor = options.get(".qpmin").as<double>();
110 qpmin =
111 num_of_occlevels - Index(qpminfactor * double(num_of_occlevels)) - 1;
112
113 double qpmaxfactor = options.get(".qpmax").as<double>();
114 qpmax =
115 num_of_occlevels + Index(qpmaxfactor * double(num_of_occlevels)) - 1;
116
117 double bseminfactor = options.get(".bsemin").as<double>();
118 bse_vmin =
119 num_of_occlevels - Index(bseminfactor * double(num_of_occlevels)) - 1;
120
121 double bsemaxfactor = options.get(".bsemax").as<double>();
122 bse_cmax =
123 num_of_occlevels + Index(bsemaxfactor * double(num_of_occlevels)) - 1;
124
125 } else if (ranges == "explicit") {
126 // get explicit numbers
127 rpamax = options.get(".rpamax").as<Index>();
128 qpmin = options.get(".qpmin").as<Index>();
129 qpmax = options.get(".qpmax").as<Index>();
130 bse_vmin = options.get(".bsemin").as<Index>();
131 bse_cmax = options.get(".bsemax").as<Index>();
132 } else if (ranges == "default") {
133 rpamax = num_of_levels - 1;
134 qpmin = 0;
135 qpmax = 3 * homo + 1;
136 bse_vmin = 0;
137 bse_cmax = 3 * homo + 1;
138 } else if (ranges == "full") {
139 rpamax = num_of_levels - 1;
140 qpmin = 0;
141 qpmax = num_of_levels - 1;
142 bse_vmin = 0;
143 bse_cmax = num_of_levels - 1;
144 }
145 std::string ignore_corelevels =
146 options.get(".ignore_corelevels").as<std::string>();
147
149 options.ifExistsReturnElseReturnDefault<std::string>(
150 ".inactive_screening", "none"));
151 // 0: from the --memory budget when the response is built
153 ".inactive_screening_memory", 0.0);
155 options.ifExistsReturnElseReturnDefault<bool>(".stream_rpa", false);
157 ".inactive_screening_virtual_cutoff", 0.0) *
160 ignore_corelevels != "none") {
161 // CountCoreLevels counts the cores of all atoms, the embedded active
162 // space holds only those of the active region
163 throw std::runtime_error(
164 "inactive_screening is not implemented together with "
165 "ignore_corelevels.");
166 }
167
168 if (ignore_corelevels == "RPA" || ignore_corelevels == "GW" ||
169 ignore_corelevels == "BSE") {
170 Index ignored_corelevels = CountCoreLevels();
171 if (ignore_corelevels == "RPA") {
172 rpamin = ignored_corelevels;
173 }
174 if (ignore_corelevels == "GW" || ignore_corelevels == "RPA") {
175 if (qpmin < ignored_corelevels) {
176 qpmin = ignored_corelevels;
177 }
178 }
179 if (ignore_corelevels == "GW" || ignore_corelevels == "RPA" ||
180 ignore_corelevels == "BSE") {
181 if (bse_vmin < ignored_corelevels) {
182 bse_vmin = ignored_corelevels;
183 }
184 }
185
187 << TimeStamp() << " Ignoring " << ignored_corelevels
188 << " core levels for " << ignore_corelevels << " and beyond." << flush;
189 }
190
191 // check maximum and minimum sizes
192 if (rpamax >= num_of_levels) {
193 rpamax = num_of_levels - 1;
194 }
195 if (qpmax >= num_of_levels) {
196 qpmax = num_of_levels - 1;
197 }
198 if (bse_cmax >= num_of_levels) {
199 bse_cmax = num_of_levels - 1;
200 }
201 if (bse_vmin < 0) {
202 bse_vmin = 0;
203 }
204 if (qpmin < 0) {
205 qpmin = 0;
206 }
207
208 Index bse_vmax = homo;
209 Index bse_cmin = homo + 1;
210
211 // Energy caps on the GW and BSE windows (eV above the DFT LUMO / below the
212 // DFT HOMO; <= 0: off). They only shrink the windows set above, and never
213 // cut a (near-)degenerate set of levels.
214 {
215 const double qp_emax =
216 options.ifExistsReturnElseReturnDefault<double>(".qp_emax", 0.0);
217 const double qp_emin =
218 options.ifExistsReturnElseReturnDefault<double>(".qp_emin", 0.0);
219 const double bse_emax =
220 options.ifExistsReturnElseReturnDefault<double>(".bse_emax", 0.0);
221 const double bse_emin =
222 options.ifExistsReturnElseReturnDefault<double>(".bse_emin", 0.0);
223 std::vector<const Eigen::VectorXd*> spins{&orbitals_.MOs().eigenvalues()};
224 std::vector<Index> homos{is_uks ? orbitals_.getHomoAlpha() : homo};
225 if (is_uks) {
226 spins.push_back(&orbitals_.MOs_beta().eigenvalues());
227 homos.push_back(orbitals_.getHomoBeta());
228 }
229 // levels closer than this count as degenerate and stay together
230 constexpr double degenerate = 5e-4; // Ha
231 // highest level (over the spins) within cap_ev above the LUMO
232 auto upper = [&](double cap_ev) {
233 Index last = homo + 1;
234 for (std::size_t s = 0; s < spins.size(); ++s) {
235 const Eigen::VectorXd& e = *spins[s];
236 const double limit = e(homos[s] + 1) + cap_ev * tools::conv::ev2hrt;
237 Index i = homos[s] + 1;
238 while (i + 1 < e.size() && e(i + 1) <= limit) {
239 ++i;
240 }
241 const double edge = e(i); // keep the set degenerate with it
242 while (i + 1 < e.size() && e(i + 1) - edge < degenerate) {
243 ++i;
244 }
245 last = std::max(last, i);
246 }
247 return last;
248 };
249 // lowest level (over the spins) within cap_ev below the HOMO
250 auto lower = [&](double cap_ev) {
251 Index first = homo;
252 for (std::size_t s = 0; s < spins.size(); ++s) {
253 const Eigen::VectorXd& e = *spins[s];
254 const double limit = e(homos[s]) - cap_ev * tools::conv::ev2hrt;
255 Index i = homos[s];
256 while (i - 1 >= 0 && e(i - 1) >= limit) {
257 --i;
258 }
259 const double edge = e(i);
260 while (i - 1 >= 0 && edge - e(i - 1) < degenerate) {
261 --i;
262 }
263 first = std::min(first, i);
264 }
265 return first;
266 };
267 auto report = [&](const std::string& what, const std::string& option,
268 double cap, Index before, Index after) {
270 << TimeStamp() << " " << what << " capped by " << option << " = "
271 << cap << " eV: " << before << " -> " << after << " ("
272 << std::abs(before - after) << " levels removed)" << flush;
273 };
274 if (qp_emax > 0.0) {
275 const Index cap = std::min(qpmax, upper(qp_emax));
276 report("GW window top", "qp_emax", qp_emax, qpmax, cap);
277 qpmax = cap;
278 }
279 if (qp_emin > 0.0) {
280 const Index cap = std::max(qpmin, lower(qp_emin));
281 report("GW window bottom", "qp_emin", qp_emin, qpmin, cap);
282 qpmin = cap;
283 }
284 if (bse_emax > 0.0) {
285 const Index cap = std::min(bse_cmax, upper(bse_emax));
286 report("BSE window top", "bse_emax", bse_emax, bse_cmax, cap);
287 bse_cmax = cap;
288 }
289 if (bse_emin > 0.0) {
290 const Index cap = std::max(bse_vmin, lower(bse_emin));
291 report("BSE window bottom", "bse_emin", bse_emin, bse_vmin, cap);
292 bse_vmin = cap;
293 }
294 }
295
296 if (inactive_.complete_g) {
297 // the inactive occupied levels in front belong to G only: no QP
298 // energies, no BSE holes for them
299 const Index nB = inactive_.n_inactive;
300 if (rpamin != 0) {
301 throw std::runtime_error(
302 "inactive_screening_complete_g needs all levels in the RPA range "
303 "(rpamin 0)");
304 }
305 qpmin = std::max(qpmin, nB);
306 bse_vmin = std::max(bse_vmin, nB);
308 << " Complete G: GW and BSE windows start at level " << nB
309 << " (the first active occupied level)" << flush;
310 }
311
312 if (rpamin < 0 || rpamax < 0 || rpamin > rpamax) {
313 throw std::runtime_error("Invalid RPA level range after setup/clamping.");
314 }
315 if (qpmin < 0 || qpmax < 0 || qpmin > qpmax) {
316 throw std::runtime_error("Invalid GW level range after setup/clamping.");
317 }
318 if (bse_vmin < 0 || bse_vmax < 0 || bse_vmin > bse_vmax) {
319 throw std::runtime_error(
320 "Invalid BSE occupied level range after setup/clamping.");
321 }
322 if (bse_cmin < 0 || bse_cmax < 0 || bse_cmin > bse_cmax) {
323 throw std::runtime_error(
324 "Invalid BSE virtual level range after setup/clamping.");
325 }
326 if (bse_vmax >= num_of_levels || bse_cmax >= num_of_levels) {
327 throw std::runtime_error(
328 "BSE level range exceeds available orbital indices.");
329 }
330 if (bse_vmax >= bse_cmin) {
331 throw std::runtime_error("BSE occupied and virtual level ranges overlap.");
332 }
333
334 gwopt_.homo = homo;
335 gwopt_.qpmin = qpmin;
336 gwopt_.qpmax = qpmax;
337 gwopt_.rpamin = rpamin;
338 gwopt_.rpamax = rpamax;
339
340 bseopt_.vmin = bse_vmin;
341 bseopt_.cmax = bse_cmax;
342 bseopt_.homo = homo;
343 bseopt_.qpmin = qpmin;
344 bseopt_.qpmax = qpmax;
345 bseopt_.rpamin = rpamin;
346 bseopt_.rpamax = rpamax;
347
348 orbitals_.setRPAindices(rpamin, rpamax);
349 orbitals_.setGWindices(qpmin, qpmax);
350 orbitals_.setBSEindices(bse_vmin, bse_cmax);
351
352 // Index bse_vmax = homo;
353 // Index bse_cmin = homo + 1;
354 Index bse_vtotal = bse_vmax - bse_vmin + 1;
355 Index bse_ctotal = bse_cmax - bse_cmin + 1;
356 Index bse_size = bse_vtotal * bse_ctotal;
357
358 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " RPA level range [" << rpamin
359 << ":" << rpamax << "]" << flush;
360 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " GW level range [" << qpmin
361 << ":" << qpmax << "]" << flush;
363 << TimeStamp() << " BSE level range occ[" << bse_vmin << ":" << bse_vmax
364 << "] virt[" << bse_cmin << ":" << bse_cmax << "]" << flush;
365
366 gwopt_.reset_3c = options.get(".gw.rebuild_3c_freq").as<Index>();
367
368 bseopt_.nmax = options.get(".bse.exctotal").as<Index>();
369 if (bseopt_.nmax > bse_size || bseopt_.nmax < 0) {
370 bseopt_.nmax = bse_size;
371 }
372
373 bseopt_.davidson_correction =
374 options.get("bse.davidson.correction").as<std::string>();
375
376 bseopt_.davidson_tolerance =
377 options.get("bse.davidson.tolerance").as<std::string>();
378
379 bseopt_.davidson_update =
380 options.get("bse.davidson.update").as<std::string>();
381
382 bseopt_.davidson_maxiter = options.get("bse.davidson.maxiter").as<Index>();
383
384 bseopt_.useTDA = options.get("bse.useTDA").as<bool>();
385 orbitals_.setTDAApprox(bseopt_.useTDA);
386 if (!bseopt_.useTDA) {
387 XTP_LOG(Log::error, *pLog_) << " BSE type: full" << flush;
388 } else {
389 XTP_LOG(Log::error, *pLog_) << " BSE type: TDA" << flush;
390 }
391
392 Index full_bse_size = (bseopt_.useTDA) ? bse_size : 2 * bse_size;
393 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " BSE Hamiltonian has size "
394 << full_bse_size << "x" << full_bse_size << flush;
395
396 bseopt_.use_Hqp_offdiag = options.get("bse.use_Hqp_offdiag").as<bool>();
397 orbitals_.SetFlagUseHqpOffdiag(bseopt_.use_Hqp_offdiag);
398
399 if (!bseopt_.use_Hqp_offdiag) {
401 << " BSE without Hqp offdiagonal elements" << flush;
402 } else {
404 << " BSE with Hqp offdiagonal elements" << flush;
405 }
406
407 bseopt_.max_dyn_iter = options.get("bse.dyn_screen_max_iter").as<Index>();
408 bseopt_.dyn_tolerance = options.get("bse.dyn_screen_tol").as<double>();
409 if (bseopt_.max_dyn_iter > 0) {
411 }
412
413 functional_ = orbitals_.getXCFunctionalName();
414 grid_ = orbitals_.getXCGrid();
415
416 dftbasis_name_ = orbitals_.getDFTbasisName();
417 if (orbitals_.hasAuxbasisName()) {
418 auxbasis_name_ = orbitals_.getAuxbasisName();
419 } else if (options.exists(".auxbasisset")) {
420 auxbasis_name_ = options.get(".auxbasisset").as<std::string>();
421 } else {
423 try {
424 BasisSet b;
426 } catch (std::runtime_error&) {
427 throw std::runtime_error(
428 "There is no auxbasis from the dftcalculation nor did you specify an "
429 "auxbasisset for the gwbse calculation. Also no auxiliary basisset "
430 "for basisset " +
431 dftbasis_name_ + " could be found!");
432 }
434 << " No auxbasisset given, using " << auxbasis_name_ << flush;
435 }
436
437 std::string mode = options.get("gw.mode").as<std::string>();
438 if (mode == "G0W0") {
439 gwopt_.gw_sc_max_iterations = 1;
440 }
441
442 XTP_LOG(Log::error, *pLog_) << " Running GW as: " << mode << flush;
443 gwopt_.ScaHFX = orbitals_.getScaHFX();
444
445 gwopt_.shift = options.get("gw.scissor_shift").as<double>();
446 gwopt_.g_sc_limit =
447 options.get("gw.qp_sc_limit").as<double>(); // convergence criteria
448 // for qp iteration
449 // [Hartree]]
450 gwopt_.g_sc_max_iterations =
451 options.get("gw.qp_sc_max_iter").as<Index>(); // convergence
452 // criteria for qp
453 // iteration
454 // [Hartree]]
455
456 if (mode == "evGW") {
457 gwopt_.gw_sc_max_iterations = options.get("gw.sc_max_iter").as<Index>();
458 }
459
460 gwopt_.gw_sc_limit =
461 options.get("gw.sc_limit").as<double>(); // convergence criteria
462 // for shift it
464 << " qp_sc_limit [Hartree]: " << gwopt_.g_sc_limit << flush;
465 if (gwopt_.gw_sc_max_iterations > 1) {
467 << " gw_sc_limit [Hartree]: " << gwopt_.gw_sc_limit << flush;
468 }
469 bseopt_.min_print_weight = options.get("bse.print_weight").as<double>();
470 // print exciton WF composition weight larger than minimum
471
472 // possible tasks
473 std::string tasks_string = options.get(".tasks").as<std::string>();
474 boost::algorithm::to_lower(tasks_string);
475
476 if (tasks_string.find("gw") != std::string::npos) {
477 do_gw_ = true;
478 }
479
480 if (is_uks) {
481 if (tasks_string.find("excitons") != std::string::npos ||
482 tasks_string.find("exciton_uks") != std::string::npos) {
483 do_gw_ = true;
484 do_bse_exciton_uks_ = true;
485 }
486
487 if (tasks_string.find("singlets") != std::string::npos ||
488 tasks_string.find("triplets") != std::string::npos) {
489 throw std::runtime_error(
490 "Invalid gwbse task for UKS reference: 'singlets' and 'triplets' "
491 "are not defined for open-shell systems.\n"
492 "Use 'exciton_uks' (or 'excitons') instead.");
493 }
494 } else {
495 if (tasks_string.find("all") != std::string::npos) {
496 if (is_uks) {
497 do_gw_ = true;
498 do_bse_exciton_uks_ = true;
499 } else {
500 do_gw_ = true;
501 do_bse_singlets_ = true;
502 do_bse_triplets_ = true;
503 }
504 }
505 if (tasks_string.find("singlets") != std::string::npos) {
506 do_bse_singlets_ = true;
507 }
508 if (tasks_string.find("triplets") != std::string::npos) {
509 do_bse_triplets_ = true;
510 }
511 }
512
513 XTP_LOG(Log::error, *pLog_) << " Tasks: " << flush;
514 if (do_gw_) {
515 XTP_LOG(Log::error, *pLog_) << " GW " << flush;
516 }
517 if (do_bse_singlets_) {
518 XTP_LOG(Log::error, *pLog_) << " singlets " << flush;
519 }
520 if (do_bse_triplets_) {
521 XTP_LOG(Log::error, *pLog_) << " triplets " << flush;
522 }
524 XTP_LOG(Log::error, *pLog_) << " exciton_uks " << flush;
525 }
526
527 if (options.exists("bse.fragments")) {
528 std::vector<tools::Property*> prop_region =
529 options.Select("bse.fragments.fragment");
530 Index index = 0;
531 for (tools::Property* prop : prop_region) {
532 std::string indices = prop->get("indices").as<std::string>();
533 fragments_.push_back(QMFragment<BSE_Population>(index, indices));
534 index++;
535 }
536 }
537
538 gwopt_.sigma_integration =
539 options.get("gw.sigma_integrator").as<std::string>();
541 << " Sigma integration: " << gwopt_.sigma_integration << flush;
542 gwopt_.eta = options.get("gw.eta").as<double>();
543 XTP_LOG(Log::error, *pLog_) << " eta: " << gwopt_.eta << flush;
544 if (gwopt_.sigma_integration == "exact") {
546 << " RPA Hamiltonian size: " << (homo + 1 - rpamin) * (rpamax - homo)
547 << flush;
548 }
549 if (gwopt_.sigma_integration == "cda") {
550 gwopt_.order = options.get("gw.quadrature_order").as<Index>();
552 << " Quadrature integration order : " << gwopt_.order << flush;
553 gwopt_.quadrature_scheme =
554 options.get("gw.quadrature_scheme").as<std::string>();
556 << " Quadrature integration scheme : " << gwopt_.quadrature_scheme
557 << flush;
558 gwopt_.alpha = options.get("gw.alpha").as<double>();
560 << " Alpha smoothing parameter : " << gwopt_.alpha << flush;
561 }
562 gwopt_.qp_solver = options.get("gw.qp_solver").as<std::string>();
563
564 XTP_LOG(Log::error, *pLog_) << " QP solver: " << gwopt_.qp_solver << flush;
565 if (gwopt_.qp_solver == "grid") {
566 // Grid-based QP solving now exposes the full search extent, dense scan,
567 // and adaptive shell scan as separate controls. This avoids the old
568 // overloading of qp_grid_steps / qp_grid_spacing, where one pair of values
569 // simultaneously determined the window width and both scan resolutions.
570 //
571 // New-style options are read first. Deprecated legacy aliases are then
572 // translated only for values that are still unset.
573 // New decoupled QP search options
574 if (options.exists("gw.qp_full_window_half_width")) {
575 gwopt_.qp_full_window_half_width =
576 options.get("gw.qp_full_window_half_width").as<double>();
577 }
578 if (options.exists("gw.qp_dense_spacing")) {
579 gwopt_.qp_dense_spacing = options.get("gw.qp_dense_spacing").as<double>();
580 }
581 if (options.exists("gw.qp_adaptive_shell_width")) {
582 gwopt_.qp_adaptive_shell_width =
583 options.get("gw.qp_adaptive_shell_width").as<double>();
584 }
585 if (options.exists("gw.qp_adaptive_shell_count")) {
586 gwopt_.qp_adaptive_shell_count =
587 options.get("gw.qp_adaptive_shell_count").as<Index>();
588 }
589
590 // Deprecated legacy aliases. They are accepted for backward
591 // compatibility and mapped onto the canonical controls below.
592 const bool has_legacy_steps = options.exists("gw.qp_grid_steps");
593 const bool has_legacy_spacing = options.exists("gw.qp_grid_spacing");
594
595 if (has_legacy_steps != has_legacy_spacing) {
596 throw std::runtime_error(
597 "Deprecated gw.qp_grid_steps and gw.qp_grid_spacing must be given "
598 "together if either is used.");
599 }
600
601 if (has_legacy_steps && has_legacy_spacing) {
602 gwopt_.qp_grid_steps = options.get("gw.qp_grid_steps").as<Index>();
603 gwopt_.qp_grid_spacing = options.get("gw.qp_grid_spacing").as<double>();
604
605 const double legacy_half_width =
607 const double legacy_shell_width =
609
610 if (gwopt_.qp_full_window_half_width <= 0.0) {
611 gwopt_.qp_full_window_half_width = legacy_half_width;
612 }
613 if (gwopt_.qp_dense_spacing <= 0.0) {
614 gwopt_.qp_dense_spacing = gwopt_.qp_grid_spacing;
615 }
616 if (gwopt_.qp_adaptive_shell_count <= 0 &&
617 gwopt_.qp_adaptive_shell_width <= 0.0) {
618 gwopt_.qp_adaptive_shell_width = legacy_shell_width;
619 }
620
622 << " Deprecated GW QP options detected: " << "qp_grid_steps="
623 << gwopt_.qp_grid_steps
624 << " qp_grid_spacing=" << gwopt_.qp_grid_spacing
625 << " -> mapped to qp_full_window_half_width="
626 << gwopt_.qp_full_window_half_width
627 << " qp_dense_spacing=" << gwopt_.qp_dense_spacing
628 << " qp_adaptive_shell_width=" << gwopt_.qp_adaptive_shell_width
629 << flush;
630 }
631
632 // Final normalization produces one canonical option set used by both
633 // GW and GW_UKS implementations.
635
637 << " QP full window half-width: " << gwopt_.qp_full_window_half_width
638 << flush;
640 << " QP dense spacing: " << gwopt_.qp_dense_spacing << flush;
642 << " QP adaptive shell width: " << gwopt_.qp_adaptive_shell_width
643 << flush;
645 << " QP adaptive shell count: " << gwopt_.qp_adaptive_shell_count
646 << flush;
647 }
648 gwopt_.qp_root_finder = options.get("gw.qp_root_finder").as<std::string>();
650 << " QP root finder: " << gwopt_.qp_root_finder << flush;
651
652 gwopt_.gw_mixing_order =
653 options.get("gw.mixing_order").as<Index>(); // max history in
654 // mixing (0: plain,
655 // 1: linear, >1
656 // Anderson)
657
658 gwopt_.gw_mixing_alpha = options.get("gw.mixing_alpha").as<double>();
659 gwopt_.qp_grid_search_mode =
660 options.get("gw.qp_grid_search_mode").as<std::string>();
661 gwopt_.qp_restrict_search = options.get("gw.qp_restrict_search").as<bool>();
662 gwopt_.qp_root_continuity = options.ifExistsReturnElseReturnDefault<bool>(
663 "gw.qp_root_continuity", true);
664 gwopt_.qp_root_tracking = options.ifExistsReturnElseReturnDefault<bool>(
665 "gw.qp_root_tracking", false);
667 options.ifExistsReturnElseReturnDefault<bool>("gw.core_exchange", true);
668 gwopt_.screening_update =
669 options.ifExistsReturnElseReturnDefault<std::string>(
670 "gw.screening_update", "adaptive");
671 ScreeningUpdate::Parse(gwopt_.screening_update); // validates
672 gwopt_.screening_update_ratio =
673 options.ifExistsReturnElseReturnDefault<double>(
674 "gw.screening_update_ratio", 0.1);
675 gwopt_.screening_update_max_inner =
677 "gw.screening_update_max_inner", 10);
678 gwopt_.qp_zero_margin = options.get("gw.qp_zero_margin").as<double>();
679 gwopt_.qp_virtual_min_energy =
680 options.get("gw.qp_virtual_min_energy").as<double>();
681
682 // QSGW options
683 gwopt_.do_qsgw =
684 options.ifExistsReturnElseReturnDefault<bool>("gw.do_qsgw", false);
685 gwopt_.qsgw_max_iterations = options.ifExistsReturnElseReturnDefault<Index>(
686 "gw.qsgw_max_iterations", 50);
687 gwopt_.qsgw_sc_limit =
688 options.ifExistsReturnElseReturnDefault<double>("gw.qsgw_sc_limit", 1e-5);
689 gwopt_.qsgw_max_virt_correction =
690 options.ifExistsReturnElseReturnDefault<double>(
691 "gw.qsgw_max_virt_correction", 0.5);
692 gwopt_.qsgw_hartree_update = options.ifExistsReturnElseReturnDefault<bool>(
693 "gw.qsgw_hartree_update", true);
694 gwopt_.qsgw_hartree_screened = options.ifExistsReturnElseReturnDefault<bool>(
695 "gw.qsgw_hartree_screened", true);
696 gwopt_.qsgw_regularization =
697 options.ifExistsReturnElseReturnDefault<std::string>(
698 "gw.qsgw_regularization", "eta");
699 if (gwopt_.qsgw_regularization != "eta" &&
700 gwopt_.qsgw_regularization != "srg") {
701 throw std::runtime_error("gw.qsgw_regularization must be eta or srg, got " +
702 gwopt_.qsgw_regularization);
703 }
704 gwopt_.qsgw_srg_s =
705 options.ifExistsReturnElseReturnDefault<double>("gw.qsgw_srg_s", 500.0);
706 if (!(gwopt_.qsgw_srg_s > 0.0)) {
707 throw std::runtime_error("gw.qsgw_srg_s must be positive");
708 }
709 gwopt_.out_of_window_shift.mode = OutOfWindowShift::Parse(
710 options.ifExistsReturnElseReturnDefault<std::string>(
711 "gw.out_of_window_shift", "max"));
712 gwopt_.out_of_window_shift.fit_fraction =
713 options.ifExistsReturnElseReturnDefault<double>(
714 "gw.out_of_window_fit_fraction", 0.3);
715 if (gwopt_.out_of_window_shift.mode != OutOfWindowShift::Mode::Max) {
717 << " Levels outside the GW window shifted by: "
718 << OutOfWindowShift::Name(gwopt_.out_of_window_shift.mode)
719 << ((gwopt_.out_of_window_shift.mode == OutOfWindowShift::Mode::Linear)
720 ? " (fit fraction " +
721 std::to_string(gwopt_.out_of_window_shift.fit_fraction) +
722 ")"
723 : std::string())
724 << flush;
725 }
726 if (gwopt_.do_qsgw) {
728 << " QSGW enabled: max_iter=" << gwopt_.qsgw_max_iterations
729 << " sc_limit=" << gwopt_.qsgw_sc_limit << " Ha"
730 << " max_virt_correction=" << gwopt_.qsgw_max_virt_correction << " Ha ("
731 << gwopt_.qsgw_max_virt_correction * 27.2114 << " eV)" << std::flush;
732 }
733
734 if (mode == "evGW") {
735 if (gwopt_.gw_mixing_order == 0) {
736 XTP_LOG(Log::error, *pLog_) << " evGW with plain update " << std::flush;
737 } else if (gwopt_.gw_mixing_order == 1) {
738 XTP_LOG(Log::error, *pLog_) << " evGW with linear update using alpha "
739 << gwopt_.gw_mixing_alpha << std::flush;
740 } else {
741 XTP_LOG(Log::error, *pLog_) << " evGW with Anderson update with history "
742 << gwopt_.gw_mixing_order << " using alpha "
743 << gwopt_.gw_mixing_alpha << std::flush;
744 }
745 if (gwopt_.screening_update == "adaptive") {
747 << " evGW screening update: adaptive (ratio "
748 << gwopt_.screening_update_ratio << ", at most "
749 << gwopt_.screening_update_max_inner << " iterations per W)"
750 << std::flush;
751 } else {
753 << " evGW screening update: every iteration" << std::flush;
754 }
755 }
756
757 if (options.exists("gw.sigma_plot")) {
758 sigma_plot_states_ = options.get("gw.sigma_plot.states").as<std::string>();
759 sigma_plot_steps_ = options.get("gw.sigma_plot.steps").as<Index>();
760 sigma_plot_spacing_ = options.get("gw.sigma_plot.spacing").as<double>();
762 options.get("gw.sigma_plot.filename").as<std::string>();
763
765 << " Sigma plot states: " << sigma_plot_states_ << flush;
767 << " Sigma plot steps: " << sigma_plot_steps_ << flush;
769 << " Sigma plot spacing: " << sigma_plot_spacing_ << flush;
771 << " Sigma plot filename: " << sigma_plot_filename_ << flush;
772 }
773
774 if (options.exists("gw.sigma_dump")) {
775 if (is_uks) {
776 throw std::runtime_error("gw.sigma_dump is not implemented for UKS");
777 }
778 if (gwopt_.sigma_integration != "ppm" &&
779 gwopt_.sigma_integration != "exact") {
780 throw std::runtime_error(
781 "gw.sigma_dump needs sigma_integrator ppm or exact");
782 }
783 if (gwopt_.do_qsgw) {
784 throw std::runtime_error("gw.sigma_dump is not implemented for QSGW");
785 }
786 do_sigma_dump_ = true;
787 sigma_dump_opt_.filename =
788 options.get("gw.sigma_dump.filename").as<std::string>();
789 sigma_dump_opt_.levels_emax_ev =
790 options.get("gw.sigma_dump.levels_emax").as<double>();
791 sigma_dump_opt_.imag_points =
792 options.get("gw.sigma_dump.imag_points").as<Index>();
793 sigma_dump_opt_.imag_scale =
794 options.get("gw.sigma_dump.imag_scale").as<double>();
795 sigma_dump_opt_.spectral_min =
796 options.get("gw.sigma_dump.spectral_min").as<double>();
797 sigma_dump_opt_.spectral_max =
798 options.get("gw.sigma_dump.spectral_max").as<double>();
799 sigma_dump_opt_.spectral_binwidth =
800 options.get("gw.sigma_dump.spectral_binwidth").as<double>();
801 sigma_dump_opt_.plot_steps =
802 options.get("gw.sigma_dump.plot_steps").as<Index>();
803 sigma_dump_opt_.plot_spacing =
804 options.get("gw.sigma_dump.plot_spacing").as<double>();
806 << " Sigma dump filename: " << sigma_dump_opt_.filename << flush;
807 }
808}
809
811
812 const double hrt2ev = tools::conv::hrt2ev;
813 tools::Property& gwbse_summary = summary.add("GWBSE", "");
814 if (do_gw_) {
815 gwbse_summary.setAttribute("units", "eV");
816 gwbse_summary.setAttribute(
817 "DFTEnergy",
818 (boost::format("%1$+1.6f ") % (orbitals_.getDFTTotalEnergy() * hrt2ev))
819 .str());
820
821 if (orbitals_.hasUnrestrictedOrbitals()) {
822 gwbse_summary.add("dft_alpha", "");
823 gwbse_summary.add("dft_beta", "");
824
825 tools::Property& alpha_summary = gwbse_summary.get("dft_alpha");
826 tools::Property& beta_summary = gwbse_summary.get("dft_beta");
827
828 alpha_summary.setAttribute("HOMO", orbitals_.getHomoAlpha());
829 alpha_summary.setAttribute("LUMO", orbitals_.getLumoAlpha());
830 beta_summary.setAttribute("HOMO", orbitals_.getHomoBeta());
831 beta_summary.setAttribute("LUMO", orbitals_.getLumoBeta());
832 for (Index state = 0; state < gwopt_.qpmax + 1 - gwopt_.qpmin; state++) {
833 tools::Property& level_alpha = alpha_summary.add("level", "");
834 level_alpha.setAttribute("number", state + gwopt_.qpmin);
835 level_alpha.add(
836 "dft_energy",
837 (boost::format("%1$+1.6f ") %
838 (orbitals_.MOs().eigenvalues()(state + gwopt_.qpmin) * hrt2ev))
839 .str());
840 level_alpha.add("gw_energy",
841 (boost::format("%1$+1.6f ") %
842 (orbitals_.QPpertEnergiesAlpha()(state) * hrt2ev))
843 .str());
844 level_alpha.add(
845 "qp_energy",
846 (boost::format("%1$+1.6f ") %
847 (orbitals_.QPdiagAlpha().eigenvalues()(state) * hrt2ev))
848 .str());
849
850 tools::Property& level_beta = beta_summary.add("level", "");
851 level_beta.setAttribute("number", state + gwopt_.qpmin);
852 level_beta.add(
853 "dft_energy",
854 (boost::format("%1$+1.6f ") %
855 (orbitals_.MOs_beta().eigenvalues()(state + gwopt_.qpmin) *
856 hrt2ev))
857 .str());
858 level_beta.add("gw_energy",
859 (boost::format("%1$+1.6f ") %
860 (orbitals_.QPpertEnergiesBeta()(state) * hrt2ev))
861 .str());
862 level_beta.add("qp_energy",
863 (boost::format("%1$+1.6f ") %
864 (orbitals_.QPdiagBeta().eigenvalues()(state) * hrt2ev))
865 .str());
866 }
867 } else {
868 tools::Property& dft_summary = gwbse_summary.add("dft", "");
869 dft_summary.setAttribute("HOMO", gwopt_.homo);
870 dft_summary.setAttribute("LUMO", gwopt_.homo + 1);
871
872 for (Index state = 0; state < gwopt_.qpmax + 1 - gwopt_.qpmin; state++) {
873 tools::Property& level_summary = dft_summary.add("level", "");
874 level_summary.setAttribute("number", state + gwopt_.qpmin);
875 level_summary.add(
876 "dft_energy",
877 (boost::format("%1$+1.6f ") %
878 (orbitals_.MOs().eigenvalues()(state + gwopt_.qpmin) * hrt2ev))
879 .str());
880 level_summary.add("gw_energy",
881 (boost::format("%1$+1.6f ") %
882 (orbitals_.QPpertEnergies()(state) * hrt2ev))
883 .str());
884 level_summary.add("qp_energy",
885 (boost::format("%1$+1.6f ") %
886 (orbitals_.QPdiag().eigenvalues()(state) * hrt2ev))
887 .str());
888 }
889 }
890 }
891 if (do_bse_singlets_) {
892 tools::Property& singlet_summary = gwbse_summary.add("singlets", "");
893 for (Index state = 0; state < bseopt_.nmax; ++state) {
894 tools::Property& level_summary = singlet_summary.add("level", "");
895 level_summary.setAttribute("number", state + 1);
896 level_summary.add(
897 "omega", (boost::format("%1$+1.6f ") %
898 (orbitals_.BSESinglets().eigenvalues()(state) * hrt2ev))
899 .str());
900 if (orbitals_.hasTransitionDipoles()) {
901
902 const Eigen::Vector3d& dipoles = (orbitals_.TransitionDipoles())[state];
903 double f = 2 * dipoles.squaredNorm() *
904 orbitals_.BSESinglets().eigenvalues()(state) / 3.0;
905
906 level_summary.add("f", (boost::format("%1$+1.6f ") % f).str());
907 tools::Property& dipol_summary = level_summary.add(
908 "Trdipole", (boost::format("%1$+1.4f %2$+1.4f %3$+1.4f") %
909 dipoles.x() % dipoles.y() % dipoles.z())
910 .str());
911 dipol_summary.setAttribute("unit", "e*bohr");
912 dipol_summary.setAttribute("gauge", "length");
913 }
914 }
915 }
916 if (do_bse_triplets_) {
917 tools::Property& triplet_summary = gwbse_summary.add("triplets", "");
918 for (Index state = 0; state < bseopt_.nmax; ++state) {
919 tools::Property& level_summary = triplet_summary.add("level", "");
920 level_summary.setAttribute("number", state + 1);
921 level_summary.add(
922 "omega", (boost::format("%1$+1.6f ") %
923 (orbitals_.BSETriplets().eigenvalues()(state) * hrt2ev))
924 .str());
925 }
926 }
928 tools::Property& exciton_uks_summary = gwbse_summary.add("exciton_uks", "");
929
930 const bool has_dipoles = orbitals_.hasTransitionDipoles();
931 Eigen::VectorXd oscs = Eigen::VectorXd::Zero(0);
932 if (has_dipoles) {
933 oscs =
934 orbitals_.Oscillatorstrengths(QMStateType(QMStateType::ExcitonUKS));
935 }
936
937 for (Index state = 0;
938 state <
939 std::min<Index>(bseopt_.nmax, orbitals_.BSEUKS().eigenvalues().size());
940 ++state) {
941 tools::Property& level_summary = exciton_uks_summary.add("level", "");
942 level_summary.setAttribute("number", state + 1);
943 level_summary.add("omega",
944 (boost::format("%1$+1.6f ") %
945 (orbitals_.BSEUKS().eigenvalues()(state) * hrt2ev))
946 .str());
947 const Index ndip =
948 static_cast<Index>(orbitals_.TransitionDipoles().size());
949 const Index nosc = static_cast<Index>(oscs.size());
950
951 if (has_dipoles && state < ndip && state < nosc) {
952 const Eigen::Vector3d& dipoles = orbitals_.TransitionDipoles()[state];
953
954 level_summary.add("f",
955 (boost::format("%1$+1.6f ") % oscs(state)).str());
956
957 tools::Property& dipol_summary = level_summary.add(
958 "Trdipole", (boost::format("%1$+1.4f %2$+1.4f %3$+1.4f") %
959 dipoles.x() % dipoles.y() % dipoles.z())
960 .str());
961 dipol_summary.setAttribute("unit", "e*bohr");
962 dipol_summary.setAttribute("gauge", "length");
963 }
964 }
965 }
966
967 return;
968}
969
970/*
971 * Many-body Green's fuctions theory implementation
972 *
973 * data required from orbitals file
974 * - atomic coordinates
975 * - DFT molecular orbitals (energies and coeffcients)
976 * - DFT exchange-correlation potential matrix in atomic orbitals
977 * - number of electrons, number of levels
978 */
979
980Eigen::MatrixXd GWBSE::CalculateVXC(const AOBasis& dftbasis) {
981 if (orbitals_.getXCFunctionalName().empty()) {
982 orbitals_.setXCFunctionalName(functional_);
983 } else {
984 if (!(functional_ == orbitals_.getXCFunctionalName())) {
985 throw std::runtime_error("Functionals from DFT " +
986 orbitals_.getXCFunctionalName() + " GWBSE " +
987 functional_ + " differ!");
988 }
989 }
990
991 Vxc_Grid grid;
992 grid.GridSetup(grid_, orbitals_.QMAtoms(), dftbasis);
994 << TimeStamp() << " Setup grid for integration with gridsize: " << grid_
995 << " with " << grid.getGridSize() << " points, divided into "
996 << grid.getBoxesSize() << " boxes" << flush;
997 Vxc_Potential<Vxc_Grid> vxcpotential(grid);
998 vxcpotential.setXCfunctional(functional_);
1000 << TimeStamp() << " Integrating Vxc with functional " << functional_
1001 << flush;
1002 Eigen::MatrixXd DMAT = orbitals_.DensityMatrixGroundState();
1003 Mat_p_Energy e_vxc_ao = vxcpotential.IntegrateVXC(DMAT);
1004 XTP_LOG(Log::info, *pLog_) << TimeStamp() << " Calculated Vxc" << flush;
1006 << TimeStamp() << " Set hybrid exchange factor: " << orbitals_.getScaHFX()
1007 << flush;
1008 Index qptotal = gwopt_.qpmax - gwopt_.qpmin + 1;
1009 Eigen::MatrixXd mos =
1010 orbitals_.MOs().eigenvectors().middleCols(gwopt_.qpmin, qptotal);
1011
1012 Eigen::MatrixXd vxc = mos.transpose() * e_vxc_ao.matrix() * mos;
1014 << TimeStamp() << " Calculated exchange-correlation expectation values "
1015 << flush;
1016
1017 return vxc;
1018}
1019
1020std::pair<Eigen::MatrixXd, Eigen::MatrixXd> GWBSE::CalculateVXCSpinResolved(
1021 const AOBasis& dftbasis) {
1022 if (orbitals_.getXCFunctionalName().empty()) {
1023 orbitals_.setXCFunctionalName(functional_);
1024 } else if (!(functional_ == orbitals_.getXCFunctionalName())) {
1025 throw std::runtime_error("Functionals from DFT " +
1026 orbitals_.getXCFunctionalName() + " GWBSE " +
1027 functional_ + " differ!");
1028 }
1029
1030 Vxc_Grid grid;
1031 grid.GridSetup(grid_, orbitals_.QMAtoms(), dftbasis);
1032 Vxc_Potential<Vxc_Grid> vxcpotential(grid);
1033 vxcpotential.setXCfunctional(functional_);
1034
1035 auto dmats = orbitals_.DensityMatrixGroundStateSpinResolved();
1036 auto e_vxc_ao = vxcpotential.IntegrateVXCSpin(dmats[0], dmats[1]);
1037
1038 Index qptotal = gwopt_.qpmax - gwopt_.qpmin + 1;
1039 Eigen::MatrixXd mos_alpha =
1040 orbitals_.MOs().eigenvectors().middleCols(gwopt_.qpmin, qptotal);
1041 Eigen::MatrixXd mos_beta =
1042 orbitals_.MOs_beta().eigenvectors().middleCols(gwopt_.qpmin, qptotal);
1043
1044 Eigen::MatrixXd vxc_alpha =
1045 mos_alpha.transpose() * e_vxc_ao.vxc_alpha * mos_alpha;
1046 Eigen::MatrixXd vxc_beta =
1047 mos_beta.transpose() * e_vxc_ao.vxc_beta * mos_beta;
1048
1049 return std::make_pair(vxc_alpha, vxc_beta);
1050}
1051
1053 citations::Cite("Wehner:2018");
1054 citations::Cite("Tirimbo:2020a");
1055 if (gwopt_.do_qsgw) {
1056 citations::Cite("vanSchilfgaarde:2006");
1057 }
1058
1059 // set the parallelization
1060 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " Using "
1061 << OPENMP::getMaxThreads() << " threads" << flush;
1062
1064 << TimeStamp() << " Using " << EigenBackendDescription() << flush;
1067 << " Screening eigensolvers and inverses call "
1068 "Accelerate LAPACK directly"
1069 << flush;
1070 }
1072 << TimeStamp() << " Molecule Coordinates [A] " << flush;
1073 for (QMAtom& atom : orbitals_.QMAtoms()) {
1074 std::string output = (boost::format("%5d"
1075 "%5s"
1076 " %1.4f %1.4f %1.4f") %
1077 atom.getId() % atom.getElement() %
1078 (atom.getPos().x() * tools::conv::bohr2ang) %
1079 (atom.getPos().y() * tools::conv::bohr2ang) %
1080 (atom.getPos().z() * tools::conv::bohr2ang))
1081 .str();
1082
1083 XTP_LOG(Log::error, *pLog_) << output << flush;
1084 }
1085
1086 std::string dft_package = orbitals_.getQMpackage();
1088 << TimeStamp() << " DFT data was created by " << dft_package << flush;
1089
1090 BasisSet dftbs;
1091 dftbs.Load(dftbasis_name_);
1092
1094 << TimeStamp() << " Loaded DFT Basis Set " << dftbasis_name_ << flush;
1095
1096 AOBasis dftbasis = orbitals_.getDftBasis();
1097 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " Filled DFT Basis of size "
1098 << dftbasis.AOBasisSize() << flush;
1100 << TimeStamp() << " Loaded Auxbasis Set " << auxbasis_name_ << flush;
1101
1102 orbitals_.SetupAuxBasis(auxbasis_name_);
1103 AOBasis auxbasis = orbitals_.getAuxBasis();
1104 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " Filled Auxbasis of size "
1105 << auxbasis.AOBasisSize() << flush;
1106
1108 fragments_.size() > 0) {
1109 for (const auto& frag : fragments_) {
1110 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " Fragment " << frag.getId()
1111 << " size:" << frag.size() << flush;
1112 }
1113 }
1114
1115 if (!do_gw_) {
1116 if ((!orbitals_.hasUnrestrictedOrbitals() && !orbitals_.hasQPdiag()) ||
1117 (orbitals_.hasUnrestrictedOrbitals() &&
1118 (!orbitals_.hasQPdiagAlpha() || !orbitals_.hasQPdiagBeta()))) {
1119 throw std::runtime_error(
1120 "You want no GW calculation but the orb file has no matching QP "
1121 "coefficients.");
1122 }
1123 }
1124 const bool is_uks = orbitals_.hasUnrestrictedOrbitals();
1125 TCMatrix_gwbse Mmn;
1126 TCMatrix_gwbse_spin Mmn_spin;
1127 // rpamin here, because RPA needs till rpamin
1128 Index max_3c = std::max(bseopt_.cmax, gwopt_.qpmax);
1129 if (is_uks) {
1130 Mmn_spin.alpha.Initialize(auxbasis.AOBasisSize(), gwopt_.rpamin, max_3c,
1131 gwopt_.rpamin, gwopt_.rpamax);
1132 Mmn_spin.beta.Initialize(auxbasis.AOBasisSize(), gwopt_.rpamin, max_3c,
1133 gwopt_.rpamin, gwopt_.rpamax);
1135 << TimeStamp() << " Calculating alpha spin Mmn " << flush;
1136 Mmn_spin.alpha.Fill(auxbasis, dftbasis, orbitals_.MOs().eigenvectors());
1138 << TimeStamp() << " Calculating beta spin Mmn " << flush;
1139 Mmn_spin.beta.Fill(auxbasis, dftbasis, orbitals_.MOs_beta().eigenvectors());
1140 } else {
1141 if (inactive_.complete_g) {
1142 // the inactive occupied levels in front stay in G, not in the RPA;
1143 // only their rows in the other slices are stored
1144 Mmn.setExcludedHoles(inactive_.n_inactive);
1146 << TimeStamp() << " Complete G: the " << inactive_.n_inactive
1147 << " inactive occupied levels enter the self-energy (and Vxc), "
1148 << "not the RPA; their own slices are not stored" << flush;
1149 }
1150 if (stream_rpa_) {
1151 if (!do_gw_ || gwopt_.do_qsgw || gwopt_.sigma_integration != "ppm" ||
1153 throw std::runtime_error(
1154 "GWBSE: stream_rpa is implemented for G0W0 and evGW with "
1155 "sigma_integrator ppm, without QSGW and inactive_screening.");
1156 }
1157 // occupied levels below the QP and BSE windows: only in the RPA
1158 Index first_needed = gwopt_.qpmin;
1160 first_needed = std::min(first_needed, bseopt_.vmin);
1161 }
1162 streamed_holes_ = std::clamp<Index>(first_needed - gwopt_.rpamin, 0,
1163 gwopt_.homo + 1 - gwopt_.rpamin);
1166 << TimeStamp() << " stream_rpa: " << streamed_holes_
1167 << " occupied levels below the windows are not stored; their "
1168 << "response is streamed" << flush;
1169 }
1170 Mmn.Initialize(auxbasis.AOBasisSize(), gwopt_.rpamin, max_3c, gwopt_.rpamin,
1171 gwopt_.rpamax);
1173 << TimeStamp() << " Calculating Mmn (3-center-repulsion x orbitals) "
1174 << flush;
1175 Mmn.Fill(auxbasis, dftbasis, orbitals_.MOs().eigenvectors());
1177 << TimeStamp() << " Removed " << Mmn.Removedfunctions()
1178 << " functions from Aux Coulomb matrix to avoid near linear "
1179 "dependencies"
1180 << flush;
1182 << TimeStamp() << " Calculated Mmn (3-center-repulsion x orbitals) "
1183 << flush;
1184 }
1185 if (const std::string usage = MemoryBudget::Usage(); !usage.empty()) {
1187 << TimeStamp() << " After Mmn: " << usage << flush;
1188 }
1189
1190 // The environment's reaction field in the metric of Mmn. Needs only the
1191 // auxiliary basis and its metric, so it stays valid however Mmn is later
1192 // rotated, dressed or refilled (QSGW) -- all of it on this basis.
1193 Eigen::MatrixXd reaction_field;
1194 if (!environment_.empty()) {
1195 if (is_uks) {
1196 throw std::runtime_error(
1197 "GWBSE: environment screening is implemented for closed-shell "
1198 "systems only.");
1199 }
1200 std::chrono::time_point<std::chrono::system_clock> start =
1201 std::chrono::system_clock::now();
1202 Index n_explicit = 0;
1203 for (const PolarSegment& seg : environment_.explicit_segments) {
1204 n_explicit += seg.size();
1205 }
1206 Index n_shell = 0;
1207 for (const PolarSegment& seg : environment_.shell_segments) {
1208 n_shell += seg.size();
1209 }
1211 << TimeStamp() << " Environment screening: " << n_explicit
1212 << " explicit Thole sites (exp_damp " << environment_.exp_damp << "), "
1213 << n_shell << " shell sites (epsilon " << environment_.shell_dielectric
1214 << ")" << flush;
1215 const Eigen::MatrixXd B =
1217 reaction_field =
1219 std::chrono::duration<double> elapsed_time =
1220 std::chrono::system_clock::now() - start;
1222 << TimeStamp() << " Built environment reaction field in "
1223 << elapsed_time.count() << " seconds." << flush;
1224 }
1225
1226 // Screening by the inactive region of a projection-based embedding
1227 // (InactiveScreening): its response Y at the two plasmon-pole fitting
1228 // points enters epsilon (ppm: TCMatrix_gwbse::setAdditionalResponse;
1229 // exact Sigma: fitted poles as pseudo-transitions), so that the inactive
1230 // region screens dynamically in GW. The BSE adds the static Y to its
1231 // epsilon. An environment keeps its own reaction field in both.
1232 Eigen::MatrixXd inactive_static_bse; // empty without inactive screening
1234 if (gwopt_.do_qsgw) {
1235 throw std::runtime_error(
1236 "GWBSE: inactive_screening is not implemented for QSGW.");
1237 }
1239 throw std::runtime_error(
1240 "GWBSE: dynamical screening in the BSE is not implemented with "
1241 "inactive_screening (the BSE has the inactive response only at "
1242 "zero frequency).");
1243 }
1244 if (is_uks) {
1245 throw std::runtime_error(
1246 "GWBSE: inactive_screening is implemented for closed-shell "
1247 "systems only.");
1248 }
1249 if (inactive_.mode != inactive_mode_) {
1250 throw std::runtime_error(
1251 "GWBSE: inactive_screening=" +
1253 " needs the inactive region of a projection-based embedding "
1254 "(dft_in_dft) prepared by the caller.");
1255 }
1256 if (inactive_.n_inactive == 0) {
1258 << TimeStamp() << " Warning: the inactive region holds no occupied "
1259 << "orbitals; inactive_screening has nothing to add" << flush;
1260 }
1261 std::chrono::time_point<std::chrono::system_clock> start =
1262 std::chrono::system_clock::now();
1264 << TimeStamp() << " Inactive-region screening ("
1266 << "): " << inactive_.n_inactive
1267 << " inactive occupied orbitals, response over "
1268 << inactive_.occupied.cols() << " occupied"
1269 << (inactive_.n_far > 0
1270 ? " (" + std::to_string(inactive_.n_far) +
1271 " inactive orbitals beyond " +
1272 std::to_string(inactive_.radius * tools::conv::bohr2ang) +
1273 " Angstrom left out)"
1274 : std::string())
1275 << " x " << inactive_.virtuals.cols() << " virtual levels; projector "
1276 << "idempotency error " << inactive_.idempotency_error << flush;
1277 // the static point with the RPA's broadening, as calculate_epsilon_r(0),
1278 // and the PPM's imaginary fitting point, from one integral pass
1279 const PPM ppm;
1280 const double w_i = ppm.ImaginaryFittingFrequency();
1281 const std::vector<double> frequencies{RPA(*pLog_, Mmn).getEta(), w_i};
1282 double chunk_bytes = inactive_chunk_bytes_;
1283 if (chunk_bytes <= 0.0) {
1284 // what the budget leaves after the stored integrals (default
1285 // allowance without --memory), at least 1 GB
1286 chunk_bytes = std::max(1e9, MemoryBudget::AvailableBytes());
1287 }
1288 {
1289 Index nv = std::min(gwopt_.rpamax - orbitals_.getHomo(),
1290 inactive_.virtuals.cols());
1291 if (inactive_virtual_cutoff_ > 0.0 && nv > 0) {
1292 nv = std::min(
1293 nv, Index((inactive_.virtual_energies.head(nv).array() <=
1294 inactive_.virtual_energies(0) + inactive_virtual_cutoff_)
1295 .count()));
1297 << TimeStamp() << " Inactive response: " << nv
1298 << " virtual levels up to "
1300 << " eV above the LUMO" << flush;
1301 }
1302 const double nvirt = double(nv);
1303 const double total = 8.0 * double(inactive_.occupied.cols()) * nvirt *
1304 double(auxbasis.AOBasisSize());
1306 << TimeStamp() << " Inactive response integrals: " << total / 1e9
1307 << " GB in chunks of at most " << chunk_bytes / 1e9 << " GB ("
1308 << Index(std::ceil(total / chunk_bytes))
1309 << " pass(es) over the AO integrals)" << flush;
1310 }
1311 const std::vector<Eigen::MatrixXd> Ys = InactiveScreening::Kernels(
1312 auxbasis, dftbasis, inactive_, Mmn,
1313 orbitals_.MOs().eigenvalues().segment(
1314 gwopt_.rpamin, gwopt_.rpamax - gwopt_.rpamin + 1),
1315 orbitals_.getHomo(), gwopt_.rpamin, gwopt_.rpamax, chunk_bytes,
1316 frequencies, inactive_virtual_cutoff_);
1317 const Eigen::MatrixXd& Y = Ys.front();
1318 {
1319 std::chrono::duration<double> t =
1320 std::chrono::system_clock::now() - start;
1322 << TimeStamp() << " Inactive response at " << frequencies.size()
1323 << " frequencies in " << t.count() << " seconds" << flush;
1324 }
1325 const Eigen::VectorXd y = SymmetricEigen(Y, false).values;
1326 if (y.minCoeff() < -1e-6 * std::max(1.0, y.maxCoeff())) {
1328 << TimeStamp() << " Warning: the inactive response has a negative "
1329 << "eigenvalue " << y.minCoeff() << " (max " << y.maxCoeff()
1330 << "); is the embedding converged?" << flush;
1331 }
1332 {
1333 const Eigen::MatrixXd& Y_i = Ys[1];
1334 // the BSE: epsilon_BSE = 1 + X_A + Y at zero frequency, which gives
1335 // the same W as the combined reaction field, without redressing the
1336 // integrals; the environment keeps its reaction field
1337 inactive_static_bse = Y;
1338 if (gwopt_.sigma_integration == "ppm") {
1339 // exactly at the two points the PPM fits
1341 {{false, ppm.RealFittingFrequency(), Y}, {true, w_i, Y_i}});
1343 << TimeStamp() << " Inactive region enters the plasmon-pole "
1344 << "model of W (responses at 0 and " << w_i
1345 << "i Ha); GW keeps only the environment's reaction field" << flush;
1346 } else {
1347 // exact Sigma: the inactive region as poles fitted at the same
1348 // two points, which become pseudo-transitions of the RPA
1349 const InactiveScreening::PoleFit fit =
1350 InactiveScreening::FitPoles(Y, Y_i, w_i);
1352 << TimeStamp() << " Inactive region as " << fit.energies.size()
1353 << " poles fitted at 0 and " << w_i << "i Ha (energies "
1354 << (fit.energies.size() ? fit.energies.minCoeff() : 0.0) << " to "
1355 << (fit.energies.size() ? fit.energies.maxCoeff() : 0.0) << " Ha; "
1356 << fit.dropped << " directions below tolerance, " << fit.clipped
1357 << " modes clipped); they enter the RPA of "
1358 << gwopt_.sigma_integration << " Sigma as pseudo-transitions"
1359 << flush;
1360 if (fit.clipped > 0) {
1362 << TimeStamp() << " Warning: " << fit.clipped
1363 << " inactive modes have no real pole frequency (expected "
1364 << "only for inactive_screening exact); the fit is then not "
1365 << "exact at the two points" << flush;
1366 }
1367 Mmn.setAdditionalPoles(fit.energies, fit.vectors);
1368 }
1369 }
1370 std::chrono::duration<double> elapsed_time =
1371 std::chrono::system_clock::now() - start;
1373 << TimeStamp() << " Built inactive-region response in "
1374 << elapsed_time.count() << " seconds; eigenvalues of the static Y in ["
1375 << y.minCoeff() << ", " << y.maxCoeff() << "]" << flush;
1376 }
1377
1378 Eigen::MatrixXd Hqp;
1379 Eigen::MatrixXd Hqp_alpha;
1380 Eigen::MatrixXd Hqp_beta;
1381 if (do_gw_) {
1382
1383 std::chrono::time_point<std::chrono::system_clock> start =
1384 std::chrono::system_clock::now();
1385 if (is_uks) {
1386 if (gwopt_.do_qsgw) {
1387 throw std::runtime_error(
1388 "QSGW (gw.do_qsgw) is not implemented for open-shell (UKS) "
1389 "references.");
1390 }
1391 auto vxc = CalculateVXCSpinResolved(dftbasis);
1392 GW_UKS::options gwopt_uks;
1393 gwopt_uks.homo_alpha = orbitals_.getHomoAlpha();
1394 gwopt_uks.homo_beta = orbitals_.getHomoBeta();
1395 gwopt_uks.qpmin = gwopt_.qpmin;
1396 gwopt_uks.qpmax = gwopt_.qpmax;
1397 gwopt_uks.rpamin = gwopt_.rpamin;
1398 gwopt_uks.rpamax = gwopt_.rpamax;
1399 gwopt_uks.eta = gwopt_.eta;
1400 gwopt_uks.g_sc_limit = gwopt_.g_sc_limit;
1401 gwopt_uks.g_sc_max_iterations = gwopt_.g_sc_max_iterations;
1402 gwopt_uks.gw_sc_limit = gwopt_.gw_sc_limit;
1403 gwopt_uks.gw_sc_max_iterations = gwopt_.gw_sc_max_iterations;
1404 gwopt_uks.shift = gwopt_.shift;
1405 gwopt_uks.ScaHFX = gwopt_.ScaHFX;
1406 gwopt_uks.sigma_integration = gwopt_.sigma_integration;
1407 gwopt_uks.reset_3c = gwopt_.reset_3c;
1408 gwopt_uks.qp_solver = gwopt_.qp_solver;
1409 gwopt_uks.qp_solver_alpha = gwopt_.qp_solver_alpha;
1410 gwopt_uks.qp_grid_steps = gwopt_.qp_grid_steps;
1411 gwopt_uks.qp_grid_spacing = gwopt_.qp_grid_spacing;
1412
1413 // Forward the already-normalized canonical grid-search controls so that
1414 // RKS and UKS use the same search design and only differ in the
1415 // spin-dependent sigma evaluation.
1416 gwopt_uks.qp_full_window_half_width = gwopt_.qp_full_window_half_width;
1417 gwopt_uks.qp_dense_spacing = gwopt_.qp_dense_spacing;
1418 gwopt_uks.qp_adaptive_shell_width = gwopt_.qp_adaptive_shell_width;
1419 gwopt_uks.qp_adaptive_shell_count = gwopt_.qp_adaptive_shell_count;
1420 gwopt_uks.gw_mixing_order = gwopt_.gw_mixing_order;
1421 gwopt_uks.gw_mixing_alpha = gwopt_.gw_mixing_alpha;
1422 gwopt_uks.quadrature_scheme = gwopt_.quadrature_scheme;
1423 gwopt_uks.order = gwopt_.order;
1424 gwopt_uks.alpha = gwopt_.alpha;
1425 gwopt_uks.out_of_window_shift = gwopt_.out_of_window_shift;
1426 gwopt_uks.qp_restrict_search = gwopt_.qp_restrict_search;
1427 gwopt_uks.qp_root_continuity = gwopt_.qp_root_continuity;
1428 gwopt_uks.screening_update = gwopt_.screening_update;
1429 gwopt_uks.qp_root_tracking = gwopt_.qp_root_tracking;
1430 gwopt_uks.screening_update_ratio = gwopt_.screening_update_ratio;
1431 gwopt_uks.screening_update_max_inner = gwopt_.screening_update_max_inner;
1432 gwopt_uks.qp_zero_margin = gwopt_.qp_zero_margin;
1433 gwopt_uks.qp_virtual_min_energy = gwopt_.qp_virtual_min_energy;
1434 gwopt_uks.qp_root_finder = gwopt_.qp_root_finder;
1435 gwopt_uks.qp_grid_search_mode = gwopt_.qp_grid_search_mode;
1436
1437 GW_UKS gw = GW_UKS(*pLog_, Mmn_spin, vxc.first, vxc.second,
1438 orbitals_.MOs().eigenvalues(),
1439 orbitals_.MOs_beta().eigenvalues());
1440 gw.configure(gwopt_uks);
1441 if (gwopt_.rpamin > 0 && core_exchange_) {
1442 gw.setCoreExchange(
1444 auxbasis, dftbasis, orbitals_.MOs().eigenvectors(),
1445 gwopt_.rpamin, gwopt_.qpmin, gwopt_.qpmax),
1447 auxbasis, dftbasis, orbitals_.MOs_beta().eigenvectors(),
1448 gwopt_.rpamin, gwopt_.qpmin, gwopt_.qpmax));
1450 << TimeStamp() << " Sigma_x includes the exchange with the "
1451 << gwopt_.rpamin << " frozen core levels" << flush;
1452 }
1454 orbitals_.QPpertEnergiesAlpha() = gw.getGWAResultsAlpha();
1455 orbitals_.QPpertEnergiesBeta() = gw.getGWAResultsBeta();
1456 orbitals_.RPAInputEnergiesAlpha() = gw.RPAInputEnergiesAlpha();
1457 orbitals_.RPAInputEnergiesBeta() = gw.RPAInputEnergiesBeta();
1458 gw.CalculateHQP();
1459 Hqp_alpha = gw.getHQPAlpha();
1460 Hqp_beta = gw.getHQPBeta();
1461 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es_alpha =
1463 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es_beta =
1465 orbitals_.QPdiagAlpha().eigenvectors() = es_alpha.eigenvectors();
1466 orbitals_.QPdiagAlpha().eigenvalues() = es_alpha.eigenvalues();
1467 orbitals_.QPdiagBeta().eigenvectors() = es_beta.eigenvectors();
1468 orbitals_.QPdiagBeta().eigenvalues() = es_beta.eigenvalues();
1469 std::chrono::duration<double> elapsed_time =
1470 std::chrono::system_clock::now() - start;
1472 << TimeStamp() << " UKS GW calculation took " << elapsed_time.count()
1473 << " seconds." << flush;
1474 } else {
1475 Eigen::MatrixXd vxc = CalculateVXC(dftbasis);
1476 GW gw = GW(*pLog_, Mmn, vxc, orbitals_.MOs().eigenvalues());
1477 gw.configure(gwopt_);
1479 gw.setReactionField(reaction_field);
1480 if (streamed_holes_ > 0) {
1481 // the response of the levels not stored, at the plasmon-pole
1482 // fitting points, with the RPA energies of each screening build
1483 // (G0W0: once; evGW: every rebuild)
1484 gw.setScreeningHook([&](const Eigen::VectorXd& rpa_energies) {
1485 const PPM ppm;
1486 const double w_i = ppm.ImaginaryFittingFrequency();
1487 std::vector<Eigen::MatrixXd> Y;
1488 StreamedResponse(auxbasis, dftbasis, Mmn, rpa_energies,
1489 {RPA(*pLog_, Mmn).getEta(), w_i}, Y);
1491 {{false, ppm.RealFittingFrequency(), Y[0]}, {true, w_i, Y[1]}});
1492 });
1493 }
1494 if (gwopt_.rpamin > 0 && core_exchange_) {
1496 auxbasis, dftbasis, orbitals_.MOs().eigenvectors(), gwopt_.rpamin,
1497 gwopt_.qpmin, gwopt_.qpmax));
1499 << TimeStamp() << " Sigma_x includes the exchange with the "
1500 << gwopt_.rpamin << " frozen core levels" << flush;
1501 }
1502
1504
1505 if (!sigma_plot_states_.empty()) {
1508 }
1509
1510 orbitals_.QPpertEnergies() = gw.getGWAResults();
1511 orbitals_.RPAInputEnergies() = gw.RPAInputEnergies();
1512
1514 << TimeStamp() << " Calculating offdiagonal part of Sigma " << flush;
1515 if (gwopt_.do_qsgw) {
1516 // QSGW: iterate rotation of Mmn until QP energies converge.
1517 gw.CalculateQSGW();
1518
1519 // Print seed vs QSGW comparison. Label the seed column by what
1520 // CalculateGWPerturbation ran: G0W0 if one iteration, evGW otherwise.
1521 const std::string seed_label =
1522 (gwopt_.gw_sc_max_iterations == 1) ? "G0W0" : "evGW";
1523 Eigen::VectorXd qsgw_energies = gw.getGWAResults();
1524 gw.PrintQSGW_Energies(seed_label, gw.getQSGWSeedEnergies(),
1525 qsgw_energies);
1527
1528 // The QSGW orbitals and energies: QPdiag holds U (DFT MOs -> QSGW
1529 // orbitals, columns) and the energies, so that BSE-only runs from the
1530 // .orb file and everything built from the BSE vectors (transition
1531 // dipoles, densities) use C_qp = C_dft U (Orbitals::BSEOrbitals).
1532 // QPpertEnergies() keeps the G0W0/evGW seed. The RPA energies are
1533 // the QSGW ones in the window.
1534 orbitals_.QPdiag().eigenvalues() = qsgw_energies;
1535 orbitals_.QPdiag().eigenvectors() = gw.getQSGWRotation();
1536 orbitals_.setQSGW(true);
1537 orbitals_.RPAInputEnergies() = gw.RPAInputEnergies();
1538
1539 // The BSE in the QSGW orbitals: integrals refilled with C_qp (levels
1540 // outside the GW window stay DFT MOs, scissor-shifted in the RPA
1541 // energies and in AdjustHqpSize), and H_qp is diagonal.
1543 << TimeStamp() << " Rebuilding Mmn in QSGW QP wavefunction basis"
1544 << flush;
1545 Mmn.Fill(auxbasis, dftbasis, orbitals_.BSEOrbitals());
1547 << TimeStamp() << " Rebuilt Mmn (3-center-repulsion x QP orbitals)"
1548 << flush;
1549 Hqp = qsgw_energies.asDiagonal();
1550 } else {
1551 orbitals_.setQSGW(false);
1552 gw.CalculateHQP();
1554 << TimeStamp() << " Calculated offdiagonal part of Sigma "
1555 << flush;
1556
1557 Hqp = gw.getHQP();
1558
1559 if (do_sigma_dump_) {
1561 (gwopt_.gw_sc_max_iterations > 1) ? "evGW" : "G0W0",
1563 }
1564
1565 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es =
1567 if (es.info() == Eigen::ComputationInfo::Success) {
1569 << TimeStamp() << " Diagonalized QP Hamiltonian " << flush;
1570 }
1571
1572 orbitals_.QPdiag().eigenvectors() = es.eigenvectors();
1573 orbitals_.QPdiag().eigenvalues() = es.eigenvalues();
1574 }
1575 std::chrono::duration<double> elapsed_time =
1576 std::chrono::system_clock::now() - start;
1578 << TimeStamp() << " GW calculation took " << elapsed_time.count()
1579 << " seconds." << flush;
1580 }
1581
1582 } else {
1583 if (orbitals_.getGWAmax() != gwopt_.qpmax ||
1584 orbitals_.getGWAmin() != gwopt_.qpmin ||
1585 orbitals_.getRPAmax() != gwopt_.rpamax ||
1586 orbitals_.getRPAmin() != gwopt_.rpamin) {
1587 throw std::runtime_error(
1588 "The ranges for GW and RPA do not agree with the ranges from the "
1589 ".orb file, rerun your GW calculation");
1590 }
1591
1592 if (is_uks) {
1593 const Eigen::MatrixXd& qpcoeff_alpha =
1594 orbitals_.QPdiagAlpha().eigenvectors();
1595 const Eigen::MatrixXd& qpcoeff_beta =
1596 orbitals_.QPdiagBeta().eigenvectors();
1597
1598 Hqp_alpha = qpcoeff_alpha *
1599 orbitals_.QPdiagAlpha().eigenvalues().asDiagonal() *
1600 qpcoeff_alpha.transpose();
1601 Hqp_beta = qpcoeff_beta *
1602 orbitals_.QPdiagBeta().eigenvalues().asDiagonal() *
1603 qpcoeff_beta.transpose();
1604 } else if (orbitals_.isQSGW()) {
1605 // QSGW BSE-only run: rebuild Mmn_ in the QSGW orbitals stored in
1606 // QPdiag. Hqp is diagonal in that basis.
1608 << TimeStamp()
1609 << " Rebuilding Mmn in QSGW QP wavefunction basis (BSE-only)"
1610 << flush;
1611 Mmn.Fill(auxbasis, dftbasis, orbitals_.BSEOrbitals());
1613 << TimeStamp() << " Rebuilt Mmn (3-center-repulsion x QP orbitals)"
1614 << flush;
1615 Hqp = orbitals_.QPdiag().eigenvalues().asDiagonal();
1616 } else {
1617 const Eigen::MatrixXd& qpcoeff = orbitals_.QPdiag().eigenvectors();
1618
1619 Hqp = qpcoeff * orbitals_.QPdiag().eigenvalues().asDiagonal() *
1620 qpcoeff.transpose();
1621 }
1622 }
1623
1624 // proceed only if BSE requested
1626
1627 std::chrono::time_point<std::chrono::system_clock> start =
1628 std::chrono::system_clock::now();
1629
1630 if (is_uks) {
1631
1632 // Build a single spin-summed dielectric screening from the unrestricted
1633 // RPA response and reuse it for both exciton spin channels.
1634 RPA_UKS rpa_uks_bse(*pLog_, Mmn_spin);
1635 rpa_uks_bse.configure(orbitals_.getHomoAlpha(), orbitals_.getHomoBeta(),
1636 bseopt_.rpamin, bseopt_.rpamax);
1637 rpa_uks_bse.setRPAInputEnergies(orbitals_.RPAInputEnergiesAlpha(),
1638 orbitals_.RPAInputEnergiesBeta());
1639
1640 const SymmetricEigenSystem es_bse =
1641 SymmetricEigen(rpa_uks_bse.calculate_epsilon_r(0.0));
1642
1643 Eigen::VectorXd epsilon_0_inv_bse =
1644 Eigen::VectorXd::Zero(es_bse.values.size());
1645 for (Index i = 0; i < es_bse.values.size(); ++i) {
1646 if (es_bse.values(i) > 1e-8) {
1647 epsilon_0_inv_bse(i) = 1.0 / es_bse.values(i);
1648 }
1649 }
1650
1651 if (do_bse_exciton_uks_) {
1652 BSE_UKS::options bseopt_uks;
1653 bseopt_uks.useTDA = bseopt_.useTDA;
1654 bseopt_uks.rpamin = bseopt_.rpamin;
1655 bseopt_uks.rpamax = bseopt_.rpamax;
1656 bseopt_uks.qpmin = bseopt_.qpmin;
1657 bseopt_uks.qpmax = bseopt_.qpmax;
1658 bseopt_uks.vmin = bseopt_.vmin;
1659 bseopt_uks.cmax = bseopt_.cmax;
1660 bseopt_uks.nmax = bseopt_.nmax;
1661 bseopt_uks.davidson_correction = bseopt_.davidson_correction;
1662 bseopt_uks.davidson_tolerance = bseopt_.davidson_tolerance;
1663 bseopt_uks.davidson_update = bseopt_.davidson_update;
1664 bseopt_uks.davidson_maxiter = bseopt_.davidson_maxiter;
1665 bseopt_uks.min_print_weight = bseopt_.min_print_weight;
1666 bseopt_uks.use_Hqp_offdiag = bseopt_.use_Hqp_offdiag;
1667 bseopt_uks.max_dyn_iter = bseopt_.max_dyn_iter;
1668 bseopt_uks.dyn_tolerance = bseopt_.dyn_tolerance;
1669
1670 BSE_UKS bse_uks(*pLog_, Mmn_spin);
1672 bseopt_uks, orbitals_.getHomoAlpha(), orbitals_.getHomoBeta(),
1673 orbitals_.RPAInputEnergiesAlpha(), orbitals_.RPAInputEnergiesBeta(),
1674 Hqp_alpha, Hqp_beta, epsilon_0_inv_bse, es_bse.vectors);
1675
1678 << TimeStamp() << " Solved combined UKS BSE exciton problem "
1679 << flush;
1681
1684 }
1685 }
1686 } else {
1687 // the BSE screens statically: the inactive region enters epsilon at
1688 // zero frequency, the environment through its reaction field (so the
1689 // integrals keep GW's dressing)
1690 Mmn.setAdditionalPoles(Eigen::VectorXd(), Eigen::MatrixXd());
1691 if (streamed_holes_ > 0) {
1692 // again with the energies the BSE screens with
1693 std::vector<Eigen::MatrixXd> Y;
1694 StreamedResponse(auxbasis, dftbasis, Mmn, orbitals_.RPAInputEnergies(),
1695 {RPA(*pLog_, Mmn).getEta()}, Y);
1696 inactive_static_bse = Y[0];
1697 }
1698 BSE bse = BSE(*pLog_, Mmn);
1699 if (inactive_static_bse.size() > 0) {
1700 Mmn.setAdditionalResponse({{false, 0.0, inactive_static_bse}});
1701 } else {
1702 Mmn.setAdditionalResponse({});
1703 }
1704 bse.setReactionField(reaction_field, environment_.include_kreac);
1705 bse.configure(bseopt_, orbitals_.RPAInputEnergies(), Hqp);
1706
1707 if (do_bse_triplets_) {
1710 << TimeStamp() << " Solved BSE for triplets " << flush;
1712 }
1713
1714 if (do_bse_singlets_) {
1717 << TimeStamp() << " Solved BSE for singlets " << flush;
1719 }
1720
1722 if (do_bse_triplets_) {
1724 orbitals_);
1725 }
1726
1727 if (do_bse_singlets_) {
1729 orbitals_);
1730 }
1731 }
1732 }
1733
1734 std::chrono::duration<double> elapsed_time =
1735 std::chrono::system_clock::now() - start;
1736 XTP_LOG(Log::error, *pLog_) << TimeStamp() << " BSE calculation took "
1737 << elapsed_time.count() << " seconds." << flush;
1738 }
1739 if (const std::string usage = MemoryBudget::Usage(); !usage.empty()) {
1741 << TimeStamp() << " End of GWBSE: " << usage << flush;
1742 }
1744 << TimeStamp() << " GWBSE calculation finished " << flush;
1745 return true;
1746}
1747
1748Eigen::MatrixXd GWBSE::StreamedResponse(const AOBasis& auxbasis,
1749 const AOBasis& dftbasis,
1750 const TCMatrix_gwbse& Mmn,
1751 const Eigen::VectorXd& rpa_energies,
1752 const std::vector<double>& frequencies,
1753 std::vector<Eigen::MatrixXd>& Y) const {
1754 std::chrono::time_point<std::chrono::system_clock> start =
1755 std::chrono::system_clock::now();
1756 const Eigen::MatrixXd& C = orbitals_.MOs().eigenvectors();
1757 const Index nh = streamed_holes_;
1758 const Index nv = gwopt_.rpamax - gwopt_.homo;
1759 const Index lumo = gwopt_.homo + 1 - gwopt_.rpamin; // in rpa_energies
1760 double chunk_bytes = inactive_chunk_bytes_;
1761 if (chunk_bytes <= 0.0) {
1762 chunk_bytes = std::max(1e9, MemoryBudget::AvailableBytes());
1763 }
1765 auxbasis, dftbasis, C.middleCols(gwopt_.rpamin, nh),
1766 rpa_energies.head(nh), C.middleCols(gwopt_.homo + 1, nv),
1767 rpa_energies.segment(lumo, nv), chunk_bytes, frequencies, Mmn.InvSqrt());
1768 for (Eigen::MatrixXd& y : Y) {
1769 y = 0.5 * (y + y.transpose()).eval();
1770 }
1771 const double total =
1772 8.0 * double(nh) * double(nv) * double(auxbasis.AOBasisSize());
1773 std::chrono::duration<double> t = std::chrono::system_clock::now() - start;
1775 << TimeStamp() << " Streamed the response of " << nh
1776 << " occupied levels at " << frequencies.size() << " frequencies ("
1777 << total / 1e9 << " GB of integrals in "
1778 << Index(std::ceil(total / chunk_bytes)) << " pass(es)) in " << t.count()
1779 << " seconds" << flush;
1780 return Y.front();
1781}
1782
1783} // namespace xtp
1784} // namespace votca
class to manage program options with xml serialization functionality
Definition property.h:55
Property & add(const std::string &key, const std::string &value)
add a new property to structure
Definition property.cc:108
Property & get(const std::string &key)
get existing property
Definition property.cc:79
bool exists(const std::string &key) const
check whether property exists
Definition property.cc:122
T as() const
return value as type
Definition property.h:283
T ifExistsReturnElseReturnDefault(const std::string &key, T defaultvalue) const
Definition property.h:332
std::vector< Property * > Select(const std::string &filter)
select property based on a filter
Definition property.cc:185
void setAttribute(const std::string &attribute, const T &value)
set an attribute
Definition property.h:325
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Index AOBasisSize() const
Definition aobasis.h:46
void configure_with_precomputed_screening(const options &opt, Index homo_alpha, Index homo_beta, const Eigen::VectorXd &RPAInputEnergiesAlpha, const Eigen::VectorXd &RPAInputEnergiesBeta, const Eigen::MatrixXd &Hqp_alpha_in, const Eigen::MatrixXd &Hqp_beta_in, const Eigen::VectorXd &epsilon_0_inv, const Eigen::MatrixXd &epsilon_eigenvectors)
Definition bse_uks.cc:67
void Perturbative_DynamicalScreening(Orbitals &orb)
Definition bse_uks.cc:674
void Solve_excitons_uks(Orbitals &orb) const
Definition bse_uks.cc:474
void Analyze_excitons_uks(std::vector< QMFragment< BSE_Population > > fragments, const Orbitals &orb) const
Definition bse_uks.cc:631
void setReactionField(const Eigen::MatrixXd &R, bool include_kreac)
Screen by a static polarizable environment (embedded BSE).
Definition bse.cc:209
void Solve_singlets(Orbitals &orb) const
Definition bse.cc:376
void Analyze_singlets(std::vector< QMFragment< BSE_Population > > fragments, const Orbitals &orb) const
Definition bse.cc:614
void Perturbative_DynamicalScreening(const QMStateType &type, Orbitals &orb)
Definition bse.cc:942
void Analyze_triplets(std::vector< QMFragment< BSE_Population > > fragments, const Orbitals &orb) const
Definition bse.cc:673
void configure(const options &opt, const Eigen::VectorXd &RPAEnergies, const Eigen::MatrixXd &Hqp_in)
Definition bse.cc:63
void Solve_triplets(Orbitals &orb) const
Definition bse.cc:434
void Load(const std::string &name)
Definition basisset.cc:149
void Load(const std::string &name)
const ECPElement & getElement(std::string element_type) const
Index getNcore() const
Definition ecpbasisset.h:90
static Eigen::MatrixXd Kernel(const AOBasis &auxbasis, const ScreeningEnvironment &env)
B for a whole environment: ReactionFieldKernel of the explicit segments plus ShellKernel of the shell...
static Eigen::MatrixXd SymmetrizedReactionField(const Eigen::MatrixXd &B, const Eigen::MatrixXd &T)
R = T^T B T, the reaction field in the metric of the stored three-centre integrals,...
InactiveScreeningInput::Mode inactive_mode_
Definition gwbse.h:128
Eigen::MatrixXd StreamedResponse(const AOBasis &auxbasis, const AOBasis &dftbasis, const TCMatrix_gwbse &Mmn, const Eigen::VectorXd &rpa_energies, const std::vector< double > &frequencies, std::vector< Eigen::MatrixXd > &Y) const
Definition gwbse.cc:1748
InactiveScreeningInput inactive_
Definition gwbse.h:142
bool do_bse_exciton_uks_
Definition gwbse.h:101
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > CalculateVXCSpinResolved(const AOBasis &dftbasis)
Definition gwbse.cc:1020
std::vector< QMFragment< BSE_Population > > fragments_
Definition gwbse.h:125
std::string sigma_plot_states_
Definition gwbse.h:110
ScreeningEnvironment environment_
Definition gwbse.h:127
Index streamed_holes_
Definition gwbse.h:135
double inactive_virtual_cutoff_
Definition gwbse.h:131
BSE::options bseopt_
Definition gwbse.h:108
void addoutput(tools::Property &summary)
Definition gwbse.cc:810
bool do_bse_triplets_
Definition gwbse.h:99
std::string grid_
Definition gwbse.h:105
GW::sigma_dump_options sigma_dump_opt_
Definition gwbse.h:119
std::string auxbasis_name_
Definition gwbse.h:122
bool do_bse_singlets_
Definition gwbse.h:98
bool core_exchange_
Definition gwbse.h:113
Orbitals & orbitals_
Definition gwbse.h:94
Eigen::MatrixXd CalculateVXC(const AOBasis &dftbasis)
Definition gwbse.cc:980
Index sigma_plot_steps_
Definition gwbse.h:114
std::string functional_
Definition gwbse.h:104
std::string dftbasis_name_
Definition gwbse.h:123
double inactive_chunk_bytes_
Definition gwbse.h:130
bool do_sigma_dump_
Definition gwbse.h:118
void Initialize(tools::Property &options)
Definition gwbse.cc:77
Index CountCoreLevels()
Definition gwbse.cc:56
GW::options gwopt_
Definition gwbse.h:107
double sigma_plot_spacing_
Definition gwbse.h:115
Logger * pLog_
Definition gwbse.h:93
std::string sigma_plot_filename_
Definition gwbse.h:116
bool do_dynamical_screening_bse_
Definition gwbse.h:100
Eigen::SelfAdjointEigenSolver< Eigen::MatrixXd > DiagonalizeQPHamiltonianAlpha() const
Definition gw_uks.cc:1028
Eigen::VectorXd getGWAResultsAlpha() const
Definition gw_uks.cc:461
const Eigen::VectorXd & RPAInputEnergiesAlpha() const
Definition gw_uks.cc:471
const Eigen::VectorXd & RPAInputEnergiesBeta() const
Definition gw_uks.cc:474
Eigen::MatrixXd getHQPBeta() const
Definition gw_uks.cc:1021
Eigen::VectorXd getGWAResultsBeta() const
Definition gw_uks.cc:466
Eigen::MatrixXd getHQPAlpha() const
Definition gw_uks.cc:1016
void setCoreExchange(const Eigen::MatrixXd &alpha, const Eigen::MatrixXd &beta)
Frozen-core exchange per spin, see GW::setCoreExchange.
Definition gw_uks.h:103
Eigen::SelfAdjointEigenSolver< Eigen::MatrixXd > DiagonalizeQPHamiltonianBeta() const
Definition gw_uks.cc:1035
void configure(const options &opt)
Definition gw_uks.cc:41
void CalculateGWPerturbation()
Definition gw_uks.cc:192
const Eigen::VectorXd & getQSGWSeedEnergies() const
Return the seed (G0W0/evGW) energies that QSGW started from.
Definition gw.h:229
void setReactionField(const Eigen::MatrixXd &R)
Screen by a static polarizable environment (embedded GW).
Definition gw.cc:72
Eigen::SelfAdjointEigenSolver< Eigen::MatrixXd > DiagonalizeQPHamiltonian() const
Definition gw.cc:135
void DumpSigma(const sigma_dump_options &dopt, const std::string &gw_mode, const std::string &version)
Write the self-energy and screening data of the finished GW (G0W0 or evGW, ppm or exact) to a separat...
Definition gw.cc:1726
void setCoreExchange(const Eigen::MatrixXd &X)
Definition gw.h:197
void PrintQSGW_Energies(const std::string &seed_label, const Eigen::VectorXd &seed_energies, const Eigen::VectorXd &qsgw_energies) const
Print a two-column comparison of seed (G0W0 or evGW) vs converged QSGW quasiparticle energies.
Definition gw.cc:225
void CalculateQSGW()
Run quasiparticle self-consistent GW (QSGW).
Definition gw.cc:1187
void configure(const options &opt)
Definition gw.cc:43
Eigen::MatrixXd getHQP() const
Definition gw.cc:129
void setKeepScreeningData(bool keep)
Call after configure and before CalculateGWPerturbation.
Definition gw.h:298
void PrintQSGW_Composition(double threshold=0.01) const
Print the dominant DFT-KS orbital contributions to each converged QSGW quasiparticle state.
Definition gw.cc:182
void CalculateHQP()
Definition gw.cc:1147
Eigen::VectorXd getGWAResults() const
Definition gw.cc:554
Eigen::VectorXd RPAInputEnergies() const
Definition gw.h:305
void setScreeningHook(ScreeningHook hook)
Definition gw.h:190
const Eigen::MatrixXd & getQSGWRotation() const
Definition gw.h:226
void PlotSigma(std::string filename, Index steps, double spacing, std::string states)
Definition gw.cc:1666
void CalculateGWPerturbation()
Definition gw.cc:287
static PoleFit FitPoles(const Eigen::MatrixXd &Y0, const Eigen::MatrixXd &Yi, double w_i, double tolerance=1e-10)
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())
Eigen::MatrixXd & matrix()
Definition eigen.h:97
static std::string Usage()
static double AvailableBytes()
double ImaginaryFittingFrequency() const
Definition ppm.h:48
double RealFittingFrequency() const
Definition ppm.h:47
container for QM atoms
Definition qmatom.h:37
Unrestricted RPA helper for spin-resolved GW screening.
Definition rpa_uks.h:70
void setRPAInputEnergies(const Eigen::VectorXd &rpaenergies_alpha, const Eigen::VectorXd &rpaenergies_beta)
Set alpha and beta RPA input energies directly.
Definition rpa_uks.h:168
void configure(Index homo_alpha, Index homo_beta, Index rpamin, Index rpamax)
Definition rpa_uks.h:98
Eigen::MatrixXd calculate_epsilon_r(double frequency) const
Dielectric matrix on the real frequency axis.
Definition rpa_uks.h:131
double getEta() const
Definition rpa.h:47
static Mode Parse(const std::string &name)
void Fill(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 setAdditionalResponse(std::vector< ResponsePoint > points)
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 & InvSqrt() const
Index Removedfunctions() const
Definition threecenter.h:55
Timestamp returns the current time as a string Example: cout << TimeStamp().
Definition logger.h:224
Index getGridSize() const
Definition vxc_grid.h:41
void GridSetup(const std::string &type, const QMMolecule &atoms, const AOBasis &basis)
Definition vxc_grid.cc:230
Index getBoxesSize() const
Definition vxc_grid.h:42
Mat_p_Energy IntegrateVXC(const Eigen::MatrixXd &density_matrix) const
void setXCfunctional(const std::string &functional)
SpinResult IntegrateVXCSpin(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta) const
#define XTP_LOG(level, log)
Definition logger.h:40
const double ev2hrt
Definition constants.h:54
const double hrt2ev
Definition constants.h:53
const double bohr2ang
Definition constants.h:49
const std::string & ToolsVersionStr()
Definition version.cc:33
Index getMaxThreads()
Definition eigen.h:145
bool LapackAvailable()
Whether the routines below can run here (Accelerate build, macOS 13.3+).
void Cite(const std::string &key)
marks a reference as used (thread safe; unknown keys are printed as such)
Definition citations.cc:289
double LegacyAdaptiveShellWidth(const Opt &opt)
void NormalizeGridSearchOptions(Opt &opt)
double LegacyFullWindowHalfWidth(const Opt &opt)
Charge transport classes.
Definition ERIs.h:28
SymmetricEigenSystem SymmetricEigen(const Eigen::MatrixXd &A, bool vectors=true)
With vectors = false only the eigenvalues are computed.
ClassicalSegment< PolarSite > PolarSegment
std::string EigenBackendDescription()
Definition eigen.h:63
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
std::string davidson_update
Definition bse_uks.h:47
std::string davidson_tolerance
Definition bse_uks.h:46
std::string davidson_correction
Definition bse_uks.h:45
std::string screening_update
Definition gw_uks.h:82
std::string quadrature_scheme
Definition gw_uks.h:73
OutOfWindowShift out_of_window_shift
Definition gw_uks.h:35
std::string qp_grid_search_mode
Definition gw_uks.h:92
std::string sigma_integration
Definition gw_uks.h:49
std::string qp_root_finder
Definition gw_uks.h:91
static Mode Parse(const std::string &name)
static std::string Name(Mode mode)
Poles in the form of transitions that reproduce a response at zero and at one imaginary frequency: Y(...
static Mode Parse(const std::string &name)
static std::string Name(Mode mode)