Пример #1
0
def test_methods():
    tr = crys.Trajectory(coords=rand(100, 10, 3),
                         cell=rand(100, 3, 3),
                         symbols=['H'] * 10)
    st = crys.Structure(coords=rand(10, 3),
                        cell=rand(3, 3),
                        symbols=['H'] * 10)

    for obj, indices in [(st, [0, 1, 2]), (tr, [0, 0, 1, 2])]:
        if obj.is_traj:
            v0 = obj.coords[indices[0], indices[1], ...]
            v1 = obj.coords[indices[0], indices[2], ...]
            v2 = obj.coords[indices[0], indices[3], ...]
        else:
            v0 = obj.coords[indices[0], ...]
            v1 = obj.coords[indices[1], ...]
            v2 = obj.coords[indices[2], ...]
        # use eps=0 since the new system is not orthogonal, only test API
        o1 = crys.align_cart(obj, x=v1 - v0, y=v2 - v0, eps=0)
        o2 = crys.align_cart(obj, vecs=np.array([v0, v1, v2]), eps=0)
        o3 = crys.align_cart(obj, indices=indices, eps=0)
        tools.assert_dict_with_all_types_almost_equal(o1.__dict__,
                                                      o2.__dict__,
                                                      keys=o1.attr_lst)
        tools.assert_dict_with_all_types_almost_equal(o1.__dict__,
                                                      o3.__dict__,
                                                      keys=o1.attr_lst)
Пример #2
0
def test_get_ase_atoms():
    natoms = 10
    st = crys.Structure(coords_frac=rand(natoms,3),
                        symbols=['H']*10,
                        cell=rand(3,3))
    try:
        import ase
        st2 = crys.atoms2struct(crys.struct2atoms(st))
        keys = ['natoms', 'coords', 'coords_frac', 'symbols', 'cryst_const',
                'cell', 'volume', 'mass']
        tools.assert_dict_with_all_types_almost_equal(\
            st.__dict__,
            st2.__dict__,
            keys=keys,
            strict=True)
        # in case the test fails, use this to find out which key failed            
##        for kk in keys:
##            print("testing: %s ..." %kk)
##            tools.assert_all_types_almost_equal(st.__dict__[kk], st2.__dict__[kk])
        for pbc in [True,False]:
            at = st.get_ase_atoms(pbc=pbc)
            assert (at.pbc == np.array([pbc]*3)).all()
            at = crys.struct2atoms(st, pbc=pbc)
            assert (at.pbc == np.array([pbc]*3)).all()
    except ImportError:
        tools.skip("cannot import ase, skipping test get_ase_atoms()")
Пример #3
0
def test_methods():
    tr = crys.Trajectory(coords=rand(100,10,3),
                         cell=rand(100,3,3),
                         symbols=['H']*10)
    st = crys.Structure(coords=rand(10,3),
                        cell=rand(3,3),
                        symbols=['H']*10)
    
    for obj,indices in [(st, [0,1,2]), (tr, [0,0,1,2])]:
        if obj.is_traj:
            v0 = obj.coords[indices[0],indices[1],...]
            v1 = obj.coords[indices[0],indices[2],...]
            v2 = obj.coords[indices[0],indices[3],...]
        else:            
            v0 = obj.coords[indices[0],...]
            v1 = obj.coords[indices[1],...]
            v2 = obj.coords[indices[2],...]
        # use eps=0 since the new system is not orthogonal, only test API            
        o1 = crys.align_cart(obj, x=v1-v0, y=v2-v0, eps=0)
        o2 = crys.align_cart(obj, vecs=np.array([v0,v1,v2]), eps=0)
        o3 = crys.align_cart(obj, indices=indices, eps=0)
        tools.assert_dict_with_all_types_almost_equal(o1.__dict__, 
                                                      o2.__dict__, 
                                                      keys=o1.attr_lst)
        tools.assert_dict_with_all_types_almost_equal(o1.__dict__, 
                                                      o3.__dict__, 
                                                      keys=o1.attr_lst)
