def init(self, continue_run=None, init_walkers=None, **kwargs): """ Parameters ---------- continue_run : (Default value = None) init_walkers : (Default value = None) **kwargs : Returns ------- """ # do the inherited stuff super().init(**kwargs) # open and initialize the HDF5 file logging.info("Initializing HDF5 file at {}".format(self.file_path)) self.wepy_h5 = WepyHDF5(self.file_path, mode=self.mode, topology=self._tmp_topology, units=self.units, sparse_fields=list(self._sparse_fields.keys()), feature_shapes=self._feature_shapes, feature_dtypes=self._feature_dtypes, n_dims=self._n_dims, main_rep_idxs=self.main_rep_idxs, alt_reps=self.alt_reps_idxs) # if we specify save fields only save these for the initial walkers if self.save_fields is not None: state_fields = list(init_walkers[0].state.dict().keys()) # make sure all the save_fields are present in the state assert all([True if save_field in state_fields else False for save_field in self.save_fields]), \ "Not all specified save_fields present in walker states" filtered_init_walkers = [] for walker in init_walkers: # make a new state by filtering the attributes of the old ones state_d = { k: v for k, v in walker.state.dict().items() if k in self.save_fields } # and saving alternate representations as we would # expect them # if there are any alternate representations set them for alt_rep_name, alt_rep_idxs in self.alt_reps_idxs.items(): alt_rep_path = 'alt_reps/{}'.format(alt_rep_name) # if the idxs are None we want all of the atoms if alt_rep_idxs is None: state_d[alt_rep_path] = state_d['positions'][:] # otherwise get only the atoms we want else: state_d[alt_rep_path] = state_d['positions'][ alt_rep_idxs] # if the main rep is different then the full state # positions set that if self.main_rep_idxs is not None: state_d['positions'] = state_d['positions'][ self.main_rep_idxs] # then making the new state new_state = WalkerState(**state_d) filtered_init_walkers.append(Walker(new_state, walker.weight)) # otherwise save the full state else: filtered_init_walkers = init_walkers with self.wepy_h5: # if this is a continuation run of another run we want to # initialize it as such # initialize a new run run_grp = self.wepy_h5.new_run(filtered_init_walkers, continue_run=continue_run) self.wepy_run_idx = run_grp.attrs['run_idx'] # initialize the run record groups using their fields self.wepy_h5.init_run_fields_resampling(self.wepy_run_idx, self.resampling_fields) # the enumeration for the values of resampling self.wepy_h5.init_run_fields_resampling_decision( self.wepy_run_idx, self.decision_enum) self.wepy_h5.init_run_fields_resampler(self.wepy_run_idx, self.resampler_fields) # set the fields that are records for tables etc. unless # they are already set if 'resampling' not in self.wepy_h5.record_fields: self.wepy_h5.init_record_fields('resampling', self.resampling_records) if 'resampler' not in self.wepy_h5.record_fields: self.wepy_h5.init_record_fields('resampler', self.resampler_records) # if there were no warping fields set there is no boundary # conditions and we don't initialize them if self.warping_fields is not None: self.wepy_h5.init_run_fields_warping(self.wepy_run_idx, self.warping_fields) self.wepy_h5.init_run_fields_progress(self.wepy_run_idx, self.progress_fields) self.wepy_h5.init_run_fields_bc(self.wepy_run_idx, self.bc_fields) # table records if 'warping' not in self.wepy_h5.record_fields: self.wepy_h5.init_record_fields('warping', self.warping_records) if 'boundary_conditions' not in self.wepy_h5.record_fields: self.wepy_h5.init_record_fields('boundary_conditions', self.bc_records) if 'progress' not in self.wepy_h5.record_fields: self.wepy_h5.init_record_fields('progress', self.progress_records) # if this was opened in a truncation mode, we don't want to # overwrite old runs with future calls to init(). so we # change the mode to read/write 'r+' if self.mode == 'w': self.set_mode(0, 'r+')
def _run(self, num_runs, num_cycles, num_walkers): """Runs a random walk simulation. Parameters ---------- num_runs: int The number independet simulations. num_cycles: int The number of cycles that will be run in the simulation. num_walkers: int The number of walkers. """ print("Random walk simulation with: ") print("Dimension = {}".format(self.dimension)) print("Probability = {}".format(self.probability)) print("Number of Walkers = {}".format(num_walkers)) print("Number of Cycles ={}".format(num_cycles)) # set up initial state for walkers positions = np.zeros((1, self.dimension)) init_state = WalkerState(positions=positions, time=0.0) # create list of init_walkers initial_weight = 1/num_walkers init_walkers = [] init_walkers = [Walker(init_state, initial_weight) for i in range(num_walkers)] # set up raunner for system runner = RandomWalkRunner(probability=self.probability) units = dict(UNIT_NAMES) # instantiate a revo unbindingboudaryconditiobs segment_length = 10 # set up the reporter randomwalk_system_top_json = self.generate_topology() hdf5_reporter = WepyHDF5Reporter(file_path=self.hdf5_filename, mode='w', save_fields=SAVE_FIELDS, topology=randomwalk_system_top_json, resampler=self.resampler, units=dict(UNITS), n_dims=self.dimension) # running the simulation sim_manager = Manager(init_walkers, runner=runner, resampler=self.resampler, work_mapper=Mapper(), reporters=[hdf5_reporter]) # run a simulation with the manager for n_steps cycles of length 1000 each steps = [segment_length for i in range(num_cycles)] ### RUN the simulation for run_idx in range(num_runs): print("Starting run: {}".format(run_idx)) sim_manager.run_simulation(num_cycles, steps) print("Finished run: {}".format(run_idx)) print("Finished Simulation")