1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_ITERATIVESOLVER_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_ITERATIVESOLVER_H
3#include <molpro/linalg/array/ArrayHandler.h>
4#include <molpro/linalg/array/Span.h>
5#include <molpro/linalg/itsolv/Options.h>
6#include <molpro/linalg/itsolv/Statistics.h>
7#include <molpro/linalg/itsolv/helper.h>
8#include <molpro/linalg/itsolv/subspace/Dimensions.h>
9#include <molpro/linalg/itsolv/wrap.h>
10#include <molpro/linalg/itsolv/Logger.h>
11#include <molpro/linalg/itsolv/Metric.h>
13#include <molpro/linalg/array/DistrArray.h>
14#include <molpro/linalg/array/util/Distribution.h>
30 constexpr static std::true_type
test(
typename C::iterator*);
33 constexpr static std::false_type
test(...);
42template <
typename T,
typename S,
43 typename = std::enable_if_t<std::is_base_of<molpro::linalg::array::DistrArray, T>::value>>
50 for (
size_t k = 0;
k < action.size();
k++) {
52 auto& distribution = action[
k].get().distribution();
53 auto range = distribution.range(molpro::mpi::rank_global());
54 for (
auto i = range.first;
i < range.second;
i++) {
55 const auto j =
i - range.first;
62template <
class T,
typename S>
65 typename T::iterator* =
nullptr
68 for (
size_t k = 0;
k < action.size();
k++) {
69 auto a = action[
k].get().begin();
70 auto m = metric_diagonals ? metric_diagonals->begin() :
decltype(diagonals.begin()){};
71 for (
auto d = diagonals.begin();
d != diagonals.end(); ++
d, ++
a) {
78template <
typename T,
typename S,
79 typename = std::enable_if_t<!std::is_base_of<molpro::linalg::array::DistrArray, T>::value>,
class =
void>
82 typename std::enable_if<!has_iterator<T>::value,
void*>::type =
nullptr
84 throw std::logic_error(
"Unimplemented preconditioner");
94template <
typename T,
typename S>
107template <
typename T,
typename S>
109 const T& metric_diagonals) {
124template <
typename R,
typename P = std::map<
size_t,
typename R::value_type>>
230 throw std::logic_error(
"Metric unavailable: unimplemented metric_action() in Problem class");
243 action(parameters, actions);
274 throw std::logic_error(
"P-space unavailable: unimplemented pp_action_matrix() in Problem class");
275 return std::vector<value_t>(0);
287 throw std::logic_error(
"P-space unavailable: unimplemented p_action() in Problem class");
297 throw std::logic_error(
"P-space unavailable: unimplemented pp_metric_matrix() in Problem class");
298 return std::vector<value_t>(0);
314 throw std::logic_error(
"P-space unavailable: unimplemented p_metric_action() in Problem class");
334template <
class R,
class P = std::map<
size_t,
typename R::value_type>>
360template <
class R,
class Q,
class P>
480 throw std::invalid_argument(
"This solver does not support solving a problem with a metric");
536 throw std::invalid_argument(
"This solver does not support a metric");
564 virtual int add_vector(std::vector<R>& parameters, std::vector<R>& action) = 0;
577 throw std::invalid_argument(
"This solver does not support a metric");
619 throw std::invalid_argument(
"This solver does not support a metric");
666 virtual void solution(
const std::vector<int>& roots, std::vector<R>& parameters, std::vector<R>& residual) = 0;
667 virtual void solution(R& parameters, R& residual) = 0;
668 virtual void solution_params(
const std::vector<int>& roots, std::vector<R>& parameters) = 0;
670 virtual size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) = 0;
680 return std::vector<scalar_type>(
working_set().size(), 0);
687 virtual const std::vector<value_type_abs>&
errors()
const = 0;
702 [[
deprecated(
"Set the verbosity on the logger directly")]]
741 virtual const std::shared_ptr<molpro::profiler::Profiler>&
profiler()
const = 0;
769template <
class R,
class Q,
class P>
781template <
class R,
class Q,
class P>
797template <
class R,
class Q,
class P>
801template <
class R,
class Q,
class P>
decltype(value_type_L{} *value_type_R{}) value_type
Definition ArrayHandler.h:182
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition Span.h:31
Base class defining the interface common to all iterative solvers.
Definition IterativeSolver.h:361
typename R::value_type value_type
The underlying type of elements of vectors.
Definition IterativeSolver.h:363
virtual void set_check_metric(bool check)
Sets whether add_p() and add_vector() check that the metric is Hermitian and, for the positive (semi)...
Definition IterativeSolver.h:547
virtual void set_profiler(molpro::profiler::Profiler &profiler)=0
Attach a profiler in order to collect performance data.
virtual void set_verbosity(int v)=0
virtual void report() const =0
Writes a report to std::cout.
virtual bool get_check_metric() const
Whether the metric is checked, see set_check_metric()
Definition IterativeSolver.h:549
IterativeSolver(const IterativeSolver< R, Q, P > &)=delete
virtual void finalize()=0
virtual int add_vector(R ¶meters, R &actions, R &metric_actions)
Like add_vector(const VecRef<R>&, const VecRef<R>&), for a problem with a metric declared with set_me...
Definition IterativeSolver.h:584
typename array::ArrayHandler< R, R >::value_type_abs value_type_abs
Definition IterativeSolver.h:366
virtual scalar_type value() const =0
Report the function value for the current optimum solution.
virtual size_t add_p(const CVecRef< P > &pparams, const array::Span< value_type > &pp_action_matrix, const VecRef< R > ¶meters, const VecRef< R > &action, fapply_on_p_type apply_p, const PSpaceMetric &metric)
Like add_p(const CVecRef.
Definition IterativeSolver.h:616
virtual size_t end_iteration(R ¶meters, R &action)=0
virtual void set_verbosity(Verbosity v)=0
virtual int add_vector(R ¶meters, R &action, value_type value=0)=0
virtual void set_convergence_threshold_value(value_type_abs thresh)=0
Sets the value convergence threshold.
virtual void report(std::ostream &cout, bool endl=true) const =0
Writes a report to cout output stream.
virtual std::vector< size_t > suggest_p(const CVecRef< R > &solution, const CVecRef< R > &residual, size_t max_number, value_type_abs threshold)=0
Get the solver's suggestion of which degrees of freedom would be best to add to the P-space.
IterativeSolver(IterativeSolver< R, Q, P > &&) noexcept=default
typename array::ArrayHandler< R, Q >::value_type scalar_type
Definition IterativeSolver.h:365
virtual void solution(const std::vector< int > &roots, const VecRef< R > ¶meters, const VecRef< R > &residual)=0
Construct solution and residual for a given set of roots.
virtual bool solve(const VecRef< R > ¶meters, const VecRef< R > &actions, const Problem< R > &problem, bool generate_initial_guess=false)=0
Simplified one-call solver.
virtual const subspace::Dimensions & dimensions() const =0
virtual void set_n_roots(size_t nroots)=0
bool solve(std::vector< R > ¶meters, std::vector< R > &actions, std::vector< R > &metric_actions, const Problem< R > &&problem, bool generate_initial_guess=false)=delete
A temporary problem is rejected, as the solver keeps references to the problem beyond solve()
virtual void solution_params(const std::vector< int > &roots, const VecRef< R > ¶meters)=0
Constructs parameters of selected roots; throws like solution()
virtual void set_convergence_threshold(value_type_abs thresh)=0
Sets the convergence threshold.
virtual bool test_problem(const Problem< R > &problem, R &v0, R &v1, int verbosity=0, value_type_abs threshold=1e-5) const =0
Test a supplied problem class.
std::function< void(const std::vector< VectorP > &, const CVecRef< P > &, const VecRef< R > &)> fapply_on_p_type
Definition IterativeSolver.h:372
virtual bool nonlinear() const =0
Report whether the class is a non-linear solver.
virtual void set_p_threshold(value_type_abs thresh)=0
virtual void set_max_p(int n)=0
virtual value_type_abs convergence_threshold() const =0
Reports the convergence threshold.
virtual int add_vector(const VecRef< R > ¶meters, const VecRef< R > &actions)=0
Take, typically, a current solution and residual, and add it to the solution space.
virtual value_type_abs get_p_threshold() const =0
virtual int add_vector(const VecRef< R > ¶meters, const VecRef< R > &actions, const VecRef< R > &metric_actions)
Like add_vector(const VecRef<R>&, const VecRef<R>&), for a problem with a metric declared with set_me...
Definition IterativeSolver.h:576
IterativeSolver()=default
virtual void set_options(const Options &options)=0
virtual int add_vector(std::vector< R > ¶meters, std::vector< R > &actions, std::vector< R > &metric_actions)
Like add_vector(const VecRef<R>&, const VecRef<R>&), for a problem with a metric declared with set_me...
Definition IterativeSolver.h:580
virtual void solution_params(R ¶meters)=0
std::vector< value_type > VectorP
Definition IterativeSolver.h:369
bool solve(const VecRef< R > ¶meters, const VecRef< R > &actions, const VecRef< R > &metric_actions, const Problem< R > &&problem, bool generate_initial_guess=false)=delete
A temporary problem is rejected, as the solver keeps references to the problem beyond solve()
std::function< void(const CVecRef< R > &, const VecRef< R > &)> fapply_metric_type
Function type for applying the metric to a set of vectors (first argument), writing the results into ...
Definition IterativeSolver.h:374
virtual const std::shared_ptr< molpro::profiler::Profiler > & profiler() const =0
virtual bool solve(R ¶meters, R &actions, R &metric_actions, const Problem< R > &problem, bool generate_initial_guess=false)
Simplified one-call solver for problems that may have a metric, e.g. the generalised eigenproblem A v...
Definition IterativeSolver.h:483
virtual int add_vector(std::vector< R > ¶meters, std::vector< R > &action)=0
virtual void set_max_iter(int n)=0
virtual size_t add_p(const CVecRef< P > &pparams, const array::Span< value_type > &pp_action_matrix, const VecRef< R > ¶meters, const VecRef< R > &action, fapply_on_p_type apply_p)=0
Add P-space vectors to the expansion set for linear methods.
virtual void solution(const std::vector< int > &roots, std::vector< R > ¶meters, std::vector< R > &residual)=0
virtual void set_profiler(std::shared_ptr< molpro::profiler::Profiler > profiler)=0
Attach a profiler in order to collect performance data.
virtual void solution(R ¶meters, R &residual)=0
virtual Verbosity get_verbosity() const =0
virtual ~IterativeSolver()=default
virtual MetricStorage metric_storage() const
How the actions of the metric declared with set_metric() are obtained.
Definition IterativeSolver.h:541
IterativeSolver< R, Q, P > & operator=(const IterativeSolver< R, Q, P > &)=delete
virtual std::vector< scalar_type > working_set_eigenvalues() const
Definition IterativeSolver.h:679
virtual MetricKind metric_kind() const
The kind of metric declared with set_metric()
Definition IterativeSolver.h:539
virtual bool end_iteration_needed()=0
signal whether end_iteration should be called
virtual size_t end_iteration(const VecRef< R > ¶meters, const VecRef< R > &residual)=0
Behaviour depends on the solver.
virtual Logger & logger()=0
virtual std::shared_ptr< Options > get_options() const =0
virtual void set_logger(std::shared_ptr< Logger > logger)=0
Set the logger instance that shall be used.
virtual int get_max_p() const =0
virtual void solution_params(const std::vector< int > &roots, std::vector< R > ¶meters)=0
virtual bool supports_metric(MetricKind kind) const
Whether the solver supports a metric of the given kind. MetricKind::Identity is always supported.
Definition IterativeSolver.h:505
virtual int get_max_iter() const =0
virtual value_type_abs convergence_threshold_value() const =0
Reports the value convergence threshold.
virtual bool solve(std::vector< R > ¶meters, std::vector< R > &actions, std::vector< R > &metric_actions, const Problem< R > &problem, bool generate_initial_guess=false)
Simplified one-call solver for problems that may have a metric, e.g. the generalised eigenproblem A v...
Definition IterativeSolver.h:488
virtual void set_metric(MetricKind kind, MetricStorage storage=MetricStorage::Stored, fapply_metric_type apply_metric={})
Declares the metric M of a generalised problem, e.g. the eigenproblem A v = lambda M v,...
Definition IterativeSolver.h:533
virtual const std::vector< value_type_abs > & errors() const =0
Errors are norms of the residuals, so they are real even when the problem is complex.
virtual const Statistics & statistics() const =0
virtual size_t end_iteration(std::vector< R > ¶meters, std::vector< R > &action)=0
bool solve(R ¶meters, R &actions, R &metric_actions, const Problem< R > &&problem, bool generate_initial_guess=false)=delete
A temporary problem is rejected, as the solver keeps references to the problem beyond solve()
virtual size_t n_roots() const =0
virtual const std::vector< int > & working_set() const =0
Working set of roots that are not yet converged.
Interface for a specific iterative solver, it can add special member functions or variables.
Definition IterativeSolver.h:770
virtual bool get_hermiticity() const =0
Gets hermiticity of kernel, if true than it is hermitian, otherwise it is not.
virtual void set_hermiticity(bool hermitian)=0
Sets hermiticity of kernel.
virtual std::vector< scalar_type > eigenvalues() const =0
The calculated eigenvalues of the subspace matrix.
Definition IterativeSolver.h:782
virtual void add_equations(const CVecRef< R > &rhs)=0
virtual void add_equations(const std::vector< R > &rhs)=0
virtual bool get_hermiticity() const =0
Gets hermiticity of kernel, if true than it is hermitian, otherwise it is not.
virtual CVecRef< Q > rhs() const =0
virtual void set_hermiticity(bool hermitian)=0
Sets hermiticity of kernel.
virtual void add_equations(const R &rhs)=0
Logger class implementation. Provides (customizable) logging functionalities.
Definition Logger.h:370
Solves non-linear system of equations using methods such as DIIS.
Definition IterativeSolver.h:802
Definition IterativeSolver.h:798
Abstract class defining the problem-specific interface for the simplified solver interface to Iterati...
Definition IterativeSolver.h:125
virtual MetricKind metric_kind() const
The kind of metric M of the problem. Anything other than MetricKind::Identity turns the eigenproblem ...
Definition IterativeSolver.h:211
virtual void action(const CVecRef< R > ¶meters, const VecRef< R > &action) const
Calculate the action of the kernel matrix on a set of parameters. Used by linear solvers,...
Definition IterativeSolver.h:149
R container_t
Definition IterativeSolver.h:129
virtual MetricStorage metric_storage() const
How IterativeSolver::solve() obtains the actions of the metric on its subspace vectors,...
Definition IterativeSolver.h:221
P p_container_t
Definition IterativeSolver.h:130
virtual void precondition(const VecRef< R > &residual, const std::vector< value_t > &shift) const
Apply preconditioning to a residual vector in order to predict a step towards the solution.
Definition IterativeSolver.h:175
typename R::value_type value_t
Definition IterativeSolver.h:131
virtual bool metric_diagonals(R &d) const
Provide the diagonal elements of the metric. For a problem with a metric that provides diagonals(),...
Definition IterativeSolver.h:256
virtual ~Problem()=default
virtual void p_metric_action(const std::vector< std::vector< value_t > > &p_coefficients, const CVecRef< P > &pparams, const VecRef< container_t > &metric_actions) const
Calculate the action of the metric on a set of vectors in the P space, for a problem with a metric (m...
Definition IterativeSolver.h:311
virtual bool diagonals(container_t &d) const
Optionally provide the diagonal elements of the underlying kernel. If implemented and returning true,...
Definition IterativeSolver.h:161
virtual void precondition_with_metric(const VecRef< R > &residual, const std::vector< value_t > &shift, const R &diagonals, const R &metric_diagonals) const
Apply preconditioning to a residual vector of a generalised eigenproblem. Used instead of preconditio...
Definition IterativeSolver.h:200
virtual void p_action(const std::vector< std::vector< value_t > > &p_coefficients, const CVecRef< P > &pparams, const VecRef< container_t > &actions) const
Calculate the action of the kernel matrix on a set of vectors in the P space.
Definition IterativeSolver.h:284
virtual std::vector< value_t > pp_action_matrix(const std::vector< P > &pparams) const
Calculate the kernel matrix in the P space.
Definition IterativeSolver.h:272
virtual void action_with_metric(const CVecRef< R > ¶meters, const VecRef< R > &actions, const VecRef< R > &metric_actions) const
Calculate the actions of the kernel and of the metric on a set of parameters together....
Definition IterativeSolver.h:241
virtual void metric_action(const CVecRef< R > ¶meters, const VecRef< R > &metric_actions) const
Calculate the action of the metric on a set of parameters. Called only if metric_kind() is not Metric...
Definition IterativeSolver.h:229
virtual value_t residual(const R ¶meters, R &residual) const
Calculate the residual vector. Used by non-linear solvers (NonLinearEquations, Optimize) only.
Definition IterativeSolver.h:141
virtual bool test_parameters(unsigned int instance, R ¶meters) const
Provide values of R vectors for testing the problem class. For use in a non-linear solver,...
Definition IterativeSolver.h:326
virtual bool RHS(R &RHS, unsigned int instance) const
Return the inhomogeneous part of a linear equation system.
Definition IterativeSolver.h:265
virtual void precondition(const VecRef< R > &residual, const std::vector< value_t > &shift, const R &diagonals) const
Apply preconditioning to a residual vector in order to predict a step towards the solution.
Definition IterativeSolver.h:187
virtual std::vector< value_t > pp_metric_matrix(const std::vector< P > &pparams) const
Calculate the metric matrix in the P space, for a problem with a metric (metric_kind())
Definition IterativeSolver.h:295
void precondition_default(const VecRef< T > &action, const std::vector< S > &shift, const T &diagonals, const T *metric_diagonals, const bool regularise)
Definition IterativeSolver.h:44
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition helper.h:14
auto wrap_arg(T &&arg, S &&... args) -> std::enable_if_t< std::conjunction_v< std::is_same< decay_t< T >, decay_t< S > >... >, VecRef< decay_t< T > > >
Constructs a vector of reference wrappers with provided arguments.
Definition wrap.h:135
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
void precondition_default(const VecRef< T > &action, const std::vector< S > &shift, const T &diagonals)
Davidson preconditioner: divides each action[k] elementwise by (diagonals - shift[k])
Definition IterativeSolver.h:95
MetricStorage
How a solver obtains the actions of a metric on its subspace vectors, see IterativeSolver::set_metric...
Definition Metric.h:21
@ Stored
the actions of the metric on all subspace vectors are kept
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
Verbosity
Specifies how much detail IterativeSolver::solve should write to output.
Definition Options.h:12
MetricKind
The kind of metric M of a generalised problem, such as the eigenproblem A v = lambda M v.
Definition Metric.h:13
@ Identity
M = 1, i.e. no metric.
const std::shared_ptr< const molpro::Options > options()
Get the Options object associated with iterative-solver.
Definition linalg_options.cpp:4
profiler::Profiler Profiler
Access point for different options in iterative solvers.
Definition Options.h:20
The metric of a problem in the P space, for IterativeSolver::add_p()
Definition IterativeSolver.h:335
array::Span< value_type > pp_matrix
the metric matrix in the P space, in the layout of the kernel matrix given to add_p(),...
Definition IterativeSolver.h:338
std::function< void(const std::vector< std::vector< value_type > > &, const CVecRef< P > &, const VecRef< R > &)> apply
Definition IterativeSolver.h:341
typename R::value_type value_type
Definition IterativeSolver.h:336
Information about performance of IterativeSolver instance.
Definition Statistics.h:10
Definition IterativeSolver.h:28
static constexpr bool value
Definition IterativeSolver.h:35
static constexpr std::true_type test(typename C::iterator *)
static constexpr std::false_type test(...)
Definition Dimensions.h:9