Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Simulating the 2D Ising Model with Metropolis Monte Carlo

Hello! Welcome to the first lesson in our module on computational methods.

Over the past eleven modules, we have built a substantial theoretical edifice for understanding phase transitions and critical phenomena. We've journeyed from classical thermodynamics through mean-field and Landau theories, all the way to the powerful but abstract frameworks of the renormalization group and scaling hypotheses. You've seen how these theories predict universal behaviors, described by critical exponents and scaling functions.

Today, we pivot from pure theory to computation. Our goal is to develop the primary tool used to test these theories, explore models without exact solutions (like the 3D Ising model), and gain a concrete, dynamic intuition for the phenomena we've been studying.

This lesson's learning outcome is to implement the Metropolis Monte Carlo algorithm to simulate the 2D Ising model. We will break down the "why" and the "how" of this foundational algorithm, which enables us to generate representative configurations of a system in thermal equilibrium. This will be a practical lesson that leverages your programming skills, providing a computational laboratory for the physics we've learned.

A quick note connecting to our previous topic: in the last module, we discussed dynamic critical phenomena, particularly "critical slowing down." The simple algorithm we will implement today is famously susceptible to this issue. While our focus today is just on getting the simulation working correctly, in the next lesson, we will see this slowing down in action and understand why more advanced algorithms are needed near a critical point.

1. The Challenge of Statistical Averages and the Idea of Importance Sampling

Recall that for a system described by a Hamiltonian , the thermal average of an observable is given by:

where the sum is over all possible configurations of the system, is the energy of a configuration, , and is the partition function.

The fundamental computational challenge is the size of this sum. For an Ising model, there are spins, leading to configurations. As noted in the paper by Walter and Barkema (LINK), even for a tiny 3D system, this is states—a number far beyond any possibility of direct enumeration.

The solution is not to sample every state, but to sample the important ones. The Boltzmann factor tells us that at any given temperature, configurations with low energy contribute exponentially more to the average than those with high energy. This leads to the strategy of importance sampling: we want to generate a sequence of configurations in such a way that the probability of a configuration appearing in our sequence is proportional to its Boltzmann weight.

2. Markov Chain Monte Carlo

A powerful method for achieving importance sampling is Markov Chain Monte Carlo (MCMC). The core idea is to construct a "random walk" through the space of all possible configurations. This walk is a Markov chain, meaning the next step in the walk only depends on the current configuration, not the history of how we got there.

To ensure this random walk correctly samples the Boltzmann distribution, the process must satisfy two key properties.

An introduction to Monte Carlo methods by Walter and Barkema

The following paper provides a concise introduction to the key principles of Monte Carlo simulations. It's a great starting point for understanding the theoretical underpinnings of our algorithm.

Please read Section 2, 'Principles of MC simulations: Ergodicity & detailed balance condition' (pages 3-4). Focus on understanding the definitions of ergodicity and detailed balance, and how the detailed balance condition (Eq. 4) ensures that the Markov chain will eventually sample configurations according to the desired stationary distribution (Eq. 5).

As the paper explains, the two crucial conditions are:

  1. Ergodicity: It must be possible to get from any configuration with a non-zero Boltzmann weight to any other such configuration in a finite number of steps. For our Ising model, where a "step" will involve flipping a single spin, this condition is met. You can transform any configuration into any other by flipping spins one by one.

  2. Detailed Balance: A sufficient (though not strictly necessary) condition for ensuring the chain converges to the correct equilibrium distribution is detailed balance. For any two configurations and , the rate of transitions from to must equal the rate of transitions from to in equilibrium:

    Here, is the transition probability of moving from state to . Since , our task is to design transition probabilities that satisfy this condition.

3. The Metropolis Algorithm

The Metropolis algorithm (or more generally, Metropolis-Hastings) is a brilliant and simple recipe for constructing transition probabilities that satisfy detailed balance. The transition is broken into two sub-steps:

  • Proposal: Propose a move from to a new trial configuration with probability .
  • Acceptance: Accept this proposed move with probability .

Thus, .

For the Ising model, the simplest approach is to use a symmetric proposal:

  1. Proposal: Select a single spin at random from the spins on the lattice and propose to flip it.
  2. The trial configuration is the same as , but with one spin flipped. The probability of proposing this specific move is . The probability of proposing the reverse move (starting in and flipping the same spin to get back to ) is also . So, .

With a symmetric proposal, the detailed balance condition simplifies considerably:

The genius of the Metropolis algorithm is its choice for the acceptance probability, which satisfies this ratio while maximizing the acceptance rate:

This rule has a clear physical interpretation:

  • If flipping the spin lowers the energy (), the move is always accepted (). This drives the system towards lower energy states.
  • If flipping the spin increases the energy (), the move is accepted with a probability . This is the crucial step that allows the system to escape from local energy minima and explore the full configuration space, simulating the effect of thermal fluctuations.

