{
  "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), MAF\n\n29Si (I=1/2) magic angle flipping.\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 Method2D\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the sites and spin systems\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sites = [\n    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    ),\n    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    ),\n    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]\n\nspin_systems = [SpinSystem(sites=[s]) for s in sites]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the generic 2D method, `Method2D`, to simulate a MAF spectrum by customizing the\nmethod parameters, as shown below. Note, the Method2D method simulates an infinite\nspinning speed spectrum.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "maf = Method2D(\n    channels=[\"29Si\"],\n    magnetic_flux_density=14.1,  # in T\n    spectral_dimensions=[\n        {\n            \"count\": 128,\n            \"spectral_width\": 2e4,  # in Hz\n            \"label\": \"Anisotropic dimension\",\n            \"events\": [{\"rotor_angle\": 90 * 3.14159 / 180}],\n        },\n        {\n            \"count\": 128,\n            \"spectral_width\": 3e3,  # in Hz\n            \"reference_offset\": -1.05e4,  # in Hz\n            \"label\": \"Isotropic dimension\",\n            \"events\": [{\"rotor_angle\": 54.735 * 3.14159 / 180}],\n        },\n    ],\n    affine_matrix=[[1, -1], [0, 1]],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object, add the method and spin system objects, and run the\nsimulation.\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 = [maf]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "csdm_data = sim.methods[0].simulation\nprocessor = sp.SignalProcessor(\n    operations=[\n        sp.IFFT(dim_index=(0, 1)),\n        apo.Gaussian(FWHM=\"50 Hz\", dim_index=0),\n        apo.Gaussian(FWHM=\"50 Hz\", dim_index=1),\n        sp.FFT(dim_index=(0, 1)),\n    ]\n)\nprocessed_data = processor.apply_operations(data=csdm_data).real\nprocessed_data /= processed_data.max()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation after signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\ncb = ax.imshow(processed_data.T, aspect=\"auto\", cmap=\"gist_ncar_r\")\nplt.colorbar(cb)\nax.invert_xaxis()\nax.invert_yaxis()\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
}