-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultiFittingProblem class and example (#364)
* Add MultiFittingProblem, example and test * Remove unused n_problems property * Update multi_fitting.py * Update CHANGELOG.md * Remove unused weights * Update problem_list to problem args * Concatenate the whole list * Apply suggestions from code review * Update description * Update default init_soc * Update CHANGELOG.md * Update check_params * Add pybamm_model as default attribute * Ensure predict uses unprocessed_model * Move rebuild check to model.simulate * Align simulate output with predict * Replace init_soc with init_ocv for FittingProblem * Update notebooks * Update test_observers.py * Update descriptions and simplify * Add test_set_initial_state * Copy each model into MultiFittingProblem * Update test_problem.py * Update ecm.py * style: pre-commit fixes * Break connection between parameter_sets * Allow predict to update initial state * Fix typo * Add nbstripout pre-commit hook * Add -q and re-run all notebooks * Copy parameter sets and remove model.initial_state * Reset spm_NelderMead.py * Update CHANGELOG.md * Update CHANGELOG.md * Allow parameter_set is None * Re-run notebooks * Update bounds * Update notebooks * Update notebooks * Set numpy random seed in notebooks * Re-run with fixed seed * Update bounds * Update notebooks to initial_state * Add set_initial_state for ECMs * Add init_ocv setter * Add init_ocv values * Re-run notebooks * Add tests for ECM get_initial_state * Add ECM initial state error tests * Remove unused store_optimised_parameters * Update parameters.initial_value * Use any Initial SoC from parameter_set * Update bounds again * Update init_soc in notebooks * Move dataset check within unscented_kalman * Remove unnecessary lines from spm_UKF * Update all parameters for rebuild * Update init_ocv to _init_ocv * Ensure value updates alongside initial_value * Update multi_model_identification * Update spm_electrode_design.ipynb * Update spm_electrode_design.ipynb * Fix identation * Fix test_plots design problem * Move Changelog entry to breaking changes * Move Changelog entry * style: pre-commit fixes * Fix merge mistake * style: pre-commit fixes * Allow kwargs in MultiFitting evaluate * Add tests * Update integration tests * Update spm_weighted_cost.py * Fix tests * style: pre-commit fixes * Fix model type check * Update _parameter_set to parameter_set * style: pre-commit fixes * Update tests with parameter set * Add model build description * Revert to _parameter_set * Fix predict without pybamm test * Apply suggestions from code review Co-authored-by: Brady Planden <[email protected]> * Apply suggestions from code review Co-authored-by: Brady Planden <[email protected]> * Fix syntax * Fix variable name * Update model type check * Update parameter_set setter * style: pre-commit fixes * Add parameters.reset_initial_value * Add n_outputs property * style: pre-commit fixes * Remove public parameter_set setter * Correct integer to float * Convert initial_state to dict * Add guidance * Remove empty dictionary defaults * style: pre-commit fixes * Add warning stacklevels * Catch simulation errors in problem evaluation * Add pybamm version comment * Add set initial ocv check * Add model.clear and remove setters * Update unscented_kalman.py * Update unscented_kalman.py * Update test_models.py * Update test_set_initial_state * Use clear in model.new_copy * Reference public attributes * Move MultiFittingProblem into separate file * Update description * Add dataset property * Fix changes due to linting * Add test_multi_fitting_problem * Add problem.set_initial_state * Merge rebuild into build * Update CHANGELOG.md * Update base_model.py * Fix notebooks * Update multi_fitting with different initial SoC * Update copying * Add check for identical models * refactor: model.new_copy() args as dictionary and single construction --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Brady Planden <[email protected]> Co-authored-by: Brady Planden <[email protected]>
- Loading branch information
1 parent
df429f7
commit 778a72f
Showing
14 changed files
with
437 additions
and
41 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,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) | ||
|
||
# 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 dataset and a fitting problem | ||
sigma = 0.001 | ||
experiment = pybop.Experiment([("Discharge at 0.5C for 2 minutes (4 second period)")]) | ||
values = model.predict(initial_state={"Initial SoC": 0.8}, 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)), | ||
} | ||
) | ||
problem_1 = pybop.FittingProblem(model, parameters, dataset_1) | ||
|
||
# Generate a second dataset and problem | ||
model = model.new_copy() | ||
experiment = pybop.Experiment([("Discharge at 1C for 1 minutes (4 second period)")]) | ||
values = model.predict(initial_state={"Initial SoC": 0.8}, 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)), | ||
} | ||
) | ||
problem_2 = pybop.FittingProblem(model, parameters, dataset_2) | ||
|
||
# Combine the problems into one | ||
problem = pybop.MultiFittingProblem(problem_1, problem_2) | ||
|
||
# Generate the cost function and optimisation class | ||
cost = pybop.SumSquaredError(problem) | ||
optim = pybop.IRPropMin( | ||
cost, | ||
verbose=True, | ||
max_iterations=100, | ||
) | ||
|
||
# Run optimisation | ||
x, final_cost = optim.run() | ||
print("True parameters:", parameters.true_value()) | ||
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) |
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
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
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
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
Oops, something went wrong.