From 17c8d36d95f99730c1cdacf8095a3abc4bbb5454 Mon Sep 17 00:00:00 2001 From: GUANGYUAN MA <57339960+1836005678@users.noreply.github.com> Date: Tue, 7 Mar 2023 22:27:09 +0800 Subject: [PATCH 1/6] Update NCA_Kim2011.py Fixing #2744. to make sure we hit the voltage cut-off, instead of the particle stoichiometry going above 1. Using the function in Kim2011 article to take the place of pybamm\input\parameters\lithium_ion\data\nca_ocp_Kim2011_data.csv for parameter 'Positive electrode OCP[V]' in Kim2011 parameter set. The function 'nca_ocp_Kim2011(sto)' in pybamm\input\parameters\lithium_ion\NCA_Kim2011.py is edited. --- .../parameters/lithium_ion/NCA_Kim2011.py | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py index 3f0fdb703c..a2882e324a 100644 --- a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py +++ b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py @@ -260,16 +260,35 @@ def electrolyte_conductivity_Kim2011(c_e, T): return sigma_e -# Load data in the appropriate format -path, _ = os.path.split(os.path.abspath(__file__)) -nca_ocp_Kim2011_data = pybamm.parameters.process_1D_data( - "nca_ocp_Kim2011_data.csv", path=path -) - - def nca_ocp_Kim2011(sto): - name, (x, y) = nca_ocp_Kim2011_data - return pybamm.Interpolant(x, y, sto, name=name, interpolator="linear") + + """ + References + ---------- + Graphite Open Circuit Potential (OCP) as a function of the stochiometry [1]. + ---------- + .. [1] Kim, G. H., Smith, K., Lee, K. J., Santhanagopalan, S., & Pesaran, A. + (2011). Multi-domain modeling of lithium-ion batteries encompassing + multi-physics in varied length scales. Journal of The Electrochemical + Society, 158(8), A955-A969. + """ + + U_posi = ( + 1.638 * sto ** 10 + - 2.222 * sto ** 9 + + 15.056 * sto ** 8 + - 23.488 * sto ** 7 + + 81.246 * sto ** 6 + - 344.566 * sto ** 5 + + 621.3475 * sto ** 4 + - 554.774 * sto ** 3 + + 264.427 * sto ** 2 + - 66.3691 * sto + + 11.8058 + - 0.61386 * pybamm.exp(5.8201*sto**136.4) + ) + + return U_posi # Call dict via a function to avoid errors when editing in place From 31357ff5142cd0059a5a9f2a5d557cedb2c3f5cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Mar 2023 14:29:54 +0000 Subject: [PATCH 2/6] style: pre-commit fixes --- .../parameters/lithium_ion/NCA_Kim2011.py | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py index a2882e324a..f47b4dc98d 100644 --- a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py +++ b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py @@ -261,7 +261,6 @@ def electrolyte_conductivity_Kim2011(c_e, T): def nca_ocp_Kim2011(sto): - """ References ---------- @@ -272,20 +271,20 @@ def nca_ocp_Kim2011(sto): multi-physics in varied length scales. Journal of The Electrochemical Society, 158(8), A955-A969. """ - + U_posi = ( - 1.638 * sto ** 10 - - 2.222 * sto ** 9 - + 15.056 * sto ** 8 - - 23.488 * sto ** 7 - + 81.246 * sto ** 6 - - 344.566 * sto ** 5 - + 621.3475 * sto ** 4 - - 554.774 * sto ** 3 - + 264.427 * sto ** 2 + 1.638 * sto**10 + - 2.222 * sto**9 + + 15.056 * sto**8 + - 23.488 * sto**7 + + 81.246 * sto**6 + - 344.566 * sto**5 + + 621.3475 * sto**4 + - 554.774 * sto**3 + + 264.427 * sto**2 - 66.3691 * sto + 11.8058 - - 0.61386 * pybamm.exp(5.8201*sto**136.4) + - 0.61386 * pybamm.exp(5.8201 * sto**136.4) ) return U_posi From 5272b4c1919e4734a78834f4292dec5eaa9fe6ed Mon Sep 17 00:00:00 2001 From: GUANGYUAN MA <57339960+1836005678@users.noreply.github.com> Date: Tue, 7 Mar 2023 22:54:19 +0800 Subject: [PATCH 3/6] Update NCA_Kim2011.py detele 'import os' as it is not used now. --- pybamm/input/parameters/lithium_ion/NCA_Kim2011.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py index f47b4dc98d..459ba7bec1 100644 --- a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py +++ b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py @@ -1,5 +1,4 @@ import pybamm -import os def graphite_diffusivity_Kim2011(sto, T): From 27905ac22c38062a568e651e21b2b270083e3ab1 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Sun, 19 Mar 2023 11:02:41 -0400 Subject: [PATCH 4/6] fix test --- tests/unit/test_solvers/test_casadi_solver.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit/test_solvers/test_casadi_solver.py b/tests/unit/test_solvers/test_casadi_solver.py index e8cc7c7838..30d15e33ac 100644 --- a/tests/unit/test_solvers/test_casadi_solver.py +++ b/tests/unit/test_solvers/test_casadi_solver.py @@ -491,27 +491,27 @@ def test_dae_solver_algebraic_model(self): solver.solve(model, t_eval) def test_interpolant_extrapolate(self): - model = pybamm.lithium_ion.DFN() - param = pybamm.ParameterValues("NCA_Kim2011") - experiment = pybamm.Experiment( - ["Charge at 1C until 4.2 V"], period="10 seconds" - ) + x = np.linspace(0, 2) + var = pybamm.Variable("var") + rhs = pybamm.FunctionParameter("func", {"var": var}) - param["Upper voltage cut-off [V]"] = 4.8 + model = pybamm.BaseModel() + model.rhs[var] = rhs + model.initial_conditions[var] = pybamm.Scalar(1) + + # Bug: we need to set the interpolant via parameter values for the extrapolation + # to be detected + def func(var): + return pybamm.Interpolant(x, x, var, interpolator="linear") + + parameter_values = pybamm.ParameterValues({"func": func}) + parameter_values.process_model(model) + + solver = pybamm.CasadiSolver() + t_eval = [0, 5] - sim = pybamm.Simulation( - model, - parameter_values=param, - experiment=experiment, - solver=pybamm.CasadiSolver( - mode="safe", - dt_max=0.001, - extrap_tol=1e-3, - extra_options_setup={"max_num_steps": 500}, - ), - ) with self.assertRaisesRegex(pybamm.SolverError, "interpolation bounds"): - sim.solve() + solver.solve(model, t_eval) def test_casadi_safe_no_termination(self): model = pybamm.BaseModel() From ae2b91e1d552390611b1e84e930f4e2a5061fb5b Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Sun, 19 Mar 2023 11:23:01 -0400 Subject: [PATCH 5/6] fix formatting --- pybamm/input/parameters/lithium_ion/NCA_Kim2011.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py index 915bfe09dc..5da3a37620 100644 --- a/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py +++ b/pybamm/input/parameters/lithium_ion/NCA_Kim2011.py @@ -261,9 +261,9 @@ def electrolyte_conductivity_Kim2011(c_e, T): def nca_ocp_Kim2011(sto): """ - References - ---------- Graphite Open Circuit Potential (OCP) as a function of the stochiometry [1]. + + References ---------- .. [1] Kim, G. H., Smith, K., Lee, K. J., Santhanagopalan, S., & Pesaran, A. (2011). Multi-domain modeling of lithium-ion batteries encompassing From d773b8d5363fb4dc87c2284986f434b6396522c4 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Sun, 19 Mar 2023 14:54:23 -0400 Subject: [PATCH 6/6] coverage --- .../test_simulation_with_experiment.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/unit/test_experiments/test_simulation_with_experiment.py b/tests/unit/test_experiments/test_simulation_with_experiment.py index ba14639e10..c82b09548b 100644 --- a/tests/unit/test_experiments/test_simulation_with_experiment.py +++ b/tests/unit/test_experiments/test_simulation_with_experiment.py @@ -546,6 +546,23 @@ def test_all_empty_solution_errors(self): with self.assertRaisesRegex(pybamm.SolverError, "All steps in the cycle"): sim.solve() + def test_solver_error(self): + model = pybamm.lithium_ion.DFN() # load model + parameter_values = pybamm.ParameterValues("Chen2020") + experiment = pybamm.Experiment( + ["Discharge at 10C for 6 minutes or until 2.5 V"] + ) + + sim = pybamm.Simulation( + model, + parameter_values=parameter_values, + experiment=experiment, + solver=pybamm.CasadiSolver(mode="fast"), + ) + + with self.assertRaisesRegex(pybamm.SolverError, "IDA_CONV_FAIL"): + sim.solve() + def test_run_experiment_half_cell(self): experiment = pybamm.Experiment( [("Discharge at C/20 until 3.5V", "Charge at 1C until 3.8 V")]