GQCP
Loading...
Searching...
No Matches
ConsecutiveIteratesNormConvergence.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
22#include "Utilities/complex.hpp"
24
25#include <deque>
26#include <functional>
27
28
29namespace GQCP {
30
31
38template <typename _Iterate, typename _Environment>
40 public ConvergenceCriterion<_Environment> {
41
42public:
43 using Iterate = _Iterate;
44 using Scalar = typename Iterate::Scalar;
45 using Environment = _Environment;
46 static_assert(std::is_same<Scalar, typename Environment::Scalar>::value, "The scalar types of the iterate and environment must match.");
47
48
49private:
50 double m_threshold; // the threshold that is used in comparing the iterates
51
52 std::string iterate_description; // the description of the the iterates that are compared
53
54 std::function<std::deque<Iterate>(const Environment&)> extractor; // a function that can extract the correct iterates from the environment, as it's not mandatory to check convergence on the variables, but any iterate (whose .norm() can be calculated) can in principle be used
55
56
57public:
58 /*
59 * CONSTRUCTORS
60 */
61
68 const double threshold = 1.0e-08, const std::function<std::deque<Iterate>(const Environment&)> extractor = [](const Environment& environment) { return environment.variables; }, const std::string& iterate_description = "a general iterate") :
69 m_threshold {threshold},
70 extractor {extractor},
71 iterate_description {iterate_description} {}
72
73
74 /*
75 * PUBLIC OVERRIDDEN METHODS
76 */
77
81 std::string description() const override {
82 return (boost::format("A convergence criterion that checks if the norm of the difference of two iterates (%s) is converged, with a tolerance of %.2e.") % this->iterate_description % this->threshold()).str();
83 }
84
85
91 bool isFulfilled(Environment& environment) override {
92
93 const auto iterates = this->extractor(environment);
94
95 if (iterates.size() < 2) {
96 return false; // we can't calculate convergence
97 }
98
99 // Get the two most recent density matrices and compare the norm of their difference
100 const auto second_to_last_it = iterates.end() - 2; // 'it' for 'iterator'
101 const auto& previous = *second_to_last_it; // Dereference the iterator.
102 const auto current = iterates.back();
103
104 return (std::real((current - previous).norm()) <= this->m_threshold);
105 }
106
107
111 double threshold() const { return this->m_threshold; }
112};
113
114
115} // namespace GQCP
Definition: ConsecutiveIteratesNormConvergence.hpp:40
bool isFulfilled(Environment &environment) override
Definition: ConsecutiveIteratesNormConvergence.hpp:91
typename Iterate::Scalar Scalar
Definition: ConsecutiveIteratesNormConvergence.hpp:44
_Iterate Iterate
Definition: ConsecutiveIteratesNormConvergence.hpp:43
ConsecutiveIteratesNormConvergence(const double threshold=1.0e-08, const std::function< std::deque< Iterate >(const Environment &)> extractor=[](const Environment &environment) { return environment.variables;}, const std::string &iterate_description="a general iterate")
Definition: ConsecutiveIteratesNormConvergence.hpp:67
std::string description() const override
Definition: ConsecutiveIteratesNormConvergence.hpp:81
_Environment Environment
Definition: ConsecutiveIteratesNormConvergence.hpp:45
double threshold() const
Definition: ConsecutiveIteratesNormConvergence.hpp:111
Definition: ConvergenceCriterion.hpp:33
Definition: BaseOneElectronIntegralBuffer.hpp:25