votca 2026-dev
Loading...
Searching...
No Matches
dftengine.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_DFTENGINE_H
22#define VOTCA_XTP_DFTENGINE_H
23
24// Standard includes
25#include <map>
26
27// VOTCA includes
29
30// Local VOTCA includes
31#include "ERIs.h"
32#include "convergenceacc.h"
33#include "hirshfeldpartition.h"
34#include "uks_convergenceacc.h"
35
36#include "ecpaobasis.h"
37#include "extended_hueckel.h"
38#include "logger.h"
39#include "qmmolecule.h"
40#include "staticsite.h"
41#include "vxc_grid.h"
42#include "vxc_potential.h"
43namespace votca {
44namespace xtp {
45class Orbitals;
46class DFTEngineTestAccess;
47
62
72
73class DFTEngine {
74 public:
76 void Initialize(tools::Property& options);
77
79 void setLogger(Logger* pLog) { pLog_ = pLog; }
80
84 std::vector<std::unique_ptr<StaticSite> >* externalsites) {
85 externalsites_ = externalsites;
86 }
87
90 bool Evaluate(Orbitals& orb);
91
122 bool RunCDFT(Orbitals& orb, HirshfeldPartition::Constraint& constraint);
123
130
132 std::string getDFTBasisName() const { return dftbasis_name_; };
133
136 struct SpinDensity {
137 Eigen::MatrixXd alpha;
138 Eigen::MatrixXd beta;
139
141 Eigen::MatrixXd total() const { return alpha + beta; }
142
144 Eigen::MatrixXd spin() const { return alpha - beta; }
145 };
146
152
158
162
166
167 // development path RKS vs UKS
168 void setForceUKSPath(bool force) { force_uks_path_ = force; }
169
170 private:
172
175 void Prepare(Orbitals& orb, Index numofelectrons = -1);
176
180
182 Eigen::MatrixXd OrthogonalizeGuess(const Eigen::MatrixXd& GuessMOs) const;
184 void PrintMOs(const Eigen::VectorXd& MOEnergies, Log::Level level);
186 void PrintMOsUKS(const Eigen::VectorXd& alpha_energies,
187 const Eigen::VectorXd& beta_energies,
188 Log::Level level) const;
190 void CalcElDipole(const Orbitals& orb) const;
191
194 std::array<Eigen::MatrixXd, 2> CalcERIs_EXX(const Eigen::MatrixXd& MOCoeff,
195 const Eigen::MatrixXd& Dmat,
196 double error) const;
197
199 Eigen::MatrixXd CalcERIs(const Eigen::MatrixXd& Dmat, double error) const;
200
202 void ConfigOrbfile(Orbitals& orb);
207 Eigen::MatrixXd McWeenyPurification(Eigen::MatrixXd& Dmat_in,
208 AOOverlap& overlap);
209
211 Mat_p_Energy SetupH0(const QMMolecule& mol) const;
215 const QMMolecule& mol,
216 const std::vector<std::unique_ptr<StaticSite> >& multipoles) const;
220 const Orbitals& extdensity) const;
221
223 Eigen::MatrixXd IntegrateExternalField(const QMMolecule& mol) const;
224
230 const Mat_p_Energy& H0, const QMMolecule& mol,
231 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
232
234 Eigen::MatrixXd AtomicGuess(const QMMolecule& mol) const;
235
237 Eigen::VectorXd BuildEHTOrbitalEnergies(const QMMolecule& mol) const;
239 Eigen::MatrixXd BuildEHTHamiltonian(const QMMolecule& mol) const;
246 const Mat_p_Energy& H0, const QMMolecule& mol,
247 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
248
271
295 Eigen::MatrixXd RunAtomicDFT_unrestricted(
296 const QMAtom& uniqueAtom, bool use_hunds_rule_occupation = false) const;
297
312 std::map<std::string, Eigen::MatrixXd> ComputeHirshfeldReferenceDensities(
313 const QMMolecule& mol) const;
314
330 std::vector<Index> atom_indices;
331 // Relative to the fragment's own neutral reference state (the sum
332 // of its atoms' nuclear charges) -- e.g. +1.0 means one electron
333 // REMOVED (a cation). Converted to an absolute target electron
334 // count once, inside BuildCDFTConstraint, matching CP2K's own
335 // internal (absolute) TARGET convention exactly -- only the
336 // user-facing options syntax is charge-relative, per the earlier
337 // design discussion on this.
338 double target_charge = 0.0;
339 double initial_lambda = 0.0;
340 };
341
358 const QMMolecule& mol, const CDFTConstraintSpec& spec) const;
359
361 double NuclearRepulsion(const QMMolecule& mol) const;
364 double ExternalRepulsion(
365 const QMMolecule& mol,
366 const std::vector<std::unique_ptr<StaticSite> >& multipoles) const;
369 Eigen::MatrixXd SphericalAverageShells(const Eigen::MatrixXd& dmat,
370 const AOBasis& dftbasis) const;
371
374 void TruncateBasis(Orbitals& orb, std::vector<Index>& activeatoms,
375 Mat_p_Energy& H0,
376 Eigen::MatrixXd InitialActiveDensityMatrix,
377 Eigen::MatrixXd v_embedding,
378 Eigen::MatrixXd InitialInactiveMOs);
379
382 void TruncMOsFullBasis(Orbitals& orb, std::vector<Index> activeatoms,
383 std::vector<Index> numfuncpatom);
386 Eigen::MatrixXd InsertZeroCols(Eigen::MatrixXd MOsMatrix, Index startidx,
387 Index numofzerocols);
389 Eigen::MatrixXd InsertZeroRows(Eigen::MatrixXd MOsMatrix, Index startidx,
390 Index numofzerorows);
391
393 bool EvaluateClosedShell(Orbitals& orb, const Mat_p_Energy& H0,
394 const Vxc_Potential<Vxc_Grid>& vxcpotential);
395
398 bool EvaluateUKS(Orbitals& orb, const Mat_p_Energy& H0,
399 const Vxc_Potential<Vxc_Grid>& vxcpotential);
400
450 void ComputeAndStoreForces(Orbitals& orb, const Eigen::MatrixXd& Dmat,
451 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
452
464 Eigen::MatrixXd ComputeOverlapPulayGradientUKS(
465 const QMMolecule& mol, const tools::EigenSystem& MOs_alpha,
466 const tools::EigenSystem& MOs_beta) const;
467
476 Eigen::MatrixXd ComputeNonXCGradientUKS(
477 const QMMolecule& mol, const UKSConvergenceAcc::SpinDensity& Dspin,
478 const tools::EigenSystem& MOs_alpha,
479 const tools::EigenSystem& MOs_beta) const;
480
513 Orbitals& orb, const UKSConvergenceAcc::SpinDensity& Dspin,
514 const tools::EigenSystem& MOs_alpha, const tools::EigenSystem& MOs_beta,
515 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
516
518
519 // basis sets
520 std::string auxbasis_name_;
521 std::string dftbasis_name_;
522 std::string ecp_name_;
526
528 // Pre-screening
530
531 // numerical integration Vxc
532 std::string grid_name_;
533
534 // AO Matrices
536
537 std::string initial_guess_;
538
539 // Only read from options / actually used when initial_guess_ ==
540 // "dimer_guess" -- see BuildDimerGuessFromMonomerFiles's own header
541 // comment for what this guess does and why it exists.
544
545 // Convergence
549 // DIIS variables
551 // Electron repulsion integrals
553
554 // external charges
555 std::vector<std::unique_ptr<StaticSite> >* externalsites_ = nullptr;
556
557 // exchange and correlation
558 double ScaHFX_;
560
562 // integrate external density
563 std::string orbfilename_;
564 std::string gridquality_;
565 std::string state_;
566
568 QMMolecule("molecule made of atoms participating in Active region", 1);
569
570 Eigen::Vector3d extfield_ = Eigen::Vector3d::Zero();
572
576
577 // truncation
578 Eigen::MatrixXd H0_trunc_;
580 Eigen::MatrixXd v_embedding_trunc_;
584 double E_nuc_;
586 std::vector<Index> active_and_border_atoms_;
587 std::vector<Index> numfuncpatom_;
588
589 // Spin-DFT Extension
596 bool force_uks_path_ = false;
597 // Default false to preserve existing performance for callers not
598 // using this feature -- computing forces adds real, non-trivial cost
599 // (kinetic/nuclear-attraction/overlap derivatives, RI-J gradient, full
600 // XC gradient, RI-K for hybrids) to every converged SCF, so this must
601 // be explicit opt-in, not silently always-on. Settable via the
602 // <xtpdft> options block, which flows through unmodified from
603 // XTPDFT::RunDFT() (options_) straight into DFTEngine::Initialize --
604 // confirmed directly by reading XTPDFT::ParseSpecificOptions, which
605 // only extracts a single unrelated field (temporary_file) and does
606 // not filter/transform anything else -- so this option is
607 // automatically available through the full QMPackage/XTPDFT flow with
608 // no changes needed there.
609 bool compute_forces_ = false;
610
611 // Empty by default -- the ONLY thing a standard, non-CDFT run needs
612 // to know about this member is that it is empty, checked via a
613 // single, cheap constraints_.empty() guard inside
614 // EvaluateUKS's own Fock-matrix assembly (see that function's own
615 // comment at the point the constraint potential term is added).
616 // When empty, that guard means the added term is a complete no-op:
617 // the Hamiltonian is built exactly as it always was, with no
618 // measurable overhead and no change in behavior whatsoever for any
619 // run that never touches this member. Populated only by the
620 // (not yet implemented) outer Lagrange-multiplier optimization loop,
621 // which is expected to modify each Constraint's own lambda field in
622 // place between successive, warm-started calls into EvaluateUKS --
623 // per the design discussion this grew out of (CP2K's own documented
624 // approach: restart the inner SCF from the previous trial's
625 // converged density at each new lambda, rather than a cold start).
626 std::vector<HirshfeldPartition::Constraint> constraints_;
627
628 // CDFT outer-loop (Lagrange-multiplier) control, used only by
629 // RunCDFT below -- never read by the ordinary Evaluate/EvaluateUKS
630 // path at all, so these have no bearing on any standard run either.
631 // max_cdft_iterations_/cdft_population_tolerance_ are also settable
632 // from options (see Initialize()'s own cdft.max_iterations/
633 // cdft.population_tolerance parsing) when cdft.enabled=true; their
634 // defaults here are what a directly-constructed RunCDFT call (e.g.
635 // from a test, bypassing Initialize() entirely) gets instead.
638
639 bool cdft_enabled_ = false;
641};
642
643} // namespace xtp
644} // namespace votca
645
646#endif // VOTCA_XTP_DFTENGINE_H
class to manage program options with xml serialization functionality
Definition property.h:55
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Electronic ground-state via Density-Functional Theory.
Definition dftengine.h:73
Eigen::MatrixXd CalcERIs(const Eigen::MatrixXd &Dmat, double error) const
Build the Coulomb matrix contribution from the current density matrix.
Definition dftengine.cc:836
Eigen::MatrixXd ComputeOverlapPulayGradientUKS(const QMMolecule &mol, const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta) const
Definition dftengine.cc:613
void setExternalcharges(std::vector< std::unique_ptr< StaticSite > > *externalsites)
Definition dftengine.h:83
bool EvaluateTruncatedActiveRegion(Orbitals &trunc_orb)
std::string auxbasis_name_
Definition dftengine.h:520
std::string getDFTBasisName() const
Return the configured AO basis-set name for the DFT calculation.
Definition dftengine.h:132
std::string gridquality_
Definition dftengine.h:564
tools::EigenSystem ModelPotentialGuess(const Mat_p_Energy &H0, const QMMolecule &mol, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
Definition dftengine.cc:856
Mat_p_Energy IntegrateExternalMultipoles(const QMMolecule &mol, const std::vector< std::unique_ptr< StaticSite > > &multipoles) const
friend class DFTEngineTestAccess
Definition dftengine.h:171
tools::EigenSystem IndependentElectronGuess(const Mat_p_Energy &H0) const
Generate an initial guess by diagonalizing the core Hamiltonian only.
Definition dftengine.cc:845
Eigen::MatrixXd InitialActiveDmat_trunc_
Definition dftengine.h:579
void TruncMOsFullBasis(Orbitals &orb, std::vector< Index > activeatoms, std::vector< Index > numfuncpatom)
Orbitals BuildDimerGuessFromMonomerFiles(const QMMolecule &dimer_mol) const
double cdft_population_tolerance_
Definition dftengine.h:637
void ComputeAndStoreForces(Orbitals &orb, const Eigen::MatrixXd &Dmat, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
Definition dftengine.cc:421
bool EvaluateClosedShell(Orbitals &orb, const Mat_p_Energy &H0, const Vxc_Potential< Vxc_Grid > &vxcpotential)
Run the restricted closed-shell SCF loop and store the converged result.
void setLogger(Logger *pLog)
Attach the logger used for SCF progress and diagnostics.
Definition dftengine.h:79
void TruncateBasis(Orbitals &orb, std::vector< Index > &activeatoms, Mat_p_Energy &H0, Eigen::MatrixXd InitialActiveDensityMatrix, Eigen::MatrixXd v_embedding, Eigen::MatrixXd InitialInactiveMOs)
void PrintMOsUKS(const Eigen::VectorXd &alpha_energies, const Eigen::VectorXd &beta_energies, Log::Level level) const
Print separate alpha and beta orbital energies for a UKS calculation.
Definition dftengine.cc:323
std::string dftbasis_name_
Definition dftengine.h:521
bool EvaluateUKS(Orbitals &orb, const Mat_p_Energy &H0, const Vxc_Potential< Vxc_Grid > &vxcpotential)
Eigen::MatrixXd RunAtomicDFT_unrestricted(const QMAtom &uniqueAtom, bool use_hunds_rule_occupation=false) const
Mat_p_Energy IntegrateExternalDensity(const QMMolecule &mol, const Orbitals &extdensity) const
std::string ecp_name_
Definition dftengine.h:522
std::string grid_name_
Definition dftengine.h:532
bool EvaluateActiveRegion(Orbitals &orb)
void Prepare(Orbitals &orb, Index numofelectrons=-1)
std::string initial_guess_
Definition dftengine.h:537
std::string orbfilename_
Definition dftengine.h:563
Eigen::MatrixXd AtomicGuess(const QMMolecule &mol) const
Build an atomic-density based initial guess in the AO basis.
Eigen::MatrixXd InsertZeroRows(Eigen::MatrixXd MOsMatrix, Index startidx, Index numofzerorows)
Insert zero rows into an MO coefficient matrix at the requested position.
Eigen::MatrixXd BuildEHTHamiltonian(const QMMolecule &mol) const
Build the extended-Hückel Hamiltonian for the current molecule.
Eigen::MatrixXd IntegrateExternalField(const QMMolecule &mol) const
Integrate a homogeneous external electric field into the AO basis.
Eigen::Vector3d extfield_
Definition dftengine.h:570
std::array< Eigen::MatrixXd, 2 > CalcERIs_EXX(const Eigen::MatrixXd &MOCoeff, const Eigen::MatrixXd &Dmat, double error) const
Definition dftengine.cc:819
Eigen::MatrixXd H0_trunc_
Definition dftengine.h:578
Index NumberOfRestrictedOccupiedOrbitals() const
Definition dftengine.h:155
HirshfeldPartition::Constraint BuildCDFTConstraint(const QMMolecule &mol, const CDFTConstraintSpec &spec) const
void Initialize(tools::Property &options)
Read DFT, grid, and SCF settings from the user options tree.
Definition dftengine.cc:100
std::string xc_functional_name_
Definition dftengine.h:559
void CalcElDipole(const Orbitals &orb) const
Evaluate and print the electronic dipole moment from the final density.
Definition dftengine.cc:383
double ExternalRepulsion(const QMMolecule &mol, const std::vector< std::unique_ptr< StaticSite > > &multipoles) const
ConvergenceAcc::options conv_opt_
Definition dftengine.h:548
void SetupInvariantMatrices()
Precompute AO matrices that remain unchanged throughout the SCF procedure.
tools::EigenSystem ExtendedHuckelDFTGuess(const Mat_p_Energy &H0, const QMMolecule &mol, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
std::map< std::string, Eigen::MatrixXd > ComputeHirshfeldReferenceDensities(const QMMolecule &mol) const
std::string active_atoms_as_string_
Definition dftengine.h:573
void ComputeAndStoreForcesUKS(Orbitals &orb, const UKSConvergenceAcc::SpinDensity &Dspin, const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
Definition dftengine.cc:714
void ConfigOrbfile(Orbitals &orb)
Propagate basis-set, XC, and metadata settings into the orbital container.
Eigen::VectorXd BuildEHTOrbitalEnergies(const QMMolecule &mol) const
Build orbital energies used in the extended-Hückel starting guess.
std::vector< HirshfeldPartition::Constraint > constraints_
Definition dftengine.h:626
QMMolecule activemol_
Definition dftengine.h:567
void PrintMOs(const Eigen::VectorXd &MOEnergies, Log::Level level)
Print a one-spin list of orbital energies and occupations to the logger.
Definition dftengine.cc:303
void setForceUKSPath(bool force)
Definition dftengine.h:168
std::vector< Index > numfuncpatom_
Definition dftengine.h:587
AOOverlap dftAOoverlap_
Definition dftengine.h:535
std::vector< Index > active_and_border_atoms_
Definition dftengine.h:586
Mat_p_Energy SetupH0(const QMMolecule &mol) const
Assemble the one-electron core Hamiltonian for the current molecule.
tools::EigenSystem ExtendedHuckelGuess(const QMMolecule &mol) const
bool IsRestrictedOpenShell() const
Definition dftengine.h:149
Eigen::MatrixXd OrthogonalizeGuess(const Eigen::MatrixXd &GuessMOs) const
Orthonormalize an initial MO guess with respect to the AO overlap matrix.
Eigen::MatrixXd v_embedding_trunc_
Definition dftengine.h:580
Eigen::MatrixXd SphericalAverageShells(const Eigen::MatrixXd &dmat, const AOBasis &dftbasis) const
ConvergenceAcc::options BuildConvergenceOptions() const
bool Evaluate(Orbitals &orb)
Definition dftengine.cc:877
CDFTConstraintSpec cdft_constraint_spec_
Definition dftengine.h:640
Eigen::MatrixXd ComputeNonXCGradientUKS(const QMMolecule &mol, const UKSConvergenceAcc::SpinDensity &Dspin, const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta) const
Definition dftengine.cc:661
bool RunCDFT(Orbitals &orb, HirshfeldPartition::Constraint &constraint)
Definition dftengine.cc:971
std::string dimer_guess_orbB_name_
Definition dftengine.h:543
SpinDensity BuildSpinDensity(const tools::EigenSystem &MOs) const
std::vector< std::unique_ptr< StaticSite > > * externalsites_
Definition dftengine.h:555
Eigen::MatrixXd McWeenyPurification(Eigen::MatrixXd &Dmat_in, AOOverlap &overlap)
Vxc_Potential< Vxc_Grid > SetupVxc(const QMMolecule &mol)
double NuclearRepulsion(const QMMolecule &mol) const
Compute the classical nucleus-nucleus repulsion energy.
ConvergenceAcc conv_accelerator_
Definition dftengine.h:550
Eigen::MatrixXd InsertZeroCols(Eigen::MatrixXd MOsMatrix, Index startidx, Index numofzerocols)
std::string dimer_guess_orbA_name_
Definition dftengine.h:542
Container to hold ECPs for all atoms.
Definition ecpaobasis.h:43
Takes a density matrix and and an auxiliary basis set and calculates the electron repulsion integrals...
Definition ERIs.h:35
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
container for QM atoms
Definition qmatom.h:37
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
Level
be loud and noisy
Definition globals.h:28
Eigen::MatrixXd spin() const
Return the spin density P^alpha - P^beta.
Definition dftengine.h:144
Eigen::MatrixXd total() const
Return the total density P = P^alpha + P^beta.
Definition dftengine.h:141