LIBINT  2.1.0-stable
braket.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_braket_h_
21 #define _libint2_src_bin_libint_braket_h_
22 
23 #include <polyconstr.h>
24 #include <bfset.h>
25 #include <algebra.h>
26 #include <libint2/intrinsic_types.h>
27 #include <global_macros.h>
28 
29 namespace libint2 {
30 
35  template <class BFS> class VectorBraket: public Hashable<LIBINT2_UINT_LEAST64,ComputeKey> {
36 
37  public:
38  typedef BFS bfs_type;
39  typedef BFS bfs_stor;
40  typedef bfs_stor& bfs_ref;
41  typedef const bfs_stor& bfs_cref;
42  typedef vector< bfs_stor > BFSVector;
43  typedef vector< BFSVector > BFSMatrix;
45  typedef struct{} parent_type;
46 
49  VectorBraket();
50  VectorBraket(const BFSMatrix&);
51  VectorBraket(const VectorBraket&);
52  ~VectorBraket();
53 
55  bool operator==(const VectorBraket&) const;
57  bfs_ref member(unsigned int p, unsigned int i);
59  bfs_cref member(unsigned int p, unsigned int i) const;
61  SubIterator* member_subiter(unsigned int p, unsigned int i) const;
63  void set_member(bfs_cref, unsigned int p, unsigned int i);
65  void set_member(const ConstructablePolymorphically&, unsigned int p, unsigned int i);
67  unsigned int num_members(unsigned int p) const;
69  unsigned int num_part() const;
71  inline LIBINT2_UINT_LEAST64 key() const;
73  LIBINT2_UINT_LEAST64 max_key() const;
74 
75  private:
76 
77  BFSMatrix bfs_;
78 
79  };
80 
81  template <class BFS>
83  bfs_(0)
84  {
85  }
86 
87  template <class BFS>
88  VectorBraket<BFS>::VectorBraket(const BFSMatrix& bfs) :
89  bfs_(bfs)
90  {
91  }
92 
93  template <class BFS>
95  bfs_(a.bfs_)
96  {
97  }
98 
99  template <class BFS>
101  {
102  }
103 
104  template <class BFS>
105  typename VectorBraket<BFS>::bfs_ref
106  VectorBraket<BFS>::member(unsigned int p, unsigned int i)
107  {
108  return bfs_.at(p).at(i);
109  }
110 
111  template <class BFS>
112  typename VectorBraket<BFS>::bfs_cref
113  VectorBraket<BFS>::member(unsigned int p, unsigned int i) const
114  {
115  return bfs_.at(p).at(i);
116  }
117 
118  template <class BFS>
119  SubIterator*
120  VectorBraket<BFS>::member_subiter(unsigned int p, unsigned int i) const
121  {
122  return static_cast<SubIterator*>(new SubIteratorBase<BFS>( member(p,i) ) );
123  }
124 
125  template <class BFS>
126  void
127  VectorBraket<BFS>::set_member(bfs_cref bfs, unsigned int p, unsigned int i)
128  {
129  if (p >= bfs_.size())
130  bfs_.resize(p+1);
131  if (i >= bfs_[p].size())
132  bfs_[p].resize(i+1);
133  BFS bfs_tmp(bfs);
134  bfs_[p][i] = bfs_tmp;
135  }
136 
137  template <class BFS>
138  void
139  VectorBraket<BFS>::set_member(const ConstructablePolymorphically& bfs, unsigned int p, unsigned int i)
140  {
141  // WARNING : can be VERY dangerous
142  // try constructing BFS from bfs.
143  BFS bfs_cast(bfs);
144 
145  if (p >= bfs_.size())
146  bfs_.resize(p+1);
147  if (i >= bfs_[p].size())
148  bfs_[p].resize(i+1);
149  bfs_[p][i] = bfs_cast;
150  }
151 
152  template <class BFS>
153  unsigned int
154  VectorBraket<BFS>::num_members(unsigned int p) const
155  {
156  return bfs_.at(p).size();
157  }
158 
159  template <class BFS>
160  unsigned int
162  {
163  return bfs_.size();
164  }
165 
166  template <class BFS>
167  bool
169  {
170  return bfs_ == a.bfs_;
171  }
172 
173  template <class BFS>
174  LIBINT2_UINT_LEAST64
176  {
177  LIBINT2_UINT_LEAST64 pfac = 1;
178  LIBINT2_UINT_LEAST64 key = 0;
179  const int np = bfs_.size();
180  for(int p=np-1; p>=0; p--) {
181  const BFSVector& row = bfs_[p];
182  const int nf = row.size();
183  for(int f=nf-1; f>=0; f--) {
184  key += pfac*row[f].key();
185  pfac *= BFS::max_key;
186  }
187  }
188  assert(key < this->max_key());
189  return key;
190  }
191 
192  template <class BFS>
193  LIBINT2_UINT_LEAST64
195  {
196  LIBINT2_UINT_LEAST64 max_key = 1;
197  const int np = bfs_.size();
198  for(int p=np-1; p>=0; p--) {
199  const BFSVector& row = bfs_[p];
200  const int nf = row.size();
201  for(int f=nf-1; f>=0; f--) {
202  max_key *= BFS::max_key;
203  }
204  }
205  return max_key;
206  }
207 
209 
214  template <class BFS, unsigned int NP> class ArrayBraket {
215  public:
219  typedef struct{} parent_type;
222 
223  typedef BFS bfs_type;
224  typedef bfs_type& bfs_ref;
225  typedef const BFS& bfs_cref;
226 
229  ArrayBraket();
230  ArrayBraket(const BFS* braket);
231  ArrayBraket(const vector<vector<BFS> >& braket);
232  ~ArrayBraket();
233 
235  bool operator==(const this_type&) const;
237  bfs_cref member(unsigned int p, unsigned int i=0) const;
239  bfs_ref member(unsigned int p, unsigned int i=0);
241  SubIterator* member_subiter(unsigned int p, unsigned int i=0) const;
243  void set_member(const BFS&, unsigned int p, unsigned int i=0);
245  void set_member(const ConstructablePolymorphically&, unsigned int p, unsigned int i=0);
247  unsigned int num_members(unsigned int p) const { assert(p<NP); return 1; }
249  unsigned int num_part() const { return NP; }
251  inline LIBINT2_UINT_LEAST64 key() const;
253  LIBINT2_UINT_LEAST64 max_key() const;
254 #if COMPUTE_SIZE_DIRECTLY
255  unsigned int size() const;
256 #endif
257 
258  private:
259  BFS bfs_[NP];
260 
261  // this function resets all cached values
262  void reset_cache();
263 #if COMPUTE_SIZE_DIRECTLY
264  mutable unsigned int size_;
265 #endif
266  };
267 
268  template <class BFS, unsigned int NP>
270  {
271 #if COMPUTE_SIZE_DIRECTLY
272  size_ = 0;
273 #endif
274  }
275 
276  template <class BFS, unsigned int NP>
277  ArrayBraket<BFS,NP>::ArrayBraket(const BFS* braket) {
278  for(int i=0; i<NP; i++)
279  bfs_[i] = braket[i];
280 #if COMPUTE_SIZE_DIRECTLY
281  size_ = 0;
282 #endif
283  }
284 
285  template <class BFS, unsigned int NP>
286  ArrayBraket<BFS,NP>::ArrayBraket(const vector<vector<BFS> >& braket) {
287  assert(braket.size()==NP);
288  for(unsigned int i=0; i<NP; i++) {
289  assert(braket[i].size()==1);
290  bfs_[i] = braket[i][0];
291  }
292 #if COMPUTE_SIZE_DIRECTLY
293  size_ = 0;
294 #endif
295  }
296 
297  template <class BFS, unsigned int NP>
299 
300  template <class BFS, unsigned int NP>
301  bool
303  for(int i=0; i<NP; i++)
304  if (bfs_[i] != a[i])
305  return false;
306  return true;
307  }
308 
309  template <class BFS, unsigned int NP>
310  typename ArrayBraket<BFS,NP>::bfs_cref
311  ArrayBraket<BFS,NP>::member(unsigned int p, unsigned int i) const {
312  assert(i==0 && p<NP);
313  return bfs_[p];
314  }
315 
316  template <class BFS, unsigned int NP>
317  typename ArrayBraket<BFS,NP>::bfs_ref
318  ArrayBraket<BFS,NP>::member(unsigned int p, unsigned int i) {
319  assert(i==0 && p<NP);
320  reset_cache();
321  return bfs_[p];
322  }
323 
324  template <class BFS, unsigned int NP>
325  SubIterator*
326  ArrayBraket<BFS,NP>::member_subiter(unsigned int p, unsigned int i) const {
327  assert(i==0 && p<NP);
328  return static_cast<SubIterator*>(new SubIteratorBase<BFS>( member(p,i) ) );
329  }
330 
331  template <class BFS, unsigned int NP>
332  void
333  ArrayBraket<BFS,NP>::set_member(const BFS& f, unsigned int p, unsigned int i) {
334  assert(i==0 && p<NP);
335  reset_cache();
336  bfs_[p] = f;
337  }
338 
339  template <class BFS, unsigned int NP>
340  void
341  ArrayBraket<BFS,NP>::set_member(const ConstructablePolymorphically& f, unsigned int p, unsigned int i) {
342  assert(i==0 && p<NP);
343  reset_cache();
344  bfs_[p] = BFS(f);
345  }
346 
347  template <class BFS, unsigned int NP>
348  LIBINT2_UINT_LEAST64
350  LIBINT2_UINT_LEAST64 pfac = 1;
351  LIBINT2_UINT_LEAST64 key = 0;
352  for(int p=NP-1; p>=0; p--) {
353  key += pfac*bfs_[p].key();
354  pfac *= BFS::max_key;
355  }
356  assert(key < this->max_key());
357  return key;
358  }
359 
360  template <class BFS, unsigned int NP>
361  LIBINT2_UINT_LEAST64
363  LIBINT2_UINT_LEAST64 max_key = 1;
364  for(int p=NP-1; p>=0; p--) {
365  max_key *= BFS::max_key;
366  }
367  return max_key;
368  }
369 
370 #if COMPUTE_SIZE_DIRECTLY
371  template <class BFS, unsigned int NP>
372  unsigned int
373  ArrayBraket<BFS,NP>::size() const {
374  if (size_ > 0) return size_;
375  size_ = 1;
377  for(int p=NP-1; p>=0; p--) {
378  size_ *= bfs_[p].num_bf();
379  }
380  }
381  return size_;
382  }
383 #endif
384 
385  template <class BFS, unsigned int NP>
386  void
388  size_ = 0;
389  }
390 
392  // really need to have typedef template!
393  template <typename BFS>
397  };
398 
400  // really need to have typedef template!
401  template <typename BFS>
405  };
406 
408 
412  enum BraketType {CBra, CKet, PBra, PKet};
416  template <class BFS, BraketType BKType> class BraketPair {
417  public:
420  typedef BFS bfs_type;
421 
424  BraketPair(const BFS& f1, const BFS& f2) : bfs_(f1,f2) {}
425  BraketPair(const BraketPair& source) : bfs_(source.bfs_) {}
426  BraketPair& operator=(const BraketPair& rhs) { bfs_ = rhs.bfs_; return *this; }
427  const BFS& operator[](unsigned int i) const {
428  if (i == 0) return bfs_.first;
429  if (i == 1) return bfs_.second;
430  throw std::logic_error("BraketPair::operator[] -- argument out of range");
431  }
433  bool operator==(const this_type& rhs) const {
434  return rhs.bfs_ == bfs_;
435  }
436 
437  private:
438  std::pair<BFS,BFS> bfs_;
439  };
440 
442  namespace braket {
444  template <class F> BraketPair<F,PBra> _pbra(const F& f1, const F& f2) {
445  return BraketPair<F,PBra>(f1,f2);
446  }
448  template <class F> BraketPair<F,PKet> _pket(const F& f1, const F& f2) {
449  return BraketPair<F,PKet>(f1,f2);
450  }
452  template <class F> BraketPair<F,CBra> _cbra(const F& f1, const F& f2) {
453  return BraketPair<F,CBra>(f1,f2);
454  }
456  template <class F> BraketPair<F,CKet> _cket(const F& f1, const F& f2) {
457  return BraketPair<F,CKet>(f1,f2);
458  }
459  };
460 
461  template <class BFS, BraketType BKTypeL, BraketType BKTypeR>
463  operator^(const BraketPair<BFS,BKTypeL>& L, const BraketPair<BFS,BKTypeR>& R) {
464  return algebra::make_wedge(L,R);
465  }
466 
467 };
468 
469 #endif
LIBINT2_UINT_LEAST64 key() const
Implements Hashable::key()
Definition: braket.h:175
BraketPair< F, CBra > _cbra(const F &f1, const F &f2)
Chemists bra.
Definition: braket.h:452
Definition: braket.h:45
LIBINT2_UINT_LEAST64 max_key() const
key lies in range [0,max_key())
Definition: braket.h:362
bfs_cref member(unsigned int p, unsigned int i=0) const
Returns cref to the i-th function for particle p.
Definition: braket.h:311
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:879
unsigned int num_members(unsigned int p) const
Returns the number of BFS for particle p.
Definition: braket.h:247
ConstructablePolymorphically is a base for all objects which can be constructed using a SafePtr to a ...
Definition: polyconstr.h:30
LIBINT2_UINT_LEAST64 max_key() const
key lies in range [0,max_key())
Definition: braket.h:194
bool operator==(const this_type &rhs) const
Comparison function.
Definition: braket.h:433
There&#39;s no parent.
Definition: braket.h:219
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType...
Definition: hashable.h:72
BraketPair< F, PKet > _pket(const F &f1, const F &f2)
Physicists ket.
Definition: braket.h:448
bool operator==(const this_type &) const
Comparison function.
Definition: braket.h:302
BraketPair< F, CKet > _cket(const F &f1, const F &f2)
Chemists ket.
Definition: braket.h:456
Wedge is a typeholder for the result of a wedge product.
Definition: algebra.h:245
ArrayBraket< BFS, NP > this_type
This type.
Definition: braket.h:217
ArrayBraket()
This one is a very dangerous constructor – do not to use it if at all possible.
Definition: braket.h:269
void set_member(bfs_cref, unsigned int p, unsigned int i)
Sets i-th function for particle p.
Definition: braket.h:127
BraketType
enumerates types of brakets used for describing two-electron integrals: CBra and CKet are bra and ket...
Definition: braket.h:412
SubIteratorBase<T> provides a base class for a sub-iterator class for T.
Definition: iter.h:72
LIBINT2_UINT_LEAST64 key() const
Implements Hashable::key()
Definition: braket.h:349
unsigned int num_part() const
Returns the number of particles.
Definition: braket.h:161
ArrayBraket< typename BFS::iter_type, NP > iter_type
The iterator through ArrayBraket.
Definition: braket.h:221
SubIterator * member_subiter(unsigned int p, unsigned int i=0) const
Returns pointer to the SubIterator for i-th BFS of particle p.
Definition: braket.h:326
SubIterator * member_subiter(unsigned int p, unsigned int i) const
Returns pointer to the SubIterator for i-th BFS of particle p.
Definition: braket.h:120
void set_member(const BFS &, unsigned int p, unsigned int i=0)
Sets i-th function for particle p.
Definition: braket.h:333
ArrayBraket< BFS, 1 > Result
This defines which Braket implementation to use.
Definition: braket.h:396
VectorBraket()
This one is a very dangerous constructor – do not to use it if at all possible.
Definition: braket.h:82
This is the implementation of the Braket concept used by GenIntegralSet_1_1.
Definition: braket.h:394
Iterator provides a base class for all object iterator classes.
Definition: iter.h:44
unsigned int num_part() const
Returns the number of particles.
Definition: braket.h:249
bool operator==(const VectorBraket &) const
Comparison function.
Definition: braket.h:168
unsigned int num_members(unsigned int p) const
Returns the number of BFS for particle p.
Definition: braket.h:154
This is the implementation of the Braket concept used by GenIntegralSet_11_11.
Definition: braket.h:402
BraketPair is a trimmed down version of ArrayBraket specialized for same-particle or different-partic...
Definition: braket.h:416
BraketPair< F, PBra > _pbra(const F &f1, const F &f2)
Physicists bra.
Definition: braket.h:444
ArrayBraket is a lightweight implementation of Braket concept.
Definition: braket.h:214
BraketPair< BFS, BKType > this_type
This type.
Definition: braket.h:419
bfs_ref member(unsigned int p, unsigned int i)
Returns pointer to the i-th function for particle p.
Definition: braket.h:106
BraketPair(const BFS &f1, const BFS &f2)
This one is a very dangerous constructor – do not to use it if at all possible.
Definition: braket.h:424
ArrayBraket< BFS, 2 > Result
This defines which Braket implementation to use.
Definition: braket.h:404
VectorBraket is a std::vector-based type that can be used as a BraSetType or a KetSetType parameter t...
Definition: braket.h:35