{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from pyharmx import PolyHarmInterpolator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Interpolation\n",
    "smoothing = 1e-2\n",
    "order = 2\n",
    "# Data\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 3D data on 2D grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data coordinates\n",
    "c = torch.rand(1, nb_pts, 2) * l\n",
    "# Data vectors at each point\n",
    "f = torch.rand(1, nb_pts, 3)\n",
    "# Query coordinates (100x100 grid of points)\n",
    "x = torch.linspace(0, l, nb_pts)\n",
    "y = torch.linspace(0, l, nb_pts)\n",
    "grid = torch.meshgrid(x, y, indexing='ij')\n",
    "grid = torch.stack(grid, dim=-1).reshape(1, -1, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interpolate"
   ]
  },
  {
   "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",
    "fp = intp(grid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fp0 = fp.numpy(force=True).reshape(nb_pts, nb_pts, 3)[:,:,0]\n",
    "plt.imshow(fp0, extent=[0,l,0,l])\n",
    "plt.title('Interpolated solution')\n",
    "plt.show()"
   ]
  }
 ],
 "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
}
