1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_NONLINEAREQUATIONSDIIS_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_NONLINEAREQUATIONSDIIS_H
4#include <Eigen/Eigenvalues>
8#include <molpro/linalg/itsolv/CastOptions.h>
9#include <molpro/linalg/itsolv/DSpaceResetter.h>
10#include <molpro/linalg/itsolv/IterativeSolverTemplate.h>
11#include <molpro/linalg/itsolv/propose_rspace.h>
12#include <molpro/linalg/itsolv/subspace/Matrix.h>
13#include <molpro/linalg/itsolv/subspace/SubspaceSolverDIIS.h>
14#include <molpro/linalg/itsolv/subspace/XSpace.h>
15#include <molpro/profiler/Profiler.h>
26template <
class R,
class Q = R,
class P = std::map<
size_t,
typename R::value_type>>
33 using typename SolverTemplate::scalar_type;
34 using typename SolverTemplate::value_type;
35 using typename SolverTemplate::value_type_abs;
38 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
39 :
SolverTemplate(std::make_shared<subspace::XSpace<R, Q, P>>(handlers, logger_),
40 std::static_pointer_cast<subspace::ISubspaceSolver<R, Q, P>>(
41 std::make_shared<subspace::SubspaceSolverDIIS<R, Q, P>>(logger_, m_converged)),
42 handlers, std::make_shared<
Statistics>(), logger_),
43 logger(logger_), m_converged(false) {
44 auto xspace = std::dynamic_pointer_cast<subspace::XSpace<R, Q, P>>(this->
m_xspace);
45 xspace->set_hermiticity(
true);
46 xspace->set_action_action();
53 const auto He = Eigen::Map<const Eigen::MatrixXd>(H.data().data(), H.rows(), H.cols());
54 std::pair<size_t, value_type> result{0, std::numeric_limits<value_type>::max()};
57 auto evs = Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd>();
64 for (Eigen::Index i = 0; i < He.cols(); ++i) {
65 evmax = std::max(evmax, evs.eigenvalues()(i));
66 if (evs.eigenvalues()(i) < result.second) {
67 result.second = evs.eigenvalues()(i);
69 for (Eigen::Index j = 1; j < He.rows(); ++j) {
70 if (std::abs(evs.eigenvectors().col(i)(j)) > std::abs(evs.eigenvectors().col(i)(result.first)))
75 result.second /= evmax;
77 result = {He.cols() - 1, std::numeric_limits<value_type>::max()};
84 auto prof = this->
profiler()->push(
"itsolv::add_vector");
85 auto error = std::sqrt(this->
m_handlers->rr().dot(residual, residual));
87 using namespace subspace;
89 const auto& H = xspace->
data[EqnData::H];
91 for (std::pair<size_t, value_type> deleter = least_important_vector(H);
93 deleter = least_important_vector(H)) {
95 xspace->eraseq(deleter.first);
104 auto prof = this->
profiler()->push(
"itsolv::end_iteration");
107 if (this->
m_errors.front() < this->m_convergence_threshold) {
114 this->
m_handlers->rr().axpy(-1, action.front(), parameters.front());
121 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action)
override {
142 if (opt.max_size_qspace)
151 auto opt = std::make_shared<NonLinearEquationsDIISOptions>();
159 void report(std::ostream& cout,
bool endl =
true)
const override {
171 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
172 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
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
void solution_params(const std::vector< int > &roots, std::vector< R > ¶meters) override
Definition: IterativeSolverTemplate.h:262
std::vector< int > m_working_set
indices of roots in the working set
Definition: IterativeSolverTemplate.h:674
scalar_type value() const override
Report the function value for the current optimum solution.
Definition: IterativeSolverTemplate.h:384
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:670
std::shared_ptr< ArrayHandlers< R, R, std::map< size_t, typename R::value_type > > > m_handlers
Array handlers.
Definition: IterativeSolverTemplate.h:669
std::shared_ptr< Options > get_options() const override
Definition: IterativeSolverTemplate.h:306
const std::shared_ptr< molpro::profiler::Profiler > & profiler() const override
Definition: IterativeSolverTemplate.h:392
bool m_end_iteration_needed
whether end_iteration should be called after any preconditioner
Definition: IterativeSolverTemplate.h:691
int add_vector(const VecRef< R > ¶meters, const VecRef< R > &actions) override
Definition: IterativeSolverTemplate.h:181
void report() const override
Definition: IterativeSolverTemplate.h:347
double m_convergence_threshold
residual norms less than this mark a converged solution
Definition: IterativeSolverTemplate.h:676
void set_options(const Options &options) override
Definition: IterativeSolverTemplate.h:291
std::vector< double > m_errors
errors from the most recent solution
Definition: IterativeSolverTemplate.h:672
std::shared_ptr< Statistics > m_stats
accumulates statistics of operations performed by the solver
Definition: IterativeSolverTemplate.h:679
typename R::value_type value_type
The underlying type of elements of vectors.
Definition: IterativeSolver.h:204
A class that optimises a function using a Quasi-Newton or other method.
Definition: NonLinearEquationsDIIS.h:27
size_t end_iteration(R ¶meters, R &actions) override
Definition: NonLinearEquationsDIIS.h:170
double m_svd_thresh
svd values smaller than this mark the null space
Definition: NonLinearEquationsDIIS.h:181
void set_norm_thresh(double thresh)
Set threshold on the norm of parameters that should be considered null.
Definition: NonLinearEquationsDIIS.h:128
NonLinearEquationsDIIS(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition: NonLinearEquationsDIIS.h:37
double get_svd_thresh() const
Definition: NonLinearEquationsDIIS.h:133
double m_norm_thresh
vectors with norm less than threshold can be considered null.
Definition: NonLinearEquationsDIIS.h:180
size_t end_iteration(std::vector< R > ¶meters, std::vector< R > &action) override
Definition: NonLinearEquationsDIIS.h:121
size_t end_iteration(const VecRef< R > ¶meters, const VecRef< R > &action) override
Behaviour depends on the solver.
Definition: NonLinearEquationsDIIS.h:103
int get_max_size_qspace() const
Definition: NonLinearEquationsDIIS.h:137
bool nonlinear() const override
Report whether the class is a non-linear solver.
Definition: NonLinearEquationsDIIS.h:49
void set_svd_thresh(double thresh)
Definition: NonLinearEquationsDIIS.h:132
int add_vector(R ¶meters, R &residual, value_type value) override
Definition: NonLinearEquationsDIIS.h:83
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: NonLinearEquationsDIIS.h:178
void report() const override
Writes a report to std::cout.
Definition: IterativeSolverTemplate.h:347
void report(std::ostream &cout, bool endl=true) const override
Writes a report to cout output stream.
Definition: NonLinearEquationsDIIS.h:159
std::shared_ptr< Options > get_options() const override
Definition: NonLinearEquationsDIIS.h:150
int m_max_size_qspace
maximum size of Q space
Definition: NonLinearEquationsDIIS.h:182
std::shared_ptr< Logger > logger
Definition: NonLinearEquationsDIIS.h:168
void set_max_size_qspace(int n)
Definition: NonLinearEquationsDIIS.h:136
void set_options(const Options &options) override
Definition: NonLinearEquationsDIIS.h:139
double get_norm_thresh() const
Definition: NonLinearEquationsDIIS.h:129
const std::vector< T > & data() const &
Access the underlying data buffer.
Definition: Matrix.h:67
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition: helper.h:10
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: options.cpp:4
static std::shared_ptr< NonLinearEquationsDIISOptions > NonLinearEquationsDIIS(const std::shared_ptr< Options > &options)
Definition: CastOptions.h:67
Access point for different options in iterative solvers.
Definition: Options.h:20
Information about performance of IterativeSolver instance.
Definition: Statistics.h:10