Example #1
0
def test_plumed_md():
    with tmpdir(__name__, 'test_plumed_md') as dirname:
        ff = get_ff_water32()
        kappa, V0 = 1.6 * kjmol / angstrom**6, ff.system.cell.volume
        # PLUMED input commands
        commands = "vol: VOLUME\n"
        commands += "RESTRAINT ARG=vol AT=%.20f KAPPA=%.20f LABEL=restraint\n"%\
            (V0/nanometer**3,kappa/kjmol*nanometer**6)
        commands += "PRINT STRIDE=1 ARG=vol FILE=%s\n" % (os.path.join(
            dirname, 'cv.log'))
        commands += "FLUSH STRIDE=1\n"
        # Write PLUMED commands to file
        fn = os.path.join(dirname, 'plumed.dat')
        with open(fn, 'w') as f:
            f.write(commands)
        # Setup Plumed
        timestep = 1.0 * femtosecond
        plumed = ForcePartPlumed(ff.system, timestep=timestep, fn=fn)
        ff.add_part(plumed)
        # Setup integrator with a barostat, so plumed has to compute forces
        # more than once per timestep
        tbc = TBCombination(MTKBarostat(ff, 300, 1 * bar), NHCThermostat(300))
        verlet = VerletIntegrator(ff, timestep, hooks=[tbc, plumed])
        # Run a short MD simulation, keeping track of the CV (volume in this case)
        cvref = [ff.system.cell.volume]
        for i in range(4):
            verlet.run(1)
            cvref.append(ff.system.cell.volume)
        # Read the PLUMED output and compare with reference
        cv = np.loadtxt(os.path.join(dirname, 'cv.log'))
        assert cv.shape[0] == 5
        assert np.allclose(cv[:, 0] * picosecond, np.arange(5) * timestep)
        assert np.allclose(cv[:, 1] * nanometer**3, cvref)
Example #2
0
def test_basic_langevin_nvt():
    thermostat = LangevinThermostat(300)
    nvt = VerletIntegrator(get_ff_water32(),
                           1.0 * femtosecond,
                           hooks=thermostat)
    nvt.run(5)
    assert nvt.counter == 5
Example #3
0
def test_spectrum_online_blind():
    # Setup a test FF
    ff = get_ff_water32()
    spectrum = Spectrum(bsize=2)
    nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=spectrum)
    nve.run(5)
    assert nve.counter == 5
Example #4
0
def test_spectrum_online():
    for bsize in 2, 4, 5:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        f = h5.File(
            'yaff.analysis.test.test_spectrum.test_spectrum_online_%i.h5' %
            bsize,
            driver='core',
            backing_store=False)
        try:
            hdf5 = HDF5Writer(f)
            spectrum0 = Spectrum(f, bsize=bsize)
            nve = VerletIntegrator(ff,
                                   1.0 * femtosecond,
                                   hooks=[hdf5, spectrum0])
            nve.run(5)
            assert nve.counter == 5
            # Also run an off-line spectrum and compare
            spectrum1 = Spectrum(f, bsize=bsize)
            assert abs(spectrum0.timestep - spectrum1.timestep) < 1e-10
            assert abs(spectrum0.amps - spectrum1.amps).max() < 1e-10
            assert abs(spectrum0.freqs - spectrum1.freqs).max() < 1e-10
            assert abs(spectrum0.ac - spectrum1.ac).max() < 1e-10
            assert abs(spectrum0.time - spectrum1.time).max() < 1e-10
            assert f['trajectory/vel_spectrum'].attrs['nfft'] == 3 * 3 * 32 * (
                6 / bsize)
        finally:
            f.close()
