{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from Regler_class_file import P_controller_class\n",
    "from Regler_class_file import PI_controller_class\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # %matplotlib widget\n",
    "# pi_controller = PI_controller_class(setpoint=100,proportionality_constant=1,Ti = 30,timestep = 0.1)\n",
    "\n",
    "# t_max   = 100   #s\n",
    "# dt      = 0.1   #s\n",
    "# nt = int(t_max//dt)\n",
    "# t_vec = np.arange(0,nt+1,1)*dt\n",
    "\n",
    "# PV_0 = 50\n",
    "\n",
    "# PV_vec = np.full_like(t_vec,PV_0)\n",
    "\n",
    "# for i in range(1,nt+1):\n",
    "#     pi_controller.calculate_error(PV_vec[i-1])\n",
    "\n",
    "#     if i == 500:\n",
    "#         pi_controller.SP = 0.\n",
    "#     PV_vec[i] = PV_vec[i-1]+pi_controller.get_control_variable()\n",
    "\n",
    "\n",
    "\n",
    "# plt.plot(t_vec,PV_vec,'.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "SP_0 = 1.1\n",
    "SP_1 = 0.24\n",
    "PV_0 = 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "n = 10\n",
    "Kp_max = 5.\n",
    "Ti_max = 5.\n",
    "d_Kp = Kp_max/n\n",
    "d_Ti = Ti_max/n\n",
    "kp_vec = np.arange(1,n+1,1)*d_Kp\n",
    "Ti_vec = np.arange(1,n+1,1)*d_Ti\n",
    "\n",
    "XX,YY = np.meshgrid(kp_vec,Ti_vec)\n",
    "\n",
    "ise_mat = np.empty_like(XX)\n",
    "iae_mat = np.empty_like(XX)\n",
    "itse_mat = np.empty_like(XX)\n",
    "itae_mat = np.empty_like(XX)\n",
    "\n",
    "\n",
    "t_max   = 100   #s\n",
    "dt      = 0.05   #s\n",
    "nt = int(t_max//dt)\n",
    "t_vec = np.arange(0,nt+1,1)*dt\n",
    "\n",
    "for i in range(n):\n",
    "    if i%10 == 0:\n",
    "        print(i)\n",
    "    for j in range(n):\n",
    "        Kp = XX[i,j]\n",
    "        Ti = YY[i,j]\n",
    "\n",
    "        c = PI_controller_class(SP_0,Kp,Ti,dt)\n",
    "\n",
    "        PV_vec = np.full_like(t_vec,PV_0)\n",
    "\n",
    "        for t in range(1,nt+1):\n",
    "            c.calculate_error(PV_vec[t-1])\n",
    "\n",
    "            if t == 500:\n",
    "                c.SP = SP_1\n",
    "            PV_vec[t] = PV_vec[t-1]+c.get_control_variable()\n",
    "        \n",
    "        ise_mat[i,j],iae_mat[i,j],itse_mat[i,j],itae_mat[i,j] = np.log(c.get_performance_indicators())\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0x162b29d6880>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d9226014321c46daa6791bc1dfd0f06d",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 12636 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, 12636 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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "573c49fc68ec444583bcedf327db5c7c",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 11928 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, 11928 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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b5663fc661a64698ad0230c2174ef8a5",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 12748 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, 12748 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"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "224a5fbc49b344c4ab9b241e6bc56d96",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 11928 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, 11928 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": [
    "%matplotlib widget\n",
    "fig1,axs1 = plt.subplots(1,1)\n",
    "ise_min = np.min(ise_mat)\n",
    "ise_max = np.max(ise_mat)\n",
    "mesh1 = axs1.pcolormesh(XX,YY,ise_mat,cmap='RdBu', vmin=ise_min, vmax=ise_max,shading='nearest')\n",
    "fig1.colorbar(mesh1)\n",
    "fig2,axs2 = plt.subplots(1,1)\n",
    "iae_min = np.min(iae_mat)\n",
    "iae_max = np.max(iae_mat)\n",
    "mesh2 = axs2.pcolormesh(XX,YY,iae_mat,cmap='RdBu', vmin=iae_min, vmax=iae_max,shading='nearest')\n",
    "fig2.colorbar(mesh2)\n",
    "fig3,axs3 = plt.subplots(1,1)\n",
    "itse_min = np.min(itse_mat)\n",
    "itse_max = np.max(itse_mat)\n",
    "mesh3 = axs3.pcolormesh(XX,YY,itse_mat,cmap='RdBu', vmin=itse_min, vmax=itse_max,shading='nearest')\n",
    "fig3.colorbar(mesh3)\n",
    "fig4,axs4 = plt.subplots(1,1)\n",
    "itae_min = np.min(itae_mat)\n",
    "itae_max = np.max(itae_mat)\n",
    "mesh4 = axs4.pcolormesh(XX,YY,itae_mat,cmap='RdBu', vmin=itae_min, vmax=itae_max,shading='nearest')\n",
    "fig4.colorbar(mesh4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ise_ind = np.unravel_index(np.argmin(ise_mat,axis=None),ise_mat.shape)\n",
    "Kp_ise = XX[ise_ind]\n",
    "Ti_ise = YY[ise_ind]\n",
    "iae_ind = np.unravel_index(np.argmin(iae_mat,axis=None),iae_mat.shape)\n",
    "Kp_iae = XX[iae_ind]\n",
    "Ti_iae = YY[iae_ind]\n",
    "itse_ind = np.unravel_index(np.argmin(itse_mat,axis=None),itse_mat.shape)\n",
    "Kp_itse = XX[itse_ind]\n",
    "Ti_itse = YY[itse_ind]\n",
    "itae_ind = np.unravel_index(np.argmin(itae_mat,axis=None),itae_mat.shape)\n",
    "Kp_itae = XX[itae_ind]\n",
    "Ti_itae = YY[itae_ind]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x162b38c74f0>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "05f5663bd9ab4b23a30ec4ee94e41a1e",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 14292 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, 14292 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": [
    "t_max   = 100   #s\n",
    "dt      = 0.1   #s\n",
    "nt = int(t_max//dt)\n",
    "t_vec = np.arange(0,nt+1,1)*dt\n",
    "\n",
    "\n",
    "PV_vec_ise = np.full_like(t_vec,PV_0)\n",
    "PV_vec_iae = np.full_like(t_vec,PV_0)\n",
    "PV_vec_itse = np.full_like(t_vec,PV_0)\n",
    "PV_vec_itae = np.full_like(t_vec,PV_0)\n",
    "\n",
    "c_ise = PI_controller_class(SP_0,Kp_ise,Ti_ise,timestep = 0.1)\n",
    "c_iae = PI_controller_class(SP_0,Kp_iae,Ti_iae,timestep = 0.1)\n",
    "c_itse = PI_controller_class(SP_0,Kp_itse,Ti_itse,timestep = 0.1)\n",
    "c_itae = PI_controller_class(SP_0,Kp_itae,Ti_itae,timestep = 0.1)\n",
    "\n",
    "\n",
    "for i in range(1,nt+1):\n",
    "    c_ise.calculate_error(PV_vec_ise[i-1])\n",
    "    c_iae.calculate_error(PV_vec_iae[i-1])\n",
    "    c_itse.calculate_error(PV_vec_itse[i-1])\n",
    "    c_itae.calculate_error(PV_vec_itae[i-1])\n",
    "\n",
    "    if i == 500:\n",
    "        c_ise.SP = SP_1\n",
    "        c_iae.SP = SP_1\n",
    "        c_itse.SP = SP_1\n",
    "        c_itae.SP = SP_1\n",
    "\n",
    "    PV_vec_ise[i] = PV_vec_ise[i-1]+c_ise.get_control_variable()\n",
    "    PV_vec_iae[i] = PV_vec_iae[i-1]+c_iae.get_control_variable()\n",
    "    PV_vec_itse[i] = PV_vec_itse[i-1]+c_itse.get_control_variable()\n",
    "    PV_vec_itae[i] = PV_vec_itae[i-1]+c_itae.get_control_variable()\n",
    "\n",
    "fig5 = plt.figure()\n",
    "plt.plot(t_vec,PV_vec_ise,label='ise')\n",
    "plt.plot(t_vec,PV_vec_iae,'.-',label='iae')\n",
    "plt.plot(t_vec,PV_vec_itse,'+',label='itse')\n",
    "plt.plot(t_vec,PV_vec_itae,'*',label='itae')"
   ]
  }
 ],
 "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
}
