# 2 # import scipy import DynamicLattice # class IsingModel: """Ising model class""" def __init__(self, N=10, T=2./scipy.log(1.+scipy.sqrt(2.)), H=0., seed=1): """ Call scipy.random.seed with argument seed; Set self.N to be N Build self.lattice to be random 50/50 0/1, using random_integers Call self.SetTemperatureField with T, H """ pass def SetTemperatureField(self, T, H): """ Sets self.privateTemperature, self.privateField # # After debugging, for faster performance, set up algorithm-dependent data structures to avoid taking exponentials for every flipped spin # Heat bath algorithm: Sets up self.heatBathProbUp to be array of doubles of length Z+1=5 (the number of different neighbors up (nUp) possible) Figure out spin energies eUp, eDown given nUp; figure out Boltzmann relative probabilities boltzUp, boltzDown; If T!= 0, set heatBathProbUp(nUp) to boltzUp/(boltzUp+boltzDown) otherwise set it to (0, 0.5, 1) if eUp is (positive, zero, negative). # Metropolis algorithm: Sets up self.MetropolisProbUp to be a 2x5 matrix of Floats (first index is current state of spin, second index is nUp) Iterate over nUp; If T != 0 If eDown > eUp, set probability to flip from down to up to one, up to down to 1-exp(-(eDown-eUp)/T) If eDown <= eUp, set probability to flip from up to down to one, down to up to 1-exp(-(eUp-eDown)/T) Otherwise (T=0) set appropriately # Wolff algorithm set p """ pass def GetTemperature(self): """ Returns self.privateTemperature """ pass def GetField(self): """ Returns self.privateField """ pass def NeighborsUp(self, i, j): """ Sums self.lattice at four neighbor sites, modulo self.N """ pass def SweepHeatBath(self, nTimes=1): """ Slow variant (for debugging): For each time in range(ntimes): For n in range(N): Pick a random spin (i,j) Find NeighborsUp(i,j) Find the probability heatBathProbUp that the spin will be up Create a random number r in (0,1] if rand < heatBathProbUp, set spin lattice[i][j]=1 # Fast variant: For each time in range(ntimes): Creates N random (i,j) pairs Creates N random numbers in (0,1] (note: use 1-scipy.random.random() to avoid zero) if rand < heatBathProbUp for NeighborsUp(i,j) set spin lattice[i][j]=1 else set it to zero """ pass def SweepMetropolis(self, nTimes=1): """ For each time in range(ntimes): Creates N random (i,j) pairs Creates N random numbers in (0,1] if rand < MetropolisProbUp for current spin, NeighborsUp(i,j) set spin lattice[i][j]=1 else set it to zero """ pass def WolffMoveRecursive(self): """ Slow, recursive variant of Wolff move # Pick a random spin; remember its direction Flip it Call FlipNeighbors """ pass def FlipNeighbors(self, i, j, oldSpin): """ Used by WolffMoveRecursive # Initialize spinsFlipped to zero For m, n in neighbors of i, j: if lattice[m][n]==oldSpin and random()

def runHeatBath(N=100, nSweepsPerShow=1, nShow=500): """ Set up ising as an IsingModel Set up dl as an NxN DynamicLattice (dl = DynamicLattice.DynamicLattice(N,N)) for t in range nShow, display lattice with dl.display(ising.Lattice) and then sweep with ising.SweepHeatBath(nSweepsPerShow) """ pass # def runMetropolis(N=100, nSweepsPerShow=1, nShow=500): """ Same as runHeatBath except use SweepMetropolis """ pass # def runWolff(N=100, nSweeps=500): """ Same as runHeatBath except initialize partialSweep=0, and use SweepWolff(partialSweep=partialSweep) """ pass # # Copyright (C) Cornell University # All rights reserved. # Apache License, Version 2.0