iterative-solver 0.0
 
Loading...
Searching...
No Matches
QSpace.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_QSPACE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_QSPACE_H
3#include <array>
4#include <algorithm>
5#include <cassert>
6#include <functional>
7#include <list>
8
9#include <molpro/linalg/itsolv/ArrayHandlers.h>
10#include <molpro/linalg/itsolv/Logger.h>
11#include <molpro/linalg/itsolv/subspace/Dimensions.h>
12#include <molpro/linalg/itsolv/subspace/PSpace.h>
13#include <molpro/linalg/itsolv/wrap.h>
14#include <molpro/linalg/itsolv/util.h>
15#include <molpro/linalg/itsolv/helper-implementation.h>
16
18
19namespace qspace {
23template <class Q>
24struct QParam {
25 std::unique_ptr<Q> param;
26 std::unique_ptr<Q> action;
27 size_t id;
28};
29
31template <class Q, class ForwardIt>
33 auto param_action = std::array<CVecRef<Q>, 2>{};
34 for (auto it = begin; it != end; ++it) {
35 param_action[0].emplace_back(*it->param);
36 param_action[1].emplace_back(*it->action);
37 }
38 return param_action;
39}
40
41template <class Q, class ForwardIt>
43 auto param_action = std::array<VecRef<Q>, 2>{};
44 for (auto it = begin; it != end; ++it) {
45 param_action[0].emplace_back(*it->param);
46 param_action[1].emplace_back(*it->action);
47 }
48 return param_action;
49}
50} // namespace qspace
51
59template <class Rt, class Qt, class Pt>
60struct QSpace {
61 using R = Rt;
62 using Q = Qt;
63 using P = Pt;
66
67 explicit QSpace(std::shared_ptr<ArrayHandlers<R, Q, P>> handlers, std::shared_ptr<Logger> logger)
68 : m_handlers(std::move(handlers)), m_logger(std::move(logger)) {}
69
83 template <typename T>
87 m_logger->trace("QSpace::update");
88 auto it_begin = m_params.begin();
89 for (size_t i = 0; i < params.size(); ++i) {
90 m_params.emplace(it_begin,
91 qspace::QParam<Q>{std::make_unique<Q>(m_handlers->qr().copy(params.at(i))),
92 std::make_unique<Q>(m_handlers->qr().copy(actions.at(i))), m_unique_id++});
93 }
94 size_t nQnew = params.size();
95 auto qq = qq_in;
96 auto qx = qx_in;
97 auto xq = xq_in;
98 if (nQnew > 1) {
101 // New parameters occupy the first nQnew positions of m_params, in the same order as the rows/cols of qq.
102 // Remove the redundant ones from the highest index down so that the remaining indices stay valid.
103 std::sort(rp.begin(), rp.end(), std::greater<>());
104 for (auto i : rp) {
105 m_params.erase(std::next(m_params.begin(), i));
106 for (auto d : {EqnData::H, EqnData::S}) {
107 qq.at(d).remove_row_col(i, i);
108 qx.at(d).remove_row(i);
109 xq.at(d).remove_col(i);
110 }
111 if (!qq.at(EqnData::rhs).empty())
112 qq.at(EqnData::rhs).remove_row(i);
113 }
114 nQnew -= rp.size();
115 }
116 const auto nXnew = dims.nX() + nQnew;
117 auto data = old_data;
118 for (auto d : {EqnData::H, EqnData::S}) {
119 data[d].resize({dims.nX() + nQnew, dims.nX() + nQnew});
120 data[d].slice({dims.oQ() + nQnew, dims.oQ() + nQnew}, {data[d].rows(), data[d].cols()}) =
121 old_data[d].slice({dims.oQ(), dims.oQ()}, {dims.nX(), dims.nX()});
122 data[d].slice({dims.oQ(), dims.oQ()}, {dims.oQ() + nQnew, dims.oQ() + nQnew}) =
123 qq.at(d).slice({0, 0}, {nQnew, nQnew});
124 data[d].slice({dims.oQ(), 0}, {dims.oQ() + nQnew, dims.oQ()}) = qx.at(d).slice({0, 0}, {nQnew, dims.oQ()});
125 data[d].slice({dims.oQ(), dims.oQ() + nQnew}, {dims.oQ() + nQnew, nXnew}) =
126 qx.at(d).slice({0, dims.oQ()}, {nQnew, dims.nX()});
127 data[d].slice({0, dims.oQ()}, {dims.oQ(), dims.oQ() + nQnew}) = xq.at(d).slice({0, 0}, {dims.oQ(), nQnew});
128 data[d].slice({dims.oQ() + nQnew, dims.oQ()}, {nXnew, dims.oQ() + nQnew}) =
129 xq.at(d).slice({dims.oQ(), 0}, {dims.nX(), nQnew});
130 data[d].slice({0, 0}, {dims.oQ(), dims.oQ()}) = old_data[d].slice({0, 0}, {dims.oQ(), dims.oQ()});
131 data[d].slice({0, dims.oQ() + nQnew}, {dims.oQ(), nXnew}) =
132 old_data[d].slice({0, dims.oQ()}, {dims.oQ(), dims.nX()});
133 data[d].slice({dims.oQ() + nQnew, 0}, {nXnew, dims.oQ()}) =
134 old_data[d].slice({dims.oQ(), 0}, {dims.nX(), dims.oQ()});
135 old_data[d] = data[d];
136 }
137 if (!qq.at(EqnData::rhs).empty()) {
138 data[EqnData::rhs].resize({dims.nX() + nQnew, dims.nRHS});
139 data[EqnData::rhs].slice({dims.oQ(), 0}, {dims.oQ() + nQnew, dims.nRHS}) = qq.at(EqnData::rhs).slice();
140 data[EqnData::rhs].slice({dims.oQ() + nQnew, 0}, {dims.nX() + nQnew, dims.nRHS}) =
141 old_data[EqnData::rhs].slice({dims.oQ(), 0}, {dims.nX(), dims.nRHS});
143 }
144 m_logger->data_dump("S = ", data.at(EqnData::S));
145 m_logger->data_dump("H = ", data.at(EqnData::H));
146 m_logger->data_dump("rhs = ", data.at(EqnData::rhs));
147 }
148
149 void clear() { m_params.clear(); }
150
152 void erase(size_t i) {
153 assert(m_params.size() > i);
154 auto it = std::next(begin(m_params), i);
155 m_params.erase(it);
156 }
157
158 size_t size() const { return m_params.size(); }
159
161 auto qparams = qspace::wrap_params<Q>(m_params.begin(), m_params.end());
162 return qparams[0];
163 }
164
166 auto qparams = qspace::cwrap_params<Q>(m_params.begin(), m_params.end());
167 return qparams[0];
168 }
169
170 CVecRef<Q> cparams() const { return params(); }
171
173 auto qparams = qspace::wrap_params<Q>(m_params.begin(), m_params.end());
174 return qparams[1];
175 }
176
178 auto qparams = qspace::cwrap_params<Q>(m_params.begin(), m_params.end());
179 return qparams[1];
180 }
181
182 CVecRef<Q> cactions() const { return actions(); }
183
184protected:
185 std::shared_ptr<ArrayHandlers<R, Q, P>> m_handlers;
186 std::shared_ptr<Logger> m_logger;
187 size_t m_unique_id{0};
188 std::list<qspace::QParam<Q>> m_params;
189};
190
191} // namespace molpro::linalg::itsolv::subspace
192
193#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_QSPACE_H
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
Class, containing a collection of array handlers used in IterativeSolver Provides a Builder sub-class...
Definition ArrayHandlers.h:25
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
auto redundant_parameters(const subspace::Matrix< value_type > &overlap, const size_t oR, const size_t nR, const value_type_abs svd_thresh, Logger &logger)
Deduces a set of parameters that are redundant due to linear dependencies.
Definition helper-implementation.h:856
auto wrap_params(ForwardIt begin, ForwardIt end)
Definition QSpace.h:42
auto cwrap_params(ForwardIt begin, ForwardIt end)
Generates vector of reference wrappers to param and action in a range of QParam objects.
Definition QSpace.h:32
std::map< EqnData, Matrix< T > > SubspaceData
Equation data blocks of the subspace, in the scalar type of the problem.
Definition SubspaceData.h:11
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
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
Container storing the Q space parameters.
Definition QSpace.h:60
typename array::ArrayHandler< R, R >::value_type_abs value_type_abs
Definition QSpace.h:65
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Definition QSpace.h:185
VecRef< Q > params()
Definition QSpace.h:160
typename array::ArrayHandler< R, R >::value_type value_type
Definition QSpace.h:64
CVecRef< Q > cactions() const
Definition QSpace.h:182
void update(const CVecRef< R > &params, const CVecRef< R > &actions, const SubspaceData< T > &qq_in, const SubspaceData< T > &qx_in, const SubspaceData< T > &xq_in, const Dimensions &dims, SubspaceData< T > &old_data)
Prepends parameters to the start of Q space.
Definition QSpace.h:84
size_t size() const
Definition QSpace.h:158
void clear()
Definition QSpace.h:149
CVecRef< Q > cparams() const
Definition QSpace.h:170
size_t m_unique_id
unique id for any new parameter set
Definition QSpace.h:187
void erase(size_t i)
Erases q parameter i.
Definition QSpace.h:152
std::list< qspace::QParam< Q > > m_params
q parameter sets with new parameters first
Definition QSpace.h:188
std::shared_ptr< Logger > m_logger
Definition QSpace.h:186
VecRef< Q > actions()
Definition QSpace.h:172
QSpace(std::shared_ptr< ArrayHandlers< R, Q, P > > handlers, std::shared_ptr< Logger > logger)
Definition QSpace.h:67
CVecRef< Q > params() const
Definition QSpace.h:165
CVecRef< Q > actions() const
Definition QSpace.h:177
Parameter in the Q space.
Definition QSpace.h:24
std::unique_ptr< Q > param
parameter
Definition QSpace.h:25
std::unique_ptr< Q > action
corresponding action
Definition QSpace.h:26
size_t id
unique id
Definition QSpace.h:27