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

Issue 1276 add models #1408

Merged
merged 15 commits into from
Mar 2, 2021
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
4 changes: 2 additions & 2 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ jobs:
run: tox -e examples

- name: Install and run coverage
if: success() && (matrix.os == 'ubuntu-latest' && matrix.python-version == 3.7)
if: success() && (matrix.os == 'ubuntu-latest' && matrix.python-version == 3.9)
run: tox -e coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.7
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.9
uses: codecov/codecov-action@v1

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- `Solution` objects can now be created by stepping *different* models ([#1408](https://github.com/pybamm-team/PyBaMM/pull/1408))
- Added support for Python 3.9 and dropped support for Python 3.6. Python 3.6 may still work but is now untested ([#1370](https://github.com/pybamm-team/PyBaMM/pull/1370))
- Added the electrolyte overpotential and Ohmic losses for full conductivity, including surface form ([#1350](https://github.com/pybamm-team/PyBaMM/pull/1350))
- Added functionality to `Citations` to print formatted citations ([#1340](https://github.com/pybamm-team/PyBaMM/pull/1340))
Expand All @@ -22,6 +23,7 @@

## Optimizations

- Improved the way an `Experiment` is simulated to reduce solve time (at the cost of slightly higher set-up time) ([#1408](https://github.com/pybamm-team/PyBaMM/pull/1408))
- Add script and workflow to automatically update parameter_sets.py docstrings ([#1371](https://github.com/pybamm-team/PyBaMM/pull/1371))
- Add URLs checker in workflows ([#1347](https://github.com/pybamm-team/PyBaMM/pull/1347))
- The `Solution` class now only creates the concatenated `y` when the user asks for it. This is an optimization step as the concatenation can be slow, especially with larger experiments ([#1331](https://github.com/pybamm-team/PyBaMM/pull/1331))
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

# load parameter values and process model and geometry
param = model.default_parameter_values
param.process_model(model)
param.process_geometry(geometry)
param.process_model(model)

# set mesh
var = pybamm.standard_spatial_vars
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/experimental_protocols/cccv.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Rest for 1 hour",
),
]
* 3
* 3,
)
model = pybamm.lithium_ion.DFN()
sim = pybamm.Simulation(model, experiment=experiment, solver=pybamm.CasadiSolver())
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/experimental_protocols/gitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

pybamm.set_logging_level("INFO")
experiment = pybamm.Experiment(
[("Discharge at C/20 for 1 hour", "Rest for 1 hour")] * 20
[("Discharge at C/20 for 1 hour", "Rest for 1 hour")] * 20,
)
model = pybamm.lithium_ion.DFN()
sim = pybamm.Simulation(model, experiment=experiment, solver=pybamm.CasadiSolver())
Expand Down
14 changes: 12 additions & 2 deletions pybamm/experiments/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ class Experiment:
period : string, optional
Period (1/frequency) at which to record outputs. Default is 1 minute. Can be
overwritten by individual operating conditions.

use_simulation_setup_type : str
Whether to use the "new" (default) or "old" simulation set-up type. "new" is
faster at simulating individual steps but has higher set-up overhead
"""

def __init__(self, operating_conditions, parameters=None, period="1 minute"):
def __init__(
self,
operating_conditions,
parameters=None,
period="1 minute",
use_simulation_setup_type="new",
):
self.period = self.convert_time_to_seconds(period.split())
operating_conditions_cycles = []
for cycle in operating_conditions:
Expand Down Expand Up @@ -84,6 +92,8 @@ def __init__(self, operating_conditions, parameters=None, period="1 minute"):
else:
raise TypeError("experimental parameters should be a dictionary")

self.use_simulation_setup_type = use_simulation_setup_type

def __str__(self):
return str(self.operating_conditions_strings)

Expand Down
28 changes: 24 additions & 4 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, name="Unnamed model"):
self._algebraic = {}
self._initial_conditions = {}
self._boundary_conditions = {}
self._variables = {}
self._variables = pybamm.FuzzyDict({})
self._events = []
self._concatenated_rhs = None
self._concatenated_algebraic = None
Expand Down Expand Up @@ -382,13 +382,25 @@ def set_initial_conditions_from(self, solution, inplace=True):
else:
model = self.new_copy()

if isinstance(solution, pybamm.Solution):
solution = solution.last_state
else:
solution = pybamm.FuzzyDict(solution)
for var, equation in model.initial_conditions.items():
if isinstance(var, pybamm.Variable):
final_state = solution[var.name]
try:
final_state = solution[var.name]
except KeyError as e:
raise pybamm.ModelError(
"To update a model from a solution, each variable in "
"model.initial_conditions must appear in the solution with "
"the same key as the variable name. In the solution provided, "
f"{e.args[0]}"
)
if isinstance(solution, pybamm.Solution):
final_state = final_state.data
if final_state.ndim == 1:
final_state_eval = np.array([final_state[-1]])
final_state_eval = final_state[-1:]
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a stupid question, but is this colon necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just discovered this in python, it returns an array (which is needed) instead of a scalar

np.array([1,2,3])[-1] # --> 3
np.array([1,2,3])[-1:] # --> np.array([3])

elif final_state.ndim == 2:
final_state_eval = final_state[:, -1]
elif final_state.ndim == 3:
Expand All @@ -399,7 +411,15 @@ def set_initial_conditions_from(self, solution, inplace=True):
elif isinstance(var, pybamm.Concatenation):
children = []
for child in var.orphans:
final_state = solution[child.name]
try:
final_state = solution[child.name]
except KeyError as e:
raise pybamm.ModelError(
"To update a model from a solution, each variable in "
"model.initial_conditions must appear in the solution with "
"the same key as the variable name. In the solution "
f"provided, {e.args[0]}"
)
if isinstance(solution, pybamm.Solution):
final_state = final_state.data
if final_state.ndim == 2:
Expand Down
8 changes: 4 additions & 4 deletions pybamm/models/standard_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,18 @@

# SEI variables
L_inner_av = pybamm.Variable(
"X-averaged inner SEI thickness", domain="current collector"
"X-averaged inner negative electrode SEI thickness", domain="current collector"
)
L_inner = pybamm.Variable(
"Inner SEI thickness",
"Inner negative electrode SEI thickness",
domain=["negative electrode"],
auxiliary_domains={"secondary": "current collector"},
)
L_outer_av = pybamm.Variable(
"X-averaged outer SEI thickness", domain="current collector"
"X-averaged outer negative electrode SEI thickness", domain="current collector"
)
L_outer = pybamm.Variable(
"Outer SEI thickness",
"Outer negative electrode SEI thickness",
domain=["negative electrode"],
auxiliary_domains={"secondary": "current collector"},
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_fundamental_variables(self):
j = pybamm.Variable(
"Total "
+ self.domain.lower()
+ " electrode interfacial current density",
+ " electrode interfacial current density variable",
domain=self.domain.lower() + " electrode",
auxiliary_domains={"secondary": "current collector"},
)
Expand Down
5 changes: 3 additions & 2 deletions pybamm/plotting/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


class LoopList(list):
"""A list which loops over itself when accessing an index so that it never runs out
"""
A list which loops over itself when accessing an index so that it never runs out
"""

def __getitem__(self, i):
Expand Down Expand Up @@ -114,7 +115,7 @@ def __init__(
# attribute
solutions[idx] = sol.solution

models = [solution.model for solution in solutions]
models = [solution.all_models[0] for solution in solutions]

# Set labels
if labels is None:
Expand Down
Loading