Skip to content

Commit

Permalink
Add outputs to example notebook
Browse files Browse the repository at this point in the history
Fixes doctests error
  • Loading branch information
pipliggins committed Nov 28, 2023
1 parent 95935a0 commit 04f4230
Showing 1 changed file with 130 additions and 16 deletions.
146 changes: 130 additions & 16 deletions docs/source/examples/notebooks/models/saving_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install pybamm -q # install PyBaMM if it is not installed\n",
"import pybamm\n",
Expand All @@ -43,9 +51,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<pybamm.solvers.solution.Solution at 0x2a4e10550>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Recreate the pybamm model from the JSON file\n",
"new_dfn_model = pybamm.load_model(\"sim_model_example.json\")\n",
Expand All @@ -65,13 +84,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "No variables to plot",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/pipliggins/Documents/repos/pybamm-local/docs/source/examples/notebooks/models/saving_models.ipynb Cell 7\u001b[0m line \u001b[0;36m8\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/pipliggins/Documents/repos/pybamm-local/docs/source/examples/notebooks/models/saving_models.ipynb#W6sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m plot_sim\u001b[39m.\u001b[39msolve([\u001b[39m0\u001b[39m, \u001b[39m3600\u001b[39m])\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/pipliggins/Documents/repos/pybamm-local/docs/source/examples/notebooks/models/saving_models.ipynb#W6sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m sims\u001b[39m.\u001b[39mappend(plot_sim)\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/pipliggins/Documents/repos/pybamm-local/docs/source/examples/notebooks/models/saving_models.ipynb#W6sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m pybamm\u001b[39m.\u001b[39;49mdynamic_plot(sims, time_unit\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mseconds\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n",
"File \u001b[0;32m~/Documents/repos/pybamm-local/pybamm/plotting/dynamic_plot.py:20\u001b[0m, in \u001b[0;36mdynamic_plot\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 9\u001b[0m \u001b[39mCreates a :class:`pybamm.QuickPlot` object (with arguments 'args' and keyword\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[39marguments 'kwargs') and then calls :meth:`pybamm.QuickPlot.dynamic_plot`.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[39m The 'QuickPlot' object that was created\u001b[39;00m\n\u001b[1;32m 18\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 19\u001b[0m kwargs_for_class \u001b[39m=\u001b[39m {k: v \u001b[39mfor\u001b[39;00m k, v \u001b[39min\u001b[39;00m kwargs\u001b[39m.\u001b[39mitems() \u001b[39mif\u001b[39;00m k \u001b[39m!=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mtesting\u001b[39m\u001b[39m\"\u001b[39m}\n\u001b[0;32m---> 20\u001b[0m plot \u001b[39m=\u001b[39m pybamm\u001b[39m.\u001b[39;49mQuickPlot(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs_for_class)\n\u001b[1;32m 21\u001b[0m plot\u001b[39m.\u001b[39mdynamic_plot(kwargs\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mtesting\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39mFalse\u001b[39;00m))\n\u001b[1;32m 22\u001b[0m \u001b[39mreturn\u001b[39;00m plot\n",
"File \u001b[0;32m~/Documents/repos/pybamm-local/pybamm/plotting/quick_plot.py:146\u001b[0m, in \u001b[0;36mQuickPlot.__init__\u001b[0;34m(self, solutions, output_variables, labels, colors, linestyles, shading, figsize, n_rows, time_unit, spatial_unit, variable_limits)\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[39m# check variables have been provided after any serialisation\u001b[39;00m\n\u001b[1;32m 145\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39many\u001b[39m(\u001b[39mlen\u001b[39m(m\u001b[39m.\u001b[39mvariables) \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m \u001b[39mfor\u001b[39;00m m \u001b[39min\u001b[39;00m models):\n\u001b[0;32m--> 146\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mAttributeError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mNo variables to plot\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 148\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mn_rows \u001b[39m=\u001b[39m n_rows \u001b[39mor\u001b[39;00m \u001b[39mint\u001b[39m(\n\u001b[1;32m 149\u001b[0m \u001b[39mlen\u001b[39m(output_variables) \u001b[39m/\u001b[39m\u001b[39m/\u001b[39m np\u001b[39m.\u001b[39msqrt(\u001b[39mlen\u001b[39m(output_variables))\n\u001b[1;32m 150\u001b[0m )\n\u001b[1;32m 151\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mn_cols \u001b[39m=\u001b[39m \u001b[39mint\u001b[39m(np\u001b[39m.\u001b[39mceil(\u001b[39mlen\u001b[39m(output_variables) \u001b[39m/\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mn_rows))\n",
"\u001b[0;31mAttributeError\u001b[0m: No variables to plot"
]
}
],
"source": [
"dfn_models = [dfn_model, new_dfn_model]\n",
"sims = []\n",
Expand All @@ -94,9 +127,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "81d8329fab424264bd56c65d53d34f63",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=3600.0, step=36.0), Output()), _dom_classes=…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<pybamm.plotting.quick_plot.QuickPlot at 0x111963010>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# using the first simulation, save a new file which includes a list of the model variables\n",
"dfn_sim.save_model(\"sim_model_variables\", variables=True)\n",
Expand Down Expand Up @@ -130,9 +188,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<pybamm.models.full_battery_models.lithium_ion.spm.SPM at 0x29cf65c90>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create the model\n",
"spm_model = pybamm.lithium_ion.SPM()\n",
Expand All @@ -156,7 +225,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -173,9 +242,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ce5addf4f59c447e97d2fbee633cb6e0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=1.0, step=0.01), Output()), _dom_classes=('w…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<pybamm.plotting.quick_plot.QuickPlot at 0x29c8a3d10>"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# read back in\n",
"new_spm_model = pybamm.load_model(\"example_model.json\")\n",
Expand Down Expand Up @@ -208,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -229,12 +323,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.\n",
"[2] Marc Doyle, Thomas F. Fuller, and John Newman. Modeling of galvanostatic charge and discharge of the lithium/polymer/insertion cell. Journal of the Electrochemical society, 140(6):1526–1533, 1993. doi:10.1149/1.2221597.\n",
"[3] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.\n",
"[4] Scott G. Marquis, Valentin Sulzer, Robert Timms, Colin P. Please, and S. Jon Chapman. An asymptotic derivation of a single particle model with electrolyte. Journal of The Electrochemical Society, 166(15):A3693–A3706, 2019. doi:10.1149/2.0341915jes.\n",
"[5] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.\n",
"\n"
]
}
],
"source": [
"pybamm.print_citations()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 04f4230

Please sign in to comment.