{
  "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# Czjzek distribution, 27Al (I=5/2) 3QMAS\n\n27Al (I=5/2) 3QMAS simulation of amorphous material.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this section, we illustrate the simulation of a quadrupolar MQMAS spectrum arising\nfrom a distribution of the electric field gradient (EFG) tensors from amorphous\nmaterial. We proceed by employing the Czjzek distribution model.\n\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 ThreeQ_VAS\nfrom mrsimulator.models import CzjzekDistribution\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": [
        "## Generate probability distribution\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The range of isotropic chemical shifts, the quadrupolar coupling constant, and\n# asymmetry parameters used in generating a 3D grid.\niso_r = np.arange(101) / 1.5 + 30  # in ppm\nCq_r = np.arange(100) / 4  # in MHz\neta_r = np.arange(10) / 9\n\n# The 3D mesh grid over which the distribution amplitudes are evaluated.\niso, Cq, eta = np.meshgrid(iso_r, Cq_r, eta_r, indexing=\"ij\")\n\n# The 2D amplitude grid of Cq and eta is sampled from the Czjzek distribution model.\nCq_dist, e_dist, amp = CzjzekDistribution(sigma=1).pdf(pos=[Cq_r, eta_r])\n\n# The 1D amplitude grid of isotropic chemical shifts is sampled from a Gaussian model.\niso_amp = multivariate_normal(mean=58, cov=[4]).pdf(iso_r)\n\n# The 3D amplitude grid is generated as an uncorrelated distribution of the above two\n# distribution, which is the product of the two distributions.\npdf = np.repeat(amp, iso_r.size).reshape(eta_r.size, Cq_r.size, iso_r.size)\npdf *= iso_amp\npdf = pdf.T"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The 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_r, iso_r, 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_r, iso_r, 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_r, Cq_r, 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": [
        "## Simulation setup\nLet's create the site and spin system objects from these parameters. Use the\n:func:`~mrsimulator.utils.collection.single_site_system_generator` utility function to\ngenerate 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)\nlen(spin_systems)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate a $^{27}\\text{Al}$ 3Q-MAS spectrum by using the `ThreeQ_MAS` method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mqvas = ThreeQ_VAS(\n    channels=[\"27Al\"],\n    spectral_dimensions=[\n        {\n            \"count\": 512,\n            \"spectral_width\": 26718.475776,  # in Hz\n            \"reference_offset\": -4174.76184,  # in Hz\n            \"label\": \"Isotropic dimension\",\n        },\n        {\n            \"count\": 512,\n            \"spectral_width\": 2e4,  # in Hz\n            \"reference_offset\": 2e3,  # in Hz\n            \"label\": \"MAS dimension\",\n        },\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the simulator object, add the spin systems and method, and run 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 = [mqvas]  # add the method\nsim.config.number_of_sidebands = 1\nsim.run()\n\ndata = sim.methods[0].simulation"
      ]
    },
    {
      "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\")\ncb = ax.imshow(data / data.max(), cmap=\"gist_ncar_r\", aspect=\"auto\")\nplt.colorbar(cb)\nax.set_ylim(-20, -50)\nax.set_xlim(80, 20)\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
}