Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative Python multiprocessing implementation #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def T_anneal(T, ii, num_steps, num_burnin):

return float(T_a)


def B_anneal(B, ii, num_steps, num_burnin):

#implement annealing code here
Expand Down
33 changes: 21 additions & 12 deletions python/ising.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
except:
from tqdm import tqdm

def run_ising(N,T,num_steps,num_burnin,flip_prop,J,B,disable_tqdm=False):

def run_ising(N, T, num_steps, num_burnin, flip_prop, J, B,
disable_tqdm=False):

# Description of parameters:
# N = Grid Size
Expand All @@ -22,15 +24,15 @@ def run_ising(N,T,num_steps,num_burnin,flip_prop,J,B,disable_tqdm=False):
# flip_prop = Total ratio of spins to possibly flip per step

# Initialize variables
M,E = 0,0 # Magnetization and Energy Initial Values
Msamp, Esamp = [],[] #Arrays to hold magnetization and energy values
M, E = 0, 0 # Magnetization and Energy Initial Values
Msamp, Esamp = [], [] #Arrays to hold magnetization and energy values

# We obtain the sum of nearest neighbors by convoluting
# this matrix with the spin matrix
conv_mat = np.matrix('0 1 0; 1 0 1; 0 1 0')

# Generate a random initial configuration
spin = np.random.choice([-1,1],(N,N))
spin = np.random.choice([-1, 1], (N, N))

try:
__IPYTHON__
Expand All @@ -51,34 +53,41 @@ def run_ising(N,T,num_steps,num_burnin,flip_prop,J,B,disable_tqdm=False):
pass
else:
steps.set_description("Working on T = %.2f" % T)
steps.refresh() # to show immediately the update
steps.refresh() # to show immediately the update

#implement annealing in annealing.py file
T_step = T_anneal(T, step, num_steps, num_burnin)
B_step = B_anneal(B, step, num_steps, num_burnin)

#Calculating the total spin of neighbouring cells
neighbors = signal.convolve2d(spin,conv_mat,mode='same',boundary='wrap')
neighbors = signal.convolve2d(
spin, conv_mat, mode='same', boundary='wrap')

#Sum up our variables of interest, normalize by N^2
M = float(np.sum(spin))/float(N**2)
M = float(np.sum(spin)) / float(N**2)
Msamp.append(M)

#Divide by two because of double counting
E = float(-J*(np.sum((spin*neighbors)))/2.0)/float(N**2) - float(B_step)*M
E = float(-J * (np.sum(
(spin * neighbors))) / 2.0) / float(N**2) - float(B_step) * M
Esamp.append(E)

#Calculate the change in energy of flipping a spin
DeltaE = 2.0 * (J*(spin*neighbors) + float(B_step)*spin)
DeltaE = 2.0 * (J * (spin * neighbors) + float(B_step) * spin)

#Calculate the transition
p_trans = np.where(DeltaE >= 0.0, np.exp(-1.0*DeltaE/float(T_step)),1.0)
p_trans = np.where(DeltaE >= 0.0,
np.exp(-1.0 * DeltaE / float(T_step)), 1.0)
#If DeltaE is positive, calculate the Boltzman flipping probability.
#If not, assign a transition probability of 1

#Decide which transitions will occur
transitions = [[-1 if (cell>random.random() and flip_prop>random.random()) else 1 for cell in row] for row in p_trans]
transitions = [[
-1
if (cell > random.random() and flip_prop > random.random()) else 1
for cell in row
] for row in p_trans]
#Perform the transitions
spin = spin*transitions
spin = spin * transitions

return Msamp, Esamp, spin
Loading