Skip to content

Monte Carlo simulation of the Ising Model using the Metropolis Algorithm

License

Notifications You must be signed in to change notification settings

davifeliciano/ising_animate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ising_animate

A Python Package to easily generate animations of the Ising Model using the Metropolis Algorithm, the most commonly used Markov Chain Monte Carlo method to calculate estimations for this system.

ising_2021-10-12_15-14-51

Installation

Be sure to have Python 3.6 or newer installed on your machine. Then you can simply use pip to install the package and its dependencies.

pip install ising_animate

Usage

Command Line Tool

This package can be used as a command line tool to generate the desired animations. For instance, the animation above was created using the command

python -m ising_animate

You can specify the desired size, initial state, temperature or field using some command line options. For example, the command

python -m ising_animate --size 256 --temp 1.5 --field 1.0 --init-state "down" --time-series

yields ising_2021-10-12_15-26-03

For a full description of all the available options, type in python -m ising_animate --help.

Import

When imported, there are four classes of objects that can be used to create custom animations:

  • Ising: just the core implementation of the Ising Model, no animation;
  • AnimatedIsing: an animation of the Ising Model with both temperature and external magnetic field held at fixed values;
  • CoolingAnimatedIsing: an animation of the Ising Model with the temperature droping (or raising) exponentially to a target value, at a given rate;
  • DynamicAnimatedIsing: an animation of the Ising Model with temperature and external magnetic field both described by given functions of time.

The usage of all of them are very similar. You just have to create an instance with the desired arguments...

from ising_animate import DynamicAnimatedIsing
from math import sin

frames = 100

dynamic = DynamicAnimatedIsing(
    shape=(256, 256),                # the shape of the lattice
    temp=lambda t: 1.0 + 0.3 * t,    # temperature as a function of time
    field=lambda t: sin(t),          # external magnetic field as a function of time
    time_series=True,                # plot evolution of physical quantities over time
    interval=100,                    # interval of each frame
    frames=frames,                   # amount of frames in the animation
)

and the animation itself is now given by a matplotlib FuncAnimation object stored at dynamic.animation. This object has a save method of which the most important arguments is the output filename and the fps of the animation. The fps can be chosen as you wish, but the natural value is 1000 (the amount of milliseconds in one second) divided by the interval of each frame. So, in that case, fps is 10.

dynamic.animation.save("outfile.gif", fps=10)

outfile Another useful feature of the save method is the possibility to pass a progress callback function that will be called after drawing each frame, with the count of already drawn frames and the total number of frames on the animation. This makes easy to use a package like progressbar2 to show the progress in a progress bar.

import progressbar

with progressbar.ProgressBar(max_value=frames) as bar:
    dynamic.animation.save(
        "outfile.gif",
        fps=10,
        progress_callback=lambda i, n: bar.update(i),
    )

To install progressbar2, use the following command in a terminal.

pip install progressbar2

To view a full description of each class, take a look at the full documentation.

Examples

There is a subpackage ising_animate.examples with some usage examples of usage of this library. The example above is one of them, and can be run using

python -m ising_animate.examples.dynamic

Another interesting one can be run with

python -m ising_animate.examples.heating

which yields two plots with the dependency of the mean energy and the specific heat density with the temperature. This example is a good illustration of the phase thansition that occurs at the temperature of 2.27, in energy units.

heating_2021-10-26_17-01