{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from aesim.simba import ProjectRepository, ThermalData\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import Select, interactive\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "igbt_xml_list = [filename for filename in os.listdir('./ThermalDataFile/') if filename.endswith('IGBT.xml')]\n",
    "\n",
    "scrolling_menu = Select(options=igbt_xml_list, rows=len(igbt_xml_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def handle_selection(igbt_xml):\n",
    "    file_path = os.path.join(os.getcwd(), \"scrolling_menu_thermal_buck.jsimba\")\n",
    "    project = ProjectRepository(file_path)\n",
    "    design = project.GetDesignByName('Design')\n",
    "    design.TransientAnalysis.StopAtSteadyState = True\n",
    "    igbt = design.Circuit.GetDeviceByName('IGBT1')\n",
    "    for scope in igbt.Scopes:\n",
    "        if scope.Name == 'Junction Temperature (°)' or scope.Name == 'Average Total Losses (W)':\n",
    "            scope.Enabled = True\n",
    "\n",
    "    igbt.ThermalData = ThermalData('ThermalDataFile/' + igbt_xml)\n",
    "\n",
    "    job = design.TransientAnalysis.NewJob()\n",
    "    status = job.Run()\n",
    "    if str(status) != \"OK\": \n",
    "        print (\"\\nSimulation Failed \")\n",
    "        print (job.Summary()[:-1])\n",
    "\n",
    "    Tj = job.GetSignalByName('IGBT1 - Junction Temperature (°)').DataPoints\n",
    "    Loss = job.GetSignalByName('IGBT1 - Average Total Losses (W)').DataPoints\n",
    "    Tj_time = job.GetSignalByName('IGBT1 - Junction Temperature (°)').TimePoints\n",
    "    Loss_time = job.GetSignalByName('IGBT1 - Average Total Losses (W)').TimePoints\n",
    "        \n",
    "    fig, ax = plt.subplots()\n",
    "    ax.set_title(\"IGBT - Junction Temperature (°)\")\n",
    "    ax.set_ylabel('Junction Temperature (°)')\n",
    "    ax.set_xlabel('time (s)')\n",
    "    ax.plot(Tj_time,Tj)\n",
    "\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.set_title(\"IGBT - Average Total Losses (W)\")\n",
    "    ax.set_ylabel('Average Total Losses (W)')\n",
    "    ax.set_xlabel('time (s)')\n",
    "    ax.plot(Loss_time,Loss)\n",
    "    plt.show\n",
    "    print(\"Selected item:\", igbt_xml)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a0c511dd1c1f4d2abab90edaa130dff0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Select(description='igbt_xml', options=('IGW15T120_IGBT.xml', 'IGW40T120_IGBT.xml', 'IGW…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "if os.environ.get('SIMBA_SCRIPT_TEST') == 'True':\n",
    "    if igbt_xml_list:\n",
    "        handle_selection(igbt_xml_list[0])\n",
    "    else:\n",
    "        print('No IGBT XML files found in ./ThermalDataFile/')\n",
    "else:\n",
    "    interactive_widget = interactive(handle_selection, igbt_xml=scrolling_menu)\n",
    "\n",
    "    display(interactive_widget)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv",
   "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.13.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
