From 86a1889f207ea94b50dbbdad22cb8568f61bd564 Mon Sep 17 00:00:00 2001 From: scemama Date: Wed, 20 Jan 2021 18:13:44 +0000 Subject: [PATCH] deploy: 3877c981a37eff664acbfa61bdd94ac5bd555be7 --- index.html | 924 ++++++++++++++++++++++------------------------------- 1 file changed, 387 insertions(+), 537 deletions(-) diff --git a/index.html b/index.html index 97316a3..f746ae4 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + Quantum Monte Carlo @@ -257,64 +257,65 @@ for the JavaScript code in this tag.

Table of Contents

@@ -322,8 +323,8 @@ for the JavaScript code in this tag.
-
-

1 Introduction

+
+

1 Introduction

We propose different exercises to understand quantum Monte Carlo (QMC) @@ -365,8 +366,8 @@ interpreted as a single precision value

-
-

2 Numerical evaluation of the energy

+
+

2 Numerical evaluation of the energy

In this section we consider the Hydrogen atom with the following @@ -440,13 +441,13 @@ E & = & \frac{\langle \Psi| \hat{H} | \Psi\rangle}{\langle \Psi |\Psi \rangle} \end{eqnarray*}

-
-

2.1 Local energy

+
+

2.1 Local energy

-
-

2.1.1 Exercise 1

+
+

2.1.1 Exercise 1

@@ -490,8 +491,8 @@ and returns the potential.

-
-

2.1.2 Exercise 2

+
+

2.1.2 Exercise 2

@@ -526,8 +527,8 @@ input arguments, and returns a scalar.

-
-

2.1.3 Exercise 3

+
+

2.1.3 Exercise 3

@@ -608,8 +609,8 @@ So the local kinetic energy is

-
-

2.1.4 Exercise 4

+
+

2.1.4 Exercise 4

@@ -652,14 +653,14 @@ local energy.

-
-

2.2 Plot of the local energy along the \(x\) axis

+
+

2.2 Plot of the local energy along the \(x\) axis

-
-

2.2.1 Exercise

+
+

2.2.1 Exercise

@@ -776,8 +777,8 @@ plot './data' index 0 using 1:2 with lines title 'a=0.1', \

-
-

2.3 Numerical estimation of the energy

+
+

2.3 Numerical estimation of the energy

If the space is discretized in small volume elements \(\mathbf{r}_i\) @@ -807,8 +808,8 @@ The energy is biased because:

-
-

2.3.1 Exercise

+
+

2.3.1 Exercise

@@ -918,8 +919,8 @@ a = 2.0000000000000000 E = -8.0869806678448772E-002

-
-

2.4 Variance of the local energy

+
+

2.4 Variance of the local energy

The variance of the local energy is a functional of \(\Psi\) @@ -936,7 +937,7 @@ which can be simplified as

-\[ \sigma^2(E_L) = \langle E^2 \rangle - \langle E \rangle^2 \] +\[ \sigma^2(E_L) = \langle E_L^2 \rangle - \langle E_L \rangle^2 \]

@@ -946,8 +947,8 @@ energy can be used as a measure of the quality of a wave function.

-
-

2.4.1 Exercise (optional)

+
+

2.4.1 Exercise (optional)

@@ -959,8 +960,8 @@ Prove that :

-
-

2.4.2 Exercise

+
+

2.4.2 Exercise

@@ -1095,8 +1096,8 @@ a = 2.0000000000000000 E = -8.0869806678448772E-002 s2 = 1.806881

-
-

3 Variational Monte Carlo

+
+

3 Variational Monte Carlo

Numerical integration with deterministic methods is very efficient @@ -1112,8 +1113,8 @@ interval.

-
-

3.1 Computation of the statistical error

+
+

3.1 Computation of the statistical error

To compute the statistical error, you need to perform \(M\) @@ -1153,8 +1154,8 @@ And the confidence interval is given by

-
-

3.1.1 Exercise

+
+

3.1.1 Exercise

@@ -1203,8 +1204,8 @@ input array.

