{ "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", "from matplotlib.colorbar import ColorbarBase\n", "\n", "from IPython.display import display, HTML\n", "\n", "from enum import Enum\n", "import subprocess\n", "import time\n", "import os\n", "import gc\n", "import datetime\n", "import importlib\n", "import logging\n", "import netCDF4\n", "import json\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", "from GPUSimulators import Common, IPythonMagic\n", "from GPUSimulators.EE2D_KP07_dimsplit import EE2D_KP07_dimsplit\n", "from GPUSimulators.helpers import InitialConditions, Visualization" ] }, { "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": {}, "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 <346033291232>\n", "Using CUDA cache dir c:\\Users\\anbro\\Documents\\projects\\GPUSimulators\\GPUSimulators\\cuda_cache\n" ] } ], "source": [ "%cuda_context_handler --no_autotuning my_context " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#Set large figure sizes as default\n", "rc('figure', figsize=(16.0, 12.0))\n", "rc('animation', html='html5')\n", "rc('animation', bitrate=1800)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def plotVars(rho, rho_u, rho_v, E):\n", " plt.figure()\n", " plt.subplot(1,4,1)\n", " plt.imshow(rho, origin='bottom')\n", " plt.colorbar(orientation='horizontal', pad=0.02, shrink=0.9)\n", "\n", " plt.subplot(1,4,2)\n", " plt.imshow(rho_u, origin='bottom')\n", " plt.colorbar(orientation='horizontal', pad=0.02, shrink=0.9)\n", "\n", " plt.subplot(1,4,3)\n", " plt.imshow(rho_v, origin='bottom')\n", " plt.colorbar(orientation='horizontal', pad=0.02, shrink=0.9)\n", "\n", " plt.subplot(1,4,4)\n", " plt.imshow(E, origin='bottom')\n", " plt.colorbar(orientation='horizontal', pad=0.02, shrink=0.9)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def runSimulation(outfile, t_end, sim_args):\n", " with Common.Timer(\"construct\") as t:\n", " sim = EE2D_KP07_dimsplit(**sim_args)\n", " print(\"Constructed in \" + str(t.secs) + \" seconds\")\n", "\n", " #Create netcdf file and simulate\n", " with Common.DataDumper(outfile, mode='w', clobber=False) as outdata:\n", " outdata.ncfile.createDimension('time', None)\n", " outdata.ncfile.createDimension('x', sim.nx)\n", " outdata.ncfile.createDimension('y', sim.ny)\n", "\n", " #Create variables\n", " outdata.time_var = outdata.ncfile.createVariable('time', np.dtype('float32').char, 'time')\n", " outdata.x_var = outdata.ncfile.createVariable('x', np.dtype('float32').char, 'x')\n", " outdata.y_var = outdata.ncfile.createVariable('y', np.dtype('float32').char, 'y')\n", " outdata.rho_var = outdata.ncfile.createVariable('rho', np.dtype('float32').char, ('time', 'y', 'x'), zlib=True, least_significant_digit=3)\n", " outdata.rho_u_var = outdata.ncfile.createVariable('rho_u', np.dtype('float32').char, ('time', 'y', 'x'), zlib=True, least_significant_digit=3)\n", " outdata.rho_v_var = outdata.ncfile.createVariable('rho_v', np.dtype('float32').char, ('time', 'y', 'x'), zlib=True, least_significant_digit=3)\n", " outdata.E_var = outdata.ncfile.createVariable('E', np.dtype('float32').char, ('time', 'y', 'x'), zlib=True, least_significant_digit=3)\n", " \n", " #Create attributes\n", " def toJson(in_dict):\n", " out_dict = in_dict.copy()\n", "\n", " for key in out_dict:\n", " if isinstance(out_dict[key], np.ndarray):\n", " out_dict[key] = out_dict[key].tolist()\n", " else:\n", " try:\n", " json.dumps(out_dict[key])\n", " except:\n", " out_dict[key] = str(out_dict[key])\n", "\n", " return json.dumps(out_dict)\n", " outdata.ncfile.created = time.ctime(time.time())\n", " outdata.ncfile.sim_args = toJson(sim_args)\n", "\n", " outdata.x_var[:] = np.linspace(0, sim.nx*sim.dx, sim.nx)\n", " outdata.y_var[:] = np.linspace(0, sim.ny*sim.dy, sim.ny)\n", "\n", " progress_printer = Common.ProgressPrinter(n_save, print_every=10)\n", " print(\"Simulating to t={:f}. Estimated {:d} timesteps (dt={:f})\".format(t_end, int(t_end / sim.dt), sim.dt))\n", " for i in range(n_save+1):\n", " #Sanity check simulator\n", " try:\n", " sim.check()\n", " except AssertionError as e:\n", " print(\"Error after {:d} steps (t={:f}: {:s}\".format(sim.simSteps(), sim.simTime(), str(e)))\n", " return outdata.filename\n", "\n", " #Simulate\n", " if (i > 0):\n", " sim.simulate(t_end/n_save)\n", "\n", " #Save to file\n", " #rho = sim.u0[0].download(sim.stream)\n", " rho, rho_u, rho_v, E = sim.download()\n", " outdata.time_var[i] = sim.simTime()\n", " outdata.rho_var[i, :] = rho\n", " outdata.rho_u_var[i, :] = rho_u\n", " outdata.rho_v_var[i, :] = rho_v\n", " outdata.E_var[i, :] = E\n", "\n", " #Write progress to screen\n", " print_string = progress_printer.getPrintString(i)\n", " if (print_string):\n", " print(print_string)\n", "\n", " return outdata.filename " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "class VisType(Enum):\n", " Schlieren = 0\n", " Density = 1\n", "\n", "def createAnimation(infile, vis_type, vmin, vmax, save_anim=False, cmap=plt.cm.coolwarm, fig_args=None):\n", " fig = plt.figure(**fig_args)\n", " \n", " with Common.DataDumper(infile, 'r') as indata:\n", " time = indata.ncfile.variables['time'][:]\n", " x = indata.ncfile.variables['x'][:]\n", " y = indata.ncfile.variables['y'][:]\n", " rho = indata.ncfile.variables['rho'][0]\n", " rho_u = indata.ncfile.variables['rho_u'][0]\n", " rho_v = indata.ncfile.variables['rho_v'][0]\n", " \n", " created = indata.ncfile.created\n", " sim_args = json.loads(indata.ncfile.sim_args)\n", " for key in sim_args:\n", " if isinstance(sim_args[key], list):\n", " sim_args[key] = \"[...]\"\n", " num_frames = len(time)\n", " print(\"{:s} created {:s} contains {:d} timesteps\".format(infile, created, num_frames))\n", " print(\"Simulator arguments: \\n\", sim_args)\n", "\n", " ax1 = fig.gca()\n", " extents = [0, x.max(), 0, y.max()]\n", " \n", " if (vis_type == VisType.Schlieren):\n", " im = ax1.imshow(Visualization.genColors(rho, rho_u, rho_v, cmap, vmax, vmin), origin='bottom', extent=extents, cmap='gray', vmin=0.0, vmax=1.0)\n", " fig.suptitle(\"Schlieren / vorticity at t={:.2f}\".format(time[0]))\n", " elif (vis_type == VisType.Density):\n", " im = ax1.imshow(rho, origin='bottom', extent=extents, cmap=cmap, vmin=vmin, vmax=vmax)\n", " fig.suptitle(\"Density at t={:.2f}\".format(time[0]))\n", " else:\n", " assert False, \"Wrong vis_type\"\n", "\n", " #Create colorbar\n", " from matplotlib.colors import Normalize\n", " from mpl_toolkits.axes_grid1 import make_axes_locatable\n", " divider = make_axes_locatable(ax1)\n", " ax2 = divider.append_axes(\"right\", size=0.1, pad=0.05)\n", " cb1 = ColorbarBase(ax2, cmap=cmap,\n", " norm=Normalize(vmin=vmin, vmax=vmax),\n", " orientation='vertical')\n", " \n", " #Label colorbar\n", " if (vis_type == VisType.Schlieren):\n", " cb1.set_label('Vorticity')\n", " elif (vis_type == VisType.Density):\n", " cb1.set_label('Density')\n", "\n", " progress_printer = Common.ProgressPrinter(num_frames, print_every=5)\n", " \n", " def animate(i):\n", " rho = indata.ncfile.variables['rho'][i]\n", " rho_u = indata.ncfile.variables['rho_u'][i]\n", " rho_v = indata.ncfile.variables['rho_v'][i]\n", " \n", " if (vis_type == VisType.Schlieren):\n", " im.set_data(Visualization.genColors(rho, rho_u, rho_v, cmap, vmax, vmin))\n", " fig.suptitle(\"Schlieren / vorticity at t={:.2f}\".format(time[i]))\n", " elif (vis_type == VisType.Density):\n", " im.set_data(rho) \n", " fig.suptitle(\"Density at t={:.2f}\".format(time[i]))\n", " \n", " #Write progress to screen\n", " print_string = progress_printer.getPrintString(i)\n", " if (print_string):\n", " print(print_string)\n", "\n", " anim = animation.FuncAnimation(fig, animate, interval=50, frames=range(num_frames))\n", " plt.close()\n", "\n", " if (save_anim):\n", " root, _ = os.path.splitext(infile)\n", " movie_outpath = os.path.abspath(root + \".mp4\")\n", " if (os.path.isfile(movie_outpath)):\n", " print(\"Reusing previously created movie \" + movie_outpath)\n", " else:\n", " print(\"Creating movie \" + movie_outpath)\n", " #from matplotlib.animation import FFMpegFileWriter\n", " #writer = FFMpegFileWriter(fps=25)\n", " from matplotlib.animation import FFMpegWriter\n", " writer = FFMpegWriter(fps=25)\n", " anim.save(movie_outpath, writer=writer)\n", " display(HTML(\"\"\"\n", "