LIBINT  2.6.0
tactic.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 #include <cmath>
22 #include <cstdlib>
23 #include <ctime>
24 #include <smart_ptr.h>
25 
26 #ifndef _libint2_src_bin_libint_tactic_h_
27 #define _libint2_src_bin_libint_tactic_h_
28 
29 using namespace std;
30 
31 namespace libint2 {
32 
33  class DirectedGraph;
34  class RecurrenceRelation;
35 
36  class DummyRandomizePolicy;
37  class StdRandomizePolicy;
38 
42  class Tactic {
43  public:
44  typedef SafePtr<RecurrenceRelation> RR;
45  typedef vector<RR> rr_stack;
46 
47  Tactic() {}
48  virtual ~Tactic() {}
49 
50  virtual RR optimal_rr(const rr_stack& stack) const =0;
51 
52  // make return true if need to debug Tactic classes
53  static constexpr bool class_debug() {
54  return false;
55  }
56  };
57 
60  template <class RandomizePolicy = DummyRandomizePolicy>
61  class FirstChoiceTactic : public Tactic {
62  public:
63  FirstChoiceTactic(const SafePtr<RandomizePolicy>& rpolicy = SafePtr<RandomizePolicy>(new RandomizePolicy)) : Tactic(), rpolicy_(rpolicy) {}
64  virtual ~FirstChoiceTactic() {}
65 
66  RR optimal_rr(const rr_stack& stack) const {
67  if (!stack.empty())
68  return stack[0 + rpolicy_->noise(stack.size())];
69  else
70  return RR();
71  }
72 
73  private:
74  SafePtr<RandomizePolicy> rpolicy_;
75  };
76 
81  public:
82  FewestNewVerticesTactic(const SafePtr<DirectedGraph>& dg) : Tactic(), dg_(dg) {}
83  virtual ~FewestNewVerticesTactic() {}
84 
85  RR optimal_rr(const rr_stack& stack) const;
86 
87  private:
88  SafePtr<DirectedGraph> dg_;
89  };
90 
94  class ZeroNewVerticesTactic : public Tactic {
95  public:
96  ZeroNewVerticesTactic(const SafePtr<DirectedGraph>& dg) : Tactic(), dg_(dg) {}
97  virtual ~ZeroNewVerticesTactic() {}
98 
99  RR optimal_rr(const rr_stack& stack) const;
100 
101  private:
102  SafePtr<DirectedGraph> dg_;
103  };
104 
107  class RandomChoiceTactic : public Tactic {
108  public:
110  virtual ~RandomChoiceTactic() {}
111 
112  RR optimal_rr(const rr_stack& stack) const;
113  };
114 
117  class NullTactic : public Tactic {
118  public:
119  NullTactic() : Tactic() {}
120  virtual ~NullTactic() {}
121 
122  RR optimal_rr(const rr_stack& stack) const;
123  };
124 
130  public:
134  ParticleDirectionTactic(bool increase) : Tactic(), increase_(increase) {}
135  virtual ~ParticleDirectionTactic() {}
136 
137  RR optimal_rr(const rr_stack& stack) const;
138  private:
139  bool increase_;
140  };
141 
145  class TwoCenter_OS_Tactic : public Tactic {
146  public:
151  TwoCenter_OS_Tactic(unsigned lbra0,
152  unsigned lket0) : Tactic(), lbra0_(lbra0), lket0_(lket0) {}
153  virtual ~TwoCenter_OS_Tactic() {}
154 
155  RR optimal_rr(const rr_stack& stack) const;
156  private:
157  unsigned lbra0_;
158  unsigned lket0_;
159  };
160 
164  class FourCenter_OS_Tactic : public Tactic {
165  public:
172  FourCenter_OS_Tactic(unsigned lbra0,
173  unsigned lket0,
174  unsigned lbra1,
175  unsigned lket1) : Tactic(), lbra0_(lbra0), lket0_(lket0),
176  lbra1_(lbra1), lket1_(lket1) {}
177  virtual ~FourCenter_OS_Tactic() {}
178 
179  RR optimal_rr(const rr_stack& stack) const;
180  private:
181  unsigned lbra0_;
182  unsigned lket0_;
183  unsigned lbra1_;
184  unsigned lket1_;
185  };
186 
188 
190  unsigned int noise(unsigned int nrrs) const { return 0; }
191  };
192 
199  public:
200  StdRandomizePolicy(double scale) : scale_(scale) {
201  // Initialize state randomly
202  time_t t;
203  srandom(time(&t));
204  }
205 
206  unsigned int noise(unsigned int nrrs) const {
207  unsigned long rand = random();
208  const unsigned long range = RAND_MAX;
209  const unsigned int result = static_cast<unsigned int>(std::floor(nrrs*scale_*rand/range));
210  return result;
211  }
212 
213  private:
214  double scale_;
215  };
216 
217 
218 };
219 
220 #endif
221 
FirstChoiceTactic simply chooses the first RR.
Definition: tactic.h:61
TwoCenter_OS_Tactic(unsigned lbra0, unsigned lket0)
Definition: tactic.h:151
NullTactic always returns null RecurrenceRelation.
Definition: tactic.h:117
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
Tactic is used to choose the optimal (in some sense) recurrence relation to reduce a vertex.
Definition: tactic.h:42
ParticleDirectionTactic(bool increase)
Definition: tactic.h:134
ZeroNewVerticesTactic chooses first RR which adds no new vertices on DirectedGraph dg.
Definition: tactic.h:94
FourCenter_OS_Tactic decides graph build for (bra0 ket0| bra1 ket1) = <bra0 bra1|ket0 ket1>
Definition: tactic.h:164
TwoCenter_OS_Tactic decides graph build for <bra0|ket0>
Definition: tactic.h:145
ParticleDirectionTactic returns the first RR that transfers the quantum numbers between particles in ...
Definition: tactic.h:129
FewestNewVerticesTactic chooses RR which adds fewest new vertices to DirectedGraph dg.
Definition: tactic.h:80
The shift parameter is computed as follows: delta = floor(nrrs*scale*random()/RAND_MAX) where nrrs is...
Definition: tactic.h:198
FourCenter_OS_Tactic(unsigned lbra0, unsigned lket0, unsigned lbra1, unsigned lket1)
Definition: tactic.h:172
Definition: tactic.h:189
RandomChoiceTactic chooses randomly among the applicable RRs.
Definition: tactic.h:107