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>
31template <
class R,
class Q = R,
class P = std::map<
size_t,
typename R::value_type>>
36 using typename SolverTemplate::value_type;
37 using typename SolverTemplate::value_type_abs;
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_),
51 bool generate_initial_guess =
false)
override {
52 R& vector = actions[0];
53 for (
unsigned int instance = 0; problem.
RHS(vector, instance); ++instance)
56 generate_initial_guess);
62 auto prof = this->
profiler()->push(
"itsolv::end_iteration");
63 if (
m_dspace_resetter.do_reset(this->m_stats->iterations, this->m_xspace->dimensions())) {
78 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action)
override {
82 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
83 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
88 auto prof = this->
profiler()->push(
"itsolv::add_equations");
89 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->
m_xspace);
90 xspace->add_rhs_equations(
rhs);
98 auto xspace = std::static_pointer_cast<subspace::XSpace<R, Q, P>>(this->
m_xspace);
129 auto xspace = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->
m_xspace);
130 xspace->set_hermiticity(hermitian);
131 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->
m_subspace_solver);
132 subspace_solver->set_hermiticity(hermitian);
137 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->
m_subspace_solver);
138 subspace_solver->set_augmented_hessian(parameter);
142 auto subspace_solver = std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->
m_subspace_solver);
143 return subspace_solver->get_augmented_hessian();
151 if (opt.reset_D_max_Q_size)
153 if (opt.max_size_qspace)
155 if (opt.min_size_qspace)
157 if (opt.contrib_thresh)
158 qspace_opts.contrib_thresh = opt.contrib_thresh.value();
165 if (opt.augmented_hessian)
170 auto opt = std::make_shared<LinearEquationsDavidsonOptions>();
184 void report(std::ostream& cout,
bool endl =
true)
const override {
186 cout <<
", errors " << std::scientific;
188 std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout,
", "));
189 cout << std::defaultfloat;
198 assert(params.size() >= roots.size());
199 const auto& norm = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->
m_xspace)->rhs_norm();
200 for (
size_t i = 0; i < roots.size(); ++i) {
201 const auto ii = roots[i];
202 this->
m_handlers->rq().axpy(-1,
rhs().at(ii), actions.at(i));
203 if (norm.at(ii) != 0) {
204 auto scal = 1 / norm[ii];
205 this->
m_handlers->rr().scal(scal, actions.at(i));
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
void set_n_roots(size_t roots) override
Definition: IterativeSolverTemplate.h:287
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
std::shared_ptr< Logger > m_logger
logger
Definition: IterativeSolverTemplate.h:682
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 > ¶meters, 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:32
size_t get_reset_D() const
Definition: LinearEquationsDavidson.h:111
detail::DSpaceResetter< Q > m_dspace_resetter
resets D space
Definition: LinearEquationsDavidson.h:212
bool solve(const VecRef< R > ¶meters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false) override
Simplified one-call solver.
Definition: LinearEquationsDavidson.h:50
std::shared_ptr< Logger > logger
Definition: LinearEquationsDavidson.h:193
void add_equations(const std::vector< R > &rhs) override
Definition: LinearEquationsDavidson.h:95
void set_hermiticity(bool hermitian) override
Sets hermiticity of kernel.
Definition: LinearEquationsDavidson.h:127
void set_augmented_hessian(const double parameter)
Set value of augmented hessian parameter. If 0, than augmented Hessian is not used.
Definition: LinearEquationsDavidson.h:136
void set_svd_thresh(double thresh)
Definition: LinearEquationsDavidson.h:107
size_t end_iteration(const VecRef< R > ¶meters, const VecRef< R > &action) override
Behaviour depends on the solver.
Definition: LinearEquationsDavidson.h:61
double get_norm_thresh() const
Definition: LinearEquationsDavidson.h:104
std::size_t get_min_size_qspace() const
Definition: LinearEquationsDavidson.h:126
std::size_t get_max_size_qspace() const
Definition: LinearEquationsDavidson.h:122
size_t end_iteration(R ¶meters, R &actions) override
Definition: LinearEquationsDavidson.h:81
void set_min_size_qspace(std::size_t n)
Definition: LinearEquationsDavidson.h:123
void add_equations(const CVecRef< R > &rhs) override
Definition: LinearEquationsDavidson.h:87
std::shared_ptr< Options > get_options() const override
Definition: LinearEquationsDavidson.h:169
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 set_norm_thresh(double thresh)
Set threshold on the norm of parameters that should be considered null.
Definition: LinearEquationsDavidson.h:103
void add_equations(const R &rhs) override
Definition: LinearEquationsDavidson.h:94
int get_reset_D_maxQ_size() const
Definition: LinearEquationsDavidson.h:114
QSpaceOptions qspace_opts
Options concerning Q-space handling.
Definition: LinearEquationsDavidson.h:211
void set_max_size_qspace(std::size_t n)
Definition: LinearEquationsDavidson.h:117
bool nonlinear() const override
Report whether the class is a non-linear solver.
Definition: LinearEquationsDavidson.h:59
size_t end_iteration(std::vector< R > ¶meters, std::vector< R > &action) override
Definition: LinearEquationsDavidson.h:78
void set_options(const Options &options) override
Definition: LinearEquationsDavidson.h:146
bool m_hermiticity
whether the problem is hermitian or not
Definition: LinearEquationsDavidson.h:213
void construct_residual(const std::vector< int > &roots, const CVecRef< R > ¶ms, const VecRef< R > &actions) override
Constructs residual for given roots provided their parameters and actions.
Definition: LinearEquationsDavidson.h:197
double get_augmented_hessian() const
Definition: LinearEquationsDavidson.h:141
CVecRef< Q > rhs() const override
Definition: LinearEquationsDavidson.h:97
void set_reset_D(size_t n)
Set the period in iterations for resetting the D space.
Definition: LinearEquationsDavidson.h:110
void report(std::ostream &cout, bool endl=true) const override
Writes a report to cout output stream.
Definition: LinearEquationsDavidson.h:184
bool get_hermiticity() const override
Gets hermiticity of kernel, if true than it is hermitian, otherwise it is not.
Definition: LinearEquationsDavidson.h:134
RSpaceOptions rspace_opts
Options concerning R-space handling.
Definition: LinearEquationsDavidson.h:210
void report() const override
Writes a report to std::cout.
Definition: IterativeSolverTemplate.h:349
double get_svd_thresh() const
Definition: LinearEquationsDavidson.h:108
void set_reset_D_maxQ_size(size_t n)
Set the maximum size of Q space after resetting the D space.
Definition: LinearEquationsDavidson.h:113
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:74
auto propose_rspace(IterativeSolver< R, Q, P > &solver, const VecRef< R > ¶meters, 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 &r_opts, const QSpaceOptions &q_opts, molpro::profiler::Profiler &profiler)
Proposes new parameters for the subspace from the preconditioned residuals.
Definition: propose_rspace.h:552
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: 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