Exemple #1
0
def run(filename, none_attrs=[]):
    # filename = 'files/cpmd/md_bo/cpmd.bo.out'
    # basename = 'cpmd.bo.out'
    # archive  = 'files/cpmd/md_bo.tgz'
    bar = '='*78
    print bar
    print "@@testing: %s" %filename
    print bar
    basename = os.path.basename(filename)
    archive = os.path.dirname(filename) + '.tgz'
    workdir = unpack_compressed(archive)
    pp = CpmdMDOutputFile(filename=pj(workdir, basename))
    pp.parse()
    assert_attrs_not_none(pp, none_attrs=none_attrs)
    traj = pp.get_traj()
    attrs3d = ['coords', 
               'coords_frac', 
               'forces', 
               'cell',
               'stress',
               ]
    for attr_name in traj.attr_lst:
        attr = getattr(traj, attr_name)
        if attr_name not in none_attrs:
            if hasattr(attr, 'ndim'):
                print "%s: ndim: %s, shape: %s" %(attr_name, attr.ndim, attr.shape)
            assert attr is not None, "FAILED - None: %s" %attr_name
            if attr_name in attrs3d:
                assert attr.ndim == 3, "FAILED - not 3d: %s" %attr_name
Exemple #2
0
def run(filename, none_attrs=[]):
    # filename = 'files/cpmd/md_bo/cpmd.bo.out'
    # basename = 'cpmd.bo.out'
    # archive  = 'files/cpmd/md_bo.tgz'
    bar = '=' * 78
    print(bar)
    print("@@testing: %s" % filename)
    print(bar)
    basename = os.path.basename(filename)
    archive = os.path.dirname(filename) + '.tgz'
    workdir = unpack_compressed(archive)
    pp = CpmdMDOutputFile(filename=pj(workdir, basename))
    pp.parse()
    assert_attrs_not_none(pp, none_attrs=none_attrs)
    traj = pp.get_traj()
    attrs3d = [
        'coords',
        'coords_frac',
        'forces',
        'cell',
        'stress',
    ]
    for attr_name in traj.attr_lst:
        attr = getattr(traj, attr_name)
        if attr_name not in none_attrs:
            if hasattr(attr, 'ndim'):
                print("%s: ndim: %s, shape: %s" %
                      (attr_name, attr.ndim, attr.shape))
            assert attr is not None, "FAILED - None: %s" % attr_name
            if attr_name in attrs3d:
                assert attr.ndim == 3, "FAILED - not 3d: %s" % attr_name
Exemple #3
0
def test_cpmd_scf():
    filename = "files/cpmd/scf/cpmd.out"
    basename = os.path.basename(filename)
    archive = os.path.dirname(filename) + ".tgz"
    workdir = unpack_compressed(archive)
    pp = CpmdSCFOutputFile(filename=pj(workdir, basename))
    pp.parse()
    assert_attrs_not_none(pp, none_attrs=[])
Exemple #4
0
def test_cp2k_scf():
    attr_lst = parse.Cp2kSCFOutputFile().attr_lst
    for base in ['cp2k.scf.out.print_low', 'cp2k.scf.out.print_medium']:
        fn = 'files/cp2k/scf/%s' % base
        print("testing: %s" % fn)
        print(common.backtick("gunzip %s.gz" % fn))
        st = io.read_cp2k_scf(fn)
        assert_attrs_not_none(st, attr_lst=attr_lst)
Exemple #5
0
def test_cp2k_scf():
    attr_lst = parse.Cp2kSCFOutputFile().attr_lst
    for base in ['cp2k.scf.out.print_low', 'cp2k.scf.out.print_medium']:
        fn = 'files/cp2k/scf/%s' %base
        print "testing: %s" %fn
        print common.backtick("gunzip %s.gz" %fn)
        st = io.read_cp2k_scf(fn)
        assert_attrs_not_none(st, attr_lst=attr_lst)
