Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store slices #925

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Store variable slices in model for inspection ([#925](https://github.com/pybamm-team/PyBaMM/pull/925))
- Made t_plus (optionally) a function of electrolyte concentration, and added (1 + dlnf/dlnc) to models ([#921](https://github.com/pybamm-team/PyBaMM/pull/921))
- Added `DummySolver` for empty models ([#915](https://github.com/pybamm-team/PyBaMM/pull/915))
- Added functionality to broadcast to edges ([#891](https://github.com/pybamm-team/PyBaMM/pull/891))
- Reformatted and cleaned up `QuickPlot` ([#886](https://github.com/pybamm-team/PyBaMM/pull/886))
Expand Down
7 changes: 7 additions & 0 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def process_model(self, model, inplace=True, check_model=True):
# Set the y split for variables
pybamm.logger.info("Set variable slices for {}".format(model.name))
self.set_variable_slices(variables)
# Keep a record of y_slices in the model
model.y_slices = self.y_slices_explicit

# now add extrapolated external variables to the boundary conditions
# if required by the spatial method
Expand Down Expand Up @@ -214,6 +216,7 @@ def set_variable_slices(self, variables):
"""
# Set up y_slices
y_slices = defaultdict(list)
y_slices_explicit = defaultdict(list)
start = 0
end = 0
# Iterate through unpacked variables, adding appropriate slices to y_slices
Expand All @@ -233,14 +236,18 @@ def set_variable_slices(self, variables):
submesh = domain_mesh[i]
end += submesh.npts_for_broadcast_to_nodes
y_slices[child.id].append(slice(start, end))
y_slices_explicit[child].append(slice(start, end))
start = end
else:
end += self._get_variable_size(variable)
y_slices[variable.id].append(slice(start, end))
y_slices_explicit[variable].append(slice(start, end))
start = end

# Convert y_slices back to normal dictionary
self.y_slices = dict(y_slices)
# Also keep a record of what the y_slices are, to be stored in the model
self.y_slices_explicit = dict(y_slices_explicit)

# reset discretised_symbols
self._discretised_symbols = {}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_discretisations/test_discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def test_adding_1D_external_variable(self):

self.assertEqual(disc.y_slices[a.id][0], slice(0, 10, None))

self.assertEqual(model.y_slices[a][0], slice(0, 10, None))

b_test = np.ones((10, 1))
np.testing.assert_array_equal(
model.variables["b"].evaluate(inputs={"b": b_test}), b_test
Expand Down