Interactive optimizer¶
This notebook shows how to use the interactive fitting optimizer, together with a chain optimizer for the final fit.
import ionworkspipeline as iwp
import pandas as pd
import numpy as np
import pybamm
Set up the model and generate some synthetic data.
model = pybamm.lithium_ion.SPM()
parameter_values = iwp.ParameterValues("Chen2020")
sim = iwp.Simulation(model, parameter_values=parameter_values)
sim.solve(np.linspace(0, 3600, 1000))
data = pd.DataFrame(
{x: sim.solution[x].entries for x in ["Time [s]", "Current [A]", "Voltage [V]"]}
)
Set up the data fit using the interactive optimizer.
Note: you’ll need to update the
_testing
flag toFalse
to run this interactively. We skip the interactive widget for automated testing, so that the notebook can be run without user intervention.
# For testing purposes we skip the interactive widget
# Change this to false to use the interactive widget
_testing = True
# Create pipeline element and prepare parameters
parameters = {
"Positive particle diffusivity [m2.s-1]": iwp.Parameter(
"D_s", initial_value=1e-15, bounds=(1e-16, 1e-14)
),
"Positive electrode active material volume fraction": iwp.Parameter(
"eps", initial_value=0.7, bounds=(0.3, 1.0)
),
}
objective = iwp.objectives.CurrentDriven(data, options={"model": model})
current_driven = iwp.DataFit(
objective,
parameters=parameters,
optimizer=iwp.parameter_estimators.Chain(
iwp.optimizers.Interactive(_testing=_testing),
iwp.optimizers.ScipyMinimize(method="COBYQA"),
),
cost=iwp.costs.RMSE(),
)
# make sure we're not accidentally initializing with the correct values by passing
# them in
params_for_pipeline = {k: v for k, v in parameter_values.items() if k not in parameters}
To use the interactive optimizer, move the slider(s) to the desired value(s) and click the “Submit” button. This will then pass the current value(s) of the slider(s) the the next step, which in this case is the fine-tuning (COBYQA) optimizer.
results = current_driven.run(params_for_pipeline)
for p in results:
print(f"'{p}': true: {parameter_values[p]}, fit: {results[p]}")
'Positive particle diffusivity [m2.s-1]': true: 4e-15, fit: 3.9987335599305285e-15
'Positive electrode active material volume fraction': true: 0.665, fit: 0.6650366505760176