title: Ising Model in Python
body: <p>I'm currently working on writing code for the Ising Model using Python3. I'm still pretty new to coding. I have working code, but the output result is not as expected and I can't seem to find the error. Here is my code:</p>
  
  <pre><code>import numpy as np
  import random
  
  
  def init_spin_array(rows, cols):
      return np.random.choice((-1, 1), size=(rows, cols))
  
  
  def find_neighbors(spin_array, lattice, x, y):
      left = (x , y - 1)
      right = (x, y + 1 if y + 1 &lt; (lattice - 1) else 0)
      top = (x - 1, y)
      bottom = (x + 1 if x + 1 &lt; (lattice - 1) else 0, y)
  
      return [spin_array[left[0], left[1]],
              spin_array[right[0], right[1]],
              spin_array[top[0], top[1]],
              spin_array[bottom[0], bottom[1]]]
  
  def energy(spin_array, lattice, x ,y):
      return -1 * spin_array[x, y] * sum(find_neighbors(spin_array, lattice, x, y))
  
  
  def main():
      lattice = eval(input("Enter lattice size: "))
      temperature = eval(input("Enter the temperature: "))
      sweeps = eval(input("Enter the number of Monte Carlo Sweeps: "))
      spin_array = init_spin_array(lattice, lattice)
      print("Original System: \n", spin_array)
      # the Monte Carlo follows below
      for sweep in range(sweeps):
          for i in range(lattice):
              for j in range(lattice):
                  e = energy(spin_array, lattice, i, j)
                  if e &lt;= 0:
                  spin_array[i, j] *= -1
              elif np.exp(-1 * e/temperature) &gt; random.randint(0, 1):
                  spin_array[i, j] *= -1
              else:
                  continue
  print("Modified System: \n", spin_array)
  
  main()
  </code></pre>
  
  <p>I think the error is in the Monte Carlo Loop, but I am not sure. The system should be highly ordered at low temperatures and become disordered past the critical temperature of 2.27. In other words, the randomness of the system should increase as T approaches 2.27. For example, at T=.1, we should see large patches of spins that are aligned, i.e. patches of -1s and 1s. Past 2.27 the system should be disordered and we should not see these patches.</p>