{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib notebook"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contributions of up- and downgoing TM- and TE-modes\n\nThis is an example for the add-on ``tmtemod``. The example is taken from the\nCSEM-book by Ziolkowski and Slob, 2019. Have a look at the CSEM-book repository\non `empymod/csem-ziolkowski-and-slob\n<https://github.com/emsig/csem-ziolkowski-and-slob>`_ for many more examples.\n\n**Reference**\n\n- Ziolkowski, A., and E. Slob, 2019, **Introduction to Controlled-Source\n  Electromagnetic Methods**: Cambridge University Press; ISBN `9781107058620\n  <https://www.cambridge.org/9781107058620>`_.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import empymod\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Model parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Offsets\nx = np.linspace(10, 1.5e4, 128)\n\n# Resistivity model\nrtg = [2e14, 1/3, 1, 70, 1]  # With target\nrhs = [2e14, 1/3, 1, 1, 1]   # Half-space\n\n# Common model parameters (deep sea parameters)\nmodel = {\n    'src': [0, 0, 975],              # Source location\n    'rec': [x, x*0, 1000],           # Receiver location\n    'depth': [0, 1000, 2000, 2040],  # 1 km water, target 40 m thick 1 km below\n    'freqtime': 0.5,                 # Frequencies\n    'verb': 1,                       # Verbosity\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Computation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "target = empymod.dipole(res=rtg, **model)\ntgTM, tgTE = empymod.tmtemod.dipole(res=rtg, **model)\n\n# Without reservoir\nnotarg = empymod.dipole(res=rhs, **model)\nntTM, ntTE = empymod.tmtemod.dipole(res=rhs, **model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Figure 1\n\nPlot all reflected contributions (without direct field), for the models with\nand without a reservoir.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(10, 4))\n\n# 1st subplot\nax1 = plt.subplot(121)\nplt.semilogy(x/1000, np.abs(tgTE[0]), 'C0-.')\nplt.semilogy(x/1000, np.abs(ntTE[0]), 'k-.', label='TE$^{--}$')\nplt.semilogy(x/1000, np.abs(tgTE[2]), 'C0-')\nplt.semilogy(x/1000, np.abs(ntTE[2]), 'k-', label='TE$^{+-}$')\nplt.semilogy(x/1000, np.abs(tgTE[1]), 'C0--')\nplt.semilogy(x/1000, np.abs(ntTE[1]), 'k--', label='TE$^{-+}$')\nplt.semilogy(x/1000, np.abs(tgTE[3]), 'C0:')\nplt.semilogy(x/1000, np.abs(ntTE[3]), 'k:', label='TE$^{++}$')\nplt.semilogy(-1, 1, 'k-', label='No target')  # Dummy entries for labels\nplt.semilogy(-1, 1, 'C0-', label='Target')     # \"\nplt.legend()\nplt.xlabel('Offset (km)')\nplt.ylabel('Electric field amplitude (V/m)')\nplt.xlim([0, 15])\n\n# 2nd subplot\nplt.subplot(122, sharey=ax1)\nplt.semilogy(x/1000, np.abs(tgTM[0]), 'C0-.')\nplt.semilogy(x/1000, np.abs(ntTM[0]), 'k-.', label='TM$^{--}$')\nplt.semilogy(x/1000, np.abs(tgTM[2]), 'C0-')\nplt.semilogy(x/1000, np.abs(ntTM[2]), 'k-', label='TM$^{+-}$')\nplt.semilogy(x/1000, np.abs(tgTM[1]), 'C0--')\nplt.semilogy(x/1000, np.abs(ntTM[1]), 'k--', label='TM$^{-+}$')\nplt.semilogy(x/1000, np.abs(tgTM[3]), 'C0:')\nplt.semilogy(x/1000, np.abs(ntTM[3]), 'k:', label='TM$^{++}$')\nplt.semilogy(-1, 1, 'k-', label='No target')  # Dummy entries for labels\nplt.semilogy(-1, 1, 'C0-', label='Target')     # \"\nplt.legend()\nplt.xlabel('Offset (km)')\nplt.ylim([1e-17, 1e-9])\nplt.xlim([0, 15])\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The result shows that mainly the TM-mode contributions are sensitive to the\nreservoir. For TM, all modes contribute significantly except $T^{+-}$, which\nis the field that travels upwards from the source and downwards to the\nreceiver.\n\n### Figure 2\n\nFinally we check if the result from ``empymod.dipole`` equals the sum of the\noutput of ``empymod.tmtemod.dipole``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure()\n\nnt = ntTM[0]+ntTM[1]+ntTM[2]+ntTM[3]+ntTM[4]\nnt += ntTE[0]+ntTE[1]+ntTE[2]+ntTE[3]+ntTE[4]\ntg = tgTM[0]+tgTM[1]+tgTM[2]+tgTM[3]+tgTM[4]\ntg += tgTE[0]+tgTE[1]+tgTE[2]+tgTE[3]+tgTE[4]\n\nplt.semilogy(x/1000, np.abs(target), 'C0-', label='dipole no target')\nplt.semilogy(x/1000, np.abs(notarg), 'C3-', label='dipole target')\nplt.semilogy(x/1000, np.abs(tg), 'C1--', label='tmte no target')\nplt.semilogy(x/1000, np.abs(nt), 'C4--', label='tmte target')\nplt.legend()\nplt.xlabel('Offset (km)')\nplt.ylabel('Electric field amplitude (V/m)')\nplt.ylim([1e-17, 1e-9])\nplt.xlim([0, 15])\n\nplt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "empymod.Report()"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}