def test_as_dict(self): si = Specie("Si", 4) mn = Element("Mn") coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, [{si: 0.5, mn: 0.5}, {si: 0.5}], coords) self.assertIn("lattice", struct.as_dict()) self.assertIn("sites", struct.as_dict()) d = self.propertied_structure.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{Specie('O', -2, properties={"spin": 3}): 1.0}, {Specie('Mg', 2, properties={"spin": 2}): 0.8}], coords, site_properties={'magmom': [5, -5]}) d = s.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) self.assertEqual(d['sites'][0]['species'][0]['properties']['spin'], 3) d = s.as_dict(0) self.assertNotIn("volume", d['lattice']) self.assertNotIn("xyz", d['sites'][0])
def test_as_dict(self): si = Specie("Si", 4) mn = Element("Mn") coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, [{ si: 0.5, mn: 0.5 }, { si: 0.5 }], coords) self.assertIn("lattice", struct.as_dict()) self.assertIn("sites", struct.as_dict()) d = self.propertied_structure.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{ Specie('O', -2, properties={"spin": 3}): 1.0 }, { Specie('Mg', 2, properties={"spin": 2}): 0.8 }], coords, site_properties={'magmom': [5, -5]}) d = s.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) self.assertEqual(d['sites'][0]['species'][0]['properties']['spin'], 3) d = s.as_dict(0) self.assertNotIn("volume", d['lattice']) self.assertNotIn("xyz", d['sites'][0])
class IStructureTest(PymatgenTest): def setUp(self): coords = [[0, 0, 0], [0.75, 0.5, 0.75]] self.lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) self.struct = IStructure(self.lattice, ["Si"] * 2, coords) self.assertEqual(len(self.struct), 2, "Wrong number of sites in structure!") self.assertTrue(self.struct.is_ordered) self.assertTrue(self.struct.ntypesp == 1) coords = list() coords.append([0, 0, 0]) coords.append([0., 0, 0.0000001]) self.assertRaises(StructureError, IStructure, self.lattice, ["Si"] * 2, coords, True) self.propertied_structure = IStructure( self.lattice, ["Si"] * 2, coords, site_properties={'magmom': [5, -5]}) def test_matches(self): ss = self.struct * 2 self.assertTrue(ss.matches(self.struct)) def test_bad_structure(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) coords.append([0.75, 0.5, 0.75]) self.assertRaises(StructureError, IStructure, self.lattice, ["Si"] * 3, coords, validate_proximity=True) #these shouldn't raise an error IStructure(self.lattice, ["Si"] * 2, coords[:2], True) IStructure(self.lattice, ["Si"], coords[:1], True) def test_volume_and_density(self): self.assertAlmostEqual(self.struct.volume, 40.04, 2, "Volume wrong!") self.assertAlmostEqual(self.struct.density, 2.33, 2, "Incorrect density") def test_specie_init(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{Specie('O', -2): 1.0}, {Specie('Mg', 2): 0.8}], coords) self.assertEqual(s.composition.formula, 'Mg0.8 O1') def test_get_sorted_structure(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, ["O", "Li"], coords, site_properties={'charge': [-2, 1]}) sorted_s = s.get_sorted_structure() self.assertEqual(sorted_s[0].species_and_occu, Composition("Li")) self.assertEqual(sorted_s[1].species_and_occu, Composition("O")) self.assertEqual(sorted_s[0].charge, 1) self.assertEqual(sorted_s[1].charge, -2) s = IStructure(self.lattice, ["Se", "C", "Se", "C"], [[0] * 3, [0.5] * 3, [0.25] * 3, [0.75] * 3]) self.assertEqual([site.specie.symbol for site in s.get_sorted_structure()], ["C", "C", "Se", "Se"]) def test_get_spacegroup_data(self): self.assertEqual(self.struct.get_spacegroup_info(), ('Fd-3m', 227)) def test_fractional_occupations(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{'O': 1.0}, {'Mg': 0.8}], coords) self.assertEqual(s.composition.formula, 'Mg0.8 O1') self.assertFalse(s.is_ordered) def test_get_distance(self): self.assertAlmostEqual(self.struct.get_distance(0, 1), 2.35, 2, "Distance calculated wrongly!") pt = [0.9, 0.9, 0.8] self.assertAlmostEqual(self.struct[0].distance_from_point(pt), 1.50332963784, 2, "Distance calculated wrongly!") def test_as_dict(self): si = Specie("Si", 4) mn = Element("Mn") coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, [{si: 0.5, mn: 0.5}, {si: 0.5}], coords) self.assertIn("lattice", struct.as_dict()) self.assertIn("sites", struct.as_dict()) d = self.propertied_structure.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{Specie('O', -2, properties={"spin": 3}): 1.0}, {Specie('Mg', 2, properties={"spin": 2}): 0.8}], coords, site_properties={'magmom': [5, -5]}) d = s.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) self.assertEqual(d['sites'][0]['species'][0]['properties']['spin'], 3) d = s.as_dict(0) self.assertNotIn("volume", d['lattice']) self.assertNotIn("xyz", d['sites'][0]) def test_from_dict(self): d = self.propertied_structure.as_dict() s = IStructure.from_dict(d) self.assertEqual(s[0].magmom, 5) d = self.propertied_structure.as_dict(0) s2 = IStructure.from_dict(d) self.assertEqual(s, s2) d = {'lattice': {'a': 3.8401979337, 'volume': 40.044794644251596, 'c': 3.8401979337177736, 'b': 3.840198994344244, 'matrix': [[3.8401979337, 0.0, 0.0], [1.9200989668, 3.3257101909, 0.0], [0.0, -2.2171384943, 3.1355090603]], 'alpha': 119.9999908639842, 'beta': 90.0, 'gamma': 60.000009137322195}, 'sites': [{'properties': {'magmom': 5}, 'abc': [0.0, 0.0, 0.0], 'occu': 1.0, 'species': [{'occu': 1.0, 'oxidation_state': -2, 'properties': {'spin': 3}, 'element': 'O'}], 'label': 'O2-', 'xyz': [0.0, 0.0, 0.0]}, {'properties': {'magmom': -5}, 'abc': [0.75, 0.5, 0.75], 'occu': 0.8, 'species': [{'occu': 0.8, 'oxidation_state': 2, 'properties': {'spin': 2}, 'element': 'Mg'}], 'label': 'Mg2+:0.800', 'xyz': [3.8401979336749994, 1.2247250003039056e-06, 2.351631795225]}]} s = IStructure.from_dict(d) self.assertEqual(s[0].magmom, 5) self.assertEqual(s[0].specie.spin, 3) self.assertEqual(type(s), IStructure) def test_site_properties(self): site_props = self.propertied_structure.site_properties self.assertEqual(site_props['magmom'], [5, -5]) self.assertEqual(self.propertied_structure[0].magmom, 5) self.assertEqual(self.propertied_structure[1].magmom, -5) def test_copy(self): new_struct = self.propertied_structure.copy(site_properties={'charge': [2, 3]}) self.assertEqual(new_struct[0].magmom, 5) self.assertEqual(new_struct[1].magmom, -5) self.assertEqual(new_struct[0].charge, 2) self.assertEqual(new_struct[1].charge, 3) coords = list() coords.append([0, 0, 0]) coords.append([0., 0, 0.0000001]) structure = IStructure(self.lattice, ["O", "Si"], coords, site_properties={'magmom': [5, -5]}) new_struct = structure.copy(site_properties={'charge': [2, 3]}, sanitize=True) self.assertEqual(new_struct[0].magmom, -5) self.assertEqual(new_struct[1].magmom, 5) self.assertEqual(new_struct[0].charge, 3) self.assertEqual(new_struct[1].charge, 2) self.assertAlmostEqual(new_struct.volume, structure.volume) def test_interpolate(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, ["Si"] * 2, coords) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) struct2 = IStructure(self.struct.lattice, ["Si"] * 2, coords2) int_s = struct.interpolate(struct2, 10) for s in int_s: self.assertIsNotNone(s, "Interpolation Failed!") self.assertEqual(int_s[0].lattice, s.lattice) self.assertArrayEqual(int_s[1][1].frac_coords, [0.725, 0.5, 0.725]) badlattice = [[1, 0.00, 0.00], [0, 1, 0.00], [0.00, 0, 1]] struct2 = IStructure(badlattice, ["Si"] * 2, coords2) self.assertRaises(ValueError, struct.interpolate, struct2) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) struct2 = IStructure(self.struct.lattice, ["Si", "Fe"], coords2) self.assertRaises(ValueError, struct.interpolate, struct2) # Test autosort feature. s1 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s1.pop(0) s2 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2.pop(2) random.shuffle(s2) for s in s1.interpolate(s2, autosort_tol=0.5): self.assertArrayAlmostEqual(s1[0].frac_coords, s[0].frac_coords) self.assertArrayAlmostEqual(s1[2].frac_coords, s[2].frac_coords) # Make sure autosort has no effect on simpler interpolations, # and with shuffled sites. s1 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2[0] = "Fe", [0.01, 0.01, 0.01] random.shuffle(s2) for s in s1.interpolate(s2, autosort_tol=0.5): self.assertArrayAlmostEqual(s1[1].frac_coords, s[1].frac_coords) self.assertArrayAlmostEqual(s1[2].frac_coords, s[2].frac_coords) self.assertArrayAlmostEqual(s1[3].frac_coords, s[3].frac_coords) def test_interpolate_lattice(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, ["Si"] * 2, coords) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) l2 = Lattice.from_lengths_and_angles([3,4,4], [100,100,70]) struct2 = IStructure(l2, ["Si"] * 2, coords2) int_s = struct.interpolate(struct2, 2, interpolate_lattices=True) self.assertArrayAlmostEqual(struct.lattice.abc, int_s[0].lattice.abc) self.assertArrayAlmostEqual(struct.lattice.angles, int_s[0].lattice.angles) self.assertArrayAlmostEqual(struct2.lattice.abc, int_s[2].lattice.abc) self.assertArrayAlmostEqual(struct2.lattice.angles, int_s[2].lattice.angles) int_angles = [(a + struct2.lattice.angles[i]) / 2 for i, a in enumerate(struct.lattice.angles)] self.assertArrayAlmostEqual(int_angles, int_s[1].lattice.angles) def test_get_primitive_structure(self): coords = [[0, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0.5], [0.5, 0, 0.5]] fcc_ag = IStructure(Lattice.cubic(4.09), ["Ag"] * 4, coords) self.assertEqual(len(fcc_ag.get_primitive_structure()), 1) coords = [[0, 0, 0], [0.5, 0.5, 0.5]] bcc_li = IStructure(Lattice.cubic(4.09), ["Li"] * 2, coords) bcc_prim = bcc_li.get_primitive_structure() self.assertEqual(len(bcc_prim), 1) self.assertAlmostEqual(bcc_prim.lattice.alpha, 109.47122, 3) coords = [[0] * 3, [0.5] * 3, [0.25] * 3, [0.26] * 3] s = IStructure(Lattice.cubic(4.09), ["Ag"] * 4, coords) self.assertEqual(len(s.get_primitive_structure()), 4) def test_primitive_cell_site_merging(self): l = Lattice.cubic(10) coords = [[0, 0, 0], [0, 0, 0.5], [0, 0, 0.26], [0, 0, 0.74]] sp = ['Ag', 'Ag', 'Be', 'Be'] s = Structure(l, sp, coords) dm = s.get_primitive_structure().distance_matrix self.assertArrayAlmostEqual(dm, [[0, 2.5], [2.5, 0]]) def test_primitive_on_large_supercell(self): coords = [[0, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0.5], [0.5, 0, 0.5]] fcc_ag = Structure(Lattice.cubic(4.09), ["Ag"] * 4, coords) fcc_ag.make_supercell([2, 2, 2]) fcc_ag_prim = fcc_ag.get_primitive_structure() self.assertEqual(len(fcc_ag_prim), 1) self.assertAlmostEqual(fcc_ag_prim.volume, 17.10448225) def test_primitive_positions(self): coords = [[0, 0, 0], [0.3, 0.35, 0.45]] s = Structure(Lattice.from_parameters(1,2,3,50,66,88), ["Ag"] * 2, coords) a = [[-1,2,-3], [3,2,-4], [1,0,-1]] b = [[4, 0, 0], [1, 1, 0], [3, 0, 1]] c = [[2, 0, 0], [1, 3, 0], [1, 1, 1]] for sc_matrix in [c]: sc = s.copy() sc.make_supercell(sc_matrix) prim = sc.get_primitive_structure(0.01) self.assertEqual(len(prim), 2) self.assertAlmostEqual(prim.distance_matrix[0,1], 1.0203432356739286) def test_primitive_structure_volume_check(self): l = Lattice.tetragonal(10, 30) coords = [[0.5, 0.8, 0], [0.5, 0.2, 0], [0.5, 0.8, 0.333], [0.5, 0.5, 0.333], [0.5, 0.5, 0.666], [0.5, 0.2, 0.666]] s = IStructure(l, ["Ag"] * 6, coords) sprim = s.get_primitive_structure(tolerance=0.1) self.assertEqual(len(sprim), 6) def test_get_all_neighbors_and_get_neighbors(self): s = self.struct nn = s.get_neighbors_in_shell(s[0].frac_coords, 2, 4, include_index=True) self.assertEqual(len(nn), 47) self.assertEqual(nn[0][-1], 0) r = random.uniform(3, 6) all_nn = s.get_all_neighbors(r, True) for i in range(len(s)): self.assertEqual(len(all_nn[i]), len(s.get_neighbors(s[i], r))) for site, nns in zip(s, all_nn): for nn in nns: self.assertTrue(nn[0].is_periodic_image(s[nn[2]])) d = sum((site.coords - nn[0].coords) ** 2) ** 0.5 self.assertAlmostEqual(d, nn[1]) s = Structure(Lattice.cubic(1), ['Li'], [[0,0,0]]) s.make_supercell([2,2,2]) self.assertEqual(sum(map(len, s.get_all_neighbors(3))), 976) def test_get_all_neighbors_outside_cell(self): s = Structure(Lattice.cubic(2), ['Li', 'Li', 'Li', 'Si'], [[3.1] * 3, [0.11] * 3, [-1.91] * 3, [0.5] * 3]) all_nn = s.get_all_neighbors(0.2, True) for site, nns in zip(s, all_nn): for nn in nns: self.assertTrue(nn[0].is_periodic_image(s[nn[2]])) d = sum((site.coords - nn[0].coords) ** 2) ** 0.5 self.assertAlmostEqual(d, nn[1]) self.assertEqual(list(map(len, all_nn)), [2, 2, 2, 0]) def test_get_dist_matrix(self): ans = [[0., 2.3516318], [2.3516318, 0.]] self.assertArrayAlmostEqual(self.struct.distance_matrix, ans) def test_to_from_file_string(self): for fmt in ["cif", "json", "poscar", "cssr"]: s = self.struct.to(fmt=fmt) self.assertIsNotNone(s) ss = IStructure.from_str(s, fmt=fmt) self.assertArrayAlmostEqual( ss.lattice.lengths_and_angles, self.struct.lattice.lengths_and_angles, decimal=5) self.assertArrayAlmostEqual(ss.frac_coords, self.struct.frac_coords) self.assertIsInstance(ss, IStructure) self.struct.to(filename="POSCAR.testing") self.assertTrue(os.path.exists("POSCAR.testing")) os.remove("POSCAR.testing") self.struct.to(filename="Si_testing.yaml") self.assertTrue(os.path.exists("Si_testing.yaml")) s = Structure.from_file("Si_testing.yaml") self.assertEqual(s, self.struct) os.remove("Si_testing.yaml")
class IStructureTest(PymatgenTest): def setUp(self): coords = [[0, 0, 0], [0.75, 0.5, 0.75]] self.lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) self.struct = IStructure(self.lattice, ["Si"] * 2, coords) self.assertEqual(len(self.struct), 2, "Wrong number of sites in structure!") self.assertTrue(self.struct.is_ordered) self.assertTrue(self.struct.ntypesp == 1) coords = list() coords.append([0, 0, 0]) coords.append([0., 0, 0.0000001]) self.assertRaises(StructureError, IStructure, self.lattice, ["Si"] * 2, coords, True) self.propertied_structure = IStructure( self.lattice, ["Si"] * 2, coords, site_properties={'magmom': [5, -5]}) def test_matches(self): ss = self.struct * 2 self.assertTrue(ss.matches(self.struct)) def test_bad_structure(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) coords.append([0.75, 0.5, 0.75]) self.assertRaises(StructureError, IStructure, self.lattice, ["Si"] * 3, coords, validate_proximity=True) # these shouldn't raise an error IStructure(self.lattice, ["Si"] * 2, coords[:2], True) IStructure(self.lattice, ["Si"], coords[:1], True) def test_volume_and_density(self): self.assertAlmostEqual(self.struct.volume, 40.04, 2, "Volume wrong!") self.assertAlmostEqual(self.struct.density, 2.33, 2, "Incorrect density") def test_specie_init(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{ Specie('O', -2): 1.0 }, { Specie('Mg', 2): 0.8 }], coords) self.assertEqual(s.composition.formula, 'Mg0.8 O1') def test_get_sorted_structure(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, ["O", "Li"], coords, site_properties={'charge': [-2, 1]}) sorted_s = s.get_sorted_structure() self.assertEqual(sorted_s[0].species_and_occu, Composition("Li")) self.assertEqual(sorted_s[1].species_and_occu, Composition("O")) self.assertEqual(sorted_s[0].charge, 1) self.assertEqual(sorted_s[1].charge, -2) s = IStructure(self.lattice, ["Se", "C", "Se", "C"], [[0] * 3, [0.5] * 3, [0.25] * 3, [0.75] * 3]) self.assertEqual( [site.specie.symbol for site in s.get_sorted_structure()], ["C", "C", "Se", "Se"]) def test_get_space_group_data(self): self.assertEqual(self.struct.get_space_group_info(), ('Fd-3m', 227)) def test_fractional_occupations(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{'O': 1.0}, {'Mg': 0.8}], coords) self.assertEqual(s.composition.formula, 'Mg0.8 O1') self.assertFalse(s.is_ordered) def test_get_distance(self): self.assertAlmostEqual(self.struct.get_distance(0, 1), 2.35, 2, "Distance calculated wrongly!") pt = [0.9, 0.9, 0.8] self.assertAlmostEqual(self.struct[0].distance_from_point(pt), 1.50332963784, 2, "Distance calculated wrongly!") def test_as_dict(self): si = Specie("Si", 4) mn = Element("Mn") coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, [{ si: 0.5, mn: 0.5 }, { si: 0.5 }], coords) self.assertIn("lattice", struct.as_dict()) self.assertIn("sites", struct.as_dict()) d = self.propertied_structure.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) s = IStructure(self.lattice, [{ Specie('O', -2, properties={"spin": 3}): 1.0 }, { Specie('Mg', 2, properties={"spin": 2}): 0.8 }], coords, site_properties={'magmom': [5, -5]}) d = s.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 5) self.assertEqual(d['sites'][0]['species'][0]['properties']['spin'], 3) d = s.as_dict(0) self.assertNotIn("volume", d['lattice']) self.assertNotIn("xyz", d['sites'][0]) def test_from_dict(self): d = self.propertied_structure.as_dict() s = IStructure.from_dict(d) self.assertEqual(s[0].magmom, 5) d = self.propertied_structure.as_dict(0) s2 = IStructure.from_dict(d) self.assertEqual(s, s2) d = { 'lattice': { 'a': 3.8401979337, 'volume': 40.044794644251596, 'c': 3.8401979337177736, 'b': 3.840198994344244, 'matrix': [[3.8401979337, 0.0, 0.0], [1.9200989668, 3.3257101909, 0.0], [0.0, -2.2171384943, 3.1355090603]], 'alpha': 119.9999908639842, 'beta': 90.0, 'gamma': 60.000009137322195 }, 'sites': [{ 'properties': { 'magmom': 5 }, 'abc': [0.0, 0.0, 0.0], 'occu': 1.0, 'species': [{ 'occu': 1.0, 'oxidation_state': -2, 'properties': { 'spin': 3 }, 'element': 'O' }], 'label': 'O2-', 'xyz': [0.0, 0.0, 0.0] }, { 'properties': { 'magmom': -5 }, 'abc': [0.75, 0.5, 0.75], 'occu': 0.8, 'species': [{ 'occu': 0.8, 'oxidation_state': 2, 'properties': { 'spin': 2 }, 'element': 'Mg' }], 'label': 'Mg2+:0.800', 'xyz': [3.8401979336749994, 1.2247250003039056e-06, 2.351631795225] }] } s = IStructure.from_dict(d) self.assertEqual(s[0].magmom, 5) self.assertEqual(s[0].specie.spin, 3) self.assertEqual(type(s), IStructure) def test_site_properties(self): site_props = self.propertied_structure.site_properties self.assertEqual(site_props['magmom'], [5, -5]) self.assertEqual(self.propertied_structure[0].magmom, 5) self.assertEqual(self.propertied_structure[1].magmom, -5) def test_copy(self): new_struct = self.propertied_structure.copy( site_properties={'charge': [2, 3]}) self.assertEqual(new_struct[0].magmom, 5) self.assertEqual(new_struct[1].magmom, -5) self.assertEqual(new_struct[0].charge, 2) self.assertEqual(new_struct[1].charge, 3) coords = list() coords.append([0, 0, 0]) coords.append([0., 0, 0.0000001]) structure = IStructure(self.lattice, ["O", "Si"], coords, site_properties={'magmom': [5, -5]}) new_struct = structure.copy(site_properties={'charge': [2, 3]}, sanitize=True) self.assertEqual(new_struct[0].magmom, -5) self.assertEqual(new_struct[1].magmom, 5) self.assertEqual(new_struct[0].charge, 3) self.assertEqual(new_struct[1].charge, 2) self.assertAlmostEqual(new_struct.volume, structure.volume) def test_interpolate(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, ["Si"] * 2, coords) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) struct2 = IStructure(self.struct.lattice, ["Si"] * 2, coords2) int_s = struct.interpolate(struct2, 10) for s in int_s: self.assertIsNotNone(s, "Interpolation Failed!") self.assertEqual(int_s[0].lattice, s.lattice) self.assertArrayEqual(int_s[1][1].frac_coords, [0.725, 0.5, 0.725]) badlattice = [[1, 0.00, 0.00], [0, 1, 0.00], [0.00, 0, 1]] struct2 = IStructure(badlattice, ["Si"] * 2, coords2) self.assertRaises(ValueError, struct.interpolate, struct2) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) struct2 = IStructure(self.struct.lattice, ["Si", "Fe"], coords2) self.assertRaises(ValueError, struct.interpolate, struct2) # Test autosort feature. s1 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s1.pop(0) s2 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2.pop(2) random.shuffle(s2) for s in s1.interpolate(s2, autosort_tol=0.5): self.assertArrayAlmostEqual(s1[0].frac_coords, s[0].frac_coords) self.assertArrayAlmostEqual(s1[2].frac_coords, s[2].frac_coords) # Make sure autosort has no effect on simpler interpolations, # and with shuffled sites. s1 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) s2[0] = "Fe", [0.01, 0.01, 0.01] random.shuffle(s2) for s in s1.interpolate(s2, autosort_tol=0.5): self.assertArrayAlmostEqual(s1[1].frac_coords, s[1].frac_coords) self.assertArrayAlmostEqual(s1[2].frac_coords, s[2].frac_coords) self.assertArrayAlmostEqual(s1[3].frac_coords, s[3].frac_coords) def test_interpolate_lattice(self): coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) struct = IStructure(self.lattice, ["Si"] * 2, coords) coords2 = list() coords2.append([0, 0, 0]) coords2.append([0.5, 0.5, 0.5]) l2 = Lattice.from_lengths_and_angles([3, 4, 4], [100, 100, 70]) struct2 = IStructure(l2, ["Si"] * 2, coords2) int_s = struct.interpolate(struct2, 2, interpolate_lattices=True) self.assertArrayAlmostEqual(struct.lattice.abc, int_s[0].lattice.abc) self.assertArrayAlmostEqual(struct.lattice.angles, int_s[0].lattice.angles) self.assertArrayAlmostEqual(struct2.lattice.abc, int_s[2].lattice.abc) self.assertArrayAlmostEqual(struct2.lattice.angles, int_s[2].lattice.angles) int_angles = [110.3976469, 94.5359731, 64.5165856] self.assertArrayAlmostEqual(int_angles, int_s[1].lattice.angles) # Assert that volume is monotonic self.assertTrue(struct2.lattice.volume >= int_s[1].lattice.volume) self.assertTrue(int_s[1].lattice.volume >= struct.lattice.volume) def test_interpolate_lattice_rotation(self): l1 = Lattice([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) l2 = Lattice([[-1.01, 0, 0], [0, -1.01, 0], [0, 0, 1]]) coords = [[0, 0, 0], [0.75, 0.5, 0.75]] struct1 = IStructure(l1, ["Si"] * 2, coords) struct2 = IStructure(l2, ["Si"] * 2, coords) int_s = struct1.interpolate(struct2, 2, interpolate_lattices=True) # Assert that volume is monotonic self.assertTrue(struct2.lattice.volume >= int_s[1].lattice.volume) self.assertTrue(int_s[1].lattice.volume >= struct1.lattice.volume) def test_get_primitive_structure(self): coords = [[0, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0.5], [0.5, 0, 0.5]] fcc_ag = IStructure(Lattice.cubic(4.09), ["Ag"] * 4, coords) self.assertEqual(len(fcc_ag.get_primitive_structure()), 1) coords = [[0, 0, 0], [0.5, 0.5, 0.5]] bcc_li = IStructure(Lattice.cubic(4.09), ["Li"] * 2, coords) bcc_prim = bcc_li.get_primitive_structure() self.assertEqual(len(bcc_prim), 1) self.assertAlmostEqual(bcc_prim.lattice.alpha, 109.47122, 3) coords = [[0] * 3, [0.5] * 3, [0.25] * 3, [0.26] * 3] s = IStructure(Lattice.cubic(4.09), ["Ag"] * 4, coords) self.assertEqual(len(s.get_primitive_structure()), 4) def test_primitive_cell_site_merging(self): l = Lattice.cubic(10) coords = [[0, 0, 0], [0, 0, 0.5], [0, 0, 0.26], [0, 0, 0.74]] sp = ['Ag', 'Ag', 'Be', 'Be'] s = Structure(l, sp, coords) dm = s.get_primitive_structure().distance_matrix self.assertArrayAlmostEqual(dm, [[0, 2.5], [2.5, 0]]) def test_primitive_on_large_supercell(self): coords = [[0, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0.5], [0.5, 0, 0.5]] fcc_ag = Structure(Lattice.cubic(4.09), ["Ag"] * 4, coords) fcc_ag.make_supercell([2, 2, 2]) fcc_ag_prim = fcc_ag.get_primitive_structure() self.assertEqual(len(fcc_ag_prim), 1) self.assertAlmostEqual(fcc_ag_prim.volume, 17.10448225) def test_primitive_positions(self): coords = [[0, 0, 0], [0.3, 0.35, 0.45]] s = Structure(Lattice.from_parameters(1, 2, 3, 50, 66, 88), ["Ag"] * 2, coords) a = [[-1, 2, -3], [3, 2, -4], [1, 0, -1]] b = [[4, 0, 0], [1, 1, 0], [3, 0, 1]] c = [[2, 0, 0], [1, 3, 0], [1, 1, 1]] for sc_matrix in [c]: sc = s.copy() sc.make_supercell(sc_matrix) prim = sc.get_primitive_structure(0.01) self.assertEqual(len(prim), 2) self.assertAlmostEqual(prim.distance_matrix[0, 1], 1.0203432356739286) def test_primitive_structure_volume_check(self): l = Lattice.tetragonal(10, 30) coords = [[0.5, 0.8, 0], [0.5, 0.2, 0], [0.5, 0.8, 0.333], [0.5, 0.5, 0.333], [0.5, 0.5, 0.666], [0.5, 0.2, 0.666]] s = IStructure(l, ["Ag"] * 6, coords) sprim = s.get_primitive_structure(tolerance=0.1) self.assertEqual(len(sprim), 6) def test_get_all_neighbors_and_get_neighbors(self): s = self.struct nn = s.get_neighbors_in_shell(s[0].frac_coords, 2, 4, include_index=True) self.assertEqual(len(nn), 47) self.assertEqual(nn[0][-1], 0) r = random.uniform(3, 6) all_nn = s.get_all_neighbors(r, True) for i in range(len(s)): self.assertEqual(len(all_nn[i]), len(s.get_neighbors(s[i], r))) for site, nns in zip(s, all_nn): for nn in nns: self.assertTrue(nn[0].is_periodic_image(s[nn[2]])) d = sum((site.coords - nn[0].coords)**2)**0.5 self.assertAlmostEqual(d, nn[1]) s = Structure(Lattice.cubic(1), ['Li'], [[0, 0, 0]]) s.make_supercell([2, 2, 2]) self.assertEqual(sum(map(len, s.get_all_neighbors(3))), 976) def test_get_all_neighbors_outside_cell(self): s = Structure(Lattice.cubic(2), ['Li', 'Li', 'Li', 'Si'], [[3.1] * 3, [0.11] * 3, [-1.91] * 3, [0.5] * 3]) all_nn = s.get_all_neighbors(0.2, True) for site, nns in zip(s, all_nn): for nn in nns: self.assertTrue(nn[0].is_periodic_image(s[nn[2]])) d = sum((site.coords - nn[0].coords)**2)**0.5 self.assertAlmostEqual(d, nn[1]) self.assertEqual(list(map(len, all_nn)), [2, 2, 2, 0]) def test_get_dist_matrix(self): ans = [[0., 2.3516318], [2.3516318, 0.]] self.assertArrayAlmostEqual(self.struct.distance_matrix, ans) def test_to_from_file_string(self): for fmt in ["cif", "json", "poscar", "cssr"]: s = self.struct.to(fmt=fmt) self.assertIsNotNone(s) ss = IStructure.from_str(s, fmt=fmt) self.assertArrayAlmostEqual(ss.lattice.lengths_and_angles, self.struct.lattice.lengths_and_angles, decimal=5) self.assertArrayAlmostEqual(ss.frac_coords, self.struct.frac_coords) self.assertIsInstance(ss, IStructure) self.struct.to(filename="POSCAR.testing") self.assertTrue(os.path.exists("POSCAR.testing")) os.remove("POSCAR.testing") self.struct.to(filename="Si_testing.yaml") self.assertTrue(os.path.exists("Si_testing.yaml")) s = Structure.from_file("Si_testing.yaml") self.assertEqual(s, self.struct) os.remove("Si_testing.yaml") self.struct.to(filename="POSCAR.testing.gz") s = Structure.from_file("POSCAR.testing.gz") self.assertEqual(s, self.struct) os.remove("POSCAR.testing.gz")