Exemple #6
0
def test_cpmd_scf():
    filename = 'files/cpmd/scf/cpmd.out'
    basename = os.path.basename(filename)
    archive = os.path.dirname(filename) + '.tgz'
    workdir = unpack_compressed(archive)
    pp = CpmdSCFOutputFile(filename=pj(workdir, basename))
    pp.parse()
    assert_attrs_not_none(pp, none_attrs=[])
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
def test_pw_vc_relax_out():
    filename = 'files/pw.vc_relax_cell_unit.out'
    common.system('gunzip %s.gz' % filename)
    pp = PwMDOutputFile(filename=filename)
    pp.parse()
    common.system('gzip %s' % filename)
    none_attrs = [
        'coords',
        'ekin',
        'temperature',
        'timestep',
    ]
    assert_attrs_not_none(pp, none_attrs=none_attrs)
    traj = pp.get_traj()
    none_attrs = [\
        'ekin',
        'temperature',
        'timestep',
        'velocity',
        'time',
        ]
    assert_attrs_not_none(traj, none_attrs=none_attrs)
    assert pp.cell_unit == 'alat'
    assert pp.cell.shape == (6, 3, 3)
    for idx in range(1, pp.cell.shape[0]):
        assert crys.rms(pp.cell[idx, ...] - pp.cell[0, ...]) > 0.0

    # Test _get_block_header_unit, which is used in get_cell_unit().
    dct = \
        {'FOO': None,
         'FOO alat': 'alat',
         'FOO (alat)': 'alat',
         'FOO {alat}': 'alat',
         'FOO (alat=1.23)': 'alat',
         'FOO (alat=  1.23)': 'alat',
         }

    for txt, val in dct.items():
        fn = pj(testdir, 'test_block_header_unit.txt')
        common.file_write(fn, txt)
        pp.filename = fn
        assert pp._get_block_header_unit('FOO') == val
Exemple #10
0
def test_pw_scf_one_atom():
    fn = unpack_compressed('files/pw.scf_one_atom.out.gz')
    pp = PwSCFOutputFile(fn)
    pp.parse()
    not_none = [\
        'coords',
        'symbols',
        'stress',
        'etot',
        'forces',
        'nstep_scf',
        'cell',
        'natoms',
        'nkpoints',
    ]
    tools.assert_attrs_not_none(pp, attr_lst=not_none)
    assert pp.forces.shape == (1, 3)
    assert pp.coords.shape == (1, 3)
    assert pp.stress.shape == (3, 3)
    assert pp.cell.shape == (3, 3)
Exemple #11
0
def test_pw_scf_one_atom():
    fn = unpack_compressed('files/pw.scf_one_atom.out.gz')
    pp = PwSCFOutputFile(fn)
    pp.parse()
    not_none = [\
        'coords',
        'symbols',
        'stress',
        'etot',
        'forces',
        'nstep_scf',
        'cell',
        'natoms',
        'nkpoints',
    ]
    tools.assert_attrs_not_none(pp, attr_lst=not_none)
    assert pp.forces.shape == (1,3)
    assert pp.coords.shape == (1,3)
    assert pp.stress.shape == (3,3)
    assert pp.cell.shape == (3,3)
def test_pw_vc_relax_out():
    filename = 'files/pw.vc_relax_cell_unit.out'
    common.system('gunzip %s.gz' %filename)
    pp = PwMDOutputFile(filename=filename)
    pp.parse()
    common.system('gzip %s' %filename)
    none_attrs = ['coords', 
                  'ekin', 
                  'temperature',
                  'timestep',
                  ]
    assert_attrs_not_none(pp, none_attrs=none_attrs)
    traj = pp.get_traj()
    none_attrs = [\
        'ekin', 
        'temperature',
        'timestep',
        'velocity',
        'time',
        ]
    assert_attrs_not_none(traj, none_attrs=none_attrs)
    assert pp.cell_unit == 'alat'
    assert pp.cell.shape == (6,3,3)
    for idx in range(1, pp.cell.shape[0]):
        assert crys.rms(pp.cell[idx,...] - pp.cell[0,...]) > 0.0
    
    # Test _get_block_header_unit, which is used in get_cell_unit().
    dct = \
        {'FOO': None,
         'FOO alat': 'alat',
         'FOO (alat)': 'alat',
         'FOO {alat}': 'alat',
         'FOO (alat=1.23)': 'alat',
         'FOO (alat=  1.23)': 'alat',
         }

    for txt,val in dct.iteritems():
        fn = pj(testdir, 'test_block_header_unit.txt')
        common.file_write(fn, txt)
        pp.filename = fn
        assert pp._get_block_header_unit('FOO') == val
