Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates example script file names #637

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 0 additions & 83 deletions examples/scripts/comparison_examples/exp_UKF.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
# Plot the timeseries output (requires model that returns Voltage)
pybop.plot.quick(observer, problem_inputs=results.x, title="Optimised Comparison")

# # Plot convergence
# pybop.plot.convergence(optim)
# Plot convergence
pybop.plot.convergence(optim)

# # Plot the parameter traces
# pybop.plot.parameters(optim)
# Plot the parameter traces
pybop.plot.parameters(optim)

# # Plot the cost landscape with optimisation path
# pybop.plot.surface(optim)
# Plot the cost landscape with optimisation path
pybop.plot.surface(optim)
87 changes: 87 additions & 0 deletions examples/scripts/getting_started/ask-tell-interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import numpy as np
import pybamm

import pybop

# Define model and use high-performant solver for sensitivities
solver = pybamm.IDAKLUSolver()
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set, solver=solver)

# Fitting parameters
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.05),
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.05),
),
)

# Generate data
sigma = 0.003
experiment = pybop.Experiment(
[
(
"Discharge at 0.5C for 3 minutes (3 second period)",
"Charge at 0.5C for 3 minutes (3 second period)",
),
]
* 2
)
values = model.predict(initial_state={"Initial SoC": 0.5}, experiment=experiment)


def noise(sigma):
return np.random.normal(0, sigma, len(values["Voltage [V]"].data))


# Form dataset
dataset = pybop.Dataset(
{
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": values["Voltage [V]"].data + noise(sigma),
"Bulk open-circuit voltage [V]": values["Bulk open-circuit voltage [V]"].data
+ noise(sigma),
}
)

signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Construct the problem and cost classes
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
cost = pybop.Minkowski(problem, p=2)

# We construct the optimiser class the same as normal
# but will be using the `optimiser` attribute directly
# for this example. This interface works for all
# non SciPy-based optimisers.
# Warning: not all arguments are supported via this
# interface.
optim = pybop.AdamW(cost)

# Create storage vars
x_best = []
f_best = []

# Run optimisation
for i in range(100):
x = optim.optimiser.ask()
f = [cost(x[0], calculate_grad=True)]
optim.optimiser.tell(f)

# Store best solution so far
x_best.append(optim.optimiser.x_best())
f_best.append(optim.optimiser.x_best())

if i % 10 == 0:
print(
f"Iteration: {i} | Cost: {optim.optimiser.f_best()} | Parameters: {optim.optimiser.x_best()}"
)

# Plot the timeseries output
pybop.plot.quick(
problem, problem_inputs=optim.optimiser.x_best(), title="Optimised Comparison"
)