1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_PROPOSE_RSPACE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_PROPOSE_RSPACE_H
4#include <molpro/linalg/itsolv/IterativeSolver.h>
5#include <molpro/linalg/itsolv/helper.h>
6#include <molpro/linalg/itsolv/qspace_options.h>
7#include <molpro/linalg/itsolv/rspace_options.h>
8#include <molpro/linalg/itsolv/subspace/Dimensions.h>
9#include <molpro/linalg/itsolv/subspace/ISubspaceSolver.h>
10#include <molpro/linalg/itsolv/subspace/IXSpace.h>
11#include <molpro/linalg/itsolv/subspace/QSpace.h>
12#include <molpro/linalg/itsolv/subspace/gram_schmidt.h>
13#include <molpro/linalg/itsolv/subspace/util.h>
14#include <molpro/linalg/itsolv/util.h>
15#include <molpro/linalg/itsolv/wrap_util.h>
16#include <molpro/profiler/Profiler.h>
39 for (
auto&
p : params) {
41 if (dot >
thresh and std::isfinite(dot)) {
42 handler.
scal(1. / dot,
p);
44 logger.
warn(
"parameter's length is too small for normalisation, dot = " + std::format(
"{:.2e}",
double(dot)));
50template <
class R,
class Q,
class P,
typename Index>
52 std::sort(begin(indices), end(indices), std::greater<Index>());
53 for (
auto i : indices)
67 auto basis = std::vector<std::size_t>{};
70 for (
size_t k = 0;
k <
dims.nD; ++
k)
83template <
typename value_type>
86 logger.
trace(
"construct_projected_solution()");
91 for (
size_t i = 0;
i <
nSol; ++
i)
92 for (
size_t j = 0;
j <
basis.size(); ++
j)
110template <
typename value_type>
113 const std::vector<std::size_t>& indices) {
117 for (
size_t i = 0;
i <
nSol; ++
i)
118 for (
size_t m = 0;
m < indices.size(); ++
m)
119 for (
size_t k = 0;
k <
basis.size(); ++
k)
135template <
typename value_type>
141 for (
size_t i = 0;
i <
nSol; ++
i) {
142 for (
size_t j = 0;
j <=
i; ++
j) {
143 for (
size_t l = 0; l <
basis.size(); ++l)
160template <
typename value_type>
165 logger.
trace(
"construct_projected_solution_overlap()");
176template <
typename value_type,
typename value_type_abs>
178 const value_type_abs norm_thresh,
Logger& logger) {
179 logger.
trace(
"remove_null_norm_and_normalise()");
180 const auto nSol = parameters.
rows();
182 for (
size_t i = 0;
i <
nSol; ++
i)
184 for (
size_t i = 0,
j = 0;
i <
nSol; ++
i) {
192 overlap.remove_row_col(
j,
j);
193 std::stringstream
ss;
194 ss << std::setprecision(3) <<
"remove projected solution parameter i = " <<
i <<
", norm = " <<
norm_proj[
i];
199 logger.
data_dump(
"parameters after normalisation = ", parameters);
200 logger.
data_dump(
"overlap of parameters = ", overlap);
215template <
typename value_type,
typename value_type_abs>
219 logger.
trace(
"remove_null_projected_solutions()");
221 value_type*
m =
const_cast<std::vector<value_type>&
>(
overlap_proj.data()).data();
223 std::numeric_limits<value_type_abs>::max(),
true);
230 for (
size_t i = 0;
i < nD; ++
i, ++
svd)
232 for (
size_t k = 0;
k < nX; ++
k)
250template <
typename value_type,
typename value_type_abs>
253 const std::vector<std::size_t>&
remove_qspace,
const value_type_abs norm_thresh,
254 const value_type_abs svd_thresh,
Logger& logger) {
273template <
typename value_type>
281 for (
size_t i = 0;
i <
dims.nD; ++
i) {
287 for (
size_t i = 0,
j = 0;
i <
dims.nQ; ++
i) {
296 auto remaining = std::vector<std::size_t>{};
297 for (
size_t j = 0;
j <
dims.nP; ++
j)
299 for (
size_t j = 0;
j <
dims.nQ; ++
j)
302 for (
size_t j = 0;
j <
nR; ++
j)
308 for (
size_t i = 0;
i <
nDnew; ++
i) {
309 for (
size_t j = 0;
j <
oDnew; ++
j) {
332template <
class R,
class Q,
class P,
typename value_type>
336 logger.
trace(
"append_overlap_with_r()");
338 const auto nX = nP + nQ + nD +
nN;
340 const auto oQ = oP + nP;
341 const auto oD = oQ + nQ;
342 const auto oN = oD + nD;
350 for (
size_t i = 0;
i < nX; ++
i)
351 for (
size_t j = 0;
j <
nN; ++
j)
366template <
class R,
class Q,
class P,
typename value_type>
382template <
typename value_type>
385 using value_type_abs =
decltype(std::abs(solutions(0, 0)));
386 using contrib_pair = std::pair<std::size_t, value_type_abs>;
390 logger.
trace(
"limit_qspace_size()");
407 const std::size_t
nSol = solutions.
rows();
412 for (std::size_t
idx : std::ranges::views::iota(std::size_t(0),
dims.nQ)) {
415 for (
size_t j = 0;
j <
nSol; ++
j) {
416 const value_type_abs
current = std::abs(solutions(
j,
dims.oQ() +
idx));
423 logger.
trace(
"contribution to solutions =",
432 logger.
warn(
"deleted Q vector with max. contribution = ",
contrib);
434 logger.
debug(
"deleted Q vector with max. contribution = ",
contrib);
456template <
class R,
class Q,
class P,
typename value_type,
typename value_type_abs>
458 const std::vector<std::size_t>&
q_delete,
const value_type_abs norm_thresh,
469 assert(
svd_vecs.empty() &&
"P+Q+D subspace should be stable by construction");
484 auto basis = std::vector<size_t>{};
487 for (
size_t k = 0;
k <
dims.nD; ++
k)
490 for (
size_t j = 0;
j <
basis.size(); ++
j)
491 for (
size_t k = 0;
k <
basis.size(); ++
k)
494 for (
size_t i = 0;
i < nD; ++
i) {
495 if (
norms[
i] < norm_thresh) {
496 logger.
warn(std::format(
"construct_dspace: skipping normalisation of D vector {} with near-zero norm = {:.2e}",
507 for (
size_t i = 0;
i < nD; ++
i) {
515 for (
size_t i = 0;
i < nD; ++
i)
563template <
class R,
class Q,
class P,
typename value_type,
typename value_type_abs>
569 logger.
trace(
"project_out_and_orthonormalise()");
577 prof->start(
"orthoganalise");
578 const auto nX = nP + nQ + nD;
579 if (
nR > 0
and nX > 0) {
580 using EMatrix = Eigen::Matrix<value_type, Eigen::Dynamic, Eigen::Dynamic>;
582 for (
size_t i = 0;
i < nX; ++
i)
583 for (
size_t j = 0;
j < nX; ++
j)
584 S(
i,
j) = overlap(
i,
j);
586 Eigen::Vector<value_type, Eigen::Dynamic>
scale(nX);
587 for (
size_t i = 0;
i < nX; ++
i)
588 scale(
i) = std::abs(S(
i,
i)) > 0 ? 1 / std::sqrt(std::abs(S(
i,
i))) : 0;
589 const auto Sinv = (
scale.asDiagonal() * S *
scale.asDiagonal()).eval().completeOrthogonalDecomposition();
592 for (
size_t i = 0;
i < nX; ++
i)
593 for (
size_t j = 0;
j <
nR; ++
j)
600 for (
size_t i = 0;
i <
n; ++
i)
601 for (
size_t j = 0;
j <
nR; ++
j)
610 prof->start(
"get null_params");
612 for (
size_t i = 0;
i <
nR; ++
i) {
614 if (norm > norm_thresh) {
616 for (
size_t j =
i + 1;
j <
nR; ++
j) {
670template <
class R,
class Q,
class P>
676 logger.
trace(
"itsolv::detail::propose_rspace");
679 profiler.
start(
"itsolv::ISubspaceSolver::solutions");
688 auto prof = profiler.
push(
"construct_dspace");
706 profiler.
start(
"normalise");
709 profiler.
start(
"append_overlap_with_r");
714 profiler.
start(
"redundant_indices");
722 auto kept = std::vector<size_t>{};
727 for (
size_t j = 0;
j <
kept.size(); ++
j)
728 for (
size_t i = 0;
i < nX; ++
i)
731 profiler.
start(
"project_out_and_orthonormalise");
742 profiler.
start(
"get_new_working_set");
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
virtual void scal(value_type alpha, AL &x)=0
virtual void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)=0
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
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
Base class defining the interface common to all iterative solvers.
Definition IterativeSolver.h:361
virtual const std::vector< int > & working_set() const =0
Working set of roots that are not yet converged.
Logger class implementation. Provides (customizable) logging functionalities.
Definition Logger.h:370
void info(std::string_view message, const Ts &...args) const
Definition Logger.h:473
void warn(std::string_view message, const Ts &...args) const
Definition Logger.h:478
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
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
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 CVecRef< P > cparamsp() const =0
virtual MetricKind metric_kind() const =0
The kind of metric declared with set_metric()
size_t size() const
Number of vectors forming the subspace.
Definition IXSpace.h:33
virtual CVecRef< Q > cactionsd() const =0
virtual void eraseq(size_t i)=0
Removes parameter i from Q subspace.
virtual 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={})=0
Replaces the old D space and the Q vectors q_delete by new D vectors, which are linear combinations o...
virtual CVecRef< Q > cactionsq() const =0
virtual CVecRef< Q > cparamsd() const =0
virtual CVecRef< Q > cmetric_actionsd() const =0
Actions of the metric on the D space parameters, empty unless there is a metric.
bool stores_metric_actions() const
Whether the actions of the metric on the Q and D vectors are kept.
Definition IXSpace.h:65
virtual const Dimensions & dimensions() const =0
virtual CVecRef< Q > cmetric_actionsq() const =0
Actions of the metric on the Q space parameters, empty unless there is a metric.
Slice & scal(T a)
Scale all elements of the slice.
Definition Matrix.h:230
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
Slice row(size_t i)
Access row slice.
Definition Matrix.h:122
void remove_row(index_type row)
removes a row from the matrix
Definition Matrix.h:146
index_type rows() const
Definition Matrix.h:169
Profiler & start(const std::string &name)
Profiler & stop(const std::string &name="")
Proxy push(const std::string &name)
static std::shared_ptr< Profiler > single()
auto projected_solutions_overlap(const subspace::Matrix< value_type > &solutions_proj, const subspace::Matrix< value_type > &overlap, const std::vector< std::size_t > &basis, const std::vector< std::size_t > &indices)
Constructs overlaps of projected solutions with subspace vectors.
Definition propose_rspace.h:111
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
auto construct_projected_solution(const subspace::Matrix< value_type > &solutions, const subspace::Dimensions &dims, const std::vector< std::size_t > &remove_qspace, Logger &logger)
Projects solution from the full subspace on to Q_{delete} and current D space.
Definition propose_rspace.h:84
auto construct_projected_solutions_overlap(const subspace::Matrix< value_type > &solutions_proj, const subspace::Matrix< value_type > &overlap, const subspace::Dimensions &dims, const std::vector< std::size_t > &remove_qspace, Logger &logger)
Constructs overlap matrix for projected solutions.
Definition propose_rspace.h:161
auto remove_null_projected_solutions(const subspace::Matrix< value_type > &solutions_proj, const subspace::Matrix< value_type > &overlap_proj, const value_type_abs svd_thresh, Logger &logger)
Transforms to a stable subspace of projected solutions via SVD.
Definition propose_rspace.h:216
void remove_null_norm_and_normalise(subspace::Matrix< value_type > ¶meters, subspace::Matrix< value_type > &overlap, const value_type_abs norm_thresh, Logger &logger)
Removes parameters with norm less than threshold and normalises the rest.
Definition propose_rspace.h:177
auto construct_full_subspace_overlap(const subspace::Matrix< value_type > &solutions_proj, const subspace::Dimensions &dims, const std::vector< std::size_t > &remove_qspace, const subspace::Matrix< value_type > &overlap, const size_t nR)
Constructs overlap matrix of P+Q+R+(projected solutions) subspaces, where Q is without removed parame...
Definition propose_rspace.h:274
std::vector< std::size_t > projected_solutions_basis(const subspace::Dimensions &dims, const std::vector< std::size_t > &remove_qspace)
Returns subspace indices of the vectors u_k that the columns of a projected solutions matrix refer to...
Definition propose_rspace.h:65
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
void normalise(VecRef< R > ¶ms, array::ArrayHandler< R, R > &handler, Logger &logger, typename array::ArrayHandler< R, R >::value_type_abs thresh=1/std::numeric_limits< typename array::ArrayHandler< R, R >::value_type_abs >::max())
Definition propose_rspace.h:36
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
auto propose_rspace(IterativeSolver< R, Q, P > &solver, const VecRef< R > ¶meters, const VecRef< R > &residuals, subspace::IXSpace< R, Q, P > &xspace, subspace::ISubspaceSolver< R, Q, P > &subspace_solver, ArrayHandlers< R, Q, P > &handlers, Logger &logger, const RSpaceOptions< typename array::ArrayHandler< R, R >::value_type_abs > &r_opts, const QSpaceOptions &q_opts, molpro::profiler::Profiler &profiler)
Proposes new parameters for the subspace from the preconditioned residuals.
Definition propose_rspace.h:671
auto construct_dspace(const subspace::Matrix< value_type > &solutions, const subspace::IXSpace< R, Q, P > &xspace, const std::vector< std::size_t > &q_delete, const value_type_abs norm_thresh, const value_type_abs svd_thresh, array::ArrayHandler< Q, Q > &handler, Logger &logger)
Constructs the new D space by projecting the solutions onto Qd+D subspace and ensuring they are well ...
Definition propose_rspace.h:457
auto project_out_and_orthonormalise(const VecRef< R > &rparams, const subspace::Matrix< value_type > &overlap, const subspace::Matrix< value_type > &rx_overlap, const subspace::Dimensions &dims, const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &dparams, const value_type_abs norm_thresh, ArrayHandlers< R, Q, P > &handlers, Logger &logger)
Projects R parameters out of the P+Q+D subspace and orthonormalises them among themselves.
Definition propose_rspace.h:564
auto get_new_working_set(const std::vector< int > &working_set, const CVecRef< R > ¶ms, const CVecRef< R > &wparams)
Returns new working set based on parameters included in wparams.
Definition propose_rspace.h:630
auto append_overlap_with_r(const subspace::Matrix< value_type > &overlap, const CVecRef< R > ¶ms, const CVecRef< P > &pparams, const CVecRef< Q > &qparams, const CVecRef< Q > &dparams, ArrayHandlers< R, Q, P > &handlers, Logger &logger)
Constructs overlap of the full subspace by appending overlap with new parameters to the overlap of pr...
Definition propose_rspace.h:333
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 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:960
Matrix< T > unit_length_solutions(const Matrix< T > &solutions, const Matrix< T > &overlap)
The subspace coefficients of the solutions scaled to unit Euclidean length, so that their sizes can b...
Definition util.h:106
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
std::vector< real_type_t< T > > solution_norms(const Matrix< T > &solutions, const Matrix< T > &overlap)
The Euclidean norms of the solutions x_i = sum_j c_ij b_j, from their subspace coefficients c (one ro...
Definition util.h:82
Q construct_zeroed_copy(const R ¶m, 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
auto norm(array::ArrayHandler< R, R > &handler, const std::type_identity_t< R > &x) -> typename array::ArrayHandler< R, R >::value_type_abs
The length of x, sqrt(|<x|x>|), which is real also for complex values.
Definition util.h:21
void delete_parameters(std::vector< int > indices, Container ¶ms)
Removes parameters.
Definition util.h:128
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::list< SVD< value_type > > svd_system(size_t nrows, size_t ncols, const array::Span< value_type > &m, real_type_t< value_type > threshold, bool hermitian=false, bool reduce_to_rank=false)
Performs singular value decomposition and returns SVD objects for singular values less than threshold...
Definition helper-implementation.h:268
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
@ Identity
M = 1, i.e. no metric.
std::vector< size_t > find_ref(const VecRef< R > &wparams, ForwardIt begin, ForwardIt end)
Given wrapped references in wparams and a range of original parameters [begin, end),...
Definition wrap_util.h:17
real_type_t< T > real_part(const T &x)
The real part of a scalar; the value itself for a real type.
Definition scalar_traits.h:58
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 nD
Definition Dimensions.h:14
size_t nQ
Definition Dimensions.h:13
size_t nP
Definition Dimensions.h:12
size_t nX() const
total size of the X space
Definition Dimensions.h:16
Manages solution of the subspace problem and storage of those solutions.
Definition ISubspaceSolver.h:21