Python Assignment #1 for LMC

import sys

sys.path.append("D:\\Lmc\\LmcPython")

import Lmc
dir(Lmc)

sim = Lmc.IsingSimulation()
magnetizationObserver = Lmc.MagnetizationObserver()
sim.Attach(magnetizationObserver)

for i in xrange(100):
	sim.Sweep()
	sim.Notify()
	
magnetizationObserver.Mbar()
magnetizationObserver.Susceptibility()

# Make sure your X-windows manager is running.
# Try out the new xmgr interface:

sys.path.append("K:\\Simulations\\xmgr") # Your path here
import xmgr
# Early in the semester we needed this voodoo to get xmgr to work
# File -> Open, K:\Simulations\xmgr\xmgr.py, then close it
graph = xmgr.xmgr()

import math

CosX = [[0.0,1.0]]
CosX.append([0.1,math.cos(0.1)])
CosX
for i in xrange(2,20):
    CosX.append([i/10.0, math.cos(i/10.0)])
CosX

graph.PlotSet(0, CosX)	# Cos X as plot 0
graph.PlotSet(1, [[0,0],[1,1]])	# Plot 1
graph.SetKill(0)		# Delete old sets before graphing new

# Now let's plot magnetization and susceptibility!
# Let's set up the three algorithms
# At high temperatures, the heat-bath algorithm is most efficient

S = sim.GetLattice()
heatBath = Lmc.SpinDynamics(S)
wolff = Lmc.WolffDynamics(S)
# Store current dynamics
BKL = sim.SwitchDynamics(heatBath)
sim.SetTemperature(10.0)

# Try it out, make sure it works:
magnetizationObserver.Reset()
sim.Sweep()
sim.Notify()

magnetizationObserver.Mbar()
sim.GetLattice().GetMagnetization()

# When we measure  and  at a new temperature, we first relax the
# system for a while, and then start measuring:

MagVsT = []
ChiVsT = []

def Measure(T, nRelax, nMeasure):
    sim.SetTemperature(T)
    magnetizationObserver.Reset()
    for i in xrange(nRelax):
    	sim.Sweep()
    for i in xrange(nMeasure):
       sim.Sweep()
       sim.Notify()
    MagVsT.append([T,magnetizationObserver.Mbar()])
    ChiVsT.append([T,magnetizationObserver.Susceptibility()])

# Does the Curie law hold?    
Measure(100.0,10,100)

MagVsT = []
ChiVsT = []

for T in xrange(10,5,-1):
    Measure(T, 10, 100)

MagVsT
ChiVsT
graph.PlotSet(0,ChiVsT)

# Switch to Wolff near critical point

sim.SwitchDynamics(wolff)

for fiveT in xrange(25,15,-1):
    Measure(fiveT/5.0,10,100)
    
for tenT in xrange(30,20,-1):
    Measure(tenT/10.0,10,100)

graph.SetKill(0)
graph.PlotSet(0,ChiVsT)
graph.PlotSet(1,MagVsT)

# At low temperatures, we want to see the magnetization
# Don't want to flip whole cluster over! Use BKL
# Put on field first to align spins

sim.SetTemperature(0.1)
sim.SwitchDynamics(BKL)
sim.SetMagneticField(10.0)
for i in xrange(10):
	sim.Sweep()

sim.GetLattice().GetMagnetization()
# Are all spins aligned?

sim.SetMagneticField(0.0)
sim.Sweep()
sim.GetLattice().GetMagnetization()

# if you get 9998, you're exiting after flipping in BKL

# Store cooling runs: start new heating arrays
MagVsTCool = MagVsT
MagVsT = []
ChiVsTCool = ChiVsT
ChiVsT = []

for fiveT in xrange(1,10):
	Measure(fiveT/5.0,10,100)
	
for tenT in xrange(20,30)
	Measure(tenT/10.0,10,100)

graph.PlotSet(2,MagVsT)
graph.PlotSet(3,ChiVsT)

# You'll need to zoom in to look at M(T) (magnifying glass)
# You may want to change some of the curve colors (double-click on curve)

# See if you can see these features:
# (1) The magnetization M(T) goes to zero as a power-law at Tc.
# (2) The susceptibility Chi(T) diverges as a power-law at Tc,
#     coming from above or below.
# (3) When the magnetization becomes non-zero in BKL,
#     it will typically stay zero in Wolff. Why?
# (4) When the BKL magnetization becomes non-zero, the
#     Wolff susceptibility goes crazy. Why?
# (5) The BKL susceptibility above Tc will often undershoot
#     the Wolff measurement, unless one cools very slowly. Why?

Statistical Mechanics: Entropy, Order Parameters, and Complexity, now available at Oxford University Press (USA, Europe).