Exemplo n.º 1
0
def monte_carlo_ising( (L, T, num_sweeps) ):

    V = L*L
    beta = 1.0/T

    # generate random configuration
    sigma = random.randint(0, 2, (L, L))
    sigma *= 2
    sigma -= 1

    E  = compute_energy(sigma)
    mu = sigma.sum()

    Es = empty(num_sweeps, int)
    ms = empty(num_sweeps, int)

    mc.core(E, mu, Es, ms, num_sweeps, sigma, beta)

    Es = Es/float(V)
    ms = abs(ms)/float(V)
    Emean, _, Eerr, tauE, _, _, _, _ = compute_act_error(Es)
    mmean, _, merr, tauM, _, _, _, _ = compute_act_error(ms)
    print "\rT = {} tau_E = {} tau_M = {} E = {}+/-{} m = {}+/-{}"\
        .format(T, tauE, tauM, Emean, Eerr, mmean, merr)

    return Emean, Eerr, mmean, merr, sigma, Es, ms
Exemplo n.º 2
0
def exact_sum(L, Ts):
    # we compute the mean energy and magnetization for all
    # temperatures at once!
    ws = zeros_like(Ts)
    Es = zeros_like(Ts)
    ms = zeros_like(Ts)
    # beta is a NumPy array with len(Ts) elements
    beta = 1./Ts

    V = float(L*L)

    sigma = ones((L, L), dtype=int)
    # the bit pattern of the integer "state" is used to generate all
    # possible states of sigma
    for state in range(2**(L*L)):
        # read out the bitpattern
        for i in range(L):
            for j in range(L):
                k = i*L + j
                if state & 2**k > 0:
                    sigma[i,j] = 1
                else:
                    sigma[i,j] = -1

        # compute energy and magnetization of this state
        E = compute_energy(sigma)
        mu = compute_magnetization(sigma)

        # this is a vector operation, as beta is a vector
        w = exp(-beta*E)
        ws += w
        Es += E/V*w
        ms += abs(mu)/V*w

    Emeans = Es/ws
    mmeans = ms/ws
    return Emeans, mmeans