Example #5
0
def test_qn_5steps():
    opt = QNOptimizer(CartesianDOF(get_ff_water32()))
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #6
0
def test_xyz():
    xyz = XYZWriter('/dev/null')
    nve = VerletIntegrator(get_ff_water32(), 1.0*femtosecond, hooks=[xyz])
    com_vel = np.dot(nve.masses, nve.vel)/nve.masses.sum()
    nve.run(15)
    com_vel = np.dot(nve.masses, nve.vel)/nve.masses.sum()
    assert nve.counter == 15
Example #7
0
def test_spectrum_online_blind():
    # Setup a test FF
    ff = get_ff_water32()
    spectrum = Spectrum(bsize=2)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=spectrum)
    nve.run(5)
    assert nve.counter == 5
Example #8
0
def test_xyz():
    xyz = XYZWriter('/dev/null')
    nve = VerletIntegrator(get_ff_water32(), 1.0 * femtosecond, hooks=[xyz])
    com_vel = np.dot(nve.masses, nve.vel) / nve.masses.sum()
    nve.run(15)
    com_vel = np.dot(nve.masses, nve.vel) / nve.masses.sum()
    assert nve.counter == 15
Example #9
0
def test_amb():
    nve = VerletIntegrator(get_ff_water32(),
                           1.0 * femtosecond,
                           hooks=TBCombination(McDonaldBarostat(300, 1 * bar),
                                               AndersenThermostat(300)))
    nve.run(5)
    assert nve.counter == 5
Example #10
0
def test_qn_5steps():
    opt = QNOptimizer(CartesianDOF(get_ff_water32()))
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #11
0
def test_h5_flush():
    # Test if we can read intermediate hdf5 files that are flushed
    # This test relies on the `h5ls` command being  available in the command line.
    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix="yaff", prefix="nve_water_32")
    # Setup a test FF
    ff = get_ff_water32()
    # Make two different hdf5 hooks
    f = h5.File("%s/output.h5" % dn_tmp)
    hdf5 = HDF5Writer(f)
    f_flushed = h5.File("%s/output_flushed.h5" % dn_tmp)
    hdf5_flushed = HDF5Writer(f_flushed, flush=4)
    # Run a test simulation
    nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=[hdf5, hdf5_flushed])
    nve.run(5)
    try:  # Actual test
        # Check that we can read the h5 files from the command line
        # This should not work
        p = Popen(["h5ls", "%s/output.h5" % dn_tmp], stdout=PIPE, stderr=PIPE)
        result = p.communicate()
        assert "unable" in result[1]
        # This should work
        p = Popen(["h5ls", "%s/output_flushed.h5" % dn_tmp], stdout=PIPE, stderr=PIPE)
        result = p.communicate()
        assert "trajectory" in result[0]
    finally:  # Cleanup
        f.close()
        f_flushed.close()
        shutil.rmtree(dn_tmp)
Example #12
0
def test_diff_bsize():
    ff = get_ff_water32()
    select = ff.system.get_indexes('O')
    diff = Diffusion(None, select=select, bsize=3, mult=2)
    nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=diff)
    nve.run(10)
    assert diff.msdcounters[0] == 7
    assert diff.msdcounters[1] == 2
Example #13
0
def test_diff_bsize():
    ff = get_ff_water32()
    select = ff.system.get_indexes('O')
    diff = Diffusion(None, select=select, bsize=3, mult=2)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=diff)
    nve.run(10)
    assert diff.msdcounters[0] == 7
    assert diff.msdcounters[1] == 2
Example #14
0
def test_spectrum_online_weights():
    # Setup a test FF
    ff = get_ff_water32()
    ff.system.set_standard_masses()
    weights = np.array([ff.system.masses] * 3).T
    spectrum = Spectrum(bsize=2, weights=weights)
    nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=spectrum)
    nve.run(5)
    assert nve.counter == 5
