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 "orbitals.h"
28#include "qmpackagefactory.h"
29#include "region.h"
30#include "statetracker.h"
31#include "vxc_grid.h"
32
39
40namespace votca {
41namespace xtp {
42
43class PolarRegion;
44class StaticRegion;
45class EwaldRegion;
46class QMRegion : public Region {
47
48 public:
49 QMRegion(Index id, Logger& log, std::string workdir)
50 : Region(id, log), workdir_(workdir) {};
51 ~QMRegion() override = default;
52
53 void Initialize(const tools::Property& prop) override;
54
55 bool Converged() const override;
56
57 void Evaluate(std::vector<std::unique_ptr<Region> >& regions) override;
58
59 void WriteToCpt(CheckpointWriter& w) const override;
60
61 void ReadFromCpt(CheckpointReader& r) override;
62
63 void ApplyQMFieldToPolarSegments(std::vector<PolarSegment>& segments) const;
64
65 // Builds the integration grid the background's potential is sampled on.
66 // Called lazily by InteractwithEwaldRegion rather than from Initialize,
67 // for two reasons. A job with no EwaldRegion must not pay for a grid it
68 // never uses -- and, less obviously, must not have ewald_grid_ready_ set,
69 // because Evaluate takes that flag as the signal to reject any qmpackage
70 // other than xtp. Building it eagerly would break every existing
71 // qmmm job that runs orca.
72 //
73 // Takes no options: the grid name and basis come from dftoptions_, which
74 // Initialize has already stored. They MUST be the ones DFTEngine uses --
75 // see the comment in the definition.
77
78 Index size() const override { return size_; }
79
80 void WritePDB(csg::PDBWriter& writer) const override;
81
82 std::string identify() const override { return "qmregion"; }
83
84 void push_back(const QMMolecule& mol);
85
86 void Reset() override;
87
88 double charge() const override;
89 double Etotal() const override { return E_hist_.back(); }
90
91 // Coordinates only, by value. That is not a convenience -- see
92 // ewaldgrid_ below for why the grid object itself must not leave this
93 // class.
94 std::vector<Eigen::Vector3d> copyEwaldGrid();
95
110 void EvaluateScreenedGWBSE(std::vector<std::unique_ptr<Region> >& regions);
111 // After EvaluateScreenedGWBSE: adds <screened_site_energies> (h, e and
112 // s or t, in eV, relative to the ground state) for -j read. No-op
113 // otherwise.
115 // Builds the reaction field for the job's polar regions and throws, with
116 // a diagnosis, unless 1 + R is positive definite. Needs no ground state:
117 // call it before the QM/MM loop. No-op without environment_screening.
119 const std::vector<std::unique_ptr<Region> >& regions) const;
120
121 protected:
122 void AppendResult(tools::Property& prop) const override;
123 double InteractwithQMRegion(const QMRegion& region) override;
124 double InteractwithPolarRegion(const PolarRegion& region) override;
125 double InteractwithStaticRegion(const StaticRegion& region) override;
126 double InteractwithEwaldRegion(const EwaldRegion& region) override;
127
128 private:
129 void AddNucleiFields(std::vector<PolarSegment>& segments,
130 const StaticSegment& seg) const;
132 const std::vector<std::unique_ptr<Region> >& regions) const;
133 std::string ScreeningAuxBasisName() const;
134
137
139 std::string workdir_ = "";
140 std::unique_ptr<QMPackage> qmpackage_ = nullptr;
141 // RI integrals and Ewald potential matrix kept across the inter-region
142 // iterations (DFT-only regions)
143 std::shared_ptr<DFTSetupCache> setup_cache_;
144
146
149 // S^1/2 of the DFT basis, for measuring density changes in the
150 // Loewdin-orthonormalised basis (empty: AO basis)
151 Eigen::MatrixXd overlap_sqrt_;
152 void UpdateOverlapSqrt();
153
154 // convergence options
155 double DeltaD_ = 5e-5;
156 double DeltaE_ = 5e-5;
157 double DeltaDmax_ = 1e-3;
158
159 bool do_gwbse_ = false;
160
161 // environment screening (embedded GW-BSE)
162 bool screening_ = false;
164 std::vector<Index> screening_shell_regions_;
167 bool screened_done_ = false;
169 // DFT-in-DFT rewrite of orb_ and closed-shell check, before any GW-BSE
171 // state energy of the tracked state, added to the DFT total energy
172 double StateEnergy(const QMState& state) const;
173 bool do_localize_ = false;
174 bool do_dft_in_dft_ = false;
175
179
181
182 // for QMEwald. The periodic background reaches the Hamiltonian as a
183 // potential sampled on this grid, which the DFT engine integrates
184 // against the density.
185 //
186 // There used to be a second route, handing the engine multipole
187 // moments and k-vectors to build its own AO matrices from, gated by a
188 // second flag. Nothing ever set it up -- the legacy jobcalculator that
189 // once did was removed -- so it has been deleted along with the
190 // machinery behind it (DFTEngine's IntegrateEwald* helpers, the
191 // AOEwald* matrices, the ewaldcontainer types). Two things are worth
192 // recording about that, since the decision was to remove code that had
193 // been deliberately kept:
194 //
195 // - It was blocked anyway. A rank-1 (induced dipole) source needs
196 // operator-centre derivatives from libint2, which is why
197 // AOEwaldRealSpaceDipoles was never instantiated even from the dead
198 // path -- the real-space route split every dipole into a pair of
199 // point charges instead.
200 // - It would not have been the faster route in any case. It replaces
201 // a loop over grid points (linear in QM size) with one over shell
202 // pairs (quadratic), so it wins only for small QM regions -- the
203 // opposite of what it was being kept for.
204 //
205 // Recoverable from the git history if either of those ever changes.
206 //
207 // ONLY ITS POINTS AND VALUES ARE VALID. PrepareEwaldPotentialGrid
208 // builds this grid against a BasisSet, an AOBasis and a QMMolecule
209 // that are all locals of that function, and GridBox::
210 // FindSignificantShells stores raw `const AOShell*` into the basis it
211 // is handed (gridbox.cc, addShell(&store)). Those shells die with the
212 // function, so from the moment PrepareEwaldPotentialGrid returns this
213 // object holds dangling pointers.
214 //
215 // What remains safe is everything that does not follow them:
216 // getGridpoints, getPotentialValues, getBoxesSize, GridBox::size.
217 // CalcAOValues, Matrixsize, AddtoBigMatrix and anything else touching
218 // significant_shells is undefined behaviour. That is why the grid is
219 // never integrated on here or in QMPackage, and why DFTEngine rebuilds
220 // its own from grid_name_ and copies only the values across -- see
221 // dftengine.cc.
222 //
223 // Nothing dereferences them today. The public Vxc_Grid& accessor that
224 // used to sit next to copyEwaldGrid() was removed because it handed
225 // this object out with no way to know that, and had no callers.
226 // Giving the basis a longer life would remove the hazard, but it buys
227 // nothing on its own: the transport is by value either way, and
228 // DFTEngine has to integrate against the AO ordering of its own
229 // dftbasis_ regardless.
231 bool ewald_grid_ready_ = false;
232 // Whether the background's potential has already been laid down on that
233 // grid. Separate from ewald_grid_ready_ because the grid is built once
234 // and the potential is evaluated once, but for different reasons: the
235 // grid because geometry and basis are fixed, the potential because the
236 // background is frozen. See InteractwithEwaldRegion.
238 // sum_A Z_A phi(R_A). The grid carries the potential the ELECTRONS
239 // feel; the nuclei sit in the same potential and have no other way in.
241};
242
243} // namespace xtp
244} // namespace votca
245
246#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:76
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:719
StateTracker statetracker_
Definition qmregion.h:180
hist< Eigen::MatrixXd > Dmat_hist_
Definition qmregion.h:148
void PrepareEwaldPotentialGrid()
Definition qmregion.cc:383
std::string grid_accuracy_for_ext_interaction_
Definition qmregion.h:145
std::string identify() const override
Definition qmregion.h:82
double Etotal() const override
Definition qmregion.h:89
hist< double > E_hist_
Definition qmregion.h:147
void AddNucleiFields(std::vector< PolarSegment > &segments, const StaticSegment &seg) const
Definition qmregion.cc:761
std::shared_ptr< DFTSetupCache > setup_cache_
Definition qmregion.h:143
std::unique_ptr< QMPackage > qmpackage_
Definition qmregion.h:140
bool screening_include_kreac_
Definition qmregion.h:163
void push_back(const QMMolecule &mol)
Definition qmregion.cc:678
void WriteToCpt(CheckpointWriter &w) const override
Definition qmregion.cc:814
double InteractwithStaticRegion(const StaticRegion &region) override
Definition qmregion.cc:752
void EvaluateScreenedGWBSE(std::vector< std::unique_ptr< Region > > &regions)
Definition qmregion.cc:311
void AppendScreenedSiteEnergies(tools::Property &prop) const
Definition qmregion.cc:344
std::string ScreeningAuxBasisName() const
Definition qmregion.cc:258
double ewald_nuclear_energy_
Definition qmregion.h:240
double InteractwithPolarRegion(const PolarRegion &region) override
Definition qmregion.cc:748
std::string workdir_
Definition qmregion.h:139
void Reset() override
Definition qmregion.cc:727
QMState screened_state_
Definition qmregion.h:168
QMRegion(Index id, Logger &log, std::string workdir)
Definition qmregion.h:49
double StateEnergy(const QMState &state) const
Definition qmregion.cc:190
Index size() const override
Definition qmregion.h:78
void WritePDB(csg::PDBWriter &writer) const override
Definition qmregion.cc:757
double InteractwithEwaldRegion(const EwaldRegion &region) override
Definition qmregion.cc:859
void ReadFromCpt(CheckpointReader &r) override
Definition qmregion.cc:836
ScreeningEnvironment BuildScreeningEnvironment(const std::vector< std::unique_ptr< Region > > &regions) const
Definition qmregion.cc:202
bool Converged() const override
Definition qmregion.cc:422
void Evaluate(std::vector< std::unique_ptr< Region > > &regions) override
Definition qmregion.cc:461
tools::Property localize_options_
Definition qmregion.h:178
std::vector< Eigen::Vector3d > copyEwaldGrid()
Definition qmregion.cc:374
void PrepareOrbitalsForGWBSE()
Definition qmregion.cc:164
double InteractwithQMRegion(const QMRegion &region) override
Definition qmregion.cc:743
void ApplyQMFieldToPolarSegments(std::vector< PolarSegment > &segments) const
Definition qmregion.cc:770
bool ewald_potential_evaluated_
Definition qmregion.h:237
Eigen::MatrixXd overlap_sqrt_
Definition qmregion.h:151
double screening_shell_dielectric_
Definition qmregion.h:165
~QMRegion() override=default
bool EnvironmentScreeningEnabled() const
Embedded GW-BSE, screened by the polar regions (see EnvironmentScreening, GWBSE::setScreeningEnvironm...
Definition qmregion.h:109
void Initialize(const tools::Property &prop) override
Definition qmregion.cc:45
std::vector< Index > screening_shell_regions_
Definition qmregion.h:164
double charge() const override
Definition qmregion.cc:687
double screening_site_width_
Definition qmregion.h:166
void CheckScreeningEnvironment(const std::vector< std::unique_ptr< Region > > &regions) const
Definition qmregion.cc:272
tools::Property dftoptions_
Definition qmregion.h:176
tools::Property gwbseoptions_
Definition qmregion.h:177
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
The polarizable environment a GW-BSE calculation is screened by: what a QM/MM job hands to GWBSE (GWB...