From ac8f0117dc13856d45faca874f3e082bb759f164 Mon Sep 17 00:00:00 2001 From: Daniel Marchand Date: Fri, 30 Jun 2023 15:23:26 +0000 Subject: [PATCH] fixed display bug for ap3 and added defaults for ap2 and ap3 --- uc3/steps.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ uc3_ap2.ipynb | 28 ++++++++++----------- uc3_ap3.ipynb | 70 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 128 insertions(+), 40 deletions(-) diff --git a/uc3/steps.py b/uc3/steps.py index 283f995..8a8841c 100644 --- a/uc3/steps.py +++ b/uc3/steps.py @@ -572,3 +572,73 @@ def _observe_configuration(self, _, change): self.final_output.value = ( "

Configuration

[Please configure user inputs]" ) + +class DisplayAp3FinalOutput(ipw.VBox, WizardAppWidgetStep): + + output = traitlets.Instance(ArrayData, allow_none=True) + + def __init__(self, **kwargs): + self.output_figure = plt.figure() + self.final_output = ipw.Output() + + super().__init__([self.final_output], **kwargs) + + @traitlets.observe("output") + def _call_observe(self, change): + self._observe_configuration(self, change) + + def _observe_configuration(self, _, change): + if change["new"]: + output = self.output + + # Assuming these arrays have the same length + temperature_setpoint = output.get_array("temperature_setpoint") + conversion = output.get_array("conversion") + max_bed_temperature = output.get_array("max_bed_temperature") + + # Create a DataFrame using the output variables + data = { + 'temperature_setpoint': temperature_setpoint, + 'conversion': conversion, + 'max_bed_temperature': max_bed_temperature, + } + df = pd.DataFrame(data) + + # Create the plot using the plot_data function + fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(8, 12)) + + x = df['temperature_setpoint'] - 273.15 + + y1 = df['conversion'] + ax1.plot(x, y1) + ax1.grid() + ax1.set_xlabel('Temperature') + ax1.set_ylabel('Conversion') + + y2 = df['max_bed_temperature'] - 273.15 + ax2.plot(x, y2) + ax2.grid() + ax2.set_xlabel('Temperature') + ax2.set_ylabel('Max Bed Temperature') + + # Save the plot to a buffer + buffer = BytesIO() + plt.savefig(buffer, format='svg') + plt.close() + + # Convert the buffer to a base64-encoded string and create the SVG image + svg_data = buffer.getvalue().decode() + base64_data = base64.b64encode(svg_data.encode()).decode() + svg_image = f'' + + # Show the plot + self.final_output.clear_output(wait=True) + with self.final_output: + display(ipw.HTML(svg_image)) + + self.state = self.State.SUCCESS + else: + self.final_output.value = ( + "

Configuration

[Please configure user inputs]" + ) + diff --git a/uc3_ap2.ipynb b/uc3_ap2.ipynb index 48a297c..46fb160 100644 --- a/uc3_ap2.ipynb +++ b/uc3_ap2.ipynb @@ -108,20 +108,20 @@ "\n", "computer_code_setup_step = ComputerCodeSetupStep(auto_advance=True)\n", "configure_userinput_step = ConfigureUserInputStep(auto_advance=True, description_label_default=[ \n", - " ['dmf', 'dmf', 0],\n", - " ['gfr', 'gfr', 0],\n", - " ['ch4mf', 'ch4mf', 0],\n", - " ['h2omf', 'h2omf', 0],\n", - " ['surfa', 'surfa', 0],\n", - " ['amf', 'amf', 0],\n", - " ['macropor', 'macropor', 0],\n", - " ['macrotor', 'macrotor', 0],\n", - " ['k1', 'k1', 16676.31155],\n", - " ['ea1', 'ea1', 96636],\n", - " ['k2', 'k2', 6210120111],\n", - " ['ea2', 'ea2', 133788],\n", - " ['koeq', 'koeq', 1593.272279],\n", - " ['dhh20', 'dhh20', -4880]\n", + " ['dmf', 'Diluent mass fraction', 0.9],\n", + " ['gfr', 'Gas Flow Rate (mL/min)', 100],\n", + " ['ch4mf', 'CH4 Mole Fraction', 0.02],\n", + " ['h2omf', 'H20 Mole Fraction', 0],\n", + " ['surfa', 'Specific Surface Area (m^2/g)', 100],\n", + " ['amf', 'Active Mass Fraction', 0.01],\n", + " ['macropor', 'Catalyst Macroporosity', 0.1],\n", + " ['macrotor', 'Catalyst Tortuosity', 2],\n", + " ['k1', 'k1 (m/s)', 20000],\n", + " ['ea1', 'Ea1 (J/mol)', 10000],\n", + " ['k2', 'k2 (m^7 / mol^2 / s)', 60000000],\n", + " ['ea2', 'Ea2 (J/mol)', 150000],\n", + " ['koeq', 'K0eq (m^6/mol^2)', 2000],\n", + " ['dhh20', 'dH,H20 (J/mol)', -5000]\n", "])\n", "\n", "\n", diff --git a/uc3_ap3.ipynb b/uc3_ap3.ipynb index 48a297c..18405e3 100644 --- a/uc3_ap3.ipynb +++ b/uc3_ap3.ipynb @@ -72,7 +72,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "9f587b5d4ea34a4aad4deba0102f0c2d", + "model_id": "6901c81b59424728976b5114e6bae77b", "version_major": 2, "version_minor": 0 }, @@ -82,15 +82,27 @@ "output_type": "display_data" }, { - "ename": "NameError", - "evalue": "name 'DisplayAp2FinalOutput' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/tmp/ipykernel_26756/2862219995.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0mconfirm_userinput_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mConfirmUserInputStep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauto_advance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0mmonitorprocess_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMonitorProcessStep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauto_advance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 33\u001b[0;31m \u001b[0mdisplayfinaloutput_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDisplayAp2FinalOutput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauto_advance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# DisplayFinalOutput(auto_advance=False)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m ipw.dlink(\n", - "\u001b[0;31mNameError\u001b[0m: name 'DisplayAp2FinalOutput' is not defined" - ] + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "df374e1c4919421984cd55e9202142bd", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "WizardAppWidget(children=(HBox(children=(Button(description='Previous step', disabled=True, icon='step-backwar…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ @@ -103,31 +115,30 @@ " ConfigureUserInputStep,\n", " ConfirmUserInputStep,\n", " MonitorProcessStep,\n", - " DisplayAp2FinalOutput,\n", + " DisplayAp3FinalOutput,\n", ")\n", "\n", "computer_code_setup_step = ComputerCodeSetupStep(auto_advance=True)\n", "configure_userinput_step = ConfigureUserInputStep(auto_advance=True, description_label_default=[ \n", - " ['dmf', 'dmf', 0],\n", - " ['gfr', 'gfr', 0],\n", - " ['ch4mf', 'ch4mf', 0],\n", - " ['h2omf', 'h2omf', 0],\n", - " ['surfa', 'surfa', 0],\n", - " ['amf', 'amf', 0],\n", - " ['macropor', 'macropor', 0],\n", - " ['macrotor', 'macrotor', 0],\n", - " ['k1', 'k1', 16676.31155],\n", - " ['ea1', 'ea1', 96636],\n", - " ['k2', 'k2', 6210120111],\n", - " ['ea2', 'ea2', 133788],\n", - " ['koeq', 'koeq', 1593.272279],\n", - " ['dhh20', 'dhh20', -4880]\n", + " ['gfr', 'Gas Flow Rate (L/min)', 21],\n", + " ['ch4mf', 'CH4 Mole Fraction', 0.02],\n", + " ['h2omf', 'H20 Mole Fraction', 0],\n", + " ['surfa', 'Specific Surface Area (m^2/g)', 80.31],\n", + " ['amf', 'Active Mass Fraction', 0.02],\n", + " ['macropor', 'Washcoat Macroporosity', 0.536],\n", + " ['macrotor', 'Washcoat Tortuosity', 2.5],\n", + " ['k1', 'k1 (m/s)', 16676.31],\n", + " ['ea1', 'Ea1 (J/mol)', 966636],\n", + " ['k2', 'k2 (m^7 / mol^2 / s)', 6210120111],\n", + " ['ea2', 'Ea2 (J/mol)', 133788],\n", + " ['koeq', 'K0eq (m^6/mol^2)', 1593],\n", + " ['dhh20', 'dH,H20 (J/mol)', -4880]\n", "])\n", "\n", "\n", "confirm_userinput_step = ConfirmUserInputStep(auto_advance=True)\n", "monitorprocess_step = MonitorProcessStep(auto_advance=True)\n", - "displayfinaloutput_step = DisplayAp2FinalOutput(auto_advance=False) # DisplayFinalOutput(auto_advance=False)\n", + "displayfinaloutput_step = DisplayAp3FinalOutput(auto_advance=False) # DisplayFinalOutput(auto_advance=False)\n", "\n", "ipw.dlink(\n", " (configure_userinput_step, \"user_inputs\"),\n", @@ -158,6 +169,13 @@ " ]\n", ")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {