{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from matplotlib import rc\n",
    "\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams[\"text.usetex\"] = True\n",
    "plt.rcParams[\"font.family\"] = 'serif'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def setBwStyles(ax):\n",
    "    from cycler import cycler\n",
    "\n",
    "    ax.set_prop_cycle( #cycler('marker', ['.', 'x', 4, '+', '*', '1']) +\n",
    "                       #cycler('linestyle', ['-.', '--', ':', '-.', '--', ':']) +\n",
    "                       #cycler('markersize', [15, 15, 15, 15, 15, 15]) +\n",
    "                       cycler('color', ['k', 'k', 'k', 'k', 'k', 'k']) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/andreb/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:35: RuntimeWarning: invalid value encountered in true_divide\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 50288 characters]\n",
      "text/plain": [
       "<Figure size 750x600 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/andreb/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:20: RuntimeWarning: divide by zero encountered in true_divide\n",
      "/home/andreb/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:23: RuntimeWarning: divide by zero encountered in true_divide\n",
      "/home/andreb/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:26: RuntimeWarning: divide by zero encountered in true_divide\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 65540 characters]\n",
      "text/plain": [
       "<Figure size 750x600 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def desingularise_our(hu, h, eps):\n",
    "    return hu / np.maximum(np.minimum(h*h/(2.0*eps)+0.5*eps, eps), np.abs(h))\n",
    "\n",
    "def desingularise_kp1(hu, h, eps):\n",
    "    return np.sqrt(2.0)*h*hu/np.sqrt(h**4+np.maximum(h**4, eps**4))\n",
    "\n",
    "def desingularise_kp2(hu, h, eps):\n",
    "    return h*hu/(h**2+eps**2)\n",
    "\n",
    "def desingularise_kp3(hu, h, eps):\n",
    "    return 2*h*hu/(h**2+np.maximum(h**2, eps**2))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def desingularised_h_our(hu, h, eps):\n",
    "    return np.maximum(np.minimum(h*h/(2.0*eps)+0.5*eps, eps), np.abs(h))\n",
    "\n",
    "def desingularised_h_kp1(hu, h, eps):\n",
    "    return np.sqrt(h**4+np.maximum(h**4, eps**4)) / (np.sqrt(2.0)*h)\n",
    "\n",
    "def desingularised_h_kp2(hu, h, eps):\n",
    "    return (h**2+eps**2) / h\n",
    "\n",
    "def desingularised_h_kp3(hu, h, eps):\n",
    "    return (h**2+np.maximum(h**2, eps**2)) / (2*h)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "x = np.linspace(0, 3.0e-11, 500).astype(np.float32)\n",
    "h = x.copy()\n",
    "u = np.ones_like(h)*1.0\n",
    "hu = h*u\n",
    "u_true = hu/h\n",
    "u_our = desingularise_our(hu, h, 1.0e-11)\n",
    "u_kp1 = desingularise_kp1(hu, h, 1.0e-11)\n",
    "u_kp2 = desingularise_kp2(hu, h, 1.0e-11)\n",
    "u_kp3 = desingularise_kp3(hu, h, 1.0e-11)\n",
    "\n",
    "\n",
    "fig = plt.figure(figsize=(5, 4), dpi=150)\n",
    "#setBwStyles(fig.gca())\n",
    "plt.plot(x, u_kp1, label='$u^*_1$', markevery=50, marker='.', color='k', markersize=10)\n",
    "plt.plot(x, u_kp2, label='$u^*_2$', markevery=50, marker='+', color='k', markersize=7)\n",
    "plt.plot(x, u_kp3, label='$u^*_3$', markevery=50, marker='x', color='k', markersize=7)\n",
    "plt.plot(x, u_our, label='Our $u^*$', markevery=50, marker='*', color='k', markersize=10)\n",
    "plt.plot(x, u_true, label='Exact $u$', markevery=50, color='k', markersize=5)\n",
    "plt.ylim([0.6, 1.1])\n",
    "plt.xlim([0, 3.0e-11])\n",
    "plt.xlabel('h')\n",
    "plt.ylabel('Desingularized $hu/h$')\n",
    "plt.legend()\n",
    "plt.savefig(\"desing_hu.pdf\", bbox_inches = \"tight\")\n",
    "plt.show()\n",
    "\n",
    "\n",
    "h = x.copy()\n",
    "u = np.ones_like(h)*1.01\n",
    "hu = h*u\n",
    "h_true = h\n",
    "h_our = desingularised_h_our(hu, h, 1.0e-11)\n",
    "h_kp1 = desingularised_h_kp1(hu, h, 1.0e-11)\n",
    "h_kp2 = desingularised_h_kp2(hu, h, 1.0e-11)\n",
    "h_kp3 = desingularised_h_kp3(hu, h, 1.0e-11)\n",
    "\n",
    "fig = plt.figure(figsize=(5, 4), dpi=150)\n",
    "#setBwStyles(fig.gca())\n",
    "plt.plot(x, h_kp1, label='$h^*_1$', markevery=50, marker='.', color='k', markersize=10)\n",
    "plt.plot(x, h_kp2, label='$h^*_2$', markevery=50, marker='+', color='k', markersize=7)\n",
    "plt.plot(x, h_kp3, label='$h^*_3$', markevery=50, marker='x', color='k', markersize=7)\n",
    "plt.plot(x, h_our, label='Our $h^*$', markevery=50, marker='*', color='k', markersize=10)\n",
    "plt.plot(x, h_true, label='Exact $h$', markevery=50, color='k', markersize=5)\n",
    "plt.ylim([0, 3.0e-11])\n",
    "plt.xlim([0, 3.0e-11])\n",
    "plt.xlabel('h')\n",
    "plt.ylabel('Desingularized $h$')\n",
    "plt.legend()\n",
    "plt.savefig(\"desing_h.pdf\", bbox_inches = \"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
