Example #1
0
def run_simulation(index,temp,n,num_steps,num_burnin,num_analysis,flip_prop,j,b,data_filename,corr_filename,data_listener,corr_listener):
    print("Working on Temp {0}".format(round(temp,4)))
    try:
        #run the Ising model
        lattice = IsingLattice(n, flip_prop)
        time_start = time.time()
        Msamp, Esamp = run_ising(lattice,temp,num_steps,num_burnin,j,b,disable_tqdm=True)

        try:
            #calculate statistical values
            M_mean = np.average(Msamp[-num_analysis:])
            E_mean = np.average(Esamp[-num_analysis:])
            M_std = np.std(Msamp[-num_analysis:])
            E_std = np.std(Esamp[-num_analysis:])

            #average over every 1000 datapoints, within [-num_analysis:]
            M_analysis = Msamp[-num_analysis:]
            shuffle(M_analysis)
            M_std_1000 = np.std(np.array(M_analysis).reshape(-1, 1000), axis=1) 

            Chi_1000 = M_std_1000**2/temp
            Chi_mean = np.average(M_std**2/temp)
            Chi_mean2 = np.average(Chi_1000)
            Chi_std = np.std(Chi_1000) 
            
            E_analysis = Esamp[-num_analysis:]
            shuffle(E_analysis)
            E_std_1000 = np.std(np.array(E_analysis).reshape(-1, 1000), axis=1)
            Cv_1000 = 1/temp**2*E_std_1000**2
            Cv_mean = 1/temp**2*E_std**2
            Cv_mean2 = np.average(Cv_1000)
            Cv_std = np.std(Cv_1000)
            data_array = [np.abs(M_mean),M_std,E_mean,E_std, Chi_mean, Chi_std, Cv_mean, Cv_std, Chi_mean2, Cv_mean2]
            
            data_listener.put([temp]+data_array)

            corr = lattice.calc_auto_correlation()
            lattice.free_memory()
            [corr_listener.put([temp]+corr_value) for corr_value in corr]

            print("Done with Temp {0} in {1} seconds".format(round(temp,4), round(time.time()-time_start,2)))
            return True

        except:
            logging.error("Temp="+str(temp)+": Statistical Calculation Failed. No Data Written.")
            return False

    except KeyboardInterrupt:
        print("\n\nProgram Terminated. Good Bye!")
        data_listener.put('kill')
        corr_listener.put('kill')
        sys.exit()

    except:
        logging.error("Temp="+str(temp)+": Simulation Failed. No Data Written")
Example #2
0
def run_simulation(t_min, t_max, t_step, n, num_steps, num_analysis,
                   num_burnin, j, b, flip_prop, output, savespins, t0, b0):

    time_start = time.time()

    check_step_values(num_steps, num_analysis, num_burnin)

    lattice = IsingLattice(n, flip_prop)
    # lattice.print_spins()

    T = get_temp_array(t_min, t_max, t_step)

    data_filename, corr_filename, iter_filename = get_filenames(output)

    spin_save_dir = os.path.dirname(data_filename) if savespins else None

    write_sim_parameters(data_filename, corr_filename, n, num_steps,
                         num_analysis, num_burnin, j, b, flip_prop)

    print('\nSimulation Started! Data will be written to ' + data_filename +
          '\n')

    # temp_range = tqdm(T) #set fancy progress bar range
    for index, temp in enumerate(T):

        print('Working on temp %d: %f' % (index, temp))

        # Run the Ising model
        Msamp, Esamp, Tstep, Bstep = run_ising(lattice, temp, num_steps,
                                               num_burnin, j, b, t0, b0)

        # Get and save statistical values
        calculate_and_save_values(lattice, Msamp, Esamp, Tstep, Bstep,
                                  num_analysis, index, temp, data_filename,
                                  corr_filename, iter_filename, spin_save_dir)

        # except KeyboardInterrupt:
        #     print("\n\nProgram Terminated. Good Bye!")
        #     sys.exit()
        #
        # except:
        #     logging.error("Temp="+str(temp)+": Simulation Failed. No Data Written")

    print('\n ------ Time start(%f) Time finished(%f), total time(%f)' %
          (time_start, time.time(), time.time() - time_start))
    print('\n\nSimulation Finished! Data written to ' + data_filename)

    lattice.free_memory()