Пример #4
0
def test_get_ase_atoms():
    natoms = 10
    st = crys.Structure(coords_frac=rand(natoms, 3),
                        symbols=['H'] * 10,
                        cell=rand(3, 3))
    try:
        import ase
        st2 = crys.atoms2struct(crys.struct2atoms(st))
        keys = [
            'natoms', 'coords', 'coords_frac', 'symbols', 'cryst_const',
            'cell', 'volume', 'mass'
        ]
        tools.assert_dict_with_all_types_almost_equal(\
            st.__dict__,
            st2.__dict__,
            keys=keys,
            strict=True)
        # in case the test fails, use this to find out which key failed
        ##        for kk in keys:
        ##            print("testing: %s ..." %kk)
        ##            tools.assert_all_types_almost_equal(st.__dict__[kk], st2.__dict__[kk])
        for pbc in [True, False]:
            at = st.get_ase_atoms(pbc=pbc)
            assert (at.pbc == np.array([pbc] * 3)).all()
            at = crys.struct2atoms(st, pbc=pbc)
            assert (at.pbc == np.array([pbc] * 3)).all()
    except ImportError:
        tools.skip("cannot import ase, skipping test get_ase_atoms()")
Пример #5
0
def test_mix():
    symbols = ['H']*3 + ['Au']*7
    natoms = len(symbols)
    st1nf = Structure(coords_frac=rand(natoms,3),
                    symbols=symbols,
                    cell=rand(3,3))
    st2nf = Structure(coords_frac=rand(natoms,3),
                    symbols=symbols,
                    cell=rand(3,3))
    st1f = Structure(coords_frac=rand(natoms,3),
                    symbols=symbols,
                    cell=rand(3,3),
                    forces=rand(natoms,3))
    st2f = Structure(coords_frac=rand(natoms,3),
                    symbols=symbols,
                    cell=rand(3,3),
                    forces=rand(natoms,3))
    for st1,st2 in [(st1f, st2f), (st1nf, st2nf)]:
        tr = crys.mix(st1, st2, alpha=np.linspace(0,1,20))
        assert tr.nstep == 20
        assert tr.coords_frac.shape == (20, st1.natoms, 3)
        
        for idx,st in [(0,st1), (-1, st2)]:
            tools.assert_dict_with_all_types_almost_equal(st.__dict__,
                                                          tr[idx].__dict__,
                                                          keys=st1.attr_lst)
        for x in [0.5, 0.9]:
            tr = crys.mix(st1, st2, alpha=np.array([x]))
            assert tr.nstep == 1
            tools.assert_all_types_almost_equal(tr[0].coords, (1-x)*st1.coords + x*st2.coords)
            tools.assert_all_types_almost_equal(tr[0].cell, (1-x)*st1.cell + x*st2.cell)
            if tr.forces is not None:
                tools.assert_all_types_almost_equal(tr[0].forces, (1-x)*st1.forces + x*st2.forces)
Пример #6
0
def test_symmetry():
    tools.skip_if_pkg_missing('pyspglib')
    st_prim = crys.Structure(coords_frac=np.array([[0] * 3, [.5] * 3]),
                             cryst_const=np.array([3.5] * 3 + [60] * 3),
                             symbols=['Al', 'N'])
    st_sc = crys.scell(st_prim, (2, 3, 4))
    st_prim2 = symmetry.spglib_get_primitive(st_sc, symprec=1e-2)
    # irreducible structs
    assert symmetry.spglib_get_primitive(st_prim, symprec=1e-2) is None
    assert symmetry.spglib_get_primitive(st_prim2, symprec=1e-2) is None
    for st in [st_prim, st_sc, st_prim2]:
        assert symmetry.spglib_get_spacegroup(st_prim,
                                              symprec=1e-2) == (225, 'Fm-3m')
    # this is redundant since we have is_same_struct(), but keep it anyway
    tools.assert_dict_with_all_types_almost_equal(
        st_prim.__dict__,
        st_prim2.__dict__,
        keys=['natoms', 'symbols', 'volume', 'cryst_const'])
    assert symmetry.is_same_struct(st_prim, st_prim2)
