iterative-solver 0.0
SubspaceSolverOptBFGS.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVEROPTBFGS_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVEROPTBFGS_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 SubspaceSolverOptBFGS : public ISubspaceSolver<RT, QT, PT> {
17public:
23
24 explicit SubspaceSolverOptBFGS(std::shared_ptr<Logger> logger) : m_logger(std::move(logger)) {}
25
26 void solve(IXSpace<R, Q, P>& xspace, const size_t nroots_max) override {
27 m_logger->trace("SubspaceSolverOptBFGS::solve");
28 assert(xspace.data.end() != xspace.data.find(EqnData::value));
29 auto values = xspace.data[EqnData::value];
30 assert(xspace.size() == values.size());
31
32 auto kH = xspace.data[EqnData::H];
33 auto kS = xspace.data[EqnData::S];
34 auto kValue = xspace.data[EqnData::value];
35 m_logger->data_dump("S = ", kS);
36 m_logger->data_dump<15>("H = ", kH);
37 m_logger->data_dump<15>("value = ", kValue);
38 auto kDim = kH.rows();
39 m_solutions.resize({1, kDim});
40 if (kDim == 0) {
41 m_errors.clear();
42 return;
43 }
44 m_solutions.slice().fill(0);
45 m_solutions(0, 0) = 1;
46 m_errors.assign(1, kH(0, 0)); // FIXME
47 m_logger->data_dump("solution = ", m_solutions);
48 }
49
50public:
52 void set_error(int root, value_type_abs error) override { m_errors.at(root) = error; }
53 void set_error(const std::vector<int>& roots, const std::vector<value_type_abs>& errors) override {
54 for (size_t i = 0; i < roots.size(); ++i)
55 set_error(roots[i], errors[i]);
56 }
57
58 const Matrix<value_type>& solutions() const override { return m_solutions; }
59 const std::vector<value_type>& eigenvalues() const override {
60 throw std::logic_error("eigenvalues() not available in non-linear method");
61 }
62 const std::vector<value_type_abs>& errors() const override { return m_errors; }
63
65 size_t size() const override { return m_solutions.rows(); }
66
67 void set_logger(std::shared_ptr<Logger> logger) override { m_logger = std::move(logger); }
68
69protected:
71 std::vector<value_type_abs> m_errors;
72 std::shared_ptr<Logger> m_logger{};
73};
74
75} // namespace molpro::linalg::itsolv::subspace
76
77#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_SUBSPACESOLVEROPTBFGS_H
size_t size() const
Number of vectors forming the subspace.
Definition: IXSpace.h:31
SubspaceData data
Equation data in the subspace.
Definition: IXSpace.h:28
Slice slice(coord_type upper_left, coord_type bottom_right)
Access a rectangular slice of the matrix.
Definition: Matrix.h:99
void resize(const coord_type &dims)
Resize the matrix. The old data is preserved and any new rows/cols are zeroed.
Definition: Matrix.h:128
index_type rows() const
Definition: Matrix.h:167
Solves subspace problem for minimisation using the Steepest Descent algorithm.
Definition: SubspaceSolverOptBFGS.h:16
std::shared_ptr< Logger > m_logger
Definition: SubspaceSolverOptBFGS.h:72
void set_logger(std::shared_ptr< Logger > logger) override
Definition: SubspaceSolverOptBFGS.h:67
typename ISubspaceSolver< RT, QT, PT >::value_type value_type
Definition: SubspaceSolverOptBFGS.h:18
const std::vector< value_type > & eigenvalues() const override
Access eigenvalues from the last solve() call.
Definition: SubspaceSolverOptBFGS.h:59
void solve(IXSpace< R, Q, P > &xspace, const size_t nroots_max) override
Solve the subspace problem.
Definition: SubspaceSolverOptBFGS.h:26
typename ISubspaceSolver< RT, QT, PT >::value_type_abs value_type_abs
Definition: SubspaceSolverOptBFGS.h:19
std::vector< value_type_abs > m_errors
errors in subspace solutions
Definition: SubspaceSolverOptBFGS.h:71
void set_error(const std::vector< int > &roots, const std::vector< value_type_abs > &errors) override
Update errors for a group of roots.
Definition: SubspaceSolverOptBFGS.h:53
Matrix< value_type > m_solutions
solution matrix with row vectors
Definition: SubspaceSolverOptBFGS.h:70
SubspaceSolverOptBFGS(std::shared_ptr< Logger > logger)
Definition: SubspaceSolverOptBFGS.h:24
size_t size() const override
Number of solutions.
Definition: SubspaceSolverOptBFGS.h:65
void set_error(int root, value_type_abs error) override
Set error value for solution root
Definition: SubspaceSolverOptBFGS.h:52
const Matrix< value_type > & solutions() const override
Access solutions from the last solve() call.
Definition: SubspaceSolverOptBFGS.h:58
const std::vector< value_type_abs > & errors() const override
Access errors corresponding to each solution.
Definition: SubspaceSolverOptBFGS.h:62
Definition: PSpace.h:7
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