{
  "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# Extended Czjzek distribution (Shielding and Quadrupolar)\n\nIn this example, we illustrate the simulation of spectrum originating from an\nextended Czjzek distribution of traceless symmetric tensors. We show two cases, an\nextended Czjzek distribution of the shielding and quadrupolar tensor parameters,\nrespectively.\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 ExtCzjzekDistribution\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 extended Czjzek distribution\n\nFirst, create a distribution of the zeta and eta parameters of the shielding tensors\nusing the `extended_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_lim = np.arange(100) * 0.4 + 40  # in ppm\ne_lim = np.arange(21) / 20\n\ndominant = {\"zeta\": 60, \"eta\": 0.3}\nz_dist, e_dist, amp = ExtCzjzekDistribution(dominant, eps=0.14).pdf(pos=[z_lim, e_lim])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the plot of the extended 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\nCreate the spin systems from the above $\\zeta$ and $\\eta$ parameters.\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)\nprint(len(systems))"
      ]
    },
    {
      "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 = [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.25, 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 extended Czjzek distribution\n\nSimilarly, you may also create an extended Czjzek distribution of the electric field\ngradient (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_lim = np.arange(100) * 0.1  # assumed in MHz\ne_lim = np.arange(21) / 20\n\ndominant = {\"Cq\": 6.1, \"eta\": 0.1}\ncq_dist, e_dist, amp = ExtCzjzekDistribution(dominant, eps=0.25).pdf(\n    pos=[cq_lim, e_lim]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the plot of the extended Czjzek distribution.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.contourf(cq_dist, e_dist, amp, levels=10)\nplt.xlabel(r\"$C_q$ / MHz\")\nplt.ylabel(r\"$\\eta$\")\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Simulate the spectrum\n**Static spectrum**\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=9.4,  # in T\n        spectral_dimensions=[{\"count\": 2048, \"spectral_width\": 2e5}],\n    )\n]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is a static spectrum arising from an extended Czjzek distribution of\nthe second-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()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**MAS spectrum**\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.methods = [\n    BlochDecayCentralTransitionSpectrum(\n        channels=[\"71Ga\"],\n        magnetic_flux_density=9.4,  # in T\n        rotor_frequency=25000,  # in Hz\n        spectral_dimensions=[\n            {\"count\": 2048, \"spectral_width\": 2e5, \"reference_offset\": -1e4}\n        ],\n    )\n]  # add the method\nsim.config.number_of_sidebands = 16\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the MAS spectrum arising from an extended 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
}