Skip to content

Commit

Permalink
#2418 working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 14, 2022
1 parent e8abbc0 commit 7ff854c
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 256 deletions.
4 changes: 2 additions & 2 deletions examples/scripts/compare_lithium_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
pybamm.lithium_ion.SPM(),
pybamm.lithium_ion.SPMe(),
pybamm.lithium_ion.DFN(),
# pybamm.lithium_ion.NewmanTobias(),
pybamm.lithium_ion.NewmanTobias(),
]

# create and run simulations
Expand All @@ -18,7 +18,7 @@
sim = pybamm.Simulation(
model,
# parameter_values=pybamm.ParameterValues("Ecker2015"),
solver=pybamm.CasadiSolver(dt_max=600), # root_method="lm"),
solver=pybamm.CasadiSolver(dt_max=600), # , root_method="lm"),
)
sim.solve([0, 3600])
sims.append(sim)
Expand Down
11 changes: 8 additions & 3 deletions examples/scripts/compare_lithium_ion_two_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
"Average negative primary particle concentration",
"Average negative secondary particle concentration",
],
"X-averaged negative electrode primary interfacial current density [A.m-2]",
"X-averaged negative electrode secondary interfacial current density [A.m-2]",
[
"X-averaged negative electrode primary volumetric "
"interfacial current density [A.m-3]",
"X-averaged negative electrode secondary volumetric "
"interfacial current density [A.m-3]",
"X-averaged negative electrode volumetric "
"interfacial current density [A.m-3]",
],
"X-averaged negative electrode primary open circuit potential [V]",
"X-averaged negative electrode secondary open circuit potential [V]",
"X-averaged negative electrode volumetric interfacial current density",
"Average positive particle concentration [mol.m-3]",
"Terminal voltage [V]",
],
Expand Down
17 changes: 10 additions & 7 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ def set_variable_slices(self, variables):
y_slices[variable].append(slice(start, end))
y_slices_explicit[variable].append(slice(start, end))
# Add to bounds
lower_bounds.extend([variable.bounds[0]] * (end - start))
upper_bounds.extend([variable.bounds[1]] * (end - start))
lower_bounds.extend([variable.bounds[0].evaluate()] * (end - start))
upper_bounds.extend([variable.bounds[1].evaluate()] * (end - start))
# Increment start
start = end

