MPQC  3.0.0-alpha
ci.hpp
1 #ifndef MPQC_CI_CI_HPP
2 #define MPQC_CI_CI_HPP
3 
4 #include <util/misc/formio.h>
5 #include "mpqc/ci/subspace.hpp"
6 #include "mpqc/ci/string.hpp"
7 #include "mpqc/math/matrix.hpp"
8 #include "mpqc/mpi.hpp"
9 #include "mpqc/file.hpp"
10 #include "mpqc/utility/check.hpp"
11 #include "mpqc/utility/exception.hpp"
12 
13 
14 namespace mpqc {
15 namespace ci {
16 
19 
21  struct Config {
22  size_t core, orbitals;
23  struct {
24  size_t alpha, beta;
25  } electrons;
26  size_t ms;
27  size_t rank;
28  size_t roots;
29  size_t max;
30  size_t collapse;
31  double e_ref;
32  mutable double e_core;
33  double convergence;
34  size_t block;
35  int incore;
36  struct {
37  int chunk;
38  int compress;
39  bool direct;
40  } hdf5;
41  Config() {
42  core = 0;
43  orbitals = 0;
44  electrons.alpha = 0;
45  electrons.beta = 0;
46  ms = 0;
47  rank = 0;
48  roots = 1;
49  max = 10;
50  collapse = 0;
51  e_ref = 0.0;
52  e_core = 0.0;
53  convergence = 1e-10;
54  block = 1024*4;
55  incore = 2;
56  hdf5.chunk = 0;
57  hdf5.compress = 0;
58  hdf5.direct = false;
59  }
60  void print(std::ostream& o = sc::ExEnv::out0()) const {
61  o << sc::indent << "rank = " << rank << std::endl;
62  o << sc::indent << "# of roots = " << roots << std::endl;
63  o << sc::indent << "# of electrons = {" << electrons.alpha << "," << electrons.beta << "}" << std::endl;
64  o << sc::indent << "magnetic moment = " << ms << std::endl;
65  }
66  };
67 
70  template<class CIFunctor, class Index = String::Index>
71  struct CI;
72 
74  template<class CIFunctor, class Index>
75  struct CI : boost::noncopyable {
76 
77  public:
78 
80 
83 
85 
87 
88  struct IO : boost::noncopyable {
90  } vector;
91 
92  protected:
93  std::vector<mpqc::range> locals_; // range of local determinants
94 
95  public:
96 
97  template<class Spin>
98  const ci::String::List<Index>& strings() const {
99  if (Spin::value == Alpha::value)
100  return alpha;
101  if (Spin::value == Beta::value)
102  return beta;
103  throw MPQC_EXCEPTION("");
104  }
105 
106  public:
107 
108  size_t dets() const {
109  return subspace.dets();
110  }
111 
112  const std::vector<mpqc::range>& local() const {
113  return this->locals_;
114  }
115 
117  int excitation(const String &s) const {
118  return String::difference(s, String(s.size(), s.count()));
119  }
120 
121  public:
122 
126  : config(config),
127  alpha(ci::strings(config.orbitals, config.electrons.alpha, config.rank)),
128  beta(ci::strings(config.orbitals, config.electrons.beta, config.rank)),
129  comm(comm)
130  {
131  // sort/space strings according to rank (excitation)
132  auto sa = sort<Alpha>(this->alpha);
133  auto sb = sort<Beta>(this->beta);
134 
135  this->subspace = CIFunctor::grid(*this,
136  split(sa, this->config.block),
137  split(sb, this->config.block));
138 
139  {
140  auto &out = sc::ExEnv::out0();
141  // general
142  out << sc::indent
143  << sc::scprintf("CI space = (%lu %lu): ",
144  this->config.orbitals, (this->config.electrons.alpha+
145  this->config.electrons.beta))
146  << sc::scprintf("alpha = %lu, beta = %lu, dets = %lu\n",
147  alpha.size(), beta.size(), this->subspace.dets())
148  << std::endl;
149  // alpha
150  print(out, "Alpha excitations:\n", sb);
151  print(out, "Beta excitations:\n", sa);
152  }
153 
154  // I/O
155  {
156  size_t dets = this->subspace.dets();
157  mpqc::range locals =
158  partition(mpqc::range(0, dets), this->comm.size()).at(this->comm.rank());
159  MPQC_CHECK(locals.size() > 0);
160  std::vector<range> extents(2);
161  extents[0] = locals;
162  extents[1] = range(0,this->config.max);
163  File::Properties dcpl(H5P_DATASET_CREATE);
164  if (this->config.hdf5.chunk) {
165  hsize_t chunk[] = { 1, std::min<hsize_t>(locals.size(), this->config.hdf5.chunk) };
166  H5Pset_chunk(dcpl.id(), 2, chunk);
167  std::cout << "hdf5.chunk=" << this->config.hdf5.chunk << std::endl;
168  }
169  if (this->config.hdf5.compress) {
170  //H5Pset_szip(dcpl.id(), H5_SZIP_NN_OPTION_MASK, 64);
171  H5Pset_deflate (dcpl.id(), this->config.hdf5.compress);
172  std::cout << "hdf5.compress=" << this->config.hdf5.compress << std::endl;
173  }
174  this->vector.b = File::Dataset<double>(io, "b", extents, dcpl);
175  this->vector.Hb = File::Dataset<double>(io, "Hb", extents, dcpl);
176  this->locals_ = split(locals, this->config.block*this->config.block);
177  }
178 
179  // initial guess
180  {
181  guess();
182  }
183 
184  }
185 
187  template<class Spin>
188  static int diff(const Space<Spin> &a, const Space<Spin> &b) {
189  return abs(a.rank() - b.rank());
190  }
191 
193  bool test(const String &a) const {
194  return CIFunctor::test(*this, a);
195  }
196 
198  bool test(const Space<Alpha> &a, const Space<Beta> &b) const {
199  return CIFunctor::test(*this, a, b);
200  }
201 
202 
203  protected:
204 
205  // /// initialize IO
206  // void initialize(File::Group io, range local) {
207  // std::cout << "local = " << local << std::endl;
208  // // can't have size-0 datasets (size-0 has special meaning)
209  // }
210 
212  void guess() {
213  BOOST_FOREACH (auto r, this->local()) {
214  mpqc::Vector b = mpqc::Vector::Zero(r.size());
215  if (r.test(0)) b(0) = 1;
216  vector.b(r,0) << b;
217  }
218  comm.barrier();
219  }
220 
222  template<class Spin>
223  static void print(std::ostream &out, const std::string &header,
224  const std::vector< Subspace<Spin> > &S)
225  {
226  out << header;
227  for (int i = 0; i < S.size(); ++i) {
228  range r = S.at(i);
229  out << sc::scprintf("%i: size = %lu, \t range = %s\n",
230  i, r.size(), string_cast(r).c_str());
231  }
232  out << std::endl;
233  }
234 
235  protected:
236 
239  template<class Spin>
240  std::vector< Subspace<Spin> > sort(ci::String::List<Index> &S) const {
241  S.sort(Sort(this));
242  int r = 0;
243  std::vector< Subspace<Spin> > R;
244  int begin = 0, end = 0;
245  BOOST_FOREACH (const auto &s, S) {
246  int x = this->excitation(s);
247  if (x == r+1) {
248  R.push_back(Subspace<Spin>(Space<Spin>(r), mpqc::range(begin, end)));
249  begin = end;
250  r = x;
251  }
252  else if (x == r) {
253  /* do nothing */
254  }
255  else {
256  throw MPQC_EXCEPTION("internal error in CI::sort");
257  }
258  ++end;
259  }
260  if (begin != end)
261  R.push_back(Subspace<Spin>(Space<Spin>(r), mpqc::range(begin, end)));
262  MPQC_ASSERT(S.size() == *R.back().end());
263  return R;
264  }
265 
267  struct Sort {
268  explicit Sort(const CI *ci) : ci_(ci) {}
269  bool operator()(const String &a, const String &b) const {
270  return ci_->excitation(a) < ci_->excitation(b);
271  }
272  private:
273  const CI *ci_;
274  };
275 
276  };
277 
279 
280 } // namespace ci
281 } // namespace mpqc
282 
283 
284 namespace sc {
285 
287  inline void ToStateOut(const mpqc::ci::Config &a, StateOut &so, int &count) {
288  const char* a_cast = reinterpret_cast<const char*>(&a);
289  count += so.put(a_cast, sizeof(mpqc::ci::Config));
290  }
291 
293  inline void FromStateIn(mpqc::ci::Config &a, StateIn &si, int &count) {
294  char* a_cast = reinterpret_cast<char*>(&a);
295  count += si.get(a_cast);
296  }
297 
298 }
299 
300 #endif // MPQC_CI_CI_HPP
mpqc::ci::CI::print
static void print(std::ostream &out, const std::string &header, const std::vector< Subspace< Spin > > &S)
prints CI configuration summary
Definition: ci.hpp:223
mpqc::ci::CI::test
bool test(const Space< Alpha > &a, const Space< Beta > &b) const
test if space configuration is allowed
Definition: ci.hpp:198
mpqc::ci::SubspaceGrid
Grid of subspaces, represented as blocks of determinants defined by alpha/beta pair,...
Definition: subspace.hpp:103
sc::ToStateOut
void ToStateOut(const Atom &a, StateOut &so, int &count)
writes Atom to sc::StateOut
mpqc::ci::Config::block
size_t block
CI matrix blocking factor (values 1024 to 8192 are ok)
Definition: ci.hpp:34
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
mpqc::ci::Config::rank
size_t rank
Restricted CI order, rank=0 implies Full CI.
Definition: ci.hpp:27
mpqc::ci::CI::IO
Definition: ci.hpp:88
mpqc::ci::CI::alpha
ci::String::List< Index > alpha
Alpha string list.
Definition: ci.hpp:81
mpqc::range
Definition: range.hpp:23
mpqc::ci::Config::direct
bool direct
HDF5 direct I/O.
Definition: ci.hpp:39
mpqc::ci::CI::config
const ci::Config config
CI configuration.
Definition: ci.hpp:79
mpqc::ci::Config::chunk
int chunk
HDF5 chunking (should be about 256k)
Definition: ci.hpp:37
mpqc::ci::CI::guess
void guess()
initialize guess vector
Definition: ci.hpp:212
mpqc::ci::CI::sort
std::vector< Subspace< Spin > > sort(ci::String::List< Index > &S) const
sort string list by rank/excitation and return vector of spaces, where each space corresponds to rang...
Definition: ci.hpp:240
mpqc::ci::CI::Sort
compare-by-rank functor
Definition: ci.hpp:267
mpqc::ci::CI::beta
ci::String::List< Index > beta
Beta string list.
Definition: ci.hpp:82
mpqc::ci::String::List< Index >
mpqc::ci::Config::ms
size_t ms
magnetic moment/Ms
Definition: ci.hpp:26
mpqc::ci::SubspaceGrid::dets
size_t dets() const
Returns number of determinants in the grid.
Definition: subspace.hpp:164
mpqc::ci::Config::roots
size_t roots
number of roots to find
Definition: ci.hpp:28
MPQC_EXCEPTION
#define MPQC_EXCEPTION(...)
Definition: exception.hpp:51
mpqc::string_cast
std::string string_cast(const T &value)
cast type T to string
Definition: string.hpp:14
mpqc::ci::CI::test
bool test(const String &a) const
test if string is allowed
Definition: ci.hpp:193
sc::StateIn
Definition: statein.h:79
mpqc::ci::Config::incore
int incore
determines if arrays C+S (2), C (1), or none(0) will be in core
Definition: ci.hpp:35
mpqc::detail::File::Properties
Definition: file.hpp:76
mpqc::File::Dataset< double >
mpqc::ci::CI::subspace
SubspaceGrid subspace
CI subspaces grid.
Definition: ci.hpp:84
mpqc::ci::Config::electrons
struct mpqc::ci::Config::@55 electrons
number of electrons of each spin in CI
mpqc::ci::CI::diff
static int diff(const Space< Spin > &a, const Space< Spin > &b)
test if space configuration is allowed
Definition: ci.hpp:188
mpqc::ci::Config
CI configuration.
Definition: ci.hpp:21
mpqc::ci::CI
CI class template.
Definition: ci.hpp:71
mpqc::File::Group
Directory-like container that holds datasets and other groups.
Definition: file.hpp:682
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::Config::convergence
double convergence
energy convergence criteria
Definition: ci.hpp:33
mpqc::ci::Config::compress
int compress
GZIP compress level (0 to 9)
Definition: ci.hpp:38
sc::StateOut
Definition: stateout.h:71
mpqc::partition
std::vector< range > partition(const range &R, size_t N)
Partition range into N blocks.
Definition: range.hpp:76
mpqc::ci::CI::comm
MPI::Comm comm
CI communicator.
Definition: ci.hpp:86
mpqc::ci::Config::max
size_t max
maximum number of iterations
Definition: ci.hpp:29
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::CI::vector
mpqc::ci::CI::IO vector
CI vectors b (aka C), Hb (aka sigma) file datasets.
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
mpqc::ci::CI::CI
CI(const Config &config, MPI::Comm comm, File::Group io)
Construct CI base given configuration and communicator.
Definition: ci.hpp:125
sc::StateOut::put
virtual int put(const ClassDesc *)
Write out information about the given ClassDesc.
mpqc::MPI::Comm
MPI_Comm object wrapper/stub.
Definition: comm.hpp:14
sc::StateIn::get
virtual int get(const ClassDesc **)
This restores ClassDesc's.
mpqc::vector
Vector class derived from Eigen::Matrix with additional MPQC integration.
Definition: matrix.hpp:133
sc::FromStateIn
void FromStateIn(Atom &a, StateIn &si, int &count)
reads Atom from sc::StateIn
sc::scprintf
This class allows printf-like output to be sent to an ostream.
Definition: formio.h:97
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
mpqc::ci::CI::excitation
int excitation(const String &s) const
rank/excitation of the string relative to its ground state, ie [11..00]
Definition: ci.hpp:117
mpqc::ci::String
Definition: string.hpp:24

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