def main(a, ftype, fpar, Nrun, dt, timefac, intmeth, ephi, vb): """ """ me = me0 + ".main: " t0 = time.time() ## ---------------------------------------------------------------- ## CHOOSE FORCE R, S, lam, nu = fpar if ftype == "const": force = lambda xy, r: force_const(xy, r, R) fstr = "C" fparstr = "" elif ftype == "lin": force = lambda xy, r: force_lin(xy, r, R) fstr = "L" fparstr = "" elif ftype == "lico": force = lambda xy, r: force_lico(xy, r, R, g) fstr = "LC" fparstr = "" elif ftype == "dcon": force = lambda xy, r: force_dcon(xy, r, R, S) fstr = "DC" fparstr = "_S" + str(S) elif ftype == "dlin": force = lambda xy, r: force_dlin(xy, r, R, S) fstr = "DL" fparstr = "_S" + str(S) elif ftype == "tan": force = lambda xy, r: force_tan(xy, r, R, lam) fstr = "T" fparstr = "_l" + str(lam) elif ftype == "dtan": force = lambda xy, r: force_dtan(xy, r, R, S, lam) fstr = "DT" fparstr = "_S" + str(S) + "_l" + str(lam) elif ftype == "nu": force = lambda xy, r: force_nu(xy, r, R, lam, nu) fstr = "N" fparstr = "_l" + str(lam) + "_n" + str(nu) elif ftype == "dnu": force = lambda xy, r: force_dnu(xy, r, R, S, lam, nu) fstr = "DN" fparstr = "_S" + str(S) + "_l" + str(lam) + "_n" + str(nu) else: raise IOError, me + "ftype must be one of {const, lin, lico, dcon, dlin, tan, dtan, nu}." dblpot = True if ftype[0] == "d" else False infpot = True if (ftype[-3:] == "tan" or ftype[-2:] == "nu") else False ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2 * timefac ## Simulation limits rmax = R + 2.0 * lam if infpot else R + 6.0 # rmin = 0.0 #max([0.0, 0.9*R-5*np.sqrt(a)]) rmin = max(0.0, S - 2.0 * lam if infpot else S - 4.0) ## Injection x coordinate -- half-way into bulk # rini = 0.5*(S+R) if dblpot else 0.5*(rmin+R) rini = np.sqrt(0.5 * (S * S + R * R)) if dblpot else np.sqrt( 0.5 * (rmin * rmin + R * R)) if R == 0.0: rini += 0.001 ## Limits for finite potential wb = [R + lam, (S > 0.0) * (S - lam)] if infpot else [False, False] ## ------------ ## Bin edges rbins = calc_rbins(infpot, fpar, rmin, rmax) Nrbin = rbins.size - 1 Npbin = 50 pbins = np.linspace(0.0, 2 * np.pi, Npbin) ermax = 4 / np.sqrt(a) if a != 0 else 4 / np.sqrt(dt) Nerbin = 150 erbins = np.linspace(0.0, ermax, Nerbin + 1) if ephi: # Nepbin = 50 # epbins = np.linspace(0.0,2*np.pi,Nepbin+1) # pstr = "_phi" Nepbin = 100 epbins = np.linspace(-2 * np.pi, +2 * np.pi, Nepbin + 1) pstr = "_psi" bins = [rbins, erbins, epbins] else: pstr = "" bins = [rbins, erbins] ## ------------ ## Particles Nparticles = Npbin * Nrun ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2 / dt) * np.random.normal(0.0, 1.0, [Nparticles, 2]) else: eIC = 1. / np.sqrt(a) * np.random.normal(0.0, 1.0, [Nparticles, 2]) ## Apply boundary conditions (should be integrated into force?) fxy = lambda xy, r: fxy_infpot(xy, r, force, wb[0], wb[1], dt ) if infpot else force(xy, r) ## ---------------------------------------------------------------- ## Integration algorithm eul_step = lambda xy, r, exy: eul(xy, r, fxy, exy, dt) if intmeth == "rk4": xy_step = lambda xy, r, exy: RK4(xy, r, fxy, exy, dt, eul_step) intmeth = "_rk4" elif intmeth == "rk2": xy_step = lambda xy, r, exy: RK2(xy, r, fxy, exy, dt, eul_step) intmeth = "_rk2" else: xy_step = eul_step intmeth = "" ## ---------------------------------------------------------------- ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_POL_"+fstr+"_dt"+str(dt)+intmeth+pstr+"/" hisfile = "BHIS_POL_" + fstr + "_a" + str(a) + "_R" + str( R) + fparstr + "_dt" + str(dt) + intmeth binfile = "BHISBIN" + hisfile[4:] filepath = hisdir + hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins if ephi: np.savez(hisdir + binfile, rbins=rbins, erbins=erbins, epbins=epbins) else: np.savez(hisdir + binfile, rbins=rbins, erbins=erbins) ## ---------------------------------------------------------------- ## SIMULATION if vb: print me + "Computing", Nparticles, "trajectories. Arena: POL. Force: " + str( ftype) + "." ## Precompute exp(-t) if a == 0: ## Not used in calculations expmt = None # elif a <= 0.1: # ## a is small, and exponential is dominated by first term # if vb: print me+"rescaling time" # expmt = np.exp(np.arange(-10,0.1,0.1)) else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10 * a, dt, dt)) / a) simulate_trajectory = lambda xyini, eIC, vb2:\ boundary_sim(xyini, eIC, a, xy_step, dt, tmax, expmt, ephi, vb2) ## ---------------------------------------------------------------- ## Initialise histogram in space H = np.zeros([b.size - 1 for b in bins]) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for pini in pbins: ## Perform several runs in Cartesian coordinates xyini = [rini * np.cos(pini), rini * np.sin(pini)] for run in xrange(Nrun): if vb: print me + "Run", i, "of", Nparticles coords = simulate_trajectory(xyini, eIC[i], (vb and run % 50 == 0)) H += np.histogramdd(coords, bins=bins, normed=False)[0] i += 1 ## Divide by bin area and number of particles binc = [np.diff(b) for b in bins] H /= reduce(np.multiply, np.ix_(*binc)) H /= Nparticles check_path(filepath, vb) save_data(filepath, H, vb) if vb: print me + "execution time", round(time.time() - t0, 2), "seconds" return
def main(): """ NAME LE_2DLBS.py PURPOSE Simulate coloured noise trajectories in 2D. EXECUTION FLAGS -a --alpha 0.1 Slope of the potential -X --wallpos 10.0 Position of wall -D --Delta 0.0 Width of wall onset in units of X -r --nruns 100 Number of runs for each (x0,y0) -t --timefac 1.0 Multiply t_max by factor -v --verbose False Print useful information to screen -h --help False Print docstring and exit -R --radius 1.0 Radius of wall curvature --BC-impenetrable Change BC to impenetrable rather then periodic --schematic Plot a schematic of the simulation space --trajectory Output some trajectory plots EXAMPLE NOTES BUGS HISTORY 20 February 2016 Adapted from LE_LightBoundarySim.py """ me = "LE_2DLBS.main: " t0 = time.time() ## ---------------------------------------------------------------- ## INPUT OPTIONS parser = optparse.OptionParser(conflict_handler="resolve") parser.add_option('-a', '--alpha', dest="a", default=0.1, type="float", help="The steepness of the potential.") parser.add_option('-X', '--wallpos', dest="X", default=10.0, type="float") parser.add_option('-D', '--Delta', dest="Delta", default=0.0, type="float") parser.add_option('-r', '--nrun', dest="Nrun", default=100, type="int") parser.add_option('--dt', dest="dt", default=0.01, type="float") parser.add_option('-t', '--timefac', dest="timefac", default=1.0, type="float") parser.add_option('-v', '--verbose', dest="vb", default=False, action="store_true", help="Print useful information to screen.") parser.add_option('-R', '--radius', dest="R", default=1.0, type="float") parser.add_option('--BC-impenetrable', dest="PBC", default=True, action="store_false") parser.add_option('--schematic', dest="schematic", default=False, action="store_true") parser.add_option('--trajectory', dest="traj", default=False, action="store_true") parser.add_option('-v', '--verbose', dest="vb", default=False, action="store_true", help="Print useful information to screen.") parser.add_option('-h', '--help', dest="help", default=False, action="store_true", help="Print docstring.") opts, argv = parser.parse_args() if opts.help: print main.__doc__ return a = opts.a X = opts.X Delta = opts.Delta Nrun = opts.Nrun global dt dt = opts.dt timefac = opts.timefac vb = opts.vb R = opts.R schematic = opts.schematic traj = opts.traj PBC = opts.PBC if Delta != 0.0: print me + "WARNING: Resetting Delta = 0.0." Delta = 0.0 if vb: print "\n==\n" + me + "Input parameters:\n\t", opts ## ---------------------------------------------------------------- ## SETUP CALCULATIONS ## Simulation time tmax = 5e2 * timefac ## Space: y, circle, x ymax = 0.5 assert (R >= ymax), me + "The wall must enclose the volume." if (vb and ymax <= 2 * a): print me + "Warning: the width of the space is comparable to or smaller than the memory length-scale." ## Centre of circle for curved boundary R2 = R * R c = [X - np.sqrt(R2 - ymax * ymax), 0.0] ## Simulation limits xmax = lookup_xmax(c[0] + R, a) xmin = calculate_xmin(X, a) ## Simulation cutoff ## Injection x coordinate xini = calculate_xini(X, a) ## Histogramming; bin edges Nxbin = 200 Nybin = 50 xbins = calculate_xbin(xini, X, xmax, Nxbin) ybins = calculate_ybin(0.0, ymax, Nybin + 1) ## Particles Nparticles = 1 * len(ybins) * Nrun ## Initial noise drawn from Gaussian eIC = np.random.normal( 0.0, 1.0 / a, [Nparticles, 2 ]) if a > 0.0 else 100 * (np.random.random([Nparticles, 2]) - 0.5) ## Centre of circle for curved boundary R2 = R * R c = [X - np.sqrt(R2 - ymax * ymax), 0.0] ## Filename; directory and file existence; readme BCstr = "PBC" if PBC else "IBC" hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_2D_"+BCstr+"_r"+str(Nrun)+"_dt"+str(dt)+"/" hisfile = "BHIS_2D_" + BCstr + "_a" + str(a) + "_X" + str(X) + "_R" + str( R) + "_r" + str(Nrun) + "_dt" + str(dt) binfile = "BHISBIN" + hisfile[4:] filepath = hisdir + hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins np.savez(hisdir + binfile, xbins=xbins, ybins=ybins) ## ---------------------------------------------------------------- ## SCHEMATIC IMAGE if schematic: draw_schematic(xmin, xbins, ybins, c, R, hisdir + "SCHM" + hisfile[4:] + ".png", True) return ## ---------------------------------------------------------------- ## SIMULATION if vb: print me + "Computing", Nparticles, "trajectories." ## Precompute exp(-t/a^2) expmt = np.exp(-np.arange(0, tmax, dt) / (a * a)) if a > 0 else np.array([1.] + [0.] * (int(tmax / dt) - 1)) ## Initialise histogram in space H = np.zeros((Nxbin, Nybin)) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for yini in ybins: ## Perform several runs for run in xrange(Nrun): ## x, y are coordinates as a function of time x, y = boundary_sim((xini, yini), eIC[i], a, X, Delta, xmin, ymax, R2, c, tmax, expmt, PBC, (vb and run % 50 == 0)) if traj and run == 0: plot_traj(x, y, xmin, X, xmax, ymax, hisdir + "TRAJ" + str(i) + hisfile[4:] + ".png") H += np.histogram2d(x, y, bins=[xbins, ybins], normed=False)[0] i += 1 H = (H.T)[::-1] ## When normed=False, need to divide by the bin area H /= np.outer(np.diff(ybins), np.diff(xbins)) ## Normalise by number of particles H /= Nparticles save_data(filepath, H, vb) if vb: print me + "execution time", round(time.time() - t0, 2), "seconds" return filepath
def main(a, ftype, R, S, T, P, dt, timefac, vb): """ """ me = me0 + ".main: " t0 = time.time() ## ---------------------------------------------------------------- ## CHOOSE FORCE, FILENAME, SPACE xydata = False ## Only interested in x data -- symmetric in y ymin, ymax = 0.0, 1.0 ## Only relevant when xydata = True ## Double quadratic potential if ftype == "dcon": ## Force fxy = lambda xy: force_dcon(xy, R, S) ## Filename fstr = "DC" filepar = "" ## Simulation limits xmax = R + 4.0 xmin = S - 4.0 ## Double quadratic potential elif ftype == "dlin": ## Force fxy = lambda xy: force_dlin(xy, R, S) ## Filename fstr = "DL" filepar = "" ## Simulation limits xmax = R + 4.0 xmin = S - 4.0 ## Casimir quadratic potential. Symmetric about x=0. elif ftype == "clin": ## Force fxy = lambda xy: force_clin(xy, R, S, T) ## Filename fstr = "CL" filepar = "_T%.1f" % (T) ## Simulation limits xmax = R + 4.0 xmin = 0.0 ## Single central wall. elif ftype == "mlin": ## Force fxy = lambda xy: force_mlin(xy, R, S, T) ## Filename fstr = "ML" filepar = "_T%.1f" % (T) ## Simulation limits xmax = +R + 4.0 xmin = -R - 4.0 ## Single central wall, centred at S with zero bulk. elif ftype == "nlin": ## Force fxy = lambda xy: force_nlin(xy, R, S) ## Filename fstr = "NL" filepar = "" ## Simulation limits xmax = +R + 4.0 xmin = -R - 4.0 ## Undulating wall, one at R and the other at -R. elif ftype == "ulin": ## Force ## R is position of right wall, S is amplitude, T is wavelength fxy = lambda xy: force_ulin(xy, R, S, T, P * np.pi) ## Filename fstr = "UL" filepar = "_T%.1f_P%.1f" % (T, P) ## Simulation limits xmax = R + 4.0 xmin = 0.0 ymax = T ymin = 0.0 xydata = True else: raise IOError, me + "check ftype." ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2 * timefac ## Particles Nparticles = 50 ## Injection coordinate: random sample from WN distribution x = np.linspace(xmin, xmax, 1000) rhoWN = np.exp(sp.integrate.cumtrapz(fxy([x, 0])[0], x, initial=0.0)) rhoWN /= rhoWN.sum() xini = np.random.choice(x, size=Nparticles, p=rhoWN) yini = (ymax - ymin) * np.random.random(Nparticles) ## ------------ ## Bin edges: x, etax, etay Nxbin = int(200 * (xmax - xmin)) Nybin = int(100 * (ymax - ymin)) xbins = np.linspace(xmin, xmax, Nxbin + 1) ybins = np.linspace(ymin, ymax, Nybin + 1) emax = 4 / np.sqrt(a) if a != 0 else 4 / np.sqrt(dt) Nebin = 100 exbins = np.linspace(-emax, +emax, Nebin + 1) eybins = np.linspace(-emax, +emax, Nebin + 1) bins = [xbins, ybins] if xydata else [xbins, exbins, eybins] ## ------------ ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2 / dt) * np.random.normal(0.0, 1.0, [Nparticles, 2]) else: eIC = 1. / np.sqrt(a) * np.random.normal(0.0, 1.0, [Nparticles, 2]) ## ---------------------------------------------------------------- ## Integration algorithm xy_step = lambda xy, exy: eul(xy, exy, fxy, dt) ## ---------------------------------------------------------------- ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_CAR_"+fstr+"_dt"+str(dt)+"/" hisfile = "BHIS_CAR_" + fstr + "_a" + str(a) + "_R" + str(R) + "_S" + str( S) + filepar + "_dt" + str(dt) binfile = "BHISBIN" + hisfile[4:] filepath = hisdir + hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins np.savez(hisdir + binfile, xbins=xbins, ybins=ybins, exbins=exbins, eybins=eybins) ## ---------------------------------------------------------------- ## SIMULATION if vb: print me + "Computing", Nparticles, "trajectories. Arena: CAR. Force: " + str( ftype) + "." ## Precompute exp(-t) if a == 0: ## Not used in calculations expmt = None elif a <= 0.1: raise ValueError, me + "Must have alpha>0.1" else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10 * a, dt, dt)) / a) ## ---------------------------------------------------------------- ## Initialise histogram in space H = np.zeros([b.size - 1 for b in bins]) ## Loop over initial coordinates for i in range(Nparticles): ## Perform run in Cartesian coordinates if vb: print me + "Run", i, "of", Nparticles coords = simulate_trajectory([xini[i], yini[i]], eIC[i], a, xy_step, dt, tmax, expmt, xydata, vb) ## Apply BCs where appropriate ## CLIN if ftype[0] == "c": ## Reflecting BC at x=0 idx = coords[0] < 0.0 coords[0][idx] *= -1 coords[1][idx] *= -1 coords[2][idx] *= -1 ## ULIN if ftype[ 0] == "u": ## Periodic BC -- what to do with x<0 and (y<0 and y>T) coords = np.array(coords) if P == 0: coords[:, coords[0] < 0.0] *= -1 ## Reflect around x and y: x->-1,y->-y else: coords[1, coords[0] < 0.0] -= T * 0.5 * ( 1 - P) ## Translate y->y-T*(pi-phi)/2pi coords[0, coords[0] < 0.0] *= -1 ## Reflect x->-x coords[1] %= T ## Periodic in y coords = coords.tolist() ## Histogram H += np.histogramdd(coords, bins=bins, normed=False)[0] ## Divide by bin area and number of particles binc = [np.diff(b) for b in bins] H /= reduce(np.multiply, np.ix_(*binc)) H /= Nparticles check_path(filepath, vb) save_data(filepath, H, vb) if vb: print me + "execution time", round(time.time() - t0, 2), "seconds" return
def main(a, ftype, S, dt, timefac, intmeth, ephi, vb): """ """ me = "LE_inSBS.main: " t0 = time.time() ## ---------------------------------------------------------------- ## CHOOSE FORCE if "linin": force = lambda xy, r: force_linin(xy, r, S) fstr = "L" fparstr = "" else: raise IOError, me + "ftype must be one of {linin}." ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2 * timefac ## Simulation limits rmax = S + max(1, 4.0 * round(np.sqrt(a), 0)) rmin = 0.0 ## Injection coordinate rini = S + 1.0 ## ------------ ## Bin edges Nrbin = int(150 * (rmax - rmin)) rbins = np.linspace(rmin, rmax, Nrbin + 1) Npbin = 50 pbins = np.linspace(0.0, 2 * np.pi, Npbin) ermax = 4 / np.sqrt(a) if a != 0 else 4 / np.sqrt(dt) Nerbin = 150 erbins = np.linspace(0.0, ermax, Nerbin + 1) if ephi: Nepbin = 50 epbins = np.linspace(0.0, 2 * np.pi, Nepbin + 1) pstr = "_phi" bins = [rbins, erbins, epbins] else: pstr = "" bins = [rbins, erbins] ## ------------ ## Particles Nparticles = Npbin ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2 / dt) * np.random.normal(0.0, 1.0, [Nparticles, 2]) else: eIC = 1. / np.sqrt(a) * np.random.normal(0.0, 1.0, [Nparticles, 2]) ## Apply boundary conditions (should be integrated into force?) fxy = lambda xy, r: force(xy, r) ## ---------------------------------------------------------------- ## Integration algorithm xy_step = lambda xy, r, exy: eul(xy, r, fxy, exy, dt) ## ---------------------------------------------------------------- ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_INCIR_"+fstr+"_dt"+str(dt)+pstr+"/" hisfile = "BHIS_INCIR_" + fstr + "_a" + str(a) + "_S" + str( S) + "_dt" + str(dt) binfile = "BHISBIN" + hisfile[4:] filepath = hisdir + hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins if ephi: np.savez(hisdir + binfile, rbins=rbins, erbins=erbins, epbins=epbins) else: np.savez(hisdir + binfile, rbins=rbins, erbins=erbins) ## ---------------------------------------------------------------- ## SIMULATION if vb: print me + "Computing", Nparticles, "trajectories. Arena: CIR. Force: " + str( ftype) + "." ## Precompute exp(-t) if a == 0: ## Not used in calculations expmt = None elif a <= 0.1: raise ValueError, me + "Must have alpha>0.1" else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10 * a, dt, dt)) / a) simulate_trajectory = lambda xyini, eIC, vb2:\ boundary_sim(xyini, eIC, a, xy_step, rmax, dt, tmax, expmt, ephi, vb2) ## ---------------------------------------------------------------- ## Initialise histogram in space H = np.zeros([b.size - 1 for b in bins]) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for pini in pbins: ## Perform several runs in Cartesian coordinates xyini = [rini * np.cos(pini), rini * np.sin(pini)] if vb: print me + "Run", i, "of", Nparticles coords = simulate_trajectory(xyini, eIC[i], vb) H += np.histogramdd(coords, bins=bins, normed=False)[0] i += 1 ## Divide by bin area and number of particles binc = [np.diff(b) for b in bins] H /= reduce(np.multiply, np.ix_(*binc)) H /= Nparticles check_path(filepath, vb) save_data(filepath, H, vb) if vb: print me + "execution time", round(time.time() - t0, 2), "seconds" return
def main(): """ NAME LE_2DLBS.py PURPOSE Simulate coloured noise trajectories in 2D. EXECUTION FLAGS -a --alpha 0.1 Slope of the potential -X --wallpos 10.0 Position of wall -D --Delta 0.0 Width of wall onset in units of X -r --nruns 100 Number of runs for each (x0,y0) -t --timefac 1.0 Multiply t_max by factor -v --verbose False Print useful information to screen -h --help False Print docstring and exit -R --radius 1.0 Radius of wall curvature --BC-impenetrable Change BC to impenetrable rather then periodic --schematic Plot a schematic of the simulation space --trajectory Output some trajectory plots EXAMPLE NOTES BUGS HISTORY 20 February 2016 Adapted from LE_LightBoundarySim.py """ me = "LE_2DLBS.main: " t0 = time.time() ## ---------------------------------------------------------------- ## INPUT OPTIONS parser = optparse.OptionParser(conflict_handler="resolve") parser.add_option('-a','--alpha', dest="a",default=0.1,type="float", help="The steepness of the potential.") parser.add_option('-X','--wallpos', dest="X",default=10.0,type="float") parser.add_option('-D','--Delta', dest="Delta",default=0.0,type="float") parser.add_option('-r','--nrun', dest="Nrun",default=100,type="int") parser.add_option('--dt', dest="dt",default=0.01,type="float") parser.add_option('-t','--timefac', dest="timefac",default=1.0,type="float") parser.add_option('-v','--verbose', dest="vb",default=False,action="store_true", help="Print useful information to screen.") parser.add_option('-R','--radius', dest="R",default=1.0,type="float") parser.add_option('--BC-impenetrable', dest="PBC",default=True,action="store_false") parser.add_option('--schematic', dest="schematic",default=False,action="store_true") parser.add_option('--trajectory', dest="traj",default=False,action="store_true") parser.add_option('-v','--verbose', dest="vb",default=False,action="store_true", help="Print useful information to screen.") parser.add_option('-h','--help', dest="help",default=False,action="store_true", help="Print docstring.") opts, argv = parser.parse_args() if opts.help: print main.__doc__; return a = opts.a X = opts.X Delta = opts.Delta Nrun = opts.Nrun global dt; dt = opts.dt timefac = opts.timefac vb = opts.vb R = opts.R schematic = opts.schematic traj = opts.traj PBC = opts.PBC if Delta!=0.0: print me+"WARNING: Resetting Delta = 0.0." Delta = 0.0 if vb: print "\n==\n"+me+"Input parameters:\n\t",opts ## ---------------------------------------------------------------- ## SETUP CALCULATIONS ## Simulation time tmax = 5e2*timefac ## Space: y, circle, x ymax = 0.5 assert (R>=ymax), me+"The wall must enclose the volume." if (vb and ymax<=2*a): print me+"Warning: the width of the space is comparable to or smaller than the memory length-scale." ## Centre of circle for curved boundary R2 = R*R c = [X-np.sqrt(R2-ymax*ymax),0.0] ## Simulation limits xmax = lookup_xmax(c[0]+R,a) xmin = calculate_xmin(X,a) ## Simulation cutoff ## Injection x coordinate xini = calculate_xini(X,a) ## Histogramming; bin edges Nxbin = 200 Nybin = 50 xbins = calculate_xbin(xini,X,xmax,Nxbin) ybins = calculate_ybin(0.0,ymax,Nybin+1) ## Particles Nparticles = 1*len(ybins)*Nrun ## Initial noise drawn from Gaussian eIC = np.random.normal(0.0,1.0/a,[Nparticles,2]) if a>0.0 else 100*(np.random.random([Nparticles,2])-0.5) ## Centre of circle for curved boundary R2 = R*R c = [X-np.sqrt(R2-ymax*ymax),0.0] ## Filename; directory and file existence; readme BCstr = "PBC" if PBC else "IBC" hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_2D_"+BCstr+"_r"+str(Nrun)+"_dt"+str(dt)+"/" hisfile = "BHIS_2D_"+BCstr+"_a"+str(a)+"_X"+str(X)+"_R"+str(R)+"_r"+str(Nrun)+"_dt"+str(dt) binfile = "BHISBIN"+hisfile[4:] filepath = hisdir+hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins np.savez(hisdir+binfile,xbins=xbins,ybins=ybins) ## ---------------------------------------------------------------- ## SCHEMATIC IMAGE if schematic: draw_schematic(xmin,xbins,ybins,c,R, hisdir+"SCHM"+hisfile[4:]+".png",True) return ## ---------------------------------------------------------------- ## SIMULATION if vb: print me+"Computing",Nparticles,"trajectories." ## Precompute exp(-t/a^2) expmt = np.exp(-np.arange(0,tmax,dt)/(a*a)) if a>0 else np.array([1.]+[0.]*(int(tmax/dt)-1)) ## Initialise histogram in space H = np.zeros((Nxbin,Nybin)) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for yini in ybins: ## Perform several runs for run in xrange(Nrun): ## x, y are coordinates as a function of time x, y = boundary_sim((xini,yini), eIC[i], a, X,Delta, xmin,ymax, R2,c, tmax,expmt, PBC, (vb and run%50==0)) if traj and run==0: plot_traj(x,y,xmin,X,xmax,ymax, hisdir+"TRAJ"+str(i)+hisfile[4:]+".png") H += np.histogram2d(x,y,bins=[xbins,ybins],normed=False)[0] i += 1 H = (H.T)[::-1] ## When normed=False, need to divide by the bin area H /= np.outer(np.diff(ybins),np.diff(xbins)) ## Normalise by number of particles H /= Nparticles save_data(filepath, H, vb) if vb: print me+"execution time",round(time.time()-t0,2),"seconds" return filepath
def main(a,ftype,X,Delta,Nrun,dt,timefac,vb): """ """ me = "LE_LightBoundarySim.main: " t0 = time.time() ## ---------------------------------------------------------------- ## Parameters ## Choose potential type if ftype=="const": force_x = force_1D_const fstr = "C" elif ftype=="lin": force_x = force_1D_lin fstr = "L" Delta = 0.0 ## Simulation time tmax = 5e2*timefac ## Space xmax = X+6.0#lookup_xmax(X,a) xmin = 0.0#calculate_xmin(X,a) ## Simulation cutoff xini = 0.5*(xmin+X)#calculate_xini(X,a) ## Particle initial x emax = 4/np.sqrt(a) if a!=0.0 else 4/np.sqrt(dt) ## Histogramming; xbins and ebins are bin edges. Nxbin = int(150 * xmax) Nebin = 50 xbins = np.linspace(xmin,xmax,Nxbin+1)#calculate_xbin(xini,X,xmax,Nxbin) ebins = np.linspace(-emax,emax,Nebin+1) ## Initial conditions X0E0 = np.array([[xini,e0] for e0 in ebins]) Nparticles = Nebin*Nrun if vb: print me+"Computing",Nparticles,"trajectories" ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+"_1D_"+fstr+"_D"+str(Delta)+"_dt"+str(dt)+"/" hisfile = "BHIS_1D_"+fstr+"_a"+str(a)+"_X"+str(X)+"_D"+str(Delta)+"_dt"+str(dt) binfile = "BHISBIN"+hisfile[4:] hisfile = hisdir+hisfile check_path(hisfile, vb) create_readme(hisfile, vb) ## Save bins np.savez(hisdir+binfile,xbins=xbins,ebins=ebins) ## ---------------------------------------------------------------- ## Precompute exp(-t) and initialise histogram if a == 0: ## Not used in calculations expmt = None elif a <= 0.1: ## a is small, and exponential is dominated by first term, ## which is then exaggerated by 1/a ## Use a reference expmt to be rescaled. ## 1/(a) larger array which will be integrated to usual size. if vb: print me+"rescaling time" # expmt = np.exp(np.arange(-10,dt,dt)) expmt = np.exp(np.arange(-10,0.1,0.1)) else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10*a,dt,dt))/(a)) # expmt[:int(tmax-10*a/dt)]=0.0 ## Approximation ## ---------------------------------------------------------------- ## Initialise histogram H = np.zeros((Nxbin,Nebin)) i = 1 ## Loop over initial y-position for x0e0 in X0E0: for run in xrange(Nrun): if vb: print me+"Run",i,"of",Nparticles ## x, e are coordinates as a function of time x, e = boundary_sim(x0e0, a, X, force_x, Delta, xmin, tmax, dt, expmt, (vb and run%50==0)) h = np.histogram2d(x,e,bins=[xbins,ebins],normed=False)[0] H += h*histogram_weight(x0e0[1], e[-1], a) i += 1 H = (H.T)[::-1] ## When normed=False, need to divide by the bin area H /= np.outer(np.diff(ebins),np.diff(xbins)) ## Normalise by number of particles H /= Nparticles check_path(hisfile, vb) save_data(hisfile, H, vb) return hisfile
def main(a,ftype,S,dt,timefac,intmeth,ephi,vb): """ """ me = "LE_inSBS.main: " t0 = time.time() ## ---------------------------------------------------------------- ## CHOOSE FORCE if "linin": force = lambda xy, r: force_linin(xy,r,S) fstr = "L" fparstr = "" else: raise IOError, me+"ftype must be one of {linin}." ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2*timefac ## Simulation limits rmax = S+max(1,4.0*round(np.sqrt(a),0)) rmin = 0.0 ## Injection coordinate rini = S + 1.0 ## ------------ ## Bin edges Nrbin = int(150 * (rmax-rmin)) rbins = np.linspace(rmin,rmax,Nrbin+1) Npbin = 50 pbins = np.linspace(0.0,2*np.pi,Npbin) ermax = 4/np.sqrt(a) if a!=0 else 4/np.sqrt(dt) Nerbin = 150 erbins = np.linspace(0.0,ermax,Nerbin+1) if ephi: Nepbin = 50 epbins = np.linspace(0.0,2*np.pi,Nepbin+1) pstr = "_phi" bins = [rbins,erbins,epbins] else: pstr = "" bins = [rbins,erbins] ## ------------ ## Particles Nparticles = Npbin ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2/dt)*np.random.normal(0.0, 1.0, [Nparticles,2]) else: eIC = 1./np.sqrt(a)*np.random.normal(0.0, 1.0, [Nparticles,2]) ## Apply boundary conditions (should be integrated into force?) fxy = lambda xy, r: force(xy,r) ## ---------------------------------------------------------------- ## Integration algorithm xy_step = lambda xy, r, exy: eul(xy, r, fxy, exy, dt) ## ---------------------------------------------------------------- ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_INCIR_"+fstr+"_dt"+str(dt)+pstr+"/" hisfile = "BHIS_INCIR_"+fstr+"_a"+str(a)+"_S"+str(S)+"_dt"+str(dt) binfile = "BHISBIN"+hisfile[4:] filepath = hisdir+hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins if ephi: np.savez(hisdir+binfile,rbins=rbins,erbins=erbins,epbins=epbins) else: np.savez(hisdir+binfile,rbins=rbins,erbins=erbins) ## ---------------------------------------------------------------- ## SIMULATION if vb: print me+"Computing",Nparticles,"trajectories. Arena: CIR. Force: "+str(ftype)+"." ## Precompute exp(-t) if a == 0: ## Not used in calculations expmt = None elif a <= 0.1: raise ValueError, me+"Must have alpha>0.1" else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10*a,dt,dt))/a) simulate_trajectory = lambda xyini, eIC, vb2:\ boundary_sim(xyini, eIC, a, xy_step, rmax, dt, tmax, expmt, ephi, vb2) ## ---------------------------------------------------------------- ## Initialise histogram in space H = np.zeros([b.size-1 for b in bins]) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for pini in pbins: ## Perform several runs in Cartesian coordinates xyini = [rini*np.cos(pini),rini*np.sin(pini)] if vb: print me+"Run",i,"of",Nparticles coords = simulate_trajectory(xyini, eIC[i], vb) H += np.histogramdd(coords,bins=bins,normed=False)[0] i += 1 ## Divide by bin area and number of particles binc = [np.diff(b) for b in bins] H /= reduce(np.multiply, np.ix_(*binc)) H /= Nparticles check_path(filepath, vb) save_data(filepath, H, vb) if vb: print me+"execution time",round(time.time()-t0,2),"seconds" return
def main(a,ftype,fpar,Nrun,dt,timefac,intmeth,ephi,vb): """ """ me = me0+".main: " t0 = time.time() ## ---------------------------------------------------------------- ## CHOOSE FORCE R, S, lam, nu = fpar if ftype == "const": force = lambda xy, r: force_const(xy,r,R) fstr = "C" fparstr = "" elif ftype == "lin": force = lambda xy, r: force_lin(xy,r,R) fstr = "L" fparstr = "" elif ftype == "lico": force = lambda xy, r: force_lico(xy,r,R,g) fstr = "LC" fparstr = "" elif ftype == "dcon": force = lambda xy, r: force_dcon(xy,r,R,S) fstr = "DC" fparstr = "_S"+str(S) elif ftype == "dlin": force = lambda xy, r: force_dlin(xy,r,R,S) fstr = "DL" fparstr = "_S"+str(S) elif ftype == "tan": force = lambda xy, r: force_tan(xy,r,R,lam) fstr = "T" fparstr = "_l"+str(lam) elif ftype == "dtan": force = lambda xy, r: force_dtan(xy,r,R,S,lam) fstr = "DT" fparstr = "_S"+str(S)+"_l"+str(lam) elif ftype == "nu": force = lambda xy, r: force_nu(xy,r,R,lam,nu) fstr = "N" fparstr = "_l"+str(lam)+"_n"+str(nu) elif ftype == "dnu": force = lambda xy, r: force_dnu(xy,r,R,S,lam,nu) fstr = "DN" fparstr = "_S"+str(S)+"_l"+str(lam)+"_n"+str(nu) else: raise IOError, me+"ftype must be one of {const, lin, lico, dcon, dlin, tan, dtan, nu}." dblpot = True if ftype[0] == "d" else False infpot = True if (ftype[-3:] == "tan" or ftype[-2:] == "nu") else False ## ---------------------------------------------------------------- ## SET UP CALCULATIONS ## Simulation time tmax = 5e2*timefac ## Simulation limits rmax = R+2.0*lam if infpot else R+6.0 # rmin = 0.0 #max([0.0, 0.9*R-5*np.sqrt(a)]) rmin = max(0.0, S-2.0*lam if infpot else S-4.0) ## Injection x coordinate -- half-way into bulk # rini = 0.5*(S+R) if dblpot else 0.5*(rmin+R) rini = np.sqrt(0.5*(S*S+R*R)) if dblpot else np.sqrt(0.5*(rmin*rmin+R*R)) if R==0.0: rini += 0.001 ## Limits for finite potential wb = [R+lam, (S>0.0)*(S-lam)] if infpot else [False, False] ## ------------ ## Bin edges rbins = calc_rbins(infpot,fpar,rmin,rmax) Nrbin = rbins.size - 1 Npbin = 50 pbins = np.linspace(0.0,2*np.pi,Npbin) ermax = 4/np.sqrt(a) if a!=0 else 4/np.sqrt(dt) Nerbin = 150 erbins = np.linspace(0.0,ermax,Nerbin+1) if ephi: # Nepbin = 50 # epbins = np.linspace(0.0,2*np.pi,Nepbin+1) # pstr = "_phi" Nepbin = 100 epbins = np.linspace(-2*np.pi,+2*np.pi,Nepbin+1) pstr = "_psi" bins = [rbins,erbins,epbins] else: pstr = "" bins = [rbins,erbins] ## ------------ ## Particles Nparticles = Npbin*Nrun ## Initial noise drawn from Gaussian if a == 0.0: eIC = np.sqrt(2/dt)*np.random.normal(0.0, 1.0, [Nparticles,2]) else: eIC = 1./np.sqrt(a)*np.random.normal(0.0, 1.0, [Nparticles,2]) ## Apply boundary conditions (should be integrated into force?) fxy = lambda xy, r: fxy_infpot(xy,r,force,wb[0],wb[1],dt) if infpot else force(xy,r) ## ---------------------------------------------------------------- ## Integration algorithm eul_step = lambda xy, r, exy: eul(xy, r, fxy, exy, dt) if intmeth == "rk4": xy_step = lambda xy, r, exy: RK4(xy, r, fxy, exy, dt, eul_step) intmeth = "_rk4" elif intmeth == "rk2": xy_step = lambda xy, r, exy: RK2(xy, r, fxy, exy, dt, eul_step) intmeth = "_rk2" else: xy_step = eul_step intmeth = "" ## ---------------------------------------------------------------- ## Filename; directory and file existence; readme hisdir = "Pressure/"+str(datetime.now().strftime("%y%m%d"))+\ "_POL_"+fstr+"_dt"+str(dt)+intmeth+pstr+"/" hisfile = "BHIS_POL_"+fstr+"_a"+str(a)+"_R"+str(R)+fparstr+"_dt"+str(dt)+intmeth binfile = "BHISBIN"+hisfile[4:] filepath = hisdir+hisfile check_path(filepath, vb) create_readme(filepath, vb) ## Save bins if ephi: np.savez(hisdir+binfile,rbins=rbins,erbins=erbins,epbins=epbins) else: np.savez(hisdir+binfile,rbins=rbins,erbins=erbins) ## ---------------------------------------------------------------- ## SIMULATION if vb: print me+"Computing",Nparticles,"trajectories. Arena: POL. Force: "+str(ftype)+"." ## Precompute exp(-t) if a == 0: ## Not used in calculations expmt = None # elif a <= 0.1: # ## a is small, and exponential is dominated by first term # if vb: print me+"rescaling time" # expmt = np.exp(np.arange(-10,0.1,0.1)) else: ## a is large enough that the exponential is well resolved. expmt = np.exp((np.arange(-10*a,dt,dt))/a) simulate_trajectory = lambda xyini, eIC, vb2:\ boundary_sim(xyini, eIC, a, xy_step, dt, tmax, expmt, ephi, vb2) ## ---------------------------------------------------------------- ## Initialise histogram in space H = np.zeros([b.size-1 for b in bins]) ## Counter for noise initial conditions i = 0 ## Loop over initial coordinates for pini in pbins: ## Perform several runs in Cartesian coordinates xyini = [rini*np.cos(pini),rini*np.sin(pini)] for run in xrange(Nrun): if vb: print me+"Run",i,"of",Nparticles coords = simulate_trajectory(xyini, eIC[i], (vb and run%50==0)) H += np.histogramdd(coords,bins=bins,normed=False)[0] i += 1 ## Divide by bin area and number of particles binc = [np.diff(b) for b in bins] H /= reduce(np.multiply, np.ix_(*binc)) H /= Nparticles check_path(filepath, vb) save_data(filepath, H, vb) if vb: print me+"execution time",round(time.time()-t0,2),"seconds" return