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 get_xrd_plot(args): """ Plot XRD Args: args (dict): Args from argparse """ s = Structure.from_file(args.xrd_structure_file) c = XRDCalculator() return c.get_plot(s)
def get_xrd(): struct = readstructure() xrd = XRDCalculator() # xrd_data=xrd.get_xrd_pattern(struct) xrd_data = xrd.get_pattern(struct) jxrd_data = jsanitize(xrd_data.as_dict()) fname = 'XRD.json' proc_str = "Saving data to " + fname + " File ..." procs(proc_str, 1, sp='-->>') json_store(jxrd_data, fname) data = np.vstack((xrd_data.x, xrd_data.y)).T margin = 10. ds = xrd_data.x[0] - margin de = xrd_data.x[-1] + margin tmp_data = [ds] + xrd_data.x.tolist() + [de] tmp_data1 = np.diff(tmp_data).tolist() tmp_data2 = np.array([0] + np.cumsum(tmp_data1).tolist()) tmp_data3 = tmp_data2 / tmp_data2[-1] x_data = np.linspace(ds, de, 10000) y_data = np.zeros((len(x_data))) for i in range(1, len(tmp_data3) - 1): index = int(tmp_data3[i] * 10000) y_data[index] = xrd_data.y[i - 1] data = np.vstack((x_data, y_data)) data = (smear(data, sigma=0.1)).T head_line = "#%(key1)+12s %(key2)+12s" % { 'key1': '2theta', 'key2': 'Intensity' } fname = 'XRD.dat' proc_str = "Saving data to " + fname + " File ..." procs(proc_str, 2, sp='-->>') write_col_data(fname, data, head_line) check_matplotlib() fname = 'XRD.png' proc_str = "Saving plot to " + fname + " File ..." procs(proc_str, 3, sp='-->>') plt = xrd.get_plot(struct) plt.savefig(fname, format='png')
from IPython.display import Image, display from matplotlib import pyplot # Create CsCl structure a = 4.209 #Angstrom(埃:一个长度单位,用来表示原子尺寸、键长和电磁波波长。) latt = Lattice.cubic(a) #latt:点阵一个长度为a的立方体 #species:物质,这里是["Cs", "Cl"] #坐标:三位数组的列表,表示物质的坐标 structure = Structure(latt, ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) c = XRDCalculator() #get_plot返回的结果就是一个pyplot类型的图片 image = c.get_plot(structure, two_theta_range=(0, 90), annotate_peaks=True, ax=None, with_labels=True, fontsize=16) #image.show() image.savefig("alpha CsCl.jpg") #display(Image(filename=('./PDF - alpha CsCl.png'))) a = 6.923 #Angstrom latt = Lattice.cubic(a) structure = Structure(latt, ["Cs", "Cs", "Cs", "Cs", "Cl", "Cl", "Cl", "Cl"], [[0, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0.5], [0, 0, 0.5], [0, 0.5, 0], [0.5, 0, 0]]) image = c.get_plot(structure, two_theta_range=(0, 90), annotate_peaks=True, ax=None, with_labels=True, fontsize=16) #display(Image(filename=('./PDF - beta CsCl.png')))这段代码display我还没有弄明白,帮我看看哪里出问题了 #image.show() image.savefig("beta CsCl.jpg")
def get_xrd_plot(args): s = Structure.from_file(args.xrd_structure_file) c = XRDCalculator() return c.get_plot(s)
# This initializes the REST adaptor. Put your own API key in. from pymatgen import MPRester, Element from pymatgen.analysis.diffraction.xrd import XRDCalculator a = MPRester("") # Entries are the basic unit for thermodynamic and other analyses in pymatgen. # This gets all entries belonging to the MnO2 material. entry = a.get_entry_by_material_id( "mp-18759", inc_structure=True, property_data=["material_id", "energy", "energy_per_atom", "volume"]) #print(entry) xrdc = XRDCalculator() xrdc.get_plot(entry.structure).show()
def get_xrd_plot(args): s = Structure.from_file(args.xrd_structure_file) c = XRDCalculator() return c.get_plot(s)