{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib ipympl\n",
    "import pyvisco as visco\n",
    "from pyvisco import styles\n",
    "styles.format_fig()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "# Verification - Comparison with APDL 2021 R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Master curve in frequency domain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load user master curve in frequency domain and user shift factors\n",
    "data = visco.load.file('./freq_master/freq_user_master.csv')\n",
    "shift = visco.load.file('./freq_master/freq_user_master__shift_factors.csv')\n",
    "RefT = -5\n",
    "domain = 'freq'\n",
    "modul = 'E'\n",
    "df_master, units = visco.load.user_master(data, domain, RefT, modul)\n",
    "df_aT = visco.load.user_shift(shift)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "msg = (\"Storage modulus ('{0}_stor') and \"    \n",
    "      + \"loss modulus('{0}_loss') must have same unit!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Storage modulus ('{0}_stor') and loss modulus('{0}_loss') must have same unit!\n"
     ]
    }
   ],
   "source": [
    "print(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d825d4f8067c4bbfa6aa3ed5c9173702",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 43032 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, 43032 characters]' width=400.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": [
    "#Shift functions\n",
    "df_WLF = visco.shift.fit_WLF(df_master.RefT, df_aT)\n",
    "df_poly_C, df_poly_K = visco.shift.fit_poly(df_aT)\n",
    "\n",
    "fig_shift, df_shift = visco.shift.plot(df_aT, df_WLF, df_poly_C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a5264c2b50144236b391582579b17f2a",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 41496 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, 41496 characters]' width=400.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": [
    "#Smooth master curve\n",
    "win = 5\n",
    "df_master = visco.master.smooth(df_master, win)\n",
    "fig_smooth = visco.master.plot_smooth(df_master, units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7ff16f10e2374c0794091622c7a2b43a",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 36636 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, 36636 characters]' width=400.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": [
    "#Discretize number of Prony terms\n",
    "df_dis = visco.prony.discretize(df_master)\n",
    "fig_dis = visco.prony.plot_dis(df_master, df_dis, units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b9b94af5866f4733a49b7caf2bbc6fb5",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 40476 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, 40476 characters]' width=400.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": [
    "#Fit Prony series parameter\n",
    "prony, df_GMaxw = visco.prony.fit(df_dis)\n",
    "fig_fit = visco.prony.plot_fit(df_master, df_GMaxw, units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5142ab5f8c7a4ae49e213a8a23e84662",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 43024 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, 43024 characters]' width=400.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": [
    "#Plot Generalized Maxwell model\n",
    "fig_GMaxw = visco.prony.plot_GMaxw(df_GMaxw, units);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b10196f405394bf293304bf799cf7985",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 48664 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, 48664 characters]' width=400.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": [
    "#Compare Python fit with ANSYS APDL\n",
    "df_prony_ANSYS = visco.verify.load_prony_ANSYS('./freq_master/prony_terms.MPL')\n",
    "prony_ANSYS = visco.verify.prep_prony_ANSYS(df_prony_ANSYS, prony)\n",
    "nprony = prony_ANSYS['df_terms'].shape[0]\n",
    "df_GMaxw_ANSYS = visco.prony.calc_GMaxw(**prony_ANSYS, decades = nprony)\n",
    "fig_fit_ANSYS = visco.verify.plot_fit_ANSYS(df_master, df_GMaxw, df_GMaxw_ANSYS, units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "127540125e9547fb8950d157300e8fad",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 18808 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, 18808 characters]' width=800.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": [
    "fig_coeff = visco.prony.plot_param([prony, prony_ANSYS], ['Python', 'Ansys'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Optional optimization of number of Prony terms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prony series N = 24: Convergence criterion reached!\n",
      "Prony series N = 22: Convergence criterion reached!\n",
      "Prony series N = 20: Convergence criterion reached!\n",
      "Prony series N = 19: Convergence criterion reached!\n",
      "Prony series N = 18: Convergence criterion reached!\n",
      "Prony series N = 17: Convergence criterion reached!\n",
      "Prony series N = 16: Convergence criterion reached!\n",
      "Prony series N = 15: Convergence criterion reached!\n",
      "Prony series N = 14: Convergence criterion reached!\n",
      "Prony series N = 13: Convergence criterion reached!\n",
      "Prony series N = 12: Convergence criterion reached!\n",
      "Prony series N = 11: Convergence criterion reached!\n",
      "Prony series N = 10: Convergence criterion reached!\n",
      "Prony series N = 09: Convergence criterion reached!\n",
      "Prony series N = 08: Convergence criterion reached!\n",
      "Prony series N = 07: Convergence criterion reached!\n",
      "Prony series N = 06: Convergence criterion reached!\n",
      "Prony series N = 05: Convergence criterion reached!\n",
      "Prony series N = 04: Convergence criterion reached!\n",
      "Prony series N = 03: Convergence criterion reached!\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cc3dea3fce204e68bbf8c48e4295926d",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 41872 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, 41872 characters]' width=400.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": "6543b2fd397049b19d93fa5e35323f19",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 29504 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, 29504 characters]' width=400.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": [
    "dict_prony, N_opt, N_opt_err = visco.opt.nprony(df_master, prony, window='min')\n",
    "df_GMaxw_opt, fig_opt = visco.opt.plot_fit(df_master, dict_prony, N_opt, units)\n",
    "fig_res = visco.opt.plot_residual(N_opt_err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1e21f87aa5a44393960b357b857e6c6e",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 24212 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, 24212 characters]' width=800.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": [
    "fig_coeff = visco.prony.plot_param([prony, dict_prony[N_opt]], ['initial', 'optimized'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  },
  "voila": {
   "template": "classic"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
