votca 2026-dev
Loading...
Searching...
No Matches
qmregion.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2023 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#pragma once
21#ifndef VOTCA_XTP_QMREGION_H
22#define VOTCA_XTP_QMREGION_H
23
24// Local VOTCA includes
26#include "hist.h"
27#include "inactivescreening.h"
28#include "orbitals.h"
29#include "qmpackagefactory.h"
30#include "region.h"
31#include "statetracker.h"
32#include "vxc_grid.h"
33
40
41namespace votca {
42namespace xtp {
43
44class PolarRegion;
45class StaticRegion;
46class EwaldRegion;
47class QMRegion : public Region {
48
49 public:
50 QMRegion(Index id, Logger& log, std::string workdir)
51 : Region(id, log), workdir_(workdir) {};
52 ~QMRegion() override = default;
53
54 void Initialize(const tools::Property& prop) override;
55
56 bool Converged() const override;
57
58 void Evaluate(std::vector<std::unique_ptr<Region> >& regions) override;
59
60 void WriteToCpt(CheckpointWriter& w) const override;
61
62 void ReadFromCpt(CheckpointReader& r) override;
63
64 void ApplyQMFieldToPolarSegments(std::vector<PolarSegment>& segments) const;
65
66 // Builds the integration grid the background's potential is sampled on.
67 // Called lazily by InteractwithEwaldRegion rather than from Initialize,
68 // for two reasons. A job with no EwaldRegion must not pay for a grid it
69 // never uses -- and, less obviously, must not have ewald_grid_ready_ set,
70 // because Evaluate takes that flag as the signal to reject any qmpackage
71 // other than xtp. Building it eagerly would break every existing
72 // qmmm job that runs orca.
73 //
74 // Takes no options: the grid name and basis come from dftoptions_, which
75 // Initialize has already stored. They MUST be the ones DFTEngine uses --
76 // see the comment in the definition.
78
79 Index size() const override { return size_; }
80
81 void WritePDB(csg::PDBWriter& writer) const override;
82
83 std::string identify() const override { return "qmregion"; }
84
85 void push_back(const QMMolecule& mol);
86
87 void Reset() override;
88
89 double charge() const override;
90 double Etotal() const override { return E_hist_.back(); }
91
92 // Coordinates only, by value. That is not a convenience -- see
93 // ewaldgrid_ below for why the grid object itself must not leave this
94 // class.
95 std::vector<Eigen::Vector3d> copyEwaldGrid();
96
111 void EvaluateScreenedGWBSE(std::vector<std::unique_ptr<Region> >& regions);
112 // After EvaluateScreenedGWBSE: adds <screened_site_energies> (h, e and
113 // s or t, in eV, relative to the ground state) for -j read. No-op
114 // otherwise.
116 // Builds the reaction field for the job's polar regions and throws, with
117 // a diagnosis, unless 1 + R is positive definite. Needs no ground state:
118 // call it before the QM/MM loop. No-op without environment_screening.
120 const std::vector<std::unique_ptr<Region> >& regions) const;
121
122 protected:
123 void AppendResult(tools::Property& prop) const override;
124 double InteractwithQMRegion(const QMRegion& region) override;
125 double InteractwithPolarRegion(const PolarRegion& region) override;
126 double InteractwithStaticRegion(const StaticRegion& region) override;
127 double InteractwithEwaldRegion(const EwaldRegion& region) override;
128
129 private:
130 void AddNucleiFields(std::vector<PolarSegment>& segments,
131 const StaticSegment& seg) const;
133 const std::vector<std::unique_ptr<Region> >& regions) const;
134 std::string ScreeningAuxBasisName() const;
135
138
140 std::string workdir_ = "";
141 std::unique_ptr<QMPackage> qmpackage_ = nullptr;
142 // RI integrals and Ewald potential matrix kept across the inter-region
143 // iterations (DFT-only regions)
144 std::shared_ptr<DFTSetupCache> setup_cache_;
145
147
150 // S^1/2 of the DFT basis, for measuring density changes in the
151 // Loewdin-orthonormalised basis (empty: AO basis)
152 Eigen::MatrixXd overlap_sqrt_;
153 void UpdateOverlapSqrt();
154
155 // convergence options
156 double DeltaD_ = 5e-5;
157 double DeltaE_ = 5e-5;
158 double DeltaDmax_ = 1e-3;
159
160 bool do_gwbse_ = false;
161
162 // relaxed_density: the environment is polarized by (and convergence is
163 // measured with) the relaxed density of the tracked state instead of the
164 // unrelaxed one; computed once per GW-BSE run (cache)
165 bool relaxed_density_ = false;
166 mutable Eigen::MatrixXd relaxed_dmat_;
168 mutable bool relaxed_dmat_valid_ = false;
169 // density of state used by the loop: the relaxed one with relaxed_density
170 // (BSE states and perturbative QP levels), else Orbitals::DensityMatrixFull
171 Eigen::MatrixXd StateDensity(const QMState& state) const;
172
173 // environment screening (embedded GW-BSE)
174 bool screening_ = false;
176 std::vector<Index> screening_shell_regions_;
179 bool screened_done_ = false;
181 // DFT-in-DFT rewrite of orb_ and closed-shell check, before any GW-BSE
182 // Replaces the MOs by the embedded ones for a PbE run; returns the
183 // inactive-region screening input (gwbse.inactive_screening), which needs
184 // the full MOs and is built before they are replaced.
186 // state energy of the tracked state, added to the DFT total energy
187 double StateEnergy(const QMState& state) const;
188 bool do_localize_ = false;
189 bool do_dft_in_dft_ = false;
190 // PbE: the full reference MOs and occupation of the last iteration. The
191 // embedding and the GW-BSE preparation leave other MOs and electron
192 // counts in orb_; the next iteration's warm start and the inactive
193 // screening need these.
202
206
208
209 // for QMEwald. The periodic background reaches the Hamiltonian as a
210 // potential sampled on this grid, which the DFT engine integrates
211 // against the density.
212 //
213 // There used to be a second route, handing the engine multipole
214 // moments and k-vectors to build its own AO matrices from, gated by a
215 // second flag. Nothing ever set it up -- the legacy jobcalculator that
216 // once did was removed -- so it has been deleted along with the
217 // machinery behind it (DFTEngine's IntegrateEwald* helpers, the
218 // AOEwald* matrices, the ewaldcontainer types). Two things are worth
219 // recording about that, since the decision was to remove code that had
220 // been deliberately kept:
221 //
222 // - It was blocked anyway. A rank-1 (induced dipole) source needs
223 // operator-centre derivatives from libint2, which is why
224 // AOEwaldRealSpaceDipoles was never instantiated even from the dead
225 // path -- the real-space route split every dipole into a pair of
226 // point charges instead.
227 // - It would not have been the faster route in any case. It replaces
228 // a loop over grid points (linear in QM size) with one over shell
229 // pairs (quadratic), so it wins only for small QM regions -- the
230 // opposite of what it was being kept for.
231 //
232 // Recoverable from the git history if either of those ever changes.
233 //
234 // ONLY ITS POINTS AND VALUES ARE VALID. PrepareEwaldPotentialGrid
235 // builds this grid against a BasisSet, an AOBasis and a QMMolecule
236 // that are all locals of that function, and GridBox::
237 // FindSignificantShells stores raw `const AOShell*` into the basis it
238 // is handed (gridbox.cc, addShell(&store)). Those shells die with the
239 // function, so from the moment PrepareEwaldPotentialGrid returns this
240 // object holds dangling pointers.
241 //
242 // What remains safe is everything that does not follow them:
243 // getGridpoints, getPotentialValues, getBoxesSize, GridBox::size.
244 // CalcAOValues, Matrixsize, AddtoBigMatrix and anything else touching
245 // significant_shells is undefined behaviour. That is why the grid is
246 // never integrated on here or in QMPackage, and why DFTEngine rebuilds
247 // its own from grid_name_ and copies only the values across -- see
248 // dftengine.cc.
249 //
250 // Nothing dereferences them today. The public Vxc_Grid& accessor that
251 // used to sit next to copyEwaldGrid() was removed because it handed
252 // this object out with no way to know that, and had no callers.
253 // Giving the basis a longer life would remove the hazard, but it buys
254 // nothing on its own: the transport is by value either way, and
255 // DFTEngine has to integrate against the AO ordering of its own
256 // dftbasis_ regardless.
258 bool ewald_grid_ready_ = false;
259 // Whether the background's potential has already been laid down on that
260 // grid. Separate from ewald_grid_ready_ because the grid is built once
261 // and the potential is evaluated once, but for different reasons: the
262 // grid because geometry and basis are fixed, the potential because the
263 // background is frozen. See InteractwithEwaldRegion.
265 // sum_A Z_A phi(R_A). The grid carries the potential the ELECTRONS
266 // feel; the nuclei sit in the same potential and have no other way in.
268};
269
270} // namespace xtp
271} // namespace votca
272
273#endif // VOTCA_XTP_QMREGION_H
class to manage program options with xml serialization functionality
Definition property.h:55
The periodic Ewald background, as a Region.
Definition ewaldregion.h:74
Logger is used for thread-safe output of messages.
Definition logger.h:164
Container for molecular orbitals and derived one-particle data.
Definition orbitals.h:47
void AppendResult(tools::Property &prop) const override
Definition qmregion.cc:841
StateTracker statetracker_
Definition qmregion.h:207
hist< Eigen::MatrixXd > Dmat_hist_
Definition qmregion.h:149
void PrepareEwaldPotentialGrid()
Definition qmregion.cc:467
std::string grid_accuracy_for_ext_interaction_
Definition qmregion.h:146
std::string identify() const override
Definition qmregion.h:83
double Etotal() const override
Definition qmregion.h:90
hist< double > E_hist_
Definition qmregion.h:148
void AddNucleiFields(std::vector< PolarSegment > &segments, const StaticSegment &seg) const
Definition qmregion.cc:895
std::shared_ptr< DFTSetupCache > setup_cache_
Definition qmregion.h:144
void SaveReferenceOrbitals()
Definition qmregion.cc:189
std::unique_ptr< QMPackage > qmpackage_
Definition qmregion.h:141
bool screening_include_kreac_
Definition qmregion.h:175
void push_back(const QMMolecule &mol)
Definition qmregion.cc:800
ReferenceOrbitals reference_
Definition qmregion.h:199
void WriteToCpt(CheckpointWriter &w) const override
Definition qmregion.cc:990
double InteractwithStaticRegion(const StaticRegion &region) override
Definition qmregion.cc:886
void EvaluateScreenedGWBSE(std::vector< std::unique_ptr< Region > > &regions)
Definition qmregion.cc:393
QMState relaxed_dmat_state_
Definition qmregion.h:167
void AppendScreenedSiteEnergies(tools::Property &prop) const
Definition qmregion.cc:428
std::string ScreeningAuxBasisName() const
Definition qmregion.cc:340
double ewald_nuclear_energy_
Definition qmregion.h:267
double InteractwithPolarRegion(const PolarRegion &region) override
Definition qmregion.cc:870
std::string workdir_
Definition qmregion.h:140
void Reset() override
Definition qmregion.cc:849
QMState screened_state_
Definition qmregion.h:180
QMRegion(Index id, Logger &log, std::string workdir)
Definition qmregion.h:50
double StateEnergy(const QMState &state) const
Definition qmregion.cc:264
Index size() const override
Definition qmregion.h:79
void WritePDB(csg::PDBWriter &writer) const override
Definition qmregion.cc:891
void RestoreReferenceOrbitals()
Definition qmregion.cc:198
double InteractwithEwaldRegion(const EwaldRegion &region) override
Definition qmregion.cc:1035
void ReadFromCpt(CheckpointReader &r) override
Definition qmregion.cc:1012
ScreeningEnvironment BuildScreeningEnvironment(const std::vector< std::unique_ptr< Region > > &regions) const
Definition qmregion.cc:276
InactiveScreeningInput PrepareOrbitalsForGWBSE()
Definition qmregion.cc:209
bool Converged() const override
Definition qmregion.cc:506
void Evaluate(std::vector< std::unique_ptr< Region > > &regions) override
Definition qmregion.cc:545
tools::Property localize_options_
Definition qmregion.h:205
std::vector< Eigen::Vector3d > copyEwaldGrid()
Definition qmregion.cc:458
double InteractwithQMRegion(const QMRegion &region) override
Definition qmregion.cc:865
void ApplyQMFieldToPolarSegments(std::vector< PolarSegment > &segments) const
Definition qmregion.cc:907
bool ewald_potential_evaluated_
Definition qmregion.h:264
Eigen::MatrixXd StateDensity(const QMState &state) const
Definition qmregion.cc:743
Eigen::MatrixXd overlap_sqrt_
Definition qmregion.h:152
double screening_shell_dielectric_
Definition qmregion.h:177
~QMRegion() override=default
bool EnvironmentScreeningEnabled() const
Embedded GW-BSE, screened by the polar regions (see EnvironmentScreening, GWBSE::setScreeningEnvironm...
Definition qmregion.h:110
void Initialize(const tools::Property &prop) override
Definition qmregion.cc:50
std::vector< Index > screening_shell_regions_
Definition qmregion.h:176
double charge() const override
Definition qmregion.cc:809
double screening_site_width_
Definition qmregion.h:178
Eigen::MatrixXd relaxed_dmat_
Definition qmregion.h:166
void CheckScreeningEnvironment(const std::vector< std::unique_ptr< Region > > &regions) const
Definition qmregion.cc:354
tools::Property dftoptions_
Definition qmregion.h:203
tools::Property gwbseoptions_
Definition qmregion.h:204
Identifier for QMstates. Strings like S1 are converted into enum +zero indexed int.
Definition qmstate.h:135
Region(Index id, Logger &log)
Definition region.h:51
Tracks from a spectrum of states the state, which fullfills certain criteria.
ClassicalSegment< StaticSite > StaticSegment
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...
The polarizable environment a GW-BSE calculation is screened by: what a QM/MM job hands to GWBSE (GWB...