MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
vector.hpp
1#ifndef MPQC_CI_VECTOR_HPP
2#define MPQC_CI_VECTOR_HPP
3
4#include "mpqc/ci/subspace.hpp"
5#include "mpqc/math/matrix.hpp"
6#include "mpqc/array.hpp"
7#include "mpqc/mpi.hpp"
8
9#include "mpqc/utility/profile.hpp"
10
11namespace mpqc {
12namespace ci {
13
16
18 struct Vector : boost::noncopyable {
19
25 Vector(std::string name, const SubspaceGrid &G, MPI::Comm comm, bool incore) {
26 blocks_.resize(G.alpha().size(), G.beta().size());
27 size_t dets = 0;
28 for (size_t j = 0; j < G.beta().size(); ++j) {
29 for (size_t i = 0; i < G.alpha().size(); ++i) {
30 Block b;
31 b.rows = G.alpha().at(i).size();
32 b.cols = G.beta().at(j).size();
33 b.begin = dets;
34 b.allowed = G.allowed(i,j);
35 if (b.allowed) {
36 dets += b.rows*b.cols;
37 }
38 blocks_(i,j) = b;
39 }
40 }
41 MPQC_CHECK(dets == G.dets());
42 BOOST_FOREACH (const auto &s, G.alpha())
43 this->alpha_.push_back(s);
44 BOOST_FOREACH (const auto &s, G.beta())
45 this->beta_.push_back(s);
46 std::vector<size_t> extents;
47 extents.push_back(dets);
48 this->array_ = (incore)
49 ? Array<double>(name, extents, ARRAY_CORE, comm)
50 : Array<double>(name, extents, ARRAY_FILE, comm);
51 }
52
53
55 struct Block1d {
56 private:
57 Array<double> array_;
58 friend void operator<<(Vector::Block1d block, const mpqc::Vector &v);
59 friend void operator>>(Vector::Block1d block, mpqc::Vector &v);
60 private:
61 friend class Vector;
62 Block1d(Array<double> array) : array_(array) {}
63 };
64
68 return Block1d(this->array_(r));
69 }
70
71
75 void operator=(const mpqc::Matrix &m) const {
76 MPQC_CHECK(m.rows() == this->rows_);
77 MPQC_CHECK(m.cols() == this->cols_);
78 this->array_ << m;
79 }
81 void assign_to(mpqc::Matrix &m) const {
82 m.resize(this->rows_, this->cols_);
83 this->array_ >> m;
84 }
85 private:
86 friend class Vector;
87 Block2d(Array<double> array, size_t rows, size_t cols)
88 : array_(array), rows_(rows), cols_(cols) {}
89 private:
90 Array<double> array_;
91 size_t rows_, cols_;
92 };
93
96 Block b = this->block(A,B);
97 return Block2d(this->array_(b.range()), b.rows, b.cols);
98 }
99
100 void sync() {
101 this->array_.sync();
102 }
103
104 private:
105
106 struct Block {
107 size_t rows, cols;
108 size_t begin;
109 bool allowed;
110 mpqc::range range() const {
111 return mpqc::range(begin, begin+rows*cols);
112 }
113 };
114
115 std::vector<mpqc::range> alpha_;
116 std::vector<mpqc::range> beta_;
117 mpqc::Array<double> array_;
118 mpqc::matrix<Block> blocks_;
119
120 private:
121
123 Block block(mpqc::range A, mpqc::range B) const {
124 size_t i = range_to_block(A, this->alpha_);
125 size_t j = range_to_block(B, this->beta_);
126 Block b = this->blocks_(i,j);
127 if (!b.allowed) {
128 throw MPQC_EXCEPTION("ci::Vector: block (%s,%s) is not allowed\n",
129 string_cast(A).c_str(), string_cast(B).c_str());
130 }
131 return b;
132 }
133
135 static size_t range_to_block(mpqc::range r, const std::vector<mpqc::range> &R) {
136 auto it = std::find(R.begin(), R.end(), r);
137 if (it == R.end()) {
138 throw MPQC_EXCEPTION("ci::Vector: range r=(%s) does not map an exact block",
139 string_cast(mpqc::range(r)).c_str());
140 }
141 return it-R.begin();
142 }
143
144 };
145
147 void operator<<(Vector::Block1d block, const mpqc::Vector &v) {
148 block.array_ << v;
149 }
150
152 void operator>>(Vector::Block1d block, mpqc::Vector &v) {
153 block.array_ >> v;
154 }
155
160 double norm(ci::Vector &V,
161 const std::vector<mpqc::range> &local,
162 const MPI::Comm &comm) {
163 double norm = 0;
164 BOOST_FOREACH (auto r, local) {
165 mpqc::Vector v(r.size());
166 V(r) >> v;
167 norm += v.norm();
168 }
169 comm.sum(norm);
170 return norm;
171 }
172
181 const std::vector<mpqc::range> &local,
182 const MPI::Comm &comm)
183 {
184 MPQC_PROFILE_LINE;
185 double db = 0;
186#pragma omp parallel for reduction(+:db)
187 for (int j = 0; j < local.size(); ++j) {
188 mpqc::range rj = local.at(j);
189 mpqc::Vector Dj(rj.size()), bj(rj.size());
190 D(rj) >> Dj;
191 b(rj) >> bj;
192 db += Dj.dot(bj);
193 }
194 comm.sum(db);
195
196 // D = D - db*b;
197 double dd = 0;
198#pragma omp parallel for reduction(+:dd)
199 for (int j = 0; j < local.size(); ++j) {
200 mpqc::range rj = local.at(j);
201 mpqc::Vector Dj(rj.size()), bj(rj.size());
202 D(rj) >> Dj;
203 b(rj) >> bj;
204 Dj -= db*bj;
205 dd += Dj.dot(Dj);
206 D(rj) << Dj;
207 }
208 comm.sum(dd);
209
210 // D = D/||D||
211 dd = 1/sqrt(dd);
212#pragma omp parallel for
213 for (int j = 0; j < local.size(); ++j) {
214 mpqc::range rj = local.at(j);
215 mpqc::Vector Dj(rj.size());
216 D(rj) >> Dj;
217 Dj *= dd;
218 D(rj) << Dj;
219 }
220 return db*dd;
221 }
222
224
225}
226}
227
228#endif /* MPQC_CI_VECTOR_HPP */
double orthonormalize(ci::Vector &b, ci::Vector &D, const std::vector< mpqc::range > &local, const MPI::Comm &comm)
Schmidt orthogonalization d' = normalized(d - <d,b>*b)
Definition vector.hpp:180
void operator>>(Vector::Block1d block, mpqc::Vector &v)
Get vector block into v.
Definition vector.hpp:152
double norm(ci::Vector &V, const std::vector< mpqc::range > &local, const MPI::Comm &comm)
Compute CI vector norm.
Definition vector.hpp:160
std::string string_cast(const T &value)
cast type T to string
Definition string.hpp:14
#define MPQC_EXCEPTION(...)
Constructs mpqc::Exception with file, line information and an optional printf-style format and argume...
Definition exception.hpp:51
Contains new MPQC code since version 3.
Definition integralenginepool.hpp:37
Array implementation.
Definition forward.hpp:10
MPI_Comm object wrapper/stub.
Definition comm.hpp:14
Grid of subspaces, represented as blocks of determinants defined by alpha/beta pair,...
Definition subspace.hpp:103
bool allowed(int a, int b) const
Returns whenever a subspace alpha/beta block is allowed.
Definition subspace.hpp:139
const std::vector< Subspace< Alpha > > & alpha() const
Returns all alpha subspaces.
Definition subspace.hpp:144
const std::vector< Subspace< Beta > > & beta() const
Returns all beta subspaces.
Definition subspace.hpp:149
size_t dets() const
Returns number of determinants in the grid.
Definition subspace.hpp:164
1-d vector sub-block
Definition vector.hpp:55
friend void operator>>(Vector::Block1d block, mpqc::Vector &v)
Get vector block into v.
Definition vector.hpp:152
friend void operator<<(Vector::Block1d block, const mpqc::Vector &v)
Set vector block to v.
Definition vector.hpp:147
2-d vector sub-block
Definition vector.hpp:73
void operator=(const mpqc::Matrix &m) const
Assign matrix to this sub-block.
Definition vector.hpp:75
void assign_to(mpqc::Matrix &m) const
mpqc::Matrix::Assignable::assign_to implementation
Definition vector.hpp:81
Block CI Vector, with 1-d (vector) and 2-d (matrix) access.
Definition vector.hpp:18
Block1d operator()(range r)
Returns 1-d sub-block of vector.
Definition vector.hpp:67
Block2d operator()(mpqc::range A, mpqc::range B)
Returns 2-d sub-block of vector.
Definition vector.hpp:95
Vector(std::string name, const SubspaceGrid &G, MPI::Comm comm, bool incore)
Construct vector.
Definition vector.hpp:25
An interface to enable matrix assignment from other containers.
Definition matrix.hpp:54
Matrix class derived from Eigen::Matrix with additional MPQC integration.
Definition matrix.hpp:23
Definition range.hpp:25
Vector class derived from Eigen::Matrix with additional MPQC integration.
Definition matrix.hpp:133

Generated at Wed Sep 25 2024 02:45:30 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.