Skip to content

Commit

Permalink
BPX example (#4635)
Browse files Browse the repository at this point in the history
* fix bpx bug when activation energies not provided

* add bpx example

* create from bpx obj

* style: pre-commit fixes

* docs

* update BPX tests

* Move function and rename variable

* Update docs/source/examples/notebooks/parameterization/bpx.ipynb

Co-authored-by: Eric G. Kratz <[email protected]>

* update pybamm-data and bpx example

* style: pre-commit fixes

* move initial soc check

* fix tests

* Update docs/source/examples/notebooks/parameterization/bpx.ipynb

Co-authored-by: Eric G. Kratz <[email protected]>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <[email protected]>
Co-authored-by: kratman <[email protected]>
  • Loading branch information
4 people authored Dec 3, 2024
1 parent 879e746 commit 16b15b9
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/source/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ The notebooks are organised into subfolders, and can be viewed in the galleries
notebooks/parameterization/parameter-values.ipynb
notebooks/parameterization/parameterization.ipynb
notebooks/parameterization/sensitivities_and_data_fitting.ipynb
notebooks/parameterization/bpx.ipynb

.. nbgallery::
:caption: Simulations and Experiments
Expand Down
341 changes: 341 additions & 0 deletions docs/source/examples/notebooks/parameterization/bpx.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/source/examples/notebooks/pybamm_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"This notebook is a reference for using pybamm.DataLoader module for using and fetching data files from the pybamm-data registry.\n",
"Checkout the [documentation](../../api/pybamm_data.rst) for further implementation details on this module.\n",
"\n",
"The following steps provide an example for using pybamm.DataLoader to download data files from PyBaMM data registry upstream at [pybamm-data](https://github.com/pybamm-team/pybamm-data/releases/tag/v1.0.0).\n"
"The following steps provide an example for using pybamm.DataLoader to download data files from PyBaMM data registry upstream at [pybamm-data](https://github.com/pybamm-team/pybamm-data/releases).\n"
]
},
{
Expand Down
79 changes: 57 additions & 22 deletions src/pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,34 @@ def __init__(self, values):
pybamm.citations.register(citation)

@staticmethod
def create_from_bpx(filename, target_soc: float = 1):
"""
Parameters
----------
filename: str
The filename of the bpx file
target_soc : float, optional
Target state of charge. Must be between 0 and 1. Default is 1.
Returns
-------
ParameterValues
A parameter values object with the parameters in the bpx file
def _create_from_bpx(bpx, target_soc):
from bpx import get_electrode_concentrations
from bpx.schema import ElectrodeBlended, ElectrodeBlendedSPM
from .bpx import bpx_to_param_dict

"""
if target_soc < 0 or target_soc > 1:
raise ValueError("Target SOC should be between 0 and 1")

from bpx import parse_bpx_file, get_electrode_concentrations
from bpx.schema import ElectrodeBlended, ElectrodeBlendedSPM
from .bpx import bpx_to_param_dict

# parse bpx
bpx = parse_bpx_file(filename)
pybamm_dict = bpx_to_param_dict(bpx)

if "Open-circuit voltage at 0% SOC [V]" not in pybamm_dict:
pybamm_dict["Open-circuit voltage at 0% SOC [V]"] = pybamm_dict[
"Lower voltage cut-off [V]"
]
warn(
"'Open-circuit voltage at 0% SOC [V]' not found in BPX file. Using "
"'Lower voltage cut-off [V]'.",
stacklevel=2,
)
if "Open-circuit voltage at 100% SOC [V]" not in pybamm_dict:
pybamm_dict["Open-circuit voltage at 100% SOC [V]"] = pybamm_dict[
"Upper voltage cut-off [V]"
]
# probably should put a warning here to indicate we are going
# ahead with the low voltage limit.
warn(
"'Open-circuit voltage at 100% SOC [V]' not found in BPX file. Using "
"'Upper voltage cut-off [V]'.",
stacklevel=2,
)

# get initial concentrations based on SOC
# Note: we cannot set SOC for blended electrodes,
Expand All @@ -127,6 +120,48 @@ def create_from_bpx(filename, target_soc: float = 1):

return pybamm.ParameterValues(pybamm_dict)

@staticmethod
def create_from_bpx_obj(bpx_obj, target_soc: float = 1):
"""
Parameters
----------
bpx_obj: dict
A dictionary containing the parameters in the `BPX <https://bpxstandard.com/>`_ format
target_soc : float, optional
Target state of charge. Must be between 0 and 1. Default is 1.
Returns
-------
ParameterValues
A parameter values object with the parameters in the bpx file
"""
from bpx import parse_bpx_obj

bpx = parse_bpx_obj(bpx_obj)
return ParameterValues._create_from_bpx(bpx, target_soc)

@staticmethod
def create_from_bpx(filename, target_soc: float = 1):
"""
Parameters
----------
filename: str
The filename of the `BPX <https://bpxstandard.com/>`_ file
target_soc : float, optional
Target state of charge. Must be between 0 and 1. Default is 1.
Returns
-------
ParameterValues
A parameter values object with the parameters in the bpx file
"""
from bpx import parse_bpx_file

bpx = parse_bpx_file(filename)
return ParameterValues._create_from_bpx(bpx, target_soc)

def __getitem__(self, key):
try:
return self._dict_items[key]
Expand Down
4 changes: 3 additions & 1 deletion src/pybamm/pybamm_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self):
"""
Create a pooch registry with the following data files available upstream at https://github.com/pybamm-team/pybamm-data/
"""
self.version = "v1.0.0" # Version of pybamm-data release
self.version = "v1.0.1" # Version of pybamm-data release
self.path = pooch.os_cache("pybamm")
self.files = {
# COMSOL results
Expand Down Expand Up @@ -112,6 +112,8 @@ def __init__(self):
"US06.csv": "sha256:5909eb2ec7983fae86a050ff3b35a2041d0ab698710a6b0f95d5816e348077ba",
"WLTC.csv": "sha256:bb2f95018a44ac1425cb9c787c34721192af502c7385f1358f28e4f75df11fd8",
"car_current.csv": "sha256:4305b91b9df073cb048c25dd3fae725e06a94fe200e322e5c08db290d6799e36",
# BPX files
"nmc_pouch_cell_BPX.json": "sha256:27261e2a7012725ed16c0c8b799b870388bd6c77f86bbf1949353286569c7d0d",
}
self.registry = pooch.create(
path=self.path,
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/test_parameters/test_bpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ def test_table_data(self):
assert isinstance(dUdT, pybamm.Interpolant)

def test_bpx_soc_error(self):
bpx_obj = copy.copy(self.base)
with pytest.raises(ValueError, match="Target SOC"):
pybamm.ParameterValues.create_from_bpx("blah.json", target_soc=10)
pybamm.ParameterValues.create_from_bpx_obj(bpx_obj, target_soc=10)

def test_bpx_arrhenius(self):
bpx_obj = copy.copy(self.base)
Expand Down Expand Up @@ -500,3 +501,8 @@ def test_bpx_activation_energy_default(self):
assert param[
"Negative electrode diffusivity activation energy [J.mol-1]"
] == pytest.approx(0.0, rel=1e-12)

def test_bpx_from_obj(self):
bpx_obj = copy.copy(self.base)
param = pybamm.ParameterValues.create_from_bpx_obj(bpx_obj)
assert isinstance(param, pybamm.ParameterValues)

0 comments on commit 16b15b9

Please sign in to comment.