Ejemplo n.º 1
0
def test_init():
    """ Test atom initialization. """
    from numpy import all, abs, array

    # Try correct initialization. Check for garbage collection.
    a = Atom()
    assert a.type is None and all(abs(a.pos) < 1e-8) and len(a.__dict__) == 2
    assert a.type is None and all(abs(a.pos) < 1e-8) and len(a.__dict__) == 2

    a = Atom(0.1, 0.1, 0.1, "Au")
    assert a.type == "Au"
    assert all(abs(a.pos - 0.1) < 1e-8)
    assert len(a.__dict__) == 2

    a = Atom(0.1, 0.1, 0.1, type=["Au", "Pd"])
    assert a.type == ["Au", "Pd"]
    assert all(abs(a.pos - 0.1) < 1e-8)
    assert len(a.__dict__) == 2

    a = Atom(type="Au", pos=[0.1, 0.1, 0.1])
    assert a.type == "Au" and all(abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 2
    a = Atom(type="Au", pos=[0.1, 0.1, 0.1], m=5)
    assert a.type == "Au" and all(abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 3 and getattr(a, "m", 3) == 5
    a = Atom(0.1, 0.1, 0.1, 0.1, 0.1)
    assert all(abs(array(a.type) - 0.1) < 1e-8) and all(abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 2
    l = [None]
    a = Atom(0.1, 0.1, 0.1, l)
    assert a.type is l
    assert all(abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 2
    a.pos[0] = 0.2
    a.pos[1] = 0.3
    a.pos[2] = 0.4
    assert all(abs(a.pos - [0.2, 0.3, 0.4]) < 1e-8)
Ejemplo n.º 2
0
def test_fail_init():
    """ Test failures during initialization. """
    from pytest import raises
    from pylada import error
    with raises(error.TypeError):
        a = Atom(0.1, 0.1, 0.1, 'Au', type='Au')
    with raises(error.TypeError):
        a = Atom(0.1, 0.1, 0.1, pos=[0.1, 0.1, 0.1])
Ejemplo n.º 3
0
def test_copy():
    """ Test copy and deep copy. """
    from copy import copy, deepcopy
    b = Atom(0, 0, 0, 'Au', m=0)
    a = copy(b)
    b.type = 'Pd'
    assert a.pos is b.pos
    assert a.type == 'Au'
    a = deepcopy(b)
    b.type = 'Au'
    b.pos += 1
    del b.m
    assert a.type == "Pd"
    assert all(abs(a.pos - 0.0) < 1e-8)
    assert len(a.__dict__) == 3
    assert getattr(a, 'm', 1) == 0
Ejemplo n.º 4
0
 def create_al():
     types = 'ABCDEFGHIJKLMN'
     result = Structure(identity(3) * 2.5, scale=5.45,
                        m=True), list(range(10))
     for i in range(10):
         result[0].add_atom(Atom(i, i, i, types[i]))
     return result
Ejemplo n.º 5
0
def test_copy():
    """ Test copy and deep copy. """
    from copy import copy, deepcopy

    b = Atom(0, 0, 0, "Au", m=0)
    a = copy(b)
    b.type = "Pd"
    assert a.pos is b.pos
    assert a.type == "Au"
    a = deepcopy(b)
    b.type = "Au"
    b.pos += 1
    del b.m
    assert a.type == "Pd"
    assert all(abs(a.pos - 0.0) < 1e-8)
    assert len(a.__dict__) == 3
    assert getattr(a, "m", 1) == 0
Ejemplo n.º 6
0
def test_pickle():
    """ Test pickling. """
    from pickle import loads, dumps
    a = Atom(pos=[0, 1, 2], type="Au", m=6)
    b = loads(dumps(a))
    assert b.__class__ is a.__class__
    assert all(abs(b.pos - [0, 1, 2]) < 1e-12)
    assert b.type == 'Au'
    assert len(b.__dict__) == 3
    assert b.__dict__.get('m', 0) == 6
    b = loads(dumps((a, a)))
    assert b[0] is b[1]
Ejemplo n.º 7
0
def test_pickle():
    """ Check pickling. """
    from numpy import all, abs, array, identity
    from pickle import loads, dumps
    a = Structure(identity(3) * 2.5, scale=5.45, m=True)\
        .add_atom(Atom(0, 0, 0, "Au"))\
        .add_atom(Atom(0.25, 0.5, 0.25, "Au", "Pd", m=True))\
        .add_atom(Atom(0.1, 0.1, 0.1, 6, m=True))
    b = loads(dumps(a))
    assert a is not b
    assert b.__class__ is Structure
    assert all(abs(a.cell - b.cell) < 1e-8)
    assert abs(a.scale - b.scale) < 1e-8
    assert getattr(b, 'm', False) == True
    assert len(b) == 3

    for i, j in zip(a, b):
        assert i is not j
        assert i.__class__ is j.__class__
        assert all(abs(i.pos - j.pos) < 1e-8)
        assert i.type == j.type
        assert getattr(i, 'm', False) == getattr(j, 'm', False)
Ejemplo n.º 8
0
def test_sequence_of_atoms():
    a = Structure(identity(3) * 2.5, scale=5.45, m=True)
    a.add_atom(0, 0, 0, "Au")\
     .add_atom(0.25, 0.5, 0.25, "Au", "Pd", m=True)

    assert len(a) == 2
    assert all(abs(a[0].pos) < 1e-8)
    assert a[0].type == "Au"

    assert all(abs(a[1].pos - (0.25, 0.5, 0.25)) < 1e-8)
    assert a[1].type == ("Au", "Pd")
    assert getattr(a[1], 'm', False) == True

    a.insert(1, 0.1, 0.1, 0.1, 6)
    assert len(a) == 3
    assert all(abs(a[1].pos - 0.1) < 1e-8)
    assert a[1].type == 6

    b = a.pop(1)
    assert all(abs(b.pos - 0.1) < 1e-8)
    assert b.type == 6
    assert len(a) == 2

    a.append(b)
    assert len(a) == 3
    assert all(abs(a[2].pos - 0.1) < 1e-8)
    assert a[2].type == 6

    b = a[0], a[1], a[2]
    a.clear()
    assert len(a) == 0
    a.extend(b)
    assert len(a) == 3 and a[0] is b[0] and a[1] is b[1] and a[2] is b[2]

    a.clear()
    b = Structure(identity(3) * 2.5, scale=5.45, m=True)
    b.add_atom(0, 0, 0, "Au")\
     .add_atom(0.25, 0.5, 0.25, "Au", "Pd", m=True)\
     .add_atom(0.1, 0.1, 0.1, 6, m=True)
    assert len(a) == 0
    a.extend(b)
    assert len(b) == 3
    assert a[0] is b[0]
    assert a[1] is b[1]
    assert a[2] is b[2]
    assert a is not b

    a[2] = Atom(-1, -1, -1, None)
    assert abs(all(a[2].pos + 1) < 1e-8) and a[2].type is None
Ejemplo n.º 9
0
def test_repr():
    """ Test representability. """
    actual = repr(Atom(type='Au', pos=[1, 1, 1], m=1, dtype='int64'))
    assert actual == "{0.__name__}(1, 1, 1, 'Au', dtype='int64', m=1)".format(Atom)
    actual = str(Atom(type='Au', pos=[1., 1, 1], site=1))
    assert actual == "{0.__name__}(1.0, 1.0, 1.0, 'Au', site=1)".format(Atom)
Ejemplo n.º 10
0
def test_init():
    """ Test atom initialization. """
    from numpy import all, abs, array

    # Try correct initialization. Check for garbage collection.
    a = Atom()
    assert a.type is None and all(abs(a.pos) < 1e-8) and len(a.__dict__) == 2
    assert a.type is None and all(abs(a.pos) < 1e-8) and len(a.__dict__) == 2

    a = Atom(0.1, 0.1, 0.1, 'Au')
    assert a.type == "Au"
    assert all(abs(a.pos - 0.1) < 1e-8)
    assert len(a.__dict__) == 2

    a = Atom(0.1, 0.1, 0.1, type=['Au', 'Pd'])
    assert a.type == ["Au", 'Pd']
    assert all(abs(a.pos - 0.1) < 1e-8)
    assert len(a.__dict__) == 2

    a = Atom(type='Au', pos=[0.1, 0.1, 0.1])
    assert a.type == "Au" and all(
        abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 2
    a = Atom(type='Au', pos=[0.1, 0.1, 0.1], m=5)
    assert a.type == "Au" and all(
        abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 3 and getattr(a, 'm', 3) == 5
    a = Atom(0.1, 0.1, 0.1, 0.1, 0.1)
    assert all(abs(array(a.type) - 0.1) < 1e-8) and all(abs(a.pos - 0.1)
                                                        < 1e-8) and len(a.__dict__) == 2
    l = [None]
    a = Atom(0.1, 0.1, 0.1, l)
    assert a.type is l
    assert all(abs(a.pos - 0.1) < 1e-8) and len(a.__dict__) == 2
    a.pos[0] = 0.2
    a.pos[1] = 0.3
    a.pos[2] = 0.4
    assert all(abs(a.pos - [0.2, 0.3, 0.4]) < 1e-8)
Ejemplo n.º 11
0
def test_todict():
    """ Test to_dict member. """
    a = Atom(0, 0, 0, 'Au', m=0)
    a = a.to_dict()
    assert a['type'] == "Au" and all(a['pos'] == 0) and a['m'] == 0
Ejemplo n.º 12
0
def test_todict():
    """ Test to_dict member. """
    a = Atom(0, 0, 0, "Au", m=0)
    a = a.to_dict()
    assert a["type"] == "Au" and all(a["pos"] == 0) and a["m"] == 0