votca 2024.2-dev
Loading...
Searching...
No Matches
anderson_mixing.cc
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// Third party includes
21#include <boost/format.hpp>
22
23// Local VOTCA includes
25
26namespace votca {
27namespace xtp {
28
29void Anderson::Configure(const Index order, const double alpha) {
30 order_ = order + 1;
31 alpha_ = alpha;
32}
33
34void Anderson::UpdateOutput(const Eigen::VectorXd &newOutput) {
35
36 // Check if max mixing history is reached and adding new step to history
37 Index size = output_.size();
38 if (size > order_ - 1) {
39 output_.erase(output_.begin());
40 }
41 output_.push_back(newOutput);
42}
43
44void Anderson::UpdateInput(const Eigen::VectorXd &newInput) {
45 Index size = output_.size();
46 if (size > order_ - 1) {
47 input_.erase(input_.begin());
48 }
49 input_.push_back(newInput);
50}
51
52const Eigen::VectorXd Anderson::MixHistory() {
53
54 const Index iteration = output_.size();
55 const Index used_history = iteration - 1;
56 Eigen::VectorXd OutMixed = output_.back();
57 Eigen::VectorXd InMixed = input_.back();
58
59 if (iteration > 1 && order_ > 1) {
60
61 Eigen::VectorXd DeltaN = OutMixed - InMixed;
62
63 // Building Linear System for Coefficients
64 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(used_history, used_history);
65 Eigen::VectorXd c = Eigen::VectorXd::Zero(used_history);
66
67 for (Index m = 1; m < iteration; m++) {
68
69 c(m - 1) = (DeltaN - output_[used_history - m] + input_[used_history - m])
70 .dot(DeltaN);
71
72 for (Index j = 1; j < iteration; j++) {
73 A(m - 1, j - 1) =
74 (DeltaN - output_[used_history - m] + input_[used_history - m])
75 .dot((DeltaN - output_[used_history - j] +
76 input_[used_history - j]));
77 }
78 }
79 // Solving the System to obtain coefficients
80 Eigen::VectorXd coefficients = A.fullPivHouseholderQr().solve(c);
81
82 // Mixing the Potentials
83 for (Index n = 1; n < iteration; n++) {
84
85 OutMixed += coefficients(n - 1) *
86 (output_[used_history - n] - output_[used_history]);
87 InMixed += coefficients(n - 1) *
88 (input_[used_history - n] - input_[used_history]);
89 }
90 }
91
92 // Returning the linear Mix of Input and Output
93 return alpha_ * OutMixed + (1 - alpha_) * InMixed;
94}
95} // namespace xtp
96} // namespace votca
void UpdateInput(const Eigen::VectorXd &newInput)
const Eigen::VectorXd MixHistory()
void UpdateOutput(const Eigen::VectorXd &newOutput)
std::vector< Eigen::VectorXd > input_
void Configure(const Index order, const double alpha)
std::vector< Eigen::VectorXd > output_
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26