-
-

3.2 Uniform sampling in the box

+
+

3.2 Uniform sampling in the box

We will now do our first Monte Carlo calculation to compute the @@ -1238,8 +1239,8 @@ statistical error.

-
-

3.2.1 Exercise

+
+

3.2.1 Exercise

@@ -1349,19 +1350,228 @@ E = -0.49588321986667677 +/- 7.1758863546737969E-004

-
-

3.3 Gaussian sampling

+
+

3.3 Metropolis sampling with \(\Psi^2\)

-We will now improve the sampling and allow to sample in the whole -3D space, correcting the bias related to the sampling in the box. +We will now use the square of the wave function to sample random +points distributed with the probability density +\[ + P(\mathbf{r}) = \left[\Psi(\mathbf{r})\right]^2 + \]

-Instead of drawing uniform random numbers, we will draw Gaussian -random numbers centered on 0 and with a variance of 1. +The expression of the average energy is now simplified to the average of +the local energies, since the weights are taken care of by the +sampling :

+

+\[ + E \approx \frac{1}{M}\sum_{i=1}^M E_L(\mathbf{r}_i) + \] +

+ + +

+To sample a chosen probability density, an efficient method is the +Metropolis-Hastings sampling algorithm. Starting from a random +initial position \(\mathbf{r}_0\), we will realize a random walk as follows: +

+ +

+\[ + \mathbf{r}_{n+1} = \mathbf{r}_{n} + \tau \mathbf{u} + \] +

+ +

+where \(\tau\) is a fixed constant (the so-called time-step), and +\(\mathbf{u}\) is a uniform random number in a 3-dimensional box +\((-1,-1,-1) \le \mathbf{u} \le (1,1,1)\). We will then add the +accept/reject step that will guarantee that the distribution of the +\(\mathbf{r}_n\) is \(\Psi^2\): +

+ +
    +
  • Compute a new position \(\mathbf{r}_{n+1}\)
  • +
  • Draw a uniform random number \(v \in [0,1]\)
  • +
  • Compute the ratio \(R = \frac{\left[\Psi(\mathbf{r}_{n+1})\right]^2}{\left[\Psi(\mathbf{r}_{n})\right]^2}\)
  • +
  • if \(v \le R\), accept the move (do nothing)
  • +
  • else, reject the move (set \(\mathbf{r}_{n+1} = \mathbf{r}_n\))
  • +
  • evaluate the local energy at \(\mathbf{r}_{n+1}\)
  • +
+ +
+

+A common error is to remove the rejected samples from the +calculation of the average. Don't do it! +

+ +

+All samples should be kept, from both accepted and rejected moves. +

+ +
+ +

+If the time step is infinitely small, the ratio will be very close +to one and all the steps will be accepted. But the trajectory will +be infinitely too short to have statistical significance. +

+ +

+On the other hand, as the time step increases, the number of +accepted steps will decrease because the ratios might become +small. If the number of accepted steps is close to zero, then the +space is not well sampled either. +

+ +

+The time step should be adjusted so that it is as large as +possible, keeping the number of accepted steps not too small. To +achieve that we define the acceptance rate as the number of +accepted steps over the total number of steps. Adjusting the time +step such that the acceptance rate is close to 0.5 is a good compromise. +

+
+ + +
+

3.3.1 Exercise

+
+
+

+Modify the program of the previous section to compute the energy, sampling with +\(Psi^2\). +Compute also the acceptance rate, so that you can adapt the time +step in order to have an acceptance rate close to 0.5 . +Can you observe a reduction in the statistical error? +

+ +
+ +

+Python +

