Python Ising Model Plotting

时间:2017-04-10 03:00:52

标签: python plot

I should probably start of with saying I have no idea how to code and don't consider myself even a beginner when it comes to coding. That being said I would really appreciate some help with getting started with some code. As the title suggests I have to code what is known as the Ising model. The premise of the model is:

E= -Σ(hs(i)) - Σ(Js(i)*s(j))

this will follow what i believe is the Monte Carlo simulation. so for each configuration of {s(i)} there is a probability e^(-ßE{s(i)})

We start with a random spin to yield potential {s(i)}

If E(1)>E(0) we flip the sign

If E(1) < E(0), then you draw a random number and compare to e^(ß∆E)

if the number , say x is:

x< e^(ß∆E) then flip

x > e^(ß∆E) do nothing then {s(i)}={s(0)}

I hope that is enough info, but I did pickup some code which I think is relevant

import numpy as np
import random


def init_spin_array(rows, cols):
return np.ones((rows, cols))


def find_neighbors(spin_array, lattice, x, y):
left   = (x, y - 1)
right  = (x, (y + 1) % lattice)
top    = (x - 1, y)
bottom = ((x + 1) % lattice, y)

return [spin_array[left[0], left[1]],
    spin_array[right[0], right[1]],
    spin_array[top[0], top[1]],
    spin_array[bottom[0], bottom[1]]]


def energy(spin_array, lattice, x ,y):
return 2 * spin_array[x, y] * sum(find_neighbors(spin_array, lattice, x, y))


def main():
RELAX_SWEEPS = 50
lattice = eval(input("Enter lattice size: "))
sweeps = eval(input("Enter the number of Monte Carlo Sweeps: "))
for temperature in np.arange(0.1, 5.0, 0.1):
spin_array = init_spin_array(lattice, lattice)
# the Monte Carlo follows below
mag = np.zeros(sweeps + RELAX_SWEEPS)
for sweep in range(sweeps + RELAX_SWEEPS):
    for i in range(lattice):
        for j in range(lattice):
            e = energy(spin_array, lattice, i, j)
            if e <= 0:
                spin_array[i, j] *= -1
            elif np.exp((-1.0 * e)/temperature) > random.random():
                spin_array[i, j] *= -1
    mag[sweep] = abs(sum(sum(spin_array))) / (lattice ** 2)
print(temperature, sum(mag[RELAX_SWEEPS:]) / sweeps)

main()

All i need is to plot this info into a M vs T plot, and somehow change the code to allow three parameter h,J,T to be varied, as in if I hold T at a certain #, what is h vs. J plot look like. Please any help would be immensely appreciated.

0 个答案:

没有答案