CJahns commited on
Commit
8758d05
·
1 Parent(s): 4de981d

Upload 4 files

Browse files
Files changed (4) hide show
  1. Input_Jahr_2021.xlsx +2 -2
  2. app.py +141 -180
  3. model_data.pkl +3 -0
  4. sourced.py +205 -0
Input_Jahr_2021.xlsx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:630332987718f2aa27e4cd917a37a95ad9d81d4682367982be8301d79445267b
3
- size 994601
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c92d04350980ce78a44f5ce2cc57fef81682eb12247d80b9f89bed4954302d7d
3
+ size 995396
app.py CHANGED
@@ -10,198 +10,116 @@ This is a temporary script file.
10
 
11
 
12
  from numpy import arange
13
- # %%
14
  import xarray as xr
15
- # %%
16
  import highspy
17
- # %%
18
- import linopy
19
-
20
- # %%
21
  from linopy import Model, EQUAL
22
- # %%
23
  import pandas as pd
24
- #%%
25
  import plotly.express as px
26
- ##import gurobipy
27
-
28
- # %%
29
  import streamlit as st
30
-
31
-
 
 
 
 
 
32
  # %%
 
 
 
33
 
34
  #url_excel = r'Input_Jahr_2021.xlsx'
35
- url_excel = st.file_uploader(label = 'Excel Upload')
 
 
36
  if url_excel == None:
37
  url_excel = r'Input_Jahr_2021.xlsx'
 
 
 
 
 
 
 
38
 
39
  # # %%
40
- # # Slider for gas price [€/MWh_th]
41
- price_gas = st.slider(value=100, min_value=0, max_value=400, label="Natural gas price [€/MWh]", step=10)
42
-
43
- # Slider for CO2 price [€/t]
44
- price_co2 = st.slider(value=0, min_value=0, max_value=400, label="CO2 price [€/t CO2eq]", step=10)
45
-
46
- # Slider for CO2 limit [mio. t]
47
- limit_co2 = st.slider(value=400, min_value=0, max_value=750, label="CO2 limit [mio. t]", step=50)
48
 
49
- # Slider for H2 price / usevalue [€/MWH_th]
50
- price_h2 = st.slider(value=100, min_value=0, max_value=300, label="Hydrogen price [€/MWh]", step=10)
51
-
52
- # %%
53
 
 
54
 
55
 
56
- #time_steps_aggregate = 6
57
- #= xr_profiles.rolling( time_step = time_steps_aggregate).mean().sel(time_step = time[0::time_steps_aggregate])
58
 
59
- def timstep_aggregate(time_steps_aggregate, xr ):
60
- return xr.rolling( t = time_steps_aggregate).mean().sel(t = t[0::time_steps_aggregate])
61
 
 
62
 
63
 
64
- # %%
65
- ## Define all sets for the model
66
- # Timesteps
67
- df_excel= pd.read_excel(url_excel, sheet_name = 'Timesteps_All', header=None)
68
- t = pd.Index(df_excel.iloc[:,0], name = 't')
69
- #t = pd.Index(df_excel.iloc[:,0], name = 't')
70
 
71
- # Technologies
72
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
73
- i = pd.Index(df_excel.iloc[:,0], name = 'i')
74
 
75
- multiselect = st.multiselect(label = 'Technolgy invest', options=i)
76
 
 
 
 
 
 
 
 
 
 
77
 
78
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
79
- iConv = pd.Index(df_excel.iloc[0:7,2], name = 'iConv')
 
80
 
81
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
82
- iRes = pd.Index(df_excel.iloc[0:4,4], name = 'iRes')
 
 
 
 
 
 
 
 
 
83
 
84
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
85
- iSto = pd.Index(df_excel.iloc[0:2,6], name = 'iSto')
 
 
86
 
87
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
88
- iPtG = pd.Index(df_excel.iloc[0:1,8], name = 'iPtG')
89
 
90
- df_excel = pd.read_excel(url_excel, sheet_name = 'Technologies')
91
- iHyRes = pd.Index(df_excel.iloc[0:1,10], name = 'iHyRes')
 
92
 
