context = openmm.Context(testsystem.system, integrator) context.setPositions(testsystem.positions) openmm.LocalEnergyMinimizer.minimize(context) testsystem.positions = context.getState(getPositions=True).getPositions() del context, integrator # Benchmark integrators. for integrator_name in integrators_to_benchmark: if integrator_name == 'VerletIntegrator': integrator = openmm.VerletIntegrator(timestep) elif integrator_name == 'VelocityVerletIntegrator': integrator = integrators.VelocityVerletIntegrator(timestep) elif integrator_name == 'VVVRIntegrator': integrator = integrators.VVVRIntegrator(temperature=temperature, collision_rate=collision_rate, timestep=timestep) elif integrator_name == 'GHMCIntegrator': integrator = integrators.GHMCIntegrator(temperature=temperature, collision_rate=collision_rate, timestep=timestep) # Create system. context = openmm.Context(testsystem.system, integrator) context.setPositions(testsystem.positions) # Run one step. integrator.step(1) # Perform timing trials. elapsed_time = np.zeros([ntrials], np.float64) for trial in range(ntrials): initial_time = time.time() integrator.step(nsteps) final_time = time.time() elapsed_time[trial] = final_time - initial_time
hmc_integrators.guess_force_groups(system, nonbonded=1, fft=1, others=0) groups = [(0, 2), (1, 1)] idict = { "verlet": mm.VerletIntegrator(timestep), "langevin": mm.LangevinIntegrator(temperature, collision_rate, timestep), "vv": integrators.VelocityVerletIntegrator(timestep), "vvvr": integrators.VelocityVerletIntegrator(timestep), "ghmc10": integrators.GHMCIntegrator(temperature=temperature, collision_rate=collision_rate, timestep=timestep, steps_per_hmc=10), "ghmc20": integrators.GHMCIntegrator(temperature=temperature, collision_rate=collision_rate, timestep=timestep, steps_per_hmc=20), "ghmcrespa20": integrators.GHMCRESPA(temperature, steps_per_hmc=20, timestep=timestep, collision_rate=collision_rate, groups=groups) } factors = {"ghmc10": 10, "ghmc20": 20, "ghmcrespa20": 20}
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 >>> from openmmtools 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