Exemple #13
0
def test_pw_vc_relax_out():
    filename = 'files/pw.vc_relax.out'
    common.system('gunzip %s.gz' %filename)
    pp = PwMDOutputFile(filename=filename)
    pp.parse()
    common.system('gzip %s' %filename)
    none_attrs = ['coords', 
                  'ekin', 
                  'temperature',
                  'timestep',
                  ]
    assert_attrs_not_none(pp, none_attrs=none_attrs)
    traj = pp.get_traj()
    none_attrs = [\
        'ekin', 
        'temperature',
        'timestep',
        'velocity',
        'time',
        ]
    assert_attrs_not_none(traj, none_attrs=none_attrs)   
Exemple #14
0
def test_pw_md_out():
    filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__)
    alat = 5.9098  # Bohr
    pp1 = PwMDOutputFile(filename=filename, use_alat=True)
    pp1.parse()
    none_attrs = [\
        'coords_frac',
        ]
    assert_attrs_not_none(pp1, none_attrs=none_attrs)
    assert np.allclose(pp1.timestep, 150.0)  # tryd
    traj1 = pp1.get_traj()
    assert_attrs_not_none(traj1)

    pp2 = PwMDOutputFile(filename=filename,
                         use_alat=False,
                         units={'length': alat * Bohr / Ang})
    pp2.parse()
    assert np.allclose(pp2.timestep, 150.0)  # tryd
    assert_attrs_not_none(pp2, none_attrs=none_attrs)

    # Skip coords and cell b/c they are modified by self.alat and
    # pp1.alat = 1.0, pp2.alat = 5.9098
    attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell'])
    adae(pp1.__dict__, pp2.__dict__, keys=attr_lst)

    traj2 = pp2.get_traj()
    adae(traj1.__dict__, traj2.__dict__, keys=traj1.attr_lst)

    pp3 = PwMDOutputFile(filename=filename)
    assert alat == pp3.get_alat()  # self.use_alat=True default
Exemple #15
0
def test_pw_md_out():
    filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__)
    alat = 5.9098 # Bohr
    pp1 = PwMDOutputFile(filename=filename, use_alat=True)
    pp1.parse()
    none_attrs = [\
        'coords_frac',
        ]
    assert_attrs_not_none(pp1, none_attrs=none_attrs)
    assert np.allclose(pp1.timestep, 150.0) # tryd
    traj1 = pp1.get_traj()
    assert_attrs_not_none(traj1)

    pp2 = PwMDOutputFile(filename=filename, 
                         use_alat=False,
                         units={'length': alat*Bohr/Ang})
    pp2.parse()                         
    assert np.allclose(pp2.timestep, 150.0) # tryd
    assert_attrs_not_none(pp2, none_attrs=none_attrs)
    
    # Skip coords and cell b/c they are modified by self.alat and
    # pp1.alat = 1.0, pp2.alat = 5.9098
    attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell'])
    adae(pp1.__dict__, pp2.__dict__, keys=attr_lst)         
    
    traj2 = pp2.get_traj()
    adae(traj1.__dict__, traj2.__dict__, keys=traj1.attr_lst)

    pp3 = PwMDOutputFile(filename=filename)
    assert alat == pp3.get_alat() # self.use_alat=True default
Exemple #16
0
def test_smooth():
    tr = get_rand_traj()
    assert len(tr.attrs_nstep) > 0
    trs = crys.smooth(tr, hanning(11))
    assert len(trs.attrs_nstep) > 0
    assert_attrs_not_none(trs, attr_lst=tr.attr_lst)
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs, name)
        assert a1.shape == a2.shape
        assert np.abs(a1 - a2).sum() > 0.0
    assert trs.timestep == tr.timestep
    assert trs.nstep == tr.nstep
    
    # reproduce data with kernel [0,1,0]
    trs = crys.smooth(tr, hanning(3))
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs, name)
        assert np.allclose(a1, a2)
    
    trs1 = crys.smooth(tr, hanning(3), method=1)
    trs2 = crys.smooth(tr, hanning(3), method=2)
    assert len(trs1.attrs_nstep) > 0
    assert len(trs2.attrs_nstep) > 0
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs1, name)
        a3 = getattr(trs2, name)
        assert np.allclose(a1, a2)
        assert np.allclose(a1, a3)
    
    trs1 = crys.smooth(tr, hanning(11), method=1)
    trs2 = crys.smooth(tr, hanning(11), method=2)
    assert len(trs1.attrs_nstep) > 0
    assert len(trs2.attrs_nstep) > 0
    for name in trs1.attrs_nstep:
        a1 = getattr(trs1, name)
        a2 = getattr(trs2, name)
        assert np.allclose(a1, a2)
