-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
total_interfacial_current.py
172 lines (148 loc) · 6.76 KB
/
total_interfacial_current.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#
# Total interfacial current class, summing up contributions from all reactions
#
import pybamm
class TotalInterfacialCurrent(pybamm.BaseSubModel):
"""
Total interfacial current, summing up contributions from all reactions
Parameters
----------
param :
model parameters
chemistry : str
The name of the battery chemistry whose reactions need to be summed up
options: dict
A dictionary of options to be passed to the model.
See :class:`pybamm.BaseBatteryModel`
"""
def __init__(self, param, chemistry, options):
super().__init__(param, options=options)
self.chemistry = chemistry
def get_coupled_variables(self, variables):
"""
Get variables associated with interfacial current over the whole cell domain
This function also creates the "total source term" variables by summing all
the reactions.
"""
for domain in self.options.whole_cell_domains:
if domain != "separator":
variables.update(
self._get_coupled_variables_by_domain(variables, domain.split()[0])
)
variables.update(self._get_whole_cell_coupled_variables(variables))
return variables
def _get_coupled_variables_by_domain(self, variables, domain):
phase_names = [""]
num_phases = int(getattr(self.options, domain)["particle phases"])
if num_phases > 1:
phase_names += ["primary ", "secondary "]
for phase_name in phase_names:
variables.update(
self._get_coupled_variables_by_phase_and_domain(
variables, domain, phase_name
)
)
return variables
def _get_coupled_variables_by_phase_and_domain(self, variables, domain, phase_name):
Domain = domain.capitalize()
if self.chemistry == "lithium-ion":
reaction_names = [""]
if phase_name == "":
reaction_names += ["SEI "]
if self.options.electrode_types[domain] == "porous":
# separate plating reaction only if the electrode is porous,
# since plating is the main reaction otherwise.
# Likewise, SEI on cracks only in a porous electrode
reaction_names.extend(["lithium plating ", "SEI on cracks "])
elif self.chemistry == "lead-acid":
reaction_names = ["", "oxygen "]
# Create separate 'new_variables' so that variables only get updated once
# everything is computed
new_variables = variables.copy()
# Initialize "total reaction" variables
# These will get populated by each reaction, and then used
# later by "set_rhs" or "set_algebraic", which ensures that we always have
# added all the necessary variables by the time the sum is used
new_variables.update(
{
f"Sum of {domain} electrode {phase_name}"
"electrolyte reaction source terms [A.m-3]": 0,
f"Sum of x-averaged {domain} electrode {phase_name}"
"electrolyte reaction source terms [A.m-3]": 0,
f"Sum of {domain} electrode {phase_name}"
"volumetric interfacial current densities [A.m-3]": 0,
f"Sum of x-averaged {domain} electrode {phase_name}"
"volumetric interfacial current densities [A.m-3]": 0,
}
)
for reaction_name in reaction_names:
# Sum variables
a_j_k = new_variables[
f"{Domain} electrode {reaction_name}{phase_name}volumetric "
"interfacial current density [A.m-3]"
]
if self.chemistry == "lithium-ion":
# Both the main reaction current contribute to the electrolyte reaction
# current
s_k = 1
elif self.chemistry == "lead-acid":
if reaction_name == "": # main reaction
s_k = self.param.domain_params[domain].prim.s_plus_S
elif reaction_name == "oxygen ":
s_k = self.param.s_plus_Ox
new_variables[
f"Sum of {domain} electrode {phase_name}"
"electrolyte reaction source terms [A.m-3]"
] += s_k * a_j_k
new_variables[
f"Sum of x-averaged {domain} electrode {phase_name}"
"electrolyte reaction source terms [A.m-3]"
] += pybamm.x_average(s_k * a_j_k)
new_variables[
f"Sum of {domain} electrode {phase_name}volumetric "
"interfacial current densities [A.m-3]"
] += a_j_k
new_variables[
f"Sum of x-averaged {domain} electrode {phase_name}"
"volumetric interfacial current densities [A.m-3]"
] += pybamm.x_average(a_j_k)
variables.update(new_variables)
return variables
def _get_whole_cell_coupled_variables(self, variables):
# Interfacial current density and exchange-current density for the main reaction
zero_s = pybamm.FullBroadcast(0, "separator", "current collector")
if (
self.options.negative["particle phases"] == "1"
and self.options.positive["particle phases"] == "1"
):
for variable_template in [
"{}interfacial current density [A.m-2]",
"{}exchange current density [A.m-2]",
]:
var_dict = {}
for domain in self.options.whole_cell_domains:
if domain == "separator":
var_dict[domain] = zero_s
else:
Domain = domain.capitalize()
var_dict[domain] = variables[
variable_template.format(Domain + " ")
]
var = pybamm.concatenation(*var_dict.values())
var_name = variable_template.format("")
var_name = var_name[0].upper() + var_name[1:] # capitalise first letter
variables.update({var_name: var})
# Sum variables
for variable_template in [
"Sum of {}volumetric interfacial current densities [A.m-3]",
"Sum of {}electrolyte reaction source terms [A.m-3]",
]:
var_dict = {}
for domain in self.options.whole_cell_domains:
if domain == "separator":
var_dict[domain] = zero_s
else:
var_dict[domain] = variables[variable_template.format(domain + " ")]
var = pybamm.concatenation(*var_dict.values())
variables.update({variable_template.format(""): var})
return variables