Exemplo n.º 1
0
def test_recip_cell():
    # reciprocal cell
    cell = rand(3, 3)
    rcell = crys.recip_cell(cell)
    vol = crys.volume_cell(cell)
    try:
        assert np.allclose(crys.recip_cell(rcell), cell)
    except AssertionError:
        assert np.allclose(crys.recip_cell(rcell), -1.0 * cell)
    assert np.allclose(crys.volume_cell(rcell), (2 * pi)**3.0 / vol)
Exemplo n.º 2
0
def test_recip_cell():
    # reciprocal cell
    cell = rand(3,3)
    rcell = crys.recip_cell(cell)
    vol = crys.volume_cell(cell)
    try:
        assert np.allclose(crys.recip_cell(rcell), cell)
    except AssertionError:        
        assert np.allclose(crys.recip_cell(rcell), -1.0 * cell)
    assert np.allclose(crys.volume_cell(rcell), (2*pi)**3.0 / vol)
Exemplo n.º 3
0
def test_cell_tools():
    # test known values: simple cubic
    cell_a = 5.0
    cell = np.identity(3) * cell_a
    volume = cell_a**3.0
    cryst_const = np.array([cell_a] * 3 + [90.0] * 3)
    np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const)
    np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell)
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, crys.volume_cell(cell))
    np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)),
                                         cell)
    np.testing.assert_array_almost_equal(
        crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const)

    # random
    #
    # volume : volume_cc() always returns positive values, whereas det() and
    #     volume_cell() may return the volume with negative sign but correct
    #     magnitude.
    # cell : A random cell does also have a random orientation in space. It
    #     does NOT conform the usual convention: a along x, b in x-y plane.
    #     However, the cryst_const and volume must be invariant.
    cell = np.random.rand(3, 3)
    cryst_const = crys.cell2cc(cell)
    volume = abs(np.linalg.det(cell))
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell)))
    # this must always fail for random cells
    try:
        np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)),
                                             cell)
    except AssertionError:
        pass
    # Here, we convert cryst_const to a *different* cell which conforms to the
    # orientation convention, and back to cryst_const.
    np.testing.assert_array_almost_equal(
        crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const)

    # 3d
    cell = rand(100, 3, 3)
    cc = crys.cell2cc3d(cell, axis=0)
    vol_cell = np.abs(crys.volume_cell3d(cell, axis=0))
    vol_cc = crys.volume_cc3d(cc, axis=0)

    assert crys.cell2cc3d(cell, axis=0).shape == (100, 6)
    assert crys.cc2cell3d(cc, axis=0).shape == (100, 3, 3)

    assert vol_cc.shape == (100, )
    assert vol_cell.shape == (100, )
    aaae(vol_cell, vol_cc)
    aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
Exemplo n.º 4
0
def test_cell_tools():
    # test known values: simple cubic
    cell_a = 5.0
    cell = np.identity(3)*cell_a
    volume = cell_a**3.0
    cryst_const = np.array([cell_a]*3 + [90.0]*3)
    np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const)
    np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell)
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, crys.volume_cell(cell))
    np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell)
    np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)),
                                         cryst_const)
                                         
    # random
    #
    # volume : volume_cc() always returns positive values, whereas det() and
    #     volume_cell() may return the volume with negative sign but correct
    #     magnitude.
    # cell : A random cell does also have a random orientation in space. It
    #     does NOT conform the usual convention: a along x, b in x-y plane.
    #     However, the cryst_const and volume must be invariant.
    cell = np.random.rand(3,3)
    cryst_const = crys.cell2cc(cell)
    volume = abs(np.linalg.det(cell))
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell)))
    # this must always fail for random cells
    try:
        np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell)
    except AssertionError:
        pass
    # Here, we convert cryst_const to a *different* cell which conforms to the
    # orientation convention, and back to cryst_const.
    np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)),
                                         cryst_const)

    # 3d
    cell = rand(100,3,3)
    cc = crys.cell2cc3d(cell, axis=0)
    vol_cell = np.abs(crys.volume_cell3d(cell, axis=0))
    vol_cc = crys.volume_cc3d(cc, axis=0)

    assert crys.cell2cc3d(cell, axis=0).shape == (100,6)
    assert crys.cc2cell3d(cc, axis=0).shape == (100,3,3)
    
    assert vol_cc.shape == (100,)
    assert vol_cell.shape == (100,)
    aaae(vol_cell, vol_cc)
    aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
