{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This cell is added by sphinx-gallery\n\n%matplotlib inline\n\nimport mrsimulator\nprint(f'You are using mrsimulator v{mrsimulator.__version__}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Simulate arbitrary transitions (single-quantum)\n\n27Al (I=5/2) quadrupolar spectrum simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The mrsimulator built-in one-dimensional methods, BlochDecaySpectrum and\nBlochDecayCentralTransitionSpectrum, are designed to simulate spectrum from all single\nquantum transitions or central transition selective transition, respectively. In this\nexample, we show how you can simulate any arbitrary transition using the generic\nMethod1D method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom mrsimulator import Simulator, SpinSystem, Site\nfrom mrsimulator.methods import Method1D\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a single-site arbitrary spin system.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "site = Site(\n    name=\"27Al\",\n    isotope=\"27Al\",\n    isotropic_chemical_shift=35.7,  # in ppm\n    quadrupolar={\"Cq\": 5.959e6, \"eta\": 0.32},  # Cq is in Hz\n)\nspin_system = SpinSystem(sites=[site])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Selecting spin transitions for simulation\n\nThe arguments of the Method1D object are the same as the arguments of the\nBlochDecaySpectrum method; however, unlike a BlochDecaySpectrum method, the\n`spectral_dim_api` object in Method1D contains additional argument---`events`.\n\nThe `event_api` object is a collection of attributes, which are local to the\nevent. It is here where we define a `transition_query` to select one or more\ntransitions for simulating the spectrum. The two attributes of the `transition_query`\nare `P` and `D`, which are given as,\n\n\\begin{align}P = m_f - m_i \\\\\n      D = m_f^2 - m_i^2,\\end{align}\n\nwhere $m_f$ and $m_i$ are the spin quantum numbers for the final and\ninitial energy states. Based on the query, the method selects all transitions from\nthe spin system that satisfy the query selection criterion. For example, to simulate\na spectrum for the satellite transition, $|-1/2\\rangle\\rightarrow|-3/2\\rangle$,\nset the value of\n\n\\begin{align}P &= (-3/2) - (-1/2) = -1 \\\\\n      D &= (9/4) - (1/4) = 2.\\end{align}\n\nFor illustrative purposes, let's look at the infinite speed spectrum from this\nsatellite transition.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method = Method1D(\n    channels=[\"27Al\"],\n    magnetic_flux_density=21.14,  # in T\n    rotor_frequency=1e9,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 1024,\n            \"spectral_width\": 1e4,  # in Hz\n            \"reference_offset\": 1e4,  # in Hz\n            \"events\": [\n                {\"transition_query\": {\"P\": [-1], \"D\": [2]}}  # <-- select transitions\n            ],\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object and add the method and the spin system object.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\nsim.spin_systems += [spin_system]  # add the spin system\nsim.methods += [method]  # add the method"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate the spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.run()\n\n# The plot of the simulation before signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.plot(sim.methods[0].simulation.real, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Selecting both inner and outer-satellite transitions\nYou may use the same transition query selection criterion to select multiple\ntransitions. Consider the following transitions with respective P and D values.\n\n- $|-1/2\\rangle\\rightarrow|-3/2\\rangle$ ($P=-1, D=2$)\n- $|-3/2\\rangle\\rightarrow|-5/2\\rangle$ ($P=-1, D=4$)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method2 = Method1D(\n    channels=[\"27Al\"],\n    magnetic_flux_density=21.14,  # in T\n    rotor_frequency=1e9,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 1024,\n            \"spectral_width\": 1e4,  # in Hz\n            \"reference_offset\": 1e4,  # in Hz\n            \"events\": [\n                {\"transition_query\": {\"P\": [-1], \"D\": [2, 4]}}  # <-- select transitions\n            ],\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Update the method object in the Simulator object.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.methods[0] = method2  # add the method"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate the spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.run()\n\n# The plot of the simulation before signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.plot(sim.methods[0].simulation.real, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}