1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_LINEAREIGENSYSTEMDAVIDSON_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_LINEAREIGENSYSTEMDAVIDSON_H
3#include <molpro/Profiler.h>
4#include <molpro/linalg/itsolv/CastOptions.h>
5#include <molpro/linalg/itsolv/DavidsonCommon.h>
6#include <molpro/linalg/itsolv/IterativeSolverTemplate.h>
7#include <molpro/linalg/itsolv/Logger.h>
8#include <molpro/linalg/itsolv/helper.h>
9#include <molpro/linalg/itsolv/subspace/SubspaceSolverLinEig.h>
10#include <molpro/linalg/itsolv/subspace/XSpace.h>
30template <
typename Scalar>
32 static constexpr std::string_view
name =
"ComplexRootsDavidson";
35 std::span<const std::pair<std::size_t, Scalar>>
roots;
39 std::vector<std::pair<std::size_t, Scalar>>
one_based(
p.roots.begin(),
p.roots.end());
59template <
class R,
class Q = R,
class P = std::map<
size_t,
typename R::value_type>>
64 using typename SolverTemplate::scalar_type;
65 using typename SolverTemplate::value_type;
66 using typename SolverTemplate::value_type_abs;
70 const std::shared_ptr<Logger>&
logger_ = std::make_shared<Logger>())
83 std::dynamic_pointer_cast<subspace::SubspaceSolverLinEig<R, Q, P>>(this->
m_subspace_solver);
85 auto imag_eigval_components =
subspace_solver->imag_eigval_components();
86 if (imag_eigval_components.empty()) {
93 "The following roots are complex-valued. Associated eigenvectors are the real and imaginary part "
94 "of the pairs and the imaginary parts of the eigenvalues are ",
95 [&] {
return typename Ctx::Payload{imag_eigval_components}; });
99 this->
m_logger->warn(std::format(
"The complex conjugate partner of root {} is not among the requested roots, so "
100 "only the real part of its eigenvector is available",
108 auto eval = std::vector<scalar_type>{};
119 cout <<
"errors " << std::scientific;
121 std::copy(begin(
err), end(
err), std::ostream_iterator<scalar_type>(
cout,
", "));
123 cout <<
"eigenvalues ";
125 cout << std::fixed << std::setprecision(14);
126 std::copy(begin(
ev), end(
ev), std::ostream_iterator<scalar_type>(
cout,
", "));
155 const VecRef<R>&
scratch,
const std::vector<value_type_abs>& solution_norms,
156 std::vector<value_type_abs>&
errors)
override {
168 if (!solution_norms.empty())
173 auto& error =
errors.at(std::ranges::distance(roots.begin(), std::ranges::find(roots,
int(
nroots) - 1)));
187 auto prof = this->
profiler()->push(
"itsolv::construct_residual");
188 assert(params.size() >= roots.size());
193 const auto& imag_eigval_components =
subspace_solver->imag_eigval_components();
198 const auto& xspace = *this->
m_xspace;
202 const auto&
dims = xspace.dimensions();
212 const size_t k,
const size_t i) {
216 for (
size_t j = 0;
j <
dims.nP; ++
j)
218 for (
size_t j = 0;
j <
dims.nQ; ++
j)
220 for (
size_t j = 0;
j <
dims.nD; ++
j)
225 for (
size_t i = 0;
i < roots.size(); ++
i) {
231 auto it = std::ranges::find(imag_eigval_components, roots[
i], [](
const auto&
pair) {
return pair.first; });
233 if (
it != imag_eigval_components.end()) {
258 const auto k = std::ranges::distance(roots.begin(),
root_it);
262 this->
m_handlers->rr().axpy(
it->second, params.at(
k), actions.at(
i));
266 throw std::logic_error(
"The complex conjugate partner of a root is not among the retained solutions");
271 throw std::logic_error(
"The complex conjugate partner of a root is not among the retained solutions");
286 auto metric_action = this->
m_handlers->rr().copy(actions.at(
i));
306 const auto residuals =
wrap(actions.begin(), actions.begin() + roots.size());
309 using VectorP =
typename SolverTemplate::VectorP;
310 auto pvectors = std::vector<VectorP>(roots.size(), VectorP(
dims.nP));
311 for (
size_t i = 0;
i < roots.size(); ++
i)
312 for (
size_t j = 0;
j <
dims.nP; ++
j)
350 auto metric_action = this->
m_handlers->rr().copy(param);
352 this->
m_handlers->rr().copy(param, metric_action);
362 this->m_handlers->rq());
Class, containing a collection of array handlers used in IterativeSolver Provides a Builder sub-class...
Definition ArrayHandlers.h:25
Implementation shared by the linear Davidson solvers, LinearEigensystemDavidson and LinearEquationsDa...
Definition DavidsonCommon.h:35
void set_davidson_options(const Options &options, const DavidsonOptions &opt)
Sets the general options and those of the Davidson solvers, after checking them.
Definition DavidsonCommon.h:155
Implements IterativeSolver interface that is common to all solvers.
Definition IterativeSolverTemplate.h:187
fapply_on_p_type m_apply_metric_p
adds the action of the metric on the P space, if its actions are stored
Definition IterativeSolverTemplate.h:1230
std::shared_ptr< subspace::IXSpace< R, Q, P > > m_xspace
manages the subspace and associated data
Definition IterativeSolverTemplate.h:1210
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Array handlers.
Definition IterativeSolverTemplate.h:1209
virtual void compute_errors(const std::vector< int > &roots, const CVecRef< R > ¶ms, const CVecRef< R > &residuals, const std::vector< value_type_abs > &solution_norms, std::vector< value_type_abs > &errors)
Computes the errors of roots, given their parameters and residuals; by default the norms of the resid...
Definition IterativeSolverTemplate.h:888
void construct_actions(const std::vector< int > &rows, const subspace::Matrix< value_type > &solutions, const VecRef< R > &actions)
Constructs the actions on the subspace vectors with the coefficients in the given rows of solutions.
Definition IterativeSolverTemplate.h:783
std::shared_ptr< subspace::ISubspaceSolver< R, Q, P > > m_subspace_solver
solves the subspace problem
Definition IterativeSolverTemplate.h:1211
std::vector< value_type_abs > m_errors
errors from the most recent solution
Definition IterativeSolverTemplate.h:1212
void solution_impl(const std::vector< int > &roots, const VecRef< R > ¶meters, const VecRef< R > &residual, const VecRef< R > &scratch)
Implements solution()
Definition IterativeSolverTemplate.h:717
void construct_solutions(const std::vector< int > &rows, const subspace::Matrix< value_type > &solutions, const VecRef< R > ¶meters)
Constructs the subspace vectors with the coefficients in the given rows of solutions.
Definition IterativeSolverTemplate.h:766
const std::shared_ptr< molpro::profiler::Profiler > & profiler() const override
Definition IterativeSolverTemplate.h:434
const std::vector< value_type_abs > & errors() const override
Definition IterativeSolverTemplate.h:353
void report() const override
Definition IterativeSolverTemplate.h:384
const std::vector< int > & working_set() const override
Definition IterativeSolverTemplate.h:314
std::shared_ptr< Logger > m_logger
logger
Definition IterativeSolverTemplate.h:1220
fapply_metric_type m_apply_metric
applies the metric on the fly, if its actions are not stored
Definition IterativeSolverTemplate.h:1227
One specific implementation of LinearEigensystem using Davidson's algorithm with modifications to man...
Definition LinearEigensystemDavidson.h:60
void finalize() override
Definition LinearEigensystemDavidson.h:78
std::vector< value_type > values() const override
The values of the current solutions, whose changes between iterations are the value errors.
Definition LinearEigensystemDavidson.h:140
void apply_metric(R ¶m)
Replaces param by the action of the metric on it, with the metric applied on the fly.
Definition LinearEigensystemDavidson.h:349
std::vector< scalar_type > eigenvalues() const override
Definition LinearEigensystemDavidson.h:105
void construct_residual(const std::vector< int > &roots, const CVecRef< R > ¶ms, const VecRef< R > &actions, const detail::MetricSolutions< R, Q > &metric_solutions, std::optional< value_type_abs > *truncated_partner_residual_norm)
Constructs residual for given roots provided their parameters and actions.
Definition LinearEigensystemDavidson.h:182
void set_options(const Options &options) override
Definition LinearEigensystemDavidson.h:131
void construct_residual(const std::vector< int > &roots, const CVecRef< R > ¶ms, const VecRef< R > &actions, const detail::MetricSolutions< R, Q > &metric_solutions) override
Constructs residual for given roots provided their parameters and actions.
Definition LinearEigensystemDavidson.h:142
void combine_errors() override
The error of both members of a complex pair is the norm of their complex residual.
Definition LinearEigensystemDavidson.h:334
bool supports_metric(MetricKind kind) const override
Supports generalised eigenproblems A v = lambda M v with a Hermitian, positive definite metric M.
Definition LinearEigensystemDavidson.h:74
void report(std::ostream &cout, bool endl=true) const override
Definition LinearEigensystemDavidson.h:116
virtual std::vector< scalar_type > working_set_eigenvalues() const override
Definition LinearEigensystemDavidson.h:107
LinearEigensystemDavidson(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers=std::make_shared< molpro::linalg::itsolv::ArrayHandlers< R, Q, P > >(), const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition LinearEigensystemDavidson.h:68
std::shared_ptr< Options > get_options() const override
Definition LinearEigensystemDavidson.h:135
void solution_with_errors(const std::vector< int > &roots, const VecRef< R > ¶meters, const VecRef< R > &residual, const VecRef< R > &scratch, const std::vector< value_type_abs > &solution_norms, std::vector< value_type_abs > &errors) override
Constructs the parameters and residuals of roots and their errors.
Definition LinearEigensystemDavidson.h:154
void construct_subspace_metric_action(const subspace::Matrix< scalar_type > &solutions, int row, R &metric_action)
Constructs in metric_action the action of the metric on the subspace vector with the given row of coe...
Definition LinearEigensystemDavidson.h:356
Interface for a specific iterative solver, it can add special member functions or variables.
Definition IterativeSolver.h:770
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
index_type cols() const
Definition Matrix.h:170
std::vector< std::vector< T > > construct_vectorP(const std::vector< int > &roots, const subspace::Matrix< T > &solutions, const size_t oP, const size_t nP)
Definition IterativeSolverTemplate.h:47
std::string stringify(const T &t, std::size_t precision=default_precision)
Converts the given object into a string.
Definition Logger.h:178
std::vector< real_type_t< T > > solution_norms(const Matrix< T > &solutions, const Matrix< T > &overlap)
The Euclidean norms of the solutions x_i = sum_j c_ij b_j, from their subspace coefficients c (one ro...
Definition util.h:82
void construct_solutions(const VecRef< R > ¶ms, const std::vector< int > &roots, const subspace::Matrix< value_type > &solutions, const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &dparams, size_t oP, size_t oQ, size_t oD, array::ArrayHandler< R, R > &handler_rr, array::ArrayHandler< R, P > &handler_rp, array::ArrayHandler< R, Q > &handler_rq)
Construct solutions.
Definition util.h:82
auto norm(array::ArrayHandler< R, R > &handler, const std::type_identity_t< R > &x) -> typename array::ArrayHandler< R, R >::value_type_abs
The length of x, sqrt(|<x|x>|), which is real also for complex values.
Definition util.h:21
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 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
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
@ OnTheFly
the metric is applied to the solutions whenever residuals are constructed
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
MetricKind
The kind of metric M of a generalised problem, such as the eigenproblem A v = lambda M v.
Definition Metric.h:13
@ HermitianPositiveDefinite
Hermitian and positive definite, e.g. the overlap matrix of a non-orthogonal basis.
@ 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
Definition scalar_traits.h:19
static std::shared_ptr< LinearEigensystemDavidsonOptions > LinearEigensystem(const std::shared_ptr< Options > &options)
Definition CastOptions.h:39
Access point for different options in iterative solvers.
Definition Options.h:20
The actions of a metric applied on the fly on solutions, in R vectors or, if there are not enough of ...
Definition IterativeSolverTemplate.h:40
CVecRef< R > r
Definition IterativeSolverTemplate.h:41
CVecRef< Q > q
Definition IterativeSolverTemplate.h:42
Definition LinearEigensystemDavidson.h:33
std::span< const std::pair< std::size_t, Scalar > > roots
pairs of (0-based) root index and imaginary part of the eigenvalue, one per complex root
Definition LinearEigensystemDavidson.h:35
Definition LinearEigensystemDavidson.h:31
static constexpr std::string_view name
Definition LinearEigensystemDavidson.h:32
static std::string format(const Payload &p)
Prints 1-based root indices.
Definition LinearEigensystemDavidson.h:38