-
Notifications
You must be signed in to change notification settings - Fork 10
/
runBattery1DOptimize.m
253 lines (186 loc) · 8 KB
/
runBattery1DOptimize.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
%% Setup
% Clear the workspace and close open figures
clear all
close all
% Load MRST modules
mrstModule add ad-core mrst-gui mpfa optimization
%% Setup the properties of Li-ion battery materials and cell design
jsonstruct = parseBattmoJson(fullfile('ParameterData','BatteryCellParameters','LithiumIonBatteryCell','lithium_ion_battery_nmc_graphite.json'));
jsonstruct.include_current_collectors = false;
jsonstruct.use_thermal = false;
% We define some shorthand names for simplicity.
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
am = 'ActiveMaterial';
cc = 'CurrentCollector';
elyte = 'Electrolyte';
thermal = 'ThermalModel';
itf = 'Interface';
sd = 'SolidDiffusion';
ctrl = 'Control';
sep = 'Separator';
jsonstruct.(ne).(am).diffusionModelType = 'simple';
jsonstruct.(pe).(am).diffusionModelType = 'simple';
inputparams = BatteryInputParams(jsonstruct);
inputparams.(ctrl).useCVswitch = true;
%% Setup the geometry and computational grid
gen = BatteryGeneratorP2D();
% Now, we update the inputparams with the properties of the grid.
inputparams = gen.updateBatteryInputParams(inputparams);
% Initialize the battery model.
model = Battery(inputparams);
%% Setup the time step schedule
% Smaller time steps are used to ramp up the current from zero to its
% operational value. Larger time steps are then used for the normal
% operation.
CRate = model.Control.CRate;
total = 1.2*hour/CRate;
n = 40;
dt = total*0.7/n;
step = struct('val', dt*ones(n, 1), 'control', ones(n, 1));
% Setup the control by assigning a source and stop function.
control = model.Control.setupScheduleControl();
nc = 1;
nst = numel(step.control);
ind = floor(((0 : nst - 1)/nst)*nc) + 1;
step.control = ind;
control.Imax = model.Control.Imax;
control = repmat(control, nc, 1);
schedule = struct('control', control, 'step', step);
%% Setup the nonlinear solver
nls = NonLinearSolver();
% Change the number of maximum nonlinear iterations
nls.maxIterations = 10;
% Change default behavior of nonlinear solver, in case of error
nls.errorOnFailure = false;
% Change tolerance for the nonlinear iterations
model.nonlinearTolerance = 1e-3*model.Control.Imax;
% Set verbosity
model.verbose = false;
%% Setup the initial state and solve
% Setup the initial state
initstate = model.setupInitialState();
% Run the simulation
[~, states, ~] = simulateScheduleAD(initstate, model, schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls);
model0 = model;
%% Process output and recover the output voltage and current from the output states.
ind = cellfun(@(x) not(isempty(x)), states);
states = states(ind);
E = cellfun(@(x) x.Control.E, states);
I = cellfun(@(x) x.Control.I, states);
time = cellfun(@(x) x.time, states);
doPlot = false;
if doPlot
figure;
plot(time/hour, E, '*-', 'displayname', 'initial');
xlabel('time / h');
ylabel('voltage / V');
grid on
end
%% Calculate the energy
obj = @(model, states, schedule, varargin) EnergyOutput(model, states, schedule, varargin{:});
vals = obj(model, states, schedule);
totval = sum([vals{:}]);
% Compare with trapezoidal integral: they should be about the same
totval_trapz = trapz(time, E.*I);
fprintf('Rectangle rule: %g Wh, trapezoidal rule: %g Wh\n', totval/hour, totval_trapz/hour);
%% Setup the optimization problem
state0 = initstate;
SimulatorSetup = struct('model', model, 'schedule', schedule, 'state0', state0);
parameters = {};
paramsetter = PorositySetter(model, {ne, sep, pe});
getporo = @(model, notused) paramsetter.getValues(model);
setporo = @(model, notused, v) paramsetter.setValues(model, v);
parameters = addParameter(parameters, SimulatorSetup, ...
'name' , 'porosity', ...
'belongsTo', 'model' , ...
'boxLims' , [0.1, 0.9] , ...
'location' , {''} , ...
'getfun' , getporo , ...
'setfun' , setporo);
setfun = @(x, location, v) struct('Imax', v, ...
'src', @(time, I, E) rampupSwitchControl(time, model.Control.rampupTime, I, E, v, model.Control.lowerCutoffVoltage), ...
'stopFunction', schedule.control.stopFunction, ...
'CCDischarge', true);
parameters = addParameter(parameters, SimulatorSetup, ...
'name' , 'Imax' , ...
'belongsTo' , 'schedule' , ...
'boxLims' , model.Control.Imax*[0.5, 2], ...
'location' , {'control', 'Imax'} , ...
'getfun' , [] , ...
'setfun' , setfun);
%% Setup the objective function and auxiliary plotting
objmatch = @(model, states, schedule, varargin) EnergyOutput(model, states, schedule, varargin{:});
if doPlot
fn = @plotAfterStepIV;
else
fn = [];
end
obj = @(p) evalObjectiveBattmo(p, objmatch, SimulatorSetup, parameters, 'objScaling', totval, 'afterStepFn', fn);
%% Setup initial parameters
% The parameters must be scaled to [0,1]
p_base = getScaledParameterVector(SimulatorSetup, parameters);
p_base = p_base - 0.1;
%% Optimize
% Solve the optimization problem using BFGS. One can adjust the
% tolerances and the maxIt option to see how it effects the
% optimum.
[v, p_opt, history] = unitBoxBFGS(p_base, obj, 'gradTol', 1e-7, 'objChangeTol', 1e-4, 'maxIt', 20);
% Compute objective at optimum
setup_opt = updateSetupFromScaledParameters(SimulatorSetup, parameters, p_opt);
[~, states_opt, ~] = simulateScheduleAD(setup_opt.state0, setup_opt.model, setup_opt.schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls);
time_opt = cellfun(@(x) x.time, states_opt);
E_opt = cellfun(@(x) x.Control.E, states_opt);
I_opt = cellfun(@(x) x.Control.I, states_opt);
totval_trapz_opt = trapz(time_opt, E_opt.*I_opt);
% Print optimal parameters
fprintf('Base and optimized parameters:\n');
for k = 1:numel(parameters)
% Get the original and optimized values
p0 = parameters{k}.getParameter(SimulatorSetup);
pu = parameters{k}.getParameter(setup_opt);
% Print
fprintf('%s\n', parameters{k}.name);
fprintf('%g %g\n', p0, pu);
end
fprintf('Energy changed from %g to %g mWh\n', totval_trapz/hour/milli, totval_trapz_opt/hour/milli);
%% Plot
if doPlot
% Plot
figure; hold on; grid on
E = cellfun(@(x) x.Control.E, states);
time = cellfun(@(x) x.time, states);
plot(time/hour, E, '*-', 'displayname', 'initial');
plot(time_opt/hour, E_opt, 'r*-', 'displayname', 'optimized');
xlabel('time / h');
ylabel('voltage / V');
legend;
end
%%
doCompareGradient = false;
if doCompareGradient
p = getScaledParameterVector(SimulatorSetup, parameters);
[vad, gad] = evalObjectiveBattmo(p, objmatch, SimulatorSetup, parameters, 'gradientMethod', 'AdjointAD');
[vnum, gnum] = evalObjectiveBattmo(p, objmatch, SimulatorSetup, parameters, 'gradientMethod', 'PerturbationADNUM', 'PerturbationSize', 1e-5);
fprintf('Gradient computed using adjoint:\n');
display(gad);
fprintf('Numerical gradient:\n');
display(gnum);
fprintf('Relative error:\n')
display(abs(gnum-gad)./abs(gad));
end
%{
Copyright 2021-2024 SINTEF Industry, Sustainable Energy Technology
and SINTEF Digital, Mathematics & Cybernetics.
This file is part of The Battery Modeling Toolbox BattMo
BattMo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BattMo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BattMo. If not, see <http://www.gnu.org/licenses/>.
%}