.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/creating_a_simulation_library/simulating_diffraction_patterns.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_examples_creating_a_simulation_library_simulating_diffraction_patterns.py: ============================================== Simple Diffraction Pattern Simulation Examples ============================================== This example demonstrates how to simulate diffraction patterns using the :class:`diffsims.generators.simulation_generator.SimulationGenerator` class. A single diffraction pattern can be simulated for a single phase or multiple diffraction patterns can be simulated for a single/multiple phases given a rotation. One Pattern for One Phase -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 15-45 .. code-block:: Python from orix.crystal_map import Phase from orix.quaternion import Rotation from diffpy.structure import Atom, Lattice, Structure import matplotlib.pyplot as plt from diffsims.generators.simulation_generator import SimulationGenerator a = 5.431 latt = Lattice(a, a, a, 90, 90, 90) atom_list = [] for coords in [[0, 0, 0], [0.5, 0, 0.5], [0, 0.5, 0.5], [0.5, 0.5, 0]]: x, y, z = coords[0], coords[1], coords[2] atom_list.append(Atom(atype="Si", xyz=[x, y, z], lattice=latt)) # Motif part A atom_list.append( Atom(atype="Si", xyz=[x + 0.25, y + 0.25, z + 0.25], lattice=latt) ) # Motif part B struct = Structure(atoms=atom_list, lattice=latt) p = Phase(structure=struct, space_group=227) gen = SimulationGenerator( accelerating_voltage=200, ) rot = Rotation.from_axes_angles( [1, 0, 0], 45, degrees=True ) # 45 degree rotation around x-axis sim = gen.calculate_diffraction2d(phase=p, rotation=rot) _ = sim.plot(show_labels=True) # plot the first (and only) diffraction pattern .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_001.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python sim.coordinates # coordinates of the first (and only) diffraction pattern .. rst-class:: sphx-glr-script-out .. code-block:: none DiffractingVector (18,), (m-3m) [[ 4. 0. 0.] [ 3. 1. 1.] [ 3. -1. -1.] [ 1. 3. 3.] [ 1. 1. 1.] [ 1. -1. -1.] [ 1. -3. -3.] [ 0. 2. 2.] [ 0. -2. -2.] [-1. 3. 3.] [-1. 1. 1.] [-1. -1. -1.] [-1. -3. -3.] [-3. 1. 1.] [-3. -1. -1.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] .. GENERATED FROM PYTHON SOURCE LINES 50-52 Simulating Multiple Patterns for a Single Phase ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-60 .. code-block:: Python rot = Rotation.from_axes_angles( [1, 0, 0], (0, 15, 30, 45, 60, 75, 90), degrees=True ) # 45 degree rotation around x-axis sim = gen.calculate_diffraction2d(phase=p, rotation=rot) _ = sim.plot(show_labels=True) # plot the first diffraction pattern .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_002.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. code-block:: Python _ = sim.irot[3].plot( show_labels=True ) # plot the fourth(45 degrees) diffraction pattern .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_003.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python sim.coordinates # coordinates of all the diffraction patterns .. rst-class:: sphx-glr-script-out .. code-block:: none array([DiffractingVector (10,), (m-3m) [[ 4. 0. 0.] [ 2. 2. 0.] [ 2. -2. 0.] [ 0. 4. 0.] [ 0. -4. 0.] [-2. 2. 0.] [-2. -2. 0.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (4,), (m-3m) [[ 4. 0. 0.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (4,), (m-3m) [[ 4. 0. 0.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (18,), (m-3m) [[ 4. 0. 0.] [ 3. 1. 1.] [ 3. -1. -1.] [ 1. 3. 3.] [ 1. 1. 1.] [ 1. -1. -1.] [ 1. -3. -3.] [ 0. 2. 2.] [ 0. -2. -2.] [-1. 3. 3.] [-1. 1. 1.] [-1. -1. -1.] [-1. -3. -3.] [-3. 1. 1.] [-3. -1. -1.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (4,), (m-3m) [[ 4. 0. 0.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (4,), (m-3m) [[ 4. 0. 0.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] , DiffractingVector (10,), (m-3m) [[ 4. 0. 0.] [ 2. 0. 2.] [ 2. -0. -2.] [ 0. 0. 4.] [ 0. -0. -4.] [-2. 0. 2.] [-2. -0. -2.] [-4. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] ], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 70-72 Simulating Multiple Patterns for Multiple Phases ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 72-86 .. code-block:: Python p2 = p.deepcopy() # copy the phase p2.name = "al_2" rot = Rotation.from_axes_angles( [1, 0, 0], (0, 15, 30, 45, 60, 75, 90), degrees=True ) # 45 degree rotation around x-axis sim = gen.calculate_diffraction2d(phase=[p, p2], rotation=[rot, rot]) _ = sim.plot( include_direct_beam=True, show_labels=True, min_label_intensity=0.1 ) # plot the first diffraction pattern .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_004.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-92 .. code-block:: Python _ = ( sim.iphase["al_2"].irot[3].plot(show_labels=True, min_label_intensity=0.1) ) # plot the fourth(45 degrees) diffraction pattern .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_005.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-95 Plotting a Pixelated Diffraction Pattern ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 95-101 .. code-block:: Python dp = sim.get_diffraction_pattern( shape=(512, 512), calibration=0.01, ) plt.figure() _ = plt.imshow(dp) .. image-sg:: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_006.png :alt: simulating diffraction patterns :srcset: /examples/creating_a_simulation_library/images/sphx_glr_simulating_diffraction_patterns_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.789 seconds) .. _sphx_glr_download_examples_creating_a_simulation_library_simulating_diffraction_patterns.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: simulating_diffraction_patterns.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: simulating_diffraction_patterns.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: simulating_diffraction_patterns.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_