ConniKLu commited on
Commit
c70152b
·
1 Parent(s): 49616e7

add basic files

Browse files
Files changed (9) hide show
  1. .gitignore +4 -0
  2. 01_InstallPythonEnv.bat +41 -0
  3. README.md +2 -12
  4. StartServer.bat +2 -0
  5. activate.bat +1 -0
  6. app.py +348 -0
  7. model_data.pkl +3 -0
  8. requirements.txt +87 -0
  9. sourced.py +200 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ myenvEEW
2
+
3
+ # PowerPoint temporary
4
+ ~$*.pptx*
01_InstallPythonEnv.bat ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ REM Check if Python 3.11 is installed
2
+
3
+ REM Replace "3.x" with the desired Python version (e.g., 3.8, 3.9, etc.)
4
+ set PythonVersion=3.9.0
5
+ set "PythonVersionMain=%PythonVersion:~0,3%"
6
+ set "PythonVersionWithoutDots=%PythonVersionMain:.=%"
7
+ REM Replace "myenv" with the desired name for the virtual environment
8
+ echo %PythonVersionWithoutDots%
9
+ set EnvName= myenvEEW
10
+
11
+
12
+
13
+ if exist %LocalAppData%\Programs\Python\Python39\python.exe (
14
+ %LocalAppData%\Programs\Python\Python39\python -m venv %EnvName%
15
+
16
+ ) else (
17
+ echo Python 3.9 not found in user app folder.. searching fo install for all users
18
+ if exist "%ProgramFiles%\Programs\Python39\python.exe" (
19
+ %ProgramFiles%\Programs\Python39\python -m venv %EnvName%
20
+ ) else (
21
+ echo Python 3.11 is not installed. Installing Python 3.9...
22
+ REM Download and install Python 3.11.0 in the current directory
23
+ curl -o python_installer.exe https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe
24
+ python_installer.exe \silent InstallAllUsers=0 PrependPath=0 Include_test=0
25
+ del python_installer.exe
26
+ %LocalAppData%\Programs\Python\Python39\Python -m venv %EnvName%
27
+ )
28
+ )
29
+
30
+
31
+ REM Activate the virtual environment
32
+ call myenvEEW\Scripts\activate
33
+
34
+ REM Install required packages
35
+ pip install -r requirements.txt
36
+
37
+ REM Deactivate the virtual environment
38
+ deactivate
39
+
40
+ REM This will pause the Command Prompt
41
+ call cmd
README.md CHANGED
@@ -1,12 +1,2 @@
1
- ---
2
- title: EEW Model 1
3
- emoji: 👀
4
- colorFrom: blue
5
- colorTo: purple
6
- sdk: streamlit
7
- sdk_version: 1.33.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # EEW_Peak_Load_Model
2
+ Simplified version of the ERD Peak_Load_Model
 
 
 
 
 
 
 
 
 
 
StartServer.bat ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ call activate
2
+ call streamlit run app.py
activate.bat ADDED
@@ -0,0 +1 @@
 
 
1
+ call myenv/Scripts/activate
app.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Spyder Editor
5
+
6
+ This is a temporary script file.
7
+ """
8
+
9
+
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
+ # %%
28
+ # Color dictionary for figures
29
+ color_dict = {'Biomass': 'lightgreen',
30
+ 'Lignite': 'brown',
31
+ 'Fossil Gas': 'grey',
32
+ 'Fossil Hard coal': 'darkgrey',
33
+ 'Fossil Oil': 'maroon',
34
+ 'RoR': 'aquamarine',
35
+ 'Hydro Water Reservoir': 'azure',
36
+ 'Nuclear': 'orange',
37
+ 'PV': 'yellow',
38
+ 'WindOff': 'darkblue',
39
+ 'WindOn': 'green',
40
+ 'H2': 'crimson',
41
+ 'Pumped Hydro Storage': 'lightblue',
42
+ 'Battery storages': 'red',
43
+ 'Electrolyzer': 'olive'}
44
+
45
+ # %%
46
+ with col1:
47
+ with open('Input_Jahr_2021.xlsx', 'rb') as f:
48
+ st.download_button('Download Excel Template', f, file_name='Input_Jahr_2021.xlsx') # Defaults to 'application/octet-stream'
49
+
50
+ #url_excel = r'Input_Jahr_2021.xlsx'
51
+ url_excel = st.file_uploader(label = 'Excel Upload')
52
+
53
+ # %%
54
+ if url_excel == None:
55
+ url_excel = r'Input_Jahr_2021.xlsx'
56
+ sets_dict, params_dict= src.load_data_from_excel(url_excel, load_from_pickle_flag = True)
57
+ with col4:
58
+ st.write('Running with standard data')
59
+ else:
60
+ sets_dict, params_dict= src.load_data_from_excel(url_excel, load_from_pickle_flag = False)
61
+ with col4:
62
+ st.write('Running with user data')
63
+
64
+ # %%
65
+
66
+ def timstep_aggregate(time_steps_aggregate, xr ):
67
+ return xr.rolling( t = time_steps_aggregate).mean().sel(t = t[0::time_steps_aggregate])
68
+
69
+ #s_t_r_iRes = timstep_aggregate(6,s_t_r_iRes)
70
+
71
+
72
+
73
+ # %%
74
+ #sets_dict, params_dict= src.load_data_from_excel(url_excel,write_to_pickle_flag=True)
75
+
76
+ # %%
77
+ # sets_dict, params_dict= load_data_from_excel(url_excel, load_from_pickle_flag = False)
78
+ dt = 6
79
+ # Unpack sets_dict into the workspace
80
+ t = sets_dict['t']
81
+ i = sets_dict['i']
82
+ iSto = sets_dict['iSto']
83
+ iConv = sets_dict['iConv']
84
+ iPtG = sets_dict['iPtG']
85
+ iRes = sets_dict['iRes']
86
+ iHyRes = sets_dict['iHyRes']
87
+
88
+ # Unpack params_dict into the workspace
89
+ l_co2 = params_dict['l_co2']
90
+ p_co2 = params_dict['p_co2']
91
+
92
+ # %%
93
+ eff_i = params_dict['eff_i']
94
+ c_fuel_i = params_dict['c_fuel_i']
95
+ c_other_i = params_dict['c_other_i']
96
+ c_inv_i = params_dict['c_inv_i']
97
+ co2_factor_i = params_dict['co2_factor_i']
98
+ #c_var_i = params_dict['c_var_i']
99
+ K_0_i = params_dict['K_0_i']
100
+ e2p_iSto = params_dict['e2p_iSto']
101
+ # Aggregate time series
102
+ D_t = timstep_aggregate(dt,params_dict['D_t'])
103
+ s_t_r_iRes = timstep_aggregate(dt,params_dict['s_t_r_iRes'])
104
+ h_t = timstep_aggregate(dt,params_dict['h_t'])
105
+ t = D_t.get_index('t')
106
+ partial_year_factor = (8760/len(t))/dt
107
+
108
+ # %%
109
+ # Sliders and input boxes for parameters
110
+ with col2:
111
+ # Slider for CO2 limit [mio. t]
112
+ l_co2 = st.slider(value=int(params_dict['l_co2']), min_value=0, max_value=750, label="CO2 limit [mio. t]", step=50)
113
+
114
+ # Slider for H2 price / usevalue [€/MWH_th]
115
+ price_h2 = st.slider(value=100, min_value=0, max_value=300, label="Hydrogen price [€/MWh]", step=10)
116
+
117
+ for i_idx in c_fuel_i.get_index('i'):
118
+ if i_idx in ['Lignite']:
119
+ 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)
120
+
121
+ dt = st.number_input(label="Length of timesteps [int]", min_value=1, max_value=len(t), value=6, help="Enter only integers between 1 and 8760 (or 8784 for leap years).")
122
+
123
+ with col3:
124
+ # Slider for CO2 limit [mio. t]
125
+ for i_idx in c_fuel_i.get_index('i'):
126
+ if i_idx in ['Fossil Hard coal', 'Fossil Oil','Fossil Gas']:
127
+ 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)
128
+
129
+ technologies_invest = st.multiselect(label='Technologies for investment', options=i, default=['Lignite','Fossil Gas','Fossil Hard coal','Fossil Oil','PV','WindOff','WindOn','H2','Pumped Hydro Storage','Battery storages'])
130
+ technologies_no_invest = [x for x in i if x not in technologies_invest]
131
+
132
+ #time_steps_aggregate = 6
133
+ #= xr_profiles.rolling( time_step = time_steps_aggregate).mean().sel(time_step = time[0::time_steps_aggregate])
134
+ price_co2 = 0
135
+
136
+ # Aggregate time series
137
+ #D_t = timstep_aggregate(dt,params_dict['D_t'])
138
+ #s_t_r_iRes = timstep_aggregate(dt,params_dict['s_t_r_iRes'])
139
+ #h_t = timstep_aggregate(dt,params_dict['h_t'])
140
+ #t = D_t.get_index('t')
141
+ #partial_year_factor = (8760/len(t))/dt
142
+
143
+ #technologies_no_invest = st.multiselect(label='Technolgy invest', options=i)
144
+ #technologies_no_invest = ['Electrolyzer','Biomass','RoR','Hydro Water Reservoir','Nuclear']
145
+ # %%
146
+ ### Variables
147
+ m = Model()
148
+
149
+ C_tot = m.add_variables(name = 'C_tot') # Total costs
150
+ C_op = m.add_variables(name = 'C_op', lower = 0) # Operational costs
151
+ C_inv = m.add_variables(name = 'C_inv', lower = 0) # Investment costs
152
+
153
+ K = m.add_variables(coords = [i], name = 'K', lower = 0) # Endogenous capacity
154
+ y = m.add_variables(coords = [t,i], name = 'y', lower = 0) # Electricity production --> für Elektrolyseure ausschließen
155
+ y_ch = m.add_variables(coords = [t,i], name = 'y_ch', lower = 0) # Electricity consumption --> für alles außer Elektrolyseure und Speicher ausschließen
156
+ l = m.add_variables(coords = [t,i], name = 'l', lower = 0) # Storage filling level
157
+ w = m.add_variables(coords = [t], name = 'w', lower = 0) # RES curtailment
158
+ y_curt = m.add_variables(coords = [t,i], name = 'y_curt', lower = 0)
159
+ y_h2 = m.add_variables(coords = [t,i], name = 'y_h2', lower = 0)
160
+
161
+ ## Objective function
162
+ C_tot = C_op + C_inv
163
+ m.add_objective(C_tot)
164
+
165
+ ## Costs terms for objective function
166
+ # Operational costs minus revenue for produced hydrogen
167
+ C_op_sum = m.add_constraints((y * c_fuel_i/eff_i).sum() * dt - (y_h2.sel(i = iPtG) * price_h2).sum() * dt == C_op, name = 'C_op_sum')
168
+
169
+ # Investment costs
170
+ C_inv_sum = m.add_constraints((K * c_inv_i).sum() == C_inv, name = 'C_inv_sum')
171
+
172
+ ## Load serving
173
+ loadserve_t = m.add_constraints((((y ).sum(dims = 'i') - y_ch.sum(dims = 'i')) * dt == D_t.sel(t = t) * dt), name = 'load')
174
+
175
+ ## Maximum capacity limit
176
+ maxcap_i_t = m.add_constraints((y - K <= K_0_i), name = 'max_cap')
177
+
178
+ ## Maximum capacity limit
179
+ maxcap_invest_i = m.add_constraints((K.sel(i = technologies_no_invest) <= 0), name = 'max_cap_invest')
180
+
181
+ ## Prevent power production by PtG
182
+ no_power_prod_iPtG_t = m.add_constraints((y.sel(i = iPtG) <= 0), name = 'prevent_ptg_prod')
183
+
184
+ ## Maximum storage charging and discharging
185
+ 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')
186
+
187
+ ## Maximum electrolyzer capacity
188
+ ptg_prod_iPtG_t = m.add_constraints((y_ch.sel(i = iPtG) - K.sel(i = iPtG) <= K_0_i.sel(i = iPtG)), name = 'max_cha_ptg')
189
+
190
+ ## PtG H2 production
191
+ h2_prod_iPtG_t = m.add_constraints(y_ch.sel(i = iPtG) * eff_i.sel(i = iPtG) == y_h2.sel(i = iPtG), name = 'ptg_h2_prod')
192
+
193
+ ## Infeed of renewables
194
+ infeed_iRes_t = m.add_constraints((y.sel(i = iRes) - s_t_r_iRes.sel(i = iRes).sel(t = t) * K.sel(i = iRes) + y_curt.sel(i = iRes) == s_t_r_iRes.sel(i = iRes).sel(t = t) * K_0_i.sel(i = iRes)), name = 'infeed')
195
+
196
+ ## Maximum filling level restriction storage power plant
197
+ maxcapsto_iSto_t = m.add_constraints((l.sel(i = iSto) - K.sel(i = iSto) * e2p_iSto.sel(i = iSto) <= K_0_i.sel(i = iSto) * e2p_iSto.sel(i = iSto)), name = 'max_sto_filling')
198
+
199
+ ## Filling level restriction hydro reservoir
200
+ filling_iHydro_t = m.add_constraints(l.sel(i = iHyRes) - l.sel(i = iHyRes).roll(t = -1) + y.sel(i = iHyRes) * dt == h_t.sel(t = t) * dt, name = 'filling_level_hydro')
201
+
202
+ ## Filling level restriction other storages
203
+ filling_iSto_t = m.add_constraints(l.sel(i = iSto) - (l.sel(i = iSto).roll(t = -1) + (y.sel(i = iSto) / eff_i.sel(i = iSto)) * dt - y_ch.sel(i = iSto) * eff_i.sel(i = iSto) * dt) == 0, name = 'filling_level')
204
+
205
+ ## CO2 limit
206
+ CO2_limit = m.add_constraints(((y / eff_i) * co2_factor_i * dt).sum() <= l_co2 * 1_000_000 , name = 'CO2_limit')
207
+
208
+
209
+ # %%
210
+ m.solve(solver_name = 'highs')
211
+
212
+ st.markdown("---")
213
+
214
+ colb1, colb2 = st.columns(2)
215
+
216
+ # %%
217
+ #c_var_i.to_dataframe(name='VarCosts')
218
+ # %%
219
+ # Installed Cap
220
+ # Assuming df_excel has columns 'All' and 'Capacities'
221
+
222
+ fig = px.bar((m.solution['K']+K_0_i).to_dataframe(name='K').reset_index(), \
223
+ y='i', x='K', orientation='h', title='Total Installed Capacities [MW]', color='i')
224
+
225
+ #fig
226
+
227
+ # %%
228
+ total_costs = float(m.solution['C_inv'].values) + float(m.solution['C_op'].values)
229
+ total_costs_rounded = round(total_costs/1e9, 2)
230
+ df_total_costs = pd.DataFrame({'Total costs':[total_costs]})
231
+
232
+ with colb1:
233
+ st.write('Total costs: ' + str(total_costs_rounded) + ' bn. €')
234
+
235
+ # %%
236
+ #df_Co2_price = pd.DataFrame({'CO2_Price: ':[float(m.constraints['CO2_limit'].dual.values) * (-1)]})
237
+ CO2_price = float(m.constraints['CO2_limit'].dual.values) * (-1)
238
+ CO2_price_rounded = round(CO2_price, 2)
239
+ df_CO2_price = pd.DataFrame({'CO2 price':[CO2_price]})
240
+
241
+ with colb2:
242
+ #st.write(str(df_Co2_price))
243
+ st.write('CO2 price: ' + str(CO2_price_rounded) + ' €/t')
244
+
245
+ # %%
246
+ df_new_capacities = m.solution['K'].to_dataframe().reset_index()
247
+ fig = px.bar(m.solution['K'].to_dataframe().reset_index(), y='i', x='K', orientation='h', title='New Capacities [MW]', color='i', color_discrete_map=color_dict)
248
+
249
+ with colb1:
250
+ fig
251
+
252
+ # %%
253
+ i_with_capacity = m.solution['K'].where( m.solution['K'] > 0).dropna(dim = 'i').get_index('i')
254
+ df_production = m.solution['y'].sel(i = i_with_capacity).to_dataframe().reset_index()
255
+ fig = px.area(m.solution['y'].sel(i = i_with_capacity).to_dataframe().reset_index(), y='y', x='t', title='Production [MWh]', color='i', color_discrete_map=color_dict)
256
+ fig.update_traces(line=dict(width=0))
257
+ fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
258
+
259
+ with colb2:
260
+ fig
261
+
262
+ # %%
263
+
264
+ df_price = m.constraints['load'].dual.to_dataframe().reset_index()
265
+ #df_price['dual'] = df_price['dual']
266
+
267
+ # %%
268
+ fig = px.line(df_price, y='dual', x='t', title='Electricity prices [€/MWh]', range_y=[0,250])
269
+ with colb1:
270
+ fig
271
+
272
+ # %% price duration curve
273
+ # sort df_price by dual
274
+ df_price_sorted = df_price.sort_values('dual', ascending=False)
275
+ # %%
276
+
277
+ df_contr_marg = m.constraints['max_cap'].dual.to_dataframe().reset_index()
278
+ df_contr_marg['dual'] = df_contr_marg['dual'] / dt * (-1)
279
+
280
+ # %%
281
+
282
+ fig = px.line(df_contr_marg, y='dual', x='t',title='Contribution margin [€]', color='i', range_y=[0,250], color_discrete_map=color_dict)
283
+ with colb2:
284
+ fig
285
+
286
+ # %%
287
+
288
+ # curtailment
289
+ df_curtailment = m.solution['y_curt'].sel(i = iRes).to_dataframe().reset_index()
290
+ fig = px.area(m.solution['y_curt'].sel(i = iRes).to_dataframe().reset_index(), y='y_curt', x='t', title='Curtailment [MWh]', color='i', color_discrete_map=color_dict)
291
+ fig.update_traces(line=dict(width=0))
292
+ fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
293
+
294
+ with colb1:
295
+ fig
296
+
297
+ # %%
298
+ df_charging = m.solution['y_ch'].sel(i = iSto).to_dataframe().reset_index()
299
+ fig = px.area(m.solution['y_ch'].sel(i = iSto).to_dataframe().reset_index(), y='y_ch', x='t', title='Storage charging [MWh]', color='i', color_discrete_map=color_dict)
300
+ fig.update_traces(line=dict(width=0))
301
+ fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
302
+
303
+ with colb2:
304
+ fig
305
+
306
+ # %%
307
+ df_h2_prod = m.solution['y_h2'].sel(i = iPtG).to_dataframe().reset_index()
308
+ fig = px.area(m.solution['y_h2'].sel(i = iPtG).to_dataframe().reset_index(), y='y_h2', x='t', title='Hydrogen production [MWh_th]', color='i', color_discrete_map=color_dict)
309
+ fig.update_traces(line=dict(width=0))
310
+ fig.for_each_trace(lambda trace: trace.update(fillcolor = trace.line.color))
311
+
312
+ with colb2:
313
+ fig
314
+
315
+ # %%
316
+ ((m.solution['y'] / eff_i) * co2_factor_i * dt).sum()
317
+ # %%
318
+
319
+ import pandas as pd
320
+ from io import BytesIO
321
+ #from pyxlsb import open_workbook as open_xlsb
322
+ import streamlit as st
323
+ import xlsxwriter
324
+ # %%
325
+ output = BytesIO()
326
+
327
+
328
+ # Create a Pandas Excel writer using XlsxWriter as the engine
329
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
330
+ # Write each DataFrame to a different sheet
331
+ df_total_costs.to_excel(writer, sheet_name='Total costs', index=False)
332
+ df_CO2_price.to_excel(writer, sheet_name='CO2 price', index=False)
333
+ df_price.to_excel(writer, sheet_name='Prices', index=False)
334
+ df_contr_marg.to_excel(writer, sheet_name='Contribution Margin', index=False)
335
+ df_new_capacities.to_excel(writer, sheet_name='Capacities', index=False)
336
+ df_production.to_excel(writer, sheet_name='Production', index=False)
337
+ df_charging.to_excel(writer, sheet_name='Charging', index=False)
338
+ D_t.to_dataframe().reset_index().to_excel(writer, sheet_name='Demand', index=False)
339
+ df_curtailment.to_excel(writer, sheet_name='Curtailment', index=False)
340
+ df_h2_prod.to_excel(writer, sheet_name='H2 production', index=False)
341
+
342
+ with col4:
343
+ st.download_button(
344
+ label="Download Excel workbook Results",
345
+ data=output.getvalue(),
346
+ file_name="workbook.xlsx",
347
+ mime="application/vnd.ms-excel"
348
+ )
model_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:882997f874bcad2db648bdfb6559637c77e7767b8f7d81311111b01191e84b31
3
+ size 1373467
requirements.txt ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.1.1
2
+ asttokens==2.4.0
3
+ attrs==23.1.0
4
+ backcall==0.2.0
5
+ blinker==1.6.2
6
+ Bottleneck==1.3.7
7
+ cachetools==5.3.1
8
+ certifi==2023.7.22
9
+ charset-normalizer==3.2.0
10
+ click==8.1.7
11
+ cloudpickle==2.2.1
12
+ colorama==0.4.6
13
+ comm==0.1.4
14
+ dask==2023.9.2
15
+ debugpy==1.8.0
16
+ decorator==5.1.1
17
+ deprecation==2.1.0
18
+ et-xmlfile==1.1.0
19
+ executing==1.2.0
20
+ fsspec==2023.9.2
21
+ gitdb==4.0.10
22
+ GitPython==3.1.37
23
+ highspy==1.5.3
24
+ idna==3.4
25
+ importlib-metadata==6.8.0
26
+ ipykernel==6.25.2
27
+ ipython==8.15.0
28
+ jedi==0.19.0
29
+ Jinja2==3.1.2
30
+ jsonschema==4.19.1
31
+ jsonschema-specifications==2023.7.1
32
+ jupyter_client==8.3.1
33
+ jupyter_core==5.3.1
34
+ linopy==0.2.6
35
+ locket==1.0.0
36
+ markdown-it-py==3.0.0
37
+ MarkupSafe==2.1.3
38
+ matplotlib-inline==0.1.6
39
+ mdurl==0.1.2
40
+ nest-asyncio==1.5.8
41
+ numexpr==2.8.6
42
+ numpy==1.26.0
43
+ openpyxl==3.1.2
44
+ packaging==23.1
45
+ pandas==2.1.1
46
+ parso==0.8.3
47
+ partd==1.4.1
48
+ pickleshare==0.7.5
49
+ Pillow==9.5.0
50
+ platformdirs==3.10.0
51
+ plotly==5.17.0
52
+ prompt-toolkit==3.0.39
53
+ protobuf==4.24.3
54
+ psutil==5.9.5
55
+ pure-eval==0.2.2
56
+ pyarrow==13.0.0
57
+ pydeck==0.8.1b0
58
+ Pygments==2.16.1
59
+ python-dateutil==2.8.2
60
+ pytz==2023.3.post1
61
+ pywin32==306
62
+ PyYAML==6.0.1
63
+ pyzmq==25.1.1
64
+ referencing==0.30.2
65
+ requests==2.31.0
66
+ rich==13.5.3
67
+ rpds-py==0.10.3
68
+ scipy==1.11.2
69
+ six==1.16.0
70
+ smmap==5.0.1
71
+ stack-data==0.6.2
72
+ streamlit==1.27.0
73
+ tenacity==8.2.3
74
+ toml==0.10.2
75
+ toolz==0.12.0
76
+ tornado==6.3.3
77
+ tqdm==4.66.1
78
+ traitlets==5.10.0
79
+ typing_extensions==4.8.0
80
+ tzdata==2023.3
81
+ tzlocal==5.0.1
82
+ urllib3==2.0.5
83
+ validators==0.22.0
84
+ watchdog==3.0.0
85
+ wcwidth==0.2.6
86
+ xarray==2023.8.0
87
+ zipp==3.17.0
sourced.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
39
+ iConv = pd.Index(df_excel.iloc[0:7, 2], name='iConv')
40
+
41
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
42
+ iRes = pd.Index(df_excel.iloc[0:4, 4], name='iRes')
43
+
44
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
45
+ iSto = pd.Index(df_excel.iloc[0:2, 6], name='iSto')
46
+
47
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
48
+ iPtG = pd.Index(df_excel.iloc[0:1, 8], name='iPtG')
49
+
50
+ df_excel = pd.read_excel(url_excel, sheet_name='Technologies')
51
+ iHyRes = pd.Index(df_excel.iloc[0:1, 10], name='iHyRes')
52
+
53
+ # Parameters
54
+ l_co2 = pd.read_excel(url_excel, sheet_name='CO2_Cap').iloc[0,0]
55
+ p_co2 = 0
56
+ dt = 1
57
+
58
+ # Demand
59
+ df_excel= pd.read_excel(url_excel, sheet_name = 'Demand')
60
+ #df_melt = pd.melt(df_excel, id_vars='Zeit')
61
+ df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Unnamed: 1':'Demand'})
62
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
63
+ df_excel = df_excel.fillna(0)
64
+ df_excel = df_excel.set_index('t')
65
+ D_t = df_excel.iloc[:,0].to_xarray()
66
+ ## Efficiencies
67
+ df_excel = pd.read_excel(url_excel, sheet_name = 'Efficiency')
68
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'Efficiency'})
69
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
70
+ df_excel = df_excel.fillna(0)
71
+ df_excel = df_excel.set_index('i')
72
+ eff_i = df_excel.iloc[:,0].to_xarray()
73
+
74
+ ## Variable costs
75
+ # Fuel costs
76
+ df_excel = pd.read_excel(url_excel, sheet_name = 'FuelCosts')
77
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'FuelCosts'})
78
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
79
+ df_excel = df_excel.fillna(0)
80
+ df_excel = df_excel.set_index('i')
81
+ c_fuel_i = df_excel.iloc[:,0].to_xarray()
82
+ # Apply slider value
83
+ #c_fuel_i.loc[dict(i = 'Fossil Gas')] = price_gas
84
+ #c_fuel_i.loc[dict(i = 'H2')] = price_h2
85
+
86
+ # Other var. costs
87
+ df_excel = pd.read_excel(url_excel, sheet_name = 'OtherVarCosts')
88
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'OtherVarCosts'})
89
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
90
+ df_excel = df_excel.fillna(0)
91
+ df_excel = df_excel.set_index('i')
92
+ c_other_i = df_excel.iloc[:,0].to_xarray()
93
+
94
+ # Investment costs
95
+ df_excel = pd.read_excel(url_excel, sheet_name = 'InvCosts')
96
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InvCosts'})
97
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
98
+ df_excel = df_excel.fillna(0)
99
+ df_excel = df_excel.set_index('i')
100
+ c_inv_i = df_excel.iloc[:,0].to_xarray()*1000*0.1 # kw to MW and annuity factor
101
+
102
+ # Emission factor
103
+ df_excel = pd.read_excel(url_excel, sheet_name = 'EmFactor')
104
+ df_excel = df_excel.rename(columns = {'Conventionals':'i', 'Unnamed: 1':'EmFactor'})
105
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
106
+ df_excel = df_excel.fillna(0)
107
+ df_excel = df_excel.set_index('i')
108
+ co2_factor_i = df_excel.iloc[:,0].to_xarray()
109
+
110
+ ## Calculation of variable costs
111
+ 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)
112
+
113
+ # RES capacity factors
114
+ #df_excel = pd.read_excel(url_excel, sheet_name = 'RES',header=[0,1])
115
+ #df_excel = pd.read_excel(url_excel, sheet_name = 'RES', index_col=['Timesteps'], columns=['PV', 'WindOn', 'WindOff', 'RoR'])
116
+ df_excel = pd.read_excel(url_excel, sheet_name = 'RES')
117
+ df_excel = df_excel.set_index(['Timesteps'])
118
+ df_test = df_excel
119
+ df_excel = df_excel.stack()
120
+ #df_excel = df_excel.rename(columns={'PV', 'WindOn', 'WindOff', 'RoR'})
121
+ df_test2 = df_excel
122
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
123
+ #df_excel = df_excel.fillna(0)
124
+
125
+ #df_test = df_excel.set_index(['Timesteps', 'PV', 'WindOn', 'WindOff', 'RoR']).stack([0])
126
+ #df_test.index = df_test.index.set_names(['t','i'])
127
+ s_t_r_iRes = df_excel.to_xarray().rename({'level_1': 'i','Timesteps':'t'})
128
+
129
+ #s_t_r_iRes = df_excel.iloc[:,0].to_xarray()
130
+
131
+ # Base capacities
132
+ df_excel = pd.read_excel(url_excel, sheet_name = 'InstalledCap')
133
+ df_excel = df_excel.rename(columns = {'All':'i', 'Unnamed: 1':'InstalledCap'})
134
+ df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
135
+ df_excel = df_excel.fillna(0)
136
+ df_excel = df_excel.set_index('i')
137
+ K_0_i = df_excel.iloc[:,0].to_xarray()
138
+
139
+ # Energy-to-power ratio storages
140
+ df_excel = pd.read_excel(url_excel, sheet_name = 'E2P')
141
+ df_excel = df_excel.rename(columns = {'Storage':'i', 'Unnamed: 1':'E2P ratio'})
142
+ #df_excel = i.to_frame().reset_index(drop=True).merge(df_excel, how = 'left')
143
+ df_excel = df_excel.fillna(0)
144
+ df_excel = df_excel.set_index('i')
145
+ e2p_iSto = df_excel.iloc[:,0].to_xarray()
146
+
147
+ # Inflow for hydro reservoir
148
+ df_excel = pd.read_excel(url_excel, sheet_name = 'HydroInflow')
149
+ df_excel = df_excel.rename(columns = {'Timesteps':'t', 'Hydro Water Reservoir':'Inflow'})
150
+ df_excel = df_excel.fillna(0)
151
+ df_excel = df_excel.set_index('t')
152
+ h_t = df_excel.iloc[:,0].to_xarray()
153
+
154
+
155
+
156
+ sets_dict = {}
157
+ params_dict = {}
158
+ # Append parameters to the dictionary
159
+ sets_dict['t'] = t
160
+ sets_dict['i'] = i
161
+ sets_dict['iSto'] = iSto
162
+ sets_dict['iConv'] = iConv
163
+ sets_dict['iPtG'] = iPtG
164
+ sets_dict['iRes'] = iRes
165
+ sets_dict['iHyRes'] = iHyRes
166
+ # Append parameters to the dictionary
167
+ params_dict['l_co2'] = l_co2
168
+ params_dict['p_co2'] = p_co2
169
+ params_dict['dt'] = dt
170
+ params_dict['D_t'] = D_t
171
+ params_dict['eff_i'] = eff_i
172
+ params_dict['c_fuel_i'] = c_fuel_i
173
+ params_dict['c_other_i'] = c_other_i
174
+ params_dict['c_inv_i'] = c_inv_i
175
+ params_dict['co2_factor_i'] = co2_factor_i
176
+ params_dict['c_var_i'] = c_var_i
177
+ params_dict['s_t_r_iRes'] = s_t_r_iRes
178
+ params_dict['K_0_i'] = K_0_i
179
+ params_dict['e2p_iSto'] = e2p_iSto
180
+ params_dict['h_t'] = h_t
181
+
182
+ if write_to_pickle_flag:
183
+ save_to_pickle(sets_dict, params_dict)
184
+
185
+ return sets_dict, params_dict
186
+
187
+
188
+ # %%
189
+ # # Example usage:
190
+ # url_excel = "Input_Jahr_2021.xlsx" # Replace with your actual file path
191
+ # limit_co2 = 0.5
192
+ # price_co2 = 50
193
+ # price_gas = 3
194
+ # price_h2 = 5
195
+
196
+ # sets, params = load_data_from_excel(url_excel,write_to_pickle_flag=True)
197
+
198
+ # # %%
199
+ # sets, params = load_data_from_excel(url_excel,load_from_pickle_flag=True)
200
+ # # %%