Пример #7
0
def test_mix():
    symbols = ['H'] * 3 + ['Au'] * 7
    natoms = len(symbols)
    st1nf = Structure(coords_frac=rand(natoms, 3),
                      symbols=symbols,
                      cell=rand(3, 3))
    st2nf = Structure(coords_frac=rand(natoms, 3),
                      symbols=symbols,
                      cell=rand(3, 3))
    st1f = Structure(coords_frac=rand(natoms, 3),
                     symbols=symbols,
                     cell=rand(3, 3),
                     forces=rand(natoms, 3))
    st2f = Structure(coords_frac=rand(natoms, 3),
                     symbols=symbols,
                     cell=rand(3, 3),
                     forces=rand(natoms, 3))
    for st1, st2 in [(st1f, st2f), (st1nf, st2nf)]:
        tr = crys.mix(st1, st2, alpha=np.linspace(0, 1, 20))
        assert tr.nstep == 20
        assert tr.coords_frac.shape == (20, st1.natoms, 3)

        for idx, st in [(0, st1), (-1, st2)]:
            tools.assert_dict_with_all_types_almost_equal(st.__dict__,
                                                          tr[idx].__dict__,
                                                          keys=st1.attr_lst)
        for x in [0.5, 0.9]:
            tr = crys.mix(st1, st2, alpha=np.array([x]))
            assert tr.nstep == 1
            tools.assert_all_types_almost_equal(
                tr[0].coords, (1 - x) * st1.coords + x * st2.coords)
            tools.assert_all_types_almost_equal(
                tr[0].cell, (1 - x) * st1.cell + x * st2.cell)
            if tr.forces is not None:
                tools.assert_all_types_almost_equal(
                    tr[0].forces, (1 - x) * st1.forces + x * st2.forces)
