iterative-solver 0.0
 
Loading...
Searching...
No Matches
DSpaceResetter.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_DSPACERESETTER_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_DSPACERESETTER_H
3#include <molpro/linalg/itsolv/IterativeSolver.h>
4#include <molpro/linalg/itsolv/propose_rspace.h>
5#include <molpro/linalg/itsolv/qspace_options.h>
6#include <molpro/linalg/itsolv/subspace/Dimensions.h>
7#include <molpro/linalg/itsolv/subspace/IXSpace.h>
8#include <molpro/linalg/itsolv/subspace/QSpace.h>
9#include <molpro/linalg/itsolv/subspace/gram_schmidt.h>
10#include <molpro/linalg/itsolv/subspace/util.h>
11#include <molpro/linalg/itsolv/util.h>
12
13#include <algorithm>
14#include <cassert>
15#include <cstddef>
16#include <functional>
17#include <limits>
18
25template <class R, class Q, class P, typename value_type>
27 std::size_t max_size, Logger& logger) {
28 logger.trace("resize_qspace()");
29 auto q_delete = limit_qspace_size(xspace.dimensions(), QSpaceOptions{.max_size = max_size}, solutions, logger);
30 logger.debug("delete Q parameter indices = ", q_delete);
31 erase_qspace(xspace, q_delete);
32}
33
35template <class R, class Q, class P, typename value_type, typename Index>
37 std::vector<Index> indices) {
38 assert(solutions.cols() == xspace.dimensions().nX());
39 const auto oQ = xspace.dimensions().oQ();
40 std::sort(begin(indices), end(indices), std::greater<Index>());
41 for (auto i : indices) {
42 xspace.eraseq(i);
43 solutions.remove_col(oQ + i);
44 }
45}
46
54template <class R, class Q>
56 Logger& logger) {
57 logger.trace("max_overlap_with_R()");
58 const auto nR = rparams.size();
59 auto overlap = subspace::util::overlap(rparams, qparams, handler);
60 auto q_indices = std::vector<int>(qparams.size());
61 std::iota(std::begin(q_indices), std::end(q_indices), 0);
62 auto q_max_overlap = std::vector<int>{};
63 for (size_t i = 0; i < nR && !q_indices.empty(); ++i) {
64 auto ov = std::vector<typename array::ArrayHandler<R, Q>::value_type_abs>{};
65 for (auto j : q_indices)
66 ov.push_back(std::abs(overlap(i, j)));
67 auto it_max = std::max_element(ov.begin(), ov.end());
68 auto i_max = std::distance(ov.begin(), it_max);
69 logger.debug("removed q index = " + std::to_string(q_indices[i_max]) +
70 ", with overlap = " + std::to_string(*it_max));
71 q_max_overlap.push_back(q_indices[i_max]);
72 q_indices.erase(q_indices.begin() + i_max);
73 }
74 std::sort(std::begin(q_max_overlap), std::end(q_max_overlap), std::greater<int>());
75 return q_max_overlap;
76}
77
91template <class Q>
93protected:
94 std::size_t m_nreset = 0;
96 std::numeric_limits<std::size_t>::max();
97 std::list<Q> solution_params;
98
99public:
100 DSpaceResetter() = default;
102
104 bool do_reset(size_t iter, const subspace::Dimensions& dims) {
105 return (m_nreset != 0 && (iter + 1) % m_nreset == 0 && dims.nD > 0) || in_progress();
106 }
107
109 bool in_progress() const { return !solution_params.empty(); }
110
111 void set_nreset(size_t i) { m_nreset = i; }
112 auto get_nreset() const { return m_nreset; }
114 auto get_max_Qsize() const { return m_max_Qsize_after_reset; }
115
121 template <class R, class P, typename value_type, typename value_type_abs>
122 std::vector<int> run(const VecRef<R>& rparams, subspace::IXSpace<R, Q, P>& xspace,
123 const subspace::Matrix<value_type>& solutions, const value_type_abs norm_thresh,
124 const value_type_abs svd_thresh, ArrayHandlers<R, Q, P>& handlers, Logger& logger) {
125 logger.trace("DSpaceResetter::run()");
126 logger.trace("dimensions {nP, nQ, nD, nR} = ", xspace.dimensions().nP, xspace.dimensions().nQ,
127 xspace.dimensions().nD, rparams.size());
128 // the solutions scaled to unit length, as in construct_dspace(), so that thresholds on norms and contributions do not
129 // depend on how the solutions are normalised; this has to happen before the subspace changes. Their columns are kept
130 // in step with the changes to xspace below.
131 auto solutions_xspace = contribution_solutions(solutions, xspace);
132 if (solution_params.empty() && !rparams.empty()) {
133 logger.debug("constructing solutions");
134 const auto dims = xspace.dimensions();
135 const auto overlap = xspace.data.at(subspace::EqnData::S);
136 auto q_indices = std::vector<std::size_t>(xspace.dimensions().nQ);
137 std::iota(q_indices.begin(), q_indices.end(), 0);
139 norm_thresh, svd_thresh, logger);
140 const auto nC = solutions_proj.rows();
141 for (size_t i = 0; i < nC; ++i)
142 solution_params.emplace_back(util::construct_zeroed_copy(rparams.front().get(), handlers.qr()));
143 auto roots = std::vector<int>(nC);
144 std::iota(begin(roots), end(roots), 0);
146 xspace.cparamsq(), xspace.cparamsd(), 0, 0, dims.nQ, handlers.qq(), handlers.qp(),
147 handlers.qq());
148 assert(solutions_xspace.cols() == dims.nX());
149 for (size_t i = 0; i < dims.nD; ++i)
150 solutions_xspace.remove_col(dims.oD());
153 }
154 const auto nR = std::min(rparams.size(), solution_params.size());
155 for (size_t i = 0; i < nR; ++i) {
156 handlers.rq().copy(rparams[i], solution_params.front());
157 solution_params.pop_front();
158 }
159 const auto wparams = cwrap(begin(rparams), begin(rparams) + nR);
160 auto q_delete = max_overlap_with_R(wparams, xspace.cparamsq(), handlers.rq(), logger);
162 assert(solutions_xspace.cols() == xspace.dimensions().nX());
163 if (xspace.dimensions().nQ + nR > m_max_Qsize_after_reset)
165 auto new_working_set = std::vector<int>(nR);
166 std::iota(begin(new_working_set), end(new_working_set), 0);
167 return new_working_set;
168 }
169};
170
171} // namespace molpro::linalg::itsolv::detail
172#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_DSPACERESETTER_H
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
Class, containing a collection of array handlers used in IterativeSolver Provides a Builder sub-class...
Definition ArrayHandlers.h:25
Logger class implementation. Provides (customizable) logging functionalities.
Definition Logger.h:370
void trace(std::string_view message, const Ts &...args) const
Definition Logger.h:463
void debug(std::string_view message, const Ts &...args) const
Definition Logger.h:468
Resets D space constructing full solutions as the new working set, removing instabilities from Q spac...
Definition DSpaceResetter.h:92
std::size_t m_max_Qsize_after_reset
maximum size of Q space after reset
Definition DSpaceResetter.h:95
DSpaceResetter(std::size_t nreset, std::size_t max_Qsize)
Definition DSpaceResetter.h:101
auto get_nreset() const
Definition DSpaceResetter.h:112
bool do_reset(size_t iter, const subspace::Dimensions &dims)
Whether reset operation should be run.
Definition DSpaceResetter.h:104
std::list< Q > solution_params
all current solutions that will be moved to the Q space
Definition DSpaceResetter.h:97
void set_max_Qsize(size_t i)
Definition DSpaceResetter.h:113
bool in_progress() const
Whether a reset has started and not all solutions have been moved into the Q space yet.
Definition DSpaceResetter.h:109
std::vector< int > run(const VecRef< R > &rparams, subspace::IXSpace< R, Q, P > &xspace, const subspace::Matrix< value_type > &solutions, const value_type_abs norm_thresh, const value_type_abs svd_thresh, ArrayHandlers< R, Q, P > &handlers, Logger &logger)
Run the reset operation.
Definition DSpaceResetter.h:122
std::size_t m_nreset
reset D space every n iterations, 0 for never
Definition DSpaceResetter.h:94
void set_nreset(size_t i)
Definition DSpaceResetter.h:111
auto get_max_Qsize() const
Definition DSpaceResetter.h:114
Full subspace.
Definition IXSpace.h:21
SubspaceData< value_type > data
Equation data in the subspace.
Definition IXSpace.h:30
virtual CVecRef< Q > cparamsq() const =0
virtual void eraseq(size_t i)=0
Removes parameter i from Q subspace.
virtual void update_dspace(VecRef< Q > &params, VecRef< Q > &actions, const VecRef< Q > &metric_actions={}, const std::vector< size_t > &q_delete={}, const Matrix< value_type > &coefficients={})=0
Replaces the old D space and the Q vectors q_delete by new D vectors, which are linear combinations o...
virtual CVecRef< Q > cparamsd() const =0
virtual const Dimensions & dimensions() const =0
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
void remove_col(index_type col)
removes a column from the matrix
Definition Matrix.h:154
index_type cols() const
Definition Matrix.h:170
auto stable_projected_solutions(const subspace::Matrix< value_type > &solutions, const subspace::Matrix< value_type > &overlap, const subspace::Dimensions &dims, const std::vector< std::size_t > &remove_qspace, const value_type_abs norm_thresh, const value_type_abs svd_thresh, Logger &logger)
Projects solutions onto Q_{delete} and the current D space, and transforms them to a normalised set w...
Definition propose_rspace.h:251
Definition helper-dispatch.h:224
void erase_qspace(subspace::IXSpace< R, Q, P > &xspace, subspace::Matrix< value_type > &solutions, std::vector< Index > indices)
Erases Q parameters with the given indices together with the corresponding columns of a solutions mat...
Definition DSpaceResetter.h:36
auto max_overlap_with_R(const CVecRef< R > &rparams, const CVecRef< Q > &qparams, array::ArrayHandler< R, Q > &handler, Logger &logger)
Returns list of Q indices with maximum overlap to R parameters, sorted in descending order.
Definition DSpaceResetter.h:55
subspace::Matrix< value_type > contribution_solutions(const subspace::Matrix< value_type > &solutions, const subspace::IXSpace< R, Q, P > &xspace)
The solutions scaled to unit length, as needed wherever thresholds apply to their coefficients or to ...
Definition propose_rspace.h:367
void resize_qspace(subspace::IXSpace< R, Q, P > &xspace, const subspace::Matrix< value_type > &solutions, std::size_t max_size, Logger &logger)
Removes Q parameters that have smallest contribution to any solution until Q space size is within lim...
Definition DSpaceResetter.h:26
std::vector< std::size_t > limit_qspace_size(const subspace::Dimensions &dims, const QSpaceOptions &opts, const subspace::Matrix< value_type > &solutions, Logger &logger)
Ensures that size Q space is within limit by proposing Q parameters for deletion.
Definition propose_rspace.h:383
auto overlap(const CVecRef< R > &left, const CVecRef< Q > &right, array::ArrayHandler< Z, W > &handler) -> std::enable_if_t< detail::Z_and_W_are_one_of_R_and_Q< R, Q, Z, W >, Matrix< typename array::ArrayHandler< Z, W >::value_type > >
Calculates overlap matrix between left and right vectors.
Definition util.h:57
Q construct_zeroed_copy(const R &param, array::ArrayHandler< Q, R > &handler)
Copy R parameter to Q and zero it. This ensures same size and distribution of the copy.
Definition util.h:60
void construct_solutions(const VecRef< R > &params, 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 cwrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition wrap.h:52
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
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
size_t nD
Definition Dimensions.h:14
size_t nQ
Definition Dimensions.h:13
size_t nP
Definition Dimensions.h:12
size_t oQ() const
offset of the Q block
Definition Dimensions.h:18
size_t nX() const
total size of the X space
Definition Dimensions.h:16