-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix workflow for changing experiment (#240)
* Update if statement * Update base_model.py * Create test_model_experiment_changes.py * Restrict import * Rebuild model on problem definition * Update CHANGELOG.md * Update t_eval Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
- Loading branch information
1 parent
68b4c9b
commit cb73cec
Showing
5 changed files
with
144 additions
and
36 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import pybop | ||
import pytest | ||
import numpy as np | ||
|
||
|
||
class TestModelAndExperimentChanges: | ||
""" | ||
A class to test that different inputs return different outputs. | ||
""" | ||
|
||
@pytest.fixture( | ||
params=[ | ||
pybop.Parameter( # geometric parameter | ||
"Negative particle radius [m]", | ||
prior=pybop.Gaussian(6e-06, 0.1e-6), | ||
bounds=[1e-6, 9e-6], | ||
true_value=5.86e-6, | ||
), | ||
pybop.Parameter( # non-geometric parameter | ||
"Positive electrode diffusivity [m2.s-1]", | ||
prior=pybop.Gaussian(3.43e-15, 1e-15), | ||
bounds=[1e-15, 5e-15], | ||
true_value=4e-15, | ||
), | ||
] | ||
) | ||
def parameter(self, request): | ||
return request.param | ||
|
||
@pytest.mark.integration | ||
def test_changing_experiment(self, parameter): | ||
# Change the experiment and check that the results are different. | ||
|
||
parameter_set = pybop.ParameterSet.pybamm("Chen2020") | ||
parameters = [parameter] | ||
init_soc = 0.5 | ||
model = pybop.lithium_ion.SPM(parameter_set=parameter_set) | ||
|
||
t_eval = np.arange(0, 3600, 2) # Default 1C discharge to cut-off voltage | ||
solution_1 = model.predict(init_soc=init_soc, t_eval=t_eval) | ||
cost_1 = self.final_cost(solution_1, model, parameters, init_soc) | ||
|
||
experiment = pybop.Experiment(["Charge at 1C until 4.1 V (2 seconds period)"]) | ||
solution_2 = model.predict( | ||
init_soc=init_soc, experiment=experiment, inputs=[parameter.true_value] | ||
) | ||
cost_2 = self.final_cost(solution_2, model, parameters, init_soc) | ||
|
||
with np.testing.assert_raises(AssertionError): | ||
np.testing.assert_array_equal( | ||
solution_1["Voltage [V]"].data, | ||
solution_2["Voltage [V]"].data, | ||
) | ||
|
||
# The datasets are not corrupted so the costs should be zero | ||
np.testing.assert_almost_equal(cost_1, 0) | ||
np.testing.assert_almost_equal(cost_2, 0) | ||
|
||
@pytest.mark.integration | ||
def test_changing_model(self, parameter): | ||
# Change the model and check that the results are different. | ||
|
||
parameter_set = pybop.ParameterSet.pybamm("Chen2020") | ||
parameters = [parameter] | ||
init_soc = 0.5 | ||
experiment = pybop.Experiment(["Charge at 1C until 4.1 V (2 seconds period)"]) | ||
|
||
model = pybop.lithium_ion.SPM(parameter_set=parameter_set) | ||
solution_1 = model.predict(init_soc=init_soc, experiment=experiment) | ||
cost_1 = self.final_cost(solution_1, model, parameters, init_soc) | ||
|
||
model = pybop.lithium_ion.SPMe(parameter_set=parameter_set) | ||
solution_2 = model.predict(init_soc=init_soc, experiment=experiment) | ||
cost_2 = self.final_cost(solution_2, model, parameters, init_soc) | ||
|
||
with np.testing.assert_raises(AssertionError): | ||
np.testing.assert_array_equal( | ||
solution_1["Voltage [V]"].data, | ||
solution_2["Voltage [V]"].data, | ||
) | ||
|
||
# The datasets are not corrupted so the costs should be zero | ||
np.testing.assert_almost_equal(cost_1, 0) | ||
np.testing.assert_almost_equal(cost_2, 0) | ||
|
||
def final_cost(self, solution, model, parameters, init_soc): | ||
# Compute the cost corresponding to a particular solution | ||
x0 = np.array([parameters[0].true_value]) | ||
dataset = pybop.Dataset( | ||
{ | ||
"Time [s]": solution["Time [s]"].data, | ||
"Current function [A]": solution["Current [A]"].data, | ||
"Voltage [V]": solution["Voltage [V]"].data, | ||
} | ||
) | ||
signal = ["Voltage [V]"] | ||
problem = pybop.FittingProblem( | ||
model, parameters, dataset, signal=signal, x0=x0, init_soc=init_soc | ||
) | ||
cost = pybop.RootMeanSquaredError(problem) | ||
optim = pybop.Optimisation(cost, optimiser=pybop.PSO) | ||
x, final_cost = optim.run() | ||
return final_cost |