LIBINT  2.1.0-stable
tactic.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 #include <cmath>
21 #include <cstdlib>
22 #include <smart_ptr.h>
23 
24 #ifndef _libint2_src_bin_libint_tactic_h_
25 #define _libint2_src_bin_libint_tactic_h_
26 
27 using namespace std;
28 
29 namespace libint2 {
30 
31  class DirectedGraph;
32  class RecurrenceRelation;
33 
34  class DummyRandomizePolicy;
35  class StdRandomizePolicy;
36 
40  class Tactic {
41  public:
42  typedef SafePtr<RecurrenceRelation> RR;
43  typedef vector<RR> rr_stack;
44 
45  Tactic() {}
46  virtual ~Tactic() {}
47 
48  virtual RR optimal_rr(const rr_stack& stack) const =0;
49  };
50 
53  template <class RandomizePolicy = DummyRandomizePolicy>
54  class FirstChoiceTactic : public Tactic {
55  public:
56  FirstChoiceTactic(const SafePtr<RandomizePolicy>& rpolicy = SafePtr<RandomizePolicy>(new RandomizePolicy)) : Tactic(), rpolicy_(rpolicy) {}
57  virtual ~FirstChoiceTactic() {}
58 
59  RR optimal_rr(const rr_stack& stack) const {
60  if (!stack.empty())
61  return stack[0 + rpolicy_->noise(stack.size())];
62  else
63  return RR();
64  }
65 
66  private:
67  SafePtr<RandomizePolicy> rpolicy_;
68  };
69 
74  public:
75  FewestNewVerticesTactic(const SafePtr<DirectedGraph>& dg) : Tactic(), dg_(dg) {}
76  virtual ~FewestNewVerticesTactic() {}
77 
78  RR optimal_rr(const rr_stack& stack) const;
79 
80  private:
81  SafePtr<DirectedGraph> dg_;
82  };
83 
87  class ZeroNewVerticesTactic : public Tactic {
88  public:
89  ZeroNewVerticesTactic(const SafePtr<DirectedGraph>& dg) : Tactic(), dg_(dg) {}
90  virtual ~ZeroNewVerticesTactic() {}
91 
92  RR optimal_rr(const rr_stack& stack) const;
93 
94  private:
95  SafePtr<DirectedGraph> dg_;
96  };
97 
100  class RandomChoiceTactic : public Tactic {
101  public:
103  virtual ~RandomChoiceTactic() {}
104 
105  RR optimal_rr(const rr_stack& stack) const;
106  };
107 
110  class NullTactic : public Tactic {
111  public:
112  NullTactic() : Tactic() {}
113  virtual ~NullTactic() {}
114 
115  RR optimal_rr(const rr_stack& stack) const;
116  };
117 
123  public:
127  ParticleDirectionTactic(bool increase) : Tactic(), increase_(increase) {}
128  virtual ~ParticleDirectionTactic() {}
129 
130  RR optimal_rr(const rr_stack& stack) const;
131  private:
132  bool increase_;
133  };
134 
138  class FourCenter_OS_Tactic : public Tactic {
139  public:
146  FourCenter_OS_Tactic(unsigned lbra0,
147  unsigned lket0,
148  unsigned lbra1,
149  unsigned lket1) : Tactic(), lbra0_(lbra0), lket0_(lket0),
150  lbra1_(lbra1), lket1_(lket1) {}
151  virtual ~FourCenter_OS_Tactic() {}
152 
153  RR optimal_rr(const rr_stack& stack) const;
154  private:
155  unsigned lbra0_;
156  unsigned lket0_;
157  unsigned lbra1_;
158  unsigned lket1_;
159  };
160 
162 
164  unsigned int noise(unsigned int nrrs) const { return 0; }
165  };
166 
173  public:
174  StdRandomizePolicy(double scale) : scale_(scale) {
175  // Initialize state randomly
176  time_t t;
177  srandom(time(&t));
178  }
179 
180  unsigned int noise(unsigned int nrrs) const {
181  unsigned long rand = random();
182  const unsigned long range = RAND_MAX;
183  const unsigned int result = static_cast<unsigned int>(std::floor(nrrs*scale_*rand/range));
184  return result;
185  }
186 
187  private:
188  double scale_;
189  };
190 
191 
192 };
193 
194 #endif
195 
FirstChoiceTactic simply chooses the first RR.
Definition: tactic.h:54
NullTactic always returns null RecurrenceRelation.
Definition: tactic.h:110
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
Definition: stdarray.h:18
Tactic is used to choose the optimal (in some sense) recurrence relation to reduce a vertex...
Definition: tactic.h:40
ParticleDirectionTactic(bool increase)
Definition: tactic.h:127
ZeroNewVerticesTactic chooses first RR which adds no new vertices on DirectedGraph dg...
Definition: tactic.h:87
FourCenter_OS_Tactic decides graph build for (bra0 ket0| bra1 ket1) = <bra0 bra1|ket0 ket1> ...
Definition: tactic.h:138
ParticleDirectionTactic returns the first RR that transfers the quantum numbers between particles in ...
Definition: tactic.h:122
FewestNewVerticesTactic chooses RR which adds fewest new vertices to DirectedGraph dg...
Definition: tactic.h:73
The shift parameter is computed as follows: delta = floor(nrrs*scale*random()/RAND_MAX) where nrrs is...
Definition: tactic.h:172
FourCenter_OS_Tactic(unsigned lbra0, unsigned lket0, unsigned lbra1, unsigned lket1)
Definition: tactic.h:146
Definition: tactic.h:163
RandomChoiceTactic chooses randomly among the applicable RRs.
Definition: tactic.h:100