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 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 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 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()