.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/powder_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_powder_demo.py: Powder patterns of anatase ========================== Read a CIF file, let DISCUS build a supercell, calculate X-ray and neutron powder patterns with the Debye equation, and plot them with matplotlib. The structure is anatase (TiO\ :sub:`2`), COD entry 1010942 (``python/examples/anatase.cif``). To run it yourself, from the repository root:: DIFFUSE_PYTHON_PATH=build/python PYTHONPATH=python python3 python/examples/powder_demo.py .. GENERATED FROM PYTHON SOURCE LINES 15-24 .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt import diffuse as dc CIF = HERE / "anatase.cif" .. GENERATED FROM PYTHON SOURCE LINES 31-35 Read the structure ------------------ A CIF file holds the asymmetric unit. DISCUS applies the space group (I4\ :sub:`1`/amd) and builds 10 x 10 x 4 unit cells. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python crystal = dc.read_structure(CIF, supercell=(10, 10, 4)) print(crystal) .. rst-class:: sphx-glr-script-out .. code-block:: none Crystal('anatase.cif', supercell=(10, 10, 4), natoms=4800, lattice=(3.73, 3.73, 9.37, 90, 90, 90)) .. GENERATED FROM PYTHON SOURCE LINES 40-44 Calculate powder patterns ------------------------- The results are NumPy arrays (``q`` and ``intensity``), so any plotting or analysis library can use them. .. GENERATED FROM PYTHON SOURCE LINES 44-49 .. code-block:: Python xray = dc.powder(crystal, qmin=0.5, qmax=10, dq=0.005, radiation="xray") neutron = dc.powder(crystal, qmin=0.5, qmax=10, dq=0.005, radiation="neutron") print(f"{len(xray.q)} points from Q = {xray.q[0]} to {xray.q[-1]} Å⁻¹") .. rst-class:: sphx-glr-script-out .. code-block:: none 1901 points from Q = 0.5 to 10.0 Å⁻¹ .. GENERATED FROM PYTHON SOURCE LINES 50-52 A much smaller crystal of the same structure shows how the crystal size broadens the peaks. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python small = dc.read_structure(CIF, supercell=(3, 3, 1)) xray_small = dc.powder(small, qmin=0.5, qmax=10, dq=0.005, radiation="xray") .. GENERATED FROM PYTHON SOURCE LINES 57-61 Plot ---- X-rays see mostly titanium; neutrons see oxygen much more strongly, so the relative peak heights change. .. GENERATED FROM PYTHON SOURCE LINES 61-79 .. code-block:: Python fig, (top, bottom) = plt.subplots(2, 1, figsize=(9, 7), sharex=True, constrained_layout=True) top.plot(xray.q, xray.intensity / xray.intensity.max(), lw=1, label="X-ray") top.plot(neutron.q, neutron.intensity / neutron.intensity.max(), lw=1, label="neutron") top.set_title(f"Anatase, 10×10×4 unit cells ({crystal.natoms} atoms)") top.set_ylabel("Intensity (normalised)") top.legend() for p, label in ((xray, f"{crystal.natoms} atoms"), (xray_small, f"{small.natoms} atoms")): bottom.plot(p.q, p.intensity / p.intensity.max(), lw=1, label=label) bottom.set_title("X-ray: crystal size broadens the peaks") bottom.set_xlabel(r"$Q$ (Å$^{-1}$)") bottom.set_ylabel("Intensity (normalised)") bottom.set_xlim(xray.q[0], xray.q[-1]) bottom.legend() plt.show() .. image-sg:: /gallery/images/sphx_glr_powder_demo_001.png :alt: Anatase, 10×10×4 unit cells (4800 atoms), X-ray: crystal size broadens the peaks :srcset: /gallery/images/sphx_glr_powder_demo_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.199 seconds) .. _sphx_glr_download_gallery_powder_demo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: powder_demo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: powder_demo.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: powder_demo.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_