votca 2026-dev
Loading...
Searching...
No Matches
gwbseengine.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2022 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// Third party includes
21#include <boost/format.hpp>
22#include <string>
23#include <utility>
24
25// Local VOTCA includes
27
28#include "votca/xtp/gwbse.h"
30#include "votca/xtp/logger.h"
32#include "votca/xtp/qmpackage.h"
33
34using boost::format;
35using std::flush;
36namespace votca {
37namespace xtp {
38
39// +++++++++++++++++++++++++++++ //
40// GWBSEENGINE MEMBER FUNCTIONS //
41// +++++++++++++++++++++++++++++ //
42
44 std::string archive_filename) {
45
46 archive_file_ = archive_filename;
47
48 std::string tasks_string = options.get(".tasks").as<std::string>();
49
50 // We split either on a space or a comma
51 tools::Tokenizer tokenizedTasks(tasks_string, " ,");
52 std::vector<std::string> tasks = tokenizedTasks.ToVector();
53 // Check which tasks exist and set them to true
54 do_guess_ = std::find(tasks.begin(), tasks.end(), "guess") != tasks.end();
55 do_dft_input_ = std::find(tasks.begin(), tasks.end(), "input") != tasks.end();
56 do_dft_run_ = std::find(tasks.begin(), tasks.end(), "dft") != tasks.end();
57 do_dft_parse_ = std::find(tasks.begin(), tasks.end(), "parse") != tasks.end();
58 do_gwbse_ = std::find(tasks.begin(), tasks.end(), "gwbse") != tasks.end();
60 std::find(tasks.begin(), tasks.end(), "localize") != tasks.end();
62 std::find(tasks.begin(), tasks.end(), "dft_in_dft") != tasks.end();
63
64 // XML option file for GWBSE
65 if (do_gwbse_) {
66 gwbse_options_ = options.get(".gwbse");
67 }
68 if (do_localize_) {
69 localize_options_ = options.get(".localize");
70 }
71 // DFT log and MO file names
72 MO_file_ = qmpackage_->getMOFile();
73 dftlog_file_ = qmpackage_->getLogFile();
74
75 // Logger redirection
76 if (options.exists(".logging_file")) {
77 logger_file_ = options.get(".logging_file").as<std::string>();
78 }
79
80 // for requested merged guess, two archived orbitals objects are needed
81 if (do_guess_) {
82 guess_archiveA_ = options.get(".archiveA").as<std::string>();
83 guess_archiveB_ = options.get(".archiveB").as<std::string>();
84 }
85
86 return;
87}
88
89/*
90 * CALL DFT and GWBSE modules to get excitation energies
91 *
92 */
93
95
96 // redirect log, if required
97 // define own logger for GW-BSE that is written into a runFolder logfile
98 Logger gwbse_engine_logger(pLog_->getReportLevel());
99 Logger* logger = pLog_;
100 if (!logger_file_.empty()) {
101 gwbse_engine_logger.setMultithreading(false);
102 gwbse_engine_logger.setPreface(Log::info, "\n ...");
103 gwbse_engine_logger.setPreface(Log::error, "\n ...");
104 gwbse_engine_logger.setPreface(Log::warning, "\n ...");
105 gwbse_engine_logger.setPreface(Log::debug, "\n ...");
106 logger = &gwbse_engine_logger;
107 }
108 qmpackage_->setLog(logger);
109 if (do_dft_input_) {
110 // required for merged guess
111 if (qmpackage_->GuessRequested() && do_guess_) { // do not want to do an
112 // SCF loop for a dimer
113 XTP_LOG(Log::error, *logger)
114 << "Guess requested, reading molecular orbitals" << flush;
115 Orbitals orbitalsA, orbitalsB;
116 orbitalsA.ReadFromCpt(guess_archiveA_);
117 orbitalsB.ReadFromCpt(guess_archiveB_);
118 orbitals.PrepareDimerGuess(orbitalsA, orbitalsB);
119 }
120 qmpackage_->WriteInputFile(orbitals);
121 }
122 if (do_dft_run_) {
123 bool run_success = qmpackage_->Run();
124 if (!run_success) {
125 throw std::runtime_error("\n DFT-run failed. Stopping!");
126 }
127 }
128
129 // parse DFT data, if required
130 if (do_dft_parse_) {
131 XTP_LOG(Log::error, *logger) << "Parsing DFT data from " << dftlog_file_
132 << " and " << MO_file_ << flush;
133 qmpackage_->setLogFileName(dftlog_file_);
134 qmpackage_->setMOsFileName(MO_file_);
135
136 bool Logfile_parse = qmpackage_->ParseLogFile(orbitals);
137 if (!Logfile_parse) {
138 throw std::runtime_error("\n Parsing DFT logfile " + dftlog_file_ +
139 " failed. Stopping!");
140 }
141 bool Orbfile_parse = qmpackage_->ParseMOsFile(orbitals);
142 if (!Orbfile_parse) {
143 throw std::runtime_error("\n Parsing DFT orbfile " + MO_file_ +
144 " failed. Stopping!");
145 }
146 qmpackage_->CleanUp();
147 }
148
149 if (do_localize_) {
150 if (!do_dft_parse_) {
151 orbitals.ReadFromCpt(archive_file_);
152 }
153 if (orbitals.isOpenShell()) {
154 throw std::runtime_error(
155 "MO localization not implemented for open-shell systems");
156 }
157 PMLocalization pml(*logger, localize_options_);
158 pml.computePML(orbitals);
159 }
160
162 if (!do_localize_) {
163 XTP_LOG(Log::error, *logger)
164 << "Loading serialized data from " << archive_file_ << flush;
165 orbitals.ReadFromCpt(archive_file_);
166 if (do_dft_in_dft_) {
167 if (orbitals.getLMOs().size() == 0) {
168 throw std::runtime_error(
169 "Can't do DFT in DFT embedding without localization");
170 }
171 }
172 }
173 }
174
175 if (do_dft_in_dft_) {
176 if (orbitals.isOpenShell()) {
177 throw std::runtime_error(
178 "DFT-in-DFT not implemented for open-shell systems");
179 }
180 qmpackage_->WriteInputFile(orbitals);
181 bool run_success = qmpackage_->RunActiveRegion();
182 if (!run_success) {
183 throw std::runtime_error("\n DFT in DFT embedding failed. Stopping!");
184 }
185 bool Logfile_parse = qmpackage_->ParseLogFile(orbitals);
186 if (!Logfile_parse) {
187 throw std::runtime_error("\n Parsing DFT logfile " + dftlog_file_ +
188 " failed. Stopping!");
189 }
190 bool Orbfile_parse = qmpackage_->ParseMOsFile(orbitals);
191 if (!Orbfile_parse) {
192 throw std::runtime_error("\n Parsing DFT orbfile " + MO_file_ +
193 " failed. Stopping!");
194 }
195 }
196
197 // if no parsing of DFT data is requested, reload serialized orbitals object
198
199 tools::Property& output_summary = summary_.add("output", "");
200
201 if (do_gwbse_) {
202 const InactiveScreeningInput::Mode inactive_mode =
204 gwbse_options_.ifExistsReturnElseReturnDefault<std::string>(
205 "inactive_screening", "none"));
206 if (do_dft_in_dft_ || orbitals.getEmbeddedMOs().eigenvectors().size() > 0) {
207 // from the full reference MOs, before the embedded ones replace them
209 orbitals, inactive_mode,
210 gwbse_options_.ifExistsReturnElseReturnDefault<bool>(
211 "inactive_screening_complete_g", false),
212 gwbse_options_.ifExistsReturnElseReturnDefault<double>(
213 "inactive_screening_radius", 0.0) *
215 Orbitals orb_embedded = orbitals;
216 orb_embedded.MOs() = orb_embedded.getEmbeddedMOs();
217
218 Index active_electrons = orb_embedded.getNumOfActiveElectrons();
219 if (active_electrons % 2 != 0) {
220 throw std::runtime_error(
221 "Embedded GWBSE currently supports only closed-shell active "
222 "spaces with an even number of electrons");
223 }
224
225 Index active_occ = active_electrons / 2;
226 if (inactive.complete_g) {
227 // inactive occupied levels in front of the active ones
229 orb_embedded.getEmbeddedMOs(), active_occ, inactive);
230 active_occ += inactive.n_inactive;
231 }
232
233 // Restricted closed-shell rewrite of the embedded orbital object
234 orb_embedded.setNumberOfOccupiedLevels(active_occ);
235 orb_embedded.setNumberOfOccupiedLevelsBeta(active_occ);
236 orb_embedded.setNumberOfAlphaElectrons(active_occ);
237 orb_embedded.setNumberOfBetaElectrons(active_occ);
238 orb_embedded.setChargeAndSpin(orb_embedded.getCharge(), 1);
239
240 GWBSE gwbse = GWBSE(orb_embedded);
241 gwbse.setLogger(logger);
242 gwbse.setInactiveScreening(std::move(inactive));
244 gwbse.Evaluate();
245 gwbse.addoutput(output_summary);
246 orbitals = orb_embedded;
247 } else {
248 GWBSE gwbse = GWBSE(orbitals);
249 gwbse.setLogger(logger);
251 gwbse.Evaluate();
252 gwbse.addoutput(output_summary);
253 }
254 }
255
256 if (!logger_file_.empty()) {
257 WriteLoggerToFile(logger);
258 }
259 return;
260}
261
263 std::ofstream ofs;
264 ofs.open(logger_file_, std::ofstream::out);
265 if (!ofs.is_open()) {
266 throw std::runtime_error("Bad file handle: " + logger_file_);
267 }
268 ofs << (*pLog) << std::endl;
269 ofs.close();
270 return;
271}
272
273} // namespace xtp
274} // namespace votca
const Eigen::MatrixXd & eigenvectors() const
Definition eigensystem.h:33
class to manage program options with xml serialization functionality
Definition property.h:55
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
break string into words
Definition tokenizer.h:72
std::string archive_file_
Definition gwbseengine.h:79
void WriteLoggerToFile(Logger *pLog)
tools::Property gwbse_options_
Definition gwbseengine.h:84
void ExcitationEnergies(Orbitals &orbitals)
std::string guess_archiveA_
Definition gwbseengine.h:80
tools::Property localize_options_
Definition gwbseengine.h:85
tools::Property summary_
Definition gwbseengine.h:87
void Initialize(tools::Property &options, std::string archive_filename)
std::string guess_archiveB_
Definition gwbseengine.h:81
Electronic excitations from GW-BSE.
Definition gwbse.h:61
void addoutput(tools::Property &summary)
Definition gwbse.cc:810
void setInactiveScreening(InactiveScreeningInput input)
Definition gwbse.h:80
void Initialize(tools::Property &options)
Definition gwbse.cc:77
void setLogger(Logger *pLog)
Definition gwbse.h:68
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 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)...
Logger is used for thread-safe output of messages.
Definition logger.h:164
void setPreface(Log::Level level, const std::string &preface)
Definition logger.h:194
void setMultithreading(bool maverick)
Definition logger.h:186
Container for molecular orbitals and derived one-particle data.
Definition orbitals.h:47
Index getNumOfActiveElectrons()
Return the number of electrons assigned to the active region.
Definition orbitals.h:745
Index getCharge() const
Return the stored total charge.
Definition orbitals.h:254
void PrepareDimerGuess(const Orbitals &orbitalsA, const Orbitals &orbitalsB)
Guess for a dimer based on monomer orbitals.
Definition orbitals.cc:970
void setNumberOfAlphaElectrons(Index electrons)
Store the total number of alpha electrons.
Definition orbitals.h:139
const Eigen::MatrixXd & getLMOs() const
Return localized molecular orbitals, if available.
Definition orbitals.h:733
void setNumberOfBetaElectrons(Index electrons)
Store the total number of beta electrons.
Definition orbitals.h:144
void setNumberOfOccupiedLevels(Index occupied_levels)
Definition orbitals.h:122
const tools::EigenSystem & MOs() const
Return read-only access to alpha/restricted molecular orbitals.
Definition orbitals.h:192
void ReadFromCpt(const std::string &filename)
Read the orbital container from a checkpoint file on disk.
Definition orbitals.cc:1242
void setNumberOfOccupiedLevelsBeta(Index occupied_levels_beta)
Store the number of occupied beta-spin orbitals.
Definition orbitals.h:134
void setChargeAndSpin(Index charge, Index spin)
Definition orbitals.h:246
bool isOpenShell() const
Report whether the stored state corresponds to an open-shell system.
Definition orbitals.h:256
const tools::EigenSystem & getEmbeddedMOs() const
Return molecular orbitals obtained from an embedding calculation.
Definition orbitals.h:61
void computePML(Orbitals &orbitals)
#define XTP_LOG(level, log)
Definition logger.h:40
const double ang2bohr
Definition constants.h:48
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
What GWBSE needs to add the static screening of the inactive region: the mode and the orbitals of the...
static Mode Parse(const std::string &name)