+
+
from hydrogen  import *
+from qmc_stats import *
+
+def MonteCarlo(a,nmax,tau):
+    E = 0.
+    N = 0.
+    N_accep = 0.
+    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
+        v = np.random.uniform(0,1,(1))
+        if v < ratio:
+            N_accep += 1.
+            r_old = r_new
+            psi_old = psi_new
+        N += 1.
+        E += e_loc(a,r_old)
+    return E/N, N_accep/N
+
+a = 0.9
+nmax = 100000
+tau = 1.3
+X0 = [ MonteCarlo(a,nmax,tau) for i in range(30)]
+X = [ x for x, _ in X0 ]
+A = [ x for _, x in X0 ]
+E, deltaE = ave_error(X)
+A, deltaA = ave_error(A)
+print(f"E = {E} +/- {deltaE}")
+print(f"A = {A} +/- {deltaA}")
+
+
+ +

+Fortran +

+
+
subroutine metropolis_montecarlo(a,nmax,tau,energy,accep)
+  implicit none
+  double precision, intent(in)  :: a
+  integer*8       , intent(in)  :: nmax 
+  double precision, intent(in)  :: tau
+  double precision, intent(out) :: energy
+  double precision, intent(out) :: accep
+
+  integer*8 :: istep
+
+  double precision :: norm, r_old(3), r_new(3), psi_old, psi_new
+  double precision :: v, ratio, n_accep
+  double precision, external :: e_loc, psi, gaussian
+
+  energy = 0.d0
+  norm   = 0.d0
+  n_accep = 0.d0
+  call random_number(r_old)
+  r_old(:) = tau * (2.d0*r_old(:) - 1.d0)
+  psi_old = psi(a,r_old)
+  do istep = 1,nmax
+     call random_number(r_new)
+     r_new(:) = r_old(:) + tau * (2.d0*r_new(:) - 1.d0)
+     psi_new = psi(a,r_new)
+     ratio = (psi_new / psi_old)**2
+     call random_number(v)
+     if (v < ratio) then
+        r_old(:) = r_new(:)
+        psi_old = psi_new
+        n_accep = n_accep + 1.d0
+     endif
+     norm = norm + 1.d0
+     energy = energy + e_loc(a,r_old)
+  end do
+  energy = energy / norm
+  accep  = n_accep / norm
+end subroutine metropolis_montecarlo
+
+program qmc
+  implicit none
+  double precision, parameter :: a = 0.9d0
+  double precision, parameter :: tau = 1.3d0
+  integer*8       , parameter :: nmax = 100000
+  integer         , parameter :: nruns = 30
+
+  integer :: irun
+  double precision :: X(nruns), Y(nruns)
+  double precision :: ave, err
+
+  do irun=1,nruns
+     call metropolis_montecarlo(a,nmax,tau,X(irun),Y(irun))
+  enddo
+  call ave_error(X,nruns,ave,err)
+  print *, 'E = ', ave, '+/-', err
+  call ave_error(Y,nruns,ave,err)
+  print *, 'A = ', ave, '+/-', err
+end program qmc
+
+
+ +
+
gfortran hydrogen.f90 qmc_stats.f90 qmc_metropolis.f90 -o qmc_metropolis
+./qmc_metropolis
+
+
+
+E =  -0.49478505004797046      +/-   2.0493795299184956E-004
+A =   0.51737800000000000      +/-   4.1827406733181444E-004
+
+
+
+
+
+ +
+

3.4 Gaussian random number generator

+

To obtain Gaussian-distributed random numbers, you can apply the Box Muller transform to uniform random numbers: @@ -1373,8 +1583,9 @@ z_2 &=& \sqrt{-2 \ln u_1} \sin(2 \pi u_2) \end{eqnarray*}

-Here is a Fortran implementation returning a Gaussian-distributed -n-dimensional vector \(\mathbf{z}\); +Below is a Fortran implementation returning a Gaussian-distributed +n-dimensional vector \(\mathbf{z}\). This will be useful for the +following sections.

@@ -1410,296 +1621,112 @@ n-dimensional vector \(\mathbf{z}\); end subroutine random_gauss

+
+
+
+

3.5 Generalized Metropolis algorithm

+
+

+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 +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}\): +

