iterative-solver 0.0
NonLinearEquationsDIIS.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_NONLINEAREQUATIONSDIIS_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_NONLINEAREQUATIONSDIIS_H
3#include <Eigen/Core>
4#include <Eigen/Eigenvalues>
5#include <algorithm>
6#include <cmath>
7#include <limits>
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>
16
17namespace molpro::linalg::itsolv {
26template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
27class NonLinearEquationsDIIS : public IterativeSolverTemplate<NonLinearEquations, R, Q, P> {
28 bool m_converged;
29
30public:
33 using typename SolverTemplate::scalar_type;
34 using typename SolverTemplate::value_type;
35 using typename SolverTemplate::value_type_abs;
36
37 explicit NonLinearEquationsDIIS(const std::shared_ptr<ArrayHandlers<R, Q, P>>& handlers,
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();
47 }
48
49 bool nonlinear() const override { return true; }
50
51private:
52 std::pair<size_t, value_type> least_important_vector(const subspace::Matrix<value_type>& H) {
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()};
55 if (He.cols() < 2)
56 return result;
57 auto evs = Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd>();
58 evs.compute(He);
59 // std::cout << "Eigenvalues: "<<evs.eigenvalues().transpose()<<std::endl;
60 // std::cout << "Eigenvectors:\n"<<evs.eigenvectors()<<std::endl;
61 // result.first = He.cols() - 1;
62 // return result;
63 value_type evmax = 0;
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);
68 result.first = 1;
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)))
71 result.first = j;
72 }
73 }
74 }
75 result.second /= evmax;
76 if (result.second > m_svd_thresh)
77 result = {He.cols() - 1, std::numeric_limits<value_type>::max()};
78 // std::cout << "least important vector " << result.first << " : " << result.second << std::endl;
79 return result;
80 }
81
82public:
83 int add_vector(R& parameters, R& residual, value_type value) override {
84 auto prof = this->profiler()->push("itsolv::add_vector");
85 auto error = std::sqrt(this->m_handlers->rr().dot(residual, residual));
86 m_converged = error < this->m_convergence_threshold;
87 using namespace subspace;
88 auto& xspace = this->m_xspace;
89 const auto& H = xspace->data[EqnData::H];
90 // std::cout << "H " << as_string(H) << std::endl;
91 for (std::pair<size_t, value_type> deleter = least_important_vector(H);
92 xspace->size() >= size_t(this->m_max_size_qspace) or deleter.second < m_svd_thresh;
93 deleter = least_important_vector(H)) {
94 // std::cout << "deleter " << deleter.first << " : " << deleter.second << std::endl;
95 xspace->eraseq(deleter.first);
96 }
97 // std::cout << "H after delete Q "<<as_string(H)<<std::endl;
98
100 this->m_errors.front() = error;
101 return nwork;
102 }
103 size_t end_iteration(const VecRef<R>& parameters, const VecRef<R>& action) override {
104 auto prof = this->profiler()->push("itsolv::end_iteration");
105 this->solution_params(this->m_working_set, parameters);
106 this->m_end_iteration_needed = false;
107 if (this->m_errors.front() < this->m_convergence_threshold) {
108 this->m_working_set.clear();
109 return 0;
110 }
111 this->m_working_set.assign(1, 0);
112 bool precon = true; // TODO implement
113 if (precon) { // action is expected to hold the preconditioned residual, and here we should add it to parameters
114 this->m_handlers->rr().axpy(-1, action.front(), parameters.front());
115 } else { // residual not used, simply leave parameters alone
116 }
117 this->m_stats->iterations++;
118 return 1;
119 }
120
121 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) override {
122 auto result = end_iteration(wrap(parameters), wrap(action));
123 // std::cout << ", max q" << this->m_max_size_qspace << std::endl; // get_max_size_qspace();
124 return result;
125 }
126
128 void set_norm_thresh(double thresh) { m_norm_thresh = thresh; }
129 double get_norm_thresh() const { return m_norm_thresh; }
132 void set_svd_thresh(double thresh) { m_svd_thresh = thresh; }
133 double get_svd_thresh() const { return m_svd_thresh; }
138
139 void set_options(const Options& options) override {
142 if (opt.max_size_qspace)
143 set_max_size_qspace(opt.max_size_qspace.value());
144 if (opt.norm_thresh)
145 set_norm_thresh(opt.norm_thresh.value());
146 if (opt.svd_thresh)
147 set_svd_thresh(opt.svd_thresh.value());
148 }
149
150 std::shared_ptr<Options> get_options() const override {
151 auto opt = std::make_shared<NonLinearEquationsDIISOptions>();
152 opt->copy(*SolverTemplate::get_options());
153 opt->max_size_qspace = get_max_size_qspace();
154 opt->norm_thresh = get_norm_thresh();
155 opt->svd_thresh = get_svd_thresh();
156 return opt;
157 }
158
159 void report(std::ostream& cout, bool endl = true) const override {
160 SolverTemplate::report(cout, endl);
161 // auto& err = this->m_errors;
162 // std::copy(begin(err), end(err), std::ostream_iterator<value_type_abs>(cout, ", "));
163 // cout << std::defaultfloat;
164 // cout << ", max q" << this->m_max_size_qspace; // get_max_size_qspace();
165 // if (endl)
166 // cout << std::endl;
167 }
168 std::shared_ptr<Logger> logger;
169
170 size_t end_iteration(R& parameters, R& actions) 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)};
173 return end_iteration(wparams, wactions);
174 }
175
176protected:
177 // for non-linear problems, actions already contains the residual
178 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions) override {}
179
180 double m_norm_thresh = 1e-10;
181 double m_svd_thresh = 1e-12;
182 int m_max_size_qspace = std::numeric_limits<int>::max();
183};
184
185} // namespace molpro::linalg::itsolv
186#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_NONLINEAREQUATIONSDIIS_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:161
void solution_params(const std::vector< int > &roots, std::vector< R > &parameters) 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 > &parameters, 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 &parameters, 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 > &parameters, std::vector< R > &action) override
Definition: NonLinearEquationsDIIS.h:121
size_t end_iteration(const VecRef< R > &parameters, 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 &parameters, R &residual, value_type value) override
Definition: NonLinearEquationsDIIS.h:83
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: 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