def test_onesite_sams_sampling_binary_all_stages(self): """Test the one site sams sampling approach with binary updates through all 4 stages of the algorithm""" old_log_level = log.getEffectiveLevel() log.setLevel(logging.INFO) pep = self.setup_peptide_implicit("yeah", createsim=False) pep.drive.enable_calibration( SAMSApproach.ONESITE, group_index=0, min_burn=200, min_fast=100, min_slow=100, flatness_criterion=0.15, ) pep.simulation = app.ConstantPHSimulation( pep.topology, pep.system, pep.integrator, pep.drive, platform=TestSAMS.platform, ) pep.simulation.context.setPositions(pep.positions) pep.context = pep.simulation.context pep.simulation.minimizeEnergy() total_iterations = 1000 for x in range(total_iterations): if x == total_iterations - 1: log.setLevel(logging.DEBUG) pep.simulation.step(1) pep.simulation.update(1) pep.simulation.adapt() # log.info(pep.drive.calibration_state.observed_counts) log.setLevel(old_log_level)
def test_calibration_class_multisite(self): """Test the multisite SAMS sampling approach.""" old_log_level = log.getEffectiveLevel() pep = self.setup_peptide_implicit("yeah", createsim=True) pep.drive.enable_calibration(SAMSApproach.MULTISITE) sampler = SAMSCalibrationEngine(pep.drive) total_iterations = 1500 for x in range(total_iterations): if x == total_iterations - 1: log.setLevel(logging.DEBUG) pep.simulation.step(1) pep.simulation.update(1) sampler.adapt_zetas(UpdateRule.BINARY, b=0.51, stage=Stage.NODECAY) log.setLevel(old_log_level) return
def test_onesite_sams_sampling_global(self): """Test the one site sams sampling approach with global updates""" old_log_level = log.getEffectiveLevel() pep = self.setup_peptide_implicit("yeah") pep.drive.enable_calibration(SAMSApproach.ONESITE, group_index=1) sampler = SAMSCalibrationEngine(pep.drive) total_iterations = 1500 for x in range(total_iterations): if x == total_iterations - 1: log.setLevel(logging.DEBUG) pep.simulation.step(1) pep.simulation.update(1) sampler.adapt_zetas(UpdateRule.GLOBAL, b=0.51, stage=Stage.NODECAY) log.setLevel(old_log_level)
def test_dummy_moving_mc(self) -> None: """Move dummies with monte carlo and evaluate the energy differences.""" md_steps_between_mc = 100 total_loops = 100 viologen = self.setup_viologen_vacuum() # viologen = self.setup_viologen_water() if log.getEffectiveLevel() == logging.DEBUG: viologen.simulation.reporters.append( app.DCDReporter( "cooh-viologen-{}.dcd".format(str(uuid.uuid4())), md_steps_between_mc // 10, )) cooh1 = COOHDummyMover.from_system(viologen.system, viologen.cooh1) cooh2 = COOHDummyMover.from_system(viologen.system, viologen.cooh2) moveset = {cooh1.random_move, cooh2.random_move} n_accept = 0 for iteration in range(total_loops): viologen.simulation.step(md_steps_between_mc) state = viologen.context.getState(getPositions=True, getVelocities=True) pos = state.getPositions(asNumpy=True)._value # perform a move. move = sample(moveset, 1)[0] log.debug(move.__name__) new_pos, logp = move(pos) if exp(logp) > uniform(0.0, 1.0): log.debug("Accepted: logp %f", logp) viologen.context.setPositions(new_pos) viologen.context.setVelocitiesToTemperature( viologen.temperature) n_accept += 1 else: log.debug("Rejected: logp %f", logp) raise RuntimeError("These moves should never be rejected.") return
def test_dummy_moving_mc(self) -> None: """Move dummies with monte carlo and evaluate the energy differences.""" md_steps_between_mc = 100 total_loops = 100 viologen = self.setup_viologen_vacuum() # viologen = self.setup_viologen_water() if log.getEffectiveLevel() == logging.DEBUG: viologen.simulation.reporters.append( app.DCDReporter( "cooh-viologen-{}.dcd".format(str(uuid.uuid4())), md_steps_between_mc // 10, ) ) cooh1 = COOHDummyMover.from_system(viologen.system, viologen.cooh1) cooh2 = COOHDummyMover.from_system(viologen.system, viologen.cooh2) moveset = {cooh1.random_move, cooh2.random_move} n_accept = 0 for iteration in range(total_loops): viologen.simulation.step(md_steps_between_mc) state = viologen.context.getState(getPositions=True, getVelocities=True) pos = state.getPositions(asNumpy=True)._value # perform a move. move = sample(moveset, 1)[0] log.debug(move.__name__) new_pos, logp = move(pos) if exp(logp) > uniform(0.0, 1.0): log.debug("Accepted: logp %f", logp) viologen.context.setPositions(new_pos) viologen.context.setVelocitiesToTemperature(viologen.temperature) n_accept += 1 else: log.debug("Rejected: logp %f", logp) raise RuntimeError("These moves should never be rejected.") return
def test_glutamic_acid_cooh(self): """Use the dummy mover with the amino acid glutamic acid""" md_steps_between_mc = 10 total_loops = 5 glh = self.setup_amino_acid_water("glh") drive = ForceFieldProtonDrive( glh.temperature, glh.topology, glh.system, glh.forcefield, glh.ffxml_files, pressure=glh.pressure, perturbations_per_trial=glh.perturbations_per_trial, propagations_per_step=glh.propagations_per_step, residues_by_name=None, residues_by_index=None, ) if log.getEffectiveLevel() == logging.DEBUG: glh.simulation.reporters.append( app.DCDReporter( "cooh-glh-{}.dcd".format(str(uuid.uuid4())), md_steps_between_mc // 5, enforcePeriodicBox=True, ) ) drive.attach_context(glh.context) drive.adjust_to_ph(4.6) for iteration in range(total_loops): glh.simulation.step(md_steps_between_mc) drive.update("COOH", nattempts=1) return
def test_glutamic_acid_cooh(self): """Use the dummy mover with the amino acid glutamic acid""" md_steps_between_mc = 10 total_loops = 5 glh = self.setup_amino_acid_water("glh") drive = ForceFieldProtonDrive( glh.temperature, glh.topology, glh.system, glh.forcefield, glh.ffxml_files, pressure=glh.pressure, perturbations_per_trial=glh.perturbations_per_trial, propagations_per_step=glh.propagations_per_step, residues_by_name=None, residues_by_index=None, ) if log.getEffectiveLevel() == logging.DEBUG: glh.simulation.reporters.append( app.DCDReporter( "cooh-glh-{}.dcd".format(str(uuid.uuid4())), md_steps_between_mc // 5, enforcePeriodicBox=True, )) drive.attach_context(glh.context) drive.adjust_to_ph(4.6) for iteration in range(total_loops): glh.simulation.step(md_steps_between_mc) drive.update("COOH", nattempts=1) return
def test_onesite_sams_sampling_binary_all_stages_serialization(self): """Test the one site sams sampling approach with binary updates through all 4 stages of the algorithm and after serialization.""" old_log_level = log.getEffectiveLevel() log.setLevel(logging.INFO) pep = self.setup_peptide_implicit("yeah", createsim=False) pep.drive.enable_calibration( SAMSApproach.ONESITE, group_index=0, min_burn=200, min_fast=200, min_slow=200, flatness_criterion=0.15, ) pep.simulation = app.ConstantPHSimulation( pep.topology, pep.system, pep.integrator, pep.drive, platform=TestSAMS.platform, ) pep.simulation.context.setPositions(pep.positions) pep.context = pep.simulation.context pep.simulation.minimizeEnergy() total_iterations = 1000 for x in range(total_iterations): pep.simulation.step(1) pep.simulation.update(1) pep.simulation.adapt() pep.drive: NCMCProtonDrive drive_state = pep.drive.state_to_xml() new_drive = NCMCProtonDrive( pep.temperature, pep.topology, pep.system, pressure=None, perturbations_per_trial=pep.perturbations_per_trial, propagations_per_step=pep.propagations_per_step, ) new_drive.state_from_xml_tree(etree.fromstring(drive_state)) new_simulation = app.ConstantPHSimulation( pep.topology, pep.system, create_compound_gbaoab_integrator(pep), new_drive, platform=TestSAMS.platform, ) old_state: app.State = pep.simulation.context.getState( getPositions=True, getVelocities=True ) new_simulation.context.setPositions(old_state.getPositions()) new_simulation.context.setVelocities(old_state.getVelocities()) total_iterations = 1000 for x in range(total_iterations): if x == total_iterations - 1: log.setLevel(logging.DEBUG) new_simulation.step(1) new_simulation.update(1) new_simulation.adapt() log.debug("Last deviation: %f", new_simulation.last_dev) log.info( "Observed counts: %s", str(pep.drive.calibration_state.observed_counts) ) log.setLevel(old_log_level)