示例#1
0
def simulate():
    """
    Run several Ising model simulations with different temperatures.
    Store them in 1 hdf5 file.

    """
    temperatures = numpy.linspace(args.tmin, args.tmax, args.steps)

    with HDF5Handler(args.filename) as handler:
        for index, T in enumerate(temperatures):
            h5path = "/"+"sim_"+str(index).zfill(4)+"/"
            # h5path thus looks like:
            # "/sim_0000/", "/sim_0001/", etc.
            handler.prefix = h5path

            i = Ising(args.shape, args.sweeps, temperature=T, handler=handler,
                      aligned=args.aligned, mode=args.algorithm,
                      saveinterval=args.saveinterval, skip_n_steps=args.skip)

            if args.verbose:
                i.print_sim_parameters()

            widgets=drawwidget("  T = {}  {}/{} ".format(round(T,4), index+1,
                                                         len(temperatures)))
            pbar = pb.ProgressBar(widgets=widgets, maxval=args.sweeps).start()
            i.evolve(pbar)
            pbar.finish()

            handler.file.flush()
示例#2
0
def worker(tasks_queue, done_queue):
    """
    Pulls a task from the task queue and initiates the ising model
    simulation.

    Ising.evolve() is run within the context of HDF5Handler so a
    handler can be passed to Ising object. The HDF5Handler context
    block is run within the context of Pbar to track the progress
    of the simulation.
    """

    for task, hash_ in iter(tasks_queue.get, 'STOP'):
        process_id = int((mp.current_process().name)[-1]) #find nicer way
        writer = Writer((0, process_id), TERM)

        with Pbar(task, writer) as bar:
            with HDF5Handler(filename=ARGS.tempdir+'/'+hash_+'.hdf5') as h:
                time_start = time.time()
                isingsim = Ising(shape=task['shape'], sweeps=task['mcs'],
                                 temperature=task['temperature'],
                                 aligned=task['aligned'],
                                 algorithm=task['algorithm'], handler=h,
                                 saveinterval=task['saveinterval'],
                                 skip_n_steps=task['skip_n_steps'])
                isingsim.evolve(pbar=bar)
                runtime = round(time.time() - time_start, 2)


        subs = {'temp'     : task["temperature"],
                'shape'    : task['shape'],
                'algo'     : task['algorithm'],
                'aligned'  : task['aligned'],
                'mcs'      : task['mcs'],
                'runtime'  : runtime,
                'timestamp': time.strftime("%d %b %Y %H:%M:%S")
                }

        s = "T={temp:.3f}  {shape}  {algo}  {aligned}  {mcs} {runtime:.2f}  {timestamp} "
        job_report = s.format(**subs)

        logging.info(job_report)
        done_queue.put(job_report)