iterative-solver 0.0
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/DSpaceResetter.h>
5#include <molpro/linalg/itsolv/IterativeSolverTemplate.h>
6#include <molpro/linalg/itsolv/propose_rspace.h>
7#include <molpro/linalg/itsolv/subspace/SubspaceSolverLinEig.h>
8#include <molpro/linalg/itsolv/subspace/XSpace.h>
9
10namespace molpro::linalg::itsolv {
29template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
30class LinearEquationsDavidson : public IterativeSolverTemplate<LinearEquations, R, Q, P> {
31public:
34 using typename SolverTemplate::value_type;
35 using typename SolverTemplate::value_type_abs;
36
37 explicit LinearEquationsDavidson(const std::shared_ptr<ArrayHandlers<R, Q, P>>& handlers,
38 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
39 : SolverTemplate(std::make_shared<subspace::XSpace<R, Q, P>>(handlers, logger_),
40 std::static_pointer_cast<subspace::ISubspaceSolver<R, Q, P>>(
41 std::make_shared<subspace::SubspaceSolverLinEig<R, Q, P>>(logger_)),
42 handlers, std::make_shared<Statistics>(), logger_),
43 logger(logger_) {
45 this->m_normalise_solution = false;
46 }
47
48 bool solve(const VecRef<R>& parameters, const VecRef<R>& actions, const Problem<R>& problem,
49 bool generate_initial_guess = false) override {
50 R& vector = actions[0];
51 for (unsigned int instance = 0; problem.RHS(vector, instance); ++instance)
52 add_equations(vector);
53 return IterativeSolverTemplate<LinearEquations, R, Q, P>::solve(parameters, actions, problem,
54 generate_initial_guess);
55 }
56
57 bool nonlinear() const override { return false; }
58
59 size_t end_iteration(const VecRef<R>& parameters, const VecRef<R>& action) override {
60 auto prof = this->profiler()->push("itsolv::end_iteration");
61 if (m_dspace_resetter.do_reset(this->m_stats->iterations, this->m_xspace->dimensions())) {
62 this->m_working_set = m_dspace_resetter.run(parameters, *this->m_xspace, this->m_subspace_solver->solutions(),
63 m_norm_thresh, m_svd_thresh, *this->m_handlers, *this->m_logger);
64 } else {
65 this->m_working_set = detail::propose_rspace(*this, parameters, action, *this->m_xspace, *this->m_subspace_solver,
67 m_max_size_qspace, *this->profiler());
68 }
69 this->m_stats->iterations++;
70 this->m_end_iteration_needed = false;
71 return this->working_set().size();
72 }
73
74 // FIXME move this to the template
75 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) override {
76 return end_iteration(wrap(parameters), wrap(action));
77 }
78 size_t end_iteration(R& parameters, R& actions) override {
79 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
80 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
81 return end_iteration(wparams, wactions);
82 }
83
84 void add_equations(const CVecRef<R>& rhs) override {
85 auto prof = this->profiler()->push("itsolv::add_equations");
86 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
87 xspace->add_rhs_equations(rhs);
88 this->set_n_roots(xspace->dimensions().nRHS);
89 }
90
91 void add_equations(const R& rhs) override { add_equations(cwrap_arg(rhs)); }
92 void add_equations(const std::vector<R>& rhs) override { add_equations(cwrap(rhs)); }
93
94 CVecRef<Q> rhs() const override {
95 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
96 return xspace->rhs();
97 }
98
100 void set_norm_thresh(double thresh) { m_norm_thresh = thresh; }
101 double get_norm_thresh() const { return m_norm_thresh; }
104 void set_svd_thresh(double thresh) { m_svd_thresh = thresh; }
105 double get_svd_thresh() const { return m_svd_thresh; }
107 void set_reset_D(size_t n) { m_dspace_resetter.set_nreset(n); }
108 size_t get_reset_D() const { return m_dspace_resetter.get_nreset(); }
110 void set_reset_D_maxQ_size(size_t n) { m_dspace_resetter.set_max_Qsize(n); }
111 int get_reset_D_maxQ_size() const { return m_dspace_resetter.get_max_Qsize(); }
116 if (m_dspace_resetter.get_max_Qsize() > m_max_size_qspace)
118 }
120 void set_hermiticity(bool hermitian) override {
121 m_hermiticity = hermitian;
122 auto xspace = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
123 xspace->set_hermiticity(hermitian);
124 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
125 subspace_solver->set_hermiticity(hermitian);
126 }
127 bool get_hermiticity() const override { return m_hermiticity; }
129 void set_augmented_hessian(const double parameter) {
130 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
131 subspace_solver->set_augmented_hessian(parameter);
132 }
134 double get_augmented_hessian() const {
135 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
136 return subspace_solver->get_augmented_hessian();
137 }
138
139 void set_options(const Options& options) override {
142 if (opt.reset_D)
143 set_reset_D(opt.reset_D.value());
144 if (opt.reset_D_max_Q_size)
145 set_reset_D_maxQ_size(opt.reset_D_max_Q_size.value());
146 if (opt.max_size_qspace)
147 set_max_size_qspace(opt.max_size_qspace.value());
148 if (opt.norm_thresh)
149 set_norm_thresh(opt.norm_thresh.value());
150 if (opt.svd_thresh)
151 set_svd_thresh(opt.svd_thresh.value());
152 if (opt.hermiticity)
153 set_hermiticity(opt.hermiticity.value());
154 if (opt.augmented_hessian)
155 set_augmented_hessian(opt.augmented_hessian.value());
156 }
157
158 std::shared_ptr<Options> get_options() const override {
159 auto opt = std::make_shared<LinearEquationsDavidsonOptions>();
160 opt->copy(*SolverTemplate::get_options());
161 opt->reset_D = get_reset_D();
162 opt->reset_D_max_Q_size = get_reset_D_maxQ_size();
163 opt->max_size_qspace = get_max_size_qspace();
164 opt->norm_thresh = get_norm_thresh();
165 opt->svd_thresh = get_svd_thresh();
166 opt->hermiticity = get_hermiticity();
167 opt->augmented_hessian = get_augmented_hessian();
168 return opt;
169 }
170
171 void report(std::ostream& cout, bool endl = true) const override {
172 SolverTemplate::report(cout, false);
173 cout << ", errors " << std::scientific;
174 auto& err = this->m_errors;
175 std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout, ", "));
176 cout << std::defaultfloat;
177 if (endl)
178 cout << std::endl;
179 }
180 std::shared_ptr<Logger> logger;
181
182protected:
183 // 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
184 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions) override {
185 assert(params.size() >= roots.size());
186 const auto& norm = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace)->rhs_norm();
187 for (size_t i = 0; i < roots.size(); ++i) {
188 const auto ii = roots[i];
189 this->m_handlers->rq().axpy(-1, rhs().at(ii), actions.at(i));
190 if (norm.at(ii) != 0) {
191 auto scal = 1 / norm[ii];
192 this->m_handlers->rr().scal(scal, actions.at(i));
193 }
194 }
195 }
196
197 double m_norm_thresh = 1e-10;
198 double m_svd_thresh = 1e-12;
199 int m_max_size_qspace = std::numeric_limits<int>::max();
201 bool m_hermiticity = true;
202};
203
204} // namespace molpro::linalg::itsolv
205#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
Implements IterativeSolver interface that is common to all solvers.
Definition: IterativeSolverTemplate.h:161
std::vector< int > m_working_set
indices of roots in the working set
Definition: IterativeSolverTemplate.h:676
std::shared_ptr< subspace::IXSpace< R, R, std::map< size_t, typename R::value_type > > > m_xspace
manages the subspace and associated data
Definition: IterativeSolverTemplate.h:672
std::shared_ptr< ArrayHandlers< R, R, std::map< size_t, typename R::value_type > > > m_handlers
Array handlers.
Definition: IterativeSolverTemplate.h:671
std::shared_ptr< subspace::ISubspaceSolver< R, R, std::map< size_t, typename R::value_type > > > m_subspace_solver
solves the subspace problem
Definition: IterativeSolverTemplate.h:673
std::shared_ptr< Options > get_options() const override
Definition: IterativeSolverTemplate.h:308
const std::shared_ptr< molpro::profiler::Profiler > & profiler() const override
Definition: IterativeSolverTemplate.h:394
bool m_end_iteration_needed
whether end_iteration should be called after any preconditioner
Definition: IterativeSolverTemplate.h:693
void report() const override
Definition: IterativeSolverTemplate.h:349
bool m_normalise_solution
whether to normalise the solutions
Definition: IterativeSolverTemplate.h:683
const std::vector< int > & working_set() const override
Working set of roots that are not yet converged.
Definition: IterativeSolverTemplate.h:283
void set_options(const Options &options) override
Definition: IterativeSolverTemplate.h:293
std::vector< double > m_errors
errors from the most recent solution
Definition: IterativeSolverTemplate.h:674
bool solve(const VecRef< R > &parameters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false) override
Definition: IterativeSolverTemplate.h:396
std::shared_ptr< Statistics > m_stats
accumulates statistics of operations performed by the solver
Definition: IterativeSolverTemplate.h:681
Solves a system of linear equation, A x = b.
Definition: LinearEquationsDavidson.h:30
size_t get_reset_D() const
Definition: LinearEquationsDavidson.h:108
detail::DSpaceResetter< Q > m_dspace_resetter
resets D space
Definition: LinearEquationsDavidson.h:200
bool solve(const VecRef< R > &parameters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false) override
Simplified one-call solver.
Definition: LinearEquationsDavidson.h:48
std::shared_ptr< Logger > logger
Definition: LinearEquationsDavidson.h:180
double m_norm_thresh
vectors with norm less than threshold can be considered null.
Definition: LinearEquationsDavidson.h:197
void add_equations(const std::vector< R > &rhs) override
Definition: LinearEquationsDavidson.h:92
void set_hermiticity(bool hermitian) override
Sets hermiticity of kernel.
Definition: LinearEquationsDavidson.h:120
void set_augmented_hessian(const double parameter)
Set value of augmented hessian parameter. If 0, than augmented Hessian is not used.
Definition: LinearEquationsDavidson.h:129
void set_svd_thresh(double thresh)
Definition: LinearEquationsDavidson.h:104
size_t end_iteration(const VecRef< R > &parameters, const VecRef< R > &action) override
Behaviour depends on the solver.
Definition: LinearEquationsDavidson.h:59
double get_norm_thresh() const
Definition: LinearEquationsDavidson.h:101
double m_svd_thresh
svd values smaller than this mark the null space
Definition: LinearEquationsDavidson.h:198
size_t end_iteration(R &parameters, R &actions) override
Definition: LinearEquationsDavidson.h:78
void add_equations(const CVecRef< R > &rhs) override
Definition: LinearEquationsDavidson.h:84
std::shared_ptr< Options > get_options() const override
Definition: LinearEquationsDavidson.h:158
LinearEquationsDavidson(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition: LinearEquationsDavidson.h:37
void set_norm_thresh(double thresh)
Set threshold on the norm of parameters that should be considered null.
Definition: LinearEquationsDavidson.h:100
void add_equations(const R &rhs) override
Definition: LinearEquationsDavidson.h:91
int get_reset_D_maxQ_size() const
Definition: LinearEquationsDavidson.h:111
bool nonlinear() const override
Report whether the class is a non-linear solver.
Definition: LinearEquationsDavidson.h:57
size_t end_iteration(std::vector< R > &parameters, std::vector< R > &action) override
Definition: LinearEquationsDavidson.h:75
void set_options(const Options &options) override
Definition: LinearEquationsDavidson.h:139
bool m_hermiticity
whether the problem is hermitian or not
Definition: LinearEquationsDavidson.h:201
int m_max_size_qspace
maximum size of Q space
Definition: LinearEquationsDavidson.h:199
void construct_residual(const std::vector< int > &roots, const CVecRef< R > &params, const VecRef< R > &actions) override
Constructs residual for given roots provided their parameters and actions.
Definition: LinearEquationsDavidson.h:184
double get_augmented_hessian() const
Definition: LinearEquationsDavidson.h:134
CVecRef< Q > rhs() const override
Definition: LinearEquationsDavidson.h:94
void set_reset_D(size_t n)
Set the period in iterations for resetting the D space.
Definition: LinearEquationsDavidson.h:107
void report(std::ostream &cout, bool endl=true) const override
Writes a report to cout output stream.
Definition: LinearEquationsDavidson.h:171
bool get_hermiticity() const override
Gets hermiticity of kernel, if true than it is hermitian, otherwise it is not.
Definition: LinearEquationsDavidson.h:127
void report() const override
Writes a report to std::cout.
Definition: IterativeSolverTemplate.h:349
double get_svd_thresh() const
Definition: LinearEquationsDavidson.h:105
void set_reset_D_maxQ_size(size_t n)
Set the maximum size of Q space after resetting the D space.
Definition: LinearEquationsDavidson.h:110
int get_max_size_qspace() const
Definition: LinearEquationsDavidson.h:119
void set_max_size_qspace(int n)
Definition: LinearEquationsDavidson.h:114
Abstract class defining the problem-specific interface for the simplified solver interface to Iterati...
Definition: IterativeSolver.h:78
virtual bool RHS(R &RHS, unsigned int instance) const
Return the inhomogeneous part of a linear equation system.
Definition: IterativeSolver.h:148
Resets D space constructing full solutions as the new working set, removing instabilities from Q spac...
Definition: DSpaceResetter.h:69
auto propose_rspace(IterativeSolver< R, Q, P > &solver, const VecRef< R > &parameters, const VecRef< R > &residuals, subspace::IXSpace< R, Q, P > &xspace, subspace::ISubspaceSolver< R, Q, P > &subspace_solver, ArrayHandlers< R, Q, P > &handlers, Logger &logger, value_type_abs svd_thresh, value_type_abs res_norm_thresh, int max_size_qspace, molpro::profiler::Profiler &profiler)
Proposes new parameters for the subspace from the preconditioned residuals.
Definition: propose_rspace.h:509
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition: helper.h:11
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
auto wrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition: wrap.h:32
std::vector< std::reference_wrapper< const A > > CVecRef
Definition: wrap.h:14
std::vector< std::reference_wrapper< A > > VecRef
Definition: wrap.h:11
const std::shared_ptr< const molpro::Options > options()
Get the Options object associated with iterative-solver.
Definition: 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
Information about performance of IterativeSolver instance.
Definition: Statistics.h:10