Пример #8
0
def test_gibbs():
    # number of varied axis points
    nax = 6
    # phonon freq axis
    freq = np.linspace(0, 1000, 300)  # cm^-1
    T = np.linspace(5, 2000, 50)
    P = np.linspace(0, 5, 2)

    # 2d case
    case = '2d'
    cell_a = np.linspace(2.5, 3.5, nax)
    cell_c = np.linspace(3, 3.8, nax)
    volfunc_ax = lambda x: x[0]**2 * x[1]
    axes_flat = np.array([x for x in product(cell_a, cell_c)])
    V = np.array([volfunc_ax(x) for x in axes_flat])
    cell_a_mean = cell_a.mean()
    cell_c_mean = cell_c.mean()
    cell_a_min = cell_a.min()
    cell_c_min = cell_c.min()
    etot = np.array([(a - cell_a_mean)**2.0 + (c - cell_c_mean)**2.0
                     for a, c in axes_flat])
    phdos = []
    Vmax = V.max()
    # phonon dos (just a gaussian) shifted to lower (higher) freqs for higher
    # (lower) volume
    for ii in range(axes_flat.shape[0]):
        a, c = axes_flat[ii, :]
        fc = 550 - 50 * V[ii] / Vmax
        phdos.append(np.array([freq, gauss(freq - fc, 100) * 0.01]).T)

    gibbs = Gibbs(T=T,
                  P=P,
                  etot=etot,
                  phdos=phdos,
                  axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax,
                  case=case,
                  dosarea=None)
    gibbs.set_fitfunc('C',
                      lambda x, y: num.Spline(x, y, s=None, k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)

    dr = 'files/gibbs/2d'
    for name in os.listdir(dr):
        fn = '%s/%s' % (dr, name)
        gref = io.read_h5(fn)
        print("testing: %s" % fn)
        compare_dicts_with_arrays(gref, g)
        tools.assert_dict_with_all_types_almost_equal(gref,
                                                      g,
                                                      keys=list(gref.keys()),
                                                      atol=1e-8,
                                                      rtol=1e-3)

    # 1d case
    case = '1d'
    V = np.linspace(10, 20, nax)
    axes_flat = V**(1 / 3.)  # cubic
    volfunc_ax = lambda x: x[0]**3.0
    etot = (V - V.mean())**2
    fcenter = 450 + 100 * (axes_flat - axes_flat.min())
    # fake phonon dos data (Gaussian), shift to lower freq for higher volume
    phdos = [np.array([freq, gauss(freq - fc, 100)]).T for fc in fcenter[::-1]]

    gibbs = Gibbs(T=T,
                  P=P,
                  etot=etot,
                  phdos=phdos,
                  axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax,
                  case=case,
                  dosarea=None)
    gibbs.set_fitfunc('C',
                      lambda x, y: num.Spline(x, y, s=None, k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)

    dr = 'files/gibbs/1d'
    for name in os.listdir(dr):
        fn = '%s/%s' % (dr, name)
        gref = io.read_h5(fn)
        print("testing: %s" % fn)
        compare_dicts_with_arrays(gref, g)
        tools.assert_dict_with_all_types_almost_equal(gref,
                                                      g,
                                                      keys=list(gref.keys()),
                                                      atol=1e-14,
                                                      rtol=1e-8)

    # test enthalpy stuff for 1d case
    # E(V)
    ev = num.PolyFit1D(g['/ax0/V'], g['/ax0/Etot'], deg=5)
    # P(V)
    pv = lambda v: -ev(v, der=1) * constants.eV_by_Ang3_to_GPa
    assert np.allclose(g['/P/P'], pv(g['/#opt/P/V']))
    assert np.allclose(g['/#opt/P/H'],
                       ev(g['/#opt/P/V']) + g['/P/P']*g['/#opt/P/V'] / \
                       constants.eV_by_Ang3_to_GPa)
Пример #9
0
def test_scell():
    cell = np.identity(3)
    coords_frac = np.array([[0.5, 0.5, 0.5],
                       [1,1,1]])
    symbols = ['Al', 'N']
    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=symbols), (2,2,2))

    sc_coords_frac = \
        np.array([[ 0.25,  0.25,  0.25],
                  [ 0.25,  0.25,  0.75],
                  [ 0.25,  0.75,  0.25],
                  [ 0.25,  0.75,  0.75],
                  [ 0.75,  0.25,  0.25],
                  [ 0.75,  0.25,  0.75],
                  [ 0.75,  0.75,  0.25],
                  [ 0.75,  0.75,  0.75],
                  [ 0.5 ,  0.5 ,  0.5 ],
                  [ 0.5 ,  0.5 ,  1.  ],
                  [ 0.5 ,  1.  ,  0.5 ],
                  [ 0.5 ,  1.  ,  1.  ],
                  [ 1.  ,  0.5 ,  0.5 ],
                  [ 1.  ,  0.5 ,  1.  ],
                  [ 1.  ,  1.  ,  0.5 ],
                  [ 1.  ,  1.  ,  1.  ]])

    sc_symbols = ['Al']*8 + ['N']*8
    sc_cell = \
        np.array([[ 2.,  0.,  0.],
                  [ 0.,  2.,  0.],
                  [ 0.,  0.,  2.]])

    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    
    # non-orthorhombic cell
    cell = \
        np.array([[ 1.,  0.5,  0.5],
                  [ 0.25,  1.,  0.2],
                  [ 0.2,  0.5,  1.]])

    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=symbols), (2,2,2))
    sc_cell = \
        np.array([[ 2. ,  1. ,  1. ],
                  [ 0.5,  2. ,  0.4],
                  [ 0.4,  1. ,  2. ]])
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    # crystal coords are cell-independent
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    
    # slab
    #
    # Test if old and new implementation behave for a tricky case: natoms == 2
    # mask.shape[0], i.e. if reshape() behaves correctly. 
    # Reference generated with old implementation. Default is new.
    cell = np.identity(3)
    coords_frac = np.array([[0.5, 0.5, 0.5],
                       [1,1,1]])
    symbols = ['Al', 'N']
    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=symbols), (1,1,2))
    sc_coords_frac = \
        np.array([[ 0.5 ,  0.5 ,  0.25],
                  [ 0.5 ,  0.5 ,  0.75],
                  [ 1.  ,  1.  ,  0.5 ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 1.,  0.,  0.],
                  [ 0.,  1.,  0.],
                  [ 0.,  0.,  2.]])
    sc_symbols = ['Al', 'Al', 'N', 'N']
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)
    
    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=symbols), (1,2,1))
    sc_coords_frac = \
        np.array([[ 0.5 ,  0.25,  0.5 ],
                  [ 0.5 ,  0.75,  0.5 ],
                  [ 1.  ,  0.5 ,  1.  ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 1.,  0.,  0.],
                  [ 0.,  2.,  0.],
                  [ 0.,  0.,  1.]])
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=symbols), (2,1,1))
    sc_coords_frac = \
        np.array([[ 0.25,  0.5 ,  0.5 ],
                  [ 0.75,  0.5 ,  0.5 ],
                  [ 0.5 ,  1.  ,  1.  ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 2.,  0.,  0.],
                  [ 0.,  1.,  0.],
                  [ 0.,  0.,  1.]])
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)
    
    # symbols = None
    sc = crys.scell(Structure(coords_frac=coords_frac,
                              cell=cell,
                              symbols=None), (2,2,2))
    assert sc.symbols is None
    
    # Trajectory
    natoms = 4
    nstep = 100
    symbols = [syms.next() for ii in range(natoms)]
    # cell 2d
    coords_frac = rand(nstep,natoms,3)
    cell = rand(3,3)
    dims = (2,3,4)
    nmask = np.prod(dims)
    sc = crys.scell(Trajectory(coords_frac=coords_frac,
                               cell=cell,
                               symbols=symbols), 
                      dims=dims)
    assert sc.coords_frac.shape == (nstep, nmask*natoms, 3)
    assert sc.symbols == common.flatten([[sym]*nmask for sym in symbols])
    assert sc.cell.shape == (nstep,3,3)                                            
    np.testing.assert_array_almost_equal(sc.cell, 
                                         num.extend_array(cell * np.asarray(dims)[:,None], 
                                                          sc.nstep,
                                                          axis=0))
    # cell 3d
    cell = rand(nstep,3,3)
    sc = crys.scell(Trajectory(coords_frac=coords_frac,
                               cell=cell,
                               symbols=symbols), 
                      dims=dims)
    assert sc.coords_frac.shape == (nstep, nmask*natoms, 3)
    coords_frac2 = np.array([crys.scell(Structure(coords_frac=coords_frac[ii,...,], 
                                                  cell=cell[ii,...],
                                                  symbols=symbols), dims=dims).coords_frac \
                             for ii in range(nstep)])
    np.testing.assert_array_almost_equal(sc.coords_frac, coords_frac2) 
    assert sc.symbols == common.flatten([[sym]*nmask for sym in symbols])
    assert sc.cell.shape == (nstep,3,3) 
    np.testing.assert_array_almost_equal(sc.cell, 
                                         cell * np.asarray(dims)[None,:,None])
    
    # methods
    natoms = 20
    coords_frac = rand(natoms,3)
    cell = rand(3,3)
    dims = (2,3,4)
    symbols = [syms.next() for ii in range(natoms)]
    struct = Structure(coords_frac=coords_frac,
                       cell=cell,
                       symbols=symbols)
    sc1 = crys.scell(struct, dims=dims, method=1)
    sc2 = crys.scell(struct, dims=dims, method=2)
    d1 = dict([(key, getattr(sc1, key)) for key in sc1.attr_lst])
    d2 = dict([(key, getattr(sc2, key)) for key in sc2.attr_lst])
    tools.assert_dict_with_all_types_almost_equal(d1, d2)
