def test_recycle(self): segments = [ self.segment(0.0, 1.5, weight=0.5), self.segment(0.0, 0.5, weight=0.5) ] tstate = TargetState('recycle', [1.5], 0) istate = InitialState(0, 0, 0, pcoord=[0.0]) self.we_driver.new_iteration(initial_states=[istate], target_states=[tstate]) n_needed = self.we_driver.assign(segments) assert n_needed == 0 self.we_driver.construct_next() n_recycled = len(list(self.we_driver.recycling_segments)) assert n_recycled == 1 assert len(self.we_driver.next_iter_binning[0]) == 4 assert len(self.we_driver.next_iter_binning[1]) == 0 assert abs( sum(seg.weight for seg in self.we_driver.next_iter_binning[0]) - 1.0) < 4 * EPS assert np.allclose( [seg.weight for seg in self.we_driver.next_iter_binning[0]], [0.25 for _i in range(4)]) assert segments[0].endpoint_type == Segment.SEG_ENDPOINT_RECYCLED
def target_states(self): """list[TargetState]: Target states.""" if self._tstates is None: return [] return [ TargetState(row.label, pcoord, state_id=row.Index) for row, pcoord in zip(self.target_state_info.itertuples(), self.target_state_pcoords) ]
def target_states(self): """list[TargetState]: Target states in use for the iteration.""" if not self.has_target_states: return [] return [ TargetState(row.label, pcoord, state_id=row.Index) for row, pcoord in zip(self.target_state_summaries.itertuples(), self.target_state_pcoords) ]
def target_state(self, index): """Return the target state with the given index. Parameters ---------- index : int Target state index (0-based). Returns ------- TargetState The target state indexed by `index`. """ row = self.h5group['tstates']['index'][index] pcoord = self.h5group['tstates']['pcoord'][index] return TargetState(row['label'].decode(), pcoord, state_id=index)
def entry_point(): parser = argparse.ArgumentParser( 'w_states', description='''\ Display or manipulate basis (initial) or target (recycling) states for a WEST simulation. By default, states are displayed (or dumped to files). If ``--replace`` is specified, all basis/target states are replaced for the next iteration. If ``--append`` is specified, the given target state(s) are appended to the list for the next iteration. Appending basis states is not permitted, as this would require renormalizing basis state probabilities in ways that may be error-prone. Instead, use ``w_states --show --bstate-file=bstates.txt`` and then edit the resulting ``bstates.txt`` file to include the new desired basis states, then use ``w_states --replace --bstate-file=bstates.txt`` to update the WEST HDF5 file appropriately. ''', ) westpa.rc.add_args(parser) smgroup = parser.add_argument_group('modes of operation') mode_group = smgroup.add_mutually_exclusive_group() mode_group.add_argument( '--show', dest='mode', action='store_const', const='show', help='Display current basis/target states (or dump to files).' ) mode_group.add_argument( '--append', dest='mode', action='store_const', const='append', help='Append the given basis/target states to those currently in use.', ) mode_group.add_argument( '--replace', dest='mode', action='store_const', const='replace', help='Replace current basis/target states with those specified.', ) parser.add_argument( '--bstate-file', metavar='BSTATE_FILE', help='''Read (--append/--replace) or write (--show) basis state names, probabilities, and data references from/to BSTATE_FILE.''', ) parser.add_argument( '--bstate', action='append', dest='bstates', help='''Add the given basis state (specified as a string 'label,probability[,auxref]') to the list of basis states (after those specified in --bstate-file, if any). This argument may be specified more than once, in which case the given states are appended in the order they are given on the command line.''', ) parser.add_argument( '--tstate-file', metavar='TSTATE_FILE', help='''Read (--append/--replace) or write (--show) target state names and representative progress coordinates from/to TSTATE_FILE''', ) parser.add_argument( '--tstate', action='append', dest='tstates', help='''Add the given target state (specified as a string 'label,pcoord0[,pcoord1[,...]]') to the list of target states (after those specified in the file given by --tstates-from, if any). This argument may be specified more than once, in which case the given states are appended in the order they appear on the command line.''', ) parser.set_defaults(mode='show') work_managers.environment.add_wm_args(parser) args = parser.parse_args() westpa.rc.process_args(args) work_managers.environment.process_wm_args(args) work_manager = make_work_manager() system = westpa.rc.get_system_driver() with work_manager: if work_manager.is_master: data_manager = westpa.rc.get_data_manager() data_manager.open_backing(mode='a') sim_manager = westpa.rc.get_sim_manager() n_iter = data_manager.current_iteration assert args.mode in ('show', 'replace', 'append') if args.mode == 'show': basis_states = data_manager.get_basis_states(n_iter) if basis_states: bstate_file = sys.stdout if not args.bstate_file else open(args.bstate_file, 'wt') bstate_file.write('# Basis states for iteration {:d}\n'.format(n_iter)) BasisState.states_to_file(basis_states, bstate_file) target_states = data_manager.get_target_states(n_iter) if target_states: tstate_file = sys.stdout if not args.tstate_file else open(args.tstate_file, 'wt') tstate_file.write('# Target states for iteration {:d}\n'.format(n_iter)) TargetState.states_to_file(target_states, tstate_file) elif args.mode == 'replace': seg_index = data_manager.get_seg_index(n_iter) if (seg_index['status'] == Segment.SEG_STATUS_COMPLETE).any(): print('Iteration {:d} has completed segments; applying new states to iteration {:d}'.format(n_iter, n_iter + 1)) n_iter += 1 basis_states = [] if args.bstate_file: basis_states.extend(BasisState.states_from_file(args.bstate_file)) if args.bstates: for bstate_str in args.bstates: fields = bstate_str.split(',') label = fields[0] probability = float(fields[1]) try: auxref = fields[2] except IndexError: auxref = None basis_states.append(BasisState(label=label, probability=probability, auxref=auxref)) if basis_states: # Check that the total probability of basis states adds to one tprob = sum(bstate.probability for bstate in basis_states) if abs(1.0 - tprob) > len(basis_states) * EPS: pscale = 1 / tprob log.warning('Basis state probabilities do not add to unity; rescaling by {:g}'.format(pscale)) for bstate in basis_states: bstate.probability *= pscale # Assign progress coordinates to basis states sim_manager.get_bstate_pcoords(basis_states, n_iter) data_manager.create_ibstate_group(basis_states, n_iter) sim_manager.report_basis_states(basis_states) # Now handle target states target_states = [] if args.tstate_file: target_states.extend(TargetState.states_from_file(args.tstate_file, system.pcoord_dtype)) if args.tstates: tstates_strio = io.StringIO('\n'.join(args.tstates).replace(',', ' ')) target_states.extend(TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio if not target_states: westpa.rc.pstatus('No target states specified.') else: data_manager.save_target_states(target_states, n_iter) sim_manager.report_target_states(target_states) data_manager.update_iter_group_links(n_iter) else: # args.mode == 'append' if args.bstate_file or args.bstates: sys.stderr.write('refusing to append basis states; use --show followed by --replace instead\n') sys.exit(2) target_states = data_manager.get_target_states(n_iter) seg_index = data_manager.get_seg_index(n_iter) if (seg_index['status'] == Segment.SEG_STATUS_COMPLETE).any(): print('Iteration {:d} has completed segments; applying new states to iteration {:d}'.format(n_iter, n_iter + 1)) n_iter += 1 if args.tstate_file: target_states.extend(TargetState.states_from_file(args.tstate_file, system.pcoord_dtype)) if args.tstates: tstates_strio = io.StringIO('\n'.join(args.tstates).replace(',', ' ')) target_states.extend(TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio if not target_states: westpa.rc.pstatus('No target states specified.') else: data_manager.save_target_states(target_states, n_iter) sim_manager.report_target_states(target_states) data_manager.update_iter_group_links(n_iter) else: work_manager.run()
def entry_point(): parser = argparse.ArgumentParser('w_init', description='''\ Initialize a new WEST simulation, creating the WEST HDF5 file and preparing the first iteration's segments. Initial states are generated from one or more "basis states" which are specified either in a file specified with --bstates-from, or by one or more "--bstate" arguments. If neither --bstates-from nor at least one "--bstate" argument is provided, then a default basis state of probability one identified by the state ID zero and label "basis" will be created (a warning will be printed in this case, to remind you of this behavior, in case it is not what you wanted). Target states for (non-equilibrium) steady-state simulations are specified either in a file specified with --tstates-from, or by one or more --tstate arguments. If neither --tstates-from nor at least one --tstate argument is provided, then an equilibrium simulation (without any sinks) will be performed. ''') westpa.rc.add_args(parser) parser.add_argument('--force', dest='force', action='store_true', help='Overwrite any existing simulation data') parser.add_argument( '--bstate-file', '--bstates-from', metavar='BSTATE_FILE', help= 'Read basis state names, probabilities, and (optionally) data references from BSTATE_FILE.' ) parser.add_argument( '--bstate', action='append', dest='bstates', help= '''Add the given basis state (specified as a string 'label,probability[,auxref]') to the list of basis states (after those specified in --bstates-from, if any). This argument may be specified more than once, in which case the given states are appended in the order they are given on the command line.''') parser.add_argument( '--tstate-file', '--tstates-from', metavar='TSTATE_FILE', help= 'Read target state names and representative progress coordinates from TSTATE_FILE' ) parser.add_argument( '--tstate', action='append', dest='tstates', help= '''Add the given target state (specified as a string 'label,pcoord0[,pcoord1[,...]]') to the list of target states (after those specified in the file given by --tstates-from, if any). This argument may be specified more than once, in which case the given states are appended in the order they appear on the command line.''') parser.add_argument( '--segs-per-state', type=int, metavar='N', default=1, help= '''Initialize N segments from each basis state (default: %(default)s).''' ) parser.add_argument( '--no-we', '--shotgun', dest='shotgun', action='store_true', help= '''Do not run the weighted ensemble bin/split/merge algorithm on newly-created segments.''' ) work_managers.environment.add_wm_args(parser) args = parser.parse_args() westpa.rc.process_args(args) work_managers.environment.process_wm_args(args) westpa.rc.work_manager = work_manager = make_work_manager() system = westpa.rc.get_system_driver() sim_manager = westpa.rc.get_sim_manager() propagator = westpa.rc.get_propagator() data_manager = westpa.rc.get_data_manager() h5file = data_manager.we_h5filename data_manager.system = system we_driver = westpa.rc.get_we_driver() with work_manager: if work_manager.is_master: # Process target states target_states = [] if args.tstate_file: target_states.extend( TargetState.states_from_file(args.tstate_file, system.pcoord_dtype)) if args.tstates: tstates_strio = io.StringIO('\n'.join(args.tstates).replace( ',', ' ')) target_states.extend( TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio # Process basis states basis_states = [] if args.bstate_file: basis_states.extend( BasisState.states_from_file(args.bstate_file)) if args.bstates: for bstate_str in args.bstates: fields = bstate_str.split(',') label = fields[0] probability = float(fields[1]) try: auxref = fields[2] except IndexError: auxref = None basis_states.append( BasisState(label=label, probability=probability, auxref=auxref)) if not basis_states: log.error('At least one basis state is required') sys.exit(3) # Check that the total probability of basis states adds to one tprob = sum(bstate.probability for bstate in basis_states) if abs(1.0 - tprob) > len(basis_states) * EPS: pscale = 1 / tprob log.warning( 'Basis state probabilities do not add to unity; rescaling by {:g}' .format(pscale)) for bstate in basis_states: bstate.probability *= pscale # Prepare simulation sim_manager.initialize_simulation( basis_states, target_states, segs_per_state=args.segs_per_state, suppress_we=args.shotgun) else: work_manager.run()
def initialize(mode, bstates, _bstate_file, tstates, _tstate_file): work_manager = make_work_manager() system = westpa.rc.get_system_driver() with work_manager: if work_manager.is_master: data_manager = westpa.rc.get_data_manager() data_manager.open_backing(mode='a') sim_manager = westpa.rc.get_sim_manager() n_iter = data_manager.current_iteration assert mode in ('show', 'replace', 'append') if mode == 'show': basis_states = data_manager.get_basis_states(n_iter) if basis_states: bstate_file = sys.stdout if not _bstate_file else open( _bstate_file, 'wt') bstate_file.write( '# Basis states for iteration {:d}\n'.format(n_iter)) BasisState.states_to_file(basis_states, bstate_file) target_states = data_manager.get_target_states(n_iter) if target_states: tstate_file = sys.stdout if not _tstate_file else open( _tstate_file, 'wt') tstate_file.write( '# Target states for iteration {:d}\n'.format(n_iter)) TargetState.states_to_file(target_states, tstate_file) elif mode == 'replace': seg_index = data_manager.get_seg_index(n_iter) if (seg_index['status'] == Segment.SEG_STATUS_COMPLETE).any(): print( 'Iteration {:d} has completed segments; applying new states to iteration {:d}' .format(n_iter, n_iter + 1)) n_iter += 1 basis_states = [] if _bstate_file: basis_states.extend( BasisState.states_from_file(_bstate_file)) if bstates: for bstate_str in bstates: fields = bstate_str.split(',') label = fields[0] probability = float(fields[1]) try: auxref = fields[2] except IndexError: auxref = None basis_states.append( BasisState(label=label, probability=probability, auxref=auxref)) if basis_states: # Check that the total probability of basis states adds to one tprob = sum(bstate.probability for bstate in basis_states) if abs(1.0 - tprob) > len(basis_states) * EPS: pscale = 1 / tprob log.warning( 'Basis state probabilities do not add to unity; rescaling by {:g}' .format(pscale)) for bstate in basis_states: bstate.probability *= pscale # Assign progress coordinates to basis states sim_manager.get_bstate_pcoords(basis_states, n_iter) data_manager.create_ibstate_group(basis_states, n_iter) sim_manager.report_basis_states(basis_states) # Now handle target states target_states = [] if _tstate_file: target_states.extend( TargetState.states_from_file(_tstate_file, system.pcoord_dtype)) if tstates: tstates_strio = io.StringIO('\n'.join(tstates).replace( ',', ' ')) target_states.extend( TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio if not target_states: westpa.rc.pstatus('No target states specified.') else: data_manager.save_target_states(target_states, n_iter) sim_manager.report_target_states(target_states) data_manager.update_iter_group_links(n_iter) else: # args.mode == 'append' if _bstate_file or bstates: sys.stderr.write( 'refusing to append basis states; use --show followed by --replace instead\n' ) sys.exit(2) target_states = data_manager.get_target_states(n_iter) seg_index = data_manager.get_seg_index(n_iter) if (seg_index['status'] == Segment.SEG_STATUS_COMPLETE).any(): print( 'Iteration {:d} has completed segments; applying new states to iteration {:d}' .format(n_iter, n_iter + 1)) n_iter += 1 if _tstate_file: target_states.extend( TargetState.states_from_file(_tstate_file, system.pcoord_dtype)) if tstates: tstates_strio = io.StringIO('\n'.join(tstates).replace( ',', ' ')) target_states.extend( TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio if not target_states: westpa.rc.pstatus('No target states specified.') else: data_manager.save_target_states(target_states, n_iter) sim_manager.report_target_states(target_states) data_manager.update_iter_group_links(n_iter) else: work_manager.run()
def initialize(tstates, tstate_file, bstates, bstate_file, sstates=None, sstate_file=None, segs_per_state=1, shotgun=False): """ Initialize a WESTPA simulation. tstates : list of str tstate_file : str bstates : list of str bstate_file : str sstates : list of str sstate_file : str segs_per_state : int shotgun : bool """ westpa.rc.work_manager = work_manager = make_work_manager() system = westpa.rc.get_system_driver() sim_manager = westpa.rc.get_sim_manager() data_manager = westpa.rc.get_data_manager() data_manager.system = system with work_manager: if work_manager.is_master: # Process target states target_states = [] if tstate_file: target_states.extend(TargetState.states_from_file(tstate_file, system.pcoord_dtype)) if tstates: tstates_strio = io.StringIO('\n'.join(tstates).replace(',', ' ')) target_states.extend(TargetState.states_from_file(tstates_strio, system.pcoord_dtype)) del tstates_strio # Process basis states basis_states = [] if bstate_file: basis_states.extend(BasisState.states_from_file(bstate_file)) if bstates: for bstate_str in bstates: fields = bstate_str.split(',') label = fields[0] probability = float(fields[1]) try: auxref = fields[2] except IndexError: auxref = None basis_states.append(BasisState(label=label, probability=probability, auxref=auxref)) # Process the list of start states, creating a BasisState from each start_states = [] if sstate_file: start_states.extend(BasisState.states_from_file(sstate_file)) if sstates: for sstate_str in sstates: fields = sstate_str.split(',') label = fields[0] probability = float(fields[1]) try: auxref = fields[2] except IndexError: auxref = None start_states.append(BasisState(label=label, probability=probability, auxref=auxref)) if not basis_states: log.error('At least one basis state is required') sys.exit(3) # Check that the total probability of basis states adds to one bstate_prob, sstate_prob = ( sum(bstate.probability for bstate in basis_states), sum(sstate.probability for sstate in start_states), ) # tprob = sum(bstate.probability for bstate in basis_states) tprob = bstate_prob + sstate_prob if abs(1.0 - tprob) > len(basis_states) * EPS: pscale = 1 / tprob log.warning( 'Basis state probabilities do not add to unity (basis: {:.2f}, start states: {:.2f}); rescaling by {:g}. If using start states, some rescaling is normal.'.format( bstate_prob, sstate_prob, pscale ) ) for bstate in basis_states: bstate.probability *= pscale for sstate in start_states: sstate.probability *= pscale # Prepare simulation sim_manager.initialize_simulation( basis_states=basis_states, target_states=target_states, start_states=start_states, segs_per_state=segs_per_state, suppress_we=shotgun, ) else: work_manager.run()