source: ThirdParty/mpqc_open/doc/devsamp/mp2.cc@ 788dce

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChangeBugEmailaddress ChemicalSpaceEvaluator Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_oldresults ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps
Last change on this file since 788dce was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 5.2 KB
Line 
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
8using namespace std;
9using namespace sc;
10
11class 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
27static ClassDesc MP2_cd(typeid(MP2), "MP2", 1, "public Wavefunction",
28 0, create<MP2>, create<MP2>);
29
30MP2::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
39MP2::MP2(StateIn &statein):Wavefunction(statein)
40{
41 ref_mp2_wfn_ << SavableState::restore_state(statein);
42}
43
44void
45MP2::save_data_state(StateOut &stateout) {
46 Wavefunction::save_data_state(stateout);
47
48 SavableState::save_state(ref_mp2_wfn_.pointer(),stateout);
49}
50
51void
52MP2::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
72void
73MP2::obsolete(void) {
74 Wavefunction::obsolete();
75 ref_mp2_wfn_->obsolete();
76}
77
78int
79MP2::nelectron(void) {
80 return ref_mp2_wfn_->nelectron();
81}
82
83RefSymmSCMatrix
84MP2::density(void) {
85 throw FeatureNotImplemented("no density yet",
86 __FILE__, __LINE__, class_desc());
87 return 0;
88}
89
90int
91MP2::spin_polarized(void) {
92 return 0;
93}
94
95int
96MP2::value_implemented(void) const {
97 return 1;
98}
99
100double
101MP2::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}
Note: See TracBrowser for help on using the repository browser.