{
  "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 (Shielding and Quadrupolar)\n\nIn this example, we illustrate the simulation of spectrum originating from a Czjzek\ndistribution of traceless symmetric tensors. We show two cases, the Czjzek distribution\nof the shielding and quadrupolar tensor parameters, respectively.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import the required modules.\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 BlochDecaySpectrum, BlochDecayCentralTransitionSpectrum\nfrom mrsimulator.models import CzjzekDistribution\nfrom mrsimulator.utils.collection import single_site_system_generator\n\n# pre config the figures\nmpl.rcParams[\"figure.figsize\"] = [4.25, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Symmetric shielding tensor\n\n### Create the Czjzek distribution\n\nFirst, create a distribution of the zeta and eta parameters of the shielding tensors\nusing the `czjzek_distribution` model as follows.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The range of zeta and eta coordinates over which the distribution is sampled.\nz_range = np.arange(100) - 50  # in ppm\ne_range = np.arange(21) / 20\nz_dist, e_dist, amp = CzjzekDistribution(sigma=3.1415).pdf(pos=[z_range, e_range])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here ``z_range`` and ``e_range`` are the coordinates along the $\\zeta$ and\n$\\eta$ dimensions that form a two-dimensional $\\zeta$-$\\eta$ grid.\nThe argument `sigma` of the CzjzekDistribution class is the standard deviation of the\nsecond-rank tensor parameters used in generating the distribution, and `pos` hold the\none-dimensional arrays of $\\zeta$ and $\\eta$ coordinates, respectively.\n\nThe following is the contour plot of the Czjzek distribution.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.contourf(z_dist, e_dist, amp, levels=10)\nplt.xlabel(r\"$\\zeta$ / ppm\")\nplt.ylabel(r\"$\\eta$\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Simulate the spectrum\n\nTo quickly generate single-site spin systems from the above $\\zeta$ and\n$\\eta$ parameters, use the\n:func:`~mrsimulator.utils.collection.single_site_system_generator` utility function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "systems = single_site_system_generator(\n    isotopes=\"13C\", shielding_symmetric={\"zeta\": z_dist, \"eta\": e_dist}, abundance=amp\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here, the variable ``systems`` hold an array of single-site spin systems.\nNext, create a simulator object and add the above system and a method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\nsim.spin_systems = systems  # add the systems\nsim.methods = [BlochDecaySpectrum(channels=[\"13C\"])]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the static spectrum arising from a Czjzek distribution of the\nsecond-rank traceless shielding tensors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(4.5, 3.0))\nax = plt.gca(projection=\"csdm\")\nax.plot(sim.methods[0].simulation, color=\"black\", linewidth=1)\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Quadrupolar tensor\n\n### Create the Czjzek distribution\n\nSimilarly, you may also create a Czjzek distribution of the electric field gradient\n(EFG) tensor parameters.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The range of Cq and eta coordinates over which the distribution is sampled.\ncq_range = np.arange(100) * 0.6 - 30  # in MHz\ne_range = np.arange(21) / 20\ncq_dist, e_dist, amp = CzjzekDistribution(sigma=2.3).pdf(pos=[cq_range, e_range])\n\n# The following is the contour plot of the Czjzek distribution.\nplt.contourf(cq_dist, e_dist, amp, levels=10)\nplt.xlabel(r\"Cq / MHz\")\nplt.ylabel(r\"$\\eta$\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Simulate the spectrum\n\nCreate the spin systems.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "systems = single_site_system_generator(\n    isotopes=\"71Ga\", quadrupolar={\"Cq\": cq_dist * 1e6, \"eta\": e_dist}, abundance=amp\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a simulator object and add the above system.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\nsim.spin_systems = systems  # add the systems\nsim.methods = [\n    BlochDecayCentralTransitionSpectrum(\n        channels=[\"71Ga\"],\n        magnetic_flux_density=4.8,  # in T\n        spectral_dimensions=[{\"count\": 2048, \"spectral_width\": 1.2e6}],\n    )\n]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the static spectrum arising from a Czjzek distribution of the\nsecond-rank traceless EFG tensors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(4.25, 3.0))\nax = plt.gca(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
}