source: ThirdParty/mpqc_open/src/lib/chemistry/qc/dft/uks.cc

Candidate_v1.6.1
Last change on this file 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: 11.9 KB
Line 
1//
2// uks.cc --- implementation of the unrestricted Hartree-Fock class
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Edward Seidl <seidl@janed.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit 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 Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <math.h>
33
34#include <util/misc/timer.h>
35#include <util/misc/formio.h>
36#include <util/state/stateio.h>
37
38#include <math/optimize/scextrapmat.h>
39
40#include <chemistry/qc/basis/petite.h>
41
42#include <chemistry/qc/dft/uks.h>
43#include <chemistry/qc/scf/lgbuild.h>
44#include <chemistry/qc/scf/ltbgrad.h>
45
46#include <chemistry/qc/dft/ukstmpl.h>
47
48using namespace std;
49using namespace sc;
50
51///////////////////////////////////////////////////////////////////////////
52// UKS
53
54static ClassDesc UKS_cd(
55 typeid(UKS),"UKS",1,"public UnrestrictedSCF",
56 0, create<UKS>, create<UKS>);
57
58UKS::UKS(StateIn& s) :
59 SavableState(s),
60 UnrestrictedSCF(s)
61{
62 exc_=0;
63 integrator_ << SavableState::restore_state(s);
64 functional_ << SavableState::restore_state(s);
65 vaxc_ = basis_matrixkit()->symmmatrix(so_dimension());
66 vaxc_.restore(s);
67 vbxc_ = basis_matrixkit()->symmmatrix(so_dimension());
68 vbxc_.restore(s);
69}
70
71UKS::UKS(const Ref<KeyVal>& keyval) :
72 UnrestrictedSCF(keyval)
73{
74 exc_=0;
75 integrator_ << keyval->describedclassvalue("integrator");
76 if (integrator_.null()) integrator_ = new RadialAngularIntegrator();
77
78 functional_ << keyval->describedclassvalue("functional");
79 if (functional_.null()) {
80 ExEnv::outn() << "ERROR: " << class_name() << ": no \"functional\" given" << endl;
81 abort();
82 }
83}
84
85UKS::~UKS()
86{
87}
88
89void
90UKS::save_data_state(StateOut& s)
91{
92 UnrestrictedSCF::save_data_state(s);
93 SavableState::save_state(integrator_.pointer(),s);
94 SavableState::save_state(functional_.pointer(),s);
95 vaxc_.save(s);
96 vbxc_.save(s);
97}
98
99int
100UKS::value_implemented() const
101{
102 return 1;
103}
104
105int
106UKS::gradient_implemented() const
107{
108 return 1;
109}
110
111double
112UKS::scf_energy()
113{
114 RefSymmSCMatrix mva = vaxc_.copy();
115 mva.scale(-1.0);
116 focka_.result_noupdate().accumulate(mva);
117 RefSymmSCMatrix mvb = vbxc_.copy();
118 mvb.scale(-1.0);
119 fockb_.result_noupdate().accumulate(mvb);
120 double ehf = UnrestrictedSCF::scf_energy();
121 focka_.result_noupdate().accumulate(vaxc_);
122 fockb_.result_noupdate().accumulate(vbxc_);
123 return ehf + exc_;
124}
125
126Ref<SCExtrapData>
127UKS::extrap_data()
128{
129 RefSymmSCMatrix *m = new RefSymmSCMatrix[4];
130 m[0] = focka_.result_noupdate();
131 m[1] = fockb_.result_noupdate();
132 m[2] = vaxc_;
133 m[3] = vbxc_;
134
135 Ref<SCExtrapData> data = new SymmSCMatrixNSCExtrapData(4, m);
136 delete[] m;
137
138 return data;
139}
140
141void
142UKS::print(ostream&o) const
143{
144 o << indent << "Unrestricted Kohn-Sham (UKS) Parameters:" << endl;
145 o << incindent;
146 UnrestrictedSCF::print(o);
147 o << indent << "Functional:" << endl;
148 o << incindent;
149 functional_->print(o);
150 o << decindent;
151 o << indent << "Integrator:" << endl;
152 o << incindent;
153 integrator_->print(o);
154 o << decindent;
155 o << decindent;
156}
157
158//////////////////////////////////////////////////////////////////////////////
159
160void
161UKS::two_body_energy(double &ec, double &ex)
162{
163 tim_enter("uks e2");
164 ec = 0.0;
165 ex = 0.0;
166 if (local_ || local_dens_) {
167 // grab the data pointers from the G and P matrices
168 double *apmat;
169 double *bpmat;
170 tim_enter("local data");
171 RefSymmSCMatrix adens = alpha_ao_density();
172 RefSymmSCMatrix bdens = beta_ao_density();
173 adens->scale(2.0);
174 adens->scale_diagonal(0.5);
175 bdens->scale(2.0);
176 bdens->scale_diagonal(0.5);
177 RefSymmSCMatrix aptmp = get_local_data(adens, apmat, SCF::Read);
178 RefSymmSCMatrix bptmp = get_local_data(bdens, bpmat, SCF::Read);
179 tim_exit("local data");
180
181 // initialize the two electron integral classes
182 Ref<TwoBodyInt> tbi = integral()->electron_repulsion();
183 tbi->set_integral_storage(0);
184
185 signed char * pmax = init_pmax(apmat);
186
187 LocalUKSEnergyContribution lclc(apmat, bpmat, 0);
188 Ref<PetiteList> pl = integral()->petite_list();
189 LocalGBuild<LocalUKSEnergyContribution>
190 gb(lclc, tbi_, pl, basis(), scf_grp_, pmax, desired_value_accuracy()/100.0);
191 gb.run();
192
193 delete[] pmax;
194
195 ec = lclc.ec;
196 ex = lclc.ex;
197 }
198 else {
199 ExEnv::out0() << indent << "Cannot yet use anything but Local matrices\n";
200 abort();
201 }
202 tim_exit("uks e2");
203}
204
205//////////////////////////////////////////////////////////////////////////////
206
207void
208UKS::ao_fock(double accuracy)
209{
210 Ref<PetiteList> pl = integral()->petite_list(basis());
211
212 // calculate G. First transform diff_densa_ to the AO basis, then
213 // scale the off-diagonal elements by 2.0
214 RefSymmSCMatrix dda = diff_densa_;
215 diff_densa_ = pl->to_AO_basis(dda);
216 diff_densa_->scale(2.0);
217 diff_densa_->scale_diagonal(0.5);
218
219 RefSymmSCMatrix ddb = diff_densb_;
220 diff_densb_ = pl->to_AO_basis(ddb);
221 diff_densb_->scale(2.0);
222 diff_densb_->scale_diagonal(0.5);
223
224 // now try to figure out the matrix specialization we're dealing with
225 // if we're using Local matrices, then there's just one subblock, or
226 // see if we can convert G and P to local matrices
227 if (local_ || local_dens_) {
228 double *gmat, *gmato, *pmat, *pmato;
229
230 // grab the data pointers from the G and P matrices
231 RefSymmSCMatrix gtmp = get_local_data(gmata_, gmat, SCF::Accum);
232 RefSymmSCMatrix ptmp = get_local_data(diff_densa_, pmat, SCF::Read);
233 RefSymmSCMatrix gotmp = get_local_data(gmatb_, gmato, SCF::Accum);
234 RefSymmSCMatrix potmp = get_local_data(diff_densb_, pmato, SCF::Read);
235
236 signed char * pmax = init_pmax(pmat);
237
238// LocalUKSContribution lclc(gmat, pmat, gmato, pmato, functional_->a0());
239// LocalGBuild<LocalUKSContribution>
240// gb(lclc, tbi_, pl, basis(), scf_grp_, pmax, desired_value_accuracy()/100.0);
241// gb.run();
242 int i;
243 int nthread = threadgrp_->nthread();
244 LocalGBuild<LocalUKSContribution> **gblds =
245 new LocalGBuild<LocalUKSContribution>*[nthread];
246 LocalUKSContribution **conts = new LocalUKSContribution*[nthread];
247
248 double **gmats = new double*[nthread];
249 gmats[0] = gmat;
250 double **gmatos = new double*[nthread];
251 gmatos[0] = gmato;
252
253 Ref<GaussianBasisSet> bs = basis();
254 int ntri = i_offset(bs->nbasis());
255
256 double gmat_accuracy = accuracy;
257 if (min_orthog_res() < 1.0) { gmat_accuracy *= min_orthog_res(); }
258
259 for (i=0; i < nthread; i++) {
260 if (i) {
261 gmats[i] = new double[ntri];
262 memset(gmats[i], 0, sizeof(double)*ntri);
263 gmatos[i] = new double[ntri];
264 memset(gmatos[i], 0, sizeof(double)*ntri);
265 }
266 conts[i] = new LocalUKSContribution(gmats[i], pmat, gmatos[i], pmato,
267 functional_->a0());
268 gblds[i] = new LocalGBuild<LocalUKSContribution>(*conts[i], tbis_[i],
269 pl, bs, scf_grp_, pmax, gmat_accuracy, nthread, i
270 );
271
272 threadgrp_->add_thread(i, gblds[i]);
273 }
274
275 tim_enter("start thread");
276 if (threadgrp_->start_threads() < 0) {
277 ExEnv::err0() << indent
278 << "UKS: error starting threads" << endl;
279 abort();
280 }
281 tim_exit("start thread");
282
283 tim_enter("stop thread");
284 if (threadgrp_->wait_threads() < 0) {
285 ExEnv::err0() << indent
286 << "UKS: error waiting for threads" << endl;
287 abort();
288 }
289 tim_exit("stop thread");
290
291 double tnint=0;
292 for (i=0; i < nthread; i++) {
293 tnint += gblds[i]->tnint;
294
295 if (i) {
296 for (int j=0; j < ntri; j++) {
297 gmat[j] += gmats[i][j];
298 gmato[j] += gmatos[i][j];
299 }
300 delete[] gmats[i];
301 delete[] gmatos[i];
302 }
303
304 delete gblds[i];
305 delete conts[i];
306 }
307
308 delete[] gmats;
309 delete[] gmatos;
310 delete[] gblds;
311 delete[] conts;
312
313 delete[] pmax;
314
315 scf_grp_->sum(&tnint, 1, 0, 0);
316 ExEnv::out0() << indent << scprintf("%20.0f integrals\n", tnint);
317
318 // if we're running on multiple processors, then sum the G matrices
319 if (scf_grp_->n() > 1) {
320 scf_grp_->sum(gmat, i_offset(basis()->nbasis()));
321 scf_grp_->sum(gmato, i_offset(basis()->nbasis()));
322 }
323
324 // if we're running on multiple processors, or we don't have local
325 // matrices, then accumulate gtmp back into G
326 if (!local_ || scf_grp_->n() > 1) {
327 gmata_->convert_accumulate(gtmp);
328 gmatb_->convert_accumulate(gotmp);
329 }
330 }
331
332 // for now quit
333 else {
334 ExEnv::out0() << indent << "Cannot yet use anything but Local matrices\n";
335 abort();
336 }
337
338 diff_densa_ = pl->to_AO_basis(densa_);
339 diff_densb_ = pl->to_AO_basis(densb_);
340 integrator_->set_compute_potential_integrals(1);
341 integrator_->set_accuracy(accuracy);
342 integrator_->integrate(functional_, diff_densa_, diff_densb_);
343 exc_ = integrator_->value();
344 RefSymmSCMatrix vxa = gmata_.clone();
345 RefSymmSCMatrix vxb = gmatb_.clone();
346 vxa->assign((double*)integrator_->alpha_vmat());
347 vxb->assign((double*)integrator_->beta_vmat());
348 vxa = pl->to_SO_basis(vxa);
349 vxb = pl->to_SO_basis(vxb);
350 vaxc_ = vxa;
351 vbxc_ = vxb;
352
353 // get rid of AO delta P
354 diff_densa_ = dda;
355 dda = diff_densa_.clone();
356
357 diff_densb_ = ddb;
358 ddb = diff_densb_.clone();
359
360 // now symmetrize the skeleton G matrix, placing the result in dda
361 RefSymmSCMatrix skel_gmat = gmata_.copy();
362 skel_gmat.scale(1.0/(double)pl->order());
363 pl->symmetrize(skel_gmat,dda);
364
365 skel_gmat = gmatb_.copy();
366 skel_gmat.scale(1.0/(double)pl->order());
367 pl->symmetrize(skel_gmat,ddb);
368
369 // Fa = H+Ga
370 focka_.result_noupdate().assign(hcore_);
371 focka_.result_noupdate().accumulate(dda);
372 focka_.result_noupdate().accumulate(vaxc_);
373
374 // Fb = H+Gb
375 fockb_.result_noupdate().assign(hcore_);
376 fockb_.result_noupdate().accumulate(ddb);
377 fockb_.result_noupdate().accumulate(vbxc_);
378
379 dda.assign(0.0);
380 accumddh_->accum(dda);
381 focka_.result_noupdate().accumulate(dda);
382 fockb_.result_noupdate().accumulate(dda);
383
384 focka_.computed()=1;
385 fockb_.computed()=1;
386}
387
388/////////////////////////////////////////////////////////////////////////////
389
390void
391UKS::two_body_deriv(double * tbgrad)
392{
393 tim_enter("grad");
394
395 int natom3 = 3*molecule()->natom();
396
397 tim_enter("two-body");
398 double *hfgrad = new double[natom3];
399 memset(hfgrad,0,sizeof(double)*natom3);
400 two_body_deriv_hf(hfgrad,functional_->a0());
401 //print_natom_3(hfgrad, "Two-body contribution to DFT gradient");
402 tim_exit("two-body");
403
404 double *dftgrad = new double[natom3];
405 memset(dftgrad,0,sizeof(double)*natom3);
406 RefSymmSCMatrix ao_dens_a = alpha_ao_density();
407 RefSymmSCMatrix ao_dens_b = beta_ao_density();
408 integrator_->init(this);
409 integrator_->set_compute_potential_integrals(0);
410 integrator_->set_accuracy(desired_gradient_accuracy());
411 integrator_->integrate(functional_, ao_dens_a, ao_dens_b, dftgrad);
412 integrator_->done();
413 //print_natom_3(dftgrad, "E-X contribution to DFT gradient");
414
415 scf_grp_->sum(dftgrad, natom3);
416
417 for (int i=0; i<natom3; i++) tbgrad[i] += dftgrad[i] + hfgrad[i];
418 delete[] dftgrad;
419 delete[] hfgrad;
420
421 tim_exit("grad");
422}
423
424/////////////////////////////////////////////////////////////////////////////
425
426void
427UKS::init_vector()
428{
429 integrator_->init(this);
430 UnrestrictedSCF::init_vector();
431}
432
433void
434UKS::done_vector()
435{
436 integrator_->done();
437 UnrestrictedSCF::done_vector();
438}
439
440/////////////////////////////////////////////////////////////////////////////
441
442// Local Variables:
443// mode: c++
444// c-file-style: "ETS"
445// End:
Note: See TracBrowser for help on using the repository browser.