forked from BattMoTeam/BattMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattery.m
1949 lines (1371 loc) · 68 KB
/
Battery.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef Battery < BaseModel
%
% The battery model consists of
%
% * an Electrolyte model given in :attr:`Electrolyte` property
% * a Negative Electrode Model given in :attr:`NegativeElectrode` property
% * a Positive Electrode Model given in :attr:`PositiveElectrode` property
% * a Thermal model given in :attr:`ThermalModel` property
%
properties
con = PhysicalConstants();
NegativeElectrode % Negative Electrode Model, instance of :class:`Electrode <Electrochemistry.Electrodes.Electrode>`
PositiveElectrode % Positive Electrode Model, instance of :class:`Electrode <Electrochemistry.Electrodes.Electrode>`
Electrolyte % Electrolyte model, instance of :class:`Electrolyte <Electrochemistry.Electrodes.Electrolyte>`
Separator % Separator model, instance of :class:`Separator <Electrochemistry.Electrodes.Separator>`
ThermalModel % Thermal model, instance of :class:`ThermalComponent <Electrochemistry.ThermalComponent>`
Control % Control Model
SOC % State Of Charge
initT % Initial temperature
couplingTerms % Coupling terms
cmin % mininum concentration used in capping
couplingNames
mappings
% flag that decide the model setup
use_thermal
include_current_collectors
equationIndices
end
methods
function model = Battery(inputparams)
model = model@BaseModel();
inputparams = inputparams.validateInputParams();
% All the submodels should have same backend (this is not assigned automaticallly for the moment)
model.AutoDiffBackend = SparseAutoDiffBackend('useBlocks', true);
%% Setup the model using the input parameters
fdnames = {'G' , ...
'couplingTerms' , ...
'initT' , ...
'use_thermal' , ...
'include_current_collectors', ...
'use_thermal' , ...
'SOC'};
model = dispatchParams(model, inputparams, fdnames);
model.NegativeElectrode = Electrode(inputparams.NegativeElectrode);
model.PositiveElectrode = Electrode(inputparams.PositiveElectrode);
model.Separator = Separator(inputparams.Separator);
% We setup the electrolyte model (in particular we compute the volume fraction from the other components)
model = model.setupElectrolyteModel(inputparams);
if model.use_thermal
model.ThermalModel = ThermalComponent(inputparams.ThermalModel);
end
model.Control = model.setupControl(inputparams.Control);
% define shorthands
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
am = 'ActiveMaterial';
cc = 'CurrentCollector';
am = 'ActiveMaterial';
itf = 'Interface';
thermal = 'ThermalModel';
if model.use_thermal
% setup Thermal Model by assigning the effective heat capacity and conductivity, which is computed from the sub-models.
model = model.setupThermalModel();
end
% setup couplingNames
model.couplingNames = cellfun(@(x) x.name, model.couplingTerms, 'uniformoutput', false);
% setup equations and variable names selected in the model
model = model.setupSelectedModel();
% setup some mappings (mappings from electrodes to electrolyte)
model = model.setupMappings();
% setup capping
model = model.setupCapping();
% setup computational graph
model = model.setupComputationalGraph();
model.funcCallList = model.computationalGraph.getOrderedFunctionCallList();
end
function model = setupSelectedModel(model, varargin)
% The system of equation should fullfill a special structure to fit into the iterative linear solver with
% preconditioner. We create this structure here.
opt = struct('reduction', []);
opt = merge_options(opt, varargin{:});
% For the reduction structure format, see battmodDir()/Utilities/JsonSchemas/linearsolver.schema.json and the reduction property
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
am = 'ActiveMaterial';
am1 = 'ActiveMaterial1';
am2 = 'ActiveMaterial2';
sd = 'SolidDiffusion';
cc = 'CurrentCollector';
ctrl = 'Control';
thermal = 'ThermalModel';
addedVarNames = {};
varEqTypes ={{elyte, 'c'} , {elyte, 'massCons'} , 'cell'; ...
{elyte, 'phi'} , {elyte, 'chargeCons'} , 'cell'; ...
{ne, co, 'phi'}, {ne, co, 'chargeCons'} , 'cell'; ...
{pe, co, 'phi'}, {pe, co, 'chargeCons'} , 'cell'; ...
{ctrl, 'E'} , {ctrl, 'EIequation'} , 'ctrl'; ...
{ctrl, 'I'} , {ctrl, 'controlEquation'}, 'ctrl'};
eldes = {ne, pe};
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
switch model.(elde).(co).active_material_type
case 'default'
ams = {am};
case 'composite'
ams = {am1, am2};
otherwise
error('active_material_type not recognized');
end
for iam = 1 : numel(ams)
amc = ams{iam};
switch model.(elde).(co).(amc).diffusionModelType
case 'simple'
newentries = {{elde, co, amc, sd, 'cAverage'}, {elde, co, amc, sd, 'massCons'} , 'cell'; ...
{elde, co, amc, sd, 'cSurface'}, {elde, co, amc, sd, 'solidDiffusionEq'}, 'cell'};
case 'full'
newentries = {{elde, co, amc, sd, 'c'} , {elde, co, amc, sd, 'massCons'} , 'cell'; ...
{elde, co, amc, sd, 'cSurface'}, {elde, co, amc, sd, 'solidDiffusionEq'}, 'cell'};
otherwise
error('diffusionModelType not recognized');
end
varEqTypes = vertcat(varEqTypes, newentries);
end
if model.include_current_collectors
newentries = {{elde, cc, 'phi'}, {elde, cc, 'chargeCons'}, 'cell'};
varEqTypes = vertcat(varEqTypes, newentries);
end
end
if model.use_thermal
newentries = {{thermal, 'T'}, {thermal, 'energyCons'}, 'cell'};
varEqTypes = vertcat(varEqTypes, newentries);
end
primaryVarNames = varEqTypes(:, 1);
equationTypes = varEqTypes(:, 3);
% The variable and equation lists are not a priori ordered (in the sense that we have 'cell' types first and
% the other types after). It is a requirement in some setup of the linear solver.
% Note : if you use a direct solver, this is not used.
variableReordered = false;
if ~isempty(opt.reduction)
reduc = opt.reduction;
% We set the type of the variable to be reduced as 'reduced' or 'specialReduced' (anything but 'cell')
% and we move them at the end of list respecting the order in which they have been given.
if ~isempty(reduc) && reduc.doReduction
neq = numel(equationTypes);
equationTypes = cell(neq, 1);
for ieqtype = 1 : neq
equationTypes{ieqtype} = 'cell';
end
variables = reduc.variables;
if isstruct(variables)
variables = num2cell(variables);
end
einds = nan(numel(variables),1);
for ivar = 1 : numel(variables)
var = variables{ivar};
[found, ind] = Battery.getVarIndex(var.name, primaryVarNames);
if ~found
error('variable to be reduce has not been found');
end
equationTypes{ind} = 'reduced';
if isfield(var, "special") && var.special
equationTypes{ind} = 'specialReduced';
end
einds(ivar) = ind;
order(ivar) = var.order;
end
[~, ind] = sort(order);
einds = einds(ind);
inds = (1 : neq)';
inds(einds) = [];
inds = [inds; einds];
variableReordered = true;
end
end
if ~variableReordered
% We reorder to get first the 'cells' type (required for reduction in iterative solver)
iscell = ismember(equationTypes, {'cell'});
inds = [find(iscell); find(~iscell)];
end
primaryVarNames = varEqTypes(inds, 1);
equationVarNames = varEqTypes(inds, 2);
equationTypes = equationTypes(inds);
% We use shortened names for easier visualisation and because Matlab also has a limitation on the lenght of
% a field name in a structure.
function str = setupName(varname)
shortvarname = cellfun(@(elt) Battery.shortenName(elt), varname, 'uniformoutput', false);
str = Battery.varToStr(shortvarname);
end
equationNames = cellfun(@(varname) setupName(varname), equationVarNames, 'uniformoutput', false);
equationIndices = struct();
for ieq = 1 : numel(equationNames)
equationIndices.(equationNames{ieq}) = ieq;
end
model.primaryVarNames = primaryVarNames;
model.equationVarNames = equationVarNames;
model.equationNames = equationNames;
model.equationTypes = equationTypes;
model.equationIndices = equationIndices;
end
function model = registerVarAndPropfuncNames(model)
%% Declaration of the Dynamical Variables and Function of the model
% (setup of varnameList and propertyFunctionList)
model = registerVarAndPropfuncNames@BaseModel(model);
% defines shorthands for the submodels
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
am = 'ActiveMaterial';
am1 = 'ActiveMaterial1';
am2 = 'ActiveMaterial2';
cc = 'CurrentCollector';
itf = 'Interface';
sd = 'SolidDiffusion';
thermal = 'ThermalModel';
ctrl = 'Control';
if ~model.use_thermal
% we register the temperature variable, as it is not done by the ThermalModel which is empty in this case
model = model.registerVarName({thermal, 'T'});
end
eldes = {ne, pe};
%% Temperature dispatch functions
fn = @Battery.updateTemperature;
inputnames = {{thermal, 'T'}};
model = model.registerPropFunction({{elyte, 'T'} , fn, inputnames});
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
model = model.registerPropFunction({{elde, co, 'T'} , fn, inputnames});
end
%% Coupling functions
% Dispatch electrolyte concentration and potential in the electrodes
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
switch model.(elde).(co).active_material_type
case 'default'
ams = {am};
case 'composite'
ams = {am1, am2};
otherwise
error('active_material_type not recognized');
end
for iam = 1 : numel(ams)
amc = ams{iam};
fn = @Battery.updateElectrodeCoupling;
inputnames = {{elyte, 'c'}, ...
{elyte, 'phi'}};
model = model.registerPropFunction({{elde, co, amc, itf, 'phiElectrolyte'}, fn, inputnames});
model = model.registerPropFunction({{elde, co, amc, itf, 'cElectrolyte'} , fn, inputnames});
end
end
fn = @Battery.updateElectrolyteCoupling;
inputnames = {{ne, co, 'eSource'}, ...
{pe, co, 'eSource'}};
model = model.registerPropFunction({{elyte, 'massSource'}, fn, inputnames});
model = model.registerPropFunction({{elyte, 'eSource'}, fn, inputnames});
% Function that assemble the control equation
fn = @Battery.setupEIEquation;
inputnames = {{ctrl, 'E'}, ...
{ctrl, 'I'}};
if model.include_current_collectors
inputnames{end + 1} = {pe, cc, 'phi'};
else
inputnames{end + 1} = {pe, co, 'phi'};
end
model = model.registerPropFunction({{ctrl, 'EIequation'}, fn, inputnames});
inputnames = {};
fn = @Battery.updateControl;
fn = {fn, @(propfunction) PropFunction.drivingForceFuncCallSetupFn(propfunction)};
switch model.(ctrl).controlPolicy
case {'CCDischarge', 'CCCharge', 'CC'}
model = model.registerPropFunction({{ctrl, 'ctrlVal'}, fn, inputnames});
case {'CCCV'}
% do nothing
otherwise
error('controlPolicy not recognized');
end
model = model.registerPropFunction({{ctrl, 'ctrlType'}, fn, inputnames});
%% Function that update the Thermal Ohmic Terms
if model.use_thermal
fn = @Battery.updateThermalOhmicSourceTerms;
inputnames = {{elyte, 'jFace'} , ...
{ne, co, 'jFace'} , ...
{pe, co, 'jFace'} , ...
{elyte, 'conductivity'} , ...
{ne, co, 'conductivity'}, ...
{pe, co, 'conductivity'}};
if model.include_current_collectors
varnames ={{ne, cc, 'jFace'} , ...
{pe, cc, 'jFace'} , ...
{ne, cc, 'conductivity'}, ...
{pe, cc, 'conductivity'}};
inputnames = horzcat(inputnames, varnames);
end
model = model.registerPropFunction({{thermal, 'jHeatOhmSource'}, fn, inputnames});
%% Function that updates the Thermal Chemical Terms
fn = @Battery.updateThermalChemicalSourceTerms;
inputnames = {{elyte, 'diffFlux'}, ...
{elyte, 'D'} , ...
VarName({elyte}, 'dmudcs', 2)};
model = model.registerPropFunction({{thermal, 'jHeatChemicalSource'}, fn, inputnames});
%% Function that updates Thermal Reaction Terms
fn = @Battery.updateThermalReactionSourceTerms;
inputnames = {{thermal, 'T'} , ...
{ne, co, am, sd, 'Rvol'} , ...
{ne, co, am, itf, 'eta'} , ...
{ne, co, am, itf, 'dUdT'}, ...
{pe, co, am, sd, 'Rvol'} , ...
{pe, co, am, itf, 'eta'} , ...
{pe, co, am, itf, 'dUdT'}};
model = model.registerPropFunction({{thermal, 'jHeatReactionSource'}, fn, inputnames});
else
model = model.removeVarName({elyte, 'diffFlux'});
model = model.removeVarName({ne, co, am, itf, 'dUdT'});
model = model.removeVarName({pe, co, am, itf, 'dUdT'});
end
%% Functions that setup external coupling for negative electrode
fns{1} = @Battery.setupExternalCouplingNegativeElectrode;
fns{2} = @Battery.setupExternalCouplingPositiveElectrode;
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
fn = fns{ielde};
if model.include_current_collectors
inputnames = {{elde, cc, 'phi'}, ...
{elde, cc, 'conductivity'}};
model = model.registerPropFunction({{elde, cc, 'jExternal'}, fn, inputnames});
if model.use_thermal
model = model.registerPropFunction({{elde, cc, 'jFaceExternal'}, fn, inputnames});
end
inputnames = {{elde, co, 'phi'}, ...
{elde, co, 'conductivity'}};
model = model.registerPropFunction({{elde, co, 'jExternal'}, fn, inputnames});
else
inputnames = {{elde, co, 'phi'}, ...
{elde, co, 'conductivity'}};
model = model.registerPropFunction({{elde, co, 'jExternal'}, fn, inputnames});
if model.use_thermal
model = model.registerPropFunction({{elde, co, 'jFaceExternal'}, fn, inputnames});
end
end
end
%% Declare the "static" variables
varnames = {};
if ~model.use_thermal
varnames{end + 1} = {thermal, 'T'};
end
model = model.setAsStaticVarNames(varnames);
end
function control = setupControl(model, inputparams)
C = computeCellCapacity(model);
switch inputparams.controlPolicy
case "CCDischarge"
control = CCDischargeControlModel(inputparams);
CRate = control.CRate;
control.Imax = (C/hour)*CRate;
case 'CCCharge'
control = CCChargeControlModel(inputparams);
CRate = control.CRate;
control.Imax = (C/hour)*CRate;
case "CCCV"
control = CcCvControlModel(inputparams);
CRate = control.CRate;
control.Imax = (C/hour)*CRate;
case "powerControl"
control = PowerControlModel(inputparams);
case "CC"
control = CCcontrolModel(inputparams);
otherwise
error('Error controlPolicy not recognized');
end
end
function model = setupThermalModel(model, inputparams)
% Setup the thermal model :attr:`ThermalModel`. Here, :code:`inputparams` is instance of
% :class:`ThermalComponentInputParams <Electrochemistry.ThermalComponentInputParams>`
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
am = 'ActiveMaterial';
cc = 'CurrentCollector';
elyte = 'Electrolyte';
sep = 'Separator';
thermal = 'ThermalModel';
eldes = {ne, pe}; % electrodes
G = model.G;
nc = G.getNumberOfCells();
vhcap = zeros(nc, 1); % effective heat capacity
hcond = zeros(nc, 1); % effective heat conductivity
for ind = 1 : numel(eldes)
elde = eldes{ind};
if model.include_current_collectors
% The effecive and intrinsic thermal parameters for the current collector are the same.
cc_map = model.(elde).(cc).G.mappings.cellmap;
cc_hcond = model.(elde).(cc).effectiveThermalConductivity;
cc_vhcap = model.(elde).(cc).effectiveVolumetricHeatCapacity;
hcond(cc_map) = hcond(cc_map) + cc_hcond;
vhcap(cc_map) = vhcap(cc_map) + cc_vhcap;
end
% Effective parameters from the Electrode Active Component region.
co_map = model.(elde).(co).G.mappings.cellmap;
co_hcond = model.(elde).(co).effectiveThermalConductivity;
co_vhcap = model.(elde).(co).effectiveVolumetricHeatCapacity;
hcond(co_map) = hcond(co_map) + co_hcond;
vhcap(co_map) = vhcap(co_map) + co_vhcap;
end
% Electrolyte
elyte_map = model.(elyte).G.mappings.cellmap;
elyte_hcond = model.(elyte).effectiveThermalConductivity;
elyte_vhcap = model.(elyte).effectiveVolumetricHeatCapacity;
vhcap(elyte_map) = vhcap(elyte_map) + elyte_vhcap;
hcond(elyte_map) = hcond(elyte_map) + elyte_hcond;
% Separator
sep_map = model.(sep).G.mappings.cellmap;
sep_hcond = model.(sep).effectiveThermalConductivity;
sep_vhcap = model.(sep).effectiveVolumetricHeatCapacity;
vhcap(sep_map) = vhcap(sep_map) + sep_vhcap;
hcond(sep_map) = hcond(sep_map) + sep_hcond;
% Assign values
model.(thermal).effectiveVolumetricHeatCapacity = vhcap;
model.(thermal).effectiveThermalConductivity = hcond;
end
function model = setupMappings(model)
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
cc = 'CurrentCollector';
elyte = 'Electrolyte';
G_elyte = model.(elyte).G;
elytecelltbl.cells = (1 : G_elyte.getNumberOfCells())';
elytecelltbl.globalcells = G_elyte.mappings.cellmap;
elytecelltbl = IndexArray(elytecelltbl);
eldes = {ne, pe};
for ind = 1 : numel(eldes)
elde = eldes{ind};
G_elde = model.(elde).(co).G;
clear eldecelltbl;
eldecelltbl.cells = (1 : G_elde.getNumberOfCells())';
eldecelltbl.globalcells = G_elde.mappings.cellmap;
eldecelltbl = IndexArray(eldecelltbl);
map = TensorMap();
map.fromTbl = elytecelltbl;
map.toTbl = eldecelltbl;
map.replaceFromTblfds = {{'cells', 'elytecells'}};
map.replaceToTblfds = {{'cells', 'eldecells'}};
map.mergefds = {'globalcells'};
mappings.(elde) = map.getDispatchInd();
end
model.mappings = mappings;
end
function model = setupElectrolyteModel(model, inputparams)
% Assign the electrolyte volume fractions in the different regions
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
sep = 'Separator';
elyte_cells = zeros(model.G.getNumberOfCells(), 1);
elyte_cells(inputparams.(elyte).G.mappings.cellmap) = (1 : inputparams.(elyte).G.getNumberOfCells())';
inputparams.(elyte).volumeFraction = ones(inputparams.(elyte).G.getNumberOfCells(), 1);
inputparams.(elyte).volumeFraction = subsasgnAD(inputparams.(elyte).volumeFraction, elyte_cells(model.(ne).(co).G.mappings.cellmap), 1 - model.(ne).(co).volumeFraction);
inputparams.(elyte).volumeFraction = subsasgnAD(inputparams.(elyte).volumeFraction, elyte_cells(model.(pe).(co).G.mappings.cellmap), 1 - model.(pe).(co).volumeFraction);
inputparams.(elyte).volumeFraction = subsasgnAD(inputparams.(elyte).volumeFraction, elyte_cells(model.(sep).G.mappings.cellmap), model.(sep).porosity);
model.(elyte) = Electrolyte(inputparams.(elyte));
end
function initstate = setupInitialState(model, jsonstruct)
% Setup the values of the primary variables at initial state
%
% The jsonstruct structure (standard matlab struct type) contains some parameters for the initialization. For
% the moment, it includes only the initial electrolyte concentration
%
if nargin < 2 | isempty(jsonstruct)
jsonstruct.Electrolyte.initialConcentration = model.Electrolyte.species.nominalConcentration;
end
nc = model.G.getNumberOfCells();
SOC = model.SOC;
T = model.initT;
bat = model;
elyte = 'Electrolyte';
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
am = 'ActiveMaterial';
am1 = 'ActiveMaterial1';
am2 = 'ActiveMaterial2';
itf = 'Interface';
sd = 'SolidDiffusion';
cc = 'CurrentCollector';
thermal = 'ThermalModel';
ctrl = 'Control';
initstate.(thermal).T = T*ones(nc, 1);
%% Synchronize temperatures
initstate = model.updateTemperature(initstate);
%% Setup initial state for electrodes
eldes = {ne, pe};
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
switch model.(elde).(co).active_material_type
case 'default'
ams = {am};
case 'composite'
ams = {am1, am2};
otherwise
error('active_material_type not recognized');
end
for iam = 1 : numel(ams)
amc = ams{iam};
elde_itf = bat.(elde).(co).(amc).(itf);
theta = SOC*(elde_itf.guestStoichiometry100 - elde_itf.guestStoichiometry0) + elde_itf.guestStoichiometry0;
c = theta*elde_itf.saturationConcentration;
nc = model.(elde).(co).G.getNumberOfCells();
switch model.(elde).(co).(amc).diffusionModelType
case 'simple'
initstate.(elde).(co).(amc).(sd).cSurface = c*ones(nc, 1);
initstate.(elde).(co).(amc).(sd).cAverage = c*ones(nc, 1);
case 'full'
initstate.(elde).(co).(amc).(sd).cSurface = c*ones(nc, 1);
N = model.(elde).(co).(amc).(sd).N;
np = model.(elde).(co).(amc).(sd).np; % Note : we have by construction np = nc
initstate.(elde).(co).(amc).(sd).c = c*ones(N*np, 1);
otherwise
error('diffusionModelType not recognized')
end
end
% In case of two material, we choose first material for the initial OCP
amc = ams{1};
initstate = model.evalVarName(initstate, {elde, co, amc, itf, 'OCP'});
OCP = initstate.(elde).(co).(amc).(itf).OCP;
if ielde == 1
% The value in the first cell is used as reference.
ref = OCP(1);
end
initstate.(elde).(co).phi = OCP - ref;
end
%% Setup initial Electrolyte state
initstate.(elyte).phi = zeros(bat.(elyte).G.getNumberOfCells(), 1) - ref;
initstate.(elyte).c = jsonstruct.Electrolyte.initialConcentration*ones(bat.(elyte).G.getNumberOfCells(), 1);
%% Setup initial Current collectors state
if model.(ne).include_current_collectors
OCP = initstate.(ne).(co).(amc).(itf).OCP;
OCP = OCP(1) .* ones(bat.(ne).(cc).G.getNumberOfCells(), 1);
initstate.(ne).(cc).phi = OCP - ref;
end
if model.(pe).include_current_collectors
OCP = initstate.(pe).(co).(amc).(itf).OCP;
OCP = OCP(1) .* ones(bat.(pe).(cc).G.getNumberOfCells(), 1);
initstate.(pe).(cc).phi = OCP - ref;
end
initstate.(ctrl).E = OCP(1) - ref;
switch model.(ctrl).controlPolicy
case {'CCDischarge', 'CCCharge'}
initstate.(ctrl).ctrlType = 'constantCurrent';
initstate.(ctrl).I = model.(ctrl).Imax;
case 'CC'
initstate.(ctrl).ctrlType = 'constantCurrent';
initstate.(ctrl).I = 0;
case 'CCCV'
initstate.(ctrl).numberOfCycles = 0;
switch model.(ctrl).initialControl
case 'discharging'
initstate.(ctrl).ctrlType = 'CC_discharge1';
initstate.(ctrl).nextCtrlType = 'CC_discharge1';
initstate.(ctrl).I = model.(ctrl).Imax;
case 'charging'
initstate.(ctrl).ctrlType = 'CC_charge1';
initstate.(ctrl).nextCtrlType = 'CC_charge1';
initstate.(ctrl).I = - model.(ctrl).Imax;
otherwise
error('initialControl not recognized');
end
case 'powerControl'
switch model.(ctrl).initialControl
case 'discharging'
error('to implement (should be easy...)')
case 'charging'
initstate.(ctrl).ctrlType = 'charge';
E = initstate.(ctrl).E;
P = model.(ctrl).chargingPower;
initstate.(ctrl).I = -P/E;
otherwise
error('initialControl not recognized');
end
case 'CC'
% this value will be overwritten after first iteration
initstate.(ctrl).I = 0;
switch model.(ctrl).initialControl
case 'discharging'
initstate.(ctrl).ctrlType = 'discharge';
case 'charging'
initstate.(ctrl).ctrlType = 'charge';
otherwise
error('initialControl not recognized');
end
otherwise
error('control policy not recognized');
end
initstate.time = 0;
end
function state = addVariables(model, state)
% Given a state where only the primary variables are defined, this
% functions add all the additional variables that are computed in the assembly process and have some physical
% interpretation.
%
% To do so, we use getEquations function and sends dummy variable for state0, dt and drivingForces
% Values that need to be set to get the function getEquations running
dt = 1;
state0 = state;
inputparams = ControlModelInputParams([]);
model.Control = ControlModel(inputparams);
model.Control.controlPolicy = 'None';
drivingForces = model.getValidDrivingForces();
% We call getEquations to update state
[~, state] = getEquations(model, state0, state, dt, drivingForces, 'ResOnly', true);
% We set to empty the fields we know that are not meaningfull
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
am = 'ActiveMaterial';
co = 'Coating';
cc = 'CurrentCollector';
elyte = 'Electrolyte';
am1 = 'ActiveMaterial1';
am2 = 'ActiveMaterial2';
itf = 'Interface';
sd = "SolidDiffusion";
thermal = 'ThermalModel';
ctrl = 'Control';
state.(elyte).massAccum = [];
state.(elyte).massCons = [];
eldes = {ne, pe};
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
switch model.(elde).(co).active_material_type
case 'default'
ams = {am};
case 'composite'
ams = {am1, am2};
otherwise
error('active_material_type not recognized');
end
for iam = 1 : numel(ams)
amc = ams{iam};
state.(elde).(co).(amc).(sd).massAccum = [];
state.(elde).(co).(amc).(sd).massCons = [];
end
state = model.evalVarName(state, {elde, co, 'SOC'});
end
if model.use_thermal
state = model.updateThermalIrreversibleReactionSourceTerms(state);
state = model.updateThermalReversibleReactionSourceTerms(state);
end
end
function [problem, state] = getEquations(model, state0, state, dt, drivingForces, varargin)
% Assembly of the governing equation
opts = struct('ResOnly', false, 'iteration', 0, 'reverseMode', false);
opts = merge_options(opts, varargin{:});
time = state0.time + dt;
if(not(opts.ResOnly) && not(opts.reverseMode))
state = model.initStateAD(state);
elseif(opts.reverseMode)
dispif(mrstVerbose, 'No AD initialization in equation old style')
state0 = model.initStateAD(state0);
else
assert(opts.ResOnly);
end
%% We call the assembly equations ordered from the graph
funcCallList = model.funcCallList;
for ifunc = 1 : numel(funcCallList)
eval(funcCallList{ifunc});
end
%% We apply some scaling
% Shorthands used in this function
battery = model;
ne = 'NegativeElectrode';
pe = 'PositiveElectrode';
co = 'Coating';
am = 'ActiveMaterial';
am1 = 'ActiveMaterial1';
am2 = 'ActiveMaterial2';
elyte = 'Electrolyte';
am = 'ActiveMaterial';
itf = 'Interface';
sd = "SolidDiffusion";
thermal = 'ThermalModel';
ctrl = 'Control';
eldes = {ne, pe};
massConsScaling = model.con.F;
state.(elyte).massCons = state.(elyte).massCons*massConsScaling;
for ielde = 1 : numel(eldes)
elde = eldes{ielde};
switch model.(elde).(co).active_material_type
case 'default'
ams = {am};
case 'composite'
ams = {am1, am2};
otherwise
error('active_material_type not recognized');
end
for iam = 1 : numel(ams)
amc = ams{iam};
switch model.(elde).(co).(amc).diffusionModelType
case 'simple'
state.(elde).(co).(amc).(sd).massCons = massConsScaling*state.(elde).(co).(amc).(sd).massCons;
state.(elde).(co).(amc).(sd).solidDiffusionEq = massConsScaling.*battery.(elde).(co).G.getVolumes()/dt.*state.(elde).(co).(amc).(sd).solidDiffusionEq;
case 'full'
n = model.(elde).(co).(amc).(itf).numberOfElectronsTransferred;
F = model.con.F;
vol = model.(elde).(co).G.getVolumes();
rp = model.(elde).(co).(amc).(sd).particleRadius;
vsf = model.(elde).(co).(amc).(sd).volumetricSurfaceArea;
surfp = 4*pi*rp^2;
scalingcoef = (vsf*vol(1)*n*F)/surfp;
state.(elde).(co).(amc).(sd).massCons = scalingcoef*state.(elde).(co).(amc).(sd).massCons;
state.(elde).(co).(amc).(sd).solidDiffusionEq = scalingcoef*state.(elde).(co).(amc).(sd).solidDiffusionEq;
otherwise
error('diffusionModelType not recognized');
end
end
end
for ieq = 1 : numel(model.equationVarNames)
eqs{ieq} = model.getProp(state, model.equationVarNames{ieq});
end
ei = model.equationIndices;
% By doing this linear transformation, we remove the direct dependency of the mass conservation equation with
% respect to the potential gradien. Only the concentration gradient remains in the equation. This is a special
% property when the transference t is a constant.
mieq = ei.(Battery.varToStr({'elyte', 'massCons'}));