Skip to content

Commit

Permalink
#2418 integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 30, 2022
1 parent 352b6d1 commit dc3a9c2
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 73 deletions.
6 changes: 3 additions & 3 deletions examples/scripts/compare_lithium_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
pybamm.set_logging_level("INFO")
# load models
models = [
pybamm.lithium_ion.SPM(),
pybamm.lithium_ion.SPMe(),
pybamm.lithium_ion.DFN(),
# pybamm.lithium_ion.SPM(),
# pybamm.lithium_ion.SPMe(),
pybamm.lithium_ion.DFN({"dimensionality": 1}),
# pybamm.lithium_ion.DFN(
# {"particle": "uniform profile"}
# ),
Expand Down
43 changes: 0 additions & 43 deletions pybamm/models/standard_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@

class StandardVariables:
def __init__(self):
# Discharge capacity and energy
self.Q_Ah = pybamm.Variable("Discharge capacity [A.h]")
self.Q_Wh = pybamm.Variable("Discharge energy [W.h]")

# Throughput capacity and energy (cumulative)
self.Qt_Ah = pybamm.Variable("Throughput capacity [A.h]")
self.Qt_Wh = pybamm.Variable("Throughput energy [W.h]")

# Electrode potential
self.phi_s_n = pybamm.Variable(
"Negative electrode potential [V]",
domain="negative electrode",
auxiliary_domains={"secondary": "current collector"},
)
self.phi_s_p = pybamm.Variable(
"Positive electrode potential [V]",
domain="positive electrode",
auxiliary_domains={"secondary": "current collector"},
)

# Potential difference
self.delta_phi_n = pybamm.Variable(
"Negative electrode surface potential difference [V]",
Expand All @@ -47,29 +27,6 @@ def __init__(self):
domain="current collector",
)

# current collector variables
self.phi_s_cn = pybamm.Variable(
"Negative current collector potential [V]", domain="current collector"
)
self.phi_s_cp = pybamm.Variable(
"Positive current collector potential [V]", domain="current collector"
)
self.i_boundary_cc = pybamm.Variable(
"Current collector current density [A.m-2]", domain="current collector"
)
self.phi_s_cn_composite = pybamm.Variable(
"Composite negative current collector potential [V]",
domain="current collector",
)
self.phi_s_cp_composite = pybamm.Variable(
"Composite positive current collector potential [V]",
domain="current collector",
)
self.i_boundary_cc_composite = pybamm.Variable(
"Composite current collector current density [A.m-2]",
domain="current collector",
)

def __setattr__(self, name, value):
value.print_name = name
super().__setattr__(name, value)
Expand Down
12 changes: 9 additions & 3 deletions pybamm/models/submodels/current_collector/potential_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ def __init__(self, param):
pybamm.citations.register("Timms2021")

def get_fundamental_variables(self):

phi_s_cn = pybamm.standard_variables.phi_s_cn
param = self.param
phi_s_cn = pybamm.Variable(
"Negative current collector potential [V]", domain="current collector"
)

variables = self._get_standard_negative_potential_variables(phi_s_cn)

# TODO: grad not implemented for 2D yet
i_cc = pybamm.Scalar(0)
i_boundary_cc = pybamm.standard_variables.i_boundary_cc
i_boundary_cc = pybamm.Variable(
"Current collector current density [A.m-2]",
domain="current collector",
scale=param.Q / (param.A_cc * param.n_electrodes_parallel),
)

variables.update(self._get_standard_current_variables(i_cc, i_boundary_cc))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ def __init__(self, param):

def get_fundamental_variables(self):

phi_s_cn = pybamm.standard_variables.phi_s_cn
phi_s_cn = pybamm.Variable(
"Negative current collector potential [V]", domain="current collector"
)

variables = self._get_standard_negative_potential_variables(phi_s_cn)

# TODO: grad not implemented for 2D yet
i_cc = pybamm.Scalar(0)
i_boundary_cc = pybamm.standard_variables.i_boundary_cc
i_boundary_cc = pybamm.Variable(
"Current collector current density [A.m-2]",
domain="current collector",
scale=param.Q / (param.A_cc * param.n_electrodes_parallel),
)

variables.update(self._get_standard_current_variables(i_cc, i_boundary_cc))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def __init__(self, param, options):
super().__init__(param, options=options)

def get_fundamental_variables(self):
Q_Ah = pybamm.standard_variables.Q_Ah
Q_Ah = pybamm.Variable("Discharge capacity [A.h]")

variables = {"Discharge capacity [A.h]": Q_Ah}
if self.options["calculate discharge energy"] == "true":
Q_Wh = pybamm.standard_variables.Q_Wh
Qt_Wh = pybamm.standard_variables.Qt_Wh
Qt_Ah = pybamm.standard_variables.Qt_Ah
Q_Wh = pybamm.Variable("Discharge energy [W.h]")

# Throughput capacity and energy (cumulative)
Qt_Ah = pybamm.Variable("Throughput capacity [A.h]")
Qt_Wh = pybamm.Variable("Throughput energy [W.h]")
variables.update(
{
"Discharge energy [W.h]": Q_Wh,
Expand Down
2 changes: 2 additions & 0 deletions pybamm/models/submodels/interface/lithium_plating/plating.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def get_fundamental_variables(self):
c_plated_Li_av = pybamm.Variable(
"X-averaged lithium plating concentration [mol.m-3]",
domain="current collector",
scale=self.param.c_plated_Li_0,
)
c_plated_Li = pybamm.PrimaryBroadcast(c_plated_Li_av, "negative electrode")
c_dead_Li_av = pybamm.Variable(
Expand All @@ -53,6 +54,7 @@ def get_fundamental_variables(self):
"Lithium plating concentration [mol.m-3]",
domain="negative electrode",
auxiliary_domains={"secondary": "current collector"},
scale=self.param.c_plated_Li_0,
)
c_dead_Li = pybamm.Variable(
"Dead lithium concentration [mol.m-3]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,38 @@ def compare_outputs_two_phase_graphite_graphite(self, model_class):
# Compare two phase model to standard model
for variable in [
"X-averaged negative electrode active material volume fraction",
"X-averaged negative electrode volumetric interfacial current density [A.m-3]",
"X-averaged negative electrode volumetric "
"interfacial current density [A.m-3]",
"Terminal voltage [V]",
]:
np.testing.assert_array_almost_equal(
sol[variable].entries, sol_two_phase[variable].entries, decimal=2
np.testing.assert_allclose(
sol[variable].entries, sol_two_phase[variable].entries, rtol=1e-2
)

# Compare each phase in the two-phase model
np.testing.assert_array_almost_equal(
np.testing.assert_allclose(
sol_two_phase[
"Negative primary particle concentration [mol.m-3]"
].entries,
sol_two_phase[
"Negative secondary particle concentration [mol.m-3]"
].entries,
decimal=6,
rtol=1e-6,
)
np.testing.assert_array_almost_equal(
np.testing.assert_allclose(
sol_two_phase[
"Negative electrode primary volumetric interfacial current density [A.m-3]"
"Negative electrode primary volumetric "
"interfacial current density [A.m-3]"
].entries
/ x,
sol_two_phase[
"Negative electrode secondary volumetric interfacial current density [A.m-3]"
"Negative electrode secondary volumetric "
"interfacial current density [A.m-3]"
].entries
/ (1 - x),
decimal=6,
rtol=1e-6,
)
np.testing.assert_array_almost_equal(
np.testing.assert_allclose(
sol_two_phase[
"Negative electrode primary active material volume fraction"
].entries
Expand All @@ -106,7 +109,7 @@ def compare_outputs_two_phase_graphite_graphite(self, model_class):
"Negative electrode secondary active material volume fraction"
].entries
/ (1 - x),
decimal=6,
rtol=1e-6,
)

def test_compare_SPM_graphite_graphite(self):
Expand Down Expand Up @@ -151,8 +154,8 @@ def compare_outputs_two_phase_silicon_graphite(self, model_class):
"Average negative primary particle concentration",
"Average negative secondary particle concentration",
]:
np.testing.assert_array_almost_equal(
sol1[var].data[:20], sol2[var].data[:20], decimal=2
np.testing.assert_allclose(
sol1[var].data[:20], sol2[var].data[:20], rtol=1e-2
)

# More silicon means longer sim
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_expression_tree/test_binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def test_sparse_divide(self):
def test_inner(self):
model = pybamm.lithium_ion.BaseModel()

phi_s = pybamm.standard_variables.phi_s_n
phi_s = pybamm.Variable(
"Negative electrode potential [V]", domain="negative electrode"
)
i = pybamm.grad(phi_s)

model.rhs = {phi_s: pybamm.inner(i, i)}
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_expression_tree/test_operations/test_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ def test_jac_of_binary_operator(self):
a = pybamm.Symbol("a")
b = pybamm.Symbol("b")

phi_s = pybamm.standard_variables.phi_s_n
phi_s = pybamm.Variable(
"Negative electrode potential [V]", domain="negative electrode"
)
i = pybamm.grad(phi_s)

inner = pybamm.inner(2, i)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_solvers/test_idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def test_set_atol(self):
disc.process_model(model)
solver = pybamm.IDAKLUSolver()

variable_tols = {"Porosity times concentration [mol.m-3]": 1e-3}
variable_tols = {"Negative electrode potential [V]": 1e-3}
solver.set_atol_by_variable(variable_tols, model)

model = pybamm.BaseModel()
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_solvers/test_scikits_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,12 @@ def nonsmooth_rate(t):
dindex = np.searchsorted(solution.t, discontinuity)
value_before = solution.t[dindex - 1]
value_after = solution.t[dindex]
self.assertEqual(value_before + sys.float_info.epsilon, discontinuity)
self.assertEqual(value_after - sys.float_info.epsilon, discontinuity)
self.assertEqual(
value_before / (1 - sys.float_info.epsilon), discontinuity
)
self.assertEqual(
value_after / (1 + sys.float_info.epsilon), discontinuity
)

# both solution time vectors should have same number of points
self.assertEqual(len(solution1.t), len(solution2.t))
Expand Down

0 comments on commit dc3a9c2

Please sign in to comment.