Skip to content

Commit

Permalink
#492 updating current function and C-rate
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 29, 2019
1 parent 172f1ec commit a3b302e
Show file tree
Hide file tree
Showing 46 changed files with 134 additions and 132 deletions.
2 changes: 1 addition & 1 deletion examples/scripts/DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# load parameter values and process model and geometry
param = model.default_parameter_values
param["Voltage function"] = 4.1
param["Voltage function [V]"] = 4.1
param.process_model(model)
param.process_geometry(geometry)

Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/SPMe_SOC.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Initial concentration in positive electrode [mol.m-3]": 25000,
"Negative electrode surface area density [m-1]": 180000.0,
"Positive electrode surface area density [m-1]": 150000.0,
"Typical current [A]": I_app,
"Current function [A]": I_app,
}
)
param.process_model(model)
Expand Down
6 changes: 3 additions & 3 deletions examples/scripts/charging_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
params = [model.default_parameter_values for model in models]

# 1. Charge at 1A
params[0]["Typical current [A]"] = -0.1
params[0]["Current function [A]"] = -0.1

# # 2. CV: Charge at 4.1V
params[1]["Voltage function"] = 4.1
params[1]["Voltage function [V]"] = 4.1

# 3. CP-CV: Charge at 4W
params[2]["Power function"] = -0.4
params[2]["Power function [W]"] = -0.4


solutions = []
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_SPM_diffusion_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# load parameter values and process models and geometry
param = models[0].default_parameter_values
param.update({"Typical current [A]": 1})
param.update({"Current function [A]": 1})
for model in models:
param.process_model(model)

Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_comsol/compare_comsol_DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
param = pybamm_model.default_parameter_values
param["Electrode width [m]"] = 1
param["Electrode height [m]"] = 1
param["Typical current [A]"] = 24 * C_rates[C_rate]
param["Current function [A]"] = 24 * C_rates[C_rate]
param.process_model(pybamm_model)
param.process_geometry(geometry)

Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_comsol/discharge_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
comsol_voltage = comsol_variables["voltage"]

# update current density
param["Typical current [A]"] = 24 * C_rate
param["Current function [A]"] = 24 * C_rate
param.update_model(model, disc)

# discharge timescale
Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/compare_lead_acid.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
param = models[0].default_parameter_values
param.update(
{
"Typical current [A]": 10,
"Current function [A]": 10,
"Initial State of Charge": 1,
"Voltage function": 14 / 6,
"Voltage function [V]": 14 / 6,
}
)
for model in models:
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_lead_acid_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
param = models[0].default_parameter_values
param.update(
{
"Typical current [A]": 1,
"Current function [A]": 1,
"Bruggeman coefficient": 0.001,
"Initial State of Charge": 1,
"Typical electrolyte concentration [mol.m-3]": 5600,
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_lithium_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# load parameter values and process models and geometry
param = models[0].default_parameter_values
param["Typical current [A]"] = 1.0
param["Current function [A]"] = 1.0

for model in models:
param.process_model(model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Positive tab centre z-coordinate [m],0.114,Tab at top,
,,,
# Electrical,,,
Cell capacity [A.h],17,Manufacturer,
Typical current [A],1,,
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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,1,,
Current function,[constant],,
,,,
# Initial conditions
Initial State of Charge,1,-,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,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],1.46925726107,,1C current
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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,,
Current function,[constant],,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],19986.609595075,Scott Moura FastDFN,
Expand Down
14 changes: 14 additions & 0 deletions pybamm/models/submodels/external_circuit/base_external_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ class BaseModel(pybamm.BaseSubModel):
def __init__(self, param):
super().__init__(param)

def _get_current_variables(self, i_cell):
param = self.param
I = i_cell * abs(param.I_typ)
i_cell_dim = I / (param.n_electrodes_parallel * param.A_cc)

variables = {
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
"C-rate": I / param.Q,
}

return variables

def get_fundamental_variables(self):
Q = pybamm.Variable("Discharge capacity [A.h]")
variables = {"Discharge capacity [A.h]": Q}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_fundamental_variables(self):
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
"C-rate": I / self.param.Q,
}

# Add discharge capacity variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ def __init__(self, param, external_circuit_class):

def get_fundamental_variables(self):
# Current is a variable
param = self.param
i_cell = pybamm.Variable("Total current density")
I = i_cell * abs(param.I_typ)
i_cell_dim = I / (param.n_electrodes_parallel * param.A_cc)

variables = {
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
}
variables = self._get_current_variables(i_cell)

# Add discharge capacity variable
variables.update(super().get_fundamental_variables())
Expand Down Expand Up @@ -78,7 +70,7 @@ class ConstantVoltage:

