diff --git a/examples/notebooks/Getting Started/Tutorial 4 - Setting parameter values.ipynb b/examples/notebooks/Getting Started/Tutorial 4 - Setting parameter values.ipynb index 4a77d2d8b6..dd1b70142b 100644 --- a/examples/notebooks/Getting Started/Tutorial 4 - Setting parameter values.ipynb +++ b/examples/notebooks/Getting Started/Tutorial 4 - Setting parameter values.ipynb @@ -135,8 +135,8 @@ " 'EC initial concentration in electrolyte [mol.m-3]': 4541.0,\n", " 'Electrode height [m]': 0.065,\n", " 'Electrode width [m]': 1.58,\n", - " 'Electrolyte conductivity [S.m-1]': ,\n", - " 'Electrolyte diffusivity [m2.s-1]': ,\n", + " 'Electrolyte conductivity [S.m-1]': ,\n", + " 'Electrolyte diffusivity [m2.s-1]': ,\n", " 'Initial concentration in electrolyte [mol.m-3]': 1000.0,\n", " 'Initial concentration in negative electrode [mol.m-3]': 29866.0,\n", " 'Initial concentration in positive electrode [mol.m-3]': 17038.0,\n", @@ -407,7 +407,7 @@ " 'Negative electrode diffusivity [m2.s-1]': 3.3e-14,\n", " 'Negative electrode double-layer capacity [F.m-2]': 0.2,\n", " 'Negative electrode electrons in reaction': 1.0,\n", - " 'Negative electrode exchange-current density [A.m-2]': ,\n", + " 'Negative electrode exchange-current density [A.m-2]': ,\n", " 'Negative electrode porosity': 0.25,\n", " 'Negative electrode specific heat capacity [J.kg-1.K-1]': 700.0,\n", " 'Negative electrode surface area to volume ratio [m-1]': 383959.0,\n", @@ -675,7 +675,7 @@ " 'Positive electrode diffusivity [m2.s-1]': 4e-15,\n", " 'Positive electrode double-layer capacity [F.m-2]': 0.2,\n", " 'Positive electrode electrons in reaction': 1.0,\n", - " 'Positive electrode exchange-current density [A.m-2]': ,\n", + " 'Positive electrode exchange-current density [A.m-2]': ,\n", " 'Positive electrode porosity': 0.335,\n", " 'Positive electrode specific heat capacity [J.kg-1.K-1]': 700.0,\n", " 'Positive electrode surface area to volume ratio [m-1]': 382184.0,\n", @@ -730,8 +730,8 @@ "output_type": "stream", "text": [ "EC initial concentration in electrolyte [mol.m-3]\t4541.0\n", - "Electrolyte conductivity [S.m-1]\t\n", - "Electrolyte diffusivity [m2.s-1]\t\n", + "Electrolyte conductivity [S.m-1]\t\n", + "Electrolyte diffusivity [m2.s-1]\t\n", "Initial concentration in electrolyte [mol.m-3]\t1000.0\n", "Negative electrode Bruggeman coefficient (electrolyte)\t1.5\n", "Positive electrode Bruggeman coefficient (electrolyte)\t1.5\n", @@ -753,13 +753,13 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "b5f87571fee74cbb90479e86401f1eb4", + "model_id": "972080a3919746689eafda23f53570fc", "version_major": 2, "version_minor": 0 }, @@ -796,7 +796,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -813,7 +813,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -829,13 +829,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "947dc63e8bf449028214c950ff6f6844", + "model_id": "2429167130dd43038d4689be994e39f9", "version_major": 2, "version_minor": 0 }, @@ -864,12 +864,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can also simulate drive cycles by passing the data directly" + "### Drive cycle" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also simulate the PyBaMM default drive cycles by calling the dataset directly" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -880,18 +887,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Then we can just define the model and solve it" + "Then we can just define the model and solve it. In this case we do not need to specify a time interval to solve as PyBaMM automatically picks it from data" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "08ca39eec4544f6b86e227156d3937b2", + "model_id": "899c5194ed124a76a3cbc9ea8c372956", "version_major": 2, "version_minor": 0 }, @@ -910,6 +917,74 @@ "sim.plot([\"Current [A]\", \"Terminal voltage [V]\"])" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Alternatively, you can implement your own drive cycles importing the dataset and creating an interpolant to pass as the current function." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd # needed to read the csv data file\n", + "\n", + "# Import drive cycle from file\n", + "drive_cycle = pd.read_csv(\"../../../pybamm/input/drive_cycles/US06.csv\", comment=\"#\", header=None).to_numpy()\n", + "\n", + "# Create interpolant\n", + "timescale = parameter_values.evaluate(model.timescale)\n", + "current_interpolant = pybamm.Interpolant(drive_cycle, timescale * pybamm.t)\n", + "\n", + "# Set drive cycle\n", + "parameter_values[\"Current function [A]\"] = current_interpolant" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then, again, the model can be solved as usual but notice that now we do need to pass `t_eval` to the solver." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "349094f0a1dc43ecbf1fa0b67931823d", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.0, description='t', max=600.0, step=6.0), Output()), _dom_classes=('…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "model = pybamm.lithium_ion.SPMe()\n", + "sim = pybamm.Simulation(model, parameter_values=parameter_values)\n", + "t_eval = drive_cycle[:, 0] # define t_eval from the dataset\n", + "sim.solve(t_eval)\n", + "sim.plot([\"Current [A]\", \"Terminal voltage [V]\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Custom current function" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -919,7 +994,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -940,13 +1015,13 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "e166a9f922d14eaebe4be57072829277", + "model_id": "d382e196729d4354a4e8fd50d700c50e", "version_major": 2, "version_minor": 0 }, @@ -976,9 +1051,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "PyBaMM development (env)", "language": "python", - "name": "python3" + "name": "pybamm-dev" }, "language_info": { "codemirror_mode": { @@ -990,7 +1065,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.6.8" } }, "nbformat": 4, diff --git a/examples/notebooks/change-input-current.ipynb b/examples/notebooks/change-input-current.ipynb index 3bc1a17255..af64514c8a 100644 --- a/examples/notebooks/change-input-current.ipynb +++ b/examples/notebooks/change-input-current.ipynb @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -75,7 +75,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "807f6dec221b4226aea64326d51c751b", + "model_id": "b90f73879a2c45cea9f0ffa6df8a0eaa", "version_major": 2, "version_minor": 0 }, @@ -124,7 +124,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "eaab57697cef4852b4fbab5ee1f7efd7", + "model_id": "7a1b19df4edd47289d78802ff345bda2", "version_major": 2, "version_minor": 0 }, @@ -162,7 +162,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "a72be467b6f84733927cb2e7a5c4b3f9", + "model_id": "c5adb83033c84f06a8478ae913e89044", "version_major": 2, "version_minor": 0 }, @@ -209,6 +209,72 @@ "Note that some solvers try to evaluate the model equations at a very large value of `t` during the first step. This may raise a warning if the time requested by the solver is outside of the range of the data provided. However, this does not affect the solve since this large timestep is rejected by the solver, and a suitable shorter initial step is taken." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In some cases you might want to run a drive cycle that is not implemented in PyBaMM. In this case, you can create an interpolant from the custom data set and pass it as the current function." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "a61ea9c059754b40b790835e87774052", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.0, description='t', max=600.0, step=6.0), Output()), _dom_classes=('…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import pandas as pd # needed to read the csv data file\n", + "\n", + "model = pybamm.lithium_ion.DFN()\n", + "\n", + "# create geometry\n", + "geometry = model.default_geometry\n", + "\n", + "# import drive cycle from file\n", + "drive_cycle = pd.read_csv(\"pybamm/input/drive_cycles/US06.csv\", comment=\"#\", header=None).to_numpy()\n", + "\n", + "# load parameter values\n", + "param = model.default_parameter_values\n", + "\n", + "# create interpolant\n", + "timescale = param.evaluate(model.timescale)\n", + "current_interpolant = pybamm.Interpolant(drive_cycle, timescale * pybamm.t)\n", + "\n", + "# set drive cycle and process model and geometry\n", + "param[\"Current function [A]\"] = current_interpolant\n", + "param.process_model(model)\n", + "param.process_geometry(geometry)\n", + "\n", + "# set mesh\n", + "mesh = pybamm.Mesh(geometry, model.default_submesh_types, model.default_var_pts)\n", + "\n", + "# discretise model\n", + "disc = pybamm.Discretisation(mesh, model.default_spatial_methods)\n", + "disc.process_model(model)\n", + "\n", + "# simulate US06 drive cycle (duration 600 seconds)\n", + "t_eval = np.linspace(0, 600, 600)\n", + "solution = solver.solve(model, t_eval)\n", + "\n", + "# plot\n", + "quick_plot = pybamm.QuickPlot(solution)\n", + "quick_plot.dynamic_plot();" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -222,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -244,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -275,13 +341,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "7dff98a2d9a44f87b71abeb0a94e16c9", + "model_id": "75930d9bd3da40dab88ff2b69e0571a7", "version_major": 2, "version_minor": 0 }, @@ -324,9 +390,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "PyBaMM development (env)", "language": "python", - "name": "python3" + "name": "pybamm-dev" }, "language_info": { "codemirror_mode": { @@ -338,7 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.7" + "version": "3.6.8" } }, "nbformat": 4,