# Thermodynamics of the 2D Ising model

I want to study the thermodynamics of the 2D Nearest Neighbour Ising model (calculate the average energy, susceptibility, etc.). I have the Hamiltonian

$$\mathcal{H} = J\sum_{\langle i j \rangle} s_i s_j + h\sum_i s_i$$

where $$s_i \in \{-1, 1\}$$. I put these spins $$s_i$$ on a lattice of size $$n\times n$$. To calculate the partition function, I need to sum over all possible spin configurations (permutations) of the spins $$s_i$$.

However, I am not sure how I should go about summing over all possible permutations in Mathematica and implementing the 'nearest neighbour' interaction for a 2D grid.

• For the small models you'll be able to evaluate in Mathematica, it's important to think about (1) the lattice type (square, triangular, kagome, etc.) and the (2) boundary conditions (open, toric, helical, etc.). They have a huge influence on the results. It would probably help to make an explicit list of spins and a list of their connections (a connectivity graph). Sep 10, 2023 at 8:12

The energy can be calculated exactly as written in the formula using Sum and correct indices:

energy[s_] := J*Sum[(s[[i + 1, j]] + s[[i, j + 1]] +
s[[i - 1, j]] + s[[i, j - 1]])*s[[i,j]],
{i, 2, Length[s] - 1}, {j, 2, Length[s] - 1}] - h*Total[s, 2]


To calculate all possible spin configurations, you can use Tuples:

n = 3;
configs = Partition[#, n] & /@ Tuples[{0, 1}, {n^2}];


Note that the number of configurations is $$2^{n^2}$$, which grows extremely fast. You will not be able to enumerate them for $$n\gtrsim 5$$.

You can now calculate the energy of all configurations:

energy /@ configs
(* {0, -h, -h, -2 h, -h, -2 h, -2 h, ..., -8 h + 4 J, -9 h + 4 J} *)


Instead of pre-generating all possible configurations, you can generate them on-fly, as suggested by @Roman, for example with @Szabolcs' answer. This will not throw a memory allocation failure, but it will still take a lot of time to calculate:

Sum[energy[Partition[IntegerDigits[k, 2, n^2], n]], {k, 0, 2^(n^2) - 1}]

• I don't think it's a good idea to first generate a list of tuples and then sum over them, because such methods waste a lot of resources. RAM will be full and the CPU will be throttled by RAM access delays. Better to generate the list of configurations "on the fly" and add up the results right away. Mathematica is probably the wrong tool for this job, though. Sep 10, 2023 at 8:51
• @Roman, I absolutely agree. I tried to make the answer simple and verbose, since OP didn't really give many details (What is their target grid size? Numerical or exact results? Perhaps they want to do Monte Carlo?). Anyhow, I included a "lazy-list" way of generating configurations. Sep 10, 2023 at 9:33