votca 2026-dev
Loading...
Searching...
No Matches
convergenceacc.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 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_CONVERGENCEACC_H
22#define VOTCA_XTP_CONVERGENCEACC_H
23
24// VOTCA includes
25#include <votca/tools/linalg.h>
26
27// Local VOTCA includes
28#include "adiis.h"
29#include "aomatrix.h"
30#include "diis.h"
31#include "logger.h"
32
33namespace votca {
34namespace xtp {
35
44 public:
47
50 struct options {
52 bool usediis;
53 bool noisy = false;
55 bool maxout;
57 double diis_start;
58 double levelshift;
60 // Independent from adiis_start deliberately -- see
61 // UKSConvergenceAcc::Iterate's own comment on where this is used
62 // for the full reasoning: ORCA's own DampErr is kept fully
63 // separate from its own DIISStart, and their own guidance for
64 // difficult systems is to make DampErr much SMALLER than default
65 // (keeping damping active LONGER), independent of when DIIS
66 // itself engages. Reusing adiis_start for both purposes could not
67 // represent that independently.
68 double mixingend;
71 // Ceiling mixingparameter can adaptively ramp up toward as the SCF
72 // struggles, rather than staying fixed at mixingparameter (the
73 // BASE/starting value) for the whole run -- matches ORCA's own
74 // static-damping design directly (confirmed from a real ORCA log's
75 // own resolved SCF settings, not the manual's generic defaults):
76 // DampFac (the base, 0.7 by default) and DampMax (the ceiling, 0.98
77 // by default) are two separate parameters there, not one fixed
78 // value. Notably, 0.98 is exactly the value this session's own
79 // hand-tuning independently landed on for a difficult water dimer
80 // case -- this adaptive design is a more principled way to obtain
81 // that same benefit only when actually needed, rather than paying
82 // its cost (slower convergence on easy iterations) for an entire
83 // run regardless of whether the system is struggling at all.
84 double mixingmax;
85 double Econverged;
89 };
90
92 struct SpinDensity {
93 Eigen::MatrixXd alpha;
94 Eigen::MatrixXd beta;
95
97 Eigen::MatrixXd total() const { return alpha + beta; }
98
100 Eigen::MatrixXd spin() const { return alpha - beta; }
101 };
102
106 opt_ = opt;
107 if (opt_.mode == KSmode::closed) {
108 nocclevels_ = opt_.numberofelectrons / 2;
109 } else if (opt_.mode == KSmode::open) {
110 nocclevels_ = opt_.numberofelectrons;
111 } else if (opt_.mode == KSmode::fractional) {
112 nocclevels_ = 0;
113 } else if (opt_.mode == KSmode::restricted_open) {
115 std::max(opt_.number_alpha_electrons, opt_.number_beta_electrons);
116 }
117 diis_.setHistLength(opt_.histlength);
118 }
119
120 void setLogger(Logger* log) { log_ = log; }
121
123 void PrintConfigOptions() const;
124
127 bool isConverged() const {
128 if (totE_.size() < 2) {
129 return false;
130 } else {
131 return std::abs(getDeltaE()) < opt_.Econverged &&
132 getDIIsError() < opt_.error_converged;
133 }
134 }
135
137 double getDeltaE() const {
138 if (totE_.size() < 2) {
139 return 0;
140 } else {
141 return totE_.back() - totE_[totE_.size() - 2];
142 }
143 }
144
145 void setOverlap(AOOverlap& S, double etol);
146
148 double getDIIsError() const { return diiserror_; }
149
152 bool getUseMixing() const { return usedmixing_; }
153
156 Eigen::MatrixXd Iterate(const Eigen::MatrixXd& dmat, Eigen::MatrixXd& H,
157 tools::EigenSystem& MOs, double totE);
159 tools::EigenSystem SolveFockmatrix(const Eigen::MatrixXd& H) const;
161 void Levelshift(Eigen::MatrixXd& H, const Eigen::MatrixXd& MOs_old) const;
162
165 Eigen::MatrixXd DensityMatrix(const tools::EigenSystem& MOs) const;
166
169 SpinDensity DensityMatrixSpinResolved(const tools::EigenSystem& MOs) const;
170
171 private:
173
176 Eigen::MatrixXd DensityMatrixGroundState(const Eigen::MatrixXd& MOs) const;
179 Eigen::MatrixXd DensityMatrixGroundState_unres(
180 const Eigen::MatrixXd& MOs) const;
182 Eigen::MatrixXd DensityMatrixGroundState_frac(
183 const tools::EigenSystem& MOs) const;
184
188 const Eigen::MatrixXd& MOs) const;
189
190 bool usedmixing_ = true;
191 double diiserror_ = std::numeric_limits<double>::max();
193 const AOOverlap* S_;
194
195 Eigen::MatrixXd Sminusahalf;
196 std::vector<Eigen::MatrixXd> mathist_;
197 std::vector<Eigen::MatrixXd> dmatHist_;
198 std::vector<double> totE_;
199
202 double maxerror_ = 0.0;
205};
206
207} // namespace xtp
208} // namespace votca
209
210#endif // VOTCA_XTP_CONVERGENCEACC_H
Eigen::MatrixXd DensityMatrix(const tools::EigenSystem &MOs) const
void Configure(const ConvergenceAcc::options &opt)
Eigen::MatrixXd DensityMatrixGroundState_unres(const Eigen::MatrixXd &MOs) const
std::vector< Eigen::MatrixXd > mathist_
void setLogger(Logger *log)
Attach the logger used for convergence diagnostics.
Eigen::MatrixXd Iterate(const Eigen::MatrixXd &dmat, Eigen::MatrixXd &H, tools::EigenSystem &MOs, double totE)
double getDIIsError() const
Return the DIIS commutator norm from the latest iteration.
tools::EigenSystem SolveFockmatrix(const Eigen::MatrixXd &H) const
Solve the generalized eigenvalue problem for the current Fock matrix.
void PrintConfigOptions() const
Print the active convergence-acceleration settings to the logger.
void Levelshift(Eigen::MatrixXd &H, const Eigen::MatrixXd &MOs_old) const
Apply a virtual-space level shift in the molecular-orbital basis.
double getDeltaE() const
Return the total-energy change between the two most recent SCF iterations.
Eigen::MatrixXd DensityMatrixGroundState_frac(const tools::EigenSystem &MOs) const
Construct a fractional-occupation density matrix from orbital occupations.
std::vector< double > totE_
void setOverlap(AOOverlap &S, double etol)
Precompute overlap-dependent quantities used when solving the Fock matrix.
Eigen::MatrixXd DensityMatrixGroundState(const Eigen::MatrixXd &MOs) const
std::vector< Eigen::MatrixXd > dmatHist_
KSmode
Occupation model used when constructing density matrices.
SpinDensity DensityMatrixSpinResolved(const tools::EigenSystem &MOs) const
SpinDensity DensityMatrixGroundState_restricted_open(const Eigen::MatrixXd &MOs) const
Logger is used for thread-safe output of messages.
Definition logger.h:164
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
Spin-resolved density matrices returned for open-shell SCF updates.
Eigen::MatrixXd total() const
Return the total density P = P^alpha + P^beta.
Eigen::MatrixXd spin() const
Return the spin density P^alpha - P^beta.