2328 lines
84 KiB
C++
2328 lines
84 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/>.
|
|
*/
|
|
|
|
//this code is too slow - cf. JCTC 12.1207,2016 for improvements - mainly precomputing of alpha and beta factors and laplace expansion of last column for CIS w.f.s
|
|
//the main improvement coult be easy to implement by making a hashtable of already computed alpha, beta factors on the fly
|
|
//and reading from the table if already has been computed previously
|
|
//this program computes overlaps of arbitrary wave functions expressed in Slater determinants or GUGA CSFs
|
|
//UHF version now available
|
|
//NO particular order of determinants is assumed - all dets are listed in Slaterfile and CI coefficients
|
|
//in matching order are in the input
|
|
//
|
|
//-m selects the algorithm (notes and references in doc/cioverlap.tex), the default is 2:
|
|
//0 the original one: loop over pairs of determinants with screening (-t, -e, -A) and precomputed elimination of inactive orbitals (-i)
|
|
//1 string tables: the determinants are split into distinct alpha and beta strings, the determinant of the MO overlaps is computed
|
|
// once for each pair of strings (frozen core orbitals and orbitals common to all strings are eliminated beforehand)
|
|
// and the tables are contracted with the CI vectors; no screening, any wave function
|
|
//2 closed formulas for CIS-type wave functions (single replacements in one spin from a common reference) and for spin-flip lists
|
|
// (one electron removed from one spin and added to the other): no determinants for pairs of strings, only matrix products
|
|
// if the overlap block of the reference orbitals is ill-conditioned (the reference changed its character between the geometries),
|
|
// a warning is printed and the overlap is computed by 1; other wave functions are computed by 1 as well
|
|
|
|
#undef oldversion
|
|
|
|
#include <iostream>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <time.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"
|
|
|
|
#undef DEBUG
|
|
#undef DEBUG2
|
|
#undef DEBUG3
|
|
|
|
#include <cstdlib>
|
|
#include <map>
|
|
#include <cmath>
|
|
|
|
template<class MAT>
|
|
struct twooverlaps
|
|
{
|
|
NRMat<typename LA_traits<MAT>::elementtype> alpha;
|
|
NRMat<typename LA_traits<MAT>::elementtype> beta;
|
|
int rowparity; //parity of permutation to get rows in alpha,beta order
|
|
int colparity; //parity of permutation to get columns in alpha,beta order
|
|
};
|
|
|
|
template<class MAT, class INDEX>
|
|
const twooverlaps<MAT> slatersubmatrices(const MAT (&a)[2], const int nelec, const INDEX rows, const INDEX cols, const int ncore, const int nactive, const bool inversedorbitals)
|
|
{
|
|
twooverlaps<MAT> r;
|
|
|
|
const int ncoreshift=0; //even with frozen core, the numbering of MOs is NOT shifted
|
|
|
|
int nalpha=0;
|
|
int nbeta=0;
|
|
for(int j=0; j<nelec; ++j) if(rows[j]<0) ++nbeta; else ++nalpha;
|
|
|
|
r.alpha.resize(ncore+nalpha,ncore+nalpha);
|
|
r.beta.resize(ncore+nbeta,ncore+nbeta);
|
|
r.alpha.clear();
|
|
r.beta.clear();
|
|
|
|
//count the number of betas all alphas must transpose with when moved to the left in conservative order
|
|
r.rowparity=ncore*(ncore-1)/2;
|
|
r.colparity=ncore*(ncore-1)/2;
|
|
int rowbeta=ncore;
|
|
int colbeta=ncore;
|
|
for(int j=0; j<nelec; ++j)
|
|
{
|
|
if(rows[j]<0) ++rowbeta; else r.rowparity += rowbeta;
|
|
if(cols[j]<0) ++colbeta; else r.colparity += colbeta;
|
|
}
|
|
|
|
|
|
//core-core block
|
|
for(int i=0; i<ncore; ++i)
|
|
for(int j=0; j<ncore; ++j)
|
|
{
|
|
r.alpha(i,j) = a[0](i,j);
|
|
r.beta(i,j) = a[1](i,j);
|
|
}
|
|
|
|
//core-active and active-core
|
|
for(int i=0; i<ncore; ++i)
|
|
{
|
|
int jac=0;
|
|
int jbc=0;
|
|
int jar=0;
|
|
int jbr=0;
|
|
for(int j=0; j<nelec; ++j)
|
|
{
|
|
if(inversedorbitals)
|
|
{
|
|
if(cols[j]<0) r.beta(i,ncore+ jbc++) = a[1](i,nactive-abs(cols[j])+ncoreshift);
|
|
else r.alpha(i,ncore+ jac++) = a[0](i,nactive-abs(cols[j])+ncoreshift);
|
|
|
|
if(rows[j]<0) r.beta(ncore+ jbr++,i) = a[1](nactive-abs(rows[j])+ncoreshift,i);
|
|
else r.alpha(ncore+ jar++,i) = a[0](nactive-abs(rows[j])+ncoreshift,i);
|
|
}
|
|
else
|
|
{
|
|
if(cols[j]<0) r.beta(i,ncore+ jbc++) = a[1](i,abs(cols[j])+ncoreshift-1);
|
|
else r.alpha(i,ncore+ jac++) = a[0](i,abs(cols[j])+ncoreshift-1);
|
|
|
|
if(rows[j]<0) r.beta(ncore+ jbr++,i) = a[1](abs(rows[j])+ncoreshift-1,i);
|
|
else r.alpha(ncore+ jar++,i) = a[0](abs(rows[j])+ncoreshift-1,i);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//active-active block
|
|
if(inversedorbitals)
|
|
{
|
|
int ia=0; int ib=0;
|
|
for(int i=0; i<nelec; ++i)
|
|
{
|
|
int ja=0; int jb=0;
|
|
for(int j=0; j<nelec; ++j)
|
|
{
|
|
cout <<"here2\n";
|
|
if(rows[i]<0 && cols[j]<0) r.beta(ncore+ib,ncore+jb) = a[1](nactive-abs(rows[i])+ncoreshift,nactive-abs(cols[j])+ncoreshift);
|
|
if(rows[i]>0 && cols[j]>0) r.alpha(ncore+ia,ncore+ja) = a[0](nactive-abs(rows[i])+ncoreshift,nactive-abs(cols[j])+ncoreshift);
|
|
if(cols[j]<0) ++jb; else ++ja;
|
|
}
|
|
if(rows[i]<0) ++ib; else ++ia;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int ia=0; int ib=0;
|
|
for(int i=0; i<nelec; ++i)
|
|
{
|
|
int ja=0; int jb=0;
|
|
for(int j=0; j<nelec; ++j)
|
|
{
|
|
|
|
if(rows[i]<0 && cols[j]<0) r.beta(ncore+ib,ncore+jb) = a[1](abs(rows[i])+ncoreshift-1,abs(cols[j])+ncoreshift-1);
|
|
if(rows[i]>0 && cols[j]>0) r.alpha(ncore+ia,ncore+ja) = a[0](abs(rows[i])+ncoreshift-1,abs(cols[j])+ncoreshift-1);
|
|
if(cols[j]<0) ++jb; else ++ja;
|
|
}
|
|
if(rows[i]<0) ++ib; else ++ia;
|
|
}
|
|
}
|
|
|
|
return r;
|
|
}
|
|
|
|
|
|
#if defined(oldversion) || defined(DEBUG3)
|
|
template<class MAT, class INDEX>
|
|
const NRMat<typename LA_traits<MAT>::elementtype> slatersubmatrix(const MAT (&a)[2], const int nelec, const INDEX rows, const INDEX cols, const int ncore, const int nactive, const bool inversedorbitals)
|
|
{
|
|
const int ncoreshift=0; //even with frozen core, the numbering of MOs is NOT shifted
|
|
|
|
int ncore2=2*ncore;
|
|
NRMat<typename LA_traits<MAT>::elementtype> r(nelec+ncore2,nelec+ncore2);
|
|
|
|
//core-core block
|
|
for(int i=0; i<ncore2; ++i)
|
|
for(int j=0; j<ncore2; ++j)
|
|
{
|
|
r(i,j) = (i^j)&1 ? 0 : a[i&1](i/2,j/2);
|
|
}
|
|
|
|
//core-active and active-core
|
|
for(int i=0; i<ncore2; ++i)
|
|
for(int j=0; j<nelec; ++j)
|
|
{
|
|
int si= i&1? -1:1;
|
|
if(inversedorbitals)
|
|
{
|
|
r(i,ncore2+j) = si*cols[j]<0 ? 0. : a[i&1](i/2,nactive-abs(cols[j])+ncoreshift);
|
|
r(ncore2+j,i) = si*rows[j]<0 ? 0. : a[i&1](nactive-abs(rows[j])+ncoreshift,i/2);
|
|
}
|
|
else
|
|
{
|
|
r(i,ncore2+j) = si*cols[j]<0 ? 0. : a[i&1](i/2,abs(cols[j])+ncoreshift-1);
|
|
r(ncore2+j,i) = si*rows[j]<0 ? 0. : a[i&1](abs(rows[j])+ncoreshift-1,i/2);
|
|
}
|
|
}
|
|
|
|
//active-active block
|
|
if(inversedorbitals)
|
|
for(int i=0; i<nelec; ++i)
|
|
for(int j=0; j<nelec; ++j)
|
|
r(ncore2+i,ncore2+j) = rows[i]*cols[j]<0?0.:a[rows[i]<0?1:0](nactive-abs(rows[i])+ncoreshift,nactive-abs(cols[j])+ncoreshift);
|
|
else
|
|
for(int i=0; i<nelec; ++i)
|
|
for(int j=0; j<nelec; ++j)
|
|
r(ncore2+i,ncore2+j) = rows[i]*cols[j]<0?0.:a[rows[i]<0?1:0](abs(rows[i])+ncoreshift-1,abs(cols[j])+ncoreshift-1);
|
|
return r;
|
|
}
|
|
#endif
|
|
|
|
|
|
struct part_eliminated
|
|
{
|
|
NRMat<REAL> submat;
|
|
NRSMat<REAL> coeffs;
|
|
REAL det;
|
|
};
|
|
|
|
static part_eliminated *precompa=NULL, *precompb=NULL;
|
|
|
|
void prepare_elimination(part_eliminated **precomp, const NRMat<REAL> &a, int inactive)
|
|
{
|
|
*precomp = new part_eliminated;
|
|
(*precomp)->submat = a.submatrix(0,inactive-1,0,inactive-1);
|
|
(*precomp)->coeffs.resize(inactive);
|
|
(*precomp)->coeffs.clear();
|
|
|
|
//cout <<"Constant part of overlap matrices\n"<<(*precomp)->submat;
|
|
|
|
//no pivoting necessary, assumed inactive orbitals do not change character and diagonal elements are about 0.99
|
|
for(int i=0; i<inactive-1; ++i)
|
|
{
|
|
REAL p=(*precomp)->submat(i,i);
|
|
for(int j=i+1; j<inactive; ++j)
|
|
{
|
|
REAL q= -(*precomp)->submat(j,i)/p;
|
|
(*precomp)->coeffs(i,j) = q;
|
|
xaxpy(inactive,q,(*precomp)->submat[i],1,(*precomp)->submat[j],1);
|
|
}
|
|
}
|
|
REAL d=1.;
|
|
for(int i=0; i<inactive; ++i) d *= (*precomp)->submat(i,i);
|
|
(*precomp)->det=d;
|
|
|
|
//cout <<"Pre-eliminated ovelap submatrix\n"<<(*precomp)->submat;
|
|
//cout.flush();
|
|
//cout <<"Elimination coefficients\n"<<(*precomp)->coeffs;
|
|
//cout.flush();
|
|
}
|
|
|
|
|
|
REAL finish_elimination(const part_eliminated *precomp, NRMat<REAL> &a, int inactive)
|
|
{
|
|
//store in the matrix the precomputed part
|
|
a.storesubmatrix(0,0,precomp->submat);
|
|
|
|
//perform the precomputed linear combinations on the right columns
|
|
for(int i=0; i<inactive-1; ++i)
|
|
for(int j=i+1; j<inactive; ++j)
|
|
xaxpy(a.ncols()-inactive,precomp->coeffs(i,j),a[i]+inactive,1,a[j]+inactive,1);
|
|
|
|
|
|
//finish elimination
|
|
//should a pivoting be done for accuracy (in principle an orbital swap could lead to division by a near-zero) ?
|
|
for(int i=0; i<a.nrows()-1; ++i)
|
|
{
|
|
REAL p=a(i,i);
|
|
int jlow= i+1;
|
|
if(inactive > jlow) jlow= inactive;
|
|
for(int j=jlow; j<a.nrows(); ++j)
|
|
{
|
|
REAL q= -a(j,i)/p;
|
|
xaxpy(a.ncols(),q,a[i],1,a[j],1);
|
|
}
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
cout << "eliminated matrix\n"<<a;
|
|
#endif
|
|
|
|
//compute determinant
|
|
REAL d=precomp->det;
|
|
for(int i=inactive; i<a.nrows(); ++i) d *= a(i,i);
|
|
#ifdef DEBUG
|
|
cout << "determinant = "<<d<<endl;
|
|
#endif
|
|
return d;
|
|
}
|
|
|
|
|
|
|
|
void perform_overlap(lexindex i, lexindex j, unsigned long *skipcount,
|
|
bool (&needpermute)[2],int nelec,const slaterbasis &slaters, const NRVec<int> (&bra2ketperm)[2],
|
|
bool permuteslvectors, const NRVec<SPMatindex> &slperm,REAL screeningthr,
|
|
const NRMat<REAL> &brasl,const NRMat<REAL> &ketsl,const NRMat<REAL> (&Smo)[2],int ncore,int mo,
|
|
bool inversedorbitals,unsigned long *computedcount,SparseMat<REAL> &Ssl, int inactive,
|
|
NRMat<int> *screening_mask)
|
|
{
|
|
if(screening_mask)
|
|
{
|
|
if((*screening_mask).nrows() != brasl.ncols() || (*screening_mask).ncols() != ketsl.ncols()) laerror("error in screening_mask dimensions");
|
|
}
|
|
lexindex k;
|
|
if(needpermute[0]||needpermute[1]) //only general need in principle
|
|
{
|
|
bool waspermuted=false;
|
|
slaterdet permuted(nelec);
|
|
for(int e=0; e<nelec; ++e)
|
|
{
|
|
spinorbindex ee=slaters(j,e);
|
|
permuted[e]=bra2ketperm[ee<0?1:0][abs(ee)];
|
|
if(ee<0) permuted[e]= -permuted[e];
|
|
if(permuted[e]!=ee) waspermuted=true;
|
|
}
|
|
|
|
if(waspermuted)
|
|
{
|
|
permuted.sort();
|
|
k=permuted.find(slaters,slaterorder);
|
|
#ifdef DEBUG
|
|
cout << "original determinant "<<slaters.row(j)<<endl;
|
|
cout << "permuted determinant "<<j<<" "<<k<<" : "<<permuted<<endl;
|
|
cout <<" ci size = "<<slaters.nrows()<<endl;
|
|
#endif
|
|
if(k == (lexindex)-1)
|
|
{
|
|
#ifdef DEBUG
|
|
cout <<"WARNING: permuted slater determinant outside CI, computed overlap will be inaccurate\n";
|
|
#endif
|
|
++ (*skipcount);
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
k=j;
|
|
}
|
|
else
|
|
k=j;
|
|
|
|
lexindex itrue = permuteslvectors ? slperm[i] : i ;
|
|
lexindex ktrue = permuteslvectors ? slperm[k] : k ;
|
|
|
|
//implement screening
|
|
if(screeningthr>0)
|
|
{
|
|
REAL s=0.;
|
|
for(int a=0; a<brasl.ncols(); ++a)
|
|
for(int b=0; b<ketsl.ncols(); ++b)
|
|
{
|
|
if(!screening_mask || (*screening_mask)(a,b) )
|
|
{
|
|
s += abs(brasl[itrue][a] * ketsl[ktrue][b]);
|
|
}
|
|
}
|
|
if(s<screeningthr)
|
|
{
|
|
++ (*skipcount);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
{
|
|
|
|
#ifdef oldversion
|
|
NRMat<REAL> overlaps=slatersubmatrix(Smo,nelec,slaters[i],slaters[k],ncore,mo,inversedorbitals);
|
|
REAL dd=determinant_destroy(overlaps);
|
|
#else
|
|
#ifdef DEBUG3
|
|
NRMat<REAL> overlaps=slatersubmatrix(Smo,nelec,slaters[i],slaters[k],ncore,mo,inversedorbitals);
|
|
#ifdef DEBUG2
|
|
if(i==0 && k==13)
|
|
{
|
|
cout <<"Ssl "<<i<<" "<<k<<" (j="<<j<<") : " <<itrue<<" "<<ktrue<<" = "<<endl;
|
|
cout <<"Overlap submatrix original algorithm " <<overlaps<<endl;
|
|
}
|
|
#endif
|
|
REAL ddorig=determinant_destroy(overlaps);
|
|
cout <<"original_result = "<<ddorig<<endl;
|
|
#endif
|
|
|
|
|
|
twooverlaps<NRMat<REAL> > overlaps2 = slatersubmatrices(Smo,nelec,slaters[i],slaters[k],ncore,mo,inversedorbitals);
|
|
|
|
#ifdef DEBUG2
|
|
if(i==0 && k==13)
|
|
{
|
|
cout <<"Ssl "<<i<<" "<<k<<" (j="<<j<<") : " <<itrue<<" "<<ktrue<<" = "<<endl;
|
|
cout <<"i= ";for(int ii=0;ii<nelec;++ii) cout <<slaters[i][ii]<<" "; cout <<endl;
|
|
cout <<"k= ";for(int ii=0;ii<nelec;++ii) cout <<slaters[k][ii]<<" "; cout <<endl;
|
|
cout <<"Overlap submatrices alpha, beta:\n"<<overlaps2.alpha<<endl<<overlaps2.beta<<endl;
|
|
}
|
|
#endif
|
|
REAL da,db;
|
|
if(inactive>1) //simplified computation from a precomputed intermediate for efficiency
|
|
{
|
|
if(!precompa) prepare_elimination(&precompa,overlaps2.alpha,inactive);
|
|
if(!precompb) prepare_elimination(&precompb,overlaps2.beta,inactive);
|
|
da = finish_elimination(precompa,overlaps2.alpha,inactive);
|
|
db = finish_elimination(precompb,overlaps2.beta,inactive);
|
|
}
|
|
else //full determinant computation
|
|
{
|
|
da=determinant_destroy(overlaps2.alpha);
|
|
db=determinant_destroy(overlaps2.beta);
|
|
}
|
|
|
|
REAL dd=da*db; if((overlaps2.rowparity+overlaps2.colparity)&1) dd= -dd;
|
|
#ifdef DEBUG
|
|
cout <<"da db rowparity colparity = "<<da<<" "<<db<<" "<<overlaps2.rowparity<<" "<<overlaps2.colparity<<endl;
|
|
cout <<"full_result = "<<dd<<endl;
|
|
#ifdef DEBUG3
|
|
if(fabs(ddorig-dd) > 1e-13) cout <<"INTERNAL ERROR\n";
|
|
#endif
|
|
#endif
|
|
#endif //oldversion
|
|
++ *computedcount;
|
|
Ssl.add(itrue,ktrue,dd);
|
|
}
|
|
}
|
|
|
|
|
|
//largest condition number of the overlap block of the frozen core and common orbitals for which they are eliminated in -m 1
|
|
#ifndef STRINGS_MAXCOND
|
|
#define STRINGS_MAXCOND 1e6
|
|
#endif
|
|
|
|
//factors of one spin: determinants of the MO overlaps of pairs of strings (bra string, ket string)
|
|
//the frozen core orbitals and the orbitals common to all strings are eliminated by the pivot transform,
|
|
//so that only determinants of the dimension of the remaining orbitals of a string are computed
|
|
//(unless their overlap block is ill-conditioned, see STRINGS_MAXCOND)
|
|
struct stringfactors
|
|
{
|
|
NRMat<REAL> S; //MO overlaps after the pivot transform with respect to the eliminated orbitals
|
|
REAL det0; //determinant of the overlap block of the eliminated orbitals
|
|
NRMat<int> rest; //the orbitals of each string which are not eliminated (no columns if there are none)
|
|
NRVec<int> sign; //parity of moving the eliminated orbitals of each string in front of the others
|
|
stringfactors(const NRMat<REAL> &Smo, const NRMat<spinorbindex> &strings, const int ncore);
|
|
void block(const int *rows, const int nrows, const int *cols, const int ncols, NRMat<REAL> &f) const; //f(i,j) for the strings rows[i] and cols[j]
|
|
};
|
|
|
|
stringfactors::stringfactors(const NRMat<REAL> &Smo, const NRMat<spinorbindex> &strings, const int ncore)
|
|
{
|
|
const int nstr=strings.nrows();
|
|
const int nel=strings.ncols();
|
|
const int norb=Smo.nrows();
|
|
|
|
//orbitals common to all strings (orbital number o in a string is the MO number o-1, the frozen core MOs come first)
|
|
NRVec<int> count(0,norb);
|
|
for(int i=0; i<nstr; ++i) for(int e=0; e<nel; ++e)
|
|
{
|
|
if(strings(i,e)<=ncore || strings(i,e)>norb) laerror("orbital number in slaterfile out of range");
|
|
++count[strings(i,e)-1];
|
|
}
|
|
int ncommon=ncore;
|
|
for(int o=ncore; o<norb; ++o) if(count[o]==nstr && nstr>0) ++ncommon;
|
|
NRVec<int> common(ncommon);
|
|
for(int o=0; o<ncore; ++o) common[o]=o;
|
|
{
|
|
int j=ncore;
|
|
for(int o=ncore; o<norb; ++o) if(count[o]==nstr && nstr>0) common[j++]=o;
|
|
}
|
|
det0=1.;
|
|
const int nrest=nel-(ncommon-ncore);
|
|
if(nstr==0 || nrest==0) //a single string made of common orbitals only: its factor is the determinant of their overlaps
|
|
{
|
|
if(ncommon) det0=determinant(Smo.submatrix(common,common));
|
|
return;
|
|
}
|
|
sign.resize(nstr);
|
|
if(ncommon)
|
|
{
|
|
//the elimination needs a well-conditioned overlap block of the eliminated orbitals (it is not, if one of them
|
|
//changed its character between the geometries); otherwise they are left in the determinants, in front of the others
|
|
const REAL cond=CondNumber(Smo.submatrix(common,common),'1');
|
|
if(!(cond<=STRINGS_MAXCOND))
|
|
{
|
|
char buf[32];
|
|
sprintf(buf,"%.1e",cond);
|
|
cout <<"WARNING: -m 1: the overlap block of the frozen core orbitals and of the orbitals common to all strings of one spin is ill-conditioned (condition number "<<buf<<"), they are not eliminated from the determinants"<<endl;
|
|
S=Smo;
|
|
rest.resize(nstr,ncore+nel);
|
|
for(int i=0; i<nstr; ++i)
|
|
{
|
|
for(int o=0; o<ncore; ++o) rest(i,o)=o;
|
|
for(int e=0; e<nel; ++e) rest(i,ncore+e)=strings(i,e)-1;
|
|
sign[i]=1;
|
|
}
|
|
return;
|
|
}
|
|
S=pivot_transform(Smo,common,common,&det0);
|
|
}
|
|
else S=Smo;
|
|
|
|
//the remaining orbitals of each string and the parity of moving the common ones in front of them
|
|
rest.resize(nstr,nrest);
|
|
for(int i=0; i<nstr; ++i)
|
|
{
|
|
int n=0;
|
|
int parity=0;
|
|
for(int e=0; e<nel; ++e)
|
|
{
|
|
const int o=strings(i,e)-1;
|
|
if(count[o]==nstr) parity+=n; else rest(i,n++)=o;
|
|
}
|
|
sign[i]= parity&1? -1:1;
|
|
}
|
|
}
|
|
|
|
//consecutive ket strings cols[] have the same leading orbitals, since the distinct strings are sorted (the result does not depend
|
|
//on it, only the time): the determinant is therefore eliminated column by column with pivoting over the rows, which are not moved,
|
|
//so that the eliminated leading columns are kept for the next ket string as far as its orbitals are the same
|
|
//the last column is not eliminated at all, the determinant being linear in it:
|
|
//det = sum_k y[k] S(orbital k of the bra string, last orbital of the ket string), where y is computed once for the leading columns
|
|
void stringfactors::block(const int *rows, const int nrows, const int *cols, const int ncols, NRMat<REAL> &f) const
|
|
{
|
|
const int n=rest.ncols();
|
|
f.resize(nrows,ncols);
|
|
if(n==0) //a single string made of the eliminated orbitals only
|
|
{
|
|
for(int i=0; i<nrows; ++i) for(int j=0; j<ncols; ++j) f(i,j)=det0;
|
|
return;
|
|
}
|
|
const int norb=S.nrows();
|
|
NRMat<REAL> c(norb,n); //c(o,k) = S(orbital k of the bra string, o): the possible columns of the determinant, contiguous
|
|
NRMat<REAL> L(n,n); //L(r,m) multiplier of the row r in the step m
|
|
NRVec<REAL> x(n),t(n),y(n);
|
|
NRVec<REAL> detp(n+1); //determinant of the leading columns including the sign of the pivoting, zero if they are singular
|
|
NRVec<int> piv(n); //pivot row of each step
|
|
NRMat<int> rem(n+1,n); //rows not yet used as pivots before each step, in increasing order
|
|
NRVec<int> prev(n); //orbitals of the eliminated columns
|
|
for(int i=0; i<nrows; ++i)
|
|
{
|
|
const int *ri=rest[rows[i]];
|
|
const REAL si=sign[rows[i]]*det0;
|
|
REAL *fi=f[i];
|
|
for(int k=0; k<n; ++k)
|
|
{
|
|
const REAL *s=S[ri[k]];
|
|
for(int o=0; o<norb; ++o) c(o,k)=s[o];
|
|
}
|
|
int valid= -1; //number of leading columns eliminated for this bra string (-1: not even y is available)
|
|
detp[0]=1.;
|
|
for(int k=0; k<n; ++k) rem(0,k)=k;
|
|
for(int j=0; j<ncols; ++j)
|
|
{
|
|
const int *rj=rest[cols[j]];
|
|
int same=0;
|
|
while(same<valid && prev[same]==rj[same]) ++same;
|
|
if(same<n-1 || valid<0)
|
|
{
|
|
for(int l=same; l<n-1; ++l)
|
|
{
|
|
prev[l]=rj[l];
|
|
if(detp[l]==0.) {detp[l+1]=0.; continue;}
|
|
//the column after the previous steps: first its values t in the pivot rows, then the remaining rows
|
|
const REAL *co=c[rj[l]];
|
|
for(int m=0; m<l; ++m)
|
|
{
|
|
const REAL *lm=L[piv[m]];
|
|
REAL s=co[piv[m]];
|
|
for(int q=0; q<m; ++q) s-=lm[q]*t[q];
|
|
t[m]=s;
|
|
}
|
|
const int *rl=rem[l];
|
|
for(int q=0; q<n-l; ++q)
|
|
{
|
|
const REAL *lr=L[rl[q]];
|
|
REAL s=co[rl[q]];
|
|
for(int m=0; m<l; ++m) s-=lr[m]*t[m];
|
|
x[rl[q]]=s;
|
|
}
|
|
//pivot
|
|
int p=rl[0];
|
|
int parity=0;
|
|
REAL big=abs(x[p]);
|
|
for(int q=1; q<n-l; ++q) if(abs(x[rl[q]])>big) {big=abs(x[rl[q]]); p=rl[q]; parity=q;}
|
|
if(!(big>=LA_precision<REAL>::det_tiny())) {detp[l+1]=0.; continue;} //singular, as in determinant() of LA
|
|
piv[l]=p;
|
|
detp[l+1]= parity&1 ? -detp[l]*x[p] : detp[l]*x[p];
|
|
const REAL r=1./x[p];
|
|
int *rn=rem[l+1];
|
|
for(int q=0; q<n-l; ++q) if(rl[q]!=p)
|
|
{
|
|
*rn++=rl[q];
|
|
L(rl[q],l)=x[rl[q]]*r;
|
|
}
|
|
}
|
|
valid=n-1;
|
|
if(detp[n-1]!=0.) //the value left in the last row after the elimination of a column x is sum_k y[k] x[k]
|
|
{
|
|
const int last=rem(n-1,0);
|
|
y.clear();
|
|
for(int m=0; m<n-1; ++m) t[m]= -L(last,m);
|
|
for(int m=n-2; m>=0; --m)
|
|
{
|
|
const REAL *lm=L[piv[m]];
|
|
y[piv[m]]=t[m];
|
|
for(int q=0; q<m; ++q) t[q]-=lm[q]*t[m];
|
|
}
|
|
y[last]=1.;
|
|
y*=detp[n-1];
|
|
}
|
|
}
|
|
if(detp[n-1]==0.) fi[j]=0.;
|
|
else
|
|
{
|
|
const REAL *co=c[rj[n-1]];
|
|
REAL s=0.;
|
|
for(int k=0; k<n; ++k) s+=y[k]*co[k];
|
|
fi[j]=si*sign[cols[j]]*s;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//slaterstrings() with its result kept in slaterstringsfile: the strings depend only on the Slater basis, which stays the same
|
|
//along a trajectory, so that they are computed in the first step only (like excitlistfile of -m 0)
|
|
//the file starts with the number of determinants, the number of electrons and the hash of the Slater basis (hash() of LA);
|
|
//a file which does not belong to the present Slater basis or is unfinished is replaced with a warning
|
|
static NRVec<slater_in_strings> cachedslaterstrings(const slaterbasis &slaters, NRMat<spinorbindex> (&strings)[2])
|
|
{
|
|
const lexindex sl=slaters.nrows();
|
|
const int nelec=slaters.ncols();
|
|
const unsigned long long header[3]={sl,(unsigned long long)nelec,slaters.hash()};
|
|
int f=open("slaterstringsfile",O_CREAT|O_RDWR|O_LARGEFILE,0777); if(f<0) {perror("cannot open slaterstringsfile");laerror("IO error");}
|
|
NRVec<slater_in_strings> ds;
|
|
unsigned long long h[3];
|
|
const ssize_t r=read(f,h,sizeof(h));
|
|
if(r==(ssize_t)sizeof(h) && h[0]==header[0] && h[1]==header[1] && h[2]==header[2])
|
|
{
|
|
strings[0].get(f,true);
|
|
strings[1].get(f,true);
|
|
ds.get(f,true);
|
|
cout <<"Strings of the determinants read from slaterstringsfile"<<endl;
|
|
}
|
|
else
|
|
{
|
|
if(r!=0) cout <<"WARNING: slaterstringsfile does not belong to the present slaterfile or is unfinished, it is replaced"<<endl;
|
|
ds=slaterstrings(slaters,strings);
|
|
//the header is written as the last item, an interrupted write thus leaves a file which is recognized as unfinished
|
|
h[0]=h[1]=h[2]=0;
|
|
if(lseek(f,0,SEEK_SET)!=0 || ftruncate(f,0) || write(f,h,sizeof(h))!=(ssize_t)sizeof(h)) laerror("write error in slaterstringsfile");
|
|
strings[0].put(f,true);
|
|
strings[1].put(f,true);
|
|
ds.put(f,true);
|
|
if(lseek(f,0,SEEK_SET)!=0 || write(f,header,sizeof(header))!=(ssize_t)sizeof(header)) laerror("write error in slaterstringsfile");
|
|
cout <<"Strings of the determinants stored in slaterstringsfile"<<endl;
|
|
}
|
|
close(f);
|
|
return ds;
|
|
}
|
|
|
|
|
|
//classes of strings: two strings of one spin belong to the same class if the determinants combine them with the same set of strings
|
|
//of the other spin, so that the determinants of two classes (alpha, beta) form either a complete block or none
|
|
//(e.g. the strings with 0, 1 and 2 electrons in the external orbitals of MR-CISD)
|
|
//the sets are compared by a checksum only; this is safe, since the blocks are treated as dense matrices padded by zeros,
|
|
//so that classes which are not exact could only cost time and memory
|
|
//cls[spin][string] is the class of the string, ncls[spin] the number of classes
|
|
static void stringclasses(const NRVec<slater_in_strings> &ds, const int nstr[2], NRVec<int> (&cls)[2], int ncls[2])
|
|
{
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
NRVec<unsigned long long> h((unsigned long long)0,nstr[spin]);
|
|
NRVec<int> count(0,nstr[spin]);
|
|
for(lexindex k=0; k<(lexindex)ds.size(); ++k)
|
|
{
|
|
h[ds[k].string[spin]]+=hash_mix(ds[k].string[1-spin],0); //the sum is independent of the order of the determinants
|
|
++count[ds[k].string[spin]];
|
|
}
|
|
std::map<std::pair<unsigned long long,int>,int> classes;
|
|
cls[spin].resize(nstr[spin]);
|
|
for(int i=0; i<nstr[spin]; ++i)
|
|
{
|
|
const std::pair<unsigned long long,int> key(h[i],count[i]);
|
|
std::map<std::pair<unsigned long long,int>,int>::const_iterator c=classes.find(key);
|
|
if(c==classes.end())
|
|
{
|
|
cls[spin][i]=classes.size();
|
|
classes[key]=cls[spin][i];
|
|
}
|
|
else cls[spin][i]=c->second;
|
|
}
|
|
ncls[spin]=classes.size();
|
|
}
|
|
}
|
|
|
|
|
|
//largest number of classes of strings of one spin for which the contraction by blocks is used
|
|
#ifndef STRINGS_MAXCLASSES
|
|
#define STRINGS_MAXCLASSES 64
|
|
#endif
|
|
//largest number of elements of the matrices processed at once in the contraction by blocks (a batch of rows of a block of factors)
|
|
#ifndef STRINGS_BATCH
|
|
#define STRINGS_BATCH 4000000
|
|
#endif
|
|
|
|
//for the bra block (ca,cb) and the ket block (ka,kb) with the numbers of strings nca,ncb,nka,nkb: are the products
|
|
//Fa(ca,ka) D'(ka,kb) and D(ca,cb) Fb(cb,kb) cheaper than D(ca,cb)^T Fa(ca,ka) and Fb(cb,kb) D'(ka,kb)^T?
|
|
static inline bool ketalphafirst(const REAL nca, const REAL ncb, const REAL nka, const REAL nkb)
|
|
{
|
|
return nca*nkb*(nka+ncb) <= nka*ncb*(nkb+nca);
|
|
}
|
|
|
|
//overlap of the CI vectors by means of the factors for pairs of alpha and beta strings (-m 1), any wave function
|
|
//S_IJ = sum_{a,b,a',b'} D_I(a,b) Fa(a,a') D'_J(a',b') Fb(b,b'), where D(a,b) is the CI coefficient (with the sign) of the determinant
|
|
//made of the alpha string a and the beta string b, or zero if there is no such determinant
|
|
//the strings are divided into classes (see stringclasses), so that D consists of dense blocks; for a block (ca,cb) of the bras and
|
|
//a block (ka,kb) of the kets the contribution is the scalar product either of Fa(ca,ka) D'(ka,kb) with D(ca,cb) Fb(cb,kb)
|
|
//or of D(ca,cb)^T Fa(ca,ka) with Fb(cb,kb) D'(ka,kb)^T, whichever needs less operations
|
|
//each block of the factors thus occurs only in products with the coefficients: it is computed once, in batches of rows, and not stored
|
|
//if the determinants have no such structure (too many classes), the tables of the factors of all pairs of strings are stored and
|
|
//contracted determinant by determinant: W_J(b,a') = sum_{l with alpha string a'} Fb(b,b_l) D'_J(l), S_IJ = sum_k D_I(k) sum_a' Fa(a_k,a') W_J(b_k,a')
|
|
static NRMat<REAL> CIoverlap_strings(const NRMat<REAL> &brasl, const NRMat<REAL> &ketsl, const NRMat<REAL> (&Smo)[2], const slaterbasis &slaters, const int ncore, const NRVec<SPMatindex> *slperm)
|
|
{
|
|
const lexindex sl=slaters.nrows();
|
|
const int nbra=brasl.ncols();
|
|
const int nket=ketsl.ncols();
|
|
clock_t t0=clock();
|
|
clock_t tfactors=0;
|
|
NRMat<spinorbindex> strings[2];
|
|
const NRVec<slater_in_strings> ds=cachedslaterstrings(slaters,strings);
|
|
const int nstr[2]={strings[0].nrows()?strings[0].nrows():1, strings[1].nrows()?strings[1].nrows():1}; //a spin without electrons has one empty string
|
|
const stringfactors Fa(Smo[0],strings[0],ncore), Fb(Smo[1],strings[1],ncore);
|
|
cout <<"Number of determinants in the wavefunctions = "<<sl<<endl;
|
|
cout <<"Number of distinct alpha and beta strings = "<<nstr[0]<<" "<<nstr[1]<<endl;
|
|
|
|
//classes of the strings, their members and the blocks of determinants
|
|
NRVec<int> cls[2],pos[2];
|
|
NRVec<NRVec<int> > members[2];
|
|
int ncls[2];
|
|
stringclasses(ds,nstr,cls,ncls);
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
NRVec<int> n(0,ncls[spin]);
|
|
for(int i=0; i<nstr[spin]; ++i) ++n[cls[spin][i]];
|
|
members[spin].resize(ncls[spin]);
|
|
for(int c=0; c<ncls[spin]; ++c) members[spin][c].resize(n[c]);
|
|
n.clear();
|
|
pos[spin].resize(nstr[spin]);
|
|
for(int i=0; i<nstr[spin]; ++i)
|
|
{
|
|
const int c=cls[spin][i];
|
|
pos[spin][i]=n[c];
|
|
members[spin][c][n[c]++]=i;
|
|
}
|
|
}
|
|
NRMat<int> isblock(ncls[0],ncls[1]);
|
|
isblock.clear();
|
|
for(lexindex k=0; k<sl; ++k) isblock(cls[0][ds[k].string[0]],cls[1][ds[k].string[1]])=1;
|
|
int nblocks=0;
|
|
REAL blocksize=0;
|
|
for(int ca=0; ca<ncls[0]; ++ca) for(int cb=0; cb<ncls[1]; ++cb) if(isblock(ca,cb))
|
|
{
|
|
++nblocks;
|
|
blocksize+=(REAL)members[0][ca].size()*members[1][cb].size();
|
|
}
|
|
|
|
if(ncls[0]>STRINGS_MAXCLASSES || ncls[1]>STRINGS_MAXCLASSES || blocksize>2.*sl) //no block structure
|
|
{
|
|
cout <<"The determinants do not consist of a few blocks of classes of strings, the tables of factors for all pairs of strings are used"<<endl;
|
|
NRMat<REAL> Fat,Fbt;
|
|
{
|
|
NRVec<int> all(nstr[0]>nstr[1]?nstr[0]:nstr[1]);
|
|
for(int i=0; i<all.size(); ++i) all[i]=i;
|
|
Fa.block(&all[0],nstr[0],&all[0],nstr[0],Fat);
|
|
Fb.block(&all[0],nstr[1],&all[0],nstr[1],Fbt);
|
|
}
|
|
Fbt.transposeme(); //rows are needed contiguous for the ket strings
|
|
const int na=nstr[0];
|
|
const int nb=nstr[1];
|
|
cout <<"string factors CPU time "<<(1.*clock()-t0)/CLOCKS_PER_SEC<<endl;
|
|
t0=clock();
|
|
|
|
NRMat<REAL> result(nbra,nket);
|
|
NRMat<REAL> Wt(na,nb); //transposed W is accumulated to access the memory contiguously
|
|
NRVec<REAL> x(sl); //in the order of the CI coefficients
|
|
for(int J=0; J<nket; ++J)
|
|
{
|
|
Wt.clear();
|
|
for(lexindex l=0; l<sl; ++l)
|
|
{
|
|
const REAL c=ds[l].sign*ketsl(slperm?(*slperm)[l]:l,J);
|
|
if(c!=0.) xaxpy(nb,c,Fbt[ds[l].string[1]],1,Wt[ds[l].string[0]],1);
|
|
}
|
|
const NRMat<REAL> W=Wt.transpose();
|
|
for(lexindex k=0; k<sl; ++k) x[slperm?(*slperm)[k]:k] = ds[k].sign*xdot(na,Fat[ds[k].string[0]],1,W[ds[k].string[1]],1);
|
|
NRVec<REAL> s(nbra);
|
|
s.gemv(0.,brasl,'t',1.,x);
|
|
for(int I=0; I<nbra; ++I) result(I,J)=s[I];
|
|
}
|
|
cout <<"contraction CPU time "<<(1.*clock()-t0)/CLOCKS_PER_SEC<<endl;
|
|
return result;
|
|
}
|
|
cout <<"Number of classes of alpha and beta strings = "<<ncls[0]<<" "<<ncls[1]<<", blocks of determinants = "<<nblocks<<endl;
|
|
|
|
//the coefficients with the signs in the blocks: D[0] bras, D[1] kets, index (block, state)
|
|
#define BLOCK(ca,cb) ((ca)*ncls[1]+(cb))
|
|
NRVec<NRMat<REAL> > D[2];
|
|
for(int bk=0; bk<2; ++bk)
|
|
{
|
|
const NRMat<REAL> &v= bk?ketsl:brasl;
|
|
const int n=v.ncols();
|
|
D[bk].resize(ncls[0]*ncls[1]*n);
|
|
for(int ca=0; ca<ncls[0]; ++ca) for(int cb=0; cb<ncls[1]; ++cb) if(isblock(ca,cb)) for(int I=0; I<n; ++I)
|
|
{
|
|
D[bk][BLOCK(ca,cb)*n+I].resize(members[0][ca].size(),members[1][cb].size());
|
|
D[bk][BLOCK(ca,cb)*n+I].clear();
|
|
}
|
|
for(lexindex k=0; k<sl; ++k)
|
|
{
|
|
const lexindex kk= slperm?(*slperm)[k]:k;
|
|
const int a=ds[k].string[0];
|
|
const int b=ds[k].string[1];
|
|
for(int I=0; I<n; ++I) D[bk][BLOCK(cls[0][a],cls[1][b])*n+I](pos[0][a],pos[1][b]) += ds[k].sign*v(kk,I);
|
|
}
|
|
}
|
|
|
|
//products of the beta factors with the coefficients, stored:
|
|
//V(ca,cb,kb) = D(ca,cb) Fb(cb,kb) for the bras, Ht(ka,kb,cb) = Fb(cb,kb) D'(ka,kb)^T for the kets
|
|
#define VINDEX(ca,cb,kb,I) (((BLOCK(ca,cb))*ncls[1]+(kb))*nbra+(I))
|
|
#define HINDEX(ka,kb,cb,J) (((BLOCK(ka,kb))*ncls[1]+(cb))*nket+(J))
|
|
#define NA(c) members[0][c].size()
|
|
#define NB(c) members[1][c].size()
|
|
NRVec<NRMat<REAL> > V(ncls[0]*ncls[1]*ncls[1]*nbra), Ht(ncls[0]*ncls[1]*ncls[1]*nket);
|
|
NRMat<REAL> f;
|
|
for(int cb=0; cb<ncls[1]; ++cb) for(int kb=0; kb<ncls[1]; ++kb)
|
|
{
|
|
NRVec<int> needv(0,ncls[0]), needh(0,ncls[0]);
|
|
int maxdim=NB(kb);
|
|
bool needed=false;
|
|
for(int ca=0; ca<ncls[0]; ++ca) if(isblock(ca,cb)) for(int ka=0; ka<ncls[0]; ++ka) if(isblock(ka,kb))
|
|
{
|
|
needed=true;
|
|
if(ketalphafirst(NA(ca),NB(cb),NA(ka),NB(kb))) {needv[ca]=1; if(NA(ca)>maxdim) maxdim=NA(ca);}
|
|
else {needh[ka]=1; if(NA(ka)>maxdim) maxdim=NA(ka);}
|
|
}
|
|
for(int ca=0; ca<ncls[0]; ++ca) if(needv[ca]) for(int I=0; I<nbra; ++I)
|
|
{
|
|
V[VINDEX(ca,cb,kb,I)].resize(NA(ca),NB(kb));
|
|
V[VINDEX(ca,cb,kb,I)].clear();
|
|
}
|
|
if(!needed) continue;
|
|
for(int ka=0; ka<ncls[0]; ++ka) if(needh[ka]) for(int J=0; J<nket; ++J) Ht[HINDEX(ka,kb,cb,J)].resize(NB(cb),NA(ka));
|
|
const NRVec<int> &rows=members[1][cb];
|
|
const NRVec<int> &cols=members[1][kb];
|
|
const int batch= STRINGS_BATCH/maxdim>0 ? STRINGS_BATCH/maxdim : 1;
|
|
for(int r0=0; r0<rows.size(); r0+=batch)
|
|
{
|
|
const int nr= rows.size()-r0<batch ? rows.size()-r0 : batch;
|
|
clock_t t1=clock();
|
|
Fb.block(&rows[r0],nr,&cols[0],cols.size(),f);
|
|
tfactors+=clock()-t1;
|
|
for(int ca=0; ca<ncls[0]; ++ca) if(needv[ca]) for(int I=0; I<nbra; ++I)
|
|
{
|
|
const NRMat<REAL> d=D[0][BLOCK(ca,cb)*nbra+I].submatrix(0,NA(ca)-1,r0,r0+nr-1);
|
|
V[VINDEX(ca,cb,kb,I)].gemm(1.,d,'n',f,'n',1.);
|
|
}
|
|
for(int ka=0; ka<ncls[0]; ++ka) if(needh[ka]) for(int J=0; J<nket; ++J)
|
|
{
|
|
NRMat<REAL> h(nr,NA(ka));
|
|
h.gemm(0.,f,'n',D[1][BLOCK(ka,kb)*nket+J],'t',1.);
|
|
Ht[HINDEX(ka,kb,cb,J)].storesubmatrix(r0,0,h);
|
|
}
|
|
}
|
|
}
|
|
|
|
//products of the alpha factors with the coefficients and the scalar products with the stored ones:
|
|
//T(ca,ka,kb) = Fa(ca,ka) D'(ka,kb) with V(ca,cb,kb), Et(ca,ka,cb) = D(ca,cb)^T Fa(ca,ka) with Ht(ka,kb,cb)
|
|
NRMat<REAL> result(nbra,nket);
|
|
result.clear();
|
|
for(int ca=0; ca<ncls[0]; ++ca) for(int ka=0; ka<ncls[0]; ++ka)
|
|
{
|
|
NRVec<int> needt(0,ncls[1]), neede(0,ncls[1]);
|
|
int maxdim=NA(ka);
|
|
bool needed=false;
|
|
for(int cb=0; cb<ncls[1]; ++cb) if(isblock(ca,cb)) for(int kb=0; kb<ncls[1]; ++kb) if(isblock(ka,kb))
|
|
{
|
|
needed=true;
|
|
if(ketalphafirst(NA(ca),NB(cb),NA(ka),NB(kb))) {needt[kb]=1; if(NB(kb)>maxdim) maxdim=NB(kb);}
|
|
else {neede[cb]=1; if(NB(cb)>maxdim) maxdim=NB(cb);}
|
|
}
|
|
if(!needed) continue;
|
|
NRVec<NRMat<REAL> > Et(ncls[1]*nbra);
|
|
for(int cb=0; cb<ncls[1]; ++cb) if(neede[cb]) for(int I=0; I<nbra; ++I)
|
|
{
|
|
Et[cb*nbra+I].resize(NB(cb),NA(ka));
|
|
Et[cb*nbra+I].clear();
|
|
}
|
|
const NRVec<int> &rows=members[0][ca];
|
|
const NRVec<int> &cols=members[0][ka];
|
|
const int batch= STRINGS_BATCH/maxdim>0 ? STRINGS_BATCH/maxdim : 1;
|
|
for(int r0=0; r0<rows.size(); r0+=batch)
|
|
{
|
|
const int nr= rows.size()-r0<batch ? rows.size()-r0 : batch;
|
|
clock_t t1=clock();
|
|
Fa.block(&rows[r0],nr,&cols[0],cols.size(),f);
|
|
tfactors+=clock()-t1;
|
|
for(int kb=0; kb<ncls[1]; ++kb) if(needt[kb]) for(int J=0; J<nket; ++J)
|
|
{
|
|
NRMat<REAL> t(nr,NB(kb));
|
|
t.gemm(0.,f,'n',D[1][BLOCK(ka,kb)*nket+J],'n',1.);
|
|
for(int cb=0; cb<ncls[1]; ++cb) if(isblock(ca,cb) && ketalphafirst(NA(ca),NB(cb),NA(ka),NB(kb))) for(int I=0; I<nbra; ++I)
|
|
{
|
|
const NRMat<REAL> &v=V[VINDEX(ca,cb,kb,I)];
|
|
result(I,J) += xdot(nr*NB(kb),t[0],1,v[r0],1);
|
|
}
|
|
}
|
|
for(int cb=0; cb<ncls[1]; ++cb) if(neede[cb]) for(int I=0; I<nbra; ++I)
|
|
{
|
|
const NRMat<REAL> d=D[0][BLOCK(ca,cb)*nbra+I].submatrix(r0,r0+nr-1,0,NB(cb)-1);
|
|
Et[cb*nbra+I].gemm(1.,d,'t',f,'n',1.);
|
|
}
|
|
}
|
|
for(int cb=0; cb<ncls[1]; ++cb) if(neede[cb]) for(int kb=0; kb<ncls[1]; ++kb) if(isblock(ka,kb) && !ketalphafirst(NA(ca),NB(cb),NA(ka),NB(kb)))
|
|
for(int I=0; I<nbra; ++I) for(int J=0; J<nket; ++J)
|
|
{
|
|
const NRMat<REAL> &e=Et[cb*nbra+I];
|
|
const NRMat<REAL> &h=Ht[HINDEX(ka,kb,cb,J)];
|
|
for(int r=0; r<NB(cb); ++r) result(I,J) += xdot(NA(ka),e[r],1,h[r],1);
|
|
}
|
|
}
|
|
#undef BLOCK
|
|
#undef VINDEX
|
|
#undef HINDEX
|
|
#undef NA
|
|
#undef NB
|
|
cout <<"string factors CPU time "<<(1.*tfactors)/CLOCKS_PER_SEC<<endl;
|
|
cout <<"contraction CPU time "<<(1.*clock()-t0-tfactors)/CLOCKS_PER_SEC<<endl;
|
|
return result;
|
|
}
|
|
|
|
|
|
//largest condition number of the overlap block of the reference orbitals accepted by -m 2
|
|
//(when only one spin block is ill-conditioned, the error of the closed formulas is about 2e-16 times the condition number)
|
|
#ifndef CIS_MAXCOND
|
|
#define CIS_MAXCOND 1e6
|
|
#endif
|
|
|
|
//for -m 2: describe the strings of one spin as a set of reference orbitals with at most one hole and at most one particle
|
|
//kind 0: the reference are the most occupied orbitals (the strings are single replacements), 1: the orbitals common to all strings
|
|
//(one particle added), -1: all orbitals which occur (one hole); the frozen core orbitals always belong to the reference
|
|
//weight: number of determinants with each string; ref, virt: MO numbers (from 0) of the reference and of the other orbitals
|
|
//hp(i,0), hp(i,1): position of the hole in ref and of the particle in virt for string i (-1 if none)
|
|
//hp(i,2): parity of the sorted string with respect to the hole moved behind the reference orbitals and the particle appended
|
|
//(for a replacement the same as the particle put in the place of the hole)
|
|
//returns false if the strings are not of this form
|
|
static bool holesparticles(const NRMat<spinorbindex> &strings, const NRVec<int> &weight, const int kind, const int ncore, const int norb, NRVec<int> &ref, NRVec<int> &virt, NRMat<int> &hp)
|
|
{
|
|
const int nstr=strings.nrows();
|
|
const int nel=strings.ncols();
|
|
NRVec<int> count(0,norb), common(0,norb);
|
|
for(int i=0; i<nstr; ++i) for(int e=0; e<nel; ++e)
|
|
{
|
|
const int o=strings(i,e)-1;
|
|
if(o<ncore || o>=norb) laerror("orbital number in slaterfile out of range");
|
|
count[o]+=weight[i];
|
|
++common[o];
|
|
}
|
|
NRVec<char> isref((char)0,norb);
|
|
int nref=0;
|
|
if(kind==0) //the nel most occupied orbitals, the lower one of equally occupied
|
|
{
|
|
if(nel>norb-ncore) laerror("more electrons than orbitals");
|
|
for(int e=0; e<nel; ++e)
|
|
{
|
|
int best= -1;
|
|
for(int o=ncore; o<norb; ++o) if(!isref[o] && (best<0 || count[o]>count[best])) best=o;
|
|
isref[best]=1;
|
|
++nref;
|
|
}
|
|
}
|
|
else for(int o=ncore; o<norb; ++o) if(kind>0 ? (nstr>0 && common[o]==nstr) : common[o]>0) {isref[o]=1; ++nref;}
|
|
if(nref!=nel-kind) return false;
|
|
|
|
ref.resize(ncore+nref);
|
|
virt.resize(norb-ncore-nref);
|
|
NRVec<int> pos(norb); //position in ref or in virt
|
|
{
|
|
int r=0,v=0;
|
|
for(int o=0; o<norb; ++o) if(o<ncore||isref[o]) {pos[o]=r; ref[r++]=o;} else {pos[o]=v; virt[v++]=o;}
|
|
}
|
|
hp.resize(nstr?nstr:1,3);
|
|
if(nstr==0) {hp(0,0)= -1; hp(0,1)= -1; hp(0,2)=1; return true;} //the empty string
|
|
|
|
NRVec<char> occ((char)0,norb);
|
|
for(int i=0; i<nstr; ++i)
|
|
{
|
|
int nh=0,np=0,parity=0;
|
|
hp(i,0)= -1;
|
|
hp(i,1)= -1;
|
|
for(int e=0; e<nel; ++e) occ[strings(i,e)-1]=1;
|
|
for(int r=ncore; r<ref.size(); ++r) if(!occ[ref[r]]) {++nh; hp(i,0)=r; parity+=ref.size()-1-r;} //reference orbitals above the hole
|
|
for(int e=0; e<nel; ++e)
|
|
{
|
|
const int o=strings(i,e)-1;
|
|
occ[o]=0;
|
|
if(!isref[o]) {++np; hp(i,1)=pos[o]; parity+=nel-1-e;} //reference orbitals of the string above the particle
|
|
}
|
|
if(nh>1||np>1) return false;
|
|
hp(i,2)= parity&1? -1:1;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
//overlap of CIS-type wave functions and of spin-flip wave functions by closed formulas (-m 2)
|
|
//with the pivot transform of the MO overlaps with respect to the reference orbitals o (v are the other orbitals),
|
|
//M=s_oo^-1, X=s_vo M, Y=M s_ov, Sigma=s_vv-s_vo M s_ov, D=det s_oo, the determinant of the overlaps of two strings is
|
|
//D for reference-reference, D X_vi for (i->v)-reference, D Y_jw for reference-(j->w), D (X_vi Y_jw + M_ji Sigma_vw) for (i->v)-(j->w),
|
|
//D Sigma_vw for strings with a particle v, w added and D M_ji for strings with a hole i, j (signs in holesparticles)
|
|
//CIS: S_IJ = Da Db [(c0_I + x_I)(c0'_J + y_J) + sum_spin Tr(C_I^T M^T C'_J Sigma^T)], x_I = sum_spin Tr(C_I X), y_J = sum_spin Tr(C'_J^T Y)
|
|
//spin-flip: S_IJ = Dp Dh Tr(C_I^T M_h^T C'_J Sigma_p^T), h is the spin with the hole and p the spin with the particle
|
|
//if the determinants are not of these kinds or s_oo is ill-conditioned, nothing is computed and method is changed to 1
|
|
//for the caller to use that algorithm
|
|
static NRMat<REAL> CIoverlap_cis(const NRMat<REAL> &brasl, const NRMat<REAL> &ketsl, const NRMat<REAL> (&Smo)[2], const slaterbasis &slaters, const int ncore, const NRVec<SPMatindex> *slperm, int &method)
|
|
{
|
|
const lexindex sl=slaters.nrows();
|
|
const int norb=Smo[0].nrows();
|
|
clock_t t0=clock();
|
|
NRMat<spinorbindex> strings[2];
|
|
const NRVec<slater_in_strings> ds=cachedslaterstrings(slaters,strings);
|
|
NRVec<int> weight[2];
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
weight[spin].resize(strings[spin].nrows()?strings[spin].nrows():1);
|
|
weight[spin].clear();
|
|
for(lexindex k=0; k<sl; ++k) ++weight[spin][ds[k].string[spin]];
|
|
}
|
|
|
|
//recognize the kind of the wave functions
|
|
NRVec<int> ref[2],virt[2];
|
|
NRMat<int> hp[2];
|
|
int kind[2]={0,0};
|
|
bool ok=holesparticles(strings[0],weight[0],0,ncore,norb,ref[0],virt[0],hp[0]) && holesparticles(strings[1],weight[1],0,ncore,norb,ref[1],virt[1],hp[1]);
|
|
if(ok) for(lexindex k=0; k<sl; ++k) if(hp[0](ds[k].string[0],0)>=0 && hp[1](ds[k].string[1],0)>=0) {ok=false; break;} //both spins excited
|
|
for(int pspin=0; pspin<2 && !ok; ++pspin) //spin-flip, pspin is the spin with the particle
|
|
{
|
|
kind[pspin]=1;
|
|
kind[1-pspin]= -1;
|
|
ok=holesparticles(strings[0],weight[0],kind[0],ncore,norb,ref[0],virt[0],hp[0]) && holesparticles(strings[1],weight[1],kind[1],ncore,norb,ref[1],virt[1],hp[1]);
|
|
}
|
|
if(!ok)
|
|
{
|
|
method=1;
|
|
cout <<"The determinants are neither single replacements from one reference nor a spin-flip list, the overlap is computed by -m 1"<<endl;
|
|
return NRMat<REAL>();
|
|
}
|
|
const bool spinflip= kind[0]!=0;
|
|
cout <<"Number of determinants in the wavefunctions = "<<sl<<endl;
|
|
cout <<"Number of distinct alpha and beta strings = "<<weight[0].size()<<" "<<weight[1].size()<<endl;
|
|
if(spinflip) cout <<"Spin-flip wave functions, the particle has spin "<<(kind[0]>0?"alpha":"beta")<<endl;
|
|
else cout <<"CIS-type wave functions"<<endl;
|
|
|
|
//the formulas need a well-conditioned overlap block of the reference orbitals
|
|
for(int spin=0; spin<2; ++spin) if(ref[spin].size() && virt[spin].size())
|
|
{
|
|
const REAL cond=CondNumber(Smo[spin].submatrix(ref[spin],ref[spin]),'1');
|
|
if(!(cond<=CIS_MAXCOND))
|
|
{
|
|
method=1;
|
|
char buf[32];
|
|
sprintf(buf,"%.1e",cond);
|
|
cout <<"WARNING: -m 2: the overlap block of the reference "<<(spin?"beta":"alpha")<<" orbitals is ill-conditioned (condition number "<<buf<<"), the overlap is computed by -m "<<method<<endl;
|
|
return NRMat<REAL>();
|
|
}
|
|
}
|
|
|
|
//pivot transforms of the MO overlaps
|
|
REAL det=1.;
|
|
NRMat<REAL> M[2],Xt[2],Y[2],Sigma[2];
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
if(ref[spin].size()==0 || virt[spin].size()==0) continue; //no electrons or no other orbitals: nothing to contract
|
|
REAL d;
|
|
const NRMat<REAL> P=pivot_transform(Smo[spin],ref[spin],ref[spin],&d);
|
|
det*=d;
|
|
M[spin]=P.submatrix(ref[spin],ref[spin]);
|
|
Sigma[spin]=P.submatrix(virt[spin],virt[spin]);
|
|
if(!spinflip)
|
|
{
|
|
Xt[spin]=P.submatrix(virt[spin],ref[spin]).transpose();
|
|
Y[spin]=P.submatrix(ref[spin],virt[spin]);
|
|
Y[spin]*= -1.;
|
|
}
|
|
}
|
|
for(int spin=0; spin<2; ++spin) if(ref[spin].size() && virt[spin].size()==0) det*=determinant(Smo[spin]); //all orbitals occupied
|
|
|
|
//channels of the contraction: CIS has one for each spin, spin-flip a single one with the hole in hspin and the particle in pspin
|
|
const int nchannel= spinflip?1:2;
|
|
const int pspin= kind[1]>0?1:0;
|
|
const int hspin= spinflip?1-pspin:0;
|
|
int nh[2],np[2]; //dimensions of the coefficient matrices
|
|
bool active[2];
|
|
for(int c=0; c<nchannel; ++c)
|
|
{
|
|
nh[c]=ref[spinflip?hspin:c].size();
|
|
np[c]=virt[spinflip?pspin:c].size();
|
|
active[c]= nh[c]>0 && np[c]>0;
|
|
}
|
|
|
|
//coefficients of the reference determinant and matrices of the coefficients C(hole,particle) with the signs
|
|
const int nbra=brasl.ncols();
|
|
const int nket=ketsl.ncols();
|
|
NRVec<REAL> c0[2];
|
|
NRVec<NRMat<REAL> > C[2][2]; //[bra or ket][channel]
|
|
for(int bk=0; bk<2; ++bk)
|
|
{
|
|
const NRMat<REAL> &v= bk?ketsl:brasl;
|
|
c0[bk].resize(v.ncols());
|
|
c0[bk].clear();
|
|
for(int c=0; c<nchannel; ++c) if(active[c])
|
|
{
|
|
C[bk][c].resize(v.ncols());
|
|
for(int I=0; I<v.ncols(); ++I) {C[bk][c][I].resize(nh[c],np[c]); C[bk][c][I].clear();}
|
|
}
|
|
for(lexindex k=0; k<sl; ++k)
|
|
{
|
|
const lexindex kk= slperm?(*slperm)[k]:k;
|
|
const int a=ds[k].string[0];
|
|
const int b=ds[k].string[1];
|
|
if(spinflip)
|
|
{
|
|
const int sa= hspin?b:a; //string with the hole
|
|
const int sb= hspin?a:b; //string with the particle
|
|
const REAL sign=ds[k].sign*hp[hspin](sa,2)*hp[pspin](sb,2);
|
|
for(int I=0; I<v.ncols(); ++I) C[bk][0][I](hp[hspin](sa,0),hp[pspin](sb,1)) += sign*v(kk,I);
|
|
}
|
|
else if(hp[0](a,0)>=0)
|
|
{
|
|
const REAL sign=ds[k].sign*hp[0](a,2);
|
|
for(int I=0; I<v.ncols(); ++I) C[bk][0][I](hp[0](a,0),hp[0](a,1)) += sign*v(kk,I);
|
|
}
|
|
else if(hp[1](b,0)>=0)
|
|
{
|
|
const REAL sign=ds[k].sign*hp[1](b,2);
|
|
for(int I=0; I<v.ncols(); ++I) C[bk][1][I](hp[1](b,0),hp[1](b,1)) += sign*v(kk,I);
|
|
}
|
|
else for(int I=0; I<v.ncols(); ++I) c0[bk][I] += ds[k].sign*v(kk,I);
|
|
}
|
|
}
|
|
|
|
NRMat<REAL> result(nbra,nket);
|
|
result.clear();
|
|
NRVec<REAL> x(nbra);
|
|
x.clear();
|
|
if(!spinflip) for(int I=0; I<nbra; ++I)
|
|
{
|
|
x[I]=c0[0][I];
|
|
for(int c=0; c<2; ++c) if(active[c]) x[I] += C[0][c][I].dot(Xt[c]);
|
|
}
|
|
for(int J=0; J<nket; ++J)
|
|
{
|
|
REAL y=0;
|
|
if(!spinflip)
|
|
{
|
|
y=c0[1][J];
|
|
for(int c=0; c<2; ++c) if(active[c]) y += C[1][c][J].dot(Y[c]);
|
|
for(int I=0; I<nbra; ++I) result(I,J)=x[I]*y;
|
|
}
|
|
for(int c=0; c<nchannel; ++c) if(active[c])
|
|
{
|
|
NRMat<REAL> tmp(nh[c],np[c]), G(nh[c],np[c]);
|
|
tmp.gemm(0.,M[spinflip?hspin:c],'t',C[1][c][J],'n',1.);
|
|
G.gemm(0.,tmp,'n',Sigma[spinflip?pspin:c],'t',1.);
|
|
for(int I=0; I<nbra; ++I) result(I,J) += C[0][c][I].dot(G);
|
|
}
|
|
}
|
|
result*=det;
|
|
cout <<"closed formula CPU time "<<(1.*clock()-t0)/CLOCKS_PER_SEC<<endl;
|
|
return result;
|
|
}
|
|
|
|
|
|
//suitable for small cis, we do not care much about memory needs, just try not to really waste
|
|
//all paths go to this routine
|
|
|
|
NRMat<REAL> CIoverlap_slater(
|
|
bool uhf,
|
|
const NRMat<REAL> &brasl, //slaterCIsize x n
|
|
const NRMat<REAL> &ketsl, //slaterCIsize x m
|
|
const NRMat<REAL> &Sraw, //2*AO x 2*AO (generated by artificial doubled geometry input)
|
|
const NRMat<REAL> (&braLCAO)[2], //AO x MO
|
|
const NRMat<REAL> (&ketLCAO)[2], //AO x MO
|
|
int slaterfile, //file descriptor for slater basis
|
|
int excitlistfile, //file descriptor for list of not neglected excitations
|
|
const int nelec,
|
|
const int mo, //active MOs
|
|
const int ncore=0,
|
|
const int ndiscarded=0,
|
|
bool make_excitlist=true,
|
|
const int truncation=4, //neglect overlap of Slater dets differing in more than ... spinorbitals (take into account arbitrary MO swapping)
|
|
const bool inversedorbitals=true, //lowest MOs are at the top of DRT when true
|
|
const int slaterorder=1,
|
|
const bool reorderslbasis=true, //the slater basis was not sorted before
|
|
const int slaterpermfile=-1, //file handle for permutation of slater overlap matrix (slater CI vectors will remain same, slater basis is permuted)
|
|
REAL screeningthr=0,
|
|
NRMat<int> *activerange=NULL,
|
|
int inactive=0, //for performance improving precompute a constant part of the determinants
|
|
NRMat<int> *screening_mask = NULL,
|
|
bool SMO_only=false,
|
|
int method=0 //0 pairs of determinants with screening, 1 string tables, 2 closed formulas for CIS and spin-flip
|
|
)
|
|
{
|
|
|
|
int motot=mo+ncore+ndiscarded;
|
|
cout <<"Number of frozen+active+discarded MOs = "<<motot<<endl;
|
|
int ao=braLCAO[0].nrows();
|
|
if(ao!= braLCAO[1].nrows() || ao!= ketLCAO[0].nrows() || ao!= ketLCAO[1].nrows()) laerror("inconsistent row dimension of LCAO matrices");
|
|
if(braLCAO[0].ncols() != braLCAO[1].ncols() || braLCAO[0].ncols() != ketLCAO[0].ncols() || braLCAO[0].ncols() != ketLCAO[1].ncols() ) laerror("inconsistent column dimension of LCAO matrices");
|
|
|
|
lexindex sl=(lexindex)brasl.nrows();
|
|
|
|
|
|
if(activerange)
|
|
{
|
|
if((*activerange)(0,0)<=0||(*activerange)(0,1)<=0||(*activerange)(1,0)<=0||(*activerange)(1,1)<=0||
|
|
(*activerange)(0,0)>mo||(*activerange)(0,1)>mo||(*activerange)(1,0)>mo||(*activerange)(1,1)>mo) laerror("illegal active orbital range specified");
|
|
}
|
|
|
|
//consistency checks
|
|
if(ao!=ketLCAO[0].nrows()
|
|
|| ao!=ketLCAO[1].nrows()
|
|
|| ao!=braLCAO[0].nrows()
|
|
|| ao!=braLCAO[1].nrows()
|
|
|| 2*ao!=Sraw.nrows()
|
|
|| motot!=braLCAO[0].ncols()
|
|
|| motot!=ketLCAO[0].ncols()
|
|
|| motot!=braLCAO[1].ncols()
|
|
|| motot!=ketLCAO[1].ncols()
|
|
|| sl!=(lexindex)ketsl.nrows()
|
|
)
|
|
{
|
|
cout <<"ao " <<ao <<" ketLCAO[0].nrows() "<<ketLCAO[0].nrows()<< endl;
|
|
cout <<"ao " <<ao <<" ketLCAO[1].nrows() "<<ketLCAO[1].nrows()<< endl;
|
|
cout <<"ao " <<ao <<" braLCAO[0].nrows() "<<braLCAO[0].nrows()<< endl;
|
|
cout <<"ao " <<ao <<" braLCAO[1].nrows() "<<braLCAO[1].nrows()<< endl;
|
|
cout <<"2*ao "<<2*ao<<" Sraw.nrows() "<<Sraw.nrows()<<endl;
|
|
cout <<"motot "<<motot<<" braLCAO[0].ncols() "<<braLCAO[0].ncols()<<" ketLCAO[0].ncols() "<<ketLCAO[0].ncols()<<endl;
|
|
cout <<"motot "<<motot<<" braLCAO[1].ncols() "<<braLCAO[1].ncols()<<" ketLCAO[1].ncols() "<<ketLCAO[1].ncols()<<endl;
|
|
cout <<"sl "<<sl<<" ketsl.nrows() "<<ketsl.nrows()<<endl;
|
|
laerror("inconsistent dimensions in CIoverlap");
|
|
}
|
|
|
|
//calculate one-particle overlaps in the MO basis
|
|
NRMat<REAL> Smo[2];
|
|
Smo[0].resize(motot,motot);
|
|
Smo[1].resize(motot,motot);
|
|
{
|
|
NRMat<REAL> Sao12=Sraw.submatrix(0,ao-1,ao,2*ao-1);
|
|
#if 0
|
|
cout <<"test Sao12\n"<<Sao12;
|
|
#endif
|
|
NRMat<REAL> tmp;
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
tmp=Sao12*ketLCAO[spin];
|
|
Smo[spin].gemm(0.,braLCAO[spin],'t',tmp,'n',1.);
|
|
}
|
|
}
|
|
|
|
/* for test symmetrize Smo and test symmetry of Ssl
|
|
Smo[0]= (Smo[0]+Smo[0].transpose())*.5;
|
|
Smo[1]= (Smo[1]+Smo[1].transpose())*.5;
|
|
*/
|
|
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
#if 0
|
|
cout <<"test Smo["<<spin<<"]\n"<<Smo[spin];
|
|
#endif
|
|
char name[64];
|
|
sprintf(name,"SMO.%d",spin);
|
|
ofstream file;
|
|
file.open(name);
|
|
file <<Smo[spin];
|
|
file.close();
|
|
}
|
|
|
|
if(SMO_only) exit(0);
|
|
|
|
//#define UNIT_SMO
|
|
#ifdef UNIT_SMO
|
|
//try to put here a fake diagonal Smo
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
for(int kk=0; kk<Smo[spin].nrows(); ++kk)
|
|
for(int ll=0; ll<Smo[spin].ncols(); ++ll)
|
|
{
|
|
if(kk!=ll) Smo[spin](kk,ll)=0; else
|
|
{
|
|
if(Smo[spin](kk,ll)<0) Smo[spin](kk,ll)= -1; else Smo[spin](kk,ll)=1;
|
|
}
|
|
}
|
|
}
|
|
cout <<"Adjusted Smo["<<spin<<"]\n"<<Smo[spin];
|
|
#endif
|
|
|
|
|
|
//find out largest overlap mapping between bra and ket MOs for later need
|
|
//this should preferably be based not on Smo but on overlap of Loewdin-normalized LCAO at the two geometries
|
|
//thus it will yield identity for rotated-translated molecule
|
|
bool needpermute[2];
|
|
needpermute[0]=false;
|
|
needpermute[1]=false;
|
|
NRVec<int> bra2ketperm[2];
|
|
#if 0 //the application of the permutation is switched off (see the end of this block), so the mapping is not computed either
|
|
NRVec<int> bra2ket[2];
|
|
bra2ket[0].resize(motot);
|
|
bra2ket[1].resize(motot);
|
|
{
|
|
NRMat<REAL> Sao11=realsqrt(Sraw.submatrix(0,ao-1,0,ao-1));
|
|
NRMat<REAL> Sao22=realsqrt(Sraw.submatrix(ao,2*ao-1,ao,2*ao-1));
|
|
for(int spin=0; spin<2; ++spin)
|
|
{
|
|
NRMat<REAL> braL = Sao11 * braLCAO[spin];
|
|
NRMat<REAL> ketL = Sao22 * ketLCAO[spin];
|
|
NRMat<REAL> Sloewdin(motot,motot);
|
|
Sloewdin.gemm(0.,braL,'t',ketL,'n',1.);
|
|
cout <<"test Sloewdin["<<spin<<"]\n"<<Sloewdin;
|
|
|
|
NRVec<int> counts(motot);
|
|
NRVec<int> mophase(motot);
|
|
counts=0;
|
|
mophase=0;
|
|
for(int i=0; i<motot; ++i)
|
|
{
|
|
int jmaxforbid = -1;
|
|
findmax:
|
|
int jmax= -1;
|
|
int pmax= 1;
|
|
REAL smax= -1.;
|
|
for(int j=0; j<motot; ++j)
|
|
if(abs(Sloewdin(i,j)) > smax && j != jmaxforbid)
|
|
{
|
|
jmax=j;
|
|
smax=abs(Sloewdin(i,j));
|
|
pmax = Sloewdin(i,j)>0. ?1 : -1;
|
|
}
|
|
bra2ket[spin][i]=jmax;
|
|
mophase[i] = pmax;
|
|
if(counts[jmax] && jmaxforbid== -1) //this already had a big overlap and we were not in the second search already
|
|
{
|
|
//find second biggest one
|
|
jmaxforbid=jmax;
|
|
goto findmax;
|
|
}
|
|
else counts[jmax]++;
|
|
}
|
|
|
|
cout <<"test bra2ket["<<spin<<"], counts, mophase\n"<<bra2ket[spin]<<counts<<mophase;
|
|
for(int i=0; i<motot; ++i) if(counts[i]!=1)
|
|
{
|
|
cout << "cannot determine MO permutation, orbital rotations do not represent simple orbital swaps\n";
|
|
for(int j=0; j<motot; ++j) bra2ket[spin][j]=j;
|
|
goto skippermute;
|
|
}
|
|
|
|
cout <<"Smo["<<spin<<"] (permuted) diagonal elements\n";
|
|
for(int i=0; i<motot; ++i) cout << i<<" : "<<Smo[spin](i,bra2ket[spin][i])<<endl;
|
|
|
|
for(int i=ncore; i<motot-ndiscarded; ++i) if(bra2ket[spin][i]!=i)
|
|
{
|
|
needpermute[spin]=true;
|
|
if(bra2ket[spin][i]<ncore||bra2ket[spin][i]>=motot-ndiscarded) {cout <<"Warning: MO swap outside of active space occured for spin "<<spin<<"\n";}
|
|
}
|
|
|
|
skippermute:
|
|
|
|
//adjust the bra2ket permutation to the CI-active orbitals
|
|
bra2ketperm[spin].resize(mo+1); //index from 1 over active
|
|
{
|
|
bra2ketperm[spin][0]= -1; //dummy
|
|
if(inversedorbitals) for(int i=0; i<mo; ++i)
|
|
{
|
|
bra2ketperm[spin][i+1]=mo-(bra2ket[spin][ncore+mo-i-1]-ncore);
|
|
}
|
|
else for(int i=0; i<mo; ++i) bra2ketperm[spin][i+1]=bra2ket[spin][ncore+i]-ncore+1;
|
|
}
|
|
cout <<"bra2ketperm["<<spin<<"] "<<bra2ketperm[spin]<<endl;
|
|
cout <<"needpermute["<<spin<<"] "<<needpermute[spin]<<endl;
|
|
|
|
}//spin
|
|
}//local scope
|
|
|
|
cout <<"application of the permutation has been switched off, since typically the determinants from permuted orbitals are outside of the CI space anyway\n";
|
|
needpermute[0]=false;
|
|
needpermute[1]=false;
|
|
#endif
|
|
|
|
//read the slater basis into memory
|
|
cout << "Slater basis dimensions are "<<sl<<" "<<nelec<<endl;
|
|
cout <<"(total number of electrons including the ones in frozen orbitals is "<<nelec+2*ncore<<")"<<endl;
|
|
slaterbasis slaters(sl,nelec);
|
|
bool permuteslvectors=false;
|
|
{
|
|
struct stat buf;
|
|
if(! fstat(slaterpermfile,&buf)) if(buf.st_size>0) permuteslvectors=true;
|
|
}
|
|
if(reorderslbasis && !permuteslvectors) //was not reordered previously
|
|
{
|
|
//read the input basis
|
|
lseek(slaterfile,0,SEEK_SET);
|
|
slaters.get(slaterfile,false);
|
|
//check that the whole slaterfile has been exhausted by the read to prevent mismatch in the number of electrons
|
|
{
|
|
unsigned char dummy;
|
|
int r = read(slaterfile,&dummy,1);
|
|
if (r>0) laerror("slaterfile is larger than expected, check the number of electrons in cioverlap input");
|
|
if (r<0) laerror("unexpected error when testing slaterfile eof");
|
|
}
|
|
//check for illegal content (orbital number 0)
|
|
if(slaters.checkzero()) laerror("malformed slaterfile encountered, perhaps cipc/mcpc was compiled without --assume byterecl");
|
|
//sort
|
|
NRVec<SPMatindex> slperm(sl);
|
|
for(lexindex i=0; i<sl; ++i) slperm[i]=i;
|
|
slsort_sldetbase=&slaters;
|
|
slsort_permbase=&slperm[0];
|
|
slsort_nelectrons=nelec;
|
|
genqsort(0,(int)sl-1,slaterorder>0 ? slsort_slbascmp : slsort_slbascmp2 ,slsort_slbasswap);
|
|
//save the new basis
|
|
lseek(slaterfile,0,SEEK_SET);
|
|
for(lexindex i=0; i<sl; ++i)
|
|
if(slsort_nelectrons*(int)sizeof(spinorbindex) != write(slaterfile, slaters[slperm[i]] ,slsort_nelectrons*sizeof(spinorbindex))) laerror("write error in CIoverlap_slater");
|
|
|
|
//save the permutation, to be used in contraction with CI coefficients
|
|
lseek(slaterpermfile,0,SEEK_SET);
|
|
slperm.put(slaterpermfile,true);
|
|
permuteslvectors=true;
|
|
}
|
|
lseek(slaterfile,0,SEEK_SET);
|
|
slaters.get(slaterfile,false);
|
|
{
|
|
unsigned char dummy;
|
|
int r = read(slaterfile,&dummy,1);
|
|
if (r>0) laerror("slaterfile is larger than expected, check the number of electrons in cioverlap input");
|
|
if (r<0) laerror("unexpected error when testing slaterfile eof");
|
|
}
|
|
|
|
//check for illegal content (orbital number 0)
|
|
if(slaters.checkzero()) laerror("malformed slaterfile encountered, perhaps cipc was compiled without --assume byterecl");
|
|
#ifdef DEBUG
|
|
cout <<"slater basis "<<slaters<<endl;
|
|
#endif
|
|
|
|
if(method==1||method==2) //string tables or closed formulas instead of pairs of determinants
|
|
{
|
|
if(inversedorbitals) laerror("-m 1 and -m 2 are not implemented for the inversed order of orbitals");
|
|
NRVec<SPMatindex> slperm;
|
|
if(permuteslvectors)
|
|
{
|
|
lseek(slaterpermfile,0,SEEK_SET);
|
|
slperm.get(slaterpermfile,true);
|
|
if(sl!= (lexindex)slperm.size()) laerror("inconsistent slaterpermfile");
|
|
}
|
|
NRMat<REAL> result;
|
|
if(method==2) result=CIoverlap_cis(brasl,ketsl,Smo,slaters,ncore,permuteslvectors?&slperm:NULL,method); //may change method to 1
|
|
if(method==1) result=CIoverlap_strings(brasl,ketsl,Smo,slaters,ncore,permuteslvectors?&slperm:NULL);
|
|
if(screening_mask) for(int i=0; i<result.nrows(); ++i) for(int j=0; j<result.ncols(); ++j) if((*screening_mask)(i,j) == 0) result(i,j)=0;
|
|
return result;
|
|
}
|
|
|
|
//precompute and store in a file a list giving all determinants to consider from given one in the Ssl computation
|
|
//Note: symmetry of non-neglected Ssl entries is not in general present, if the bra2ket permutation has cycles longer than two
|
|
|
|
if(truncation<0)
|
|
{
|
|
cout <<"Omitting excitlistfile generation\n";
|
|
make_excitlist=false;
|
|
}
|
|
|
|
if(make_excitlist)
|
|
{
|
|
clock_t t0=clock();
|
|
cout <<"Generating excitation list for CI size " <<sl<<endl;
|
|
lseek(excitlistfile,0,SEEK_SET);
|
|
lexindex maxlen=0;
|
|
if(sizeof(lexindex)!=write(excitlistfile,&maxlen,sizeof(lexindex))) laerror("write error");
|
|
NRVec<lexindex> allowedlist(sl);
|
|
NRVec<unsigned char> n_act_elec((unsigned char)0,sl);
|
|
if(activerange) //count electrons in active orbitals for each determinant
|
|
for(lexindex i=0; i<sl; ++i)
|
|
{
|
|
for(int e=0; e<nelec; ++e)
|
|
{
|
|
int imo=slaters[i][e];
|
|
if(imo>0 && imo>=(*activerange)(0,0) && imo<=(*activerange)(0,1)
|
|
|| imo<0 && imo>=(*activerange)(1,0) && imo<=(*activerange)(1,1)) ++n_act_elec[i];
|
|
}
|
|
}
|
|
for(lexindex i=0; i<sl; ++i) //prepare the list for each slater det.
|
|
{
|
|
NRVec<char> notocc_i(2*(mo+ncore)+1); //is in principle a bitvector but the "unpacked form" should be more efficient
|
|
for(int o=0; o<2*(mo+ncore)+1; ++o) notocc_i[o]=1;
|
|
for(int e=0; e<nelec; ++e) notocc_i[mo+ncore+slaters[i][e]]=0;
|
|
//setup an active space, where a difference in occupation does not count
|
|
//not to degrade performance it is also handeled via notocc_i flags
|
|
if(activerange)
|
|
{
|
|
for(int o=(*activerange)(0,0); o<=(*activerange)(0,1); ++o) notocc_i[mo+ncore+o]=0;
|
|
for(int o=(*activerange)(1,0); o<=(*activerange)(1,1); ++o) notocc_i[mo+ncore+o]=0;
|
|
}
|
|
//
|
|
allowedlist[0]=i;
|
|
lexindex ntot=1;
|
|
for(lexindex j=i+1; j<sl; ++j)
|
|
{
|
|
//if(slater_excitation(nelec,slaters[i],slaters[j],false,mo)>truncation) goto skipthis ;
|
|
//replaced by more optimized code
|
|
int include_it = truncation;
|
|
int activediff = (int)n_act_elec[j] - (int)n_act_elec[i];
|
|
spinorbindex *sj = slaters[j];
|
|
if(abs(activediff) > truncation) goto skipthis;
|
|
if(activediff>0) include_it -= activediff; //account for electrons excited into active orbitals
|
|
if(include_it<0) goto skipthis;
|
|
for(int e=0; e<nelec; ++e)
|
|
{
|
|
if(notocc_i[mo+ncore+sj[e]]) --include_it;
|
|
if(include_it<0) goto skipthis;
|
|
}
|
|
allowedlist[ntot++]=j;
|
|
skipthis:;
|
|
}
|
|
cout <<"For "<<i<< " "<<ntot <<endl;
|
|
//write the list for i-th slater
|
|
if(ntot>maxlen) maxlen=ntot;
|
|
if(sizeof(lexindex)!=write(excitlistfile,&ntot,sizeof(lexindex))) laerror("write error");
|
|
if((lexindex)ntot*sizeof(lexindex)!=(lexindex)write(excitlistfile,&allowedlist[0],ntot*sizeof(lexindex))) laerror("write error");
|
|
}
|
|
lseek(excitlistfile,0,SEEK_SET);
|
|
if(sizeof(lexindex)!=write(excitlistfile,&maxlen,sizeof(lexindex))) laerror("write error");
|
|
cout <<"make_excitlist cpu time "<<(1.*clock()-t0)/CLOCKS_PER_SEC<<endl;
|
|
}
|
|
|
|
|
|
//prepare many-particle overlap matrix in the Slater basis
|
|
NRVec<SPMatindex> slperm;
|
|
if(permuteslvectors)
|
|
{
|
|
lseek(slaterpermfile,0,SEEK_SET);
|
|
slperm.get(slaterpermfile,true);
|
|
if(sl!= (lexindex)slperm.size()) laerror("inconsistent slaterpermfile");
|
|
}
|
|
|
|
SparseMat<REAL> Ssl(sl,sl);
|
|
unsigned long skipcount=0;
|
|
unsigned long computedcount=0;
|
|
clock_t t0=clock();
|
|
if(truncation>=0) lseek(excitlistfile,0,SEEK_SET);
|
|
lexindex maxlen=0;
|
|
if(truncation>=0)
|
|
{
|
|
if(sizeof(lexindex)!=read(excitlistfile,&maxlen,sizeof(lexindex))) laerror("excitlist read error 1");
|
|
if(maxlen==0) laerror("unfinished excitlistfile encountered");
|
|
}
|
|
else
|
|
maxlen=1;
|
|
NRVec<lexindex> excitlist(maxlen);
|
|
cout<<"Number of determinants in the wavefunctions = "<<sl<<endl;
|
|
cout<<"Done 0%\n"; cout.flush();
|
|
int percent=0;
|
|
for(lexindex i=0; i<sl; ++i)
|
|
{
|
|
#ifdef DEBUG
|
|
cout<< "Processing determinant "<<i<<endl;
|
|
#endif
|
|
if((i*100)/sl > percent)
|
|
{
|
|
percent = (i*100)/sl;
|
|
cout<<" "<<percent<<"%"; cout.flush();
|
|
}
|
|
lexindex ntot;
|
|
if(truncation>=0)
|
|
{
|
|
if(sizeof(lexindex)!=read(excitlistfile,&ntot,sizeof(lexindex))) laerror("excitlist read error 2");
|
|
if(ntot*sizeof(lexindex)!=(lexindex)read(excitlistfile,&excitlist[0],ntot*sizeof(lexindex))) laerror("excitlist read error 3");
|
|
}
|
|
for(lexindex jj= (truncation>=0? 0 : i) ; jj<(truncation>=0? ntot :sl); ++jj)
|
|
{
|
|
lexindex jjx;
|
|
jjx = truncation>=0 ? excitlist[jj] : jj;
|
|
#ifdef DEBUG
|
|
cout<< "Processing its peer "<<jj<<" "<<jjx<<endl;
|
|
#endif
|
|
perform_overlap(i,jjx,&skipcount,needpermute,nelec,slaters,bra2ketperm,permuteslvectors,slperm,screeningthr,brasl,ketsl,Smo,ncore,mo,inversedorbitals,&computedcount,Ssl,inactive,screening_mask);
|
|
if(i!=jjx) perform_overlap(jjx,i,&skipcount,needpermute,nelec,slaters,bra2ketperm,permuteslvectors,slperm,screeningthr,brasl,ketsl,Smo,ncore,mo,inversedorbitals,&computedcount,Ssl,inactive,screening_mask);
|
|
}
|
|
}
|
|
cout<< " 100%\noverlap calculation CPU time "<<(1.*clock()-t0)/CLOCKS_PER_SEC<<endl;
|
|
cout.flush();
|
|
slaters.resize(0,0);// free memory
|
|
|
|
#ifdef DEBUG
|
|
NRMat<REAL>Ssltest(Ssl);
|
|
/*
|
|
for(lexindex i=0; i<sl; ++i)
|
|
{
|
|
cout <<"for "<<i<<endl;
|
|
for(lexindex j=0; j<sl; ++j)
|
|
{
|
|
if(abs(Ssltest(i,j))>.01) cout <<"Ssl "<<i<<" "<<j<<" = "<<Ssltest(i,j)<<endl;
|
|
//cout <<"Ssl asymetry "<<i<<" "<<j<<" "<<Ssltest(i,j)-Ssltest(j,i) <<" from "<<Ssltest(i,j)<<endl;
|
|
}
|
|
cout <<endl;
|
|
}
|
|
*/
|
|
cout <<"Ssl diagonal elements\n";
|
|
for(lexindex i=0; i<sl; ++i) cout <<"det "<<i<<": "<<Ssltest(i,i)<<endl;
|
|
#endif
|
|
|
|
cout <<"Number of computed overlap determinant pairs = "<<computedcount<<endl;
|
|
if(screeningthr>0) cout <<"Number of skipped determinant pairs at screening threshold "<<screeningthr<<" = "<<skipcount<<endl;
|
|
|
|
|
|
//contract bras and kets with the overlaps in Slater basis
|
|
NRMat<REAL> tmp=Ssl*ketsl;
|
|
Ssl.resize(0,0);//free memory
|
|
NRMat<REAL> result(brasl.ncols(),ketsl.ncols());
|
|
result.gemm(0.,brasl,'t',tmp,'n',1.);
|
|
|
|
//zero out inaccurate elements according to screening_mask
|
|
if(screening_mask)
|
|
{
|
|
int i,j;
|
|
for(i=0; i<result.nrows(); ++i) for(j=0; j<result.ncols(); ++j)
|
|
{
|
|
if((*screening_mask)(i,j) == 0) result(i,j)=0;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
NRMat<int> *read_screening_mask(char *screening_mask_file,int nbras, int nkets)
|
|
{
|
|
if(!screening_mask_file) return NULL;
|
|
NRMat<int> *r = new NRMat<int>(0,nbras,nkets);
|
|
FILE *f = fopen(screening_mask_file,"r");
|
|
if(!f) laerror("cannot read screening_mask_file");
|
|
char line[1024];
|
|
if(fgets(line, 1024,f)==NULL) laerror("cannot read_screening_mask");
|
|
int x1,x2,x3,x4;
|
|
while(4==fscanf(f,"%d %d %d %d",&x1,&x2,&x3,&x4))
|
|
{
|
|
(*r)(x2-1,x4-1)=1;
|
|
(*r)(x4-1,x2-1)=1;
|
|
}
|
|
fclose(f);
|
|
for(int i=0; i<min(nbras,nkets); ++i) (*r)[i][i]=1;
|
|
cout <<"Screening mask\n"<< *r<<endl;
|
|
return r;
|
|
}
|
|
|
|
|
|
NRMat<REAL> CIoverlap(
|
|
bool uhf,
|
|
const NRMat<REAL> &bras, //CIsize x n
|
|
const NRMat<REAL> &kets, //CIsize x m
|
|
const NRMat<REAL> &Sraw, //2*AO x 2*AO (generated by artificial doubled geometry input)
|
|
const NRMat<REAL> (&braLCAO)[2], //AO x MO
|
|
const NRMat<REAL> (&ketLCAO)[2], //AO x MO
|
|
const drt &d,
|
|
int slaterfile, //file descriptor for slater basis
|
|
int citrfile, //file descriptor for CItrafo matrix
|
|
int excitlistfile, //file descriptor for list of not neglected excitations
|
|
const int ncore=0,
|
|
const int ndiscarded=0,
|
|
bool generate_trafo=true,
|
|
bool make_excitlist=true,
|
|
const int truncation=4, //neglect overlap of Slater dets differing in more than ... spinorbitals (take into account arbitrary MO swapping)
|
|
const bool inversedorbitals=true, //lowest MOs are at the top of DRT when true
|
|
const bool inverselex=false,
|
|
const int slaterorder=1,
|
|
REAL screeningthr=0,
|
|
NRMat<int> *activerange=NULL,
|
|
int inactive=0,
|
|
NRMat<int> *screening_mask = NULL,
|
|
bool SMO_only=false,
|
|
int method=0
|
|
)
|
|
{
|
|
//generate and save, or read from files Slater basis and transformation matrix
|
|
//this computation can be done only once and remains constant
|
|
lexindex ci=d.cisize();
|
|
NRMat<REAL> brasl, ketsl;
|
|
if(!SMO_only) //with -S only the MO overlaps are computed in CIoverlap_slater
|
|
{
|
|
SparseMat<REAL> citrafo;
|
|
if(generate_trafo)
|
|
{
|
|
citrafo=CItrafo(slaterfile,d,slaterorder,inverselex);
|
|
citrafo.put(citrfile,true);
|
|
}
|
|
else
|
|
{
|
|
lseek(citrfile,0,SEEK_SET);
|
|
citrafo.get(citrfile,true);
|
|
if((lexindex)citrafo.ncols()!=ci) laerror("inconsistent dimension of citrafo");
|
|
}
|
|
|
|
//transform CI vectors to the Slater basis
|
|
brasl=citrafo*bras;
|
|
ketsl=citrafo*kets;
|
|
citrafo.resize(0,0); //free memory
|
|
}
|
|
|
|
return CIoverlap_slater(uhf,brasl,ketsl,Sraw,braLCAO,ketLCAO,slaterfile,excitlistfile,d.electrons(),d.orbnum(),ncore,ndiscarded,make_excitlist,truncation,inversedorbitals,slaterorder,false,-1,screeningthr,activerange,inactive,screening_mask,SMO_only,method);
|
|
}
|
|
|
|
|
|
|
|
//defaults of the option -C: largest deviations from orthonormality of the MOs and of the CI vectors accepted by the check of the input
|
|
//accurate inputs give below 1e-8; the AO overlap with 5 decimals (Turbomole >= 7.5 print) gives 1e-3..5e-2 and exchanged halves of the
|
|
//doubled molecule (Columbus interface) 4e-2..6e-2, both stop the program with these defaults
|
|
#ifndef CHECK_MOTHRESHOLD
|
|
#define CHECK_MOTHRESHOLD 1e-6
|
|
#endif
|
|
#ifndef CHECK_CITHRESHOLD
|
|
#define CHECK_CITHRESHOLD 1e-6
|
|
#endif
|
|
|
|
//largest element (in the absolute value) of C^T S C - 1 for the AO overlap S and the MO coefficients in the columns of C
|
|
static REAL nonorthonormality(const NRMat<REAL> &S, const NRMat<REAL> &C)
|
|
{
|
|
NRMat<REAL> x=C.transpose()*(S*C);
|
|
for(int i=0; i<x.nrows(); ++i) x(i,i)-=1.;
|
|
return abs(x.amax());
|
|
}
|
|
|
|
//check of the input (option -C): the bra MOs have to be orthonormal in the first half of the AO overlap of the doubled molecule and the
|
|
//ket MOs in the second one (so that exchanged halves are found), and the bra CI vectors and the ket CI vectors have to be orthonormal
|
|
//(overlap of the vectors of coefficients, the determinants or CSFs of one geometry being orthonormal)
|
|
//the deviations are printed, and if any of them exceeds its threshold, the program stops; a zero threshold switches that check off
|
|
static void checkinput(const REAL mothreshold, const REAL cithreshold, const bool uhf, const NRMat<REAL> &bras, const NRMat<REAL> &kets, const NRMat<REAL> &Sraw, const NRMat<REAL> (&braLCAO)[2], const NRMat<REAL> (&ketLCAO)[2])
|
|
{
|
|
const int ao=braLCAO[0].nrows();
|
|
if(Sraw.nrows()!=2*ao || Sraw.ncols()!=2*ao) laerror("-C: the dimensions of the AO overlap and of the MO coefficients are inconsistent");
|
|
const NRMat<REAL> S11=Sraw.submatrix(0,ao-1,0,ao-1);
|
|
const NRMat<REAL> S22=Sraw.submatrix(ao,2*ao-1,ao,2*ao-1);
|
|
cout.setf(ios::scientific,ios::floatfield);
|
|
const streamsize precision=cout.precision(1);
|
|
cout <<"Check of the input (-C), largest deviations from orthonormality:"<<endl;
|
|
bool mobad=false;
|
|
if(mothreshold>0) for(int spin=0; spin<(uhf?2:1); ++spin)
|
|
{
|
|
const REAL b=nonorthonormality(S11,braLCAO[spin]);
|
|
const REAL k=nonorthonormality(S22,ketLCAO[spin]);
|
|
cout <<"bra "<<(uhf?(spin?"beta ":"alpha "):"")<<"MOs in the first half of the AO overlap "<<b<<", ket "<<(uhf?(spin?"beta ":"alpha "):"")<<"MOs in the second half "<<k<<" (accepted up to "<<mothreshold<<")"<<endl;
|
|
if(!(b<=mothreshold && k<=mothreshold)) mobad=true;
|
|
}
|
|
bool exchanged=mobad;
|
|
if(mobad) for(int spin=0; spin<(uhf?2:1); ++spin) //are the two halves exchanged?
|
|
{
|
|
const REAL b=nonorthonormality(S22,braLCAO[spin]);
|
|
const REAL k=nonorthonormality(S11,ketLCAO[spin]);
|
|
cout <<"bra "<<(uhf?(spin?"beta ":"alpha "):"")<<"MOs in the second half of the AO overlap "<<b<<", ket "<<(uhf?(spin?"beta ":"alpha "):"")<<"MOs in the first half "<<k<<endl;
|
|
if(!(b<=mothreshold && k<=mothreshold)) exchanged=false;
|
|
}
|
|
bool cibad=false;
|
|
if(cithreshold>0 && bras.nrows() && kets.nrows())
|
|
{
|
|
NRMat<REAL> x=bras.transpose()*bras;
|
|
for(int i=0; i<x.nrows(); ++i) x(i,i)-=1.;
|
|
const REAL b=abs(x.amax());
|
|
x=kets.transpose()*kets;
|
|
for(int i=0; i<x.nrows(); ++i) x(i,i)-=1.;
|
|
const REAL k=abs(x.amax());
|
|
cout <<"bra CI vectors "<<b<<", ket CI vectors "<<k<<" (accepted up to "<<cithreshold<<")"<<endl;
|
|
if(!(b<=cithreshold && k<=cithreshold)) cibad=true;
|
|
}
|
|
cout.setf(ios::fixed,ios::floatfield);
|
|
cout.precision(precision);
|
|
if(exchanged) laerror("-C: the two halves of the AO overlap of the doubled molecule are exchanged: the bra MOs are orthonormal in the second half and the ket MOs in the first one");
|
|
if(mobad) laerror("-C: the MOs are not orthonormal in the AO overlap of their geometry");
|
|
if(cibad) laerror("-C: the CI vectors are not orthonormal");
|
|
}
|
|
|
|
NRMat<REAL> docigugaoverlap(bool uhf, REAL screeningthr, int excitrank, NRMat<int> *activerange=NULL,int inactive=0, char *screening_mask_file=NULL,bool SMO_only=false,int method=0,REAL checkmo=0,REAL checkci=0)
|
|
{
|
|
NRVec<gelfpat> gelfbasis;
|
|
|
|
gelfpat g;
|
|
cin >>g;
|
|
paldustab paldus(g);
|
|
int n=g.nrows();
|
|
drt mydrt(paldus[n]);
|
|
cout <<mydrt;
|
|
|
|
int nbas,ncore,ndisc; //this is assumed same in alpha and beta
|
|
cin >>nbas >>ncore >>ndisc;
|
|
if(nbas!=n) laerror("inconsistent CIoverlap input");
|
|
|
|
NRMat<REAL> bras,kets;
|
|
int f;
|
|
|
|
int slaterfile,citrfile,excitlistfile;
|
|
|
|
if(!SMO_only)
|
|
{
|
|
slaterfile=open("slaterfile",O_CREAT|O_RDWR|O_LARGEFILE,0777); if(slaterfile<0) {perror("cannot open slaterfile");laerror("IO error");}
|
|
citrfile=open("citrfile",O_CREAT|O_RDWR|O_LARGEFILE,0777); if(citrfile<0) {perror("cannot open");laerror("IO error");}
|
|
excitlistfile=open("excitlistfile",O_CREAT|O_RDWR|O_LARGEFILE,0777); if(excitlistfile<0) {perror("cannot open excitlistfile");laerror("IO error");}
|
|
|
|
f=open("eivectors1",O_RDONLY|O_LARGEFILE); if(f<0) {perror("cannot open");laerror("IO error");}
|
|
bras.get(f,true,true);
|
|
close(f);
|
|
f=open("eivectors2",O_RDONLY|O_LARGEFILE); if(f<0) {perror("cannot open");laerror("IO error");}
|
|
kets.get(f,true,true);
|
|
close(f);
|
|
}
|
|
|
|
NRMat<int> *screening_mask=NULL;
|
|
if(!SMO_only) screening_mask = read_screening_mask(screening_mask_file,bras.ncols(), kets.ncols());
|
|
|
|
NRMat<REAL> Sraw; //this is in AO basis
|
|
cin >>Sraw;
|
|
|
|
NRMat<REAL> braLCAO[2],ketLCAO[2];
|
|
cin >> braLCAO[0];
|
|
if(uhf) cin >> braLCAO[1]; else braLCAO[1] = braLCAO[0];
|
|
cin >> ketLCAO[0];
|
|
if(uhf) cin >> ketLCAO[1]; else ketLCAO[1] = ketLCAO[0];
|
|
if(checkmo>0||checkci>0) checkinput(checkmo,checkci,uhf,bras,kets,Sraw,braLCAO,ketLCAO);
|
|
|
|
NRMat<REAL> SCI = CIoverlap(uhf, bras, kets, Sraw, braLCAO, ketLCAO, mydrt, slaterfile, citrfile, excitlistfile, ncore, ndisc, true, true, excitrank,inverseorb,inverselex,slaterorder,screeningthr,activerange,inactive,screening_mask,SMO_only,method);
|
|
|
|
close(slaterfile);
|
|
close(excitlistfile);
|
|
close(citrfile);
|
|
|
|
return SCI;
|
|
|
|
}
|
|
|
|
|
|
|
|
NRMat<REAL> docislateroverlap(bool uhf, REAL screeningthr, int excitrank,NRMat<int> *activerange=NULL,int inactive=0, char *screening_mask_file=NULL, bool SMO_only=false, int method=0, REAL checkmo=0, REAL checkci=0)
|
|
{
|
|
|
|
//NOTE: ncore are only such core orbitals which are not included in slaterfile
|
|
//NOTE: slaterfile numbering is expected to start from ncore+1
|
|
//NOTE inactive orbitals by -i option are NOT counted as core here, they are only approximation to make computation faster
|
|
//NOTE: discarded orbitals have to be included so that all dimensions fit
|
|
//NOTE: nelec should NOT include the 2*ncore frozen electrons
|
|
//
|
|
int nbas,ncore,ndisc,nactive,nelec;
|
|
cin >>nbas >>ncore >>ndisc >>nelec; //NOTE: nelec here does not have to distinguish alpha and beta
|
|
nactive=nbas-ncore-ndisc;
|
|
cout << "Number of frozen core orbitals (not in slaterfile) = "<<ncore<<endl;
|
|
cout << "Number of discarded virtual orbitals = "<<ndisc<<endl;
|
|
cout << "Number of not frozen electrons = " <<nelec<<endl;
|
|
|
|
if(inactive*2>=nelec) laerror("more inactive doubly occ orbitals than nelec/2");
|
|
|
|
int slaterfile;
|
|
int excitlistfile;
|
|
int slaterpermfile;
|
|
|
|
NRMat<REAL> bras,kets;
|
|
int f;
|
|
|
|
if(!SMO_only)
|
|
{
|
|
slaterfile=open("slaterfile",O_RDWR|O_LARGEFILE,0777); if(slaterfile<0) {perror("cannot open slaterfile");laerror("IO error");}
|
|
excitlistfile=open("excitlistfile",O_CREAT|O_LARGEFILE|O_RDWR,0777); if(excitlistfile<0) {perror("cannot open excitlistfile");laerror("IO error");}
|
|
slaterpermfile=open("slaterpermfile",O_CREAT|O_RDWR|O_LARGEFILE,0777); if(slaterpermfile<0) {perror("cannot open slaterpermfile");laerror("IO error");}
|
|
|
|
f=open("eivectors1",O_RDONLY|O_LARGEFILE); if(f<0) {perror("cannot open eivectors1");laerror("IO error");}
|
|
bras.get(f,true,true);
|
|
close(f);
|
|
f=open("eivectors2",O_RDONLY|O_LARGEFILE); if(f<0) {perror("cannot open eivectors2");laerror("IO error");}
|
|
kets.get(f,true,true);
|
|
close(f);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
cout <<"test eivectors1 (bras) "<<bras<<endl;
|
|
cout <<"test eivectors2 (kets) "<<kets<<endl;
|
|
#else
|
|
cout <<"Dominant contributions to bras\n";
|
|
for(int k=0; k<bras.ncols(); ++k)
|
|
{
|
|
cout <<"bra state "<<k<<":\n";
|
|
for(lexindex l=0; l<bras.nrows(); ++l)
|
|
if(abs(bras(l,k))>0.1) cout <<"det "<<l<<" "<<bras(l,k)<<endl;
|
|
}
|
|
cout <<"Dominant contributions to kets\n";
|
|
for(int k=0; k<kets.ncols(); ++k)
|
|
{
|
|
cout <<"ket state "<<k<<":\n";
|
|
for(lexindex l=0; l<kets.nrows(); ++l)
|
|
if(abs(kets(l,k))>0.1) cout <<"det "<<l<<" "<<kets(l,k)<<endl;
|
|
}
|
|
#endif
|
|
|
|
NRMat<int> *screening_mask = read_screening_mask(screening_mask_file,bras.ncols(), kets.ncols());
|
|
|
|
if(!SMO_only)
|
|
{
|
|
cout <<"Coefficient-overlap bra-ket:\n";
|
|
cout << bras.transpose() * kets;
|
|
}
|
|
|
|
NRMat<REAL> Sraw;
|
|
cin >>Sraw;
|
|
|
|
NRMat<REAL> braLCAO[2],ketLCAO[2];
|
|
cin >> braLCAO[0];
|
|
if(uhf) cin >> braLCAO[1]; else braLCAO[1] = braLCAO[0];
|
|
cin >> ketLCAO[0];
|
|
if(uhf) cin >> ketLCAO[1]; else ketLCAO[1] = ketLCAO[0];
|
|
if(checkmo>0||checkci>0) checkinput(checkmo,checkci,uhf,bras,kets,Sraw,braLCAO,ketLCAO);
|
|
|
|
|
|
bool make_excitlist=true;
|
|
if(SMO_only||method) make_excitlist=false;
|
|
else
|
|
{
|
|
struct stat64 buf;
|
|
if(fstat64(excitlistfile,&buf)) laerror("cannot stat excitlistfile");
|
|
if(buf.st_size>0) make_excitlist=false;
|
|
}
|
|
|
|
|
|
NRMat<REAL> SCI = CIoverlap_slater(uhf, bras, kets, Sraw, braLCAO, ketLCAO, slaterfile, excitlistfile, nelec, nactive, ncore, ndisc, make_excitlist, excitrank,inverseorb,slaterorder,false /*we do not need lexical order now*/,slaterpermfile,screeningthr,activerange,inactive,screening_mask,SMO_only,method);
|
|
|
|
close(slaterfile);
|
|
close(slaterpermfile);
|
|
close(excitlistfile);
|
|
|
|
return SCI;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
//without the synchronization with C stdio the long formatted input of the matrices from cin is several times faster
|
|
//(stdio locks at every character as soon as the threads of the BLAS exist); no C stdio is used on stdin and stdout here
|
|
ios::sync_with_stdio(false);
|
|
sigtraceback(SIGSEGV,1);
|
|
sigtraceback(SIGABRT,1);
|
|
sigtraceback(SIGBUS,1);
|
|
sigtraceback(SIGFPE,1);
|
|
|
|
clock_t clock0 = clock();
|
|
|
|
cout.setf(ios::fixed);
|
|
//cout.setf(ios::scientific);
|
|
cout.precision(12);
|
|
|
|
print_version(argc,argv,cout);
|
|
|
|
bool SMO_only=false;
|
|
bool uhf=0;
|
|
char *screening_mask_file=NULL;
|
|
bool alignrows=0;
|
|
bool useoldphase=0;
|
|
bool nooldphase=0; //-o overrides -a and -b in any order
|
|
bool aligncolumns=0;
|
|
REAL oldphasediff_threshold = 0.02;
|
|
REAL screeningthr=0.;
|
|
int excitrank=3;
|
|
int inactive=0;
|
|
NRMat<int> *activerange=NULL;
|
|
bool computenormalized = false;
|
|
bool computelog = false;
|
|
int method=2;
|
|
REAL checkmo=CHECK_MOTHRESHOLD;
|
|
REAL checkci=CHECK_CITHRESHOLD;
|
|
//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],"-S"))
|
|
{
|
|
SMO_only=true;
|
|
argc -=1; argv+=1;
|
|
}
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-U"))
|
|
{
|
|
uhf=1;
|
|
if(activerange) laerror("-U has to be given before -A, which reads 4 numbers with -U and 2 without");
|
|
argc -=1; argv+=1;
|
|
}
|
|
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-s"))
|
|
{
|
|
screening_mask_file=strdup(argv[2]);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-a"))
|
|
{
|
|
alignrows=1;
|
|
useoldphase=1;
|
|
--argc; ++argv;
|
|
}
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-b"))
|
|
{
|
|
aligncolumns=1;
|
|
useoldphase=1;
|
|
--argc; ++argv;
|
|
}
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-o"))
|
|
{
|
|
nooldphase=1;
|
|
--argc; ++argv;
|
|
}
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-T"))
|
|
{
|
|
sscanf(argv[2],"%lf",&oldphasediff_threshold);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-t"))
|
|
{
|
|
sscanf(argv[2],"%lf",&screeningthr);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-e"))
|
|
{
|
|
sscanf(argv[2],"%d",&excitrank);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-i"))
|
|
{
|
|
sscanf(argv[2],"%d",&inactive);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
|
|
if(argc>(uhf?5:3) && !strcmp(argv[1],"-A"))
|
|
{
|
|
activerange = new NRMat<int>(2,2);
|
|
if(uhf)
|
|
{
|
|
sscanf(argv[2],"%d",&(*activerange)(0,0));
|
|
sscanf(argv[3],"%d",&(*activerange)(0,1));
|
|
sscanf(argv[4],"%d",&(*activerange)(1,0));
|
|
sscanf(argv[5],"%d",&(*activerange)(1,1));
|
|
argc -=5; argv+=5;
|
|
}
|
|
else
|
|
{
|
|
sscanf(argv[2],"%d",&(*activerange)(0,0));
|
|
sscanf(argv[3],"%d",&(*activerange)(0,1));
|
|
(*activerange)(1,0)= -(*activerange)(0,1);
|
|
(*activerange)(1,1)= -(*activerange)(0,0);
|
|
argc -=3; argv+=3;
|
|
}
|
|
}
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-N"))
|
|
{
|
|
computenormalized = true;
|
|
--argc; ++argv;
|
|
}
|
|
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-L"))
|
|
{
|
|
computelog = true;
|
|
--argc; ++argv;
|
|
}
|
|
|
|
if(argc>2 && !strcmp(argv[1],"-m"))
|
|
{
|
|
sscanf(argv[2],"%d",&method);
|
|
argc -=2; argv+=2;
|
|
}
|
|
|
|
if(argc>3 && !strcmp(argv[1],"-C"))
|
|
{
|
|
sscanf(argv[2],"%lf",&checkmo);
|
|
sscanf(argv[3],"%lf",&checkci);
|
|
argc -=3; argv+=3;
|
|
}
|
|
|
|
if(argc>1 && !strcmp(argv[1],"-h"))
|
|
{
|
|
cout <<"Usage: cioverlap [options] [type] <input\n";
|
|
cout <<"type: 0 (default) wave functions in Slater determinants, 1 in GUGA CSFs (Gelfand pattern at the beginning of the input)\n";
|
|
cout <<"files: slaterfile (type 0), eivectors1 (bras), eivectors2 (kets)\n";
|
|
cout <<"files made in the first step and reused later (remove them when the list of determinants changes): excitlistfile (-m 0),\n";
|
|
cout <<" slaterstringsfile (-m 1 and 2; it is checked against the slaterfile and replaced if it does not belong to it)\n";
|
|
cout <<"input: nbas ncore ndisc nelec (type 0) or nbas ncore ndisc (type 1), AO overlap of the doubled molecule, bra MO coefficients, ket MO coefficients\n";
|
|
cout <<"options (in any order):\n";
|
|
cout <<"-m n algorithm: 2 (default) closed formulas for CIS-type and spin-flip wave functions; for other wave functions,\n";
|
|
cout <<" or if the overlap of the reference orbitals is ill-conditioned (with a warning), 1 is used;\n";
|
|
cout <<" 1 factors for pairs of alpha and beta strings, any wave function; both are without any screening;\n";
|
|
cout <<" 0 the old algorithm: pairs of determinants with the screening by -t, -e, -A (approximate)\n";
|
|
cout <<"-t thr neglect pairs of determinants with products of CI coefficients below thr (-m 0 only)\n";
|
|
cout <<"-e n neglect pairs of determinants differing in more than n spinorbitals, default 3, negative = no restriction (-m 0 only)\n";
|
|
cout <<"-A i j range of active orbitals where a different occupation does not count for -e, with -U (given before) i j for alpha and for beta (-m 0 only)\n";
|
|
cout <<"-i n number of doubly occupied inactive (not frozen) orbitals whose elimination is precomputed (-m 0 only)\n";
|
|
cout <<"-U unrestricted MOs (alpha and beta MO coefficients in the input)\n";
|
|
cout <<"-s file file with the pairs of states to be computed, the other overlaps are zeroed\n";
|
|
cout <<"-a, -b align the phases of the rows (-a) or columns (-b) of the overlap matrix, using the file phases.old\n";
|
|
cout <<"-o do not use phases.old\n";
|
|
cout <<"-T thr threshold for forgetting the stored phase difference of states which exchanged their character, default 0.02\n";
|
|
cout <<"-N print also the orthonormalized overlap matrix\n";
|
|
cout <<"-L print also the logarithm of the overlap matrix\n";
|
|
cout <<"-S compute only the MO overlaps (files SMO.0, SMO.1)\n";
|
|
cout <<"-C tmo tci check of the input, defaults 1e-6 1e-6, 0 = no check: the bra MOs must be orthonormal in the first half of the AO overlap\n";
|
|
cout <<" and the ket MOs in the second one within tmo, the bra CI vectors and the ket CI vectors must be orthonormal within tci;\n";
|
|
cout <<" the largest deviations are printed and the program stops if one of them exceeds its threshold\n";
|
|
exit(0);
|
|
}
|
|
}
|
|
if(nooldphase) useoldphase=0;
|
|
|
|
|
|
|
|
cout << "Number of (not frozen) inactive orbitals = "<<inactive<<endl;
|
|
cout << "Excitation rank threshold = "<<excitrank<<endl;
|
|
cout << "Screening threshold = "<<screeningthr<<endl;
|
|
if(method<0||method>2) laerror("illegal algorithm in the -m option");
|
|
if(method==1) cout <<"Algorithm with tables of alpha and beta strings (-m 1): the options -t, -e, -A and -i are ignored\n";
|
|
if(method==2) cout <<"Closed formulas for CIS-type and spin-flip wave functions, the algorithm of -m 1 for other ones (-m 2): the options -t, -e, -A and -i are ignored\n";
|
|
|
|
int type=0; if(argc>1) sscanf(argv[1],"%d",&type);
|
|
NRMat<REAL> SCI;
|
|
|
|
if(type) SCI = docigugaoverlap(uhf,screeningthr,excitrank,activerange,inactive,screening_mask_file,SMO_only,method,checkmo,checkci);
|
|
else SCI = docislateroverlap(uhf,screeningthr,excitrank,activerange,inactive,screening_mask_file,SMO_only,method,checkmo,checkci);
|
|
|
|
|
|
|
|
cout << "CI overlap before alignment\n";
|
|
cout << SCI;
|
|
|
|
//find maximum overlap , make it positive; store the resulting phases, use previous phases
|
|
|
|
SCI.copyonwrite();
|
|
if(aligncolumns) SCI.transposeme();
|
|
|
|
//new approach for tracing the phases using a generalized permutation matrix
|
|
NRMat<REAL> phasetrace(SCI.nrows(),SCI.nrows());
|
|
phasetrace.clear();
|
|
for(int i=0; i<SCI.nrows(); ++i)
|
|
{
|
|
REAL maxoverlap = -1;
|
|
int maxoverlapindex = -1;
|
|
for(int j=0; j<SCI.ncols(); ++j) if(abs(SCI(i,j)) > maxoverlap)
|
|
{
|
|
maxoverlapindex=j;
|
|
maxoverlap=abs(SCI(i,j));
|
|
}
|
|
phasetrace(i,maxoverlapindex) = SCI(i,maxoverlapindex)<0 ? -1 : 1;
|
|
}
|
|
cout <<"Phase trace matrix\n"<<phasetrace<<endl;
|
|
|
|
//original approach
|
|
if(alignrows||aligncolumns)
|
|
{
|
|
NRVec<REAL> phases,oldphases;
|
|
phases.resize(SCI.nrows());
|
|
oldphases.resize(SCI.nrows());
|
|
|
|
//default init old phases
|
|
for(int j=0; j<SCI.ncols(); ++j) oldphases[j]=1;
|
|
|
|
//apply old phases if available
|
|
if(useoldphase)
|
|
{
|
|
int f=open("phases.old",O_RDONLY);
|
|
if(f>=0)
|
|
{
|
|
oldphases.get(f,true,false);
|
|
close(f);
|
|
cout <<"Old phases:\n"<<oldphases<<endl;
|
|
if(oldphases.size() != SCI.ncols() || oldphases.size() != SCI.nrows()) laerror("size mismatch in phases.old");
|
|
for(int j=0; j<SCI.ncols(); ++j) for(int i=0; i<SCI.nrows(); ++i) SCI(i,j) *= oldphases[j];
|
|
cout <<"CI overlap patched by old phases\n"<< (aligncolumns? SCI.transpose():SCI);
|
|
}
|
|
}
|
|
|
|
//determine "new" phases which will make the diagonal positive, just for correct derivative couplings calculation
|
|
//they will then be 'forgotten' and the true new phases determined based on off-diagonal overlap
|
|
//For this purpose we also keep old phases which were previously determined by the diagonal approach to be consistent
|
|
//This is done only in a single step immediately following the step where diagonal and offdiagonal phase alignment
|
|
//gave different results
|
|
NRMat<REAL> SCI2(SCI);
|
|
SCI2.copyonwrite();
|
|
{
|
|
NRVec<REAL> oldphasediff;
|
|
NRVec<int> oldphasepair;
|
|
int f=open("phasediff",O_RDONLY);
|
|
if(f>=0)
|
|
{
|
|
oldphasediff.get(f,true);
|
|
oldphasepair.get(f,true);
|
|
close(f);
|
|
//delete phasediff file after the overlap of critical states falls below threshold
|
|
if(fabs(SCI(oldphasepair[0],oldphasepair[1]))+fabs(SCI(oldphasepair[1],oldphasepair[0])) < 2.*oldphasediff_threshold) unlink("phasediff");
|
|
cout <<"Old phase diff:\n"<<oldphasediff<<endl;
|
|
if(oldphasediff.size() != SCI2.ncols() || oldphasediff.size() != SCI2.nrows()) laerror("size mismatch in phasediff");
|
|
for(int j=0; j<SCI2.ncols(); ++j) for(int i=0; i<SCI2.nrows(); ++i) SCI2(i,j) *= oldphasediff[j];
|
|
cout <<"CI overlap patched by old phasediff\n"<< (aligncolumns? SCI2.transpose():SCI2);
|
|
}
|
|
}
|
|
|
|
NRVec<REAL> diagonalphases;
|
|
diagonalphases.resize(SCI2.nrows());
|
|
for(int i=0; i<SCI2.nrows(); ++i)
|
|
{
|
|
if(SCI2(i,i)<0)
|
|
{
|
|
diagonalphases[i]= -1;
|
|
for(int j=0; j<SCI2.ncols(); ++j) SCI2(i,j) *= -1;
|
|
}
|
|
else diagonalphases[i]= 1;
|
|
}
|
|
cout <<"\nCI overlap aligned by diagonal for TD couplings:\n"<<(aligncolumns? SCI2.transpose():SCI2)<<endl;
|
|
cout <<"Phases aligned by diagonal for TD couplings:\n"<<diagonalphases<<endl;
|
|
|
|
//determine phases for new state no. i
|
|
NRVec<int> phasepair(2);
|
|
bool storephasediff=false;
|
|
for(int i=0; i<SCI.nrows(); ++i)
|
|
{
|
|
REAL maxoverlap = -1;
|
|
int maxoverlapindex = -1;
|
|
for(int j=0; j<SCI.ncols(); ++j) if(abs(SCI(i,j)) > maxoverlap)
|
|
{
|
|
maxoverlapindex=j;
|
|
maxoverlap=abs(SCI(i,j));
|
|
}
|
|
|
|
if(i!=maxoverlapindex)
|
|
{
|
|
phasepair[0]=i;
|
|
phasepair[1]=maxoverlapindex;
|
|
cout <<"Note: state character exchanged, phase based on off-diagonal overlap: "<<i<<" -> "<<maxoverlapindex<<endl;
|
|
storephasediff=true;
|
|
}
|
|
|
|
//if(SCI(i,i)<0) changed to have positive not the diagonal, but the maximal overlap
|
|
if(SCI(i,maxoverlapindex)<0)
|
|
{
|
|
phases[i]= -1;
|
|
for(int j=0; j<SCI.ncols(); ++j) SCI(i,j) *= -1;
|
|
}
|
|
else
|
|
phases[i]= 1;
|
|
}
|
|
|
|
int p=open("phases",O_WRONLY|O_CREAT,0777);
|
|
if(p<0) laerror("cannot write-open phases");
|
|
phases.put(p,true);
|
|
close(p);
|
|
cout <<"Phases aligned:\n"<<phases<<endl;
|
|
|
|
if(storephasediff)
|
|
{
|
|
NRVec<REAL> phasediff(diagonalphases.size());
|
|
for(int i=0; i<phasediff.size(); ++i) phasediff[i] = diagonalphases[i] * phases[i];
|
|
int p=open("phasediff",O_WRONLY|O_CREAT|O_TRUNC,0777);
|
|
if(p<0) laerror("cannot write-open phasediff");
|
|
phasediff.put(p,true);
|
|
phasepair.put(p,true);
|
|
close(p);
|
|
cout <<"Phases difference:\n"<<phasediff<<endl;
|
|
}
|
|
|
|
//check old and new approach
|
|
NRVec<REAL> phases2 = phasetrace * oldphases;
|
|
if((phases2-phases).norm()>1e-12) cout <<"Warning: new and old phase alignment does not match\n";
|
|
}
|
|
|
|
if(aligncolumns) SCI.transposeme();
|
|
|
|
cout << "CPU time "<< (clock()-clock0)/CLOCKS_PER_SEC<<endl;
|
|
|
|
cout << "CI overlap matrix\n";
|
|
cout << SCI;
|
|
|
|
if(computenormalized)
|
|
{
|
|
NRSMat<REAL> SS(SCI.transpose()*SCI);
|
|
NRMat<REAL> SCInorm = SCI * sqrtinv(SS);
|
|
cout << "Orthonormalized CI overlap matrix\n";
|
|
cout << SCInorm;
|
|
}
|
|
|
|
if(computelog)
|
|
{
|
|
if(!alignrows && !aligncolumns) cout<<"Warning: without phase alignment the overlap may not be positive definite and log() might fail\n";
|
|
NRMat<REAL> SCIlog = log(SCI);
|
|
cout << "Logarithm of the CI overlap matrix\n";
|
|
cout << SCIlog;
|
|
}
|
|
}
|
|
|
|
|