-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from pybop-team/72-54-restructure-and-add-pint…
- Loading branch information
Showing
30 changed files
with
881 additions
and
315 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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,57 @@ | ||
import pybop | ||
import pints | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
model = pybop.lithium_ion.SPMe() | ||
|
||
inputs = { | ||
"Negative electrode active material volume fraction": 0.58, | ||
"Positive electrode active material volume fraction": 0.44, | ||
"Current function [A]": 1, | ||
} | ||
t_eval = np.arange(0, 900, 2) | ||
model.build(fit_parameters=inputs) | ||
|
||
values = model.predict(inputs=inputs, t_eval=t_eval) | ||
voltage = values["Terminal voltage [V]"].data | ||
time = values["Time [s]"].data | ||
|
||
sigma = 0.001 | ||
CorruptValues = voltage + np.random.normal(0, sigma, len(voltage)) | ||
|
||
# Show the generated data | ||
plt.figure() | ||
plt.xlabel("Time") | ||
plt.ylabel("Values") | ||
plt.plot(time, CorruptValues) | ||
plt.plot(time, voltage) | ||
plt.show() | ||
|
||
|
||
problem = pints.SingleOutputProblem(model, time, CorruptValues) | ||
|
||
# Select a score function | ||
score = pints.SumOfSquaresError(problem) | ||
|
||
x0 = np.array([0.48, 0.55, 1.4]) | ||
opt = pints.OptimisationController(score, x0, method=pints.GradientDescent) | ||
|
||
opt.optimiser().set_learning_rate(0.025) | ||
opt.set_max_unchanged_iterations(50) | ||
opt.set_max_iterations(200) | ||
|
||
x1, f1 = opt.run() | ||
print("Estimated parameters:") | ||
print(x1) | ||
|
||
# Show the generated data | ||
simulated_values = problem.evaluate(x1[:3]) | ||
|
||
plt.figure() | ||
plt.xlabel("Time") | ||
plt.ylabel("Values") | ||
plt.plot(time, CorruptValues) | ||
plt.fill_between(time, simulated_values - sigma, simulated_values + sigma, alpha=0.2) | ||
plt.plot(time, simulated_values) | ||
plt.show() |
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,60 @@ | ||
import numpy as np | ||
|
||
|
||
class SingleOutputProblem: | ||
""" | ||
Defines a PyBOP single output problem, follows the PINTS interface. | ||
""" | ||
|
||
def __init__(self, model, parameters, signal, dataset): | ||
self._model = model | ||
self.parameters = {o.name: o for o in parameters} | ||
self.signal = signal | ||
self._dataset = dataset | ||
|
||
if self._model._built_model is None: | ||
self._model.build(fit_parameters=self.parameters) | ||
|
||
for i, item in enumerate(self._dataset): | ||
if item.name == "Time [s]": | ||
self._time_data_available = True | ||
self._time_data = self._dataset[i].data | ||
|
||
if item.name == signal: | ||
self._ground_truth = self._dataset[i].data | ||
|
||
if self._time_data_available is False: | ||
raise ValueError("Dataset must contain time data") | ||
|
||
if np.any(self._time_data < 0): | ||
raise ValueError("Times can not be negative.") | ||
if np.any(self._time_data[:-1] >= self._time_data[1:]): | ||
raise ValueError("Times must be increasing.") | ||
|
||
if len(self._ground_truth) != len(self._time_data): | ||
raise ValueError("Time data and signal data must be the same length.") | ||
|
||
def evaluate(self, parameters): | ||
""" | ||
Evaluate the model with the given parameters and return the signal. | ||
""" | ||
|
||
y = np.asarray( | ||
self._model.simulate(inputs=parameters, t_eval=self.model.time_data)[ | ||
self.signal | ||
].data | ||
) | ||
|
||
return y | ||
|
||
def evaluateS1(self, parameters): | ||
""" | ||
Evaluate the model with the given parameters and return the signal and | ||
its derivatives. | ||
""" | ||
|
||
y, dy_dp = self._model.simulateS1( | ||
inputs=parameters, t_eval=self.model.time_data, calculate_sensitivities=True | ||
)[self.signal] | ||
|
||
return (np.asarray(y), np.asarray(dy_dp)) |
Oops, something went wrong.