XY Model Project

I took a grad-level physics course on condensed matter physics. I chose to do a final project about the classical XY Model because it was interesting to me and I wanted to learn how to use the Metropolis algorithm for a statistical ensemble such as this.
Download
You can download a JAR file of this simulation by clicking the button below. If you have Java installed on your computer, you can run it simply by opening it like you would any other program. Unfortunately, you will need a computer (not a mobile device) to run this program.
If you download the simulation, I suggest setting the slider to 1e-1 (one notch to the left of zero) and watching vortex-antivortex pairs form, then annihilate.
Background
The classical XY model is a physical model that describes (loosely speaking) how ferromagnetism (a particular kind of magnetism) works at different temperatures. In this model, a 2D grid of particles interact with each other via their magnetic spin (represented by arrows in my simulation) and together form a magnet. The bulk magnet has a net magnetic field when the spins align. When the spins do not align, they cancel each other's magnetic field out and the bulk magnet does not have a net magnetic field.
The energy of a particle in the system has is lowest when the particle's spin is aligned with nearby particle's spins. The particles tend to align with each other at very low temperatures because there is little thermal energy to prevent them from settling into their lowest-energy configuration. If the temperature is increased, the particles gain energy in the form of thermal fluctuations and their spin moves around a little bit, and their spin is occasionally able to bump out of alignment with their neighbors. At very high temperatures, the spins fluctuate too frequently for there to be any large-scale alignment. This causes the ferromagnet to "melt" and lose its magnetization.
This is the basis for the field of paleomagnetism ("paleo" meaning "early" or "prehistoric"), which is the study of Earth's magnetic field throughout Earth's history. One of the greatest successes of paleomagnetism was to demonstrate that continents really do drift apart from each other. This idea had been proposed as early as 1596 by Abraham Ortelius as a way to explain why the eastern coast of the Americas fit so neatly with the western coast of Europe and Africa. It was also suggested again several times by other scientists through the first half of the 20th century, but was rejected because nobody could imagine what could possibly move something as massive as a continent.
During World War II, naval submarines noticed odd patterns in magnetic field measurements. Scientists later deduced from these and similar measurements that the seafloor was spreading apart at underwater seams in between continents. At the spreading point, lava poured out of the seam and cooled. The small magnetic particles in the lava were initially hot and were able to spin in alignment with Earth's much stronger magnetic field. As the lava hit the ocean water and cooled, the spinners were no longer able to rotate, leaving a lasting imprint of the direction of Earth's magnetic field at the time the lava froze. They key to the continental drift puzzle was the discovery that the magnetic field patterns on one side of the seam were a mirror-image to the magnetic field patterns on the other side of the seam. This implied that lava was being carried away from the seam in both directions as if by an underground conveyor belt. This could only make sense if the continents were drifting apart from that seam. This explanation is now known as the Vine–Mathews–Morley hypothesis.
Vortices and Anti-Vortices
At low temperatures, nearly all spins align. Those that do not form vortices (colored red in my simulation) or anti-vortices (colored blue in my simulation). At low temperature, vortices and antivortices come in bound pairs, but above a critical temperature, they become unpaired. This type of transition is now known as a Berezinskii–Kosterlitz–Thouless phase transition (BKT transition), for which the 2016 Nobel Prize in Physics was awarded.

Simulation
In this simulation, the direction of each particle's spin is represented by arrows. Red color indicates vorticity and blue indicates anti-vorticity, while green indicates that a given particle is approximately aligned with its neighbors. You can adjust the temperature of the simulation via the slider at the top of the animation. The system can be re-randomized either by restarting the app or by using a high temperature to "melt" the magnet.
Adjusting the temperature allows one to explore BKT transitions with the vortex-antivortex pairings. Low temperatures force the spins to algin or cluster in vortex-antivortex pairs, while high temperatures cause the spins to fluctuate and destroy any large-scale alignment, effectively "melting" the magnet.
Many times per second, the Metropolis algorithm perturbs a randomly-selected spinner's direction. If the change is energetically favorable ($\Delta E < 0$), the spinner keeps its new direction; otherwise the spinner keeps its new direction with probability $p=e^{-\beta \Delta E}$, where $\beta = \left(k_B T\right)^{-1}$ is the inverse temperature. The temperature scale on the slider is in natural units of $1/\beta$ with the Boltzmann constant $k_B = 1$.
"If a picture is worth a thousand words, then a movie is worth a thousand pictures, a simulation is worth a thousand movies, and a framework is worth a thousand simulations."