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

Add functional parameter example #442

Merged
merged 8 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -2,6 +2,7 @@

## Features

- [#441](https://github.com/pybop-team/PyBOP/issues/441) - Adds an example for estimating constants within a `pybamm.FunctionalParameter`.
- [#405](https://github.com/pybop-team/PyBOP/pull/405) - Adds frequency-domain based EIS prediction methods via `model.simulateEIS` and updates to `problem.evaluate` with examples and tests.
- [#460](https://github.com/pybop-team/PyBOP/pull/460) - Notebook example files added for ECM and folder structure updated.
- [#450](https://github.com/pybop-team/PyBOP/pull/450) - Adds support for IDAKLU with output variables, and corresponding examples, tests.
Expand Down
97 changes: 97 additions & 0 deletions examples/scripts/functional_parameters.py
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 constant parameter for use in a functional parameter
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
# that already exists in the model, for example an exchange current density.


# Load default parameter set
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
parameter_set = pybop.ParameterSet.pybamm("Chen2020")


# Define a new function using new parameters
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
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 constant parameters and pass the new function
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading