{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Lets have matplotlib \"inline\"\n",
    "%matplotlib inline\n",
    "\n",
    "# Add line profiler\n",
    "%load_ext line_profiler\n",
    "\n",
    "#Import packages we need\n",
    "import numpy as np\n",
    "from matplotlib import animation, rc\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import subprocess\n",
    "import os\n",
    "import gc\n",
    "import datetime\n",
    "import importlib\n",
    "import logging\n",
    "\n",
    "import pycuda.driver as cuda\n",
    "import pycuda.compiler\n",
    "\n",
    "try:\n",
    "    from StringIO import StringIO\n",
    "except ImportError:\n",
    "    from io import StringIO\n",
    "    \n",
    "#Set large figure sizes\n",
    "#Note, this prevents nice figures for articles...\n",
    "rc('figure', figsize=(16.0, 12.0))\n",
    "rc('animation', html='html5')\n",
    "\n",
    "from GPUSimulators import Common, IPythonMagic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Console logger using level INFO\n",
      "File logger using level DEBUG to test_schemes.log\n",
      "Python version 3.6.7 |Anaconda custom (64-bit)| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)]\n"
     ]
    }
   ],
   "source": [
    "%setup_logging --out test_schemes.log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Registering my_context in user workspace\n",
      "PyCUDA version 2018.1.1\n",
      "CUDA version (10, 0, 0)\n",
      "Driver version 10000\n",
      "Using 'GeForce 840M' GPU\n",
      "Created context handle <816454711568>\n",
      "Using CUDA cache dir c:\\Users\\anbro\\Documents\\projects\\GPUSimulators\\GPUSimulators\\cuda_cache\n",
      "Autotuning enabled. It may take several minutes to run the code the first time: have patience\n"
     ]
    }
   ],
   "source": [
    "%cuda_context_handler my_context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_test_data(nx, ny, g):\n",
    "    width = 100.0\n",
    "    height = 100.0\n",
    "    dx = width / float(nx)\n",
    "    dy = height / float(ny)\n",
    "\n",
    "    x_center = dx*nx/2.0\n",
    "    y_center = dy*ny/2.0\n",
    "\n",
    "    #Create a gaussian \"dam break\" that will not form shocks\n",
    "    size = width / 5.0\n",
    "    dt = 10**10\n",
    "\n",
    "    h  = np.zeros((ny, nx), dtype=np.float32); \n",
    "    hu = np.zeros((ny, nx), dtype=np.float32);\n",
    "    hv = np.zeros((ny, nx), dtype=np.float32);\n",
    "\n",
    "    extent = 1.0/np.sqrt(2.0)\n",
    "    x = (dx*(np.arange(0, nx, dtype=np.float32)+0.5) - x_center) / size\n",
    "    y = (dy*(np.arange(0, ny, dtype=np.float32)+0.5) - y_center) / size\n",
    "    xv, yv = np.meshgrid(x, y, sparse=False, indexing='xy')\n",
    "    r = np.minimum(1.0, np.sqrt(xv**2 + yv**2))\n",
    "    xv = None\n",
    "    yv = None\n",
    "    gc.collect()\n",
    "\n",
    "    #Generate highres\n",
    "    cos = np.cos(np.pi*r)\n",
    "    h = 0.5 + 0.1*0.5*(1.0 + cos)\n",
    "    hu = 0.1*0.5*(1.0 + cos)\n",
    "    hv = hu.copy()\n",
    "\n",
    "    scale = 0.7\n",
    "    max_h_estimate = 0.6\n",
    "    max_u_estimate = 0.1*np.sqrt(2.0)\n",
    "    dx = width/nx\n",
    "    dy = height/ny\n",
    "    dt = scale * min(dx, dy) / (max_u_estimate + np.sqrt(g*max_h_estimate))\n",
    "\n",
    "    return h, hu, hv, dx, dy, dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%lprun -f gen_test_data gen_test_data(100, 150, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum of absolute difference:  0.0\n"
     ]
    }
   ],
   "source": [
    "nx = 1000\n",
    "ny = 1500\n",
    "nx_halo = 2\n",
    "ny_halo = 3\n",
    "a = np.random.rand(ny, nx).astype(np.float32)\n",
    "\n",
    "import pycuda.driver as cuda\n",
    "stream = cuda.Stream()\n",
    "\n",
    "with Common.Timer(\"upload (async)\") as t:\n",
    "    a_gpu = Common.CudaArray2D(stream, nx, ny, nx_halo, ny_halo, a)\n",
    "\n",
    "with Common.Timer(\"download (async)\") as t:\n",
    "    b = a_gpu.download(stream, async=True)\n",
    "    \n",
    "with Common.Timer(\"sync\") as t:\n",
    "    cuda.Context.synchronize()\n",
    "    \n",
    "print(\"Sum of absolute difference: \", np.sum(np.abs(a-b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%lprun -f Common.get_kernel Common.get_kernel(\"FORCE_kernel.cu\", 16, 16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "nx = 256\n",
    "ny = 128\n",
    "g = 9.81\n",
    "\n",
    "h0, hu0, hv0, dx, dy, dt = gen_test_data(nx, ny, g)\n",
    "\n",
    "arguments = {\n",
    "    'context': my_context,\n",
    "    'h0': h0, 'hu0': hu0, 'hv0': hv0,\n",
    "    'nx': nx, 'ny': ny,\n",
    "    'dx': dx, 'dy': dy, 'dt': 0.9*dt,\n",
    "    'g': g\n",
    "} \n",
    "\n",
    "t_end = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe23bc79e8>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 35908 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import LxF\n",
    "importlib.reload(LxF)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = LxF.LxF(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe23b2b668>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 37192 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import FORCE\n",
    "importlib.reload(FORCE)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = FORCE.FORCE(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe23a8f2e8>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38664 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import HLL\n",
    "importlib.reload(HLL)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = HLL.HLL(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe239eef28>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39256 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import HLL2\n",
    "importlib.reload(HLL2)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = HLL2.HLL2(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe2394eb38>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40220 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import KP07\n",
    "importlib.reload(KP07)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = KP07.KP07(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe238b3748>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import KP07_dimsplit\n",
    "importlib.reload(KP07_dimsplit)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = KP07_dimsplit.KP07_dimsplit(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0xbe23815358>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39528 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from GPUSimulators import WAF\n",
    "importlib.reload(WAF)\n",
    "\n",
    "with Common.Timer(\"construct\") as t:\n",
    "    sim = WAF.WAF(**arguments)\n",
    "\n",
    "with Common.Timer(\"step\") as t:\n",
    "    t = sim.simulate(t_end)\n",
    "    \n",
    "with Common.Timer(\"download\") as t:\n",
    "    h1, hu1, hv1 = sim.download()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(str(sim) + \", t=\" + str(sim.simTime()))\n",
    "plt.imshow(h1, vmin=0.49, vmax=0.54)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
