def main(fileList): """ This script shows how to make plots using the parseparticles module. usage: ./test.py particleFile1 [particleFile2 particleFile3 ... ] """ # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(fileList) # get just the particle objects particles = particlesDict.values() print "there are %d unique particles" % len(particles) # plots - this is based on the plotting done in the original # parseparticles.py script, which has since become a module for importing pylab.clf() # global domain extrema xmin = 1.e33 xmax = -1e33 ymin = 1.e33 ymax = -1.e33 # loop over the particle objects and create the particle paths for particle in particles: coords, time = particle.flatten() # find the extrema of the particles over time to make all frames the # same size xmin = min(xmin,min(coords[0,:])) xmax = max(xmax,max(coords[0,:])) ymin = min(ymin,min(coords[1,:])) ymax = max(ymax,max(coords[1,:])) pylab.scatter([coords[0,0]], [coords[1,0]], marker="x") pylab.plot(coords[0,:], coords[1,:]) pylab.xlabel("x") pylab.ylabel("y") pylab.savefig("particle_paths.png") # make an animation -- note: this assumes that all particles exist at all # timesteps # grab the total number of timesteps from the first particle's history totalSteps = len(particles[0].history)
def main(fileList): """ This script shows how to make plots using the parseparticles module. usage: ./test.py particleFile1 [particleFile2 particleFile3 ... ] """ # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(fileList) # get just the particle objects particles = particlesDict.values() print "there are %d unique particles" % len(particles) # plots - this is based on the plotting done in the original # parseparticles.py script, which has since become a module for importing pylab.clf() # global domain extrema xmin = 1.e33 xmax = -1e33 ymin = 1.e33 ymax = -1.e33 # loop over the particle objects and create the particle paths for particle in particles: coords, time = particle.flatten() # find the extrema of the particles over time to make all frames the # same size xmin = min(xmin, min(coords[0, :])) xmax = max(xmax, max(coords[0, :])) ymin = min(ymin, min(coords[1, :])) ymax = max(ymax, max(coords[1, :])) pylab.scatter([coords[0, 0]], [coords[1, 0]], marker="x") pylab.plot(coords[0, :], coords[1, :]) pylab.xlabel("x") pylab.ylabel("y") pylab.savefig("particle_paths.png") # make an animation -- note: this assumes that all particles exist at all # timesteps # grab the total number of timesteps from the first particle's history totalSteps = len(particles[0].history)
def main(files): # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) # make a plot of the particle paths pylab.clf() n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([coords[0, 0]], [coords[1, 0]], marker="x") pylab.plot(coords[0, :], coords[1, :]) n += 1 pylab.xlabel("x") pylab.ylabel("y") a = pylab.gca() a.set_aspect("equal") pylab.savefig("particle_paths.png") # get the index in the associate data corresponding to Mg24 and density # assume that all particles have the same data img24 = particles[0].getVarIndex("magnesium-24") idens = particles[0].getVarIndex("density") # make an animation -- note: this assumes that all particles exist # at all timesteps nstep = 0 while (nstep < len(particles[0].history)): pylab.clf() n = 0 while (n < len(particles)): # plot the position of the current particle (n) at the # current step (nstep). Color it by the abundance of # X(Mg24). XMg24_min = 1.e-6 XMg24_max = 1.e-5 pylab.scatter([particles[n].history[nstep].xyz[0]], [particles[n].history[nstep].xyz[1]], marker="o", s=1.0, c=math.log10( particles[n].history[nstep].data[img24] / particles[n].history[nstep].data[idens]), vmin=math.log10(XMg24_min), vmax=math.log10(XMg24_max), edgecolor="None") n += 1 # axis labels pylab.xlabel("x") pylab.ylabel("y") # color bar cb = pylab.colorbar(orientation="horizontal") cb.set_label(r"log$_{10}$[X($^{24}$Mg)]") pylab.axis([2.e7, 2.2e8, 4.e7, 1.2e8]) a = pylab.gca() a.set_aspect("equal") a.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) a.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) f = pylab.gcf() f.set_size_inches(8.0, 6.0) pylab.savefig("particles_%04d.png" % (nstep)) nstep += 1
def main(files): # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) # output particle instance info (debugging) #n = 0 #while (n < len(particles)): # print n # print particles[n] # print " " # n += 1 # dump out the data, particle-by-particle n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() dim = particles[n].dim # output to a flie of = open("particle_history.%03d" % (n), 'w') # header # first the particle id information idstuff = str(particles[n]) for line in idstuff.split("\n"): of.write("# %s\n" % (line)) # next the column labels if (dim == 1): of.write("# %20s %20s\n" % ("time", "x")) elif (dim == 2): of.write("# %20s %20s %20s\n" % ("time", "x", "y")) elif (dim == 3): of.write("# %20s %20s %20s %20s\n" % ("time", "x", "y", "z")) # t, x, [y, [z]] in columns i = 0 while (i < len(particles[n].history)): if (dim == 1): of.write(" %20.10f %20.10f\n" % (time[i], coords[0,i])) elif (dim == 2): of.write(" %20.10f %20.10f %20.10f\n" % (time[i], coords[0,i], coords[1,i])) elif (dim == 3): of.write(" %20.10f %20.10f %20.10f %20.10f\n" % (time[i], coords[0,i], coords[1,i], coords[2,i])) i += 1 of.close() n += 1
def main(files): # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) if (not particles[0].dim == 2): print "ERROR: ony 2-d supported\n" sys.exit(-1) # maxdx is the maximum distance moved by a particle in a single # timestep maxdx = numpy.zeros((len(particles)), dtype=numpy.float64) maxdx[:] = -1.e33 # dump out the data, particle-by-particle n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() i = 1 while (i < len(particles[n].history)): xo = coords[0, i - 1] xn = coords[0, i] yo = coords[1, i - 1] yn = coords[1, i] # compute distance -- check for periodic BCs if (xo > xmax - dx and xn < xmin + dx): # we flowed through right BC d = math.sqrt((xn - (xo - xmax))**2 + (yn - yo)**2) elif (xo < xmin + dx and xn > xmax - dx): # we flowed through the left BC d = math.sqrt((xn - (xo + xmax))**2 + (yn - yo)**2) else: # compute the distance d = math.sqrt((xn - xo)**2 + (yn - yo)**2) # store maximum maxdx[n] = max(maxdx[n], d) i += 1 n += 1 # output to a flie of = open("particle_maxdx.out", 'w') n = 0 of.write("# particle, max{d}, max{d}/dx\n") while (n < len(particles)): of.write("%d %f %f\n" % (n, maxdx[n], maxdx[n] / dx)) n += 1 of.close()
def main(files): # get start time for timing the program starttime = tm.time() # to allow for an inputs file of variables # dict to hold all variables variables = {} # to run the script in the background set flag to true and use: # nohup plotparticles.py timestamp_* & variables['run_in_background'] = True # use plotsinglevar_parts.py to plot particles on top of another # variable: variables['component_plots'] = True # 1st and 2nd components to plot using plotsinglevar_parts.py: variables['component_one'] = "tfromp" variables['component_two'] = "" # what kind of movie to make mp4 or avi: variables['movie'] = "mp4" # save plots as an eps: variables['eps'] = False # compute turnover time of particles: variables['turnover_time'] = False # number of grids to use in calculating the turnover time variables['num_grids'] = "3" # make animations in time variables['animation'] = False # graph positions variables['position_plots'] = False # what to graph variables['y_vs_x'] = True variables['y_vs_t'] = False variables['x_vs_t'] = False # what fraction of the particles to plot: # program will plot frac*(num particles) variables['frac'] = "0.1" # if not running in background ask whether or not # to read variable values from an inputs file if variables['run_in_background']: read_input_file = 'n' else: read_input_file = raw_input("\nRead Variables From Input File?: ") if (read_input_file[0] == 'y' or read_input_file[0] == 'Y'): filename = raw_input("\n Input Filename: ") # read variables from input file read_from_input_file(variables, filename) # maximum value of frac is 1.0 frac = min(1.0, float(variables['frac'])) print "\nCounting Particles:" # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() print " Unique Particles:", len(particles) print " Only Using:", int(frac * len(particles)) # output directory for all images outputDir = "Images" # current directory topDir = os.getcwd() # create outputDir if it does not exist already if (not os.path.isdir(outputDir)): os.mkdir(outputDir) # clear plot pylab.clf() # global domain extrema xmin = 1.e33 xmax = -1e33 ymin = 1.e33 ymax = -1.e33 # Find xmin,xmax,ymin,ymax for all particles and # take into account the periodic boundary conditions in x direction # this causes lines when the particle jumps from one edge of the # domain to the other. loop through the particles and find where # the x jumps a significant amount and split the x plots according to # these positions. x_jump_indices = {} n = 0 while (n < int(frac * len(particles))): # get numpy arrays containing the time and coordinate # information for particle n coords, time = particles[n].flatten() # find the extrema of the particles over time to make all frames the # same size xmin = min(xmin, min(coords[0, :])) xmax = max(xmax, max(coords[0, :])) ymin = min(ymin, min(coords[1, :])) ymax = max(ymax, max(coords[1, :])) # reset indices list indices = [] i = 0 while (i < len(coords[0, :]) - 1): if (abs(coords[0, i] - coords[0, i + 1]) >= 1.0e7): # holds the index where a jump across the boundary occurs indices.append(i) i += 1 # each particle has a list of indices where # it crosses the boundary x_jump_indices[n] = indices n += 1 # COMPONENT PLOTS: plot particle data on top of another variable or # on top of two variables using the plotsinglevar_plots.py routine if (variables['component_plots']): print "Plotting Particles and Components:" print "\n Plotfiles from ./plt*" # IDEA: # -loop over all pltfiles and get the time of each plotfile # -plot the component data from the pltfile # -overplot the particle data from that time # list to hold all the pltfile directories pltFiles = [] # find all directories with name plt????? for (path, dirs, files) in os.walk(topDir): for directory in dirs: index = string.find(directory, "plt") if not (index < 0): pltFiles.append(directory) # dictionary to hold time of each plot file pltFile_times = {} # get time for each plot file for plotfile in pltFiles: time_pltfile = [] # use fsnapshot routine to find time of plotfile plot_file_time = \ plotsinglevar_parts.fsnapshot.fplotfile_get_time(plotfile) # use a list so it can hold both the time and the index of that time time_pltfile.append(plot_file_time) pltFile_times[plotfile] = time_pltfile # return time and coord data for particle 0 coords, time = particles[0].flatten() # the time from particles[0].flatten() do not exactly match (to # machine precision) the times from the fsnapshot fortran routine. # use a tolerance to find the index associated to the time time_tol = 1.0e-4 nstep = 0 while (nstep < len(time[:])): # loop over all plotfiles for key, value in sorted(pltFile_times.iteritems()): # compare the times if (abs(time[nstep] - pltFile_times[key][0]) < time_tol): # attach the time index to the dictionary pltFile_times[key].append(nstep) nstep += 1 # component data and particle data will be plotted using # plotsinglevar_parts.py (which uses fsnapshot) count = 1 for plotfile, value in sorted(pltFile_times.iteritems()): pylab.clf() # generate a specific output name for the image if (variables['component_two'] != ""): outFile = variables['component_one'] + str("_") + \ variables['component_two'] + str("_particles_") + \ plotfile else: outFile = variables['component_one'] + str("_particles_") + \ plotfile if (variables['eps']): outFile += ".eps" eps_send = 1 else: outFile += ".png" eps_send = 0 # plot component data # calling sequence: plotfile, comp1, comp2, outfile, log, # minval, maxval, minval2, maxval2, eps, # dpi, origin, annotation, particles, time_ind plotsinglevar_parts.do_plot(plotfile, variables['component_one'], variables['component_two'], outputDir + "/" + outFile, 0, None, None, None, None, eps_send, 100, 0, "", particles, pltFile_times[plotfile][1]) if (count % 5 == 0): print " plotfile: "+str(count)+\ " of "+str(len(pltFile_times)) count += 1 # # Animate the particle positions: # # output file to contain list of all image files list_name = "list.txt" if os.path.isfile(list_name): os.remove(list_name) if (variables['eps']): os.system("ls -v "+outputDir+"/"+\ variables['component_one']+"*_particles_*.eps > "+list_name) else: os.system("ls -v "+outputDir+"/"+\ variables['component_one']+"*_particles_*.png > "+list_name) # use mencoder to generate an animation from the list fps = "15" # frames per sec # mencoder cannot handle eps files if not (variables['eps']): if (variables['movie'] == "mp4"): movie_output3 = "Component-Animation.mp4" if os.path.isfile(movie_output3): os.remove(movie_output3) string1 = "mencoder mf://@" + list_name + " -o " + movie_output3 string2 = " -of lavf -lavfopts format=mp4 -ss 1 -ovc " string3 = "x264 -x264encopts " string4 = "crf=20.0:nocabac:level_idc=30:" string5 = "global_header:threads=2 -fps " + fps os.system(string1 + string2 + string3 + string4 + string5) elif (variables['movie'] == "avi"): movie_output3 = "Component-Animation.avi" if os.path.isfile(movie_output3): os.remove(movie_output3) string1 = "mencoder mf://@" + list_name + " -o " + movie_output3 string2 = " -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=3000" string3 = ":vhq -mf type=png -fps " + fps os.system(string1 + string2 + string3) else: print "\n---ERROR: unknown movie format---\n" os.remove(list_name) else: print "\n---ERROR: mencoder cannot animate eps images---" print " list of image files: " + list_name + "\n" # TURNOVER TIME for all particles if (variables['turnover_time']): print "Calculate Turnover Time:" print " Only Using: " + str(len(particles)) # divide domain into how many grids num_grids = int(variables['num_grids']) # complete cycle up means go from y_bot to y_top y_top = float(num_grids - 1) / float(num_grids) * (ymax - ymin) + ymin y_bot = (ymax - ymin) / float(num_grids) + ymin # dictionaries/lists to hold data turnover_up_times = {} # up data of each particle turnover_down_times = {} # down data of each particle counts = {} tot_up_count = 0 tot_down_count = 0 # put all up data in one list and all down data in one list up_data_all_particles = [] down_data_all_particles = [] # loop over each particle n = 0 while (n < len(particles)): # coordinate and time info for nth particle coords, time = particles[n].flatten() # reset various counters and lists time_in_bot = 0 time_in_top = 0 flag_bot = 0 flag_top = 0 up_count = 0 down_count = 0 count = [] up_period = [] down_period = [] # sometimes len(coords[1,:]) is less # than len(particles[0].history) if (len(coords[1, :]) == len(particles[0].history)): loop_max = len(particles[0].history) else: loop_max = len(coords[1, :]) # loop over time nstep = 0 while (nstep < loop_max): # particle is below y_bot if (coords[1, nstep] < y_bot): flag_bot = 1 time_in_bot = nstep # particle is above y_top if (coords[1, nstep] > y_top): flag_top = 1 time_in_top = nstep # particle has made journey either # top --> bottom or bottom --> top if ((flag_bot == 1) and (flag_top == 1)): # particle went down if (time_in_top < time_in_bot): down = time[time_in_bot] - time[time_in_top] down_period.append(down) down_data_all_particles.append(down) flag_top = 0 down_count += 1 tot_down_count += 1 # particle went up else: up = time[time_in_top] - time[time_in_bot] up_period.append(up) up_data_all_particles.append(up) flag_bot = 0 up_count += 1 tot_up_count += 1 nstep += 1 # debugging: count.append(up_count) count.append(down_count) counts[n] = count turnover_up_times[n] = up_period turnover_down_times[n] = down_period n += 1 # sanity check/debugging purposes: # n=0 # while n < len(particles): # if (len(turnover_up_times[n][:]) != counts[n][0]): # print "---ERROR: Up counts differ for particle "+str(n) # if (len(turnover_down_times[n][:]) != counts[n][1]): # print "---ERROR: Down counts differ for particle "+str(n) # n += 1 #-------------------------------------------------------- # Generate Histograms #-------------------------------------------------------- print "Generating Histograms:" average_up_time = sum(up_data_all_particles)/ \ float(len(up_data_all_particles)) average_down_time = sum(down_data_all_particles)/ \ float(len(down_data_all_particles)) turnover_time = average_up_time + average_down_time # individual histograms # (one histogram per particle) n = 0 while (n < len(particles)): pylab.clf() pylab.hist([turnover_up_times[n][:], turnover_down_times[n][:]], bins=50, range=[0, 250], color=['b', 'r'], label=['Up', 'Down'], histtype='bar') pylab.legend() pylab.ylabel('Number') pylab.xlabel('Bins of 5 sec') pylab.figtext(0.65, 0.73, 'Number of Grids: ' + str(num_grids)) pylab.title('Histogram - Up/Down Time for Particle ' + str(n)) outFile = outputDir + "/hist_part_%04d_grids_" % n + str(num_grids) if (variables['eps']): outFile += ".eps" else: outFile += ".png" pylab.savefig(outFile) n += 1 # all particles on one histogram pylab.clf() pylab.hist([up_data_all_particles, down_data_all_particles], bins=50, range=[0, 250], color=['b', 'r'], label=['Up', 'Down'], histtype='bar') pylab.legend() pylab.ylabel('Number') pylab.xlabel('Bins of 5 sec') pylab.figtext(0.65, 0.73, 'Number of Grids: ' + str(num_grids)) pylab.figtext( 0.6, 0.69, "Avg Turnover: " + str(int(1000 * turnover_time) / 1000.) + " sec") pylab.title('Histogram - Up/Down Time for All Particles') outFile = outputDir + "/histogram_grids_" + str(num_grids) if (variables['eps']): outFile += ".eps" else: outFile += ".png" pylab.savefig(outFile) print print " Histogram Results:" print " Number of Grids: " + str(num_grids) print " Avg Up Time : " + str(average_up_time) + " sec" print " Avg Down Time: " + str(average_down_time) + " sec" print print " Avg Turnover Time: " + str(turnover_time) + " sec" #-------------------------------------------------------- # ANIMATION of positions # # These animations will plot the entire path of a particle each frame. # Every frame, an entire path is added to the picture. The first # frame has one particles path, the second frame has two particle # paths and the last frame has all particle paths. # #-------------------------------------------------------- if (variables['position_plots']): # give value to nstep inorder to call plotting routine # it has no other use in this loop nstep = 2 print "Creating Images:" # plot x vs. y if (variables['y_vs_x']): print " plotting y vs x" n = 0 while (n < int(frac * len(particles))): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([coords[0, 0]], [coords[1, 0]], marker="x") # call plotting routine plotting(variables, x_jump_indices, n, nstep, time, coords) pylab.xlabel("x") pylab.ylabel("y") pylab.axis([xmin, xmax, ymin, ymax]) outFile = "particle_paths_%04d" % n if (variables['eps']): outFile += ".eps" else: outFile += ".png" if (os.path.isfile(outputDir + "/" + outFile)): os.remove(outputDir + "/" + outFile) pylab.savefig(outputDir + "/" + outFile) n += 1 # plot y vs time elif (variables['y_vs_t']): print " plotting y vs t" n = 0 while (n < int(frac * len(particles))): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([time[0]], [coords[1, 0]], marker="x") # call plotting routine plotting(variables, x_jump_indices, n, nstep, time, coords) pylab.xlabel("time") pylab.ylabel("y") pylab.axis([min(time[:]), max(time[:]), ymin, ymax]) outFile = "particle_paths_%04d" % n if (variables['eps']): outFile += ".eps" else: outFile += ".png" if (os.path.isfile(outputDir + "/" + outFile)): os.remove(outputDir + "/" + outFile) pylab.savefig(outputDir + "/" + outFile) n += 1 # plot x vs time elif (variables['x_vs_t']): print " plotting x vs t" n = 0 while (n < int(frac * len(particles))): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([time[0]], [coords[0, 0]], marker="x") # call plotting routine plotting(variables, x_jump_indices, n, nstep, time, coords) pylab.xlabel("time") pylab.ylabel("x") pylab.axis([min(time[:]), max(time[:]), xmin, xmax]) output = "particle_paths_%04d" % n if (variables['eps']): output += ".eps" else: output += ".png" if (os.path.isfile(outputDir + "/" + output)): os.remove(outputDir + "/" + output) pylab.savefig(outputDir + "/" + output) n += 1 # output a file containing all images list_name = "list.txt" if os.path.isfile(list_name): os.remove(list_name) if (variables['eps']): os.system("ls -v " + outputDir + "/particle_paths_*.eps > " + list_name) else: os.system("ls -v " + outputDir + "/particle_paths_*.png > " + list_name) # use mencoder to generate an animation from the list fps = "5" # frames per sec # mencoder cannot handle eps files if not (variables['eps']): if (variables['movie'] == "mp4"): movie_output2 = "Position-Animation.mp4" if os.path.isfile(movie_output2): os.remove(movie_output2) string1 = "mencoder mf://@" + list_name + " -o " + movie_output2 string2 = " -of lavf -lavfopts format=mp4 -ss 1 -ovc " string3 = "x264 -x264encopts " string4 = "crf=20.0:nocabac:level_idc=30:" string5 = "global_header:threads=2 -fps " + fps os.system(string1 + string2 + string3 + string4 + string5) elif (variables['movie'] == "avi"): movie_output2 = "Position-Animation.avi" if os.path.isfile(movie_output2): os.remove(movie_output2) string1 = "mencoder mf://@" + list_name + " -o " + movie_output2 string2 = " -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=3000" string3 = ":vhq -mf type=png -fps " + fps os.system(string1 + string2 + string3) else: print "\n---ERROR: unknown movie format---\n" os.remove(list_name) else: print "\n---ERROR: mencoder cannot animate eps images---" print " list of image files: " + list_name + "\n" # ANIMATION in time if (variables['animation']): print "Creating Animation:" if (variables['y_vs_x']): print " animating y vs x" elif (variables['y_vs_t']): print " animating y vs t" elif (variables['x_vs_t']): print " animating x vs t" # make an animation -- note: this assumes that all particles exist # at all timesteps print " Total timesteps: " + str(len(particles[0].history)) nstep = 0 while (nstep < len(particles[0].history)): pylab.clf() n = 0 while (n < int(frac * len(particles))): # get numpy arrays containing the time and coordinate # information for particle n coords, time = particles[n].flatten() # label particle position with an "x" pylab.scatter([particles[n].history[nstep].xyz[0]], [particles[n].history[nstep].xyz[1]], marker="x") # call plotting routine plotting(variables, x_jump_indices, n, nstep, time, coords) n += 1 # axis labels if (variables['y_vs_x']): pylab.xlabel("x") pylab.ylabel("y") pylab.axis([xmin, xmax, ymin, ymax]) elif (variables['x_vs_t']): pylab.xlabel("time") pylab.ylabel("x") pylab.axis([min(time[0:]), max(time[0:]), xmin, xmax]) elif (variables['y_vs_t']): pylab.xlabel("time") pylab.ylabel("y") pylab.axis([min(time[0:]), max(time[0:]), ymin, ymax]) output = "particles_%04d" % nstep if (variables['eps']): output += ".eps" else: output += ".png" if (os.path.isfile(outputDir + "/" + output)): os.remove(outputDir + "/" + output) # one image per timestep pylab.savefig(outputDir + "/" + output) # update time: if nstep % 500 == 0: print " nstep = " + str(nstep) nstep += 100 if (variables['eps']): os.system("ls -v " + outputDir + "/particles_*.eps > " + list_name) else: os.system("ls -v " + outputDir + "/particles_*.png > " + list_name) # output a list of particle files list_name = "list.txt" if os.path.isfile(list_name): os.remove(list_name) # use mencoder to generate an animation from the list fps = "5" # frames per sec # mencoder cannot handle eps files if not (variables['eps']): if (variables['movie'] == "mp4"): movie_output2 = "Time-Animation.mp4" if os.path.isfile(movie_output2): os.remove(movie_output2) string1 = "mencoder mf://@" + list_name + " -o " + movie_output2 string2 = " -of lavf -lavfopts format=mp4 -ss 1 -ovc " string3 = "x264 -x264encopts " string4 = "crf=20.0:nocabac:level_idc=30:" string5 = "global_header:threads=2 -fps " + fps os.system(string1 + string2 + string3 + string4 + string5) elif (variables['movie'] == "avi"): movie_output2 = "Time-Animation.avi" if os.path.isfile(movie_output2): os.remove(movie_output2) string1 = "mencoder mf://@" + list_name + " -o " + movie_output2 string2 = " -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=3000" string3 = ":vhq -mf type=png -fps " + fps os.system(string1 + string2 + string3) else: print "\n---ERROR: unknown movie format---\n" os.remove(list_name) else: print "\n---ERROR: mencoder cannot animate eps images---" print " list of image files: " + list_name + "\n" # get stop time stoptime = tm.time() duration = (stoptime - starttime) / 60.0 duration = int(1000 * duration) / 1000.0 print "--------------------------------------------" if (variables['component_plots']): print "\nCreated Animation: " + movie_output3 if (variables['position_plots']): print "\nCreated Animation: " + movie_output2 if (variables['animation']): print "\nCreated Animation: " + movie_output print "\nImages are located in: " + outputDir + "/" print "\nProgram Duration: " + str(duration) + " min" print "\n---Complete---\n"
def main(files): # domain information xcenter = 0.5 ycenter = 0.5 # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) # make a plot of the particle paths pylab.clf() n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([coords[0, 0]], [coords[1, 0]], marker="x") pylab.plot(coords[0, :], coords[1, :]) n += 1 pylab.xlabel("x") pylab.ylabel("y") pylab.axis([0, 1, 0, 1]) a = pylab.gca() a.set_aspect("equal") pylab.savefig("particle_paths.png") # compute the relative change in the radius of the particle from # start to end -- this is the error in the integration. pylab.clf() # assume that all particles were around for all timesteps nstep = len(particles[0].history) error = numpy.zeros(len(particles), dtype=numpy.float64) n = 0 while (n < len(particles)): # compute the relative change in radius r_start = math.sqrt((particles[n].history[0].xyz[0] - xcenter)**2 + (particles[n].history[0].xyz[1] - ycenter)**2) r_end = math.sqrt( (particles[n].history[nstep - 1].xyz[0] - xcenter)**2 + (particles[n].history[nstep - 1].xyz[1] - ycenter)**2) error[n] = math.fabs(r_start - r_end) / r_start print "particle: (%d, %d), r init = %f, rel error = %g" % \ (particles[n].pid, particles[n].originCPU, r_start, error[n]) n += 1 print " " print "maximum error = ", max(error) print "L2 norm error = ", math.sqrt( numpy.dot(error, error) / len(particles))
def main(files): # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) if (not particles[0].dim == 2): print "ERROR: ony 2-d supported\n" sys.exit(-1) # maxdx is the maximum distance moved by a particle in a single # timestep maxdx = numpy.zeros((len(particles)), dtype=numpy.float64) maxdx[:] = -1.e33 # dump out the data, particle-by-particle n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() i = 1 while (i < len(particles[n].history)): xo = coords[0,i-1] xn = coords[0,i] yo = coords[1,i-1] yn = coords[1,i] # compute distance -- check for periodic BCs if (xo > xmax-dx and xn < xmin+dx): # we flowed through right BC d = math.sqrt((xn - (xo - xmax))**2 + (yn - yo)**2) elif (xo < xmin+dx and xn > xmax-dx): # we flowed through the left BC d = math.sqrt((xn - (xo + xmax))**2 + (yn - yo)**2) else: # compute the distance d = math.sqrt((xn - xo)**2 + (yn - yo)**2) # store maximum maxdx[n] = max(maxdx[n],d) i += 1 n += 1 # output to a flie of = open("particle_maxdx.out", 'w') n = 0 of.write("# particle, max{d}, max{d}/dx\n") while (n < len(particles)): of.write("%d %f %f\n" % (n, maxdx[n], maxdx[n]/dx)) n += 1 of.close()
def main(files): # domain information xcenter = 0.5 ycenter = 0.5 # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) # make a plot of the particle paths pylab.clf() n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([coords[0,0]], [coords[1,0]], marker="x") pylab.plot(coords[0,:], coords[1,:]) n += 1 pylab.xlabel("x") pylab.ylabel("y") pylab.axis([0,1,0,1]) a = pylab.gca() a.set_aspect("equal") pylab.savefig("particle_paths.png") # compute the relative change in the radius of the particle from # start to end -- this is the error in the integration. pylab.clf() # assume that all particles were around for all timesteps nstep = len(particles[0].history) error = numpy.zeros(len(particles), dtype=numpy.float64) n = 0 while (n < len(particles)): # compute the relative change in radius r_start = math.sqrt( (particles[n].history[0].xyz[0] - xcenter)**2 + (particles[n].history[0].xyz[1] - ycenter)**2) r_end = math.sqrt( (particles[n].history[nstep-1].xyz[0] - xcenter)**2 + (particles[n].history[nstep-1].xyz[1] - ycenter)**2) error[n] = math.fabs(r_start - r_end)/r_start print "particle: (%d, %d), r init = %f, rel error = %g" % \ (particles[n].pid, particles[n].originCPU, r_start, error[n]) n += 1 print " " print "maximum error = ", max(error) print "L2 norm error = ", math.sqrt(numpy.dot(error,error)/len(particles))
def main(files): # this returns a dict whose keys are a unique identifier (based on # id and CPU) and values are the actual particle objects particlesDict = parseparticles.parseParticleFile(files) # get just the particle objects particles = particlesDict.values() # print out some info and test the routines print "number of unique particles = ", len(particles) # make a plot of the particle paths pylab.clf() n = 0 while (n < len(particles)): # get numpy arrays containing the time and coordinate # information for particle 0 coords, time = particles[n].flatten() pylab.scatter([coords[0,0]], [coords[1,0]], marker="x") pylab.plot(coords[0,:], coords[1,:]) n += 1 pylab.xlabel("x") pylab.ylabel("y") a = pylab.gca() a.set_aspect("equal") pylab.savefig("particle_paths.png") # get the index in the associate data corresponding to Mg24 and density # assume that all particles have the same data img24 = particles[0].getVarIndex("magnesium-24") idens = particles[0].getVarIndex("density") # make an animation -- note: this assumes that all particles exist # at all timesteps nstep = 0 while (nstep < len(particles[0].history)): pylab.clf() n = 0 while (n < len(particles)): # plot the position of the current particle (n) at the # current step (nstep). Color it by the abundance of # X(Mg24). XMg24_min = 1.e-6 XMg24_max = 1.e-5 pylab.scatter([particles[n].history[nstep].xyz[0]], [particles[n].history[nstep].xyz[1]] , marker="o", s=1.0, c=math.log10(particles[n].history[nstep].data[img24]/ particles[n].history[nstep].data[idens]), vmin=math.log10(XMg24_min), vmax=math.log10(XMg24_max), edgecolor="None") n += 1 # axis labels pylab.xlabel("x") pylab.ylabel("y") # color bar cb = pylab.colorbar(orientation="horizontal") cb.set_label(r"log$_{10}$[X($^{24}$Mg)]") pylab.axis([2.e7,2.2e8,4.e7,1.2e8]) a = pylab.gca() a.set_aspect("equal") a.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) a.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) f = pylab.gcf() f.set_size_inches(8.0,6.0) pylab.savefig("particles_%04d.png" % (nstep) ) nstep += 1