Ejemplo n.º 1
0
def test_Atoms_copy_from_aseAtoms():
    """Tests the mothed Atoms.copy_from() to copy from an ase.atoms
    """
    from ase.atoms import Atoms as aseAtoms
    from matdb.atoms import Atoms
    from numpy import array_equal

    at1 = aseAtoms("Co3W2V3",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[1.75,1.75,1.25],
                                  [1.5,1,1.5],[2.75,2.25,2.75],[2,2.5,2.5],[2.25,2.75,2.75]],
                 cell=[5.43,5.43,5.43])

    at1.info['nneightol'] = 1112
    at1.info['cutoff'] = 521
    at1.info['cutoff_break'] = 1042

    at2 = Atoms()
    at2.copy_from(at1)

    assert at2.info["params"]['nneightol'] == at1.info['nneightol']
    assert at2.info["params"]['cutoff'] == at1.info['cutoff']
    assert at2.info["params"]['cutoff_break'] == at1.info['cutoff_break']

    # make sure the symbols and positions are still match
    assert at1.get_chemical_symbols() == at2.get_chemical_symbols()
    assert array_equal(at1.positions, at2.positions)
    assert array_equal(at1.cell, at2.cell)
Ejemplo n.º 2
0
def test_AtomsList_io(tmpdir):
    """Tests the AtomsList writing and reading from file.
    """
    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]])
    at4 = Atoms()
    at4.copy_from(at3)
    
    al1 = AtomsList([at1,at2,at3,at4])
    
    target = str(tmpdir.join("atomList_to_hdf5"))
    if not path.isdir(target):
        mkdir(target)

    al1.write(path.join(target,"temp.h5"))

    aR = AtomsList()
    aR.read(path.join(target,"temp.h5"))

    assert len(aR) == len(al1)

    alpos = aR.positions
    assert any([np.allclose(alpos[i],at1.positions) for i in range(4) if
                len(alpos[i])==len(at1.positions)])
    assert any([np.allclose(alpos[i],at2.positions) for i in range(4) if
                len(alpos[i])==len(at2.positions)])
    assert any([np.allclose(alpos[i],at3.positions) for i in range(4) if
                len(alpos[i])==len(at3.positions)])
    assert any([np.allclose(alpos[i],at4.positions) for i in range(4) if
                len(alpos[i])==len(at4.positions)])

    al1.write(path.join(target,"temp.xyz"))

    aR = AtomsList()
    aR.read(path.join(target,"temp.xyz"))

    assert len(aR) == len(al1)

    aR.read(path.join(target,"temp.xyz"))
    assert len(aR) == 2*len(al1)


    # Test reading in of a single atoms object.

    aR1 = Atoms(path.join(target,"temp.h5"))
    assert isinstance(aR1,Atoms)
    assert any([np.allclose(alpos[i],at1.positions) for i in range(4) if
                len(alpos[i])==len(at1.positions)])
Ejemplo n.º 3
0
def test_Atoms_copy_from():
    """Tests the mothed Atoms.copy_from to copy from an matdb.atoms
    """
    from matdb.atoms import Atoms
    from numpy import array_equal

    at1 = Atoms("Co3W2V3",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[1.75,1.75,1.25],
                                  [1.5,1,1.5],[2.75,2.25,2.75],[2,2.5,2.5],[2.25,2.75,2.75]],
                 cell=[5.43,5.43,5.43],info={"rand":10, "params":{"vasp_energy": 1234}, "properties":{}})
    at1.__setattr__("magnetic_moments", [1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9])

    at2 = Atoms()
    at2.copy_from(at1)

    assert at1.info["params"] == at2.info["params"]
    assert at1.info["properties"] == at2.info["properties"]

    # make sure the symbols and positions are still match
    assert at1.get_chemical_symbols() == at2.get_chemical_symbols()
    assert array_equal(at1.positions, at2.positions)
    assert array_equal(at1.cell, at2.cell)
Ejemplo n.º 4
0
def _cfgd_to_atoms(cfgd, species=None):
    """Converts a CFG dictionary to an atoms object.
    Args:
        cfgd (dict): dict of a single config extracted by :func:`cfg_to_xyz`.
        species (list): list of element names corresponding to the integer species in
          the CFG dictionary.
    """
    from matdb.atoms import Atoms

    lattice = np.array(cfgd["Supercell"]["vals"])
    natoms = cfgd["Size"]["vals"][0][0]
    stressdict = cfgd["PlusStress"]
    stress = {k: v for k, v in zip(stressdict["cols"], stressdict["vals"][0])}
    energy = cfgd["Energy"]["vals"][0][0]

    alabels = cfgd["AtomData"]["cols"]
    positions = []
    forces = []
    types = []

    for entry in cfgd["AtomData"]["vals"]:
        vals = {k: v for k, v in zip(alabels, entry)}
        pos = []
        force = []
        types.append(species[vals["type"]])
        for poslabel in _cfg_pos:
            pos.append(vals[poslabel])
        positions.append(pos)

        for flabel in _cfg_force:
            force.append(vals[flabel])
        forces.append(force)

    aseatoms = ase.Atoms(symbols=types,
                         positions=np.array(positions),
                         cell=lattice)
    aseatoms.calc = SinglePointCalculator(aseatoms,
                                          energy=energy,
                                          forces=np.array(forces),
                                          stress=order_stress(**stress))
    aseatoms.get_total_energy()
    aseatoms.get_forces()
    aseatoms.get_stress()

    result = Atoms()
    result.copy_from(aseatoms)
    result.pbc = True

    prefix = ""
    if "EFS_by" in cfgd["features"]:
        prefix = "{}_".format(cfgd["features"]["EFS_by"][0].lower())

    force_name, energy_name, virial_name = [
        "{}{}".format(prefix, l) for l in ["force", "energy", "virial"]
    ]
    result.properties[force_name] = aseatoms.calc.results["forces"].T
    result.params[energy_name] = energy
    result.params[virial_name] = symmetrize(*aseatoms.calc.results["stress"])

    assert result.n == natoms

    return result
Ejemplo n.º 5
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)