21 #ifndef _libint2_src_bin_libint_integral_h_ 22 #define _libint2_src_bin_libint_integral_h_ 25 #include <smart_ptr.h> 29 #include <policy_spec.h> 32 #include <singl_stack.h> 33 #include <class_registry.h> 34 #include <global_macros.h> 42 extern long living_count;
56 virtual unsigned int num_part()
const =0;
59 virtual unsigned int num_func_bra(
unsigned int p)
const =0;
61 virtual unsigned int num_func_ket(
unsigned int p)
const =0;
63 virtual const BasisFunctionSet& bra(
unsigned int p,
unsigned int i)
const =0;
65 virtual const BasisFunctionSet& ket(
unsigned int p,
unsigned int i)
const =0;
67 virtual BasisFunctionSet& bra(
unsigned int p,
unsigned int i) =0;
69 virtual BasisFunctionSet& ket(
unsigned int p,
unsigned int i) =0;
71 virtual unsigned int np()
const =0;
74 virtual const SafePtr<BasisFunctionSet> bra(
unsigned int p,
unsigned int i)
const =0;
76 virtual const SafePtr<BasisFunctionSet> ket(
unsigned int p,
unsigned int i)
const =0;
90 template <
class Oper,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta = EmptySet>
93 public EnableSafePtrFromThis< GenIntegralSet<Oper,BFS,BraSetType,KetSetType,AuxQuanta> >
103 #if USE_INT_KEY_TO_HASH 104 typedef mpz_class key_type;
106 typedef std::string key_type;
115 static constexpr
auto num_particles = BraSetType::num_particles;
116 static_assert(BraSetType::num_particles == KetSetType::num_particles,
117 "number of particles in bra and ket must be same");
119 static constexpr
auto num_bf = BraSetType::num_bf + KetSetType::num_bf;
131 static const SafePtr<GenIntegralSet> Instance(
const BraSetType& bra,
const KetSetType& ket,
const AuxQuanta& aux,
const Oper& oper=
Oper());
136 bool equiv(
const SafePtr<DGVertex>& v)
const 138 return PtrComp::equiv(
this,v);
141 virtual unsigned int size()
const;
143 virtual const std::string& label()
const;
145 virtual const std::string& id()
const;
147 virtual std::string description()
const;
150 unsigned int num_part()
const {
return OperType::Properties::np; }
152 virtual unsigned int num_func_bra(
unsigned int p)
const {
return bra_.num_members(p); }
154 virtual unsigned int num_func_ket(
unsigned int p)
const {
return ket_.num_members(p); }
156 typename BraSetType::bfs_cref bra(
unsigned int p,
unsigned int i)
const;
158 typename KetSetType::bfs_cref ket(
unsigned int p,
unsigned int i)
const;
160 typename BraSetType::bfs_ref bra(
unsigned int p,
unsigned int i);
162 typename KetSetType::bfs_ref ket(
unsigned int p,
unsigned int i);
164 typedef BraSetType BraType;
165 typedef KetSetType KetType;
166 typedef Oper OperatorType;
167 typedef AuxQuanta AuxQuantaType;
170 const SafePtr<Oper> oper()
const;
172 const BraType& bra()
const;
174 const KetType& ket()
const;
176 const SafePtr<AuxQuanta> aux()
const;
179 DGVertex::KeyReturnType
key()
const {
180 if (key_ == 0) compute_key();
185 void unregister()
const;
192 GenIntegralSet(
const Oper& oper,
const BraSetType& bra,
const KetSetType& ket,
const AuxQuanta& aux);
195 #define TEST_KEYTYPE_SAFETY 0 196 #if TEST_KEYTYPE_SAFETY 197 key_type remainder = UINT64_MAX;
198 remainder /= (key_type)aux.max_key(); assert(remainder != 0);
199 remainder /= (key_type)ket.
max_key(); assert(remainder != 0);
200 remainder /= (key_type)bra.
max_key(); assert(remainder != 0);
201 remainder /= (key_type)O.max_key; assert(remainder != 0);
206 key = ( (key_type(O.key()) * KeyTypes::cast(bra.
max_key()) + KeyTypes::cast(bra.
key()) ) * KeyTypes::cast(ket.
max_key()) +
207 KeyTypes::cast(ket.
key()) ) * KeyTypes::cast(aux.max_key()) + KeyTypes::cast(aux.key());
217 void set_size(
unsigned int sz);
235 static SingletonManagerType singl_manager_;
240 SafePtr<AuxQuanta> aux_;
243 mutable unsigned int size_;
246 mutable std::string label_;
248 std::string generate_label()
const;
250 mutable key_type key_;
253 void compute_key()
const {
254 key_ = compute_key(*(O_.get()),bra_,ket_,*(aux_.get()));
259 #if USE_INT_KEY_TO_HASH 261 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
262 typename GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::SingletonManagerType
263 GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::singl_manager_(&GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::key);
266 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
267 typename GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::SingletonManagerType
268 GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::singl_manager_(&GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::label);
271 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
272 GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::GenIntegralSet(
const Op& oper,
const BraSetType& bra,
const KetSetType& ket,
const AuxQuanta& aux) :
273 DGVertex(ClassInfo<GenIntegralSet>::Instance().id()), bra_(bra), ket_(ket), O_(SafePtr<Op>(new Op(oper))), aux_(SafePtr<AuxQuanta>(new AuxQuanta(aux))),
276 if (Op::Properties::np != bra.num_part())
277 throw std::runtime_error(
"GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::GenIntegralSet(bra,ket) -- number of particles in bra doesn't match that in the operator");
278 if (Op::Properties::np != ket.num_part())
279 throw std::runtime_error(
"GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::GenIntegralSet(bra,ket) -- number of particles in ket doesn't match that in the operator");
282 std::cout <<
"GenIntegralSet: constructed " << label() << std::endl;
283 std::cout <<
"GenIntegralSet: living_count = " << ++living_count << std::endl;
287 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
291 std::cout <<
"GenIntegralSet: destructed " << label() << std::endl;
292 std::cout <<
"GenIntegralSet: living_count = " << --living_count << std::endl;
296 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
297 const SafePtr< GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta> >
300 typedef typename SingletonManagerType::value_type map_value_type;
301 key_type key = compute_key(oper,bra,ket,aux);
302 const map_value_type& val = singl_manager_.find(key);
304 SafePtr<this_type> this_int(
new this_type(oper,bra,ket,aux));
306 const map_value_type& val = singl_manager_.find(this_int);
307 val.second->instid_ = val.first;
314 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
315 typename BraSetType::bfs_cref
318 return bra_.member(p,i);
321 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
322 typename KetSetType::bfs_cref
325 return ket_.member(p,i);
328 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
329 typename BraSetType::bfs_ref
333 return bra_.member(p,i);
336 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
337 typename KetSetType::bfs_ref
341 return ket_.member(p,i);
344 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
345 const typename GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::BraType&
351 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
352 const typename GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::KetType&
358 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
365 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
366 const SafePtr<AuxQuanta>
372 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
376 #if USE_INT_KEY_TO_COMPARE 377 return key() == a.
key();
380 if (!aux_equiv)
return false;
383 if (!bra_equiv)
return false;
385 if (!ket_equiv)
return false;
390 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
394 singl_manager_.remove(const_pointer_cast<this_type,const this_type>(EnableSafePtrFromThis<this_type>::SafePtr_from_this()));
397 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
404 #if COMPUTE_SIZE_DIRECTLY 407 size_ = bra_.
size() * ket_.size() * O_->num_oper();
410 SafePtr<this_type> this_ptr = const_pointer_cast<this_type,const this_type>(EnableSafePtrFromThis<GenIntegralSet>::SafePtr_from_this());
412 size_ = siter->num_iter();
414 throw std::runtime_error(
"GenIntegralSet<Op,BFS,BraSetType,KetSetType,AuxQuanta>::size() -- size is 0");
420 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
427 template <
class BraSetType,
class KetSetType,
class AuxQuanta,
class Op>
429 genintegralset_label(
const BraSetType& bra,
const KetSetType& ket,
const SafePtr<AuxQuanta>& aux,
const SafePtr<Op>& O)
433 for(
unsigned int p=0; p<Op::Properties::np; p++) {
434 unsigned int nbra = bra.num_members(p);
435 for(
unsigned int i=0; i<nbra; i++)
437 os << bra.member(p,i).label() <<
"(" << p <<
") ";
439 os << bra.member(p,i)->label() <<
"(" << p <<
") ";
442 os <<
"| " << O->label() <<
" | ";
443 for(
unsigned int p=0; p<Op::Properties::np; p++) {
444 unsigned int nket = ket.num_members(p);
445 for(
unsigned int i=0; i<nket; i++)
447 os << ket.member(p,i).label() <<
"(" << p <<
") ";
449 os << ket.member(p,i)->label() <<
"(" << p <<
") ";
452 os <<
"> ^ { " << aux->label() <<
" }";
456 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
461 label_ = generate_label();
465 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
469 return genintegralset_label(bra_,ket_,aux_,O_);
472 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
479 template <
class Op,
class BFS,
class BraSetType,
class KetSetType,
class AuxQuanta>
484 os <<
" GenIntegralSet: " << label();
485 const std::string descr = os.str();
virtual unsigned int size() const
Specialization of DGVertex::size()
Definition: integral.h:399
GenIntegralSet< typename Oper::iter_type, BFS, typename BraSetType::iter_type, typename KetSetType::iter_type, typename AuxQuanta::iter_type > iter_type
GenIntegralSet is a set of these subobjects.
Definition: integral.h:98
IntegralSet< BFS > parent_type
GenIntegralSet is derived from IntegralSet.
Definition: integral.h:100
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition: singl_stack.h:43
bool equiv(const SafePtr< DGVertex > &v) const
Specialization of DGVertex::equiv()
Definition: integral.h:136
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
GenIntegralSet is a set of integrals over functions derived from BFS.
Definition: integral.h:91
This is a vertex of a Directed Graph (DG)
Definition: dgvertex.h:43
SubIteratorBase<T> provides a base class for a sub-iterator class for T.
Definition: iter.h:73
virtual bool auto_unroll() const
If consists of precomputed elements, override this to return true.
Definition: integral.h:188
PtrEquiv<T> provides a set of comparison functions named 'equiv' which take as arguments a mix of ref...
Definition: equiv.h:36
Oper is OperSet characterized by properties Props.
Definition: oper.h:90
BraSetType::bfs_type BasisFunctionType
This is the real type of basis functions.
Definition: integral.h:113
PtrEquiv< GenIntegralSet > PtrComp
This type provides comparison operations on pointers to GenIntegralSet.
Definition: integral.h:102
DGVertex::KeyReturnType key() const
Implements Hashable::key()
Definition: integral.h:179
LIBINT2_UINT_LEAST64 key() const
Implements Hashable::key()
Definition: braket.h:177
virtual unsigned int num_func_bra(unsigned int p) const
Implementation of IntegralSet::num_func_bra.
Definition: integral.h:152
unsigned int num_part() const
Implementation of IntegralSet::num_part.
Definition: integral.h:150
LIBINT2_UINT_LEAST64 max_key() const
key lies in range [0,max_key())
Definition: braket.h:190
This is an abstract base for sets of all types of integrals.
Definition: integral.h:50
virtual bool this_precomputed() const
Specialization of DGVertex::this_precomputed()
Definition: integral.h:219
virtual unsigned int num_func_ket(unsigned int p) const
Implementation of IntegralSet::num_func_ket.
Definition: integral.h:154
void reset_cache()
Resets all cached values.
Definition: integral.h:221
ArrayBraket is a lightweight implementation of Braket concept.
Definition: braket.h:38
Oper OperType
This is the type of the operator.
Definition: integral.h:111
static key_type compute_key(const Oper &O, const BraType &bra, const KetType &ket, const AuxQuanta &aux)
computes a key. it's protected so that derived classes can use it to implement smart caching in const...
Definition: integral.h:194