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_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")
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_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_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_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")
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_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_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) 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_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(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_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()
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 mpicomm = dummympi.DummyMPIComm() 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) eq(replica_exchange.iteration, 200) replica_exchange.run()
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()
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_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_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()
# RUN PARALLEL TEMPERING SIMULATION #============================================================================================= output_filename = "repex.nc" # name of NetCDF file to store simulation output # If simulation file already exists, try to resume. import os.path resume = False if os.path.exists(output_filename): resume = True if resume: try: print "Attempting to resume existing simulation..." import repex simulation = repex.resume(output_filename) # Extend the simulation by a few iterations. niterations_to_extend = 10 simulation.extend(niterations_to_extend) except Exception as e: print "Could not resume existing simulation due to exception:" print e print "" resume = False if not resume: print "Starting new simulation..." # Set parallel tempering parameters