def loadsim(run_id, fileformat=None, buffer_flag='cache'): '''Given the run_id of a simulation, reads it from the disk. Returns the newly created simulation object. Required arguments: run_id : Simulation run identification string. Optional qrguments: fileformat : Format of all snapshot files of simulation. buffer_flag : Record snapshot data in simulation buffer. ''' SimBuffer.loadsim(run_id, fileformat=fileformat, buffer_flag=buffer_flag) return SimBuffer.get_current_sim()
def run(no=None): '''Run a simulation. If no argument is given, run the current one; otherwise queries the buffer for the given simulation number. If the simulation has not been setup, does it before running. Optional arguments: no : Simulation number ''' #gets the correct simulation object from the buffer try: if no is None: sim = SimBuffer.get_current_sim() else: no = int(no) sim = SimBuffer.get_sim_no(no) except BufferError as e: handle(e) #setup the simulation if not sim.setup: sim.SetupSimulation() SimBuffer.load_live_snapshot(sim) while sim.t < sim.tend and sim.Nsteps < sim.Nstepsmax: #TODO: maybe some of these operations could be done in another thread, so that the computation is #not slowed down when compared to the stand-alone c++ executable #But need to think carefully, because of the GIL... (??) snap_list = sim.InteractiveRun() for snap in snap_list: SimBuffer.add_snapshot(snap, sim) SimBuffer.load_live_snapshot(sim) update("live")
def setupsim(): '''Set up the current simulation object. Note that after calling this function, no parameter change it\'s possible. ''' sim = SimBuffer.get_current_sim() sim.SetupSimulation() sim.simparams.RecordParametersToFile()
def make_movie(filename, snapshots='all', window_no=0, fps=24): '''Generates movie for plots generated in given window''' # Remove all temporary files in the directory (in case they still exist) tmpfilelist = glob.glob('tmp.?????.png') for file in tmpfilelist: os.remove(file) sim = SimBuffer.get_current_sim() nframes = len(sim.snapshots) # Loop through all snapshots and create temporary images if snapshots == 'all': for isnap in range(len(sim.snapshots)): snap(isnap) tmpfile = 'tmp.' + str(isnap).zfill(5) + '.png' savefig(tmpfile) # Wait until all plotting processes have finished before making mp4 file Singletons.free.wait() # Now join all temporary files together with ffmpeg subprocess.call(["ffmpeg","-y","-r",str(fps),"-i", "tmp.%05d.png", \ "-vcodec","mpeg4", "-qscale","5", "-r", str(fps), \ filename]) # Now remove all temporary files just created to make movie tmpfilelist = glob.glob('tmp.?????.png') for file in tmpfilelist: os.remove(file)
def newsim(paramfile=None, ndim=None, sim=None): '''Create a new simulation object. Need to specify either the parameter file, or the number of dimensions and the simulation type. Note that it is not possible to change the number of dimensions afterwards or simulation type afterwards. ''' return SimBuffer.newsim(paramfile=paramfile, ndim=ndim, simtype=sim)
def previous(): '''Decrements the current snapshot of the current simulation. Return the new snapshot, or None if the call failed.''' try: snapshot=snap(SimBuffer.get_no_previous_snapshot()) return snapshot except BufferException as e: handle(e)
def next(): '''Advances the current snapshot of the current simulation. Return the new snapshot, or None if the call failed.''' try: snapshot=snap(SimBuffer.get_no_next_snapshot()) return snapshot except BufferException as e: handle(e)
def plot(x, y, type="default", snap="current", sim="current", overplot=False, autoscale=False, xunit="default", yunit="default", xaxis="linear", yaxis="linear", **kwargs): '''Plot particle data as a scatter plot. Creates a new plotting window if one does not already exist. Required arguments: x : Quantity on the x-axis. Must be a string. y : Quantity on the y-axis. Must be a string. Optional arguments: type : The type of the particles to plot (e.g. 'star' or 'sph'). snap : Number of the snapshot to plot. Defaults to 'current'. sim : Number of the simulation to plot. Defaults to 'current'. overplot : If True, overplots on the previous existing plot rather than deleting it. Defaults to False. autoscale : If True, the limits of the plot are set automatically. Can also be set to 'x' or 'y' to specify that only one of the axis has to use autoscaling. If False (default), autoscaling is not used. On an axis that does not have autoscaling turned on, global limits are used if defined for the plotted quantity. xunit : Specify the unit to use for the plotting for the quantity on the x-axis. yunit : Specify the unit to use for the plotting for the quantity on the y-axis. **kwargs : Extra keyword arguments will be passed to matplotlib. ''' simno = get_sim_no(sim) overplot=to_bool(overplot) # If we are plotting all particle species, call plot in turn if type=="all": sim = SimBuffer.get_sim_no(simno) snapobject = SimBuffer.get_snapshot_extended(sim, snap) nspecies = snapobject.GetNTypes() for ispecies in range(nspecies): plot(x,y,snapobject.GetSpecies(ispecies),snap,simno, (overplot or ispecies>0),autoscale,xunit,yunit, xaxis,yaxis,**kwargs) return command = Commands.ParticlePlotCommand(x, y, type, snap, simno, overplot, autoscale, xunit, yunit, xaxis, yaxis, **kwargs) data = command.prepareData(Singletons.globallimits) Singletons.queue.put([command, data]) sleep(0.001)
def set_current_sim(simno): '''Set the current simulation to the given number. Returns the newly set current simulation. Required argument: simno : Simulation number ''' simno = int(simno) return SimBuffer.set_current_sim_no(simno)
def get_sim_no(sim): '''Returns the simulation id of the currently active simulation object Required argument: sim : Simulation ''' if sim == "current": simno = SimBuffer.get_current_sim_no() else: simno = int(sim) return simno
def get_data(quantity, snap="current",type="default",sim="current",unit="default" ): '''Returns the array with the data for the given quantity. The data is returned scaled to the specified unit Required argument: quantity :The quantity required. Must be a string Optional arguments: type :The type of the particles (e.g. 'star') snap :Number of the snapshot. Defaults to 'current' sim :Number of the simulation. Defaults to 'current' unit :Specifies the unit to use to return the data ''' simno = get_sim_no(sim) sim = SimBuffer.get_sim_no(simno) snapobject = SimBuffer.get_snapshot_extended(sim, snap) nspecies = snapobject.GetNTypes() if type=="all": raise Exception("You requested all particle types to get_data, but we can return only one array!") fetcher=UserQuantity(quantity) unitinfo,data,scaling,label=fetcher.fetch(type=type,snap=snapobject,unit=unit) return data*scaling
def snap(no): '''Jump to the given snapshot number of the current simulation. Note that you can use standard Numpy index notation (e.g., -1 is the last snapshot). Return the new snapshot, or None if the call failed. Required arguments: snapno : Snapshot number ''' no = int(no) snapshot=None try: snapshot=SimBuffer.set_current_snapshot_number(no) except BufferException as e: handle(e) if snapshot is not None: update("current") return snapshot
def snaps(simno): '''For the given simulation number, print a list of all the snapshots Required argument: simno : Simulation number from which to print the snapshot list. ''' simno = int(simno) sim = SimBuffer.get_sim_no(simno) print "The run_id of the requested simulation is " + sim.simparams.stringparams["run_id"] print "These are the snapshots that we know about for this simulation:" for num, snap in enumerate(sim.snapshots): #TODO: snap.t is set correctly only the first time that the snapshot is read from the disc, should be fixed print str(num) + ' ' + snap.filename + " " + str(snap.t) try: live = None live = sim.live except AttributeError: pass if live is not None: print "In addition, there is a live snapshot in memory, at time " + str(live.t)