Example #15
0
def test_cg_5steps():
    dof = CartesianDOF(get_ff_water32())
    dof.check_delta()
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #16
0
def test_cg_5steps_partial():
    dof = CartesianDOF(get_ff_water32(), select=[0, 1, 2, 3, 4, 5])
    dof.check_delta()
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #17
0
def test_hdf5_start():
    with h5.File('yaff.sampling.test.test_verlet.test_hdf5_start.h5', driver='core', backing_store=False) as f:
        hdf5 = HDF5Writer(f, start=2)
        nve = VerletIntegrator(get_ff_water32(), 1.0*femtosecond, hooks=hdf5)
        nve.run(5)
        assert nve.counter == 5
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 4
        assert f['trajectory/counter'][3] == 5
Example #18
0
def test_qn_5steps_initial_hessian():
    ff = get_ff_water32()
    hessian = estimate_cart_hessian(ff)
    opt = QNOptimizer(CartesianDOF(ff), hessian0=hessian)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #19
0
def test_spectrum_online_weights():
    # Setup a test FF
    ff = get_ff_water32()
    ff.system.set_standard_masses()
    weights = np.array([ff.system.masses]*3).T
    spectrum = Spectrum(bsize=2, weights=weights)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=spectrum)
    nve.run(5)
    assert nve.counter == 5
Example #20
0
def test_cg_iso_cell_5steps():
    dof = IsoCellDOF(get_ff_water32())
    dof.check_delta
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #21
0
def test_cg_iso_cell_5steps():
    dof = IsoCellDOF(get_ff_water32())
    dof.check_delta
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #22
0
def test_cg_5steps_partial():
    dof = CartesianDOF(get_ff_water32(), select=[0, 1, 2, 3, 4, 5])
    dof.check_delta()
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #23
0
def test_cg_5steps():
    dof = CartesianDOF(get_ff_water32())
    dof.check_delta()
    opt = CGOptimizer(dof)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #24
0
def test_qn_5steps_initial_hessian():
    ff = get_ff_water32()
    hessian = estimate_cart_hessian(ff)
    opt = QNOptimizer(CartesianDOF(ff), hessian0=hessian)
    epot0 = opt.epot
    opt.run(5)
    epot1 = opt.epot
    assert opt.counter == 5
    assert epot1 < epot0
Example #25
0
def test_cg_hdf5():
    with h5.File(__name__ + '.test_cg_hdf5.h5', driver='core', backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        opt = CGOptimizer(CartesianDOF(get_ff_water32()), hooks=hdf5)
        opt.run(15)
        assert opt.counter == 15
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 16
        assert f['trajectory/counter'][15] == 15
Example #26
0
def test_rdf2_online_blind():
    # Setup a test FF and run simulation without any HDF5 file
    ff = get_ff_water32()
    select0 = ff.system.get_indexes('O')
    select1 = ff.system.get_indexes('H')
    rdf = RDF(4.5*angstrom, 0.1*angstrom, select0=select0, select1=select1)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=rdf)
    nve.run(5)
    assert nve.counter == 5
    assert rdf.nsample == 6
Example #27
0
def test_rdf2_online_blind():
    # Setup a test FF and run simulation without any HDF5 file
    ff = get_ff_water32()
    select0 = ff.system.get_indexes('O')
    select1 = ff.system.get_indexes('H')
    rdf = RDF(4.5*angstrom, 0.1*angstrom, select0=select0, select1=select1)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=rdf)
    nve.run(5)
    assert nve.counter == 5
    assert rdf.nsample == 6
Example #28
0
def test_cg_until_converged():
    opt = CGOptimizer(CartesianDOF(get_ff_water32(), gpos_rms=1e-1, dpos_rms=None))
    assert opt.dof.th_gpos_rms == 1e-1
    assert opt.dof.th_dpos_rms is None
    opt.run()
    assert opt.dof.conv_count == 0
    assert opt.dof.conv_val < 1
    assert opt.dof.conv_worst.startswith('gpos_')
    assert opt.dof.gpos_max < 1e-1*3
    assert opt.dof.gpos_rms < 1e-1
Example #29
0
def test_cg_hdf5():
    with h5.File(__name__ + '.test_cg_hdf5.h5',
                 driver='core',
                 backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        opt = CGOptimizer(CartesianDOF(get_ff_water32()), hooks=hdf5)
        opt.run(15)
        assert opt.counter == 15
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 16
        assert f['trajectory/counter'][15] == 15
Example #30
0
def test_cg_until_converged():
    opt = CGOptimizer(
        CartesianDOF(get_ff_water32(), gpos_rms=1e-1, dpos_rms=None))
    assert opt.dof.th_gpos_rms == 1e-1
    assert opt.dof.th_dpos_rms is None
    opt.run()
    assert opt.dof.conv_count == 0
    assert opt.dof.conv_val < 1
    assert opt.dof.conv_worst.startswith('gpos_')
    assert opt.dof.gpos_max < 1e-1 * 3
    assert opt.dof.gpos_rms < 1e-1
Example #31
0
def test_xyz_select():
    with tmpdir(__name__, 'test_xyz_select') as dn:
        fn_xyz = os.path.join(dn, 'foobar.xyz')
        xyz = XYZWriter(fn_xyz, select=[0,1,2])
        nve = VerletIntegrator(get_ff_water32(), 1.0*femtosecond, hooks=[xyz])
        nve.run(15)
        assert os.path.isfile(fn_xyz)
        assert nve.counter == 15
        ## Ugly hack to make tests pass on Windows. The root cause is that the SliceReader
        ## in molmod.io.common is poorly written.
        xyz.xyz_writer._auto_close = False
        xyz.xyz_writer._f.close()
Example #32
0
def test_hdf5_step():
    f = h5.File('yaff.sampling.test.test_verlet.test_hdf5_step.h5', driver='core', backing_store=False)
    try:
        hdf5 = HDF5Writer(f, step=2)
        nve = VerletIntegrator(get_ff_water32(), 1.0*femtosecond, hooks=hdf5)
        nve.run(5)
        assert nve.counter == 5
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 3
        assert f['trajectory/counter'][2] == 4
    finally:
        f.close()
Example #33
0
def run_nve_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=hdf5)
            nve.run(5)
            assert nve.counter == 5
            yield dn_tmp, nve, f
Example #34
0
def get_opt_water32():
    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix='yaff', prefix='opt_water_32')
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    f = h5.File('%s/output.h5' % dn_tmp)
    hdf5 = HDF5Writer(f)
    opt = CGOptimizer(FullCellDOF(ff), hooks=hdf5)
    opt.run(5)
    assert opt.counter == 5
    return dn_tmp, opt, hdf5.f
Example #35
0
def run_opt_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            opt = CGOptimizer(FullCellDOF(ff), hooks=hdf5)
            opt.run(5)
            assert opt.counter == 5
            yield dn_tmp, opt, f
Example #36
0
def run_opt_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            opt = CGOptimizer(FullCellDOF(ff), hooks=hdf5)
            opt.run(5)
            assert opt.counter == 5
            yield dn_tmp, opt, f
Example #37
0
def get_nve_water32():
    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix='yaff', prefix='nve_water_32')
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    f = h5.File('%s/output.h5' % dn_tmp)
    hdf5 = HDF5Writer(f)
    nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=hdf5)
    nve.run(5)
    assert nve.counter == 5
    return dn_tmp, nve, hdf5.f
Example #38
0
def test_cg_hdf5():
    f = h5.File('yaff.sampling.test.test_opt.test_cg_hdf5.h5', driver='core', backing_store=False)
    try:
        hdf5 = HDF5Writer(f)
        opt = CGOptimizer(CartesianDOF(get_ff_water32()), hooks=hdf5)
        opt.run(15)
        assert opt.counter == 15
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 16
        assert f['trajectory/counter'][15] == 15
    finally:
        f.close()
Example #39
0
def run_nve_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=hdf5)
            nve.run(5)
            assert nve.counter == 5
            yield dn_tmp, nve, f
Example #40
0
def run_nvt_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            thermostat = LangevinThermostat(temp=300)
            nvt = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, thermostat])
            nvt.run(5)
            assert nvt.counter == 5
            yield dn_tmp, nvt, f
Example #41
0
def get_nvt_water32():
    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix='yaff', prefix='nvt_water_32')
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    f = h5.File('%s/output.h5' % dn_tmp)
    hdf5 = HDF5Writer(f)
    thermostat = LangevinThermostat(temp=300)
    nvt = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, thermostat])
    nvt.run(5)
    assert nvt.counter == 5
    return dn_tmp, nvt, hdf5.f
Example #42
0
def test_cg_hdf5():
    f = h5.File('yaff.sampling.test.test_opt.test_cg_hdf5.h5',
                driver='core',
                backing_store=False)
    try:
        hdf5 = HDF5Writer(f)
        opt = CGOptimizer(CartesianDOF(get_ff_water32()), hooks=hdf5)
        opt.run(15)
        assert opt.counter == 15
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 16
        assert f['trajectory/counter'][15] == 15
    finally:
        f.close()
Example #43
0
def test_hdf5():
    f = h5.File('yaff.sampling.test.test_verlet.test_hdf5.h5',
                driver='core',
                backing_store=False)
    try:
        hdf5 = HDF5Writer(f)
        nve = VerletIntegrator(get_ff_water32(), 1.0 * femtosecond, hooks=hdf5)
        nve.run(15)
        assert nve.counter == 15
        check_hdf5_common(hdf5.f)
        assert get_last_trajectory_row(f['trajectory']) == 16
        assert f['trajectory/counter'][15] == 15
    finally:
        f.close()
Example #44
0
def run_nvt_water32(suffix, prefix):
    # Work in a temporary directory
    with tmpdir(suffix, prefix) as dn_tmp:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('%s/output.h5' % dn_tmp) as f:
            hdf5 = HDF5Writer(f)
            thermostat = LangevinThermostat(temp=300)
            nvt = VerletIntegrator(ff,
                                   1.0 * femtosecond,
                                   hooks=[hdf5, thermostat])
            nvt.run(5)
            assert nvt.counter == 5
            yield dn_tmp, nvt, f
Example #45
0
def test_rdf1_online():
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    with h5.File(__name__ + 'test_rdf1_online.h5', driver='core', backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        select = ff.system.get_indexes('O')
        rdf0 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select)
        nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, rdf0])
        nve.run(5)
        assert nve.counter == 5
        # Also run an off-line rdf and compare
        rdf1 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select)
        assert rdf0.nsample == rdf1.nsample
        assert abs(rdf0.d - rdf1.d).max() < 1e-10
        assert abs(rdf0.rdf - rdf1.rdf).max() < 1e-10
Example #46
0
def test_rdf1_online():
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    with h5.File(__name__ + 'test_rdf1_online.h5', driver='core', backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        select = ff.system.get_indexes('O')
        rdf0 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select)
        nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, rdf0])
        nve.run(5)
        assert nve.counter == 5
        # Also run an off-line rdf and compare
        rdf1 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select)
        assert rdf0.nsample == rdf1.nsample
        assert abs(rdf0.d - rdf1.d).max() < 1e-10
        assert abs(rdf0.rdf - rdf1.rdf).max() < 1e-10
Example #47
0
def test_h5_flush():
    # Test if we can read intermediate hdf5 files that are flushed.

    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix='yaff', prefix='nve_water_32')
    try:
        # Setup a test FF
        ff = get_ff_water32()
        # Make two different hdf5 hooks
        with h5.File('%s/output.h5' % dn_tmp) as f, \
             h5.File('%s/output_flushed.h5' % dn_tmp) as f_flushed:
            hdf5 = HDF5Writer(f)
            hdf5_flushed = HDF5Writer(f_flushed, flush=4)
            # Run a test simulation
            nve = VerletIntegrator(ff,
                                   1.0 * femtosecond,
                                   hooks=[hdf5, hdf5_flushed])
            nve.run(5)

            # Actual tests

            # 1) Check that we can't read the trajectory group in the h5 file without flushing.
            # This must be done in a subprocess, otherwise the HDF5 library will fake
            # flushing while it does not really flush to disk.
            command = [
                'python', '-c',
                'import h5py as h5; f = h5.File(\'%s/output.h5\', '
                r'); f.close()' % dn_tmp
            ]
            p = Popen(command, stdout=PIPE, stderr=STDOUT)
            output = p.communicate()[0]
            assert 'IOError' in output
            assert p.returncode != 0

            # 2) This should work in a subprocess.
            command = [
                'python', '-c',
                'import h5py as h5; f = h5.File(\'%s/output_flushed.h5\', '
                r'); f.close()' % dn_tmp
            ]
            p = Popen(command, stdout=PIPE, stderr=STDOUT)
            output = p.communicate()[0]
            assert 'IOError' not in output
            assert p.returncode == 0
    finally:
        shutil.rmtree(dn_tmp)
Example #48
0
def test_spectrum_online():
    for bsize in 2, 4, 5:
        # Setup a test FF
        ff = get_ff_water32()
        # Run a test simulation
        with h5.File('yaff.analysis.test.test_spectrum.test_spectrum_online_%i.h5' % bsize, driver='core', backing_store=False) as f:
            hdf5 = HDF5Writer(f)
            spectrum0 = Spectrum(f, bsize=bsize)
            nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, spectrum0])
            nve.run(5)
            assert nve.counter == 5
            # Also run an off-line spectrum and compare
            spectrum1 = Spectrum(f, bsize=bsize)
            assert abs(spectrum0.timestep - spectrum1.timestep) < 1e-10
            assert abs(spectrum0.amps - spectrum1.amps).max() < 1e-10
            assert abs(spectrum0.freqs - spectrum1.freqs).max() < 1e-10
            assert abs(spectrum0.ac - spectrum1.ac).max() < 1e-10
            assert abs(spectrum0.time - spectrum1.time).max() < 1e-10
            assert f['trajectory/vel_spectrum'].attrs['nfft'] == 3*3*32*(6//bsize)
Example #49
0
def test_diff_online():
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    with h5.File('yaff.analysis.test.test_diffusion.test_diff_online.h5', driver='core', backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        select = ff.system.get_indexes('O')
        diff0 = Diffusion(f, select=select)
        nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, diff0])
        nve.run(5)
        assert nve.counter == 5
        # Also run an off-line rdf and compare
        diff1 = Diffusion(f, select=select)
        assert abs(diff0.A - diff1.A) < 1e-10
        assert abs(diff0.B - diff1.B) < 1e-10
        assert abs(diff0.time - diff1.time).max() < 1e-10
        assert abs(diff0.msds - diff1.msds).max() < 1e-10
        assert abs(diff0.msdsums - diff1.msdsums).max() < 1e-10
        assert abs(diff0.msdcounters - diff1.msdcounters).max() < 1e-10
Example #50
0
def test_rdf2_online():
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    f = h5.File('yaff.analysis.test.test_rdf.test_rdf2_online.h5', driver='core', backing_store=False)
    try:
        hdf5 = HDF5Writer(f)
        select0 = ff.system.get_indexes('O')
        select1 = ff.system.get_indexes('H')
        rdf0 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select0, select1=select1)
        nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5, rdf0])
        nve.run(5)
        assert nve.counter == 5
        # Also run an off-line rdf and compare
        rdf1 = RDF(4.5*angstrom, 0.1*angstrom, f, select0=select0, select1=select1)
        assert rdf0.nsample == rdf1.nsample
        assert abs(rdf0.d - rdf1.d).max() < 1e-10
        assert abs(rdf0.rdf - rdf1.rdf).max() < 1e-10
    finally:
        f.close()
Example #51
0
def test_diff_online():
    # Setup a test FF
    ff = get_ff_water32()
    # Run a test simulation
    with h5.File('yaff.analysis.test.test_diffusion.test_diff_online.h5',
                 driver='core',
                 backing_store=False) as f:
        hdf5 = HDF5Writer(f)
        select = ff.system.get_indexes('O')
        diff0 = Diffusion(f, select=select)
        nve = VerletIntegrator(ff, 1.0 * femtosecond, hooks=[hdf5, diff0])
        nve.run(5)
        assert nve.counter == 5
        # Also run an off-line rdf and compare
        diff1 = Diffusion(f, select=select)
        assert abs(diff0.A - diff1.A) < 1e-10
        assert abs(diff0.B - diff1.B) < 1e-10
        assert abs(diff0.time - diff1.time).max() < 1e-10
        assert abs(diff0.msds - diff1.msds).max() < 1e-10
        assert abs(diff0.msdsums - diff1.msdsums).max() < 1e-10
        assert abs(diff0.msdcounters - diff1.msdcounters).max() < 1e-10
Example #52
0
def test_h5_flush():
    # Test if we can read intermediate hdf5 files that are flushed.

    # Make a temporary directory
    dn_tmp = tempfile.mkdtemp(suffix='yaff', prefix='nve_water_32')
    try:
        # Setup a test FF
        ff = get_ff_water32()
        # Make two different hdf5 hooks
        with h5.File('%s/output.h5' % dn_tmp) as f, \
             h5.File('%s/output_flushed.h5' % dn_tmp) as f_flushed:
            hdf5 = HDF5Writer(f)
            hdf5_flushed = HDF5Writer(f_flushed, flush=4)
            # Run a test simulation
            nve = VerletIntegrator(ff, 1.0*femtosecond, hooks=[hdf5,hdf5_flushed])
            nve.run(5)

            # Actual tests

            # 1) Check that we can't read the trajectory group in the h5 file without flushing.
            # This must be done in a subprocess, otherwise the HDF5 library will fake
            # flushing while it does not really flush to disk.
            command = ['python', '-c', 'import h5py as h5; f = h5.File(\'%s/output.h5\', 'r'); f.close()' % dn_tmp]
            p = Popen(command, stdout=PIPE, stderr=STDOUT)
            output = p.communicate()[0]
            assert 'IOError' in output
            assert p.returncode != 0

            # 2) This should work in a subprocess.
            command = ['python', '-c', 'import h5py as h5; f = h5.File(\'%s/output_flushed.h5\', 'r'); f.close()' % dn_tmp]
            p = Popen(command, stdout=PIPE, stderr=STDOUT)
            output = p.communicate()[0]
            assert 'IOError' not in output
            assert p.returncode == 0
    finally:
        shutil.rmtree(dn_tmp)
Example #53
0
def test_amb():
    nve = VerletIntegrator(get_ff_water32(), 1.0*femtosecond, hooks=TBCombination(McDonaldBarostat(300, 1*bar), AndersenThermostat(300)))
    nve.run(5)
    assert nve.counter == 5
Example #54
0
def test_amb():
    nve = VerletIntegrator(get_ff_water32(), 1.0 * femtosecond, hooks=AndersenMcDonaldBarostat(300, 1 * bar))
    nve.run(5)
    assert nve.counter == 5
Example #55
0
def test_elastic_water32():
    ff = get_ff_water32()
    elastic = estimate_elastic(ff, do_frozen=True)
    assert elastic.shape == (6, 6)
Example #56
0
def test_hessian_partial_water32():
    ff = get_ff_water32()
    select = [1, 2, 3, 14, 15, 16]
    hessian = estimate_cart_hessian(ff, select=select)
    assert hessian.shape == (18, 18)