Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ElectrodeSOH.solve output {str: float} dict instead of Solution} #2779

Merged
merged 7 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

## Breaking changes

- `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))
- 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))
- Renamed all references to "open circuit" to be "open-circuit" instead. ([#2740](https://github.com/pybamm-team/PyBaMM/pull/2740))
- Renamed parameter "1 + dlnf/dlnc" to "Thermodynamic factor". ([#2727](https://github.com/pybamm-team/PyBaMM/pull/2727))
Expand Down
12 changes: 6 additions & 6 deletions examples/notebooks/models/electrode-state-of-health.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
"esoh_sol = esoh_solver.solve(inputs)\n",
"\n",
"for var in [\"x_100\", \"y_100\", \"Q\", \"x_0\", \"y_0\"]:\n",
" print(var, \":\", esoh_sol[var].data[0])"
" print(var, \":\", esoh_sol[var])"
]
},
{
Expand Down Expand Up @@ -373,7 +373,7 @@
" try:\n",
" sol = esoh_solver.solve(inputs)\n",
" for var in variables:\n",
" sweep[var].append(sol[var].data[0])\n",
" sweep[var].append(sol[var])\n",
" except (ValueError, pybamm.SolverError):\n",
" pass\n",
"\n",
Expand Down Expand Up @@ -417,13 +417,13 @@
" else:\n",
" label1 = label2 = label3 = None\n",
" ax.plot(sweep[\"Q_Li\"], sweep[k],\"b-\", label=label1)\n",
" ax.axhline(sol_init_QLi[k].data[0],c=\"k\",linestyle=\"--\", label=label2)\n",
" ax.axhline(sol_init_Q[k].data[0],c=\"r\",linestyle=\"--\", label=label3)\n",
" ax.axhline(sol_init_QLi[k],c=\"k\",linestyle=\"--\", label=label2)\n",
" ax.axhline(sol_init_Q[k],c=\"r\",linestyle=\"--\", label=label3)\n",
" ax.set_xlabel(\"Cyclable lithium [A.h]\")\n",
" ax.set_ylabel(ks[0][0])\n",
" ax.set_xlim([np.min(sweep[\"Q_Li\"]),np.max(sweep[\"Q_Li\"])])\n",
" ax.axvline(sol_init_QLi[\"Q_Li\"].data[0],c=\"k\",linestyle=\"--\")\n",
" ax.axvline(sol_init_Q[\"Q_Li\"].data[0],c=\"r\",linestyle=\"--\")\n",
" ax.axvline(sol_init_QLi[\"Q_Li\"],c=\"k\",linestyle=\"--\")\n",
" ax.axvline(sol_init_Q[\"Q_Li\"],c=\"r\",linestyle=\"--\")\n",
" # Plot capacities of electrodes\n",
" # ax.axvline(Qn,c=\"b\",linestyle=\"--\")\n",
" # ax.axvline(Qp,c=\"r\",linestyle=\"--\")\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ def solve(self, inputs):
# if that didn't raise an error, raise the original error instead
raise split_error

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

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


def get_initial_stoichiometries(
Expand Down
3 changes: 1 addition & 2 deletions pybamm/solvers/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ def _get_cycle_summary_variables(cycle_solution, esoh_solver):
"`sim.solve(calc_esoh=False)` to skip this step"
)

for var in esoh_sol.all_models[0].variables:
cycle_summary_variables[var] = esoh_sol[var].data[0]
cycle_summary_variables.update(esoh_sol)

return cycle_summary_variables
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ def test_known_solution(self):
# Solve the model and check outputs
sol = esoh_solver.solve(inputs)

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

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

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

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

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

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

def test_error(self):
param = pybamm.LithiumIonParameters()
Expand Down