Exemple #1
0
def Training_Data(size, temp, K, length, number, type='1d'):
    if type == '1d':
        data = np.zeros((number, size))
    if type == '2d':
        data = np.zeros((number, size**2))
    if type == '3d':
        data = np.zeros((number, size**3))

    for num in range(number):
        Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
        for i in range(length):
            Initial.metropolis()
        if type == '1d':
            spins = np.reshape(Initial.get_spins(), size)
            for i in range(size):
                if (spins[i] == -1.0):
                    spins[i] = 0
        elif type == '2d':
            spins = np.reshape(Initial.get_spins(), size**2)
            for i in range(size**2):
                if (spins[i] == -1.0):
                    spins[i] = 0
        elif type == '3d':
            spins = np.reshape(Initial.get_spins(), size**3)
            for i in range(size**3):
                if (spins[i] == -1.0):
                    spins[i] = 0
        data[num, :] = spins[:]

    #print(data)
    return data
Exemple #2
0
def Converged_SHeat_Susept(size, temp, K, length, samples, type='1d'):

    Initial = lattice.Initialise_Random_State(size, temp, K, type=type)

    mean_M = 0.0
    mean_M_squared = 0.0
    mean_E = 0.0
    mean_E_squared = 0.0
    const = 0

    if temp == 0:
        temp = 0.001
    for i in range(length + samples):
        Initial.metropolis()

        if i > length:
            m = Initial.magnetisation()
            en = Initial.energy()
            mean_M += m
            mean_M_squared += m**2
            mean_E += en
            mean_E_squared += en**2

    mean_M = np.abs(mean_M / samples)
    mean_M_squared = mean_M_squared / samples

    susept = np.abs(mean_M_squared - mean_M**2) / (temp)

    mean_E = mean_E / samples
    mean_E_squared = mean_E_squared / samples

    specific_heat = np.abs(mean_E_squared - mean_E**2) / (temp**2)

    return Initial, specific_heat, susept
Exemple #3
0
def Energy_Magnetization(size, temp, K, length, type='1d', show=True):
    num = []
    final_E = []
    final_M = []

    Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
    for i in range(length):
        Initial.metropolis()
        num.append(i)
        final_E.append(Initial.energy())
        final_M.append(np.abs(Initial.magnetisation()))

    if show == True:
        fig, ax = plt.subplots()
        plt.plot(num, final_E, marker='.', label='Energy')
        plt.legend()
        plt.xlabel('Iterations of Metropolis Algorithm')
        plt.ylabel('Average Energy per cell')

        fig, ax = plt.subplots()
        plt.plot(num, final_M, marker='.', label='Mag')
        plt.legend()
        plt.xlabel('Iterations of Metropolis Algorithm')
        plt.ylabel('Average Magnetisation per cell')

        plt.show()
Exemple #4
0
def Test_Generated(data, size, temp, K, type=type, autocorrelation=1):
    (num_samples, num_sites) = data.shape
    num_samples = int(num_samples / autocorrelation)
    mean_M = 0
    mean_E = 0
    mean_M_squared = 0
    mean_E_squared = 0

    # Should only take one in every few.. correlations between adjacent values = ?
    for i in range(num_samples * autocorrelation):
        if (i % autocorrelation == 0):
            Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
            #print(size,data[i].shape)
            Initial.set_spins(data[i])
            m = np.abs(Initial.magnetisation())
            en = Initial.energy()
            mean_M += m
            mean_M_squared += m**2
            mean_E += en
            mean_E_squared += en**2

    mean_M = mean_M / float(num_samples)
    mean_M_squared = mean_M_squared / float(num_samples)

    susept = np.abs(mean_M_squared - mean_M**2) / (temp)

    mean_E = mean_E / float(num_samples - 1)
    mean_E_squared = mean_E_squared / float(num_samples - 1)

    specific_heat = np.abs(mean_E_squared - mean_E**2) / (temp**2)

    return mean_M, mean_E, susept, specific_heat
Exemple #5
0
def Converged_E_M(size, temp, K, length, samples, type='1d'):
    final_E = []
    final_M = []
    if temp == 0:
        temp = 0.00001
    Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
    for i in range(length):
        Initial.metropolis()
    for sample in range(samples):
        for i in range(length):
            Initial.metropolis()
        final_E.append(Initial.energy())
        final_M.append(np.abs(Initial.magnetisation()))

    E = np.sum(final_E) / samples
    M = np.sum(final_M) / samples

    return Initial, E, M
