def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the GHMC MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = GHMCMove(nsteps=10, timestep=1.0*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Create integrator. integrator = integrators.GHMCIntegrator(temperature=thermodynamic_state.temperature, collision_rate=self.collision_rate, timestep=self.timestep) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # TODO: Enforce constraints? #tol = 1.0e-8 #context.applyConstraints(tol) #context.applyVelocityConstraints(tol) # Run dynamics. timer.start("step()") integrator.step(self.nsteps) timer.stop("step()") # Get updated sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.start("update_sampler_state") # Accumulate acceptance statistics. ghmc_global_variables = { integrator.getGlobalVariableName(index) : index for index in range(integrator.getNumGlobalVariables()) } naccepted = integrator.getGlobalVariable(ghmc_global_variables['naccept']) nattempted = integrator.getGlobalVariable(ghmc_global_variables['ntrials']) self.naccepted += naccepted self.nattempted += nattempted # DEBUG. #print " GHMC accepted %d / %d (%.1f%%)" % (naccepted, nattempted, float(naccepted) / float(nattempted) * 100.0) # Clean up. del context timer.report_timing() return updated_sampler_state
def minimize(self, tolerance=None, maxIterations=None, platform=None): """ Minimize the current configuration. Parameters ---------- tolerance : simtk.unit.Quantity compatible with kilocalories_per_mole/anstroms, optional, default = 1*kilocalories_per_mole/anstrom Tolerance to use for minimization termination criterion. maxIterations : int, optional, default = 100 Maximum number of iterations to use for minimization. platform : simtk.openmm.Platform, optional Platform to use for minimization. Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Minimize >>> sampler_state.minimize() """ timer = Timer() if (tolerance is None): tolerance = 1.0 * u.kilocalories_per_mole / u.angstroms if (maxIterations is None): maxIterations = 100 # Use LocalEnergyMinimizer from simtk.openmm import LocalEnergyMinimizer timer.start("Context creation") context = self.createContext(platform=platform) logger.debug("LocalEnergyMinimizer: platform is %s" % context.getPlatform().getName()) logger.debug("Minimizing with tolerance %s and %d max. iterations." % (tolerance, maxIterations)) timer.stop("Context creation") timer.start("LocalEnergyMinimizer minimize") LocalEnergyMinimizer.minimize(context, tolerance, maxIterations) timer.stop("LocalEnergyMinimizer minimize") # Retrieve data. sampler_state = SamplerState.createFromContext(context) self.positions = sampler_state.positions self.potential_energy = sampler_state.potential_energy self.total_energy = sampler_state.total_energy del context timer.report_timing() return
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the Langevin dynamics MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- Alanine dipeptide in vacuum. >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = LangevinDynamicsMove(nsteps=10, timestep=0.5*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) Ideal gas. >>> # Create a test system >>> import testsystems >>> test = testsystems.IdealGas() >>> # Add a MonteCarloBarostat. >>> barostat = mm.MonteCarloBarostat(1*u.atmospheres, 298*u.kelvin, 25) >>> force_index = test.system.addForce(barostat) >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = LangevinDynamicsMove(nsteps=500, timestep=0.5*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Check if the system contains a barostat. system = sampler_state.system forces = { system.getForce(index).__class__.__name__ : system.getForce(index) for index in range(system.getNumForces()) } barostat = None if 'MonteCarloBarostat' in forces: barostat = forces['MonteCarloBarostat'] barostat.setTemperature(thermodynamic_state.temperature) parameter_name = barostat.Pressure() # Create integrator. integrator = mm.LangevinIntegrator(thermodynamic_state.temperature, self.collision_rate, self.timestep) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") logger.debug("LangevinDynamicMove: Context created, platform is %s" % context.getPlatform().getName()) # Set pressure, if barostat is included. if barostat is not None: context.setParameter(parameter_name, thermodynamic_state.pressure) if self.reassign_velocities: # Assign Maxwell-Boltzmann velocities. context.setVelocitiesToTemperature(thermodynamic_state.temperature) # Run dynamics. timer.start("step()") integrator.step(self.nsteps) timer.stop("step()") # Get updated sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.start("update_sampler_state") # Clean up. del context timer.report_timing() return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create an HMC move. >>> move = HMCMove(nsteps=10, timestep=0.5*u.femtoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Create integrator. integrator = integrators.HMCIntegrator(temperature=thermodynamic_state.temperature, timestep=self.timestep, nsteps=self.nsteps) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # Run dynamics. # Note that ONE step of this integrator is equal to self.nsteps of velocity Verlet dynamics followed by Metropolis accept/reject. timer.start("HMC integration") integrator.step(1) timer.stop("HMC integration") # Get sampler state. timer.start("updated_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.stop("updated_sampler_state") # Clean up. del context timer.report_timing() # Return updated sampler state. return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.LennardJonesFluid() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions, box_vectors=test.system.getDefaultPeriodicBoxVectors()) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin, pressure=1*u.atmospheres) >>> # Create a Monte Carlo Barostat move. >>> move = MonteCarloBarostatMove(nattempts=5) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Make sure system contains a barostat. system = sampler_state.system forces = { system.getForce(index).__class__.__name__ : system.getForce(index) for index in range(system.getNumForces()) } old_barostat_frequency = None if 'MonteCarloBarostat' in forces: force = forces['MonteCarloBarostat'] force.setTemperature(thermodynamic_state.temperature) old_barostat_frequency = force.getFrequency() force.setFrequency(1) parameter_name = force.Pressure() else: # Add MonteCarloBarostat. force = mm.MonteCarloBarostat(thermodynamic_state.pressure, thermodynamic_state.temperature, 1) system.addForce(force) parameter_name = force.Pressure() # Create integrator. integrator = integrators.DummyIntegrator() # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) force.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # Set pressure. context.setParameter(parameter_name, thermodynamic_state.pressure) # Run update. # Note that ONE step of this integrator is equal to self.nsteps of velocity Verlet dynamics followed by Metropolis accept/reject. timer.start("step(1)") integrator.step(self.nattempts) timer.stop("step(1)") # Get sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.stop("update_sampler_state") # DEBUG #print thermodynamics.volume(updated_sampler_state.box_vectors) # Clean up. del context # Restore frequency of barostat. if old_barostat_frequency: force.setFrequency(old_barostat_frequency) timer.report_timing() # Return updated sampler state. return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the GHMC MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = GHMCMove(nsteps=10, timestep=1.0*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Create integrator. integrator = integrators.GHMCIntegrator( temperature=thermodynamic_state.temperature, collision_rate=self.collision_rate, timestep=self.timestep) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # TODO: Enforce constraints? #tol = 1.0e-8 #context.applyConstraints(tol) #context.applyVelocityConstraints(tol) # Run dynamics. timer.start("step()") integrator.step(self.nsteps) timer.stop("step()") # Get updated sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.start("update_sampler_state") # Accumulate acceptance statistics. ghmc_global_variables = { integrator.getGlobalVariableName(index): index for index in range(integrator.getNumGlobalVariables()) } naccepted = integrator.getGlobalVariable( ghmc_global_variables['naccept']) nattempted = integrator.getGlobalVariable( ghmc_global_variables['ntrials']) self.naccepted += naccepted self.nattempted += nattempted # DEBUG. #print " GHMC accepted %d / %d (%.1f%%)" % (naccepted, nattempted, float(naccepted) / float(nattempted) * 100.0) # Clean up. del context timer.report_timing() return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the Langevin dynamics MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- Alanine dipeptide in vacuum. >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = LangevinDynamicsMove(nsteps=10, timestep=0.5*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) Ideal gas. >>> # Create a test system >>> import testsystems >>> test = testsystems.IdealGas() >>> # Add a MonteCarloBarostat. >>> barostat = mm.MonteCarloBarostat(1*u.atmospheres, 298*u.kelvin, 25) >>> force_index = test.system.addForce(barostat) >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create a LangevinDynamicsMove >>> move = LangevinDynamicsMove(nsteps=500, timestep=0.5*u.femtoseconds, collision_rate=20.0/u.picoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Check if the system contains a barostat. system = sampler_state.system forces = { system.getForce(index).__class__.__name__: system.getForce(index) for index in range(system.getNumForces()) } barostat = None if 'MonteCarloBarostat' in forces: barostat = forces['MonteCarloBarostat'] barostat.setTemperature(thermodynamic_state.temperature) parameter_name = barostat.Pressure() # Create integrator. integrator = mm.LangevinIntegrator(thermodynamic_state.temperature, self.collision_rate, self.timestep) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") logger.debug("LangevinDynamicMove: Context created, platform is %s" % context.getPlatform().getName()) # Set pressure, if barostat is included. if barostat is not None: context.setParameter(parameter_name, thermodynamic_state.pressure) if self.reassign_velocities: # Assign Maxwell-Boltzmann velocities. context.setVelocitiesToTemperature(thermodynamic_state.temperature) # Run dynamics. timer.start("step()") integrator.step(self.nsteps) timer.stop("step()") # Get updated sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.start("update_sampler_state") # Clean up. del context timer.report_timing() return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.LennardJonesFluid() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions, box_vectors=test.system.getDefaultPeriodicBoxVectors()) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin, pressure=1*u.atmospheres) >>> # Create a Monte Carlo Barostat move. >>> move = MonteCarloBarostatMove(nattempts=5) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Make sure system contains a barostat. system = sampler_state.system forces = { system.getForce(index).__class__.__name__: system.getForce(index) for index in range(system.getNumForces()) } old_barostat_frequency = None if 'MonteCarloBarostat' in forces: force = forces['MonteCarloBarostat'] force.setTemperature(thermodynamic_state.temperature) old_barostat_frequency = force.getFrequency() force.setFrequency(1) parameter_name = force.Pressure() else: # Add MonteCarloBarostat. force = mm.MonteCarloBarostat(thermodynamic_state.pressure, thermodynamic_state.temperature, 1) system.addForce(force) parameter_name = force.Pressure() # Create integrator. integrator = integrators.DummyIntegrator() # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) force.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # Set pressure. context.setParameter(parameter_name, thermodynamic_state.pressure) # Run update. # Note that ONE step of this integrator is equal to self.nsteps of velocity Verlet dynamics followed by Metropolis accept/reject. timer.start("step(1)") integrator.step(self.nattempts) timer.stop("step(1)") # Get sampler state. timer.start("update_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.stop("update_sampler_state") # DEBUG #print thermodynamics.volume(updated_sampler_state.box_vectors) # Clean up. del context # Restore frequency of barostat. if old_barostat_frequency: force.setFrequency(old_barostat_frequency) timer.report_timing() # Return updated sampler state. return updated_sampler_state
def apply(self, thermodynamic_state, sampler_state, platform=None): """ Apply the MCMC move. Parameters ---------- thermodynamic_state : ThermodynamicState The thermodynamic state to use when applying the MCMC move sampler_state : SamplerState The sampler state to apply the move to platform : simtk.openmm.Platform, optional, default = None If not None, the specified platform will be used. Returns ------- updated_sampler_state : SamplerState The updated sampler state Examples -------- >>> # Create a test system >>> import testsystems >>> test = testsystems.AlanineDipeptideVacuum() >>> # Create a sampler state. >>> sampler_state = SamplerState(system=test.system, positions=test.positions) >>> # Create a thermodynamic state. >>> from thermodynamics import ThermodynamicState >>> thermodynamic_state = ThermodynamicState(system=test.system, temperature=298*u.kelvin) >>> # Create an HMC move. >>> move = HMCMove(nsteps=10, timestep=0.5*u.femtoseconds) >>> # Perform one update of the sampler state. >>> updated_sampler_state = move.apply(thermodynamic_state, sampler_state) """ timer = Timer() # Create integrator. integrator = integrators.HMCIntegrator( temperature=thermodynamic_state.temperature, timestep=self.timestep, nsteps=self.nsteps) # Random number seed. seed = np.random.randint(_RANDOM_SEED_MAX) integrator.setRandomNumberSeed(seed) # Create context. timer.start("Context Creation") context = sampler_state.createContext(integrator, platform=platform) timer.stop("Context Creation") # Run dynamics. # Note that ONE step of this integrator is equal to self.nsteps of velocity Verlet dynamics followed by Metropolis accept/reject. timer.start("HMC integration") integrator.step(1) timer.stop("HMC integration") # Get sampler state. timer.start("updated_sampler_state") updated_sampler_state = SamplerState.createFromContext(context) timer.stop("updated_sampler_state") # Clean up. del context timer.report_timing() # Return updated sampler state. return updated_sampler_state