Exemple #17
0
def test_smooth():
    tr = get_rand_traj()
    assert len(tr.attrs_nstep) > 0
    trs = crys.smooth(tr, hanning(11))
    assert len(trs.attrs_nstep) > 0
    assert_attrs_not_none(trs, attr_lst=tr.attr_lst)
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs, name)
        assert a1.shape == a2.shape
        assert np.abs(a1 - a2).sum() > 0.0
    assert trs.timestep == tr.timestep
    assert trs.nstep == tr.nstep

    # reproduce data with kernel [0,1,0]
    trs = crys.smooth(tr, hanning(3))
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs, name)
        assert np.allclose(a1, a2)

    trs1 = crys.smooth(tr, hanning(3), method=1)
    trs2 = crys.smooth(tr, hanning(3), method=2)
    assert len(trs1.attrs_nstep) > 0
    assert len(trs2.attrs_nstep) > 0
    for name in tr.attrs_nstep:
        a1 = getattr(tr, name)
        a2 = getattr(trs1, name)
        a3 = getattr(trs2, name)
        assert np.allclose(a1, a2)
        assert np.allclose(a1, a3)

    trs1 = crys.smooth(tr, hanning(11), method=1)
    trs2 = crys.smooth(tr, hanning(11), method=2)
    assert len(trs1.attrs_nstep) > 0
    assert len(trs2.attrs_nstep) > 0
    for name in trs1.attrs_nstep:
        a1 = getattr(trs1, name)
        a2 = getattr(trs2, name)
        assert np.allclose(a1, a2)
Exemple #18
0
def test_cp2k_cell_opt():
    attr_lst = parse.Cp2kRelaxOutputFile().attr_lst
    attr_lst.pop(attr_lst.index('econst'))
    # There is no PROJECT-frc-1.xyz file, but the input file has 
    #    &force_eval
    #        &print
    #            &forces
    #            &end forces
    #        &end print
    #    &end force_eval
    # therefore we can parse forces from the outfile.
    none_attrs = ['ekin',
                  'temperature',
                  'timestep',
                  'velocity',
                  ]
    for dr in ['files/cp2k/cell_opt/cell_opt']:
        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_relax(fn)
        assert_attrs_not_none(tr, attr_lst=attr_lst, none_attrs=none_attrs)        
Exemple #19
0
def test_cp2k_cell_opt():
    attr_lst = parse.Cp2kRelaxOutputFile().attr_lst
    attr_lst.pop(attr_lst.index('econst'))
    # There is no PROJECT-frc-1.xyz file, but the input file has
    #    &force_eval
    #        &print
    #            &forces
    #            &end forces
    #        &end print
    #    &end force_eval
    # therefore we can parse forces from the outfile.
    none_attrs = [
        'ekin',
        'temperature',
        'timestep',
        'velocity',
    ]
    for dr in ['files/cp2k/cell_opt/cell_opt']:
        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_relax(fn)
        assert_attrs_not_none(tr, attr_lst=attr_lst, none_attrs=none_attrs)
