{
  "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) DAS\n\n17O (I=5/2) Dynamic-angle spinning (DAS) simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is a dynamic angle spinning (DAS) simulation of Coesite. Coesite has\nfive crystallographic $^{17}\\text{O}$ sites. In the following, we use the\n$^{17}\\text{O}$ EFG tensor information from Grandinetti `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\nfrom mrsimulator.methods import Method2D\n\n# global plot configuration\nfont = {\"size\": 9}\nmpl.rc(\"font\", **font)\nmpl.rcParams[\"figure.figsize\"] = [4.25, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object and load the spin systems database or url address.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\n\n# load the spin systems from url.\nfilename = \"https://sandbox.zenodo.org/record/687656/files/coesite.mrsys\"\nsim.load_spin_systems(filename)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the generic 2D method, `Method2D`, to simulate a DAS spectrum by customizing the\nmethod parameters, as shown below. Note, the Method2D method simulates an infinite\nspinning speed spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "das = Method2D(\n    channels=[\"17O\"],\n    magnetic_flux_density=11.7,  # in T\n    spectral_dimensions=[\n        {\n            \"count\": 256,\n            \"spectral_width\": 5e3,  # in Hz\n            \"reference_offset\": 0,  # in Hz\n            \"label\": \"DAS isotropic dimension\",\n            \"events\": [\n                {\"fraction\": 0.5, \"rotor_angle\": 37.38 * 3.14159 / 180},\n                {\"fraction\": 0.5, \"rotor_angle\": 79.19 * 3.14159 / 180},\n            ],\n        },\n        # The last spectral dimension block is the direct-dimension\n        {\n            \"count\": 256,\n            \"spectral_width\": 2e4,  # in Hz\n            \"reference_offset\": 0,  # in Hz\n            \"label\": \"MAS dimension\",\n            \"events\": [{\"rotor_angle\": 54.735 * 3.14159 / 180}],\n        },\n    ],\n)\nsim.methods = [das]  # add the method."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the simulation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = sim.methods[0].simulation\nax = plt.subplot(projection=\"csdm\")\ncb = ax.imshow(data / data.max(), aspect=\"auto\", cmap=\"gist_ncar_r\")\nplt.colorbar(cb)\nax.invert_xaxis()\nax.invert_yaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[\n        # Gaussian convolution along both dimensions.\n        sp.IFFT(dim_index=(0, 1)),\n        apo.Gaussian(FWHM=\"0.3 kHz\", dim_index=0),\n        apo.Gaussian(FWHM=\"0.15 kHz\", dim_index=1),\n        sp.FFT(dim_index=(0, 1)),\n    ]\n)\nprocessed_data = processor.apply_operations(data=data)\nprocessed_data /= processed_data.max()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation after signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\ncb = ax.imshow(processed_data.real, cmap=\"gist_ncar_r\", aspect=\"auto\")\nplt.colorbar(cb)\nax.invert_xaxis()\nax.invert_yaxis()\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
}