Skip to content

Commit

Permalink
Pass parameters to get_data
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaCourtier committed May 22, 2024
1 parent d042e0d commit ed9de21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
15 changes: 5 additions & 10 deletions tests/integration/test_optimisation_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def noise(self, sigma, values):
def spm_costs(self, model, parameters, cost_class):
# Form dataset
init_soc = 0.5
solution = self.getdata(model, self.ground_truth, init_soc)
solution = self.get_data(model, parameters, self.ground_truth, init_soc)
dataset = pybop.Dataset(
{
"Time [s]": solution["Time [s]"].data,
Expand Down Expand Up @@ -99,15 +99,10 @@ def test_optimisation_f_guessed(self, f_guessed, spm_costs):

# Assertions
assert initial_cost > final_cost
np.testing.assert_allclose(x, self.ground_truth, atol=2.5e-2)
np.testing.assert_allclose(x, self.ground_truth, atol=1e-2)

def getdata(self, model, x, init_soc):
model.parameter_set.update(
{
"Negative electrode active material volume fraction": x[0],
"Positive electrode active material volume fraction": x[1],
}
)
def get_data(self, model, parameters, x, init_soc):
model.parameters = parameters
experiment = pybop.Experiment(
[
(
Expand All @@ -117,5 +112,5 @@ def getdata(self, model, x, init_soc):
]
* 2
)
sim = model.predict(init_soc=init_soc, experiment=experiment)
sim = model.predict(init_soc=init_soc, experiment=experiment, inputs=x)
return sim
25 changes: 8 additions & 17 deletions tests/integration/test_spm_parameterisations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import pytest
from pybamm import __version__ as pybamm_version

import pybop

Expand Down Expand Up @@ -59,7 +58,7 @@ def noise(self, sigma, values):
@pytest.fixture
def spm_costs(self, model, parameters, cost_class, init_soc):
# Form dataset
solution = self.getdata(model, self.ground_truth, init_soc)
solution = self.get_data(model, parameters, self.ground_truth, init_soc)
dataset = pybop.Dataset(
{
"Time [s]": solution["Time [s]"].data,
Expand Down Expand Up @@ -119,16 +118,13 @@ def test_spm_optimisers(self, optimiser, spm_costs):
# Assertions
if not np.allclose(x0, self.ground_truth, atol=1e-5):
assert initial_cost > final_cost
if pybamm_version <= "23.9":
np.testing.assert_allclose(x, self.ground_truth, atol=2.5e-2)
else:
np.testing.assert_allclose(x, self.ground_truth, atol=1.75e-2)
np.testing.assert_allclose(x, self.ground_truth, atol=1e-2)

@pytest.fixture
def spm_two_signal_cost(self, parameters, model, cost_class):
# Form dataset
init_soc = 0.5
solution = self.getdata(model, self.ground_truth, init_soc)
solution = self.get_data(model, parameters, self.ground_truth, init_soc)
dataset = pybop.Dataset(
{
"Time [s]": solution["Time [s]"].data,
Expand Down Expand Up @@ -188,7 +184,7 @@ def test_multiple_signals(self, multi_optimiser, spm_two_signal_cost):
# Assertions
if not np.allclose(x0, self.ground_truth, atol=1e-5):
assert initial_cost > final_cost
np.testing.assert_allclose(x, self.ground_truth, atol=2.5e-2)
np.testing.assert_allclose(x, self.ground_truth, atol=1e-2)

@pytest.mark.parametrize("init_soc", [0.4, 0.6])
@pytest.mark.integration
Expand All @@ -199,7 +195,7 @@ def test_model_misparameterisation(self, parameters, model, init_soc):
second_model = pybop.lithium_ion.SPMe(parameter_set=second_parameter_set)

# Form dataset
solution = self.getdata(second_model, self.ground_truth, init_soc)
solution = self.get_data(second_model, parameters, self.ground_truth, init_soc)
dataset = pybop.Dataset(
{
"Time [s]": solution["Time [s]"].data,
Expand All @@ -226,13 +222,8 @@ def test_model_misparameterisation(self, parameters, model, init_soc):
np.testing.assert_allclose(final_cost, 0, atol=1e-2)
np.testing.assert_allclose(x, self.ground_truth, atol=2e-2)

def getdata(self, model, x, init_soc):
model.parameter_set.update(
{
"Negative electrode active material volume fraction": x[0],
"Positive electrode active material volume fraction": x[1],
}
)
def get_data(self, model, parameters, x, init_soc):
model.parameters = parameters
experiment = pybop.Experiment(
[
(
Expand All @@ -242,5 +233,5 @@ def getdata(self, model, x, init_soc):
]
* 2
)
sim = model.predict(init_soc=init_soc, experiment=experiment)
sim = model.predict(init_soc=init_soc, experiment=experiment, inputs=x)
return sim
13 changes: 4 additions & 9 deletions tests/integration/test_thevenin_parameterisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cost_class(self, request):
@pytest.fixture
def cost(self, model, parameters, cost_class):
# Form dataset
solution = self.getdata(model, self.ground_truth)
solution = self.get_data(model, parameters, self.ground_truth)
dataset = pybop.Dataset(
{
"Time [s]": solution["Time [s]"].data,
Expand Down Expand Up @@ -88,13 +88,8 @@ def test_optimisers_on_simple_model(self, optimiser, cost):
assert initial_cost > final_cost
np.testing.assert_allclose(x, self.ground_truth, atol=1e-2)

def getdata(self, model, x):
model.parameter_set.update(
{
"R0 [Ohm]": x[0],
"R1 [Ohm]": x[1],
}
)
def get_data(self, model, parameters, x):
model.parameters = parameters
experiment = pybop.Experiment(
[
(
Expand All @@ -103,5 +98,5 @@ def getdata(self, model, x):
),
]
)
sim = model.predict(experiment=experiment)
sim = model.predict(experiment=experiment, inputs=x)
return sim

0 comments on commit ed9de21

Please sign in to comment.