def __call__(self, variables):
V = variables["Terminal voltage [V]"]
return V - pybamm.FunctionParameter("Voltage function", pybamm.t)
return V - pybamm.FunctionParameter("Voltage function [V]", pybamm.t)


class PowerFunctionControl(FunctionControl):
Expand All @@ -94,5 +86,5 @@ class ConstantPower:
def __call__(self, variables):
I = variables["Current [A]"]
V = variables["Terminal voltage [V]"]
return I * V - pybamm.FunctionParameter("Power function", pybamm.t)
return I * V - pybamm.FunctionParameter("Power function [W]", pybamm.t)

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, param):

def get_fundamental_variables(self):
# Voltage is given as a function of time
V_dim = pybamm.FunctionParameter("Voltage function", pybamm.t)
V_dim = pybamm.FunctionParameter("Voltage function [V]", pybamm.t)

param = self.param
V = (V_dim - (param.U_p_ref - param.U_n_ref)) / param.potential_scale
Expand All @@ -37,13 +37,6 @@ def get_coupled_variables(self, variables):
param.sigma_p * tor, "right"
) * pybamm.BoundaryGradient(phi_s_p, "right")
i_cell = pybamm.BoundaryValue(i_boundary_cc, "positive tab")
I = i_cell * abs(param.I_typ)
i_cell_dim = I / (param.n_electrodes_parallel * param.A_cc)

variables = {
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
}
variables = self._get_current_variables(i_cell)

return variables
2 changes: 1 addition & 1 deletion pybamm/parameters/electrical_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# the user may provide the typical timescale as a parameter.
timescale = pybamm.Parameter("Typical timescale [s]")
dimensional_current_with_time = pybamm.FunctionParameter(
"Current function", pybamm.t * timescale
"Current function [A]", pybamm.t * timescale
)
dimensional_current_density_with_time = dimensional_current_with_time / (
n_electrodes_parallel * pybamm.geometric_parameters.A_cc
Expand Down
74 changes: 41 additions & 33 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ def update(self, values, check_conflict=False, path=""):
).to_numpy()
# Save name and data
super().__setitem__(name, (function_name, data))
# Special case (hacky) for zero current
elif value == "[zero]":
super().__setitem__(name, 0)
# Anything else should be a converted to a float
else:
super().__setitem__(name, float(value))
Expand All @@ -190,19 +187,18 @@ def update(self, values, check_conflict=False, path=""):
self._processed_symbols = {}

def check_and_update_parameter_values(self, values):
# Make sure "C-rate" and current are both non-zero
if "C-rate" in values and values["C-rate"] == 0:
# Make typical current is non-zero
if "Typical current [A]" in values and values["Typical current [A]"] == 0:
raise ValueError(
"""
"C-rate" cannot be zero. A possible alternative is to set
"Current function" to `0` instead.
"Typical current [A]" cannot be zero. A possible alternative is to set
"Current function [A]" to `0` instead.
"""
)
if "Typical current [A]" in values and values["Typical current [A]"] == 0:
if "C-rate" in values and "Current function [A]" in values:
raise ValueError(
"""
"Typical current [A]" cannot be zero. A possible alternative is to set
"Current function" to `0` instead.
Cannot provide both "C-rate" and "Current function [A]" simultaneously
"""
)
# If the capacity of the cell has been provided, make sure "C-rate" and current
Expand All @@ -214,30 +210,22 @@ def check_and_update_parameter_values(self, values):
else:
capacity = self["Cell capacity [A.h]"]
# Make sure they match if both provided
if "C-rate" in values and "Typical current [A]" in values:
if values["C-rate"] * capacity != values["Typical current [A]"]:
raise ValueError(
"""
"C-rate" ({}C) and Typical current ({} A) provided do not match
given capacity ({} Ah). These can be updated individually
instead.
""".format(
values["C-rate"], values["Typical current [A]"], capacity
)
)
# Update the other if only one provided
elif "C-rate" in values:
values["Typical current [A]"] = float(values["C-rate"]) * capacity
elif "Typical current [A]" in values:
values["C-rate"] = float(values["Typical current [A]"]) / capacity

# Update the current function if it is constant
self_and_values = {**self, **values}
if "Current function" in self_and_values and (
self_and_values["Current function"] == "[constant]"
or isinstance(self_and_values["Current function"], numbers.Number)
):
values["Current function"] = {**self, **values}["Typical current [A]"]
if "C-rate" in values:
# Can't provide C-rate as a function
if callable(values["C-rate"]):
values["Current function [A]"] = CrateToCurrent(
values["C-rate"], capacity
)
else:
values["Current function [A]"] = float(values["C-rate"]) * capacity
elif "Current function [A]" in values:
if callable(values["Current function [A]"]):
values["C-rate"] = CurrentToCrate(
values["Current function [A]"], capacity
)
else:
values["C-rate"] = float(values["Current function [A]"]) / capacity