Exemple #6
0
def SHeat_Susept(size,
                 min_temp,
                 max_temp,
                 number_temps,
                 length,
                 samples,
                 K,
                 type='1d',
                 plot=True,
                 analytic_compare=False):
    final_T = []
    final_M = []
    final_E = []
    final_X = []
    final_SH = []
    if min_temp == 0:
        min_temp = 0.0001
    temperatures = np.linspace(min_temp, max_temp, number_temps)
    for temp in temperatures:

        mean_M = 0.0
        mean_M_squared = 0.0
        mean_E = 0.0
        mean_E_squared = 0.0
        const = 0

        Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
        for i in range(length + samples):
            Initial.metropolis()

            if i > length:
                for n in range(length):
                    Initial.metropolis()
                m = Initial.magnetisation()
                en = Initial.energy()
                mean_M += np.abs(m)
                mean_M_squared += m**2
                mean_E += en
                mean_E_squared += en**2

        mean_M = mean_M / float(samples)
        mean_M_squared = mean_M_squared / float(samples)

        susept = np.abs(mean_M_squared - mean_M**2) / (temp)

        mean_E = mean_E / float(samples)
        mean_E_squared = mean_E_squared / float(samples)

        specific_heat = np.abs(mean_E_squared - mean_E**2) / (temp**2)

        final_T.append(temp)
        final_M.append(mean_M)
        final_E.append(mean_E)
        final_X.append(susept)
        final_SH.append(specific_heat)
        print(temp)
    '''deriving Curie temp from M and E'''

    smoothed_e = gaussian_filter(final_E, 3.)
    smoothed_m = gaussian_filter(final_M, 3.)

    dx = np.diff(final_T)
    dy_e = np.diff(smoothed_e)
    dy_m = np.diff(smoothed_m)

    slope_e = (dy_e / dx)
    slope_m = (dy_m / dx)

    max_slope_e = (max(slope_e))
    min_slope_m = (min(slope_m))

    Curie_e = 0.0
    Curie_m = 0.0

    for i in range(len(final_T) - 1):
        if slope_e[i] == max_slope_e:
            Curie_e = final_T[i]
        if slope_m[i] == min_slope_m:
            Curie_m = final_T[i]

    print('Curie temperature from energy is', Curie_e)
    print('Curie temperature from magnetisation is', Curie_m)
    '''saving data and graphing'''

    #mag_data = np.concatenate((np.array([final_T]).T,np.array([final_M]).T),axis=1)
    #np.savetxt("magnetization3.csv",mag_data)

    if plot == True:

        def analytic_energy(temp):
            return -K * np.tanh(temp**(-1) * K)

        def analytic_Cv(temp):
            return spc.k * (temp**(-1) * K)**2 * (np.cosh(
                temp**(-1) * K))**(-2)

        fig, ax = plt.subplots()
        plt.plot(final_T, final_E, '.', label='Metropolis')
        plt.plot(np.array(final_T),
                 analytic_energy(np.array(final_T)),
                 '.',
                 label='Exact')
        plt.title('Energy')
        plt.legend()
        plt.xlabel('Fundamental Temperature')
        plt.ylabel('Average Energy per cell after Convergence')

        fig, ax = plt.subplots()
        plt.plot(final_T, final_M, '.', label='Mag-Temp')
        plt.legend()
        plt.xlabel('Fundamental Temperature')
        plt.ylabel('Average Magnetisation per cell after Convergence')

        fig, ax = plt.subplots()
        plt.plot(final_T, final_X, '.', label='Suseptibility')
        plt.legend()
        plt.xlabel('Fundamental Temperature')
        plt.ylabel('Suseptibility')

        fig, ax = plt.subplots()
        plt.plot(final_T, final_SH, '.', label='Metropolis')
        plt.plot(np.array(final_T),
                 analytic_Cv(np.array(final_T)),
                 '.',
                 label='Exact')
        plt.title('Specific Heat')
        plt.legend()
        plt.xlabel('Fundamental Temperature')
        plt.ylabel('Specific Heat')

        plt.show()

    return final_T, final_E, final_M, final_X, final_SH