-Now the sampling probability can be inserted into the equation of the energy: +\[ A(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = \min \left( 1, + \frac{T(\mathbf{r}_{n+1} \rightarrow \mathbf{r}_{n}) P(\mathbf{r}_{n+1})} + {T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) P(\mathbf{r}_{n})} + \right) + \] +where \(T(\mathbf{r}_n \rightarrow \mathbf{r}_{n+1})\) is the +probability of transition from \(\mathbf{r}_n\) to +\(\mathbf{r}_{n+1}\). +

+ +

+In the previous example, we were using uniform random +numbers. Hence, the transition probability was

\[ - E = \frac{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r})\right]^2}{P(\mathbf{r})}\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r}) \right]^2}{P(\mathbf{r})} d\mathbf{r}} + T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) & = & + \text{constant} \]

-with the Gaussian probability +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 +\(\tau\), the transition probability becomes:

\[ - P(\mathbf{r}) = \frac{1}{(2 \pi)^{3/2}}\exp\left( -\frac{\mathbf{r}^2}{2} \right). + 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] + \] +

+ + +

+To sample even better the density, we can "push" the electrons +into in the regions of high probability, and "pull" them away from +the low-probability regions. This will mechanically increase the +acceptance ratios and improve the sampling. +

+ +

+To do this, we can add the drift vector +

+ +

+\[ + \frac{\nabla [ \Psi^2 ]}{\Psi^2} = 2 \frac{\nabla \Psi}{\Psi} + \]. +

+ +

+The numerical scheme becomes a drifted diffusion: +

+ +

+\[ + \mathbf{r}_{n+1} = \mathbf{r}_{n} + \tau \frac{\nabla + \Psi(\mathbf{r})}{\Psi(\mathbf{r})} + \chi \]

-As the coordinates are drawn with probability \(P(\mathbf{r})\), the -average energy can be computed as -

- -

-\[ - E \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\; - w_i = \frac{\left[\Psi(\mathbf{r}_i)\right]^2}{P(\mathbf{r}_i)} \delta \mathbf{r} - \] -

-
- - -
-

3.3.1 Exercise

-
-
-

-Modify the exercise of the previous section to sample with -Gaussian-distributed random numbers. Can you see an reduction in -the statistical error? -

- -
- -

-Python -

-
-
from hydrogen  import *
-from qmc_stats import *
-
-norm_gauss = 1./(2.*np.pi)**(1.5)
-def gaussian(r):
-    return norm_gauss * np.exp(-np.dot(r,r)*0.5)
-
-def MonteCarlo(a,nmax):
-    E = 0.
-    N = 0.
-    for istep in range(nmax):
-        r = np.random.normal(loc=0., scale=1.0, size=(3))
-        w = psi(a,r)
-        w = w*w / gaussian(r)
-        N += w
-        E += w * e_loc(a,r)
-    return E/N
-
-a = 0.9
-nmax = 100000
-X = [MonteCarlo(a,nmax) for i in range(30)]
-E, deltaE = ave_error(X)
-print(f"E = {E} +/- {deltaE}")
-
-
- - -

-Fortran -

-
-
double precision function gaussian(r)
-  implicit none
-  double precision, intent(in) :: r(3)
-  double precision, parameter :: norm_gauss = 1.d0/(2.d0*dacos(-1.d0))**(1.5d0)
-  gaussian = norm_gauss * dexp( -0.5d0 * (r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ))
-end function gaussian
-
-
-subroutine gaussian_montecarlo(a,nmax,energy)
-  implicit none
-  double precision, intent(in)  :: a
-  integer*8       , intent(in)  :: nmax 
-  double precision, intent(out) :: energy
-
-  integer*8 :: istep
-
-  double precision :: norm, r(3), w
-
-  double precision, external :: e_loc, psi, gaussian
-
-  energy = 0.d0
-  norm   = 0.d0
-  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)
-  end do
-  energy = energy / norm
-end subroutine gaussian_montecarlo
-
-program qmc
-  implicit none
-  double precision, parameter :: a = 0.9
-  integer*8       , parameter :: nmax = 100000
-  integer         , parameter :: nruns = 30
-
-  integer :: irun
-  double precision :: X(nruns)
-  double precision :: ave, err
-
-  do irun=1,nruns
-     call gaussian_montecarlo(a,nmax,X(irun))
-  enddo
-  call ave_error(X,nruns,ave,err)
-  print *, 'E = ', ave, '+/-', err
-end program qmc
-
-
- -
-
gfortran hydrogen.f90 qmc_stats.f90 qmc_gaussian.f90 -o qmc_gaussian
-./qmc_gaussian
-
-
- -
-E =  -0.49517104619091717      +/-   1.0685523607878961E-004
-
-
-
-
-
- -
-

