1 //
2 // BAGEL - Brilliantly Advanced General Electronic Structure Library
3 // Filename: gamma_tree.cc
4 // Copyright (C) 2013 Toru Shiozaki
5 //
6 // Author: Shane Parker <shane.parker@u.northwestern.edu>
7 // Maintainer: Toru Shiozaki
8 //
9 // This file is part of the BAGEL package.
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 //
24 
25 #include <src/asd/gamma_tree.h>
26 
27 using namespace std;
28 using namespace bagel;
29 
30 template<typename VecType>
insert(shared_ptr<const VecType> bra,const size_t bra_tag,const std::list<GammaSQ> & gsq)31 void GammaBranch<VecType>::insert(shared_ptr<const VecType> bra, const size_t bra_tag, const std::list<GammaSQ>& gsq) {
32   if (gsq.empty()) {
33     bras_.emplace(bra_tag, bra);
34   } else {
35     auto first = gsq.back();
36     auto rest = gsq; rest.pop_back();
37     shared_ptr<GammaBranch<VecType>> target = branches_[static_cast<int>(first)];
38 
39     target->activate();
40     target->insert(bra, bra_tag, rest);
41   }
42 }
43 
44 
45 template<typename VecType>
search(const size_t tag,const std::list<GammaSQ> & gsq) const46 shared_ptr<const Matrix> GammaBranch<VecType>::search(const size_t tag, const std::list<GammaSQ>& gsq) const {
47   if (gsq.empty()) {
48     assert(gammas_.find(tag)!=gammas_.end()); return gammas_.find(tag)->second;
49   } else {
50     auto first = gsq.back();
51     auto rest = gsq; rest.pop_back();
52     return branch(first)->search(tag, rest);
53   }
54 }
55 
56 
57 template<typename VecType>
search(const size_t tag,const std::list<GammaSQ> & gsq)58 shared_ptr<Matrix> GammaBranch<VecType>::search(const size_t tag, const std::list<GammaSQ>& gsq) {
59   if (gsq.empty()) {
60     assert(gammas_.find(tag)!=gammas_.end()); return gammas_.find(tag)->second;
61   } else {
62     auto first = gsq.back();
63     auto rest = gsq; rest.pop_back();
64     return branch(first)->search(tag, rest);
65   }
66 }
67 
68 
69 template<typename VecType>
exist(const size_t tag,const std::list<GammaSQ> & gsq) const70 bool GammaBranch<VecType>::exist(const size_t tag, const std::list<GammaSQ>& gsq) const {
71   if (gsq.empty())
72     return gammas_.find(tag) != gammas_.end();
73   else {
74     auto first = gsq.back();
75     auto rest = gsq; rest.pop_back();
76     return branch(first) ? branch(first)->exist(tag, rest) : false;
77   }
78 }
79 
80 
81 template<typename VecType>
if_contributes(std::set<int> needed)82 bool GammaBranch<VecType>::if_contributes(std::set<int> needed) {
83   bool contributes = false;
84   for (const int& b : needed) {
85     if (branch(b)) contributes |= branch(b)->active();
86   }
87   if (!contributes) {
88     for (int i = 0; i < 4; ++i) {
89       if (branch(i)) {
90         if (branch(i)->active()) contributes |= branch(i)->if_contributes(needed);
91       }
92     }
93   }
94   return contributes;
95 }
96 
97 
98 template<typename VecType>
is_distterminal()99 bool GammaBranch<VecType>::is_distterminal() {
100   return false;
101 #if 0
102   bool not_terminal = false;
103   if (branch(GammaSQ::CreateAlpha))
104     not_terminal |= branch(GammaSQ::CreateAlpha)->active();
105   if (branch(GammaSQ::AnnihilateAlpha))
106     not_terminal |= branch(GammaSQ::AnnihilateAlpha)->active();
107   if (!not_terminal) {
108     if (branch(GammaSQ::CreateBeta) && branch(GammaSQ::CreateBeta)->active())
109       not_terminal |= branch(GammaSQ::CreateBeta)->is_distterminal();
110     if (branch(GammaSQ::AnnihilateBeta) && branch(GammaSQ::AnnihilateBeta)->active())
111       not_terminal |= branch(GammaSQ::AnnihilateBeta)->is_distterminal();
112   }
113   return !not_terminal;
114 #endif
115 }
116 
117 template<typename VecType>
GammaTree(shared_ptr<const VecType> ket)118 GammaTree<VecType>::GammaTree(shared_ptr<const VecType> ket) : ket_(ket) {
119   base_ = make_shared<GammaBranch<VecType>>();
120   constexpr int nops = 4;
121 
122   for (int i = 0; i < nops; ++i) {
123     base_->branch(i) = make_shared<GammaBranch<VecType>>();
124     for (int j = 0; j < nops; ++j) {
125       base_->branch(i)->branch(j) = make_shared<GammaBranch<VecType>>();
126       for (int k = 0; k < nops; ++k)
127         base_->branch(i)->branch(j)->branch(k) = make_shared<GammaBranch<VecType>>();
128     }
129   }
130 }
131 
132 
133 template class bagel::GammaBranch<CASDvec>;
134 template class bagel::GammaBranch<RASDvec>;
135 template class bagel::GammaTree<CASDvec>;
136 template class bagel::GammaTree<RASDvec>;
137