{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "This notebook sets up and runs a set of benchmarks to compare\n",
    "different numerical discretizations of the SWEs\n",
    "\n",
    "Copyright (C) 2016  SINTEF ICT\n",
    "\n",
    "This program is free software: you can redistribute it and/or modify\n",
    "it under the terms of the GNU General Public License as published by\n",
    "the Free Software Foundation, either version 3 of the License, or\n",
    "(at your option) any later version.\n",
    "\n",
    "This program is distributed in the hope that it will be useful,\n",
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n",
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n",
    "GNU General Public License for more details.\n",
    "\n",
    "You should have received a copy of the GNU General Public License\n",
    "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Lets have matplotlib \"inline\"\n",
    "%matplotlib inline\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",
    "from matplotlib import gridspec\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "#import mpld3\n",
    "\n",
    "import subprocess\n",
    "import socket\n",
    "import time\n",
    "import os\n",
    "import gc\n",
    "import datetime\n",
    "import logging\n",
    "\n",
    "import pycuda.driver as cuda\n",
    "\n",
    "try:\n",
    "    from StringIO import StringIO\n",
    "except ImportError:\n",
    "    from io import StringIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Finally, import our simulator\n",
    "from GPUSimulators import Common, LxF, FORCE, HLL, HLL2, KP07, KP07_dimsplit, WAF, IPythonMagic\n",
    "from GPUSimulators.helpers import InitialConditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Console logger using level INFO\n",
      "File logger using level DEBUG to output.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",
      "Registering cuda_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 <1097222361648>\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": [
    "%setup_logging\n",
    "%cuda_context_handler cuda_context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Misc plotting setup\n",
    "def setBwStyles(ax):\n",
    "    from cycler import cycler\n",
    "\n",
    "    ax.set_prop_cycle( cycler('marker', ['.', 'x', 4, '+', '*', '1', 5]) +\n",
    "                       cycler('linestyle', ['-.', '--', ':', '-.', '--', ':', '-.']) +\n",
    "                       #cycler('markersize', [5, 5, 5, 5, 5, 5]) +\n",
    "                       cycler('color', ['k', 'k', 'k', 'k', 'k', 'k', 'k']) )    \n",
    "\n",
    "#Set large figure sizes\n",
    "plt.rcParams['figure.figsize'] = [12, 8]\n",
    "plt.rcParams['figure.dpi'] = 100\n",
    "plt.rcParams['animation.html'] = 'html5'\n",
    "#plt.rcParams['legend.markerscale'] = 1.0\n",
    "#plt.rcParams['lines.markersize'] = 6\n",
    "plt.rcParams['lines.markeredgewidth'] = 1.5\n",
    "#plt.rcParams['savefig.dpi'] = 400"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_figure(fig, stem):\n",
    "    if (not os.path.isdir(\"figures\")):\n",
    "        os.mkdir(\"figures\")\n",
    "    \n",
    "    fig_filename = os.path.join(\"figures\", \"convergence_smooth2d_\" + stem + \".pdf\")\n",
    "    \n",
    "    metadata = {\n",
    "        'created': time.strftime(\"%Y_%m_%d-%H_%M_%S\"),\n",
    "        'hostname': socket.gethostname()\n",
    "    }\n",
    "        \n",
    "    legend = fig.gca().legend_\n",
    "    if (legend != None):\n",
    "        fig.savefig(fig_filename, dpi=300,format='pdf',\n",
    "                transparent=True, pad_inches=0.0, frameon=None, \n",
    "                metadata=metadata, \n",
    "                bbox_extra_artists=(legend, ), bbox_inches='tight')\n",
    "    else:\n",
    "        fig.savefig(fig_filename, dpi=300,format='pdf',\n",
    "                transparent=True, pad_inches=0.0, frameon=None, \n",
    "                metadata=metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32736 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nx=100\n",
    "ref_nx=1000\n",
    "ny=500\n",
    "ref_ny=500\n",
    "width = 50\n",
    "height = width\n",
    "bump_size = 10\n",
    "h, hu, hv, dx, dy = InitialConditions.bump(nx=nx, ny=ny, ref_nx=ref_nx, ref_ny=ref_ny, width=width, height=height, bump_size=bump_size)\n",
    "\n",
    "plt.figure()\n",
    "ax=plt.subplot(1,3,1)\n",
    "im=plt.imshow(h, extent=[0, nx*dx, 0, ny*dy])\n",
    "plt.colorbar(orientation='horizontal')\n",
    "\n",
    "ax=plt.subplot(1,3,2)\n",
    "im=plt.imshow(hu, extent=[0, nx*dx, 0, ny*dy])\n",
    "plt.colorbar(orientation='horizontal')\n",
    "\n",
    "ax=plt.subplot(1,3,3)\n",
    "im=plt.imshow(hv, extent=[0, nx*dx, 0, ny*dy])\n",
    "plt.colorbar(orientation='horizontal')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_filename(simulator, nx):\n",
    "    return os.path.abspath(os.path.join(\"data\", \"smooth2d\", str(simulator.__name__) + \"_\" + str(nx) + \".npz\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_benchmark(datafilename, simulator, simulator_args, nx, reference_nx, ny, reference_ny,\n",
    "                  h_ref=0.5, h_amp=0.1, u_ref=0.0, u_amp=0.1, v_ref=0.0, v_amp=0.1,\n",
    "                  dt=None, force_rerun=False, transpose=False):\n",
    "\n",
    "    if (datafilename and os.path.isfile(datafilename) and force_rerun == False):\n",
    "        print(\"WARNING: Existing simulation exists, skipping simulation\")\n",
    "        return [0, 0, 0]\n",
    "    else:\n",
    "        test_data_args= {\n",
    "            'nx': nx,\n",
    "            'ny': ny,\n",
    "            'width': 100,\n",
    "            'height': 100,\n",
    "            'bump_size': 20,\n",
    "            'ref_nx': reference_nx,\n",
    "            'ref_ny': reference_ny,\n",
    "            'h_ref': h_ref, 'h_amp': h_amp,\n",
    "            'u_ref': u_ref, 'u_amp': u_amp,\n",
    "            'v_ref': v_ref, 'v_amp': v_amp\n",
    "        }\n",
    "        h0, hu0, hv0, dx, dy = InitialConditions.bump(**test_data_args)\n",
    "\n",
    "        #Initialize simulator\n",
    "        with Common.Timer(simulator.__name__ + \"_\" + str(nx)) as timer:\n",
    "            if (transpose):\n",
    "                h0 = np.ascontiguousarray(np.transpose(h0))\n",
    "                hu0, hv0 = np.ascontiguousarray(np.transpose(hv0)), np.ascontiguousarray(np.transpose(hu0))\n",
    "                dx, dy = dy, dx\n",
    "                nx, ny = ny, nx\n",
    "                \n",
    "            sim_args = {\n",
    "                'h0': h0, 'hu0': hu0, 'hv0': hv0,\n",
    "                'nx': nx, 'ny': ny,\n",
    "                'dx': dx, 'dy': dy\n",
    "            }\n",
    "            sim_args.update(simulator_args)\n",
    "\n",
    "            sim = simulator(**sim_args)\n",
    "            sim.simulate(1.0, dt=dt)\n",
    "            sim.check()\n",
    "            \n",
    "            nt = sim.simSteps()\n",
    "            dt = sim.simTime() / nt\n",
    "            h, hu, hv = sim.download()\n",
    "            \n",
    "            if (transpose):\n",
    "                h = np.ascontiguousarray(np.transpose(h))\n",
    "                hu, hv = np.ascontiguousarray(np.transpose(hv)), np.ascontiguousarray(np.transpose(hu))\n",
    "            \n",
    "\n",
    "            if (datafilename):\n",
    "                dirname = os.path.dirname(datafilename)\n",
    "                if (dirname and not os.path.isdir(dirname)):\n",
    "                    os.makedirs(dirname)\n",
    "                np.savez_compressed(datafilename, h=h, hu=hu, hv=hv)\n",
    "        \n",
    "    gc.collect() # Force run garbage collection to free up memory\n",
    "    \n",
    "    return [timer.secs, nt, dt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100, 50) =downsample=> (25, 25)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14688 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "3475"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datafilename = \"temp_data.npz\"\n",
    "\n",
    "sim_args = {\n",
    "    'context': cuda_context,\n",
    "    'cfl_scale': 0.9,\n",
    "    'g': 9.81\n",
    "}\n",
    "\n",
    "\n",
    "elapsed, nt, dt = run_benchmark(datafilename, \n",
    "                    KP07.KP07, \n",
    "                    sim_args,\n",
    "                    nx=50, reference_nx=500, ny=100, reference_ny=200,\n",
    "                    #dt=0.25*0.7*(width/nx)/(u_ref+u_amp + np.sqrt(g*(h_ref+h_amp))),\n",
    "                    force_rerun=True,\n",
    "                    transpose=False)\n",
    "\n",
    "with np.load(datafilename) as data:\n",
    "    h = data['h']\n",
    "    rh = InitialConditions.downsample(h, 2, 4)\n",
    "    \n",
    "    print(str(h.shape) + \" =downsample=> \" + str(rh.shape))\n",
    "\n",
    "    plt.figure(figsize=(8, 6))\n",
    "    plt.imshow(rh, extent=[0, 100, 0, 100])\n",
    "    plt.colorbar()\n",
    "    plt.show()\n",
    "    \n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed warmup in 0.94 s\n",
      "Completed simulation (LxF) in 8.05 s\n",
      "Completed simulation (FORCE) in 7.49 s\n",
      "Completed simulation (HLL) in 10.18 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "HLL2 [2048x2048]: 0% [##########################====] 100%. Total: 9s, elapsed: 8s, remaining: 1s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed simulation (HLL2) in 16.63 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WAF [2048x2048]: 0% [##########################====] 100%. Total: 8s, elapsed: 7s, remaining: 1s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed simulation (WAF) in 15.55 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "KP07 [2048x2048]: 0% [##########################====] 100%. Total: 7s, elapsed: 7s, remaining: 1s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed simulation (KP07) in 14.44 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "KP07_dimsplit [2048x2048]: 0% [##########################====] 100%. Total: 8s, elapsed: 7s, remaining: 1s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completed simulation (KP07_dimsplit) in 15.66 s\n",
      "Completed simulation in 88.01 s\n"
     ]
    }
   ],
   "source": [
    "reference_nx = 2048\n",
    "force_rerun=True\n",
    "transpose=True\n",
    "domain_sizes = [8, 16, 32, 64, 128, 256, 512, 1024]\n",
    "simulators = [LxF.LxF, FORCE.FORCE, HLL.HLL, HLL2.HLL2, WAF.WAF, KP07.KP07, KP07_dimsplit.KP07_dimsplit]\n",
    "\n",
    "\n",
    "\n",
    "sim_elapsed_time = np.zeros((len(simulators), len(domain_sizes)))\n",
    "sim_dt = np.zeros((len(simulators), len(domain_sizes)))\n",
    "sim_nt = np.zeros((len(simulators), len(domain_sizes)))\n",
    "\n",
    "#Warmup to compile kernels\n",
    "with Common.Timer(\"warmup\") as timer:\n",
    "    for i in range(len(simulators)):\n",
    "        run_benchmark(datafilename=None, \n",
    "                    simulator=simulators[i],\n",
    "                    simulator_args=sim_args,\n",
    "                    nx=16, reference_nx=16, ny=16, reference_ny=16,\n",
    "                    #dt=0.25*0.7*(width/nx)/(u_ref+u_amp + np.sqrt(g*(h_ref+h_amp))),\n",
    "                    force_rerun=force_rerun,\n",
    "                    transpose=transpose)\n",
    "print(\"Completed warmup in {:.2f} s\".format(timer.secs))\n",
    "    \n",
    "with Common.Timer(\"simulation\") as timer:\n",
    "    for i in range(len(simulators)):\n",
    "        with Common.Timer(simulators[i].__name__) as sim_timer:\n",
    "            # Run the simulation for all the domain sizes\n",
    "            for j, nx in enumerate(domain_sizes):\n",
    "                datafilename = gen_filename(simulators[i], nx)\n",
    "                secs, nt, dt = run_benchmark(datafilename=datafilename, \n",
    "                    simulator=simulators[i],\n",
    "                    simulator_args=sim_args,\n",
    "                    nx=nx, ny=nx, reference_nx=reference_nx, reference_ny=reference_nx,\n",
    "                    #dt=0.25*0.7*(width/nx)/(u_ref+u_amp + np.sqrt(g*(h_ref+h_amp))),\n",
    "                    force_rerun=force_rerun,\n",
    "                    transpose=transpose)\n",
    "                sim_elapsed_time[i, j] = secs\n",
    "                sim_dt[i, j] = dt\n",
    "                sim_nt[i, j] = nt\n",
    "\n",
    "            # Run reference with a low CFL-number\n",
    "            datafilename = gen_filename(simulators[i], reference_nx)\n",
    "            run_benchmark(datafilename=datafilename, \n",
    "                    simulator=simulators[i],\n",
    "                    simulator_args=sim_args,\n",
    "                    nx=reference_nx, ny=reference_nx, reference_nx=reference_nx, reference_ny=reference_nx,\n",
    "                    #dt=0.25*0.7*(width/nx)/(u_ref+u_amp + np.sqrt(g*(h_ref+h_amp))),\n",
    "                    force_rerun=force_rerun,\n",
    "                    transpose=transpose)\n",
    "        print(\"Completed simulation ({:}) in {:.2f} s\".format(simulators[i].__name__, sim_timer.secs))\n",
    "print(\"Completed simulation in {:.2f} s\".format(timer.secs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0xff000036d8>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 99064 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56936 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 59784 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "setBwStyles(fig.gca())\n",
    "plt.title(\"Elapsed time vs number of cells\")\n",
    "for i, simulator in enumerate(simulators):\n",
    "    plt.plot(domain_sizes, sim_elapsed_time[i,:], label=simulator.__name__)\n",
    "plt.legend()\n",
    "    \n",
    "fig = plt.figure()\n",
    "setBwStyles(fig.gca())\n",
    "plt.title(\"Dt vs number of cells\")\n",
    "for i, simulator in enumerate(simulators):\n",
    "    plt.loglog(domain_sizes, sim_dt[i,:], label=simulator.__name__)\n",
    "plt.legend()\n",
    "    \n",
    "fig = plt.figure()\n",
    "setBwStyles(fig.gca())\n",
    "plt.title(\"Number of timesteps vs number of cells\")\n",
    "for i, simulator in enumerate(simulators):\n",
    "    plt.plot(domain_sizes, sim_nt[i,:], label=simulator.__name__)\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>8</th>\n",
       "      <th>16</th>\n",
       "      <th>32</th>\n",
       "      <th>64</th>\n",
       "      <th>128</th>\n",
       "      <th>256</th>\n",
       "      <th>512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>LxF</th>\n",
       "      <td>2.004500</td>\n",
       "      <td>0.962610</td>\n",
       "      <td>1.011157</td>\n",
       "      <td>1.055375</td>\n",
       "      <td>1.229462</td>\n",
       "      <td>1.305015</td>\n",
       "      <td>1.572475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FORCE</th>\n",
       "      <td>1.694690</td>\n",
       "      <td>2.743635</td>\n",
       "      <td>1.027326</td>\n",
       "      <td>1.078696</td>\n",
       "      <td>1.170647</td>\n",
       "      <td>1.467705</td>\n",
       "      <td>1.790797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HLL</th>\n",
       "      <td>0.948501</td>\n",
       "      <td>0.947602</td>\n",
       "      <td>1.002329</td>\n",
       "      <td>1.070378</td>\n",
       "      <td>1.093793</td>\n",
       "      <td>1.227389</td>\n",
       "      <td>1.583368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HLL2</th>\n",
       "      <td>2.055230</td>\n",
       "      <td>2.675372</td>\n",
       "      <td>2.038733</td>\n",
       "      <td>1.901313</td>\n",
       "      <td>2.136504</td>\n",
       "      <td>2.029882</td>\n",
       "      <td>1.151141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WAF</th>\n",
       "      <td>2.044203</td>\n",
       "      <td>2.359680</td>\n",
       "      <td>1.420239</td>\n",
       "      <td>2.082590</td>\n",
       "      <td>1.969953</td>\n",
       "      <td>2.062400</td>\n",
       "      <td>2.008906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KP07</th>\n",
       "      <td>1.499732</td>\n",
       "      <td>2.332833</td>\n",
       "      <td>1.649850</td>\n",
       "      <td>1.867316</td>\n",
       "      <td>2.171336</td>\n",
       "      <td>2.137609</td>\n",
       "      <td>2.304406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KP07_dimsplit</th>\n",
       "      <td>1.946673</td>\n",
       "      <td>2.480605</td>\n",
       "      <td>1.604544</td>\n",
       "      <td>1.875116</td>\n",
       "      <td>2.236785</td>\n",
       "      <td>2.150302</td>\n",
       "      <td>1.534703</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    8         16        32        64        128       256  \\\n",
       "LxF            2.004500  0.962610  1.011157  1.055375  1.229462  1.305015   \n",
       "FORCE          1.694690  2.743635  1.027326  1.078696  1.170647  1.467705   \n",
       "HLL            0.948501  0.947602  1.002329  1.070378  1.093793  1.227389   \n",
       "HLL2           2.055230  2.675372  2.038733  1.901313  2.136504  2.029882   \n",
       "WAF            2.044203  2.359680  1.420239  2.082590  1.969953  2.062400   \n",
       "KP07           1.499732  2.332833  1.649850  1.867316  2.171336  2.137609   \n",
       "KP07_dimsplit  1.946673  2.480605  1.604544  1.875116  2.236785  2.150302   \n",
       "\n",
       "                    512  \n",
       "LxF            1.572475  \n",
       "FORCE          1.790797  \n",
       "HLL            1.583368  \n",
       "HLL2           1.151141  \n",
       "WAF            2.008906  \n",
       "KP07           2.304406  \n",
       "KP07_dimsplit  1.534703  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Max</th>\n",
       "      <th>Min</th>\n",
       "      <th>Mean</th>\n",
       "      <th>Stdev</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>LxF</th>\n",
       "      <td>2.004500</td>\n",
       "      <td>0.962610</td>\n",
       "      <td>1.305799</td>\n",
       "      <td>0.344846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FORCE</th>\n",
       "      <td>2.743635</td>\n",
       "      <td>1.027326</td>\n",
       "      <td>1.567642</td>\n",
       "      <td>0.553882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HLL</th>\n",
       "      <td>1.583368</td>\n",
       "      <td>0.947602</td>\n",
       "      <td>1.124766</td>\n",
       "      <td>0.207781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HLL2</th>\n",
       "      <td>2.675372</td>\n",
       "      <td>1.151141</td>\n",
       "      <td>1.998311</td>\n",
       "      <td>0.415844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WAF</th>\n",
       "      <td>2.359680</td>\n",
       "      <td>1.420239</td>\n",
       "      <td>1.992567</td>\n",
       "      <td>0.261522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KP07</th>\n",
       "      <td>2.332833</td>\n",
       "      <td>1.499732</td>\n",
       "      <td>1.994726</td>\n",
       "      <td>0.302848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KP07_dimsplit</th>\n",
       "      <td>2.480605</td>\n",
       "      <td>1.534703</td>\n",
       "      <td>1.975533</td>\n",
       "      <td>0.315498</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Max       Min      Mean     Stdev\n",
       "LxF            2.004500  0.962610  1.305799  0.344846\n",
       "FORCE          2.743635  1.027326  1.567642  0.553882\n",
       "HLL            1.583368  0.947602  1.124766  0.207781\n",
       "HLL2           2.675372  1.151141  1.998311  0.415844\n",
       "WAF            2.359680  1.420239  1.992567  0.261522\n",
       "KP07           2.332833  1.499732  1.994726  0.302848\n",
       "KP07_dimsplit  2.480605  1.534703  1.975533  0.315498"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from IPython.display import display\n",
    "\n",
    "conservation = np.zeros((len(simulators), len(domain_sizes)))\n",
    "error = np.zeros((len(simulators), len(domain_sizes)))\n",
    "order = np.zeros((len(simulators), len(domain_sizes)-1))\n",
    "\n",
    "for i, simulator in enumerate(simulators):\n",
    "    #Read the reference solution\n",
    "    datafilename = gen_filename(simulator, reference_nx)\n",
    "    with np.load(datafilename) as data:\n",
    "        h_ref = data['h'].astype(np.float64)\n",
    "    \n",
    "    #Compute conservation and error for subsequent resolutions\n",
    "    for j, nx in enumerate(domain_sizes):\n",
    "        #Read current resolution\n",
    "        datafilename = gen_filename(simulator, nx)\n",
    "        with np.load(datafilename) as data:\n",
    "            h = data['h'].astype(np.float64)\n",
    "        \n",
    "        #Downsample reference to current resolution\n",
    "        h_ref_downsampled = InitialConditions.downsample(h_ref, reference_nx/nx)\n",
    "        \n",
    "        #Compute error per cell\n",
    "        error[i, j] = np.linalg.norm((h_ref_downsampled - h).flatten(), ord=1) / (nx*nx)\n",
    "        conservation[i, j] = (np.sum(h_ref_downsampled) - np.sum(h))\n",
    "    \n",
    "    #Compute the numerical order\n",
    "    a = np.arange(1, len(domain_sizes))\n",
    "    order[i,:] = np.log2(error[i, :-1] / error[i, 1:])\n",
    "\n",
    "\n",
    "table = pd.DataFrame(order, columns=domain_sizes[:-1], index=[simulator.__name__ for simulator in simulators])\n",
    "display(table)\n",
    "\n",
    "order_summary = pd.DataFrame(np.empty((len(simulators), 4)), columns=['Max', 'Min', 'Mean', 'Stdev'], index=[simulator.__name__ for simulator in simulators])\n",
    "for i, simulator in enumerate(simulators):\n",
    "    order_summary.loc[simulator.__name__] = [np.max(order[i,:]), \n",
    "        np.min(order[i,:]), \n",
    "        np.mean(order[i,:]), \n",
    "        np.std(order[i,:])]\n",
    "display(order_summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\anbro\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\matplotlib\\backends\\backend_pdf.py:594: UserWarning: Unknown infodict keyword: created\n",
      "  self.writeInfoDict()\n",
      "c:\\Users\\anbro\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\matplotlib\\backends\\backend_pdf.py:594: UserWarning: Unknown infodict keyword: hostname\n",
      "  self.writeInfoDict()\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 145152 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "setBwStyles(fig.gca())\n",
    "\n",
    "x = np.linspace(domain_sizes[0], domain_sizes[-1], 100);\n",
    "\n",
    "scaling = np.max(error[:,0]) * domain_sizes[0] * 1.5\n",
    "plt.loglog(x, scaling/x, '-', color='gray', label='Order 1')\n",
    "\n",
    "scaling = np.min(error[:,0]) * domain_sizes[0]**2 * 0.5\n",
    "plt.loglog(x, scaling/(x*x), '-', color='gray', label='Order 2')\n",
    "\n",
    "for i, simulator in enumerate(simulators):\n",
    "    plt.loglog(domain_sizes, error[i,:], label=simulators[i].__name__)\n",
    "\n",
    "plt.xlabel('Number of cells')\n",
    "plt.ylabel('Error')\n",
    "plt.title(\"Convergence\")\n",
    "plt.legend()\n",
    "save_figure(fig, 'convergence')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_solution(simulator, nx, label, **kwargs):\n",
    "    datafilename = gen_filename(simulator, nx)\n",
    "    \n",
    "    #Read the solution\n",
    "    with np.load(datafilename) as data:\n",
    "        h = data['h']\n",
    "        h = h[int(nx/2),:]\n",
    "        \n",
    "    x = np.linspace(0.5, nx-0.5, nx)*100.0/float(nx)\n",
    "    \n",
    "    plt.plot(x, h, label=label, **kwargs)\n",
    "    \n",
    "    h = None\n",
    "    x = None\n",
    "    gc.collect() # Force run garbage collection to free up memory\n",
    "    \n",
    "def plot_comparison(nx, **kwargs):\n",
    "    plot_solution(HLL2.HLL2, reference_nx, 'Reference', marker=' ', linestyle='-')\n",
    "\n",
    "    for i, simulator in enumerate(simulators):\n",
    "        plot_solution(simulator, nx, simulator.__name__, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 160424 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "boxes = np.array([\\\n",
    "         [[28, 35], [0.499, 0.516]], \\\n",
    "         [[45, 56], [0.58, 0.60]]\n",
    "         ])\n",
    "comparison_nx = 32\n",
    "\n",
    "fig = plt.figure(figsize=(12, 12))\n",
    "gs = gridspec.GridSpec(2, 2)\n",
    "    \n",
    "ax1 = fig.add_subplot(gs[0,:])\n",
    "setBwStyles(ax1)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, comparison_nx, simulator.__name__, marker=' ')\n",
    "for box_x, box_y in boxes:\n",
    "    x = np.kron(box_x, np.ones((2)))\n",
    "    y = np.hstack((box_y, box_y[::-1]))\n",
    "    fig.gca().fill(x, y, fill=True, linestyle='-', color='gray', alpha=0.5)\n",
    "plt.title(\"Comparison of solutions at nx=\" + str(comparison_nx))\n",
    "plt.legend()\n",
    "\n",
    "ax2 = fig.add_subplot(gs[1,0])\n",
    "setBwStyles(ax2)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, comparison_nx, simulator.__name__, markevery=2)\n",
    "plt.xlim(boxes[0, 0])\n",
    "plt.ylim(boxes[0, 1])\n",
    "\n",
    "ax3 = fig.add_subplot(gs[1,1])\n",
    "setBwStyles(ax3)\n",
    "setBwStyles(ax2)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, comparison_nx, simulator.__name__, markevery=2)\n",
    "plt.xlim(boxes[1, 0])\n",
    "plt.ylim(boxes[1, 1])\n",
    "    \n",
    "save_figure(fig, 'solution')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 91648 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(12, 12))\n",
    "gs = gridspec.GridSpec(2, 2)\n",
    "    \n",
    "ax1 = fig.add_subplot(gs[0,:])\n",
    "setBwStyles(ax1)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, reference_nx, simulator.__name__, marker=' ')\n",
    "for box_x, box_y in boxes:\n",
    "    x = np.kron(box_x, np.ones((2)))\n",
    "    y = np.hstack((box_y, box_y[::-1]))\n",
    "    fig.gca().fill(x, y, fill=True, linestyle='-', color='gray', alpha=0.5)\n",
    "plt.title(\"Comparison of reference solutions\")\n",
    "plt.legend()\n",
    "\n",
    "ax2 = fig.add_subplot(gs[1,0])\n",
    "setBwStyles(ax2)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, reference_nx, simulator.__name__, marker=' ')\n",
    "plt.xlim(boxes[0, 0])\n",
    "plt.ylim(boxes[0, 1])\n",
    "\n",
    "ax3 = fig.add_subplot(gs[1,1])\n",
    "setBwStyles(ax3)\n",
    "setBwStyles(ax2)\n",
    "for simulator in simulators:\n",
    "    plot_solution(simulator, reference_nx, simulator.__name__, marker=' ')\n",
    "plt.xlim(boxes[1, 0])\n",
    "plt.ylim(boxes[1, 1])\n",
    "    \n",
    "save_figure(fig, 'reference_solutions')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 192784 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 176988 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 182020 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 159180 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 160116 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 162740 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 162736 characters]\n",
      "text/plain": [
       "<Figure size 1200x1200 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "boxes = np.array([\\\n",
    "         [[28, 35], [0.499, 0.516]], \\\n",
    "         [[45, 56], [0.58, 0.60]]\n",
    "         ])\n",
    "\n",
    "for simulator in simulators:\n",
    "    fig = plt.figure(figsize=(12, 12))\n",
    "    \n",
    "    gs = gridspec.GridSpec(2, 2)\n",
    "    \n",
    "    ax1 = fig.add_subplot(gs[0,:])\n",
    "    setBwStyles(ax1)\n",
    "    plot_solution(simulator, reference_nx, 'Reference', linestyle='-', marker=' ')\n",
    "    for j, nx in enumerate(domain_sizes):\n",
    "        plot_solution(simulator, nx, 'nx=' + str(nx), marker=' ')\n",
    "    for box_x, box_y in boxes:\n",
    "        x = np.kron(box_x, np.ones((2)))\n",
    "        y = np.hstack((box_y, box_y[::-1]))\n",
    "        fig.gca().fill(x, y, fill=True, linestyle='-', color='gray', alpha=0.5)\n",
    "    plt.title(str(simulator.__name__))\n",
    "    plt.legend()\n",
    "    \n",
    "    ax2 = fig.add_subplot(gs[1,0])\n",
    "    setBwStyles(ax2)\n",
    "    plot_solution(simulator, reference_nx, 'Reference', linestyle='-', marker=' ')\n",
    "    for j, nx in enumerate(domain_sizes):\n",
    "        plot_solution(simulator, nx, 'nx=' + str(nx), marker=' ')  \n",
    "    plt.xlim(boxes[0, 0])\n",
    "    plt.ylim(boxes[0, 1])\n",
    "    \n",
    "    ax3 = fig.add_subplot(gs[1,1])\n",
    "    setBwStyles(ax3)\n",
    "    plot_solution(simulator, reference_nx, 'Reference', linestyle='-', marker=' ')\n",
    "    for j, nx in enumerate(domain_sizes):\n",
    "        plot_solution(simulator, nx, 'nx=' + str(nx), marker=' ')   \n",
    "    plt.xlim(boxes[1, 0])\n",
    "    plt.ylim(boxes[1, 1])\n",
    "    \n",
    "    save_figure(fig, simulator.__name__)"
   ]
  },
  {
   "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
}
