{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6173c502-e4f2-4eef-a993-ea532ef8415c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import repeat as rp\n",
    "import repeat.parametric_study as ps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a53a925-553f-4edd-ae0d-4a2ac49fe948",
   "metadata": {},
   "outputs": [],
   "source": [
    "user = rp.User(\n",
    "    \"enter a unique API key from REPEAT user cabinet instead of this comment\"\n",
    ")\n",
    "app = rp.Application(user)\n",
    "# enter a project ID from REPEAT application instead of 12345\n",
    "project = 12345\n",
    "app = rp.Application(user)\n",
    "t_interval = rp.TimeInterval(start = 900, end = 1000)\n",
    "model = app.get_exploration_model(project, t_interval)\n",
    "print(model, model.existing_variables, sep = \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22dda19a-ef77-4b5d-a5d7-0a502c70034a",
   "metadata": {},
   "outputs": [],
   "source": [
    "variable_values = dict(\n",
    "    PRESS = np.linspace(6.75e6, 9.42e6, num = 21),\n",
    "    TEMP = np.array([20.]),\n",
    ")\n",
    "input_data = ps.get_cartesian_product(model, variable_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaa72b41-090f-432a-8d87-dafc84244c7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_names = [\"flowrate_2\", \"approx_flowrate_2\"]\n",
    "design_cases = input_data.copy(deep = True)\n",
    "for param in result_names:\n",
    "    design_cases[param] = pd.NA\n",
    "\n",
    "design_cases.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "009c9309-eebc-4d6a-b485-fc29ebf15f22",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solve(input_data, design_cases):\n",
    "    for case_num, variables in input_data.iterrows():\n",
    "        with model as md:\n",
    "            md.run(variables)\n",
    "            for param in result_names:\n",
    "                design_cases.loc[case_num, param] = md.get_results(param).iloc[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd0456e1-795d-4d66-9b70-5210fc77185d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "solve(input_data, design_cases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c75ed40c-93a8-4c2d-8440-b06a2fd597e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_relative_error(flow_exact, flow_approx):\n",
    "    return (flow_exact - flow_approx).abs() / flow_exact * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94ed5fe1-b065-4639-ae40-9784fe660cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "design_cases[\"error\"] = calc_relative_error(\n",
    "    flow_exact = design_cases[\"flowrate_2\"], \n",
    "    flow_approx = design_cases[\"approx_flowrate_2\"]\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58bc4392-e91e-4406-aef1-d3754d5d0b80",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = design_cases.plot(x = \"PRESS\", y = \"flowrate_2\", c = \"tab:blue\")\n",
    "design_cases.plot.scatter(x = \"PRESS\", y = \"approx_flowrate_2\", c = \"tab:red\", ax = ax)\n",
    "ax.set_xlabel(\"pressure $[Pa]$\")\n",
    "ax.set_ylabel(\"flowrate $[kg/s]$\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e046fc44-b24b-4cf1-947a-b569a3d97e2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'{design_cases[\"error\"].max() :.3f} %')"
   ]
  }
 ],
 "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.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
