def test_parallel_tempering_save_and_load(mpicomm):

    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 10.0 * unit.kelvin
    n_temps = 3

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    parameters = {"number_of_iterations": 200}
    replica_exchange = ParallelTempering.create(system,
                                                coordinates,
                                                nc_filename,
                                                T_min=T_min,
                                                T_max=T_max,
                                                n_temps=n_temps,
                                                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()
Esempio n. 2
0
def test_double_set_thermodynamic_states():
    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 10.0 * unit.kelvin
    n_temps = 3

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    mpicomm = dummympi.DummyMPIComm()
    parameters = {"number_of_iterations": 3}
    replica_exchange = ParallelTempering.create(system,
                                                coordinates,
                                                nc_filename,
                                                T_min=T_min,
                                                T_max=T_max,
                                                n_temps=n_temps,
                                                mpicomm=mpicomm,
                                                parameters=parameters)

    eq(replica_exchange.n_replicas, n_temps)

    replica_exchange.run()

    states = replica_exchange.thermodynamic_states
    replica_exchange.database.thermodynamic_states = states
def test_parallel_tempering_explicit_temperature_input(mpicomm):

    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T0 = 1.0 * unit.kelvin
    temperatures = [T0, T0 * 2, T0 * 4]
    n_temps = len(temperatures)

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    parameters = {"number_of_iterations": 100}
    replica_exchange = ParallelTempering.create(system,
                                                coordinates,
                                                nc_filename,
                                                temperatures=temperatures,
                                                mpicomm=mpicomm,
                                                parameters=parameters)
    replica_exchange.run()

    eq(replica_exchange.n_replicas, n_temps)
Esempio n. 4
0
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")
Esempio n. 5
0
def test_parallel_tempering_save_and_load():

    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 10.0 * unit.kelvin
    n_temps = 3

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    parameters = {"number_of_iterations": 5}
    rex = parallel_tempering.ParallelTempering.create(system,
                                                      coordinates,
                                                      nc_filename,
                                                      T_min=T_min,
                                                      T_max=T_max,
                                                      n_temps=n_temps,
                                                      parameters=parameters)
    rex.run()

    rex.extend(5)

    rex = resume(nc_filename)
    rex.run()

    eq(rex.__class__.__name__, "ParallelTempering")
Esempio n. 6
0
def test_harmonic_oscillators_fewer_coordinates_than_states():
    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]

    mpicomm = dummympi.DummyMPIComm()
    parameters = {"number_of_iterations": 10}
    replica_exchange = ReplicaExchange.create(states,
                                              coordinates,
                                              nc_filename,
                                              mpicomm=mpicomm,
                                              parameters=parameters)
    replica_exchange.run()
Esempio n. 7
0
def test_parallel_tempering_pressure():
    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 10.0 * unit.kelvin
    n_temps = 3
    pressure = 1.0 * unit.atmospheres

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    mpicomm = dummympi.DummyMPIComm()
    parameters = {"number_of_iterations": 4}
    replica_exchange = ParallelTempering.create(system,
                                                coordinates,
                                                nc_filename,
                                                T_min=T_min,
                                                T_max=T_max,
                                                n_temps=n_temps,
                                                pressure=pressure,
                                                mpicomm=mpicomm,
                                                parameters=parameters)

    eq(replica_exchange.n_replicas, n_temps)

    replica_exchange.run()
Esempio n. 8
0
def test_harmonic_oscillators_save_and_load(mpicomm):
    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": 50}
    replica_exchange = ReplicaExchange.create(states,
                                              coordinates,
                                              nc_filename,
                                              mpicomm=mpicomm,
                                              parameters=parameters)
    replica_exchange.run()

    replica_exchange.extend(50)

    replica_exchange = resume(nc_filename, mpicomm=mpicomm)
    replica_exchange.run()
Esempio n. 9
0
def test_output_diagnostics():
    tempdir = tempfile.mkdtemp()
    nc_filename = tempdir + "/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()

    db.output_diagnostics(tempdir + "/diagnostics/")
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_parallel_tempering_multiple_save_and_load():

    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 2.0 * unit.kelvin
    n_temps = 3

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    parameters = {"number_of_iterations": steps}
    rex = parallel_tempering.ParallelTempering.create(system,
                                                      coordinates,
                                                      nc_filename,
                                                      T_min=T_min,
                                                      T_max=T_max,
                                                      n_temps=n_temps,
                                                      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()
Esempio n. 12
0
def test_parallel_tempering():
    nc_filename = tempfile.mkdtemp() + "/out.nc"

    T_min = 1.0 * unit.kelvin
    T_max = 10.0 * unit.kelvin
    n_temps = 3

    ho = testsystems.HarmonicOscillator()

    system = ho.system
    positions = ho.positions

    coordinates = [positions] * n_temps

    mpicomm = dummympi.DummyMPIComm()
    parameters = {"number_of_iterations": 1000}
    replica_exchange = ParallelTempering.create(system,
                                                coordinates,
                                                nc_filename,
                                                T_min=T_min,
                                                T_max=T_max,
                                                n_temps=n_temps,
                                                mpicomm=mpicomm,
                                                parameters=parameters)

    eq(replica_exchange.n_replicas, n_temps)

    replica_exchange.run()

    u_permuted = replica_exchange.database.ncfile.variables["energies"][:]
    s = replica_exchange.database.ncfile.variables["states"][:]

    u = permute_energies(u_permuted, s)

    states = replica_exchange.thermodynamic_states
    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)
Esempio n. 13
0
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)
Esempio n. 14
0
tolerance = 1.0e-8  # constraint tolerance

nsteps = 1000  # number of timesteps per iteration
nequil = 50  # number of equilibration iterations
niterations = 100  # number of production iterations

# Select system:
#testsystem = testsystems.MolecularIdealGas()
#testsystem = testsystems.AlanineDipeptideVacuum(constraints=None)
#testsystem = testsystems.AlanineDipeptideVacuum(constraints=app.HBonds)
#testsystem = testsystems.AlanineDipeptideImplicit(constraints=app.HBonds)
#testsystem = testsystems.AlanineDipeptideExplicit(constraints=app.HBonds, rigid_water=True)
#testsystem = testsystems.Diatom(constraint=True, use_central_potential=True)
#testsystem = testsystems.ConstraintCoupledHarmonicOscillator()
#testsystem = testsystems.LysozymeImplicit(flexibleConstraints=False, shake=True)
testsystem = testsystems.HarmonicOscillator()
#testsystem = testsystems.HarmonicOscillatorArray(N=16)
#testsystem = testsystems.WaterBox(constrain=True, flexible=False)

# Retrieve system and positions.
[system, positions] = [testsystem.system, testsystem.positions]

#velocities = generateMaxwellBoltzmannVelocities(system, temperature)
ndof = 3 * system.getNumParticles() - system.getNumConstraints()

# Select integrator:
#integrator = openmm.LangevinIntegrator(temperature, collision_rate, timestep)
#integrator = integrators.AndersenVelocityVerletIntegrator(temperature=temperature, timestep=timestep)
#integrator = integrators.MetropolisMonteCarloIntegrator(timestep=timestep, temperature=temperature, sigma=0.01*units.angstroms)
#integrator = integrators.HMCIntegrator(timestep=timestep, temperature=temperature)
#integrator = integrators.VVVRIntegrator(timestep=timestep, temperature=temperature, collision_rate=collision_rate)