Skip to content

Commit

Permalink
#939 remove C-rate, make copies of sim attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Apr 9, 2020
1 parent 77a3a3e commit 5a8ef1f
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 169 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

## Bug fixes

- Added default values to base model so that it works with the `Simulation` class
- Reformatted thermal submodels ([#938](https://github.com/pybamm-team/PyBaMM/pull/938)
- Reformatted electrolyte submodels ([#927](https://github.com/pybamm-team/PyBaMM/pull/927))

## Breaking changes

- Removed some inputs like `T_inf`, `R_g` and activation energies to some of the standard function parameters. This is because each of those inputs is specific to a particular function (e.g. the reference temperature at which the function was measured). To change a property such as the activation energy, users should create a new function, specifying the relevant property as a `Parameter` or `InputParameter` ([#942](https://github.com/pybamm-team/PyBaMM/pull/942))
- The thermal option 'xyz-lumped' has been removed. The option 'thermal current collector' has also been removed ([#938](https://github.com/pybamm-team/PyBaMM/pull/938)
- The 'C-rate' parameter has been deprecated. Use 'Current function [A]' instead. The cell capacity can be accessed as 'Cell capacity [A.h]', and used to calculate current from C-rate

# [v0.2.1](https://github.com/pybamm-team/PyBaMM/tree/v0.2.1) - 2020-03-31

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Positive tab centre z-coordinate [m],0.114,Tab at top,
# Electrical,,,
Cell capacity [A.h],17,Manufacturer,
Typical current [A],1,,

Current function [A],1,default current function,
,,,
# Density,,,
Negative current collector density [kg.m-3],11300, same as electrode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Number of electrodes connected in parallel to make a cell,8,Manufacturer,
Number of cells connected in series to make a battery,6,Manufacturer,
Lower voltage cut-off [V],1.73,(just under) 10.5V across 6-cell battery,
Upper voltage cut-off [V],2.44,(just over) 14.5V across 6-cell battery,
C-rate,0.1,,
,,,
# Initial conditions
Initial State of Charge,1,-,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Positive current collector thermal conductivity [W.m-1.K-1],158.079, 237 * 0.667
# Electrical,,,
Cell capacity [A.h],0.43,trial and error,
Typical current [A],0.43,0.2857,1C current
Current function [A],0.43,default current function,
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Positive current collector thermal conductivity [W.m-1.K-1],237,CRC Handbook,alu
,,,
# Electrical,,,
Cell capacity [A.h],5,Chen 2020,
Typical current [A],5,Chen 2020,
Typical current [A],5,Chen 2020,
Current function [A],5,default current function,
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Positive current collector thermal conductivity [W.m-1.K-1],237,,
# Electrical,,,
Cell capacity [A.h],5,Peyman MPM,
Typical current [A],5,,1C current
Current function [A],5,default current function,
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Electrode width [m],8.50E-02,,
# Electrical,,,
Cell capacity [A.h], 0.15625, 7.5/48 (parameter set for a single layer cell),
Typical current [A], 0.15652,,
Current function [A],0.15652,default current function,
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Positive current collector thermal conductivity [W.m-1.K-1],237,,
# Electrical,,,
Cell capacity [A.h],0.680616,,24 Ah/m2 * 0.137m * 0.207m
Typical current [A],0.680616,,1C current
Current function [A],0.680616,default current function,
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.5,,
Upper voltage cut-off [V],4.2,,
C-rate,-1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],48.8682,Peyman MPM, x0 (0.0017) * Csmax_n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.5,,
Upper voltage cut-off [V],4.4,,
C-rate,1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],30086,Chen 2020,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.5,,
Upper voltage cut-off [V],4.2,,
C-rate,1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3], 26120.05,,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.7,,
Upper voltage cut-off [V],4.5,,
C-rate,1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],18081,0.63*2.84E4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],3.105,,
Upper voltage cut-off [V],4.7,,
C-rate,1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],19986.609595075,Scott Moura FastDFN,
Expand Down
84 changes: 9 additions & 75 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pandas as pd
import os
import numbers
import numpy as np
from pprint import pformat


Expand Down Expand Up @@ -115,6 +114,11 @@ def items(self):
"Get the items of the dictionary"
return self._dict_items.items()

def copy(self):
"""Returns a copy of the parameter values. Makes sure to copy the internal
dictionary."""
return ParameterValues(values=self._dict_items.copy())

def search(self, key, print_values=True):
"""
Search dictionary for keys containing 'key'.
Expand Down Expand Up @@ -284,52 +288,12 @@ def check_and_update_parameter_values(self, values):
"'Typical current [A]' cannot be zero. A possible alternative is to "
"set 'Current function [A]' to `0` instead."
)
if "C-rate" in values and "Current function [A]" in values:
if "C-rate" in values:
raise ValueError(
"Cannot provide both 'C-rate' and 'Current function [A]' simultaneously"
"The 'C-rate' parameter has been deprecated, "
"use 'Current function [A]' instead. The cell capacity can be accessed "
"as 'Cell capacity [A.h]', and used to calculate current from C-rate."
)
# If the capacity of the cell has been provided, make sure "C-rate" and current
# match with the stated capacity
if "Cell capacity [A.h]" in values or "Cell capacity [A.h]" in self._dict_items:
# Capacity from values takes precedence
if "Cell capacity [A.h]" in values:
capacity = values["Cell capacity [A.h]"]
else:
capacity = self._dict_items["Cell capacity [A.h]"]
# Make sure they match if both provided
# Update the other if only one provided
if "C-rate" in values:
# Can't provide C-rate as a function
if callable(values["C-rate"]):
value = CrateToCurrent(values["C-rate"], capacity)
elif isinstance(values["C-rate"], tuple):
data = values["C-rate"][1]
current_data = np.stack([data[:, 0], data[:, 1] * capacity], axis=1)
value = (values["C-rate"][0] + "_to_current", current_data)
elif values["C-rate"] == "[input]":
value = CrateToCurrent(values["C-rate"], capacity, typ="input")
else:
value = values["C-rate"] * capacity
self._dict_items["Current function [A]"] = value
elif "Current function [A]" in values:
if callable(values["Current function [A]"]):
value = CurrentToCrate(values["Current function [A]"], capacity)
elif isinstance(values["Current function [A]"], tuple):
data = values["Current function [A]"][1]
c_rate_data = np.stack([data[:, 0], data[:, 1] / capacity], axis=1)
value = (
values["Current function [A]"][0] + "_to_Crate",
c_rate_data,
)
elif values["Current function [A]"] == "[input]":
value = CurrentToCrate(
values["Current function [A]"], capacity, typ="input"
)
else:
value = values["Current function [A]"] / capacity
self._dict_items["C-rate"] = value

return values

def process_model(self, unprocessed_model, inplace=True):
"""Assign parameter values to a model.
Expand Down Expand Up @@ -605,33 +569,3 @@ def evaluate(self, symbol):

def _ipython_key_completions_(self):
return list(self._dict_items.keys())


class CurrentToCrate:
"Convert a current function to a C-rate function"

def __init__(self, current, capacity, typ="function"):
self.current = current
self.capacity = capacity
self.type = typ

def __call__(self, t):
if self.type == "function":
return self.current(t) / self.capacity
elif self.type == "input":
return pybamm.InputParameter("Current function [A]") / self.capacity


class CrateToCurrent:
"Convert a C-rate function to a current function"

def __init__(self, Crate, capacity, typ="function"):
self.Crate = Crate
self.capacity = capacity
self.type = typ

def __call__(self, t):
if self.type == "function":
return self.Crate(t) * self.capacity
elif self.type == "input":
return pybamm.InputParameter("C-rate") * self.capacity
6 changes: 5 additions & 1 deletion pybamm/parameters/print_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def print_parameters(parameters, parameter_values, output_file=None):
# Calculate parameters for each C-rate
for Crate in [1, 10]:
# Update Crate
parameter_values.update({"C-rate": Crate}, check_already_exists=False)
capacity = parameter_values.get("Cell capacity [A.h]")
if capacity is not None:
parameter_values.update(
{"Current function [A]": Crate * capacity}, check_already_exists=False
)
for name, symbol in parameters.items():
if not callable(symbol):
proc_symbol = parameter_values.process_symbol(symbol)
Expand Down
47 changes: 29 additions & 18 deletions pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ def __init__(

if experiment is None:
self.operating_mode = "without experiment"
self.C_rate = C_rate
if self.C_rate:
self._parameter_values.update({"C-rate": self.C_rate})
if C_rate:
self.C_rate = C_rate
self._parameter_values.update(
{
"Current function [A]": self.C_rate
* self._parameter_values["Cell capacity [A.h]"]
}
)
self.model = model
else:
self.set_up_experiment(model, experiment)
Expand Down Expand Up @@ -344,8 +349,8 @@ def solve(
# input. If the current is provided as data then the "Current function [A]"
# is the tuple (filename, data).
# First, read the current function (if provided, otherwise return None)
current_function = self._parameter_values.get("Current function [A]")
if isinstance(current_function, tuple):
current = self._parameter_values.get("Current function [A]")
if isinstance(current, tuple):
filename = self._parameter_values["Current function [A]"][0]
time_data = self._parameter_values["Current function [A]"][1][:, 0]
# If no t_eval is provided, we use the times provided in the data.
Expand Down Expand Up @@ -392,14 +397,15 @@ def solve(
# If not using a drive cycle and t_eval is not provided, set t_eval
# to correspond to a single discharge
elif t_eval is None:
# Get C-rate, return None if it doesn't exist
C_rate = self._parameter_values.get("C-rate")
if C_rate is None:
if current is None:
t_end = 1
else:
if isinstance(C_rate, pybamm.InputParameter):
C_rate = inputs["C-rate"]
# Get C-rate, return None if it doesn't exist
capacity = self.parameter_values["Cell capacity [A.h]"]
if isinstance(current, pybamm.InputParameter):
C_rate = inputs["Current function [A]"] / capacity
try:
C_rate = current / capacity
t_end = 3600 / C_rate
except TypeError:
t_end = 3600
Expand Down Expand Up @@ -575,7 +581,7 @@ def geometry(self):

@geometry.setter
def geometry(self, geometry):
self._geometry = copy.copy(geometry)
self._geometry = geometry.copy()
self._unprocessed_geometry = copy.deepcopy(geometry)

@property
Expand All @@ -588,15 +594,15 @@ def parameter_values(self):

@parameter_values.setter
def parameter_values(self, parameter_values):
self._parameter_values = copy.copy(parameter_values)
self._parameter_values = parameter_values.copy()

@property
def submesh_types(self):
return self._submesh_types

@submesh_types.setter
def submesh_types(self, submesh_types):
self._submesh_types = copy.copy(submesh_types)
self._submesh_types = submesh_types.copy()

@property
def mesh(self):
Expand All @@ -608,23 +614,23 @@ def var_pts(self):

@var_pts.setter
def var_pts(self, var_pts):
self._var_pts = copy.copy(var_pts)
self._var_pts = var_pts.copy()

@property
def spatial_methods(self):
return self._spatial_methods

@spatial_methods.setter
def spatial_methods(self, spatial_methods):
self._spatial_methods = copy.copy(spatial_methods)
self._spatial_methods = spatial_methods.copy()

@property
def solver(self):
return self._solver

@solver.setter
def solver(self, solver):
self._solver = copy.copy(solver)
self._solver = solver.copy()

@property
def quick_plot_vars(self):
Expand Down Expand Up @@ -681,7 +687,7 @@ def specs(
"""

if model_options:
self._model_options = copy.copy(model_options)
self._model_options = model_options.copy()

if geometry:
self.geometry = geometry
Expand All @@ -701,7 +707,12 @@ def specs(

if C_rate:
self.C_rate = C_rate
self.parameter_values.update({"C-rate": self.C_rate})
self._parameter_values.update(
{
"Current function [A]": self.C_rate
* self._parameter_values["Cell capacity [A.h]"]
}
)

if (
model_options
Expand Down
8 changes: 8 additions & 0 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Base solver class
#
import casadi
import copy
import pybamm
import numbers
import numpy as np
Expand Down Expand Up @@ -105,6 +106,13 @@ def max_steps(self):
def max_steps(self, max_steps):
self._max_steps = max_steps

def copy(self):
"Returns a copy of the solver"
new_solver = copy.copy(self)
# clear models_set_up
new_solver.models_set_up = set()
return new_solver

def set_up(self, model, inputs=None):
"""Unpack model, perform checks, simplify and calculate jacobian.
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_models/standard_model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def test_solving(self, solver=None, t_eval=None):
self.solver.rtol = 1e-8
self.solver.atol = 1e-8

Crate = abs(self.parameter_values["C-rate"])
Crate = abs(
self.parameter_values["Current function [A]"]
* self.parameter_values["Cell capacity [A.h]"]
)
# don't allow zero C-rate
if Crate == 0:
Crate = 1
Expand Down
Loading

0 comments on commit 5a8ef1f

Please sign in to comment.