3.4 Sampling with \(\Psi^2\)

-
-

-We will now use the square of the wave function to make the sampling: -

- -

-\[ - P(\mathbf{r}) = \left[\Psi(\mathbf{r})\right]^2 - \] -

- -

-The expression for the energy will be simplified to the average of -the local energies, each with a weight of 1. -

- -

-\[ - E \approx \frac{1}{M}\sum_{i=1}^M E_L(\mathbf{r}_i) - \] -

-
- - -
-

3.4.1 Importance sampling

-
-

-To generate the probability density \(\Psi^2\), we consider a -diffusion process characterized by a time-dependent density -\([\Psi(\mathbf{r},t)]^2\), which obeys the Fokker-Planck equation: -

- -

-\[ - \frac{\partial \Psi^2}{\partial t} = \sum_i D - \frac{\partial}{\partial \mathbf{r}_i} \left( - \frac{\partial}{\partial \mathbf{r}_i} - F_i(\mathbf{r}) \right) - [\Psi(\mathbf{r},t)]^2. - \] -

- -

-\(D\) is the diffusion constant and \(F_i\) is the i-th component of a -drift velocity caused by an external potential. For a stationary -density, \( \frac{\partial \Psi^2}{\partial t} = 0 \), so -

- -\begin{eqnarray*} -0 & = & \sum_i D -\frac{\partial}{\partial \mathbf{r}_i} \left( -\frac{\partial}{\partial \mathbf{r}_i} - F_i(\mathbf{r}) \right) -[\Psi(\mathbf{r})]^2 \\ -0 & = & \sum_i D -\frac{\partial}{\partial \mathbf{r}_i} \left( -\frac{\partial [\Psi(\mathbf{r})]^2}{\partial \mathbf{r}_i} - -F_i(\mathbf{r})\,[\Psi(\mathbf{r})]^2 \right) \\ -0 & = & -\frac{\partial^2 \Psi^2}{\partial \mathbf{r}_i^2} - -\frac{\partial F_i }{\partial \mathbf{r}_i}[\Psi(\mathbf{r})]^2 - -\frac{\partial \Psi^2}{\partial \mathbf{r}_i} F_i(\mathbf{r}) -\end{eqnarray*} - -

-we search for a drift function which satisfies -

- -

-\[ - \frac{\partial^2 \Psi^2}{\partial \mathbf{r}_i^2} = - [\Psi(\mathbf{r})]^2 \frac{\partial F_i }{\partial \mathbf{r}_i} + - \frac{\partial \Psi^2}{\partial \mathbf{r}_i} F_i(\mathbf{r}) - \] -

- -

-to obtain a second derivative on the left, we need the drift to be -of the form -\[ - F_i(\mathbf{r}) = g(\mathbf{r}) \frac{\partial \Psi^2}{\partial \mathbf{r}_i} - \] -

- -

-\[ - \frac{\partial^2 \Psi^2}{\partial \mathbf{r}_i^2} = - [\Psi(\mathbf{r})]^2 \frac{\partial - g(\mathbf{r})}{\partial \mathbf{r}_i}\frac{\partial \Psi^2}{\partial \mathbf{r}_i} + - [\Psi(\mathbf{r})]^2 g(\mathbf{r}) \frac{\partial^2 - \Psi^2}{\partial \mathbf{r}_i^2} + - \frac{\partial \Psi^2}{\partial \mathbf{r}_i} - g(\mathbf{r}) \frac{\partial \Psi^2}{\partial \mathbf{r}_i} - \] -

- -

