1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_XSPACE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_SUBSPACE_XSPACE_H
4#include <molpro/linalg/itsolv/helper.h>
5#include <molpro/linalg/itsolv/subspace/DSpace.h>
6#include <molpro/linalg/itsolv/subspace/Dimensions.h>
7#include <molpro/linalg/itsolv/subspace/IXSpace.h>
8#include <molpro/linalg/itsolv/subspace/PSpace.h>
9#include <molpro/linalg/itsolv/subspace/QSpace.h>
10#include <molpro/linalg/itsolv/subspace/util.h>
49template <
class R,
class Q,
class P>
55 auto nQnew = params.size();
120template <
class Q,
class P>
124 const auto nP = pparams.size();
125 const auto nQ = qparams.size();
126 const auto nX = nP + nQ;
127 auto nD = dparams.size();
128 const auto nRHS = rhs.size();
130 data.
qq[EqnData::S].slice() = util::overlap(dparams, handler_qq);
131 data.qx[EqnData::S].slice({0, 0}, {nD, nP}) = util::overlap(dparams, pparams, handler_qp);
132 data.qx[EqnData::S].slice({0, nP}, {nD, nX}) = util::overlap(dparams, qparams, handler_qq);
133 data.qq[EqnData::rhs].slice() = util::overlap(dparams, rhs, handler_qq);
135 logger.
data_dump(
"xspace::update_dspace_overlap_data() nD = ", nD);
136 logger.
data_dump(
"Sdd = ", data.qq[EqnData::S]);
137 logger.
data_dump(
"Sdx = ", data.qx[EqnData::S]);
138 logger.
data_dump(
"rhs_d = ", data.qq[EqnData::rhs]);
143template <
class Q,
class P>
148 const auto nP = pparams.size();
149 const auto nQ = qparams.size();
150 const auto nX = nP + nQ;
151 auto nD = dparams.size();
153 const auto e = EqnData::H;
154 data.
qq[e].slice() = util::overlap(dparams, dactions, handler_qq);
155 data.xq[e].slice({0, 0}, {nP, nD}) = util::overlap(pparams, dactions, handler_qp);
156 data.xq[e].slice({nP, 0}, {nX, nD}) = util::overlap(qparams, dactions, handler_qq);
157 data.qx[e].slice({0, nP}, {nD, nX}) = util::overlap(dparams, qactions, handler_qq);
159 logger.
data_dump(
"xspace::update_dspace_action_data() nD = ", nD);
175template <
typename value_type>
177 const std::vector<std::size_t>& q_delete,
179 using matrix_type = Eigen::Matrix<value_type, Eigen::Dynamic, Eigen::Dynamic>;
181 auto kept = std::vector<size_t>{}, combined = std::vector<size_t>{};
182 for (
size_t i = 0; i < dims.
nP; ++i)
183 kept.push_back(dims.
oP() + i);
184 for (
size_t i = 0; i < dims.
nQ; ++i)
185 if (std::find(q_delete.begin(), q_delete.end(), i) == q_delete.end())
186 kept.push_back(dims.
oQ() + i);
187 for (
auto iq : q_delete)
188 combined.push_back(dims.
oQ() + iq);
189 for (
size_t i = 0; i < dims.
nD; ++i)
190 combined.push_back(dims.
oD() + i);
191 const auto nK = kept.
size(), nB = combined.size(), nDnew = dcoefficients.
rows();
193 matrix_type m_kb(nK, nB), m_bb(nB, nB), c(nDnew, nB);
194 for (
size_t k = 0; k < nK; ++k)
195 for (
size_t j = 0; j < nB; ++j)
196 m_kb(k, j) = metric(kept[k], combined[j]);
197 for (
size_t i = 0; i < nB; ++i)
198 for (
size_t j = 0; j < nB; ++j)
199 m_bb(i, j) = metric(combined[i], combined[j]);
200 for (
size_t i = 0; i < nDnew; ++i)
201 for (
size_t j = 0; j < nB; ++j)
202 c(i, j) = dcoefficients(i, j);
204 const matrix_type m_kd = m_kb * c.transpose();
205 const matrix_type m_dd = c.conjugate() * m_bb * c.transpose();
206 const auto nXnew = nK + nDnew;
208 for (
size_t k = 0; k < nK; ++k)
209 for (
size_t l = 0; l < nK; ++l)
210 m_new(k, l) = metric(kept[k], kept[l]);
211 for (
size_t k = 0; k < nK; ++k)
212 for (
size_t i = 0; i < nDnew; ++i) {
213 m_new(k, nK + i) = m_kd(k, i);
214 m_new(nK + i, k) =
conjugate(m_kd(k, i));
216 for (
size_t i = 0; i < nDnew; ++i)
217 for (
size_t l = 0; l < nDnew; ++l)
218 m_new(nK + i, nK + l) = m_dd(i, l);
225 const auto& dd = new_data.
qq.at(e);
226 const auto& dx = new_data.
qx.at(e);
227 const auto& xd = new_data.
xq.at(e);
228 data[e].slice({dims.
oD(), dims.
oD()}, {dims.
oD() + dims.
nD, dims.
oD() + dims.
nD}) = dd;
229 data[e].slice({dims.
oD(), dims.
oP()}, {dims.
oD() + dims.
nD, dims.
oP() + dims.
nP}) =
230 dx.slice({0, 0}, {dims.nD, dims.nP});
231 data[e].slice({dims.
oD(), dims.
oQ()}, {dims.
oD() + dims.
nD, dims.
oQ() + dims.
nQ}) =
232 dx.slice({0, dims.nP}, {dims.nD, dims.nP + dims.nQ});
233 data[e].slice({dims.
oP(), dims.
oD()}, {dims.
oP() + dims.
nP, dims.
oD() + dims.
nD}) =
234 xd.slice({0, 0}, {dims.nP, dims.nD});
235 data[e].slice({dims.
oQ(), dims.
oD()}, {dims.
oQ() + dims.
nQ, dims.
oD() + dims.
nD}) =
236 xd.slice({dims.nP, 0}, {dims.nP + dims.nQ, dims.nD});
240template <
class R,
class Q,
class P>
246 using IXSpace<
R,
Q,
P>::stores_metric_actions;
249 : pspace(), qspace(handlers, logger), dspace(logger), m_handlers(handlers), m_logger(logger) {
250 data = null_data<value_type, EqnData::H, EqnData::S, EqnData::rhs>();
254 if (kind == m_metric_kind && storage == m_metric_storage)
257 throw std::logic_error(
"XSpace::set_metric: the metric cannot be changed once the subspace is not empty");
258 m_metric_kind = kind;
259 m_metric_storage = storage;
260 if (metric_kind() != MetricKind::Identity)
261 data[EqnData::M] = {};
263 data.erase(EqnData::M);
271 const CVecRef<R>& metric_actions = {})
override {
272 m_logger->trace(
"QSpace::update_qspace");
273 if (metric_actions.size() != (metric_kind() != MetricKind::Identity ? params.size() : 0))
274 throw std::invalid_argument(
275 "XSpace::update_qspace: actions of the metric must be given for every parameter exactly when there is a "
277 auto new_data = xspace::update_qspace_data(params, actions, cparamsp(), cparamsq(), cactionsq(), cparamsd(),
278 cactionsd(),
cwrap(m_rhs), m_dim, *m_handlers, *m_logger, m_hermitian,
279 m_action_dot_action, metric_actions);
280 qspace.update(params, actions, new_data.qq, new_data.qx, new_data.xq, m_dim, data,
281 stores_metric_actions() ? metric_actions :
CVecRef<R>{});
287 const std::vector<size_t>& q_delete = {},
const Matrix<value_type>& coefficients = {})
override {
288 if (metric_actions.size() != (stores_metric_actions() ? params.size() : 0))
289 throw std::invalid_argument(
290 "XSpace::update_dspace: actions of the metric must be given exactly when they are stored");
291 if (metric_kind() != MetricKind::Identity && !params.empty() &&
292 (coefficients.rows() != params.size() || coefficients.cols() != q_delete.size() + m_dim.nD))
293 throw std::invalid_argument(
"XSpace::update_dspace: the coefficients of the new D vectors are needed");
295 const auto metric_matrix = metric_kind() != MetricKind::Identity && !params.empty()
296 ? xspace::metric_with_new_dspace(data.at(EqnData::M), m_dim, q_delete, coefficients)
297 : Matrix<value_type>{};
305 data[
e].resize({m_dim.nX(), m_dim.nX()});
307 m_handlers->qp(), m_handlers->qq(), *m_logger);
310 cparamsp(), cparamsq(), cactionsq(), cparamsd(), cactionsd(), m_handlers->qp(), m_handlers->qq(), *m_logger);
324 throw std::runtime_error(
"P space can only be used with hermitian kernels");
325 const size_t nPnew = params.size();
327 throw std::
invalid_argument(
"XSpace::update_pspace: the metric matrix in the P space must be given exactly when "
328 "there is a metric");
329 pspace.update(params, m_handlers->pp());
331 const size_t nP = m_dim.nP;
332 update_rhs_with_pspace();
336 for (
size_t i = 0,
ij = 0;
i < nP; ++
i)
337 for (
size_t j = 0;
j < nP; ++
j, ++
ij)
341 for (
size_t i = 0,
ij = 0;
i < nP; ++
i)
342 for (
size_t j = 0;
j < nP; ++
j, ++
ij)
349 m_rhs.reserve(m_rhs.size() +
rhs.size());
350 for (
const auto& r :
rhs)
351 m_rhs.emplace_back(
this->m_handlers->qr().copy(r));
352 for (
const auto& r :
rhs) {
353 auto d = std::abs(this->m_handlers->rr().dot(r, r));
355 throw std::runtime_error(
"RHS vector cannot be zero");
356 m_rhs_norm.emplace_back(std::sqrt(
d));
359 update_rhs_with_pspace();
366 const std::vector<value_type_abs>&
rhs_norm()
const {
return m_rhs_norm; }
371 if (
i >= m_dim.oP() &&
i < m_dim.oP() + m_dim.nP) {
372 erasep(
i - m_dim.oP());
373 }
else if (
i >= m_dim.oQ() &&
i < m_dim.oQ() + m_dim.nQ) {
374 eraseq(
i - m_dim.oQ());
375 }
else if (
i >= m_dim.oD() &&
i < m_dim.oD() + m_dim.nD) {
376 erased(
i - m_dim.oD());
382 remove_data(m_dim.oQ() +
i);
388 remove_data(m_dim.oP() +
i);
394 remove_data(m_dim.oD() +
i);
420 qspace.set_logger(logger);
421 dspace.set_logger(logger);
422 m_logger = std::move(logger);
437 m_dim.nRHS = m_rhs.size();
448 data[
d].remove_row_col(
i,
i);
459 bool m_hermitian =
false;
460 bool m_action_dot_action =
false;
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition Span.h:31
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 data_dump(std::string_view what, const T &data) const
Dumps data (context log::DataDump<T>, verbosity Trace). Nothing is copied.
Definition Logger.h:496
D space stores complement of P+Q+R subspace that is necessary to reproduce the previous solutions.
Definition DSpace.h:17
size_t size() const
Definition DSpace.h:56
Full subspace.
Definition IXSpace.h:21
R R
Definition IXSpace.h:23
P P
Definition IXSpace.h:25
typename array::ArrayHandler< R, R >::value_type_abs value_type_abs
Definition IXSpace.h:27
Q Q
Definition IXSpace.h:24
typename array::ArrayHandler< R, R >::value_type value_type
Definition IXSpace.h:26
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
size_t size() const
Definition Matrix.h:172
index_type rows() const
Definition Matrix.h:169
size_t size() const
Definition PSpace.h:24
CVecRef< Q > cparamsq() const override
Definition XSpace.h:411
bool get_hermiticity()
Definition XSpace.h:427
CVecRef< Q > cactionsq() const override
Definition XSpace.h:412
void remove_data(size_t i)
Definition XSpace.h:446
void update_dimensions()
Definition XSpace.h:435
std::vector< Q > m_rhs
Right hand side vectors.
Definition XSpace.h:457
CVecRef< P > cparamsp() const override
Definition XSpace.h:410
void update_qspace(const CVecRef< R > ¶ms, const CVecRef< R > &actions, const CVecRef< R > &metric_actions={}) override
Update parameters in Q space and corresponding equation data.
Definition XSpace.h:270
CVecRef< Q > cparamsd() const override
Definition XSpace.h:413
void update_dspace(VecRef< Q > ¶ms, VecRef< Q > &actions, const VecRef< Q > &metric_actions={}, const std::vector< size_t > &q_delete={}, const Matrix< value_type > &coefficients={}) override
Replaces the old D space and the Q vectors q_delete by new D vectors, see IXSpace::update_dspace()
Definition XSpace.h:286
void add_rhs_equations(const CVecRef< R > &rhs)
For a system of linear equations Ax=b, adds rhs vectors b.
Definition XSpace.h:348
void erased(size_t i) override
Removes parameter i from D subspace.
Definition XSpace.h:392
CVecRef< Q > cmetric_actionsq() const override
Actions of the metric on the Q space parameters, empty unless there is a metric.
Definition XSpace.h:416
VecRef< Q > actionsd() override
Definition XSpace.h:402
std::vector< value_type_abs > m_rhs_norm
norm of RHS vectors
Definition XSpace.h:458
PSpace< R, P > pspace
Definition XSpace.h:430
CVecRef< Q > rhs() const
Access RHS vectors in linear equations.
Definition XSpace.h:363
CVecRef< Q > paramsq() const override
Definition XSpace.h:405
Dimensions m_dim
Definition XSpace.h:456
CVecRef< Q > actionsq() const override
Definition XSpace.h:406
std::shared_ptr< Logger > m_logger
Definition XSpace.h:455
VecRef< Q > paramsq() override
Definition XSpace.h:399
CVecRef< Q > paramsd() const override
Definition XSpace.h:407
VecRef< P > paramsp() override
Definition XSpace.h:398
VecRef< Q > paramsd() override
Definition XSpace.h:401
const Dimensions & dimensions() const override
Definition XSpace.h:368
CVecRef< Q > cmetric_actionsd() const override
Actions of the metric on the D space parameters, empty unless there is a metric.
Definition XSpace.h:417
VecRef< Q > actionsq() override
Definition XSpace.h:400
void set_metric(MetricKind kind, MetricStorage storage=MetricStorage::Stored) override
Declares the metric of the subspace. Anything other than MetricKind::Identity creates the metric matr...
Definition XSpace.h:253
CVecRef< P > paramsp() const override
Definition XSpace.h:404
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Definition XSpace.h:454
void eraseq(size_t i) override
Removes parameter i from Q subspace.
Definition XSpace.h:380
const std::vector< value_type_abs > & rhs_norm() const
Norm of RHS vectors.
Definition XSpace.h:366
auto update_rhs_with_pspace()
Update projection of RHS data onto P space.
Definition XSpace.h:441
void erasep(size_t i) override
Removes parameter i from P subspace.
Definition XSpace.h:386
QSpace< R, Q, P > qspace
Definition XSpace.h:431
void set_logger(std::shared_ptr< Logger > logger) override
Definition XSpace.h:419
void set_action_action()
Definition XSpace.h:428
CVecRef< Q > actionsd() const override
Definition XSpace.h:408
CVecRef< Q > cactionsd() const override
Definition XSpace.h:414
void update_pspace(const CVecRef< P > ¶ms, const array::Span< value_type > &pp_action_matrix, const array::Span< value_type > &pp_metric_matrix={}) override
Definition XSpace.h:320
void erase(size_t i) override
Removes parameter i from the full subspace.
Definition XSpace.h:370
DSpace< Q > dspace
Definition XSpace.h:432
XSpace(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger)
Definition XSpace.h:248
void set_hermiticity(bool hermitian)
Set Hermiticity of the subspace. P space can only be used with Hermitian problems.
Definition XSpace.h:426
MetricKind metric_kind() const override
The kind of metric declared with set_metric()
Definition XSpace.h:266
MetricStorage metric_storage() const override
How the actions of the metric declared with set_metric() are obtained.
Definition XSpace.h:267
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
auto update_qspace_data(const CVecRef< R > ¶ms, const CVecRef< R > &actions, const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &qactions, const CVecRef< Q > &dparams, const CVecRef< Q > &dactions, const CVecRef< Q > &rhs, const Dimensions &dims, ArrayHandlers< R, Q, P > &handlers, Logger &logger, bool hermitian=false, bool action_dot_action=false, const CVecRef< R > &metric_actions={})
Returns new sections of equation data.
Definition XSpace.h:50
auto update_dspace_action_data(const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &qactions, const CVecRef< Q > &dparams, const CVecRef< Q > &dactions, array::ArrayHandler< Q, P > &handler_qp, array::ArrayHandler< Q, Q > &handler_qq, Logger &logger)
Calculates action blocks between D space and the rest of the subspace.
Definition XSpace.h:144
void copy_dspace_eqn_data(const NewData< T > &new_data, SubspaceData< T > &data, const subspace::EqnData e, const Dimensions &dims)
Definition XSpace.h:223
Matrix< value_type > metric_with_new_dspace(const Matrix< value_type > &metric, const Dimensions &dims, const std::vector< std::size_t > &q_delete, const Matrix< value_type > &dcoefficients)
The metric matrix of the subspace after the deletion of Q vectors and their replacement,...
Definition XSpace.h:176
auto update_dspace_overlap_data(const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &dparams, const CVecRef< Q > &rhs, array::ArrayHandler< Q, P > &handler_qp, array::ArrayHandler< Q, Q > &handler_qq, Logger &logger)
Calculates overlap blocks between D space and the rest of the subspace.
Definition XSpace.h:121
EqnData
Kinds of equation data in the subspace.
Definition SubspaceData.h:14
void conjugate_transpose_copy(ML &&ml, const MR &mr)
Copies the conjugate transpose of mr into ml.
Definition Matrix.h:299
std::map< EqnData, Matrix< T > > SubspaceData
Equation data blocks of the subspace, in the scalar type of the problem.
Definition SubspaceData.h:18
std::vector< EqnData > square_blocks(const SubspaceData< T > &data)
The square matrices, H, S and, if present, M, among the equation data.
Definition SubspaceData.h:22
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
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
MetricStorage
How a solver obtains the actions of a metric on its subspace vectors, see IterativeSolver::set_metric...
Definition Metric.h:21
@ Stored
the actions of the metric on all subspace vectors are kept
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
@ Identity
M = 1, i.e. no metric.
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
Definition Dimensions.h:9
size_t oP() const
offset of the P block
Definition Dimensions.h:17
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 oD() const
offset of the D block
Definition Dimensions.h:19
Container storing the Q space parameters.
Definition QSpace.h:62
size_t size() const
Definition QSpace.h:170
New sections of equation data.
Definition XSpace.h:23
SubspaceData< T > xq
data block between current X space and new parameters
Definition XSpace.h:39
SubspaceData< T > qx
data block between new parameters and current X space
Definition XSpace.h:38
NewData(size_t nQnew, size_t nX, size_t nRHS, bool metric=false)
Definition XSpace.h:25
SubspaceData< T > qq
data block between new paramters
Definition XSpace.h:37