Пример #10
0
def test_gibbs():
    # number of varied axis points
    nax = 6
    # phonon freq axis
    freq = np.linspace(0,1000,300) # cm^-1
    T = np.linspace(5, 2000, 50)
    P = np.linspace(0,5,2)

    # 2d case
    case = '2d'
    cell_a = np.linspace(2.5,3.5,nax)
    cell_c = np.linspace(3,3.8,nax)
    volfunc_ax = lambda x: x[0]**2 * x[1]
    axes_flat = np.array([x for x in product(cell_a, cell_c)])
    V = np.array([volfunc_ax(x) for x in axes_flat])
    cell_a_mean = cell_a.mean()
    cell_c_mean = cell_c.mean()
    cell_a_min = cell_a.min()
    cell_c_min = cell_c.min()
    etot = np.array([(a-cell_a_mean)**2.0 + (c-cell_c_mean)**2.0 for a,c in axes_flat])
    phdos = []
    Vmax = V.max()
    # phonon dos (just a gaussian) shifted to lower (higher) freqs for higher
    # (lower) volume
    for ii in range(axes_flat.shape[0]):
        a,c = axes_flat[ii,:]
        fc = 550 - 50 * V[ii] / Vmax
        phdos.append(np.array([freq,gauss(freq-fc,100)*0.01]).T)

    gibbs = Gibbs(T=T, P=P, etot=etot, phdos=phdos, axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax, case=case, dosarea=None)
    gibbs.set_fitfunc('C', lambda x,y: num.Spline(x,y,s=None,k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)
    
    dr = 'files/gibbs/2d'
    for name in os.listdir(dr):
        fn = '%s/%s' %(dr, name)
        gref = io.read_h5(fn)
        print "testing: %s" %fn
        compare_dicts_with_arrays(gref, g) 
        tools.assert_dict_with_all_types_almost_equal(gref, 
                                                      g, 
                                                      keys=gref.keys(),
                                                      atol=1e-8, rtol=1e-3)

    # 1d case
    case = '1d'
    V = np.linspace(10,20,nax)
    axes_flat = V**(1/3.) # cubic
    volfunc_ax = lambda x: x[0]**3.0
    etot = (V-V.mean())**2
    fcenter = 450 + 100*(axes_flat - axes_flat.min())
    # fake phonon dos data (Gaussian), shift to lower freq for higher volume
    phdos = [np.array([freq,gauss(freq-fc, 100)]).T for fc in
             fcenter[::-1]]

    gibbs = Gibbs(T=T, P=P, etot=etot, phdos=phdos, axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax, case=case, dosarea=None)
    gibbs.set_fitfunc('C', lambda x,y: num.Spline(x,y,s=None,k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)
    
    dr = 'files/gibbs/1d'
    for name in os.listdir(dr):
        fn = '%s/%s' %(dr, name)
        gref = io.read_h5(fn)
        print "testing: %s" %fn
        compare_dicts_with_arrays(gref, g) 
        tools.assert_dict_with_all_types_almost_equal(gref, 
                                                      g, 
                                                      keys=gref.keys(),
                                                      atol=1e-14, rtol=1e-14)
    
    # test enthalpy stuff for 1d case
    # E(V) 
    ev = num.PolyFit1D(g['/ax0/V'], g['/ax0/Etot'], deg=5)
    # P(V)
    pv = lambda v: -ev(v, der=1)*constants.eV_by_Ang3_to_GPa
    assert np.allclose(g['/P/P'], pv(g['/#opt/P/V']))
    assert np.allclose(g['/#opt/P/H'], 
                       ev(g['/#opt/P/V']) + g['/P/P']*g['/#opt/P/V'] / \
                       constants.eV_by_Ang3_to_GPa)
Пример #11
0
def test_scell():
    cell = np.identity(3)
    coords_frac = np.array([[0.5, 0.5, 0.5], [1, 1, 1]])
    symbols = ['Al', 'N']
    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=symbols),
        (2, 2, 2))

    sc_coords_frac = \
        np.array([[ 0.25,  0.25,  0.25],
                  [ 0.25,  0.25,  0.75],
                  [ 0.25,  0.75,  0.25],
                  [ 0.25,  0.75,  0.75],
                  [ 0.75,  0.25,  0.25],
                  [ 0.75,  0.25,  0.75],
                  [ 0.75,  0.75,  0.25],
                  [ 0.75,  0.75,  0.75],
                  [ 0.5 ,  0.5 ,  0.5 ],
                  [ 0.5 ,  0.5 ,  1.  ],
                  [ 0.5 ,  1.  ,  0.5 ],
                  [ 0.5 ,  1.  ,  1.  ],
                  [ 1.  ,  0.5 ,  0.5 ],
                  [ 1.  ,  0.5 ,  1.  ],
                  [ 1.  ,  1.  ,  0.5 ],
                  [ 1.  ,  1.  ,  1.  ]])

    sc_symbols = ['Al'] * 8 + ['N'] * 8
    sc_cell = \
        np.array([[ 2.,  0.,  0.],
                  [ 0.,  2.,  0.],
                  [ 0.,  0.,  2.]])

    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)

    # non-orthorhombic cell
    cell = \
        np.array([[ 1.,  0.5,  0.5],
                  [ 0.25,  1.,  0.2],
                  [ 0.2,  0.5,  1.]])

    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=symbols),
        (2, 2, 2))
    sc_cell = \
        np.array([[ 2. ,  1. ,  1. ],
                  [ 0.5,  2. ,  0.4],
                  [ 0.4,  1. ,  2. ]])
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    # crystal coords are cell-independent
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    # slab
    #
    # Test if old and new implementation behave for a tricky case: natoms == 2
    # mask.shape[0], i.e. if reshape() behaves correctly.
    # Reference generated with old implementation. Default is new.
    cell = np.identity(3)
    coords_frac = np.array([[0.5, 0.5, 0.5], [1, 1, 1]])
    symbols = ['Al', 'N']
    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=symbols),
        (1, 1, 2))
    sc_coords_frac = \
        np.array([[ 0.5 ,  0.5 ,  0.25],
                  [ 0.5 ,  0.5 ,  0.75],
                  [ 1.  ,  1.  ,  0.5 ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 1.,  0.,  0.],
                  [ 0.,  1.,  0.],
                  [ 0.,  0.,  2.]])
    sc_symbols = ['Al', 'Al', 'N', 'N']
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=symbols),
        (1, 2, 1))
    sc_coords_frac = \
        np.array([[ 0.5 ,  0.25,  0.5 ],
                  [ 0.5 ,  0.75,  0.5 ],
                  [ 1.  ,  0.5 ,  1.  ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 1.,  0.,  0.],
                  [ 0.,  2.,  0.],
                  [ 0.,  0.,  1.]])
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=symbols),
        (2, 1, 1))
    sc_coords_frac = \
        np.array([[ 0.25,  0.5 ,  0.5 ],
                  [ 0.75,  0.5 ,  0.5 ],
                  [ 0.5 ,  1.  ,  1.  ],
                  [ 1.  ,  1.  ,  1.  ]])
    sc_cell = \
        np.array([[ 2.,  0.,  0.],
                  [ 0.,  1.,  0.],
                  [ 0.,  0.,  1.]])
    assert sc.symbols == sc_symbols
    np.testing.assert_array_almost_equal(sc.cell, sc_cell)
    np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac)

    # symbols = None
    sc = crys.scell(
        Structure(coords_frac=coords_frac, cell=cell, symbols=None), (2, 2, 2))
    assert sc.symbols is None

    # Trajectory
    natoms = 4
    nstep = 100
    symbols = [next(syms) for ii in range(natoms)]
    # cell 2d
    coords_frac = rand(nstep, natoms, 3)
    cell = rand(3, 3)
    dims = (2, 3, 4)
    nmask = np.prod(dims)
    sc = crys.scell(Trajectory(coords_frac=coords_frac,
                               cell=cell,
                               symbols=symbols),
                    dims=dims)
    assert sc.coords_frac.shape == (nstep, nmask * natoms, 3)
    assert sc.symbols == common.flatten([[sym] * nmask for sym in symbols])
    assert sc.cell.shape == (nstep, 3, 3)
    np.testing.assert_array_almost_equal(
        sc.cell,
        num.extend_array(cell * np.asarray(dims)[:, None], sc.nstep, axis=0))
    # cell 3d
    cell = rand(nstep, 3, 3)
    sc = crys.scell(Trajectory(coords_frac=coords_frac,
                               cell=cell,
                               symbols=symbols),
                    dims=dims)
    assert sc.coords_frac.shape == (nstep, nmask * natoms, 3)
    coords_frac2 = np.array([crys.scell(Structure(coords_frac=coords_frac[ii,...,],
                                                  cell=cell[ii,...],
                                                  symbols=symbols), dims=dims).coords_frac \
                             for ii in range(nstep)])
    np.testing.assert_array_almost_equal(sc.coords_frac, coords_frac2)
    assert sc.symbols == common.flatten([[sym] * nmask for sym in symbols])
    assert sc.cell.shape == (nstep, 3, 3)
    np.testing.assert_array_almost_equal(
        sc.cell,
        cell * np.asarray(dims)[None, :, None])

    # methods
    natoms = 20
    coords_frac = rand(natoms, 3)
    cell = rand(3, 3)
    dims = (2, 3, 4)
    symbols = [next(syms) for ii in range(natoms)]
    struct = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols)
    sc1 = crys.scell(struct, dims=dims, method=1)
    sc2 = crys.scell(struct, dims=dims, method=2)
    d1 = dict([(key, getattr(sc1, key)) for key in sc1.attr_lst])
    d2 = dict([(key, getattr(sc2, key)) for key in sc2.attr_lst])
    tools.assert_dict_with_all_types_almost_equal(d1, d2)