The following resource provides a very clear step-by-step guide to implementing this logic.

Simulating an Ising Model — Computing in Physics (498CMP)

This guide from the University of Illinois provides excellent, practical instructions for simulating the Ising model, covering the Metropolis algorithm and key implementation details.

Please read the following sections: 'The Metropolis-Hastings algorithm', 'Computing the Energy', and 'Writing MCMC'. These sections detail the algorithm, emphasize the critical importance of the efficient O(1) energy difference calculation, and lay out the structure of the simulation loop.

4. Implementation Details for the 2D Ising Model

Let's consolidate the information from the resources into a concrete plan for implementation. Your background in Python and C++ will make this straightforward.

The Algorithm Flow

The overall process follows a simple loop, which is nicely visualized in this flowchart:

Metropolis Monte Carlo Algorithm Flowchart
This flowchart shows the complete cycle of a single Metropolis step: selecting a spin, calculating the potential energy change, and accepting or rejecting the flip based on the Metropolis criterion.

Key Implementation Steps

  1. System Representation: An array (e.g., a NumPy array in Python) is perfect for storing the spin configuration, with values . Remember to handle periodic boundary conditions. For a spin at site , its neighbors are at and , where % is the modulo operator.

  2. Efficient Calculation: As highlighted in the Illinois resource, you should not recalculate the total energy of the system after every flip. This would be an operation. The change in energy only depends on the spin being flipped and its immediate neighbors. The Ising Hamiltonian is . The local energy contribution involving a spin is . If you flip , the new local energy is . The change in total energy is thus:

    This is an calculation, as it only involves a fixed number of neighbors (4 for a 2D square lattice).

  3. The Monte Carlo Step: A function that performs one step of the algorithm would look like this:

    • Choose a random lattice site .
    • Calculate for flipping the spin using the formula above.
    • Generate a uniform random number r between 0 and 1.
    • If r < exp(-β * ΔE), flip the spin: spins[i,j] *= -1. This single condition works for both positive and negative , since exp(-β * ΔE) will be greater than 1 if is negative.
  4. Sweeps, Equilibration, and Measurement:

    • Sweep: The natural unit of "time" in the simulation is one Monte Carlo sweep, which consists of attempted spin flips.
    • Equilibration: When you start a simulation (e.g., from a random or fully ordered state), the system is not in equilibrium. You must run the simulation for a certain number of "equilibration sweeps" to let it relax. You can monitor a macroscopic quantity like the total energy or magnetization to see when it has stabilized around an average value.
    • Measurement: After equilibration, you can begin collecting data. For example, after each sweep, you can calculate and record the total energy and total magnetization . After many measurement sweeps, you can average these recorded values to find and .
2D Ising Model Simulation: Initial State and Thermodynamic Evolution
This animation shows a simulation of a 256x256 Ising model. The plots on the right show the time evolution of energy, magnetization, specific heat, and susceptibility. Notice how the values change rapidly at the beginning (equilibration phase) before settling into fluctuations around a stable average.
Test your understanding!

Consider a single spin in a 2D ferromagnetic Ising model (). Its four nearest neighbors are . What is the energy change if this spin is flipped to ? Assuming and (so ), what is the probability this flip is accepted?

Show answer

First, we calculate the sum of the neighboring spins: .

The energy change is .
Plugging in the values: .

The acceptance probability is .
The move is always accepted. This makes sense: a spin surrounded by a field of zero has no energy preference for its orientation.

Conclusion

In this lesson, we have laid the groundwork for simulating statistical systems. You now have the complete conceptual and algorithmic recipe to implement the Metropolis Monte Carlo simulation for the 2D Ising model.

Key Takeaways:

  • Direct enumeration of states is computationally impossible for non-trivial systems.
  • Importance sampling is a strategy to preferentially sample configurations with high Boltzmann weights.
  • Markov Chain Monte Carlo (MCMC) methods, like the Metropolis algorithm, generate a sequence of states that accomplishes this.
  • The algorithm's validity rests on the principles of ergodicity and detailed balance.
  • The Metropolis algorithm uses a simple but powerful acceptance rule, , which allows the system to find thermal equilibrium.
  • For the Ising model, an efficient implementation requires an O(1) calculation of the local energy change .

Preview of the Next Lesson:

Now that you have the tools to build the simulation, the next step is to analyze its behavior. As we approach the critical temperature , you will find that the simulation takes longer and longer to equilibrate and for successive configurations to become statistically independent. This is critical slowing down—the direct computational consequence of the diverging correlation length and time we discussed theoretically. In the next lesson, we will observe this phenomenon in our simulation and quantify it, which will motivate the need for more advanced cluster algorithms.

Can't find a good explanation? Sign up and we'll make it for you

Sign up