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 MultiFittingProblem class and example #364

Merged
merged 150 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
2a779e1
Add MultiFittingProblem, example and test
NicolaCourtier Jun 14, 2024
48d196e
Remove unused n_problems property
NicolaCourtier Jun 14, 2024
ac8e11a
Merge branch '358-passing-inputs' into 238b-multi-fitting
NicolaCourtier Jul 4, 2024
0b0c0e0
Update multi_fitting.py
NicolaCourtier Jul 4, 2024
2d84260
Merge branch '358-passing-inputs' into 238b-multi-fitting
NicolaCourtier Jul 4, 2024
c614542
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 4, 2024
12ab515
Update CHANGELOG.md
NicolaCourtier Jul 4, 2024
2a2eb29
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 5, 2024
cd1c7f8
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 5, 2024
851255c
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 8, 2024
abbeb84
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 11, 2024
dd01edb
Remove unused weights
NicolaCourtier Jul 12, 2024
1724cc5
Update problem_list to problem args
NicolaCourtier Jul 12, 2024
84b611d
Concatenate the whole list
NicolaCourtier Jul 12, 2024
c5f58ce
Apply suggestions from code review
NicolaCourtier Jul 12, 2024
6654b1b
Update description
NicolaCourtier Jul 12, 2024
e51decc
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 23, 2024
b66a52a
Update default init_soc
NicolaCourtier Jul 24, 2024
157c211
Update CHANGELOG.md
NicolaCourtier Jul 24, 2024
5e60cee
Update check_params
NicolaCourtier Jul 24, 2024
60ae64e
Add pybamm_model as default attribute
NicolaCourtier Jul 24, 2024
c682ace
Ensure predict uses unprocessed_model
NicolaCourtier Jul 25, 2024
914f307
Move rebuild check to model.simulate
NicolaCourtier Jul 25, 2024
367ab12
Align simulate output with predict
NicolaCourtier Jul 25, 2024
4e92192
Replace init_soc with init_ocv for FittingProblem
NicolaCourtier Jul 25, 2024
f799e06
Update notebooks
NicolaCourtier Jul 25, 2024
3f61322
Update test_observers.py
NicolaCourtier Jul 26, 2024
46f31fc
Update descriptions and simplify
NicolaCourtier Jul 26, 2024
28d56fb
Add test_set_initial_state
NicolaCourtier Jul 26, 2024
026fbbd
Copy each model into MultiFittingProblem
NicolaCourtier Jul 26, 2024
cba5c36
Update test_problem.py
NicolaCourtier Jul 26, 2024
781f889
Update ecm.py
NicolaCourtier Jul 26, 2024
61a6fc7
style: pre-commit fixes
pre-commit-ci[bot] Jul 26, 2024
6e6cb70
Break connection between parameter_sets
NicolaCourtier Jul 26, 2024
d17c728
Allow predict to update initial state
NicolaCourtier Jul 26, 2024
aeba8c4
Fix typo
NicolaCourtier Jul 26, 2024
5e65542
Add nbstripout pre-commit hook
NicolaCourtier Jul 28, 2024
c54ae5b
Add -q and re-run all notebooks
NicolaCourtier Jul 28, 2024
4cba081
Copy parameter sets and remove model.initial_state
NicolaCourtier Jul 28, 2024
01115df
Reset spm_NelderMead.py
NicolaCourtier Jul 28, 2024
08df450
Update CHANGELOG.md
NicolaCourtier Jul 28, 2024
0039d8a
Update CHANGELOG.md
NicolaCourtier Jul 29, 2024
e655ed7
Allow parameter_set is None
NicolaCourtier Jul 29, 2024
b2f389b
Merge branch '427-pre-commit-nbstripout' into 424-fitting-ocv
NicolaCourtier Jul 29, 2024
9a7282c
Re-run notebooks
NicolaCourtier Jul 29, 2024
6223251
Update bounds
NicolaCourtier Jul 30, 2024
a71350e
Update notebooks
NicolaCourtier Jul 30, 2024
37be820
Update notebooks
NicolaCourtier Jul 30, 2024
8591ba7
Set numpy random seed in notebooks
NicolaCourtier Jul 30, 2024
d2c7e68
Re-run with fixed seed
NicolaCourtier Jul 30, 2024
e2410cc
Merge branch '427-pre-commit-nbstripout' into 424-fitting-ocv
NicolaCourtier Jul 30, 2024
9b8d27a
Update bounds
NicolaCourtier Jul 30, 2024
2f73168
Update notebooks to initial_state
NicolaCourtier Jul 30, 2024
80df8fe
Add set_initial_state for ECMs
NicolaCourtier Jul 30, 2024
eda8096
Add init_ocv setter
NicolaCourtier Jul 30, 2024
285777b
Add init_ocv values
NicolaCourtier Jul 30, 2024
aebec3c
Re-run notebooks
NicolaCourtier Jul 30, 2024
8df8618
Add tests for ECM get_initial_state
NicolaCourtier Jul 30, 2024
6f05cda
Add ECM initial state error tests
NicolaCourtier Jul 30, 2024
d18b28f
Remove unused store_optimised_parameters
NicolaCourtier Jul 31, 2024
c0d0a24
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Jul 31, 2024
5d4f2ea
Update parameters.initial_value
NicolaCourtier Jul 31, 2024
eac17cc
Use any Initial SoC from parameter_set
NicolaCourtier Jul 31, 2024
24e40a0
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Jul 31, 2024
b9650c3
Update bounds again
NicolaCourtier Jul 31, 2024
7a7d58a
Update init_soc in notebooks
NicolaCourtier Jul 31, 2024
d993632
Move dataset check within unscented_kalman
NicolaCourtier Jul 31, 2024
5059f71
Remove unnecessary lines from spm_UKF
NicolaCourtier Jul 31, 2024
9bc0c0e
Update all parameters for rebuild
NicolaCourtier Jul 31, 2024
daf3f05
Update init_ocv to _init_ocv
NicolaCourtier Jul 31, 2024
b285885
Ensure value updates alongside initial_value
NicolaCourtier Jul 31, 2024
48788af
Update multi_model_identification
NicolaCourtier Jul 31, 2024
7798af5
Merge branch 'develop' into 421-design_init_soc
NicolaCourtier Jul 31, 2024
07644d4
Update spm_electrode_design.ipynb
NicolaCourtier Jul 31, 2024
799329e
Update spm_electrode_design.ipynb
NicolaCourtier Jul 31, 2024
c7ee29b
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Jul 31, 2024
61d326f
Fix identation
NicolaCourtier Jul 31, 2024
5be532c
Fix test_plots design problem
NicolaCourtier Jul 31, 2024
29e7a67
Move Changelog entry to breaking changes
NicolaCourtier Aug 1, 2024
f999b96
Move Changelog entry
NicolaCourtier Aug 1, 2024
e4f8ce6
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Aug 1, 2024
d31ee54
Merge branch 'develop' into 421-design_init_soc
NicolaCourtier Aug 1, 2024
098e3e2
style: pre-commit fixes
pre-commit-ci[bot] Aug 1, 2024
86f1d17
Fix merge mistake
NicolaCourtier Aug 1, 2024
beac8bb
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Aug 1, 2024
d205dc9
style: pre-commit fixes
pre-commit-ci[bot] Aug 1, 2024
d1fbd8d
Allow kwargs in MultiFitting evaluate
NicolaCourtier Aug 1, 2024
eef8acf
Add tests
NicolaCourtier Aug 1, 2024
5f7761f
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Aug 1, 2024
cdd4d1c
Update integration tests
NicolaCourtier Aug 1, 2024
b6c8824
Update spm_weighted_cost.py
NicolaCourtier Aug 1, 2024
ebc5114
Fix tests
NicolaCourtier Aug 2, 2024
d4421fe
style: pre-commit fixes
pre-commit-ci[bot] Aug 2, 2024
043ae52
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 2, 2024
3bba9c7
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 5, 2024
ede6363
Fix model type check
NicolaCourtier Aug 5, 2024
ab68d15
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Aug 5, 2024
fe1397e
Update _parameter_set to parameter_set
NicolaCourtier Aug 5, 2024
3cb4c4d
style: pre-commit fixes
pre-commit-ci[bot] Aug 5, 2024
e3b1466
Update tests with parameter set
NicolaCourtier Aug 5, 2024
cbda2ef
Add model build description
NicolaCourtier Aug 5, 2024
b19035d
Revert to _parameter_set
NicolaCourtier Aug 5, 2024
4d4857b
Fix predict without pybamm test
NicolaCourtier Aug 5, 2024
eba9def
Apply suggestions from code review
NicolaCourtier Aug 5, 2024
892eb77
Apply suggestions from code review
NicolaCourtier Aug 5, 2024
bf01b7e
Fix syntax
NicolaCourtier Aug 5, 2024
71a95cf
Fix variable name
NicolaCourtier Aug 5, 2024
4484951
Update model type check
NicolaCourtier Aug 5, 2024
08cdc4c
Update parameter_set setter
NicolaCourtier Aug 5, 2024
cd87e83
style: pre-commit fixes
pre-commit-ci[bot] Aug 5, 2024
14e4223
Add parameters.reset_initial_value
NicolaCourtier Aug 5, 2024
8a19542
Add n_outputs property
NicolaCourtier Aug 6, 2024
a67c564
style: pre-commit fixes
pre-commit-ci[bot] Aug 6, 2024
5450f21
Remove public parameter_set setter
NicolaCourtier Aug 6, 2024
676e7ed
Correct integer to float
NicolaCourtier Aug 6, 2024
80ef44e
Convert initial_state to dict
NicolaCourtier Aug 6, 2024
8e67d89
Add guidance
NicolaCourtier Aug 6, 2024
d5f63d0
Remove empty dictionary defaults
NicolaCourtier Aug 6, 2024
d0d1bd2
style: pre-commit fixes
pre-commit-ci[bot] Aug 6, 2024
5c52712
Add warning stacklevels
NicolaCourtier Aug 6, 2024
dacafc3
Catch simulation errors in problem evaluation
NicolaCourtier Aug 6, 2024
bcc7acf
Add pybamm version comment
NicolaCourtier Aug 6, 2024
8607a6b
Add set initial ocv check
NicolaCourtier Aug 6, 2024
605f509
Add model.clear and remove setters
NicolaCourtier Aug 7, 2024
a5be6ee
Merge branch '445-remove-setters' into 424-fitting-ocv
NicolaCourtier Aug 7, 2024
a188c4b
Update unscented_kalman.py
NicolaCourtier Aug 7, 2024
ca2a5b1
Update unscented_kalman.py
NicolaCourtier Aug 7, 2024
a4c0030
Update test_models.py
NicolaCourtier Aug 7, 2024
742f9d4
Update test_set_initial_state
NicolaCourtier Aug 7, 2024
db09455
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 7, 2024
4f7c75f
Merge branch '424-fitting-ocv' into 238b-multi-fitting
NicolaCourtier Aug 7, 2024
bbece50
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Aug 7, 2024
453d618
Use clear in model.new_copy
NicolaCourtier Aug 7, 2024
36ddd93
Reference public attributes
NicolaCourtier Aug 7, 2024
1b8cdb1
Move MultiFittingProblem into separate file
NicolaCourtier Aug 7, 2024
60a6ca8
Update description
NicolaCourtier Aug 7, 2024
9e02d24
Add dataset property
NicolaCourtier Aug 7, 2024
e494be5
Fix changes due to linting
NicolaCourtier Aug 7, 2024
172b484
Add test_multi_fitting_problem
NicolaCourtier Aug 7, 2024
0cb2917
Add problem.set_initial_state
NicolaCourtier Aug 8, 2024
a865732
Merge rebuild into build
NicolaCourtier Aug 8, 2024
0e4754b
Update CHANGELOG.md
NicolaCourtier Aug 8, 2024
791a732
Update base_model.py
NicolaCourtier Aug 8, 2024
f675e2e
Fix notebooks
NicolaCourtier Aug 8, 2024
d55a624
Merge branch '444-merge-rebuild' into 238b-multi-fitting
NicolaCourtier Aug 8, 2024
9c2f0b3
Update multi_fitting with different initial SoC
NicolaCourtier Aug 8, 2024
25f0714
Update copying
NicolaCourtier Aug 8, 2024
2cf54a6
Add check for identical models
NicolaCourtier Aug 8, 2024
dbded27
Merge branch 'develop' into 238b-multi-fitting
NicolaCourtier Aug 9, 2024
7eeeb00
refactor: model.new_copy() args as dictionary and single construction
BradyPlanden Aug 12, 2024
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

