def test_repex_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas parameters = {"number_of_iterations": 5} rex = replica_exchange.ReplicaExchange.create(states, coordinates, nc_filename, parameters=parameters) rex.run() rex.extend(5) rex = resume(nc_filename) rex.run() eq(rex.__class__.__name__, "ReplicaExchange")
def test_hrex_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin powers = [2., 2., 4.] n_replicas = len(powers) oscillators = [ testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas) ] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 200} replica_exchange = hamiltonian_exchange.HamiltonianExchange.create( state, systems, positions, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_hrex_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin powers = [2., 2., 4.] n_replicas = len(powers) oscillators = [ testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas) ] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations": 5} rex = hamiltonian_exchange.HamiltonianExchange.create( state, systems, positions, nc_filename, parameters=parameters) rex.run() rex.extend(5) rex = resume(nc_filename) rex.run() eq(rex.__class__.__name__, "HamiltonianExchange")
def test_hrex_save_and_load(mpicomm): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin K0 = 100.0 # Units are automatically added by the testsystem K = [K0, K0 * 10., K0 * 1.] powers = [2., 2., 4.] n_replicas = len(K) oscillators = [ testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas) ] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations": 200} replica_exchange = hamiltonian_exchange.HamiltonianExchange.create( state, systems, positions, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename, mpicomm=mpicomm) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_check_positions(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 10} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() db = replica_exchange.database db.check_energies()
def test_harmonic_oscillators_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 200} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename, mpicomm=mpicomm) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_power_oscillators(): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin powers = [2., 2., 4.] n_replicas = len(powers) oscillators = [testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas)] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations":2000} replica_exchange = hamiltonian_exchange.HamiltonianExchange.create(state, systems, positions, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() u_permuted = replica_exchange.database.ncfile.variables["energies"][:] s = replica_exchange.database.ncfile.variables["states"][:] u = permute_energies(u_permuted, s) beta = (state.temperature * kB) ** -1. u0 = np.array([[testsystems.PowerOscillator.reduced_potential(beta, ho2.K, ho2.b, ho.K, ho.b) for ho in oscillators] for ho2 in oscillators]) l = np.log(u.mean(0)) l0 = np.log(u0) eq(l0, l, decimal=1)
def test_properties_all_testsystems(): testsystem_classes = testsystems.TestSystem.__subclasses__() logging.info("Testing analytical property computation:") for testsystem_class in testsystem_classes: class_name = testsystem_class.__name__ logging.info(class_name) print class_name # DEBUG testsystem = testsystem_class() property_list = testsystem.analytical_properties state = ThermodynamicState(temperature=300.0*u.kelvin, pressure=1.0*u.atmosphere, system=testsystem.system) if len(property_list) > 0: for property_name in property_list: method = getattr(testsystem, 'get_' + property_name) logging.info("%32s . %32s : %32s" % (class_name, property_name, str(method(state))))
def test_repex_multiple_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 2.0, T_min * 2.0] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas parameters = {"number_of_iterations": steps} rex = replica_exchange.ReplicaExchange.create(states, coordinates, nc_filename, parameters=parameters) rex.run() for repeat in range(repeats): replica_states0 = rex.replica_states Nij_proposed0 = rex.Nij_proposed Nij_accepted0 = rex.Nij_accepted sampler_states0 = rex.sampler_states rex.extend(steps) rex = resume(nc_filename) replica_states1 = rex.replica_states Nij_proposed1 = rex.Nij_proposed Nij_accepted1 = rex.Nij_accepted sampler_states1 = rex.sampler_states eq(replica_states0, replica_states1) eq(Nij_proposed0, Nij_proposed1) eq(Nij_accepted0, Nij_accepted1) rex.run()
def test_hrex_multiple_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin powers = [2., 2., 2.] n_replicas = len(powers) oscillators = [ testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas) ] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations": steps} rex = hamiltonian_exchange.HamiltonianExchange.create( state, systems, positions, nc_filename, parameters=parameters) rex.run() for repeat in range(repeats): replica_states0 = rex.replica_states Nij_proposed0 = rex.Nij_proposed Nij_accepted0 = rex.Nij_accepted sampler_states0 = rex.sampler_states rex.extend(steps) rex = resume(nc_filename) replica_states1 = rex.replica_states Nij_proposed1 = rex.Nij_proposed Nij_accepted1 = rex.Nij_accepted sampler_states1 = rex.sampler_states eq(replica_states0, replica_states1) eq(Nij_proposed0, Nij_proposed1) eq(Nij_accepted0, Nij_accepted1) rex.run()
def test_harmonic_oscillators(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 1000} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() u_permuted = replica_exchange.database.ncfile.variables["energies"][:] s = replica_exchange.database.ncfile.variables["states"][:] u = permute_energies(u_permuted, s) u0 = np.array([[ho.reduced_potential_expectation(s0, s1) for s1 in states] for s0 in states]) l0 = np.log( u0 ) # Compare on log scale because uncertainties are proportional to values l1 = np.log(u.mean(0)) eq(l0, l1, decimal=1)
def test_get_traj(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 300.0 * unit.kelvin T_i = [T_min, T_min * 1.01, T_min * 1.1] n_replicas = len(T_i) ho = testsystems.AlanineDipeptideExplicit() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 3} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() db = replica_exchange.database prmtop_filename = testsystems.get_data_filename( "data/alanine-dipeptide-explicit/alanine-dipeptide.prmtop") crd_filename = testsystems.get_data_filename( "data/alanine-dipeptide-explicit/alanine-dipeptide.crd") trj0 = md.load_restrt(crd_filename, top=prmtop_filename) db.set_traj(trj0) trj1 = db.get_traj(state_index=0) trj2 = db.get_traj(replica_index=0)
temperature = 300 * u.kelvin friction = 0.3 / u.picosecond timestep = 2.0 * u.femtosecond forcefield = app.ForceField("amber10.xml", "tip3p.xml") pdb = app.PDBFile(pdb_filename) model = app.modeller.Modeller(pdb.topology, pdb.positions) model.addSolvent(forcefield, padding=0.01 * u.nanometer) system = forcefield.createSystem(model.topology, nonbondedMethod=app.PME, nonbondedCutoff=1.0 * u.nanometers, constraints=app.HAngles) states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [model.getPositions()] * n_replicas #database = repex.netcdf_io.NetCDFDatabase(nc_filename, states, coordinates) #replica_exchange = ReplicaExchange(states, coordinates, nc_filename) #replica_exchange = ReplicaExchange.create_repex(states, coordinates, nc_filename, **{}) replica_exchange = ReplicaExchange.resume_repex(nc_filename, **{}) replica_exchange.number_of_iterations = 20 replica_exchange.run()
def subtest_mcmc_expectation(testsystem, move_set): if debug: print testsystem.__class__.__name__ print str(move_set) # Test settings. temperature = 298.0 * units.kelvin pressure = 1.0 * units.atmospheres nequil = 10 # number of equilibration iterations niterations = 20 # number of production iterations # Retrieve system and positions. [system, positions] = [testsystem.system, testsystem.positions] platform_name = 'Reference' from simtk.openmm import Platform platform = Platform.getPlatformByName(platform_name) # Compute properties. kB = units.BOLTZMANN_CONSTANT_kB * units.AVOGADRO_CONSTANT_NA kT = kB * temperature ndof = 3 * system.getNumParticles() - system.getNumConstraints() # Create thermodynamic state from repex.thermodynamics import ThermodynamicState thermodynamic_state = ThermodynamicState(system=testsystem.system, temperature=temperature, pressure=pressure) # Create MCMC sampler. from repex.mcmc import MCMCSampler sampler = MCMCSampler(thermodynamic_state, move_set=move_set, platform=platform) # Create sampler state. from repex.mcmc import SamplerState sampler_state = SamplerState(system=testsystem.system, positions=testsystem.positions, platform=platform) # Equilibrate for iteration in range(nequil): #print "equilibration iteration %d / %d" % (iteration, nequil) # Update sampler state. sampler_state = sampler.run(sampler_state, 1) # Accumulate statistics. x_n = np.zeros( [niterations], np.float64 ) # x_n[i] is the x position of atom 1 after iteration i, in angstroms potential_n = np.zeros( [niterations], np.float64 ) # potential_n[i] is the potential energy after iteration i, in kT kinetic_n = np.zeros( [niterations], np.float64 ) # kinetic_n[i] is the kinetic energy after iteration i, in kT temperature_n = np.zeros( [niterations], np.float64 ) # temperature_n[i] is the instantaneous kinetic temperature from iteration i, in K volume_n = np.zeros( [niterations], np.float64) # volume_n[i] is the volume from iteration i, in K for iteration in range(niterations): if debug: print "iteration %d / %d" % (iteration, niterations) # Update sampler state. sampler_state = sampler.run(sampler_state, 1) # Get statistics. potential_energy = sampler_state.potential_energy kinetic_energy = sampler_state.kinetic_energy total_energy = sampler_state.total_energy instantaneous_temperature = kinetic_energy * 2.0 / ndof / ( units.BOLTZMANN_CONSTANT_kB * units.AVOGADRO_CONSTANT_NA) volume = sampler_state.volume #print "potential %8.1f kT | kinetic %8.1f kT | total %8.1f kT | volume %8.3f nm^3 | instantaneous temperature: %8.1f K" % (potential_energy/kT, kinetic_energy/kT, total_energy/kT, volume/(units.nanometers**3), instantaneous_temperature/units.kelvin) # Accumulate statistics. x_n[iteration] = sampler_state.positions[0, 0] / units.angstroms potential_n[iteration] = potential_energy / kT kinetic_n[iteration] = kinetic_energy / kT temperature_n[iteration] = instantaneous_temperature / units.kelvin volume_n[iteration] = volume / (units.nanometers**3) # Compute expected statistics. if ('get_potential_expectation' in dir(testsystem)): # Skip this check if the std dev is zero. skip_test = False if (potential_n.std() == 0.0): skip_test = True if debug: print "Skipping potential test since variance is zero." if not skip_test: potential_expectation = testsystem.get_potential_expectation( thermodynamic_state) / kT potential_mean = potential_n.mean() g = timeseries.statisticalInefficiency(potential_n, fast=True) dpotential_mean = potential_n.std() / np.sqrt(niterations / g) potential_error = potential_mean - potential_expectation nsigma = abs(potential_error) / dpotential_mean test_passed = True if (nsigma > NSIGMA_CUTOFF): test_passed = False if debug or (test_passed is False): print "Potential energy expectation" print "observed %10.5f +- %10.5f kT | expected %10.5f | error %10.5f +- %10.5f (%.1f sigma)" % ( potential_mean, dpotential_mean, potential_expectation, potential_error, dpotential_mean, nsigma) if test_passed: print "TEST PASSED" else: print "TEST FAILED" print "----------------------------------------------------------------------------" if ('get_volume_expectation' in dir(testsystem)): # Skip this check if the std dev is zero. skip_test = False if (volume_n.std() == 0.0): skip_test = True if debug: print "Skipping volume test." if not skip_test: volume_expectation = testsystem.get_volume_expectation( thermodynamic_state) / (units.nanometers**3) volume_mean = volume_n.mean() g = timeseries.statisticalInefficiency(volume_n, fast=True) dvolume_mean = volume_n.std() / np.sqrt(niterations / g) volume_error = volume_mean - volume_expectation nsigma = abs(volume_error) / dvolume_mean test_passed = True if (nsigma > NSIGMA_CUTOFF): test_passed = False if debug or (test_passed is False): print "Volume expectation" print "observed %10.5f +- %10.5f kT | expected %10.5f | error %10.5f +- %10.5f (%.1f sigma)" % ( volume_mean, dvolume_mean, volume_expectation, volume_error, dvolume_mean, nsigma) if test_passed: print "TEST PASSED" else: print "TEST FAILED" print "----------------------------------------------------------------------------"
T_i = [ T_min + (T_max - T_min) * (np.exp(float(i) / float(n_replicas-1)) - 1.0) / (np.e - 1.0) for i in range(n_replicas) ] pdb_filename = "./1vii.pdb" temperature = 300 * u.kelvin friction = 0.3 / u.picosecond timestep = 2.0 * u.femtosecond forcefield = app.ForceField("amber10.xml", "tip3p.xml") pdb = app.PDBFile(pdb_filename) model = app.modeller.Modeller(pdb.topology, pdb.positions) model.addSolvent(forcefield, padding=0.01 * u.nanometer) system = forcefield.createSystem(model.topology, nonbondedMethod=app.PME, nonbondedCutoff=1.0 * u.nanometers, constraints=app.HAngles) states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [model.getPositions()] * n_replicas #database = repex.netcdf_io.NetCDFDatabase(nc_filename, states, coordinates) #replica_exchange = ReplicaExchange(states, coordinates, nc_filename) #replica_exchange = ReplicaExchange.create_repex(states, coordinates, nc_filename, mpicomm=MPI.COMM_WORLD, **{}) replica_exchange = ReplicaExchange.resume_repex(nc_filename, mpicomm=MPI.COMM_WORLD, **{}) replica_exchange.number_of_iterations = 30 replica_exchange.run()
#context = openmm.Context(system, integrator, platform, options) #context.setPositions(positions) #context.applyConstraints(tolerance) #print context.getPlatform().getName() # Create MCMC move set. from repex.mcmc import HMCMove, GHMCMove, LangevinDynamicsMove, MonteCarloBarostatMove #move_set = [ GHMCMove(nsteps=10), HMCMove(nsteps=10) ] move_set = [GHMCMove(), MonteCarloBarostatMove()] #move_set = [ GHMCMove() ] #move_set = [ LangevinDynamicsMove() ] # Create thermodynamic state from repex.thermodynamics import ThermodynamicState thermodynamic_state = ThermodynamicState(system=testsystem.system, temperature=temperature, pressure=pressure) # Create MCMC sampler. from repex.mcmc import MCMCSampler sampler = MCMCSampler(thermodynamic_state, move_set=move_set, platform=platform) # Create sampler state. from repex.mcmc import MCMCSamplerState sampler_state = MCMCSamplerState(system=testsystem.system, positions=testsystem.positions) # Equilibrate for iteration in range(nequil):