Skip to content

Hey Physics Geeks! I have written this python code to simulate a 2D square and triangle lattice and find the associated properties with the lattice using statistical mechanics. Go ahead and try it out!

License

Notifications You must be signed in to change notification settings

SuvamT0071/Metropolis-Hastings-in-CMP

Repository files navigation

Ising Model Simulation using Metropolis-Hastings Algorithm

Overview

This repository contains a Python implementation of the Ising model simulation using the Metropolis-Hastings algorithm. The code allows for studying phase transitions in magnetic systems, computing specific heat, mean energy, magnetization, and magnetic susceptibility for a given lattice.

Features

  • Generate random 2D Ising spin configurations
  • Compute system energy with free and periodic boundary conditions
  • Simulate spin dynamics using the Metropolis-Hastings algorithm
  • Compute specific heat, mean energy, magnetization, and magnetic susceptibility
  • Support for periodic boundary conditions (PBC)
  • Support for triangular and square lattices
  • Visualise frustrated magnetism

Installation

Clone the repository and install the required dependencies:

git clone https://github.com/SuvamT0071/Metropolis-Hastings-in-CMP.git
cd Metropolis-Hastings-in-CMP/isingmetro
pip install -r requirements.txt

Alternatively, install dependencies manually:

pip install numpy matplotlib tqdm

To install isingmetro, use pip:

pip install isingmetro

or

pip install git+https://github.com/SuvamT0071/Metropolis-Hastings-in-CMP.git

Usage

1. Generate a Random Grid

from isingmetro import grid_maker

grid = grid_maker(nrows=10, ncols=10)
print(grid)

2. Compute Energy

from isingmetro import compute_energy

energy = compute_energy(grid)
print("Energy:", energy)

3. Run the Ising Model Simulation

from isingmetro import ising_model

nsamples = 10000
temperature = 2.5
saved_energies, final_grid = ising_model(nsamples, temperature, grid)
print("Final Grid:", final_grid)

4. Compute Specific Heat

from isingmetro import specific_heat

temp_range = [1.0, 2.0, 3.0, 4.0]
Cv, updated_Cv = specific_heat(grid, temp_range)
print("Specific Heat:", Cv)

5. Compute Magnetization

from isingmetro import magnetize

magnetization = magnetize(grid, temp_range)
print("Magnetization:", magnetization)

Periodic Boundary Conditions (PBC) Implementation

The repository also provides functions to compute system properties using periodic boundary conditions. Example:

from isingmetro import compute_energy_PBC

energy_pbc = compute_energy_PBC(grid)
print("Energy with PBC:", energy_pbc)

Contributing

If you'd like to contribute to improving this repository, feel free to submit a pull request.

Credit

This code was written by Suvam Tripathy, a Masters' of Physics student(2023-2025) of Indian Institute of Technology, Madras, as a part of a mini-project.

License

This project is licensed under the MIT license.

About

Hey Physics Geeks! I have written this python code to simulate a 2D square and triangle lattice and find the associated properties with the lattice using statistical mechanics. Go ahead and try it out!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published