Skip to content

Commit

Permalink
#492 clean up setting external variables for potentials
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Dec 4, 2019
1 parent f113293 commit 5f3bd18
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ class BaseModel(pybamm.BaseSubModel):
def __init__(self, param):
super().__init__(param)

def get_coupled_variables(self, variables):

# 1D models determine phi_s_cp
phi_s_cn = variables["Negative current collector potential"]
phi_s_cp = variables["Positive current collector potential"]

variables = self._get_standard_potential_variables(phi_s_cn, phi_s_cp)
return variables

def _get_standard_negative_potential_variables(self, phi_s_cn):
"""
A private function to obtain the standard variables which
Expand All @@ -54,42 +45,6 @@ def _get_standard_negative_potential_variables(self, phi_s_cn):

return variables

def _get_standard_potential_variables(self, phi_s_cn, phi_s_cp):
"""
A private function to obtain the standard variables which
can be derived from the potentials in the current collector.
Parameters
----------
phi_s_cn : :class:`pybamm.Symbol`
The potential in the negative current collector.
phi_s_cp : :class:`pybamm.Symbol`
The potential in the positive current collector.
Returns
-------
variables : dict
The variables which can be derived from the potential in the
current collector.
"""

pot_scale = self.param.potential_scale
U_ref = self.param.U_p_ref - self.param.U_n_ref

# Local potential difference
V_cc = phi_s_cp - phi_s_cn

variables = {
"Positive current collector potential": phi_s_cp,
"Positive current collector potential [V]": U_ref + phi_s_cp * pot_scale,
"Local current collector potential difference": V_cc,
"Local current collector potential difference [V]": U_ref
+ V_cc * pot_scale,
}
variables.update(self._get_standard_negative_potential_variables(phi_s_cn))

return variables

def _get_standard_current_variables(self, i_cc, i_boundary_cc):
"""
A private function to obtain the standard variables which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def __init__(self, param):
def get_fundamental_variables(self):

phi_s_cn = pybamm.standard_variables.phi_s_cn
phi_s_cp = pybamm.standard_variables.phi_s_cp

variables = self._get_standard_potential_variables(phi_s_cn, phi_s_cp)
variables = self._get_standard_negative_potential_variables(phi_s_cn)

# TO DO: grad not implemented for 2D yet
i_cc = pybamm.Scalar(0)
Expand All @@ -53,11 +52,10 @@ def get_fundamental_variables(self):

def set_rhs(self, variables):
phi_s_cn = variables["Negative current collector potential"]
phi_s_cp = variables["Positive current collector potential"]

# Dummy equations so that PyBaMM doesn't change the potentials during solve
# i.e. d_phi/d_t = 0. Potentials are set externally between steps.
self.rhs = {phi_s_cn: pybamm.Scalar(0), phi_s_cp: pybamm.Scalar(0)}
self.rhs = {phi_s_cn: pybamm.Scalar(0)}

def set_algebraic(self, variables):
ocp_p_av = variables["X-averaged positive electrode open circuit potential"]
Expand All @@ -84,17 +82,13 @@ def set_algebraic(self, variables):

def set_initial_conditions(self, variables):

param = self.param
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()
phi_s_cn = variables["Negative current collector potential"]
phi_s_cp = variables["Positive current collector potential"]
i_boundary_cc = variables["Current collector current density"]

self.initial_conditions = {
phi_s_cn: pybamm.Scalar(0),
phi_s_cp: param.U_p(param.c_p_init, param.T_init)
- param.U_n(param.c_n_init, param.T_init),
i_boundary_cc: applied_current / cc_area,
}

Expand Down
27 changes: 14 additions & 13 deletions pybamm/models/submodels/electrode/base_electrode.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ def _get_standard_current_collector_potential_variables(self, phi_s_cn, phi_s_cp

pot_scale = self.param.potential_scale
U_ref = self.param.U_p_ref - self.param.U_n_ref
phi_s_cp_dim = U_ref + phi_s_cp * pot_scale

# Local potential difference
V_cc = phi_s_cp - phi_s_cn

# terminal voltage (Note: phi_s_cn is zero at the negative tab)
# Terminal voltage
# Note phi_s_cn is always zero at the negative tab
V = pybamm.boundary_value(phi_s_cp, "positive tab")
V_dim = pybamm.boundary_value(phi_s_cp_dim, "positive tab")

# Voltage is local current collector potential difference at the tabs, in 1D
# this will be equal to the local current collector potential difference
phi_s_cp_dim = U_ref + phi_s_cp * pot_scale

variables = {
"Negative current collector potential": phi_s_cn,
Expand All @@ -142,6 +146,8 @@ def _get_standard_current_collector_potential_variables(self, phi_s_cn, phi_s_cp
"Positive current collector potential [V]": phi_s_cp_dim,
"Local voltage": V_cc,
"Local voltage [V]": U_ref + V_cc * pot_scale,
"Terminal voltage": V,
"Terminal voltage [V]": V_dim,
}

return variables
Expand Down Expand Up @@ -170,18 +176,13 @@ def _get_standard_whole_cell_variables(self, variables):
i_s = pybamm.Concatenation(i_s_n, i_s_s, i_s_p)

variables.update({"Electrode current density": i_s})

if self.set_positive_potential:
# don't overwrite current collector potentials
try:
phi_s_cn = variables["Negative current collector potential"]
except KeyError:
phi_s_n = variables["Negative electrode potential"]
phi_s_cn = pybamm.boundary_value(phi_s_n, "left")
try:
phi_s_cp = variables["Positive current collector potential"]
except KeyError:
phi_s_p = variables["Positive electrode potential"]
phi_s_cp = pybamm.boundary_value(phi_s_p, "right")
# Get phi_s_cn from the current collector submodel and phi_s_p from the
# electrode submodel
phi_s_cn = variables["Negative current collector potential"]
phi_s_p = variables["Positive electrode potential"]
phi_s_cp = pybamm.boundary_value(phi_s_p, "right")
variables.update(
self._get_standard_current_collector_potential_variables(
phi_s_cn, phi_s_cp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# External circuit with current control
#
import pybamm
from .base_external_circuit import BaseModel, LeadingOrderBaseModel


Expand Down Expand Up @@ -29,18 +28,6 @@ def get_fundamental_variables(self):

return variables

def get_coupled_variables(self, variables):
# Update terminal voltage
phi_s_cp_dim = variables["Positive current collector potential [V]"]
phi_s_cp = variables["Positive current collector potential"]

V = pybamm.boundary_value(phi_s_cp, "positive tab")
V_dim = pybamm.boundary_value(phi_s_cp_dim, "positive tab")
variables["Terminal voltage"] = V
variables["Terminal voltage [V]"] = V_dim

return variables


class LeadingOrderCurrentControl(CurrentControl, LeadingOrderBaseModel):
"""External circuit with current control, for leading order models. """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ def get_fundamental_variables(self):

return variables

def get_coupled_variables(self, variables):
# Update terminal voltage
phi_s_cp_dim = variables["Positive current collector potential [V]"]
phi_s_cp = variables["Positive current collector potential"]

V = pybamm.boundary_value(phi_s_cp, "positive tab")
V_dim = pybamm.boundary_value(phi_s_cp_dim, "positive tab")
variables["Terminal voltage"] = V
variables["Terminal voltage [V]"] = V_dim

return variables

def set_initial_conditions(self, variables):
super().set_initial_conditions(variables)
# Initial condition as a guess for consistent initial conditions
Expand Down

0 comments on commit 5f3bd18

Please sign in to comment.