Skip to content

ericaloken/Project4-FYS3150

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

189 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monte Carlo Simulation of Phase Transition in the 2D Ising—Lenz Model

This repository contains the work of Erica Løken, Mads Mestl and Heine Husdal for Project 4 in the course FYS3150 – Computational Physics at the University of Oslo.

Project introduction

In this project, we study the temperature-dependent behaviour of the two-dimensional Ising—Lenz model. The model consists of a lattice of spins, where each spin can take values $\pm 1$, representing interacting magnetic moments. The system is studied in thermal equilibirum at different temperatures $T$ to study how the system changes according to temperature.

The Ising—Lenz model is simulated using the Markov chain Monte Carlo method with the Metropolis—Hastings acceptance rule, where spins are flipped according to probabilities given by the Boltzmann distribution. We study the thermodynamic quantities of the system such as the energy per spin, magnetisation per spin, heat capacity per spin and magnetic susceptibility per spin as function of temperature and lattice size $L$. The main objective is to deterimine the critical temperature $T_c$ where the system undergoes a phase transition from disordered, non-magnetised state to a ordered, magnetised state for different lattice sizes $L$. Through a linear scaling relation, these values are used to estimate the critical temperature for an infinite lattice, and we compare this value to Lars Onsagers theretical value $T_c(L=\infty) \approx 2.269~J/k_B$. In addition, we investigate the burn-in behaviour of different Ising models and perform an analysis of the speed-up of runtime from parallelising independent walkers for different numbers of threads.

Folder structure

  • src/ - C++ source files
  • include/ - Header files
  • python/ - Python scripts for plotting results
  • figures/ - Generated plots and graphs
  • output/ - Simulation output files
  • data/ - Precomputed simulation output files used in the report
  • report/ - Project pdf report

Compiling and running C++ code

All C++ programs are compiled and executed using the provided Makefile.

Compiling the code

To compile the C++ code, navigate to the project root directory and run:

make

This will build all executables in build/. If you wish to compile a specifc program, individual builds are made using the following commands:

make 2x2            # build 2×2 validation program
make burnin         # build burn-in simulation
make hist           # build histogram simulation
make temps          # build temperature-sweep simulation
make temps_zoom     # build temperature-sweep simulation with zoomed temperatures
make timing         # build timing test program

The above was tested on Linux, but it might not work out of the box on other systems due to library paths. You might need to adjust the CXX and CXXFLAGS variables in the Makefile according to your system setup. For example, on a Mac you might use:

# Mac OS with Homebrew-installed g++
make CXX=g++-12 CXXFLAGS='-O3 -std=c++17 -I include -Wall -Wextra -fopenmp'

Running the code

To run the compiled C++ code, navigate to the project root and run the corresponding make commands:

make run-2x2           # run analytical validation of 2x2 lattice at T = 1.0 J/kB
make run-burnin        # run burn-in simulation at T = 1.0 J/kB and T = 2.4 J/kB
make run-hist          # run histogram simulation for L = 20
make run-temps         # run temperataure sweep for L = 40, 60, 80, 100
make run-temps-zoom    # run temperataure sweep for finding Cv/N peak for L = 40, 60, 80, 100
make run-timing        # run timing test for parallelisation analysis

If not already built, they will auto-compile. Output files are saved in output/ directory.

Parallelisation (OpenMP)

Some programs are parallelised with OpenMP. To specify the number of threads, set the OMP_NUM_THREADS environment variable before running. For example, to use 4 threads on main_histogram.cpp, run:

OMP_NUM_THREADS=4 make run-hist

Plotting results using Python

For plotting, a python environment is required. To install the required packages, run:

pip install -r requirements.txt

For plotting results after running all simulations, navigate to the python/ directory and run plotting scripts using:

python plot_burnin.py                   # Burn-in plot
python plot_histogram.py                # Histogram plots
python plot_error_convergence_2x2.py    # Plot of running relative error for 2x2 lattice
python plot_temperatures.py             # Observables (eps, m, Cv/N, chi/N) vs temperature
python plot_critical_temp.py            # Critical temperature extrapolation
python plot_timing_test.py              # Timing test plot

All plots are saved in the figures/ directory.

Compiler information

The C++ code has been successfully compiled and linked using

  • g++ (Ubuntu 13.3.0-6ubuntu2~24.04)
  • g++-12 (Homebrew GCC 12.4.0) 12.4.0

About

Monte Carlo Simulation of Phase Transition in the 2D Ising—Lenz Model

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors