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 = Structure(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 = Structure(Lattice.cubic(4.09), ["Li"] * 2, coords) self.assertEqual(len(bcc_li.get_primitive_structure()), 1)
def test_get_xrd_data(self): a = 4.209 latt = Lattice.cubic(a) structure = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) c = XRDCalculator() data = c.get_xrd_data(structure, two_theta_range=(0, 90)) #Check the first two peaks self.assertAlmostEqual(data[0][0], 21.107738329639844) self.assertAlmostEqual(data[0][1], 36.483184003748946) self.assertEqual(data[0][2], {(1, 0, 0): 6}) self.assertAlmostEqual(data[0][3], 4.2089999999999996) self.assertAlmostEqual(data[1][0], 30.024695921112777) self.assertAlmostEqual(data[1][1], 100) self.assertEqual(data[1][2], {(1, 1, 0): 12}) self.assertAlmostEqual(data[1][3], 2.976212442014178) s = read_structure(os.path.join(test_dir, "LiFePO4.cif")) data = c.get_xrd_data(s, two_theta_range=(0, 90)) self.assertAlmostEqual(data[1][0], 17.03504233621785) self.assertAlmostEqual(data[1][1], 50.400928948337075) s = read_structure(os.path.join(test_dir, "Li10GeP2S12.cif")) data = c.get_xrd_data(s, two_theta_range=(0, 90)) self.assertAlmostEqual(data[1][0], 14.058274883353876) self.assertAlmostEqual(data[1][1], 4.4111123641667671) # Test a hexagonal structure. s = read_structure(os.path.join(test_dir, "Graphite.cif"), primitive=False) data = c.get_xrd_data(s, two_theta_range=(0, 90)) self.assertAlmostEqual(data[0][0], 7.929279053132362) self.assertAlmostEqual(data[0][1], 100) self.assertAlmostEqual(len(list(data[0][2].keys())[0]), 4) #Add test case with different lengths of coefficients. #Also test d_hkl. coords = [[0.25, 0.25, 0.173], [0.75, 0.75, 0.827], [0.75, 0.25, 0], [0.25, 0.75, 0], [0.25, 0.25, 0.676], [0.75, 0.75, 0.324]] sp = ["Si", "Si", "Ru", "Ru", "Pr", "Pr"] s = Structure(Lattice.tetragonal(4.192, 6.88), sp, coords) data = c.get_xrd_data(s) self.assertAlmostEqual(data[0][0], 12.86727341476735) self.assertAlmostEqual(data[0][1], 31.448239816769796) self.assertAlmostEqual(data[0][3], 6.88) self.assertEqual(len(data), 42) data = c.get_xrd_data(s, two_theta_range=[0, 60]) self.assertEqual(len(data), 18) #Test with and without Debye-Waller factor tungsten = Structure(Lattice.cubic(3.1653), ["W"] * 2, [[0, 0, 0], [0.5, 0.5, 0.5]]) data = c.get_xrd_data(tungsten, scaled=False) self.assertAlmostEqual(data[0][0], 40.294828554672264) self.assertAlmostEqual(data[0][1], 2414237.5633093244) self.assertAlmostEqual(data[0][3], 2.2382050944897789) c = XRDCalculator(debye_waller_factors={"W": 0.1526}) data = c.get_xrd_data(tungsten, scaled=False) self.assertAlmostEqual(data[0][0], 40.294828554672264) self.assertAlmostEqual(data[0][1], 2377745.2296686019) self.assertAlmostEqual(data[0][3], 2.2382050944897789)
def test_get_slabs(self): gen = SlabGenerator(self.get_structure("CsCl"), [0, 0, 1], 10, 10) #Test orthogonality of some internal variables. a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0) self.assertEqual(len(gen.get_slabs()), 1) s = self.get_structure("LiFePO4") gen = SlabGenerator(s, [0, 0, 1], 10, 10) self.assertEqual(len(gen.get_slabs()), 5) self.assertEqual(len(gen.get_slabs(bonds={("P", "O"): 3})), 2) # There are no slabs in LFP that does not break either P-O or Fe-O # bonds for a miller index of [0, 0, 1]. self.assertEqual(len(gen.get_slabs( bonds={("P", "O"): 3, ("Fe", "O"): 3})), 0) #If we allow some broken bonds, there are a few slabs. self.assertEqual(len(gen.get_slabs( bonds={("P", "O"): 3, ("Fe", "O"): 3}, max_broken_bonds=2)), 2) # At this threshold, only the origin and center Li results in # clustering. All other sites are non-clustered. So the of # slabs is of sites in LiFePO4 unit cell - 2 + 1. self.assertEqual(len(gen.get_slabs(tol=1e-4)), 15) LiCoO2=Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False) gen = SlabGenerator(LiCoO2, [0, 0, 1], 10, 10) lco = gen.get_slabs(bonds={("Co", "O"): 3}) self.assertEqual(len(lco), 1) a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0) scc = Structure.from_spacegroup("Pm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) gen = SlabGenerator(scc, [0, 0, 1], 10, 10) slabs = gen.get_slabs() self.assertEqual(len(slabs), 1) gen = SlabGenerator(scc, [1, 1, 1], 10, 10, max_normal_search=1) slabs = gen.get_slabs() self.assertEqual(len(slabs), 1) # Test whether using units of hkl planes instead of Angstroms for # min_slab_size and min_vac_size will give us the same number of atoms natoms = [] for a in [1, 1.4, 2.5, 3.6]: s = Structure.from_spacegroup("Im-3m", Lattice.cubic(a), ["Fe"], [[0,0,0]]) slabgen = SlabGenerator(s, (1,1,1), 10, 10, in_unit_planes=True, max_normal_search=2) natoms.append(len(slabgen.get_slab())) n = natoms[0] for i in natoms: self.assertEqual(n, i)
def test_get_slab(self): s = self.get_structure("LiFePO4") gen = SlabGenerator(s, [0, 0, 1], 10, 10) s = gen.get_slab(0.25) self.assertAlmostEqual(s.lattice.abc[2], 20.820740000000001) fcc = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) gen = SlabGenerator(fcc, [1, 1, 1], 10, 10) slab = gen.get_slab() gen = SlabGenerator(fcc, [1, 1, 1], 10, 10, primitive=False) slab_non_prim = gen.get_slab() self.assertEqual(len(slab), 6) self.assertEqual(len(slab_non_prim), len(slab) * 4) #Some randomized testing of cell vectors for i in range(1, 231): i = random.randint(1, 230) sg = SpaceGroup.from_int_number(i) if sg.crystal_system == "hexagonal" or (sg.crystal_system == \ "trigonal" and sg.symbol.endswith("H")): latt = Lattice.hexagonal(5, 10) else: #Cubic lattice is compatible with all other space groups. latt = Lattice.cubic(5) s = Structure.from_spacegroup(i, latt, ["H"], [[0, 0, 0]]) miller = (0, 0, 0) while miller == (0, 0, 0): miller = (random.randint(0, 6), random.randint(0, 6), random.randint(0, 6)) gen = SlabGenerator(s, miller, 10, 10) a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0)
def setUp(self): zno1 = Structure.from_file(get_path("ZnO-wz.cif"), primitive=False) zno55 = SlabGenerator(zno1, [1, 0, 0], 5, 5, lll_reduce=False, center_slab=False).get_slab() Ti = Structure(Lattice.hexagonal(4.6, 2.82), ["Ti", "Ti", "Ti"], [[0.000000, 0.000000, 0.000000], [0.333333, 0.666667, 0.500000], [0.666667, 0.333333, 0.500000]]) Ag_fcc = Structure(Lattice.cubic(4.06), ["Ag", "Ag", "Ag", "Ag"], [[0.000000, 0.000000, 0.000000], [0.000000, 0.500000, 0.500000], [0.500000, 0.000000, 0.500000], [0.500000, 0.500000, 0.000000]]) laue_groups = ["-1", "2/m", "mmm", "4/m", "4/mmm", "-3", "-3m", "6/m", "6/mmm", "m-3", "m-3m"] self.ti = Ti self.agfcc = Ag_fcc self.zno1 = zno1 self.zno55 = zno55 self.h = Structure(Lattice.cubic(3), ["H"], [[0, 0, 0]]) self.libcc = Structure(Lattice.cubic(3.51004), ["Li", "Li"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.laue_groups = laue_groups
def setUp(self): l = Lattice.cubic(3.51) species = ["Ni"] coords = [[0,0,0]] self.Ni = Structure.from_spacegroup("Fm-3m", l, species, coords) self.Si = Structure.from_spacegroup("Fd-3m", Lattice.cubic(5.430500), ["Si"], [(0, 0, 0.5)])
def setUp(self): zno1 = Structure.from_file(get_path("ZnO-wz.cif"), primitive=False) zno55 = SlabGenerator(zno1, [1, 0, 0], 5, 5, lll_reduce=False, center_slab=False).get_slab() self.zno1 = zno1 self.zno55 = zno55 self.h = Structure(Lattice.cubic(3), ["H"], [[0, 0, 0]]) self.libcc = Structure(Lattice.cubic(3.51004), ["Li", "Li"], [[0, 0, 0], [0.5, 0.5, 0.5]])
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_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 setUp(self): self.cscl = Structure.from_spacegroup( "Pm-3m", Lattice.cubic(4.2), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.Fe = Structure.from_spacegroup(\ "Im-3m", Lattice.cubic(2.82), ["Fe"], [[0, 0, 0]]) self.lifepo4 = self.get_structure("LiFePO4") self.tei = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False) self.LiCoO2 = Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False) self.p1 = Structure(Lattice.from_parameters(3, 4, 5, 31, 43, 50), ["H", "He"], [[0, 0, 0], [0.1, 0.2, 0.3]]) self.graphite = self.get_structure("Graphite")
def test_get_miller_index_from_sites(self): # test on a cubic system m = Lattice.cubic(1) s1 = np.array([0.5, -1.5, 3]) s2 = np.array([0.5, 3., -1.5]) s3 = np.array([2.5, 1.5, -4.]) self.assertEqual(m.get_miller_index_from_coords([s1, s2, s3]), (2, 1, 1)) # test on a hexagonal system m = Lattice([[2.319, -4.01662582, 0.], [2.319, 4.01662582, 0.], [0., 0., 7.252]]) s1 = np.array([2.319, 1.33887527, 6.3455]) s2 = np.array([1.1595, 0.66943764, 4.5325]) s3 = np.array([1.1595, 0.66943764, 0.9065]) hkl = m.get_miller_index_from_coords([s1, s2, s3]) self.assertEqual(hkl, (2, -1, 0)) # test for previous failing structure m = Lattice([10, 0, 0, 0, 10, 0, 0, 0, 10]) sites = [[0.5, 0.8, 0.8], [0.5, 0.4, 0.2], [0.5, 0.3, 0.7]] hkl = m.get_miller_index_from_coords(sites, coords_are_cartesian=False) self.assertEqual(hkl, (1, 0, 0)) # test for more than 3 sites sites = [[0.5, 0.8, 0.8], [0.5, 0.4, 0.2], [0.5, 0.3, 0.7], [0.5, 0.1, 0.2]] hkl = m.get_miller_index_from_coords(sites, coords_are_cartesian=False) self.assertEqual(hkl, (1, 0, 0))
def setUp(self): self.lattice = Lattice.cubic(10.0) self.si = Element("Si") self.site = PeriodicSite("Fe", np.array([0.25, 0.35, 0.45]), self.lattice) self.site2 = PeriodicSite({"Si":0.5}, np.array([0, 0, 0]), self.lattice) self.assertEquals(self.site2.species_and_occu, {Element('Si'): 0.5}, "Inconsistent site created!") self.propertied_site = PeriodicSite(Specie("Fe", 2), [0.25, 0.35, 0.45], self.lattice, properties={'magmom':5.1, 'charge':4.2})
def test_is_compatible(self): cubic = Lattice.cubic(1) hexagonal = Lattice.hexagonal(1, 2) rhom = Lattice.rhombohedral(3, 80) tet = Lattice.tetragonal(1, 2) ortho = Lattice.orthorhombic(1, 2, 3) sg = SpaceGroup("Fm-3m") self.assertTrue(sg.is_compatible(cubic)) self.assertFalse(sg.is_compatible(hexagonal)) sg = SpaceGroup("R-3mH") self.assertFalse(sg.is_compatible(cubic)) self.assertTrue(sg.is_compatible(hexagonal)) sg = SpaceGroup("R-3m") self.assertTrue(sg.is_compatible(cubic)) self.assertTrue(sg.is_compatible(rhom)) self.assertFalse(sg.is_compatible(hexagonal)) sg = SpaceGroup("Pnma") self.assertTrue(sg.is_compatible(cubic)) self.assertTrue(sg.is_compatible(tet)) self.assertTrue(sg.is_compatible(ortho)) self.assertFalse(sg.is_compatible(rhom)) self.assertFalse(sg.is_compatible(hexagonal)) sg = SpaceGroup("P12/c1") self.assertTrue(sg.is_compatible(cubic)) self.assertTrue(sg.is_compatible(tet)) self.assertTrue(sg.is_compatible(ortho)) self.assertFalse(sg.is_compatible(rhom)) self.assertFalse(sg.is_compatible(hexagonal)) sg = SpaceGroup("P-1") self.assertTrue(sg.is_compatible(cubic)) self.assertTrue(sg.is_compatible(tet)) self.assertTrue(sg.is_compatible(ortho)) self.assertTrue(sg.is_compatible(rhom)) self.assertTrue(sg.is_compatible(hexagonal))
def setUp(self): lattice = Lattice.cubic(3.010) frac_coords = [[0.00000, 0.00000, 0.00000], [0.00000, 0.50000, 0.50000], [0.50000, 0.00000, 0.50000], [0.50000, 0.50000, 0.00000], [0.50000, 0.00000, 0.00000], [0.50000, 0.50000, 0.50000], [0.00000, 0.00000, 0.50000], [0.00000, 0.50000, 0.00000]] species = ['Mg', 'Mg', 'Mg', 'Mg', 'O', 'O', 'O', 'O'] self.MgO = Structure(lattice, species, frac_coords) self.MgO.add_oxidation_state_by_element({"Mg": 2, "O": -6}) lattice_Dy = Lattice.hexagonal(3.58, 25.61) frac_coords_Dy = [[0.00000, 0.00000, 0.00000], [0.66667, 0.33333, 0.11133], [0.00000, 0.00000, 0.222], [0.66667, 0.33333, 0.33333], [0.33333, 0.66666, 0.44467], [0.66667, 0.33333, 0.55533], [0.33333, 0.66667, 0.66667], [0.00000, 0.00000, 0.778], [0.33333, 0.66667, 0.88867]] species_Dy = ['Dy', 'Dy', 'Dy', 'Dy', 'Dy', 'Dy', 'Dy', 'Dy', 'Dy'] self.Dy = Structure(lattice_Dy, species_Dy, frac_coords_Dy)
def test_merge_sites(self): species = [{'Ag': 0.5}, {'Cl': 0.25}, {'Cl': 0.1}, {'Ag': 0.5}, {'F': 0.15}, {'F': 0.1}] coords = [[0, 0, 0], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0, 0, 0], [0.5, 0.5, 1.501], [0.5, 0.5, 1.501]] s = Structure(Lattice.cubic(1), species, coords) s.merge_sites(mode="s") self.assertEqual(s[0].specie.symbol, 'Ag') self.assertEqual(s[1].species_and_occu, Composition({'Cl': 0.35, 'F': 0.25})) self.assertArrayAlmostEqual(s[1].frac_coords, [.5, .5, .5005]) # Test for TaS2 with spacegroup 166 in 160 setting. l = Lattice.from_lengths_and_angles([3.374351, 3.374351, 20.308941], [90.000000, 90.000000, 120.000000]) species = ["Ta", "S", "S"] coords = [[0.000000, 0.000000, 0.944333], [0.333333, 0.666667, 0.353424], [0.666667, 0.333333, 0.535243]] tas2 = Structure.from_spacegroup(160, l, species, coords) assert len(tas2) == 13 tas2.merge_sites(mode="d") assert len(tas2) == 9 l = Lattice.from_lengths_and_angles([3.587776, 3.587776, 19.622793], [90.000000, 90.000000, 120.000000]) species = ["Na", "V", "S", "S"] coords = [[0.333333, 0.666667, 0.165000], [0.000000, 0.000000, 0.998333], [0.333333, 0.666667, 0.399394], [0.666667, 0.333333, 0.597273]] navs2 = Structure.from_spacegroup(160, l, species, coords) assert len(navs2) == 18 navs2.merge_sites(mode="d") assert len(navs2) == 12
def test_get_vertices_dist_ang_indices(self): cubic_lattice = Lattice.cubic(10.0) species = ['Cu', 'O', 'O', 'O', 'O', 'O', 'O'] valences = 'undefined' #First fake structure coords = [[5.0, 5.0, 5.0], [6.01, 5.0, 5.0], [5.0, 5.0, 3.96], [4.0, 5.0, 5.0], [5.0, 6.03, 5.0], [5.0, 3.98, 5.0], [5.0, 5.0, 6.05]] fake_structure = Structure(cubic_lattice, species, coords, coords_are_cartesian=True) #First fake structure with a given normalized_distance_tolerance of 0.0100001 detailed_voronoi_container = DetailedVoronoiContainer(structure=fake_structure, valences=valences, normalized_distance_tolerance=0.0100001, isites=[0]) fake_parameter_indices_list = [] for ii in range(2, 5): for jj in range(7, 14): fake_parameter_indices_list.append((ii, jj)) for ii in range(5, 7): for jj in range(10, 14): fake_parameter_indices_list.append((ii, jj)) points = detailed_voronoi_container._get_vertices_dist_ang_indices(fake_parameter_indices_list) self.assertEqual(points[0], (2, 7)) self.assertEqual(points[1], (4, 7)) self.assertEqual(points[2], (4, 10)) self.assertEqual(points[3], (6, 10)) self.assertEqual(points[4], (6, 13)) self.assertEqual(points[5], (2, 13))
def test_kpath_generation(self): triclinic = [1, 2] monoclinic = range(3, 16) orthorhombic = range(16, 75) tetragonal = range(75, 143) rhombohedral = range(143, 168) hexagonal = range(168, 195) cubic = range(195, 231) species = ['K', 'La', 'Ti'] coords = [[.345, 5, .77298], [.1345, 5.1, .77298], [.7, .8, .9]] for i in range(230): sg_num = i + 1 if sg_num in triclinic: lattice = Lattice([[3.0233057319441246,0,0], [0,7.9850357844548681,0], [0,0,8.1136762279561818]]) elif sg_num in monoclinic: lattice = Lattice.monoclinic(2, 9, 1, 99) elif sg_num in orthorhombic: lattice = Lattice.orthorhombic(2, 9, 1) elif sg_num in tetragonal: lattice = Lattice.tetragonal(2, 9) elif sg_num in rhombohedral: lattice = Lattice.hexagonal(2, 95) elif sg_num in hexagonal: lattice = Lattice.hexagonal(2, 9) elif sg_num in cubic: lattice = Lattice.cubic(2) struct = Structure.from_spacegroup(sg_num, lattice, species, coords) kpath = HighSymmKpath(struct) #Throws error if something doesn't work, causing test to fail.
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_normal_search(self): fcc = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) for miller in [(1, 0, 0), (1, 1, 0), (1, 1, 1), (2, 1, 1)]: gen = SlabGenerator(fcc, miller, 10, 10) gen_normal = SlabGenerator(fcc, miller, 10, 10, max_normal_search=max(miller)) slab = gen_normal.get_slab() self.assertAlmostEqual(slab.lattice.alpha, 90) self.assertAlmostEqual(slab.lattice.beta, 90) self.assertGreaterEqual(len(gen_normal.oriented_unit_cell), len(gen.oriented_unit_cell)) graphite = self.get_structure("Graphite") for miller in [(1, 0, 0), (1, 1, 0), (0, 0, 1), (2, 1, 1)]: gen = SlabGenerator(graphite, miller, 10, 10) gen_normal = SlabGenerator(graphite, miller, 10, 10, max_normal_search=max(miller)) self.assertGreaterEqual(len(gen_normal.oriented_unit_cell), len(gen.oriented_unit_cell)) sc = Structure(Lattice.hexagonal(3.32, 5.15), ["Sc", "Sc"], [[1/3, 2/3, 0.25], [2/3, 1/3, 0.75]]) gen = SlabGenerator(sc, (1, 1, 1), 10, 10, max_normal_search=1) self.assertAlmostEqual(gen.oriented_unit_cell.lattice.angles[1], 90)
def setUp(self): l = Lattice.cubic(3.51) species = ["Ni"] coords = [[0, 0, 0]] self.Ni = Structure.from_spacegroup("Fm-3m", l, species, coords) l = Lattice.cubic(2.819000) species = ["Fe"] coords = [[0, 0, 0]] self.Fe = Structure.from_spacegroup("Im-3m", l, species, coords) self.Si = Structure.from_spacegroup("Fd-3m", Lattice.cubic(5.430500), ["Si"], [(0, 0, 0.5)]) with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "reconstructions_archive.json")) as data_file: self.rec_archive = json.load(data_file)
def test_get_pattern(self): s = self.get_structure("CsCl") c = XRDCalculator() xrd = c.get_pattern(s, two_theta_range=(0, 90)) self.assertTrue(xrd.to_json()) # Test MSONAble property # Check the first two peaks self.assertAlmostEqual(xrd.x[0], 21.107738329639844) self.assertAlmostEqual(xrd.y[0], 36.483184003748946) self.assertEqual(xrd.hkls[0], [{'hkl': (1, 0, 0), 'multiplicity': 6}]) self.assertAlmostEqual(xrd.d_hkls[0], 4.2089999999999996) self.assertAlmostEqual(xrd.x[1], 30.024695921112777) self.assertAlmostEqual(xrd.y[1], 100) self.assertEqual(xrd.hkls[1], [{"hkl": (1, 1, 0), "multiplicity": 12}]) self.assertAlmostEqual(xrd.d_hkls[1], 2.976212442014178) s = self.get_structure("LiFePO4") xrd = c.get_pattern(s, two_theta_range=(0, 90)) self.assertAlmostEqual(xrd.x[1], 17.03504233621785) self.assertAlmostEqual(xrd.y[1], 50.400928948337075) s = self.get_structure("Li10GeP2S12") xrd = c.get_pattern(s, two_theta_range=(0, 90)) self.assertAlmostEqual(xrd.x[1], 14.058274883353876) self.assertAlmostEqual(xrd.y[1], 4.4111123641667671) # Test a hexagonal structure. s = self.get_structure("Graphite") xrd = c.get_pattern(s, two_theta_range=(0, 90)) self.assertAlmostEqual(xrd.x[0], 26.21057350859598) self.assertAlmostEqual(xrd.y[0], 100) self.assertAlmostEqual(len(xrd.hkls[0][0]["hkl"]), 4) # Add test case with different lengths of coefficients. # Also test d_hkl. coords = [[0.25, 0.25, 0.173], [0.75, 0.75, 0.827], [0.75, 0.25, 0], [0.25, 0.75, 0], [0.25, 0.25, 0.676], [0.75, 0.75, 0.324]] sp = ["Si", "Si", "Ru", "Ru", "Pr", "Pr"] s = Structure(Lattice.tetragonal(4.192, 6.88), sp, coords) xrd = c.get_pattern(s) self.assertAlmostEqual(xrd.x[0], 12.86727341476735) self.assertAlmostEqual(xrd.y[0], 31.448239816769796) self.assertAlmostEqual(xrd.d_hkls[0], 6.88) self.assertEqual(len(xrd), 42) xrd = c.get_pattern(s, two_theta_range=[0, 60]) self.assertEqual(len(xrd), 18) # Test with and without Debye-Waller factor tungsten = Structure(Lattice.cubic(3.1653), ["W"] * 2, [[0, 0, 0], [0.5, 0.5, 0.5]]) xrd = c.get_pattern(tungsten, scaled=False) self.assertAlmostEqual(xrd.x[0], 40.294828554672264) self.assertAlmostEqual(xrd.y[0], 2414237.5633093244) self.assertAlmostEqual(xrd.d_hkls[0], 2.2382050944897789) c = XRDCalculator(debye_waller_factors={"W": 0.1526}) xrd = c.get_pattern(tungsten, scaled=False) self.assertAlmostEqual(xrd.x[0], 40.294828554672264) self.assertAlmostEqual(xrd.y[0], 2377745.2296686019) self.assertAlmostEqual(xrd.d_hkls[0], 2.2382050944897789) c.get_plot(tungsten).show()
def setUp(self): module_dir = os.path.dirname(os.path.abspath(__file__)) with open( os.path.join(module_dir, "surface_samples.json")) as data_file: surface_properties = json.load(data_file) surface_energies, miller_indices = {}, {} for mpid in surface_properties.keys(): e_surf_list, miller_list = [], [] for surface in surface_properties[mpid]["surfaces"]: e_surf_list.append(surface["surface_energy"]) miller_list.append(surface["miller_index"]) surface_energies[mpid] = e_surf_list miller_indices[mpid] = miller_list # In the case of a high anisotropy material # Nb: mp-8636 latt_Nb = Lattice.cubic(2.992) # In the case of an fcc material # Ir: mp-101 latt_Ir = Lattice.cubic(3.8312) # In the case of a hcp material # Ti: mp-72 latt_Ti = Lattice.hexagonal(4.6000, 2.8200) self.ucell_Nb = Structure(latt_Nb, ["Nb", "Nb", "Nb", "Nb"], [[0, 0, 0], [0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]) self.wulff_Nb = WulffShape(latt_Nb, miller_indices["mp-8636"], surface_energies["mp-8636"]) self.ucell_Ir = Structure(latt_Nb, ["Ir", "Ir", "Ir", "Ir"], [[0, 0, 0], [0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]) self.wulff_Ir = WulffShape(latt_Ir, miller_indices["mp-101"], surface_energies["mp-101"]) self.ucell_Ti = Structure(latt_Ti, ["Ti", "Ti", "Ti"], [[0, 0, 0], [0.333333, 0.666667, 0.5], [0.666667, 0.333333, 0.5]]) self.wulff_Ti = WulffShape(latt_Ti, miller_indices["mp-72"], surface_energies["mp-72"]) self.cube = WulffShape(Lattice.cubic(1), [(1,0,0)], [1]) self.hex_prism = WulffShape(Lattice.hexagonal(2.63, 5.21), [(0,0,1), (1,0,0)], [0.35, 0.53]) self.surface_properties = surface_properties
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_disordered_supercell_primitive_cell(self): l = Lattice.cubic(2) f = [[0.5, 0.5, 0.5]] sp = [{'Si': 0.54738}] s = Structure(l, sp, f) #this supercell often breaks things s.make_supercell([[0,-1,1],[-1,1,0],[1,1,1]]) self.assertEqual(len(s.get_primitive_structure()), 1)
def test_mapping_symmetry(self): l = Lattice.cubic(1) l2 = Lattice.orthorhombic(1.1001, 1, 1) self.assertEqual(l.find_mapping(l2, ltol=0.1), None) self.assertEqual(l2.find_mapping(l, ltol=0.1), None) l2 = Lattice.orthorhombic(1.0999, 1, 1) self.assertNotEqual(l2.find_mapping(l, ltol=0.1), None) self.assertNotEqual(l.find_mapping(l2, ltol=0.1), None)
def test_merge_sites(self): species = [{'Ag': 0.5}, {'Cl': 0.35}, {'Ag': 0.5}, {'F': 0.25}] coords = [[0, 0, 0], [0.5, 0.5, 0.5], [0, 0, 0], [0.5, 0.5, 1.501]] s = Structure(Lattice.cubic(1), species, coords) s.merge_sites() self.assertEqual(s[0].specie.symbol, 'Ag') self.assertEqual(s[1].species_and_occu, Composition({'Cl': 0.35, 'F': 0.25})) self.assertArrayAlmostEqual(s[1].frac_coords, [.5, .5, .5005])
def setUp(self): self.lattice = Lattice.cubic(10.0) self.si = Element("Si") self.site = PeriodicSite("Fe", [0.25, 0.35, 0.45], self.lattice) self.site2 = PeriodicSite({"Si": 0.5}, [0, 0, 0], self.lattice) self.assertEquals(self.site2.species_and_occu, Composition({Element("Si"): 0.5}), "Inconsistent site created!") self.propertied_site = PeriodicSite( Specie("Fe", 2), [0.25, 0.35, 0.45], self.lattice, properties={"magmom": 5.1, "charge": 4.2} )
def setUp(self): si = Element("Si") fe = Element("Fe") coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) lattice = Lattice.cubic(10) s = Structure(lattice, [si, fe], coords) self.mod = SupercellMaker(s, [[1, 1, 0], [-1, 1, 0], [0, 0, 2]])
def test_init(self): a = 9.026 lattice = Lattice.cubic(a) self.assertIsNotNone(lattice, "Initialization from new_cubic failed") lattice2 = Lattice([[a, 0, 0], [0, a, 0], [0, 0, a]]) for i in range(0, 3): for j in range(0, 3): self.assertAlmostEqual(lattice.matrix[i][j], lattice2.matrix[i][j], 5, "Inconsistent matrix from two inits!")
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_no_oxidation(self): specie = {"Cu1+": 0.5, "Au2+": 0.5} cuau = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.677), [specie], [[0, 0, 0]]) trans = OrderDisorderedStructureTransformation() ss = trans.apply_transformation(cuau, return_ranked_list=100) self.assertEqual(ss[0]["structure"].composition["Cu+"], 2) trans = OrderDisorderedStructureTransformation(no_oxi_states=True) ss = trans.apply_transformation(cuau, return_ranked_list=100) self.assertEqual(ss[0]["structure"].composition["Cu+"], 0) self.assertEqual(ss[0]["structure"].composition["Cu"], 2)
def test_attributes(self): """docstring for test_attributes""" lattice = Lattice.cubic(10.0) self.assertEqual(lattice.a, 10.0) self.assertEqual(lattice.b, 10.0) self.assertEqual(lattice.c, 10.0) self.assertAlmostEqual(lattice.volume, 1000.0) xyz = lattice.get_cartesian_coords([0.25, 0.35, 0.45]) self.assertEqual(xyz[0], 2.5) self.assertEqual(xyz[1], 3.5) self.assertEqual(xyz[2], 4.5)
def test_cell_scattering_factors(self): # Test that fcc structure gives 0 intensity for mixed even, odd hkl. c = TEMCalculator() nacl = Structure.from_spacegroup("Fm-3m", Lattice.cubic(5.692), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) point = [(2, 1, 0)] spacings = c.get_interplanar_spacings(nacl, point) angles = c.bragg_angles(spacings) cellscatt = c.cell_scattering_factors(nacl, angles) self.assertAlmostEqual(cellscatt[(2, 1, 0)], 0)
def test_get_s2(self): # Tests that the appropriate s2 factor is returned. c = TEMCalculator() latt = Lattice.cubic(4.209) cubic = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) point = [(-10, 3, 0)] spacings = c.get_interplanar_spacings(cubic, point) angles = c.bragg_angles(spacings) s2 = c.get_s2(angles) for p in s2: self.assertAlmostEqual(s2[p], 1.5381852947115047)
def test_get_points_in_sphere_pbc(self): latt = Lattice.cubic(1) pts = [] for a, b, c in itertools.product(xrange(10), xrange(10), xrange(10)): pts.append([a / 10, b / 10, c / 10]) self.assertEqual( len(get_points_in_sphere_pbc(latt, pts, [0, 0, 0], 0.1)), 7) self.assertEqual( len(get_points_in_sphere_pbc(latt, pts, [0.5, 0.5, 0.5], 0.5)), 515)
def setUp(self): l = Lattice.cubic(3.51) species = ["Ni"] coords = [[0, 0, 0]] self.Ni = Structure.from_spacegroup("Fm-3m", l, species, coords) l = Lattice.cubic(2.819000) species = ["Fe"] coords = [[0, 0, 0]] self.Fe = Structure.from_spacegroup("Im-3m", l, species, coords) self.Si = Structure.from_spacegroup("Fd-3m", Lattice.cubic(5.430500), ["Si"], [(0, 0, 0.5)]) with open( os.path.join( os.path.abspath(os.path.dirname(__file__)), "..", "reconstructions_archive.json", )) as data_file: self.rec_archive = json.load(data_file)
def test_get_slab(self): s = self.get_structure("LiFePO4") gen = SlabGenerator(s, [0, 0, 1], 10, 10) s = gen.get_slab(0.25) self.assertAlmostEqual(s.lattice.abc[2], 20.820740000000001) fcc = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) gen = SlabGenerator(fcc, [1, 1, 1], 10, 10, max_normal_search=1) slab = gen.get_slab() self.assertEqual(len(slab), 6) gen = SlabGenerator(fcc, [1, 1, 1], 10, 10, primitive=False, max_normal_search=1) slab_non_prim = gen.get_slab() self.assertEqual(len(slab_non_prim), len(slab) * 4) # Some randomized testing of cell vectors for i in range(1, 231): i = random.randint(1, 230) sg = SpaceGroup.from_int_number(i) if sg.crystal_system == "hexagonal" or (sg.crystal_system == "trigonal" and (sg.symbol.endswith("H") or sg.int_number in [ 143, 144, 145, 147, 149, 150, 151, 152, 153, 154, 156, 157, 158, 159, 162, 163, 164, 165])): latt = Lattice.hexagonal(5, 10) else: # Cubic lattice is compatible with all other space groups. latt = Lattice.cubic(5) s = Structure.from_spacegroup(i, latt, ["H"], [[0, 0, 0]]) miller = (0, 0, 0) while miller == (0, 0, 0): miller = (random.randint(0, 6), random.randint(0, 6), random.randint(0, 6)) gen = SlabGenerator(s, miller, 10, 10) a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0)
def test_properties(self): self.assertEqual(self.kpoint.frac_coords[0], 0.1) self.assertEqual(self.kpoint.frac_coords[1], 0.4) self.assertEqual(self.kpoint.frac_coords[2], -0.5) self.assertEqual(self.kpoint.a, 0.1) self.assertEqual(self.kpoint.b, 0.4) self.assertEqual(self.kpoint.c, -0.5) self.assertEqual(self.lattice, Lattice.cubic(10.0)) self.assertEqual(self.kpoint.cart_coords[0], 1.0) self.assertEqual(self.kpoint.cart_coords[1], 4.0) self.assertEqual(self.kpoint.cart_coords[2], -5.0) self.assertEqual(self.kpoint.label, "X")
def test_from_spacegroup(self): s1 = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Li", "O"], [[0.25, 0.25, 0.25], [0, 0, 0]]) self.assertEqual(s1.formula, "Li8 O4") s2 = Structure.from_spacegroup(225, Lattice.cubic(3), ["Li", "O"], [[0.25, 0.25, 0.25], [0, 0, 0]]) self.assertEqual(s1, s2) s2 = Structure.from_spacegroup(225, Lattice.cubic(3), ["Li", "O"], [[0.25, 0.25, 0.25], [0, 0, 0]], site_properties={"charge": [1, -2]}) self.assertEqual(sum(s2.site_properties["charge"]), 0) s = Structure.from_spacegroup("Pm-3m", Lattice.cubic(3), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.assertEqual(s.formula, "Cs1 Cl1") self.assertRaises(ValueError, Structure.from_spacegroup, "Pm-3m", Lattice.tetragonal(1, 3), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]])
def test_merge_sites(self): species = [{'Ag': 0.5}, {'Cl': 0.35}, {'Ag': 0.5}, {'F': 0.25}] coords = [[0, 0, 0], [0.5, 0.5, 0.5], [0, 0, 0], [0.5, 0.5, 1.501]] s = Structure(Lattice.cubic(1), species, coords) s.merge_sites() self.assertEqual(s[0].specie.symbol, 'Ag') self.assertEqual(s[1].species_and_occu, Composition({ 'Cl': 0.35, 'F': 0.25 })) self.assertArrayAlmostEqual(s[1].frac_coords, [.5, .5, .5005])
def test_max_disordered_sites(self): l = Lattice.cubic(4) s_orig = Structure( l, [{"Li": 0.2, "Na": 0.2, "K": 0.6}, {"O": 1}], [[0, 0, 0], [0.5, 0.5, 0.5]], ) est = EnumerateStructureTransformation(max_cell_size=None, max_disordered_sites=5) dd = est.apply_transformation(s_orig, return_ranked_list=100) self.assertEqual(len(dd), 9) for d in dd: self.assertEqual(len(d["structure"]), 10)
def test_sulfide_type(self): # NaS2 -> polysulfide latt = Lattice.tetragonal(9.59650, 11.78850) species = ["Na"] * 2 + ["S"] * 2 coords = [ [0.00000, 0.00000, 0.17000], [0.27600, 0.25000, 0.12500], [0.03400, 0.25000, 0.29600], [0.14700, 0.11600, 0.40000], ] struct = Structure.from_spacegroup(122, latt, species, coords) self.assertEqual(sulfide_type(struct), "polysulfide") # NaCl type NaS -> sulfide latt = Lattice.cubic(5.75) species = ["Na", "S"] coords = [[0.00000, 0.00000, 0.00000], [0.50000, 0.50000, 0.50000]] struct = Structure.from_spacegroup(225, latt, species, coords) self.assertEqual(sulfide_type(struct), "sulfide") # Na2S2O3 -> None (sulfate) latt = Lattice.monoclinic(6.40100, 8.10000, 8.47400, 96.8800) species = ["Na"] * 2 + ["S"] * 2 + ["O"] * 3 coords = [ [0.29706, 0.62396, 0.08575], [0.37673, 0.30411, 0.45416], [0.52324, 0.10651, 0.21126], [0.29660, -0.04671, 0.26607], [0.17577, 0.03720, 0.38049], [0.38604, -0.20144, 0.33624], [0.16248, -0.08546, 0.11608], ] struct = Structure.from_spacegroup(14, latt, species, coords) self.assertEqual(sulfide_type(struct), None) # Na3PS3O -> sulfide latt = Lattice.orthorhombic(9.51050, 11.54630, 5.93230) species = ["Na"] * 2 + ["S"] * 2 + ["P", "O"] coords = [ [0.19920, 0.11580, 0.24950], [0.00000, 0.36840, 0.29380], [0.32210, 0.36730, 0.22530], [0.50000, 0.11910, 0.27210], [0.50000, 0.29400, 0.35500], [0.50000, 0.30300, 0.61140], ] struct = Structure.from_spacegroup(36, latt, species, coords) self.assertEqual(sulfide_type(struct), "sulfide") # test for unphysical cells struct.scale_lattice(struct.volume * 10) self.assertEqual(sulfide_type(struct), "sulfide")
def test_remove_oxidation_states(self): co_elem = Element("Co") o_elem = Element("O") co_specie = Specie("Co", 2) o_specie = Specie("O", -2) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) lattice = Lattice.cubic(10) s_elem = Structure(lattice, [co_elem, o_elem], coords) s_specie = Structure(lattice, [co_specie, o_specie], coords) s_specie.remove_oxidation_states() self.assertEqual(s_elem, s_specie, "Oxidation state remover " "failed")
def test_init(self): a = 9.026 lattice = Lattice.cubic(a) self.assertIsNotNone(lattice, "Initialization from new_cubic failed") lattice2 = Lattice([[a, 0, 0], [0, a, 0], [0, 0, a]]) for i in range(0, 3): for j in range(0, 3): self.assertAlmostEqual( lattice.matrix[i][j], lattice2.matrix[i][j], 5, "Inconsistent matrix from two inits!", )
def test_electron_scattering_factors(self): # Test the electron atomic scattering factor, values approximate with # international table of crystallography volume C. Rounding error when converting hkl to sin(theta)/lambda. # Error increases as sin(theta)/lambda is smaller. c = TEMCalculator() latt = Lattice.cubic(4.209) cubic = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) nacl = Structure.from_spacegroup("Fm-3m", Lattice.cubic(5.692), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) point = [(2, 1, 3)] point_nacl = [(4, 2, 0)] spacings = c.get_interplanar_spacings(cubic, point) spacings_nacl = c.get_interplanar_spacings(nacl, point_nacl) angles = c.bragg_angles(spacings) angles_nacl = c.bragg_angles(spacings_nacl) elscatt = c.electron_scattering_factors(cubic, angles) elscatt_nacl = c.electron_scattering_factors(nacl, angles_nacl) self.assertAlmostEqual(elscatt["Cs"][(2, 1, 3)], 2.890, places=1) self.assertAlmostEqual(elscatt["Cl"][(2, 1, 3)], 1.138, places=1) self.assertAlmostEqual(elscatt_nacl["Na"][(4, 2, 0)], 0.852, places=1) self.assertAlmostEqual(elscatt_nacl["Cl"][(4, 2, 0)], 1.372, places=1)
def setUp(self): self.cscl = Structure.from_spacegroup("Pm-3m", Lattice.cubic(4.2), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.Fe = Structure.from_spacegroup( \ "Im-3m", Lattice.cubic(2.82), ["Fe"], [[0, 0, 0]]) self.lifepo4 = self.get_structure("LiFePO4") self.tei = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False) self.LiCoO2 = Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False) self.p1 = Structure(Lattice.from_parameters(3, 4, 5, 31, 43, 50), ["H", "He"], [[0, 0, 0], [0.1, 0.2, 0.3]]) self.graphite = self.get_structure("Graphite") self.trigBi = Structure( Lattice.from_parameters(3, 3, 10, 90, 90, 120), ["Bi", "Bi", "Bi", "Bi", "Bi", "Bi"], [[0.3333, 0.6666, 0.39945113], [0.0000, 0.0000, 0.26721554], [0.0000, 0.0000, 0.73278446], [0.6666, 0.3333, 0.60054887], [0.6666, 0.3333, 0.06611779], [0.3333, 0.6666, 0.93388221]])
def test_get_chemsys(self): # Test with structure struct = Structure.from_spacegroup('Fm-3m', Lattice.cubic(5.0), ['Ni', 'O'], [[0, 0, 0], [0.75, 0.25, 0.75]]) self.assertEqual(get_chemsys(struct), 'Ni-O') # Test sorting test_val = get_chemsys("TiO2") self.assertEqual(test_val, "O-Ti") test_val = get_chemsys("BaNiO3") self.assertEqual(test_val, "Ba-Ni-O")
def test_md(self): s = Structure.from_spacegroup(225, Lattice.cubic(3.62126), ["Cu"], [[0, 0, 0]]) ld = LammpsData.from_structure(s, atom_style="atomic") ff = "\n".join(["pair_style eam", "pair_coeff * * Cu_u3.eam"]) md = LammpsRun.md(data=ld, force_field=ff, temperature=1600.0, nsteps=10000) md.write_inputs(output_dir="md") with open(os.path.join("md", "in.md")) as f: md_script = f.read() script_string = """# Sample input script template for MD # Initialization units metal atom_style atomic # Atom definition read_data md.data #read_restart md.restart # Force field settings (consult official document for detailed formats) pair_style eam pair_coeff * * Cu_u3.eam # Create velocities velocity all create 1600.0 142857 mom yes rot yes dist gaussian # Ensemble constraints #fix 1 all nve fix 1 all nvt temp 1600.0 1600.0 0.1 #fix 1 all npt temp 1600.0 1600.0 0.1 iso $pressure $pressure 1.0 # Various operations within timestepping #fix ... #compute ... # Output settings #thermo_style custom ... # control the thermo data type to output thermo 100 # output thermo data every N steps #dump 1 all atom 100 traj.*.gz # dump a snapshot every 100 steps # Actions run 10000 """ self.assertEqual(md_script, script_string) self.assertTrue(os.path.exists(os.path.join("md", "md.data")))
def setUp(self): self.cscl = Structure.from_spacegroup("Pm-3m", Lattice.cubic(4.2), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.lifepo4 = self.get_structure("LiFePO4") self.tei = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False) self.LiCoO2 = Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False) self.p1 = Structure(Lattice.from_parameters(3, 4, 5, 31, 43, 50), ["H", "He"], [[0, 0, 0], [0.1, 0.2, 0.3]]) self.graphite = self.get_structure("Graphite")
def setUp(self): self.lattice = Lattice.cubic(10.0) self.cubic = self.lattice self.tetragonal = Lattice.tetragonal(10, 20) self.orthorhombic = Lattice.orthorhombic(10, 20, 30) self.monoclinic = Lattice.monoclinic(10, 20, 30, 66) self.hexagonal = Lattice.hexagonal(10, 20) self.rhombohedral = Lattice.rhombohedral(10, 77) self.cubic_partial_pbc = Lattice.cubic(10.0, pbc=(True, True, False)) family_names = [ "cubic", "tetragonal", "orthorhombic", "monoclinic", "hexagonal", "rhombohedral", ] self.families = {} for name in family_names: self.families[name] = getattr(self, name)
def test_selling_dist(self): # verification process described here: https://github.com/materialsproject/pymatgen/pull/1888#issuecomment-818072164 np.testing.assert_( Lattice.selling_dist(Lattice.cubic(5), Lattice.cubic(5)) == 0) hex_lattice = Lattice.hexagonal(5, 8) triclinic_lattice = Lattice.from_parameters(4, 10, 11, 100, 110, 80) np.testing.assert_allclose(Lattice.selling_dist( hex_lattice, triclinic_lattice), 76, rtol=0.1) np.testing.assert_allclose( Lattice.selling_dist(Lattice.tetragonal(10, 12), Lattice.tetragonal(10.1, 11.9)), 3.7, rtol=0.1, ) np.testing.assert_allclose( Lattice.selling_dist( Lattice.cubic(5), Lattice.from_parameters(8, 10, 12, 80, 90, 95)), 115.6, rtol=0.1, )
def test_cell_intensity(self): # Test that bcc structure gives lower intensity for h + k + l != even. c = TEMCalculator() latt = Lattice.cubic(4.209) cubic = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) point = [(2, 1, 0)] point2 = [(2, 2, 0)] spacings = c.get_interplanar_spacings(cubic, point) spacings2 = c.get_interplanar_spacings(cubic, point2) angles = c.bragg_angles(spacings) angles2 = c.bragg_angles(spacings2) cellint = c.cell_intensity(cubic, angles) cellint2 = c.cell_intensity(cubic, angles2) self.assertGreater(cellint2[(2, 2, 0)], cellint[(2, 1, 0)])
def setUp(self): lattice = Lattice.cubic(3.010) frac_coords = [[0.00000, 0.00000, 0.00000], [0.00000, 0.50000, 0.50000], [0.50000, 0.00000, 0.50000], [0.50000, 0.50000, 0.00000], [0.50000, 0.00000, 0.00000], [0.50000, 0.50000, 0.50000], [0.00000, 0.00000, 0.50000], [0.00000, 0.50000, 0.00000]] species = ['Mg', 'Mg', 'Mg', 'Mg', 'O', 'O', 'O', 'O'] MgO = Structure(lattice, species, frac_coords) self.MgO = MgO.add_oxidation_state_by_element({"Mg": 2, "O": -6})
def test_merge_sites(self): species = [{ 'Ag': 0.5 }, { 'Cl': 0.25 }, { 'Cl': 0.1 }, { 'Ag': 0.5 }, { 'F': 0.15 }, { 'F': 0.1 }] coords = [[0, 0, 0], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0, 0, 0], [0.5, 0.5, 1.501], [0.5, 0.5, 1.501]] s = Structure(Lattice.cubic(1), species, coords) s.merge_sites(mode="s") self.assertEqual(s[0].specie.symbol, 'Ag') self.assertEqual(s[1].species_and_occu, Composition({ 'Cl': 0.35, 'F': 0.25 })) self.assertArrayAlmostEqual(s[1].frac_coords, [.5, .5, .5005]) # Test for TaS2 with spacegroup 166 in 160 setting. l = Lattice.from_lengths_and_angles([3.374351, 3.374351, 20.308941], [90.000000, 90.000000, 120.000000]) species = ["Ta", "S", "S"] coords = [[0.000000, 0.000000, 0.944333], [0.333333, 0.666667, 0.353424], [0.666667, 0.333333, 0.535243]] tas2 = Structure.from_spacegroup(160, l, species, coords) assert len(tas2) == 13 tas2.merge_sites(mode="d") assert len(tas2) == 9 l = Lattice.from_lengths_and_angles([3.587776, 3.587776, 19.622793], [90.000000, 90.000000, 120.000000]) species = ["Na", "V", "S", "S"] coords = [[0.333333, 0.666667, 0.165000], [0.000000, 0.000000, 0.998333], [0.333333, 0.666667, 0.399394], [0.666667, 0.333333, 0.597273]] navs2 = Structure.from_spacegroup(160, l, species, coords) assert len(navs2) == 18 navs2.merge_sites(mode="d") assert len(navs2) == 12
def test_interplanar_angle(self): # test interplanar angles. Reference values from KW Andrews, # Interpretation of Electron Diffraction pp70-90. c = TEMCalculator() latt = Lattice.cubic(4.209) cubic = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) phi = c.get_interplanar_angle(cubic, (0, 0, -1), (0, -1, 0)) self.assertAlmostEqual(90, phi, places=1) tet = self.get_structure("Li10GeP2S12") phi = c.get_interplanar_angle(tet, (0, 0, 1), (1, 0, 3)) self.assertAlmostEqual(25.796, phi, places=1) latt = Lattice.hexagonal(2, 4) hex = Structure(latt, ["Ab"], [[0, 0, 0]]) phi = c.get_interplanar_angle(hex, (0, 0, 1), (1, 0, 6)) self.assertAlmostEqual(21.052, phi, places=1)
def setUp(self): zno1 = Structure.from_file(get_path("ZnO-wz.cif"), primitive=False) zno55 = SlabGenerator(zno1, [1, 0, 0], 5, 5, lll_reduce=False, center_slab=False).get_slab() Ti = Structure(Lattice.hexagonal(4.6, 2.82), ["Ti", "Ti", "Ti"], [[0.000000, 0.000000, 0.000000], [0.333333, 0.666667, 0.500000], [0.666667, 0.333333, 0.500000]]) Ag_fcc = Structure(Lattice.cubic(4.06), ["Ag", "Ag", "Ag", "Ag"], [[0.000000, 0.000000, 0.000000], [0.000000, 0.500000, 0.500000], [0.500000, 0.000000, 0.500000], [0.500000, 0.500000, 0.000000]]) self.ti = Ti self.agfcc = Ag_fcc self.zno1 = zno1 self.zno55 = zno55 self.h = Structure(Lattice.cubic(3), ["H"], [[0, 0, 0]]) self.libcc = Structure(Lattice.cubic(3.51004), ["Li", "Li"], [[0, 0, 0], [0.5, 0.5, 0.5]])
def setUp(self): self.lattice = Lattice.cubic(10.0) self.si = Element("Si") self.site = PeriodicSite("Fe", [0.25, 0.35, 0.45], self.lattice) self.site2 = PeriodicSite({"Si": 0.5}, [0, 0, 0], self.lattice) self.assertEqual(self.site2.species, Composition({Element('Si'): 0.5}), "Inconsistent site created!") self.propertied_site = PeriodicSite(Specie("Fe", 2), [0.25, 0.35, 0.45], self.lattice, properties={ 'magmom': 5.1, 'charge': 4.2 }) self.dummy_site = PeriodicSite("X", [0, 0, 0], self.lattice)
def test_get_slabs(self): gen = SlabGenerator(self.get_structure("CsCl"), [0, 0, 1], 10, 10) #Test orthogonality of some internal variables. a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0) self.assertEqual(len(gen.get_slabs()), 1) s = self.get_structure("LiFePO4") gen = SlabGenerator(s, [0, 0, 1], 10, 10) self.assertEqual(len(gen.get_slabs()), 5) self.assertEqual(len(gen.get_slabs(bonds={("P", "O"): 3})), 2) # There are no slabs in LFP that does not break either P-O or Fe-O # bonds for a miller index of [0, 0, 1]. self.assertEqual(len(gen.get_slabs( bonds={("P", "O"): 3, ("Fe", "O"): 3})), 0) #If we allow some broken bonds, there are a few slabs. self.assertEqual(len(gen.get_slabs( bonds={("P", "O"): 3, ("Fe", "O"): 3}, max_broken_bonds=2)), 2) # At this threshold, only the origin and center Li results in # clustering. All other sites are non-clustered. So the of # slabs is of sites in LiFePO4 unit cell - 2 + 1. self.assertEqual(len(gen.get_slabs(tol=1e-4)), 15) LiCoO2=Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False) gen = SlabGenerator(LiCoO2, [0, 0, 1], 10, 10) lco = gen.get_slabs(bonds={("Co", "O"): 3}) self.assertEqual(len(lco), 1) a, b, c = gen.oriented_unit_cell.lattice.matrix self.assertAlmostEqual(np.dot(a, gen._normal), 0) self.assertAlmostEqual(np.dot(b, gen._normal), 0) scc = Structure.from_spacegroup("Pm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) gen = SlabGenerator(scc, [0, 0, 1], 10, 10) slabs = gen.get_slabs() self.assertEqual(len(slabs), 1) gen = SlabGenerator(scc, [1, 1, 1], 10, 10, max_normal_search=1) slabs = gen.get_slabs() self.assertEqual(len(slabs), 1)
def test_selling_vector(self): a1 = 10 np.testing.assert_array_almost_equal( Lattice.cubic(a1).selling_vector.round(4), np.array([0, 0, 0, -(a1**2), -(a1**2), -(a1**2)]), ) a2, c2 = 5, 8 np.testing.assert_array_almost_equal( Lattice.tetragonal(a2, c2).selling_vector.round(4), np.array([0, 0, 0, -(a2**2), -(a2**2), -(c2**2)]), ) a3, b3, c3 = 4, 6, 7 np.testing.assert_array_almost_equal( Lattice.orthorhombic(a3, b3, c3).selling_vector.round(4), np.array([0, 0, 0, -(a3**2), -(b3**2), -(c3**2)]), )
def test_apply_transformation(self): s = self.get_structure("LiFePO4") trans = SlabTransformation([0, 0, 1], 10, 10, shift=0.25) gen = SlabGenerator(s, [0, 0, 1], 10, 10) slab_from_gen = gen.get_slab(0.25) slab_from_trans = trans.apply_transformation(s) self.assertArrayAlmostEqual(slab_from_gen.lattice.matrix, slab_from_trans.lattice.matrix) self.assertArrayAlmostEqual(slab_from_gen.cart_coords, slab_from_trans.cart_coords) fcc = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]]) trans = SlabTransformation([1, 1, 1], 10, 10) slab_from_trans = trans.apply_transformation(fcc) gen = SlabGenerator(fcc, [1, 1, 1], 10, 10) slab_from_gen = gen.get_slab() self.assertArrayAlmostEqual(slab_from_gen.lattice.matrix, slab_from_trans.lattice.matrix) self.assertArrayAlmostEqual(slab_from_gen.cart_coords, slab_from_trans.cart_coords)