1 //
2 // BAGEL - Brilliantly Advanced General Electronic Structure Library
3 // Filename: casscf.h
4 // Copyright (C) 2011 Toru Shiozaki
5 //
6 // Author: Toru Shiozaki <shiozaki@northwestern.edu>
7 // Maintainer: Shiozaki group
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 // This is a base class for various CASSCF solvers.
26 // The assumpation is made that the CI and orbital optimizations are done independently.
27 // This should be a good strategy in large systems.
28 //
29 
30 #ifndef __BAGEL_CASSCF_CASSCF_H
31 #define __BAGEL_CASSCF_CASSCF_H
32 
33 #include <src/wfn/reference.h>
34 #include <src/util/muffle.h>
35 #include <src/ci/fci/distfci.h>
36 #include <src/ci/fci/knowles.h>
37 #include <src/ci/fci/harrison.h>
38 #include <src/multi/casscf/rotfile.h>
39 
40 namespace bagel {
41 
42 enum FCIAlgorithmType { knowles, harrison, dist };
43 
44 class FCI_algorithms {
45   protected:
46     FCIAlgorithmType type_;
47 
48   public:
FCI_algorithms()49     FCI_algorithms() : type_(FCIAlgorithmType::knowles) { }
FCI_algorithms(std::string input_algorithm)50     FCI_algorithms(std::string input_algorithm) {
51       if (input_algorithm == "knowles" || input_algorithm == "kh" || input_algorithm == "handy") {
52         type_ = FCIAlgorithmType::knowles;
53       } else if (input_algorithm == "harrison" || input_algorithm == "zarrabian" || input_algorithm == "hz") {
54         type_ = FCIAlgorithmType::harrison;
55 #ifdef HAVE_MPI_H
56       } else if (input_algorithm == "parallel" || input_algorithm == "dist") {
57         type_ = FCIAlgorithmType::dist;
58 #endif
59       } else {
60         throw std::runtime_error("Unknown FCI algorithm specified. " + input_algorithm);
61       }
62     }
63 
is_knowles()64     bool is_knowles() const { return type_ == FCIAlgorithmType::knowles; }
is_harrison()65     bool is_harrison() const { return type_ == FCIAlgorithmType::harrison; }
is_dist()66     bool is_dist() const { return type_ == FCIAlgorithmType::dist; }
67 };
68 
69 
70 class CASSCF : public Method, public std::enable_shared_from_this<CASSCF> {
71 
72   protected:
73     // some internal information
74     int nocc_; // sum of nact_ + nclosed_
75     int nclosed_;
76     int nact_;
77     int nvirt_;
78     int nmo_;
79     int nstate_;
80     int max_iter_;
81     int max_micro_iter_;
82     double thresh_;
83     double thresh_micro_;
84     double thresh_overlap_;
85     bool conv_ignore_;
86     bool restart_cas_;
87     bool natocc_;
88     bool canonical_;
89 
90     std::shared_ptr<const Coeff> coeff_;
91 
92     // RDMs are given externally (e.g., FCIQMC)
93     std::string external_rdm_;
94     std::shared_ptr<FCI_algorithms> fci_algorithm_;
95     std::shared_ptr<FCI_base> fci_;
96     void print_header() const;
97     void print_iteration(const int iter, const std::vector<double>& energy, const double error, const double time) const;
98     void common_init();
99 
100     void mute_stdcout();
101     void resume_stdcout();
102 
103     const std::shared_ptr<const Matrix> hcore_;
104 
105     std::tuple<std::shared_ptr<const Coeff>,VectorB,VectorB> semi_canonical_orb() const;
106     std::shared_ptr<const Matrix> spin_density() const;
107 
108     // orbital eigenvalues and occupations
109     VectorB eig_;
110     VectorB occup_;
111 
112     // energy
113     std::vector<double> energy_;
114     double rms_grad_;
115 
116     // properties
117     bool do_hyperfine_;
118 
119     // mask some of the output
120     mutable std::shared_ptr<Muffle> muffle_;
121 
122   public:
123     CASSCF(const std::shared_ptr<const PTree> idat, const std::shared_ptr<const Geometry> geom, const std::shared_ptr<const Reference> = nullptr);
124     virtual ~CASSCF();
125 
126     virtual void compute() override = 0;
127 
ref()128     std::shared_ptr<const Reference> ref() const { return ref_; }
129     virtual std::shared_ptr<const Reference> conv_to_ref() const override;
130 
fci()131     std::shared_ptr<FCI_base> fci() { return fci_; }
132 
133     // functions to retrieve protected members
nocc()134     int nocc() const { return nocc_; }
nclosed()135     int nclosed() const { return nclosed_; }
nact()136     int nact() const { return nact_; }
nvirt()137     int nvirt() const { return nvirt_; }
nmo()138     int nmo() const { return nmo_; }
nstate()139     int nstate() const { return nstate_; }
max_iter()140     int max_iter() const { return max_iter_; }
max_micro_iter()141     int max_micro_iter() const { return max_micro_iter_; }
thresh()142     double thresh() const { return thresh_; }
thresh_micro()143     double thresh_micro() const { return thresh_micro_; }
thresh_overlap()144     double thresh_overlap() const { return thresh_overlap_; }
145 
energy(const int i)146     double energy(const int i) const { return energy_[i]; }
energy_av()147     double energy_av() const { return blas::average(energy_); }
energy()148     const std::vector<double>& energy() const { return energy_; }
rms_grad()149     double rms_grad() const { return rms_grad_; }
150 
151     std::shared_ptr<Matrix> compute_active_fock(const MatView acoeff, std::shared_ptr<const RDM<1>> rdm1) const;
152 
153     std::shared_ptr<Matrix> ao_rdm1(std::shared_ptr<const RDM<1>> rdm1, const bool inactive_only = false) const;
hcore()154     std::shared_ptr<const Matrix> hcore() const { return hcore_; }
155 
coeff()156     std::shared_ptr<const Coeff> coeff() const { return coeff_; }
157 };
158 
159 }
160 
161 #endif
162