LIBINT 2.7.2
src/bin/libint/braket.h
1/*
2 * Copyright (C) 2004-2021 Edward F. Valeev
3 *
4 * This file is part of Libint.
5 *
6 * Libint is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Libint. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef _libint2_src_bin_libint_braket_h_
22#define _libint2_src_bin_libint_braket_h_
23
24#include <polyconstr.h>
25#include <bfset.h>
26#include <algebra.h>
27#include <libint2/util/intrinsic_types.h>
28#include <global_macros.h>
29
30namespace libint2 {
31
33
38 template <class BFS, unsigned int NP> class ArrayBraket {
39 public:
43 typedef struct{} parent_type;
47 static constexpr auto num_particles = NP;
49 static constexpr auto num_bf = NP;
50
51 typedef BFS bfs_type;
52 typedef bfs_type& bfs_ref;
53 typedef const BFS& bfs_cref;
54
58 ArrayBraket(const BFS* braket);
59 ArrayBraket(const std::vector<std::vector<BFS> >& braket);
61
63 bool operator==(const this_type&) const;
65 bfs_cref member(unsigned int p, unsigned int i=0) const;
67 bfs_ref member(unsigned int p, unsigned int i=0);
69 SubIterator* member_subiter(unsigned int p, unsigned int i=0) const;
71 void set_member(const BFS&, unsigned int p, unsigned int i=0);
73 void set_member(const ConstructablePolymorphically&, unsigned int p, unsigned int i=0);
75 unsigned int num_members(unsigned int p) const { assert(p<NP); return 1; }
77 unsigned int num_part() const { return NP; }
79 inline LIBINT2_UINT_LEAST64 key() const;
81 LIBINT2_UINT_LEAST64 max_key() const;
82#if COMPUTE_SIZE_DIRECTLY
83 unsigned int size() const;
84#endif
85
86 private:
87 BFS bfs_[NP];
88
89 // this function resets all cached values
90 void reset_cache();
91#if COMPUTE_SIZE_DIRECTLY
92 mutable unsigned int size_;
93#endif
94 };
95
96 template <class BFS, unsigned int NP>
98 {
99#if COMPUTE_SIZE_DIRECTLY
100 size_ = 0;
101#endif
102 }
103
104 template <class BFS, unsigned int NP>
105 ArrayBraket<BFS,NP>::ArrayBraket(const BFS* braket) {
106 for(int i=0; i<NP; i++)
107 bfs_[i] = braket[i];
108#if COMPUTE_SIZE_DIRECTLY
109 size_ = 0;
110#endif
111 }
112
113 template <class BFS, unsigned int NP>
114 ArrayBraket<BFS,NP>::ArrayBraket(const std::vector<std::vector<BFS> >& braket) {
115 assert(braket.size()==NP);
116 for(unsigned int i=0; i<NP; i++) {
117 assert(braket[i].size()==1);
118 bfs_[i] = braket[i][0];
119 }
120#if COMPUTE_SIZE_DIRECTLY
121 size_ = 0;
122#endif
123 }
124
125 template <class BFS, unsigned int NP>
126 ArrayBraket<BFS,NP>::~ArrayBraket() {}
127
128 template <class BFS, unsigned int NP>
129 bool
131 for(int i=0; i<NP; i++)
132 if (bfs_[i] != a[i])
133 return false;
134 return true;
135 }
136
137 template <class BFS, unsigned int NP>
138 typename ArrayBraket<BFS,NP>::bfs_cref
139 ArrayBraket<BFS,NP>::member(unsigned int p, unsigned int i) const {
140 assert(i==0 && p<NP);
141 return bfs_[p];
142 }
143
144 template <class BFS, unsigned int NP>
145 typename ArrayBraket<BFS,NP>::bfs_ref
146 ArrayBraket<BFS,NP>::member(unsigned int p, unsigned int i) {
147 assert(i==0 && p<NP);
148 reset_cache();
149 return bfs_[p];
150 }
151
152 template <class BFS, unsigned int NP>
154 ArrayBraket<BFS,NP>::member_subiter(unsigned int p, unsigned int i) const {
155 assert(i==0 && p<NP);
156 return static_cast<SubIterator*>(new SubIteratorBase<BFS>( member(p,i) ) );
157 }
158
159 template <class BFS, unsigned int NP>
160 void
161 ArrayBraket<BFS,NP>::set_member(const BFS& f, unsigned int p, unsigned int i) {
162 assert(i==0 && p<NP);
163 reset_cache();
164 bfs_[p] = f;
165 }
166
167 template <class BFS, unsigned int NP>
168 void
169 ArrayBraket<BFS,NP>::set_member(const ConstructablePolymorphically& f, unsigned int p, unsigned int i) {
170 assert(i==0 && p<NP);
171 reset_cache();
172 bfs_[p] = BFS(f);
173 }
174
175 template <class BFS, unsigned int NP>
176 LIBINT2_UINT_LEAST64
178 LIBINT2_UINT_LEAST64 pfac = 1;
179 LIBINT2_UINT_LEAST64 key = 0;
180 for(int p=NP-1; p>=0; p--) {
181 key += pfac*bfs_[p].key();
182 pfac *= BFS::max_key;
183 }
184 assert(key < this->max_key());
185 return key;
186 }
187
188 template <class BFS, unsigned int NP>
189 LIBINT2_UINT_LEAST64
191 LIBINT2_UINT_LEAST64 max_key = 1;
192 for(int p=NP-1; p>=0; p--) {
193 max_key *= BFS::max_key;
194 }
195 return max_key;
196 }
197
198#if COMPUTE_SIZE_DIRECTLY
199 template <class BFS, unsigned int NP>
200 unsigned int
202 if (size_ > 0) return size_;
203 size_ = 1;
205 for(int p=NP-1; p>=0; p--) {
206 size_ *= bfs_[p].num_bf();
207 }
208 }
209 return size_;
210 }
211#endif
212
213 template <class BFS, unsigned int NP>
214 void
215 ArrayBraket<BFS,NP>::reset_cache() {
216 size_ = 0;
217 }
218
220 // really need to have typedef template!
221 template <typename BFS>
225 };
226
228 // really need to have typedef template!
229 template <typename BFS>
233 };
234
236
240 enum BraketType {CBra, CKet, PBra, PKet};
244 template <class BFS, BraketType BKType> class BraketPair {
245 public:
248 typedef BFS bfs_type;
249
252 BraketPair(const BFS& f1, const BFS& f2) : bfs_(f1,f2) {}
253 BraketPair(const BraketPair& source) : bfs_(source.bfs_) {}
254 BraketPair& operator=(const BraketPair& rhs) { bfs_ = rhs.bfs_; return *this; }
255 const BFS& operator[](unsigned int i) const {
256 if (i == 0) return bfs_.first;
257 if (i == 1) return bfs_.second;
258 throw std::logic_error("BraketPair::operator[] -- argument out of range");
259 }
261 bool operator==(const this_type& rhs) const {
262 return rhs.bfs_ == bfs_;
263 }
264
265 private:
266 std::pair<BFS,BFS> bfs_;
267 };
268
270 namespace braket {
272 template <class F> BraketPair<F,PBra> _pbra(const F& f1, const F& f2) {
273 return BraketPair<F,PBra>(f1,f2);
274 }
276 template <class F> BraketPair<F,PKet> _pket(const F& f1, const F& f2) {
277 return BraketPair<F,PKet>(f1,f2);
278 }
280 template <class F> BraketPair<F,CBra> _cbra(const F& f1, const F& f2) {
281 return BraketPair<F,CBra>(f1,f2);
282 }
284 template <class F> BraketPair<F,CKet> _cket(const F& f1, const F& f2) {
285 return BraketPair<F,CKet>(f1,f2);
286 }
287 };
288
289 template <class BFS, BraketType BKTypeL, BraketType BKTypeR>
290 algebra::Wedge< BraketPair<BFS,BKTypeL>, BraketPair<BFS,BKTypeR> >
291 operator^(const BraketPair<BFS,BKTypeL>& L, const BraketPair<BFS,BKTypeR>& R) {
292 return algebra::make_wedge(L,R);
293 }
294
295};
296
297#endif
ArrayBraket is a lightweight implementation of Braket concept.
Definition: src/bin/libint/braket.h:38
ArrayBraket< typename BFS::iter_type, NP > iter_type
The iterator through ArrayBraket.
Definition: src/bin/libint/braket.h:45
bool operator==(const this_type &) const
Comparison function.
Definition: src/bin/libint/braket.h:130
LIBINT2_UINT_LEAST64 max_key() const
key lies in range [0,max_key())
Definition: src/bin/libint/braket.h:190
static constexpr auto num_particles
The total # of particles.
Definition: src/bin/libint/braket.h:47
static constexpr auto num_bf
The total # of basis functions = # of particles (since 1 bf / particle)
Definition: src/bin/libint/braket.h:49
unsigned int num_members(unsigned int p) const
Returns the number of BFS for particle p.
Definition: src/bin/libint/braket.h:75
LIBINT2_UINT_LEAST64 key() const
Implements Hashable::key()
Definition: src/bin/libint/braket.h:177
ArrayBraket()
This one is a very dangerous constructor – do not to use it if at all possible.
Definition: src/bin/libint/braket.h:97
void set_member(const BFS &, unsigned int p, unsigned int i=0)
Sets i-th function for particle p.
Definition: src/bin/libint/braket.h:161
SubIterator * member_subiter(unsigned int p, unsigned int i=0) const
Returns pointer to the SubIterator for i-th BFS of particle p.
Definition: src/bin/libint/braket.h:154
ArrayBraket< BFS, NP > this_type
This type.
Definition: src/bin/libint/braket.h:41
unsigned int num_part() const
Returns the number of particles.
Definition: src/bin/libint/braket.h:77
bfs_cref member(unsigned int p, unsigned int i=0) const
Returns cref to the i-th function for particle p.
Definition: src/bin/libint/braket.h:139
BraketPair is a trimmed down version of ArrayBraket specialized for same-particle or different-partic...
Definition: src/bin/libint/braket.h:244
bool operator==(const this_type &rhs) const
Comparison function.
Definition: src/bin/libint/braket.h:261
BraketPair(const BFS &f1, const BFS &f2)
This one is a very dangerous constructor – do not to use it if at all possible.
Definition: src/bin/libint/braket.h:252
BraketPair< BFS, BKType > this_type
This type.
Definition: src/bin/libint/braket.h:247
ConstructablePolymorphically is a base for all objects which can be constructed using a SafePtr to a ...
Definition: polyconstr.h:31
SubIteratorBase<T> provides a base class for a sub-iterator class for T.
Definition: iter.h:70
Iterator provides a base class for all object iterator classes.
Definition: iter.h:42
BraketPair< F, PKet > _pket(const F &f1, const F &f2)
Physicists ket.
Definition: src/bin/libint/braket.h:276
BraketPair< F, PBra > _pbra(const F &f1, const F &f2)
Physicists bra.
Definition: src/bin/libint/braket.h:272
BraketPair< F, CBra > _cbra(const F &f1, const F &f2)
Chemists bra.
Definition: src/bin/libint/braket.h:280
BraketPair< F, CKet > _cket(const F &f1, const F &f2)
Chemists ket.
Definition: src/bin/libint/braket.h:284
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
BraketType
enumerates types of brakets used for describing two-electron integrals: CBra and CKet are bra and ket...
Definition: src/bin/libint/braket.h:240
There's no parent.
Definition: src/bin/libint/braket.h:43
This is the implementation of the Braket concept used by GenIntegralSet_1_1.
Definition: src/bin/libint/braket.h:222
ArrayBraket< BFS, 1 > Result
This defines which Braket implementation to use.
Definition: src/bin/libint/braket.h:224
This is the implementation of the Braket concept used by GenIntegralSet_11_11.
Definition: src/bin/libint/braket.h:230
ArrayBraket< BFS, 2 > Result
This defines which Braket implementation to use.
Definition: src/bin/libint/braket.h:232
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:892