{
  "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# Coesite, 17O (I=5/2)\n\n17O (I=5/2) quadrupolar spectrum simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Coesite is a high-pressure (2-3 GPa) and high-temperature (700\u00b0C) polymorph of silicon\ndioxide $\\text{SiO}_2$. Coesite has five crystallographic $^{17}\\text{O}$\nsites. In the following, we use the $^{17}\\text{O}$ EFG tensor information from\nGrandinetti `et. al.` [#f1]_\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport mrsimulator.signal_processing as sp\nimport mrsimulator.signal_processing.apodization as apo\nfrom mrsimulator import Simulator, SpinSystem, Site\nfrom mrsimulator.methods import BlochDecayCentralTransitionSpectrum\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 1:** Create the sites.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# default unit of isotropic_chemical_shift is ppm and Cq is Hz.\nO17_1 = Site(\n    isotope=\"17O\", isotropic_chemical_shift=29, quadrupolar={\"Cq\": 6.05e6, \"eta\": 0.000}\n)\nO17_2 = Site(\n    isotope=\"17O\", isotropic_chemical_shift=41, quadrupolar={\"Cq\": 5.43e6, \"eta\": 0.166}\n)\nO17_3 = Site(\n    isotope=\"17O\", isotropic_chemical_shift=57, quadrupolar={\"Cq\": 5.45e6, \"eta\": 0.168}\n)\nO17_4 = Site(\n    isotope=\"17O\", isotropic_chemical_shift=53, quadrupolar={\"Cq\": 5.52e6, \"eta\": 0.169}\n)\nO17_5 = Site(\n    isotope=\"17O\", isotropic_chemical_shift=58, quadrupolar={\"Cq\": 5.16e6, \"eta\": 0.292}\n)\n\n# all five sites.\nsites = [O17_1, O17_2, O17_3, O17_4, O17_5]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 2:** Create the spin systems from these sites. For optimum performance, we\ncreate five single-site spin systems instead of a single five-site spin system. The\nabundance of each spin system is taken from above reference.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "abundance = [0.83, 1.05, 2.16, 2.05, 1.90]\nspin_systems = [SpinSystem(sites=[s], abundance=a) for s, a in zip(sites, abundance)]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 3:** Create a central transition selective Bloch decay spectrum method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method = BlochDecayCentralTransitionSpectrum(\n    channels=[\"17O\"],\n    rotor_frequency=14000,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 2048,\n            \"spectral_width\": 50000,  # in Hz\n            \"label\": r\"$^{17}$O resonances\",\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The above method is set up to record the $^{17}\\text{O}$ resonances at the\nmagic angle, spinning at 14 kHz and 9.4 T (default, if the value is not provided)\nexternal magnetic flux density. The resonances are recorded over 50 kHz spectral\nwidth using 2048 points.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 4:** Create the Simulator object and add the method and spin system objects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\nsim.spin_systems = spin_systems  # add the spin systems\nsim.methods = [method]  # add the method"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 5:** 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": [
        "**Step 6:** Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[\n        sp.IFFT(),\n        apo.Exponential(FWHM=\"30 Hz\"),\n        apo.Gaussian(FWHM=\"145 Hz\"),\n        sp.FFT(),\n    ]\n)\nprocessed_data = processor.apply_operations(data=sim.methods[0].simulation)\n\n# The plot of the simulation after signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.plot(processed_data.real, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Grandinetti, P. J., Baltisberger, J. H., Farnan, I., Stebbins, J. F.,\n      Werner, U. and Pines, A.\n      Solid-State $^{17}\\text{O}$ Magic-Angle and Dynamic-Angle Spinning NMR\n      Study of the $\\text{SiO}_2$ Polymorph Coesite, J. Phys. Chem. 1995,\n      **99**, *32*, 12341-12348.\n      `DOI: 10.1021/j100032a045 <https://doi.org/10.1021/j100032a045>`_\n\n"
      ]
    }
  ],
  "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
}