def test_spacegroup_init_valid(): international_number = 1 hermann_mauguin = "P 1" # init from number assert Spacegroup(international_number).hermann_mauguin == hermann_mauguin # init from symbol assert Spacegroup(hermann_mauguin).international_number == international_number
def test_spacegroup_to_from_json(): spg = Spacegroup(1) json_data = spg.to_json() new_spg = Spacegroup.from_json(json_data) assert new_spg.bravais_lattice == spg.bravais_lattice assert new_spg.international_number == spg.international_number assert new_spg.hermann_mauguin == spg.hermann_mauguin assert new_spg.genpos == spg.genpos
def test_spacegroup_equality(): international_number = 1 hermann_mauguin = "P 1" # valid equality assert Spacegroup(international_number) == Spacegroup(hermann_mauguin) # valid inequality assert Spacegroup(international_number) != Spacegroup(international_number + 1) # invalid comparison assert Spacegroup(international_number) != international_number
def test_spacegroup_init_invalid(): international_number = 231 hermann_mauguin = "x" # init from number with pytest.raises(ValueError): _ = Spacegroup(international_number) # init from symbol with pytest.raises(ValueError): _ = Spacegroup(hermann_mauguin) # init from invalid type with pytest.raises(TypeError): _ = Spacegroup([])
def test_transform_supercell(): # 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 crystal unit_cell = UnitCell(basis, lattparams, spg) crystal = Crystal(unit_cell) target_vectors = np.array([ [2.85, 0.0, 0.0], [0.0, 2.85, 0.0], [0.0, 0.0, 2.85], ]) # generate supercell transform supercell_size = (1, 2, 3) transform = Transform().supercell(supercell_size) # apply the transform crystal = transform.apply(crystal) assert len(crystal.atoms) == 12 assert np.allclose(crystal.lattice_vectors.vectors, target_vectors * np.array(supercell_size), atol=1E-6) # reapply the transform transform.apply(crystal) assert len(crystal.atoms) == 72 assert np.allclose(crystal.lattice_vectors.vectors, target_vectors * np.array(supercell_size)**2, atol=1E-6)
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_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_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_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
def get_cubic_unit_cell(): basis = Basis.primitive("X") lattparams = LatticeParameters.cubic(10) spg = Spacegroup(225) return UnitCell(basis, lattparams, spg)