LIBINT  2.6.0
generic_rr.h
1 /*
2  * Copyright (C) 2004-2019 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_genericrr_h_
22 #define _libint2_src_bin_libint_genericrr_h_
23 
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 #include <stdexcept>
29 #include <cassert>
30 #include <boost/type_traits/is_same.hpp>
31 
32 #include <dgvertex.h>
33 #include <rr.h>
34 #include <integral.h>
35 #include <algebra.h>
36 #include <flop.h>
37 #include <prefactors.h>
38 #include <context.h>
39 #include <default_params.h>
40 #include <util.h>
41 
42 using namespace std;
43 
44 namespace libint2 {
45 
48  template <typename RRImpl, typename F, typename Target>
50  public:
51  typedef F BasisFunctionType;
52  typedef Target TargetType;
55 
57  static SafePtr<RRImpl> Instance(const SafePtr<TargetType>& Tint, unsigned int dir) {
58  // screen out calls with nondefault extra parameters
59  if (!RRImpl::directional() && dir != 0)
60  return SafePtr<RRImpl>();
61  // attempt to construct
62  SafePtr<RRImpl> this_ptr(new RRImpl(Tint,dir));
63  // if succeeded (nchildren > 0) do post-construction
64  if (this_ptr->num_children() != 0) {
65  this_ptr->template register_with_rrstack<RRImpl>();
66  return this_ptr;
67  }
68  // else return null pointer
69  return SafePtr<RRImpl>();
70  }
71 
73  unsigned int num_children() const { return children_.size(); };
75  SafePtr<DGVertex> rr_target() const { return static_pointer_cast<DGVertex,TargetType>(target_); }
77  SafePtr<DGVertex> rr_child(unsigned int i) const {
78  return children_.at(i);
79  }
81  bool is_simple() const {
83  }
84 
86  std::string generate_label() const
87  {
88  ostringstream os;
89  os << RRImpl::descr() << " " << target_->label();
90  return os.str();
91  }
92 
93  protected:
94  GenericRecurrenceRelation(const SafePtr<TargetType>& Tint, unsigned int dir) :
95  target_(Tint), dir_(dir) {
96  children_.reserve(RRImpl::max_nchildren);
97  }
101  static bool default_directional() {
102  if (boost::is_same<BasisFunctionType,CGF>::value)
103  return true;
104  return false;
105  }
106 
107  unsigned int dir() const { return dir_; }
108 
110  const SafePtr<DGVertex>& add_child(const SafePtr<DGVertex>& child) {
111  typedef std::vector< SafePtr<DGVertex> > cvector;
112  typedef typename cvector::const_iterator citer;
113  const citer pos = std::find(children_.begin(),children_.end(),child);
114  if (pos == children_.end()) {
115  children_.push_back(child);
116  return *(children_.rbegin());
117  }
118  else
119  return *pos;
120  }
121 
123  template<class RR, class C> friend class ChildFactory;
126 #if 0
127  template <class RealChildType>
128  const SafePtr<DGVertex>& make_child(const typename RealChildType::BasisFunctionType& A,
129  const typename RealChildType::BasisFunctionType& B,
130  const typename RealChildType::BasisFunctionType& C,
131  const typename RealChildType::BasisFunctionType& D,
132  const typename RealChildType::AuxIndexType& aux = typename RealChildType::AuxIndexType(),
133  const typename RealChildType::OperType& oper = typename RealChildType::OperType()) {
134  const SafePtr<DGVertex>& i = static_pointer_cast<DGVertex,RealChildType>(ChildType::Instance(A,B,C,D,aux,oper));
135  return add_child(i);
136  }
137 #endif
138 
139  SafePtr<TargetType> target_;
140 
141  private:
142  unsigned int dir_;
143  std::vector< SafePtr<DGVertex> > children_;
144 
145  };
146 
148  template <class GenRR, class ChildType>
149  class ChildFactory {
150  public:
151  typedef typename ChildType::BasisFunctionType F;
152  typedef typename ChildType::AuxIndexType AuxIndexType;
153  typedef typename ChildType::OperType OperType;
154 
155  ChildFactory(GenRR* rr) : rr_(rr) {}
156 
158  const SafePtr<DGVertex>& make_child(const F& A,
159  const F& B,
160  const F& C,
161  const F& D,
162  const AuxIndexType& aux = AuxIndexType(),
163  const OperType& oper = OperType()) {
164  auto i = static_pointer_cast<DGVertex,ChildType>(ChildType::Instance(A,B,C,D,aux,oper));
165  return rr_->add_child(i);
166  }
168  const SafePtr<DGVertex>& make_child(const F& A,
169  const F& B,
170  const AuxIndexType& aux = AuxIndexType(),
171  const OperType& oper = OperType()) {
172  auto i = static_pointer_cast<DGVertex,ChildType>(ChildType::Instance(A,B,aux,oper));
173  return rr_->add_child(i);
174  }
176  const SafePtr<DGVertex>&
178  const AuxIndexType& aux = AuxIndexType(),
179  const OperType& oper = OperType()) {
180  auto i = static_pointer_cast<DGVertex,ChildType>(ChildType::Instance(braket_wedge,aux,oper));
181  return rr_->add_child(i);
182  }
184  const SafePtr<DGVertex>&
186  const AuxIndexType& aux = AuxIndexType(),
187  const OperType& oper = OperType()) {
188  auto i = static_pointer_cast<DGVertex,ChildType>(ChildType::Instance(braket_wedge,aux,oper));
189  return rr_->add_child(i);
190  }
192  void wedge(const LinearCombination< SafePtr<DGVertex>, BraketPair<F,PBra> >& bra_lc,
193  const LinearCombination< SafePtr<DGVertex>, BraketPair<F,PKet> >& ket_lc,
194  const AuxIndexType& aux = AuxIndexType(),
195  const OperType& oper = OperType()) {
196  using namespace libint2::algebra;
200  >
201  > ProductLC;
202  const ProductLC& product_lc = bra_lc ^ ket_lc;
203  const size_t nprod = product_lc.size();
204  for(unsigned int t=0; t<nprod; ++t) {
205  const typename ProductLC::term_t& term = product_lc[t];
206  auto child = make_child(term.second,aux,oper);
207  if (rr_->is_simple()) {
208  if (rr_->expr_)
209  rr_->expr_ += term.first * child;
210  else
211  rr_->expr_ = term.first * child;
212  }
213  }
214  }
215  void wedge(const BraketPair<F,PBra>& bra,
216  const LinearCombination< SafePtr<DGVertex>, BraketPair<F,PKet> >& ket_lc,
217  const AuxIndexType& aux = AuxIndexType(),
218  const OperType& oper = OperType()) {
219  using namespace libint2::prefactor;
221  bra_lc += make_pair(Scalar(1.0),bra);
222  wedge(bra_lc,ket_lc,aux,oper);
223  }
224  void wedge(const LinearCombination< SafePtr<DGVertex>, BraketPair<F,PBra> >& bra_lc,
225  const BraketPair<F,PKet>& ket,
226  const AuxIndexType& aux = AuxIndexType(),
227  const OperType& oper = OperType()) {
228  using namespace libint2::prefactor;
229  LinearCombination< SafePtr<DGVertex>, BraketPair<F,PKet> > ket_lc;
230  ket_lc += make_pair(Scalar(1.0),ket);
231  wedge(bra_lc,ket_lc,aux,oper);
232  }
233 
234  private:
235  GenRR* rr_;
236  };
237 
238 };
239 
240 #endif
static bool default_directional()
is this recurrence relation parameterized by a direction (x, y, or z).
Definition: generic_rr.h:101
const SafePtr< DGVertex > & make_child(const F &A, const F &B, const F &C, const F &D, const AuxIndexType &aux=AuxIndexType(), const OperType &oper=OperType())
make_child
Definition: generic_rr.h:158
static SafePtr< RRImpl > Instance(const SafePtr< TargetType > &Tint, unsigned int dir)
Return an instance if applicable, or a null pointer otherwise.
Definition: generic_rr.h:57
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:892
const SafePtr< DGVertex > & make_child(const algebra::Wedge< BraketPair< F, CBra >, BraketPair< F, CKet > > &braket_wedge, const AuxIndexType &aux=AuxIndexType(), const OperType &oper=OperType())
make a child from a wedge of chemists' brackets
Definition: generic_rr.h:185
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
Wedge is a typeholder for the result of a wedge product.
Definition: algebra.h:246
bool is_simple() const
Implementation of RecurrenceRelation::is_simple()
Definition: generic_rr.h:81
SafePtr< DGVertex > rr_child(unsigned int i) const
Implementation of RecurrenceRelation::rr_child()
Definition: generic_rr.h:77
void wedge(const LinearCombination< SafePtr< DGVertex >, BraketPair< F, PBra > > &bra_lc, const LinearCombination< SafePtr< DGVertex >, BraketPair< F, PKet > > &ket_lc, const AuxIndexType &aux=AuxIndexType(), const OperType &oper=OperType())
take a wedge product of various (linear combinations of) brakets
Definition: generic_rr.h:192
RRImpl must inherit GenericRecurrenceRelation<RRImpl>
Definition: generic_rr.h:49
represents linear combination of objects of type T with coefficients of type C
Definition: algebra.h:223
unsigned int num_children() const
Implementation of RecurrenceRelation::num_children()
Definition: generic_rr.h:73
AlgebraicOperator is an algebraic operator that acts on objects of type T.
Definition: algebra.h:48
SafePtr< DGVertex > rr_target() const
Implementation of RecurrenceRelation::rr_target()
Definition: generic_rr.h:75
const SafePtr< DGVertex > & make_child(const algebra::Wedge< BraketPair< F, PBra >, BraketPair< F, PKet > > &braket_wedge, const AuxIndexType &aux=AuxIndexType(), const OperType &oper=OperType())
make a child from a wedge of physicists' brackets
Definition: generic_rr.h:177
RecurrenceRelation describes all recurrence relations.
Definition: rr.h:101
const SafePtr< DGVertex > & add_child(const SafePtr< DGVertex > &child)
add child
Definition: generic_rr.h:110
BraketPair is a trimmed down version of ArrayBraket specialized for same-particle or different-partic...
Definition: braket.h:244
std::string generate_label() const
Implementation of RecurrenceRelation::generate_label()
Definition: generic_rr.h:86
Helps GenericRecurrenceRelation to work around the compiler problem with make_child.
Definition: generic_rr.h:149
const SafePtr< DGVertex > & make_child(const typename RealChildType::BasisFunctionType &A, const typename RealChildType::BasisFunctionType &B, const typename RealChildType::BasisFunctionType &C, const typename RealChildType::BasisFunctionType &D, const typename RealChildType::AuxIndexType &aux=typename RealChildType::AuxIndexType(), const typename RealChildType::OperType &oper=typename RealChildType::OperType())
make_child should really looks something like this, but gcc 4.3.0 craps out TODO test is this works
Definition: generic_rr.h:128
const SafePtr< DGVertex > & make_child(const F &A, const F &B, const AuxIndexType &aux=AuxIndexType(), const OperType &oper=OperType())
make_child
Definition: generic_rr.h:168