-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathadamw.py
89 lines (75 loc) · 2.32 KB
/
adamw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import numpy as np
import pybamm
import pybop
# Get the current directory location and convert to absolute path
current_dir = os.path.dirname(os.path.abspath(__file__))
dataset_path = os.path.join(
current_dir, "../../data/synthetic/spm_charge_discharge_75.csv"
)
# Define model and use high-performant solver for sensitivities
solver = pybamm.IDAKLUSolver()
parameter_set = pybop.ParameterSet("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.68, 0.05),
initial_value=0.45,
bounds=[0.4, 0.9],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.58, 0.05),
initial_value=0.45,
bounds=[0.4, 0.9],
),
)
# Import the synthetic dataset, set model initial state
csv_data = np.loadtxt(dataset_path, delimiter=",", skiprows=1)
initial_state = {"Initial open-circuit voltage [V]": csv_data[0, 2]}
model.set_initial_state(initial_state=initial_state)
# Form dataset
dataset = pybop.Dataset(
{
"Time [s]": csv_data[:, 0],
"Current function [A]": csv_data[:, 1],
"Voltage [V]": csv_data[:, 2],
"Bulk open-circuit voltage [V]": csv_data[:, 3],
}
)
signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(
model,
parameters,
dataset,
signal=signal,
)
cost = pybop.SumOfPower(problem, p=2.5)
optim = pybop.AdamW(
cost,
verbose=True,
verbose_print_rate=20,
allow_infeasible_solutions=True,
sigma0=0.02,
max_iterations=100,
max_unchanged_iterations=45,
compute_sensitivities=True,
n_sensitivity_samples=128,
)
# Reduce the momentum influence
# for the reduced number of optimiser iterations
optim.optimiser.b1 = 0.9
optim.optimiser.b2 = 0.9
# Run optimisation
results = optim.run()
# Plot the timeseries output
pybop.plot.problem(problem, problem_inputs=results.x, title="Optimised Comparison")
# Plot convergence
pybop.plot.convergence(optim)
# Plot the parameter traces
pybop.plot.parameters(optim)
# Plot the cost landscape with optimisation path
pybop.plot.surface(optim)