GQCP
Loading...
Searching...
No Matches
T2DIIS.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
26
27#include <algorithm>
28
29
30namespace GQCP {
31
32
38template <typename _Scalar>
39class T2DIIS:
40 public Step<CCSDEnvironment<_Scalar>> {
41
42public:
43 // The scalar type used to represent the T2 amplitudes.
44 using Scalar = _Scalar;
45
46 // The type of environment that this iteration step can access.
48
49
50private:
51 // The minimum number of T2 amplitudes that have to be in the subspace before enabling DIIS.
52 size_t minimum_subspace_dimension;
53
54 // The maximum number of T2 amplitues that can be handled by DIIS.
55 size_t maximum_subspace_dimension;
56
57 // The DIIS accelerator.
58 DIIS<Scalar> diis;
59
60
61public:
62 /*
63 * MARK: Constructors
64 */
65
70 T2DIIS(const size_t minimum_subspace_dimension = 6, const size_t maximum_subspace_dimension = 6) :
71 minimum_subspace_dimension {minimum_subspace_dimension},
72 maximum_subspace_dimension {maximum_subspace_dimension} {}
73
74
75 /*
76 * MARK: Conforming to `Step`.
77 */
78
82 std::string description() const override {
83 return "Calculate the accelerated T2 amplitudes and place them in the environment by overwriting the previous T2 amplitudes.";
84 }
85
86
92 void execute(Environment& environment) override {
93
94 // Don't do anything if the minimum number of T2 amplitude iterations isn't satisfied.
95 if (environment.t2_amplitude_errors.size() < this->minimum_subspace_dimension) {
96 return;
97 }
98
99 // Convert the deques in the environment to vectors that can be accepted by the DIIS accelerator. The total number of elements we can use in DIIS is either the maximum subspace dimension or the number of available error vectors.
100 // TODO: Include the possibility for an x-iteration 'relaxation', i.e. not doing DIIS for x iterations long.
101 const auto n = std::min(this->maximum_subspace_dimension, environment.t2_amplitude_errors.size());
102 const std::vector<VectorX<Scalar>> error_vectors {environment.t2_amplitude_errors.end() - n, environment.t2_amplitude_errors.end()}; // The n-th last error vectors.
103 const std::vector<T2Amplitudes<Scalar>> t2_amplitudes {environment.t2_amplitudes.end() - n, environment.t2_amplitudes.end()}; // The n-th last T2 amplitudes.
104
105
106 // Calculate the accelerated T2 amplitudes and place them in the environment by overwriting the previous T2 amplitudes.
107 const auto t2_amplitudes_accelerated = this->diis.accelerate(t2_amplitudes, error_vectors);
108
109 environment.t2_amplitudes.pop_back();
110 environment.t2_amplitudes.push_back(t2_amplitudes_accelerated);
111 }
112};
113
114
115} // namespace GQCP
Definition: CCSDEnvironment.hpp:39
std::deque< T2Amplitudes< Scalar > > t2_amplitudes
Definition: CCSDEnvironment.hpp:49
std::deque< VectorX< Scalar > > t2_amplitude_errors
Definition: CCSDEnvironment.hpp:52
Definition: DIIS.hpp:37
Subject accelerate(const std::vector< Subject > &subjects, const std::vector< VectorX< Scalar > > &errors) const
Definition: DIIS.hpp:52
Definition: Step.hpp:37
Definition: T2DIIS.hpp:40
T2DIIS(const size_t minimum_subspace_dimension=6, const size_t maximum_subspace_dimension=6)
Definition: T2DIIS.hpp:70
void execute(Environment &environment) override
Definition: T2DIIS.hpp:92
std::string description() const override
Definition: T2DIIS.hpp:82
_Scalar Scalar
Definition: T2DIIS.hpp:44
Definition: BaseOneElectronIntegralBuffer.hpp:25