votca 2026-dev
Loading...
Searching...
No Matches
uks_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
22#ifndef VOTCA_XTP_UKS_CONVERGENCEACC_H
23#define VOTCA_XTP_UKS_CONVERGENCEACC_H
24
25// Standard includes
26#include <functional>
27#include <utility>
28
29// VOTCA includes
30#include <votca/tools/linalg.h>
31
32#include "votca/xtp/adiis.h"
33#include "votca/xtp/aomatrix.h"
35#include "votca/xtp/diis.h"
36#include "votca/xtp/logger.h"
37#include "votca/xtp/trah.h"
38
39namespace votca {
40namespace xtp {
41
43 public:
46
47 struct SpinDensity {
48 Eigen::MatrixXd alpha;
49 Eigen::MatrixXd beta;
50
51 Eigen::MatrixXd total() const { return alpha + beta; }
52 };
53
54 struct SpinFock {
55 Eigen::MatrixXd alpha;
56 Eigen::MatrixXd beta;
57 };
58
67 std::function<SpinFock(const Eigen::MatrixXd& /*Dalpha_new*/,
68 const Eigen::MatrixXd& /*Dbeta_new*/)>;
69
70 void Configure(const options& opt_alpha, const options& opt_beta);
71 void setLogger(Logger* log);
72 void setOverlap(AOOverlap& S, double etol);
74 coupled_fock_builder_ = builder;
75 }
76
82 using OrbitalHessian = std::function<Eigen::MatrixXd(
83 const tools::EigenSystem& MOs_alpha, const tools::EigenSystem& MOs_beta,
84 const Eigen::MatrixXd& F_alpha, const Eigen::MatrixXd& F_beta,
85 const Eigen::MatrixXd& X)>;
86 void setOrbitalHessian(const OrbitalHessian& hessian) {
87 orbital_hessian_ = hessian;
88 }
89
90 SpinDensity DensityMatrix(const tools::EigenSystem& MOs_alpha,
91 const tools::EigenSystem& MOs_beta) const;
92
93 SpinDensity Iterate(const SpinDensity& dmat, SpinFock& H,
94 tools::EigenSystem& MOs_alpha,
95 tools::EigenSystem& MOs_beta, double totE);
96
97 tools::EigenSystem SolveFockmatrix(const Eigen::MatrixXd& H) const;
98
106 Eigen::MatrixXd UnflattenRotation(const Eigen::VectorXd& v_ov, Index nao,
107 Index nocclevels) const;
108
113 std::pair<Eigen::MatrixXd, Eigen::MatrixXd> UnflattenCoupledRotation(
114 const Eigen::VectorXd& v, Index nao_alpha, Index nocclevels_alpha,
115 Index nao_beta, Index nocclevels_beta) const;
116
124 static constexpr double EnergyGradientScale = -2.0;
125
131 Eigen::VectorXd BuildCoupledSigmaVector(
132 const Eigen::VectorXd& v, const Eigen::MatrixXd& C_alpha,
133 Index nocclevels_alpha, const Eigen::MatrixXd& C_beta,
134 Index nocclevels_beta, const CoupledFockBuilder& coupled_fock_builder,
135 double finite_diff_step = 1e-3) const;
136
143 std::pair<Eigen::MatrixXd, Eigen::MatrixXd> CoupledAugmentedHessianStep(
144 const Eigen::MatrixXd& H_AO_alpha, const tools::EigenSystem& MOs_alpha,
145 Index nocclevels_alpha, const Eigen::MatrixXd& H_AO_beta,
146 const tools::EigenSystem& MOs_beta, Index nocclevels_beta,
147 const CoupledFockBuilder& coupled_fock_builder, double trust_radius,
148 double& predicted_energy_change) const;
149
157 Eigen::MatrixXd DirectMinimizationRotation(
158 const Eigen::MatrixXd& H_AO, const tools::EigenSystem& MOs,
159 Index nocclevels, double& predicted_energy_change) const;
160
161 bool isConverged() const;
162 double getDIIsError() const { return diiserror_; }
163 double getDeltaE() const;
164 bool getUseMixing() const { return usedmixing_; }
165
166 private:
167 Eigen::MatrixXd DensityMatrixGroundState_unres(const Eigen::MatrixXd& MOs,
168 Index nocclevels) const;
169
170 void Levelshift(Eigen::MatrixXd& H, const Eigen::MatrixXd& MOs_old,
171 const options& opt, Index nocclevels) const;
172
173 Eigen::MatrixXd BuildErrorMatrix(const Eigen::MatrixXd& dmat,
174 const Eigen::MatrixXd& H) const;
175
176 double CombinedError(const Eigen::MatrixXd& err_alpha,
177 const Eigen::MatrixXd& err_beta) const;
178
179 bool TrailingAverageStalled() const;
180
183 SpinDensity SecondOrderStep(const SpinDensity& dmat, const SpinFock& H,
184 tools::EigenSystem& MOs_alpha,
185 tools::EigenSystem& MOs_beta, double totE);
186
189
190 AOOverlap* S_ = nullptr;
191 Logger* log_ = nullptr;
192 Eigen::MatrixXd Sminusahalf;
193
196 mutable bool analytic_hessian_failed_ = false;
197
200
201 std::vector<Eigen::MatrixXd> mathist_alpha_;
202 std::vector<Eigen::MatrixXd> mathist_beta_;
203 std::vector<Eigen::MatrixXd> dmatHist_alpha_;
204 std::vector<Eigen::MatrixXd> dmatHist_beta_;
205 std::vector<double> totE_;
206
209
210 double diiserror_ = 1.0;
211 // DIIS error of each history entry, aligned with mathist_alpha_.
212 std::vector<double> errhist_;
213 // See ConvergenceAcc.
214 static constexpr double kEnergyRiseForADIIS = 1e-4;
215 Eigen::MatrixXd removed_projector_;
216 static constexpr double kRemovedShift = 1e3;
217 bool usedmixing_ = true;
218
219 // Direct-minimization fallback: after kMaxConsecutiveADIISFailures
220 // failed (A)DIIS steps in a row, switch to
221 // DirectMinimizationRotation/CoupledAugmentedHessianStep (cf. ORCA's
222 // auto-TRAH) instead of mixing indefinitely.
225
226 // Second trigger for direct minimization: diiserror_ does not decrease
227 // fast enough on average over the last kTrailingWindowSize iterations
228 // (kMeanRatioTolerance), checked only after kAutoStartIteration
229 // iterations. Parameters as in ORCA's AutoTRAH (start iteration 50,
230 // 10 interpolation iterations, mean gradient ratio tolerance 1.125).
231 std::vector<double> diiserror_history_;
233 static constexpr Index kTrailingWindowSize = 10;
234 static constexpr Index kAutoStartIteration = 50;
235 static constexpr double kMeanRatioTolerance = 1.125;
236
237 // A second-order step has been returned and is checked (Fletcher,
238 // trust_) at the start of the next Iterate call, once its energy is known;
239 // the pre-step state is kept to undo a rejected step.
248 double direct_min_linear_ = 0.0;
250 mutable double last_step_linear_ = 0.0;
251 mutable double last_step_norm_ = 0.0;
252 // subspace of the last step, reused when a rejected step is repeated
254 bool last_rejected_ = false;
257 // second-order (TRAH) steps in use for this SCF
258 bool trah_active_ = false;
259
260 // Trust-radius floor: once a rejected step shrinks the trust radius
261 // below it, direct minimisation is given up for this SCF and Iterate
262 // falls back to mixing (direct_min_floor_hit_). Reset with each new
263 // UKSConvergenceAcc (one per DFTEngine::EvaluateUKS call).
265 static constexpr double kMinTrustRadius = 3e-3;
266 // Largest element of |D(MOs) - D| for which the input density is
267 // treated as the density of the current orbitals (see Iterate).
268 static constexpr double kDensityMismatchTolerance = 1e-8;
269};
270
271} // namespace xtp
272} // namespace votca
273
274#endif // VOTCA_XTP_UKS_CONVERGENCEACC_H
KSmode
Occupation model used when constructing density matrices.
Logger is used for thread-safe output of messages.
Definition logger.h:164
double CombinedError(const Eigen::MatrixXd &err_alpha, const Eigen::MatrixXd &err_beta) const
tools::EigenSystem SolveFockmatrix(const Eigen::MatrixXd &H) const
ConvergenceAcc::KSmode KSmode
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > UnflattenCoupledRotation(const Eigen::VectorXd &v, Index nao_alpha, Index nocclevels_alpha, Index nao_beta, Index nocclevels_beta) const
static constexpr Index kAutoStartIteration
SpinDensity DensityMatrix(const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta) const
void Levelshift(Eigen::MatrixXd &H, const Eigen::MatrixXd &MOs_old, const options &opt, Index nocclevels) const
std::vector< Eigen::MatrixXd > mathist_beta_
static constexpr double kMinTrustRadius
static constexpr Index kMaxConsecutiveADIISFailures
void setOverlap(AOOverlap &S, double etol)
void Configure(const options &opt_alpha, const options &opt_beta)
Eigen::VectorXd BuildCoupledSigmaVector(const Eigen::VectorXd &v, const Eigen::MatrixXd &C_alpha, Index nocclevels_alpha, const Eigen::MatrixXd &C_beta, Index nocclevels_beta, const CoupledFockBuilder &coupled_fock_builder, double finite_diff_step=1e-3) const
static constexpr double kEnergyRiseForADIIS
Eigen::MatrixXd BuildErrorMatrix(const Eigen::MatrixXd &dmat, const Eigen::MatrixXd &H) const
ConvergenceAcc::options options
SpinDensity Iterate(const SpinDensity &dmat, SpinFock &H, tools::EigenSystem &MOs_alpha, tools::EigenSystem &MOs_beta, double totE)
std::vector< Eigen::MatrixXd > dmatHist_alpha_
Eigen::MatrixXd DirectMinimizationRotation(const Eigen::MatrixXd &H_AO, const tools::EigenSystem &MOs, Index nocclevels, double &predicted_energy_change) const
Eigen::MatrixXd UnflattenRotation(const Eigen::VectorXd &v_ov, Index nao, Index nocclevels) const
Eigen::VectorXd direct_min_pre_MOs_beta_energies_
static constexpr double EnergyGradientScale
std::vector< double > diiserror_history_
std::vector< Eigen::MatrixXd > dmatHist_beta_
CoupledFockBuilder coupled_fock_builder_
Eigen::VectorXd direct_min_pre_MOs_alpha_energies_
static constexpr double kDensityMismatchTolerance
Eigen::MatrixXd DensityMatrixGroundState_unres(const Eigen::MatrixXd &MOs, Index nocclevels) const
std::function< Eigen::MatrixXd( const tools::EigenSystem &MOs_alpha, const tools::EigenSystem &MOs_beta, const Eigen::MatrixXd &F_alpha, const Eigen::MatrixXd &F_beta, const Eigen::MatrixXd &X)> OrbitalHessian
static constexpr Index kTrailingWindowSize
static constexpr double kMeanRatioTolerance
std::function< SpinFock(const Eigen::MatrixXd &, const Eigen::MatrixXd &)> CoupledFockBuilder
SpinDensity SecondOrderStep(const SpinDensity &dmat, const SpinFock &H, tools::EigenSystem &MOs_alpha, tools::EigenSystem &MOs_beta, double totE)
std::vector< Eigen::MatrixXd > mathist_alpha_
void setOrbitalHessian(const OrbitalHessian &hessian)
static constexpr double kRemovedShift
void setCoupledFockBuilder(const CoupledFockBuilder &builder)
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > CoupledAugmentedHessianStep(const Eigen::MatrixXd &H_AO_alpha, const tools::EigenSystem &MOs_alpha, Index nocclevels_alpha, const Eigen::MatrixXd &H_AO_beta, const tools::EigenSystem &MOs_beta, Index nocclevels_beta, const CoupledFockBuilder &coupled_fock_builder, double trust_radius, double &predicted_energy_change) const
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26