- [#364](https://github.com/pybop-team/PyBOP/pull/364) - Adds the MultiFittingProblem class and the multi_fitting example script.
- [#413](https://github.com/pybop-team/PyBOP/pull/413) - Adds `DesignCost` functionality to `WeightedCost` class with additional tests.
- [#357](https://github.com/pybop-team/PyBOP/pull/357) - Adds `Transformation()` class with `LogTransformation()`, `IdentityTransformation()`, and `ScaledTransformation()`, `ComposedTransformation()` implementations with corresponding examples and tests.
- [#427](https://github.com/pybop-team/PyBOP/issues/427) - Adds the nbstripout pre-commit hook to remove unnecessary metadata from notebooks.
Expand Down
79 changes: 79 additions & 0 deletions examples/scripts/multi_fitting.py
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np

import pybop

# Parameter set and model definition
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Generate a dataset
sigma = 0.001
experiment = pybop.Experiment([("Discharge at 0.5C for 2 minutes (4 second period)")])
values = model.predict(experiment=experiment)
dataset_1 = pybop.Dataset(
{
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": values["Voltage [V]"].data
+ np.random.normal(0, sigma, len(values["Voltage [V]"].data)),
}
)

# Generate a second dataset
experiment = pybop.Experiment([("Discharge at 1C for 2 minutes (4 second period)")])
values = model.predict(experiment=experiment)
dataset_2 = pybop.Dataset(
{
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": values["Voltage [V]"].data
+ np.random.normal(0, sigma, len(values["Voltage [V]"].data)),
}
)

# Fitting parameters
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.68, 0.05),
true_value=parameter_set["Negative electrode active material volume fraction"],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.58, 0.05),
true_value=parameter_set["Positive electrode active material volume fraction"],
),
)