return values

Expand Down Expand Up @@ -546,3 +534,23 @@ def evaluate(self, symbol):
return processed_symbol.evaluate()
else:
raise ValueError("symbol must evaluate to a constant scalar")


class CurrentToCrate:
"Convert a current function to a C-rate function"
def __init__(self, function, capacity):
self.function = function
self.capacity = capacity

def __call__(self, t):
return self.function(t) / self.capacity


class CrateToCurrent:
"Convert a C-rate function to a current function"
def __init__(self, function, capacity):
self.function = function
self.capacity = capacity

def __call__(self, t):
return self.function(t) * self.capacity
2 changes: 1 addition & 1 deletion pybamm/parameters/standard_parameters_lead_acid.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def U_p(c_e_p, T):
# 6. Input current and voltage

dimensional_current_with_time = pybamm.FunctionParameter(
"Current function", pybamm.t * timescale
"Current function [A]", pybamm.t * timescale
)
dimensional_current_density_with_time = dimensional_current_with_time / (
n_electrodes_parallel * pybamm.geometric_parameters.A_cc
Expand Down
2 changes: 1 addition & 1 deletion pybamm/parameters/standard_parameters_lithium_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def dUdT_p(c_s_p):
# 6. Input current and voltage

dimensional_current_with_time = pybamm.FunctionParameter(
"Current function", pybamm.t * timescale
"Current function [A]", pybamm.t * timescale
)
dimensional_current_density_with_time = dimensional_current_with_time / (
n_electrodes_parallel * pybamm.geometric_parameters.A_cc
Expand Down
4 changes: 1 addition & 3 deletions results/2019_08_sulzer_thesis/self_discharge.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def self_discharge_states(compute):
name="LOQS, with oxygen",
),
]
extra_parameter_values = {
"Current function": "[zero]"
}
extra_parameter_values = {"Current function [A]": 0}
t_eval = np.linspace(0, 1000, 100)
all_variables, t_eval = model_comparison(
models, [1], t_eval, extra_parameter_values=extra_parameter_values
Expand Down
4 changes: 2 additions & 2 deletions results/2019_08_sulzer_thesis/shared_solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def model_comparison(models, Crates, t_eval, extra_parameter_values=None):
all_variables[Crate] = {}
current = Crate * 17
pybamm.logger.info("Setting typical current to {} A".format(current))
param.update({"Typical current [A]": current})
param.update({"Current function [A]": current})
for model in models:
param.update_model(model, discs[model])
solution = model.default_solver.solve(model, t_eval)
Expand Down Expand Up @@ -86,7 +86,7 @@ def convergence_study(models, Crates, all_npts, t_eval, extra_parameter_values=N
for Crate in Crates:
current = Crate * 17
pybamm.logger.info("Setting typical current to {} A".format(current))
param.update({"Typical current [A]": current})
param.update({"Current function [A]": current})
for model in models:
model_disc = models_disc[model.name]
disc = discs[model.name]
Expand Down
2 changes: 1 addition & 1 deletion results/2plus1D/spm_1plus1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
current_1C = 24 * param.process_symbol(pybamm.geometric_parameters.A_cc).evaluate()
param.update(
{
"Typical current [A]": C_rate * current_1C,
"Current function [A]": C_rate * current_1C,
"Initial temperature [K]": 298.15,
"Negative current collector conductivity [S.m-1]": 1e7,
"Positive current collector conductivity [S.m-1]": 1e7,
Expand Down
2 changes: 1 addition & 1 deletion results/2plus1D/user_mesh_spm_1plus1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
current_1C = 24 * param.evaluate(pybamm.geometric_parameters.A_cc)
param.update(
{
"Typical current [A]": C_rate * current_1C,
"Current function [A]": C_rate * current_1C,
"Initial temperature [K]": 298.15,
"Negative current collector conductivity [S.m-1]": 1e5,
"Positive current collector conductivity [S.m-1]": 1e5,
Expand Down
2 changes: 1 addition & 1 deletion results/drive_cycles/US06_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# load parameter values and process model and geometry
param = model.default_parameter_values
param["Current function"] = "[current data]US06"
param["Current function [A]"] = "[current data]US06"
param.process_model(model)
param.process_geometry(geometry)

Expand Down
Loading

0 comments on commit a3b302e

Please sign in to comment.