-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIPmodel_noiteration_d.m
More file actions
1088 lines (832 loc) · 43.4 KB
/
IPmodel_noiteration_d.m
File metadata and controls
1088 lines (832 loc) · 43.4 KB
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 IPmodel_noiteration_d < matlab.System & matlab.system.mixin.Propagates
% Integrated Power-based battery model: Matlab System block implementation. Note that voltage curves should be specified in three columns representing the following: C-rate, Ampere-hour content (Ah in Ah), Battery/Cell Voltage (in Volts). Rows corresponding to the same C-rate should appear in contiguous chronological order.
% Public, tunable properties
properties
end
properties(Nontunable)
v_charging_filename='v_curve_charging_LTO.csv'; % CSV File containing constant-current charging voltage curves
v_discharging_filename='v_curve_discharging_LTO.csv'; % CSV File containing constant-current discharging voltage curves
nominal_capacity=30; % Nominal capacity (Ampere-hours)
R_i=0.002; % Internal impedance (Ohms)
max_charging_current=150; % Maximum charging current (Amperes)
max_discharging_current=150; % Maximum discharging current (Amperes)
initial_energy_content=41.3; % Initial energy content (Wh)
time_step=1; % simulation time-step duration (hours)
StringSetProperty='Throw BMS exceptions'; % BMS option
end
properties(Hidden, Constant)
StringSetPropertySet = matlab.system.StringSet({'Throw BMS exceptions', 'No exceptions, apply BMS power suggestion automatically'});
end
properties(DiscreteState)
V; % voltage
I; % current
b; % energy content
end
% Pre-computed constants
properties(Access = public)
a_1;
a_2;
M_function;
eff_c;
eff_d;
alpha_c;
alpha_d;
Vnom_c;
Vnom_d;
v_prev;
b_prev;
tolerance;
V_min;
V_max;
throw_exception;
b_grid;
c_grid;
v_grid;
end
methods(Access = protected)
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
% Read the voltage files, and split them according to C-rate.
% coder.extrinsic('-sync:on','csvread');
charging_voltages = csvread(obj.v_charging_filename);
num_charging_points = double(size(charging_voltages, 1));
discharging_voltages = csvread(obj.v_discharging_filename);
num_discharging_points = double(size(discharging_voltages, 1));
% Step 1: clean charging and discharging data by removing duplicates and
% sorting.
unique_charging_rates = [];
unique_charging_rate_indices = [];
unique_discharging_rates = [];
unique_discharging_rate_indices = [];
for i=1:num_charging_points
% identify unique c-rates
if ~any(charging_voltages(i,1)==unique_charging_rates)
unique_charging_rates = [unique_charging_rates, charging_voltages(i)];
unique_charging_rate_indices = [unique_charging_rate_indices, i];
end
end
num_unique_charging_rates = length(unique_charging_rates);
% sort the values and remove duplicates in the Ah dimension
new_charging_voltages = [];
for i=1:num_unique_charging_rates
start_index = unique_charging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_charging_rates)
end_index = length(charging_voltages);
else
end_index = unique_charging_rate_indices(i+1)-1;
end
voltage_curve = charging_voltages(start_index:end_index,3);
[ah_curve, order1, order2] = unique(charging_voltages(start_index:end_index,2));
voltage_curve = voltage_curve(order1);
new_charging_voltages = [new_charging_voltages;[ones(length(ah_curve),1)*unique_charging_rates(i),ah_curve, voltage_curve]];
end
charging_voltages = new_charging_voltages;
num_charging_points = size(charging_voltages, 1);
for i=1:num_discharging_points
% identify unique c-rates
if ~any(discharging_voltages(i,1)==unique_discharging_rates)
unique_discharging_rates = [unique_discharging_rates, discharging_voltages(i)];
unique_discharging_rate_indices = [unique_discharging_rate_indices, i];
end
end
num_unique_discharging_rates = length(unique_discharging_rates);
% sort the values and remove duplicates in the Ah dimension
new_discharging_voltages = [];
for i=1:num_unique_discharging_rates
start_index = unique_discharging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_discharging_rates)
end_index = length(discharging_voltages);
else
end_index = unique_discharging_rate_indices(i+1)-1;
end
voltage_curve = discharging_voltages(start_index:end_index,3);
[ah_curve, order1, order2] = unique(discharging_voltages(start_index:end_index,2));
voltage_curve = voltage_curve(order1);
new_discharging_voltages = [new_discharging_voltages;[ones(length(ah_curve),1)*unique_discharging_rates(i),ah_curve, voltage_curve]];
end
discharging_voltages = new_discharging_voltages;
num_discharging_points = size(discharging_voltages, 1);
% Step 2: redo the calculation of unique indices and rates on the cleaned charging
% and discharging data
unique_charging_rates = [];
unique_charging_rate_indices = [];
unique_discharging_rates = [];
unique_discharging_rate_indices = [];
for i=1:num_charging_points
% identify unique c-rates
if ~any(charging_voltages(i,1)==unique_charging_rates)
unique_charging_rates = [unique_charging_rates, charging_voltages(i)];
unique_charging_rate_indices = [unique_charging_rate_indices, i];
end
end
num_unique_charging_rates = length(unique_charging_rates);
for i=1:num_discharging_points
% identify unique c-rates
if ~any(discharging_voltages(i,1)==unique_discharging_rates)
unique_discharging_rates = [unique_discharging_rates, discharging_voltages(i)];
unique_discharging_rate_indices = [unique_discharging_rate_indices, i];
end
end
num_unique_discharging_rates = length(unique_discharging_rates);
% Step 3: get nominal voltages corresponding to each charging and
% discharging rate. This is done by calculating the mean of each
% voltage curve. The voltage curve is interpolated evenly
% before calculating the mean, to avoid sample bias.
obj.Vnom_c = zeros(2, num_unique_charging_rates);
obj.Vnom_d = zeros(2, num_unique_discharging_rates);
for i=1:num_unique_charging_rates
start_index = unique_charging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_charging_rates)
end_index = length(charging_voltages);
else
end_index = unique_charging_rate_indices(i+1)-1;
end
voltage_curve = charging_voltages(start_index:end_index,3);
ah_curve = charging_voltages(start_index:end_index,2);
min_ah = min(ah_curve);
max_ah = max(ah_curve);
interpolation_distance = (max_ah-min_ah)/100;
obj.Vnom_c(1,i) = mean(interp1(ah_curve, voltage_curve, min_ah:interpolation_distance:max_ah));
obj.Vnom_c(2,i) = unique_charging_rates(i)*obj.nominal_capacity;
end
for i=1:num_unique_discharging_rates
start_index = unique_discharging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_discharging_rates)
end_index = length(discharging_voltages);
else
end_index = unique_discharging_rate_indices(i+1)-1;
end
% get the voltage and Ah curves, removing any duplicates in the Ah
% dimension.
voltage_curve = discharging_voltages(start_index:end_index,3);
ah_curve = discharging_voltages(start_index:end_index,2);
min_ah = min(ah_curve);
max_ah = max(ah_curve);
interpolation_distance = (max_ah-min_ah)/100;
obj.Vnom_d(1,i) = mean(interp1(ah_curve, voltage_curve, min_ah:interpolation_distance:max_ah));
obj.Vnom_d(2,i) = -unique_discharging_rates(i)*obj.nominal_capacity;
end
% Step 4: calculate the efficiency
obj.eff_c = zeros(2, num_unique_charging_rates);
obj.eff_d = zeros(2, num_unique_discharging_rates);
for i=1:num_unique_charging_rates
obj.eff_c(1,i) = 1 - ((obj.R_i*(unique_charging_rates(i)*obj.nominal_capacity))/obj.Vnom_c(1,i));
obj.eff_c(2,i) = unique_charging_rates(i)*obj.nominal_capacity;
end
for i=1:num_unique_discharging_rates
obj.eff_d(1,i) = 1 - ((obj.R_i*(unique_discharging_rates(i)*obj.nominal_capacity))/obj.Vnom_d(1,i));
obj.eff_d(2,i) = -unique_discharging_rates(i)*obj.nominal_capacity;
end
% Step 5: calculate energy content. This is done by taking the riemann
% sum to approximate the area under the voltage curve, and
% taking the efficiency into account. At the same time,
% calculate the energy content limits, which is the maximum
% energy we could charge with or the energy left in the battery
% when discharging.
E_c = zeros(num_charging_points,1);
E_d = zeros(num_discharging_points,1);
obj.a_2 = zeros(2, num_unique_charging_rates);
obj.a_1 = zeros(2, num_unique_discharging_rates);
figure;
eff_cs = zeros(num_charging_points, 1);
eff_cs_avg = zeros(2, num_unique_charging_rates);
for i=1:num_unique_charging_rates
start_index = unique_charging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_charging_rates)
end_index = length(charging_voltages);
else
end_index = unique_charging_rate_indices(i+1)-1;
end
E_c(start_index) = 0;
% "stretch" the ah_difference to make up for voltage
% curves that start at a non-zero Ah count.
stretch_factor = 1 + (charging_voltages(start_index,2)/charging_voltages(end_index,2));
for j=(start_index+1):end_index;
current = unique_charging_rates(i)*obj.nominal_capacity;
effc = eff_charging(obj, current*charging_voltages(j,3), current);
eff_cs(j) = effc;
ah_difference = (charging_voltages(j,2)-charging_voltages(j-1,2))*stretch_factor;
E_c(j) = E_c(j-1) + charging_voltages(j,3)*ah_difference*effc;%obj.eff_c(1,i);
end
%plot3(charging_voltages(start_index:end_index,1)*obj.nominal_capacity, E_c(start_index:end_index), charging_voltages(start_index:end_index,3), 'Color', [0,114, 189]/255, 'LineWidth', 2);
%hold on;
eff_cs_avg(1,i) = mean(eff_cs((start_index+1):end_index));
eff_cs_avg(2,i) = unique_charging_rates(i)*obj.nominal_capacity;
% plot([0, E_c(start_index:end_index)'], [1.5, charging_voltages(start_index:end_index,3)'], 'LineWidth', 2);
plot(charging_voltages((start_index+1):end_index,3), eff_cs((start_index+1):end_index), 'LineWidth', 2);
% plot(E_c((start_index+1):end_index), eff_cs((start_index+1):end_index), 'LineWidth', 2);
if (i < num_unique_charging_rates)
hold on;
end
% else
% hold on;
% plot([0,72.5], [1.5, 1.5], '--k')
% hold on;
% plot([0,72.5], [2.7, 2.7], '--k')
% legend('0.1C', '0.5C', '1C', '2C', '3C', '4C', '5C')
% xlabel('Energy Content (Wh)')
% ylabel('Voltage (V)')
% set(gca, 'FontSize', 15)
% xlim([0,72.5])
% end
obj.a_2(1,i) = E_c(end_index);
obj.a_2(2,i) = unique_charging_rates(i)*obj.nominal_capacity;
end
set(gca,'FontSize', 15)
xlabel('Voltage (V)')
ylabel('Efficiency')
figure;
eff_ds = zeros(num_discharging_points, 1);
eff_ds_avg = zeros(2, num_unique_discharging_rates);
for i=1:num_unique_discharging_rates
start_index = unique_discharging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_discharging_rates)
end_index = length(discharging_voltages);
else
end_index = unique_discharging_rate_indices(i+1)-1;
end
E_d(start_index) = 0;
for j=(start_index+1):end_index;
current = -unique_discharging_rates(i)*obj.nominal_capacity;
effd = eff_discharging(obj, current*discharging_voltages(j,3), current);
eff_ds(j) = effd;
ah_difference = discharging_voltages(j,2)-discharging_voltages(j-1,2);
E_d(j) = E_d(j-1) + discharging_voltages(j,3)*ah_difference*effd;
end
eff_ds_avg(1,i) = mean(eff_ds((start_index+1):end_index));
eff_ds_avg(2,i) = -unique_discharging_rates(i)*obj.nominal_capacity;
plot(discharging_voltages((start_index+1):end_index,3), eff_ds((start_index+1):end_index), 'LineWidth', 2);
if (i < num_unique_discharging_rates)
hold on;
else
hold on;
resolution1 = 0:(-obj.max_discharging_current/10):(-obj.max_discharging_current);
Vnom_d_m1 = mean(interp1(obj.Vnom_d(2,:), obj.Vnom_d(1,:), resolution1, 'linear', 'extrap'));
plot(ones(1,num_unique_discharging_rates)*Vnom_d_m1, 1-(obj.R_i*(-unique_discharging_rates)*obj.nominal_capacity)/Vnom_d_m1, '*k')
hold on;
resolution2 = 0:(-obj.max_discharging_current/10):(-60);
eff_d_5C = mean(interp1(obj.eff_d(2,:), 2-obj.eff_d(1,:), resolution1, 'linear', 'extrap'));
eff_d_2C = mean(interp1(obj.eff_d(2,:), 2-obj.eff_d(1,:), resolution2, 'linear', 'extrap'));
plot(Vnom_d_m1, eff_d_5C, 'sb')
hold on;
plot(Vnom_d_m1, eff_d_2C, 'sr')
xlabel('Voltage (V)')
ylabel('\eta_d')
set(gca, 'FontSize', 15)
xlim([1.3,2.8])
end
end
% since the battery was being discharged, need to shift
% energy content so that it represents how much energy is
% left in the battery. We use the largest difference in energy
% content to undertand the maximum energy that can be obtained.
max_content = max(E_d);
for i=1:num_unique_discharging_rates
start_index = unique_discharging_rate_indices(i);
end_index = [];
if (i+1 > num_unique_discharging_rates)
end_index = length(discharging_voltages);
else
end_index = unique_discharging_rate_indices(i+1)-1;
end
max_rate_content = max(E_d(start_index:end_index));
for j=start_index:end_index
E_d(j) = E_d(j) + (max_content - max_rate_content);
end
%plot3(discharging_voltages(start_index:end_index,1)*-obj.nominal_capacity, E_d(start_index:end_index), discharging_voltages(start_index:end_index,3), 'Color', [0,114, 189]/255, 'LineWidth', 2);
%plot(E_d((start_index+1):end_index), eff_ds((start_index+1):end_index), 'LineWidth', 2);
% if (i < num_unique_discharging_rates)
% hold on;
% end
obj.a_1(1,i) = max_content - max_rate_content;
obj.a_1(2,i) = -unique_discharging_rates(i)*obj.nominal_capacity;
end
%
% figure;
% scatter(obj.eff_c(2,:), obj.eff_c(1,:), '*')
% hold on;
% scatter(eff_cs_avg(2,:), eff_cs_avg(1,:), '+')
% set(gca, 'FontSize', 15)
% xlabel('Current (A)')
% ylabel('Efficiency')
% legend('\eta_c with Vnom', '\eta_c averaged over SoC range')
%
% figure;
% scatter(obj.eff_d(2,:), obj.eff_d(1,:), '*')
% hold on;
% scatter(eff_ds_avg(2,:), eff_ds_avg(1,:), '+')
% set(gca, 'FontSize', 15)
% xlabel('Current (A)')
% ylabel('Efficiency')
% legend('\eta_d with Vnom', '\eta_d averaged over SoC range')
% Step 6: set maximum charging and discharging current
obj.alpha_c = obj.max_charging_current;
obj.alpha_d = -obj.max_discharging_current;
% Step 7: set up the scattered interpolants (M function)
%all_currents = vertcat(charging_voltages(:,1), -1*discharging_voltages(:,1))*obj.nominal_capacity;
%all_bks = vertcat(E_c, E_d);
%all_vs = vertcat(charging_voltages(:,3), discharging_voltages(:,3));
all_currents = -1*discharging_voltages(:,1)*obj.nominal_capacity;
all_bks = E_d;
all_vs = discharging_voltages(:,3);
M_data = scatteredInterpolant(all_currents, all_bks, all_vs, 'linear', 'linear');
%obj.M_function = scatteredInterpolant(all_currents, all_bks, all_vs, 'linear', 'linear');
% V_min and V_max can be extracted from the voltage curves
obj.V_min = min(all_vs);
obj.V_max = max(all_vs);
max_b = max(all_bks);
min_b = min(all_bks);
max_curr = max(all_currents);
min_curr = min(all_currents);
% calculate the INTerpolation RESolution
b_int_res = (max_b - min_b)/100;
curr_int_res = (max_curr - min_curr)/100;
b_range = min_b:b_int_res:(max_b);
curr_range = min_curr:curr_int_res:max_curr;
[curr_grid,b_grid] = ndgrid(curr_range,b_range);
v_grid = zeros(length(curr_range), length(b_range));
for i=1:length(curr_range)
for j=1:length(b_range)
% use M_function to get voltage estimates for the grid
% defined by the current and energy content. Limit the
% voltage estimates to be between V_min and V_max.
v_grid(i,j) = M_data(curr_grid(i,j), b_grid(i,j));
end
end
obj.b_grid = b_grid;
obj.v_grid = v_grid;
obj.c_grid = curr_grid;
obj.M_function = griddedInterpolant(curr_grid,b_grid,v_grid);
%
% test_bs = [72:-1:20];
%
% test_curs = ones(1,53)*(-100);
%
% test_vs = obj.M_function(test_curs,test_bs);
% plot the M function;
figure;
scatter3(all_currents, all_bks, all_vs);
hold on;
%figure;
scatter3(reshape(curr_grid, [1,numel(curr_grid)]), reshape(b_grid, [1,numel(b_grid)]), reshape(v_grid, [1,numel(v_grid)]), '.', 'MarkerEdgeColor', [0.5 .5 .5], 'MarkerFaceColor', [0.5 0.5 0.5]);
zlim([1.5,4])
xlabel('Current (A)')
ylabel('Energy Content (Wh)')
zlabel('Voltage (V)')
% hold on;
% scatter3(test_curs,test_bs,test_vs)
% Step 8: specify remaining constants
% initialize the previous voltage value to a reasonable
% estimate.
obj.v_prev = obj.Vnom_c(1,1);
% set the initial energy content
obj.b_prev = obj.initial_energy_content;
% tolerance that determines voltage conversion. Fixed at the
% following value:
obj.tolerance = 0.005;
% check if we want to throw exceptions or not.
obj.throw_exception = false;
if (strcmp(obj.StringSetProperty, 'Throw BMS exceptions'))
obj.throw_exception = true;
end
%obj.b_grid = [];
%obj.c_grid = [];
%obj.v_grid = [];
end
function y = stepImpl(obj,u)
% Implement algorithm. Calculate y as a function of input u and
% discrete states. u is the input power.
if (u == 0)
obj.I = 0;
obj.V = max(obj.V_min, min(obj.V_max, obj.M_function(0,obj.b)));
y = [obj.V, obj.I, obj.b];
return;
end
[curr_estimate, v_estimate, b_estimate, a_1_val, a_2_val, eff_c_val, eff_d_val, num_intersections] = getVoltage(obj,u);
if num_intersections == 0
power_resolution = u:(-u/50):0;
max_power = 0;
for p=power_resolution
[curr_estimate, v_estimate, b_estimate, a_1_val, a_2_val, eff_c_val, eff_d_val, num_intersections] = getVoltage(obj,p);
if (num_intersections > 0)
max_power = p;
break;
end
end
msgID = 'Battery:NoIntersection';
msg = 'Charging or discharging power is too high';
baseException = MException(msgID,msg);
msgID2 = 'Battery:MaxPower';
msg2 = num2str(max_power);
causeException = MException(msgID2, msg2);
baseException = addCause(baseException,causeException);
if (obj.throw_exception)
throw(baseException);
end
end
% check that current does not exceed limits
if (curr_estimate > obj.alpha_c) || (curr_estimate < obj.alpha_d)
msgID = 'Battery:ChargingOrDischargingRate';
msg = 'Charging or discharging power is too high';
baseException = MException(msgID,msg);
if (curr_estimate > 0)
% use binary search to find power whose current estimate is
% lower than alpha_c.
L = 0;
R = u;
power_resolution = (R-L)/50;
power_range = L:power_resolution:R;
max_pow = 0;
max_curr = 0;
curr_estimate = 0;
b_estimate = obj.b_prev;
v_estimate = obj.v_prev;
for p_index = 1:numel(power_range)
power_prime = power_range(p_index);
[curr_prime, v_prime, b_prime, a_1_prime, a_2_prime, eff_c_prime, eff_d_prime] = getVoltage(obj,power_prime);
if ((curr_prime < obj.alpha_c) && (abs(curr_prime) > abs(max_curr)))
max_pow = power_prime;
max_curr = curr_prime;
curr_estimate = curr_prime;
b_estimate = b_prime;
v_estimate = v_prime;
end
end
msgID2 = 'Battery:maxChargingPower';
msg2 = num2str(max_pow);
causeException = MException(msgID2, msg2);
baseException = addCause(baseException,causeException);
else
% Search to find the power corresponding to the max current
% estimate that is lower (in magnitude) than alpha_d.
L = u;
R = 0;
power_resolution = (R-L)/50;
power_range = L:power_resolution:R;
max_pow = 0;
max_curr = 0;
curr_estimate = 0;
b_estimate = obj.b_prev;
v_estimate = obj.v_prev;
for p_index = 1:numel(power_range)
power_prime = power_range(p_index);
[curr_prime, v_prime, b_prime, a_1_prime, a_2_prime, eff_c_prime, eff_d_prime] = getVoltage(obj,power_prime);
if ((curr_prime > obj.alpha_d) && (abs(curr_prime) > abs(max_curr)))
max_pow = power_prime;
max_curr = curr_prime;
curr_estimate = curr_prime;
b_estimate = b_prime;
v_estimate = v_prime;
end
end
msgID2 = 'Battery:maxDishargingPower';
% use v_estimate instead of v_prev because it is lower, to give some
% room for imprecise current estimates.
msg2 = num2str(max_pow);
causeException = MException(msgID2, msg2);
baseException = addCause(baseException,causeException);
end
if (obj.throw_exception)
throw(baseException);
end
% check that battery energy content does not exceed limits.
% Throw an exception if it occurs.
elseif curr_estimate > 0 && b_estimate > a_2_val + obj.tolerance
msgID = 'Battery:Overcharge';
msg = 'Battery energy content exceeds upper limit';
baseException = MException(msgID,msg);
% Search to find power corresponding to max current that
% doesnt violate a_2 constraint.
L = 0;
R = u;
power_resolution = (R-L)/50;
power_range = L:power_resolution:R;
max_pow = 0;
max_curr = 0;
curr_estimate = 0;
b_estimate = obj.b_prev;
v_estimate = obj.v_prev;
for p_index = 1:numel(power_range)
power_prime = power_range(p_index);
[curr_prime, v_prime, b_prime, a_1_prime, a_2_prime, eff_c_prime, eff_d_prime] = getVoltage(obj,power_prime);
if ((b_prime < a_2_prime) && (abs(curr_prime) > abs(max_curr)))
max_pow = power_prime;
max_curr = curr_prime;
curr_estimate = curr_prime;
b_estimate = b_prime;
v_estimate = v_prime;
end
end
msgID2 = 'Battery:maxPowerBeforeOvercharge';
msg2 = num2str(max_pow);
causeException = MException(msgID2, msg2);
baseException = addCause(baseException,causeException);
if (obj.throw_exception)
throw(baseException);
end
elseif (curr_estimate < 0) && (b_estimate < (a_1_val - obj.tolerance))
msgID = 'Battery:Undercharge';
msg = 'Battery energy content below lower limit';
baseException = MException(msgID,msg);
% search to find power corresponding to maximum current
% that doesn't break a_1 constraint.
L = u;
R = 0;
power_resolution = (R-L)/50;
power_range = L:power_resolution:R;
max_pow = 0;
max_curr = 0;
curr_estimate = 0;
b_estimate = obj.b_prev;
v_estimate = obj.v_prev;
for p_index = 1:numel(power_range)
power_prime = power_range(p_index);
[curr_prime, v_prime, b_prime, a_1_prime, a_2_prime, eff_c_prime, eff_d_prime] = getVoltage(obj,power_prime);
if ((b_prime > a_1_prime) && (abs(curr_prime) > abs(max_curr)))
max_pow = power_prime;
max_curr = curr_prime;
curr_estimate = curr_prime;
b_estimate = b_prime;
v_estimate = v_prime;
end
end
msgID2 = 'Battery:maxPowerBeforeUndercharge';
msg2 = num2str(max_pow);
causeException = MException(msgID2, msg2);
baseException = addCause(baseException,causeException);
if (obj.throw_exception)
throw(baseException);
end
end
obj.V = v_estimate;
obj.I = curr_estimate;
obj.b = b_estimate;
obj.v_prev = obj.V;
obj.b_prev = obj.b;
y = [obj.V, obj.I, obj.b];
end
function resetImpl(obj)
obj.b = obj.initial_energy_content;
obj.b_prev = obj.initial_energy_content;
end
function name1 = getInputNamesImpl(obj)
% Return input port names for System block
name1 = 'Power';
end
function name1 = getOutputNamesImpl(obj)
% Return output port names for System block
name1 = 'V, I, b';
end
function out = getOutputSizeImpl(obj)
% Return size for each output port
out = [1 3];
end
function out = getOutputDataTypeImpl(obj)
% Return data type for each output port
out = 'double';
end
function out = isOutputComplexImpl(obj)
% Return true for each output port with complex data
out = false;
end
function out = isOutputFixedSizeImpl(obj)
% Return true for each output port with fixed size
out = true;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(obj,name)
% Return size, data type, and complexity of discrete-state
% specified in name
sz = [1 1];
dt = 'double';
cp = false;
end
function effc = eff_charging(obj, power, current)
effc = 1 - ((obj.R_i * (current^2))/power);
%vnom = interp1(obj.Vnom_c(2,:), obj.Vnom_c(1,:), current, 'linear', 'extrap');
%effc = 1 - ((obj.R_i * (current))/vnom);
end
function effd = eff_discharging(obj, power, current)
effd = 1 - ((obj.R_i * (current^2))/power);
%vnom = interp1(obj.Vnom_d(2,:), obj.Vnom_d(1,:), current, 'linear', 'extrap');
%effd = 1 + ((obj.R_i * (current))/vnom);
end
function [curr_best, v_best, b_best, a_1_best, a_2_best, eff_c_best, eff_d_best, num_intersections] = getVoltage(obj,u)
test_volts = obj.V_min:((obj.V_max - obj.V_min)/1000):obj.V_max;
test_currents = u./test_volts;
valid_v = [];
valid_c = [];
valid_e = [];
eff_c_val = 1;
eff_d_val = 1;
% discard all voltages outside the safe voltage range.
for i=1:numel(test_volts)
a2 = interp1(obj.a_2(2,:), obj.a_2(1,:), test_currents(i), 'linear', 'extrap');
a1 = interp1(obj.a_1(2,:), obj.a_1(1,:), test_currents(i), 'linear', 'extrap');
delta_e = 0;
if (test_currents(i) < 0)
%eff_d_val = interp1(obj.eff_d(2,:), obj.eff_d(1,:), test_currents(i), 'linear', 'extrap');
eff_d_val = eff_discharging(obj, u, test_currents(i));
delta_e = (u*eff_d_val)*obj.time_step;
elseif (test_currents(i) > 0)
%eff_c_val = interp1(obj.eff_c(2,:), obj.eff_c(1,:), test_currents(i), 'linear', 'extrap');
eff_c_val = eff_charging(obj, u, test_currents(i));
delta_e = u*eff_c_val*obj.time_step;
end
b_i = obj.b_prev + delta_e;
if (test_currents(i) >= obj.alpha_d && test_currents(i) <= obj.alpha_c && b_i > a1 && b_i < a2)
valid_v = [valid_v, test_volts(i)];
valid_c = [valid_c, test_currents(i)];
valid_e = [valid_e, b_i];
end
end
% now check if E and C combinations get us close to the
% corresponding V. If it is within the obj.tolerance, keep it
% in this vector, we store the actual voltage (from M function), the corresponding
% C and E, as well as the voltage calculated as v=p/C;
valid_vvce = [];
eff_c_val = 1;
eff_d_val = 1;
actual_v = obj.M_function(valid_c, valid_e);
for i = 1:numel(actual_v)
if (actual_v(i) < obj.V_min)
actual_v(i) = obj.V_min;
elseif (actual_v(i) > obj.V_max)
actual_v(i) = obj.V_max;
end
end
for i = 1:(numel(actual_v))
% stop if we are at the last point
if (i == numel(actual_v))
break;
end
if (sign(u - actual_v(i)*valid_c(i)) ~= sign(u - actual_v(i+1)*valid_c(i+1)))
i_value = interp1([valid_c(i)*actual_v(i),valid_c(i+1)*actual_v(i+1)], [valid_c(i), valid_c(i+1)], u);
delta_e = 0;
if (i_value < 0)
%eff_d_val = interp1(obj.eff_d(2,:), obj.eff_d(1,:), i_value, 'linear', 'extrap');
eff_d_val = eff_discharging(obj, u, i_value);
delta_e = (u*eff_d_val)*obj.time_step;
elseif (i_value > 0)
%eff_c_val = interp1(obj.eff_c(2,:), obj.eff_c(1,:), i_value, 'linear', 'extrap');
eff_c_val = eff_charging(obj, u, i_value);
delta_e = u*eff_c_val*obj.time_step;
end
b_value = obj.b_prev + delta_e;
v_value = min(obj.V_max, max(obj.V_min,obj.M_function(i_value,b_value)));
valid_vvce = [valid_vvce; [v_value, u/i_value, i_value, b_value]];
end
end
num_intersections = size(valid_vvce,1);
% if we didnt find any good candidates for voltage...
if (size(valid_vvce,1) == 0)
% %stop here
% hold on;
% plot3(valid_c, valid_e, valid_v, '-', 'LineWidth', 2);
% hold on;
% plot3(valid_c, valid_e, actual_v, '-', 'LineWidth', 2);
% legend('M surface', 'points in X', 'points in Y')
% figure;
% hold on;
% plot(valid_c, (valid_c.*actual_v), 'LineWidth', 2)
% hold on;
% plot(valid_c, ones(1,numel(valid_c))*u, '-.k', 'LineWidth', 2)
% xlabel('Current (A)')
% ylabel('Power (W)')
% set(gca, 'FontSize', 15)
%xlim([min(valid_c), max(valid_c)]);
% legend('Power = M(b,i) \cdot i');
numel(valid_vvce,1);
v_best = 0;
curr_best = 0;
b_best = 0;
a_2_best = 0;
a_1_best = 0;
eff_c_best = 0;
eff_d_best = 0;
return;
end
% if more than one option, pick the best candidate based on
% closest voltage
if (size(valid_vvce,1) > 1)
min_distance = inf;
min_index = 0;
% figure;
% hold on;
% plot(valid_c, (valid_c.*actual_v), 'LineWidth', 2)
% hold on;
% plot(valid_c, ones(1,numel(valid_c))*u, '-.k', 'LineWidth', 2)
% xlabel('Current (A)')
% ylabel('Power (W)')
% set(gca, 'FontSize', 15)
% %xlim([min(valid_c), max(valid_c)]);
% legend('Power = M(b,i) \cdot i');
% size(valid_vvce,1)
for i=1:size(valid_vvce,1)
if (abs(valid_vvce(i,1) - obj.V) < min_distance)
min_distance = abs(valid_vvce(i,1) - obj.V);
min_index = i;
end
end
%check how many are more than 1 ampere from the minimum intersection
% num_intersections = 0;
% for i=1:size(valid_vvce,1)
%
% if (abs((u/valid_vvce(i,1)) - (u/valid_vvce(min_index,1))) > 1)
% num_intersections = num_intersections + 1;
% end
%
% end
valid_vvce = valid_vvce(min_index,:);
end
v_best = valid_vvce(1);
curr_best = valid_vvce(3);
b_best = valid_vvce(4);
a_2_best = interp1(obj.a_2(2,:), obj.a_2(1,:), curr_best, 'linear', 'extrap');
a_1_best = interp1(obj.a_1(2,:), obj.a_1(1,:), curr_best, 'linear', 'extrap');
eff_c_best = eff_c_val;
eff_d_best = eff_d_val;
end
function [curr_best, v_best, b_best, a_1_best, a_2_best, eff_c_best, eff_d_best] = iterateVoltage(obj,u)
max_iterations = 100;
v_best = 0;
v_diff = inf;
curr_best = 0;
b_best = 0;
a_1_best = 0;
a_2_best = inf;
eff_c_best = 1;
eff_d_best = 1;
v_res = (obj.V_max - obj.V_min)/4;
for v_0 = [obj.v_prev, obj.V_min:v_res:obj.V_max]
v_estimate = v_0;
curr_estimate = 0;
b_estimate = obj.b_prev;
% initialize change in energy in this time step