def test_basis_to_from_json(): basis = Basis.primitive("X") json_data = basis.to_json() res = Basis.from_json(json_data) assert len(basis) == len(res) == 1 assert res[0][0] == basis[0][0] assert np.array_equal(res[0][1], basis[0][1])
def test_basis_init_not_fractional(): specie = "Fe" positive_site = np.array([1.5, 0, 0]) negative_site = np.array([-1.5, 0, 0]) # test a positive value with pytest.raises(ValueError): _ = Basis([(specie, positive_site)]) # test a negative value with pytest.raises(ValueError): _ = Basis([(specie, negative_site)])
def from_json(cls, s: str) -> 'UnitCell': """Initializes from a JSON string.""" # load dict from JSON string data = orjson.loads(s) # validate type _type = data.pop("type") if _type != cls.__name__: raise TypeError(f"cannot deserialize from type `{_type}`") # process topology topology = Topology.from_json(orjson.dumps(data["topology"])) # process basis basis = Basis.from_json(orjson.dumps(data["basis"])) # process lattice parameters lattice_parameters = LatticeParameters.from_json( orjson.dumps(data["lattice_parameters"])) # process spacegroup spacegroup = Spacegroup.from_json(orjson.dumps(data["spacegroup"])) # return instance return cls(basis, lattice_parameters, spacegroup, _graph=topology._graph)
def test_basis_apply_spacegroup_complex_monoclinic(): # complex basis of arbitrary species basis = Basis([ ("X", np.array([0.0, 0.0, 0.0])), ("Y", np.array([0.1, 0.2, 0.3])), ]) # Monoclinic spacegroup from international number spg = Spacegroup(3) res = basis.apply_spacegroup(spg) assert len(res) == 3 target = [ ("X", np.array([0.0, 0.0, 0.0])), ("Y", np.array([0.1, 0.2, 0.3])), ("Y", np.array([0.9, 0.2, 0.7])), ] for (target_specie, target_site), (res_specie, res_site) in zip(target, res): assert target_specie == res_specie assert np.allclose(target_site, res_site)
def test_crystal_to_from_json(): basis = Basis.primitive("Fe") lattparams = LatticeParameters.cubic(2.85) spg = Spacegroup("I m -3 m") unit_cell = UnitCell(basis, lattparams, spg) crystal = Crystal(unit_cell) json_data = crystal.to_json() res = Crystal.from_json(json_data) assert np.array_equal( res.lattice_vectors.vectors, crystal.lattice_vectors.vectors, ) assert len(res.atoms) == len(crystal.atoms) == 2
def test_unit_cell_hexagonal(): # primitive basis of arbitrary species basis = Basis.primitive("X") # cubic lattice parameters lattparams = LatticeParameters.hexagonal(4.0, 2.5) # hexagonal spacegroup spg = Spacegroup("P 6/m c c") # build the unit cell unit_cell = UnitCell(basis, lattparams, spg) assert len(unit_cell.atoms) == 2 target = [ ("X", np.array([0.0, 0.0, 0.0])), ("X", np.array([0.0, 0.0, 1.25])), ] for i, (specie, site) in enumerate(target): assert unit_cell.atoms[i].specie == specie assert np.allclose(unit_cell.atoms[i].position, site)
def test_unit_cell_cubic(): # primitive basis of iron basis = Basis.primitive("Fe") # cubic lattice parameters lattparams = LatticeParameters.cubic(2.85) # BCC spacegroup spg = Spacegroup("I m -3 m") # build the unit cell unit_cell = UnitCell(basis, lattparams, spg) assert len(unit_cell.atoms) == 2 target = [ ("Fe", np.array([0.0, 0.0, 0.0])), ("Fe", np.array([1.425, 1.425, 1.425])), ] for i, (specie, site) in enumerate(target): assert unit_cell.atoms[i].specie == specie assert np.allclose(unit_cell.atoms[i].position, site)
def test_basis_apply_spacegroup_primitive_cubic(): # primitive basis of aluminum basis = Basis.primitive("Al") # FCC spacegroup from Hermann Mauguin symbol spg = Spacegroup("F m -3 m") res = basis.apply_spacegroup(spg) assert len(res) == 4 target = [ ("Al", np.array([0.0, 0.0, 0.0])), ("Al", np.array([0.0, 0.5, 0.5])), ("Al", np.array([0.5, 0.0, 0.5])), ("Al", np.array([0.5, 0.5, 0.0])), ] for (target_specie, target_site), (res_specie, res_site) in zip(target, res): assert target_specie == res_specie assert np.allclose(target_site, res_site)
def test_unit_cell_to_from_json(): basis = Basis.primitive("X") params = LatticeParameters.cubic(10) spg = Spacegroup("F m -3 m") unit_cell = UnitCell(basis, params, spg) json_data = unit_cell.to_json() res = UnitCell.from_json(json_data) # test basis assert res.basis[0][0] == unit_cell.basis[0][0] assert np.array_equal(res.basis[0][1], unit_cell.basis[0][1]) # test lattice parameters assert res.lattice_parameters.a == unit_cell.lattice_parameters.a assert res.lattice_parameters.alpha == unit_cell.lattice_parameters.alpha # test spacegroup assert res.spacegroup == unit_cell.spacegroup # test atoms/bonds assert len(res.atoms) == len(unit_cell.atoms) == 4 assert len(res.bonds) == len(unit_cell.bonds) == 0 assert np.array_equal( res.atoms[0].position, unit_cell.atoms[0].position, ) assert res.atoms[0].specie == unit_cell.atoms[0].specie