Пример #1
0
def test_AtomsList_sort():
    """Tests the method AtomsList.sort
    """
    from matdb.atoms import Atoms, AtomsList

    at1 = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43],info={"rand":10})
    at2 = Atoms("S6",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75]],
                 cell=[6.43,5.43,4.43],info={"rand":10})
    at3 = Atoms("CNi",positions=[[0,0,0],[0.5,0.5,0.5]], info={"rand":8})
    at4 = Atoms("CoV",positions=[[0,0,0],[0.25,0.5,0.25]], info={"rand":8})

    at1.add_param("vasp_energy", 25361.504084423999)
    at2.add_param("vasp_energy", 25362.504084423999)
    at3.add_param("vasp_energy", 25363.504084423999)
    at4.add_param("vasp_energy", 25364.504084423999)
    
    al1 = AtomsList([at4,at2,at1,at3])

    #This is to test __getitem__
    al2 = al1[0:2]
    assert len(al2) == 2

    al1.sort(key=len)
    assert al1[0].get_chemical_formula() == "CoV"
    assert al1[1].get_chemical_formula() == "CNi"
    assert al1[2].get_chemical_formula() == "S6"
    assert al1[3].get_chemical_formula() == "Si8"

    al1.sort(attr="vasp_energy")
    assert al1[0].get_chemical_formula() == "Si8"
    assert al1[1].get_chemical_formula() == "S6"
    assert al1[2].get_chemical_formula() == "CNi"
    assert al1[3].get_chemical_formula() == "CoV"

    al1.sort(attr="vasp_energy", reverse=True)
    assert al1[0].get_chemical_formula() == "CoV"
    assert al1[1].get_chemical_formula() == "CNi"
    assert al1[2].get_chemical_formula() == "S6"
    assert al1[3].get_chemical_formula() == "Si8"
Пример #2
0
def test_AtomsList_attributes():
    """Tests the atoms lists attributes.
    """
    from matdb.atoms import Atoms, AtomsList

    at1 = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43],info={"rand":10})
    
    at2 = Atoms("S6",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75]],
                 cell=[6.43,5.43,4.43],info={"rand":10})

    at3 = Atoms("CNi",positions=[[0,0,0],[0.5,0.5,0.5]], info={"rand":8})
    at4 = Atoms(info={"rand":7})
    at4.copy_from(at3)
    
    al1 = AtomsList([at1,at2,at3,at4])

    alpos = al1.positions
    assert np.allclose(alpos[0],at1.positions)
    assert np.allclose(alpos[1],at2.positions)
    assert np.allclose(alpos[2],at3.positions)
    assert np.allclose(alpos[3],at4.positions)

    with pytest.raises(AttributeError):
        al1.__getattr__('__dict__')
    assert al1.energy is None

    alslice = al1[0:2]
    assert len(alslice) == 2
    assert alslice[0] == at1
    assert alslice[1] == at2

    alitems = al1[[0,1,3]]
    assert len(alitems) == 3
    assert alitems[0] == at1
    assert alitems[1] == at2
    assert alitems[2] == at4

    with pytest.raises(IndexError):
        al1[[0,2.3]]

    alitems = al1[[True,True,False,True]]
    assert len(alitems) == 3

    for i in al1.iterframes():
        assert isinstance(i,Atoms)
    
    for i in al1.iterframes(reverse=True):
        assert isinstance(i,Atoms)

    assert al1.random_access

    def get_pos(atoms):
        return atoms.positions

    alpos=al1.apply(get_pos)
    assert np.allclose(alpos[0],at1.positions)
    assert np.allclose(alpos[1],at2.positions)
    assert np.allclose(alpos[2],at3.positions)
    assert np.allclose(alpos[3],at4.positions)

    al1.sort(reverse=True)
    al1.sort(attr='rand')

    with pytest.raises(ValueError):
        al1.sort(attr='positions',key=2)