1
0
mirror of https://gitlab.com/scemama/qp_plugins_scemama.git synced 2024-06-02 03:15:25 +02:00
qp_plugins_scemama/devel/svdwf/f2_work/f2_opt/SVD_opt/LOBD_VMC.py
2021-11-02 16:18:07 +01:00

217 lines
8.1 KiB
Python

# !!!
import sys, os
QMCCHEM_PATH=os.environ["QMCCHEM_PATH"]
sys.path.insert(0,QMCCHEM_PATH+"/EZFIO/Python/")
from ezfio import ezfio
from math import sqrt
from datetime import datetime
import time
import numpy as np
import subprocess
from scipy.linalg import eig, eigh
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
def get_energy():
buffer = subprocess.check_output(['qmcchem', 'result', '-e', 'e_loc', EZFIO_file], encoding='UTF-8')
if buffer.strip() != "":
buffer = buffer.splitlines()[-1]
_, energy, error = [float(x) for x in buffer.split()]
return energy, error
else:
return None, None
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
def get_Ci_h_matrix_svd():
Ci_h_matrix_svd = np.zeros( (n_svd,n_svd) )
SErr_H = np.zeros( (n_svd,n_svd) )
beg_Ci_h_matrix_svd = results.find('Ci_h_matrix_svd : [ ') + len( 'Ci_h_matrix_svd : [ ' )
end_Ci_h_matrix_svd = len(results)
Ci_h_matrix_svd_buf = results[beg_Ci_h_matrix_svd:end_Ci_h_matrix_svd]
Ci_h_matrix_svd_buf = Ci_h_matrix_svd_buf.split( '\n' )
for iline in range(1, n_svd**2+1):
line = Ci_h_matrix_svd_buf[iline].split()
indc = int( line[0] )
errS = float( line[4] )
if( indc != iline ):
print(' Error in reading Ci_h_matrix_svd')
print(' indc = {}'.format(indc) )
print(' iline = {}'.format(iline) )
break
else:
irow = indc % n_svd
icol = indc // n_svd
if( irow!=0 ):
Ci_h_matrix_svd[irow-1][icol] = float( line[2] )
SErr_H [irow-1][icol] = float( line[4] )
else:
Ci_h_matrix_svd[n_svd-1][icol-1] = float( line[2] )
SErr_H [n_svd-1][icol-1] = float( line[4] )
return(Ci_h_matrix_svd,SErr_H)
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
def get_Ci_overlap_matrix_svd():
Ci_overlap_matrix_svd = np.zeros( (n_svd,n_svd) )
SErr_O = np.zeros( (n_svd,n_svd) )
beg_Ci_overlap_matrix_svd = results.find('Ci_overlap_matrix_svd : [ ') + len( 'Ci_overlap_matrix_svd : [ ' )
end_Ci_overlap_matrix_svd = len(results)
Ci_overlap_matrix_svd_buf = results[beg_Ci_overlap_matrix_svd:end_Ci_overlap_matrix_svd]
Ci_overlap_matrix_svd_buf = Ci_overlap_matrix_svd_buf.split( '\n' )
for iline in range(1, n_svd**2+1):
line = Ci_overlap_matrix_svd_buf[iline].split()
indc = int( line[0] )
errS = float( line[4] )
if( indc != iline ):
print(' Error in reading Ci_overlap_matrix_svd')
print(' indc = {}'.format(indc))
print(' iline = {}'.format(iline))
break
else:
irow = indc % n_svd
icol = indc // n_svd
if( irow!=0 ):
Ci_overlap_matrix_svd[irow-1][icol] = float( line[2] )
SErr_O [irow-1][icol] = float( line[4] )
else:
Ci_overlap_matrix_svd[n_svd-1][icol-1] = float( line[2] )
SErr_O [n_svd-1][icol-1] = float( line[4] )
return(Ci_overlap_matrix_svd,SErr_O)
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
def check_symmetric(a, tol=1e-3):
return np.all(np.abs(a-a.T) < tol)
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
def get_Esvd():
# read CI_SVD matrices
Ci_h_matrix_svd , SErr_H = get_Ci_h_matrix_svd()
Ci_overlap_matrix_svd, SErr_O = get_Ci_overlap_matrix_svd()
#print( 'Ci_h_matrix_svd is symmetric ? {}' .format(check_symmetric(Ci_h_matrix_svd)) )
#print( 'Ci_overlap_matrix_svd is symmetric ? {}' .format(check_symmetric(Ci_overlap_matrix_svd)) )
if( save_SVD_QMCmat ):
with open('SVD_QMCmat.txt', 'a') as ff:
for i in range(n_svd):
for j in range(n_svd):
ff.write('{} {} {} {} {} {} \n'.format(i,j
, Ci_h_matrix_svd[i,j] , SErr_H[i,j]
, Ci_overlap_matrix_svd[i,j] , SErr_O[i,j] ))
# symmetrise and diagonalise
aa = Ci_h_matrix_svd
aa = 0.5*( aa + aa.T )
bb = Ci_overlap_matrix_svd
eigvals_svd, vr = eig(aa, bb, left=False, right=True, overwrite_a=True, overwrite_b=True,
check_finite=True, homogeneous_eigvals=False)
#print( eigvals_svd + E_toadd )
recouvre_svd = np.abs(psi_svd_coeff @ vr)
ind_gssvd = np.argmax(recouvre_svd)
E_svd = eigvals_svd[ind_gssvd] + E_toadd
return( E_svd, vr[:,ind_gssvd] )
# ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ ! ~ !
# _________________________________________________________________________________________________
# _________________________________________________________________________________________________
#
if __name__ == '__main__':
t0 = time.time()
print(" ")
print(" Today's date: \n", datetime.now() )
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ #
#EZFIO_file = "/home/aammar/qp2/src/svdwf/f2_work/f2_opt/SVD_opt/f2_cisd"
E_toadd = 30.35863309325590
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ #
ezfio.set_file(EZFIO_file)
n_svd = ezfio.get_spindeterminants_n_svd_coefs()
psi_svd_coeff = np.array(ezfio.get_spindeterminants_psi_svd_coefs())
print(" EZFIO file = {}".format(EZFIO_file))
print(" n_svd = {}\n".format(n_svd) )
t_read = time.time()
print(' getting QMCCHEM results from {}'.format(EZFIO_file) )
results = subprocess.check_output(['qmcchem', 'result', EZFIO_file], encoding='UTF-8')
print(' getting results after {} minutes \n'.format( (time.time()-t_read)/60. ))
if( len(results)<100):
print(' result is empty \n')
exit()
Eloc, ErrEloc = get_energy()
print(' Eloc = {} +/- {}'.format(Eloc, ErrEloc))
print(' {} <= Eloc <= {}'.format(Eloc-ErrEloc,Eloc+ErrEloc))
#print(' initial svd coeff = {}\n'.format(psi_svd_coeff))
save_SVD_QMCmat = True
#save_SVD_QMCmat = False
print('')
E_svd, sigma_svd = get_Esvd()
print(' svd enegry = {}'.format(E_svd) )
sigma_svd = sigma_svd * np.sign(sigma_svd[0])
#print(' svd coeff = {}'.format(sigma_svd))
print('')
# ___________________________________________________________________________
# ___________________________________________________________________________
#
#save_to_EZFIO = 'svd_QMC'
save_to_EZFIO = ''
if( save_to_EZFIO == 'svd_QMC' ):
ezfio.set_spindeterminants_psi_svd_coefs( sigma_svd )
direc_svdcoeff = EZFIO_file + '/spindeterminants/psi_svd_coefs.gz'
print(' {} is modified'.format(direc_svdcoeff) )
else:
print(' exit with no modifications in {} EZFIO'.format(EZFIO_file))
# ___________________________________________________________________________
# ___________________________________________________________________________
print("end after {:.3f} minutes".format((time.time()-t0)/60.) )
# _________________________________________________________________________________________________~
# _________________________________________________________________________________________________
# _________________________________________________________________________________________________