93
- # %%
94
- ### Parameters
95
- # CO2 limit (from slider)
96
- l_co2 = limit_co2*10**6
97
- p_co2 = price_co2
98
-
99
- # length of timesteps
100
- dt = 1
101
-
102
- # Demand
103
- df_excel= pd.read_excel(url_excel, sheet_name = 'Demand')
104
- #df_melt = pd.melt(df_excel, id_vars='Zeit')
105
- df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Unnamed: 1':'Demand'})
106
- #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
107
- df_excel = df_excel.fillna(0)
108
- df_excel = df_excel.set_index('t')
109
- D_t = df_excel.iloc[:,0].to_xarray()
110
- D_t = timstep_aggregate(6,D_t)
111
-
112
- ## Efficiencies
113
- df_excel = pd.read_excel(url_excel, sheet_name = 'Efficiency')
114
- df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'Efficiency'})
115
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
116
- df_excel = df_excel.fillna(0)
117
- df_excel = df_excel.set_index('i')
118
- eff_i = df_excel.iloc[:,0].to_xarray()
119
-
120
- ## Variable costs
121
- # Fuel costs
122
- df_excel = pd.read_excel(url_excel, sheet_name = 'FuelCosts')
123
- df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'FuelCosts'})
124
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
125
- df_excel = df_excel.fillna(0)
126
- df_excel = df_excel.set_index('i')
127
- c_fuel_i = df_excel.iloc[:,0].to_xarray()
128
- # Apply slider value
129
- c_fuel_i.loc[dict(i = 'Fossil Gas')] = price_gas
130
- c_fuel_i.loc[dict(i = 'H2')] = price_h2
131
-
132
- # Other var. costs
133
- df_excel = pd.read_excel(url_excel, sheet_name = 'OtherVarCosts')
134
- df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'OtherVarCosts'})
135
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
136
- df_excel = df_excel.fillna(0)
137
- df_excel = df_excel.set_index('i')
138
- c_other_i = df_excel.iloc[:,0].to_xarray()
139
 
140
- # Investment costs
141
- df_excel = pd.read_excel(url_excel, sheet_name = 'InvCosts')
142
- df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InvCosts'})
143
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
144
- df_excel = df_excel.fillna(0)
145
- df_excel = df_excel.set_index('i')
146
- c_inv_i = df_excel.iloc[:,0].to_xarray()*1000*0.1 # kw to MW and annuity factor
147
-
148
- # Emission factor
149
- df_excel = pd.read_excel(url_excel, sheet_name = 'EmFactor')
150
- df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'EmFactor'})
151
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
152
- df_excel = df_excel.fillna(0)
153
- df_excel = df_excel.set_index('i')
154
- co2_factor_i = df_excel.iloc[:,0].to_xarray()
155
-
156
- ## Calculation of variable costs
157
- c_var_i = (c_fuel_i.sel(i = iConv) + p_co2 * co2_factor_i.sel(i = iConv)) / eff_i.sel(i = iConv) + c_other_i.sel(i = iConv)
158
-
159
- # RES capacity factors
160
- #df_excel = pd.read_excel(url_excel, sheet_name = 'RES',header=[0,1])
161
- #df_excel = pd.read_excel(url_excel, sheet_name = 'RES', index_col=['Timesteps'], columns=['PV', 'WindOn', 'WindOff', 'RoR'])
162
- df_excel = pd.read_excel(url_excel, sheet_name = 'RES')
163
- df_excel = df_excel.set_index(['Timesteps'])
164
- df_test = df_excel
165
- df_excel = df_excel.stack()
166
- #df_excel = df_excel.rename(columns={'PV', 'WindOn', 'WindOff', 'RoR'})
167
- df_test2 = df_excel
168
- #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
169
- #df_excel = df_excel.fillna(0)
170
-
171
- #df_test = df_excel.set_index(['Timesteps', 'PV', 'WindOn', 'WindOff', 'RoR']).stack([0])
172
- #df_test.index = df_test.index.set_names(['t','i'])
173
- s_t_r_iRes = df_excel.to_xarray().rename({'level_1': 'i','Timesteps':'t'})
174
- s_t_r_iRes = timstep_aggregate(6,s_t_r_iRes)
175
- #s_t_r_iRes = df_excel.iloc[:,0].to_xarray()
176
-
177
- # Base capacities
178
- df_excel = pd.read_excel(url_excel, sheet_name = 'InstalledCap')
179
- df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InstalledCap'})
180
- df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
181
- df_excel = df_excel.fillna(0)
182
- df_excel = df_excel.set_index('i')
183
- K_0_i = df_excel.iloc[:,0].to_xarray()
184
-
185
- # Energy-to-power ratio storages
186
- df_excel = pd.read_excel(url_excel, sheet_name = 'E2P')
187
- df_excel = df_excel.rename(columns = {'Storage':'i', 'Unnamed: 1':'E2P ratio'})
188
- #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
189
- df_excel = df_excel.fillna(0)
190
- df_excel = df_excel.set_index('i')
191
- e2p_iSto = df_excel.iloc[:,0].to_xarray()
192
-
193
- # Inflow for hydro reservoir
194
- df_excel = pd.read_excel(url_excel, sheet_name = 'HydroInflow')
195
- df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Hydro Water Reservoir':'Inflow'})
196
- df_excel = df_excel.fillna(0)
197
- df_excel = df_excel.set_index('t')
198
- h_t = df_excel.iloc[:,0].to_xarray()
199
 
 
 
 
 
 
200
 
