// // cscphf.cc // // Copyright (C) 1996 Limit Point Systems, Inc. // // Author: Ida Nielsen // Maintainer: LPS // // This file is part of the SC Toolkit. // // The SC Toolkit is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // // The SC Toolkit is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with the SC Toolkit; see the file COPYING.LIB. If not, write to // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. // // The U.S. Government is granted a limited license as per AL 91-7. // #include #include #include #include #include #include #include #include #include using namespace std; using namespace sc; static void compute_alpha(int dim, double **AP, double **alpha, double **P, double *eigval, int nocc, int nvir); ////////////////////////////////////////////////////////////////////////// // Do a direct CPHF calculation in the AO basis; equations are formulated // in terms of the occ-vir block P2aj of the second order correction (P2) to // the MP2 density matrix (cf. Frisch et al., CPL 166, p. 275 (1990)). // // CPHF equations: // (I-A)P2aj - B = 0 (B(a,j) = L(a,j)/(eigval[a]-eigval[j])) // A is a matrix (dimension dimP*dimP), // P2aj and B are vectors (dimension dimP) // (P2aj is kept as a RefSCMatrix); // Only closed-shell cases handled; no orbitals can be frozen // On exit, P2aj has been computed. void MBPT2::cs_cphf(double **scf_vector, double *Laj, double *eigval, RefSCMatrix& P2aj) { double epsilon = cphf_epsilon_; //convergence criterion for P2aj int i, j, k, l, a; int niter; int dimP = nocc*nvir; Ref kit = basis()->matrixkit(); RefSCDimension nbasis_dim = ao_dimension()->blocks()->subdim(0); RefSCDimension nvir_dim(new SCDimension(nvir,1)); nvir_dim->blocks()->set_subdim(0, new SCDimension(nvir)); RefSCDimension nocc_dim(new SCDimension(nocc,1)); nocc_dim->blocks()->set_subdim(0, new SCDimension(nocc)); RefSCMatrix Cv(nbasis_dim,nvir_dim,kit); RefSCMatrix Co(nbasis_dim,nocc_dim,kit); RefSCMatrix D_matrix(nbasis_dim,nbasis_dim,kit); RefSCMatrix AP_matrix(nvir_dim,nocc_dim,kit); // holds A*P[i-1] RefSCMatrix P_matrix(nvir_dim, nocc_dim, kit); RefSymmSCMatrix G(nbasis_dim,kit); double *projctn = new double[dimP]; double *P_sum_new = new double[dimP]; double *P_sum_old = new double[dimP]; double **AP_matrix_tot; // row is A*P[k] double **P_tmp, **alpha_tmp, **AP_matrix_tmp; double **P; double *D; double **alpha; double *ptr1, *ptr2; double *laj_ptr; double dot_prod; double coef; double tmp_val1, tmp_val2; double maxabs; // Debug print if (debug_) ExEnv::out0() << indent << "Entered cphf" << endl; // End of debug print //////////////////////////////////////////////////////////// // Allocate and initialize various variables //////////////////////////////////////////////////////////// AP_matrix_tot = new double*[1]; AP_matrix_tot[0] = new double[dimP]; alpha = new double*[1]; alpha[0] = new double[1]; P = new double*[1]; P[0] = new double[dimP]; D = new double[nbasis*nbasis]; // NB: Elements in Laj are ordered as (j*nvir + a) // since this ordering has been used with benefit in // MP2 gradient program ptr1 = P[0]; ptr2 = P_sum_old; for (a=0; aset_element(j,k,*ptr1++); // Convert P[i-1] to RefSCMatrix } } D_matrix = Cv*P_matrix*Co.t(); #if 0 D_matrix = D_matrix + D_matrix.t(); D_matrix->convert(D); // Convert D_matrix to double* D make_cs_gmat(G, D); #else RefSymmSCMatrix sD(D_matrix.rowdim(), kit); sD.assign(0.0); sD.accumulate_symmetric_sum(D_matrix); make_cs_gmat_new(G, sD); #endif AP_matrix = 2*Cv.t()*G*Co; ptr1 = AP_matrix_tot[i-1]; for (j=0; jget_element(j,k)/(eigval[k]-eigval[j+nocc]); AP_matrix->set_element(j,k,tmp_val1); *ptr1++ = tmp_val1; } } // End of AP_matrix computation // Compute coefficients alpha[0],...,alpha[i-1] compute_alpha(i, AP_matrix_tot, alpha, P, eigval, nocc, nvir); // Compute the vector P_sum_new = alpha[0]P[0]+...+alpha[i-1]P[i-1] ptr1 = P_sum_new; for (j=0; j maxabs) maxabs = fabs(tmp_val2); } if (debug_) { ExEnv::out0() << indent << scprintf("RMS(P2aj_new-P2aj_old) = %12.10lf", sqrt((tmp_val1)/dimP)) << endl; ExEnv::out0() << indent << scprintf("max. abs. element of (P2aj_new-P2aj_old) = %12.10lf", maxabs) << endl; } if (sqrt(tmp_val1)/dimP < epsilon && maxabs < epsilon) break; // Converged // Put P_sum_new into P_sum old ptr1 = P_sum_new; ptr2 = P_sum_old; for (j=0; j } ptr1 = P[j]; coef = 0.0; for (k=0; kget_element(k,l); } } coef /= dot_prod; ptr1 = P[j]; ptr2 = projctn; for (k=0; kget_element(j,k) - *ptr2++; } } ///////////////////////////////////////////// // Test for convergence (based on norm(P[i]) ///////////////////////////////////////////// tmp_val1 = 0.0; for (l=0; l= warniter) { ExEnv::out0() << indent << scprintf("CPHF: iter = %2d rms(P) = %12.10f eps = %12.10f", niter, tmp_val1, epsilon) << endl; } } /////////////////////////////////////////////////////////////// // If CPHF equations did not converge, exit with error message /////////////////////////////////////////////////////////////// if (niter == maxiter) { ExEnv::out0() << indent << "CPHF equations did not converge in " << maxiter << " iterations" << endl; abort(); } ///////////////////////////////////////////////////// // The converged vector is in P_sum_new; // Put elements into P2aj // NB: Elements in P2aj are ordered as (a*nocc + j); ///////////////////////////////////////////////////// ptr1 = P_sum_new; for (i=0; iset_element(i,j,*ptr1++); } } // Debug print if (debug_) ExEnv::out0() << indent << "Exiting cphf" << endl; // End of debug print // Deallocate various arrays delete[] D; for (i=0; i kit = SCMatrixKit::default_matrixkit(); RefSCDimension C_dim(new SCDimension(dim)); RefSCMatrix C(C_dim,C_dim,kit); RefSCVector B(C_dim,kit); RefSCVector X(C_dim,kit); // Compute norms of vectors P[i] and put into norm for (i=0; iset_element(i,j,tmp1/(norm[i]*norm[j])); } } // Construct vector B B->set_element(0,norm[0]); for (i=1; iset_element(i,0.0); // Compute X = inv(C)*B X = C.i()*B; // Put elements of X into alpha for (i=0; iget_element(i)/norm[i]; } delete[] norm; } //////////////////////////////////////////////////////////////////////////// // Local Variables: // mode: c++ // c-file-style: "CLJ-CONDENSED" // End: