Beispiel #1
0
def test_velocity_traj():
    # Test Trajectory.get_velocity() against velocities output from CP2K. The
    # agreement is very good. Works only for fixed-cell MDs, however!
    dr = 'files/cp2k/md/nvt_print_low'
    base = os.path.dirname(dr) 
    fn = '%s/cp2k.out' %dr
    print common.backtick('tar -C {0} -xzf {1}.tgz'.format(base,dr))
    tr = io.read_cp2k_md(fn)
    # read from data file
    v1 = tr.velocity.copy()
    # If tr.velocity != None, then get_velocity() doesn't calculate it. Then,
    # it simply returns tr.velocity, which is what we of course usually want.
    tr.velocity = None
    # calculate from coords + time step, b/c of central diffs, only steps 1:-1
    # are the same
    v2 = tr.get_velocity()
    print ">>>> np.abs(v1).max()", np.abs(v1).max()
    print ">>>> np.abs(v1).min()", np.abs(v1).min()
    print ">>>> np.abs(v2).max()", np.abs(v2).max()
    print ">>>> np.abs(v2).min()", np.abs(v2).min()
    print ">>>> np.abs(v1-v2).max()", np.abs(v1-v2).max()
    print ">>>> np.abs(v1-v2).min()", np.abs(v1-v2).min()
    assert np.allclose(v1[1:-1,...], v2[1:-1,...], atol=1e-4)
    
    ##from pwtools import mpl
    ##fig,ax = mpl.fig_ax()
    ##ax.plot(v1[1:-1,:,0], 'b')
    ##ax.plot(v2[1:-1,:,0], 'r')
    ##mpl.plt.show()
    
    shape = (100,10,3)
    arr = np.random.rand(*shape)
    assert crys.velocity_traj(arr, axis=0).shape == shape
    assert crys.velocity_traj(arr, axis=0, endpoints=False).shape == (98,10,3)
Beispiel #2
0
def test_velocity_traj():
    # Test Trajectory.get_velocity() against velocities output from CP2K. The
    # agreement is very good. Works only for fixed-cell MDs, however!
    dr = 'files/cp2k/md/nvt_print_low'
    base = os.path.dirname(dr) 
    fn = '%s/cp2k.out' %dr
    print(common.backtick('tar -C {0} -xzf {1}.tgz'.format(base,dr)))
    tr = io.read_cp2k_md(fn)
    # read from data file
    v1 = tr.velocity.copy()
    # If tr.velocity != None, then get_velocity() doesn't calculate it. Then,
    # it simply returns tr.velocity, which is what we of course usually want.
    tr.velocity = None
    # calculate from coords + time step, b/c of central diffs, only steps 1:-1
    # are the same
    v2 = tr.get_velocity()
    print(">>>> np.abs(v1).max()", np.abs(v1).max())
    print(">>>> np.abs(v1).min()", np.abs(v1).min())
    print(">>>> np.abs(v2).max()", np.abs(v2).max())
    print(">>>> np.abs(v2).min()", np.abs(v2).min())
    print(">>>> np.abs(v1-v2).max()", np.abs(v1-v2).max())
    print(">>>> np.abs(v1-v2).min()", np.abs(v1-v2).min())
    assert np.allclose(v1[1:-1,...], v2[1:-1,...], atol=1e-4)
    
    ##from pwtools import mpl
    ##fig,ax = mpl.fig_ax()
    ##ax.plot(v1[1:-1,:,0], 'b')
    ##ax.plot(v2[1:-1,:,0], 'r')
    ##mpl.plt.show()
    
    shape = (100,10,3)
    arr = np.random.rand(*shape)
    assert crys.velocity_traj(arr, axis=0).shape == shape
    assert crys.velocity_traj(arr, axis=0, endpoints=False).shape == (98,10,3)
Beispiel #3
0
def test_cp2k_txt_vs_dcd():
    # Two exactly equal NPT runs, nstep=16, natoms=57. The dcd run uses
    #   motion/print/trajectory format dcd_aligned_cell
    # the other the default xyz format. So we have
    #   PROJECT-pos-1.dcd
    #   PROJECT-pos-1.xyz
    # Since the cell changes and we use dcd_aligned_cell, the coords from the
    # xyz run are NOT the same as the coords in the dcd file, which HAS to be
    # like this. Only coords_frac can be compared, and coords after the xyz
    # run's cell has been aligned to [[x,0,0],[xy,y,0],[xz,yz,z]].
    dir_xyz = unpack_compressed('files/cp2k/dcd/npt_xyz.tgz')
    dir_dcd = unpack_compressed('files/cp2k/dcd/npt_dcd.tgz')
    tr_xyz = io.read_cp2k_md(pj(dir_xyz, 'cp2k.out'))
    tr_dcd = io.read_cp2k_md_dcd(pj(dir_dcd, 'cp2k.out'))
    for name in ['natoms', 'nstep', 'timestep', 'symbols']:
        assert getattr(tr_xyz, name) == getattr(tr_dcd, name)
    assert tr_xyz.timestep == 1.0
    assert tr_xyz.natoms == 57
    assert tr_xyz.nstep == 16

    # coords are 32bit float in dcd files (single prec, so coords_frac can only
    # be accurate to that precision, which ~1e-8). cryst_const is double
    # precision in the dcd file, so atol can be lower
    assert np.allclose(tr_xyz.coords_frac,
                       tr_dcd.coords_frac,
                       rtol=0,
                       atol=1.5e-7)
    assert np.allclose(tr_xyz.cryst_const,
                       tr_dcd.cryst_const,
                       rtol=0,
                       atol=7e-10)
    assert not np.allclose(tr_xyz.coords, tr_dcd.coords, rtol=0, atol=5e-6)
    assert not np.allclose(tr_xyz.cell, tr_dcd.cell, rtol=0, atol=7e-10)

    # align xyz cell, now cell and coords are the same
    tr_xyz.coords = None
    tr_xyz.cell = None
    tr_xyz.set_all()

    assert np.allclose(tr_xyz.coords_frac,
                       tr_dcd.coords_frac,
                       rtol=0,
                       atol=1.5e-7)
    assert np.allclose(tr_xyz.cryst_const,
                       tr_dcd.cryst_const,
                       rtol=0,
                       atol=7e-10)
    assert np.allclose(tr_xyz.coords, tr_dcd.coords, rtol=0, atol=5e-6)
    assert np.allclose(tr_xyz.cell, tr_dcd.cell, rtol=0, atol=7e-10)