Exemple #20
0
def test_traj():
    natoms = 10
    nstep = 100
    cell = rand(nstep,3,3)
    stress = rand(nstep,3,3)
    forces = rand(nstep,natoms,3)
    etot = rand(nstep)
    cryst_const = crys.cell2cc3d(cell, axis=0)
    coords_frac = rand(nstep,natoms,3)
    coords = crys.coord_trans3d(coords=coords_frac,
                                old=cell,
                                new=num.extend_array(np.identity(3),
                                                     nstep,axis=0),
                                axis=1,
                                timeaxis=0)                                                    
    assert cryst_const.shape == (nstep, 6)
    assert coords.shape == (nstep,natoms,3)
    symbols = ['H']*natoms
    
    # automatically calculated:
    #   coords
    #   cell
    #   pressure
    #   velocity (from coords)
    #   temperature (from ekin)
    #   ekin (from velocity)
    traj = Trajectory(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    forces=forces,
                    stress=stress,
                    etot=etot,
                    timestep=1,
                    )
    # Test if all getters work.
    for name in traj.attr_lst:
        print "test if getters work:", name
        traj.try_set_attr(name)
        assert getattr(traj, name) is not None, "attr None: %s" %name
        assert eval('traj.get_%s()'%name) is not None, "getter returns None: %s" %name
        print "test if getters work:", name, "... ok"
    aaae(coords_frac, traj.coords_frac)
    aaae(coords, traj.coords)
    aaae(cryst_const, traj.cryst_const)
    aaae(np.trace(stress, axis1=1, axis2=2)/3.0, traj.pressure)
    assert traj.coords.shape == (nstep,natoms,3)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.velocity.shape == (nstep, natoms, 3)
    assert traj.temperature.shape == (nstep,)
    assert traj.ekin.shape == (nstep,)
    assert traj.nstep == nstep
    assert traj.natoms == natoms

    traj = Trajectory(coords_frac=coords_frac,
                    symbols=symbols,
                    cell=cell)
    aaae(coords, traj.coords)
    
    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    traj = Trajectory(coords_frac=coords_frac,
                    symbols=symbols,
                    cryst_const=cryst_const)
    np.testing.assert_almost_equal(crys.volume_cell3d(cell),
                                   crys.volume_cell3d(traj.cell))
    aaae(cryst_const, crys.cell2cc3d(traj.cell))
    
    # extend arrays
    cell2d = rand(3,3)
    cc2d = crys.cell2cc(cell2d)
    traj = Trajectory(coords_frac=coords_frac,
                      cell=cell2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.cryst_const.shape == (nstep,6)
    for ii in range(traj.nstep):
        assert (traj.cell[ii,...] == cell2d).all()
        assert (traj.cryst_const[ii,:] == cc2d).all()
    
    traj = Trajectory(coords_frac=coords_frac,
                      cryst_const=cc2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.cryst_const.shape == (nstep,6)
    for ii in range(traj.nstep):
        assert (traj.cryst_const[ii,:] == cc2d).all()

    # units
    traj = Trajectory(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    stress=stress,
                    forces=forces,
                    units={'length': 2, 'forces': 3, 'stress': 4})
    aaae(2*coords, traj.coords)                    
    aaae(3*forces, traj.forces)                    
    aaae(4*stress, traj.stress)                    
    
    # iterate, check if Structures are complete
    traj = Trajectory(coords=coords, 
                      symbols=symbols,
                      cell=cell,
                      forces=forces,
                      stress=stress,
                      etot=etot,
                      timestep=1.0)
    for struct in traj:
        assert struct.is_struct, "st is not Structure"
        assert not struct.is_traj, "st is Trajectory"
        assert_attrs_not_none(struct)
    struct = traj[0]
    for attr_name in traj.attr_lst:
        if attr_name in struct.attrs_only_traj:
            msg = "tr[0] %s is not None" %attr_name
            assert getattr(struct,attr_name) is None, msg
        else:            
            msg = "tr[0] %s is None" %attr_name
            assert getattr(struct,attr_name) is not None, msg
    
    # slices, return traj
    keys = traj.attr_lst[:]
    tsl = traj[10:80:2]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[slice(10,80,2)]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[np.s_[10:80:2]]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    assert tsl.is_traj
    
    # iteration over sliced traj
    tsl = traj[10:80:2]
    for x in tsl:
        pass
    for x in tsl.copy():
        pass

    # repeat iter
    for i in range(2):
        cnt = 0
        for st in traj:
            cnt += 1
        assert cnt == nstep, "%i, %i" %(cnt, nstep)    
    
    # copy
    traj2 = traj.copy()
    for name in traj.attr_lst:
        val = getattr(traj,name)
        if val is not None and not (isinstance(val, types.IntType) or \
            isinstance(val, types.FloatType)):
            val2 = getattr(traj2,name)
            print "test copy:", name, type(val), type(val2)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
    assert_dict_with_all_types_equal(traj.__dict__, traj2.__dict__,
                                     keys=traj.attr_lst)
Exemple #21
0
def test_traj():
    natoms = 10
    nstep = 100
    cell = rand(nstep, 3, 3)
    stress = rand(nstep, 3, 3)
    forces = rand(nstep, natoms, 3)
    etot = rand(nstep)
    cryst_const = crys.cell2cc3d(cell, axis=0)
    coords_frac = rand(nstep, natoms, 3)
    coords = crys.coord_trans3d(coords=coords_frac,
                                old=cell,
                                new=num.extend_array(np.identity(3),
                                                     nstep,
                                                     axis=0),
                                axis=1,
                                timeaxis=0)
    assert cryst_const.shape == (nstep, 6)
    assert coords.shape == (nstep, natoms, 3)
    symbols = ['H'] * natoms

    # automatically calculated:
    #   coords
    #   cell
    #   pressure
    #   velocity (from coords)
    #   temperature (from ekin)
    #   ekin (from velocity)
    traj = Trajectory(
        coords_frac=coords_frac,
        cell=cell,
        symbols=symbols,
        forces=forces,
        stress=stress,
        etot=etot,
        timestep=1,
    )
    # Test if all getters work.
    for name in traj.attr_lst:
        print("test if getters work:", name)
        traj.try_set_attr(name)
        assert getattr(traj, name) is not None, "attr None: %s" % name
        assert eval('traj.get_%s()' %
                    name) is not None, "getter returns None: %s" % name
        print("test if getters work:", name, "... ok")
    aaae(coords_frac, traj.coords_frac)
    aaae(coords, traj.coords)
    aaae(cryst_const, traj.cryst_const)
    aaae(np.trace(stress, axis1=1, axis2=2) / 3.0, traj.pressure)
    assert traj.coords.shape == (nstep, natoms, 3)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.velocity.shape == (nstep, natoms, 3)
    assert traj.temperature.shape == (nstep, )
    assert traj.ekin.shape == (nstep, )
    assert traj.nstep == nstep
    assert traj.natoms == natoms

    traj = Trajectory(coords_frac=coords_frac, symbols=symbols, cell=cell)
    aaae(coords, traj.coords)

    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    traj = Trajectory(coords_frac=coords_frac,
                      symbols=symbols,
                      cryst_const=cryst_const)
    np.testing.assert_almost_equal(crys.volume_cell3d(cell),
                                   crys.volume_cell3d(traj.cell))
    aaae(cryst_const, crys.cell2cc3d(traj.cell))

    # extend arrays
    cell2d = rand(3, 3)
    cc2d = crys.cell2cc(cell2d)
    traj = Trajectory(coords_frac=coords_frac, cell=cell2d, symbols=symbols)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.cryst_const.shape == (nstep, 6)
    for ii in range(traj.nstep):
        assert (traj.cell[ii, ...] == cell2d).all()
        assert (traj.cryst_const[ii, :] == cc2d).all()

    traj = Trajectory(coords_frac=coords_frac,
                      cryst_const=cc2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.cryst_const.shape == (nstep, 6)
    for ii in range(traj.nstep):
        assert (traj.cryst_const[ii, :] == cc2d).all()

    # units
    traj = Trajectory(coords_frac=coords_frac,
                      cell=cell,
                      symbols=symbols,
                      stress=stress,
                      forces=forces,
                      units={
                          'length': 2,
                          'forces': 3,
                          'stress': 4
                      })
    aaae(2 * coords, traj.coords)
    aaae(3 * forces, traj.forces)
    aaae(4 * stress, traj.stress)

    # iterate, check if Structures are complete
    traj = Trajectory(coords=coords,
                      symbols=symbols,
                      cell=cell,
                      forces=forces,
                      stress=stress,
                      etot=etot,
                      timestep=1.0)
    for struct in traj:
        assert struct.is_struct, "st is not Structure"
        assert not struct.is_traj, "st is Trajectory"
        assert_attrs_not_none(struct)
    struct = traj[0]
    for attr_name in traj.attr_lst:
        if attr_name in struct.attrs_only_traj:
            msg = "tr[0] %s is not None" % attr_name
            assert getattr(struct, attr_name) is None, msg
        else:
            msg = "tr[0] %s is None" % attr_name
            assert getattr(struct, attr_name) is not None, msg

    # slices, return traj
    keys = traj.attr_lst[:]
    tsl = traj[10:80:2]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[slice(10, 80, 2)]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[np.s_[10:80:2]]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    assert tsl.is_traj

    # iteration over sliced traj
    tsl = traj[10:80:2]
    for x in tsl:
        pass
    for x in tsl.copy():
        pass

    # repeat iter
    for i in range(2):
        cnt = 0
        for st in traj:
            cnt += 1
        assert cnt == nstep, "%i, %i" % (cnt, nstep)

    # copy
    traj2 = traj.copy()
    for name in traj.attr_lst:
        val = getattr(traj, name)
        if val is not None and not (isinstance(val, int) or \
            isinstance(val, float)):
            val2 = getattr(traj2, name)
            print("test copy:", name, type(val), type(val2))
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
    assert_dict_with_all_types_equal(traj.__dict__,
                                     traj2.__dict__,
                                     keys=traj.attr_lst)
Exemple #22
0
def test_pw_scf_out():
    
    # ref data for Structure, all lengths in Ang, energy in eV
    natoms = 2
    symbols = ['Si', 'Si']
    cell = np.array([[-2.71536701,  0.        ,  2.71536701],
           [ 0.        ,  2.71536701,  2.71536701],
           [-2.71536701,  2.71536701,  0.        ]])
    forces = np.array([[ 2.57110316,  5.14220632,  7.71330948],
                       [-2.57110316, -5.14220632, -7.71330948]]) # eV / Ang
    nspecies = {'Si': 2}
    mass = np.array([ 28.0855,  28.0855]) # amu
    cryst_const = np.array([  3.84010885,   3.84010885,   3.84010885,  60. ,
            60.        ,  60.        ])
    symbols_unique = ['Si']
    etot = -258.58148870118305 # eV
    typat = [1, 1]
    volume = 40.041985843396688 # Ang**3
    stress = np.array([[ 9.825,   0.  ,   0.  ],
           [  0.  ,  9.825,   0.  ],
           [  0.  ,   0.  ,  9.825]]) # GPa
    coords_frac = np.array([[ 0.  ,  0.  ,  0.  ],
           [ 0.25,  0.25,  0.25]])
    pressure = 9.825 # GPa
    coords = np.array([[ 0.        ,  0.        ,  0.        ],
           [-1.35768351,  1.35768351,  1.35768351]])
    order = {'Si': 1}
    alat = 10.2626 # Bohr


    filename = 'files/pw.scf.out'
    common.system('gunzip %s.gz' %filename)

    # use_alat=False. Provide high-precision alat from outside (e.g.
    # from pw.in instead of parsing and using low-precision value from pw.out).
    # Here we use the same alat for the tests.
    pp1 = PwSCFOutputFile(filename=filename, 
                         use_alat=False, # alat=1.0
                         units={'length': alat*Bohr/Ang})
    struct1 = pp1.get_struct() # pp1.parse() called here
    assert_attrs_not_none(struct1) 
    assert_attrs_not_none(pp1) 
    assert pp1.scf_converged is True
    assert alat == pp1.get_alat(True)
    assert 1.0 == pp1.get_alat(False)
    
    aaae(cryst_const, struct1.cryst_const)
    aaae(cell, struct1.cell)
    aaae(coords, struct1.coords)
    aaae(coords_frac, struct1.coords_frac)
    aaae(forces, struct1.forces)
    aaae(stress, struct1.stress)
    assert np.allclose(volume, struct1.volume)
    assert np.allclose(etot, struct1.etot)
    assert np.allclose(pressure, struct1.pressure)
    
    # use_alat=True, alat = 10.2626 Bohr
    pp2 = PwSCFOutputFile(filename=filename, use_alat=True)
    struct2 = pp2.get_struct() # pp.parse() called here
    assert_attrs_not_none(struct2) 
    assert_attrs_not_none(pp2) 
    assert np.allclose(alat, pp2.alat)
    assert pp2.scf_converged is True
    assert alat == pp2.get_alat(True)    # Bohr
    assert 1.0 == pp2.get_alat(False)
    
    # Skip coords adn cell b/c they are modified by self.alat and
    # pp1.alat = 1.0, pp2.alat = 10.2626 
    attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell'])
    adae(pp1.__dict__, pp2.__dict__, keys=attr_lst)         

    attr_lst = struct1.attr_lst
    adae(struct1.__dict__, struct2.__dict__, keys=attr_lst)         
    
    pp3 = PwSCFOutputFile(filename=filename)
    assert alat == pp3.get_alat() # self.use_alat=True default
    
    common.system('gzip %s' %filename)
Exemple #23
0
def test_pw_scf_out():

    # ref data for Structure, all lengths in Ang, energy in eV
    natoms = 2
    symbols = ['Si', 'Si']
    cell = np.array([[-2.71536701, 0.,
                      2.71536701], [0., 2.71536701, 2.71536701],
                     [-2.71536701, 2.71536701, 0.]])
    forces = np.array([[2.57110316, 5.14220632, 7.71330948],
                       [-2.57110316, -5.14220632, -7.71330948]])  # eV / Ang
    nspecies = {'Si': 2}
    mass = np.array([28.0855, 28.0855])  # amu
    cryst_const = np.array([3.84010885, 3.84010885, 3.84010885, 60., 60., 60.])
    symbols_unique = ['Si']
    etot = -258.58148870118305  # eV
    typat = [1, 1]
    volume = 40.041985843396688  # Ang**3
    stress = np.array([[9.825, 0., 0.], [0., 9.825, 0.], [0., 0.,
                                                          9.825]])  # GPa
    coords_frac = np.array([[0., 0., 0.], [0.25, 0.25, 0.25]])
    pressure = 9.825  # GPa
    coords = np.array([[0., 0., 0.], [-1.35768351, 1.35768351, 1.35768351]])
    order = {'Si': 1}
    alat = 10.2626  # Bohr

    filename = 'files/pw.scf.out'
    common.system('gunzip %s.gz' % filename)

    # use_alat=False. Provide high-precision alat from outside (e.g.
    # from pw.in instead of parsing and using low-precision value from pw.out).
    # Here we use the same alat for the tests.
    pp1 = PwSCFOutputFile(
        filename=filename,
        use_alat=False,  # alat=1.0
        units={'length': alat * Bohr / Ang})
    struct1 = pp1.get_struct()  # pp1.parse() called here
    assert_attrs_not_none(struct1)
    assert_attrs_not_none(pp1)
    assert pp1.scf_converged is True
    assert alat == pp1.get_alat(True)
    assert 1.0 == pp1.get_alat(False)

    aaae(cryst_const, struct1.cryst_const)
    aaae(cell, struct1.cell)
    aaae(coords, struct1.coords)
    aaae(coords_frac, struct1.coords_frac)
    aaae(forces, struct1.forces)
    aaae(stress, struct1.stress)
    assert np.allclose(volume, struct1.volume)
    assert np.allclose(etot, struct1.etot)
    assert np.allclose(pressure, struct1.pressure)

    # use_alat=True, alat = 10.2626 Bohr
    pp2 = PwSCFOutputFile(filename=filename, use_alat=True)
    struct2 = pp2.get_struct()  # pp.parse() called here
    assert_attrs_not_none(struct2)
    assert_attrs_not_none(pp2)
    assert np.allclose(alat, pp2.alat)
    assert pp2.scf_converged is True
    assert alat == pp2.get_alat(True)  # Bohr
    assert 1.0 == pp2.get_alat(False)

    # Skip coords adn cell b/c they are modified by self.alat and
    # pp1.alat = 1.0, pp2.alat = 10.2626
    attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell'])
    adae(pp1.__dict__, pp2.__dict__, keys=attr_lst)

    attr_lst = struct1.attr_lst
    adae(struct1.__dict__, struct2.__dict__, keys=attr_lst)

    pp3 = PwSCFOutputFile(filename=filename)
    assert alat == pp3.get_alat()  # self.use_alat=True default

    common.system('gzip %s' % filename)