1
0
mirror of https://github.com/TREX-CoE/qmc-lttc.git synced 2024-11-06 06:04:01 +01:00
qmc-lttc/qmc_metropolis.py
2021-01-29 13:23:00 +01:00

43 lines
834 B
Python

from hydrogen import *
from qmc_stats import *
def MonteCarlo(a,nmax,dt):
energy = 0.
N_accep = 0
r_old = np.random.uniform(-dt, dt, (3))
psi_old = psi(a,r_old)
for istep in range(nmax):
energy += e_loc(a,r_old)
r_new = r_old + np.random.uniform(-dt,dt,(3))
psi_new = psi(a,r_new)
ratio = (psi_new / psi_old)**2
if np.random.uniform() <= ratio:
N_accep += 1
r_old = r_new
psi_old = psi_new
return energy/nmax, N_accep/nmax
# Run simulation
a = 0.9
nmax = 100000
dt = 1.3
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
# 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}")