Skip to content

Commit

Permalink
#492 reformat constant current to be a submodel
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 14, 2019
1 parent 81bc4b6 commit c16b916
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 29 deletions.
10 changes: 4 additions & 6 deletions examples/scripts/compare_lead_acid_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@
# {"current collector": "potential pair", "dimensionality": 2}, name="2+1D LOQS"
# ),
pybamm.lead_acid.Full(
{"current collector": "potential pair", "dimensionality": 1}, name="1+1D Full"
{"current collector": "uniform", "dimensionality": 1}, name="1+1D Full"
),
# pybamm.lead_acid.Full(
# {"dimensionality": 1}, name="1+1D uniform Full"
# ),
pybamm.lead_acid.Composite(
{"current collector": "potential pair quite conductive", "dimensionality": 1},
name="1+1D composite",
{"current collector": "uniform", "dimensionality": 1}, name="1+1D composite"
),
pybamm.lead_acid.Composite(
{"current collector": "potential pair", "dimensionality": 1},
name="1+1D composite",
{"current collector": "uniform", "dimensionality": 1}, name="1+1D composite"
),
# # pybamm.lead_acid.Composite({"dimensionality": 1}, name="composite"),
pybamm.lead_acid.LOQS(
{"current collector": "potential pair", "dimensionality": 1}, name="1+1D LOQS"
{"current collector": "uniform", "dimensionality": 1}, name="1+1D LOQS"
),
# pybamm.lead_acid.LOQS({"dimensionality": 1}, name="LOQS"),
]
Expand Down
1 change: 1 addition & 0 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def version(formatted=False):
current_collector,
electrolyte,
electrode,
external_circuit,
interface,
oxygen_diffusion,
particle,
Expand Down
32 changes: 19 additions & 13 deletions pybamm/models/full_battery_models/base_battery_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def options(self):
@options.setter
def options(self, extra_options):
default_options = {
"operating mode": "current",
"dimensionality": 0,
"surface form": False,
"convection": False,
Expand All @@ -161,6 +162,10 @@ def options(self, extra_options):
raise pybamm.OptionError("option {} not recognised".format(name))

# Some standard checks to make sure options are compatible
if options["operating mode"] not in ["current", "potential"]:
raise pybamm.OptionError(
"operating mode '{}' not recognised".format(options["operating mode"])
)
if (
isinstance(self, (pybamm.lead_acid.LOQS, pybamm.lead_acid.Composite))
and options["surface form"] is False
Expand Down Expand Up @@ -341,18 +346,6 @@ def set_standard_output_variables(self):

self.variables = {}

# Current
i_cell = pybamm.electrical_parameters.current_with_time
i_cell_dim = pybamm.electrical_parameters.dimensional_current_density_with_time
I = pybamm.electrical_parameters.dimensional_current_with_time
self.variables.update(
{
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
}
)

# Time
time_scale = pybamm.electrical_parameters.timescale
self.variables.update(
Expand All @@ -361,7 +354,6 @@ def set_standard_output_variables(self):
"Time [s]": pybamm.t * time_scale,
"Time [min]": pybamm.t * time_scale / 60,
"Time [h]": pybamm.t * time_scale / 3600,
"Discharge capacity [A.h]": I * pybamm.t * time_scale / 3600,
}
)

Expand Down Expand Up @@ -491,6 +483,20 @@ def build_model(self):

self._built = True

def set_external_circuit_submodel(self):
"""
Define how the external circuit defines the boundary conditions for the model,
e.g. (not necessarily constant-) current, voltage, etc
"""
if self.options["operating mode"] == "current":
self.submodels[
"external circuit"
] = pybamm.external_circuit.ConstantCurrent(self.param)
if self.options["operating mode"] == "voltage":
self.submodels[
"external circuit"
] = pybamm.external_circuit.ConstantVoltage(self.param)

def set_thermal_submodel(self):

if self.options["thermal"] == "isothermal":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ def set_soc_variables(self):
if "Fractional Charge Input" not in self.variables:
fci = pybamm.Variable("Fractional Charge Input", domain="current collector")
self.variables["Fractional Charge Input"] = fci
self.rhs[fci] = -self.param.current_with_time * 100
self.rhs[fci] = -self.variables["Total current density"] * 100
self.initial_conditions[fci] = self.param.q_init * 100
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ class Uniform(BaseModel):
def __init__(self, param):
super().__init__(param)

def get_fundamental_variables(self):
def get_derived_variables(self, variables):

# TODO: grad not implemented for 2D yet
i_cc = pybamm.Scalar(0)
import ipdb

ipdb.set_trace()
i_boundary_cc = pybamm.PrimaryBroadcast(
self.param.current_with_time, "current collector"
variables["Total current density"], "current collector"
)
phi_s_cn = pybamm.PrimaryBroadcast(0, "current collector")

Expand Down
7 changes: 3 additions & 4 deletions pybamm/models/submodels/current_collector/potential_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def set_algebraic(self, variables):

def set_initial_conditions(self, variables):

param = self.param
applied_current = param.current_with_time
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()
phi_s_cn = variables["Negative current collector potential"]
i_boundary_cc = variables["Current collector current density"]
Expand All @@ -84,7 +83,7 @@ def set_boundary_conditions(self, variables):
phi_s_cp = variables["Positive current collector potential"]

param = self.param
applied_current = param.current_with_time
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()

# cc_area appears here due to choice of non-dimensionalisation
Expand Down Expand Up @@ -124,7 +123,7 @@ def set_boundary_conditions(self, variables):
phi_s_cp = variables["Positive current collector potential"]

param = self.param
applied_current = param.current_with_time
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()

# Note: we divide by the *numerical* tab area so that the correct total
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_fundamental_variables(self):
def set_algebraic(self, variables):

param = self.param
applied_current = param.current_with_time
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()
z = pybamm.standard_spatial_vars.z

Expand All @@ -73,7 +73,7 @@ def set_algebraic(self, variables):
def set_initial_conditions(self, variables):

param = self.param
applied_current = param.current_with_time
applied_current = variables["Total current density"]
cc_area = self._get_effective_current_collector_area()
phi_s_cn = variables["Negative current collector potential"]
i_boundary_cc = variables["Current collector current density"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def set_algebraic(self, variables):
def set_initial_conditions(self, variables):

param = self.param
applied_current = param.current_with_time
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"]
Expand Down
4 changes: 4 additions & 0 deletions pybamm/models/submodels/external_circuit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .base_external_circuit import BaseModel
from .current_control_external_circuit import CurrentControl

# from .voltage_control_external_circuit import VoltageControl
11 changes: 11 additions & 0 deletions pybamm/models/submodels/external_circuit/base_external_circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Base model for the external circuit
#
import pybamm


class BaseModel(pybamm.BaseSubModel):
"""Model to represent the behaviour of the external circuit. """

def __init__(self, param):
super().__init__(param)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# External circuit with current control
#
import pybamm
from .base_external_circuit import BaseModel


class CurrentControl(BaseModel):
"""External circuit with current control. """

def __init__(self, param):
super().__init__(param)

def get_fundamental_variables(self):
# Current is given as a function of time
i_cell = self.param.current_with_time
i_cell_dim = self.param.dimensional_current_density_with_time
I = self.param.dimensional_current_with_time

variables = {
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
"Discharge capacity [A.h]": I * pybamm.t * self.param.time_scale / 3600,
}

return variables
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# External circuit with voltage control
#
import pybamm
from .base_external_circuit import BaseModel


class VoltageControl(BaseModel):
"""External circuit with voltage control. """

def __init__(self, param):
super().__init__(param)

def get_fundamental_variables(self):
# Current is given as a function of time
i_cell = pybamm.electrical_parameters.current_with_time
i_cell_dim = pybamm.electrical_parameters.dimensional_current_density_with_time
I = pybamm.electrical_parameters.dimensional_current_with_time

variables = {
"Total current density": i_cell,
"Total current density [A.m-2]": i_cell_dim,
"Current [A]": I,
"Discharge capacity [A.h]": I * pybamm.t * self.param.time_scale / 3600,
}

return variables

0 comments on commit c16b916

Please sign in to comment.