Skip to main content
  1. Posts/

Ising model

2 mins·
Numerical Analysis Statistical Mechanics Stochastics Simulation
Table of Contents

The Metropolis-Hastings algorithm for simulating the Lenz-Ising model is an excellent example of a Markov chain, since the former is actually a version of a Markov chain Monte Carlo (MCMC) simulation. I present examples based on cartesian and isometric grids.

It follows the Hamiltonian

$$ H(\sigma)=-\sum_{{i,j}}J_{ij}\sigma_i\sigma_j-\mu\sum_jh_j\sigma_j\ , $$

Since the Hamiltonian depends strictly on nearest-neighbours interactions, it can be used as a model for opinion/voter dynamics.

For this example, we consider a warping grid of size \(N\), where the \(N+1\)th index is congruent to the first index \(\text{mod } N\). All these fancy words state that the grid behaves as when Pac-Man portals to the opposite side of the screen. This is done in the vertical direction as well, as stated by the expressions mod(k,n) + 1, ...) (the + 1s are there just due to Julia’s indexing convention.)

The following function finds the Hamiltonian (or energy) for a matrix element \(M_{ij}\) with the interaction between its neighbours, as described by the expression above. It also accounts for external interaction of a magnetic field.

function hamiltonian(selected_spin, space, interaction, external, locs)

neighbour_spin        = view(space, fill(selected_spin,length(locs)) + locs)
neighbour_interaction = view(interaction,fill(selected_spin,length(locs)) + locs)
H = (-dot(neighbour_interaction, neighbour_spin) * view(space, selected_spin))[1]

H -= sum(view(external, neighbour_spin))
return H
end

If the resulting configuration is preferable (i.e. it has a lower energy), then the system accepts this new state. This allows us to only compute the change in energy at the desired index, instead of the whole array.

Isometric model. #

The same Hamilising_wb_lossy_singletonian can also be extended to a hexagonal or triangular grid. If we think of the isometric grid as being a cartesian grid transformed by some operator \(\mathbf{f}\) (as seen below), we realize that the only change to our energy function is the addition of two diagonal indices.

ising iso 1
ising iso 2

ising

The overall effect over large iterations (order of \(10^7\)) is the appearance ofpronounced diagonal patterns (Keep in mind that there is some bias due to the heatmap being cartesian in nature.)

ising iso sl1
ising iso sl2
ising iso sl3