{
  "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# Rb2CrO4, 87Rb (I=3/2) COASTER\n\n87Rb (I=3/2) Correlation of anisotropies separated through echo refocusing (COASTER)\nsimulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is a correlation of anisotropies separated through echo refocusing\n(COASTER) simulation of $\\text{Rb}_2\\text{CrO}_4$. The Rb site with the smaller\nquadrupolar interaction is selectively observed and reported by Ash `et. al.` [#f1]_.\nThe following is the simulation based on the published tensor parameters.\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 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": [
        "Generate the site and spin system objects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "site = Site(\n    isotope=\"87Rb\",\n    isotropic_chemical_shift=-9,  # in ppm\n    shielding_symmetric={\"zeta\": 110, \"eta\": 0},\n    quadrupolar={\n        \"Cq\": 3.5e6,  # in Hz\n        \"eta\": 0.36,\n        \"alpha\": 0,  # in rads\n        \"beta\": 70 * 3.14159 / 180,  # in rads\n        \"gamma\": 0,  # in rads\n    },\n)\nspin_system = SpinSystem(sites=[site])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the generic 2D method, `Method2D`, to simulate a COASTER spectrum by customizing\nthe method 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": [
        "coaster = Method2D(\n    channels=[\"87Rb\"],\n    magnetic_flux_density=9.4,  # in T\n    rotor_angle=70.12 * 3.14159 / 180,  # in rads\n    spectral_dimensions=[\n        {\n            \"count\": 256,\n            \"spectral_width\": 4e4,  # in Hz\n            \"reference_offset\": -8e3,  # in Hz\n            \"label\": \"3Q dimension\",\n            \"events\": [{\"transition_query\": {\"P\": [3], \"D\": [0]}}],\n        },\n        # The last spectral dimension block is the direct-dimension\n        {\n            \"count\": 256,\n            \"spectral_width\": 2e4,  # in Hz\n            \"reference_offset\": -3e3,  # in Hz\n            \"label\": \"70.12 dimension\",\n            \"events\": [{\"transition_query\": {\"P\": [-1], \"D\": [0]}}],\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_system]  # add the spin systems\nsim.methods = [coaster]  # add the method.\n\n# configure the simulator object. For non-coincidental tensors, set the value of the\n# `integration_volume` attribute to `hemisphere`.\nsim.config.integration_volume = \"hemisphere\"\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.3 kHz\", dim_index=0),\n        apo.Gaussian(FWHM=\"0.3 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] Jason T. Ash, Nicole M. Trease, and Philip J. Grandinetti. Separating\n      Chemical Shift and Quadrupolar Anisotropies via Multiple-Quantum NMR\n      Spectroscopy, J. Am. Chem. Soc. (2008) **130**, 10858-10859.\n      `DOI: 10.1021/ja802865x <https://doi.org/10.1021/ja802865x>`_\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
}