{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from pyharmx import PolyHarmInterpolator\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Interpolation\n",
    "smoothing = 0.0\n",
    "order = 3\n",
    "# Domain length\n",
    "l = 1.0\n",
    "nb_pts = 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set device and dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = \"cuda:0\"\n",
    "dtype = torch.double"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.set_default_device(device)\n",
    "torch.set_default_dtype(dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate 2D grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2*np.pi/l\n",
    "x = torch.linspace(0, l, nb_pts)\n",
    "y = torch.linspace(0, l, nb_pts)\n",
    "xx, yy = torch.meshgrid(x, y, indexing='ij')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute exact solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = torch.sin(a*xx) * torch.cos(3*a*yy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interpolate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = torch.cat([xx.reshape(1,-1,1), yy.reshape(1,-1,1)], dim=-1)\n",
    "f = u.reshape(1,-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "intp = PolyHarmInterpolator(\n",
    "  c=c,\n",
    "  f=f,\n",
    "  order=order,\n",
    "  smoothing=smoothing,\n",
    "  device=device,\n",
    "  dtype=dtype\n",
    ")\n",
    "up = intp(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Evaluate interpolation error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "up = up.reshape(nb_pts, nb_pts)\n",
    "ue = torch.abs(u - up)\n",
    "emax = float(torch.max(ue))\n",
    "print(\"Max error:\", emax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 3, figsize=(12, 9))\n",
    "images = []\n",
    "# Exact solution\n",
    "im = ax[0].imshow(u.numpy(force=True), extent=[0,l,0,l])\n",
    "ax[0].set_xlabel(\"x\")\n",
    "ax[0].set_ylabel(\"y\")\n",
    "ax[0].set_title(\"Exact solution\")\n",
    "images.append(im)\n",
    "# Interpolated solution\n",
    "im = ax[1].imshow(up.numpy(force=True), extent=[0,l,0,l])\n",
    "ax[1].set_xlabel(\"x\")\n",
    "ax[1].set_title(\"Interpolated solution\")\n",
    "images.append(im)\n",
    "# Absolute error\n",
    "im = ax[2].imshow(ue.numpy(force=True), extent=[0,l,0,l])\n",
    "ax[2].set_xlabel(\"x\")\n",
    "ax[2].set_title(\"Absolute error\")\n",
    "images.append(im)\n",
    "# Color bar\n",
    "for i, im in enumerate(images):\n",
    "  divider = make_axes_locatable(ax[i])\n",
    "  cax = divider.append_axes('right', size='5%', pad=0.1)\n",
    "  fig.colorbar(im, cax=cax, orientation='vertical')\n",
    "fig.tight_layout()\n",
    "plt.show()\n",
    "plt.close()"
   ]
  }
 ],
 "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.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
