{
  "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# Albite, 27Al (I=5/2) 3QMAS\n\n27Al (I=5/2) triple-quantum magic-angle spinning (3Q-MAS) simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is an example of $^{27}\\text{Al}$ 3QMAS simulation of albite\n$\\text{NaSi}_3\\text{AlO}_8$. The $^{27}\\text{Al}$ tensor parameters were\nobtained from Massiot `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 ThreeQ_VAS\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": [
        "Generate the site and spin system objects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "site = Site(\n    isotope=\"27Al\",\n    isotropic_chemical_shift=64.7,  # in ppm\n    quadrupolar={\"Cq\": 3.25e6, \"eta\": 0.68},  # Cq is in Hz\n)\n\nspin_systems = [SpinSystem(sites=[site])]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Select a Triple Quantum variable-angle spinning method. You may optionally\nprovide a `rotor_angle` to the method. The default `rotor_angle` is the magic-angle.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method = ThreeQ_VAS(\n    channels=[\"27Al\"],\n    magnetic_flux_density=7,  # in T\n    spectral_dimensions=[\n        {\n            \"count\": 256,\n            \"spectral_width\": 1e4,  # in Hz\n            \"reference_offset\": -3e3,  # in Hz\n            \"label\": \"Isotropic dimension\",\n        },\n        {\n            \"count\": 512,\n            \"spectral_width\": 1e4,  # in Hz\n            \"reference_offset\": 4e3,  # in Hz\n            \"label\": \"MAS dimension\",\n        },\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object, add the method and spin system objects, and\nrun the simulation.\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.\nsim.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.2 kHz\", dim_index=0),\n        apo.Gaussian(FWHM=\"0.2 kHz\", dim_index=1),\n        sp.FFT(dim_index=(0, 1)),\n    ]\n)\nprocessed_data = processor.apply_operations(data=sim.methods[0].simulation)\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.set_xlim(75, 25)\nax.set_ylim(-15, -65)\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Massiot, D., Touzoa, B., Trumeaua, D., Coutures, J.P., Virlet, J., Florian,\n      P., Grandinetti, P.J. Two-dimensional magic-angle spinning isotropic\n      reconstruction sequences for quadrupolar nuclei, ssnmr, (1996), **6**, *1*,\n      73-83. `DOI: 10.1016/0926-2040(95)01210-9\n      <https://doi.org/10.1016/0926-2040(95)01210-9>`_\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
}