| 1 | 
 | 
|---|
| 2 | #include <stddef.h>
 | 
|---|
| 3 | #include <util/misc/autovec.h>
 | 
|---|
| 4 | #include <util/misc/scexception.h>
 | 
|---|
| 5 | #include <chemistry/qc/wfn/obwfn.h>
 | 
|---|
| 6 | #include <chemistry/qc/scf/clhf.h>
 | 
|---|
| 7 | 
 | 
|---|
| 8 | using namespace std;
 | 
|---|
| 9 | using namespace sc;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | class MP2: public Wavefunction {
 | 
|---|
| 12 |     Ref<OneBodyWavefunction> ref_mp2_wfn_;
 | 
|---|
| 13 |     double compute_mp2_energy();
 | 
|---|
| 14 | 
 | 
|---|
| 15 |   public: 
 | 
|---|
| 16 |     MP2(const Ref<KeyVal> &);
 | 
|---|
| 17 |     MP2(StateIn &);
 | 
|---|
| 18 |     void save_data_state(StateOut &);
 | 
|---|
| 19 |     void compute(void);
 | 
|---|
| 20 |     void obsolete(void);
 | 
|---|
| 21 |     int nelectron(void);
 | 
|---|
| 22 |     RefSymmSCMatrix density(void);
 | 
|---|
| 23 |     int spin_polarized(void);
 | 
|---|
| 24 |     int value_implemented(void) const;
 | 
|---|
| 25 | };
 | 
|---|
| 26 | 
 | 
|---|
| 27 | static ClassDesc MP2_cd(typeid(MP2), "MP2", 1, "public Wavefunction",
 | 
|---|
| 28 |                         0, create<MP2>, create<MP2>);
 | 
|---|
| 29 | 
 | 
|---|
| 30 | MP2::MP2(const Ref<KeyVal> &keyval):Wavefunction(keyval) {
 | 
|---|
| 31 |   ref_mp2_wfn_ << keyval->describedclassvalue("reference");
 | 
|---|
| 32 |   if(ref_mp2_wfn_.null()) {
 | 
|---|
| 33 |       throw InputError("require a OneBodyWavefunction object",
 | 
|---|
| 34 |                        __FILE__, __LINE__, "reference", 0,
 | 
|---|
| 35 |                        class_desc());
 | 
|---|
| 36 |   }
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | MP2::MP2(StateIn &statein):Wavefunction(statein)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   ref_mp2_wfn_ << SavableState::restore_state(statein);
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | void
 | 
|---|
| 45 | MP2::save_data_state(StateOut &stateout) {
 | 
|---|
| 46 |   Wavefunction::save_data_state(stateout);
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   SavableState::save_state(ref_mp2_wfn_.pointer(),stateout);
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | void
 | 
|---|
| 52 | MP2::compute(void)
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   if(gradient_needed()) {
 | 
|---|
| 55 |       throw FeatureNotImplemented("no gradients yet",
 | 
|---|
| 56 |                                   __FILE__, __LINE__, class_desc());
 | 
|---|
| 57 |   }
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   double extra_hf_acc = 10.;
 | 
|---|
| 60 |   ref_mp2_wfn_->set_desired_value_accuracy(desired_value_accuracy()
 | 
|---|
| 61 |                                            / extra_hf_acc);
 | 
|---|
| 62 |   double refenergy = ref_mp2_wfn_->energy();
 | 
|---|
| 63 |   double mp2energy = compute_mp2_energy();
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   ExEnv::out0() << indent << "MP2 Energy = " << mp2energy << endl;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   set_value(refenergy + mp2energy);
 | 
|---|
| 68 |   set_actual_value_accuracy(ref_mp2_wfn_->actual_value_accuracy()
 | 
|---|
| 69 |                             * extra_hf_acc);
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void
 | 
|---|
| 73 | MP2::obsolete(void) {
 | 
|---|
| 74 |   Wavefunction::obsolete();
 | 
|---|
| 75 |   ref_mp2_wfn_->obsolete();
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | int
 | 
|---|
| 79 | MP2::nelectron(void) {
 | 
|---|
| 80 |   return ref_mp2_wfn_->nelectron();
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | RefSymmSCMatrix
 | 
|---|
| 84 | MP2::density(void) {
 | 
|---|
| 85 |   throw FeatureNotImplemented("no density yet",
 | 
|---|
| 86 |                               __FILE__, __LINE__, class_desc());
 | 
|---|
| 87 |   return 0;
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | int
 | 
|---|
| 91 | MP2::spin_polarized(void) {
 | 
|---|
| 92 |   return 0;
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | int
 | 
|---|
| 96 | MP2::value_implemented(void) const {
 | 
|---|
| 97 |   return 1;
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | double
 | 
|---|
| 101 | MP2::compute_mp2_energy()
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   if(molecule()->point_group()->char_table().order() != 1) {
 | 
|---|
| 104 |       throw FeatureNotImplemented("C1 symmetry only",
 | 
|---|
| 105 |                                   __FILE__, __LINE__, class_desc());
 | 
|---|
| 106 |   }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   RefSCMatrix vec = ref_mp2_wfn_->eigenvectors();
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   int nao = vec.nrow();
 | 
|---|
| 111 |   int nmo = vec.ncol();
 | 
|---|
| 112 |   int nocc = ref_mp2_wfn_->nelectron()/2;
 | 
|---|
| 113 |   int nvir = nmo - nocc;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   auto_vec<double> cvec_av(new double [vec.nrow() * vec.ncol()]);
 | 
|---|
| 116 |   double *cvec = cvec_av.get();
 | 
|---|
| 117 |   vec->convert(cvec);
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   auto_vec<double> pqrs_av(new double [nao * nao * nao * nao]);
 | 
|---|
| 120 |   double *pqrs = pqrs_av.get();
 | 
|---|
| 121 |   for(int n = 0; n < nao*nao*nao*nao; n++) pqrs[n] = 0.0;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   Ref<TwoBodyInt> twoint = integral()->electron_repulsion();
 | 
|---|
| 124 |   const double *buffer = twoint->buffer();
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   Ref<GaussianBasisSet> basis = this->basis();
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   int nshell = basis->nshell();
 | 
|---|
| 129 |   for(int P = 0; P < nshell; P++) {
 | 
|---|
| 130 |     int nump = basis->shell(P).nfunction();
 | 
|---|
| 131 | 
 | 
|---|
| 132 |     for(int Q = 0; Q < nshell; Q++) {
 | 
|---|
| 133 |       int numq = basis->shell(Q).nfunction();
 | 
|---|
| 134 | 
 | 
|---|
| 135 |       for(int R = 0; R < nshell; R++) {
 | 
|---|
| 136 |         int numr = basis->shell(R).nfunction();
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         for(int S = 0; S < nshell; S++) {
 | 
|---|
| 139 |           int nums = basis->shell(S).nfunction();
 | 
|---|
| 140 | 
 | 
|---|
| 141 |           twoint->compute_shell(P,Q,R,S);
 | 
|---|
| 142 | 
 | 
|---|
| 143 |           int index = 0;
 | 
|---|
| 144 |           for(int p=0; p < nump; p++) {
 | 
|---|
| 145 |             int op = basis->shell_to_function(P)+p;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |             for(int q = 0; q < numq; q++) {
 | 
|---|
| 148 |               int oq = basis->shell_to_function(Q)+q;
 | 
|---|
| 149 | 
 | 
|---|
| 150 |               for(int r = 0; r < numr; r++) {
 | 
|---|
| 151 |                 int oor = basis->shell_to_function(R)+r;
 | 
|---|
| 152 | 
 | 
|---|
| 153 |                 for(int s = 0; s < nums; s++,index++) {
 | 
|---|
| 154 |                   int os = basis->shell_to_function(S)+s;
 | 
|---|
| 155 | 
 | 
|---|
| 156 |                   int ipqrs = (((op*nao+oq)*nao+oor)*nao+os);
 | 
|---|
| 157 | 
 | 
|---|
| 158 |                   pqrs[ipqrs] = buffer[index];
 | 
|---|
| 159 | 
 | 
|---|
| 160 |                 }
 | 
|---|
| 161 |               }
 | 
|---|
| 162 |             }
 | 
|---|
| 163 |           }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         }
 | 
|---|
| 166 |       }
 | 
|---|
| 167 |     }
 | 
|---|
| 168 |   }
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   twoint = 0;
 | 
|---|
| 171 | 
 | 
|---|
| 172 |   auto_vec<double> ijkl_av(new double [nmo * nmo * nmo * nmo]);
 | 
|---|
| 173 |   double *ijkl = ijkl_av.get();
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   int idx = 0;
 | 
|---|
| 176 |   for(int i = 0; i < nmo; i++) {
 | 
|---|
| 177 |     for(int j = 0; j < nmo; j++) {
 | 
|---|
| 178 |       for(int k = 0; k < nmo; k++) {
 | 
|---|
| 179 |         for(int l = 0; l < nmo; l++, idx++) {
 | 
|---|
| 180 | 
 | 
|---|
| 181 |           ijkl[idx] = 0.0;
 | 
|---|
| 182 | 
 | 
|---|
| 183 |           int index = 0;
 | 
|---|
| 184 |           for(int p = 0; p < nao; p++) {
 | 
|---|
| 185 |             for(int q = 0; q < nao; q++) {
 | 
|---|
| 186 |               for(int r = 0; r < nao; r++) {
 | 
|---|
| 187 |                 for(int s = 0; s < nao; s++,index++) {
 | 
|---|
| 188 | 
 | 
|---|
| 189 |                   ijkl[idx] += cvec[p*nmo + i] * cvec[q*nmo +j]
 | 
|---|
| 190 |                     * cvec[r*nmo + k] * cvec[s*nmo + l]
 | 
|---|
| 191 |                     * pqrs[index];
 | 
|---|
| 192 | 
 | 
|---|
| 193 |                 }
 | 
|---|
| 194 |               }
 | 
|---|
| 195 |             }
 | 
|---|
| 196 |           }
 | 
|---|
| 197 | 
 | 
|---|
| 198 |         }
 | 
|---|
| 199 |       }
 | 
|---|
| 200 |     }
 | 
|---|
| 201 |   }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |   pqrs_av.release(); pqrs = 0;
 | 
|---|
| 204 |   cvec_av.release(); cvec = 0;
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   auto_vec<double> evals_av(new double [nmo]);
 | 
|---|
| 207 |   double *evals = evals_av.get();
 | 
|---|
| 208 |   ref_mp2_wfn_->eigenvalues()->convert(evals);
 | 
|---|
| 209 | 
 | 
|---|
| 210 |   double energy = 0.0;
 | 
|---|
| 211 |   for(int i=0; i < nocc; i++) {
 | 
|---|
| 212 |     for(int j=0; j < nocc; j++) {
 | 
|---|
| 213 |       for(int a=nocc; a < nmo; a++) {
 | 
|---|
| 214 |         for(int b=nocc; b < nmo; b++) {
 | 
|---|
| 215 | 
 | 
|---|
| 216 |           int iajb = (((i*nmo+a)*nmo+j)*nmo+b);
 | 
|---|
| 217 |           int ibja = (((i*nmo+b)*nmo+j)*nmo+a);
 | 
|---|
| 218 | 
 | 
|---|
| 219 |           energy += (2 * ijkl[iajb] - ijkl[ibja]) * ijkl[iajb]/
 | 
|---|
| 220 |             (evals[i] + evals[j] - evals[a] - evals[b]);
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         }
 | 
|---|
| 223 |       }
 | 
|---|
| 224 |     }
 | 
|---|
| 225 |   }
 | 
|---|
| 226 | 
 | 
|---|
| 227 |   ijkl_av.release(); ijkl = 0;
 | 
|---|
| 228 |   evals_av.release(); evals = 0;
 | 
|---|
| 229 | 
 | 
|---|
| 230 |   return energy;
 | 
|---|
| 231 | }
 | 
|---|