Beispiel #4
0
def test_cp2k_md():
    attr_lst = parse.Cp2kMDOutputFile().attr_lst
    # This parser and others have get_econst(), but not all, so ATM it's not
    # part of the Trajectory API
    attr_lst.pop(attr_lst.index('econst'))
    for dr in ['files/cp2k/md/npt_f_print_low', 'files/cp2k/md/nvt_print_low']:
        base = os.path.dirname(dr)
        fn = '%s/cp2k.out' % dr
        print("testing: %s" % fn)
        print(common.backtick('tar -C {0} -xzf {1}.tgz'.format(base, dr)))
        tr = io.read_cp2k_md(fn)
        assert_attrs_not_none(tr, attr_lst=attr_lst)
        pp = parse.Cp2kMDOutputFile(fn)
        forces_outfile = pp._get_forces_from_outfile() * Ha / Bohr / eV * Ang
        assert np.allclose(forces_outfile, tr.forces, rtol=1e-3)
Beispiel #5
0
def test_cp2k_md():
    attr_lst = parse.Cp2kMDOutputFile().attr_lst
    # This parser and others have get_econst(), but not all, so ATM it's not
    # part of the Trajectory API
    attr_lst.pop(attr_lst.index('econst'))
    for dr in ['files/cp2k/md/npt_f_print_low', 'files/cp2k/md/nvt_print_low']:
        base = os.path.dirname(dr) 
        fn = '%s/cp2k.out' %dr
        print "testing: %s" %fn
        print common.backtick('tar -C {0} -xzf {1}.tgz'.format(base,dr))
        tr = io.read_cp2k_md(fn)
        assert_attrs_not_none(tr, attr_lst=attr_lst)        
        pp = parse.Cp2kMDOutputFile(fn)
        forces_outfile = pp._get_forces_from_outfile()*Ha/Bohr/eV*Ang
        assert np.allclose(forces_outfile, tr.forces, rtol=1e-3)
Beispiel #6
0
def test_cp2k_txt_vs_dcd():
    # Two exactly equal NPT runs, nstep=16, natoms=57. The dcd run uses 
    #   motion/print/trajectory format dcd_aligned_cell
    # the other the default xyz format. So we have 
    #   PROJECT-pos-1.dcd
    #   PROJECT-pos-1.xyz
    # Since the cell changes and we use dcd_aligned_cell, the coords from the
    # xyz run are NOT the same as the coords in the dcd file, which HAS to be
    # like this. Only coords_frac can be compared, and coords after the xyz
    # run's cell has been aligned to [[x,0,0],[xy,y,0],[xz,yz,z]].  
    dir_xyz = unpack_compressed('files/cp2k/dcd/npt_xyz.tgz')
    dir_dcd = unpack_compressed('files/cp2k/dcd/npt_dcd.tgz')
    tr_xyz = io.read_cp2k_md(pj(dir_xyz, 'cp2k.out'))
    tr_dcd = io.read_cp2k_md_dcd(pj(dir_dcd, 'cp2k.out'))
    for name in ['natoms', 'nstep', 'timestep', 'symbols']:
        assert getattr(tr_xyz,name) == getattr(tr_dcd, name)
    assert tr_xyz.timestep == 1.0
    assert tr_xyz.natoms == 57
    assert tr_xyz.nstep == 16
    
    # coords are 32bit float in dcd files (single prec, so coords_frac can only
    # be accurate to that precision, which ~1e-8). cryst_const is double
    # precision in the dcd file, so atol can be lower
    assert np.allclose(tr_xyz.coords_frac, tr_dcd.coords_frac, rtol=0,
                       atol=1.5e-7)
    assert np.allclose(tr_xyz.cryst_const, tr_dcd.cryst_const, rtol=0,
                       atol=7e-10)
    assert not np.allclose(tr_xyz.coords, tr_dcd.coords, rtol=0,
                           atol=5e-6)
    assert not np.allclose(tr_xyz.cell, tr_dcd.cell, rtol=0,
                           atol=7e-10)

    # align xyz cell, now cell and coords are the same
    tr_xyz.coords = None; tr_xyz.cell=None; tr_xyz.set_all()
    
    assert np.allclose(tr_xyz.coords_frac, tr_dcd.coords_frac, rtol=0,
                       atol=1.5e-7)
    assert np.allclose(tr_xyz.cryst_const, tr_dcd.cryst_const, rtol=0,
                       atol=7e-10)
    assert np.allclose(tr_xyz.coords, tr_dcd.coords, rtol=0,
                       atol=5e-6)
    assert np.allclose(tr_xyz.cell, tr_dcd.cell, rtol=0,
                       atol=7e-10)