Example #3
0
def run_simulation(temp,
                   n,
                   num_steps,
                   num_burnin,
                   num_analysis,
                   flip_prop,
                   j,
                   b,
                   t0=-1,
                   b0=-1):

    try:
        #run the Ising model

        lattice = IsingLattice(n, flip_prop)

        Msamp, Esamp = run_ising(lattice, temp, num_steps, num_burnin, j, b,
                                 t0, b0)

        # calculate statistical values
        M_mean = np.average(Msamp[-num_analysis:])
        E_mean = np.average(Esamp[-num_analysis:])
        M_std = np.std(Msamp[-num_analysis:])
        E_std = np.std(Esamp[-num_analysis:])
        cv = (1 / (temp * temp)) * (E_std**2)
        chi = (1 / temp) * (M_std**2)

        data_array = [np.abs(M_mean), M_std, E_mean, E_std, cv, chi]

        corr = lattice.calc_auto_correlation()
        lattice.free_memory()

        return [temp, data_array, corr]

    except KeyboardInterrupt:
        print("\n\nProgram Terminated. Good Bye!")
        sys.exit()
Example #4
0
def run_simulation(index, temp, n, num_steps, num_burnin, num_analysis,
                   flip_prop, j, b, data_filename, corr_filename,
                   traj_filename, data_listener, corr_listener, traj_listener,
                   plots, output):
    print("Working on Temp {0}".format(round(temp, 3)))

    try:
        #run the Ising model
        lattice = IsingLattice(n, flip_prop)
        time_start = time.time()
        Msamp, Esamp = run_ising(lattice,
                                 temp,
                                 num_steps,
                                 num_burnin,
                                 j,
                                 b,
                                 disable_tqdm=True)

        traj = []
        try:
            if temp == 2.0:
                traj = Esamp[0::10]
        except:
            logging.error("Temp=" + str(round(temp, 3)) +
                          ": No example trajectory written")

        try:
            K_b = 1
            B = 1 / (K_b * temp)
            M_mean = np.average(Msamp[-num_analysis:])
            E_mean = np.average(Esamp[-num_analysis:])
            M_std = np.std(Msamp[-num_analysis:])
            E_std = np.std(Esamp[-num_analysis:])
            CV = (B / temp) * (E_std**2)
            X = B * (M_std**2)

            CV_bin = []
            X_bin = []
            bins = 50
            bin_size = num_analysis / bins

            for i in range(0, bins - 1):
                bin_start = int(-num_analysis + i * bin_size)
                bin_end = int(-num_analysis + (i + 1) * bin_size)
                M_std_bin = np.std(Msamp[bin_start:bin_end])
                E_std_bin = np.std(Esamp[bin_start:bin_end])
                #print([M_std_bin,E_std_bin])
                cv_bin_val = (B / temp) * (E_std_bin**2)
                x_bin_val = B * (M_std_bin**2)
                #print(cv_bin_val)
                #print(x_bin_val)
                CV_bin.append(cv_bin_val)
                X_bin.append(x_bin_val)
                #print(CV_bin)
                #print(X_bin)

            CV_err = np.std(CV_bin) / math.sqrt(bins - 1)
            X_err = np.std(X_bin) / math.sqrt(bins - 1)

            data_array = [
                np.abs(M_mean), M_std, E_mean, E_std, CV, CV_err, X, X_err
            ]
            data_listener.put([temp] + data_array)

            corr = lattice.calc_auto_correlation()
            lattice.free_memory()
            [corr_listener.put([temp] + corr_value) for corr_value in corr]

            traj_listener.put(traj)

            print("Done with Temp {0}".format(round(temp, 3)))
            return True

        except:
            logging.error("Temp=" + str(round(temp, 3)) +
                          ": Statistical Calculation Failed. No Data Written.")
            return False

    except KeyboardInterrupt:
        print("\n\nProgram Terminated. Good Bye!")
        data_listener.put('kill')
        corr_listener.put('kill')
        sys.exit()

    except:
        logging.error("Temp=" + str(round(temp, 3)) +
                      ": Simulation Failed. No Data Written")
