{
  "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# Amorphous material, 27Al (I=5/2)\n\n27Al (I=5/2) simulation of amorphous material.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom mrsimulator import Simulator\nfrom mrsimulator.methods import BlochDecayCentralTransitionSpectrum\nfrom mrsimulator.utils.collection import single_site_system_generator\nfrom scipy.stats import multivariate_normal\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this section, we illustrate the simulation of a quadrupolar spectrum arising from\na distribution of the electric field gradient (EFG) tensors from an amorphous\nmaterial. We proceed by assuming a multi-variate normal distribution, as follows,\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mean = [20, 6.5, 0.3]  # given as [isotropic chemical shift in ppm, Cq in MHz, eta].\ncovariance = [[1.98, 0, 0], [0, 4.9, 0], [0, 0, 0.0016]]  # same order as the mean.\n\n# range of coordinates along the three dimensions\niso_range = np.arange(40)  # in ppm\nCq_range = np.arange(80) / 3 - 5  # in MHz\neta_range = np.arange(21) / 20\n\n# The coordinates grid\niso, Cq, eta = np.meshgrid(iso_range, Cq_range, eta_range, indexing=\"ij\")\npos = np.asarray([iso, Cq, eta]).T\n\n# Three-dimensional probability distribution function.\npdf = multivariate_normal(mean=mean, cov=covariance).pdf(pos).T"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here, ``iso``, ``Cq``, and ``eta`` are the isotropic chemical shift, the quadrupolar\ncoupling constant, and quadrupolar asymmetry coordinates of the 3D-grid\nsystem over which the multivariate normal probability distribution is evaluated. The\nmean of the distribution is given by the variable ``mean`` and holds a value of 20\nppm, 6.5 MHz, and 0.3 for the isotropic chemical shift, the quadrupolar coupling\nconstant, and quadrupolar asymmetry parameter, respectively. Similarly, the variable\n``covariance`` holds the covariance matrix of the multivariate normal distribution.\nThe two-dimensional projections from this three-dimensional distribution are shown\nbelow.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "_, ax = plt.subplots(1, 3, figsize=(9, 3))\n\n# isotropic shift v.s. quadrupolar coupling constant\nax[0].contourf(Cq_range, iso_range, pdf.sum(axis=2))\nax[0].set_xlabel(\"Cq / MHz\")\nax[0].set_ylabel(\"isotropic chemical shift / ppm\")\n\n# isotropic shift v.s. quadrupolar asymmetry\nax[1].contourf(eta_range, iso_range, pdf.sum(axis=1))\nax[1].set_xlabel(r\"quadrupolar asymmetry, $\\eta$\")\nax[1].set_ylabel(\"isotropic chemical shift / ppm\")\n\n# quadrupolar coupling constant v.s. quadrupolar asymmetry\nax[2].contourf(eta_range, Cq_range, pdf.sum(axis=0))\nax[2].set_xlabel(r\"quadrupolar asymmetry, $\\eta$\")\nax[2].set_ylabel(\"Cq / MHz\")\n\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's create the site and spin system objects from these parameters. Note, we create\nsingle-site spin systems for optimum performance.\nUse the :func:`~mrsimulator.utils.collection.single_site_system_generator` utility\nfunction to generate single-site spin systems.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spin_systems = single_site_system_generator(\n    isotopes=\"27Al\",\n    isotropic_chemical_shifts=iso,\n    quadrupolar={\"Cq\": Cq * 1e6, \"eta\": eta},  # Cq in Hz\n    abundance=pdf,\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Static spectrum\nObserve the static $^{27}\\text{Al}$ NMR spectrum simulation. First,\ncreate a central transition selective Bloch decay spectrum method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "static_method = BlochDecayCentralTransitionSpectrum(\n    channels=[\"27Al\"], spectral_dimensions=[{\"spectral_width\": 80000}]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the simulator object and add the spin systems and method.\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 = [static_method]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the corresponding spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\nax.plot(sim.methods[0].simulation, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Spinning sideband simulation at the magic angle\nSimulation of the same spin systems at the magic angle and spinning at 25 kHz.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "MAS_method = BlochDecayCentralTransitionSpectrum(\n    channels=[\"27Al\"],\n    rotor_frequency=25000,  # in Hz\n    rotor_angle=54.735 * np.pi / 180.0,  # in rads\n    spectral_dimensions=[\n        {\"spectral_width\": 30000, \"reference_offset\": -4000}  # values in Hz\n    ],\n)\nsim.methods[0] = MAS_method"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Configure the sim object to calculate up to 4 sidebands, and run the simulation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.config.number_of_sidebands = 4\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "and the corresponding plot.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\nax.plot(sim.methods[0].simulation, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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
}