Skip to main content

A module to easily generate animations of the Ising Model

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ising_animate-0.0.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ising_animate-0.0.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file ising_animate-0.0.2.tar.gz.

File metadata

  • Download URL: ising_animate-0.0.2.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for ising_animate-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d1afd2fcac1349f6a68aba997be80e359c98235246fbf5905f879af297b7b1cb
MD5 874ed7906b6c81bba998dd2ec340a98c
BLAKE2b-256 f0dd41bce34bf2d89efa146d49a0c522e3828f11c39b30962603a6388664ff6d

See more details on using hashes here.

File details

Details for the file ising_animate-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: ising_animate-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for ising_animate-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 646dbc6a086cadc148ec864b4f6d84d16baaaedf7bacd9bc2e728e3720f4e5f0
MD5 5a94334d4237554ab7a108085c18cc60
BLAKE2b-256 1e1a2cf96349f873a5839e99bcaa0a716c841b7ab3949245a804c1ba7f92a4d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page