The Ising Model

A one-dimensional Ising model.  See Thermal Physics, by Daniel V. Schroeder, pg. 349.

Clear["Global`*"]

initialize[L_]:= Table[ RandomChoice[{-1, 1}],{ x, 1, 1}, {y, 1, L}];

L = 100;Nsteps =20* L * L; (* Guess for how many iterations to run *)
s = initialize[L];

ΔU[i_] := Module[{left, right},
left = If[i >1, i-1, L];
right = If[i < L, i+1, 1];
(* Energy of flipping spin i *)
2* s[[1, i]] * (s[[1, left]] + s[[1, right]])
]
e[s_] := -Sum[s[[1, i]] s[[1, i+1]], {i, 1, L-1}] (* Total energy *)
etheory[T_] := -L Tanh[1/T]

newrun[T_, Nsteps_] := Module[{},
initialize[L];
run[T, Nsteps] // Quiet
]
run[T_, Nsteps_]:= Module[{},
Do[i = RandomInteger[{1, L}];
Ediff = ΔU[i];
If[Ediff ≤ 0,
s[[1, i]] = - s[[1, i]];,
If[RandomReal[1.0] < Exp[-Ediff/T],
s[[1, i]] = -s[[1, i]];
]
];, {n, 1, Nsteps}];
e[s]
]
show[s_, T_] := MatrixPlot[s, AspectRatio -> 1/10,MaxPlotPoints -> Infinity,  PlotLabel -> T, ImageSize -> Large]
showSmall[s_] := MatrixPlot[s, AspectRatio -> 1/10, MaxPlotPoints -> Infinity, Axes -> False, ImageSize -> Large, Frame -> False]

Dynamic[show[s, T]]

First, just show some relaxation at low temperature.  Then Look at high temperature.

s = initialize[L];

run[1.49, 100000] (* run[T, Nsteps].  Return value is the energy. *)

Will it ever settle down to a fully-magnetized state?

out = Table[{T, newrun[T,Nsteps], showSmall[s]}, {T, 0.1, 2, 0.1}];
EvsT = out[[All, {1, 2}]];
TableForm[out, TableHeadings -> {None, {"T", "Energy", "State"}}]
ListPlot[EvsT, LabelStyle->Larger, AxesLabel->{"T", "Total Energy"}, ImageSize -> Scaled[0.8]]

What is the equilibrium at T = 0.5?  Do we need to run even longer?  You can try it, but it’s difficult to know when everything has converged!  Instead, do an analytic calculation for the expected energy.  (See Eq. 8.44 in Schroeder.)  It undergoes a gradual continuous transition down to fully magnetized.

{Tmin, Tmax} = MinMax[EvsT[[All, 1]]];
Show[{
Plot[etheory[T], {T, Tmin, Tmax}, PlotStyle -> Red], ListPlot[EvsT]
}, LabelStyle->Larger, AxesLabel -> {"T", "E"}, ImageSize -> Large]