# Generate a problem for each dataset and combine into one
problem_1 = pybop.FittingProblem(model, parameters, dataset_1)
problem_2 = pybop.FittingProblem(model, parameters, dataset_2)
problem = pybop.MultiFittingProblem(problem_1, problem_2)

# Generate the cost function and optimisation class
cost = pybop.SumSquaredError(problem)
optim = pybop.IRPropMin(
cost,
# sigma0=0.011,
verbose=True,
max_iterations=12,
)

# Run optimisation
x, final_cost = optim.run()
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem_1, problem_inputs=x, title="Optimised Comparison")
pybop.quick_plot(problem_2, 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
bounds = np.array([[0.5, 0.8], [0.4, 0.7]])
pybop.plot2d(optim, bounds=bounds, steps=15)
2 changes: 1 addition & 1 deletion pybop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
# Problem class
#
from .problems.base_problem import BaseProblem
from .problems.fitting_problem import FittingProblem
from .problems.fitting_problem import FittingProblem, MultiFittingProblem
from .problems.design_problem import DesignProblem

#
Expand Down
62 changes: 45 additions & 17 deletions pybop/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, name="Base Model", parameter_set=None):
else: # a pybop parameter set
self._parameter_set = pybamm.ParameterValues(parameter_set.params)

self.pybamm_model = None
self.parameters = Parameters()
self.dataset = None
self.signal = None
Expand Down Expand Up @@ -506,8 +507,10 @@ def predict(
"""
inputs = self.parameters.verify(inputs)

if not self.pybamm_model._built:
self.pybamm_model.build_model()
if self.pybamm_model is None:
raise ValueError("This sim method currently only supports PyBaMM models")
elif not self._unprocessed_model._built:
self._unprocessed_model.build_model()

parameter_set = parameter_set or self._unprocessed_parameter_set
if inputs is not None:
Expand All @@ -518,22 +521,17 @@ def predict(
parameter_set=parameter_set,
allow_infeasible_solutions=self.allow_infeasible_solutions,
):
if self._unprocessed_model is not None:
if experiment is None:
return pybamm.Simulation(
self._unprocessed_model,
parameter_values=parameter_set,
).solve(t_eval=t_eval, initial_soc=init_soc)
else:
return pybamm.Simulation(
self._unprocessed_model,
experiment=experiment,
parameter_values=parameter_set,
).solve(initial_soc=init_soc)
if experiment is None:
return pybamm.Simulation(
self._unprocessed_model,
parameter_values=parameter_set,
).solve(t_eval=t_eval, initial_soc=init_soc)
else:
raise ValueError(
"This sim method currently only supports PyBaMM models"
)
return pybamm.Simulation(
self._unprocessed_model,
experiment=experiment,
parameter_values=parameter_set,
).solve(initial_soc=init_soc)

else:
return [np.inf]
Expand Down Expand Up @@ -598,6 +596,36 @@ def copy(self):
"""
return copy.copy(self)

def new_copy(self):
"""
Return a new copy of the model, explicitly copying all the mutable attributes
to avoid issues with shared objects.

Returns
-------
BaseModel
A new copy of the model.
"""
new_model = copy.copy(self)

# Reset the key attributes
new_model.param_check_counter = 0
if self.pybamm_model is not None:
new_model.parameter_set = self._unprocessed_parameter_set
new_model.pybamm_model = self._unprocessed_model.new_copy()
new_model.geometry = self.pybamm_model.default_geometry
new_model.submesh_types = self.pybamm_model.default_submesh_types
new_model.var_pts = self.pybamm_model.default_var_pts
new_model.spatial_methods = self.pybamm_model.default_spatial_methods
new_model.solver = self.pybamm_model.default_solver
new_model._model_with_set_params = None
new_model._built_model = None
new_model._built_initial_soc = None
new_model._mesh = None
new_model._disc = None

return new_model

def cell_mass(self, parameter_set: ParameterSet = None):
"""
Calculate the cell mass in kilograms.
Expand Down
7 changes: 4 additions & 3 deletions pybop/models/empirical/base_ecm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ def __init__(
model_options = dict(build=False)
for key, value in model_kwargs.items():
model_options[key] = value
self.pybamm_model = pybamm_model(**model_options)
self._unprocessed_model = self.pybamm_model
pybamm_model = pybamm_model(**model_options)

# Correct OCP if set to default
if (
parameter_set is not None
and "Open-circuit voltage [V]" in parameter_set.keys()
):
default_ocp = self.pybamm_model.default_parameter_values[
default_ocp = pybamm_model.default_parameter_values[
"Open-circuit voltage [V]"
]
if parameter_set["Open-circuit voltage [V]"] == "default":
print("Setting open-circuit voltage to default function")
parameter_set["Open-circuit voltage [V]"] = default_ocp

super().__init__(name=name, parameter_set=parameter_set)
self.pybamm_model = pybamm_model
self._unprocessed_model = self.pybamm_model

# Set parameters, using either the provided ones or the default
self.default_parameter_values = self.pybamm_model.default_parameter_values
Expand Down
20 changes: 0 additions & 20 deletions pybop/models/empirical/ecm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pybamm import equivalent_circuit as pybamm_equivalent_circuit

from pybop.models.empirical.base_ecm import ECircuitModel
from pybop.parameters.parameter import Inputs


class Thevenin(ECircuitModel):
Expand Down Expand Up @@ -44,22 +43,3 @@ def __init__(
super().__init__(
pybamm_model=pybamm_equivalent_circuit.Thevenin, name=name, **model_kwargs
)

def _check_params(self, inputs: Inputs = None, allow_infeasible_solutions=True):
"""
Check the compatibility of the model parameters.

Parameters
----------
inputs : Inputs
The input parameters for the simulation.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

Returns
-------
bool
A boolean which signifies whether the parameters are compatible.

"""
return True
3 changes: 2 additions & 1 deletion pybop/problems/base_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def __init__(
)

self.parameters = parameters
self._model = model
if model is not None:
self._model = model
self.check_model = check_model
if isinstance(signal, str):
signal = [signal]
Expand Down
Loading
Loading