-\(g = 1 / \Psi^2\) satisfies this equation, so -

- -

-\[ - F(\mathbf{r}) = \frac{\nabla [\Psi(\mathbf{r})]^2}{[\Psi(\mathbf{r})]^2} = 2 \frac{\nabla - \Psi(\mathbf{r})}{\Psi(\mathbf{r})} = 2 \nabla \left( \log \Psi(\mathbf{r}) \right) - \] -

- -

-In statistical mechanics, Fokker-Planck trajectories are generated -by a Langevin equation: -

- -

-\[ - \frac{\partial \mathbf{r}(t)}{\partial t} = 2D \frac{\nabla - \Psi(\mathbf{r}(t))}{\Psi} + \eta - \] -

- -

-where \(\eta\) is a normally-distributed fluctuating random force. -

- -

-Discretizing this differential equation gives the following drifted -diffusion scheme: -

- -

-\[ - \mathbf{r}_{n+1} = \mathbf{r}_{n} + \tau\, 2D \frac{\nabla - \Psi(\mathbf{r})}{\Psi(\mathbf{r})} + \chi - \] where \(\chi\) is a Gaussian random variable with zero mean and -variance \(\tau\,2D\). +variance \(\tau\). +The transition probability becomes: +

+ +

+\[ + 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] + \]

