[0b990d] | 1 | //
|
---|
| 2 | // clks.cc --- implementation of the closed shell Kohn-Sham SCF class
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1997 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/clks.h>
|
---|
| 43 | #include <chemistry/qc/scf/lgbuild.h>
|
---|
| 44 | #include <chemistry/qc/scf/ltbgrad.h>
|
---|
| 45 |
|
---|
| 46 | #include <chemistry/qc/dft/clkstmpl.h>
|
---|
| 47 |
|
---|
| 48 | using namespace std;
|
---|
| 49 | using namespace sc;
|
---|
| 50 |
|
---|
| 51 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 52 | // CLKS
|
---|
| 53 |
|
---|
| 54 | static ClassDesc CLKS_cd(
|
---|
| 55 | typeid(CLKS),"CLKS",1,"public CLSCF",
|
---|
| 56 | 0, create<CLKS>, create<CLKS>);
|
---|
| 57 |
|
---|
| 58 | CLKS::CLKS(StateIn& s) :
|
---|
| 59 | SavableState(s),
|
---|
| 60 | CLSCF(s)
|
---|
| 61 | {
|
---|
| 62 | exc_=0;
|
---|
| 63 | integrator_ << SavableState::restore_state(s);
|
---|
| 64 | functional_ << SavableState::restore_state(s);
|
---|
| 65 | vxc_ = basis_matrixkit()->symmmatrix(so_dimension());
|
---|
| 66 | vxc_.restore(s);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | CLKS::CLKS(const Ref<KeyVal>& keyval) :
|
---|
| 70 | CLSCF(keyval)
|
---|
| 71 | {
|
---|
| 72 | exc_=0;
|
---|
| 73 | integrator_ << keyval->describedclassvalue("integrator");
|
---|
| 74 | if (integrator_.null()) integrator_ = new RadialAngularIntegrator();
|
---|
| 75 |
|
---|
| 76 | functional_ << keyval->describedclassvalue("functional");
|
---|
| 77 | if (functional_.null()) {
|
---|
| 78 | ExEnv::outn() << "ERROR: " << class_name() << ": no \"functional\" given" << endl;
|
---|
| 79 | abort();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | CLKS::~CLKS()
|
---|
| 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void
|
---|
| 88 | CLKS::save_data_state(StateOut& s)
|
---|
| 89 | {
|
---|
| 90 | CLSCF::save_data_state(s);
|
---|
| 91 | SavableState::save_state(integrator_.pointer(),s);
|
---|
| 92 | SavableState::save_state(functional_.pointer(),s);
|
---|
| 93 | vxc_.save(s);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | int
|
---|
| 97 | CLKS::value_implemented() const
|
---|
| 98 | {
|
---|
| 99 | return 1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | int
|
---|
| 103 | CLKS::gradient_implemented() const
|
---|
| 104 | {
|
---|
| 105 | return 1;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void
|
---|
| 109 | CLKS::print(ostream&o) const
|
---|
| 110 | {
|
---|
| 111 | o << indent << "Closed Shell Kohn-Sham (CLKS) Parameters:" << endl;
|
---|
| 112 | o << incindent;
|
---|
| 113 | CLSCF::print(o);
|
---|
| 114 | o << indent << "Functional:" << endl;
|
---|
| 115 | o << incindent;
|
---|
| 116 | functional_->print(o);
|
---|
| 117 | o << decindent;
|
---|
| 118 | o << indent << "Integrator:" << endl;
|
---|
| 119 | o << incindent;
|
---|
| 120 | integrator_->print(o);
|
---|
| 121 | o << decindent;
|
---|
| 122 | o << decindent;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | RefSymmSCMatrix
|
---|
| 126 | CLKS::density()
|
---|
| 127 | {
|
---|
| 128 | RefSymmSCMatrix dens(so_dimension(), basis_matrixkit());
|
---|
| 129 | so_density(dens, 2.0);
|
---|
| 130 | dens.scale(2.0);
|
---|
| 131 | return dens;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | double
|
---|
| 135 | CLKS::scf_energy()
|
---|
| 136 | {
|
---|
| 137 | double ehf = CLSCF::scf_energy();
|
---|
| 138 | return ehf+exc_;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | RefSymmSCMatrix
|
---|
| 142 | CLKS::effective_fock()
|
---|
| 143 | {
|
---|
| 144 | RefSymmSCMatrix fa = fock(0) + vxc_;
|
---|
| 145 |
|
---|
| 146 | RefSymmSCMatrix mofock(oso_dimension(), basis_matrixkit());
|
---|
| 147 | mofock.assign(0.0);
|
---|
| 148 |
|
---|
| 149 | // use eigenvectors if scf_vector_ is null
|
---|
| 150 | if (oso_scf_vector_.null())
|
---|
| 151 | mofock.accumulate_transform(eigenvectors(), fa,
|
---|
| 152 | SCMatrix::TransposeTransform);
|
---|
| 153 | else
|
---|
| 154 | mofock.accumulate_transform(so_to_orthog_so().t() * oso_scf_vector_, fa,
|
---|
| 155 | SCMatrix::TransposeTransform);
|
---|
| 156 |
|
---|
| 157 | return mofock;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | Ref<SCExtrapData>
|
---|
| 161 | CLKS::extrap_data()
|
---|
| 162 | {
|
---|
| 163 | Ref<SCExtrapData> data =
|
---|
| 164 | new SymmSCMatrix2SCExtrapData(cl_fock_.result_noupdate(), vxc_);
|
---|
| 165 | return data;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 169 |
|
---|
| 170 | void
|
---|
| 171 | CLKS::ao_fock(double accuracy)
|
---|
| 172 | {
|
---|
| 173 | Ref<PetiteList> pl = integral()->petite_list(basis());
|
---|
| 174 |
|
---|
| 175 | // calculate G. First transform cl_dens_diff_ to the AO basis, then
|
---|
| 176 | // scale the off-diagonal elements by 2.0
|
---|
| 177 | tim_enter("setup");
|
---|
| 178 | RefSymmSCMatrix dd = cl_dens_diff_;
|
---|
| 179 | cl_dens_diff_ = pl->to_AO_basis(dd);
|
---|
| 180 | cl_dens_diff_->scale(2.0);
|
---|
| 181 | cl_dens_diff_->scale_diagonal(0.5);
|
---|
| 182 | tim_exit("setup");
|
---|
| 183 |
|
---|
| 184 | // now try to figure out the matrix specialization we're dealing with
|
---|
| 185 | // if we're using Local matrices, then there's just one subblock, or
|
---|
| 186 | // see if we can convert G and P to local matrices
|
---|
| 187 |
|
---|
| 188 | if (local_ || local_dens_) {
|
---|
| 189 | // grab the data pointers from the G and P matrices
|
---|
| 190 | double *gmat, *pmat;
|
---|
| 191 | tim_enter("local data");
|
---|
| 192 | RefSymmSCMatrix gtmp = get_local_data(cl_gmat_, gmat, SCF::Accum);
|
---|
| 193 | RefSymmSCMatrix ptmp = get_local_data(cl_dens_diff_, pmat, SCF::Read);
|
---|
| 194 | tim_exit("local data");
|
---|
| 195 |
|
---|
| 196 | tim_enter("init pmax");
|
---|
| 197 | signed char * pmax = init_pmax(pmat);
|
---|
| 198 | tim_exit("init pmax");
|
---|
| 199 |
|
---|
| 200 | // LocalCLKSContribution lclc(gmat, pmat, functional_->a0());
|
---|
| 201 | // LocalGBuild<LocalCLKSContribution>
|
---|
| 202 | // gb(lclc, tbi_, pl, basis(), scf_grp_, pmax, desired_value_accuracy()/100.0);
|
---|
| 203 | // gb.run();
|
---|
| 204 | int i;
|
---|
| 205 | int nthread = threadgrp_->nthread();
|
---|
| 206 | LocalGBuild<LocalCLKSContribution> **gblds =
|
---|
| 207 | new LocalGBuild<LocalCLKSContribution>*[nthread];
|
---|
| 208 | LocalCLKSContribution **conts = new LocalCLKSContribution*[nthread];
|
---|
| 209 |
|
---|
| 210 | double **gmats = new double*[nthread];
|
---|
| 211 | gmats[0] = gmat;
|
---|
| 212 |
|
---|
| 213 | Ref<GaussianBasisSet> bs = basis();
|
---|
| 214 | int ntri = i_offset(bs->nbasis());
|
---|
| 215 |
|
---|
| 216 | double gmat_accuracy = accuracy;
|
---|
| 217 | if (min_orthog_res() < 1.0) { gmat_accuracy *= min_orthog_res(); }
|
---|
| 218 |
|
---|
| 219 | for (i=0; i < nthread; i++) {
|
---|
| 220 | if (i) {
|
---|
| 221 | gmats[i] = new double[ntri];
|
---|
| 222 | memset(gmats[i], 0, sizeof(double)*ntri);
|
---|
| 223 | }
|
---|
| 224 | conts[i] = new LocalCLKSContribution(gmats[i], pmat, functional_->a0());
|
---|
| 225 | gblds[i] = new LocalGBuild<LocalCLKSContribution>(*conts[i], tbis_[i],
|
---|
| 226 | pl, bs, scf_grp_, pmax, gmat_accuracy, nthread, i
|
---|
| 227 | );
|
---|
| 228 |
|
---|
| 229 | threadgrp_->add_thread(i, gblds[i]);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | tim_enter("start thread");
|
---|
| 233 | if (threadgrp_->start_threads() < 0) {
|
---|
| 234 | ExEnv::err0() << indent
|
---|
| 235 | << "CLKS: error starting threads" << endl;
|
---|
| 236 | abort();
|
---|
| 237 | }
|
---|
| 238 | tim_exit("start thread");
|
---|
| 239 |
|
---|
| 240 | tim_enter("stop thread");
|
---|
| 241 | if (threadgrp_->wait_threads() < 0) {
|
---|
| 242 | ExEnv::err0() << indent
|
---|
| 243 | << "CLKS: error waiting for threads" << endl;
|
---|
| 244 | abort();
|
---|
| 245 | }
|
---|
| 246 | tim_exit("stop thread");
|
---|
| 247 |
|
---|
| 248 | double tnint=0;
|
---|
| 249 | for (i=0; i < nthread; i++) {
|
---|
| 250 | tnint += gblds[i]->tnint;
|
---|
| 251 |
|
---|
| 252 | if (i) {
|
---|
| 253 | for (int j=0; j < ntri; j++)
|
---|
| 254 | gmat[j] += gmats[i][j];
|
---|
| 255 | delete[] gmats[i];
|
---|
| 256 | }
|
---|
| 257 | delete gblds[i];
|
---|
| 258 | delete conts[i];
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | delete[] gmats;
|
---|
| 262 | delete[] gblds;
|
---|
| 263 | delete[] conts;
|
---|
| 264 |
|
---|
| 265 | delete[] pmax;
|
---|
| 266 |
|
---|
| 267 | scf_grp_->sum(&tnint, 1, 0, 0);
|
---|
| 268 | ExEnv::out0() << indent << scprintf("%20.0f integrals\n", tnint);
|
---|
| 269 |
|
---|
| 270 | // if we're running on multiple processors, then sum the G matrix
|
---|
| 271 | tim_enter("sum");
|
---|
| 272 | if (scf_grp_->n() > 1)
|
---|
| 273 | scf_grp_->sum(gmat, i_offset(basis()->nbasis()));
|
---|
| 274 | tim_exit("sum");
|
---|
| 275 |
|
---|
| 276 | // if we're running on multiple processors, or we don't have local
|
---|
| 277 | // matrices, then accumulate gtmp back into G
|
---|
| 278 | tim_enter("accum");
|
---|
| 279 | if (!local_ || scf_grp_->n() > 1)
|
---|
| 280 | cl_gmat_->convert_accumulate(gtmp);
|
---|
| 281 | tim_exit("accum");
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | // for now quit
|
---|
| 285 | else {
|
---|
| 286 | ExEnv::out0() << indent << "Cannot yet use anything but Local matrices\n";
|
---|
| 287 | abort();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | cl_dens_diff_ = pl->to_AO_basis(cl_dens_);
|
---|
| 291 | cl_dens_diff_.scale(0.5);
|
---|
| 292 | integrator_->set_compute_potential_integrals(1);
|
---|
| 293 | integrator_->set_accuracy(accuracy);
|
---|
| 294 | integrator_->integrate(functional_, cl_dens_diff_, cl_dens_diff_);
|
---|
| 295 | exc_ = integrator_->value();
|
---|
| 296 | RefSymmSCMatrix vxa = cl_gmat_.clone();
|
---|
| 297 | vxa->assign((double*)integrator_->alpha_vmat());
|
---|
| 298 | vxa = pl->to_SO_basis(vxa);
|
---|
| 299 | vxc_ = vxa;
|
---|
| 300 |
|
---|
| 301 | tim_enter("symm");
|
---|
| 302 | // get rid of AO delta P
|
---|
| 303 | cl_dens_diff_ = dd;
|
---|
| 304 | dd = cl_dens_diff_.clone();
|
---|
| 305 |
|
---|
| 306 | // now symmetrize the skeleton G matrix, placing the result in dd
|
---|
| 307 | RefSymmSCMatrix skel_gmat = cl_gmat_.copy();
|
---|
| 308 | skel_gmat.scale(1.0/(double)pl->order());
|
---|
| 309 | pl->symmetrize(skel_gmat,dd);
|
---|
| 310 | tim_exit("symm");
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | // F = H+G
|
---|
| 314 | cl_fock_.result_noupdate().assign(hcore_);
|
---|
| 315 | cl_fock_.result_noupdate().accumulate(dd);
|
---|
| 316 | accumddh_->accum(cl_fock_.result_noupdate());
|
---|
| 317 | cl_fock_.computed()=1;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 321 |
|
---|
| 322 | void
|
---|
| 323 | CLKS::two_body_energy(double &ec, double &ex)
|
---|
| 324 | {
|
---|
| 325 | tim_enter("clks e2");
|
---|
| 326 | ec = 0.0;
|
---|
| 327 | ex = 0.0;
|
---|
| 328 |
|
---|
| 329 | if (local_ || local_dens_) {
|
---|
| 330 | // grab the data pointers from the G and P matrices
|
---|
| 331 | double *pmat;
|
---|
| 332 | tim_enter("local data");
|
---|
| 333 | RefSymmSCMatrix dens = ao_density();
|
---|
| 334 | dens->scale(2.0);
|
---|
| 335 | dens->scale_diagonal(0.5);
|
---|
| 336 | RefSymmSCMatrix ptmp = get_local_data(dens, pmat, SCF::Read);
|
---|
| 337 | tim_exit("local data");
|
---|
| 338 |
|
---|
| 339 | // initialize the two electron integral classes
|
---|
| 340 | Ref<TwoBodyInt> tbi = integral()->electron_repulsion();
|
---|
| 341 | tbi->set_integral_storage(0);
|
---|
| 342 |
|
---|
| 343 | tim_enter("init pmax");
|
---|
| 344 | signed char * pmax = init_pmax(pmat);
|
---|
| 345 | tim_exit("init pmax");
|
---|
| 346 |
|
---|
| 347 | LocalCLKSEnergyContribution lclc(pmat, functional_->a0());
|
---|
| 348 | Ref<PetiteList> pl = integral()->petite_list();
|
---|
| 349 | LocalGBuild<LocalCLKSEnergyContribution>
|
---|
| 350 | gb(lclc, tbi, pl, basis(), scf_grp_, pmax,
|
---|
| 351 | desired_value_accuracy()/100.0);
|
---|
| 352 | gb.run();
|
---|
| 353 |
|
---|
| 354 | delete[] pmax;
|
---|
| 355 |
|
---|
| 356 | ec = lclc.ec;
|
---|
| 357 | ex = lclc.ex;
|
---|
| 358 | }
|
---|
| 359 | else {
|
---|
| 360 | ExEnv::out0() << indent << "Cannot yet use anything but Local matrices\n";
|
---|
| 361 | abort();
|
---|
| 362 | }
|
---|
| 363 | tim_exit("clks e2");
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 367 |
|
---|
| 368 | void
|
---|
| 369 | CLKS::two_body_deriv(double * tbgrad)
|
---|
| 370 | {
|
---|
| 371 | tim_enter("grad");
|
---|
| 372 |
|
---|
| 373 | int natom3 = 3*molecule()->natom();
|
---|
| 374 |
|
---|
| 375 | tim_enter("two-body");
|
---|
| 376 | double *hfgrad = new double[natom3];
|
---|
| 377 | memset(hfgrad,0,sizeof(double)*natom3);
|
---|
| 378 | two_body_deriv_hf(hfgrad,functional_->a0());
|
---|
| 379 | //print_natom_3(hfgrad, "Two-body contribution to DFT gradient");
|
---|
| 380 | tim_exit("two-body");
|
---|
| 381 |
|
---|
| 382 | double *dftgrad = new double[natom3];
|
---|
| 383 | memset(dftgrad,0,sizeof(double)*natom3);
|
---|
| 384 | Ref<PetiteList> pl = integral()->petite_list(basis());
|
---|
| 385 | RefSymmSCMatrix aodens = gradient_density();
|
---|
| 386 | aodens.scale(0.5);
|
---|
| 387 | integrator_->set_compute_potential_integrals(0);
|
---|
| 388 | integrator_->init(this);
|
---|
| 389 | integrator_->set_accuracy(desired_gradient_accuracy());
|
---|
| 390 | integrator_->integrate(functional_, aodens, aodens, dftgrad);
|
---|
| 391 | integrator_->done();
|
---|
| 392 | //print_natom_3(dftgrad, "E-X contribution to DFT gradient");
|
---|
| 393 |
|
---|
| 394 | scf_grp_->sum(dftgrad, natom3);
|
---|
| 395 |
|
---|
| 396 | for (int i=0; i<natom3; i++) tbgrad[i] += dftgrad[i] + hfgrad[i];
|
---|
| 397 | delete[] dftgrad;
|
---|
| 398 | delete[] hfgrad;
|
---|
| 399 |
|
---|
| 400 | tim_exit("grad");
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 404 |
|
---|
| 405 | void
|
---|
| 406 | CLKS::init_vector()
|
---|
| 407 | {
|
---|
| 408 | integrator_->init(this);
|
---|
| 409 | CLSCF::init_vector();
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | void
|
---|
| 413 | CLKS::done_vector()
|
---|
| 414 | {
|
---|
| 415 | integrator_->done();
|
---|
| 416 | CLSCF::done_vector();
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 420 |
|
---|
| 421 | // Local Variables:
|
---|
| 422 | // mode: c++
|
---|
| 423 | // c-file-style: "ETS"
|
---|
| 424 | // End:
|
---|