votca 2024.2-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
24// Local VOTCA includes
25#include "votca/xtp/gwbse.h"
27#include "votca/xtp/logger.h"
29#include "votca/xtp/qmpackage.h"
30
31using boost::format;
32using std::flush;
33namespace votca {
34namespace xtp {
35
36// +++++++++++++++++++++++++++++ //
37// GWBSEENGINE MEMBER FUNCTIONS //
38// +++++++++++++++++++++++++++++ //
39
41 std::string archive_filename) {
42
43 archive_file_ = archive_filename;
44
45 std::string tasks_string = options.get(".tasks").as<std::string>();
46
47 // We split either on a space or a comma
48 tools::Tokenizer tokenizedTasks(tasks_string, " ,");
49 std::vector<std::string> tasks = tokenizedTasks.ToVector();
50 // Check which tasks exist and set them to true
51 do_guess_ = std::find(tasks.begin(), tasks.end(), "guess") != tasks.end();
52 do_dft_input_ = std::find(tasks.begin(), tasks.end(), "input") != tasks.end();
53 do_dft_run_ = std::find(tasks.begin(), tasks.end(), "dft") != tasks.end();
54 do_dft_parse_ = std::find(tasks.begin(), tasks.end(), "parse") != tasks.end();
55 do_gwbse_ = std::find(tasks.begin(), tasks.end(), "gwbse") != tasks.end();
57 std::find(tasks.begin(), tasks.end(), "localize") != tasks.end();
59 std::find(tasks.begin(), tasks.end(), "dft_in_dft") != tasks.end();
60
61 // XML option file for GWBSE
62 if (do_gwbse_) {
63 gwbse_options_ = options.get(".gwbse");
64 }
65 if (do_localize_) {
66 localize_options_ = options.get(".localize");
67 }
68 // DFT log and MO file names
71
72 // Logger redirection
73 if (options.exists(".logging_file")) {
74 logger_file_ = options.get(".logging_file").as<std::string>();
75 }
76
77 // for requested merged guess, two archived orbitals objects are needed
78 if (do_guess_) {
79 guess_archiveA_ = options.get(".archiveA").as<std::string>();
80 guess_archiveB_ = options.get(".archiveB").as<std::string>();
81 }
82
83 return;
84}
85
86/*
87 * CALL DFT and GWBSE modules to get excitation energies
88 *
89 */
90
92
93 // redirect log, if required
94 // define own logger for GW-BSE that is written into a runFolder logfile
95 Logger gwbse_engine_logger(pLog_->getReportLevel());
96 Logger* logger = pLog_;
97 if (!logger_file_.empty()) {
98 gwbse_engine_logger.setMultithreading(false);
99 gwbse_engine_logger.setPreface(Log::info, "\n ...");
100 gwbse_engine_logger.setPreface(Log::error, "\n ...");
101 gwbse_engine_logger.setPreface(Log::warning, "\n ...");
102 gwbse_engine_logger.setPreface(Log::debug, "\n ...");
103 logger = &gwbse_engine_logger;
104 }
105 qmpackage_->setLog(logger);
106 if (do_dft_input_) {
107 // required for merged guess
108 if (qmpackage_->GuessRequested() && do_guess_) { // do not want to do an
109 // SCF loop for a dimer
110 XTP_LOG(Log::error, *logger)
111 << "Guess requested, reading molecular orbitals" << flush;
112 Orbitals orbitalsA, orbitalsB;
113 orbitalsA.ReadFromCpt(guess_archiveA_);
114 orbitalsB.ReadFromCpt(guess_archiveB_);
115 orbitals.PrepareDimerGuess(orbitalsA, orbitalsB);
116 }
117 qmpackage_->WriteInputFile(orbitals);
118 }
119 if (do_dft_run_) {
120 bool run_success = qmpackage_->Run();
121 if (!run_success) {
122 throw std::runtime_error("\n DFT-run failed. Stopping!");
123 }
124 }
125
126 // parse DFT data, if required
127 if (do_dft_parse_) {
128 XTP_LOG(Log::error, *logger) << "Parsing DFT data from " << dftlog_file_
129 << " and " << MO_file_ << flush;
132
133 bool Logfile_parse = qmpackage_->ParseLogFile(orbitals);
134 if (!Logfile_parse) {
135 throw std::runtime_error("\n Parsing DFT logfile " + dftlog_file_ +
136 " failed. Stopping!");
137 }
138 bool Orbfile_parse = qmpackage_->ParseMOsFile(orbitals);
139 if (!Orbfile_parse) {
140 throw std::runtime_error("\n Parsing DFT orbfile " + MO_file_ +
141 " failed. Stopping!");
142 }
144 }
145
146 if (do_localize_) {
147 if (!do_dft_parse_) {
148 orbitals.ReadFromCpt(archive_file_);
149 }
150 if (orbitals.isOpenShell()) {
151 throw std::runtime_error(
152 "MO localization not implemented for open-shell systems");
153 }
154 PMLocalization pml(*logger, localize_options_);
155 pml.computePML(orbitals);
156 }
157
159 if (!do_localize_) {
160 XTP_LOG(Log::error, *logger)
161 << "Loading serialized data from " << archive_file_ << flush;
162 orbitals.ReadFromCpt(archive_file_);
163 if (do_dft_in_dft_) {
164 if (orbitals.getLMOs().size() == 0) {
165 throw std::runtime_error(
166 "Can't do DFT in DFT embedding without localization");
167 }
168 }
169 }
170 }
171
172 if (do_dft_in_dft_) {
173 if (orbitals.isOpenShell()) {
174 throw std::runtime_error(
175 "DFT-in-DFT not implemented for open-shell systems");
176 }
177 qmpackage_->WriteInputFile(orbitals);
178 bool run_success = qmpackage_->RunActiveRegion();
179 if (!run_success) {
180 throw std::runtime_error("\n DFT in DFT embedding failed. Stopping!");
181 }
182 bool Logfile_parse = qmpackage_->ParseLogFile(orbitals);
183 if (!Logfile_parse) {
184 throw std::runtime_error("\n Parsing DFT logfile " + dftlog_file_ +
185 " failed. Stopping!");
186 }
187 bool Orbfile_parse = qmpackage_->ParseMOsFile(orbitals);
188 if (!Orbfile_parse) {
189 throw std::runtime_error("\n Parsing DFT orbfile " + MO_file_ +
190 " failed. Stopping!");
191 }
192 }
193
194 // if no parsing of DFT data is requested, reload serialized orbitals object
195
196 tools::Property& output_summary = summary_.add("output", "");
197
198 if (do_gwbse_) {
199 if (orbitals.isOpenShell()) {
200 throw std::runtime_error("GWBSE not implemented for open-shell systems");
201 }
202 if (do_dft_in_dft_ || orbitals.getEmbeddedMOs().eigenvectors().size() > 0) {
203 Orbitals orb_embedded = orbitals;
204 orb_embedded.MOs() = orb_embedded.getEmbeddedMOs();
205 Index active_electrons = orb_embedded.getNumOfActiveElectrons();
206 orb_embedded.setNumberOfAlphaElectrons(active_electrons);
207 orb_embedded.setNumberOfOccupiedLevels(active_electrons / 2);
208 GWBSE gwbse = GWBSE(orb_embedded);
209 gwbse.setLogger(logger);
211 gwbse.Evaluate();
212 gwbse.addoutput(output_summary);
213 orbitals = orb_embedded;
214 } else {
215 GWBSE gwbse = GWBSE(orbitals);
216 gwbse.setLogger(logger);
218 gwbse.Evaluate();
219 gwbse.addoutput(output_summary);
220 }
221 }
222
223 if (!logger_file_.empty()) {
224 WriteLoggerToFile(logger);
225 }
226 return;
227}
228
230 std::ofstream ofs;
231 ofs.open(logger_file_, std::ofstream::out);
232 if (!ofs.is_open()) {
233 throw std::runtime_error("Bad file handle: " + logger_file_);
234 }
235 ofs << (*pLog) << std::endl;
236 ofs.close();
237 return;
238}
239
240} // namespace xtp
241} // 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 & 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
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:56
bool Evaluate()
Definition gwbse.cc:544
void addoutput(tools::Property &summary)
Definition gwbse.cc:422
void Initialize(tools::Property &options)
Definition gwbse.cc:60
void setLogger(Logger *pLog)
Definition gwbse.h:63
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
Log::Level getReportLevel() const
Definition logger.h:192
void setMultithreading(bool maverick)
Definition logger.h:186
container for molecular orbitals
Definition orbitals.h:46
Index getNumOfActiveElectrons()
Definition orbitals.h:419
void PrepareDimerGuess(const Orbitals &orbitalsA, const Orbitals &orbitalsB)
Guess for a dimer based on monomer orbitals.
Definition orbitals.cc:571
void setNumberOfAlphaElectrons(Index electrons)
Definition orbitals.h:96
const Eigen::MatrixXd & getLMOs() const
Definition orbitals.h:411
void setNumberOfOccupiedLevels(Index occupied_levels)
Definition orbitals.h:78
const tools::EigenSystem & MOs() const
Definition orbitals.h:122
void ReadFromCpt(const std::string &filename)
Definition orbitals.cc:692
bool isOpenShell() const
Definition orbitals.h:168
const tools::EigenSystem & getEmbeddedMOs() const
Definition orbitals.h:56
void computePML(Orbitals &orbitals)
void setLog(Logger *pLog)
Definition qmpackage.h:85
virtual void CleanUp()=0
bool GuessRequested() const
Definition qmpackage.h:92
void setLogFileName(const std::string &log_file_name)
Definition qmpackage.h:79
std::string getMOFile() const
Definition qmpackage.h:102
virtual bool WriteInputFile(const Orbitals &orbitals)=0
writes a coordinate file WITHOUT taking into account PBCs
virtual bool ParseLogFile(Orbitals &orbitals)=0
virtual bool ParseMOsFile(Orbitals &orbitals)=0
void setMOsFileName(const std::string &mo_file)
Definition qmpackage.h:83
std::string getLogFile() const
Definition qmpackage.h:100
#define XTP_LOG(level, log)
Definition logger.h:40
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26