GQCP
Loading...
Searching...
No Matches
DIIS.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
21#include "Mathematical/Optimization/LinearEquation/LinearEquationEnvironment.hpp"
22#include "Mathematical/Optimization/LinearEquation/LinearEquationSolver.hpp"
24
25#include <vector>
26
27
28namespace GQCP {
29
30
36template <typename _Scalar>
37class DIIS {
38public:
39 using Scalar = _Scalar;
40
41public:
42 /*
43 * PUBLIC METHODS
44 */
45
51 template <typename Subject>
52 Subject accelerate(const std::vector<Subject>& subjects, const std::vector<VectorX<Scalar>>& errors) const {
53
54 const auto diis_coefficients = this->calculateDIISCoefficients(errors);
55
56 // Construct and return the DIIS-accelerated subject
57 Subject accelerated_subject = diis_coefficients(0) * subjects.at(0); // defaultly initializing may cause problems: the default constructor for a Matrix is a 0x0-matrix
58 for (size_t i = 1; i < errors.size(); i++) {
59 accelerated_subject += diis_coefficients(i) * subjects.at(i);
60 }
61 return accelerated_subject;
62 }
63
64
70 VectorX<Scalar> calculateDIISCoefficients(const std::vector<VectorX<Scalar>>& errors) const {
71
72 const auto n = errors.size();
73
74 // Initialize and calculate the augmented B matrix
75 SquareMatrix<Scalar> B = -1 * SquareMatrix<Scalar>::Ones(n + 1, n + 1); // +1 for the Lagrange multiplier
76 B(n, n) = 0;
77 for (size_t i = 0; i < n; i++) {
78 const auto& error_i = errors[i];
79
80 for (size_t j = 0; j < n; j++) {
81 const auto& error_j = errors[j];
82 B(i, j) = error_i.dot(error_j);
83 }
84 }
85
86 // Initialize the RHS of the system of equations
87 VectorX<Scalar> b = VectorX<Scalar>::Zero(n + 1); // +1 for the multiplier
88 b(n) = -1; // the last entry of b is accessed through n: dimension of b is n+1 - 1 because of computers
89
90
91 // Solve the DIIS linear equations [B x = b]
92 auto environment = LinearEquationEnvironment<Scalar>(B, b);
93 auto solver = LinearEquationSolver<Scalar>::HouseholderQR();
94 solver.perform(environment);
95
96 return environment.x;
97 }
98};
99
100
101} // namespace GQCP
Definition: DIIS.hpp:37
VectorX< Scalar > calculateDIISCoefficients(const std::vector< VectorX< Scalar > > &errors) const
Definition: DIIS.hpp:70
Subject accelerate(const std::vector< Subject > &subjects, const std::vector< VectorX< Scalar > > &errors) const
Definition: DIIS.hpp:52
_Scalar Scalar
Definition: DIIS.hpp:39
Definition: Matrix.hpp:47
Definition: SquareMatrix.hpp:39
Definition: BaseOneElectronIntegralBuffer.hpp:25