1
0
mirror of https://github.com/TREX-CoE/qmc-lttc.git synced 2024-12-22 04:15:01 +01:00
qmc-lttc/qmc_metropolis.py

36 lines
839 B
Python
Raw Normal View History

2021-01-20 18:31:49 +01:00
from hydrogen import *
from qmc_stats import *
def MonteCarlo(a,nmax,tau):
2021-01-26 10:02:38 +01:00
energy = 0.
N_accep = 0
2021-01-20 18:31:49 +01:00
r_old = np.random.uniform(-tau, tau, (3))
psi_old = psi(a,r_old)
for istep in range(nmax):
r_new = r_old + np.random.uniform(-tau,tau,(3))
psi_new = psi(a,r_new)
ratio = (psi_new / psi_old)**2
2021-01-26 10:02:38 +01:00
v = np.random.uniform()
if v <= ratio:
N_accep += 1
2021-01-20 18:31:49 +01:00
r_old = r_new
psi_old = psi_new
2021-01-26 10:02:38 +01:00
energy += e_loc(a,r_old)
return energy/nmax, N_accep/nmax
2021-01-20 18:31:49 +01:00
2021-01-26 10:02:38 +01:00
# Run simulation
2021-01-20 18:31:49 +01:00
a = 0.9
nmax = 100000
tau = 1.3
X0 = [ MonteCarlo(a,nmax,tau) for i in range(30)]
2021-01-26 10:02:38 +01:00
# Energy
X = [ x for (x, _) in X0 ]
2021-01-20 18:31:49 +01:00
E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}")
2021-01-26 10:02:38 +01:00
# Acceptance rate
X = [ x for (_, x) in X0 ]
A, deltaA = ave_error(X)
2021-01-20 18:31:49 +01:00
print(f"A = {A} +/- {deltaA}")