LIBINT 2.9.0
tactic.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <smart_ptr.h>
22
23#include <cmath>
24#include <cstdlib>
25#include <ctime>
26
27#ifndef _libint2_src_bin_libint_tactic_h_
28#define _libint2_src_bin_libint_tactic_h_
29
30namespace libint2 {
31
32class DirectedGraph;
33class RecurrenceRelation;
34
35struct DummyRandomizePolicy;
36class StdRandomizePolicy;
37
41class Tactic {
42 public:
43 typedef std::shared_ptr<RecurrenceRelation> RR;
44 typedef std::vector<RR> rr_stack;
45
46 Tactic() {}
47 virtual ~Tactic() {}
48
49 virtual RR optimal_rr(const rr_stack& stack) const = 0;
50
51 // make return true if need to debug Tactic classes
52 static constexpr bool class_debug() { return false; }
53};
54
57template <class RandomizePolicy = DummyRandomizePolicy>
58class FirstChoiceTactic : public Tactic {
59 public:
60 FirstChoiceTactic(const std::shared_ptr<RandomizePolicy>& rpolicy =
61 std::shared_ptr<RandomizePolicy>(new RandomizePolicy))
62 : Tactic(), rpolicy_(rpolicy) {}
63 virtual ~FirstChoiceTactic() {}
64
65 RR optimal_rr(const rr_stack& stack) const {
66 if (!stack.empty())
67 return stack[0 + rpolicy_->noise(stack.size())];
68 else
69 return RR();
70 }
71
72 private:
73 std::shared_ptr<RandomizePolicy> rpolicy_;
74};
75
80 public:
81 FewestNewVerticesTactic(const std::shared_ptr<DirectedGraph>& dg)
82 : Tactic(), dg_(dg) {}
83 virtual ~FewestNewVerticesTactic() {}
84
85 RR optimal_rr(const rr_stack& stack) const;
86
87 private:
88 std::shared_ptr<DirectedGraph> dg_;
89};
90
95 public:
96 ZeroNewVerticesTactic(const std::shared_ptr<DirectedGraph>& dg)
97 : Tactic(), dg_(dg) {}
98 virtual ~ZeroNewVerticesTactic() {}
99
100 RR optimal_rr(const rr_stack& stack) const;
101
102 private:
103 std::shared_ptr<DirectedGraph> dg_;
104};
105
109 public:
111 virtual ~RandomChoiceTactic() {}
112
113 RR optimal_rr(const rr_stack& stack) const;
114};
115
118class NullTactic : public Tactic {
119 public:
120 NullTactic() : Tactic() {}
121 virtual ~NullTactic() {}
122
123 RR optimal_rr(const rr_stack& stack) const;
124};
125
131 public:
136 ParticleDirectionTactic(bool increase) : Tactic(), increase_(increase) {}
137 virtual ~ParticleDirectionTactic() {}
138
139 RR optimal_rr(const rr_stack& stack) const;
140
141 private:
142 bool increase_;
143};
144
149 public:
154 TwoCenter_OS_Tactic(unsigned lbra0, unsigned lket0)
155 : Tactic(), lbra0_(lbra0), lket0_(lket0) {}
156 virtual ~TwoCenter_OS_Tactic() {}
157
158 RR optimal_rr(const rr_stack& stack) const;
159
160 private:
161 unsigned lbra0_;
162 unsigned lket0_;
163};
164
170 public:
177 FourCenter_OS_Tactic(unsigned lbra0, unsigned lket0, unsigned lbra1,
178 unsigned lket1)
179 : Tactic(), lbra0_(lbra0), lket0_(lket0), lbra1_(lbra1), lket1_(lket1) {}
180 virtual ~FourCenter_OS_Tactic() {}
181
182 RR optimal_rr(const rr_stack& stack) const;
183
184 private:
185 unsigned lbra0_;
186 unsigned lket0_;
187 unsigned lbra1_;
188 unsigned lket1_;
189};
190
192
194 unsigned int noise(unsigned int nrrs) const { return 0; }
195};
196
203 public:
204 StdRandomizePolicy(double scale) : scale_(scale) {
205 // Initialize state randomly
206 time_t t;
207 srandom(time(&t));
208 }
209
210 unsigned int noise(unsigned int nrrs) const {
211 unsigned long rand = random();
212 const unsigned long range = RAND_MAX;
213 const unsigned int result =
214 static_cast<unsigned int>(std::floor(nrrs * scale_ * rand / range));
215 return result;
216 }
217
218 private:
219 double scale_;
220};
221
222}; // namespace libint2
223
224#endif
FewestNewVerticesTactic chooses RR which adds fewest new vertices to DirectedGraph dg.
Definition tactic.h:79
FirstChoiceTactic simply chooses the first RR.
Definition tactic.h:58
FourCenter_OS_Tactic decides graph build for (bra0 ket0| bra1 ket1) = <bra0 bra1|ket0 ket1>
Definition tactic.h:169
FourCenter_OS_Tactic(unsigned lbra0, unsigned lket0, unsigned lbra1, unsigned lket1)
Definition tactic.h:177
NullTactic always returns null RecurrenceRelation.
Definition tactic.h:118
ParticleDirectionTactic returns the first RR that transfers the quantum numbers between particles in ...
Definition tactic.h:130
ParticleDirectionTactic(bool increase)
Definition tactic.h:136
RandomChoiceTactic chooses randomly among the applicable RRs.
Definition tactic.h:108
The shift parameter is computed as follows: delta = floor(nrrs*scale*random()/RAND_MAX) where nrrs is...
Definition tactic.h:202
Tactic is used to choose the optimal (in some sense) recurrence relation to reduce a vertex.
Definition tactic.h:41
TwoCenter_OS_Tactic decides graph build for <bra0|ket0>
Definition tactic.h:148
TwoCenter_OS_Tactic(unsigned lbra0, unsigned lket0)
Definition tactic.h:154
ZeroNewVerticesTactic chooses first RR which adds no new vertices on DirectedGraph dg.
Definition tactic.h:94
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
Definition tactic.h:193