Example #5
0
File: main_c.py Project: yx14/ising
def run_simulation(t_min, t_max, t_step, n, num_steps, num_analysis,
                   num_burnin, j, b, flip_prop, output, plots):

    time_start = time.time()

    check_step_values(num_steps, num_analysis, num_burnin)

    lattice = IsingLattice(n, flip_prop)
    # lattice.print_spins()

    T = get_temp_array(t_min, t_max, t_step)

    data_filename, corr_filename = get_filenames(output)

    write_sim_parameters(data_filename, corr_filename, n, num_steps,
                         num_analysis, num_burnin, j, b, flip_prop)

    if plots:
        #initialize vars for plotting values
        temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr = [],[],[],[],[]

    print('\nSimulation Started! Data will be written to ' + data_filename +
          '\n')

    temp_range = tqdm(T)  #set fancy progress bar range
    for index, temp in enumerate(temp_range):

        #show current temperature
        temp_range.set_description("Simulation Progress")

        try:
            #run the Ising model
            Msamp, Esamp = run_ising(lattice, temp, num_steps, num_burnin, j,
                                     b)
            #get and save statistical values
            if calculate_and_save_values(lattice, Msamp, Esamp, num_analysis,
                                         index, temp, data_filename,
                                         corr_filename):
                if plots:
                    #for plotting
                    M_mean, E_mean, M_std, E_std = get_plot_values(
                        temp, Msamp, Esamp, num_analysis)
                    temp_arr.append(temp)
                    M_mean_arr.append(M_mean)
                    E_mean_arr.append(E_mean)
                    M_std_arr.append(M_std)
                    E_std_arr.append(E_std)

        except KeyboardInterrupt:
            print("\n\nProgram Terminated. Good Bye!")
            sys.exit()

        except:
            logging.error("Temp=" + str(temp) +
                          ": Simulation Failed. No Data Written")

    print('\n ------ Time start(%f) Time finished(%f), total time(%f)' %
          (time_start, time.time(), time.time() - time_start))
    print('\n\nSimulation Finished! Data written to ' + data_filename)

    lattice.free_memory()
    if plots:
        plot_graphs(temp_arr, M_mean_arr, M_std_arr, E_mean_arr, E_std_arr)
Example #6
0
def run_simulation(index, temp, n, num_steps, num_burnin, num_analysis,
                   flip_prop, j, b, data_filename, corr_filename,
                   data_listener, corr_listener):
    print("Working on Temp {0}".format(round(temp, 4)))
    try:
        #run the Ising model
        lattice = IsingLattice(n, flip_prop)
        time_start = time.time()
        Msamp, Esamp = run_ising(lattice,
                                 temp,
                                 num_steps,
                                 num_burnin,
                                 j,
                                 b,
                                 disable_tqdm=True)

        try:
            #calculate statistical values
            #calculate statistical values
            M_mean = np.average(Msamp[-num_analysis:])
            E_mean = np.average(Esamp[-num_analysis:])
            M_std = np.std(Msamp[-num_analysis:])
            E_std = np.std(Esamp[-num_analysis:])

            Chi_mean = np.average(M_std**2 / temp)
            Cv_mean = 1 / temp**2 * E_std**2

            M_analysis = Msamp[-num_analysis:]
            E_analysis = Esamp[-num_analysis:]
            Chi_var = 1 / temp**2 * (
                scipy.stats.moment(M_analysis, 4) -
                scipy.stats.moment(M_analysis, 2)**2) / len(M_analysis)
            Chi_std = (Chi_var)**0.5

            Cv_var = 1 / temp**4 * (
                scipy.stats.moment(E_analysis, 4) -
                scipy.stats.moment(E_analysis, 2)**2) / len(E_analysis)
            Cv_std = (Cv_var)**0.5

            data_array = [
                np.abs(M_mean), M_std, E_mean, E_std, Chi_mean, Chi_std,
                Cv_mean, Cv_std
            ]

            data_listener.put([temp] + data_array)

            corr = lattice.calc_auto_correlation()
            lattice.free_memory()
            [corr_listener.put([temp] + corr_value) for corr_value in corr]

            print("Done with Temp {0} in {1} seconds".format(
                round(temp, 4), round(time.time() - time_start, 2)))
            return True

        except:
            logging.error("Temp=" + str(temp) +
                          ": Statistical Calculation Failed. No Data Written.")
            return False

    except KeyboardInterrupt:
        print("\n\nProgram Terminated. Good Bye!")
        data_listener.put('kill')
        corr_listener.put('kill')
        sys.exit()

    except:
        logging.error("Temp=" + str(temp) +
                      ": Simulation Failed. No Data Written")