Monte Carlo Simulations to Sample the Canonical Distribution¶
Authors: Dou Du, Taylor James Baird and Giovanni Pizzi
In this notebook, we demonstrate how Monte Carlo simulations sample the canonical distribution, in the case of simple potentials.
Goals¶
- Understand how a Monte Carlo simulation samples the canonical distribution (with simple examples).
- Appreciate the effect of temperature on the simulations.
- Familiarize with the role of numerical parameters (number of steps, step size).
- Gain insight into the difficulties encountered when considering low temperatures, high barriers, and achieving ergodicity.
Background theory¶
Tasks and exercises¶
Find the approximate
(x, y)
coordinates of the minima of the potential both from the potential energy surface plot and from the peaks of the probability distribution (after running the simulation with the default parameters). Do this for both potentials (single and double well). Note that when you hover on the potential energy surface plot, you can also see some black isosurface lines, that can help you find where the minimum is.Solution
There is only one global minimum at(x=0, y=0)
in the single parabolic well case.
In the double well case, two global minima are located at around(x=0, y=3.13)
and(x=0, y=-3.13)
.
In the probability distribution plot, if you run with the default variables, you should see clear peaks around the minima.
Select to the single-well case and use the slider to modify the temperature. What do you expect the converged average total energy to be? Is this the case?
Solution
For a single quantum well with two coordinates (x and y), we have two degrees of freedom. Therefore, by equipartition, we expect the average energy to be $2\cdot \frac {k_B T}{2}=k_B T$ (i.e., in the reduced units used here with $k_B=1$, the same numerical value as the temperature). As you change the temperature (try e.g. 0.1, 1 and 5), you should see that the average energy tends to reach the same value as the temperature. Note: in order to keep the equilibration period very short, it might be convenient to set the starting point to(0, 0)
. Otherwise, note you can zoom in on the plot to better inspect the values.
You will not necessarily converge to the exact value: try to run the simulation more than once, or try to increase the simulation steps or tune the other numerical parameters (we will do this together in the tasks below).
Select to the single well case. Set the starting position to, e.g., (10, 10) to be out of the minimum, and the temperature to 1. Change the max move size (for example, try values around 0.1, 1 and 5). Inspect the convergence of the total energy to the converged result, both in terms of how long the equilibration takes, and the statistical error.
Solution
You should notice that, with a smaller max move size, the simulation takes a longer time to reach convergence. You can for example check how many steps it takes to get within, say, 5% of the converged value (you can zoom in, and note that the y axis is automatically rescaled). On the other hand, while a bigger move size makes the simulation equilibrate quickly, the magnitude of the fluctuations remain larger. In addition, the acceptance rate drops significantly: most moves change the system coordinates by a large amount, increasing significantly the energy of the system, and only a few of these are statistically accepted. The acceptance rate is a typical parameter that one should monitor and tune in a simulation; if it is too large it indicates that we are accepting almost all moves (and probably then the move is small and we are far from the minimum); if it is too small, we are probably moving too much at every step; as a result the simulation becomes much more expensive (it has to perform many calculations and reject most of them) and the average quantities are affected by larger errors.
Let us now investigate the double-well potential, with low temperature and small max move size. You can set the starting position to, e.g., (10, 10), the temperature to 0.5, and the max move size to 0.1. Run the simulation a few times. How many minima does the simulation typically samples? What happens if you increase the max move size? What happens for higher temperatures?
Solution
When the temperature is small, the probability to jump between the two wells across the barrier is very low, and might never happen in the simulation. In this case the simulation is not ergodic, because the distribution obtained weighting the phase-space points (that would occupy identically the two wells) is different from the time average over the finite simulation (that typically sees only one well). By starting at(10, 10)
and using a small step, we typically end up almost always in the closest of the two wells. If we start at the top of the double well (starting coordinates(0, 0)
), with a small temperature (1 or smaller) and small max move size, each simulation will randomly fall in one of the two wells and be trapped there. If you increase the move size, the fluctuations become larger and one gets a better representation of the canonical distribution (e.g. for a max move size of 10) at the expense of larger fluctuations in the averaged quantities). In addition, most moves are rejected because the majority of them bring the system to a much higher energy. As the temperature increases, the thermal fluctuations increase and there is a higher probability that they allow us to visit both wells, if a long-enough simulation is considered.
Interactive visualization¶
(be patient, it might take a few seconds to load)
Legend¶
(How to use the interactive visualization)
Potential energy surface¶
The left panel shows the potential energy surface (PES).
You can rotate the surface by clicking and dragging.
The z
value indicates the value of the potential energy.
When the mouse is on the potential energy surface, you will also see a black isoline, which you can use to find the global minimum.
Controls¶
All the control widgets are on the bottom left.
You can choose the initial x
and y
coordinates of the simulation.
You can choose between two simple potentials: a single parabolic well, and a double well.
The second can help understanding how the algorithm can overcome energy barriers.
You can also tune a number of system and numerical parameters: the simulation temperature, the maximum move size, and the number of the simulation steps.
Running the simulation and resulting plots¶
You can start the simulation by clicking the Run Monte Carlo
button.
This might take a few seconds before it completes. All plots will then be updated.
The histogram of the canonical distribution as sampled by the simulation will be shown as a heatmap on the top right.
By clicking the Show traces
button, it will show 300 (evenly distributed, skipping the first 10000 steps) points on the heatmap.
The total energy as a function of the step number is shown on the bottom right panel. 10000 steps from the beginning of the simulation are skipped for the total energy figure as they are considered as an initial thermalization; note, however, that if you start in a point with very low probability (i.e., far away from the minimum), the simulation might require many more steps to thermalize.