Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 10, 2022
2 parents e7b4c85 + 8bdc3e0 commit d16497d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

## Features

- Added `scale` and `reference` attributes to `Variable` objects, which can be use to make the ODE/DAE solver better conditioned ([#2440](https://github.com/pybamm-team/PyBaMM/pull/2440))
- SEI reactions can now be asymmetric ([#2425](https://github.com/pybamm-team/PyBaMM/pull/2425))

## Optimizations

- Added more rules for simplifying expressions, especially around Concatenations. Also, meshes constructed from multiple domains are now memoized ([#2443](https://github.com/pybamm-team/PyBaMM/pull/2443))
- Added more rules for simplifying expressions, especially around Concatenations. Also, meshes constructed from multiple domains are now cached ([#2443](https://github.com/pybamm-team/PyBaMM/pull/2443))
- Added more rules for simplifying expressions. Constants in binary operators are now moved to the left by default (e.g. `x*2` returns `2*x`) ([#2424](https://github.com/pybamm-team/PyBaMM/pull/2424))

## Breaking changes
Expand Down
41 changes: 24 additions & 17 deletions pybamm/parameters/parameter_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ class ParameterSets(Mapping):
"""

def __init__(self):
# Load Parameter Sets registered to `pybamm_parameter_set`
ps = dict()
# Dict of entry points for parameter sets, lazily load entry points as
self.__all_parameter_sets = dict()
for entry_point in pkg_resources.iter_entry_points("pybamm_parameter_set"):
ps[entry_point.name] = entry_point.load()

self.__all_parameter_sets = ps
self.__all_parameter_sets[entry_point.name] = entry_point

def __new__(cls):
"""Ensure only one instance of ParameterSets exists"""
Expand All @@ -48,7 +46,18 @@ def __new__(cls):
return cls.instance

def __getitem__(self, key) -> dict:
return self.__all_parameter_sets[key]()
return self.__load_entry_point__(key)()

def __load_entry_point__(self, key) -> callable:
"""Check that ``key`` is a registered ``pybamm_parameter_set``,
and return the entry point for the parameter set, loading it needed.
"""
if key not in self.__all_parameter_sets:
raise KeyError(f"Unknown parameter set: {key}")
ps = self.__all_parameter_sets[key]
if isinstance(ps, pkg_resources.EntryPoint):
ps = self.__all_parameter_sets[key] = ps.load()
return ps

def __iter__(self):
return self.__all_parameter_sets.__iter__()
Expand All @@ -58,7 +67,7 @@ def __len__(self) -> int:

def get_docstring(self, key):
"""Return the docstring for the ``key`` parameter set"""
return textwrap.dedent(self.__all_parameter_sets[key].__doc__)
return textwrap.dedent(self.__load_entry_point__(key).__doc__)

def __getattribute__(self, name):
try:
Expand All @@ -67,16 +76,14 @@ def __getattribute__(self, name):
# For backwards compatibility, parameter sets that used to be defined in
# this file now return the name as a string, which will load the same
# parameter set as before when passed to `ParameterValues`
if name in self.__all_parameter_sets:
out = name
else:
raise error
warnings.warn(
f"Parameter sets should be called directly by their name ({name}), "
f"instead of via pybamm.parameter_sets (pybamm.parameter_sets.{name}).",
DeprecationWarning,
)
return out
if name in self:
msg = (
"Parameter sets should be called directly by their name ({0}), "
"instead of via pybamm.parameter_sets (pybamm.parameter_sets.{0})."
).format(name)
warnings.warn(msg, DeprecationWarning)
return name
raise error


#: Singleton Instance of :class:ParameterSets """
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_parameters/test_parameter_sets_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_all_registered(self):
def test_get_docstring(self):
"""Test that :meth:`pybamm.parameter_sets.get_doctstring` works"""
docstring = pybamm.parameter_sets.get_docstring("Marquis2019")
self.assertRegexpMatches(docstring, "Parameters for a Kokam SLPB78205130H cell")
self.assertRegex(docstring, "Parameters for a Kokam SLPB78205130H cell")

def test_iter(self):
"""Test that iterating `pybamm.parameter_sets` iterates over keys"""
Expand Down

0 comments on commit d16497d

Please sign in to comment.