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 // "warmstart" (default) or "fresh" -- see this field's own XML
341 // help text (dftpackage.xml) for the full reasoning. Stored as
342 // the raw string, not a bool, so an invalid value (a typo, say)
343 // is caught by the XML schema's own choices="..." validation
344 // rather than silently defaulting to one behavior or the other.
345 std::string guess_strategy = "warmstart";
346 };
347
364 const QMMolecule& mol, const CDFTConstraintSpec& spec) const;
365
367 double NuclearRepulsion(const QMMolecule& mol) const;
370 double ExternalRepulsion(
371 const QMMolecule& mol,
372 const std::vector<std::unique_ptr<StaticSite> >& multipoles) const;
375 Eigen::MatrixXd SphericalAverageShells(const Eigen::MatrixXd& dmat,
376 const AOBasis& dftbasis) const;
377
380 void TruncateBasis(Orbitals& orb, std::vector<Index>& activeatoms,
381 Mat_p_Energy& H0,
382 Eigen::MatrixXd InitialActiveDensityMatrix,
383 Eigen::MatrixXd v_embedding,
384 Eigen::MatrixXd InitialInactiveMOs);
385
388 void TruncMOsFullBasis(Orbitals& orb, std::vector<Index> activeatoms,
389 std::vector<Index> numfuncpatom);
392 Eigen::MatrixXd InsertZeroCols(Eigen::MatrixXd MOsMatrix, Index startidx,
393 Index numofzerocols);
395 Eigen::MatrixXd InsertZeroRows(Eigen::MatrixXd MOsMatrix, Index startidx,
396 Index numofzerorows);
397
399 bool EvaluateClosedShell(Orbitals& orb, const Mat_p_Energy& H0,
400 const Vxc_Potential<Vxc_Grid>& vxcpotential);
401
404 bool EvaluateUKS(Orbitals& orb, const Mat_p_Energy& H0,
405 const Vxc_Potential<Vxc_Grid>& vxcpotential);
406
456 void ComputeAndStoreForces(Orbitals& orb, const Eigen::MatrixXd& Dmat,
457 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
458
470 Eigen::MatrixXd ComputeOverlapPulayGradientUKS(
471 const QMMolecule& mol, const tools::EigenSystem& MOs_alpha,
472 const tools::EigenSystem& MOs_beta) const;
473
482 Eigen::MatrixXd ComputeNonXCGradientUKS(
483 const QMMolecule& mol, const UKSConvergenceAcc::SpinDensity& Dspin,
484 const tools::EigenSystem& MOs_alpha,
485 const tools::EigenSystem& MOs_beta) const;
486
519 Orbitals& orb, const UKSConvergenceAcc::SpinDensity& Dspin,
520 const tools::EigenSystem& MOs_alpha, const tools::EigenSystem& MOs_beta,
521 const Vxc_Potential<Vxc_Grid>& vxcpotential) const;
522
524
525 // basis sets
526 std::string auxbasis_name_;
527 std::string dftbasis_name_;
528 std::string ecp_name_;
532
534 // Pre-screening
536
537 // numerical integration Vxc
538 std::string grid_name_;
539
540 // AO Matrices
542
543 std::string initial_guess_;
544
545 // Only read from options / actually used when initial_guess_ ==
546 // "dimer_guess" -- see BuildDimerGuessFromMonomerFiles's own header
547 // comment for what this guess does and why it exists.
550
551 // Convergence
555 // DIIS variables
557 // Electron repulsion integrals
559
560 // external charges
561 std::vector<std::unique_ptr<StaticSite> >* externalsites_ = nullptr;
562
563 // exchange and correlation
564 double ScaHFX_;
566
568 // integrate external density
569 std::string orbfilename_;
570 std::string gridquality_;
571 std::string state_;
572
574 QMMolecule("molecule made of atoms participating in Active region", 1);
575
576 Eigen::Vector3d extfield_ = Eigen::Vector3d::Zero();
578
582
583 // truncation
584 Eigen::MatrixXd H0_trunc_;
586 Eigen::MatrixXd v_embedding_trunc_;
590 double E_nuc_;
592 std::vector<Index> active_and_border_atoms_;
593 std::vector<Index> numfuncpatom_;
594
595 // Spin-DFT Extension
602 bool force_uks_path_ = false;
603 // Default false to preserve existing performance for callers not
604 // using this feature -- computing forces adds real, non-trivial cost
605 // (kinetic/nuclear-attraction/overlap derivatives, RI-J gradient, full
606 // XC gradient, RI-K for hybrids) to every converged SCF, so this must
607 // be explicit opt-in, not silently always-on. Settable via the
608 // <xtpdft> options block, which flows through unmodified from
609 // XTPDFT::RunDFT() (options_) straight into DFTEngine::Initialize --
610 // confirmed directly by reading XTPDFT::ParseSpecificOptions, which
611 // only extracts a single unrelated field (temporary_file) and does
612 // not filter/transform anything else -- so this option is
613 // automatically available through the full QMPackage/XTPDFT flow with
614 // no changes needed there.
615 bool compute_forces_ = false;
616
617 // Empty by default -- the ONLY thing a standard, non-CDFT run needs
618 // to know about this member is that it is empty, checked via a
619 // single, cheap constraints_.empty() guard inside
620 // EvaluateUKS's own Fock-matrix assembly (see that function's own
621 // comment at the point the constraint potential term is added).
622 // When empty, that guard means the added term is a complete no-op:
623 // the Hamiltonian is built exactly as it always was, with no
624 // measurable overhead and no change in behavior whatsoever for any
625 // run that never touches this member. Populated only by the
626 // (not yet implemented) outer Lagrange-multiplier optimization loop,
627 // which is expected to modify each Constraint's own lambda field in
628 // place between successive, warm-started calls into EvaluateUKS --
629 // per the design discussion this grew out of (CP2K's own documented
630 // approach: restart the inner SCF from the previous trial's
631 // converged density at each new lambda, rather than a cold start).
632 std::vector<HirshfeldPartition::Constraint> constraints_;
633
634 // CDFT outer-loop (Lagrange-multiplier) control, used only by
635 // RunCDFT below -- never read by the ordinary Evaluate/EvaluateUKS
636 // path at all, so these have no bearing on any standard run either.
637 // max_cdft_iterations_/cdft_population_tolerance_ are also settable
638 // from options (see Initialize()'s own cdft.max_iterations/
639 // cdft.population_tolerance parsing) when cdft.enabled=true; their
640 // defaults here are what a directly-constructed RunCDFT call (e.g.
641 // from a test, bypassing Initialize() entirely) gets instead.
644
645 bool cdft_enabled_ = false;
647};
648
649} // namespace xtp
650} // namespace votca
651
652#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:869
Eigen::MatrixXd ComputeOverlapPulayGradientUKS(const QMMolecule &mol, const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta) const
Definition dftengine.cc:646
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:526
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:570
tools::EigenSystem ModelPotentialGuess(const Mat_p_Energy &H0, const QMMolecule &mol, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
Definition dftengine.cc:889
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:878
Eigen::MatrixXd InitialActiveDmat_trunc_
Definition dftengine.h:585
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:643
void ComputeAndStoreForces(Orbitals &orb, const Eigen::MatrixXd &Dmat, const Vxc_Potential< Vxc_Grid > &vxcpotential) const
Definition dftengine.cc:425
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:327
std::string dftbasis_name_
Definition dftengine.h:527
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:528
std::string grid_name_
Definition dftengine.h:538
bool EvaluateActiveRegion(Orbitals &orb)
void Prepare(Orbitals &orb, Index numofelectrons=-1)
std::string initial_guess_
Definition dftengine.h:543
std::string orbfilename_
Definition dftengine.h:569
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:576
std::array< Eigen::MatrixXd, 2 > CalcERIs_EXX(const Eigen::MatrixXd &MOCoeff, const Eigen::MatrixXd &Dmat, double error) const
Definition dftengine.cc:852
Eigen::MatrixXd H0_trunc_
Definition dftengine.h:584
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:565
void CalcElDipole(const Orbitals &orb) const
Evaluate and print the electronic dipole moment from the final density.
Definition dftengine.cc:387
double ExternalRepulsion(const QMMolecule &mol, const std::vector< std::unique_ptr< StaticSite > > &multipoles) const
ConvergenceAcc::options conv_opt_
Definition dftengine.h:554
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:579
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:747
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:632
QMMolecule activemol_
Definition dftengine.h:573
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:307
void setForceUKSPath(bool force)
Definition dftengine.h:168
std::vector< Index > numfuncpatom_
Definition dftengine.h:593
AOOverlap dftAOoverlap_
Definition dftengine.h:541
std::vector< Index > active_and_border_atoms_
Definition dftengine.h:592
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:586
Eigen::MatrixXd SphericalAverageShells(const Eigen::MatrixXd &dmat, const AOBasis &dftbasis) const
ConvergenceAcc::options BuildConvergenceOptions() const
bool Evaluate(Orbitals &orb)
Definition dftengine.cc:910
CDFTConstraintSpec cdft_constraint_spec_
Definition dftengine.h:646
Eigen::MatrixXd ComputeNonXCGradientUKS(const QMMolecule &mol, const UKSConvergenceAcc::SpinDensity &Dspin, const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta) const
Definition dftengine.cc:694
bool RunCDFT(Orbitals &orb, HirshfeldPartition::Constraint &constraint)
std::string dimer_guess_orbB_name_
Definition dftengine.h:549
SpinDensity BuildSpinDensity(const tools::EigenSystem &MOs) const
std::vector< std::unique_ptr< StaticSite > > * externalsites_
Definition dftengine.h:561
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:556
Eigen::MatrixXd InsertZeroCols(Eigen::MatrixXd MOsMatrix, Index startidx, Index numofzerocols)
std::string dimer_guess_orbA_name_
Definition dftengine.h:548
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