1
0
mirror of https://gitlab.com/scemama/qp_plugins_scemama.git synced 2024-12-22 20:34:05 +01:00
qp_plugins_scemama/stable/champ/qp_convert.py

392 lines
8.8 KiB
Python
Raw Normal View History

2020-03-23 17:34:43 +01:00
#!/usr/bin/env python3
2019-01-23 17:42:42 +01:00
#
# Modified from the QMCPACK interface developed by @tapplencourt and @abenali
2020-05-13 00:09:00 +02:00
print("#QP -> CHAMP")
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
# ___
# | ._ o _|_
# _|_ | | | |_
2019-01-23 17:42:42 +01:00
#
from ezfio import ezfio
import os
import sys
ezfio_path = sys.argv[1]
ezfio.set_file(ezfio_path)
do_pseudo = ezfio.get_pseudo_do_pseudo()
if do_pseudo:
2020-05-13 00:09:00 +02:00
print("do_pseudo True")
2019-01-23 17:42:42 +01:00
from qp_path import QP_ROOT
l_ele_path = os.path.join(QP_ROOT,"data","list_element.txt")
with open(l_ele_path, "r") as f:
data_raw = f.read()
l_element_raw = data_raw.split("\n")
l_element = [element_raw.split() for element_raw in l_element_raw]
2020-05-13 00:09:00 +02:00
d_z = dict((abr, z) for (z, abr, ele, _) in [x for x in l_element if x != []] )
2019-01-23 17:42:42 +01:00
else:
2020-05-13 00:09:00 +02:00
print("do_pseudo False")
2019-01-23 17:42:42 +01:00
try:
n_det = ezfio.get_determinants_n_det()
except IOError:
n_det = 1
if n_det == 1:
2020-05-13 00:09:00 +02:00
print("multi_det False")
2019-01-23 17:42:42 +01:00
else:
2020-05-13 00:09:00 +02:00
print("multi_det True")
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
#
# |\/| o _ _
# | | | _> (_
2019-01-23 17:42:42 +01:00
#
def list_to_string(l):
return " ".join(map(str, l))
ao_num = ezfio.get_ao_basis_ao_num()
2020-05-13 00:09:00 +02:00
print("ao_num", ao_num)
2019-01-23 17:42:42 +01:00
mo_num = ezfio.get_mo_basis_mo_num()
2020-05-13 00:09:00 +02:00
print("mo_num", mo_num)
2019-01-23 17:42:42 +01:00
alpha = ezfio.get_electrons_elec_alpha_num()
beta = ezfio.get_electrons_elec_beta_num()
2020-05-13 00:09:00 +02:00
print("elec_alpha_num", alpha)
print("elec_beta_num", beta)
print("elec_tot_num", alpha + beta)
print("spin_multiplicity", (alpha - beta) + 1)
2019-01-23 17:42:42 +01:00
l_label = ezfio.get_nuclei_nucl_label()
l_charge = ezfio.get_nuclei_nucl_charge()
l_coord = ezfio.get_nuclei_nucl_coord()
l_coord_str = [list_to_string(i) for i in zip(*l_coord)]
2020-05-13 00:09:00 +02:00
print("nucl_num", len(l_label))
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
# _
# / _ _ ._ _|
# \_ (_) (_) | (_|
2019-01-23 17:42:42 +01:00
#
2020-05-13 00:09:00 +02:00
print("Atomic coord in Bohr")
2019-01-23 17:42:42 +01:00
for i, t in enumerate(zip(l_label, l_charge, l_coord_str)):
t_1 = d_z[t[0]] if do_pseudo else t[1]
2020-05-13 00:09:00 +02:00
2019-01-23 17:42:42 +01:00
t_new = [t[0],t_1,t[2]]
2020-05-13 00:09:00 +02:00
print(list_to_string(t_new))
2019-01-23 17:42:42 +01:00
#
# Call externet process to get the sysmetry
#
import subprocess
process = subprocess.Popen(
['qp_print_basis', ezfio_path],
stdout=subprocess.PIPE)
out, err = process.communicate()
2020-05-13 00:54:02 +02:00
basis_raw, sym_raw, _ = out.decode().split("\n\n\n")
2020-09-24 10:07:25 +02:00
basis_split = basis_raw.split('\n')
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
# _ __
# |_) _. _ o _ (_ _ _|_
# |_) (_| _> | _> __) (/_ |_
2019-01-23 17:42:42 +01:00
#
2020-09-24 10:07:25 +02:00
beginning = 0
for x in basis_split:
if x.startswith("Basis set"):
break
beginning += 1
beginning+=2
basis_without_header = "\n".join(basis_split[beginning:])
2019-01-23 17:42:42 +01:00
import re
l_basis_raw = re.split('\n\s*\n', basis_without_header)
a_already_print = []
l_basis_clean = []
for i, (a,b) in enumerate(zip(l_label,l_basis_raw)):
if a not in a_already_print:
l_basis_clean.append(b.replace('Atom {0}'.format(i + 1), a))
a_already_print.append(a)
else:
continue
2020-05-13 00:09:00 +02:00
print("BEGIN_BASIS_SET\n")
print("\n\n".join(l_basis_clean))
print("END_BASIS_SET")
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
# _
# |\/| / \ _
# | | \_/ _>
2019-01-23 17:42:42 +01:00
#
#
# Function
#
2020-05-13 00:54:02 +02:00
d_gms_order = ["s",
"x", "y", "z" ,
"xx", "yy", "zz", "xy", "xz", "yz" ,
"xxx", "yyy", "zzz", "xxy", "xxz", "yyx", "yyz", "zzx", "zzy", "xyz",
"xxxx", "yyyy", "zzzz", "xxxy", "xxxz", "yyyx", "yyyz", "zzzx", "zzzy", "xxyy", "xxzz", "yyzz", "xxyz", "yyxz", "zzxy"]
def key_gamess_style(item1):
2019-01-23 17:42:42 +01:00
try:
2020-05-13 00:54:02 +02:00
l = d_gms_order.index(item1)
except ValueError:
2019-01-23 17:42:42 +01:00
return 0
else:
2020-05-13 00:54:02 +02:00
result = d_gms_order.index(item1)
return result
2019-01-23 17:42:42 +01:00
def expend_sym_str(str_):
#Expend x2 -> xx
# yx2 -> xxy
for i, c in enumerate(str_):
try:
n = int(c)
except ValueError:
pass
else:
str_ = str_[:i - 1] + str_[i - 1] * n + str_[i + 1:]
#Order by frequency
return "".join(sorted(str_, key=str_.count, reverse=True))
def expend_sym_l(l_l_sym):
for l in l_l_sym:
l[2] = expend_sym_str(l[2])
return l_l_sym
def n_orbital(n):
if n==0:
return 1
elif n==1:
return 3
else:
return 2*n_orbital(n-1)-n_orbital(n-2)+1
def get_nb_permutation(str_):
if (str_) == 's': return 1
else: return n_orbital(len(str_))
2020-05-13 00:54:02 +02:00
import functools
2019-01-23 17:42:42 +01:00
def order_l_l_sym(l_l_sym):
n = 1
2020-05-13 00:09:00 +02:00
iter_ = list(range(len(l_l_sym)))
2019-01-23 17:42:42 +01:00
for i in iter_:
if n != 1:
n += -1
2020-05-13 00:09:00 +02:00
continue
2019-01-23 17:42:42 +01:00
l = l_l_sym[i]
n = get_nb_permutation(l[2])
2020-05-13 00:54:02 +02:00
def local_key(x):
return key_gamess_style(x[2])
2019-01-23 17:42:42 +01:00
l_l_sym[i:i + n] = sorted(l_l_sym[i:i + n],
2020-05-13 00:54:02 +02:00
key=local_key)
2019-01-23 17:42:42 +01:00
return l_l_sym
#==========================
# We will order the symetry
#==========================
l_sym_without_header = sym_raw.split("\n")[3:-2]
l_l_sym_raw = [i.split() for i in l_sym_without_header]
l_l_sym_expend_sym = expend_sym_l(l_l_sym_raw)
l_l_sym_ordered = order_l_l_sym(l_l_sym_expend_sym)
#========
#MO COEF
#========
def order_phase(mo_coef):
#Order
mo_coef_phase = []
import math
for i in mo_coef:
if abs(max(i)) > abs(min(i)):
sign_max = math.copysign(1, max(i))
else:
sign_max = math.copysign(1, min(i))
if sign_max == -1:
ii = [-1 * l for l in i]
else:
ii = i
mo_coef_phase.append(ii)
return mo_coef_phase
def chunked(l, chunks_size):
l_block = []
for i in l:
2020-05-13 00:09:00 +02:00
chunks = [i[x:x + chunks_size] for x in range(0, len(i), chunks_size)]
2019-01-23 17:42:42 +01:00
l_block.append(chunks)
return l_block
def print_mo_coef(mo_coef_block, l_l_sym):
2020-05-13 00:09:00 +02:00
print("")
print("BEGIN_MO")
print("")
2019-01-23 17:42:42 +01:00
len_block_curent = 0
nb_block = len(mo_coef_block[0])
for i_block in range(0, nb_block):
a = [i[i_block] for i in mo_coef_block]
2020-05-13 00:09:00 +02:00
r_ = list(range(len_block_curent, len_block_curent + len(a[0])))
print(" ".join([str(i + 1) for i in r_]))
2019-01-23 17:42:42 +01:00
len_block_curent += len(a[0])
for l in l_l_sym:
i = int(l[0]) - 1
i_a = int(l[1]) - 1
sym = l[2]
2020-05-13 00:09:00 +02:00
print(l_label[i_a], sym, " ".join('%20.15e'%i
for i in a[i]))
2019-01-23 17:42:42 +01:00
if i_block != nb_block - 1:
2020-05-13 00:09:00 +02:00
print("")
2019-01-23 17:42:42 +01:00
else:
2020-05-13 00:09:00 +02:00
print("END_MO")
2019-01-23 17:42:42 +01:00
mo_coef = ezfio.get_mo_basis_mo_coef()
2020-05-13 00:09:00 +02:00
mo_coef_transp = list(zip(*mo_coef))
2019-01-23 17:42:42 +01:00
mo_coef_block = chunked(mo_coef_transp, 4)
print_mo_coef(mo_coef_block, l_l_sym_ordered)
2020-05-13 00:09:00 +02:00
# _
# |_) _ _ _| _
# | _> (/_ |_| (_| (_)
2019-01-23 17:42:42 +01:00
#
if do_pseudo:
2020-05-13 00:09:00 +02:00
print("")
print("BEGIN_PSEUDO")
2019-01-23 17:42:42 +01:00
klocmax = ezfio.get_pseudo_pseudo_klocmax()
kmax = ezfio.get_pseudo_pseudo_kmax()
lmax = ezfio.get_pseudo_pseudo_lmax()
n_k = ezfio.get_pseudo_pseudo_n_k()
v_k = ezfio.get_pseudo_pseudo_v_k()
dz_k = ezfio.get_pseudo_pseudo_dz_k()
n_kl = ezfio.get_pseudo_pseudo_n_kl()
v_kl = ezfio.get_pseudo_pseudo_v_kl()
dz_kl = ezfio.get_pseudo_pseudo_dz_kl()
for i, a in enumerate(l_label):
l_str = []
#Local
l_dump = []
for k in range(klocmax):
if v_k[k][i]:
l_ = list_to_string([v_k[k][i], n_k[k][i] + 2, dz_k[k][i]])
l_dump.append(l_)
l_str.append(l_dump)
#Non local
for l in range(lmax + 1):
l_dump = []
for k in range(kmax):
if v_kl[l][k][i]:
l_ = list_to_string([v_kl[l][k][i], n_kl[l][k][i] + 2,
dz_kl[l][k][i]])
l_dump.append(l_)
if l_dump:
l_str.append(l_dump)
str_ = "PARAMETERS FOR {0} ON ATOM {1} WITH ZCORE {2} AND LMAX {3} ARE"
2020-05-13 00:09:00 +02:00
print(str_.format(a, i + 1, int(d_z[a])-int(l_charge[i]), int(len(l_str) - 1)))
2019-01-23 17:42:42 +01:00
for i, l in enumerate(l_str):
str_ = "FOR L= {0} COEFF N ZETA"
2020-05-13 00:09:00 +02:00
print(str_.format(int(len(l_str) - i - 1)))
2019-01-23 17:42:42 +01:00
for ii, ll in enumerate(l):
2020-05-13 00:09:00 +02:00
print(" ", ii + 1, ll)
2019-01-23 17:42:42 +01:00
str_ = "THE ECP RUN REMOVES {0} CORE ELECTRONS, AND THE SAME NUMBER OF PROTONS."
2020-05-13 00:09:00 +02:00
print(str_.format(sum([int(d_z[a])-int(l_charge[i]) for i,a in enumerate(l_label)])))
print("END_PSEUDO")
2019-01-23 17:42:42 +01:00
2020-05-13 00:09:00 +02:00
# _
# | \ _ _|_
# |_/ (/_ |_
2019-01-23 17:42:42 +01:00
#
psi_coef = ezfio.get_determinants_psi_coef()
psi_det = ezfio.get_determinants_psi_det()
bit_kind = ezfio.get_determinants_bit_kind()
nexcitedstate = ezfio.get_determinants_n_states()
2020-05-13 00:09:00 +02:00
print("")
print("BEGIN_DET")
print("")
print("mo_num", mo_num)
print("det_num", n_det)
print("")
2019-01-23 17:42:42 +01:00
if "QP_STATE" in os.environ:
state = int(os.environ["QP_STATE"])-1
else:
state = 0
psi_coef_small = psi_coef[state]
encode = 8*bit_kind
def bindigits(n, bits):
s = bin(n & int("1"*bits, 2))[2:]
return ("{0:0>%s}" % (bits)).format(s)
decode = lambda det: ''.join(bindigits(i,encode)[::-1] for i in det)[:mo_num]
MultiDetAlpha = []
MultiDetBeta = []
for coef, (det_a, det_b) in zip(psi_coef_small, psi_det):
2020-05-13 00:09:00 +02:00
print(coef)
2019-01-23 17:42:42 +01:00
MyDetA=decode(det_a)
MyDetB=decode(det_b)
2020-05-13 00:09:00 +02:00
print(MyDetA)
print(MyDetB)
print('')
2019-01-23 17:42:42 +01:00
MultiDetAlpha.append( det_a )
2020-05-13 00:09:00 +02:00
MultiDetBeta.append( det_b )
print("END_DET")
2019-01-23 17:42:42 +01:00