Exemple #7
0
def Energy_Magnetization_Temp(size,
                              min_temp,
                              max_temp,
                              number_temps,
                              K,
                              length,
                              type='1d'):
    final_T = []
    final_E = []
    final_M = []

    temperatures = np.log(
        np.linspace(np.exp(min_temp), np.exp(max_temp), number_temps))

    for temp in temperatures:
        n_trials = 10
        energies = np.zeros(n_trials)
        mags = np.zeros(n_trials)
        for n in range(n_trials):
            if temp == 0:
                temp = 0.0001
            Initial = lattice.Initialise_Random_State(size, temp, K, type=type)
            for i in range(length):
                Initial.metropolis()
            energies[n] = Initial.energy()
            mags[n] = np.abs(Initial.magnetisation())

        final_T.append(temp)
        final_E.append(np.median(energies))
        final_M.append(np.median(mags))
        print(temp)

    smoothed_e = gaussian_filter(final_E, 3.)
    smoothed_m = gaussian_filter(final_M, 3.)

    dx = np.diff(final_T)
    dy_e = np.diff(smoothed_e)
    dy_m = np.diff(smoothed_m)

    slope_e = (dy_e / dx)
    slope_m = (dy_m / dx)

    max_slope_e = (max(slope_e))
    min_slope_m = (min(slope_m))

    Curie_e = 0.0
    Curie_m = 0.0

    for i in range(len(final_T) - 1):
        if slope_e[i] == max_slope_e:
            Curie_e = final_T[i]
        if slope_m[i] == min_slope_m:
            Curie_m = final_T[i]

    print('Curie temperature from energy is', Curie_e)
    print('Curie temperature from magnetisation is', Curie_m)

    mag_data = np.concatenate((np.array([final_T]).T, np.array([final_M]).T),
                              axis=1)
    np.savetxt("magnetization3.csv", mag_data)

    fig, ax = plt.subplots()
    plt.plot(final_T, final_E, '.', label='Energy-Temp')
    plt.legend()
    plt.xlabel('Fundamental Temperature')
    plt.ylabel('Average Energy per cell after Convergence')

    fig, ax = plt.subplots()
    plt.plot(final_T, final_M, '.', label='Mag-Temp')
    plt.legend()
    plt.xlabel('Fundamental Temperature')
    plt.ylabel('Average Magnetisation per cell after Convergence')

    plt.show()
Exemple #8
0
def Autocorrelations_in_generated(size,
                                  K,
                                  max_binsize,
                                  min_samples,
                                  temp,
                                  type='1d',
                                  plot=True,
                                  epochs=100,
                                  stacked=False,
                                  k=1):
    ratio_hidden = 0.5

    gen_data = []
    if type == '1d':
        vis = size
        hid = int(vis * ratio_hidden)
    if type == '2d':
        vis = size**2
    if type == '3d':
        vis = size**3
    hid = int(vis * ratio_hidden)
    if stacked == True:
        second_hid = int(hid * ratio_hidden)
        r = rbm.Stacked_RBM(num_visible=vis,
                            num_first_hidden=hid,
                            num_second_hidden=second_hid)
    else:
        r = rbm.RBM(num_visible=vis, num_hidden=hid)

    training_data_sample = np.loadtxt('training_data_temp%s.txt' % temp)
    r.train(training_data_sample,
            epochs=int(epochs),
            learning_rate=0.1,
            batch_size=100,
            k=k)

    number_gen = max_binsize * min_samples
    gen_data = r.daydream(number_gen, training_data_sample[0])

    binsizes = np.arange(1, max_binsize + 1)

    magnetisations = []
    standard_dev_magnetisations = []

    for binsize_index in range(max_binsize):
        samples = int(number_gen / binsizes[binsize_index])
        bin_magnetizations = np.zeros((samples))

        for sample in range(samples):
            for i in range(binsizes[binsize_index]):
                Ising_lattice = lattice.Initialise_Random_State(size,
                                                                temp,
                                                                K,
                                                                type=type)
                Ising_lattice.set_spins(
                    gen_data[sample * binsizes[binsize_index] + i])
                bin_magnetizations[sample] += np.abs(
                    Ising_lattice.magnetisation())
            bin_magnetizations[
                sample] = bin_magnetizations[sample] / binsizes[binsize_index]

        mean = np.mean(bin_magnetizations)
        magnetisations.append(mean)
        difference_from_mean = np.sum(np.square(bin_magnetizations - mean))
        st_dev = (difference_from_mean / (samples * (samples - 1)))**0.5
        standard_dev_magnetisations.append(st_dev)

    fig, ax = plt.subplots()
    ax.errorbar(binsizes,
                magnetisations,
                fmt='-o',
                yerr=standard_dev_magnetisations)
    plt.title('Determining Autocorrelations')
    plt.xlabel('Binsizes')
    plt.ylabel('Magnetization')

    plt.show()