Skip to content

Commit

Permalink
pybamm-team#1056 Tino's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brosaplanella committed Jul 29, 2020
1 parent bf2f41a commit 9d818a9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"outputs": [],
"source": [
"%pip install pybamm -q # install PyBaMM if it is not installed\n",
"#%pip install pybamm -q # install PyBaMM if it is not installed\n",
"import pybamm\n",
"import os\n",
"os.chdir(pybamm.__path__[0]+'/..')"
Expand Down Expand Up @@ -137,8 +129,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]': <function electrolyte_conductivity_Nyman2008 at 0x7f327d7b0a60>,\n",
" 'Electrolyte diffusivity [m2.s-1]': <function electrolyte_diffusivity_Nyman2008 at 0x7f327d7b0f28>,\n",
" 'Electrolyte conductivity [S.m-1]': <function electrolyte_conductivity_Nyman2008 at 0x7f4414a2a268>,\n",
" 'Electrolyte diffusivity [m2.s-1]': <function electrolyte_diffusivity_Nyman2008 at 0x7f4414a2a1e0>,\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",
Expand Down Expand Up @@ -409,7 +401,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]': <function graphite_LGM50_electrolyte_exchange_current_density_Chen2020 at 0x7f327d7b0ea0>,\n",
" 'Negative electrode exchange-current density [A.m-2]': <function graphite_LGM50_electrolyte_exchange_current_density_Chen2020 at 0x7f4414a66ea0>,\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",
Expand Down Expand Up @@ -677,7 +669,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]': <function nmc_LGM50_electrolyte_exchange_current_density_Chen2020 at 0x7f327d7720d0>,\n",
" 'Positive electrode exchange-current density [A.m-2]': <function nmc_LGM50_electrolyte_exchange_current_density_Chen2020 at 0x7f4414a2a158>,\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",
Expand Down Expand Up @@ -732,8 +724,8 @@
"output_type": "stream",
"text": [
"EC initial concentration in electrolyte [mol.m-3]\t4541.0\n",
"Electrolyte conductivity [S.m-1]\t<function electrolyte_conductivity_Nyman2008 at 0x7f327d7b0a60>\n",
"Electrolyte diffusivity [m2.s-1]\t<function electrolyte_diffusivity_Nyman2008 at 0x7f327d7b0f28>\n",
"Electrolyte conductivity [S.m-1]\t<function electrolyte_conductivity_Nyman2008 at 0x7f4414a2a268>\n",
"Electrolyte diffusivity [m2.s-1]\t<function electrolyte_diffusivity_Nyman2008 at 0x7f4414a2a1e0>\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",
Expand Down Expand Up @@ -761,7 +753,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "400ba653a0f9412f8e12608a803cd60f",
"model_id": "f1d953c18b294933a33140e7b890cd1d",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -837,7 +829,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "da167df5923e46fe9ff4a0873c97224d",
"model_id": "63c4c3143dd94d01b569a31435127435",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -873,64 +865,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also simulate the PyBaMM default drive cycles by calling the dataset directly"
"You can implement drive cycles importing the dataset and creating an interpolant to pass as the current function."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"parameter_values[\"Current function [A]\"] = \"[current data]US06\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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": 12,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e690dcd0af224966b65bc23ff2442003",
"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",
"sim.solve()\n",
"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",
Expand All @@ -949,18 +891,18 @@
"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."
"Note that your drive cycle data can be stored anywhere, you just need to pass the path of the file. Then, again, the model can be solved as usual but notice that now, if `t_eval` is not specified, the solver will take the time points from the data set."
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74baef19208a404496839654d684e38e",
"model_id": "3966a3e7ea5040c197d4e23a3cb9dc11",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -975,8 +917,7 @@
"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.solve()\n",
"sim.plot([\"Current [A]\", \"Terminal voltage [V]\"])"
]
},
Expand All @@ -996,7 +937,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1017,13 +958,13 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e413934add554d37a23d8a133b95152d",
"model_id": "b99031baebdc478499f3609a74144d96",
"version_major": 2,
"version_minor": 0
},
Expand Down
87 changes: 17 additions & 70 deletions examples/notebooks/change-input-current.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: You are using pip version 20.1.1; however, version 20.2 is available.\n",
"You should consider upgrading via the '/home/ferranbrosa/PyBaMM/env/bin/python -m pip install --upgrade pip' command.\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
Expand Down Expand Up @@ -75,7 +77,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b90f73879a2c45cea9f0ffa6df8a0eaa",
"model_id": "55b83465a9ea4937bfd3444fa7198460",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -124,7 +126,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7a1b19df4edd47289d78802ff345bda2",
"model_id": "b827d913896b418caf161f6d8ee9df92",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -151,7 +153,7 @@
"source": [
"## Loading in current data <a name=\"data\"></a>\n",
"\n",
"Data can be loaded in from a csv file by putting the file in the folder 'input/drive_cycles' and using the prefix \"[current data]\". As an example, we show how to solve the SPM using the US06 drive cycle"
"To run drive cycles from data we can create an interpolant and pass it as the current function. "
]
},
{
Expand All @@ -162,69 +164,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c5adb83033c84f06a8478ae913e89044",
"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.DFN()\n",
"\n",
"# create geometry\n",
"geometry = model.default_geometry\n",
"\n",
"# load parameter values and process model and geometry\n",
"param = model.default_parameter_values\n",
"param[\"Current function [A]\"] = \"[current data]US06\"\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": {},
"source": [
"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",
"model_id": "64e6f94728be4ef5bfec800bc26f0768",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -275,6 +215,13 @@
"quick_plot.dynamic_plot();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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": {},
Expand All @@ -288,7 +235,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -310,7 +257,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -341,13 +288,13 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "75930d9bd3da40dab88ff2b69e0571a7",
"model_id": "c2a1fdc7aa5246aaa9543650d96894ca",
"version_major": 2,
"version_minor": 0
},
Expand Down
Loading

0 comments on commit 9d818a9

Please sign in to comment.