296 lines
11 KiB
C++
296 lines
11 KiB
C++
/*
|
|
Copyright (C) 2008 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include "gelfand.h"
|
|
#include "drt.h"
|
|
#include "readintegrals.h"
|
|
|
|
|
|
#include "options.h"
|
|
|
|
//LA
|
|
#include "vec.h"
|
|
#include "mat.h"
|
|
#include "smat.h"
|
|
#include "sparsemat.h"
|
|
#include "nonclass.h"
|
|
#include "qsort.h"
|
|
|
|
|
|
//NOTE: ncore is the number of "frozen" orbitals already in the TDDFT program, for which no excitations have been generated and no coefficients in tddft_coefs exist
|
|
//in contrast, inactive_occ and inactive_virt are numbers core and virtuals to be dropped at this stage, for which there are
|
|
//elements in the tddft_coefs to be ignored.
|
|
|
|
void do_cis_casida(bool uhf, bool spinflip, int ncore, int (&nocc)[2], int (&nvirt)[2], bool orthonormalize, bool do_casida, bool inverse_casida, bool do_normalize, int inactive_occ, int inactive_virt)
|
|
{
|
|
cout <<"core, occ, virt, freeze, discard "<<ncore<<" "<<nocc[0]<<" "<<nocc[1]<<" "<<nvirt[0]<<" "<<nvirt[1]<<" "<<inactive_occ<<" "<<inactive_virt<<endl;
|
|
|
|
if(inactive_occ || inactive_virt) orthonormalize=true; //dropping slightly destroys the present orthonormality of tddft response coefficients
|
|
|
|
if(uhf) cout <<"UHF version\n";
|
|
if(spinflip) cout <<"SPINFLIP version\n";
|
|
cout <<"Using casida factors ? "<<do_casida<<endl;
|
|
cout <<"Performing reorthonormalization ? "<<orthonormalize<<endl;
|
|
|
|
NRVec<REAL> energies;
|
|
const NRVec<REAL> orbener[2];
|
|
|
|
cin >> *(const_cast<NRVec<REAL> * > (orbener+0));
|
|
if(uhf) cin >> *(const_cast<NRVec<REAL> * > (orbener+1));
|
|
else *(const_cast<NRVec<REAL> * > (orbener+1)) = orbener[0];
|
|
|
|
cin >> energies; int nstate=energies.size();
|
|
cout << "Number of states (incl ground) = " <<nstate<<endl;
|
|
|
|
NRMat<const NRMat<REAL> > tddft_coefs(nstate,2);
|
|
for(int i=0; i<nstate-1; ++i)
|
|
{
|
|
cin >> *(const_cast<NRMat<REAL> * > (&tddft_coefs(i,0)) );
|
|
if(uhf) cin >> *(const_cast<NRMat<REAL> * > (&tddft_coefs(i,1)) );
|
|
else {*(const_cast<NRMat<REAL> * > (&tddft_coefs(i,1)) ) = tddft_coefs(i,0);}
|
|
}
|
|
|
|
int nocc_eff[2]; nocc_eff[0]= nocc[0]-inactive_occ; nocc_eff[1]= nocc[1]-inactive_occ;
|
|
int nvirt_eff[2]; nvirt_eff[0] = nvirt[0] - inactive_virt; nvirt_eff[1] = nvirt[1] - inactive_virt;
|
|
|
|
cout <<"nocc_eff = "<<nocc_eff[0]<<" "<<nocc_eff[1]<<endl;
|
|
cout <<"virt_eff = "<<nvirt_eff[0]<<" "<<nvirt_eff[1]<<endl;
|
|
|
|
int civec_file=open("casidawf",O_WRONLY|O_CREAT|O_LARGEFILE,0777); if(civec_file<0) {perror("cannot open");laerror("IO error");}
|
|
|
|
cout <<"cis_casida freezing "<<inactive_occ <<" discarding "<<inactive_virt<<endl;
|
|
|
|
//create eivecfile header
|
|
int ndets;
|
|
if(spinflip) ndets = nvirt_eff[0]*nocc_eff[1];
|
|
else ndets = 1+nocc_eff[0]*nvirt_eff[0]+nocc_eff[1]*nvirt_eff[1];
|
|
|
|
int dim[2]={nstate-(spinflip?1:0),ndets};
|
|
write(civec_file,dim,2*sizeof(int));
|
|
cout<<"Number of determinants = "<<ndets<<endl;
|
|
|
|
|
|
//create the trivial ground state
|
|
if(!spinflip)
|
|
{
|
|
REAL zero=0;
|
|
REAL one=1;
|
|
write(civec_file,&one,sizeof(REAL));
|
|
for(int i=0; i<ndets-1; ++i) write(civec_file,&zero,sizeof(REAL));
|
|
}
|
|
|
|
|
|
//create records for excited states
|
|
REAL norm=1./sqrt(2.);
|
|
NRMat<REAL> cis_coefs(nstate-1,ndets);
|
|
NRSMat<REAL> casidaoverlaps(nstate-1);
|
|
|
|
for(int i=0; i<nstate-1; ++i)
|
|
{
|
|
if(!spinflip) cis_coefs(i,0)=0.; // no ground state det contrib.
|
|
//optional casida's adjustment of the coefficients
|
|
if(uhf) //no extra norm factor and new order of dets - first all alpha exc, then all beta exc.
|
|
{
|
|
//must be same order of loops as in cis_slatergen case so that coefficients correspond to determinants
|
|
int ithru=0;
|
|
for(int spin=0; spin<=1; ++spin)
|
|
for(int iocc=0; iocc<nocc_eff[spin]; ++iocc)
|
|
for(int ivirt=0; ivirt<nvirt_eff[spin]; ++ivirt)
|
|
{
|
|
double casidafactor=1.;
|
|
if(do_casida) casidafactor= sqrt( (orbener[spin][ivirt+ncore+nocc[spin]] - orbener[spin][iocc+ncore+inactive_occ]) / (energies[i+1] - energies[0]));
|
|
if(inverse_casida) casidafactor= sqrt( (energies[i+1] - energies[0]) / (orbener[spin][ivirt+ncore+nocc[spin]] - orbener[spin][iocc+ncore+inactive_occ]));
|
|
cis_coefs(i,++ithru) = tddft_coefs(i,spin)(iocc+inactive_occ,ivirt)*casidafactor;
|
|
}
|
|
|
|
}
|
|
else if(spinflip)
|
|
{
|
|
for(int iocc=0; iocc<nocc_eff[1]; ++iocc)
|
|
for(int ivirt=0; ivirt<nvirt_eff[0]; ++ivirt)
|
|
{
|
|
cout <<"test "<<i<<" "<<iocc<<" "<<ivirt<<endl;
|
|
//here the position is computed explicitly but it should run in ascending order, too
|
|
double casidafactor=1.;
|
|
if(do_casida) casidafactor= sqrt( (orbener[0][ivirt+ncore+nocc[0]] - orbener[0][iocc+ncore+inactive_occ]) / (energies[i+1] - energies[0]));
|
|
if(inverse_casida) casidafactor= sqrt( (energies[i+1] - energies[0])/(orbener[0][ivirt+ncore+nocc[0]] - orbener[0][iocc+ncore+inactive_occ]));
|
|
cis_coefs(i,iocc*nvirt_eff[0]+ivirt) = tddft_coefs(i,0)(iocc+inactive_occ,ivirt) * casidafactor;
|
|
}
|
|
}
|
|
else //rhf - keep original order of determinants and use 1/sqrt2 factor
|
|
{
|
|
for(int iocc=0; iocc<nocc_eff[0]; ++iocc)
|
|
for(int ivirt=0; ivirt<nvirt_eff[0]; ++ivirt)
|
|
{
|
|
//here the position is computed explicitly but it should run in ascending order, too
|
|
double casidafactor=1.;
|
|
if(do_casida) casidafactor= sqrt( (orbener[0][ivirt+ncore+nocc[0]] - orbener[0][iocc+ncore+inactive_occ]) / (energies[i+1] - energies[0]));
|
|
if(inverse_casida) casidafactor= sqrt( (energies[i+1] - energies[0]) / (orbener[0][ivirt+ncore+nocc[0]] - orbener[0][iocc+ncore+inactive_occ]) );
|
|
cis_coefs(i,1+2*(iocc*nvirt_eff[0]+ivirt)) =
|
|
cis_coefs(i,1+2*(iocc*nvirt_eff[0]+ivirt)+1) = tddft_coefs(i,0)(iocc+inactive_occ,ivirt) * casidafactor * norm;
|
|
}
|
|
}
|
|
//normalize
|
|
if((do_casida||inverse_casida||do_normalize))
|
|
{
|
|
REAL s=xdot(cis_coefs.ncols(),cis_coefs[i],1,cis_coefs[i],1);
|
|
s=1./sqrt(s);
|
|
for(int k=0; k<cis_coefs.ncols(); ++k) cis_coefs[i][k] *= s;
|
|
}
|
|
|
|
for(int j=0; j<=i; ++j) casidaoverlaps(i,j) = cis_coefs.row(i) * cis_coefs.row(j);
|
|
|
|
//check whether the neglect of inactive orbitals was adequate
|
|
REAL tmp, sumsqr=0.;
|
|
for(int spin=0; spin<(uhf?2:1); ++spin)
|
|
for(int iocc=0; iocc<inactive_occ; ++iocc)
|
|
for(int ivirt=0; ivirt<nvirt[spin]; ++ivirt)
|
|
{
|
|
tmp=tddft_coefs(i,spin)(iocc,ivirt);
|
|
sumsqr += tmp*tmp;
|
|
}
|
|
cout <<"Exc. state "<<i+1<<" sum of squares of frozen core TDDFT coefficients = "<<sumsqr<<endl;
|
|
|
|
sumsqr=0.;
|
|
for(int spin=0; spin<(uhf?2:1); ++spin)
|
|
for(int iocc=0; iocc<nocc[spin]; ++iocc)
|
|
for(int ivirt=nvirt_eff[spin]; ivirt<nvirt[spin]; ++ivirt)
|
|
{
|
|
tmp=tddft_coefs(i,spin)(iocc,ivirt);
|
|
sumsqr += tmp*tmp;
|
|
}
|
|
cout <<"Exc. state "<<i+1<<" sum of squares of frozen virtual TDDFT coefficients = "<<sumsqr<<endl;
|
|
|
|
}// state loop i
|
|
|
|
cout <<"overlap matrix of Casida wavefunctions\n" << casidaoverlaps;
|
|
|
|
//orthonormalize
|
|
NRMat<REAL> transformation = sqrtinv(casidaoverlaps);
|
|
cout << "Orthonormalization matrix\n"<<transformation<<endl;
|
|
NRMat<REAL> cis_coefs_new = transformation * cis_coefs;
|
|
cout << "overlap check after orthonormalization\n" << cis_coefs_new * cis_coefs_new.transpose();
|
|
|
|
//
|
|
#if 0
|
|
NRSMat<REAL> casidaoverlaps_diagonal(0.,casidaoverlaps.ncols());
|
|
for(int i=0; i<casidaoverlaps.ncols(); ++i) casidaoverlaps_diagonal(i,i) = casidaoverlaps(i,i);
|
|
NRMat<REAL> normalization_only = sqrtinv(casidaoverlaps_diagonal);
|
|
cout << "test normalization only trafo matrix\n"<<normalization_only<<endl;
|
|
NRVec<REAL> invcasidaoverlaps_diagonalof(casidaoverlaps.ncols());
|
|
normalization_only.diagonalof(invcasidaoverlaps_diagonalof);
|
|
NRMat<REAL> overlaps_n(casidaoverlaps);
|
|
overlaps_n.diagmultr(invcasidaoverlaps_diagonalof);
|
|
overlaps_n.diagmultl(invcasidaoverlaps_diagonalof);
|
|
cout << "overlaps of normalized\n"<<overlaps_n<<endl;
|
|
NRSMat<REAL> overlaps_ns(overlaps_n);
|
|
NRMat<REAL> transformation_n = sqrtinv(overlaps_ns);
|
|
cout << "Orthonormalization matrix of already normalized vectors\n"<<transformation_n<<endl;
|
|
transformation_n.diagmultr(invcasidaoverlaps_diagonalof);
|
|
cout << "Orthonormalization matrix of already normalized vectors, with respect to original vectors\n"<<transformation_n<<endl;
|
|
NRMat<REAL> cis_coefs_new2 = transformation_n * cis_coefs;
|
|
|
|
|
|
cout << "NORM of difference of the two approaches \n"<<(cis_coefs_new-cis_coefs_new2).norm()<<endl;
|
|
cout << "overlap check 2 after orthonormalization\n" << cis_coefs_new2 * cis_coefs_new2.transpose();
|
|
|
|
#endif
|
|
|
|
//phase alignment with previous step, are MO's aligned?
|
|
//is NOT done here, the final CI overlaps are being aligned
|
|
|
|
if(orthonormalize)
|
|
{
|
|
cis_coefs_new.put(civec_file,false);
|
|
cout <<"Orthonormalized wavefunctions used\n";
|
|
}
|
|
else
|
|
{
|
|
cis_coefs.put(civec_file,false);
|
|
cout <<"Original (non-orthonormalized) wavefunctions used\n";
|
|
}
|
|
close(civec_file);
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
sigtraceback(SIGSEGV,1);
|
|
sigtraceback(SIGABRT,1);
|
|
sigtraceback(SIGBUS,1);
|
|
sigtraceback(SIGFPE,1);
|
|
print_version(argc,argv,cout);
|
|
|
|
bool orthonormalize=false;
|
|
bool do_casida=false;
|
|
bool inverse_casida=false;
|
|
bool do_normalize=false;
|
|
bool uhf=0;
|
|
bool spinflip=0;
|
|
int inactive_occ=0;
|
|
int inactive_virt=0;
|
|
//options can be given in any order: repeat until no option is recognized
|
|
for(char **argv0=NULL; argv0!=argv;)
|
|
{
|
|
argv0=argv;
|
|
if(argc>1 && !strcmp(argv[1],"-U")) {uhf=1; --argc; ++argv;}
|
|
if(argc>1 && !strcmp(argv[1],"-S")) {spinflip=1; --argc; ++argv;}
|
|
if(argc>1 && !strcmp(argv[1],"-o")) {orthonormalize=true; --argc; ++argv;}
|
|
if(argc>1 && !strcmp(argv[1],"-c")) {do_casida=true; inverse_casida=false; --argc; ++argv;}
|
|
if(argc>1 && !strcmp(argv[1],"-C")) {inverse_casida=true; do_casida=false; --argc; ++argv;}
|
|
if(argc>1 && !strcmp(argv[1],"-n")) {do_normalize=true; --argc; ++argv;}
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-i"))
|
|
{
|
|
sscanf(argv[2],"%d",&inactive_occ);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-I"))
|
|
{
|
|
sscanf(argv[2],"%d",&inactive_virt);
|
|
argc -=2; argv+=2;
|
|
}
|
|
}
|
|
if(uhf&&spinflip) laerror("UHF and SPINFLIP options are incompatible");
|
|
|
|
|
|
|
|
cout.setf(ios::fixed);
|
|
//cout.setf(ios::scientific);
|
|
cout.precision(12);
|
|
|
|
int ncore,nocc[2],nvirt[2];
|
|
cin >>ncore >>nocc[0] >>nvirt[0];
|
|
if(uhf) cin >> nocc[1] >> nvirt[1];
|
|
else {nocc[1]=nocc[0]; nvirt[1]=nvirt[0];}
|
|
|
|
do_cis_casida(uhf,spinflip,ncore,nocc,nvirt,orthonormalize,do_casida,inverse_casida,do_normalize,inactive_occ,inactive_virt);
|
|
}
|
|
|
|
|