File: mp2.cc

package info (click to toggle)
mpqc 2.3.1-22
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 43,140 kB
  • sloc: cpp: 258,686; sh: 8,532; perl: 6,017; ansic: 5,499; makefile: 2,497; fortran: 1,970; lisp: 1,269; yacc: 313; lex: 177; csh: 45
file content (231 lines) | stat: -rw-r--r-- 5,332 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include <stddef.h>
#include <util/misc/autovec.h>
#include <util/misc/scexception.h>
#include <chemistry/qc/wfn/obwfn.h>
#include <chemistry/qc/scf/clhf.h>

using namespace std;
using namespace sc;

class MP2: public Wavefunction {
    Ref<OneBodyWavefunction> ref_mp2_wfn_;
    double compute_mp2_energy();

  public: 
    MP2(const Ref<KeyVal> &);
    MP2(StateIn &);
    void save_data_state(StateOut &);
    void compute(void);
    void obsolete(void);
    int nelectron(void);
    RefSymmSCMatrix density(void);
    int spin_polarized(void);
    int value_implemented(void) const;
};

static ClassDesc MP2_cd(typeid(MP2), "MP2", 1, "public Wavefunction",
                        0, create<MP2>, create<MP2>);

MP2::MP2(const Ref<KeyVal> &keyval):Wavefunction(keyval) {
  ref_mp2_wfn_ << keyval->describedclassvalue("reference");
  if(ref_mp2_wfn_.null()) {
      throw InputError("require a OneBodyWavefunction object",
                       __FILE__, __LINE__, "reference", 0,
                       class_desc());
  }
}

MP2::MP2(StateIn &statein):Wavefunction(statein)
{
  ref_mp2_wfn_ << SavableState::restore_state(statein);
}

void
MP2::save_data_state(StateOut &stateout) {
  Wavefunction::save_data_state(stateout);

  SavableState::save_state(ref_mp2_wfn_.pointer(),stateout);
}

void
MP2::compute(void)
{
  if(gradient_needed()) {
      throw FeatureNotImplemented("no gradients yet",
                                  __FILE__, __LINE__, class_desc());
  }

  double extra_hf_acc = 10.;
  ref_mp2_wfn_->set_desired_value_accuracy(desired_value_accuracy()
                                           / extra_hf_acc);
  double refenergy = ref_mp2_wfn_->energy();
  double mp2energy = compute_mp2_energy();

  ExEnv::out0() << indent << "MP2 Energy = " << mp2energy << endl;

  set_value(refenergy + mp2energy);
  set_actual_value_accuracy(ref_mp2_wfn_->actual_value_accuracy()
                            * extra_hf_acc);
}

void
MP2::obsolete(void) {
  Wavefunction::obsolete();
  ref_mp2_wfn_->obsolete();
}

int
MP2::nelectron(void) {
  return ref_mp2_wfn_->nelectron();
}

RefSymmSCMatrix
MP2::density(void) {
  throw FeatureNotImplemented("no density yet",
                              __FILE__, __LINE__, class_desc());
  return 0;
}

int
MP2::spin_polarized(void) {
  return 0;
}

int
MP2::value_implemented(void) const {
  return 1;
}

double
MP2::compute_mp2_energy()
{
  if(molecule()->point_group()->char_table().order() != 1) {
      throw FeatureNotImplemented("C1 symmetry only",
                                  __FILE__, __LINE__, class_desc());
  }

  RefSCMatrix vec = ref_mp2_wfn_->eigenvectors();

  int nao = vec.nrow();
  int nmo = vec.ncol();
  int nocc = ref_mp2_wfn_->nelectron()/2;
  int nvir = nmo - nocc;

  auto_vec<double> cvec_av(new double [vec.nrow() * vec.ncol()]);
  double *cvec = cvec_av.get();
  vec->convert(cvec);

  auto_vec<double> pqrs_av(new double [nao * nao * nao * nao]);
  double *pqrs = pqrs_av.get();
  for(int n = 0; n < nao*nao*nao*nao; n++) pqrs[n] = 0.0;

  Ref<TwoBodyInt> twoint = integral()->electron_repulsion();
  const double *buffer = twoint->buffer();

  Ref<GaussianBasisSet> basis = this->basis();

  int nshell = basis->nshell();
  for(int P = 0; P < nshell; P++) {
    int nump = basis->shell(P).nfunction();

    for(int Q = 0; Q < nshell; Q++) {
      int numq = basis->shell(Q).nfunction();

      for(int R = 0; R < nshell; R++) {
	int numr = basis->shell(R).nfunction();

	for(int S = 0; S < nshell; S++) {
	  int nums = basis->shell(S).nfunction();

	  twoint->compute_shell(P,Q,R,S);

	  int index = 0;
	  for(int p=0; p < nump; p++) {
	    int op = basis->shell_to_function(P)+p;

	    for(int q = 0; q < numq; q++) {
	      int oq = basis->shell_to_function(Q)+q;

	      for(int r = 0; r < numr; r++) {
		int oor = basis->shell_to_function(R)+r;

		for(int s = 0; s < nums; s++,index++) {
		  int os = basis->shell_to_function(S)+s;

		  int ipqrs = (((op*nao+oq)*nao+oor)*nao+os);

		  pqrs[ipqrs] = buffer[index];

		}
	      }
	    }
	  }

	}
      }
    }
  }

  twoint = 0;

  auto_vec<double> ijkl_av(new double [nmo * nmo * nmo * nmo]);
  double *ijkl = ijkl_av.get();

  int idx = 0;
  for(int i = 0; i < nmo; i++) {
    for(int j = 0; j < nmo; j++) {
      for(int k = 0; k < nmo; k++) {
	for(int l = 0; l < nmo; l++, idx++) {

	  ijkl[idx] = 0.0;

	  int index = 0;
	  for(int p = 0; p < nao; p++) {
	    for(int q = 0; q < nao; q++) {
	      for(int r = 0; r < nao; r++) {
		for(int s = 0; s < nao; s++,index++) {

		  ijkl[idx] += cvec[p*nmo + i] * cvec[q*nmo +j]
		    * cvec[r*nmo + k] * cvec[s*nmo + l]
		    * pqrs[index];

		}
	      }
	    }
	  }

	}
      }
    }
  }

  pqrs_av.release(); pqrs = 0;
  cvec_av.release(); cvec = 0;

  auto_vec<double> evals_av(new double [nmo]);
  double *evals = evals_av.get();
  ref_mp2_wfn_->eigenvalues()->convert(evals);

  double energy = 0.0;
  for(int i=0; i < nocc; i++) {
    for(int j=0; j < nocc; j++) {
      for(int a=nocc; a < nmo; a++) {
	for(int b=nocc; b < nmo; b++) {

	  int iajb = (((i*nmo+a)*nmo+j)*nmo+b);
	  int ibja = (((i*nmo+b)*nmo+j)*nmo+a);

	  energy += (2 * ijkl[iajb] - ijkl[ibja]) * ijkl[iajb]/
	    (evals[i] + evals[j] - evals[a] - evals[b]);

	}
      }
    }
  }

  ijkl_av.release(); ijkl = 0;
  evals_av.release(); evals = 0;

  return energy;
}