.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/1D_simulation(crystalline)/plot_9_custom_method_Hahnecho.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_1D_simulation(crystalline)_plot_9_custom_method_Hahnecho.py: Writing Custom methods (HahnEcho) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Writing custom methods using the Event objects. .. GENERATED FROM PYTHON SOURCE LINES 9-18 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from mrsimulator import Simulator, SpinSystem, Site, Coupling from mrsimulator.method import Method, SpectralDimension, SpectralEvent, MixingEvent from mrsimulator.spin_system.tensors import SymmetricTensor from pprint import pprint .. GENERATED FROM PYTHON SOURCE LINES 20-22 For demonstration, we will create two spin systems, one with a single site and other with two spin 1/2 sites. .. GENERATED FROM PYTHON SOURCE LINES 22-37 .. code-block:: Python S1 = Site( isotope="1H", isotropic_chemical_shift=10, # in ppm shielding_symmetric=SymmetricTensor(zeta=-80, eta=0.25), # zeta in ppm ) S2 = Site(isotope="1H", isotropic_chemical_shift=-10) S12 = Coupling( site_index=[0, 1], isotropic_j=100, dipolar=SymmetricTensor(D=2000, eta=0, alpha=0) ) spin_system_1 = SpinSystem(sites=[S1], label="Uncoupled system") spin_system_2 = SpinSystem(sites=[S1, S2], couplings=[S12], label="Coupled system") .. GENERATED FROM PYTHON SOURCE LINES 38-46 **Create a custom method** This example is a brief illustrate on how to write a custom method in mrsimulator. For in-depth description, please refer to the :ref:`method_documentation` documentation. In this example, we use two types of Event objects---SpectralEvent and MixingEvent to create a one-dimensional Hahn echo method. .. GENERATED FROM PYTHON SOURCE LINES 46-64 .. code-block:: Python hahn_echo = Method( channels=["1H"], magnetic_flux_density=9.4, # in T rotor_angle=0, # in rads rotor_frequency=0, # in Hz spectral_dimensions=[ SpectralDimension( count=512, spectral_width=2e4, # in Hz events=[ SpectralEvent(fraction=0.5, transition_queries=[{"ch1": {"P": [1]}}]), MixingEvent(query={"ch1": {"angle": np.pi, "phase": 0}}), SpectralEvent(fraction=0.5, transition_queries=[{"ch1": {"P": [-1]}}]), ], ) ], ) .. GENERATED FROM PYTHON SOURCE LINES 65-82 In the above code, we define the two SpectralEvent objects with fraction 0.5 and the transition_queries on channel-1 of P=[1] and P=[-1], respectively. Notice, the value for the ``P`` attribute is a list. Here, it is a list with a single integer. The list notation, ``[1]``, implies that the query selects all transitions where exactly one spin is undergoing a :math:`p=+1` transition with the remaining spin at :math:`p=0`. A similar argument holds for ``[-1]`` query. By implementing query objects, we decouple the method from the spin system, i.e., once a method is defined, it can be used to simulate spectra from any given spin system. We will demonstrate this momentarily by simulating a Hahn echo spectrum from single and two-site spin systems. Besides the SpectralEvent, you may also notice a MixingEvent sandwiched in-between the two SpectralEvent. A MixingEvent does not directly contribute to the frequencies. As the name suggests, a mixing event is used for the mixing of transitions in a multi-event method such as HahnEcho. In the above code, we define a mixing query on channel-1 by setting the attributes ``angle`` and ``phase`` to :math:`\pi` and 0, respectively. There two parameters are analogous to the pulse angle and phase. .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: Python plt.figure(figsize=(4, 2)) hahn_echo.plot() plt.show() .. image-sg:: /examples/1D_simulation(crystalline)/images/sphx_glr_plot_9_custom_method_Hahnecho_001.png :alt: plot 9 custom method Hahnecho :srcset: /examples/1D_simulation(crystalline)/images/sphx_glr_plot_9_custom_method_Hahnecho_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 89-92 As mentioned before, a method object is decoupled from the spin system object. Notice, when we get the transition pathways from this method for a single-site spin system, we get a single transition pathway. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python pprint(hahn_echo.get_transition_pathways(spin_system_1)) .. rst-class:: sphx-glr-script-out .. code-block:: none [|0.5⟩⟨-0.5| ⟶ |-0.5⟩⟨0.5|, weight=(1+0j)] .. GENERATED FROM PYTHON SOURCE LINES 95-97 In the case of a homonuclear two-site spin 1/2 spin system, the same method returns four transition pathways. .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: Python pprint(hahn_echo.get_transition_pathways(spin_system_2)) .. rst-class:: sphx-glr-script-out .. code-block:: none [|-0.5, 0.5⟩⟨-0.5, -0.5| ⟶ |0.5, -0.5⟩⟨0.5, 0.5|, weight=(1+0j), |0.5, -0.5⟩⟨-0.5, -0.5| ⟶ |-0.5, 0.5⟩⟨0.5, 0.5|, weight=(1+0j), |0.5, 0.5⟩⟨-0.5, 0.5| ⟶ |-0.5, -0.5⟩⟨0.5, -0.5|, weight=(1+0j), |0.5, 0.5⟩⟨0.5, -0.5| ⟶ |-0.5, -0.5⟩⟨-0.5, 0.5|, weight=(1+0j)] .. GENERATED FROM PYTHON SOURCE LINES 100-102 Create the Simulator object, add the method and spin system objects, and run the simulation. .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: Python sim = Simulator(spin_systems=[spin_system_1, spin_system_2], methods=[hahn_echo]) sim.config.decompose_spectrum = "spin_system" sim.run() .. GENERATED FROM PYTHON SOURCE LINES 107-110 The simulation from each spin system is stored as a dependent variable within the CSDM object. Use the `split` function to split the list of the dependent variables into a list of CSDM objects. .. GENERATED FROM PYTHON SOURCE LINES 110-120 .. code-block:: Python simulation_results = sim.methods[0].simulation.split() # The plot of the two simulations. fig, ax = plt.subplots(1, 2, figsize=(8.0, 3.0), subplot_kw={"projection": "csdm"}) for i in range(2): ax[i].plot(simulation_results[i].real, color="black", linewidth=1) ax[i].invert_xaxis() plt.tight_layout() plt.show() .. image-sg:: /examples/1D_simulation(crystalline)/images/sphx_glr_plot_9_custom_method_Hahnecho_002.png :alt: plot 9 custom method Hahnecho :srcset: /examples/1D_simulation(crystalline)/images/sphx_glr_plot_9_custom_method_Hahnecho_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-126 Notice, in the single-site spin system, the hahn echo refocuses the isotropic chemical shits and chemical shift anisotropies. The end result is a resonance at zero frequency. In the case of the two homonuclear spin 1/2 coupled spin system, the Hahn echo refocuses the isotropic chemical shits and chemical shift anisotropies, but not the dipolar and `J` couplings. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.588 seconds) .. _sphx_glr_download_examples_1D_simulation(crystalline)_plot_9_custom_method_Hahnecho.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_9_custom_method_Hahnecho.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_9_custom_method_Hahnecho.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_