{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using class Grid3d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ttcrpy.rgrid as rg\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 73048 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# grid node coordinates\n",
    "\n",
    "x = np.arange(15.0)\n",
    "y = np.arange(14.0)\n",
    "z = np.arange(21.0)\n",
    "\n",
    "# vertical gradient\n",
    "a = 1.0\n",
    "V20 = 3.0\n",
    "b = (V20-a)/20.0\n",
    "\n",
    "# fill in model\n",
    "V = np.empty((x.size, y.size, z.size))\n",
    "for n in range(z.size):\n",
    "    V[:, :, n] = a + b*z[n]\n",
    "\n",
    "# check model\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "min_val = V.min()\n",
    "max_val = V.max()\n",
    "n_x, n_y, n_z = V.shape\n",
    "colormap = plt.cm.plasma\n",
    "\n",
    "cut = V[0,:,:]\n",
    "Y, Z = np.mgrid[0:n_y, 0:n_z]\n",
    "X = np.zeros((n_y, n_z))\n",
    "ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colormap((cut-min_val)/(max_val-min_val)), shade=False)\n",
    "cut = V[:,-1,:]\n",
    "X, Z = np.mgrid[0:n_x, 0:n_z]\n",
    "Y = y[-1] + np.zeros((n_x, n_z))\n",
    "ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colormap((cut-min_val)/(max_val-min_val)), shade=False)\n",
    "cut = V[:,:,-1]\n",
    "X, Y = np.mgrid[0:n_x, 0:n_y]\n",
    "Z = z[-1] + np.zeros((n_x, n_y))\n",
    "ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colormap((cut-min_val)/(max_val-min_val)), shade=False)\n",
    "ax.invert_zaxis()\n",
    "ax.set_title(\"Velocity model\")\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "ax.set_zlabel('Z')\n",
    "fig.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define source and receiver points and create grid instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# src and rcv should be 2D arrays\n",
    "\n",
    "src = np.array([[0.5, 0.5, 0.5]])\n",
    "rcv = np.array([[2.0, 2.0, 0.5],\n",
    "                [4.0, 4.0, 0.5],\n",
    "                [6.0, 6.0, 0.5],\n",
    "                [8.0, 8.0, 0.5],\n",
    "                [10.0, 10.0, 0.5],\n",
    "                [12.0, 12.0, 2.5],\n",
    "                [12.0, 12.0, 4.5],\n",
    "                [12.0, 12.0, 6.5],\n",
    "                [12.0, 12.0, 8.5],\n",
    "                [12.0, 12.0, 10.5],\n",
    "                [12.0, 12.0, 12.5],\n",
    "                [12.0, 12.0, 14.5],\n",
    "                [12.0, 12.0, 16.5]])\n",
    "\n",
    "# slowness will de assigned to grid nodes, we must pass cell_slowness=False\n",
    "grid = rg.Grid3d(x, y, z, cell_slowness=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Perform computation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we need to input slowness\n",
    "slowness = 1./V\n",
    "\n",
    "tt, rays = grid.raytrace(src, rcv, slowness, return_rays=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Look at computed traveltimes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7328 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(tt, 'o')\n",
    "plt.xlabel('Rcv number')\n",
    "plt.ylabel('Traveltime')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Check raypaths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 74976 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111, projection='3d')\n",
    "min_val = V.min()\n",
    "max_val = V.max()\n",
    "n_x, n_y, n_z = V.shape\n",
    "colormap = plt.cm.plasma\n",
    "\n",
    "cut = V[:,:,-1]\n",
    "X, Y = np.mgrid[0:n_x, 0:n_y]\n",
    "Z = z[-1] + np.zeros((n_x, n_y))\n",
    "ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colormap((cut-min_val)/(max_val-min_val)), shade=False)\n",
    "\n",
    "for r in rays:\n",
    "    ax.plot(r[:,0], r[:,1], r[:,2],'-k')\n",
    "\n",
    "ax.invert_zaxis()\n",
    "ax.set_title(\"Rays\")\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "ax.set_zlabel('Z')\n",
    "fig.tight_layout()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