Expand Down Expand Up @@ -1000,12 +1000,15 @@ def _process_symbol(self, symbol):
# create new children without scale and reference
# the scale and reference will be applied to the concatenation instead
new_children = []
old_y_slices = self.y_slices.copy()
for child in symbol.children:
child = child.create_copy()
child._scale = 1
child._reference = 0
child.set_id()
new_children.append(self.process_symbol(child))
child_no_scale = child.create_copy()
child_no_scale._scale = 1
child_no_scale._reference = 0
child_no_scale.set_id()
self.y_slices[child_no_scale] = self.y_slices[child]
new_children.append(self.process_symbol(child_no_scale))
self.y_slices = old_y_slices
new_symbol = spatial_method.concatenation(new_children)
# apply scale to the whole concatenation
return symbol.reference + symbol.scale * new_symbol
Expand Down
23 changes: 14 additions & 9 deletions pybamm/geometry/battery_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def battery_geometry(
"""
options = pybamm.BatteryModelOptions(options or {})
geo = pybamm.geometric_parameters
geo = pybamm.GeometricParameters(options)
L_n = geo.n.L
L_s = geo.s.L
L_n_L_s = L_n + L_s
Expand All @@ -48,22 +48,27 @@ def battery_geometry(
}
# Add particle domains
if include_particles is True:
geometry.update(
{
"negative particle": {"r_n": {"min": 0, "max": geo.n.prim.R_typ}},
"positive particle": {"r_p": {"min": 0, "max": geo.p.prim.R_typ}},
}
)
for domain in ["negative", "positive"]:
if options.electrode_types[domain] == "planar":
continue
geo_domain = geo.domain_params[domain]
d = domain[0]
geometry.update(
{
f"{domain} particle": {
f"r_{d}": {"min": 0, "max": geo_domain.prim.R_typ}
},
}
)
phases = int(getattr(options, domain)["particle phases"])
if phases >= 2:
geometry.update(
{
f"{domain} primary particle": {
"r_n_prim": {"min": 0, "max": geo.n.prim.R_typ}
f"r_{d}_prim": {"min": 0, "max": geo_domain.prim.R_typ}
},
f"{domain} secondary particle": {
"r_n_sec": {"min": 0, "max": geo.n.sec.R_typ}
f"r_{d}_sec": {"min": 0, "max": geo_domain.sec.R_typ}
},
}
)
Expand Down
24 changes: 7 additions & 17 deletions pybamm/meshes/one_dimensional_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,28 +374,18 @@ def __init__(self, lims, npts, edges=None, order=2):

coord_sys = spatial_var.coord_sys

array = np.array(
[
((order + 1) - 1 - 2 * i) / (2 * (order + 1) - 2)
for i in range(order + 1)
]
)
cv_edges = np.array(
[edges[0]]
+ [
x
for (a, b) in zip(edges[:-1], edges[1:])
for x in np.flip(
a
+ 0.5
* (b - a)
* (
1
+ np.sin(
np.pi
* np.array(
[
((order + 1) - 1 - 2 * i) / (2 * (order + 1) - 2)
for i in range(order + 1)
]
)
)
)
)[1:]
for x in np.flip(a + 0.5 * (b - a) * (1 + np.sin(np.pi * array)))[1:]
]
)

Expand Down
40 changes: 27 additions & 13 deletions pybamm/models/full_battery_models/lithium_ion/basic_dfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,39 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
Q = pybamm.Variable("Discharge capacity [A.h]")
# Variables that vary spatially are created with a domain
c_e_n = pybamm.Variable(
"Negative electrolyte concentration [mol.m-3]", domain="negative electrode"
"Negative electrolyte concentration [mol.m-3]",
domain="negative electrode",
scale=param.c_e_typ,
)
c_e_s = pybamm.Variable(
"Separator electrolyte concentration [mol.m-3]", domain="separator"
"Separator electrolyte concentration [mol.m-3]",
domain="separator",
scale=param.c_e_typ,
)
c_e_p = pybamm.Variable(
"Positive electrolyte concentration [mol.m-3]", domain="positive electrode"
"Positive electrolyte concentration [mol.m-3]",
domain="positive electrode",
scale=param.c_e_typ,
)
# Concatenations combine several variables into a single variable, to simplify
# implementing equations that hold over several domains
c_e = pybamm.concatenation(c_e_n, c_e_s, c_e_p)

# Electrolyte potential
phi_e_n = pybamm.Variable(
"Negative electrolyte potential [V]", domain="negative electrode"
"Negative electrolyte potential [V]",
domain="negative electrode",
reference=-param.n.prim.U_init,
)
phi_e_s = pybamm.Variable(
"Separator electrolyte potential [V]", domain="separator"
"Separator electrolyte potential [V]",
domain="separator",
reference=-param.n.prim.U_init,
)
phi_e_p = pybamm.Variable(
"Positive electrolyte potential [V]", domain="positive electrode"
"Positive electrolyte potential [V]",
domain="positive electrode",
reference=-param.n.prim.U_init,
)
phi_e = pybamm.concatenation(phi_e_n, phi_e_s, phi_e_p)

Expand All @@ -71,7 +83,9 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
"Negative electrode potential [V]", domain="negative electrode"
)
phi_s_p = pybamm.Variable(
"Positive electrode potential [V]", domain="positive electrode"
"Positive electrode potential [V]",
domain="positive electrode",
reference=param.ocv_init,
)
# Particle concentrations are variables on the particle domain, but also vary in
# the x-direction (electrode domain) and so must be provided with auxiliary
Expand All @@ -80,11 +94,13 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
"Negative particle concentration [mol.m-3]",
domain="negative particle",
auxiliary_domains={"secondary": "negative electrode"},
scale=param.n.prim.c_max,
)
c_s_p = pybamm.Variable(
"Positive particle concentration [mol.m-3]",
domain="positive particle",
auxiliary_domains={"secondary": "positive electrode"},
scale=param.p.prim.c_max,
)

# Constant temperature
Expand Down Expand Up @@ -129,16 +145,14 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
c_s_surf_n = pybamm.surf(c_s_n)
sto_surf_n = c_s_surf_n / param.n.prim.c_max
j0_n = param.n.prim.j0(c_e_n, c_s_surf_n, T)
eta_n = (
phi_s_n - phi_e_n - (param.n.prim.U(sto_surf_n, T) - param.n.prim.U_init)
)
eta_n = phi_s_n - phi_e_n - param.n.prim.U(sto_surf_n, T)
Feta_RT_n = param.F * eta_n / (param.R * T)
j_n = 2 * j0_n * pybamm.sinh(param.n.prim.ne / 2 * Feta_RT_n)

c_s_surf_p = pybamm.surf(c_s_p)
sto_surf_p = c_s_surf_p / param.p.prim.c_max
j0_p = param.p.prim.j0(c_e_p, c_s_surf_p, T)
eta_p = phi_s_p - phi_e_p - (param.p.prim.U(sto_surf_p, T) - param.ocv_init)
eta_p = phi_s_p - phi_e_p - param.p.prim.U(sto_surf_p, T)
Feta_RT_p = param.F * eta_p / (param.R * T)
j_s = pybamm.PrimaryBroadcast(0, "separator")
j_p = 2 * j0_p * pybamm.sinh(param.p.prim.ne / 2 * Feta_RT_p)
Expand Down Expand Up @@ -207,7 +221,7 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
# initial guess for a root-finding algorithm which calculates consistent initial
# conditions
self.initial_conditions[phi_s_n] = pybamm.Scalar(0)
self.initial_conditions[phi_s_p] = pybamm.Scalar(0) # param.ocv_init
self.initial_conditions[phi_s_p] = param.ocv_init

######################
# Current in the electrolyte
Expand All @@ -220,7 +234,7 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
"left": (pybamm.Scalar(0), "Neumann"),
"right": (pybamm.Scalar(0), "Neumann"),
}
self.initial_conditions[phi_e] = pybamm.Scalar(0) # -param.n.prim.U_init
self.initial_conditions[phi_e] = -param.n.prim.U_init

######################
# Electrolyte concentration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BasicDFNComposite(BaseModel):
**Extends:** :class:`pybamm.lithium_ion.BaseModel`
"""

def __init__(self, name="Doyle-Fuller-Newman model"):
def __init__(self, name="Composite graphite/silicon Doyle-Fuller-Newman model"):
options = {"particle phases": ("2", "1")}
super().__init__(options, name)
pybamm.citations.register("Ai2022")
Expand All @@ -44,27 +44,39 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
Q = pybamm.Variable("Discharge capacity [A.h]")
# Variables that vary spatially are created with a domain
c_e_n = pybamm.Variable(
"Negative electrolyte concentration [mol.m-3]", domain="negative electrode"
"Negative electrolyte concentration [mol.m-3]",
domain="negative electrode",
scale=param.c_e_typ,
)
c_e_s = pybamm.Variable(
"Separator electrolyte concentration [mol.m-3]", domain="separator"
"Separator electrolyte concentration [mol.m-3]",
domain="separator",
scale=param.c_e_typ,
)
c_e_p = pybamm.Variable(
"Positive electrolyte concentration [mol.m-3]", domain="positive electrode"
"Positive electrolyte concentration [mol.m-3]",
domain="positive electrode",
scale=param.c_e_typ,
)
# Concatenations combine several variables into a single variable, to simplify
# implementing equations that hold over several domains
c_e = pybamm.concatenation(c_e_n, c_e_s, c_e_p)

# Electrolyte potential
phi_e_n = pybamm.Variable(
"Negative electrolyte potential [V]", domain="negative electrode"
"Negative electrolyte potential [V]",
domain="negative electrode",
reference=-param.n.prim.U_init,
)
phi_e_s = pybamm.Variable(
"Separator electrolyte potential [V]", domain="separator"
"Separator electrolyte potential [V]",
domain="separator",
reference=-param.n.prim.U_init,
)
phi_e_p = pybamm.Variable(
"Positive electrolyte potential [V]", domain="positive electrode"
"Positive electrolyte potential [V]",
domain="positive electrode",
reference=-param.n.prim.U_init,
)
phi_e = pybamm.concatenation(phi_e_n, phi_e_s, phi_e_p)

Expand All @@ -73,7 +85,9 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
"Negative electrode potential [V]", domain="negative electrode"
)
phi_s_p = pybamm.Variable(
"Positive electrode potential [V]", domain="positive electrode"
"Positive electrode potential [V]",
domain="positive electrode",
reference=param.ocv_init,
)
# Particle concentrations are variables on the particle domain, but also vary in
# the x-direction (electrode domain) and so must be provided with auxiliary
Expand All @@ -82,16 +96,19 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
"Negative primary particle concentration [mol.m-3]",
domain="negative primary particle",
auxiliary_domains={"secondary": "negative electrode"},
scale=param.n.prim.c_max,
)
c_s_n_p2 = pybamm.Variable(
"Negative secondary particle concentration [mol.m-3]",
domain="negative secondary particle",
auxiliary_domains={"secondary": "negative electrode"},
scale=param.n.sec.c_max,
)
c_s_p = pybamm.Variable(
"Positive particle concentration [mol.m-3]",
domain="positive particle",
auxiliary_domains={"secondary": "positive electrode"},
scale=param.p.prim.c_max,
)

# Constant temperature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ def get_coupled_variables(self, variables):
]
for phase in phases
)
variables[
f"{Domain} electrode volumetric interfacial current density [A.m-3]"
] = sumvar
variables.update(
{
f"{Domain} electrode volumetric "
"interfacial current density [A.m-3]": sumvar,
f"X-averaged {domain} electrode volumetric "
"interfacial current density [A.m-3]": pybamm.x_average(sumvar),
}
)

return variables
Loading

0 comments on commit 7ff854c

Please sign in to comment.