def plot_errors(models_times_and_voltages): npts = 20 linestyles = ["k-", "g--", "r:", "b-."] Crates = defaultdict(list) voltage_errors = defaultdict(list) fig, ax = plt.subplots(1, 1) for i, (model, times_and_voltages) in enumerate( models_times_and_voltages.items()): if model != "Full": for Crate, variables in times_and_voltages[npts].items(): Crates[model].append(Crate) full_voltage = models_times_and_voltages["Full"][npts][Crate][ "Battery voltage [V]"] reduced_voltage = variables["Battery voltage [V]"] voltage_errors[model].append( pybamm.rmse(full_voltage, reduced_voltage)) ax.semilogx(Crates[model], voltage_errors[model], linestyles[i], label=model) ax.set_xlabel("C-rate") ax.set_ylabel("RMSE [V]") ax.legend(loc="best") fig.tight_layout() file_name = "discharge_asymptotics_rmse.eps" if OUTPUT_DIR is not None: plt.savefig(OUTPUT_DIR + file_name, format="eps", dpi=1000)
def test_rmse(self): self.assertEqual(pybamm.rmse(np.ones(5), np.zeros(5)), 1) self.assertEqual(pybamm.rmse(2 * np.ones(5), np.zeros(5)), 2) self.assertEqual(pybamm.rmse(2 * np.ones(5), np.ones(5)), 1) x = np.array([1, 2, 3, 4, 5]) self.assertEqual(pybamm.rmse(x, x), 0) with self.assertRaisesRegex(ValueError, "same length"): pybamm.rmse(np.ones(5), np.zeros(3))
var.x_p: Npts[i], var.r_n: Npts[i], var.r_p: Npts[i], } meshes[i] = pybamm.Mesh(geometry, models[-1].default_submesh_types, var_pts) disc = pybamm.Discretisation(meshes[i], model.default_spatial_methods) disc.process_model(model) # solve model and plot voltage solutions = [None] * len(models) voltages = [None] * len(models) voltage_rmse = [None] * len(models) t_eval = np.linspace(0, 3600, 100) for i, model in enumerate(models): solutions[i] = pybamm.CasadiSolver(mode="fast").solve(model, t_eval) voltages[i] = solutions[i]["Terminal voltage [V]"](solutions[i].t) voltage_rmse[i] = pybamm.rmse(voltages[0], voltages[i]) plt.plot(solutions[i].t, voltages[i], label=model.name) for i, npts in enumerate(Npts): print( "npts = {}, solve time = {} s, Voltage RMSE = {}".format( npts, solutions[i].solve_time, voltage_rmse[i] ) ) plt.xlabel(r"$t$") plt.ylabel("Voltage [V]") plt.legend() plt.show()
# Compute RMSE at each C-rate for i, C_rate in enumerate(C_rates): # The SPM of Richardson et. al. is just the OCV spm_voltage = solutions["SP"][i]["Measured open circuit voltage [V]"]( solutions["SP"][i].t) spme_LD_voltage = solutions["SPMe (Linear)"][i]["Terminal voltage [V]"]( solutions["SPMe (Linear)"][i].t) spme_ND_voltage = solutions["SPMe (Nonlinear)"][i]["Terminal voltage [V]"]( solutions["SPMe (Nonlinear)"][i].t) csp_voltage = solutions["cSP"][i]["Terminal voltage [V]"]( solutions["cSP"][i].t) dfn_voltage = solutions["DFN"][i]["Terminal voltage [V]"]( solutions["DFN"][i].t) spm_errors[i] = pybamm.rmse(dfn_voltage, spm_voltage) * 1e3 spme_LD_errors[i] = pybamm.rmse(dfn_voltage, spme_LD_voltage) * 1e3 spme_ND_errors[i] = pybamm.rmse(dfn_voltage, spme_ND_voltage) * 1e3 csp_errors[i] = pybamm.rmse(dfn_voltage, csp_voltage) * 1e3 spm_solve_times[i] = round(solutions["SP"][i].solve_time * 1000) spme_LD_solve_times[i] = round(solutions["SPMe (Linear)"][i].solve_time * 1000) spme_ND_solve_times[i] = round( solutions["SPMe (Nonlinear)"][i].solve_time * 1000) csp_solve_times[i] = round(solutions["cSP"][i].solve_time * 1000) # print error table -- could be prettier... print("RMSE(mV) at 1C, 2.5C, 5C and 7.5C") print("SP", spm_errors) print("SPMe (Linear)", spme_LD_errors)