def run_simulation(index, temp, n, num_steps, num_burnin, num_analysis, flip_prop, j, b, data_filename, corr_filename): temp = round(temp, 2) print("Wprking on Temp {0}".format(temp)) try: #run the Ising model Msamp, Esamp, spin = run_ising(n, temp, num_steps, num_burnin, flip_prop, j, b, disable_tqdm=True) #get and save statistical values if calculate_and_save_values(Msamp, Esamp, spin, num_analysis, index, temp, data_filename, corr_filename): return temp except KeyboardInterrupt: print("\n\nProgram Terminated. Good Bye!") sys.exit() except: logging.error("Temp=" + str(temp) + ": Simulation Failed. No Data Written")
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,3))) try: #run the Ising model Msamp, Esamp, spin = run_ising(n,temp,num_steps,num_burnin,flip_prop,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:]) data_array = [np.abs(M_mean),M_std,E_mean,E_std] data_listener.put([temp]+data_array) corr = compute_autocorrelation(spin) [corr_listener.put([temp]+corr_value) for corr_value in corr] 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")
def run_simulation(t_min, t_max, t_step, n, num_steps, num_analysis, num_burnin, j, b, flip_prop, data_filename, corr_filename, plots): T = get_temp_array(t_min, t_max, t_step) if plots: #initialize vars for plotting values temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr = [],[],[],[],[] 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, spin = run_ising(n, temp, num_steps, num_burnin, flip_prop, j, b, disable_tqdm=True) #get and save statistical values if calculate_and_save_values(Msamp, Esamp, spin, 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") if plots: plot_graphs(temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr)
def run_simulation(t_min, t_max, t_step, n, num_steps, num_analysis, num_burnin, j, b, flip_prop, output, plots, t_anneal, anneal_boolean): check_step_values(num_steps, num_analysis, num_burnin) 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, t_anneal) 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, spin = run_ising(n, temp, num_steps, num_burnin, flip_prop, j, b, t_anneal, anneal_boolean) #plt.plot(Esamp[:20000]) #plt.show() #get and save statistical values if calculate_and_save_values(Msamp, Esamp, spin, 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\nSimulation Finished! Data written to ' + data_filename) if plots: plot_graphs(temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr)
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 Msamp, Esamp, spin = run_ising(n,temp,num_steps,num_burnin,flip_prop,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 = compute_autocorrelation(spin) [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")
def run_simulation(t_min, t_max, t_step, n, num_steps, num_analysis, num_burnin, j, b, flip_prop, output, plots): start_time = time.time() c_matrix = CDLL('./ising_matrix.so') c_matrix.get_E.restype = c_float c_matrix.get_M.restype = c_float c_matrix.get_spin.restype = c_int c_matrix.get_N.restype = c_int c_matrix.allocate(c_int(n)) check_step_values(num_steps, num_analysis, num_burnin) 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(c_matrix, n, temp, num_steps, num_burnin, flip_prop, j, b) # print("Msamp, Esamp\n") # print(Msamp) # print(Esamp) # exit() #get and save statistical values if calculate_and_save_values(c_matrix, 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") run_time = time.time() - start_time print('\n\nSimulation Finished! Data written to ' + data_filename) print("Run time: %f\n" % run_time) if plots: plot_graphs(temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr) c_matrix.free_mem()
def run_simulation(index, temp, n, num_steps, num_burnin, num_analysis, flip_prop, j, b, data_filename, corr_filename, data_listener, corr_listener, plots): print("Working on Temp {0}".format(round(temp, 3))) if plots: #initialize vars for plotting values temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr, CV_arr, X_arr, CV_err_arr, X_err_arr = [],[],[],[],[],[],[],[],[] try: #run the Ising model Msamp, Esamp, spin = run_ising(n, temp, num_steps, num_burnin, flip_prop, j, b, disable_tqdm=True) try: K_b = 1 B = 1 / (K_b * temp) M_mean = np.average(Msamp[-num_analysis:]) E_mean = np.average(Esamp[-num_analysis:]) M2 = [x**2 for x in Msamp] E2 = [x**2 for x in Esamp] M2_mean = np.average(M2[-num_analysis:]) E2_mean = np.average(E2[-num_analysis:]) M_std = np.std(Msamp[-num_analysis:]) E_std = np.std(Esamp[-num_analysis:]) M2_std = np.std(M2[-num_analysis:]) E2_std = np.std(E2[-num_analysis:]) CV = (B / temp) * (E2_mean - (E_mean**2)) X = B * (M2_mean - (M_mean**2)) CV_err = math.sqrt((E2_std**2) + (2 * E_mean * E_std)**2) X_err = math.sqrt(M2_std**2 + (2 * M_mean * M_std)**2) data_array = [ np.abs(M_mean), M_std, E_mean, E_std, CV, X, CV_err, X_err ] data_listener.put([temp] + data_array) corr = compute_autocorrelation(spin) [corr_listener.put([temp] + corr_value) for corr_value in corr] print("Done with Temp {0}".format(round(temp, 3))) return True if plots: #for plotting M_mean, E_mean, M_std, E_std, CV, X, CV_err, X_err = 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) CV_arr.append(CV) X_arr.append(X) CV_err_arr.append(CV_err) X_err_arr.append(X_err) 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") if plots: plot_graphs(temp_arr, M_mean_arr, E_mean_arr, M_std_arr, E_std_arr, CV_arr, X_arr, CV_err_arr, X_err_arr) print("plotting")