Exemplo n.º 5
0
def test_struct():
    natoms = 10
    cell = np.array([[3, 0, 0], [1.1, 5, -0.04], [-0.33, 1.5, 7]])
    cryst_const = crys.cell2cc(cell)
    coords_frac = rand(natoms, 3)
    coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3))
    symbols = ['H'] * natoms
    stress = rand(3, 3)
    forces = rand(natoms, 3)

    # Use ``cell`` instead of ``cryst_const` as input such that
    # atoms.get_cell() test passes (see below for why -- cell orientation)
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell,
                   stress=stress,
                   forces=forces,
                   etot=42)

    # Test if all getters work.
    for name in st.attr_lst:
        print(name)
        st.try_set_attr(name)
        assert getattr(st, name) is not None, "attr None: %s" % name
        assert eval('st.get_%s()' %
                    name) is not None, "getter returns None: %s" % name
    aaae(coords_frac, st.coords_frac)
    aaae(cryst_const, st.cryst_const)
    aaae(coords, st.coords)
    assert st.natoms == natoms

    st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell)
    aaae(coords, st.get_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.
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cryst_const=cryst_const)
    assert st.get_cell() is not None
    np.testing.assert_almost_equal(crys.volume_cell(cell),
                                   crys.volume_cell(st.get_cell()))
    aaae(cryst_const, crys.cell2cc(st.get_cell()))

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

    traj = crys.struct2traj(st)
    assert traj.is_traj

    # copy(): Assert everything has another memory location = is a new copy of
    # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in
    # Structure.copy(), apparently b/c they are always automatically copied
    # before in-place operations. Same for float type.
    #
    # >>> a=10; b=a; print id(a); print id(b)
    # 36669152
    # 36669152
    # >>> a*=100; print id(a); print id(b)
    # 72538264
    # 36669152
    # >>> a
    # 100
    # >>> b
    # 10
    #
    # >>> a=[1,2,3]; b=a; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a[0] = 44; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a
    # [44, 2, 3]
    # >>> b
    # [44, 2, 3]
    st2 = st.copy()
    for name in st.attr_lst:
        val = getattr(st, name)
        if val is not None and not (isinstance(val, int) or \
            isinstance(val, float)):
            val2 = getattr(st2, name)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
Exemplo n.º 6
0
def test_struct():
    natoms = 10
    cell = np.array([[3,0,0],
                     [1.1,5,-0.04],
                     [-0.33,1.5,7]])
    cryst_const = crys.cell2cc(cell)                 
    coords_frac = rand(natoms,3)
    coords = crys.coord_trans(coords=coords_frac,
                              old=cell,
                              new=np.identity(3))
    symbols = ['H']*natoms
    stress = rand(3,3)
    forces = rand(natoms,3)

    # Use ``cell`` instead of ``cryst_const` as input such that
    # atoms.get_cell() test passes (see below for why -- cell orientation)
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell,
                   stress=stress,
                   forces=forces,
                   etot=42)
    
    # Test if all getters work.
    for name in st.attr_lst:
        print name
        st.try_set_attr(name)
        assert getattr(st, name) is not None, "attr None: %s" %name
        assert eval('st.get_%s()'%name) is not None, "getter returns None: %s" %name
    aaae(coords_frac, st.coords_frac)
    aaae(cryst_const, st.cryst_const)
    aaae(coords, st.coords)
    assert st.natoms == natoms

    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell)
    aaae(coords, st.get_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.
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cryst_const=cryst_const)
    assert st.get_cell() is not None
    np.testing.assert_almost_equal(crys.volume_cell(cell),
                                   crys.volume_cell(st.get_cell()))
    aaae(cryst_const, crys.cell2cc(st.get_cell()))

    # units
    st = Structure(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    stress=stress,
                    forces=forces,
                    units={'length': 2, 'forces': 3, 'stress': 4})
    aaae(2*coords, st.coords)                    
    aaae(3*forces, st.forces)                    
    aaae(4*stress, st.stress)                    
    
    traj = crys.struct2traj(st)
    assert traj.is_traj

    # copy(): Assert everything has another memory location = is a new copy of
    # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in
    # Structure.copy(), apparently b/c they are always automatically copied
    # before in-place operations. Same for float type. 
    #
    # >>> a=10; b=a; print id(a); print id(b)
    # 36669152
    # 36669152
    # >>> a*=100; print id(a); print id(b)
    # 72538264
    # 36669152
    # >>> a
    # 100
    # >>> b
    # 10
    #
    # >>> a=[1,2,3]; b=a; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a[0] = 44; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a
    # [44, 2, 3]
    # >>> b
    # [44, 2, 3]
    st2 = st.copy()
    for name in st.attr_lst:
        val = getattr(st,name)
        if val is not None and not (isinstance(val, types.IntType) or \
            isinstance(val, types.FloatType)):
            val2 = getattr(st2,name)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)