iterative-solver 0.0
 
Loading...
Searching...
No Matches
LinearEquationsDavidson.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_LINEAREQUATIONSDAVIDSON_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_LINEAREQUATIONSDAVIDSON_H
3#include <molpro/linalg/itsolv/CastOptions.h>
4#include <molpro/linalg/itsolv/DavidsonCommon.h>
5#include <molpro/linalg/itsolv/IterativeSolverTemplate.h>
6#include <molpro/linalg/itsolv/subspace/SubspaceSolverLinEig.h>
7#include <molpro/linalg/itsolv/subspace/XSpace.h>
8
9#include <algorithm>
10#include <iterator>
11#include <memory>
12#include <vector>
13
14namespace molpro::linalg::itsolv {
40template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
41class LinearEquationsDavidson : public DavidsonCommon<LinearEquations, R, Q, P> {
42public:
45 using typename SolverTemplate::value_type;
46 using typename SolverTemplate::value_type_abs;
47
49 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
51
52 void add_equations(const CVecRef<R>& rhs) override {
53 auto prof = this->profiler()->push("itsolv::add_equations");
54 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
55 xspace->add_rhs_equations(rhs);
56 this->set_n_roots(xspace->dimensions().nRHS);
57 }
58
59 void add_equations(const R& rhs) override { add_equations(cwrap_arg(rhs)); }
60 void add_equations(const std::vector<R>& rhs) override { add_equations(cwrap(rhs)); }
61
62 CVecRef<Q> rhs() const override {
63 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
64 return xspace->rhs();
65 }
66
68 void set_augmented_hessian(const value_type_abs parameter) {
69 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
70 subspace_solver->set_augmented_hessian(parameter);
71 }
73 value_type_abs get_augmented_hessian() const {
74 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
75 return subspace_solver->get_augmented_hessian();
76 }
77
78 void set_options(const Options& options) override {
80 this->set_davidson_options(options, opt);
81 if (opt.augmented_hessian)
82 set_augmented_hessian(opt.augmented_hessian.value());
83 }
84
85 std::shared_ptr<Options> get_options() const override {
87 opt->augmented_hessian = get_augmented_hessian();
88 return opt;
89 }
90
91 void report(std::ostream& cout, bool endl = true) const override {
94 cout << ", errors " << std::scientific;
95 auto& err = this->m_errors;
96 std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout, ", "));
97 if (endl)
98 cout << std::endl;
99 }
100
101protected:
103 void prepare_solve(const Problem<R>& problem, const VecRef<R>& actions) override {
104 R& vector = actions[0];
105 for (unsigned int instance = 0; problem.RHS(vector, instance); ++instance)
107 }
108
110 std::vector<value_type> values() const override {
111 if (get_augmented_hessian() > 0)
112 return this->m_subspace_solver->eigenvalues();
113 const auto& solutions = this->m_subspace_solver->solutions(); // c(i, k): root i, subspace vector k
114 const auto& rhs = this->m_xspace->data.at(subspace::EqnData::rhs); // <v_k|b_i>
115 auto chi = std::vector<value_type>(std::min(solutions.rows(), rhs.cols()), value_type{0});
116 for (size_t i = 0; i < chi.size(); ++i)
117 for (size_t k = 0; k < std::min(solutions.cols(), rhs.rows()); ++k)
118 chi[i] += solutions(i, k) * molpro::linalg::conjugate(rhs(k, i));
119 return chi;
120 }
121
122 // FIXME The scale is fixed by the norm of RHS, but if RHS=0 there is no reference. We could use the norm of params
123 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions,
125 assert(params.size() >= roots.size());
126 const auto& norm = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace)->rhs_norm();
127 // With the augmented Hessian the subspace solution satisfies (H - lambda) x = rhs, with lambda the subspace
128 // eigenvalue; without it lambda is zero
129 const auto& eigenvalues = this->m_subspace_solver->eigenvalues();
130 for (size_t i = 0; i < roots.size(); ++i) {
131 const auto ii = roots[i];
132 this->m_handlers->rq().axpy(-1, rhs().at(ii), actions.at(i));
133 if (size_t(ii) < eigenvalues.size() and eigenvalues[ii] != value_type(0))
134 this->m_handlers->rr().axpy(-eigenvalues[ii], params.at(i), actions.at(i));
135 if (norm.at(ii) != 0) {
136 auto scal = 1 / norm[ii];
137 this->m_handlers->rr().scal(scal, actions.at(i));
138 }
139 }
140 }
141};
142
143} // namespace molpro::linalg::itsolv
144#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_LINEAREQUATIONSDAVIDSON_H
Class, containing a collection of array handlers used in IterativeSolver Provides a Builder sub-class...
Definition ArrayHandlers.h:25
Implementation shared by the linear Davidson solvers, LinearEigensystemDavidson and LinearEquationsDa...
Definition DavidsonCommon.h:35
void set_davidson_options(const Options &options, const DavidsonOptions &opt)
Sets the general options and those of the Davidson solvers, after checking them.
Definition DavidsonCommon.h:155
Implements IterativeSolver interface that is common to all solvers.
Definition IterativeSolverTemplate.h:187
std::shared_ptr< subspace::IXSpace< R, Q, P > > m_xspace
manages the subspace and associated data
Definition IterativeSolverTemplate.h:1210
void set_n_roots(size_t roots) override
Definition IterativeSolverTemplate.h:318
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Array handlers.
Definition IterativeSolverTemplate.h:1209
std::shared_ptr< subspace::ISubspaceSolver< R, Q, P > > m_subspace_solver
solves the subspace problem
Definition IterativeSolverTemplate.h:1211
std::vector< value_type_abs > m_errors
errors from the most recent solution
Definition IterativeSolverTemplate.h:1212
const std::shared_ptr< molpro::profiler::Profiler > & profiler() const override
Definition IterativeSolverTemplate.h:434
void report() const override
Definition IterativeSolverTemplate.h:384
Solves a system of linear equation, A x = b.
Definition LinearEquationsDavidson.h:41
void add_equations(const std::vector< R > &rhs) override
Definition LinearEquationsDavidson.h:60
std::vector< value_type > values() const override
The values chi_ii = <b_i|x_i> of the current solutions, or with the augmented Hessian the subspace ei...
Definition LinearEquationsDavidson.h:110
void add_equations(const CVecRef< R > &rhs) override
Definition LinearEquationsDavidson.h:52
std::shared_ptr< Options > get_options() const override
Definition LinearEquationsDavidson.h:85
LinearEquationsDavidson(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition LinearEquationsDavidson.h:48
void add_equations(const R &rhs) override
Definition LinearEquationsDavidson.h:59
void set_options(const Options &options) override
Definition LinearEquationsDavidson.h:78
CVecRef< Q > rhs() const override
Definition LinearEquationsDavidson.h:62
void construct_residual(const std::vector< int > &roots, const CVecRef< R > &params, const VecRef< R > &actions, const detail::MetricSolutions< R, Q > &metric_solutions) override
Constructs residual for given roots provided their parameters and actions.
Definition LinearEquationsDavidson.h:123
void report(std::ostream &cout, bool endl=true) const override
Definition LinearEquationsDavidson.h:91
void prepare_solve(const Problem< R > &problem, const VecRef< R > &actions) override
Adds the right-hand sides provided by problem.
Definition LinearEquationsDavidson.h:103
void set_augmented_hessian(const value_type_abs parameter)
Set value of augmented hessian parameter. If 0, than augmented Hessian is not used.
Definition LinearEquationsDavidson.h:68
value_type_abs get_augmented_hessian() const
Definition LinearEquationsDavidson.h:73
Definition IterativeSolver.h:782
Abstract class defining the problem-specific interface for the simplified solver interface to Iterati...
Definition IterativeSolver.h:125
virtual bool RHS(R &RHS, unsigned int instance) const
Return the inhomogeneous part of a linear equation system.
Definition IterativeSolver.h:265
Restores the format flags and precision of a stream when it goes out of scope.
Definition util.h:156
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition helper.h:14
auto cwrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition wrap.h:52
auto cwrap_arg(T &&arg, S &&... args) -> std::enable_if_t< std::conjunction_v< std::is_same< decay_t< T >, decay_t< S > >... >, CVecRef< decay_t< T > > >
Constructs a vector of const reference wrappers with provided arguments.
Definition wrap.h:149
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
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
const std::shared_ptr< const molpro::Options > options()
Get the Options object associated with iterative-solver.
Definition linalg_options.cpp:4
static std::shared_ptr< LinearEquationsDavidsonOptions > LinearEquations(const std::shared_ptr< Options > &options)
Definition CastOptions.h:54
Access point for different options in iterative solvers.
Definition Options.h:20
The actions of a metric applied on the fly on solutions, in R vectors or, if there are not enough of ...
Definition IterativeSolverTemplate.h:40