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/qspace_options.h>
8#include <molpro/linalg/itsolv/rspace_options.h>
9#include <molpro/linalg/itsolv/subspace/SubspaceSolverLinEig.h>
10#include <molpro/linalg/itsolv/subspace/XSpace.h>
11
12namespace molpro::linalg::itsolv {
31template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
32class LinearEquationsDavidson : public IterativeSolverTemplate<LinearEquations, R, Q, P> {
33public:
36 using typename SolverTemplate::value_type;
37 using typename SolverTemplate::value_type_abs;
38
39 explicit LinearEquationsDavidson(const std::shared_ptr<ArrayHandlers<R, Q, P>>& handlers,
40 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
41 : SolverTemplate(std::make_shared<subspace::XSpace<R, Q, P>>(handlers, logger_),
42 std::static_pointer_cast<subspace::ISubspaceSolver<R, Q, P>>(
43 std::make_shared<subspace::SubspaceSolverLinEig<R, Q, P>>(logger_)),
44 handlers, std::make_shared<Statistics>(), logger_) {
46 this->m_normalise_solution = false;
47 }
48
49 bool solve(const VecRef<R>& parameters, const VecRef<R>& actions, const Problem<R>& problem,
50 bool generate_initial_guess = false) override {
51 R& vector = actions[0];
52 for (unsigned int instance = 0; problem.RHS(vector, instance); ++instance)
53 add_equations(vector);
54 return IterativeSolverTemplate<LinearEquations, R, Q, P>::solve(parameters, actions, problem,
55 generate_initial_guess);
56 }
57
58 bool nonlinear() const override { return false; }
59
60 size_t end_iteration(const VecRef<R>& parameters, const VecRef<R>& action) override {
61 auto prof = this->profiler()->push("itsolv::end_iteration");
62 if (m_dspace_resetter.do_reset(this->m_stats->iterations, this->m_xspace->dimensions())) {
63 this->m_working_set =
64 m_dspace_resetter.run(parameters, *this->m_xspace, this->m_subspace_solver->solutions(),
65 rspace_opts.norm_thresh, rspace_opts.svd_thresh, *this->m_handlers, *this->m_logger);
66 } else {
67 this->m_working_set =
68 detail::propose_rspace(*this, parameters, action, *this->m_xspace, *this->m_subspace_solver,
69 *this->m_handlers, *this->m_logger, rspace_opts, qspace_opts, *this->profiler());
70 }
71 this->m_stats->iterations++;
72 this->m_end_iteration_needed = false;
73 return this->working_set().size();
74 }
75
76 // FIXME move this to the template
77 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) override {
78 return end_iteration(wrap(parameters), wrap(action));
79 }
80 size_t end_iteration(R& parameters, R& actions) override {
81 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
82 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
83 return end_iteration(wparams, wactions);
84 }
85
86 void add_equations(const CVecRef<R>& rhs) override {
87 auto prof = this->profiler()->push("itsolv::add_equations");
88 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
89 xspace->add_rhs_equations(rhs);
90 this->set_n_roots(xspace->dimensions().nRHS);
91 }
92
93 void add_equations(const R& rhs) override { add_equations(cwrap_arg(rhs)); }
94 void add_equations(const std::vector<R>& rhs) override { add_equations(cwrap(rhs)); }
95
96 CVecRef<Q> rhs() const override {
97 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
98 return xspace->rhs();
99 }
100
102 void set_norm_thresh(value_type_abs thresh) { rspace_opts.norm_thresh = thresh; }
103 value_type_abs get_norm_thresh() const { return rspace_opts.norm_thresh; }
106 void set_svd_thresh(value_type_abs thresh) { rspace_opts.svd_thresh = thresh; }
107 value_type_abs get_svd_thresh() const { return rspace_opts.svd_thresh; }
109 void set_reset_D(size_t n) { m_dspace_resetter.set_nreset(n); }
110 size_t get_reset_D() const { return m_dspace_resetter.get_nreset(); }
112 void set_reset_D_maxQ_size(size_t n) { m_dspace_resetter.set_max_Qsize(n); }
113 int get_reset_D_maxQ_size() const { return m_dspace_resetter.get_max_Qsize(); }
116 void set_max_size_qspace(std::size_t n) {
117 qspace_opts.max_size = n;
118 if (m_dspace_resetter.get_max_Qsize() > qspace_opts.max_size)
119 m_dspace_resetter.set_max_Qsize(qspace_opts.max_size);
120 }
121 std::size_t get_max_size_qspace() const { return qspace_opts.max_size; }
122 void set_min_size_qspace(std::size_t n) {
123 qspace_opts.min_size = n;
124 }
125 std::size_t get_min_size_qspace() const { return qspace_opts.min_size; }
126 void set_hermiticity(bool hermitian) override {
127 m_hermiticity = hermitian;
128 auto xspace = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
129 xspace->set_hermiticity(hermitian);
130 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
131 subspace_solver->set_hermiticity(hermitian);
132 }
133 bool get_hermiticity() const override { return m_hermiticity; }
135 void set_augmented_hessian(const value_type_abs parameter) {
136 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
137 subspace_solver->set_augmented_hessian(parameter);
138 }
141 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
142 return subspace_solver->get_augmented_hessian();
143 }
144
145 void set_options(const Options& options) override {
148 if (opt.reset_D)
149 set_reset_D(opt.reset_D.value());
150 if (opt.reset_D_max_Q_size)
151 set_reset_D_maxQ_size(opt.reset_D_max_Q_size.value());
152 if (opt.max_size_qspace)
153 set_max_size_qspace(opt.max_size_qspace.value());
154 if (opt.min_size_qspace)
155 set_min_size_qspace(opt.min_size_qspace.value());
156 if (opt.contrib_thresh)
157 qspace_opts.contrib_thresh = opt.contrib_thresh.value();
158 if (opt.norm_thresh)
159 set_norm_thresh(opt.norm_thresh.value());
160 if (opt.svd_thresh)
161 set_svd_thresh(opt.svd_thresh.value());
162 if (opt.hermiticity)
163 set_hermiticity(opt.hermiticity.value());
164 if (opt.augmented_hessian)
165 set_augmented_hessian(opt.augmented_hessian.value());
166 }
167
168 std::shared_ptr<Options> get_options() const override {
169 auto opt = std::make_shared<LinearEquationsDavidsonOptions>();
170 opt->copy(*SolverTemplate::get_options());
171 opt->reset_D = get_reset_D();
172 opt->reset_D_max_Q_size = get_reset_D_maxQ_size();
173 opt->max_size_qspace = get_max_size_qspace();
174 opt->min_size_qspace = get_min_size_qspace();
175 opt->contrib_thresh = qspace_opts.contrib_thresh;
176 opt->norm_thresh = get_norm_thresh();
177 opt->svd_thresh = get_svd_thresh();
178 opt->hermiticity = get_hermiticity();
179 opt->augmented_hessian = get_augmented_hessian();
180 return opt;
181 }
182
183 void report(std::ostream& cout, bool endl = true) const override {
184 SolverTemplate::report(cout, false);
185 cout << ", errors " << std::scientific;
186 auto& err = this->m_errors;
187 std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout, ", "));
188 cout << std::defaultfloat;
189 if (endl)
190 cout << std::endl;
191 }
192
193protected:
194 // 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
195 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions) override {
196 assert(params.size() >= roots.size());
197 const auto& norm = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace)->rhs_norm();
198 for (size_t i = 0; i < roots.size(); ++i) {
199 const auto ii = roots[i];
200 this->m_handlers->rq().axpy(-1, rhs().at(ii), actions.at(i));
201 if (norm.at(ii) != 0) {
202 auto scal = 1 / norm[ii];
203 this->m_handlers->rr().scal(scal, actions.at(i));
204 }
205 }
206 }
207
208 RSpaceOptions<value_type_abs> rspace_opts;
209 QSpaceOptions qspace_opts;
211 bool m_hermiticity = true;
212};
213
214} // namespace molpro::linalg::itsolv
215#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:163
std::vector< int > m_working_set
indices of roots in the working set
Definition: IterativeSolverTemplate.h:686
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:682
std::shared_ptr< ArrayHandlers< R, R, std::map< size_t, typename R::value_type > > > m_handlers
Array handlers.
Definition: IterativeSolverTemplate.h:681
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:683
std::vector< value_type_abs > m_errors
errors from the most recent solution
Definition: IterativeSolverTemplate.h:684
std::shared_ptr< Options > get_options() const override
Definition: IterativeSolverTemplate.h:311
const std::shared_ptr< molpro::profiler::Profiler > & profiler() const override
Definition: IterativeSolverTemplate.h:397
bool m_end_iteration_needed
whether end_iteration should be called after any preconditioner
Definition: IterativeSolverTemplate.h:703
void report() const override
Definition: IterativeSolverTemplate.h:352
bool m_normalise_solution
whether to normalise the solutions
Definition: IterativeSolverTemplate.h:693
const std::vector< int > & working_set() const override
Working set of roots that are not yet converged.
Definition: IterativeSolverTemplate.h:286
void set_options(const Options &options) override
Definition: IterativeSolverTemplate.h:296
bool solve(const VecRef< R > &parameters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false) override
Definition: IterativeSolverTemplate.h:399
std::shared_ptr< Statistics > m_stats
accumulates statistics of operations performed by the solver
Definition: IterativeSolverTemplate.h:691
typename array::ArrayHandler< R, R >::value_type_abs value_type_abs
Definition: IterativeSolver.h:214
Solves a system of linear equation, A x = b.
Definition: LinearEquationsDavidson.h:32
size_t get_reset_D() const
Definition: LinearEquationsDavidson.h:110
detail::DSpaceResetter< Q > m_dspace_resetter
resets D space
Definition: LinearEquationsDavidson.h:210
value_type_abs get_svd_thresh() const
Definition: LinearEquationsDavidson.h:107
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:49
void add_equations(const std::vector< R > &rhs) override
Definition: LinearEquationsDavidson.h:94
void set_hermiticity(bool hermitian) override
Sets hermiticity of kernel.
Definition: LinearEquationsDavidson.h:126
size_t end_iteration(const VecRef< R > &parameters, const VecRef< R > &action) override
Behaviour depends on the solver.
Definition: LinearEquationsDavidson.h:60
std::size_t get_min_size_qspace() const
Definition: LinearEquationsDavidson.h:125
RSpaceOptions< value_type_abs > rspace_opts
Options concerning R-space handling.
Definition: LinearEquationsDavidson.h:208
std::size_t get_max_size_qspace() const
Definition: LinearEquationsDavidson.h:121
size_t end_iteration(R &parameters, R &actions) override
Definition: LinearEquationsDavidson.h:80
void set_norm_thresh(value_type_abs thresh)
Set threshold on the norm of parameters that should be considered null.
Definition: LinearEquationsDavidson.h:102
void set_min_size_qspace(std::size_t n)
Definition: LinearEquationsDavidson.h:122
void add_equations(const CVecRef< R > &rhs) override
Definition: LinearEquationsDavidson.h:86
void set_svd_thresh(value_type_abs thresh)
Definition: LinearEquationsDavidson.h:106
std::shared_ptr< Options > get_options() const override
Definition: LinearEquationsDavidson.h:168
LinearEquationsDavidson(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition: LinearEquationsDavidson.h:39
void add_equations(const R &rhs) override
Definition: LinearEquationsDavidson.h:93
int get_reset_D_maxQ_size() const
Definition: LinearEquationsDavidson.h:113
QSpaceOptions qspace_opts
Options concerning Q-space handling.
Definition: LinearEquationsDavidson.h:209
void set_max_size_qspace(std::size_t n)
Definition: LinearEquationsDavidson.h:116
bool nonlinear() const override
Report whether the class is a non-linear solver.
Definition: LinearEquationsDavidson.h:58
size_t end_iteration(std::vector< R > &parameters, std::vector< R > &action) override
Definition: LinearEquationsDavidson.h:77
void set_options(const Options &options) override
Definition: LinearEquationsDavidson.h:145
bool m_hermiticity
whether the problem is hermitian or not
Definition: LinearEquationsDavidson.h:211
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:195
CVecRef< Q > rhs() const override
Definition: LinearEquationsDavidson.h:96
value_type_abs get_norm_thresh() const
Definition: LinearEquationsDavidson.h:103
void set_reset_D(size_t n)
Set the period in iterations for resetting the D space.
Definition: LinearEquationsDavidson.h:109
void report(std::ostream &cout, bool endl=true) const override
Writes a report to cout output stream.
Definition: LinearEquationsDavidson.h:183
bool get_hermiticity() const override
Gets hermiticity of kernel, if true than it is hermitian, otherwise it is not.
Definition: LinearEquationsDavidson.h:133
void report() const override
Writes a report to std::cout.
Definition: IterativeSolverTemplate.h:352
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:135
void set_reset_D_maxQ_size(size_t n)
Set the maximum size of Q space after resetting the D space.
Definition: LinearEquationsDavidson.h:112
value_type_abs get_augmented_hessian() const
Definition: LinearEquationsDavidson.h:140
Abstract class defining the problem-specific interface for the simplified solver interface to Iterati...
Definition: IterativeSolver.h:85
virtual bool RHS(R &RHS, unsigned int instance) const
Return the inhomogeneous part of a linear equation system.
Definition: IterativeSolver.h:155
Resets D space constructing full solutions as the new working set, removing instabilities from Q spac...
Definition: DSpaceResetter.h:74
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, const RSpaceOptions< typename array::ArrayHandler< R, R >::value_type_abs > &r_opts, const QSpaceOptions &q_opts, molpro::profiler::Profiler &profiler)
Proposes new parameters for the subspace from the preconditioned residuals.
Definition: propose_rspace.h:554
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
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: 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
Information about performance of IterativeSolver instance.
Definition: Statistics.h:10