201
 
202
- t = D_t.get_index('t')
 
 
203
 
204
 
 
 
205
  # %%
206
  ### Variables
207
  m = Model()
@@ -216,7 +134,6 @@ y_ch = m.add_variables(coords = [t,i], name = 'y_ch', lower = 0) # Electricit
216
  l = m.add_variables(coords = [t,i], name = 'l', lower = 0) # Storage filling level
217
  w = m.add_variables(coords = [t], name = 'w', lower = 0) # RES curtailment
218
 
219
- partial_year_factor = (8760/len(t))
220
 
221
  ## Objective function
222
  C_tot = C_op + C_inv
@@ -224,20 +141,19 @@ m.add_objective(C_tot)
224
 
225
  ## Costs terms for objective function
226
  # Operational costs minus revenue for produced hydrogen
227
- C_op_sum = m.add_constraints((y.sel(i = c_var_i.get_index('i')) * c_var_i * dt).sum()*partial_year_factor - \
228
- ((y_ch.sel(i = iPtG) * eff_i.sel(i = iPtG)) * price_h2 * dt).sum()*partial_year_factor == C_op, name = 'C_op_sum')
229
 
230
  # Investment costs
231
  C_inv_sum = m.add_constraints((K * c_inv_i).sum() == C_inv, name = 'C_inv_sum')
232
 
233
  ## Load serving
234
- loadserve_t = m.add_constraints(((y * dt).sum(dims = 'i') - (w * dt) - y_ch.sum(dims = 'i') == D_t.sel(t = t) * dt), name = 'load')
235
 
236
  ## Maximum capacity limit
237
  maxcap_i_t = m.add_constraints((y - K <= K_0_i), name = 'max_cap')
238
 
239
  ## Maximum capacity limit
240
- maxcap_invest_i = m.add_constraints((K.sel(i = ['Electrolyzer','RoR','Biomass','H2']) <= 0), name = 'max_cap_invest')
241
 
242
  ## Maximum storage charging and discharging
243
  maxcha_iSto_t = m.add_constraints((y.sel(i = iSto) + y_ch.sel(i = iSto) - K.sel(i = iSto) <= K_0_i.sel(i = iSto)), name = 'max_cha')
