10
1
mirror of https://gitlab.com/scemama/qmcchem.git synced 2024-11-09 07:33:39 +01:00
qmcchem/bin/jast_opt.py

190 lines
5.3 KiB
Python
Raw Normal View History

2021-12-01 11:14:37 +01:00
#!/usr/bin/env python3
2020-05-04 12:13:06 +02:00
import scipy as sp
import scipy.optimize
import numpy as np
import sys
import os
import time
import subprocess
2020-05-24 03:02:00 +02:00
from math import sqrt
2020-05-04 12:13:06 +02:00
2021-12-01 11:14:37 +01:00
2020-05-11 13:49:07 +02:00
QMCCHEM_PATH=os.environ["QMCCHEM_PATH"]
sys.path.insert(0,QMCCHEM_PATH+"/EZFIO/Python/")
2020-05-04 12:13:06 +02:00
from ezfio import ezfio
# PARAMETERS
2020-05-24 03:02:00 +02:00
thresh = 1.e-2
2021-07-23 23:35:55 +02:00
block_time = 6
2020-05-04 12:13:06 +02:00
def main():
2020-05-11 13:49:07 +02:00
if len(sys.argv) != 2:
print("Usage: %s <EZFIO_DIRECTORY>"%sys.argv[0])
2021-07-23 23:34:08 +02:00
sys.exit(1)
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
filename = sys.argv[1]
ezfio.set_file(filename)
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
def make_atom_map():
labels = {}
dimension = 0
for i,k in enumerate(ezfio.nuclei_nucl_label):
if k in labels:
labels[k].append(i)
else:
labels[k] = [dimension, i]
dimension += 1
atom_map = [[] for i in range(dimension)]
for atom in labels.keys():
l = labels[atom]
atom_map[l[0]] = l[1:]
return atom_map
atom_map = make_atom_map()
2020-05-11 13:49:07 +02:00
2021-07-23 23:35:55 +02:00
def get_params_b():
return ezfio.jastrow_jast_b_up_dn
2020-05-04 12:13:06 +02:00
def get_params_pen():
d = ezfio.jastrow_jast_pen
2021-07-30 18:18:18 +02:00
print(atom_map)
for m in atom_map:
print(m[0])
print (d[m[0]])
2021-12-01 11:14:37 +01:00
sys.stdout.flush()
2020-05-04 12:13:06 +02:00
return np.array([d[m[0]] for m in atom_map])
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
def get_energy():
buffer = subprocess.check_output(['qmcchem', 'result', '-e', 'e_loc', filename],
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
2020-05-24 03:02:00 +02:00
def get_variance():
buffer = subprocess.check_output(['qmcchem', 'result', '-e',
'e_loc_qmcvar', filename],
encoding='UTF-8')
if buffer.strip() != "":
buffer = buffer.splitlines()[-1]
_, variance, error = [float(x) for x in buffer.split()]
return variance, error
else:
return None, None
2020-05-11 13:49:07 +02:00
2021-07-23 23:35:55 +02:00
def set_params_b(x):
x = np.abs(x)
ezfio.set_jastrow_jast_b_up_up(x)
ezfio.set_jastrow_jast_b_up_dn(x)
2020-05-04 12:13:06 +02:00
def set_params_pen(x):
x = np.abs(x)
y=list(ezfio.jastrow_jast_pen)
for i,m in enumerate(atom_map):
for j in m:
y[j] = x[i]
ezfio.set_jastrow_jast_pen(y)
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
def run_qmc():
2020-05-24 03:02:00 +02:00
return subprocess.check_output(['qmcchem', 'run', filename])
2020-05-04 12:13:06 +02:00
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
def stop_qmc():
subprocess.check_output(['qmcchem', 'stop', filename])
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
def set_vmc_params():
2020-05-11 13:49:07 +02:00
# subprocess.check_output(['qmcchem', 'edit', '-c', '-j', 'Simple',
# '-m', 'VMC',
# '-l', str(block_time),
# '--time-step=0.3',
# '--stop-time=36000',
# '--norm=1.e-5',
# '-w', '10',
# filename])
2020-05-04 12:13:06 +02:00
subprocess.check_output(['qmcchem', 'edit', '-c', '-j', 'Simple',
'-l', str(block_time),
filename])
2020-05-11 13:49:07 +02:00
memo_energy = {'fmin': 100000000.}
2020-05-04 12:13:06 +02:00
def f(x):
print ("x = %s"%str(x))
2021-12-01 11:14:37 +01:00
sys.stdout.flush()
2020-05-04 12:13:06 +02:00
h = str(x)
if h in memo_energy:
return memo_energy[h]
2021-07-23 23:35:55 +02:00
set_params_b(x[0])
set_params_pen(x[1:])
2020-05-04 12:13:06 +02:00
set_vmc_params()
pid = os.fork()
if pid == 0:
run_qmc()
os._exit(os.EX_OK)
else:
import atexit
atexit.register(stop_qmc)
err = thresh+1.
2020-05-24 03:02:00 +02:00
time.sleep(3.*block_time/4.)
2020-05-11 13:49:07 +02:00
local_thresh = thresh
while err > local_thresh:
2020-05-04 12:13:06 +02:00
time.sleep(block_time)
2020-05-24 03:02:00 +02:00
e, e_err = get_energy()
variance, v_err = get_variance()
if e is None or variance is None:
continue
2021-07-30 18:18:18 +02:00
energy = e #+ variance
err = e_err #sqrt(e_err*e_err+v_err*v_err)
2020-05-24 03:02:00 +02:00
print(" %f %f %f %f %f %f"%(e, e_err, variance, v_err, energy, err))
2021-12-01 11:14:37 +01:00
sys.stdout.flush()
2020-05-24 03:02:00 +02:00
if (energy-2.*err) > memo_energy['fmin']+thresh:
local_thresh = 10.*thresh
elif (energy+2.*err) < memo_energy['fmin']-thresh:
local_thresh = 10.*thresh
2020-05-04 12:13:06 +02:00
# Check if PID is still running
2020-05-24 03:02:00 +02:00
try:
os.kill(pid,0)
except OSError:
print("---")
2021-12-01 11:14:37 +01:00
sys.stdout.flush()
2020-05-24 03:02:00 +02:00
break
2020-05-04 12:13:06 +02:00
stop_qmc()
os.wait()
2020-05-24 03:02:00 +02:00
memo_energy[h] = energy + err
2020-05-11 13:49:07 +02:00
memo_energy['fmin'] = min(energy, memo_energy['fmin'])
2020-05-04 12:13:06 +02:00
return energy
def run():
2021-07-23 23:35:55 +02:00
x = np.array([ get_params_b() ] + list(get_params_pen()))
2020-05-24 03:02:00 +02:00
if sum(x) == 0.:
jast_a_up_dn = ezfio.jastrow_jast_a_up_dn
x += jast_a_up_dn
2020-05-04 12:13:06 +02:00
opt = sp.optimize.minimize(f,x,method="Powell",
options= {'disp':True, 'ftol':thresh,'xtol':0.02})
print("x = "+str(opt))
2021-12-01 11:14:37 +01:00
sys.stdout.flush()
2021-07-23 23:35:55 +02:00
set_params_b(opt['x'][0])
set_params_pen(opt['x'][1:])
2020-05-11 13:49:07 +02:00
2020-05-04 12:13:06 +02:00
run()
if __name__ == '__main__':
main()