From dd40ff74d1dcd4cb899328391d4d3b02e7c06ee8 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Tue, 26 Jan 2021 13:11:57 +0100 Subject: [PATCH] OK up to DMC --- QMC.org | 314 +++++++++++++++++++++++++++++---------------- hydrogen.py | 4 +- vmc_metropolis.f90 | 12 +- vmc_metropolis.py | 24 ++-- 4 files changed, 225 insertions(+), 129 deletions(-) diff --git a/QMC.org b/QMC.org index 0f9c324..c9a515c 100644 --- a/QMC.org +++ b/QMC.org @@ -1428,7 +1428,7 @@ gfortran hydrogen.f90 qmc_stats.f90 qmc_metropolis.f90 -o qmc_metropolis : E = -0.49515370205041676 +/- 1.7660819245720729E-004 : A = 0.51713866666666664 +/- 3.7072551835783688E-004 -** TODO Gaussian random number generator +** Gaussian random number generator To obtain Gaussian-distributed random numbers, you can apply the [[https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform][Box Muller transform]] to uniform random numbers: @@ -1473,15 +1473,16 @@ subroutine random_gauss(z,n) end subroutine random_gauss #+END_SRC -** TODO Generalized Metropolis algorithm + In Python, you can use the [[https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html][~random.normal~]] function of Numpy. +** Generalized Metropolis algorithm :PROPERTIES: :header-args:python: :tangle vmc_metropolis.py :header-args:f90: :tangle vmc_metropolis.f90 :END: - One can use more efficient numerical schemes to move the electrons. - But in that case, the Metropolis accepation step has to be adapted - accordingly: the acceptance + One can use more efficient numerical schemes to move the electrons, + but the Metropolis accepation step has to be adapted accordingly: + the acceptance probability $A$ is chosen so that it is consistent with the probability of leaving $\mathbf{r}_n$ and the probability of entering $\mathbf{r}_{n+1}$: @@ -1499,19 +1500,19 @@ end subroutine random_gauss numbers. Hence, the transition probability was \[ - T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) & = & + T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = \text{constant} \] - So the expression of $A$ was simplified to the ratios of the squared + so the expression of $A$ was simplified to the ratios of the squared wave functions. - Now, if instead of drawing uniform random numbers - choose to draw Gaussian random numbers with mean 0 and variance + Now, if instead of drawing uniform random numbers we + choose to draw Gaussian random numbers with zero mean and variance $\tau$, the transition probability becomes: \[ - T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) & = & + T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = \frac{1}{(2\pi\,\tau)^{3/2}} \exp \left[ - \frac{\left( \mathbf{r}_{n+1} - \mathbf{r}_{n} \right)^2}{2\tau} \right] \] @@ -1525,8 +1526,8 @@ end subroutine random_gauss To do this, we can add the drift vector \[ - \frac{\nabla [ \Psi^2 ]}{\Psi^2} = 2 \frac{\nabla \Psi}{\Psi} - \]. + \frac{\nabla [ \Psi^2 ]}{\Psi^2} = 2 \frac{\nabla \Psi}{\Psi}. + \] The numerical scheme becomes a drifted diffusion: @@ -1540,7 +1541,7 @@ end subroutine random_gauss The transition probability becomes: \[ - T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) & = & + T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = \frac{1}{(2\pi\,\tau)^{3/2}} \exp \left[ - \frac{\left( \mathbf{r}_{n+1} - \mathbf{r}_{n} - \frac{\nabla \Psi(\mathbf{r}_n)}{\Psi(\mathbf{r}_n)} \right)^2}{2\,\tau} \right] @@ -1548,19 +1549,35 @@ end subroutine random_gauss *** Exercise 1 - + #+begin_exercise Write a function to compute the drift vector $\frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})}$. #+end_exercise - *Python* - #+BEGIN_SRC python :tangle hydrogen.py +**** Python + #+BEGIN_SRC python :tangle hydrogen.py :tangle none def drift(a,r): - ar_inv = -a/np.sqrt(np.dot(r,r)) - return r * ar_inv + # TODO #+END_SRC - *Fortran* +**** Python :solution: + #+BEGIN_SRC python :tangle hydrogen.py +def drift(a,r): + ar_inv = -a/np.sqrt(np.dot(r,r)) + return r * ar_inv + #+END_SRC + +**** Fortran + #+BEGIN_SRC f90 :tangle hydrogen.f90 :tangle none +subroutine drift(a,r,b) + implicit none + double precision, intent(in) :: a, r(3) + double precision, intent(out) :: b(3) + ! TODO +end subroutine drift + #+END_SRC + +**** Fortran :solution: #+BEGIN_SRC f90 :tangle hydrogen.f90 subroutine drift(a,r,b) implicit none @@ -1579,14 +1596,38 @@ end subroutine drift (This is a necessary step for the next section). #+end_exercise - *Python* - #+BEGIN_SRC python :results output +**** Python + #+BEGIN_SRC python :results output :tangle none from hydrogen import * from qmc_stats import * -def MonteCarlo(a,tau,nmax): - E = 0. - N = 0. +def MonteCarlo(a,nmax,tau): + # TODO + +# Run simulation +a = 0.9 +nmax = 100000 +tau = 1.3 +X0 = [ MonteCarlo(a,nmax,tau) 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}") + #+END_SRC + +**** Python :solution: + #+BEGIN_SRC python :results output +from hydrogen import * +from qmc_stats import * + +def MonteCarlo(a,nmax,tau): + energy = 0. accep_rate = 0. sq_tau = np.sqrt(tau) r_old = np.random.normal(loc=0., scale=1.0, size=(3)) @@ -1610,26 +1651,33 @@ def MonteCarlo(a,tau,nmax): d_old = d_new d2_old = d2_new psi_old = psi_new - N += 1. - E += e_loc(a,r_old) - return E/N, accep_rate/N + energy += e_loc(a,r_old) + return energy/nmax, accep_rate/nmax +# Run simulation a = 0.9 nmax = 100000 -tau = 1.0 -X = [MonteCarlo(a,tau,nmax) for i in range(30)] -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}\nA = {A} +/- {deltaA}") - #+END_SRC +tau = 1.3 +X0 = [ MonteCarlo(a,nmax,tau) for i in range(30)] - #+RESULTS: - : E = -0.4949730317138491 +/- 0.00012478601801760644 - : A = 0.7887163333333334 +/- 0.00026834549840347617 +# 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}") + #+END_SRC + + #+RESULTS: + : E = -0.4951317910667116 +/- 0.00014045774335059988 + : A = 0.7200673333333333 +/- 0.00045942791345632793 - *Fortran* - #+BEGIN_SRC f90 +**** Fortran + #+BEGIN_SRC f90 :tangle none subroutine variational_montecarlo(a,tau,nmax,energy,accep_rate) implicit none double precision, intent(in) :: a, tau @@ -1637,49 +1685,14 @@ subroutine variational_montecarlo(a,tau,nmax,energy,accep_rate) 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 :: sq_tau, chi(3) + double precision :: psi_old, psi_new double precision :: r_old(3), r_new(3) double precision :: d_old(3), d_new(3) double precision, external :: e_loc, psi - sq_tau = dsqrt(tau) + ! TODO - ! 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(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