votca 2024.2-dev
Loading...
Searching...
No Matches
diis.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// Standard includes
21#include <iostream>
22
23// Local VOTCA includes
24#include "votca/xtp/diis.h"
25
26namespace votca {
27namespace xtp {
28
29void DIIS::Update(Index maxerrorindex, const Eigen::MatrixXd& errormatrix) {
30
31 if (int(errormatrixhist_.size()) == histlength_) {
32 errormatrixhist_.erase(errormatrixhist_.begin() + maxerrorindex);
33 Diis_Bs_.erase(Diis_Bs_.begin() + maxerrorindex);
34 for (std::vector<double>& subvec : Diis_Bs_) {
35 subvec.erase(subvec.begin() + maxerrorindex);
36 }
37 }
38
39 errormatrixhist_.push_back(errormatrix);
40
41 std::vector<double> Bijs;
42 for (Index i = 0; i < Index(errormatrixhist_.size()) - 1; i++) {
43 double value =
44 errormatrix.cwiseProduct((errormatrixhist_[i]).transpose()).sum();
45 Bijs.push_back(value);
46 Diis_Bs_[i].push_back(value);
47 }
48 Bijs.push_back(errormatrix.cwiseProduct(errormatrix.transpose()).sum());
49 Diis_Bs_.push_back(Bijs);
50 return;
51}
52
53Eigen::VectorXd DIIS::CalcCoeff() {
54 success = true;
55 const Index size = Index(errormatrixhist_.size());
56
57 // C2-DIIS
58 Eigen::MatrixXd B = Eigen::MatrixXd::Zero(size, size);
59
60 for (Index i = 0; i < B.rows(); i++) {
61 for (Index j = 0; j <= i; j++) {
62 B(i, j) = Diis_Bs_[i][j];
63 if (i != j) {
64 B(j, i) = B(i, j);
65 }
66 }
67 }
68 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(B);
69 Eigen::MatrixXd eigenvectors = Eigen::MatrixXd::Zero(size, size);
70
71 for (Index i = 0; i < es.eigenvectors().cols(); i++) {
72 double norm = es.eigenvectors().col(i).sum();
73 eigenvectors.col(i) = es.eigenvectors().col(i) / norm;
74 }
75
76 // Choose solution by picking out solution with smallest absolute error
77 Eigen::VectorXd errors =
78 (eigenvectors.transpose() * B * eigenvectors).diagonal().cwiseAbs();
79
80 double MaxWeight = 10.0;
81 Index mincoeff = 0;
82 success = false;
83 for (Index i = 0; i < errors.size(); i++) {
84 errors.minCoeff(&mincoeff);
85 if (std::abs(eigenvectors.col(mincoeff).maxCoeff()) > MaxWeight) {
86 errors[mincoeff] = std::numeric_limits<double>::max();
87 } else {
88 success = true;
89 break;
90 }
91 }
92
93 Eigen::VectorXd coeffs = eigenvectors.col(mincoeff);
94
95 if (std::abs(coeffs[coeffs.size() - 1]) < 0.001) {
96 success = false;
97 }
98
99 return coeffs;
100}
101
102} // namespace xtp
103} // namespace votca
bool success
Definition diis.h:43
std::vector< Eigen::MatrixXd > errormatrixhist_
Definition diis.h:46
void Update(Index maxerrorindex, const Eigen::MatrixXd &errormatrix)
Definition diis.cc:29
std::vector< std::vector< double > > Diis_Bs_
Definition diis.h:45
Index histlength_
Definition diis.h:44
Eigen::VectorXd CalcCoeff()
Definition diis.cc:53
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26