-
    -
  1. Exercise 1
    -
    + +
    +

    3.5.1 Exercise 1

    +

    Write a function to compute the drift vector \(\frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\). @@ -1732,189 +1759,15 @@ Write a function to compute the drift vector \(\frac{\nabla \Psi(\mathbf{r})}{\P

    -
  2. +
-
  • TODO Exercise 2
    -
    +
    +

    3.5.2 Exercise 2

    +

    -Sample \(\Psi^2\) approximately using the drifted diffusion scheme, -with a diffusion constant \(D=1/2\). You can use a time step of -0.001 a.u. -

    - -
    - -

    -Python -

    -
    -
    from hydrogen  import *
    -from qmc_stats import *
    -
    -def MonteCarlo(a,tau,nmax):
    -    sq_tau = np.sqrt(tau)
    -
    -    # Initialization
    -    E = 0.
    -    N = 0.
    -    r_old = np.random.normal(loc=0., scale=1.0, size=(3))
    -
    -    for istep in range(nmax):
    -        d_old = drift(a,r_old)
    -        chi = np.random.normal(loc=0., scale=1.0, size=(3))
    -        r_new = r_old + tau * d_old + chi*sq_tau
    -        N += 1.
    -        E += e_loc(a,r_new)
    -        r_old = r_new
    -    return E/N
    -
    -
    -a = 0.9
    -nmax = 100000
    -tau = 0.2
    -X = [MonteCarlo(a,tau,nmax) for i in range(30)]
    -E, deltaE = ave_error(X)
    -print(f"E = {E} +/- {deltaE}")
    -
    -
    - -

    -Fortran -

    -
    -
    subroutine variational_montecarlo(a,tau,nmax,energy)
    -  implicit none
    -  double precision, intent(in)  :: a, tau
    -  integer*8       , intent(in)  :: nmax 
    -  double precision, intent(out) :: energy
    -
    -  integer*8 :: istep
    -  double precision :: norm, r_old(3), r_new(3), d_old(3), sq_tau, chi(3)
    -  double precision, external :: e_loc
    -
    -  sq_tau = dsqrt(tau)
    -
    -  ! Initialization
    -  energy = 0.d0
    -  norm   = 0.d0
    -  call random_gauss(r_old,3)
    -
    -  do istep = 1,nmax
    -     call drift(a,r_old,d_old)
    -     call random_gauss(chi,3)
    -     r_new(:) = r_old(:) + tau * d_old(:) + chi(:)*sq_tau
    -     norm = norm + 1.d0
    -     energy = energy + e_loc(a,r_new)
    -     r_old(:) = r_new(:)
    -  end do
    -  energy = energy / norm
    -end subroutine variational_montecarlo
    -
    -program qmc
    -  implicit none
    -  double precision, parameter :: a = 0.9
    -  double precision, parameter :: tau = 0.2
    -  integer*8       , parameter :: nmax = 100000
    -  integer         , parameter :: nruns = 30
    -
    -  integer :: irun
    -  double precision :: X(nruns)
    -  double precision :: ave, err
    -
    -  do irun=1,nruns
    -     call variational_montecarlo(a,tau,nmax,X(irun))
    -  enddo
    -  call ave_error(X,nruns,ave,err)
    -  print *, 'E = ', ave, '+/-', err
    -end program qmc
    -
    -
    - -
    -
    gfortran hydrogen.f90 qmc_stats.f90 vmc.f90 -o vmc
    -./vmc
    -
    -
    - -
    -E =  -0.48584030499187431      +/-   1.0411743995438257E-004
    -
    -
    -
    -
  • - -
    - -
    -

    3.4.2 Metropolis algorithm

    -
    -

    -Discretizing the differential equation to generate the desired -probability density will suffer from a discretization error -leading to biases in the averages. The Metropolis-Hastings -sampling algorithm removes exactly the discretization errors, so -large time steps can be employed. -

    - -

    -After the new position \(\mathbf{r}_{n+1}\) has been computed, an -additional accept/reject step is performed. 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}\): -

    - -

    -\[ A(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = \min \left( 1, - \frac{g(\mathbf{r}_{n+1} \rightarrow \mathbf{r}_{n}) P(\mathbf{r}_{n+1})} - {g(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) P(\mathbf{r}_{n})} - \right) - \] -

    - -

    -In our Hydrogen atom example, \(P\) is \(\Psi^2\) and \(g\) is a -solution of the discretized Fokker-Planck equation: -

    - -\begin{eqnarray*} -P(r_{n}) &=& \Psi^2(\mathbf{r}_n) \\ -g(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) & = & -\frac{1}{(4\pi\,D\,\tau)^{3/2}} \exp \left[ - \frac{\left( -\mathbf{r}_{n+1} - \mathbf{r}_{n} - 2D \frac{\nabla -\Psi(\mathbf{r}_n)}{\Psi(\mathbf{r}_n)} \right)^2}{4D\,\tau} \right] -\end{eqnarray*} - -

    -The accept/reject step is the following: -

    -
      -
    • Compute \(A(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1})\).
    • -
    • Draw a uniform random number \(u\)
    • -
    • if \(u \le A(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1})\), accept -the move
    • -
    • if \(u>A(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1})\), reject -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. -

    -
    - -
      -
    1. 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. +Modify the previous program to introduce the drifted diffusion scheme. +(This is a necessary step for the next section).

      @@ -1952,8 +1805,8 @@ Adjust the time-step so that the acceptance rate is 0.5. d_old = d_new d2_old = d2_new psi_old = psi_new - N += 1. - E += e_loc(a,r_old) + N += 1. + E += e_loc(a,r_old) return E/N, accep_rate/N @@ -2003,8 +1856,8 @@ Adjust the time-step so that the acceptance rate is 0.5. 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)) + (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 @@ -2057,24 +1910,21 @@ A = 0.78861366666666655 +/- 3.5096729498002445E-004
      -
    2. -
    - -
    -

    4 TODO Diffusion Monte Carlo

    +
    +

    4 TODO Diffusion Monte Carlo

    -
    -

    4.1 Hydrogen atom

    +
    +

    4.1 Hydrogen atom

      -
    1. Exercise
      +
    2. Exercise

      @@ -2233,8 +2083,8 @@ A = 0.78861366666666655 +/- 3.5096729498002445E-004

      -
      -

      4.2 Dihydrogen

      +
      +

      4.2 Dihydrogen

      We will now consider the H2 molecule in a minimal basis composed of the @@ -2256,7 +2106,7 @@ the nuclei.

      Author: Anthony Scemama, Claudia Filippi

      -

      Created: 2021-01-19 Tue 09:46

      +

      Created: 2021-01-20 Wed 18:13

      Validate