From 52ed6eff8dd63dd3475243fcd18313a004fa9fc0 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Tue, 12 Jan 2021 11:23:13 +0100 Subject: [PATCH] VMC OK --- QMC.org | 101 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 28 deletions(-) diff --git a/QMC.org b/QMC.org index 909fdf7..d1974ae 100644 --- a/QMC.org +++ b/QMC.org @@ -1248,27 +1248,36 @@ gfortran hydrogen.f90 qmc_stats.f90 vmc.f90 -o vmc the move: set $\mathbf{r}_{n+1} = \mathbf{r}_{n}$, but *don't remove the sample from the average!* + The /acceptance rate/ is the ratio of the number of accepted step + over the total number of steps. The time step should be adapted so + that the acceptance rate is around 0.5 for a good efficiency of + the simulation. **** TODO Exercise #+begin_exercise Modify the previous program to introduce the accept/reject step. You should recover the unbiased result. + Adjust the time-step so that the acceptance rate is 0.5. #+end_exercise *Python* #+BEGIN_SRC python :results output +from hydrogen import * +from qmc_stats import * + def MonteCarlo(a,tau,nmax): E = 0. N = 0. + accep_rate = 0. sq_tau = np.sqrt(tau) 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) psi_old = psi(a,r_old) for istep in range(nmax): - eta = np.random.normal(loc=0., scale=1.0, size=(3)) - r_new = r_old + tau * d_old + sq_tau * eta + chi = np.random.normal(loc=0., scale=1.0, size=(3)) + r_new = r_old + tau * d_old + sq_tau * chi d_new = drift(a,r_new) d2_new = np.dot(d_new,d_new) psi_new = psi(a,r_new) @@ -1278,75 +1287,111 @@ def MonteCarlo(a,tau,nmax): q = psi_new / psi_old q = np.exp(-argexpo) * q*q if np.random.uniform() < q: + accep_rate += 1. r_old = r_new d_old = d_new d2_old = d2_new psi_old = psi_new - N += 1. - E += e_loc(a,r_old) - return E/N + N += 1. + E += e_loc(a,r_old) + return E/N, accep_rate/N +a = 0.9 nmax = 100000 -tau = 0.1 +tau = 1.0 X = [MonteCarlo(a,tau,nmax) for i in range(30)] -E, deltaE = ave_error(X) -print(f"E = {E} +/- {deltaE}") +E, deltaE = ave_error([x[0] for x in X]) +A, deltaA = ave_error([x[1] for x in X]) +print(f"E = {E} +/- {deltaE} {A} +/- {deltaA}") #+END_SRC #+RESULTS: - : E = -0.4951783346213532 +/- 0.00022067316984271938 + : E = -0.49387078389332206 +/- 0.0033326460286729792 0.4983000000000001 +/- 0.006825097363627021 *Fortran* - #+BEGIN_SRC f90 -subroutine variational_montecarlo(a,nmax,energy) + #+BEGIN_SRC f90 +subroutine variational_montecarlo(a,tau,nmax,energy,accep_rate) implicit none - double precision, intent(in) :: a - integer , intent(in) :: nmax - double precision, intent(out) :: energy + double precision, intent(in) :: a, tau + integer*8 , intent(in) :: nmax + double precision, intent(out) :: energy, accep_rate integer*8 :: istep + double precision :: norm, sq_tau, chi(3), d2_old, prod, u + double precision :: psi_old, psi_new, d2_new, argexpo, q + double precision :: r_old(3), r_new(3) + double precision :: d_old(3), d_new(3) + double precision, external :: e_loc, psi - double precision :: norm, r(3), w - - double precision, external :: e_loc, psi, gaussian - + sq_tau = dsqrt(tau) + + ! Initialization energy = 0.d0 norm = 0.d0 + accep_rate = 0.d0 + call random_gauss(r_old,3) + call drift(a,r_old,d_old) + d2_old = d_old(1)*d_old(1) + d_old(2)*d_old(2) + d_old(3)*d_old(3) + psi_old = psi(a,r_old) + do istep = 1,nmax - call random_gauss(r,3) - w = psi(a,r) - w = w*w / gaussian(r) - norm = norm + w - energy = energy + w * e_loc(a,r) + call random_gauss(chi,3) + r_new(:) = r_old(:) + tau * d_old(:) + chi(:)*sq_tau + call drift(a,r_new,d_new) + d2_new = d_new(1)*d_new(1) + d_new(2)*d_new(2) + d_new(3)*d_new(3) + psi_new = psi(a,r_new) + ! Metropolis + prod = (d_new(1) + d_old(1))*(r_new(1) - r_old(1)) + & + (d_new(2) + d_old(2))*(r_new(2) - r_old(2)) + & + (d_new(3) + d_old(3))*(r_new(3) - r_old(3)) + argexpo = 0.5d0 * (d2_new - d2_old)*tau + prod + q = psi_new / psi_old + q = dexp(-argexpo) * q*q + call random_number(u) + if (u