{
  "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# Wollastonite, 29Si (I=1/2)\n\n29Si (I=1/2) spinning sideband simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Wollastonite is a high-temperature calcium-silicate,\n$\\beta\u2212\\text{Ca}_3\\text{Si}_3\\text{O}_9$, with three distinct\n$^{29}\\text{Si}$ sites. The $^{29}\\text{Si}$ tensor parameters\nwere obtained from Hansen `et. al.` [#f1]_\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 BlochDecaySpectrum\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 1:** Create the sites.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "S29_1 = Site(\n    isotope=\"29Si\",\n    isotropic_chemical_shift=-89.0,  # in ppm\n    shielding_symmetric={\"zeta\": 59.8, \"eta\": 0.62},  # zeta in ppm\n)\nS29_2 = Site(\n    isotope=\"29Si\",\n    isotropic_chemical_shift=-89.5,  # in ppm\n    shielding_symmetric={\"zeta\": 52.1, \"eta\": 0.68},  # zeta in ppm\n)\nS29_3 = Site(\n    isotope=\"29Si\",\n    isotropic_chemical_shift=-87.8,  # in ppm\n    shielding_symmetric={\"zeta\": 69.4, \"eta\": 0.60},  # zeta in ppm\n)\n\nsites = [S29_1, S29_2, S29_3]  # all sites"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 2:** Create the spin systems from these sites. Again, we create three\nsingle-site spin systems for better performance.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spin_systems = [SpinSystem(sites=[s]) for s in sites]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 3:** Create a Bloch decay spectrum method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method = BlochDecaySpectrum(\n    channels=[\"29Si\"],\n    magnetic_flux_density=14.1,  # in T\n    rotor_frequency=1500,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 2048,\n            \"spectral_width\": 25000,  # in Hz\n            \"reference_offset\": -10000,  # in Hz\n            \"label\": r\"$^{29}$Si resonances\",\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 4:** Create the Simulator object and add the method and spin system objects.\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 += [method]  # add the method"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 5:** Simulate the spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.run()\n\n# The plot of the simulation before signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.plot(sim.methods[0].simulation.real, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Step 6:** Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[sp.IFFT(), apo.Exponential(FWHM=\"70 Hz\"), sp.FFT()]\n)\nprocessed_data = processor.apply_operations(data=sim.methods[0].simulation)\n\n# The plot of the simulation after signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.plot(processed_data.real, color=\"black\", linewidth=1)\nax.invert_xaxis()\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Hansen, M. R., Jakobsen, H. J., Skibsted, J., $^{29}\\text{Si}$\n      Chemical Shift Anisotropies in Calcium Silicates from High-Field\n      $^{29}\\text{Si}$ MAS NMR Spectroscopy, Inorg. Chem. 2003,\n      **42**, *7*, 2368-2377.\n      `DOI: 10.1021/ic020647f <https://doi.org/10.1021/ic020647f>`_\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
}