@@ -258,49 +174,94 @@ filling_iHydro_t = m.add_constraints(l.sel(i = iHyRes) - l.sel(i = iHyRes).roll(
258
  filling_iSto_t = m.add_constraints(l.sel(i = iSto) - (l.sel(i = iSto).roll(t = -1) + (y.sel(i = iSto) ) * dt - y_ch.sel(i = iSto) * eff_i.sel(i = iSto) * dt) == 0, name = 'filling_level')
259
 
260
  ## CO2 limit --> ggf. hier auch mit Subset arbeiten (Technologien, die Brennstoff verbrauchen). (JR)
261
- CO2_limit = m.add_constraints(((y / eff_i) * co2_factor_i * dt).sum()* partial_year_factor <= l_co2 , name = 'CO2_limit')
262
 
263
 
264
  # %%
265
  m.solve(solver_name = 'highs')
266
 
267
- # %%
268
- m.solution['K'].to_dataframe().reset_index()
269
-
270
- # %%
271
- #((m.solution['y'] / eff_i) * co2_factor_i * dt).sum()*partial_year_factor
272
-
273
 
274
- # %%
275
- #m.solution['C_inv']
276
 
 
 
277
  # %%
278
  # Installed Cap
279
  # Assuming df_excel has columns 'All' and 'Capacities'
280
 
281
  fig = px.bar((m.solution['K']+K_0_i).to_dataframe(name='K').reset_index(), \
282
- y='i', x='K', orientation='h', title='TTotal Installed Capacities', color='i')
283
 
284
- fig
285
 
286
  # %%
 
287
  fig = px.bar(m.solution['K'].to_dataframe().reset_index(), y='i', x='K', orientation='h', title='New Capacities', color='i')
288
 
289
- fig
290
-
291
 
292
- i_with_capacity = m.solution['K'].where( m.solution['K'] > 0).dropna(dim = 'i').get_index('i')
293
  # %%
 
 
294
  fig = px.area(m.solution['y'].sel(i = i_with_capacity).to_dataframe().reset_index(), y='y', x='t', title='Production', color='i')
295
 
296
- fig
 
 
 
 
297
  # %%
 
 
 
 
298
  # %%
299
- fig = px.line(m.solution['l'].to_dataframe().reset_index(), y='l', x='t', title='Fillng level', color='i')
 
 
300
 
301
- fig
302
 
303
  # %%
304
 
305
- fig = px.line(m.solution['y_ch'].to_dataframe().reset_index(), y='y_ch', x='t', title='Production', color='i')
306
- fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
  from numpy import arange
 
13
  import xarray as xr
 
14
  import highspy
 
 
 
 
15
  from linopy import Model, EQUAL
 
16
  import pandas as pd
 
17
  import plotly.express as px
 
 
 
18
  import streamlit as st
19
+ import sourced as src
20
+ st.set_page_config(layout="wide")
21
+ # you can create columns to better manage the flow of your page
22
+ # this command makes 3 columns of equal width
23
+ col1, col2, col3, col4 = st.columns(4)
24
+ col1.header("Data Input")
25
+ col4.header("Download Results")
26
  # %%
27
+ with col1:
28
+ with open('Input_Jahr_2021.xlsx', 'rb') as f:
29
+ st.download_button('Download Excel Template', f, file_name='Input_Jahr_2021.xlsx') # Defaults to 'application/octet-stream'
30
 
31
  #url_excel = r'Input_Jahr_2021.xlsx'
32
+ url_excel = st.file_uploader(label = 'Excel Upload')
33
+
34
+
35
  if url_excel == None:
36
  url_excel = r'Input_Jahr_2021.xlsx'
37
+ sets_dict, params_dict= src.load_data_from_excel(url_excel, load_from_pickle_flag = True)
38
+ with col4:
39
+ st.write('Running with standard data')
40
+ else:
41
+ sets_dict, params_dict= src.load_data_from_excel(url_excel, load_from_pickle_flag = False)
42
+ with col4:
43
+ st.write('Running with user data')
44
 
45
  # # %%
 
 
 
 
 
 
 
 
46
 
47
+ def timstep_aggregate(time_steps_aggregate, xr ):
48
+ return xr.rolling( t = time_steps_aggregate).mean().sel(t = t[0::time_steps_aggregate])
 
 
49
 
50
+ #s_t_r_iRes = timstep_aggregate(6,s_t_r_iRes)
51
 
52
 
 
 
53
 
54
+ # %%
55
+ #sets_dict, params_dict= src.load_data_from_excel(url_excel,write_to_pickle_flag=True)
56
 
57
+ # %%
58
 
59
 
 
 
 
 
 
 
60
 
61
+ #sets_dict, params_dict= load_data_from_excel(url_excel, load_from_pickle_flag = False)
 
 
62
 
 
63
 
64
+ dt = 6
65
+ # Unpack sets_dict into the workspace
66
+ t = sets_dict['t']
67
+ i = sets_dict['i']
68
+ iSto = sets_dict['iSto']
69
+ iConv = sets_dict['iConv']
70
+ iPtG = sets_dict['iPtG']
71
+ iRes = sets_dict['iRes']
72
+ iHyRes = sets_dict['iHyRes']
73
 
74
+ # Unpack params_dict into the workspace
75
+ l_co2 = params_dict['l_co2']
76
+ p_co2 = params_dict['p_co2']
77
 
78
+ D_t = timstep_aggregate(dt,params_dict['D_t'])
79
+ eff_i = params_dict['eff_i']
80
+ c_fuel_i = params_dict['c_fuel_i']
81
+ c_other_i = params_dict['c_other_i']
82
+ c_inv_i = params_dict['c_inv_i']
83
+ co2_factor_i = params_dict['co2_factor_i']
84
+ #c_var_i = params_dict['c_var_i']
85
+ s_t_r_iRes = timstep_aggregate(dt,params_dict['s_t_r_iRes'])
86
+ K_0_i = params_dict['K_0_i']
87
+ e2p_iSto = params_dict['e2p_iSto']
88
+ h_t = timstep_aggregate(dt,params_dict['h_t'])
89
 
90
+ t = D_t.get_index('t')
91
+ partial_year_factor = (8760/len(t))/dt
92
+ # # Slider for gas price [€/MWh_th]
93
+ #price_gas = st.slider(value=100, min_value=0, max_value=400, label="Natural gas price [€/MWh]", step=10)
94
 
95
+ # Slider for CO2 price [€/t]
96
+ #price_co2 = st.slider(value=0, min_value=0, max_value=400, label="CO2 price [€/t CO2eq]", step=10)
97
 
98
+ with col2:
99
+ # Slider for CO2 limit [mio. t]
100
+ l_co2 = st.slider(value=int(params_dict['l_co2']), min_value=0, max_value=750, label="CO2 limit [mio. t]", step=50)
101
 
102
+ # Slider for H2 price / usevalue [€/MWH_th]
103
+ price_h2 = st.slider(value=100, min_value=0, max_value=300, label="Hydrogen price [€/MWh]", step=10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
+ for i_idx in c_fuel_i.get_index('i'):
106
+ if i_idx in ['Lignite']:
107
+ c_fuel_i.loc[i_idx] = st.slider(value=int(c_fuel_i.loc[i_idx]), min_value=0, max_value=300, label=i_idx + ' Price' , step=10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ with col3:
110
+ # Slider for CO2 limit [mio. t]
111
+ for i_idx in c_fuel_i.get_index('i'):
112
+ if i_idx in ['Fossil Hard coal', 'Fossil Oil','Fossil Gas']:
113
+ c_fuel_i.loc[i_idx] = st.slider(value=int(c_fuel_i.loc[i_idx]), min_value=0, max_value=300, label=i_idx + ' Price' , step=10)
114
 
115
 
116
+ #time_steps_aggregate = 6
117
+ #= xr_profiles.rolling( time_step = time_steps_aggregate).mean().sel(time_step = time[0::time_steps_aggregate])
118
+ price_co2 = 0
119
 
120
 
121
+ #technologies_no_invest = st.multiselect(label='Technolgy invest', options=i)
122
+ technologies_no_invest = ['Electrolyzer','Biomass','RoR']
123
  # %%
124
  ### Variables
125
  m = Model()
 
134
  l = m.add_variables(coords = [t,i], name = 'l', lower = 0) # Storage filling level
135
  w = m.add_variables(coords = [t], name = 'w', lower = 0) # RES curtailment
136
 
 
137
 
138
  ## Objective function
139
  C_tot = C_op + C_inv
 
141
 
142
  ## Costs terms for objective function
143
  # Operational costs minus revenue for produced hydrogen
144
+ C_op_sum = m.add_constraints((y * c_fuel_i/eff_i).sum()*dt*partial_year_factor == C_op, name = 'C_op_sum')
 
145
 
146
  # Investment costs
147
  C_inv_sum = m.add_constraints((K * c_inv_i).sum() == C_inv, name = 'C_inv_sum')
148
 
149
  ## Load serving
150
+ loadserve_t = m.add_constraints(((y ).sum(dims = 'i') - (w ) - y_ch.sum(dims = 'i') == D_t.sel(t = t) ), name = 'load')
151
 
152
  ## Maximum capacity limit
153
  maxcap_i_t = m.add_constraints((y - K <= K_0_i), name = 'max_cap')
154
 
155
  ## Maximum capacity limit
156
+ maxcap_invest_i = m.add_constraints((K.sel(i = technologies_no_invest) <= 0), name = 'max_cap_invest')
157
 
158
  ## Maximum storage charging and discharging
159
  maxcha_iSto_t = m.add_constraints((y.sel(i = iSto) + y_ch.sel(i = iSto) - K.sel(i = iSto) <= K_0_i.sel(i = iSto)), name = 'max_cha')
 
174
  filling_iSto_t = m.add_constraints(l.sel(i = iSto) - (l.sel(i = iSto).roll(t = -1) + (y.sel(i = iSto) ) * dt - y_ch.sel(i = iSto) * eff_i.sel(i = iSto) * dt) == 0, name = 'filling_level')
175
 
176
  ## CO2 limit --> ggf. hier auch mit Subset arbeiten (Technologien, die Brennstoff verbrauchen). (JR)
177
+ CO2_limit = m.add_constraints(((y / eff_i) * co2_factor_i * dt).sum()* partial_year_factor <= l_co2*1_000_000 , name = 'CO2_limit')
178
 
179
 
180
  # %%
181
  m.solve(solver_name = 'highs')
182
 
183
+ st.markdown("---")
 
 
 
 
 
184
 
185
+ colb1, colb2 = st.columns(2)
 
186
 
187
+ # %%
188
+ #c_var_i.to_dataframe(name='VarCosts')
189
  # %%
190
  # Installed Cap
191
  # Assuming df_excel has columns 'All' and 'Capacities'
192
 
193
  fig = px.bar((m.solution['K']+K_0_i).to_dataframe(name='K').reset_index(), \
194
+ y='i', x='K', orientation='h', title='Total Installed Capacities', color='i')
195
 
196
+ #fig
197
 
198
  # %%
199
+ df_new_capacities = m.solution['K'].to_dataframe().reset_index()
200
  fig = px.bar(m.solution['K'].to_dataframe().reset_index(), y='i', x='K', orientation='h', title='New Capacities', color='i')
201
 
202
+ with colb1:
203
+ fig
204
 
 
205
  # %%
206
+ i_with_capacity = m.solution['K'].where( m.solution['K'] > 0).dropna(dim = 'i').get_index('i')
207
+ df_production = m.solution['y'].sel(i = i_with_capacity).to_dataframe().reset_index()
208
  fig = px.area(m.solution['y'].sel(i = i_with_capacity).to_dataframe().reset_index(), y='y', x='t', title='Production', color='i')
209
 
210
+
211
+ with colb2:
212
+ fig
213
+
214
+
215
  # %%
216
+
217
+ df_price = m.constraints['load'].dual.to_dataframe().reset_index()
218
+ df_price['dual'] = df_price['dual']/dt
219
+
220
  # %%
221
+ fig = px.line(df_price, y='dual', x='t', title='Prices')
222
+ with colb1:
223
+ fig
224
 
 
225
 
226
  # %%
227
 
228
+ df_contr_marg = m.constraints['max_cap'].dual.to_dataframe().reset_index()
229
+ df_contr_marg['dual'] = df_contr_marg['dual']/dt
230
+ # %%
231
+
232
+ fig = px.line(m.constraints['max_cap'].dual.to_dataframe().reset_index(), y='dual', x='t',title='contribution margin', color='i')
233
+ with colb2:
234
+ fig
235
+
236
+
237
+
238
+ # %%
239
+ df_Co2_price = m.constraints['CO2_limit'].dual.values
240
+
241
+
242
+
243
+ import pandas as pd
244
+ from io import BytesIO
245
+ #from pyxlsb import open_workbook as open_xlsb
246
+ import streamlit as st
247
+ import xlsxwriter
248
+ # %%
249
+ output = BytesIO()
250
+
251
+
252
+ # Create a Pandas Excel writer using XlsxWriter as the engine
253
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
254
+ # Write each DataFrame to a different sheet
255
+ df_price.to_excel(writer, sheet_name='Prices', index=False)
256
+ df_contr_marg.to_excel(writer, sheet_name='Contribution Margin', index=False)
257
+ df_new_capacities.to_excel(writer, sheet_name='Capcities', index=False)
258
+ df_production.to_excel(writer, sheet_name='Capcities', index=False)
259
+
260
+
261
+ with col4:
262
+ st.download_button(
263
+ label="Download Excel workbook Results",
264
+ data=output.getvalue(),
265
+ file_name="workbook.xlsx",
266
+ mime="application/vnd.ms-excel"
267
+ )
model_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:05322da706d8d1acc11be0c013f3992ca967424ecc9f12c41ea92985af49cfa2
3
+ size 1373491
sourced.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ import pandas as pd
3
+
4
+ import pickle
5
+
6
+ # Define the file path for the pickle file
7
+ pickle_file_path = 'model_data.pkl'
8
+
9
+ # Function to save dictionaries to a pickle file
10
+ def save_to_pickle(sets_dict, params_dict):
11
+ with open(pickle_file_path, 'wb') as file:
12
+ pickle.dump({'sets': sets_dict, 'params': params_dict}, file)
13
+
14
+ # Function to load dictionaries from a pickle file
15
+ def load_from_pickle():
16
+ with open(pickle_file_path, 'rb') as file:
17
+ data = pickle.load(file)
18
+ return data['sets'], data['params']
19
+
20
+
21
+
22
+ def load_data_from_excel(url_excel,load_from_pickle_flag = False, write_to_pickle_flag = True):
23
+
24
+
25
+ if load_from_pickle_flag:
26
+ # Load dictionaries from the pickle file
27
+ loaded_sets_dict, loaded_params_dict = load_from_pickle()
28
+ return loaded_sets_dict, loaded_params_dict
29
+
30
+ # Timesteps
31
+ df_excel = pd.read_excel(url_excel, sheet_name='Timesteps_All', header=None)
32
+ t = pd.Index(df_excel.iloc[:, 0], name='t')
33
+
34
+ # Technologies
35
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
36
+ i = pd.Index(df_excel.iloc[:, 0], name='i')
37
+
38
+
39
+
40
+
41
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
42
+ iConv = pd.Index(df_excel.iloc[0:7, 2], name='iConv')
43
+
44
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
45
+ iRes = pd.Index(df_excel.iloc[0:4, 4], name='iRes')
46
+
47
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
48
+ iSto = pd.Index(df_excel.iloc[0:2, 6], name='iSto')
49
+
50
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
51
+ iPtG = pd.Index(df_excel.iloc[0:1, 8], name='iPtG')
52
+
53
+
54
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
55
+ iHyRes = pd.Index(df_excel.iloc[0:1, 10], name='iHyRes')
56
+
57
+ # Parameters
58
+ l_co2 = pd.read_excel(url_excel, sheet_name='CO2_Cap').iloc[0,0]
59
+ p_co2 = 0
60
+ dt = 1
61
+
62
+
63
+ # Demand
64
+ df_excel= pd.read_excel(url_excel, sheet_name = 'Demand')
65
+ #df_melt = pd.melt(df_excel, id_vars='Zeit')
66
+ df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Unnamed: 1':'Demand'})
67
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
68
+ df_excel = df_excel.fillna(0)
69
+ df_excel = df_excel.set_index('t')
70
+ D_t = df_excel.iloc[:,0].to_xarray()
71
+ ## Efficiencies
72
+ df_excel = pd.read_excel(url_excel, sheet_name = 'Efficiency')
73
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'Efficiency'})
74
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
75
+ df_excel = df_excel.fillna(0)
76
+ df_excel = df_excel.set_index('i')
77
+ eff_i = df_excel.iloc[:,0].to_xarray()
78
+
79
+ ## Variable costs
80
+ # Fuel costs
81
+ df_excel = pd.read_excel(url_excel, sheet_name = 'FuelCosts')
82
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'FuelCosts'})
83
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
84
+ df_excel = df_excel.fillna(0)
85
+ df_excel = df_excel.set_index('i')
86
+ c_fuel_i = df_excel.iloc[:,0].to_xarray()
87
+ # Apply slider value
88
+ #c_fuel_i.loc[dict(i = 'Fossil Gas')] = price_gas
89
+ #c_fuel_i.loc[dict(i = 'H2')] = price_h2
90
+
91
+ # Other var. costs
92
+ df_excel = pd.read_excel(url_excel, sheet_name = 'OtherVarCosts')
93
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'OtherVarCosts'})
94
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
95
+ df_excel = df_excel.fillna(0)
96
+ df_excel = df_excel.set_index('i')
97
+ c_other_i = df_excel.iloc[:,0].to_xarray()
98
+
99
+ # Investment costs
100
+ df_excel = pd.read_excel(url_excel, sheet_name = 'InvCosts')
101
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InvCosts'})
102
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
103
+ df_excel = df_excel.fillna(0)
104
+ df_excel = df_excel.set_index('i')
105
+ c_inv_i = df_excel.iloc[:,0].to_xarray()*1000*0.1 # kw to MW and annuity factor
106
+
107
+ # Emission factor
108
+ df_excel = pd.read_excel(url_excel, sheet_name = 'EmFactor')
109
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'EmFactor'})
110
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
111
+ df_excel = df_excel.fillna(0)
112
+ df_excel = df_excel.set_index('i')
113
+ co2_factor_i = df_excel.iloc[:,0].to_xarray()
114
+
115
+ ## Calculation of variable costs
116
+ c_var_i = (c_fuel_i.sel(i = iConv) + p_co2 * co2_factor_i.sel(i = iConv)) / eff_i.sel(i = iConv) + c_other_i.sel(i = iConv)
117
+
118
+ # RES capacity factors
119
+ #df_excel = pd.read_excel(url_excel, sheet_name = 'RES',header=[0,1])
120
+ #df_excel = pd.read_excel(url_excel, sheet_name = 'RES', index_col=['Timesteps'], columns=['PV', 'WindOn', 'WindOff', 'RoR'])
121
+ df_excel = pd.read_excel(url_excel, sheet_name = 'RES')
122
+ df_excel = df_excel.set_index(['Timesteps'])
123
+ df_test = df_excel
124
+ df_excel = df_excel.stack()
125
+ #df_excel = df_excel.rename(columns={'PV', 'WindOn', 'WindOff', 'RoR'})
126
+ df_test2 = df_excel
127
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
128
+ #df_excel = df_excel.fillna(0)
129
+
130
+ #df_test = df_excel.set_index(['Timesteps', 'PV', 'WindOn', 'WindOff', 'RoR']).stack([0])
131
+ #df_test.index = df_test.index.set_names(['t','i'])
132
+ s_t_r_iRes = df_excel.to_xarray().rename({'level_1': 'i','Timesteps':'t'})
133
+
134
+ #s_t_r_iRes = df_excel.iloc[:,0].to_xarray()
135
+
136
+ # Base capacities
137
+ df_excel = pd.read_excel(url_excel, sheet_name = 'InstalledCap')
138
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InstalledCap'})
139
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
140
+ df_excel = df_excel.fillna(0)
141
+ df_excel = df_excel.set_index('i')
142
+ K_0_i = df_excel.iloc[:,0].to_xarray()
143
+
144
+ # Energy-to-power ratio storages
145
+ df_excel = pd.read_excel(url_excel, sheet_name = 'E2P')
146
+ df_excel = df_excel.rename(columns = {'Storage':'i', 'Unnamed: 1':'E2P ratio'})
147
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
148
+ df_excel = df_excel.fillna(0)
149
+ df_excel = df_excel.set_index('i')
150
+ e2p_iSto = df_excel.iloc[:,0].to_xarray()
151
+
152
+ # Inflow for hydro reservoir
153
+ df_excel = pd.read_excel(url_excel, sheet_name = 'HydroInflow')
154
+ df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Hydro Water Reservoir':'Inflow'})
155
+ df_excel = df_excel.fillna(0)
156
+ df_excel = df_excel.set_index('t')
157
+ h_t = df_excel.iloc[:,0].to_xarray()
158
+
159
+
160
+
161
+ sets_dict = {}
162
+ params_dict = {}
163
+ # Append parameters to the dictionary
164
+ sets_dict['t'] = t
165
+ sets_dict['i'] = i
166
+ sets_dict['iSto'] = iSto
167
+ sets_dict['iConv'] = iConv
168
+ sets_dict['iPtG'] = iPtG
169
+ sets_dict['iRes'] = iRes
170
+ sets_dict['iHyRes'] = iHyRes
171
+ # Append parameters to the dictionary
172
+ params_dict['l_co2'] = l_co2
173
+ params_dict['p_co2'] = p_co2
174
+ params_dict['dt'] = dt
175
+ params_dict['D_t'] = D_t
176
+ params_dict['eff_i'] = eff_i
177
+ params_dict['c_fuel_i'] = c_fuel_i
178
+ params_dict['c_other_i'] = c_other_i
179
+ params_dict['c_inv_i'] = c_inv_i
180
+ params_dict['co2_factor_i'] = co2_factor_i
181
+ params_dict['c_var_i'] = c_var_i
182
+ params_dict['s_t_r_iRes'] = s_t_r_iRes
183
+ params_dict['K_0_i'] = K_0_i
184
+ params_dict['e2p_iSto'] = e2p_iSto
185
+ params_dict['h_t'] = h_t
186
+
187
+ if write_to_pickle_flag:
188
+ save_to_pickle(sets_dict, params_dict)
189
+
190
+ return sets_dict, params_dict
191
+
192
+
193
+ # %%
194
+ # # Example usage:
195
+ # url_excel = "Input_Jahr_2021.xlsx" # Replace with your actual file path
196
+ # limit_co2 = 0.5
197
+ # price_co2 = 50
198
+ # price_gas = 3
199
+ # price_h2 = 5
200
+
201
+ # sets, params = load_data_from_excel(url_excel,write_to_pickle_flag=True)
202
+
203
+ # # %%
204
+ # sets, params = load_data_from_excel(url_excel,load_from_pickle_flag=True)
205
+ # # %%