1
0
mirror of https://github.com/TREX-CoE/qmc-lttc.git synced 2024-10-05 15:56:17 +02:00
qmc-lttc/vmc_metropolis.py

59 lines
1.3 KiB
Python
Raw Normal View History

2021-01-13 18:11:13 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-29 13:23:00 +01:00
def MonteCarlo(a,nmax,dt):
sq_dt = np.sqrt(dt)
energy = 0.
N_accep = 0
r_old = np.random.normal(loc=0., scale=1.0, size=(3))
d_old = drift(a,r_old)
d2_old = np.dot(d_old,d_old)
2021-01-13 18:11:13 +01:00
psi_old = psi(a,r_old)
2021-01-29 13:23:00 +01:00
2021-01-13 18:11:13 +01:00
for istep in range(nmax):
chi = np.random.normal(loc=0., scale=1.0, size=(3))
2021-01-29 13:23:00 +01:00
energy += e_loc(a,r_old)
r_new = r_old + dt * d_old + sq_dt * chi
d_new = drift(a,r_new)
d2_new = np.dot(d_new,d_new)
2021-01-13 18:11:13 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-13 18:11:13 +01:00
# Metropolis
2021-01-29 13:23:00 +01:00
prod = np.dot((d_new + d_old), (r_new - r_old))
argexpo = 0.5 * (d2_new - d2_old)*dt + prod
2021-01-13 18:11:13 +01:00
q = psi_new / psi_old
q = np.exp(-argexpo) * q*q
2021-01-29 13:23:00 +01:00
if np.random.uniform() <= q:
N_accep += 1
r_old = r_new
d_old = d_new
d2_old = d2_new
2021-01-13 18:11:13 +01:00
psi_old = psi_new
2021-01-29 13:23:00 +01:00
return energy/nmax, accep_rate/nmax
2021-01-13 18:11:13 +01:00
2021-01-26 13:11:57 +01:00
# Run simulation
2021-01-29 13:23:00 +01:00
a = 0.9
2021-01-13 18:11:13 +01:00
nmax = 100000
2021-01-29 13:23:00 +01:00
dt = 1.3
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
2021-01-26 13:11:57 +01:00
# Energy
X = [ x for (x, _) in X0 ]
E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}")
# Acceptance rate
X = [ x for (_, x) in X0 ]
A, deltaA = ave_error(X)
print(f"A = {A} +/- {deltaA}")