Skip to content

Commit 5465052

Browse files
Merge pull request #2779 from pybamm-team/electrode-soh-solve-output
make ElectrodeSOH.solve output {str: float} dict instead of Solution}
2 parents f45829b + 54d1771 commit 5465052

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
## Breaking changes
2020

21+
- `ElectrodeSOH.solve` now returns a `{str: float}` dict instead of a `pybamm.Solution` object (to avoid having to do `.data[0]` every time). In any code that uses `sol = ElectrodeSOH.solve()`, `sol[key].data[0]` should be replaced with `sol[key]`. ([#2779](https://github.com/pybamm-team/PyBaMM/pull/2779))
2122
- Removed "... cation signed stoichiometry" and "... electrons in reaction" parameters, they are now hardcoded. ([#2778](https://github.com/pybamm-team/PyBaMM/pull/2778))
2223
- When using `solver.step()`, the first time point in the step is shifted by `pybamm.settings.step_start_offset` (default 1 ns) to avoid having duplicate times in the solution steps from the end of one step and the start of the next. ([#2773](https://github.com/pybamm-team/PyBaMM/pull/2773))
2324
- Renamed "Measured open circuit voltage [V]" to "Surface open-circuit voltage [V]". This variable was calculated from surface particle concentrations, and hence "hid" the overpotential from particle gradients. The new variable "Open-circuit voltage [V]" is calculated from bulk particle concentrations instead. ([#2740](https://github.com/pybamm-team/PyBaMM/pull/2740))

examples/notebooks/models/electrode-state-of-health.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
"esoh_sol = esoh_solver.solve(inputs)\n",
256256
"\n",
257257
"for var in [\"x_100\", \"y_100\", \"Q\", \"x_0\", \"y_0\"]:\n",
258-
" print(var, \":\", esoh_sol[var].data[0])"
258+
" print(var, \":\", esoh_sol[var])"
259259
]
260260
},
261261
{
@@ -373,7 +373,7 @@
373373
" try:\n",
374374
" sol = esoh_solver.solve(inputs)\n",
375375
" for var in variables:\n",
376-
" sweep[var].append(sol[var].data[0])\n",
376+
" sweep[var].append(sol[var])\n",
377377
" except (ValueError, pybamm.SolverError):\n",
378378
" pass\n",
379379
"\n",
@@ -417,13 +417,13 @@
417417
" else:\n",
418418
" label1 = label2 = label3 = None\n",
419419
" ax.plot(sweep[\"Q_Li\"], sweep[k],\"b-\", label=label1)\n",
420-
" ax.axhline(sol_init_QLi[k].data[0],c=\"k\",linestyle=\"--\", label=label2)\n",
421-
" ax.axhline(sol_init_Q[k].data[0],c=\"r\",linestyle=\"--\", label=label3)\n",
420+
" ax.axhline(sol_init_QLi[k],c=\"k\",linestyle=\"--\", label=label2)\n",
421+
" ax.axhline(sol_init_Q[k],c=\"r\",linestyle=\"--\", label=label3)\n",
422422
" ax.set_xlabel(\"Cyclable lithium [A.h]\")\n",
423423
" ax.set_ylabel(ks[0][0])\n",
424424
" ax.set_xlim([np.min(sweep[\"Q_Li\"]),np.max(sweep[\"Q_Li\"])])\n",
425-
" ax.axvline(sol_init_QLi[\"Q_Li\"].data[0],c=\"k\",linestyle=\"--\")\n",
426-
" ax.axvline(sol_init_Q[\"Q_Li\"].data[0],c=\"r\",linestyle=\"--\")\n",
425+
" ax.axvline(sol_init_QLi[\"Q_Li\"],c=\"k\",linestyle=\"--\")\n",
426+
" ax.axvline(sol_init_Q[\"Q_Li\"],c=\"r\",linestyle=\"--\")\n",
427427
" # Plot capacities of electrodes\n",
428428
" # ax.axvline(Qn,c=\"b\",linestyle=\"--\")\n",
429429
" # ax.axvline(Qp,c=\"r\",linestyle=\"--\")\n",

pybamm/models/full_battery_models/lithium_ion/electrode_soh.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def solve(self, inputs):
243243
# if that didn't raise an error, raise the original error instead
244244
raise split_error
245245

246-
return sol
246+
sol_dict = {key: sol[key].data[0] for key in sol.all_models[0].variables.keys()}
247+
return sol_dict
247248

248249
def _set_up_solve(self, inputs):
249250
# Try with full sim
@@ -501,7 +502,7 @@ def get_min_max_stoichiometries(self):
501502
inputs = {"Q_n": Q_n, "Q_p": Q_p, "Q": Q}
502503
# Solve the model and check outputs
503504
sol = self.solve(inputs)
504-
return [sol[var].data[0] for var in ["x_0", "x_100", "y_100", "y_0"]]
505+
return [sol["x_0"], sol["x_100"], sol["y_100"], sol["y_0"]]
505506

506507

507508
def get_initial_stoichiometries(

pybamm/solvers/solution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,6 @@ def _get_cycle_summary_variables(cycle_solution, esoh_solver):
877877
"`sim.solve(calc_esoh=False)` to skip this step"
878878
)
879879

880-
for var in esoh_sol.all_models[0].variables:
881-
cycle_summary_variables[var] = esoh_sol[var].data[0]
880+
cycle_summary_variables.update(esoh_sol)
882881

883882
return cycle_summary_variables

tests/unit/test_models/test_full_battery_models/test_lithium_ion/test_electrode_soh.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ def test_known_solution(self):
2323
# Solve the model and check outputs
2424
sol = esoh_solver.solve(inputs)
2525

26-
self.assertAlmostEqual(sol["Up(y_100) - Un(x_100)"].data[0], Vmax, places=5)
27-
self.assertAlmostEqual(sol["Up(y_0) - Un(x_0)"].data[0], Vmin, places=5)
28-
self.assertAlmostEqual(sol["Q_Li"].data[0], Q_Li, places=5)
26+
self.assertAlmostEqual(sol["Up(y_100) - Un(x_100)"], Vmax, places=5)
27+
self.assertAlmostEqual(sol["Up(y_0) - Un(x_0)"], Vmin, places=5)
28+
self.assertAlmostEqual(sol["Q_Li"], Q_Li, places=5)
2929

3030
# Solve with split esoh and check outputs
3131
ics = esoh_solver._set_up_solve(inputs)
3232
sol_split = esoh_solver._solve_split(inputs, ics)
33-
for key in sol.all_models[0].variables:
34-
self.assertAlmostEqual(sol[key].data[0], sol_split[key].data[0], places=5)
33+
for key in sol:
34+
self.assertAlmostEqual(sol[key], sol_split[key].data[0], places=5)
3535

3636
# should still work with old inputs
3737
n_Li = parameter_values.evaluate(param.n_Li_particles_init)
3838
inputs = {"V_min": 3, "V_max": 4.2, "n_Li": n_Li, "C_n": Q_n, "C_p": Q_p}
3939

4040
# Solve the model and check outputs
4141
sol = esoh_solver.solve(inputs)
42-
self.assertAlmostEqual(sol["Q_Li"].data[0], Q_Li, places=5)
42+
self.assertAlmostEqual(sol["Q_Li"], Q_Li, places=5)
4343

4444
def test_known_solution_cell_capacity(self):
4545
param = pybamm.LithiumIonParameters()
@@ -60,9 +60,9 @@ def test_known_solution_cell_capacity(self):
6060
# Solve the model and check outputs
6161
sol = esoh_solver.solve(inputs)
6262

63-
self.assertAlmostEqual(sol["Up(y_100) - Un(x_100)"].data[0], Vmax, places=5)
64-
self.assertAlmostEqual(sol["Up(y_0) - Un(x_0)"].data[0], Vmin, places=5)
65-
self.assertAlmostEqual(sol["Q"].data[0], Q, places=5)
63+
self.assertAlmostEqual(sol["Up(y_100) - Un(x_100)"], Vmax, places=5)
64+
self.assertAlmostEqual(sol["Up(y_0) - Un(x_0)"], Vmin, places=5)
65+
self.assertAlmostEqual(sol["Q"], Q, places=5)
6666

6767
def test_error(self):
6868
param = pybamm.LithiumIonParameters()

0 commit comments

Comments
 (0)