#the main ising model. It takes the number of monte carlo steps, L=lattize size, J=interaction energy, T=temperature and mu=magnetix field as inputs
from numpy import *
import random
from energy import *
from delta_e import *
#from pylab import *
def ising(mcs,L,J,T,mu):
	#generates a lattice of size L*L
	S=ones((L,L),'float')
	#Calculates beta and temperature
	T=abs(T*J)
	beta=1/T
	#defines a matrix En
	En=zeros((mcs,2),'float')
	#main loop over number of monte carlo steps
	for m in range(mcs):
		#inside loop over number of sites
		for i in range(L**2):
			#randomly picks a site on the lattice
     			x=int(floor(random.random()*L))
     			y=int(floor(random.random()*L))
			t=zeros((2))
			t[0]=x
			t[1]=y
			#calculates energy difference
      			E_diff=delta_e(S,t,J,mu)
			#accept or reject a monte carlo move
     			if E_diff<0:
     				S[x,y]=-1*S[x,y]
			else:
				#calcualtes boltzmann's distribution
     				P=exp(-beta*E_diff)
				#picks A between 0 and 1
				A=random.random()
				if A<P:
					#accepts the move
					S[x,y]=-1*S[x,y]
		#writes the energy to an array
		En[m,:]=energy(S,J,mu)

	return [En, S]
