{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "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",
    "\n",
    "%matplotlib widget\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create turbine object\n",
    "T1 = Francis_turbine_class()\n",
    "csv = T1.raw_csv\n",
    "\n",
    "T1.extract_csv()\n",
    "ps_raw  = T1.raw_ps_vec\n",
    "n_ps_raw  = np.size(T1.raw_ps_vec)\n",
    "LAs_raw = T1.raw_LA_vec\n",
    "n_LAs_raw = np.size(T1.raw_LA_vec)\n",
    "Qs_raw  = T1.raw_Qs_mat\n",
    "\n",
    "Q_fun = T1.get_Q_fun()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# interpolate Qs for more LAs\n",
    "\n",
    "n_LA_int = 2001\n",
    "\n",
    "LA_int = np.linspace(0,1,n_LA_int,endpoint=True)\n",
    "\n",
    "Q_int = np.reshape(Q_fun(ps_raw,LA_int),[n_LA_int,n_ps_raw])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit a polynomial to the data from Qs_Raw\n",
    "    # receive a coefficient matrix \n",
    "poly_deg = 3\n",
    "fit_coeff_mat = np.empty([n_LA_int,poly_deg+1])\n",
    "fit_weights = np.ones_like(ps_raw)\n",
    "fit_weights[0] = 20 # put extra weight on the p=0 => Q=0 data\n",
    "\n",
    "for i in range(n_LA_int):\n",
    "    x = ps_raw\n",
    "    y = Q_int[i,:]\n",
    "    fit_coeff_mat[i,:] = np.polynomial.polynomial.Polynomial.fit(x,y,poly_deg,w=fit_weights).convert().coef\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_ext_min = 0\n",
    "p_ext_max = 2.5*np.max(ps_raw)\n",
    "n_p_ext = 200\n",
    "\n",
    "p_ext = np.linspace(p_ext_min,p_ext_max,n_p_ext)\n",
    "\n",
    "Qs_ext = np.zeros([n_LA_int,n_p_ext])\n",
    "\n",
    "for i in range(n_LA_int):\n",
    "    Qs_ext[i,:] = polyval(p_ext,fit_coeff_mat[i,:])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'LA = 0.05')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a89d999414644e6a8d81f1e32c31e49b",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 26604 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, 26604 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": [
    "LA_test = 0.05\n",
    "LA_index = int(np.argwhere(LA_int == LA_test))\n",
    "\n",
    "fig = plt.figure()\n",
    "plt.plot(p_ext,Qs_ext[LA_index,:])\n",
    "plt.xlabel('Druck [Pa]')\n",
    "plt.ylabel('Q [m³/s]')\n",
    "plt.title('LA = '+ str(LA_test))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Q_formel(p,LA):\n",
    "    x= LA\n",
    "    y = p*1e-5\n",
    "    a = -215.9324\n",
    "    b = +332.86766\n",
    "    c = -2.40164\n",
    "    d = +67.45335\n",
    "    e = -6.92688\n",
    "    f = +0.23341\n",
    "    g = -0.0225\n",
    "    h = +0.0499\n",
    "    i = -0.09458\n",
    "    j = +0.00324\n",
    "    k = +1.\n",
    "    return (a+b*x+c*x**2+d*y+e*y**2+f*y**3)/(1+g*x+h*x**2+i*y+j*y**2)*k/1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'P = 1145728.6432160805 [Pa]')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "32ee8a91fb3c43dcb085155ea58f3b41",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 25312 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, 25312 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": [
    "p_test,_ = pressure_conversion(11.4,'bar','Pa')\n",
    "p_index = int(np.argmin(abs(p_ext-p_test)))\n",
    "p_test2 = p_ext[p_index]\n",
    "\n",
    "fig = plt.figure()\n",
    "plt.plot(LA_int,Qs_ext[:,p_index])\n",
    "plt.plot(LA_int,Q_formel(p_test2,LA_int),'+')\n",
    "plt.xlabel('LA [-]')\n",
    "plt.ylabel('Q [m³/s]')\n",
    "plt.title('P = '+ str(p_test2) + ' [Pa]')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Q [m³/s]')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5b7343c1b0f244e8a40bcb8c91920c68",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 158552 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, 158552 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": [
    "XX,YY = np.meshgrid(p_ext,LA_int)\n",
    "\n",
    "fig1 = plt.figure()\n",
    "ax1 = plt.axes(projection='3d')\n",
    "\n",
    "ax1.plot_surface(XX, YY, Qs_ext,cmap='viridis', edgecolor='none')\n",
    "ax1.set_xlabel('P [Pa]')\n",
    "ax1.set_ylabel('LA')\n",
    "ax1.set_zlabel('Q [m³/s]')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = T1.raw_csv"
   ]
  }
 ],
 "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
}
