def compute_variances(N, save=True, settings=SETTINGS_TEMPLATE, gammas=None): if gammas == None: gammas = np.linspace(1.5, 3.5, N) N = len(gammas) variances = 0 * gammas for i, gamma in enumerate(gammas): print "\n\nRunning sim for gamma = {0}".format(gamma) settings_file = open(SETTINGS_PATH + "/order_parameter.txt", "w") settings_file.write(settings) settings_file.write( "ID = order_parameter{0}\nGAMMA = {0}".format(gamma)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/order_parameter.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output( OUTPUT_PATH + "/cars_order_parameter{}.dat".format(gamma)) variances[i] = np.var(vv[-1]) if save: np.save("variances", variances) np.save("gammas", gammas) return gammas, variances
def compute_critical_gamma(n_cars = 50, max_iter = 10): left = 1.4 right = 3.5 for i in range(max_iter): gamma = (left+right)/2. print "\n\nRunning sim for gamma = {0}".format(gamma) settings_file = open(SETTINGS_PATH + "/phasediagram.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.write("ID = phasediagram{0}\nGAMMA = {0}\n".format(gamma)) settings_file.write("N_CARS = {}\n".format(n_cars)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH+"/phasediagram.txt"], cwd = SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH+"/cars_phasediagram{}.dat".format(gamma)) variance = np.var(vv[-1]) if variance > 0.04: left = gamma elif variance < 0.01: right = gamma else: return gamma return gamma
def msvs_setup_env(env): batfilename = msvs.get_batch_file() msvs = get_vs_by_version(version) if msvs is None: return # XXX: I think this is broken. This will silently set a bogus tool instead # of failing, but there is no other way with the current scons tool # framework if batfilename is not None: vars = ('LIB', 'LIBPATH', 'PATH', 'INCLUDE') msvs_list = get_installed_visual_studios() vscommonvarnames = [vs.common_tools_var for vs in msvs_list] save_ENV = env['ENV'] nenv = normalize_env(env['ENV'], ['COMSPEC'] + vscommonvarnames, force=True) try: output = get_output(batfilename, arch, env=nenv) finally: env['ENV'] = save_ENV vars = parse_output(output, vars) for k, v in vars.items(): env.PrependENVPath(k, v, delete_existing=1)
def merge_default_version(env): version = get_default_version(env) arch = get_default_arch(env) msvs = get_vs_by_version(version) if msvs is None: return batfilename = msvs.get_batch_file() # XXX: I think this is broken. This will silently set a bogus tool instead # of failing, but there is no other way with the current scons tool # framework if batfilename is not None: vars = ('LIB', 'LIBPATH', 'PATH', 'INCLUDE') msvs_list = get_installed_visual_studios() # TODO(1.5): #vscommonvarnames = [ vs.common_tools_var for vs in msvs_list ] vscommonvarnames = map(lambda vs: vs.common_tools_var, msvs_list) nenv = normalize_env(env['ENV'], vscommonvarnames + ['COMSPEC']) output = get_output(batfilename, arch, env=nenv) vars = parse_output(output, vars) for k, v in vars.items(): env.PrependENVPath(k, v, delete_existing=1)
def compute_flux(): ncars = np.arange(2, 150, 2) # set the velocity of the cars manually (in km/h) vcars = np.array([30, 50, 80, 100, 120]) / 3.6 flux = np.zeros((len(vcars), len(ncars))) density = ncars * (1000.0 / ROAD_LENGTH) for j, vel in enumerate(vcars): for i, cars in enumerate(ncars): print "\n\nRunning sim for ncars = {0} and maxV = {1}".format(cars, vel) settings_file = open(SETTINGS_PATH + "/flux.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.write("ID = flux{0}_{1}\nN_CARS= {0}\nVMAX= {1}".format(cars, vel)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/flux.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH + "/cars_flux{0}_{1}.dat".format(cars, vel)) # calculate the maximal throughput (cars per minute) # maxFlux = 0. # for t in np.arange(len(tt)-1): # maxFlux = np.max([maxFlux, throughput[t+1]-throughput[t]]) # calculates the average throughput flux[j, i] = throughput[-1] * 1.0 / tt[-1] flux = flux * 60 * 60 np.save("flux_density_v_density", density) np.save("flux_density_v_flux", flux) np.save("flux_density_v_maxV", vcars) return density, flux, vcars
def run_sim(): settings_file = open(SETTINGS_PATH + "/fourier_space.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/fourier_space.txt"], cwd=SIM_WD) # read output return parse_output(OUTPUT_PATH + "/cars_fourier_space.dat")
def script_env(script, args=None): stdout = common.get_output(script, args) # Stupid batch files do not set return code: we take a look at the # beginning of the output for an error message instead olines = stdout.splitlines() if olines[0].startswith("The specified configuration type is missing"): raise BatchFileExecutionError("\n".join(olines[:2])) return common.parse_output(stdout)
def make_plot(): matplotlib.rcParams.update({'font.size': 10}) plt.figure(figsize=(5, 3), dpi=200) settings_file = open(SETTINGS_PATH + "/free_road.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/free_road.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH + "/cars_free_road.dat") matplotlib.rcParams.update({'font.size': 11}) for i in range(0, len(xx[0, :]), max(1, len(xx[0, :]) / 10)): t = tt[:] x = xx[:, i] # detect discontinuities pos = np.array(np.where(np.abs(np.diff(x)) >= np.diff(t)[0] * 70)) + 1 for p in pos: t = np.insert(t, p, np.nan) x = np.insert(x, p, np.nan) if i % 100 == 0: plt.plot(t, x, "w:") # sort cars for i in range(len(xx)): start = np.argmin(xx[i, :]) xx[i, :] = np.hstack((xx[i, start:], xx[i, :start])) vv[i, :] = np.hstack((vv[i, start:], vv[i, :start])) tt_2d = np.tile(tt, (n, 1)).T grid_t, grid_x = np.mgrid[0.:END_TIME:512j, 0.:ROAD_LENGTH:512j] grid_v = np.nan_to_num( griddata((tt_2d.flatten(), xx.flatten()), vv.flatten(), (grid_t, grid_x), rescale=True)) plt.xlabel("time [s]") plt.ylabel("position [m]") plt.pcolormesh(grid_t, grid_x, grid_v) cbar = plt.colorbar() cbar.ax.set_ylabel('velocity [m/s]', rotation=90) ax = plt.gca() ax.set_ylim([0, np.max(np.max(xx))]) plt.tight_layout() plt.savefig("free_road.png", dpi=600) #plt.savefig("free_road.pdf") plt.show()
def make_plot(): matplotlib.rcParams.update({'font.size': 10}) plt.figure(figsize = (5,3), dpi = 200) settings_file = open(SETTINGS_PATH + "/free_road.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH+"/free_road.txt"], cwd = SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH+"/cars_free_road.dat") matplotlib.rcParams.update({'font.size': 11}) for i in range(0,len(xx[0,:]), max(1, len(xx[0,:])/10)): t = tt[:] x = xx[:,i] # detect discontinuities pos = np.array(np.where(np.abs(np.diff(x)) >= np.diff(t)[0]*70))+1 for p in pos: t = np.insert(t, p, np.nan) x = np.insert(x, p, np.nan) if i%100==0: plt.plot(t, x, "w:") # sort cars for i in range(len(xx)): start = np.argmin(xx[i,:]) xx[i,:] = np.hstack((xx[i,start:], xx[i,:start])) vv[i,:] = np.hstack((vv[i,start:], vv[i,:start])) tt_2d = np.tile(tt, (n,1)).T grid_t, grid_x = np.mgrid[0.:END_TIME:512j, 0.:ROAD_LENGTH:512j] grid_v = np.nan_to_num(griddata((tt_2d.flatten(), xx.flatten()), vv.flatten(), (grid_t, grid_x), rescale = True)) plt.xlabel("time [s]") plt.ylabel("position [m]") plt.pcolormesh(grid_t, grid_x, grid_v) cbar = plt.colorbar() cbar.ax.set_ylabel('velocity [m/s]', rotation=90) ax = plt.gca() ax.set_ylim([0,np.max(np.max(xx))]) plt.tight_layout() plt.savefig("free_road.png",dpi=600) #plt.savefig("free_road.pdf") plt.show()
def run_sim(): settings_file = open(SETTINGS_PATH + "/fourier_space.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/fourier_space.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH + "/cars_fourier_space.dat") # create grid tt_2d = np.tile(tt, (n, 1)).T grid_t, grid_x = np.mgrid[500.:END_TIME:1024j, 0.:ROAD_LENGTH:1024j] grid_v = np.nan_to_num( griddata((tt_2d.flatten(), xx.flatten()), vv.flatten(), (grid_t, grid_x), rescale=True)) sample_spacing_t = grid_t[2, 0] - grid_t[1, 0] sample_spacing_x = grid_x[0, 2] - grid_x[0, 1] grid_fft = np.log(np.abs(fftshift(fft2(grid_v)))) freq_t = fftshift(fftfreq(grid_fft.shape[0], sample_spacing_t)) freq_x = fftshift(fftfreq(grid_fft.shape[1], sample_spacing_x)) print "computation of Fourier transform complete" plt.figure() plt.subplot(1, 2, 1) plt.title("velocity field in real space (m/s)") plt.xlabel("time (s)") plt.ylabel("position (m)") plt.pcolormesh(grid_t, grid_x, grid_v) plt.colorbar() plt.subplot(1, 2, 2) plt.pcolormesh(freq_t, freq_x, grid_fft.T) plt.title("velocity field in frequency space\n(logarithmic magnitude)") plt.xlabel("frequency (1/s)") plt.ylabel("wave number (1/m)") plt.colorbar() plt.show()
def compute_flux(): ncars = np.arange(2, 150, 8) # set the exponent gamma gammas = np.arange(0.5, 4, 0.2) flux = np.zeros((len(gammas), len(ncars))) density = ncars * (1000. / ROAD_LENGTH) for j, gamma in enumerate(gammas): for i, cars in enumerate(ncars): print "\n\nRunning sim for ncars = {0} and gamma = {1}".format( cars, gamma) settings_file = open(SETTINGS_PATH + "/flux.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.write( "ID = flux{0}_{1}\nN_CARS= {0}\nGAMMA= {1}".format( cars, gamma)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/flux.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output( OUTPUT_PATH + "/cars_flux{0}_{1}.dat".format(cars, gamma)) # calculate the maximal throughput (cars per minute) # maxFlux = 0. # for t in np.arange(len(tt)-1): # maxFlux = np.max([maxFlux, throughput[t+1]-throughput[t]]) # calculates the average throughput flux[j, i] = throughput[-1] * 1. / tt[-1] flux = flux * 60 * 60 np.save("flux_density_gammas_density", density) np.save("flux_density_gammas_flux", flux) np.save("flux_density_gammas_gammas", gammas) return density, flux, gammas
def compute_variances(settings = SETTINGS_TEMPLATE, gammas = GAMMA): variances = 0*DMAX for i, decel in enumerate(DMAX): print "\n\nRunning sim for DMAX = {0}".format(decel) settings_file = open(SETTINGS_PATH + "/order_parameter.txt", "w") settings_file.write(settings) settings_file.write("ID = order_parameter{1}\nAMAX = {0}\nDMAX = {1}".format(AMAX, decel)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH+"/order_parameter.txt"], cwd = SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH+"/cars_order_parameter{}.dat".format(decel)) variances[i] = np.var(vv[-1]) np.save("variances_delta_acceleration", variances) np.save("deceleration_delta_acceleration", DMAX) return DMAX, variances
def run_sim(): settings_file = open(SETTINGS_PATH + "/fourier_space.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH+"/fourier_space.txt"], cwd = SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH + "/cars_fourier_space.dat") # create grid tt_2d = np.tile(tt, (n,1)).T grid_t, grid_x = np.mgrid[500.:END_TIME:1024j, 0.:ROAD_LENGTH:1024j] grid_v = np.nan_to_num(griddata((tt_2d.flatten(), xx.flatten()), vv.flatten(), (grid_t, grid_x), rescale = True)) sample_spacing_t = grid_t[2,0]-grid_t[1,0] sample_spacing_x = grid_x[0,2]-grid_x[0,1] grid_fft = np.log(np.abs(fftshift(fft2(grid_v)))) freq_t = fftshift(fftfreq(grid_fft.shape[0], sample_spacing_t)) freq_x = fftshift(fftfreq(grid_fft.shape[1], sample_spacing_x)) print "computation of Fourier transform complete" plt.figure() plt.subplot(1,2,1) plt.title("velocity field in real space (m/s)") plt.xlabel("time (s)") plt.ylabel("position (m)") plt.pcolormesh(grid_t, grid_x, grid_v) plt.colorbar() plt.subplot(1,2,2) plt.pcolormesh(freq_t, freq_x, grid_fft.T) plt.title("velocity field in frequency space\n(logarithmic magnitude)") plt.xlabel("frequency (1/s)") plt.ylabel("wave number (1/m)") plt.colorbar() plt.show()
def compute_variances(N, save=True, settings=SETTINGS_TEMPLATE, gammas=None): if gammas == None: gammas = np.linspace(1.5, 3.5, N) N = len(gammas) variances = 0 * gammas for i, gamma in enumerate(gammas): print "\n\nRunning sim for gamma = {0}".format(gamma) settings_file = open(SETTINGS_PATH + "/order_parameter.txt", "w") settings_file.write(settings) settings_file.write("ID = order_parameter{0}\nGAMMA = {0}".format(gamma)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH + "/order_parameter.txt"], cwd=SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH + "/cars_order_parameter{}.dat".format(gamma)) variances[i] = np.var(vv[-1]) if save: np.save("variances", variances) np.save("gammas", gammas) return gammas, variances
def compute_flux(): ncars = np.arange(2, 150, 8); # set the exponent gamma gammas = np.arange(0.5,4,0.2); flux = np.zeros((len(gammas), len(ncars))) density = ncars*(1000./ROAD_LENGTH) for j, gamma in enumerate(gammas): for i, cars in enumerate(ncars): print "\n\nRunning sim for ncars = {0} and gamma = {1}".format(cars, gamma) settings_file = open(SETTINGS_PATH + "/flux.txt", "w") settings_file.write(SETTINGS_TEMPLATE) settings_file.write("ID = flux{0}_{1}\nN_CARS= {0}\nGAMMA= {1}".format(cars, gamma)) settings_file.close() check_call([SIM_PATH, SETTINGS_PATH+"/flux.txt"], cwd = SIM_WD) # read output n, tt, xx, vv, throughput = parse_output(OUTPUT_PATH+"/cars_flux{0}_{1}.dat".format(cars, gamma)) # calculate the maximal throughput (cars per minute) # maxFlux = 0. # for t in np.arange(len(tt)-1): # maxFlux = np.max([maxFlux, throughput[t+1]-throughput[t]]) # calculates the average throughput flux[j,i] = throughput[-1]*1./tt[-1] flux = flux*60*60; np.save("flux_density_gammas_density", density) np.save("flux_density_gammas_flux", flux) np.save("flux_density_gammas_gammas", gammas) return density, flux, gammas
def script_env(script): stdout = common.get_output(script) return common.parse_output(stdout)