7 Dec 23 - Activity: The Ising Model
$$E = -J\left(\vec{S}_i\cdot\vec{S}_j\right)$$
import numpy as np
import matplotlib.pyplot as plt
import random as randomModuleNotFoundError: No module named 'numpy'
[0;31m---------------------------------------------------------------------------[0m
[0;31mModuleNotFoundError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 2[0m line [0;36m1 [0;32m----> 1[0m [39mimport[39;00m [39mnumpy[39;00m [39mas[39;00m [39mnp[39;00m [1;32m 2[0m [39mimport[39;00m [39mmatplotlib[39;00m[39m.[39;00m[39mpyplot[39;00m [39mas[39;00m [39mplt[39;00m [1;32m 3[0m [39mimport[39;00m [39mrandom[39;00m [39mas[39;00m [39mrandom[39;00m
[0;31mModuleNotFoundError[0m: No module named 'numpy'
[0;31m---------------------------------------------------------------------------[0m
[0;31mModuleNotFoundError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 2[0m line [0;36m1 [0;32m----> 1[0m [39mimport[39;00m [39mnumpy[39;00m [39mas[39;00m [39mnp[39;00m [1;32m 2[0m [39mimport[39;00m [39mmatplotlib[39;00m[39m.[39;00m[39mpyplot[39;00m [39mas[39;00m [39mplt[39;00m [1;32m 3[0m [39mimport[39;00m [39mrandom[39;00m [39mas[39;00m [39mrandom[39;00m
[0;31mModuleNotFoundError[0m: No module named 'numpy'
cellLength = 20
simulationSteps = 1000000
couplingConstant = 1.0 ## J
temperature = 1.0
def calculateEnergy(spinArray):
'''Calculate all the pairwise energy interactions and sum them up
Do rows and columns separately and add them up.'''
rowNeighborInteractionEnergy = np.sum(spinArray[0:cellLength-1,:]*spinArray[1:cellLength,:])
columnNeighborInteractionEnergy = np.sum(spinArray[:,0:cellLength-1]*spinArray[:,1:cellLength])
totalInteractionEnergy = rowNeighborInteractionEnergy+columnNeighborInteractionEnergy
return -couplingConstant*totalInteractionEnergy
## Create an empty square array
spinArray = np.empty([cellLength,cellLength], int)
## Populate it with random spins
for row in range(cellLength):
for column in range(cellLength):
if random.random()<0.5:
spinArray[row,column] = +1
else:
spinArray[row,column] = -1
# Calculate the initial energy and magnetization
energyAtStep = calculateEnergy(spinArray)
magnetizationAtStep = np.sum(spinArray)
## Show the spin array
## Black is spin up and white is spin down
plt.figure(figsize=(8,8))
c = plt.pcolor(spinArray, cmap='Greys')
plt.axis('square')NameError: name 'np' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 3[0m line [0;36m1 [1;32m 15[0m [39mreturn[39;00m [39m-[39mcouplingConstant[39m*[39mtotalInteractionEnergy [1;32m 17[0m [39m## Create an empty square array[39;00m [0;32m---> 18[0m spinArray [39m=[39m np[39m.[39mempty([cellLength,cellLength], [39mint[39m) [1;32m 20[0m [39m## Populate it with random spins[39;00m [1;32m 21[0m [39mfor[39;00m row [39min[39;00m [39mrange[39m(cellLength):
[0;31mNameError[0m: name 'np' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 3[0m line [0;36m1 [1;32m 15[0m [39mreturn[39;00m [39m-[39mcouplingConstant[39m*[39mtotalInteractionEnergy [1;32m 17[0m [39m## Create an empty square array[39;00m [0;32m---> 18[0m spinArray [39m=[39m np[39m.[39mempty([cellLength,cellLength], [39mint[39m) [1;32m 20[0m [39m## Populate it with random spins[39;00m [1;32m 21[0m [39mfor[39;00m row [39min[39;00m [39mrange[39m(cellLength):
[0;31mNameError[0m: name 'np' is not defined
## Hold onto the values of the magnetization
## for each step in the simulation
magnetizationArray = np.zeros(simulationSteps)
## Monte Carlo Loop
for step in range(simulationSteps):
## Store the magnetization at this step
magnetizationArray[step] = magnetizationAtStep
## Store the energy before swapping the spin randomly
oldEnergy = energyAtStep
## Select a spin from the cell
ithSpin = random.randrange(cellLength)
jthSpin = random.randrange(cellLength)
## Flip the spin of that one site
spinArray[ithSpin,jthSpin] = -spinArray[ithSpin,jthSpin]
## Calculate the energy after that change
energyAtStep = calculateEnergy(spinArray)
deltaE = energyAtStep - oldEnergy
## If the change resulted in an increase in the total energy,
## evaluate whether to accept the value or not
if deltaE > 0.0:
probabilityOfFlip = np.exp(-deltaE/temperature)
## If the the random value is lower than the probability,
## reverse the change to the spin, and recalculate the energy
if random.random()>probabilityOfFlip:
spinArray[ithSpin,jthSpin] = -spinArray[ithSpin,jthSpin]
energyAtStep = oldEnergy
continue
magnetizationAtStep = np.sum(spinArray)NameError: name 'np' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 4[0m line [0;36m3 [1;32m 1[0m [39m## Hold onto the values of the magnetization [39;00m [1;32m 2[0m [39m## for each step in the simulation[39;00m [0;32m----> 3[0m magnetizationArray [39m=[39m np[39m.[39mzeros(simulationSteps) [1;32m 5[0m [39m## Monte Carlo Loop[39;00m [1;32m 6[0m [39mfor[39;00m step [39min[39;00m [39mrange[39m(simulationSteps): [1;32m 7[0m [1;32m 8[0m [39m## Store the magnetization at this step[39;00m
[0;31mNameError[0m: name 'np' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 4[0m line [0;36m3 [1;32m 1[0m [39m## Hold onto the values of the magnetization [39;00m [1;32m 2[0m [39m## for each step in the simulation[39;00m [0;32m----> 3[0m magnetizationArray [39m=[39m np[39m.[39mzeros(simulationSteps) [1;32m 5[0m [39m## Monte Carlo Loop[39;00m [1;32m 6[0m [39mfor[39;00m step [39min[39;00m [39mrange[39m(simulationSteps): [1;32m 7[0m [1;32m 8[0m [39m## Store the magnetization at this step[39;00m
[0;31mNameError[0m: name 'np' is not defined
plt.figure(figsize=(8,8));
c = plt.pcolor(spinArray, cmap='Greys');
plt.axis('square');NameError: name 'plt' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 5[0m line [0;36m1 [0;32m----> 1[0m plt[39m.[39mfigure(figsize[39m=[39m([39m8[39m,[39m8[39m)); [1;32m 2[0m c [39m=[39m plt[39m.[39mpcolor(spinArray, cmap[39m=[39m[39m'[39m[39mGreys[39m[39m'[39m); [1;32m 3[0m plt[39m.[39maxis([39m'[39m[39msquare[39m[39m'[39m);
[0;31mNameError[0m: name 'plt' is not defined
[0;31m---------------------------------------------------------------------------[0m
[0;31mNameError[0m Traceback (most recent call last)
[1;32m/Users/caballero/repos/teaching/phy415fall23/content/4_distributions/activity-ising_model.ipynb Cell 5[0m line [0;36m1 [0;32m----> 1[0m plt[39m.[39mfigure(figsize[39m=[39m([39m8[39m,[39m8[39m)); [1;32m 2[0m c [39m=[39m plt[39m.[39mpcolor(spinArray, cmap[39m=[39m[39m'[39m[39mGreys[39m[39m'[39m); [1;32m 3[0m plt[39m.[39maxis([39m'[39m[39msquare[39m[39m'[39m);
[0;31mNameError[0m: name 'plt' is not defined
plt.figure(figsize=(8,6))
plt.plot(magnetizationArray) plt.ylabel('Magnetization') plt.xlabel('Simulation Steps')