| 1 | //
 | 
|---|
| 2 | // diis.cc
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.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/formio.h>
 | 
|---|
| 35 | #include <util/state/stateio.h>
 | 
|---|
| 36 | #include <math/scmat/cmatrix.h>
 | 
|---|
| 37 | #include <math/optimize/diis.h>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | using namespace sc;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | static ClassDesc DIIS_cd(
 | 
|---|
| 42 |   typeid(DIIS),"DIIS",3,"public SelfConsistentExtrapolation",
 | 
|---|
| 43 |   0, create<DIIS>, create<DIIS>);
 | 
|---|
| 44 | 
 | 
|---|
| 45 | void
 | 
|---|
| 46 | DIIS::init()
 | 
|---|
| 47 | {
 | 
|---|
| 48 |   int dim = ndiis+1;
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   iter = 0;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   btemp = new double[dim];
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   bmat = new double*[dim];
 | 
|---|
| 55 |   bold = new double*[ndiis];
 | 
|---|
| 56 |   
 | 
|---|
| 57 |   if (!btemp || !bmat || !bold) {
 | 
|---|
| 58 |     ExEnv::err0() << indent
 | 
|---|
| 59 |          << "DIIS::init: alloc of bmat, bold, and btemp failed\n";
 | 
|---|
| 60 |     abort();
 | 
|---|
| 61 |   }
 | 
|---|
| 62 |   
 | 
|---|
| 63 |   for (int i=0; i < dim; i++) {
 | 
|---|
| 64 |     bmat[i] = new double[dim];
 | 
|---|
| 65 |     if (i < dim-1)
 | 
|---|
| 66 |       bold[i] = new double[ndiis];
 | 
|---|
| 67 |   }
 | 
|---|
| 68 |   
 | 
|---|
| 69 |   diism_data = new Ref<SCExtrapData>[ndiis];
 | 
|---|
| 70 |   diism_error = new Ref<SCExtrapError>[ndiis];
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | DIIS::DIIS(int strt, int ndi, double dmp, int ngr, int ngrdiis) :
 | 
|---|
| 74 |   btemp(0), bold(0), bmat(0), diism_data(0), diism_error(0)
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   start = strt;
 | 
|---|
| 77 |   ndiis = ndi;
 | 
|---|
| 78 |   damping_factor = dmp;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   ngroup = ngr;
 | 
|---|
| 81 |   ngroupdiis = ngrdiis;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   init();
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | DIIS::DIIS(StateIn& s) :
 | 
|---|
| 87 |   SelfConsistentExtrapolation(s),
 | 
|---|
| 88 |   btemp(0), bold(0), bmat(0), diism_data(0), diism_error(0)
 | 
|---|
| 89 | {
 | 
|---|
| 90 |   int i;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   s.get(start);
 | 
|---|
| 93 |   s.get(ndiis);
 | 
|---|
| 94 |   s.get(iter);
 | 
|---|
| 95 |   if (s.version(::class_desc<DIIS>()) >= 2) {
 | 
|---|
| 96 |     s.get(ngroup);
 | 
|---|
| 97 |     s.get(ngroupdiis);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 |   s.get(damping_factor);
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   // alloc storage for arrays
 | 
|---|
| 102 |   btemp = new double[ndiis+1];
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   bold = new double*[ndiis];
 | 
|---|
| 105 |   for (i=0; i < ndiis; i++)
 | 
|---|
| 106 |     bold[i] = new double[ndiis];
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   bmat = new double*[ndiis+1];
 | 
|---|
| 109 |   for (i=0; i <= ndiis; i++)
 | 
|---|
| 110 |     bmat[i] = new double[ndiis+1];
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   diism_data = new Ref<SCExtrapData>[ndiis];
 | 
|---|
| 113 |   diism_error = new Ref<SCExtrapError>[ndiis];
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   // read arrays
 | 
|---|
| 116 |   int ndat = iter;
 | 
|---|
| 117 |   if (iter > ndiis) ndat = ndiis;
 | 
|---|
| 118 |   if (s.version(::class_desc<DIIS>()) < 3) ndat = ndiis;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   s.get_array_double(btemp,ndat+1);
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   for (i=0; i < ndat; i++)
 | 
|---|
| 123 |     s.get_array_double(bold[i],ndat);
 | 
|---|
| 124 |   
 | 
|---|
| 125 |   for (i=0; i <= ndat; i++)
 | 
|---|
| 126 |     s.get_array_double(bmat[i],ndat+1);
 | 
|---|
| 127 |   
 | 
|---|
| 128 |   for (i=0; i < ndat; i++) {
 | 
|---|
| 129 |     diism_data[i] << SavableState::restore_state(s);
 | 
|---|
| 130 |     diism_error[i] << SavableState::restore_state(s);
 | 
|---|
| 131 |   }
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | DIIS::DIIS(const Ref<KeyVal>& keyval):
 | 
|---|
| 135 |   SelfConsistentExtrapolation(keyval),
 | 
|---|
| 136 |   btemp(0), bold(0), bmat(0), diism_data(0), diism_error(0)
 | 
|---|
| 137 | {
 | 
|---|
| 138 |   ndiis = keyval->intvalue("n");
 | 
|---|
| 139 |   if (keyval->error() != KeyVal::OK) ndiis = 5;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   start = keyval->intvalue("start");
 | 
|---|
| 142 |   if (keyval->error() != KeyVal::OK) start = 1;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   ngroup = keyval->intvalue("ngroup");
 | 
|---|
| 145 |   if (keyval->error() != KeyVal::OK) ngroup = 1;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   ngroupdiis = keyval->intvalue("ngroupdiis");
 | 
|---|
| 148 |   if (keyval->error() != KeyVal::OK) ngroupdiis = 1;
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   damping_factor = keyval->doublevalue("damping_factor");
 | 
|---|
| 151 |   if (keyval->error() != KeyVal::OK) damping_factor = 0;
 | 
|---|
| 152 |   
 | 
|---|
| 153 |   if (ndiis <= 0) {
 | 
|---|
| 154 |     ExEnv::err0() << indent
 | 
|---|
| 155 |          << "DIIS::DIIS(const Ref<KeyVal>& keyval): got ndiis = 0\n";
 | 
|---|
| 156 |     abort();
 | 
|---|
| 157 |   }
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   init();
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | DIIS::~DIIS()
 | 
|---|
| 163 | {
 | 
|---|
| 164 |   if (btemp) {
 | 
|---|
| 165 |     delete[] btemp;
 | 
|---|
| 166 |     btemp=0;
 | 
|---|
| 167 |   }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   if (bold) {
 | 
|---|
| 170 |     for (int i=0; i < ndiis; i++) {
 | 
|---|
| 171 |       if (bold[i])
 | 
|---|
| 172 |         delete[] bold[i];
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |     delete[] bold;
 | 
|---|
| 175 |     bold=0;
 | 
|---|
| 176 |   }
 | 
|---|
| 177 |     
 | 
|---|
| 178 |   if (bmat) {
 | 
|---|
| 179 |     for (int i=0; i <= ndiis; i++) {
 | 
|---|
| 180 |       if (bmat[i])
 | 
|---|
| 181 |         delete[] bmat[i];
 | 
|---|
| 182 |     }
 | 
|---|
| 183 |     delete[] bmat;
 | 
|---|
| 184 |     bmat=0;
 | 
|---|
| 185 |   }
 | 
|---|
| 186 |     
 | 
|---|
| 187 |   if (diism_data) {
 | 
|---|
| 188 |     delete[] diism_data;
 | 
|---|
| 189 |     diism_data=0;
 | 
|---|
| 190 |   }
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   if (diism_error) {
 | 
|---|
| 193 |     delete[] diism_error;
 | 
|---|
| 194 |     diism_error=0;
 | 
|---|
| 195 |   }
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | void
 | 
|---|
| 199 | DIIS::save_data_state(StateOut& s)
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   int i;
 | 
|---|
| 202 | 
 | 
|---|
| 203 |   SelfConsistentExtrapolation::save_data_state(s);
 | 
|---|
| 204 |   s.put(start);
 | 
|---|
| 205 |   s.put(ndiis);
 | 
|---|
| 206 |   s.put(iter);
 | 
|---|
| 207 |   s.put(ngroup);
 | 
|---|
| 208 |   s.put(ngroupdiis);
 | 
|---|
| 209 |   s.put(damping_factor);
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   int ndat = iter;
 | 
|---|
| 212 |   if (iter > ndiis) ndat = ndiis;
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   s.put_array_double(btemp, ndat+1);
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   for (i=0; i < ndat; i++)
 | 
|---|
| 217 |     s.put_array_double(bold[i], ndat);
 | 
|---|
| 218 |   
 | 
|---|
| 219 |   for (i=0; i <= ndat; i++)
 | 
|---|
| 220 |     s.put_array_double(bmat[i], ndat+1);
 | 
|---|
| 221 |   
 | 
|---|
| 222 |   for (i=0; i < ndat; i++) {
 | 
|---|
| 223 |     SavableState::save_state(diism_data[i].pointer(),s);
 | 
|---|
| 224 |     SavableState::save_state(diism_error[i].pointer(),s);
 | 
|---|
| 225 |   }
 | 
|---|
| 226 | }
 | 
|---|
| 227 | 
 | 
|---|
| 228 | void
 | 
|---|
| 229 | DIIS::reinitialize()
 | 
|---|
| 230 | {
 | 
|---|
| 231 |   iter=0;
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | void
 | 
|---|
| 235 | DIIS::start_extrapolation()
 | 
|---|
| 236 | {
 | 
|---|
| 237 |   if (start > iter) start = iter+1;
 | 
|---|
| 238 | }
 | 
|---|
| 239 | 
 | 
|---|
| 240 | int
 | 
|---|
| 241 | DIIS::extrapolate(const Ref<SCExtrapData>& data,
 | 
|---|
| 242 |                   const Ref<SCExtrapError>& error)
 | 
|---|
| 243 | {
 | 
|---|
| 244 |   int i, j, k;
 | 
|---|
| 245 |   int last = iter;
 | 
|---|
| 246 |   int trial = 0;
 | 
|---|
| 247 |   int col = iter + 2;
 | 
|---|
| 248 |   double norm, determ;
 | 
|---|
| 249 |   double scale;
 | 
|---|
| 250 | 
 | 
|---|
| 251 |   iter++;
 | 
|---|
| 252 | 
 | 
|---|
| 253 |   scale = 1.0 + damping_factor;
 | 
|---|
| 254 | 
 | 
|---|
| 255 |   if (iter > ndiis) {
 | 
|---|
| 256 |       last = ndiis-1;
 | 
|---|
| 257 |       col = ndiis+1;
 | 
|---|
| 258 |       dtemp_data = diism_data[0];
 | 
|---|
| 259 |       dtemp_error = diism_error[0];
 | 
|---|
| 260 |       for (i=0; i < last ; i++) {
 | 
|---|
| 261 |           diism_data[i] = diism_data[i+1];
 | 
|---|
| 262 |           diism_error[i] = diism_error[i+1];
 | 
|---|
| 263 |         }
 | 
|---|
| 264 |       diism_data[last] = dtemp_data;
 | 
|---|
| 265 |       diism_error[last] = dtemp_error;
 | 
|---|
| 266 |     }
 | 
|---|
| 267 | 
 | 
|---|
| 268 |   diism_data[last] = data->copy();
 | 
|---|
| 269 |   diism_error[last] = error;
 | 
|---|
| 270 | 
 | 
|---|
| 271 |   set_error(error->error());
 | 
|---|
| 272 |                
 | 
|---|
| 273 |   // then set up B matrix, where B(i,j) = <ei|ej>
 | 
|---|
| 274 | 
 | 
|---|
| 275 |   // move bold(i+1,j+1) to bold(i,j)
 | 
|---|
| 276 |   if (iter > ndiis) {
 | 
|---|
| 277 |       for (i=0; i < last ; i++) {
 | 
|---|
| 278 |           for (j=0; j <= i ; j++) {
 | 
|---|
| 279 |               bold[i][j]=bold[j][i]=bold[i+1][j+1];
 | 
|---|
| 280 |             }
 | 
|---|
| 281 |         }
 | 
|---|
| 282 |     }
 | 
|---|
| 283 | 
 | 
|---|
| 284 |   // and set the current rows of bold
 | 
|---|
| 285 |   for (i=0; i <= last ; i++)
 | 
|---|
| 286 |       bold[i][last]=bold[last][i] = 
 | 
|---|
| 287 |                       diism_error[i]->scalar_product(diism_error[last]);
 | 
|---|
| 288 | 
 | 
|---|
| 289 |   bmat[0][0] = 0.0;
 | 
|---|
| 290 |   btemp[0] = -1.0;
 | 
|---|
| 291 | 
 | 
|---|
| 292 |   if (bold[0][0] > 1.e-10) {
 | 
|---|
| 293 |       norm = 1.0/bold[0][0];
 | 
|---|
| 294 |     }
 | 
|---|
| 295 |   else {
 | 
|---|
| 296 |       norm = 1.0;
 | 
|---|
| 297 |     }
 | 
|---|
| 298 | 
 | 
|---|
| 299 |   for (i=1; i <= last+1 ; i++) {
 | 
|---|
| 300 |       bmat[i][0]=bmat[0][i] = -1.0;
 | 
|---|
| 301 |       btemp[i] = 0.0;
 | 
|---|
| 302 |       for (j=1; j <= i ; j++) {
 | 
|---|
| 303 |           bmat[i][j]=bmat[j][i] = bold[i-1][j-1]*norm;
 | 
|---|
| 304 |           if (i==j) bmat[i][j] *= scale;
 | 
|---|
| 305 |         }
 | 
|---|
| 306 |     }
 | 
|---|
| 307 | 
 | 
|---|
| 308 |   // finally, solve the set of linear equations, obtain the coefficients,
 | 
|---|
| 309 |   // and form the new fock matrix F= sum(i=1,n) ci*Fi
 | 
|---|
| 310 | 
 | 
|---|
| 311 |   if (iter-1) {
 | 
|---|
| 312 |       determ = cmat_solve_lin(bmat,0,btemp,col);
 | 
|---|
| 313 | 
 | 
|---|
| 314 |       // test for poorly conditioned equations */
 | 
|---|
| 315 |       while (fabs(determ) < 1.0e-19 && trial < last) {
 | 
|---|
| 316 | 
 | 
|---|
| 317 |           trial++;
 | 
|---|
| 318 |           col--;
 | 
|---|
| 319 | 
 | 
|---|
| 320 |           bmat[0][0] = 0.0;
 | 
|---|
| 321 |           btemp[0] = -1.0;
 | 
|---|
| 322 | 
 | 
|---|
| 323 |           if (bold[trial][trial] > 1.e-10) {
 | 
|---|
| 324 |               norm=1.0/bold[trial][trial];
 | 
|---|
| 325 |             }
 | 
|---|
| 326 |           else {
 | 
|---|
| 327 |               norm = 1.0;
 | 
|---|
| 328 |             }
 | 
|---|
| 329 | 
 | 
|---|
| 330 |           for (i=1; i <= last-trial+1 ; i++) {
 | 
|---|
| 331 |               bmat[i][0]=bmat[0][i] = -1.0;
 | 
|---|
| 332 |               for (j=1; j <= i ; j++) {
 | 
|---|
| 333 |                   bmat[i][j]=bmat[j][i]=bold[i+trial-1][j+trial-1]*norm;
 | 
|---|
| 334 |                   if (i==j) bmat[i][j] *= scale;
 | 
|---|
| 335 |                 }
 | 
|---|
| 336 |               btemp[i] = 0.0;
 | 
|---|
| 337 |             }
 | 
|---|
| 338 | 
 | 
|---|
| 339 |           determ = cmat_solve_lin(bmat,0,btemp,col);
 | 
|---|
| 340 |         }
 | 
|---|
| 341 | 
 | 
|---|
| 342 |       if (fabs(determ) < 10.0e-20) {
 | 
|---|
| 343 |         ExEnv::err0() << indent
 | 
|---|
| 344 |              << "DIIS::extrapolate:  trial " << trial << " no good\n";
 | 
|---|
| 345 |         return -1;
 | 
|---|
| 346 |       }
 | 
|---|
| 347 | 
 | 
|---|
| 348 |       if (iter >= start && (((iter-start)%ngroup) < ngroupdiis)) {
 | 
|---|
| 349 |           int kk=1;
 | 
|---|
| 350 | 
 | 
|---|
| 351 |           data->zero();
 | 
|---|
| 352 | 
 | 
|---|
| 353 |           for (k=trial; k < last+1 ; k++) {
 | 
|---|
| 354 |               data->accumulate_scaled(btemp[kk], diism_data[k]);
 | 
|---|
| 355 |               kk++;
 | 
|---|
| 356 |             }
 | 
|---|
| 357 |         }
 | 
|---|
| 358 |     }
 | 
|---|
| 359 | 
 | 
|---|
| 360 |   return 0;
 | 
|---|
| 361 | }
 | 
|---|
| 362 | 
 | 
|---|
| 363 | void
 | 
|---|
| 364 | DIIS::print(std::ostream& o) const
 | 
|---|
| 365 | {
 | 
|---|
| 366 |   o << indent
 | 
|---|
| 367 |     << "DIIS: "
 | 
|---|
| 368 |     << "n=" << ndiis
 | 
|---|
| 369 |     << ", start=" << start
 | 
|---|
| 370 |     << ", ngroup=" << ngroup
 | 
|---|
| 371 |     << ", ngroupdiis=" << ngroupdiis
 | 
|---|
| 372 |     << ", damping_factor=" << damping_factor
 | 
|---|
| 373 |     << std::endl;
 | 
|---|
| 374 | }
 | 
|---|
| 375 | 
 | 
|---|
| 376 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 377 | 
 | 
|---|
| 378 | // Local Variables:
 | 
|---|
| 379 | // mode: c++
 | 
|---|
| 380 | // c-file-style: "ETS"
 | 
|---|
| 381 | // End:
 | 
|---|