-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEFs.m
284 lines (267 loc) · 10.1 KB
/
MEFs.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
%% Comparing marginal emissions factors
homeDir= pwd;
Plotpath= strcat(homeDir,'\PlotsMEF\');
cd model/
saver= strcat('MEF_',date);
%% Get MEFs for MISO & NYISO (v4)
grids = 2;
for region = grids:-1:1
[MEF_data{region},...
Dispatch_data{region},...
hourlyGHG0{region},...
powerplants{region}] = MEF_function(region);
end
%% Hourly MEF plots
for region = 1:grids
hourly_MEF_plots(region,Plotpath,MEF_data{region})
end
%% Apply MEFs to case studies(v4)
load('Case_Studies.mat','Case_Studies')
techs = size(Case_Studies,1);
parfor casestudy = 1:techs
for region = 1:grids
[Emissions{casestudy}{region},...
Dispatch1{casestudy}{region},...
hourly_GHG1{casestudy}{region}] = GHG_casestudy(...
region,...
Case_Studies(casestudy,:),...
hourlyGHG0{region},...
MEF_data{region});
end
end
save(saver,'Emissions','MEF_data')
%% Compare between methods and data aggregations
% Bar plots of approximations vs true emissions
parfor casestudy = 1:techs
for region = 1:grids
[barplots{casestudy}{region}] = MEF_plots(...
casestudy,...
region,...
Emissions{casestudy}{region},...
Plotpath);
end
end
%% Subplots (values)
runs = 0;
for casestudy = 1:techs
for region = 1:grids
counter = runs + 1;
Emissions_data = Emissions{casestudy}{region};
subplot(techs,grids,counter)
Approximations = Emissions_data(1:5,1:4);
Actual_value = Emissions_data(6,1);
b = bar(Approximations);
hold on
c = plot(xlim,[Actual_value Actual_value],'--k','LineWidth',1.5);
hold off
ylabel('Change in emissions (tCO_2e)')
set(gca,'XTickLabel',...
{'Incremental','Costliest','Thermal','Demand','AEF'})
set(gca,'FontSize',12)
set(gcf,'Position',[100 100 550 500])
if counter == 2
h = [b(1);b(2);b(3);b(4);c];
legend(h,{'Hourly','24-hour season','24-hour yearly','Yearly','Simulated'},'Location','Best')
legend boxoff
end
runs = counter;
end
end
set(gcf,'Position',[100 0 800 1500])
saveas(gcf,strcat(Plotpath,'subplots','_','bar_plot.png'));
%% Subplots (values; trim outliers)
extra_space = 1.5;
runs = 0;
for casestudy = 1:techs
for region = 1:grids
counter = runs + 1;
Emissions_data = Emissions{casestudy}{region};
subplot(techs,grids,counter)
Approximations = Emissions_data(1:5,1:4);
Actual_value = Emissions_data(6,1);
b = bar(Approximations);
hold on
c = plot(xlim,[Actual_value Actual_value],'--k','LineWidth',1.5);
hold off
ylabel('Change in emissions (tCO_2e)')
set(gca,'XTickLabel',{'Incremental','Costliest','Thermal','Demand','AEF'})
set(gca,'FontSize',12)
set(gcf,'Position',[100 100 550 500])
abs_aprx = abs(Approximations);
b_max = max(abs_aprx); % max value of each column (of each temporal resolution)
b_max = maxk(b_max,2); % choose the 2 highest values
b_max1 = b_max(1); % outlier
b_max2 = b_max(2); % highest non-outlier
if b_max1 > b_max2*2
[row,col] = find(abs_aprx==b_max2);
yl = [min(0,b(col).YData(row)*extra_space) max(0,b(col).YData(row)*extra_space)];
axis([xlim yl])
[row2,col2] = find(abs_aprx==b_max1);
xtips = b(col2).XData(row2)-0.16; % change for XEndPoints(row2) to place right on bar
b_max_1_nonabsolute = Approximations(row2,col2);
if b_max_1_nonabsolute < 0
ytips = yl(1)*0.91;
elseif b_max_1_nonabsolute > 0
ytips = yl(2)*0.98;
end
if counter == 1
axis([xlim [yl(1)*1.5 yl(2)]])
ytips = yl(1)*1.5*0.91;
h = [b(1);b(2);b(3);b(4);c];
legend(h,{'Hourly','24-hour season','24-hour yearly','Yearly','Simulated'},'Location','Southwest')
legend boxoff
end
outlier_label = string(round(b(col2).YData(row2)));
labels = strcat(outlier_label,' tCO_2e');
text(xtips,ytips,labels,'HorizontalAlignment','left',...
'VerticalAlignment','cap','Fontsize',15)
end
runs = counter;
end
end
set(gcf,'Position',[100 0 800 1500])
saveas(gcf,strcat(Plotpath,'subplots','_','bar_plot_TRIMMED.png'));
%% Subplots (error percentages)
runs = 0;
for casestudy = 1:techs
for region = 1:grids
counter = runs + 1;
Emissions_data = Emissions{casestudy}{region};
subplot(techs,grids,counter)
Approximations = Emissions_data(1:5,1:4);
Actual_value = Emissions_data(6,1);
Percent_error = ((Approximations - Actual_value)/Actual_value)*100;
b = bar(Percent_error);
set(gca,'XTickLabel',{'Incremental','Costliest','Thermal','Demand','AEF'})
ylabel('Estimation error (%)')
set(gca,'FontSize',12)
set(gcf,'Position',[100 100 550 500])
if counter == 4
h = [b(1);b(2);b(3);b(4)];
legend(h,{'Hourly','24-hour season','24-hour yearly','Yearly'},'Location','Best')
legend boxoff
end
runs = counter;
end
end
set(gcf,'Position',[100 0 800 1500])
saveas(gcf,strcat(Plotpath,'subplots','_','error_bar_plot.png'));
%% Subplots (error percentages; trim outliers)
extra_space = 1.5;
runs=0;
for casestudy = 1:techs
for region = 1:grids
counter = runs + 1;
Emissions_data = Emissions{casestudy}{region};
subplot(techs,grids,counter)
Approximations = Emissions_data(1:5,1:4);
Actual_value = Emissions_data(6,1);
Percent_error = ((Approximations - Actual_value)/Actual_value)*100;
b = bar(Percent_error);
ylabel('Estimation error (%)')
set(gca,'XTickLabel',{'Incremental','Costliest','Thermal','Demand','AEF'})
set(gca,'FontSize',12)
set(gcf,'Position',[100 100 550 500])
positive_approximations = Percent_error(Percent_error>=0);
negative_approximations = Percent_error(Percent_error<=0);
if find(positive_approximations) > 0
b_max_positive = maxk(positive_approximations,2);
b_max1_positive = b_max_positive(1);
b_max2_positive = b_max_positive(2);
trim_positives = b_max1_positive > b_max2_positive*2;
end
if find(negative_approximations) > 0
b_max_negative = mink(negative_approximations,2);
b_max1_negative = b_max_negative(1);
b_max2_negative = b_max_negative(2);
trim_negatives = b_max1_negative < b_max2_negative*2;
end
if trim_positives==1 && trim_negatives==1
yl = [b_max2_negative*extra_space b_max2_positive*extra_space];
axis([xlim yl])
[row,col] = find(Percent_error==b_max2_positive);
[row2,col2] = find(Percent_error==b_max1_positive);
xtips = b(col2).XData(row2)-0.16; % change for XEndPoints(row2) to place right on bar
ytips = yl(2)*0.98;
outlier_label = string(round(b(col2).YData(row2)));
labels = strcat(outlier_label,' %');
text(xtips,ytips,labels,'HorizontalAlignment','left',...
'VerticalAlignment','cap','Fontsize',15)
[row,col] = find(Percent_error==b_max2_negative);
[row2,col2] = find(Percent_error==b_max1_negative);
xtips = b(col2).XData(row2)-0.16; % change for XEndPoints(row2) to place right on bar
ytips = yl(1)*0.91;
outlier_label = string(round(b(col2).YData(row2)));
labels = strcat(outlier_label,' %');
text(xtips,ytips,labels,'HorizontalAlignment','left',...
'VerticalAlignment','cap','Fontsize',15)
elseif trim_positives==1 && trim_negatives==0
yl = [b_max1_negative*extra_space b_max2_positive*extra_space];
if counter == 5
yl = [b_max1_negative*extra_space b_max2_positive+15];
end
axis([xlim yl])
[row,col] = find(Percent_error==b_max2_positive);
[row2,col2] = find(Percent_error==b_max1_positive);
xtips = b(col2).XData(row2)-0.16; % change for XEndPoints(row2) to place right on bar
ytips = yl(2)*0.98;
if counter == 1
axis([xlim [yl(1) yl(2)*1.5]])
ytips = yl(2)*1.5*0.98;
h = [b(1);b(2);b(3);b(4)];
legend(h,{'Hourly','24-hour season','24-hour yearly','Yearly'},'Location','Southwest')
legend boxoff
end
outlier_label = string(round(b(col2).YData(row2)));
labels = strcat(outlier_label,' %');
text(xtips,ytips,labels,'HorizontalAlignment','left',...
'VerticalAlignment','cap','Fontsize',15)
elseif trim_positives==0 && trim_negatives==1
axis([xlim b_max2_negative*extra_space b_max1_positive*extra_space])
elseif trim_positives==0 && trim_negatives==0
end
runs = counter;
end
end
set(gcf,'Position',[100 0 800 1500])
saveas(gcf,strcat(Plotpath,'subplots','_','error_bar_plot_TRIMMED.png'));
close all
%% Plot load profiles
% PV
PV_spaghettis = reshape(Case_Studies(1,:),24,365)*(1000);
plot(1:24,PV_spaghettis(:,1),'Color',[1,0.647,0,0.2]);
hold on
for day = 365:-1:2
plot(1:24,PV_spaghettis(:,day),'Color',[1,0.647,0,0.2]);
end
hold off
box off
set(gca,'FontSize',15)
set(gcf,'Position',[100 100 300 300])
title('(a) PV')
axis([1 24 ylim])
saveas(gcf,strcat(Plotpath,'loadprofile_PV.png'));
% EV
EV_loadprofile = Case_Studies(2,1:24)*(1000);
plot(1:24,EV_loadprofile,'LineWidth',1.5);
box off
set(gca,'FontSize',15)
set(gcf,'Position',[100 100 300 300])
title('(b) EV')
axis([1 24 ylim])
saveas(gcf,strcat(Plotpath,'loadprofile_EV.png'));
% Heat pump
HP_spaghettis = reshape(Case_Studies(3,:),24,365)*(1000);
plot(1:24,HP_spaghettis(:,1),'Color',[1,0,0,0.2]);
hold on
for day = 365:-1:2
plot(1:24,HP_spaghettis(:,day),'Color',[1,0,0,0.2]);
end
hold off
box off
set(gca,'FontSize',15)
set(gcf,'Position',[100 100 300 300])
title('(c) HP')
axis([1 24 ylim])
saveas(gcf,strcat(Plotpath,'loadprofile_HP.png'));
close all