Skip to content

Commit

Permalink
Add _from_json functionality for pybamm.sign (#4517)
Browse files Browse the repository at this point in the history
* Add _from_json functionality for Sign + test

* Update changelog

---------

Co-authored-by: Eric G. Kratz <[email protected]>
  • Loading branch information
pipliggins and kratman authored Oct 15, 2024
1 parent 47c165d commit fb81f21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

## Bug Fixes

- Added `_from_json()` functionality to `Sign` which was erroneously omitted previously. ([#4517](https://github.com/pybamm-team/PyBaMM/pull/4517))
- Fixed bug where IDAKLU solver failed when `output variables` were specified and an extrapolation event is present. ([#4440](https://github.com/pybamm-team/PyBaMM/pull/4440))

## Breaking changes
Expand Down
3 changes: 2 additions & 1 deletion src/pybamm/expression_tree/unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def __init__(self, child):

@classmethod
def _from_json(cls, snippet: dict):
raise NotImplementedError()
"""See :meth:`pybamm.UnaryOperator._from_json()`."""
return cls(snippet["children"][0])

def diff(self, variable):
"""See :meth:`pybamm.Symbol.diff()`."""
Expand Down
17 changes: 14 additions & 3 deletions tests/unit/test_expression_tree/test_unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,20 @@ def test_sign(self):
)

# Test from_json
with pytest.raises(NotImplementedError):
# signs are always scalar/array types in a discretised model
pybamm.Sign._from_json({})
c = pybamm.Multiplication(pybamm.Variable("a"), pybamm.Scalar(3))
sign_json = {
"name": "sign",
"id": 5341515228900508018,
"domains": {
"primary": [],
"secondary": [],
"tertiary": [],
"quaternary": [],
},
"children": [c],
}

assert pybamm.sign(c) == pybamm.Sign._from_json(sign_json)

def test_floor(self, mocker):
a = pybamm.Symbol("a")
Expand Down

0 comments on commit fb81f21

Please sign in to comment.