#calculates the energy of the ising model and returns the Energy and Composition/Magnetization
from numpy import *
def energy(S,J,mu):
	#ensures periodic boundary conditions. If a spin is on the top edge it will slice the top edge and put it at the bottom so in essence the spin will still have four neighbors and similarly for all 4 edges
	S_up= vstack((S[1:,:],S[0,:]))
	S_down= vstack((S[-1,:],S[0:-1,:]))
	S_right= c_[S[:,-1],S[:,0:-1]]
	S_left=c_[S[:,1:],S[:,0]]

#calcualtes the total spin by adding all the values of +1 and -1 on the lattice
	tot_spin=sum(sum(S))
#gets the size of the lattice
	N=size(S,1)**2
#Energy according to the hamiltonian of the ising model
	E=0.5*J*sum(sum(S*(S_up+S_down+S_left+S_right)))
#magnetization per spin
	C=(tot_spin/N)
	return E,C
