-
Notifications
You must be signed in to change notification settings - Fork 10
/
runChen2020.m
196 lines (144 loc) · 6.37 KB
/
runChen2020.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
%% Chen model
% Include presentation of the test case (use rst format)
% clear the workspace and close open figures
clear
close all
% load MRST modules
mrstModule add ad-core mrst-gui mpfa
% Useful abbreviations
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
cc = 'CurrentCollector';
am = 'ActiveMaterial';
itf = 'Interface';
sd = 'SolidDiffusion';
thermal = 'ThermalModel';
% We create an instance of BatteryInputParams. This class is used to initiate the battery simulator and it propagates
% all the parameters through out the submodels.
% The input parameters can be given in json format. The json file is read and used to populate the inputparams object.
jsonstruct_material = parseBattmoJson(fullfile('ParameterData','ParameterSets','Chen2020','chen2020_lithium_ion_battery.json'));
jsonstruct_geometry = parseBattmoJson(fullfile('Examples', 'JsonDataFiles', 'geometryChen.json'));
jsonstruct = mergeJsonStructs({jsonstruct_material, ...
jsonstruct_geometry});
inputparams = BatteryInputParams(jsonstruct);
%% We setup the battery geometry ("bare" battery with no current collector).
[inputparams, gen] = setupBatteryGridFromJson(inputparams, jsonstruct);
%% The Battery model is initialized by sending inputparams to the Battery class constructor
model = Battery(inputparams);
%% We fix the input current to 5A
model.Control.Imax = 5*ampere;
%% We setup the schedule
% We use different time step for the activation phase (small time steps) and the following discharging phase
% We start with rampup time steps to go through the activation phase
fac = 2;
total = 1.4*hour;
n = 100;
dt0 = total*1e-6;
times = getTimeSteps(dt0, n, total, fac);
dt = diff(times);
dt = dt(1 : end);
step = struct('val', dt, 'control', ones(size(dt)));
% we setup the control for the schedule
control = model.Control.setupScheduleControl();
% This control is used to set up the schedule
schedule = struct('control', control, 'step', step);
%% We setup the initial state
c_ne = 29.866*mol/litre; % initial concentration at negative electrode
c_pe = 17.038*mol/litre; % initial concentration at positive electrode
initstate = initStateChen2020(model, c_ne, c_pe);
%% We setup the solver
% Default values would typically work.
nls = NonLinearSolver();
% Change default maximum iteration number in nonlinear solver
nls.maxIterations = 10;
% Change default behavior of nonlinear solver, in case of error
nls.errorOnFailure = false;
linearsolver = 'direct';
switch linearsolver
case 'agmg'
mrstModule add agmg
nls.LinearSolver = AGMGSolverAD('verbose', true, 'reduceToCell', true);
nls.LinearSolver.tolerance = 1e-3;
nls.LinearSolver.maxIterations = 30;
nls.maxIterations = 10;
nls.verbose = 10;
case 'battery'
nls.LinearSolver = LinearSolverBatteryExtra('verbose', false, 'reduceToCell', false, 'verbosity', 3, 'reuse_setup', false, 'method', 'direct');
nls.LinearSolver.tolerance=0.5e-4*2;
case 'direct'
% nothing to setup in this case
otherwise
error('linear solver not recognized.');
end
% Change default tolerance for nonlinear solver
model.nonlinearTolerance = 1e-5;
% Set verbosity
model.verbose = false;
%% We run simulation
[~, states, ~] = simulateScheduleAD(initstate, model, schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls);
% We want to consider the simplified diffusion model and therefore setup a model that can run it.
jsonstruct.(ne).(co).(am).diffusionModelType = 'simple';
jsonstruct.(pe).(co).(am).diffusionModelType = 'simple';
inputparams = BatteryInputParams(jsonstruct);
inputparams = gen.updateBatteryInputParams(inputparams);
model2 = Battery(inputparams);
% setup initial state (the variables are different for the simplified model and therfore the initialization is not the same)
initstate2 = initStateChen2020(model2, c_ne, c_pe);
% Run simulation
[~, states2, ~] = simulateScheduleAD(initstate2, model2, schedule, 'OutputMinisteps', true, 'NonLinearSolver', nls);
%% We process output and recover the output voltage and current from the output states.
ind = cellfun(@(x) not(isempty(x)), states);
states = states(ind);
E1 = cellfun(@(state) state.Control.E, states);
I1 = cellfun(@(state) state.Control.I, states);
time1 = cellfun(@(state) state.time, states);
time1 = time1/hour;
ind = cellfun(@(x) not(isempty(x)), states2);
states2 = states2(ind);
E2 = cellfun(@(state) state.Control.E, states2);
I2 = cellfun(@(state) state.Control.I, states2);
time2 = cellfun(@(state) state.time, states2);
time2 = time2/hour;
%% We plot the the output voltage and current and compare with PyBaMM results
% We load the PyBaMM results
loadChenPybammSolution
[t1, u1] = deal(t, u);
[t2, u2] = deal(t_infdiff, u_infdiff);
doplot = false;
if doplot
% We plot the solutions
l = lines(4);
figure
hold on
plot(t1, u1, 'linewidth', 3, 'color', l(1, :), 'linestyle', '--', 'displayname', 'pybamm - solid diffusion')
plot(t2, u2, 'linewidth', 3, 'color', l(2, :), 'linestyle', '--', 'displayname', 'pybamm - instantaneous solid diffusion')
plot(time1, E1,'-', 'linewidth', 3, 'color', l(3, :), 'displayname', 'battmo')
plot(time2, E2,'-', 'linewidth', 3, 'color', l(4, :), 'displayname', 'battmo - simplified diffusion model')
set(gca, 'fontsize', 18);
title('Cell Voltage / V')
xlabel('time (hours)')
legend('fontsize', 18, 'location', 'southwest')
end
% Make sure BattMo and PyBaMM match
N = 1000;
x = linspace(time1(1), t1(end), N);
pb = interp1(t1, u1, x);
battmo = interp1(time1, E1, x);
assert(norm(pb - battmo) / norm(pb) < 1e-3);
%{
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/>.
%}