1
0
mirror of https://github.com/TREX-CoE/qmc-lttc.git synced 2024-07-17 08:30:45 +02:00
qmc-lttc/QMC.org

2798 lines
79 KiB
Org Mode
Raw Normal View History

2021-01-03 18:45:58 +01:00
#+TITLE: Quantum Monte Carlo
#+AUTHOR: Anthony Scemama, Claudia Filippi
2021-01-26 10:02:38 +01:00
#+LANGUAGE: en
2021-01-28 15:04:57 +01:00
#+INFOJS_OPT: toc:t mouse:underline path:org-info.js
2021-01-03 18:45:58 +01:00
#+STARTUP: latexpreview
2021-01-21 12:49:12 +01:00
#+LATEX_CLASS: report
#+LATEX_HEADER_EXTRA: \usepackage{minted}
2021-01-11 20:54:40 +01:00
#+HTML_HEAD: <link rel="stylesheet" title="Standard" href="worg.css" type="text/css" />
2021-01-26 10:02:38 +01:00
2021-01-27 13:04:32 +01:00
#+OPTIONS: H:4 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
2021-01-21 12:49:12 +01:00
#+OPTIONS: TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
2021-02-04 13:15:14 +01:00
# EXCLUDE_TAGS: solution solution2 noexport
2021-02-03 17:58:09 +01:00
#+EXCLUDE_TAGS: solution noexport
2021-01-21 12:49:12 +01:00
#+BEGIN_SRC elisp :output none :exports none
(setq org-latex-listings 'minted
org-latex-packages-alist '(("" "minted"))
org-latex-pdf-process
'("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
"pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
"pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
(setq org-latex-minted-options '(("breaklines" "true")
("breakanywhere" "true")))
(setq org-latex-minted-options
'(("frame" "lines")
("fontsize" "\\scriptsize")
("linenos" "")))
(org-beamer-export-to-pdf)
#+END_SRC
#+RESULTS:
: /home/scemama/TREX/qmc-lttc/QMC.pdf
2021-01-03 18:45:58 +01:00
* Introduction
2021-01-30 13:19:56 +01:00
This website contains the QMC tutorial of the 2021 LTTC winter school
2021-01-27 13:04:32 +01:00
[[https://www.irsamc.ups-tlse.fr/lttc/Luchon][Tutorials in Theoretical Chemistry]].
2021-01-07 11:07:18 +01:00
We propose different exercises to understand quantum Monte Carlo (QMC)
methods. In the first section, we start with the computation of the energy of a
2021-01-07 11:07:18 +01:00
hydrogen atom using numerical integration. The goal of this section is
to familarize yourself with the concept of /local energy/.
Then, we introduce the variational Monte Carlo (VMC) method which
2021-01-07 11:07:18 +01:00
computes a statistical estimate of the expectation value of the energy
associated with a given wave function, and apply this approach to the
hydrogen atom.
Finally, we present the diffusion Monte Carlo (DMC) method which
2021-01-30 13:19:56 +01:00
we use here to estimate the exact energy of the hydrogen atom and of the H_2 molecule,
starting from an approximate wave function.
2021-01-07 11:07:18 +01:00
2021-02-02 13:30:11 +01:00
Code examples will be given in Python3 and Fortran. You can use
2021-02-02 11:28:34 +01:00
whatever language you prefer to write the programs.
2021-01-11 18:41:36 +01:00
We consider the stationary solution of the Schrödinger equation, so
the wave functions considered here are real: for an $N$ electron
system where the electrons move in the 3-dimensional space,
$\Psi : \mathbb{R}^{3N} \rightarrow \mathbb{R}$. In addition, $\Psi$
is defined everywhere, continuous, and infinitely differentiable.
2021-01-03 18:45:58 +01:00
2021-01-25 23:52:53 +01:00
All the quantities are expressed in /atomic units/ (energies,
coordinates, etc).
2021-01-03 18:45:58 +01:00
2021-01-30 22:59:13 +01:00
** Energy and local energy
2021-01-03 18:45:58 +01:00
2021-01-30 22:40:46 +01:00
For a given system with Hamiltonian $\hat{H}$ and wave function $\Psi$, we define the local energy as
2021-01-07 11:07:18 +01:00
$$
E_L(\mathbf{r}) = \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})},
$$
2021-01-03 18:45:58 +01:00
2021-01-30 22:40:46 +01:00
where $\mathbf{r}$ denotes the 3N-dimensional electronic coordinates.
The electronic energy of a system, $E$, can be rewritten in terms of the
local energy $E_L(\mathbf{r})$ as
2021-01-03 18:45:58 +01:00
2021-01-30 13:19:56 +01:00
\begin{eqnarray*}
E & = & \frac{\langle \Psi| \hat{H} | \Psi\rangle}{\langle \Psi |\Psi \rangle}
2021-02-01 13:20:26 +01:00
= \frac{\int \Psi(\mathbf{r})\, \hat{H} \Psi(\mathbf{r})\, d\mathbf{r}}{\int |\Psi(\mathbf{r}) |^2 d\mathbf{r}} \\
& = & \frac{\int |\Psi(\mathbf{r})|^2\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int |\Psi(\mathbf{r}) |^2 d\mathbf{r}}
= \frac{\int |\Psi(\mathbf{r})|^2\, E_L(\mathbf{r})\,d\mathbf{r}}{\int |\Psi(\mathbf{r}) |^2 d\mathbf{r}}
2021-01-30 13:19:56 +01:00
\end{eqnarray*}
2021-01-30 22:40:46 +01:00
2021-02-02 11:28:34 +01:00
For few dimensions, one can easily compute $E$ by evaluating the
integrals on a grid but, for a high number of dimensions, one can
resort to Monte Carlo techniques to compute $E$.
2021-01-30 13:19:56 +01:00
2021-02-02 11:28:34 +01:00
To this aim, recall that the probabilistic /expected value/ of an
arbitrary function $f(x)$ with respect to a probability density
function $P(x)$ is given by
2021-01-03 18:45:58 +01:00
2021-02-02 11:28:34 +01:00
$$ \langle f \rangle_P = \int_{-\infty}^\infty P(x)\, f(x)\,dx, $$
2021-01-03 18:45:58 +01:00
2021-02-02 11:28:34 +01:00
where a probability density function $P(x)$ is non-negative
2021-01-11 18:41:36 +01:00
and integrates to one:
2021-01-03 18:45:58 +01:00
2021-01-31 09:39:10 +01:00
$$ \int_{-\infty}^\infty P(x)\,dx = 1. $$
2021-01-11 18:41:36 +01:00
2021-01-30 22:40:46 +01:00
Similarly, we can view the the energy of a system, $E$, as the expected value of the local energy with respect to
2021-02-01 13:20:26 +01:00
a probability density $P(\mathbf{r})$ defined in 3$N$ dimensions:
2021-01-30 22:40:46 +01:00
2021-02-02 11:28:34 +01:00
$$ E = \int E_L(\mathbf{r}) P(\mathbf{r})\,d\mathbf{r} \equiv \langle E_L \rangle_{P}\,, $$
2021-01-30 22:40:46 +01:00
where the probability density is given by the square of the wave function:
2021-02-01 21:56:01 +01:00
$$ P(\mathbf{r}) = \frac{|\Psi(\mathbf{r})|^2}{\int |\Psi(\mathbf{r})|^2 d\mathbf{r}}\,. $$
2021-01-30 22:40:46 +01:00
2021-02-02 11:28:34 +01:00
If we can sample $N_{\rm MC}$ configurations $\{\mathbf{r}\}$
distributed as $P$, we can estimate $E$ as the average of the local
energy computed over these configurations:
2021-01-30 22:40:46 +01:00
2021-02-01 13:20:26 +01:00
$$ E \approx \frac{1}{N_{\rm MC}} \sum_{i=1}^{N_{\rm MC}} E_L(\mathbf{r}_i) \,. $$
2021-01-30 22:40:46 +01:00
2021-01-30 22:59:13 +01:00
* Numerical evaluation of the energy of the hydrogen atom
2021-01-11 18:41:36 +01:00
2021-01-30 22:40:46 +01:00
In this section, we consider the hydrogen atom with the following
wave function:
2021-01-11 18:41:36 +01:00
2021-01-30 22:40:46 +01:00
$$
\Psi(\mathbf{r}) = \exp(-a |\mathbf{r}|)
$$
2021-01-11 18:41:36 +01:00
2021-01-30 22:40:46 +01:00
We will first verify that, for a particular value of $a$, $\Psi$ is an
eigenfunction of the Hamiltonian
2021-01-11 18:41:36 +01:00
2021-01-30 22:40:46 +01:00
$$
\hat{H} = \hat{T} + \hat{V} = - \frac{1}{2} \Delta - \frac{1}{|\mathbf{r}|}
$$
2021-01-11 18:41:36 +01:00
2021-01-30 22:59:13 +01:00
To do that, we will compute the local energy and check whether it is constant.
2021-01-11 18:41:36 +01:00
2021-01-03 18:45:58 +01:00
** Local energy
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle hydrogen.py
:header-args:f90: :tangle hydrogen.f90
:END:
2021-01-12 01:01:52 +01:00
2021-01-30 22:59:13 +01:00
You will now program all quantities needed to compute the local energy of the H atom for the given wave function.
2021-01-21 23:24:21 +01:00
Write all the functions of this section in a single file :
~hydrogen.py~ if you use Python, or ~hydrogen.f90~ is you use
Fortran.
2021-01-26 00:22:37 +01:00
#+begin_note
- When computing a square root in $\mathbb{R}$, *always* make sure
that the argument of the square root is non-negative.
- When you divide, *always* make sure that you will not divide by zero
If a /floating-point exception/ can occur, you should make a test
to catch the error.
#+end_note
2021-01-12 01:01:52 +01:00
*** Exercise 1
#+begin_exercise
Write a function which computes the potential at $\mathbf{r}$.
2021-02-02 11:28:34 +01:00
The function accepts a 3-dimensional vector =r= as input argument
2021-01-07 11:07:18 +01:00
and returns the potential.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-01-11 20:54:40 +01:00
$\mathbf{r}=\left( \begin{array}{c} x \\ y\\ z\end{array} \right)$, so
2021-01-07 11:07:18 +01:00
$$
2021-01-11 20:54:40 +01:00
V(\mathbf{r}) = -\frac{1}{\sqrt{x^2 + y^2 + z^2}}
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-21 23:24:21 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-21 23:24:21 +01:00
import numpy as np
def potential(r):
# TODO
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
#+BEGIN_SRC f90 :tangle none
double precision function potential(r)
implicit none
double precision, intent(in) :: r(3)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
end function potential
#+END_SRC
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
*Python*
2021-01-11 20:54:40 +01:00
#+BEGIN_SRC python :results none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-03 18:45:58 +01:00
import numpy as np
def potential(r):
2021-01-26 00:22:37 +01:00
distance = np.sqrt(np.dot(r,r))
assert (distance > 0)
return -1. / distance
2021-01-11 20:54:40 +01:00
#+END_SRC
2021-01-07 10:01:55 +01:00
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-11 20:54:40 +01:00
#+BEGIN_SRC f90
2021-01-03 18:45:58 +01:00
double precision function potential(r)
implicit none
double precision, intent(in) :: r(3)
2021-01-29 13:23:00 +01:00
double precision :: distance
2021-01-26 00:22:37 +01:00
distance = dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) )
2021-01-29 13:23:00 +01:00
2021-01-26 00:22:37 +01:00
if (distance > 0.d0) then
2021-01-29 13:23:00 +01:00
potential = -1.d0 / distance
2021-01-26 00:22:37 +01:00
else
stop 'potential at r=0.d0 diverges'
end if
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
end function potential
2021-01-11 20:54:40 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-30 22:59:13 +01:00
*** Exercise 2
2021-01-12 01:01:52 +01:00
#+begin_exercise
Write a function which computes the wave function at $\mathbf{r}$.
2021-01-07 11:07:18 +01:00
The function accepts a scalar =a= and a 3-dimensional vector =r= as
input arguments, and returns a scalar.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-11 20:54:40 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-01-21 23:24:21 +01:00
def psi(a, r):
# TODO
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC f90 :tangle none
2021-01-21 23:24:21 +01:00
double precision function psi(a, r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
2021-01-21 23:24:21 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-21 23:24:21 +01:00
end function psi
#+END_SRC
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
*Python*
#+BEGIN_SRC python :results none
def psi(a, r):
return np.exp(-a*np.sqrt(np.dot(r,r)))
#+END_SRC
*Fortran*
2021-01-11 20:54:40 +01:00
#+BEGIN_SRC f90
2021-01-03 18:45:58 +01:00
double precision function psi(a, r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
psi = dexp(-a * dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ))
end function psi
2021-01-11 20:54:40 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*** Exercise 3
2021-01-12 01:01:52 +01:00
#+begin_exercise
Write a function which computes the local kinetic energy at $\mathbf{r}$.
2021-01-07 11:07:18 +01:00
The function accepts =a= and =r= as input arguments and returns the
local kinetic energy.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-02-02 11:28:34 +01:00
The local kinetic energy is defined as $$T_L(\mathbf{r}) = -\frac{1}{2}\frac{\Delta \Psi(\mathbf{r})}{\Psi(\mathbf{r})}.$$
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
We differentiate $\Psi$ with respect to $x$:
2021-01-03 18:45:58 +01:00
2021-02-02 13:30:11 +01:00
\[ \Psi(\mathbf{r}) = \exp(-a\,|\mathbf{r}|) \]
2021-01-11 20:54:40 +01:00
\[\frac{\partial \Psi}{\partial x}
= \frac{\partial \Psi}{\partial |\mathbf{r}|} \frac{\partial |\mathbf{r}|}{\partial x}
= - \frac{a\,x}{|\mathbf{r}|} \Psi(\mathbf{r}) \]
2021-01-07 11:07:18 +01:00
and we differentiate a second time:
$$
\frac{\partial^2 \Psi}{\partial x^2} =
2021-01-11 20:54:40 +01:00
\left( \frac{a^2\,x^2}{|\mathbf{r}|^2} -
\frac{a(y^2+z^2)}{|\mathbf{r}|^{3}} \right) \Psi(\mathbf{r}).
2021-01-07 11:07:18 +01:00
$$
The Laplacian operator $\Delta = \frac{\partial^2}{\partial x^2} +
\frac{\partial^2}{\partial y^2} + \frac{\partial^2}{\partial z^2}$
applied to the wave function gives:
$$
2021-01-30 23:29:19 +01:00
\Delta \Psi (\mathbf{r}) = \left(a^2 - \frac{2a}{\mathbf{|r|}} \right) \Psi(\mathbf{r})\,.
2021-01-07 11:07:18 +01:00
$$
2021-01-30 23:29:19 +01:00
Therefore, the local kinetic energy is
2021-01-07 11:07:18 +01:00
$$
2021-02-02 11:28:34 +01:00
T_L (\mathbf{r}) = -\frac{1}{2}\left(a^2 - \frac{2a}{\mathbf{|r|}} \right)
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-01-21 23:24:21 +01:00
def kinetic(a,r):
# TODO
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC f90 :tangle none
2021-01-21 23:24:21 +01:00
double precision function kinetic(a,r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
2021-01-21 23:24:21 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-21 23:24:21 +01:00
end function kinetic
#+END_SRC
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
*Python*
#+BEGIN_SRC python :results none
def kinetic(a,r):
distance = np.sqrt(np.dot(r,r))
assert (distance > 0.)
2021-01-29 13:23:00 +01:00
return a * (1./distance - 0.5 * a)
2021-01-27 13:04:32 +01:00
#+END_SRC
*Fortran*
2021-01-11 20:54:40 +01:00
#+BEGIN_SRC f90
2021-01-03 18:45:58 +01:00
double precision function kinetic(a,r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
double precision :: distance
2021-01-26 00:22:37 +01:00
distance = dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) )
2021-01-29 13:23:00 +01:00
2021-01-26 00:22:37 +01:00
if (distance > 0.d0) then
2021-01-29 13:23:00 +01:00
kinetic = a * (1.d0 / distance - 0.5d0 * a)
2021-01-26 00:22:37 +01:00
else
stop 'kinetic energy diverges at r=0'
end if
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
end function kinetic
2021-01-11 20:54:40 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*** Exercise 4
2021-01-12 01:01:52 +01:00
#+begin_exercise
2021-01-21 23:24:21 +01:00
Write a function which computes the local energy at $\mathbf{r}$,
using the previously defined functions.
The function accepts =a= and =r= as input arguments and returns the
local kinetic energy.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
$$
2021-01-11 20:54:40 +01:00
E_L(\mathbf{r}) = -\frac{1}{2} \frac{\Delta \Psi}{\Psi} (\mathbf{r}) + V(\mathbf{r})
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-11 20:54:40 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-01-21 23:24:21 +01:00
def e_loc(a,r):
#TODO
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
2021-02-02 11:28:34 +01:00
#+begin_note
When you call a function in Fortran, you need to declare its
return type.
You might by accident choose a function name which is the
same as an internal function of Fortran. So it is recommended to
*always* use the keyword ~external~ to make sure the function you
are calling is yours.
#+end_note
#+BEGIN_SRC f90 :tangle none
2021-01-21 23:24:21 +01:00
double precision function e_loc(a,r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
2021-02-02 11:28:34 +01:00
double precision, external :: kinetic
double precision, external :: potential
2021-01-21 23:24:21 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-21 23:24:21 +01:00
end function e_loc
2021-02-02 11:28:34 +01:00
#+END_SRC
2021-01-21 23:24:21 +01:00
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
*Python*
#+BEGIN_SRC python :results none
def e_loc(a,r):
return kinetic(a,r) + potential(r)
#+END_SRC
*Fortran*
2021-01-11 20:54:40 +01:00
#+BEGIN_SRC f90
2021-01-03 18:45:58 +01:00
double precision function e_loc(a,r)
implicit none
double precision, intent(in) :: a, r(3)
2021-01-29 13:23:00 +01:00
2021-02-02 11:28:34 +01:00
double precision, external :: kinetic
double precision, external :: potential
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
e_loc = kinetic(a,r) + potential(r)
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
end function e_loc
2021-01-11 20:54:40 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*** Exercise 5
#+begin_exercise
Find the theoretical value of $a$ for which $\Psi$ is an eigenfunction of $\hat{H}$.
#+end_exercise
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
\begin{eqnarray*}
E &=& \frac{\hat{H} \Psi}{\Psi} = - \frac{1}{2} \frac{\Delta \Psi}{\Psi} -
\frac{1}{|\mathbf{r}|} \\
&=& -\frac{1}{2}\left(a^2 - \frac{2a}{\mathbf{|r|}} \right) -
\frac{1}{|\mathbf{r}|} \\
&=&
-\frac{1}{2} a^2 + \frac{a-1}{\mathbf{|r|}}
\end{eqnarray*}
2021-02-01 13:20:26 +01:00
$a=1$ cancels the $1/|r|$ term, and makes the energy constant and
2021-01-27 13:04:32 +01:00
equal to -0.5 atomic units.
2021-01-11 18:41:36 +01:00
** Plot of the local energy along the $x$ axis
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle plot_hydrogen.py
:header-args:f90: :tangle plot_hydrogen.f90
:END:
2021-01-12 01:01:52 +01:00
2021-02-02 11:28:34 +01:00
The program you will write in this section will be written in
another file (~plot_hydrogen.py~ or ~plot_hydrogen.f90~ for
example).
It will use the functions previously defined.
In Python, you should put at the beginning of the file
#+BEGIN_SRC python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-02-02 11:28:34 +01:00
from hydrogen import e_loc
#+END_SRC
to be able to use the ~e_loc~ function of the ~hydrogen.py~ file.
In Fortran, you will need to compile all the source files together:
#+begin_src sh :exports both
gfortran hydrogen.f90 plot_hydrogen.f90 -o plot_hydrogen
#+end_src
2021-01-26 00:22:37 +01:00
2021-01-12 01:01:52 +01:00
*** Exercise
2021-02-02 11:28:34 +01:00
2021-01-12 01:01:52 +01:00
#+begin_exercise
For multiple values of $a$ (0.1, 0.2, 0.5, 1., 1.5, 2.), plot the
2021-01-21 23:24:21 +01:00
local energy along the $x$ axis. In Python, you can use matplotlib
for example. In Fortran, it is convenient to write in a text file
the values of $x$ and $E_L(\mathbf{r})$ for each point, and use
2021-02-02 11:28:34 +01:00
Gnuplot to plot the files. With Gnuplot, you will need 2 blank
lines to separate the data corresponding to different values of $a$.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-02-02 11:28:34 +01:00
#+begin_note
The potential and the kinetic energy both diverge at $r=0$, so we
choose a grid which does not contain the origin to avoid numerical issues.
#+end_note
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-03 18:45:58 +01:00
import numpy as np
import matplotlib.pyplot as plt
from hydrogen import e_loc
x=np.linspace(-5,5)
2021-01-21 23:24:21 +01:00
plt.figure(figsize=(10,5))
2021-01-03 18:45:58 +01:00
2021-01-21 23:24:21 +01:00
# TODO
plt.tight_layout()
plt.legend()
plt.savefig("plot_py.png")
#+end_src
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-25 23:52:53 +01:00
#+begin_src f90 :tangle none
2021-01-21 23:24:21 +01:00
program plot
implicit none
double precision, external :: e_loc
2021-01-03 18:45:58 +01:00
2021-01-21 23:24:21 +01:00
double precision :: x(50), dx
integer :: i, j
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
! TODO
end program plot
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-21 23:24:21 +01:00
2021-01-25 23:52:53 +01:00
To compile and run:
2021-01-21 23:24:21 +01:00
2021-01-25 23:52:53 +01:00
#+begin_src sh :exports both
2021-01-21 23:24:21 +01:00
gfortran hydrogen.f90 plot_hydrogen.f90 -o plot_hydrogen
./plot_hydrogen > data
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-21 23:24:21 +01:00
2021-01-25 23:52:53 +01:00
To plot the data using Gnuplot:
2021-01-21 23:24:21 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src gnuplot :file plot.png :exports code
2021-01-21 23:24:21 +01:00
set grid
set xrange [-5:5]
set yrange [-2:1]
plot './data' index 0 using 1:2 with lines title 'a=0.1', \
'./data' index 1 using 1:2 with lines title 'a=0.2', \
'./data' index 2 using 1:2 with lines title 'a=0.5', \
'./data' index 3 using 1:2 with lines title 'a=1.0', \
'./data' index 4 using 1:2 with lines title 'a=1.5', \
'./data' index 5 using 1:2 with lines title 'a=2.0'
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-21 23:24:21 +01:00
2021-02-04 13:15:14 +01:00
**** Solution :solution2:
2021-01-27 13:04:32 +01:00
*Python*
#+BEGIN_SRC python :results none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-27 13:04:32 +01:00
import numpy as np
import matplotlib.pyplot as plt
from hydrogen import e_loc
x=np.linspace(-5,5)
plt.figure(figsize=(10,5))
for a in [0.1, 0.2, 0.5, 1., 1.5, 2.]:
y=np.array([ e_loc(a, np.array([t,0.,0.]) ) for t in x])
plt.plot(x,y,label=f"a={a}")
plt.tight_layout()
plt.legend()
plt.savefig("plot_py.png")
#+end_src
#+RESULTS:
[[./plot_py.png]]
*Fortran*
2021-01-25 23:52:53 +01:00
#+begin_src f90
2021-01-03 18:45:58 +01:00
program plot
implicit none
double precision, external :: e_loc
double precision :: x(50), energy, dx, r(3), a(6)
integer :: i, j
a = (/ 0.1d0, 0.2d0, 0.5d0, 1.d0, 1.5d0, 2.d0 /)
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
r(:) = 0.d0
do j=1,size(a)
print *, '# a=', a(j)
do i=1,size(x)
r(1) = x(i)
energy = e_loc( a(j), r )
print *, x(i), energy
end do
print *, ''
print *, ''
end do
end program plot
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src sh :exports none
2021-01-03 18:45:58 +01:00
gfortran hydrogen.f90 plot_hydrogen.f90 -o plot_hydrogen
./plot_hydrogen > data
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src gnuplot :file plot.png :exports results
2021-01-03 18:45:58 +01:00
set grid
set xrange [-5:5]
set yrange [-2:1]
plot './data' index 0 using 1:2 with lines title 'a=0.1', \
'./data' index 1 using 1:2 with lines title 'a=0.2', \
'./data' index 2 using 1:2 with lines title 'a=0.5', \
'./data' index 3 using 1:2 with lines title 'a=1.0', \
'./data' index 4 using 1:2 with lines title 'a=1.5', \
'./data' index 5 using 1:2 with lines title 'a=2.0'
2021-01-25 23:52:53 +01:00
#+end_src
#+RESULTS:
[[file:plot.png]]
2021-01-03 18:45:58 +01:00
2021-01-25 23:52:53 +01:00
** Numerical estimation of the energy
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle energy_hydrogen.py
:header-args:f90: :tangle energy_hydrogen.f90
:END:
2021-01-11 20:54:40 +01:00
If the space is discretized in small volume elements $\mathbf{r}_i$
of size $\delta \mathbf{r}$, the expression of $\langle E_L \rangle_{\Psi^2}$
becomes a weighted average of the local energy, where the weights
2021-01-30 23:29:19 +01:00
are the values of the wave function square at $\mathbf{r}_i$
2021-01-11 20:54:40 +01:00
multiplied by the volume element:
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
$$
2021-01-11 18:41:36 +01:00
\langle E \rangle_{\Psi^2} \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\;
2021-02-02 13:30:11 +01:00
w_i = \left|\Psi(\mathbf{r}_i)\right|^2 \delta \mathbf{r}
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-11 20:54:40 +01:00
#+begin_note
The energy is biased because:
2021-01-11 18:41:36 +01:00
- The volume elements are not infinitely small (discretization error)
- The energy is evaluated only inside the box (incompleteness of the space)
2021-01-11 20:54:40 +01:00
#+end_note
2021-01-07 10:01:55 +01:00
2021-01-12 01:01:52 +01:00
*** Exercise
#+begin_exercise
2021-01-30 23:29:19 +01:00
Compute a numerical estimate of the energy using a grid of
2021-01-12 01:01:52 +01:00
$50\times50\times50$ points in the range $(-5,-5,-5) \le
\mathbf{r} \le (5,5,5)$.
#+end_exercise
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-25 23:52:53 +01:00
import numpy as np
from hydrogen import e_loc, psi
interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3
r = np.array([0.,0.,0.])
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
# TODO
print(f"a = {a} \t E = {E}")
#+end_src
2021-01-27 13:04:32 +01:00
*Fortran*
#+begin_src f90
program energy_hydrogen
implicit none
double precision, external :: e_loc, psi
double precision :: x(50), w, delta, energy, dx, r(3), a(6), norm
integer :: i, k, l, j
a = (/ 0.1d0, 0.2d0, 0.5d0, 1.d0, 1.5d0, 2.d0 /)
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
do j=1,size(a)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
print *, 'a = ', a(j), ' E = ', energy
end do
end program energy_hydrogen
#+end_src
To compile the Fortran and run it:
#+begin_src sh :results output :exports code
gfortran hydrogen.f90 energy_hydrogen.f90 -o energy_hydrogen
./energy_hydrogen
#+end_src
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results none :exports both
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-03 18:45:58 +01:00
import numpy as np
from hydrogen import e_loc, psi
2021-01-11 18:41:36 +01:00
interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3
2021-01-07 11:07:18 +01:00
2021-01-11 18:41:36 +01:00
r = np.array([0.,0.,0.])
2021-01-07 11:07:18 +01:00
2021-01-11 18:41:36 +01:00
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
2021-01-29 13:23:00 +01:00
E = 0.
2021-01-11 18:41:36 +01:00
norm = 0.
2021-01-29 13:23:00 +01:00
2021-01-25 23:52:53 +01:00
for x in interval:
r[0] = x
for y in interval:
r[1] = y
for z in interval:
r[2] = z
2021-01-29 13:23:00 +01:00
2021-01-25 23:52:53 +01:00
w = psi(a,r)
w = w * w * delta
2021-01-29 13:23:00 +01:00
2021-01-25 23:52:53 +01:00
E += w * e_loc(a,r)
norm += w
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
E = E / norm
print(f"a = {a} \t E = {E}")
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-07 11:07:18 +01:00
2021-01-25 23:52:53 +01:00
#+RESULTS:
: a = 0.1 E = -0.24518438948809218
: a = 0.2 E = -0.26966057967803525
: a = 0.5 E = -0.3856357612517407
: a = 0.9 E = -0.49435709786716214
: a = 1.0 E = -0.5
: a = 1.5 E = -0.39242967082602226
: a = 2.0 E = -0.08086980667844901
2021-01-07 11:07:18 +01:00
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-25 23:52:53 +01:00
#+begin_src f90
2021-01-03 18:45:58 +01:00
program energy_hydrogen
implicit none
double precision, external :: e_loc, psi
double precision :: x(50), w, delta, energy, dx, r(3), a(6), norm
2021-01-29 13:23:00 +01:00
integer :: i, k, l, j
2021-01-03 18:45:58 +01:00
a = (/ 0.1d0, 0.2d0, 0.5d0, 1.d0, 1.5d0, 2.d0 /)
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
delta = dx**3
r(:) = 0.d0
do j=1,size(a)
energy = 0.d0
2021-01-29 13:23:00 +01:00
norm = 0.d0
2021-01-03 18:45:58 +01:00
do i=1,size(x)
r(1) = x(i)
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
do k=1,size(x)
r(2) = x(k)
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
do l=1,size(x)
r(3) = x(l)
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
w = psi(a(j),r)
w = w * w * delta
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
energy = energy + w * e_loc(a(j), r)
norm = norm + w
end do
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-03 18:45:58 +01:00
energy = energy / norm
print *, 'a = ', a(j), ' E = ', energy
end do
end program energy_hydrogen
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src sh :results output :exports results
2021-01-03 18:45:58 +01:00
gfortran hydrogen.f90 energy_hydrogen.f90 -o energy_hydrogen
./energy_hydrogen
2021-01-25 23:52:53 +01:00
#+end_src
2021-01-03 18:45:58 +01:00
2021-01-25 23:52:53 +01:00
#+RESULTS:
: a = 0.10000000000000001 E = -0.24518438948809140
: a = 0.20000000000000001 E = -0.26966057967803236
: a = 0.50000000000000000 E = -0.38563576125173815
: a = 1.0000000000000000 E = -0.50000000000000000
: a = 1.5000000000000000 E = -0.39242967082602065
: a = 2.0000000000000000 E = -8.0869806678448772E-002
** Variance of the local energy
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle variance_hydrogen.py
:header-args:f90: :tangle variance_hydrogen.f90
:END:
2021-01-11 18:41:36 +01:00
The variance of the local energy is a functional of $\Psi$
which measures the magnitude of the fluctuations of the local
2021-01-25 23:52:53 +01:00
energy associated with $\Psi$ around its average:
2021-01-07 11:07:18 +01:00
$$
2021-02-01 13:20:26 +01:00
\sigma^2(E_L) = \frac{\int |\Psi(\mathbf{r})|^2\, \left[
E_L(\mathbf{r}) - E \right]^2 \, d\mathbf{r}}{\int |\Psi(\mathbf{r}) |^2 d\mathbf{r}}
2021-01-07 11:07:18 +01:00
$$
2021-01-19 10:45:36 +01:00
which can be simplified as
2021-01-25 23:52:53 +01:00
$$ \sigma^2(E_L) = \langle E_L^2 \rangle_{\Psi^2} - \langle E_L \rangle_{\Psi^2}^2.$$
2021-01-07 11:07:18 +01:00
2021-01-11 18:41:36 +01:00
If the local energy is constant (i.e. $\Psi$ is an eigenfunction of
$\hat{H}$) the variance is zero, so the variance of the local
energy can be used as a measure of the quality of a wave function.
2021-01-19 10:45:36 +01:00
*** Exercise (optional)
#+begin_exercise
Prove that :
2021-02-01 13:51:59 +01:00
$$\langle \left( E - \langle E \rangle_{\Psi^2} \right)^2\rangle_{\Psi^2} = \langle E^2 \rangle_{\Psi^2} - \langle E \rangle_{\Psi^2}^2 $$
2021-01-19 10:45:36 +01:00
#+end_exercise
2021-01-27 15:21:44 +01:00
**** Solution :solution:
$\bar{E} = \langle E \rangle$ is a constant, so $\langle \bar{E}
\rangle = \bar{E}$ .
\begin{eqnarray*}
2021-02-01 13:51:59 +01:00
\langle (E - \bar{E})^2 \rangle & = &
2021-01-27 15:21:44 +01:00
\langle E^2 - 2 E \bar{E} + \bar{E}^2 \rangle \\
&=& \langle E^2 \rangle - 2 \langle E \bar{E} \rangle + \langle \bar{E}^2 \rangle \\
&=& \langle E^2 \rangle - 2 \langle E \rangle \bar{E} + \bar{E}^2 \\
&=& \langle E^2 \rangle - 2 \bar{E}^2 + \bar{E}^2 \\
&=& \langle E^2 \rangle - \bar{E}^2 \\
&=& \langle E^2 \rangle - \langle E \rangle^2 \\
\end{eqnarray*}
2021-01-12 01:01:52 +01:00
*** Exercise
#+begin_exercise
2021-01-27 15:21:44 +01:00
Add the calculation of the variance to the previous code, and
2021-01-30 23:29:19 +01:00
compute a numerical estimate of the variance of the local energy using
2021-01-27 15:21:44 +01:00
a grid of $50\times50\times50$ points in the range $(-5,-5,-5) \le
\mathbf{r} \le (5,5,5)$ for different values of $a$.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-25 23:52:53 +01:00
#+begin_src python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-27 15:21:44 +01:00
import numpy as np from hydrogen import e_loc, psi
2021-01-25 23:52:53 +01:00
2021-01-29 13:23:00 +01:00
interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3
2021-01-25 23:52:53 +01:00
r = np.array([0.,0.,0.])
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
2021-01-29 13:23:00 +01:00
2021-01-25 23:52:53 +01:00
# TODO
2021-01-29 13:23:00 +01:00
2021-01-27 15:21:44 +01:00
print(f"a = {a} \t E = {E:10.8f} \t \sigma^2 = {s2:10.8f}")
#+end_src
2021-01-25 23:52:53 +01:00
2021-01-27 13:04:32 +01:00
*Fortran*
#+begin_src f90 :tangle none
2021-01-29 13:23:00 +01:00
program variance_hydrogen
implicit none
double precision :: x(50), w, delta, energy, energy2
double precision :: dx, r(3), a(6), norm, e_tmp, s2
integer :: i, k, l, j
double precision, external :: e_loc, psi
2021-01-27 13:04:32 +01:00
a = (/ 0.1d0, 0.2d0, 0.5d0, 1.d0, 1.5d0, 2.d0 /)
2021-01-29 13:23:00 +01:00
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
do j=1,size(a)
! TODO
print *, 'a = ', a(j), ' E = ', energy
end do
end program variance_hydrogen
#+end_src
2021-01-27 13:04:32 +01:00
To compile and run:
#+begin_src sh :results output :exports both
gfortran hydrogen.f90 variance_hydrogen.f90 -o variance_hydrogen
2021-01-29 13:23:00 +01:00
./variance_hydrogen
#+end_src
2021-01-27 13:04:32 +01:00
**** Solution :solution:
*Python*
2021-01-29 13:23:00 +01:00
#+BEGIN_SRC python :results none :exports both
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-29 13:23:00 +01:00
import numpy as np
from hydrogen import e_loc, psi
2021-01-03 18:45:58 +01:00
2021-01-29 13:23:00 +01:00
interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3
2021-01-03 18:45:58 +01:00
r = np.array([0.,0.,0.])
2021-01-29 13:23:00 +01:00
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
E = 0.
E2 = 0.
norm = 0.
2021-01-07 11:07:18 +01:00
2021-01-29 13:23:00 +01:00
for x in interval:
r[0] = x
for y in interval:
r[1] = y
for z in interval:
r[2] = z
w = psi(a,r)
w = w * w * delta
e_tmp = e_loc(a,r)
E += w * e_tmp
E2 += w * e_tmp * e_tmp
norm += w
E = E / norm
E2 = E2 / norm
s2 = E2 - E**2
print(f"a = {a} \t E = {E:10.8f} \t \sigma^2 = {s2:10.8f}")
#+end_src
2021-01-25 23:52:53 +01:00
2021-01-29 13:23:00 +01:00
#+RESULTS:
: a = 0.1 E = -0.24518439 \sigma^2 = 0.02696522
: a = 0.2 E = -0.26966058 \sigma^2 = 0.03719707
: a = 0.5 E = -0.38563576 \sigma^2 = 0.05318597
: a = 0.9 E = -0.49435710 \sigma^2 = 0.00577812
: a = 1.0 E = -0.50000000 \sigma^2 = 0.00000000
: a = 1.5 E = -0.39242967 \sigma^2 = 0.31449671
: a = 2.0 E = -0.08086981 \sigma^2 = 1.80688143
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-29 13:23:00 +01:00
#+begin_src f90
program variance_hydrogen
implicit none
double precision :: x(50), w, delta, energy, energy2
double precision :: dx, r(3), a(6), norm, e_tmp, s2
integer :: i, k, l, j
double precision, external :: e_loc, psi
2021-01-03 18:45:58 +01:00
a = (/ 0.1d0, 0.2d0, 0.5d0, 1.d0, 1.5d0, 2.d0 /)
2021-01-29 13:23:00 +01:00
dx = 10.d0/(size(x)-1)
do i=1,size(x)
x(i) = -5.d0 + (i-1)*dx
end do
2021-01-03 18:45:58 +01:00
delta = dx**3
r(:) = 0.d0
2021-01-29 13:23:00 +01:00
do j=1,size(a)
energy = 0.d0
energy2 = 0.d0
norm = 0.d0
do i=1,size(x)
r(1) = x(i)
2021-01-07 10:01:55 +01:00
2021-01-29 13:23:00 +01:00
do k=1,size(x)
r(2) = x(k)
2021-01-03 18:45:58 +01:00
2021-01-29 13:23:00 +01:00
do l=1,size(x)
r(3) = x(l)
w = psi(a(j),r)
w = w * w * delta
e_tmp = e_loc(a(j), r)
energy = energy + w * e_tmp
energy2 = energy2 + w * e_tmp * e_tmp
norm = norm + w
end do
end do
end do
energy = energy / norm
energy2 = energy2 / norm
s2 = energy2 - energy*energy
print *, 'a = ', a(j), ' E = ', energy, ' s2 = ', s2
end do
end program variance_hydrogen
#+end_src
#+begin_src sh :results output :exports results
2021-01-03 18:45:58 +01:00
gfortran hydrogen.f90 variance_hydrogen.f90 -o variance_hydrogen
2021-01-29 13:23:00 +01:00
./variance_hydrogen
#+end_src
2021-01-03 18:45:58 +01:00
2021-01-25 23:52:53 +01:00
#+RESULTS:
2021-01-29 13:23:00 +01:00
: a = 0.10000000000000001 E = -0.24518438948809140 s2 = 2.6965218719722767E-002
: a = 0.20000000000000001 E = -0.26966057967803236 s2 = 3.7197072370201284E-002
: a = 0.50000000000000000 E = -0.38563576125173815 s2 = 5.3185967578480653E-002
: a = 1.0000000000000000 E = -0.50000000000000000 s2 = 0.0000000000000000
: a = 1.5000000000000000 E = -0.39242967082602065 s2 = 0.31449670909172917
: a = 2.0000000000000000 E = -8.0869806678448772E-002 s2 = 1.8068814270846534
2021-01-03 18:45:58 +01:00
2021-01-26 00:22:37 +01:00
* Variational Monte Carlo
2021-01-03 18:45:58 +01:00
2021-01-11 18:41:36 +01:00
Numerical integration with deterministic methods is very efficient
2021-01-11 20:54:40 +01:00
in low dimensions. When the number of dimensions becomes large,
instead of computing the average energy as a numerical integration
2021-01-31 09:39:10 +01:00
on a grid, it is usually more efficient to use Monte Carlo sampling.
2021-01-03 18:45:58 +01:00
2021-02-02 13:30:11 +01:00
Moreover, Monte Carlo sampling will allow us to remove the bias due
2021-01-07 11:07:18 +01:00
to the discretization of space, and compute a statistical confidence
interval.
2021-01-07 10:01:55 +01:00
2021-01-26 00:22:37 +01:00
** Computation of the statistical error
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle qmc_stats.py
:header-args:f90: :tangle qmc_stats.f90
:END:
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
To compute the statistical error, you need to perform $M$
independent Monte Carlo calculations. You will obtain $M$ different
estimates of the energy, which are expected to have a Gaussian
2021-01-31 09:39:10 +01:00
distribution for large $M$, according to the [[https://en.wikipedia.org/wiki/Central_limit_theorem][Central Limit Theorem]].
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
The estimate of the energy is
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
$$
2021-02-01 13:51:59 +01:00
E = \frac{1}{M} \sum_{i=1}^M E_i
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
The variance of the average energies can be computed as
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
$$
2021-02-01 13:51:59 +01:00
\sigma^2 = \frac{1}{M-1} \sum_{i=1}^{M} (E_i - E)^2
2021-01-07 11:07:18 +01:00
$$
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
And the confidence interval is given by
2021-01-03 18:45:58 +01:00
2021-01-07 11:07:18 +01:00
$$
E \pm \delta E, \text{ where } \delta E = \frac{\sigma}{\sqrt{M}}
$$
2021-01-03 18:45:58 +01:00
2021-01-12 01:01:52 +01:00
*** Exercise
#+begin_exercise
2021-01-07 11:07:18 +01:00
Write a function returning the average and statistical error of an
input array.
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-26 00:22:37 +01:00
#+BEGIN_SRC python :results none :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-26 00:22:37 +01:00
from math import sqrt
def ave_error(arr):
#TODO
return (average, error)
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-29 13:23:00 +01:00
#+BEGIN_SRC f90 :tangle none
2021-01-27 13:04:32 +01:00
subroutine ave_error(x,n,ave,err)
implicit none
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision, intent(out) :: ave, err
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
end subroutine ave_error
2021-01-29 13:23:00 +01:00
#+END_SRC
2021-01-27 13:04:32 +01:00
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results none :exports code
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-07 10:01:55 +01:00
from math import sqrt
2021-01-03 18:45:58 +01:00
def ave_error(arr):
M = len(arr)
2021-01-26 00:22:37 +01:00
assert(M>0)
2021-01-29 13:23:00 +01:00
2021-01-26 00:22:37 +01:00
if M == 1:
2021-01-29 13:23:00 +01:00
average = arr[0]
error = 0.
2021-01-26 00:22:37 +01:00
else:
average = sum(arr)/M
variance = 1./(M-1) * sum( [ (x - average)**2 for x in arr ] )
error = sqrt(variance/M)
2021-01-29 13:23:00 +01:00
return (average, error)
2021-01-26 00:22:37 +01:00
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
#+BEGIN_SRC f90 :exports both
2021-01-07 10:01:55 +01:00
subroutine ave_error(x,n,ave,err)
implicit none
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision, intent(out) :: ave, err
2021-01-29 13:23:00 +01:00
double precision :: variance
2021-01-26 00:22:37 +01:00
if (n < 1) then
stop 'n<1 in ave_error'
2021-01-29 13:23:00 +01:00
2021-01-26 00:22:37 +01:00
else if (n == 1) then
2021-01-07 10:01:55 +01:00
ave = x(1)
err = 0.d0
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
else
2021-01-29 13:23:00 +01:00
ave = sum(x(:)) / dble(n)
variance = sum((x(:) - ave)**2) / dble(n-1)
err = dsqrt(variance/dble(n))
2021-01-07 10:01:55 +01:00
endif
end subroutine ave_error
2021-01-26 00:22:37 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-26 10:02:38 +01:00
** Uniform sampling in the box
2021-01-07 11:07:18 +01:00
:PROPERTIES:
:header-args:python: :tangle qmc_uniform.py
:header-args:f90: :tangle qmc_uniform.f90
:END:
2021-01-03 18:45:58 +01:00
2021-01-31 09:39:10 +01:00
We will now perform our first Monte Carlo calculation to compute the
energy of the hydrogen atom.
2021-01-12 01:01:52 +01:00
2021-01-31 09:39:10 +01:00
Consider again the expression of the energy
2021-01-12 01:01:52 +01:00
2021-01-31 09:39:10 +01:00
\begin{eqnarray*}
2021-02-01 13:20:26 +01:00
E & = & \frac{\int E_L(\mathbf{r})|\Psi(\mathbf{r})|^2\,d\mathbf{r}}{\int |\Psi(\mathbf{r}) |^2 d\mathbf{r}}\,.
2021-01-31 09:39:10 +01:00
\end{eqnarray*}
Clearly, the square of the wave function is a good choice of probability density to sample but we will start with something simpler and rewrite the energy as
\begin{eqnarray*}
2021-01-31 10:25:25 +01:00
E & = & \frac{\int E_L(\mathbf{r})\frac{|\Psi(\mathbf{r})|^2}{P(\mathbf{r})}P(\mathbf{r})\, \,d\mathbf{r}}{\int \frac{|\Psi(\mathbf{r})|^2 }{P(\mathbf{r})}P(\mathbf{r})d\mathbf{r}}\,.
2021-01-31 09:39:10 +01:00
\end{eqnarray*}
2021-01-31 10:25:25 +01:00
Here, we will sample a uniform probability $P(\mathbf{r})$ in a cube of volume $L^3$ centered at the origin:
2021-01-31 09:39:10 +01:00
2021-01-31 10:25:25 +01:00
$$ P(\mathbf{r}) = \frac{1}{L^3}\,, $$
2021-01-31 09:39:10 +01:00
and zero outside the cube.
One Monte Carlo run will consist of $N_{\rm MC}$ Monte Carlo iterations. At every Monte Carlo iteration:
2021-01-03 18:45:58 +01:00
2021-01-12 01:01:52 +01:00
- Draw a random point $\mathbf{r}_i$ in the box $(-5,-5,-5) \le
2021-01-07 11:07:18 +01:00
(x,y,z) \le (5,5,5)$
2021-02-02 13:30:11 +01:00
- Compute $|\Psi(\mathbf{r}_i)|^2$ and accumulate the result in a
2021-01-12 01:01:52 +01:00
variable =normalization=
2021-02-02 13:30:11 +01:00
- Compute $|\Psi(\mathbf{r}_i)|^2 \times E_L(\mathbf{r}_i)$, and accumulate the
2021-01-12 01:01:52 +01:00
result in a variable =energy=
2021-01-31 09:39:10 +01:00
Once all the iterations have been computed, the run returns the average energy
$\bar{E}_k$ over the $N_{\rm MC}$ iterations of the run.
2021-01-12 01:01:52 +01:00
2021-01-26 10:02:38 +01:00
To compute the statistical error, perform $M$ independent runs. The
final estimate of the energy will be the average over the
$\bar{E}_k$, and the variance of the $\bar{E}_k$ will be used to
compute the statistical error.
2021-01-12 01:01:52 +01:00
*** Exercise
2021-01-03 18:45:58 +01:00
2021-01-12 01:01:52 +01:00
#+begin_exercise
Parameterize the wave function with $a=1.2$. Perform 30
2021-01-12 01:01:52 +01:00
independent Monte Carlo runs, each with 100 000 Monte Carlo
steps. Store the final energies of each run and use this array to
compute the average energy and the associated error bar.
#+end_exercise
2021-01-07 10:01:55 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-26 10:02:38 +01:00
#+begin_note
To draw a uniform random number in Python, you can use
the [[https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html][~random.uniform~]] function of Numpy.
#+end_note
2021-01-27 13:04:32 +01:00
#+BEGIN_SRC python :tangle none :exports code
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-27 13:04:32 +01:00
from hydrogen import *
from qmc_stats import *
def MonteCarlo(a, nmax):
# TODO
2021-01-11 18:41:36 +01:00
a = 1.2
2021-01-11 18:41:36 +01:00
nmax = 100000
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
#TODO
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
print(f"E = {E} +/- {deltaE}")
2021-01-26 10:02:38 +01:00
#+END_SRC
2021-01-07 11:07:18 +01:00
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-26 10:02:38 +01:00
#+begin_note
To draw a uniform random number in Fortran, you can use
the [[https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html][~RANDOM_NUMBER~]] subroutine.
#+end_note
#+begin_note
When running Monte Carlo calculations, the number of steps is
usually very large. We expect =nmax= to be possibly larger than 2
billion, so we use 8-byte integers (=integer*8=) to represent it, as
well as the index of the current step.
#+end_note
2021-01-12 10:55:00 +01:00
2021-01-26 10:02:38 +01:00
#+BEGIN_SRC f90 :tangle none
subroutine uniform_montecarlo(a,nmax,energy)
implicit none
double precision, intent(in) :: a
integer*8 , intent(in) :: nmax
double precision, intent(out) :: energy
2021-01-29 13:23:00 +01:00
integer*8 :: istep
2021-01-26 10:02:38 +01:00
double precision :: norm, r(3), w
double precision, external :: e_loc, psi
! TODO
end subroutine uniform_montecarlo
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-01-26 10:02:38 +01:00
integer*8 , parameter :: nmax = 100000
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-26 10:02:38 +01:00
double precision :: X(nruns)
double precision :: ave, err
!TODO
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
print *, 'E = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
end program qmc
#+END_SRC
2021-01-27 13:04:32 +01:00
#+begin_src sh :results output :exports code
2021-01-26 10:02:38 +01:00
gfortran hydrogen.f90 qmc_stats.f90 qmc_uniform.f90 -o qmc_uniform
./qmc_uniform
#+end_src
2021-01-27 13:04:32 +01:00
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results output :exports both
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-27 13:04:32 +01:00
from hydrogen import *
from qmc_stats import *
def MonteCarlo(a, nmax):
energy = 0.
normalization = 0.
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
for istep in range(nmax):
r = np.random.uniform(-5., 5., (3))
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
w = psi(a,r)
w = w*w
2021-01-29 13:23:00 +01:00
energy += w * e_loc(a,r)
2021-01-27 13:04:32 +01:00
normalization += w
2021-01-29 13:23:00 +01:00
return energy / normalization
a = 1.2
2021-01-27 13:04:32 +01:00
nmax = 100000
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
X = [MonteCarlo(a,nmax) for i in range(30)]
E, deltaE = ave_error(X)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
print(f"E = {E} +/- {deltaE}")
#+END_SRC
#+RESULTS:
: E = -0.48363807880008725 +/- 0.002330876047368999
2021-01-27 13:04:32 +01:00
*Fortran*
#+BEGIN_SRC f90 :exports code
2021-01-07 10:01:55 +01:00
subroutine uniform_montecarlo(a,nmax,energy)
implicit none
double precision, intent(in) :: a
2021-01-12 10:55:00 +01:00
integer*8 , intent(in) :: nmax
2021-01-07 10:01:55 +01:00
double precision, intent(out) :: energy
2021-01-07 11:07:18 +01:00
2021-01-29 13:23:00 +01:00
integer*8 :: istep
2021-01-07 10:01:55 +01:00
double precision :: norm, r(3), w
double precision, external :: e_loc, psi
energy = 0.d0
norm = 0.d0
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
do istep = 1,nmax
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
call random_number(r)
r(:) = -5.d0 + 10.d0*r(:)
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
w = psi(a,r)
w = w*w
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
energy = energy + w * e_loc(a,r)
2021-01-29 13:23:00 +01:00
norm = norm + w
2021-01-07 10:01:55 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
energy = energy / norm
2021-01-29 13:23:00 +01:00
2021-01-07 10:01:55 +01:00
end subroutine uniform_montecarlo
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-01-29 13:23:00 +01:00
integer*8 , parameter :: nmax = 100000
2021-01-07 10:01:55 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-07 10:01:55 +01:00
double precision :: X(nruns)
double precision :: ave, err
do irun=1,nruns
2021-01-29 13:23:00 +01:00
call uniform_montecarlo(a, nmax, X(irun))
2021-01-07 10:01:55 +01:00
enddo
2021-01-29 13:23:00 +01:00
call ave_error(X, nruns, ave, err)
2021-01-07 10:01:55 +01:00
print *, 'E = ', ave, '+/-', err
end program qmc
2021-01-27 13:04:32 +01:00
#+END_SRC
2021-01-07 10:01:55 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src sh :results output :exports results
2021-01-07 10:01:55 +01:00
gfortran hydrogen.f90 qmc_stats.f90 qmc_uniform.f90 -o qmc_uniform
./qmc_uniform
2021-01-27 13:04:32 +01:00
#+end_src
2021-01-07 10:01:55 +01:00
2021-01-27 13:04:32 +01:00
#+RESULTS:
: E = -0.48084122147238995 +/- 2.4983775878329355E-003
2021-01-07 10:01:55 +01:00
2021-01-26 10:02:38 +01:00
** Metropolis sampling with $\Psi^2$
2021-01-07 11:07:18 +01:00
:PROPERTIES:
2021-01-20 18:31:49 +01:00
:header-args:python: :tangle qmc_metropolis.py
:header-args:f90: :tangle qmc_metropolis.f90
2021-01-07 11:07:18 +01:00
:END:
2021-01-03 18:45:58 +01:00
2021-01-20 18:31:49 +01:00
We will now use the square of the wave function to sample random
points distributed with the probability density
2021-01-07 11:07:18 +01:00
\[
2021-02-01 13:51:59 +01:00
P(\mathbf{r}) = \frac{|\Psi(\mathbf{r})|^2}{\int |\Psi(\mathbf{r})|^2 d\mathbf{r}}\,.
2021-01-07 11:07:18 +01:00
\]
2021-01-12 01:01:52 +01:00
2021-01-26 10:02:38 +01:00
The expression of the average energy is now simplified as the average of
2021-01-20 18:31:49 +01:00
the local energies, since the weights are taken care of by the
2021-01-31 09:39:10 +01:00
sampling:
2021-01-12 01:01:52 +01:00
2021-01-20 18:31:49 +01:00
$$
2021-02-01 13:20:26 +01:00
E \approx \frac{1}{N_{\rm MC}}\sum_{i=1}^{N_{\rm MC}} E_L(\mathbf{r}_i)\,.
2021-01-20 18:31:49 +01:00
$$
2021-01-07 11:07:18 +01:00
2021-01-20 18:31:49 +01:00
To sample a chosen probability density, an efficient method is the
[[https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm][Metropolis-Hastings sampling algorithm]]. Starting from a random
2021-01-31 10:25:25 +01:00
initial position $\mathbf{r}_0$, we will realize a random walk:
2021-02-01 13:20:26 +01:00
$$ \mathbf{r}_0 \rightarrow \mathbf{r}_1 \rightarrow \mathbf{r}_2 \ldots \rightarrow \mathbf{r}_{N_{\rm MC}}\,, $$
2021-01-31 10:25:25 +01:00
2021-02-01 13:20:26 +01:00
according to the following algorithm.
2021-01-31 10:25:25 +01:00
2021-01-31 17:01:12 +01:00
At every step, we propose a new move according to a transition probability $T(\mathbf{r}_{n}\rightarrow\mathbf{r}_{n+1})$ of our choice.
2021-01-31 10:25:25 +01:00
2021-01-31 17:01:12 +01:00
For simplicity, we will move the electron in a 3-dimensional box of side $2\delta L$ centered at the current position
2021-01-31 10:25:25 +01:00
of the electron:
2021-01-07 11:07:18 +01:00
$$
2021-01-31 10:25:25 +01:00
\mathbf{r}_{n+1} = \mathbf{r}_{n} + \delta L \, \mathbf{u}
2021-01-07 11:07:18 +01:00
$$
2021-01-31 10:25:25 +01:00
where $\delta L$ is a fixed constant, and
2021-01-20 18:31:49 +01:00
$\mathbf{u}$ is a uniform random number in a 3-dimensional box
2021-01-31 10:25:25 +01:00
$(-1,-1,-1) \le \mathbf{u} \le (1,1,1)$.
2021-01-31 17:01:12 +01:00
After having moved the electron, we add the
2021-01-26 10:02:38 +01:00
accept/reject step that guarantees that the distribution of the
2021-01-31 10:25:25 +01:00
$\mathbf{r}_n$ is $\Psi^2$. This amounts to accepting the move with
probability
$$
2021-02-01 13:51:59 +01:00
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)\,,
2021-01-31 10:25:25 +01:00
$$
which, for our choice of transition probability, becomes
$$
A(\mathbf{r}_{n}\rightarrow\mathbf{r}_{n+1}) = \min\left(1,\frac{P(\mathbf{r}_{n+1})}{P(\mathbf{r}_{n})}\right)= \min\left(1,\frac{|\Psi(\mathbf{r}_{n+1})|^2}{|\Psi(\mathbf{r}_{n})|^2}\right)\,.
2021-01-31 10:25:25 +01:00
$$
#+begin_exercise
Explain why the transition probability cancels out in the
expression of $A$.
#+end_exercise
Also note that we do not need to compute the norm of the wave function!
2021-01-31 10:25:25 +01:00
The algorithm is summarized as follows:
1) Evaluate the local energy at $\mathbf{r}_n$ and accumulate it
2) Compute a new position $\mathbf{r'} = \mathbf{r}_n + \delta L\, \mathbf{u}$
3) Evaluate $\Psi(\mathbf{r}')$ at the new position
4) Compute the ratio $A = \frac{\left|\Psi(\mathbf{r'})\right|^2}{\left|\Psi(\mathbf{r}_{n})\right|^2}$
5) Draw a uniform random number $v \in [0,1]$
6) if $v \le A$, accept the move : set $\mathbf{r}_{n+1} = \mathbf{r'}$
7) else, reject the move : set $\mathbf{r}_{n+1} = \mathbf{r}_n$
2021-01-12 01:01:52 +01:00
2021-01-20 18:31:49 +01:00
#+begin_note
A common error is to remove the rejected samples from the
calculation of the average. *Don't do it!*
2021-01-12 01:01:52 +01:00
All samples should be kept, from both accepted /and/ rejected moves.
2021-01-20 18:31:49 +01:00
#+end_note
*** Optimal step size
If the box is infinitely small, the ratio will be very close
to one and all the steps will be accepted. However, the moves will be
very correlated and you will visit the configurational space very slowly.
On the other hand, if you propose too large moves, 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 size of the move 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 for the current problem.
#+begin_note
Below, we use the symbol $\delta t$ to denote $\delta L$ since we will use
the same variable later on to store a time step.
#+end_note
2021-01-20 18:31:49 +01:00
*** Exercise
2021-01-12 01:01:52 +01:00
#+begin_exercise
2021-01-26 10:02:38 +01:00
Modify the program of the previous section to compute the energy,
sampled with $\Psi^2$.
2021-01-20 18:31:49 +01:00
Compute also the acceptance rate, so that you can adapt the time
2021-01-31 10:25:25 +01:00
step in order to have an acceptance rate close to 0.5.
2021-01-26 10:02:38 +01:00
2021-01-20 18:31:49 +01:00
Can you observe a reduction in the statistical error?
2021-01-12 01:01:52 +01:00
#+end_exercise
2021-01-27 13:04:32 +01:00
*Python*
2021-01-26 10:02:38 +01:00
#+BEGIN_SRC python :results output :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-07 11:07:18 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-07 10:01:55 +01:00
2021-01-28 15:04:57 +01:00
def MonteCarlo(a,nmax,dt):
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
# TODO
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
return energy/nmax, N_accep/nmax
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
# Run simulation
a = 1.2
2021-01-26 10:02:38 +01:00
nmax = 100000
2021-01-29 13:23:00 +01:00
dt = #TODO
2021-01-28 15:04:57 +01:00
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
2021-01-26 10:02:38 +01:00
# 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:
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-26 10:02:38 +01:00
#+BEGIN_SRC f90 :tangle none
2021-01-28 15:04:57 +01:00
subroutine metropolis_montecarlo(a,nmax,dt,energy,accep)
2021-01-26 10:02:38 +01:00
implicit none
double precision, intent(in) :: a
integer*8 , intent(in) :: nmax
2021-01-28 15:04:57 +01:00
double precision, intent(in) :: dt
2021-01-26 10:02:38 +01:00
double precision, intent(out) :: energy
double precision, intent(out) :: accep
2021-01-29 13:23:00 +01:00
integer*8 :: istep
integer*8 :: n_accep
2021-01-26 10:02:38 +01:00
double precision :: r_old(3), r_new(3), psi_old, psi_new
double precision :: v, ratio
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
double precision, external :: e_loc, psi, gaussian
! TODO
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
end subroutine metropolis_montecarlo
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-01-29 13:23:00 +01:00
double precision, parameter :: dt = ! TODO
integer*8 , parameter :: nmax = 100000
2021-01-26 10:02:38 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-26 10:02:38 +01:00
double precision :: X(nruns), Y(nruns)
double precision :: ave, err
2021-01-07 11:07:18 +01:00
2021-01-26 10:02:38 +01:00
do irun=1,nruns
2021-01-28 15:04:57 +01:00
call metropolis_montecarlo(a,nmax,dt,X(irun),Y(irun))
2021-01-26 10:02:38 +01:00
enddo
2021-01-03 18:45:58 +01:00
2021-01-26 10:02:38 +01:00
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
call ave_error(Y,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
end program qmc
#+END_SRC
#+begin_src sh :results output :exports both
gfortran hydrogen.f90 qmc_stats.f90 qmc_metropolis.f90 -o qmc_metropolis
./qmc_metropolis
#+end_src
2021-01-27 13:04:32 +01:00
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results output :exports both
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-27 13:04:32 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-28 15:04:57 +01:00
def MonteCarlo(a,nmax,dt):
2021-01-29 13:23:00 +01:00
energy = 0.
2021-01-27 13:04:32 +01:00
N_accep = 0
2021-01-29 13:23:00 +01:00
2021-01-28 15:04:57 +01:00
r_old = np.random.uniform(-dt, dt, (3))
2021-01-27 13:04:32 +01:00
psi_old = psi(a,r_old)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
for istep in range(nmax):
2021-01-29 13:23:00 +01:00
energy += e_loc(a,r_old)
2021-01-28 15:04:57 +01:00
r_new = r_old + np.random.uniform(-dt,dt,(3))
2021-01-27 13:04:32 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
ratio = (psi_new / psi_old)**2
2021-01-29 13:23:00 +01:00
if np.random.uniform() <= ratio:
2021-01-27 13:04:32 +01:00
N_accep += 1
2021-01-29 13:23:00 +01:00
r_old = r_new
2021-01-27 13:04:32 +01:00
psi_old = psi_new
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
return energy/nmax, N_accep/nmax
# Run simulation
a = 1.2
2021-01-27 13:04:32 +01:00
nmax = 100000
dt = 1.0
2021-01-29 13:23:00 +01:00
2021-01-28 15:04:57 +01:00
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
2021-01-27 13:04:32 +01:00
# 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.4802595860693983 +/- 0.0005124420418289207
: A = 0.5074913333333334 +/- 0.000350889422714878
2021-01-27 13:04:32 +01:00
*Fortran*
#+BEGIN_SRC f90 :exports code
2021-01-28 15:04:57 +01:00
subroutine metropolis_montecarlo(a,nmax,dt,energy,accep)
2021-01-07 11:07:18 +01:00
implicit none
double precision, intent(in) :: a
2021-01-12 10:55:00 +01:00
integer*8 , intent(in) :: nmax
2021-01-28 15:04:57 +01:00
double precision, intent(in) :: dt
2021-01-07 11:07:18 +01:00
double precision, intent(out) :: energy
2021-01-20 18:31:49 +01:00
double precision, intent(out) :: accep
2021-01-07 11:07:18 +01:00
2021-01-26 10:02:38 +01:00
double precision :: r_old(3), r_new(3), psi_old, psi_new
double precision :: v, ratio
integer*8 :: n_accep
2021-01-29 13:23:00 +01:00
integer*8 :: istep
2021-01-07 11:07:18 +01:00
double precision, external :: e_loc, psi, gaussian
2021-01-29 13:23:00 +01:00
energy = 0.d0
2021-01-26 10:02:38 +01:00
n_accep = 0_8
2021-01-29 13:23:00 +01:00
2021-01-20 18:31:49 +01:00
call random_number(r_old)
2021-01-28 15:04:57 +01:00
r_old(:) = dt * (2.d0*r_old(:) - 1.d0)
2021-01-20 18:31:49 +01:00
psi_old = psi(a,r_old)
2021-01-29 13:23:00 +01:00
2021-01-07 11:07:18 +01:00
do istep = 1,nmax
2021-01-29 13:23:00 +01:00
energy = energy + e_loc(a,r_old)
2021-01-20 18:31:49 +01:00
call random_number(r_new)
2021-01-29 13:23:00 +01:00
r_new(:) = r_old(:) + dt*(2.d0*r_new(:) - 1.d0)
2021-01-20 18:31:49 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-20 18:31:49 +01:00
ratio = (psi_new / psi_old)**2
call random_number(v)
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
if (v <= ratio) then
2021-01-29 13:23:00 +01:00
n_accep = n_accep + 1_8
2021-01-20 18:31:49 +01:00
r_old(:) = r_new(:)
psi_old = psi_new
2021-01-29 13:23:00 +01:00
2021-01-20 18:31:49 +01:00
endif
2021-01-29 13:23:00 +01:00
2021-01-07 11:07:18 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-26 10:02:38 +01:00
energy = energy / dble(nmax)
accep = dble(n_accep) / dble(nmax)
2021-01-29 13:23:00 +01:00
2021-01-20 18:31:49 +01:00
end subroutine metropolis_montecarlo
2021-01-07 11:07:18 +01:00
program qmc
implicit none
double precision, parameter :: a = 1.2d0
double precision, parameter :: dt = 1.0d0
2021-01-12 10:55:00 +01:00
integer*8 , parameter :: nmax = 100000
2021-01-07 11:07:18 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-20 18:31:49 +01:00
double precision :: X(nruns), Y(nruns)
2021-01-07 11:07:18 +01:00
double precision :: ave, err
do irun=1,nruns
2021-01-28 15:04:57 +01:00
call metropolis_montecarlo(a,nmax,dt,X(irun),Y(irun))
2021-01-07 11:07:18 +01:00
enddo
2021-01-26 10:02:38 +01:00
2021-01-07 11:07:18 +01:00
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
2021-01-26 10:02:38 +01:00
2021-01-20 18:31:49 +01:00
call ave_error(Y,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-07 11:07:18 +01:00
end program qmc
2021-01-26 10:02:38 +01:00
#+END_SRC
2021-01-07 11:07:18 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src sh :results output :exports results
2021-01-20 18:31:49 +01:00
gfortran hydrogen.f90 qmc_stats.f90 qmc_metropolis.f90 -o qmc_metropolis
./qmc_metropolis
2021-01-26 10:02:38 +01:00
#+end_src
#+RESULTS:
: E = -0.47948142754168033 +/- 4.8410177863919307E-004
: A = 0.50762633333333318 +/- 3.4601756760043725E-004
2021-01-20 18:31:49 +01:00
2021-01-26 13:11:57 +01:00
** Generalized Metropolis algorithm
2021-01-12 10:55:00 +01:00
:PROPERTIES:
2021-01-20 19:12:05 +01:00
:header-args:python: :tangle vmc_metropolis.py
:header-args:f90: :tangle vmc_metropolis.f90
2021-01-12 10:55:00 +01:00
:END:
2021-01-12 01:01:52 +01:00
2021-01-31 10:25:25 +01:00
One can use more efficient numerical schemes to move the electrons by choosing a smarter expression for the transition probability.
The Metropolis acceptance step has to be adapted accordingly to ensure that the detailed balance condition is satisfied. This means that
the acceptance probability $A$ is chosen so that it is consistent with the
2021-01-20 19:12:05 +01:00
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{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}$.
2021-01-12 01:01:52 +01:00
2021-01-31 17:01:12 +01:00
In the previous example, we were using uniform sampling in a box centered
at the current position. Hence, the transition probability was symmetric
2021-01-12 01:01:52 +01:00
2021-01-20 19:12:05 +01:00
\[
2021-01-31 17:01:12 +01:00
T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) = T(\mathbf{r}_{n+1} \rightarrow \mathbf{r}_{n})
= \text{constant}\,,
2021-01-20 19:12:05 +01:00
\]
2021-01-12 01:01:52 +01:00
2021-01-26 13:11:57 +01:00
so the expression of $A$ was simplified to the ratios of the squared
2021-01-20 19:12:05 +01:00
wave functions.
2021-01-31 17:01:12 +01:00
Now, if instead of drawing uniform random numbers, we
2021-01-26 13:11:57 +01:00
choose to draw Gaussian random numbers with zero mean and variance
2021-01-28 15:04:57 +01:00
$\delta t$, the transition probability becomes:
2021-01-20 19:12:05 +01:00
\[
2021-01-26 13:11:57 +01:00
T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) =
2021-01-28 15:04:57 +01:00
\frac{1}{(2\pi\,\delta t)^{3/2}} \exp \left[ - \frac{\left(
2021-01-31 10:25:25 +01:00
\mathbf{r}_{n+1} - \mathbf{r}_{n} \right)^2}{2\delta t} \right]\,.
2021-01-20 19:12:05 +01:00
\]
2021-01-11 18:41:36 +01:00
2021-01-31 17:01:12 +01:00
Furthermore, to sample the density even better, we can "push" the electrons
2021-01-20 19:12:05 +01:00
into in the regions of high probability, and "pull" them away from
the low-probability regions. This will increase the
2021-01-20 19:12:05 +01:00
acceptance ratios and improve the sampling.
2021-01-11 18:41:36 +01:00
2021-01-31 10:25:25 +01:00
To do this, we can use the gradient of the probability density
2021-01-11 18:41:36 +01:00
2021-01-20 19:12:05 +01:00
\[
2021-01-31 10:25:25 +01:00
\frac{\nabla [ \Psi^2 ]}{\Psi^2} = 2 \frac{\nabla \Psi}{\Psi}\,,
2021-01-26 13:11:57 +01:00
\]
2021-01-20 19:12:05 +01:00
2021-02-01 13:20:26 +01:00
and add the so-called drift vector, $\frac{\nabla \Psi}{\Psi}$, so that the numerical scheme becomes a
2021-01-31 17:01:12 +01:00
drifted diffusion with transition probability:
\[
T(\mathbf{r}_{n} \rightarrow \mathbf{r}_{n+1}) =
\frac{1}{(2\pi\,\delta t)^{3/2}} \exp \left[ - \frac{\left(
2021-02-01 13:20:26 +01:00
\mathbf{r}_{n+1} - \mathbf{r}_{n} - \delta t\frac{\nabla
2021-01-31 17:01:12 +01:00
\Psi(\mathbf{r}_n)}{\Psi(\mathbf{r}_n)} \right)^2}{2\,\delta t} \right]\,.
\]
2021-01-11 18:41:36 +01:00
The corresponding move is proposed as
2021-01-31 17:01:12 +01:00
2021-01-20 19:12:05 +01:00
\[
2021-01-28 15:04:57 +01:00
\mathbf{r}_{n+1} = \mathbf{r}_{n} + \delta t\, \frac{\nabla
2021-01-31 10:25:25 +01:00
\Psi(\mathbf{r})}{\Psi(\mathbf{r})} + \chi \,,
2021-01-20 19:12:05 +01:00
\]
2021-01-11 18:41:36 +01:00
2021-01-20 19:12:05 +01:00
where $\chi$ is a Gaussian random variable with zero mean and
2021-01-28 15:04:57 +01:00
variance $\delta t$.
2021-01-31 10:25:25 +01:00
2021-01-31 17:01:12 +01:00
The algorithm of the previous exercise is only slighlty modified as:
2021-01-31 10:25:25 +01:00
1) Evaluate the local energy at $\mathbf{r}_{n}$ and accumulate it
2) Compute a new position $\mathbf{r'} = \mathbf{r}_n +
2021-01-31 10:25:25 +01:00
\delta t\, \frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})} + \chi$
3) Evaluate $\Psi(\mathbf{r}')$ and $\frac{\nabla \Psi(\mathbf{r'})}{\Psi(\mathbf{r'})}$ at the new position
4) Compute the ratio $A = \frac{T(\mathbf{r}' \rightarrow \mathbf{r}_{n}) P(\mathbf{r}')}{T(\mathbf{r}_{n} \rightarrow \mathbf{r}') P(\mathbf{r}_{n})}$
5) Draw a uniform random number $v \in [0,1]$
6) if $v \le A$, accept the move : set $\mathbf{r}_{n+1} = \mathbf{r'}$
7) else, reject the move : set $\mathbf{r}_{n+1} = \mathbf{r}_n$
2021-01-31 10:25:25 +01:00
*** 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:
\begin{eqnarray*}
z_1 &=& \sqrt{-2 \ln u_1} \cos(2 \pi u_2) \\
z_2 &=& \sqrt{-2 \ln u_1} \sin(2 \pi u_2)
\end{eqnarray*}
Below is a Fortran implementation returning a Gaussian-distributed
n-dimensional vector $\mathbf{z}$. This will be useful for the
following sections.
*Fortran*
#+BEGIN_SRC f90 :tangle qmc_stats.f90
subroutine random_gauss(z,n)
implicit none
integer, intent(in) :: n
double precision, intent(out) :: z(n)
double precision :: u(n+1)
double precision, parameter :: two_pi = 2.d0*dacos(-1.d0)
integer :: i
call random_number(u)
if (iand(n,1) == 0) then
! n is even
do i=1,n,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) * dsin( two_pi*u(i+1) )
z(i) = z(i) * dcos( two_pi*u(i+1) )
end do
else
! n is odd
do i=1,n-1,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) * dsin( two_pi*u(i+1) )
z(i) = z(i) * dcos( two_pi*u(i+1) )
end do
z(n) = dsqrt(-2.d0*dlog(u(n)))
z(n) = z(n) * dcos( two_pi*u(n+1) )
end if
end subroutine random_gauss
#+END_SRC
In Python, you can use the [[https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html][~random.normal~]] function of Numpy.
2021-01-03 18:45:58 +01:00
2021-01-20 19:12:05 +01:00
*** Exercise 1
2021-01-26 13:11:57 +01:00
#+begin_exercise
If you use Fortran, copy/paste the ~random_gauss~ function in
a Fortran file.
#+end_exercise
2021-01-20 19:12:05 +01:00
#+begin_exercise
Write a function to compute the drift vector $\frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})}$.
#+end_exercise
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-26 13:11:57 +01:00
#+BEGIN_SRC python :tangle hydrogen.py :tangle none
def drift(a,r):
# TODO
#+END_SRC
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-26 13:11:57 +01:00
#+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)
2021-01-29 13:23:00 +01:00
2021-01-26 13:11:57 +01:00
! TODO
2021-01-29 13:23:00 +01:00
2021-01-26 13:11:57 +01:00
end subroutine drift
2021-01-20 19:12:05 +01:00
#+END_SRC
2021-01-03 18:45:58 +01:00
2021-01-27 13:04:32 +01:00
**** Solution :solution:
*Python*
#+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*
2021-01-20 19:12:05 +01:00
#+BEGIN_SRC f90 :tangle hydrogen.f90
2021-01-11 18:41:36 +01:00
subroutine drift(a,r,b)
implicit none
double precision, intent(in) :: a, r(3)
double precision, intent(out) :: b(3)
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
double precision :: ar_inv
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
ar_inv = -a / dsqrt(r(1)*r(1) + r(2)*r(2) + r(3)*r(3))
2021-01-29 13:23:00 +01:00
b(:) = r(:) * ar_inv
2021-01-11 18:41:36 +01:00
end subroutine drift
2021-01-20 19:12:05 +01:00
#+END_SRC
2021-01-12 01:01:52 +01:00
2021-01-20 19:12:05 +01:00
*** Exercise 2
2021-01-12 01:01:52 +01:00
2021-01-20 19:12:05 +01:00
#+begin_exercise
2021-01-31 17:01:12 +01:00
Modify the previous program to introduce the drift-diffusion scheme.
(This is a necessary step for the next section on diffusion Monte Carlo).
2021-01-20 19:12:05 +01:00
#+end_exercise
2021-01-12 01:01:52 +01:00
2021-01-27 13:04:32 +01:00
*Python*
2021-01-26 13:11:57 +01:00
#+BEGIN_SRC python :results output :tangle none
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-12 11:23:13 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-28 15:04:57 +01:00
def MonteCarlo(a,nmax,dt):
2021-01-26 13:11:57 +01:00
# TODO
# Run simulation
a = 1.2
2021-01-26 13:11:57 +01:00
nmax = 100000
2021-01-29 13:23:00 +01:00
dt = # TODO
2021-01-28 15:04:57 +01:00
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
2021-01-26 13:11:57 +01:00
# 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
2021-01-27 13:04:32 +01:00
*Fortran*
#+BEGIN_SRC f90 :tangle none
2021-01-29 13:23:00 +01:00
subroutine variational_montecarlo(a,dt,nmax,energy,accep)
2021-01-27 13:04:32 +01:00
implicit none
2021-01-28 15:04:57 +01:00
double precision, intent(in) :: a, dt
2021-01-27 13:04:32 +01:00
integer*8 , intent(in) :: nmax
2021-01-29 13:23:00 +01:00
double precision, intent(out) :: energy, accep
2021-01-27 13:04:32 +01:00
2021-01-29 13:23:00 +01:00
integer*8 :: istep
integer*8 :: n_accep
2021-01-28 15:04:57 +01:00
double precision :: sq_dt, chi(3)
2021-01-27 13:04:32 +01:00
double precision :: psi_old, psi_new
double precision :: r_old(3), r_new(3)
double precision :: d_old(3), d_new(3)
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
double precision, external :: e_loc, psi
! TODO
end subroutine variational_montecarlo
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-01-29 13:23:00 +01:00
double precision, parameter :: dt = ! TODO
integer*8 , parameter :: nmax = 100000
2021-01-27 13:04:32 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-27 13:04:32 +01:00
double precision :: X(nruns), accep(nruns)
double precision :: ave, err
do irun=1,nruns
2021-01-28 15:04:57 +01:00
call variational_montecarlo(a,dt,nmax,X(irun),accep(irun))
2021-01-27 13:04:32 +01:00
enddo
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
call ave_error(accep,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-27 13:04:32 +01:00
end program qmc
#+END_SRC
#+begin_src sh :results output :exports code
gfortran hydrogen.f90 qmc_stats.f90 vmc_metropolis.f90 -o vmc_metropolis
./vmc_metropolis
#+end_src
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results output :exports both
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-26 13:11:57 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-28 15:04:57 +01:00
def MonteCarlo(a,nmax,dt):
sq_dt = np.sqrt(dt)
2021-01-29 13:23:00 +01:00
energy = 0.
N_accep = 0
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)
2021-01-11 20:54:40 +01:00
psi_old = psi(a,r_old)
2021-01-29 13:23:00 +01:00
2021-01-11 20:54:40 +01:00
for istep in range(nmax):
2021-01-12 11:23:13 +01:00
chi = np.random.normal(loc=0., scale=1.0, size=(3))
2021-01-29 13:23:00 +01:00
energy += e_loc(a,r_old)
r_new = r_old + dt * d_old + sq_dt * chi
d_new = drift(a,r_new)
d2_new = np.dot(d_new,d_new)
2021-01-11 20:54:40 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-11 20:54:40 +01:00
# Metropolis
2021-01-29 13:23:00 +01:00
prod = np.dot((d_new + d_old), (r_new - r_old))
2021-01-28 15:04:57 +01:00
argexpo = 0.5 * (d2_new - d2_old)*dt + prod
2021-01-29 13:23:00 +01:00
2021-01-11 20:54:40 +01:00
q = psi_new / psi_old
q = np.exp(-argexpo) * q*q
2021-01-29 13:23:00 +01:00
if np.random.uniform() <= q:
N_accep += 1
r_old = r_new
d_old = d_new
d2_old = d2_new
2021-01-11 20:54:40 +01:00
psi_old = psi_new
2021-01-29 13:23:00 +01:00
return energy/nmax, N_accep/nmax
2021-01-11 20:54:40 +01:00
2021-01-26 13:11:57 +01:00
# Run simulation
a = 1.2
2021-01-11 20:54:40 +01:00
nmax = 100000
dt = 1.0
2021-01-29 13:23:00 +01:00
2021-01-28 15:04:57 +01:00
X0 = [ MonteCarlo(a,nmax,dt) for i in range(30)]
2021-01-26 13:11:57 +01:00
# 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
2021-01-11 20:54:40 +01:00
2021-01-26 13:11:57 +01:00
#+RESULTS:
2021-02-02 17:00:49 +01:00
: E = -0.48034171558629885 +/- 0.0005286038561061781
: A = 0.6210380000000001 +/- 0.0005457375900937905
2021-01-11 20:54:40 +01:00
2021-01-27 13:04:32 +01:00
*Fortran*
2021-01-26 13:11:57 +01:00
#+BEGIN_SRC f90
2021-01-29 13:23:00 +01:00
subroutine variational_montecarlo(a,dt,nmax,energy,accep)
2021-01-26 13:11:57 +01:00
implicit none
2021-01-28 15:04:57 +01:00
double precision, intent(in) :: a, dt
2021-01-26 13:11:57 +01:00
integer*8 , intent(in) :: nmax
2021-01-29 13:23:00 +01:00
double precision, intent(out) :: energy, accep
2021-01-26 13:11:57 +01:00
2021-01-29 13:23:00 +01:00
integer*8 :: istep
integer*8 :: n_accep
2021-01-28 15:04:57 +01:00
double precision :: sq_dt, chi(3), d2_old, prod, u
2021-01-12 11:23:13 +01:00
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)
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
double precision, external :: e_loc, psi
2021-01-11 18:41:36 +01:00
2021-01-28 15:04:57 +01:00
sq_dt = dsqrt(dt)
2021-01-20 19:12:05 +01:00
2021-01-12 11:23:13 +01:00
! Initialization
2021-01-29 13:23:00 +01:00
energy = 0.d0
n_accep = 0_8
2021-01-12 11:23:13 +01:00
call random_gauss(r_old,3)
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
call drift(a,r_old,d_old)
2021-01-29 13:23:00 +01:00
d2_old = d_old(1)*d_old(1) + &
d_old(2)*d_old(2) + &
d_old(3)*d_old(3)
2021-01-12 11:23:13 +01:00
psi_old = psi(a,r_old)
2021-01-11 18:41:36 +01:00
do istep = 1,nmax
2021-01-29 13:23:00 +01:00
energy = energy + e_loc(a,r_old)
2021-01-12 11:23:13 +01:00
call random_gauss(chi,3)
2021-01-29 13:23:00 +01:00
r_new(:) = r_old(:) + dt*d_old(:) + chi(:)*sq_dt
2021-01-12 11:23:13 +01:00
call drift(a,r_new,d_new)
2021-01-29 13:23:00 +01:00
d2_new = d_new(1)*d_new(1) + &
d_new(2)*d_new(2) + &
d_new(3)*d_new(3)
2021-01-12 11:23:13 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
! Metropolis
prod = (d_new(1) + d_old(1))*(r_new(1) - r_old(1)) + &
2021-01-26 13:11:57 +01:00
(d_new(2) + d_old(2))*(r_new(2) - r_old(2)) + &
(d_new(3) + d_old(3))*(r_new(3) - r_old(3))
2021-01-29 13:23:00 +01:00
2021-01-28 15:04:57 +01:00
argexpo = 0.5d0 * (d2_new - d2_old)*dt + prod
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
q = psi_new / psi_old
q = dexp(-argexpo) * q*q
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
call random_number(u)
2021-01-29 13:23:00 +01:00
if (u <= q) then
n_accep = n_accep + 1_8
2021-01-12 11:23:13 +01:00
r_old(:) = r_new(:)
d_old(:) = d_new(:)
2021-01-29 13:23:00 +01:00
d2_old = d2_new
psi_old = psi_new
2021-01-12 11:23:13 +01:00
end if
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-26 13:11:57 +01:00
energy = energy / dble(nmax)
2021-01-29 13:23:00 +01:00
accep = dble(n_accep) / dble(nmax)
2021-01-11 18:41:36 +01:00
end subroutine variational_montecarlo
program qmc
implicit none
double precision, parameter :: a = 1.2d0
double precision, parameter :: dt = 1.0d0
2021-01-29 13:23:00 +01:00
integer*8 , parameter :: nmax = 100000
2021-01-11 18:41:36 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-12 11:23:13 +01:00
double precision :: X(nruns), accep(nruns)
2021-01-11 18:41:36 +01:00
double precision :: ave, err
do irun=1,nruns
2021-01-28 15:04:57 +01:00
call variational_montecarlo(a,dt,nmax,X(irun),accep(irun))
2021-01-11 18:41:36 +01:00
enddo
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-12 11:23:13 +01:00
call ave_error(accep,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-11 18:41:36 +01:00
end program qmc
2021-01-26 13:11:57 +01:00
#+END_SRC
2021-01-11 18:41:36 +01:00
2021-01-27 13:04:32 +01:00
#+begin_src sh :results output :exports results
2021-01-12 11:23:13 +01:00
gfortran hydrogen.f90 qmc_stats.f90 vmc_metropolis.f90 -o vmc_metropolis
./vmc_metropolis
2021-01-26 13:11:57 +01:00
#+end_src
2021-01-12 10:55:00 +01:00
2021-01-26 13:11:57 +01:00
#+RESULTS:
: E = -0.47940635575542706 +/- 5.5613594433433764E-004
: A = 0.62037333333333333 +/- 4.8970160591451110E-004
2021-01-12 01:01:52 +01:00
2021-02-04 11:24:38 +01:00
* Diffusion Monte Carlo
2021-01-26 17:06:12 +01:00
2021-02-02 22:47:44 +01:00
As we have seen, Variational Monte Carlo is a powerful method to
compute integrals in large dimensions. It is often used in cases
where the expression of the wave function is such that the integrals
can't be evaluated (multi-centered Slater-type orbitals, correlation
factors, etc).
Diffusion Monte Carlo is different. It goes beyond the computation
of the integrals associated with an input wave function, and aims at
finding a near-exact numerical solution to the Schrödinger equation.
2021-01-27 13:04:32 +01:00
** Schrödinger equation in imaginary time
Consider the time-dependent Schrödinger equation:
2021-01-26 17:06:12 +01:00
2021-01-27 13:04:32 +01:00
\[
2021-02-01 13:51:59 +01:00
i\frac{\partial \Psi(\mathbf{r},t)}{\partial t} = (\hat{H} -E_{\rm ref}) \Psi(\mathbf{r},t)\,.
2021-01-27 13:04:32 +01:00
\]
2021-01-26 17:06:12 +01:00
2021-02-01 21:56:01 +01:00
where we introduced a shift in the energy, $E_{\rm ref}$, for reasons which will become apparent below.
2021-01-31 19:15:39 +01:00
2021-01-31 17:01:12 +01:00
We can expand a given starting wave function, $\Psi(\mathbf{r},0)$, in the basis of the eigenstates
2021-01-31 19:15:39 +01:00
of the time-independent Hamiltonian, $\Phi_k$, with energies $E_k$:
2021-01-26 17:06:12 +01:00
2021-01-27 13:04:32 +01:00
\[
\Psi(\mathbf{r},0) = \sum_k a_k\, \Phi_k(\mathbf{r}).
\]
2021-01-26 17:06:12 +01:00
2021-01-27 13:04:32 +01:00
The solution of the Schrödinger equation at time $t$ is
2021-01-26 17:06:12 +01:00
2021-01-27 13:04:32 +01:00
\[
2021-02-01 13:51:59 +01:00
\Psi(\mathbf{r},t) = \sum_k a_k \exp \left( -i\, (E_k-E_{\rm ref})\, t \right) \Phi_k(\mathbf{r}).
2021-01-27 13:04:32 +01:00
\]
2021-01-26 17:06:12 +01:00
2021-01-31 17:01:12 +01:00
Now, if we replace the time variable $t$ by an imaginary time variable
2021-01-27 13:04:32 +01:00
$\tau=i\,t$, we obtain
2021-01-26 17:06:12 +01:00
2021-01-27 13:04:32 +01:00
\[
2021-02-01 13:51:59 +01:00
-\frac{\partial \psi(\mathbf{r}, \tau)}{\partial \tau} = (\hat{H} -E_{\rm ref}) \psi(\mathbf{r}, \tau)
2021-01-27 13:04:32 +01:00
\]
2021-01-26 17:06:12 +01:00
2021-02-01 21:56:01 +01:00
where $\psi(\mathbf{r},\tau) = \Psi(\mathbf{r},-i\,\tau)$
2021-01-27 13:04:32 +01:00
and
2021-01-31 20:06:13 +01:00
2021-01-31 19:15:39 +01:00
\begin{eqnarray*}
2021-02-01 21:56:01 +01:00
\psi(\mathbf{r},\tau) &=& \sum_k a_k \exp( -(E_k-E_{\rm ref})\, \tau) \Phi_k(\mathbf{r})\\
&=& \exp(-(E_0-E_{\rm ref})\, \tau)\sum_k a_k \exp( -(E_k-E_0)\, \tau) \Phi_k(\mathbf{r})\,.
2021-01-31 20:06:13 +01:00
\end{eqnarray*}
2021-01-31 19:15:39 +01:00
2021-01-27 13:04:32 +01:00
For large positive values of $\tau$, $\psi$ is dominated by the
2021-02-01 13:51:59 +01:00
$k=0$ term, namely, the lowest eigenstate. If we adjust $E_{\rm ref}$ to the running estimate of $E_0$,
2021-01-31 19:15:39 +01:00
we can expect that simulating the differetial equation in
2021-01-27 13:04:32 +01:00
imaginary time will converge to the exact ground state of the
system.
2021-01-27 00:56:36 +01:00
2021-02-02 22:47:44 +01:00
** Relation to diffusion
2021-01-27 00:56:36 +01:00
2021-02-02 22:47:44 +01:00
The [[https://en.wikipedia.org/wiki/Diffusion_equation][diffusion equation]] of particles is given by
2021-01-27 00:56:36 +01:00
2021-01-27 13:04:32 +01:00
\[
2021-02-02 22:47:44 +01:00
\frac{\partial \psi(\mathbf{r},t)}{\partial t} = D\, \Delta \psi(\mathbf{r},t)
2021-01-27 13:04:32 +01:00
\]
2021-01-27 00:56:36 +01:00
2021-02-02 22:47:44 +01:00
where $D$ is the diffusion coefficient. When the imaginary-time
Schrödinger equation is written in terms of the kinetic energy and
potential,
2021-01-27 13:04:32 +01:00
\[
2021-02-02 22:47:44 +01:00
\frac{\partial \psi(\mathbf{r}, \tau)}{\partial \tau} =
\left(\frac{1}{2}\Delta - [V(\mathbf{r}) -E_{\rm ref}]\right) \psi(\mathbf{r}, \tau)\,,
2021-01-27 13:04:32 +01:00
\]
2021-02-02 22:47:44 +01:00
it can be identified as the combination of:
- a diffusion equation (Laplacian)
- an equation whose solution is an exponential (potential)
2021-01-27 00:56:36 +01:00
2021-01-27 13:04:32 +01:00
The diffusion equation can be simulated by a Brownian motion:
2021-01-31 19:15:39 +01:00
2021-01-28 15:04:57 +01:00
\[ \mathbf{r}_{n+1} = \mathbf{r}_{n} + \sqrt{\delta t}\, \chi \]
2021-01-31 19:15:39 +01:00
2021-02-02 22:47:44 +01:00
where $\chi$ is a Gaussian random variable, and the potential term
2021-01-27 13:04:32 +01:00
can be simulated by creating or destroying particles over time (a
2021-02-02 22:47:44 +01:00
so-called branching process) or by simply considering it as a
2021-02-02 23:41:07 +01:00
cumulative multiplicative weight along the diffusion trajectory
(pure Diffusion Monte Carlo):
2021-02-02 23:07:15 +01:00
\[
2021-02-03 17:24:21 +01:00
\prod_i \exp \left( - (V(\mathbf{r}_i) - E_{\text{ref}}) \delta t \right).
2021-02-02 23:07:15 +01:00
\]
2021-01-27 00:56:36 +01:00
2021-01-27 13:04:32 +01:00
2021-02-01 09:07:13 +01:00
We note that the ground-state wave function of a Fermionic system is
2021-02-01 11:30:00 +01:00
antisymmetric and changes sign. Therefore, its interpretation as a probability
2021-02-01 09:07:13 +01:00
distribution is somewhat problematic. In fact, mathematically, since
the Bosonic ground state is lower in energy than the Fermionic one, for
large $\tau$, the system will evolve towards the Bosonic solution.
2021-01-31 20:06:13 +01:00
2021-02-01 11:30:00 +01:00
For the systems you will study, this is not an issue:
2021-02-01 09:07:13 +01:00
- Hydrogen atom: You only have one electron!
2021-02-02 22:47:44 +01:00
- Two-electron system ($H_2$ or He): The ground-wave function is
antisymmetric in the spin variables but symmetric in the space ones.
2021-02-01 09:07:13 +01:00
Therefore, in both cases, you are dealing with a "Bosonic" ground state.
2021-01-31 20:06:13 +01:00
2021-01-27 13:04:32 +01:00
** Importance sampling
2021-01-27 00:56:36 +01:00
2021-01-31 19:15:39 +01:00
In a molecular system, the potential is far from being constant
2021-02-02 22:47:44 +01:00
and, in fact, diverges at the inter-particle coalescence points. Hence,
2021-02-02 23:41:07 +01:00
it results in very large fluctuations of the erm weight associated with
2021-02-02 22:47:44 +01:00
the potental, making the calculations impossible in practice.
2021-01-27 13:04:32 +01:00
Fortunately, if we multiply the Schrödinger equation by a chosen
/trial wave function/ $\Psi_T(\mathbf{r})$ (Hartree-Fock, Kohn-Sham
2021-01-29 13:23:00 +01:00
determinant, CI wave function, /etc/), one obtains
2021-01-27 13:04:32 +01:00
\[
-\frac{\partial \psi(\mathbf{r},\tau)}{\partial \tau} \Psi_T(\mathbf{r}) =
\left[ -\frac{1}{2} \Delta \psi(\mathbf{r},\tau) + V(\mathbf{r}) \psi(\mathbf{r},\tau) \right] \Psi_T(\mathbf{r})
\]
2021-01-29 13:23:00 +01:00
Defining $\Pi(\mathbf{r},\tau) = \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})$, (see appendix for details)
2021-01-27 13:04:32 +01:00
\[
-\frac{\partial \Pi(\mathbf{r},\tau)}{\partial \tau}
= -\frac{1}{2} \Delta \Pi(\mathbf{r},\tau) +
\nabla \left[ \Pi(\mathbf{r},\tau) \frac{\nabla \Psi_T(\mathbf{r})}{\Psi_T(\mathbf{r})}
2021-02-01 13:51:59 +01:00
\right] + (E_L(\mathbf{r})-E_{\rm ref})\Pi(\mathbf{r},\tau)
2021-01-27 13:04:32 +01:00
\]
2021-01-31 20:06:13 +01:00
The new "kinetic energy" can be simulated by the drift-diffusion
2021-01-28 15:04:57 +01:00
scheme presented in the previous section (VMC).
2021-01-27 13:04:32 +01:00
The new "potential" is the local energy, which has smaller fluctuations
2021-02-02 22:47:44 +01:00
when $\Psi_T$ gets closer to the exact wave function.
2021-02-02 23:41:07 +01:00
This term can be simulated by
\[
2021-02-03 17:24:21 +01:00
\prod_i \exp \left( - (E_L(\mathbf{r}_i) - E_{\text{ref}}) \delta t \right).
2021-02-02 23:41:07 +01:00
\]
2021-02-01 13:51:59 +01:00
where $E_{\rm ref}$ is the constant we had introduced above, which is adjusted to
2021-02-02 23:41:07 +01:00
an estimate of the average energy to keep the weights close to one.
2021-01-27 00:56:36 +01:00
2021-01-27 15:21:44 +01:00
This equation generates the /N/-electron density $\Pi$, which is the
2021-02-02 23:41:07 +01:00
product of the ground state solution with the trial wave
function. You may then ask: how can we compute the total energy of
the system?
2021-02-01 09:07:13 +01:00
2021-02-02 23:41:07 +01:00
To this aim, we use the /mixed estimator/ of the energy:
2021-02-01 09:07:13 +01:00
\begin{eqnarray*}
2021-02-01 22:09:14 +01:00
E(\tau) &=& \frac{\langle \psi(\tau) | \hat{H} | \Psi_T \rangle}{\langle \psi(\tau) | \Psi_T \rangle}\\
2021-02-01 09:07:13 +01:00
&=& \frac{\int \psi(\mathbf{r},\tau) \hat{H} \Psi_T(\mathbf{r}) d\mathbf{r}}
{\int \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) d\mathbf{r}} \\
2021-02-01 13:51:59 +01:00
&=& \frac{\int \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) E_L(\mathbf{r}) d\mathbf{r}}
{\int \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) d\mathbf{r}} \,.
2021-02-01 09:07:13 +01:00
\end{eqnarray*}
2021-02-01 13:51:59 +01:00
For large $\tau$, we have that
2021-02-01 09:07:13 +01:00
\[
\Pi(\mathbf{r},\tau) =\psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \rightarrow \Phi_0(\mathbf{r}) \Psi_T(\mathbf{r})\,,
\]
2021-02-02 23:41:07 +01:00
and, using that $\hat{H}$ is Hermitian and that $\Phi_0$ is an
eigenstate of the Hamiltonian, we obtain for large $\tau$
2021-01-28 15:04:57 +01:00
2021-02-01 09:07:13 +01:00
\[
E(\tau) = \frac{\langle \psi_\tau | \hat{H} | \Psi_T \rangle}
{\langle \psi_\tau | \Psi_T \rangle}
= \frac{\langle \Psi_T | \hat{H} | \psi_\tau \rangle}
{\langle \Psi_T | \psi_\tau \rangle}
2021-02-01 13:51:59 +01:00
\rightarrow E_0 \frac{\langle \Psi_T | \Phi_0 \rangle}
{\langle \Psi_T | \Phi_0 \rangle}
2021-02-01 09:07:13 +01:00
= E_0
\]
2021-01-28 15:04:57 +01:00
2021-02-01 09:07:13 +01:00
Therefore, we can compute the energy within DMC by generating the
density $\Pi$ with random walks, and simply averaging the local
energies computed with the trial wave function.
2021-01-27 13:04:32 +01:00
*** Appendix : Details of the Derivation
\[
-\frac{\partial \psi(\mathbf{r},\tau)}{\partial \tau} \Psi_T(\mathbf{r}) =
\left[ -\frac{1}{2} \Delta \psi(\mathbf{r},\tau) + V(\mathbf{r}) \psi(\mathbf{r},\tau) \right] \Psi_T(\mathbf{r})
\]
\[
-\frac{\partial \big[ \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big]}{\partial \tau}
= -\frac{1}{2} \Big( \Delta \big[
\psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big] -
\psi(\mathbf{r},\tau) \Delta \Psi_T(\mathbf{r}) - 2
\nabla \psi(\mathbf{r},\tau) \nabla \Psi_T(\mathbf{r}) \Big) + V(\mathbf{r}) \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})
\]
\[
-\frac{\partial \big[ \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big]}{\partial \tau}
= -\frac{1}{2} \Delta \big[\psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big] +
\frac{1}{2} \psi(\mathbf{r},\tau) \Delta \Psi_T(\mathbf{r}) +
\Psi_T(\mathbf{r})\nabla \psi(\mathbf{r},\tau) \frac{\nabla \Psi_T(\mathbf{r})}{\Psi_T(\mathbf{r})} + V(\mathbf{r}) \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})
\]
\[
-\frac{\partial \big[ \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big]}{\partial \tau}
= -\frac{1}{2} \Delta \big[\psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big] +
\psi(\mathbf{r},\tau) \Delta \Psi_T(\mathbf{r}) +
\Psi_T(\mathbf{r})\nabla \psi(\mathbf{r},\tau) \frac{\nabla \Psi_T(\mathbf{r})}{\Psi_T(\mathbf{r})} + E_L(\mathbf{r}) \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})
\]
\[
-\frac{\partial \big[ \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big]}{\partial \tau}
= -\frac{1}{2} \Delta \big[\psi(\mathbf{r},\tau) \Psi_T(\mathbf{r}) \big] +
\nabla \left[ \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})
\frac{\nabla \Psi_T(\mathbf{r})}{\Psi_T(\mathbf{r})}
\right] + E_L(\mathbf{r}) \psi(\mathbf{r},\tau) \Psi_T(\mathbf{r})
\]
Defining $\Pi(\mathbf{r},t) = \psi(\mathbf{r},\tau)
\Psi_T(\mathbf{r})$,
\[
-\frac{\partial \Pi(\mathbf{r},\tau)}{\partial \tau}
= -\frac{1}{2} \Delta \Pi(\mathbf{r},\tau) +
\nabla \left[ \Pi(\mathbf{r},\tau) \frac{\nabla \Psi_T(\mathbf{r})}{\Psi_T(\mathbf{r})}
\right] + E_L(\mathbf{r}) \Pi(\mathbf{r},\tau)
\]
2021-01-28 15:04:57 +01:00
2021-02-02 23:41:07 +01:00
** Pure Diffusion Monte Carlo
2021-01-27 15:21:44 +01:00
2021-01-28 15:04:57 +01:00
Instead of having a variable number of particles to simulate the
2021-02-02 23:41:07 +01:00
branching process as in the /Diffusion Monte Carlo/ (DMC) algorithm, we
use variant called /pure Diffusion Monte Carlo/ (PDMC) where
the potential term is considered as a cumulative product of weights:
2021-01-28 15:04:57 +01:00
2021-02-02 17:00:49 +01:00
\begin{eqnarray*}
2021-02-03 17:24:21 +01:00
W(\mathbf{r}_n, \tau) = \prod_{i=1}^{n} \exp \left( -\delta t\,
2021-01-29 13:23:00 +01:00
(E_L(\mathbf{r}_i) - E_{\text{ref}}) \right) =
\prod_{i=1}^{n} w(\mathbf{r}_i)
2021-02-02 17:00:49 +01:00
\end{eqnarray*}
2021-01-28 15:04:57 +01:00
2021-02-02 23:41:07 +01:00
where $\mathbf{r}_i$ are the coordinates along the trajectory and
we introduced a /time-step variable/ $\delta t$ to discretize the
integral.
2021-02-02 17:04:18 +01:00
2021-02-02 23:41:07 +01:00
The PDMC algorithm is less stable than the DMC algorithm: it
requires to have a value of $E_\text{ref}$ which is close to the
fixed-node energy, and a good trial wave function. Moreover, we
can't let $\tau$ become too large because the weight whether
explode or vanish: we need to have a fixed value of $\tau$
(projection time).
The big advantage of PDMC is that it is rather simple to implement
starting from a VMC code:
0) Start with $W(\mathbf{r}_0)=1, \tau_0 = 0$
1) Evaluate the local energy at $\mathbf{r}_{n}$
2) Compute the contribution to the weight $w(\mathbf{r}_n) =
\exp(-\delta t(E_L(\mathbf{r}_n)-E_\text{ref}))$
3) Update $W(\mathbf{r}_{n}) = W(\mathbf{r}_{n-1}) \times w(\mathbf{r}_n)$
4) Accumulate the weighted energy $W(\mathbf{r}_n) \times
E_L(\mathbf{r}_n)$,
and the weight $W(\mathbf{r}_n)$ for the normalization
5) Update $\tau_n = \tau_{n-1} + \delta t$
6) If $\tau_{n} > \tau_\text{max}$, the long projection time has
been reached and we can start an new trajectory from the current
position: reset $W(r_n) = 1$ and $\tau_n
= 0$
7) Compute a new position $\mathbf{r'} = \mathbf{r}_n +
2021-02-01 11:30:00 +01:00
\delta t\, \frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})} + \chi$
2021-02-02 23:41:07 +01:00
8) Evaluate $\Psi(\mathbf{r}')$ and $\frac{\nabla \Psi(\mathbf{r'})}{\Psi(\mathbf{r'})}$ at the new position
9) Compute the ratio $A = \frac{T(\mathbf{r}' \rightarrow \mathbf{r}_{n}) P(\mathbf{r}')}{T(\mathbf{r}_{n} \rightarrow \mathbf{r}') P(\mathbf{r}_{n})}$
10) Draw a uniform random number $v \in [0,1]$
11) if $v \le A$, accept the move : set $\mathbf{r}_{n+1} = \mathbf{r'}$
12) else, reject the move : set $\mathbf{r}_{n+1} = \mathbf{r}_n$
2021-02-02 17:04:18 +01:00
2021-02-01 11:30:00 +01:00
Some comments are needed:
- You estimate the energy as
2021-02-01 22:09:14 +01:00
\begin{eqnarray*}
E = \frac{\sum_{k=1}^{N_{\rm MC}} E_L(\mathbf{r}_k) W(\mathbf{r}_k, k\delta t)}{\sum_{k=1}^{N_{\rm MC}} W(\mathbf{r}_k, k\delta t)}
\end{eqnarray*}
2021-02-01 11:30:00 +01:00
2021-02-02 23:41:07 +01:00
- The result will be affected by a time-step error
(the finite size of $\delta t$) due to the discretization of the
integral, and one has in principle to extrapolate to the limit
$\delta t \rightarrow 0$. This amounts to fitting the energy
computed for multiple values of $\delta t$.
2021-02-01 11:30:00 +01:00
2021-02-01 22:09:14 +01:00
Here, you will be using a small enough time-step and you should not worry about the extrapolation.
2021-02-02 23:41:07 +01:00
- The accept/reject step (steps 9-12 in the algorithm) is in principle not needed for the correctness of
2021-02-01 22:09:14 +01:00
the DMC algorithm. However, its use reduces significantly the time-step error.
2021-02-01 11:30:00 +01:00
2021-01-28 15:04:57 +01:00
2021-01-29 13:23:00 +01:00
** Hydrogen atom
:PROPERTIES:
:header-args:python: :tangle pdmc.py
:header-args:f90: :tangle pdmc.f90
:END:
2021-01-13 17:59:48 +01:00
2021-01-21 23:24:21 +01:00
*** Exercise
2021-01-03 18:45:58 +01:00
2021-01-13 17:59:48 +01:00
#+begin_exercise
2021-02-02 23:41:07 +01:00
Modify the Metropolis VMC program into a PDMC program.
2021-01-28 15:04:57 +01:00
In the limit $\delta t \rightarrow 0$, you should recover the exact
2021-02-02 23:41:07 +01:00
energy of H for any value of $a$, as long as the simulation is stable.
2021-02-03 00:07:54 +01:00
We choose here a time step of 0.05 a.u. and a fixed projection
time $\tau$ =100 a.u.
2021-01-13 17:59:48 +01:00
#+end_exercise
2021-01-29 13:23:00 +01:00
*Python*
2021-01-27 00:56:36 +01:00
#+BEGIN_SRC python :results output
2021-01-13 17:59:48 +01:00
from hydrogen import *
from qmc_stats import *
2021-01-03 18:45:58 +01:00
2021-02-01 11:30:00 +01:00
def MonteCarlo(a, nmax, dt, Eref):
2021-01-29 13:23:00 +01:00
# TODO
# Run simulation
a = 1.2
2021-01-29 13:23:00 +01:00
nmax = 100000
2021-02-03 17:24:21 +01:00
dt = 0.05
tau = 100.
2021-01-29 13:23:00 +01:00
E_ref = -0.5
2021-02-01 11:30:00 +01:00
X0 = [ MonteCarlo(a, nmax, dt, E_ref) for i in range(30)]
2021-01-29 13:23:00 +01:00
# 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
2021-02-02 23:41:07 +01:00
#+RESULTS:
2021-01-29 13:23:00 +01:00
*Fortran*
#+BEGIN_SRC f90 :tangle none
subroutine pdmc(a, dt, nmax, energy, accep, tau, E_ref)
implicit none
double precision, intent(in) :: a, dt, tau
integer*8 , intent(in) :: nmax
double precision, intent(out) :: energy, accep
double precision, intent(in) :: E_ref
integer*8 :: istep
integer*8 :: n_accep
double precision :: sq_dt, 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
! TODO
end subroutine pdmc
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-02-03 17:24:21 +01:00
double precision, parameter :: dt = 0.05d0
2021-01-29 13:23:00 +01:00
double precision, parameter :: E_ref = -0.5d0
2021-02-03 17:24:21 +01:00
double precision, parameter :: tau = 100.d0
2021-01-29 13:23:00 +01:00
integer*8 , parameter :: nmax = 100000
integer , parameter :: nruns = 30
integer :: irun
double precision :: X(nruns), accep(nruns)
double precision :: ave, err
do irun=1,nruns
call pdmc(a, dt, nmax, X(irun), accep(irun), tau, E_ref)
enddo
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
call ave_error(accep,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
end program qmc
#+END_SRC
#+begin_src sh :results output :exports code
gfortran hydrogen.f90 qmc_stats.f90 pdmc.f90 -o pdmc
./pdmc
#+end_src
**** Solution :solution:
*Python*
#+BEGIN_SRC python :results output
2021-02-02 13:30:11 +01:00
#!/usr/bin/env python3
2021-01-29 13:23:00 +01:00
from hydrogen import *
from qmc_stats import *
def MonteCarlo(a, nmax, dt, tau, Eref):
sq_dt = np.sqrt(dt)
energy = 0.
N_accep = 0
2021-01-26 13:11:57 +01:00
normalization = 0.
2021-01-29 13:23:00 +01:00
2021-02-03 00:03:24 +01:00
w = 1.
2021-01-29 13:23:00 +01:00
tau_current = 0.
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)
2021-01-13 17:59:48 +01:00
psi_old = psi(a,r_old)
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
for istep in range(nmax):
el = e_loc(a,r_old)
2021-01-29 13:23:00 +01:00
w *= np.exp(-dt*(el - Eref))
2021-01-26 13:11:57 +01:00
normalization += w
2021-01-29 13:23:00 +01:00
energy += w * el
2021-01-13 17:59:48 +01:00
2021-01-29 13:23:00 +01:00
tau_current += dt
# Reset when tau is reached
2021-02-03 00:03:24 +01:00
if tau_current > tau:
w = 1.
2021-01-29 13:23:00 +01:00
tau_current = 0.
chi = np.random.normal(loc=0., scale=1.0, size=(3))
r_new = r_old + dt * d_old + sq_dt * chi
2021-01-13 17:59:48 +01:00
d_new = drift(a,r_new)
d2_new = np.dot(d_new,d_new)
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
# Metropolis
prod = np.dot((d_new + d_old), (r_new - r_old))
2021-01-29 13:23:00 +01:00
argexpo = 0.5 * (d2_new - d2_old)*dt + prod
2021-01-13 17:59:48 +01:00
q = psi_new / psi_old
q = np.exp(-argexpo) * q*q
2021-01-29 13:23:00 +01:00
if np.random.uniform() <= q:
N_accep += 1
r_old = r_new
d_old = d_new
d2_old = d2_new
2021-01-13 17:59:48 +01:00
psi_old = psi_new
2021-01-29 13:23:00 +01:00
return energy/normalization, N_accep/nmax
2021-01-13 17:59:48 +01:00
2021-01-29 13:23:00 +01:00
# Run simulation
a = 1.2
2021-01-29 13:23:00 +01:00
nmax = 100000
2021-02-03 00:03:24 +01:00
dt = 0.05
tau = 100.
2021-01-26 13:11:57 +01:00
E_ref = -0.5
2021-01-13 17:59:48 +01:00
2021-01-29 13:23:00 +01:00
X0 = [ MonteCarlo(a, nmax, dt, tau, E_ref) 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
#+RESULTS:
2021-02-03 00:03:24 +01:00
: E = -0.500188803288012 +/- 0.0010615739297642462
: A = 0.9896496666666668 +/- 7.688845979106312e-05
2021-01-29 13:23:00 +01:00
*Fortran*
2021-01-26 13:11:57 +01:00
#+BEGIN_SRC f90
2021-01-29 13:23:00 +01:00
subroutine pdmc(a, dt, nmax, energy, accep, tau, E_ref)
2021-01-13 17:59:48 +01:00
implicit none
2021-01-29 13:23:00 +01:00
double precision, intent(in) :: a, dt, tau
2021-01-13 17:59:48 +01:00
integer*8 , intent(in) :: nmax
2021-01-29 13:23:00 +01:00
double precision, intent(out) :: energy, accep
double precision, intent(in) :: E_ref
2021-01-13 17:59:48 +01:00
2021-01-29 13:23:00 +01:00
integer*8 :: istep
integer*8 :: n_accep
double precision :: sq_dt, chi(3), d2_old, prod, u
2021-01-13 17:59:48 +01:00
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)
2021-01-29 13:23:00 +01:00
double precision :: e, w, norm, tau_current
2021-01-13 17:59:48 +01:00
double precision, external :: e_loc, psi
2021-01-29 13:23:00 +01:00
sq_dt = dsqrt(dt)
2021-01-13 17:59:48 +01:00
! Initialization
2021-01-29 13:23:00 +01:00
energy = 0.d0
n_accep = 0_8
norm = 0.d0
w = 1.d0
tau_current = 0.d0
2021-01-13 17:59:48 +01:00
call random_gauss(r_old,3)
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
call drift(a,r_old,d_old)
2021-01-29 13:23:00 +01:00
d2_old = d_old(1)*d_old(1) + &
d_old(2)*d_old(2) + &
d_old(3)*d_old(3)
2021-01-13 17:59:48 +01:00
psi_old = psi(a,r_old)
do istep = 1,nmax
2021-01-29 13:23:00 +01:00
e = e_loc(a,r_old)
w = w * dexp(-dt*(e - E_ref))
norm = norm + w
2021-02-03 00:03:24 +01:00
energy = energy + w*e
2021-01-29 13:23:00 +01:00
tau_current = tau_current + dt
! Reset when tau is reached
2021-02-03 00:03:24 +01:00
if (tau_current > tau) then
w = 1.d0
2021-01-29 13:23:00 +01:00
tau_current = 0.d0
endif
2021-01-13 17:59:48 +01:00
call random_gauss(chi,3)
2021-01-29 13:23:00 +01:00
r_new(:) = r_old(:) + dt*d_old(:) + chi(:)*sq_dt
2021-01-13 17:59:48 +01:00
call drift(a,r_new,d_new)
2021-01-29 13:23:00 +01:00
d2_new = d_new(1)*d_new(1) + &
d_new(2)*d_new(2) + &
d_new(3)*d_new(3)
2021-01-13 17:59:48 +01:00
psi_new = psi(a,r_new)
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
! 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))
2021-01-29 13:23:00 +01:00
argexpo = 0.5d0 * (d2_new - d2_old)*dt + prod
2021-01-13 17:59:48 +01:00
q = psi_new / psi_old
q = dexp(-argexpo) * q*q
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
call random_number(u)
2021-01-29 13:23:00 +01:00
if (u <= q) then
n_accep = n_accep + 1_8
2021-01-13 17:59:48 +01:00
r_old(:) = r_new(:)
d_old(:) = d_new(:)
2021-01-29 13:23:00 +01:00
d2_old = d2_new
psi_old = psi_new
2021-01-13 17:59:48 +01:00
end if
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
end do
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
energy = energy / norm
2021-01-29 13:23:00 +01:00
accep = dble(n_accep) / dble(nmax)
end subroutine pdmc
2021-01-13 17:59:48 +01:00
program qmc
implicit none
double precision, parameter :: a = 1.2d0
2021-02-03 00:03:24 +01:00
double precision, parameter :: dt = 0.05d0
2021-01-29 13:23:00 +01:00
double precision, parameter :: E_ref = -0.5d0
2021-02-03 00:03:24 +01:00
double precision, parameter :: tau = 100.d0
2021-01-29 13:23:00 +01:00
integer*8 , parameter :: nmax = 100000
2021-01-13 17:59:48 +01:00
integer , parameter :: nruns = 30
2021-01-29 13:23:00 +01:00
integer :: irun
2021-01-13 17:59:48 +01:00
double precision :: X(nruns), accep(nruns)
double precision :: ave, err
do irun=1,nruns
2021-01-29 13:23:00 +01:00
call pdmc(a, dt, nmax, X(irun), accep(irun), tau, E_ref)
2021-01-13 17:59:48 +01:00
enddo
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
call ave_error(accep,nruns,ave,err)
print *, 'A = ', ave, '+/-', err
2021-01-29 13:23:00 +01:00
2021-01-13 17:59:48 +01:00
end program qmc
2021-01-29 13:23:00 +01:00
#+END_SRC
#+begin_src sh :results output :exports results
gfortran hydrogen.f90 qmc_stats.f90 pdmc.f90 -o pdmc
./pdmc
#+end_src
2021-01-13 17:59:48 +01:00
2021-01-29 13:23:00 +01:00
#+RESULTS:
2021-02-03 00:03:24 +01:00
: E = -0.49963953547336709 +/- 6.8755513992017491E-004
: A = 0.98963533333333342 +/- 6.3052128284666221E-005
2021-01-13 17:59:48 +01:00
2021-01-20 19:12:05 +01:00
2021-02-03 00:03:24 +01:00
* Project
Change your PDMC code for one of the following:
- the Helium atom, or
2021-02-03 00:05:48 +01:00
- the H_2 molecule at $R$ =1.401 bohr.
2021-02-03 00:03:24 +01:00
And compute the ground state energy.
2021-02-03 17:58:09 +01:00
* Schedule :noexport:
2021-02-02 11:28:34 +01:00
|------------------------------+---------|
| <2021-02-04 Thu 09:00-10:30> | Lecture |
|------------------------------+---------|
2021-02-04 11:24:38 +01:00
| <2021-02-04 Thu 11:00-11:20> | 2.1 |
| <2021-02-04 Thu 11:20-11:40> | 2.2 |
| <2021-02-04 Thu 11:40-12:15> | 2.3 |
2021-02-02 11:28:34 +01:00
| <2021-02-04 Thu 12:15-12:30> | 2.4 |
|------------------------------+---------|
2021-02-02 13:30:11 +01:00
| <2021-02-04 Thu 14:00-14:10> | 3.1 |
| <2021-02-04 Thu 14:10-14:30> | 3.2 |
| <2021-02-04 Thu 14:30-15:30> | 3.3 |
| <2021-02-04 Thu 15:30-16:30> | 3.4 |
2021-02-03 00:03:24 +01:00
| <2021-02-04 Thu 16:30-18:30> | 4.5 |
2021-02-02 13:30:11 +01:00
|------------------------------+---------|
2021-02-03 17:58:09 +01:00
* Acknowledgments
[[https://trex-coe.eu/sites/default/files/inline-images/euflag.jpg]]
[[https://trex-coe.eu][TREX]] : Targeting Real Chemical Accuracy at the Exascale project
has received funding from the European Unions Horizon 2020 - Research and
Innovation program - under grant agreement no. 952165. The content of this
document does not represent the opinion of the European Union, and the European
Union is not responsible for any use that might be made of such content.