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/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#include <limits>
12
13namespace molpro::linalg::itsolv {
39template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
40class LinearEquationsDavidson : public IterativeSolverTemplate<LinearEquations, R, Q, P> {
41public:
44 using typename SolverTemplate::value_type;
45 using typename SolverTemplate::value_type_abs;
46
48 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
49 : SolverTemplate(std::make_shared<subspace::XSpace<R, Q, P>>(handlers, logger_),
50 std::static_pointer_cast<subspace::ISubspaceSolver<R, Q, P>>(
51 std::make_shared<subspace::SubspaceSolverLinEig<R, Q, P>>(logger_)),
54 this->m_normalise_solution = false;
55 }
56
57 bool solve(const VecRef<R>& parameters, const VecRef<R>& actions, const Problem<R>& problem,
58 bool generate_initial_guess = false) override {
59 R& vector = actions[0];
60 for (unsigned int instance = 0; problem.RHS(vector, instance); ++instance)
62 return IterativeSolverTemplate<LinearEquations, R, Q, P>::solve(parameters, actions, problem,
64 }
65
66 bool nonlinear() const override { return false; }
67
68 size_t end_iteration(const VecRef<R>& parameters, const VecRef<R>& action) override {
69 auto prof = this->profiler()->push("itsolv::end_iteration");
70 m_resetting_in_progress = m_dspace_resetter.do_reset(this->m_stats->iterations, this->m_xspace->dimensions());
72 this->m_working_set =
73 m_dspace_resetter.run(parameters, *this->m_xspace, this->m_subspace_solver->solutions(),
74 rspace_opts.norm_thresh, rspace_opts.svd_thresh, *this->m_handlers, *this->m_logger);
75 } else {
76 this->m_working_set =
77 detail::propose_rspace(*this, parameters, action, *this->m_xspace, *this->m_subspace_solver,
78 *this->m_handlers, *this->m_logger, rspace_opts, qspace_opts, *this->profiler());
79 }
80 this->m_stats->iterations++;
81 this->m_end_iteration_needed = false;
82 return this->working_set().size();
83 }
84
85 // FIXME move this to the template
86 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) override {
87 return end_iteration(wrap(parameters), wrap(action));
88 }
89 size_t end_iteration(R& parameters, R& actions) override {
90 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
91 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
93 }
94
95 void add_equations(const CVecRef<R>& rhs) override {
96 auto prof = this->profiler()->push("itsolv::add_equations");
97 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
98 xspace->add_rhs_equations(rhs);
99 this->set_n_roots(xspace->dimensions().nRHS);
100 }
101
102 void add_equations(const R& rhs) override { add_equations(cwrap_arg(rhs)); }
103 void add_equations(const std::vector<R>& rhs) override { add_equations(cwrap(rhs)); }
104
105 CVecRef<Q> rhs() const override {
106 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
107 return xspace->rhs();
108 }
109
111 void set_norm_thresh(value_type_abs thresh) { rspace_opts.norm_thresh = thresh; }
112 value_type_abs get_norm_thresh() const { return rspace_opts.norm_thresh; }
115 void set_svd_thresh(value_type_abs thresh) { rspace_opts.svd_thresh = thresh; }
116 value_type_abs get_svd_thresh() const { return rspace_opts.svd_thresh; }
118 void set_reset_D(size_t n) { m_dspace_resetter.set_nreset(n); }
119 size_t get_reset_D() const { return m_dspace_resetter.get_nreset(); }
121 void set_reset_D_maxQ_size(size_t n) { m_dspace_resetter.set_max_Qsize(n); }
122 int get_reset_D_maxQ_size() const { return m_dspace_resetter.get_max_Qsize(); }
125 void set_max_size_qspace(std::size_t n) {
126 qspace_opts.max_size = n;
127 if (m_dspace_resetter.get_max_Qsize() > qspace_opts.max_size)
128 m_dspace_resetter.set_max_Qsize(qspace_opts.max_size);
129 }
130 std::size_t get_max_size_qspace() const { return qspace_opts.max_size; }
131 void set_min_size_qspace(std::size_t n) {
132 qspace_opts.min_size = n;
133 }
134 std::size_t get_min_size_qspace() const { return qspace_opts.min_size; }
135 void set_hermiticity(bool hermitian) override {
137 auto xspace = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace);
138 xspace->set_hermiticity(hermitian);
139 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
140 subspace_solver->set_hermiticity(hermitian);
141 }
142 bool get_hermiticity() const override { return m_hermiticity; }
144 void set_augmented_hessian(const value_type_abs parameter) {
145 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
146 subspace_solver->set_augmented_hessian(parameter);
147 }
149 value_type_abs get_augmented_hessian() const {
150 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->m_subspace_solver);
151 return subspace_solver->get_augmented_hessian();
152 }
153
154 void set_options(const Options& options) override {
157 if (opt.reset_D)
158 set_reset_D(opt.reset_D.value());
159 if (opt.reset_D_max_Q_size)
160 set_reset_D_maxQ_size(opt.reset_D_max_Q_size.value());
161 if (opt.max_size_qspace)
162 set_max_size_qspace(opt.max_size_qspace.value());
163 if (opt.min_size_qspace)
164 set_min_size_qspace(opt.min_size_qspace.value());
165 if (opt.contrib_thresh)
166 qspace_opts.contrib_thresh = opt.contrib_thresh.value();
167 if (opt.norm_thresh)
168 set_norm_thresh(opt.norm_thresh.value());
169 if (opt.svd_thresh)
170 set_svd_thresh(opt.svd_thresh.value());
171 if (opt.hermiticity)
172 set_hermiticity(opt.hermiticity.value());
173 if (opt.augmented_hessian)
174 set_augmented_hessian(opt.augmented_hessian.value());
175 }
176
177 std::shared_ptr<Options> get_options() const override {
178 auto opt = std::make_shared<LinearEquationsDavidsonOptions>();
180 opt->reset_D = get_reset_D();
181 opt->reset_D_max_Q_size = get_reset_D_maxQ_size();
182 opt->max_size_qspace = get_max_size_qspace();
183 opt->min_size_qspace = get_min_size_qspace();
184 opt->contrib_thresh = qspace_opts.contrib_thresh;
185 opt->norm_thresh = get_norm_thresh();
186 opt->svd_thresh = get_svd_thresh();
187 opt->hermiticity = get_hermiticity();
188 opt->augmented_hessian = get_augmented_hessian();
189 return opt;
190 }
191
192 void report(std::ostream& cout, bool endl = true) const override {
194 cout << ", errors " << std::scientific;
195 auto& err = this->m_errors;
196 std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout, ", "));
197 cout << std::defaultfloat;
198 if (endl)
199 cout << std::endl;
200 }
201
202protected:
204 std::vector<value_type> values() const {
205 if (get_augmented_hessian() > 0)
206 return this->m_subspace_solver->eigenvalues();
207 const auto& solutions = this->m_subspace_solver->solutions(); // c(i, k): root i, subspace vector k
208 const auto& rhs = this->m_xspace->data.at(subspace::EqnData::rhs); // <v_k|b_i>
209 auto chi = std::vector<value_type>(std::min(solutions.rows(), rhs.cols()), value_type{0});
210 for (size_t i = 0; i < chi.size(); ++i)
211 for (size_t k = 0; k < std::min(solutions.cols(), rhs.rows()); ++k)
212 chi[i] += solutions(i, k) * molpro::linalg::conjugate(rhs(k, i));
213 return chi;
214 }
215
216 void set_value_errors() override {
217 const auto current_values = values();
218 this->m_value_errors.assign(current_values.size(), std::numeric_limits<value_type_abs>::max());
219 for (size_t i = 0; i < std::min(m_last_values.size(), current_values.size()); i++)
220 this->m_value_errors[i] = std::abs(current_values[i] - m_last_values[i]);
221 // resetting the D space does not change the solutions, so the change measured across it would be meaningless
224 }
225
226 // 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
227 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions) override {
228 assert(params.size() >= roots.size());
229 const auto& norm = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->m_xspace)->rhs_norm();
230 // With the augmented Hessian the subspace solution satisfies (H - lambda) x = rhs, with lambda the subspace
231 // eigenvalue; without it lambda is zero
232 const auto& eigenvalues = this->m_subspace_solver->eigenvalues();
233 for (size_t i = 0; i < roots.size(); ++i) {
234 const auto ii = roots[i];
235 this->m_handlers->rq().axpy(-1, rhs().at(ii), actions.at(i));
236 if (size_t(ii) < eigenvalues.size() and eigenvalues[ii] != value_type(0))
237 this->m_handlers->rr().axpy(-eigenvalues[ii], params.at(i), actions.at(i));
238 if (norm.at(ii) != 0) {
239 auto scal = 1 / norm[ii];
240 this->m_handlers->rr().scal(scal, actions.at(i));
241 }
242 }
243 }
244
248 bool m_hermiticity = true;
249 std::vector<value_type> m_last_values;
251};
252
253} // namespace molpro::linalg::itsolv
254#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< value_type_abs > m_value_errors
value errors from the most recent solution
Definition IterativeSolverTemplate.h:697
std::vector< int > m_working_set
indices of roots in the working set
Definition IterativeSolverTemplate.h:698
std::shared_ptr< subspace::IXSpace< R, Q, P > > m_xspace
manages the subspace and associated data
Definition IterativeSolverTemplate.h:694
void set_n_roots(size_t roots) override
Definition IterativeSolverTemplate.h:290
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Array handlers.
Definition IterativeSolverTemplate.h:693
std::shared_ptr< subspace::ISubspaceSolver< R, Q, P > > m_subspace_solver
solves the subspace problem
Definition IterativeSolverTemplate.h:695
std::vector< value_type_abs > m_errors
errors from the most recent solution
Definition IterativeSolverTemplate.h:696
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:715
void report() const override
Definition IterativeSolverTemplate.h:352
bool m_normalise_solution
whether to normalise the solutions
Definition IterativeSolverTemplate.h:705
const std::vector< int > & working_set() const override
Definition IterativeSolverTemplate.h:286
std::shared_ptr< Logger > m_logger
logger
Definition IterativeSolverTemplate.h:704
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:703
Solves a system of linear equation, A x = b.
Definition LinearEquationsDavidson.h:40
size_t get_reset_D() const
Definition LinearEquationsDavidson.h:119
detail::DSpaceResetter< Q > m_dspace_resetter
resets D space
Definition LinearEquationsDavidson.h:247
value_type_abs get_svd_thresh() const
Definition LinearEquationsDavidson.h:116
bool solve(const VecRef< R > &parameters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false) override
Definition LinearEquationsDavidson.h:57
void add_equations(const std::vector< R > &rhs) override
Definition LinearEquationsDavidson.h:103
void set_hermiticity(bool hermitian) override
Definition LinearEquationsDavidson.h:135
size_t end_iteration(const VecRef< R > &parameters, const VecRef< R > &action) override
Definition LinearEquationsDavidson.h:68
std::size_t get_min_size_qspace() const
Definition LinearEquationsDavidson.h:134
RSpaceOptions< value_type_abs > rspace_opts
Options concerning R-space handling.
Definition LinearEquationsDavidson.h:245
std::size_t get_max_size_qspace() const
Definition LinearEquationsDavidson.h:130
bool m_resetting_in_progress
whether D space resetting is in progress
Definition LinearEquationsDavidson.h:250
size_t end_iteration(R &parameters, R &actions) override
Definition LinearEquationsDavidson.h:89
void set_norm_thresh(value_type_abs thresh)
Set threshold on the norm of parameters that should be considered null.
Definition LinearEquationsDavidson.h:111
void set_min_size_qspace(std::size_t n)
Definition LinearEquationsDavidson.h:131
void add_equations(const CVecRef< R > &rhs) override
Definition LinearEquationsDavidson.h:95
void set_svd_thresh(value_type_abs thresh)
Definition LinearEquationsDavidson.h:115
std::shared_ptr< Options > get_options() const override
Definition LinearEquationsDavidson.h:177
LinearEquationsDavidson(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition LinearEquationsDavidson.h:47
void add_equations(const R &rhs) override
Definition LinearEquationsDavidson.h:102
int get_reset_D_maxQ_size() const
Definition LinearEquationsDavidson.h:122
QSpaceOptions qspace_opts
Options concerning Q-space handling.
Definition LinearEquationsDavidson.h:246
void set_max_size_qspace(std::size_t n)
Definition LinearEquationsDavidson.h:125
bool nonlinear() const override
Definition LinearEquationsDavidson.h:66
size_t end_iteration(std::vector< R > &parameters, std::vector< R > &action) override
Definition LinearEquationsDavidson.h:86
void set_options(const Options &options) override
Definition LinearEquationsDavidson.h:154
bool m_hermiticity
whether the problem is hermitian or not
Definition LinearEquationsDavidson.h:248
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:227
std::vector< value_type > m_last_values
the values from the previous iteration
Definition LinearEquationsDavidson.h:249
CVecRef< Q > rhs() const override
Definition LinearEquationsDavidson.h:105
value_type_abs get_norm_thresh() const
Definition LinearEquationsDavidson.h:112
void set_reset_D(size_t n)
Set the period in iterations for resetting the D space.
Definition LinearEquationsDavidson.h:118
void report(std::ostream &cout, bool endl=true) const override
Definition LinearEquationsDavidson.h:192
bool get_hermiticity() const override
Definition LinearEquationsDavidson.h:142
void set_value_errors() override
Implementation class should overload this to set errors in the current values (e.g....
Definition LinearEquationsDavidson.h:216
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:144
std::vector< value_type > values() const
The values chi_ii = <b_i|x_i> of the current solutions, or with the augmented Hessian the subspace ei...
Definition LinearEquationsDavidson.h:204
void set_reset_D_maxQ_size(size_t n)
Set the maximum size of Q space after resetting the D space.
Definition LinearEquationsDavidson.h:121
value_type_abs get_augmented_hessian() const
Definition LinearEquationsDavidson.h:149
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:582
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
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
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
Information about performance of IterativeSolver instance.
Definition Statistics.h:10