-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functional parameter example (#442)
* Create functional_parameters.py * Define parameter set first * Update CHANGELOG.md * Apply suggestions from code review Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
- Loading branch information
1 parent
d414460
commit 1a76470
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import numpy as np | ||
import pybamm | ||
|
||
import pybop | ||
|
||
# This example demonstrates how to use a pybamm.FunctionalParameter to | ||
# optimise functional parameters using PyBOP. | ||
|
||
# Method: Define a new scalar parameter for use in a functional parameter | ||
# that already exists in the model, for example an exchange current density. | ||
|
||
|
||
# Load parameter set | ||
parameter_set = pybop.ParameterSet.pybamm("Chen2020") | ||
|
||
|
||
# Define a new function using pybamm parameters | ||
def positive_electrode_exchange_current_density(c_e, c_s_surf, c_s_max, T): | ||
# New parameters | ||
j0_ref = pybamm.Parameter( | ||
"Positive electrode reference exchange-current density [A.m-2]" | ||
) | ||
alpha = pybamm.Parameter("Positive electrode charge transfer coefficient") | ||
|
||
# Existing parameters | ||
c_e_init = pybamm.Parameter("Initial concentration in electrolyte [mol.m-3]") | ||
|
||
return ( | ||
j0_ref | ||
* ((c_e / c_e_init) * (c_s_surf / c_s_max) * (1 - c_s_surf / c_s_max)) ** alpha | ||
) | ||
|
||
|
||
# Give default values to the new scalar parameters and pass the new function | ||
parameter_set.update( | ||
{ | ||
"Positive electrode reference exchange-current density [A.m-2]": 1, | ||
"Positive electrode charge transfer coefficient": 0.5, | ||
}, | ||
check_already_exists=False, | ||
) | ||
parameter_set["Positive electrode exchange-current density [A.m-2]"] = ( | ||
positive_electrode_exchange_current_density | ||
) | ||
|
||
# Model definition | ||
model = pybop.lithium_ion.SPM( | ||
parameter_set=parameter_set, options={"contact resistance": "true"} | ||
) | ||
|
||
# Fitting parameters | ||
parameters = pybop.Parameters( | ||
pybop.Parameter( | ||
"Positive electrode reference exchange-current density [A.m-2]", | ||
prior=pybop.Gaussian(1, 0.1), | ||
), | ||
pybop.Parameter( | ||
"Positive electrode charge transfer coefficient", | ||
prior=pybop.Gaussian(0.5, 0.1), | ||
), | ||
) | ||
|
||
# Generate data | ||
sigma = 0.001 | ||
t_eval = np.arange(0, 900, 3) | ||
values = model.predict(t_eval=t_eval) | ||
corrupt_values = values["Voltage [V]"].data + np.random.normal(0, sigma, len(t_eval)) | ||
|
||
# Form dataset | ||
dataset = pybop.Dataset( | ||
{ | ||
"Time [s]": t_eval, | ||
"Current function [A]": values["Current [A]"].data, | ||
"Voltage [V]": corrupt_values, | ||
} | ||
) | ||
|
||
# Generate problem, cost function, and optimisation class | ||
problem = pybop.FittingProblem(model, parameters, dataset) | ||
cost = pybop.RootMeanSquaredError(problem) | ||
optim = pybop.SciPyMinimize(cost, max_iterations=125) | ||
|
||
# Run optimisation | ||
x, final_cost = optim.run() | ||
print("Estimated parameters:", x) | ||
|
||
# Plot the timeseries output | ||
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison") | ||
|
||
# Plot convergence | ||
pybop.plot_convergence(optim) | ||
|
||
# Plot the parameter traces | ||
pybop.plot_parameters(optim) | ||
|
||
# Plot the cost landscape with optimisation path | ||
pybop.plot2d(optim, steps=15) |