{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.polynomial import Polynomial\n",
    "from numpy.polynomial.polynomial import polyval\n",
    "from Turbinen_class_file import Francis_turbine_class\n",
    "from mpl_toolkits import mplot3d\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#importing pressure conversion function\n",
    "import sys\n",
    "import os\n",
    "current = os.path.dirname(os.path.realpath('messy.ipynb'))\n",
    "parent = os.path.dirname(current)\n",
    "sys.path.append(parent)\n",
    "from functions.pressure_conversion import pressure_conversion\n",
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "T1 = Francis_turbine_class()\n",
    "csv = T1.raw_csv\n",
    "\n",
    "T1.extract_csv()\n",
    "ps  = T1.raw_ps_vec\n",
    "LAs = T1.raw_LA_vec\n",
    "Qs  = T1.raw_Qs_mat\n",
    "\n",
    "Q_fun = T1.get_Q_fun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_min = np.min(ps)\n",
    "p_max = np.max(ps)\n",
    "\n",
    "n_p = 100\n",
    "n_LA = 200\n",
    "\n",
    "ps_vec = np.linspace(p_min,p_max,n_p)\n",
    "ind1 = np.argmin(np.abs(ps_vec-np.min(ps)))\n",
    "ind2 = np.argmin(np.abs(ps_vec-np.max(ps)))\n",
    "LA_vec = np.linspace(0,1,n_LA)\n",
    "\n",
    "Q_int = np.reshape(Q_fun(ps_vec,LA_vec),[n_LA,n_p])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "poly_deg = 7\n",
    "n_LAs = np.size(LAs)\n",
    "fit_coeff_mat = np.empty([n_LAs,poly_deg+1])\n",
    "fit_weights = np.ones_like(ps)\n",
    "fit_weights[0] = 1000\n",
    "\n",
    "for i in range(n_LAs):\n",
    "    x = ps\n",
    "    y = Qs[i,:]\n",
    "    fit_coeff_mat[i,:] = np.polynomial.polynomial.Polynomial.fit(x,y,poly_deg,w=fit_weights).convert().coef\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "Qs_poly = np.empty([n_LAs,n_p])\n",
    "for i in range(n_LAs):\n",
    "    Qs_poly[i,:] = polyval(ps_vec,fit_coeff_mat[i,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x2dfaebe2220>"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "667375b5fbc34d41b8d8238cc1cbfa8d",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 98180 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 98180 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "XX1,YY1 = np.meshgrid(ps,LAs)\n",
    "\n",
    "fig1 = plt.figure()\n",
    "ax1 = plt.axes(projection='3d')\n",
    "\n",
    "ax1.plot_surface(XX1, YY1, Qs,cmap='viridis', edgecolor='none')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x2dfaec28b20>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "98df0bde396247f29d513c1229bb1f54",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 150848 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 150848 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "XX2,YY2 = np.meshgrid(ps_vec,LA_vec)\n",
    "\n",
    "fig2 = plt.figure()\n",
    "ax2 = plt.axes(projection='3d')\n",
    "\n",
    "ax2.plot_surface(XX2, YY2, Q_int,cmap='viridis', edgecolor='none')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x2dfafb82310>]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6278e4d6be114c629d964392d9b296ce",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 23424 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 23424 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig3 = plt.figure()\n",
    "ax3 = plt.axes()\n",
    "\n",
    "# ax3.scatter(XX1[-1,:], (Qs[-1,:]-((XX1[-1,:]-XX1[-1,0])/(XX1[-1,-1]-XX1[-1,0])*(Qs[-1,-1]-Qs[-1,0])+Qs[-1,0])))\n",
    "# ax3.scatter(XX2[-1,:], (Q_int[-1,:]-((XX2[-1,:]-XX2[-1,ind1])/(XX2[-1,ind2]-XX2[-1,ind1])*(Q_int[-1,ind2]-Q_int[-1,0])+Q_int[-1,ind1])))\n",
    "ax3.plot(XX2[-1,:], Q_int[-1,:],'.-')\n",
    "ax3.plot(ps_vec, Qs_poly[-1,:],'+')\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('Georg_DT_Slot3')",
   "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.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "84fb123bdc47ab647d3782661abcbe80fbb79236dd2f8adf4cef30e8755eb2cd"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
