{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "## LG M50 Single Pulse Parameter Identification\n", "\n", "This example presents an experimental parameter identification method for a two-RC circuit model. The data for this notebook is located within the same directory and was obtained from WDWidanage/Simscape-Battery-Library [[1]](https://github.com/WDWidanage/Simscape-Battery-Library/tree/a3842b91b3ccda006bc9be5d59c8bcbd167ceef7/Examples/parameterEstimation_TECMD/Data).\n", "\n", "\n", "### Setting up the Environment\n", "\n", "Before we begin, we need to ensure that we have all the necessary tools. We will install PyBOP for the fitting and Pandas for the data ingestion and manipulation:" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "%pip install --upgrade pip ipywidgets -q\n", "%pip install pybop[plot] -q\n", "%pip install pandas -q" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "### Importing Libraries\n", "\n", "With the environment set up, we can now import PyBOP alongside other libraries we will need:" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import plotly.graph_objects as go\n", "import pybamm\n", "from scipy.io import loadmat\n", "\n", "import pybop" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "Let's fix the random seed in order to generate consistent output during development, although this does not need to be done in practice." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "np.random.seed(8)" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## Importing Data\n", "\n", "The data is imported as a dictionary with the following key level:\n", "- [\"LGM50_5Ah_Pulse\"]\n", " - Temperature-> [\"T0\"]\n", " - State of Charge ->[\"SoC3\"]\n", " - Cell number -> [\"Cell1\"]\n", " - Corresponding data -> [\"data\"]\n", "\n", "Using SciPy's `loadmat`, we import the data from the MAT containers:" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "ocp = loadmat(\"data/LGM50_5Ah_OCV.mat\", simplify_cells=True, mat_dtype=False)\n", "pulse_data = loadmat(\"data/LGM50_5Ah_Pulse.mat\", simplify_cells=True, mat_dtype=False)\n", "rate_data = loadmat(\"data/LGM50_5Ah_RateTest.mat\", simplify_cells=True, mat_dtype=False)" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "### Convert to Dataframes\n", "\n", "Next, we construct a dataframe from a selected pulse. In this case, we select the data for zero degrees (`T0`) with a state-of-charge of 90% (`SoC9`) and the 19th cell (`Cell19`). This can be extended to multiple pulses, or a different selection.\n", "\n", "Additionally, we apply two filters to the dataframe to ensure the data contains only monotonically increasing time samples without duplicates." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(pulse_data[\"LGM50_5Ah_Pulse\"][\"T0\"][\"SoC9\"][\"Cell19\"][\"data\"])\n", "df[\"ProgTime\"] = df[\"ProgTime\"] - df[\"ProgTime\"].min()\n", "df.drop_duplicates(subset=[\"ProgTime\"], inplace=True)" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "A plot of time vs voltage confirms the data looks correct for fitting. In this situation, we would prefer to have additional samples from the relaxation, but as we will show below, PyBOP is still able to identify parameter values that fit this system." ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "type": "scatter", "x": [ 0.0, 0.01800000004004687, 0.12100000004284084, 0.25100000004749745, 0.34300000010989606, 0.4190000001108274, 0.5600000000558794, 0.6170000000856817, 0.7250000000931323, 0.8570000000763685, 0.9250000000465661, 1.0330000000540167, 1.1739999999990687, 1.2300000000977889, 1.343000000109896, 1.4170000000158325, 1.5370000000111759, 1.6480000000447035, 1.7160000000149012, 1.8209999999962747, 1.922000000020489, 2.0010000000474975, 2.1510000000707805, 2.2290000000502914, 2.4300000000512227, 2.4980000000214204, 2.534000000101514, 2.7020000000484288, 2.7989999999990687, 2.8380000001052395, 3.002000000094995, 3.103000000002794, 3.1440000000875443, 3.2660000000614673, 3.3570000000763685, 3.4860000000335276, 3.525000000023283, 3.7220000000670552, 3.762000000104308, 3.8320000000530854, 4.0230000000447035, 4.069000000017695, 4.14000000001397, 4.282000000006519, 4.3600000001024455, 4.44800000009127, 4.5070000000996515, 4.633000000030734, 4.726000000024214, 4.94800000009127, 4.990000000107102, 5.033000000054017, 5.134000000078231, 5.24800000002142, 5.339000000036322, 5.474000000045635, 5.57300000009127, 5.614000000059605, 5.7570000000996515, 5.875, 5.926000000094064, 6.0670000000391155, 6.182000000029802, 6.2350000001024455, 6.341000000014901, 6.512000000104308, 6.579000000027008, 6.618000000016764, 6.7730000000447035, 6.856000000028871, 6.927000000025146, 7.066000000108033, 7.1510000000707805, 7.238000000012107, 7.3080000000773, 7.4610000000102445, 7.546000000089407, 7.753000000026077, 7.815000000060536, 7.853000000002794, 7.923999999999069, 8.051000000094064, 8.160000000032596, 8.23300000000745, 8.318000000086613, 8.427000000025146, 8.53900000010617, 8.660000000032596, 8.747000000090338, 8.852000000071712, 8.989000000059605, 9.078000000095926, 9.127000000094995, 9.274000000092201, 9.361000000033528, 9.434000000008382, 9.524000000092201, 9.63600000005681, 9.754000000073574, 9.83800000010524, 9.949000000022352, 10.016000000061467, 10.065000000060536, 10.085000000079162, 11.104000000050291, 12.092000000062399, 13.12100000004284, 14.091000000014901, 15.08800000010524, 16.113000000012107, 17.14000000001397, 18.087000000057742, 19.101000000024214, 20.10800000000745 ], "y": [ 4.08078, 3.96745, 3.96156, 3.96156, 3.95955, 3.959, 3.95889, 3.95855, 3.95755, 3.95755, 3.95722, 3.95644, 3.95644, 3.95611, 3.95533, 3.95511, 3.95511, 3.95433, 3.95422, 3.95422, 3.95355, 3.95333, 3.95322, 3.95266, 3.95255, 3.95221, 3.95188, 3.95166, 3.95166, 3.95121, 3.95099, 3.95088, 3.95055, 3.95032, 3.9501, 3.94999, 3.94977, 3.94955, 3.94932, 3.9491, 3.94888, 3.94866, 3.94843, 3.94832, 3.9481, 3.94788, 3.94777, 3.94766, 3.94743, 3.9471, 3.94699, 3.94677, 3.94665, 3.94665, 3.94621, 3.9461, 3.9461, 3.94588, 3.94565, 3.94554, 3.94532, 3.94521, 3.9451, 3.94488, 3.94476, 3.94454, 3.94443, 3.94421, 3.9441, 3.94399, 3.94376, 3.94365, 3.94354, 3.94332, 3.94321, 3.94321, 3.94287, 3.94276, 3.94265, 3.94243, 3.94232, 3.94221, 3.9421, 3.94187, 3.94176, 3.94176, 3.94154, 3.94143, 3.94132, 3.9411, 3.94098, 3.94087, 3.94076, 3.94054, 3.94054, 3.94043, 3.94021, 3.94021, 3.93998, 3.93987, 3.93987, 3.93976, 3.93954, 3.93943, 4.06199, 4.06411, 4.06544, 4.06644, 4.06744, 4.06822, 4.06878, 4.06922, 4.06978, 4.07011 ] } ], "layout": { "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "sequentialminus": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "go.Figure(\n", " data=go.Scatter(\n", " x=df[\"ProgTime\"],\n", " y=df[\"Voltage\"],\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "Next, we construct the OCV function from the imported `OCV` data. This is completed with a wrapper method on the `pybamm.Interpolant` function," ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "def ocv_LGM50(sto):\n", " name = \"OCV\"\n", " x = (ocp[\"LGM50_5Ah_OCV\"][\"T25\"][\"refSoC\"].reshape(-1) / 100,)\n", " y = ocp[\"LGM50_5Ah_OCV\"][\"T25\"][\"meanOCV\"].reshape(-1)\n", " return pybamm.Interpolant(x, y, sto, name)" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "We can construct the two RC parameter set with initial values as listed. Note, the initial SOC is shifted slightly to better match the zero degree data." ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "params = pybop.ParameterSet(\n", " params_dict={\n", " \"chemistry\": \"ecm\",\n", " \"Initial SoC\": 0.9 - 0.01,\n", " \"Initial temperature [K]\": 25 + 273.15,\n", " \"Cell capacity [A.h]\": 5,\n", " \"Nominal cell capacity [A.h]\": 5,\n", " \"Ambient temperature [K]\": 25 + 273.15,\n", " \"Current function [A]\": 4.85,\n", " \"Upper voltage cut-off [V]\": 4.2,\n", " \"Lower voltage cut-off [V]\": 3.0,\n", " \"Cell thermal mass [J/K]\": 1000,\n", " \"Cell-jig heat transfer coefficient [W/K]\": 10,\n", " \"Jig thermal mass [J/K]\": 500,\n", " \"Jig-air heat transfer coefficient [W/K]\": 10,\n", " \"Open-circuit voltage [V]\": ocv_LGM50,\n", " \"R0 [Ohm]\": 0.005,\n", " \"Element-1 initial overpotential [V]\": 0,\n", " \"Element-2 initial overpotential [V]\": 0,\n", " \"R1 [Ohm]\": 0.0001,\n", " \"R2 [Ohm]\": 0.0001,\n", " \"C1 [F]\": 3000,\n", " \"C2 [F]\": 6924,\n", " \"Entropic change [V/K]\": 0.0004,\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Identifying the Parameters\n", "\n", "Now that the initial parameter set is defined, we can start the PyBOP fitting process. First, we construct the model class with two RC elements and the corresponding parameter set." ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "model = pybop.empirical.Thevenin(\n", " parameter_set=params, options={\"number of rc elements\": 2}\n", ")" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "In this example, we are going to try to fit all five parameters at once. To do this, we define a `pybop.Parameter` for each fitting parameter and compile them in pybop.Parameters," ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "parameters = pybop.Parameters(\n", " pybop.Parameter(\n", " \"R0 [Ohm]\",\n", " prior=pybop.Gaussian(0.005, 0.0001),\n", " bounds=[1e-6, 2e-1],\n", " ),\n", " pybop.Parameter(\n", " \"R1 [Ohm]\",\n", " prior=pybop.Gaussian(0.0001, 0.0001),\n", " bounds=[1e-6, 1],\n", " ),\n", " pybop.Parameter(\n", " \"R2 [Ohm]\",\n", " prior=pybop.Gaussian(0.0001, 0.0001),\n", " bounds=[1e-6, 1],\n", " ),\n", " pybop.Parameter(\n", " \"C1 [F]\",\n", " prior=pybop.Gaussian(3000, 2500),\n", " bounds=[0.5, 1e4],\n", " ),\n", " pybop.Parameter(\n", " \"C2 [F]\",\n", " prior=pybop.Gaussian(3000, 2500),\n", " bounds=[0.5, 1e4],\n", " ),\n", ")" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "We can now form the `pybop.Dataset` from the experimental data. In this situation, the applied current convention is different between the experimental data and PyBOP's convention. This is solved by negating the vector on construction." ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "dataset = pybop.Dataset(\n", " {\n", " \"Time [s]\": df[\"ProgTime\"].values,\n", " \"Current function [A]\": -df[\"Current\"].values,\n", " \"Voltage [V]\": df[\"Voltage\"].values,\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "The `FittingProblem` class provides us with a class to hold all of the objects we need to evaluate our selected `SumSquaredError` cost function. " ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "problem = pybop.FittingProblem(model, parameters, dataset)\n", "cost = pybop.SumSquaredError(problem)" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "The cost function can be interrogated manually via the `cost([params])` API. In this example, that would look like the following," ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.1767544040154971" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cost([0.01, 0.01, 0.01, 20000, 20000])" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "Next, we construct the optimisation class with our algorithm of choice and run it. In this case, we select the PSO method as it provides global optimisation capability. After optimisation, we can compare the initial parameters to the identified parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initial parameters: [5.00912047e-03 2.09128273e-04 1.00999900e-04 1.49995000e+00\n", " 1.49995000e+00]\n", "Estimated parameters: [4.63261982e-02 1.68622272e-01 1.29049708e-02 7.68290523e+03\n", " 5.36867333e+02]\n" ] } ], "source": [ "optim = pybop.PSO(cost, max_unchanged_iterations=55, threshold=1e-6)\n", "x, final_cost = optim.run()\n", "print(\"Initial parameters:\", optim.x0)\n", "print(\"Estimated parameters:\", x)" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "## Plotting and Visualisation\n", "\n", "PyBOP provides various plotting utilities to visualise the results of the optimisation. The `quick_plot` method provides a fast scatter of two variables. For this example, we plot the identified parameter set to the target dataset. The default plot is voltage vs time as shown below." ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "051015203.9544.054.1ReferenceModelOptimised ComparisonTime / sVoltage / V" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "### Convergence and Parameter Trajectories\n", "\n", "To assess the optimisation process, we can plot the convergence of the cost function and the trajectories of the parameters:" ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "204060801001200.0250.030.0350.040.0450.050.055ConvergenceIterationCost" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "20040060080000.050.10.1520040060080000.5120040060080000.5120040060080005k10k20040060080005k10kR0 [Ohm]R1 [Ohm]R2 [Ohm]C1 [F]C2 [F]Parameter ConvergenceFunction CallFunction CallFunction CallFunction CallFunction CallR0 [Ohm]R1 [Ohm]R2 [Ohm]C1 [F]C2 [F]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pybop.plot_convergence(optim)\n", "pybop.plot_parameters(optim);" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "## Validating the Fit\n", "\n", "We've identified parameters from a single discharge pulse. Now, let's ask ourselves how well these parameters extrapolate to other operational conditions. To accomplish this, let's load a new pulse dataset at 80% SOC and compare it to our identified forward model." ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "df_pulse_two = pd.DataFrame(\n", " pulse_data[\"LGM50_5Ah_Pulse\"][\"T0\"][\"SoC8\"][\"Cell19\"][\"data\"]\n", ")\n", "df_pulse_two[\"ProgTime\"] = df_pulse_two[\"ProgTime\"] - df_pulse_two[\"ProgTime\"].min()\n", "df_pulse_two.drop_duplicates(subset=[\"ProgTime\"], inplace=True)" ] }, { "cell_type": "markdown", "id": "34", "metadata": {}, "source": [ "Next, we construct a new `pybop.Dataset` from the second pulse data," ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "dataset_two_pulse = pybop.Dataset(\n", " {\n", " \"Time [s]\": df_pulse_two[\"ProgTime\"].values,\n", " \"Current function [A]\": -df_pulse_two[\"Current\"].values,\n", " \"Voltage [V]\": df_pulse_two[\"Voltage\"].values,\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "Now that we have a new dataset, we update the target within the problem class as well as the `Initial SoC` value. Once that has been completed, we rebuild the model." ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "problem.set_target(dataset_two_pulse)\n", "model.build(dataset=dataset_two_pulse, initial_state={\"Initial SoC\": 0.8 - 0.0075})" ] }, { "cell_type": "markdown", "id": "38", "metadata": {}, "source": [ "Let's plot the parameterised forward model against the new pulse data:" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "051015203.853.93.954ReferenceModelParameter ExtrapolationTime / sVoltage / V" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pybop.quick_plot(problem, problem_inputs=x, title=\"Parameter Extrapolation\");" ] }, { "cell_type": "markdown", "id": "40", "metadata": {}, "source": [ "As expected, when identifying parameters from a single pulse, extrapolation to different operating conditions is challenging. To solve this issue, parameter identification with various pulse datasets is recommended." ] }, { "cell_type": "markdown", "id": "41", "metadata": {}, "source": [ "### Conclusion\n", "\n", "This notebook illustrates how to perform circuit model parameter identification using PSO in PyBOP, providing insights into the optimisation process through various visualisations." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }