{
  "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# Potassium Sulfate, 33S (I=3/2)\n\n33S (I=3/2) quadrupolar spectrum simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following example is the $^{33}\\text{S}$ NMR spectrum simulation of\npotassium sulfate ($\\text{K}_2\\text{SO}_4$). The quadrupole tensor parameters\nfor $^{33}\\text{S}$ is obtained from Moudrakovski `et. al.` [#f3]_\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 spin system\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "site = Site(\n    name=\"33S\",\n    isotope=\"33S\",\n    isotropic_chemical_shift=335.7,  # in ppm\n    quadrupolar={\"Cq\": 0.959e6, \"eta\": 0.42},  # Cq is in Hz\n)\nspin_system = SpinSystem(sites=[site])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 2:** 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=[\"33S\"],\n    magnetic_flux_density=21.14,  # in T\n    rotor_frequency=14000,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 2048,\n            \"spectral_width\": 5000,  # in Hz\n            \"reference_offset\": 22500,  # in Hz\n            \"label\": r\"$^{33}$S resonances\",\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 3:** Create the Simulator object and add method and spin system objects.\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": [
        "**Step 4:** 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 5:** Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[sp.IFFT(), apo.Exponential(FWHM=\"10 Hz\"), sp.FFT()]\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": [
        ".. [#f3] Moudrakovski, I., Lang, S., Patchkovskii, S., and Ripmeester, J. High field\n      $^{33}\\text{S}$ solid state NMR and first-principles calculations in\n      potassium sulfates. J. Phys. Chem. A, 2010, **114**, *1*, 309\u2013316.\n      `DOI: 10.1021/jp908206c <https://doi.org/10.1021/jp908206c>`_\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
}