def test_AtomsList_empty_io(tmpdir): from matdb.atoms import Atoms, AtomsList from os import path target = str(tmpdir.join("empty_AtomsList")) globals_setup(target) if not path.isdir(target): mkdir(target) empty_list = AtomsList([]) empty_list.write(path.join(target,"temp.h5")) assert len(empty_list) == 0 assert path.isfile(path.join(target,"temp.h5")) aR = AtomsList() aR.read(path.join(target,"temp.h5")) assert len(aR) == 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)])