iterative-solver 0.0
 
Loading...
Searching...
No Matches
SubspaceSolverDIIS.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVERDIIS_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVERDIIS_H
3#include <molpro/linalg/itsolv/subspace/ISubspaceSolver.h>
4#include <molpro/linalg/itsolv/subspace/IXSpace.h>
5#include <molpro/linalg/itsolv/subspace/Matrix.h>
6#include <molpro/linalg/itsolv/Logger.h>
7
8#include <memory>
9
11
15template <class RT, class QT, class PT>
16class SubspaceSolverDIIS : public ISubspaceSolver<RT, QT, PT> {
17 const bool& m_converged;
18
19public:
25
26 explicit SubspaceSolverDIIS(std::shared_ptr<Logger> logger, const bool& converged)
27 : m_converged(converged), m_logger(std::move(logger)) {}
28
29 void solve(IXSpace<R, Q, P>& xspace, const size_t nroots_max) override {
30 m_logger->trace("SubspaceSolverDIIS::solve");
31
32 auto kH = xspace.data[EqnData::H];
33 auto kS = xspace.data[EqnData::S];
34 m_logger->data_dump("S = ", kS);
35 m_logger->data_dump<15>("H = ", kH);
36 auto kDim = kH.rows();
38 if (kDim == 0) {
39 m_errors.clear();
40 return;
41 }
42 if (m_converged) {
44 m_solutions(0, 0) = 1;
45 return;
46 }
47 // int kVerbosity = m_logger->max_trace_level == Logger::Info ? 3 : 0;
48 // auto dH = kH;
49 // auto dDim = kDim - 1;
50 // dH.resize({dDim, dDim});
51 // for (size_t i = 0; i < dDim; i++)
52 // for (size_t j = 0; j < dDim; j++)
53 // dH(i, j) = kH(i, j) - kH(i + 1, j) - kH(i, j + 1) + kH(i + 1, j + 1);
54 // m_logger->msg("dH = " + as_string(kH, 15), Logger::Info);
55 std::vector<value_type> solution(kDim);
56 std::vector<value_type> matrix;
57 matrix.reserve(kDim * kDim);
58 // std::copy(std::begin(kH),std::end(kH),matrix.begin());
59 for (size_t i = 0; i < kDim; ++i)
60 for (size_t j = 0; j < kDim; ++j)
61 matrix.push_back(kH(j, i));
62 solve_DIIS(solution, matrix, kDim, precision_scaled<value_type>(1e-10), 1);
63 // std::copy(solution.begin(),solution.end(),m_solutions.begin());
64 for (size_t i = 0; i < kDim; ++i)
65 m_solutions(0, i) = solution[i];
66 m_errors.assign(1, std::abs(kH(0, 0))); // TODO fix
67 m_logger->data_dump("solution = ", m_solutions);
68 }
69
70public:
72 void set_error(int root, value_type_abs error) override { m_errors.at(root) = error; }
73 void set_error(const std::vector<int>& roots, const std::vector<value_type_abs>& errors) override {
74 for (size_t i = 0; i < roots.size(); ++i)
76 }
77
78 const Matrix<value_type>& solutions() const override { return m_solutions; }
79 const std::vector<value_type>& eigenvalues() const override {
80 throw std::logic_error("eigenvalues() not available in non-linear method");
81 }
82 const std::vector<value_type_abs>& errors() const override { return m_errors; }
83
85 size_t size() const override { return m_solutions.rows(); }
86
87 void set_logger(std::shared_ptr<Logger> logger) override { m_logger = std::move(logger); }
88
89protected:
91 std::vector<value_type_abs> m_errors;
92 std::shared_ptr<Logger> m_logger{};
93
94public:
97};
98
99} // namespace molpro::linalg::itsolv::subspace
100
101#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVERDIIS_H
Full subspace.
Definition IXSpace.h:19
SubspaceData< value_type > data
Equation data in the subspace.
Definition IXSpace.h:28
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
void fill(T value)
Sets all elements of matrix to value.
Definition Matrix.h:93
void resize(const coord_type &dims)
Resize the matrix. The old data is preserved and any new rows/cols are zeroed.
Definition Matrix.h:130
index_type rows() const
Definition Matrix.h:169
Solves non-linear equations using DIIS.
Definition SubspaceSolverDIIS.h:16
const std::vector< value_type > & eigenvalues() const override
Access eigenvalues from the last solve() call.
Definition SubspaceSolverDIIS.h:79
std::shared_ptr< Logger > m_logger
Definition SubspaceSolverDIIS.h:92
const Matrix< value_type > & solutions() const override
Access solutions from the last solve() call.
Definition SubspaceSolverDIIS.h:78
const std::vector< value_type_abs > & errors() const override
Access errors corresponding to each solution.
Definition SubspaceSolverDIIS.h:82
typename ISubspaceSolver< RT, QT, PT >::R R
Definition SubspaceSolverDIIS.h:22
typename ISubspaceSolver< RT, QT, PT >::value_type value_type
Definition SubspaceSolverDIIS.h:20
void set_logger(std::shared_ptr< Logger > logger) override
Definition SubspaceSolverDIIS.h:87
typename ISubspaceSolver< RT, QT, PT >::P P
Definition SubspaceSolverDIIS.h:24
std::vector< value_type_abs > m_errors
errors in subspace solutions
Definition SubspaceSolverDIIS.h:91
void set_error(int root, value_type_abs error) override
Set error value for solution root
Definition SubspaceSolverDIIS.h:72
void set_error(const std::vector< int > &roots, const std::vector< value_type_abs > &errors) override
Update errors for a group of roots.
Definition SubspaceSolverDIIS.h:73
Matrix< value_type > m_solutions
solution matrix with row vectors
Definition SubspaceSolverDIIS.h:90
typename ISubspaceSolver< RT, QT, PT >::value_type_abs value_type_abs
Definition SubspaceSolverDIIS.h:21
SubspaceSolverDIIS(std::shared_ptr< Logger > logger, const bool &converged)
Definition SubspaceSolverDIIS.h:26
void solve(IXSpace< R, Q, P > &xspace, const size_t nroots_max) override
Solve the subspace problem.
Definition SubspaceSolverDIIS.h:29
value_type_abs m_svd_solver_threshold
threshold to select null space during SVD in eigenproblem, rescaled from double to the working precis...
Definition SubspaceSolverDIIS.h:96
typename ISubspaceSolver< RT, QT, PT >::Q Q
Definition SubspaceSolverDIIS.h:23
size_t size() const override
Number of solutions.
Definition SubspaceSolverDIIS.h:85
std::unique_ptr< LinearEigensystem< R, Q, P > > create_LinearEigensystem(const LinearEigensystemOptions &options, const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers=std::make_shared< molpro::linalg::itsolv::ArrayHandlers< R, Q, P > >())
Definition SolverFactory.h:111
void solve_DIIS(std::vector< value_type > &solution, const std::vector< value_type > &matrix, size_t dimension, real_type_t< value_type > svdThreshold, int verbosity=0)
Definition helper-implementation.h:826
Manages solution of the subspace problem and storage of those solutions.
Definition ISubspaceSolver.h:21
typename array::ArrayHandler< R, R >::value_type value_type
Definition ISubspaceSolver.h:25
typename array::ArrayHandler< R, R >::value_type_abs value_type_abs
Definition ISubspaceSolver.h:26
PT P
Definition ISubspaceSolver.h:24
QT Q
Definition ISubspaceSolver.h:23
RT R
Definition ISubspaceSolver.h:22