MPQC  3.0.0-alpha
subspace.hpp
1 #ifndef MPQC_CI_SUBSPACE_HPP
2 #define MPQC_CI_SUBSPACE_HPP
3 
4 #include <util/misc/formio.h>
5 #include "mpqc/ci/string.hpp"
6 #include "mpqc/math/matrix.hpp"
7 #include "mpqc/mpi.hpp"
8 #include "mpqc/file.hpp"
9 
10 #include <boost/foreach.hpp>
11 #include "mpqc/utility/check.hpp"
12 #include "mpqc/utility/exception.hpp"
13 
14 #include <boost/mpl/int.hpp>
15 
16 namespace mpqc {
17 namespace ci {
18 
21 
23  template<int S>
24  struct Spin : boost::mpl::int_<S> {};
25 
26  typedef Spin<1> Alpha;
27  typedef Spin<-1> Beta;
28  typedef Spin<0> Any;
29 
31  template<class Spin>
32  struct Space {
33  explicit Space(int rank) : rank_(rank) {}
34  int rank() const { return rank_; }
35  bool operator==(const Space &s) const {
36  return this->rank_ == s.rank_;
37  }
38  private:
39  int rank_;
40  };
41 
43  template<class Spin>
44  inline bool operator<(const Space<Spin> &a, const Space<Spin> &b) {
45  return (a.rank() < b.rank());
46  }
47 
50  template<class Spin>
51  struct Subspace : ci::Space<Spin>, mpqc::range {
52  typedef ci::Space<Spin> Space;
57  : Space(s), mpqc::range(r) {}
59  bool operator==(const Subspace &a) const {
60  return ((mpqc::range(a) == mpqc::range(*this)) && (Space(a) == Space(*this)));
61  }
63  bool test(int idx) const {
64  return ((*mpqc::range::begin() <= idx) && (idx < *mpqc::range::end()));
65  }
67  std::vector<Subspace> split(size_t block) const {
68  std::vector<Subspace> s;
69  BOOST_FOREACH (auto r, balanced_split(*this, block)) {
70  s.push_back(Subspace(*this, r));
71  }
72  return s;
73  }
74  };
75 
78  template<>
79  struct Subspace<Any> : ci::Space<Any>, mpqc::range {
81  template<class Spin>
83  : ci::Space<Any>(s.rank()), mpqc::range(s) {}
85  bool operator==(const Subspace &b) const {
86  return ((mpqc::range(*this) == mpqc::range(b)) && (Space<Any>(*this) == Space<Any>(b)));
87  }
88  };
89 
90  template<class Spin>
91  std::vector< Subspace<Spin> > split(const std::vector< Subspace<Spin> > &V, size_t block) {
92  std::vector< Subspace<Spin> > S;
93  BOOST_FOREACH (auto v, V) {
94  BOOST_FOREACH (auto s, v.split(block)) {
95  S.push_back(s);
96  }
97  }
98  return S;
99  }
100 
103  struct SubspaceGrid {
104 
107 
109  SubspaceGrid(const std::vector< Subspace<Alpha> > &A,
110  const std::vector< Subspace<Beta> > &B,
111  const mpqc::matrix<bool> &mask)
112  : alpha_(A), beta_(B), mask_(mask)
113  {
114  verify(alpha_, this->mask_.rows());
115  verify(beta_, this->mask_.cols());
116  // count total size
117  this->dets_ = 0;
118  for (int i = 0; i < mask_.rows(); ++i) {
119  for (int j = 0; j < mask_.cols(); ++j) {
120  if (this->allowed(i,j))
121  this->dets_ += this->alpha(i).size()*this->beta(j).size();
122  }
123  }
124  std::vector< Subspace<Any> > a(alpha_.begin(), alpha_.end());
125  std::vector< Subspace<Any> > b(beta_.begin(), beta_.end());
126  }
127 
128  // /// Resolve string index to its alpha space
129  // Space<Alpha> alpha_string_space(int idx) const {
130  // return space(idx, this->alpha_);
131  // }
132 
133  // /// Resolve string index to its beta space
134  // Space<Beta> beta_string_space(int idx) const {
135  // return space(idx, this->beta_);
136  // }
137 
139  bool allowed(int a, int b) const {
140  return mask_(a,b);
141  }
142 
144  const std::vector< Subspace<Alpha> >& alpha() const {
145  return alpha_;
146  }
147 
149  const std::vector< Subspace<Beta> >& beta() const {
150  return beta_;
151  }
152 
154  Subspace<Alpha> alpha(int i) const {
155  return alpha_.at(i);
156  }
157 
159  Subspace<Beta> beta(int i) const {
160  return beta_.at(i);
161  }
162 
164  size_t dets() const {
165  return this->dets_;
166  }
167 
168  private:
169  std::vector< Subspace<Alpha> > alpha_;
170  std::vector< Subspace<Beta> > beta_;
171  mpqc::matrix<bool> mask_;
172  size_t dets_;
173 
174  private:
175 
178  template<class Spin>
179  static void verify(const std::vector< Subspace<Spin> > &V, int N) {
180  int begin = 0;
181  if (V.size() != N)
182  throw MPQC_EXCEPTION("Subspace vector size must be equal to %i", N);
183  BOOST_FOREACH (auto r, V) {
184  if (begin != *r.begin())
185  throw MPQC_EXCEPTION("Subspace vector is not contigous");
186  begin = *r.end();
187  }
188  }
189 
190  };
191 
192  struct SubspaceBlock {
193  int alpha, beta;
194  struct Sort {
195  const std::vector< Subspace<Alpha> > &A;
196  const std::vector< Subspace<Beta> > &B;
197  Sort(const std::vector< Subspace<Alpha> > &A,
198  const std::vector< Subspace<Beta> > &B)
199  : A(A), B(B)
200  {
201  }
202  bool operator()(const SubspaceBlock &i, const SubspaceBlock &j) const {
203  return (A.at(i.alpha).size()*B.at(i.beta).size() <
204  A.at(j.alpha).size()*B.at(j.beta).size());
205  }
206  };
207  };
208 
209  inline std::vector<SubspaceBlock> blocks(const std::vector< Subspace<Alpha> > &A,
210  const std::vector< Subspace<Beta> > &B)
211  {
212  std::vector<SubspaceBlock> blocks;
213  for (int b = 0; b < B.size(); ++b) {
214  for (int a = 0; a < A.size(); ++a) {
215  SubspaceBlock block = { a, b };
216  blocks.push_back(block);
217  }
218  }
219  std::sort(blocks.begin(), blocks.end(), SubspaceBlock::Sort(A,B));
220  std::reverse(blocks.begin(), blocks.end());
221  // BOOST_FOREACH (auto s, blocks) {
222  // std::cout << "block " << s.alpha << "," << s.beta << " "
223  // << A.at(s.alpha).size()*B.at(s.beta).size() << std::endl;
224  // }
225  return blocks;
226  }
227 
229 
230 } // namespace ci
231 } // namespace mpqc
232 
233 #endif // MPQC_CI_SUBSPACE_HPP
mpqc::ci::Subspace::Subspace
Subspace(Space s, mpqc::range r)
Constructs subspace.
Definition: subspace.hpp:56
mpqc::ci::SubspaceGrid
Grid of subspaces, represented as blocks of determinants defined by alpha/beta pair,...
Definition: subspace.hpp:103
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
mpqc::range
Definition: range.hpp:23
mpqc::ci::SubspaceBlock
Definition: subspace.hpp:192
mpqc::matrix< bool >
mpqc::ci::Subspace::operator==
bool operator==(const Subspace &a) const
Compares two subspaces by their range and space.
Definition: subspace.hpp:59
mpqc::ci::SubspaceGrid::SubspaceGrid
SubspaceGrid(const std::vector< Subspace< Alpha > > &A, const std::vector< Subspace< Beta > > &B, const mpqc::matrix< bool > &mask)
Construct subspace grid from alpha and beta subspace vectors and sparsity matrix.
Definition: subspace.hpp:109
mpqc::ci::SubspaceGrid::dets
size_t dets() const
Returns number of determinants in the grid.
Definition: subspace.hpp:164
MPQC_EXCEPTION
#define MPQC_EXCEPTION(...)
Definition: exception.hpp:51
mpqc::balanced_split
std::vector< range > balanced_split(const range &r, size_t N)
Split range into roughly equal blocks of size N or less.
Definition: range.hpp:105
mpqc::ci::SubspaceGrid::allowed
bool allowed(int a, int b) const
Returns whenever a subspace alpha/beta block is allowed.
Definition: subspace.hpp:139
mpqc::ci::Subspace::test
bool test(int idx) const
test if index is in subspace range
Definition: subspace.hpp:63
mpqc::ci::SubspaceGrid::beta
const std::vector< Subspace< Beta > > & beta() const
Returns all beta subspaces.
Definition: subspace.hpp:149
mpqc::ci::Subspace< Any >::operator==
bool operator==(const Subspace &b) const
Compares two subspaces by their range and space.
Definition: subspace.hpp:85
mpqc::ci::SubspaceGrid::alpha
const std::vector< Subspace< Alpha > > & alpha() const
Returns all alpha subspaces.
Definition: subspace.hpp:144
mpqc::ci::Subspace
A range of a space where all objects in the subspace range are assumed to have the same space rank.
Definition: subspace.hpp:51
mpqc::ci::SubspaceGrid::beta
Subspace< Beta > beta(int i) const
Returns i-th beta subspace.
Definition: subspace.hpp:159
mpqc::ci::SubspaceGrid::SubspaceGrid
SubspaceGrid()
Construct empty subspace grid.
Definition: subspace.hpp:106
mpqc::ci::Subspace::split
std::vector< Subspace > split(size_t block) const
Split the subspace into a vector of subspaces.
Definition: subspace.hpp:67
mpqc::ci::Spin
Electron spin.
Definition: subspace.hpp:24
mpqc::ci::Space
A CI space, marked by Spin S and rank.
Definition: subspace.hpp:32
mpqc::split
std::vector< range > split(range r, size_t N)
Split range into blocks of size N.
Definition: range.hpp:94
mpqc::ci::operator<
bool operator<(const Space< Spin > &a, const Space< Spin > &b)
Compare two spaces by their rank.
Definition: subspace.hpp:44
mpqc::ci::SubspaceBlock::Sort
Definition: subspace.hpp:194
mpqc::ci::Subspace< Any >::Subspace
Subspace(const Subspace< Spin > &s)
Copies subspace while discarding the Spin parameter.
Definition: subspace.hpp:82
mpqc::ci::SubspaceGrid::alpha
Subspace< Alpha > alpha(int i) const
Returns i-th alpha subspace.
Definition: subspace.hpp:154

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.