-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmodel_loop_modifier.py
More file actions
832 lines (586 loc) · 30.2 KB
/
model_loop_modifier.py
File metadata and controls
832 lines (586 loc) · 30.2 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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 6 11:23:06 2018
@author: martin-c-s
"""
import numpy as np
import pandas as pd
from grimsel.core.io import IO
from grimsel.auxiliary.aux_m_func import pdef
from grimsel.auxiliary.aux_m_func import cols2tuplelist
import grimsel.auxiliary.maps as maps
import pyomo.environ as po
class ModelLoopModifier():
'''
The purpose of this class is to modify the parameters of the BaseModel
objects in dependence on the current run_id.
This might include the modification of parameters like CO2 prices,
capacities, but also more profound modifications like the
redefinition of constraints.
'''
def __init__(self, ml):
'''
To initialize, the ModelBase object is made an instance attribute.
'''
self.ml = ml
def availability_cf_cap(self):
'''
Switch between availability constraints
* capacity factor
* capacity
Note: requires monthly availability!
'''
dict_avcfcap = {0: 'cap', 1: 'cf'}
slct_avcfcap = dict_avcfcap[self.ml.dct_step['swcfcap']]
# reset
self.ml.m.pp_cf.activate()
self.ml.m.PpStCapac.activate()
if hasattr(self.ml.m, 'ppst_cap_monthly'):
self.ml.m.ppst_cap_monthly.deactivate()
#define new constraint
if not hasattr(self.ml.m, 'ppst_cap_monthly'):
cf_max = IO.param_to_df(self.ml.m.cf_max, ('mt_id', 'pp_id', 'ca_id'))
cap_scale = cf_max.set_index(['mt_id', 'pp_id', 'ca_id'])['value'].to_dict()
def ppst_cap_monthly_rule(model, pp, ca, sy, mt):
''' Produced power less than capacity. '''
scale = (cap_scale[(mt, pp, ca)]
if (mt, pp, ca) in cap_scale.keys()
else 1)
return (self.ml.m.pwr[sy, pp, ca]
<= self.ml.m.cap_pwr_tot[pp, ca] * scale)
self.ml.m.ppst_cap_monthly = po.Constraint((self.ml.m.pp_ca - self.ml.m.pr_ca) | self.ml.m.st_ca
| self.ml.m.hyrs_ca,
self.ml.m.sy_mt, rule=ppst_cap_monthly_rule)
self.ml.m.ppst_cap_monthly.deactivate()
if slct_avcfcap == 'cap':
self.ml.m.PpStCapac.deactivate() # yearly capacity constraint
self.ml.m.pp_cf.deactivate() # monthly cf constraints
self.ml.m.ppst_cap_monthly.activate() # monthly capacity constraint
elif slct_avcfcap == 'cf':
self.ml.m.PpStCapac.activate() # yearly capacity constraint
self.ml.m.pp_cf.activate() # monthly cf constraints
self.ml.m.ppst_cap_monthly.deactivate() # monthly capacity constraint
self.ml.dct_vl['swcfcap_vl'] = str(slct_avcfcap)
def raise_demand(self, list_sy, slct_nd):
'''
Used for shadow price disaggregation.
'''
dict_syrs = {0: 'ref'}
dict_syrs.update({kk + 1: val for kk, val in enumerate(list_sy)})
slct_sy = dict_syrs[self.ml.dct_step['swsyrs']]
slct_nd_id = self.ml.m.mps.dict_nd_id[slct_nd]
dict_dmnd = self.ml.m.df_profdmnd_soy.set_index(['sy', 'nd_id', 'ca_id'])['value'].to_dict()
# reset
for ind, val in dict_dmnd.items():
self.ml.m.dmnd[ind].value = val
if slct_sy != 'ref':
self.ml.m.dmnd[(slct_sy, slct_nd_id, 0)].value += 1
self.ml.dct_vl['swsyrs_vl'] = str(slct_sy)
def chp_on_off(self, slct_nd):
dict_chp = {0: 'chp_on', 1: 'chp_off'}
slct_chp = self.ml.dct_step['swchp']
str_chp = dict_chp[slct_chp]
slct_nd_id = [self.ml.m.mps.dict_nd_id[nd] for nd in slct_nd]
# reset
for ind in self.ml.m.chp_prof:
self.ml.m.chp_prof[ind].activate()
if str_chp == 'chp_off':
for ind in [ind for ind in self.ml.m.chp_prof_index if ind[1] in slct_nd_id]:
self.ml.m.chp_prof[ind].deactivate()
self.ml.dct_vl['swchp_vl'] = str_chp
def cost_adjustment_literature(self):
# based on BUBL2017
dict_cadj = {0: 'orig', 1: 'adjs'}
slct_cadj = self.ml.dct_step['swcadj']
str_cadj = dict_cadj[slct_cadj]
self.ml.m.vc_fl_lin_0.display()
df_vc_fl = self.ml.m.df_plant_encar.set_index(['pp_id', 'ca_id'])[['vc_fl_lin_0', 'vc_fl_lin_1']]
# reset
for row in df_vc_fl.iterrows():
if row[0] in self.ml.m.vc_fl_lin_0:
self.ml.m.vc_fl_lin_0[row[0]].value = row[1].vc_fl_lin_0
self.ml.m.vc_fl_lin_1[row[0]].value = row[1].vc_fl_lin_1
if str_cadj == 'adjs':
self.ml.m.vc_fl_lin_0[(self.ml.m.mps.dict_pp_id['DE_GAS_LIN'], 0)].value = 32
self.ml.m.vc_fl_lin_1[(self.ml.m.mps.dict_pp_id['DE_GAS_LIN'], 0)].value = 0.0016
self.ml.dct_vl['swcadj_vl'] = str_cadj
def scale_vre_de(self):
dict_vre = {0: 1,
1: 1.25,
2: 1.5,
3: 1.75,
4: 2,
5: 2.25,
6: 2.5,
7: 2.75,
8: 3}
slct_vre = self.ml.dct_step['swvre']
str_vre = dict_vre[slct_vre]
# reset
df = self.ml.m.df_plant_encar.copy()
df = df.loc[df.pp_id.isin([self.ml.m.mps.dict_pp_id[pp] for pp in
['DE_SOL_PHO', 'DE_WIN_ONS', 'DE_WIN_OFF']])]
dict_cap = df.set_index(['pp_id', 'ca_id'])['cap_pwr_leg'].to_dict()
for ind, val in dict_cap.items():
self.ml.m.cap_pwr_leg[ind].value = val * str_vre
self.ml.dct_vl['swvre_vl'] = 'x%.2f'%str_vre
def deactivate_swiss_reservoir_constraint(self):
dict_chrs = {0: 'on', 1: 'off'}
slct_chrs = self.ml.dct_step['swchrs']
str_chrs = dict_chrs[slct_chrs]
# reset
for kk in self.ml.m.CapacStEn:
self.ml.m.CapacStEn[kk].activate()
if str_chrs == 'off':
for kk in self.ml.m.CapacStEn:
if kk[0] == self.ml.m.mps.dict_pp_id['CH_HYD_RES']:
self.ml.m.CapacStEn[kk].deactivate()
self.ml.dct_vl['swchrs_vl'] = str_chrs
def new_inflow_profile_for_ch(self):
dict_inflchat = {0: 'original', 1: 'new'}
slct_inflchat = self.ml.dct_step['swinflchat']
str_inflchat = dict_inflchat[slct_inflchat]
# reset
df_infl_ch = self.ml.m.df_profinflow.loc[self.ml.m.df_profinflow.pp_id == self.ml.m.mps.dict_pp_id['CH_HYD_RES']]
dict_infl_ch = df_infl_ch.set_index(['hy', 'pp_id', 'ca_id'])['value'].to_dict()
for kk, val in dict_infl_ch.items():
self.ml.m.inflowprof[kk].value = val
if str_inflchat == 'new':
dict_new_mt = {0: 0.02340,
1: 0.01614,
2: 0.02666,
3: 0.04655,
4: 0.15272,
5: 0.20410,
6: 0.17877,
7: 0.14128,
8: 0.10430,
9: 0.05646,
10: 0.02579,
11: 0.02383}
df_infl_ch_new = self.ml.m.df_tm_soy[['mt_id', 'sy']].assign(pp_id=self.ml.m.mps.dict_pp_id['CH_HYD_RES'], ca_id=0)
df_infl_ch_new['value'] = df_infl_ch_new.mt_id.replace(dict_new_mt)
df_infl_ch_new['value'] /= df_infl_ch_new.value.sum()
df_infl_ch_new['hy'] = df_infl_ch_new['sy']
df_infl_new = pd.concat([df_infl_ch_new,
df_infl_ch_new.assign(pp_id=self.ml.m.mps.dict_pp_id['DE_HYD_RES'])])
dict_infl_ch_new = df_infl_new.set_index(['hy', 'pp_id', 'ca_id'])['value'].to_dict()
for kk, val in dict_infl_ch_new.items():
self.ml.m.inflowprof[kk].value = val
self.ml.dct_vl['swinflchat_vl'] = str_inflchat
def flatten_austria_hydro_inflow(self):
dict_fh = {0: 'default', 1: 'flat'}
slct_fh = self.ml.dct_step['swfh']
str_fh = dict_fh[slct_fh]
df = self.ml.io.param_to_df(self.ml.m.inflowprof, ('sy', 'pp_id', 'ca_id'))
df = df.loc[df.pp_id.isin([kk for kk, vv in self.ml.m.mps.dict_pp.items() if 'HYD_RES' in vv and 'AT' in vv])]
# reset:
if str_fh == 'flat':
df['value'] = df.groupby(['pp_id'], as_index=False)['value'].transform(lambda x: x.sum() / len(x))
dict_df = df.set_index( ['sy', 'pp_id', 'ca_id'])['value'].to_dict()
for kk, vv in dict_df.items():
self.ml.m.inflowprof[kk].value = vv
self.ml.dct_vl['swfh_vl'] = str_fh
def set_ramping_cost(self, slct_pp):
dict_rc = {0: 1,
1: 1.1,
2: 1.2,
3: 1.3,
4: 1.4,
5: 1.5,
6: 1.6,
7: 1.7,
8: 1.8
}
slct_rc = self.ml.dct_step['swrc']
str_rc = dict_rc[slct_rc]
slct_pp_id = [self.ml.m.mps.dict_pp_id[pp] for pp in slct_pp]
dict_rc = (self.ml.m.df_plant_encar
.loc[self.ml.m.df_plant_encar.pp_id
.isin(slct_pp_id)]
.set_index(['pp_id', 'ca_id'])['vc_ramp']
.to_dict())
for kk, vv in dict_rc.items():
self.ml.m.vc_ramp[kk].value = vv * slct_rc
self.ml.dct_vl['swrc_vl'] = 'x%d'%str_rc
def set_calibration_variations(self, dict_cl=None):
if dict_cl is None:
dict_cl = {4: 'double_ramping_cost',
1: 'triple_ramping_cost',
2: 'inflexible_hydro',
3: 'fr_nuclear_reduction',
0: 'default'}
slct_cl = self.ml.dct_step['swcl']
str_cl = dict_cl[slct_cl]
# reset discharge duration
for kk, vv in self.ml.m.df_plant_encar.join(self.ml.m.df_def_plant.set_index('pp_id')['pp'], on='pp_id').loc[self.ml.m.df_plant_encar.discharge_duration > 0].set_index(['pp_id', 'ca_id'])['discharge_duration'].to_dict().items():
self.ml.m.discharge_duration[kk] = vv
self.ml.m.hy_erg_min.activate()
self.ml.m.hy_month_min.activate()
self.ml.m.hy_reservoir_boundary_conditions.activate()
self.ml.m.monthly_totals.activate()
if str_cl == 'inflexible_hydro':
self.ml.m.hy_erg_min.deactivate()
self.ml.m.hy_month_min.deactivate()
self.ml.m.hy_reservoir_boundary_conditions.deactivate()
self.ml.m.monthly_totals.deactivate()
for kk in self.ml.m.discharge_duration:
self.ml.m.discharge_duration[kk] = 0
self.ml.m.cap_pwr_leg[kk] = 100000
# reset ramping
for kk, vv in self.ml.m.df_plant_encar.set_index(['pp_id', 'ca_id'])['vc_ramp'].to_dict().items():
if kk in [k for k in self.ml.m.vc_ramp]:
self.ml.m.vc_ramp[kk].value = vv
if str_cl == 'double_ramping_cost':
for kk in self.ml.m.vc_ramp:
self.ml.m.vc_ramp[kk].value = self.ml.m.vc_ramp[kk].value * 2
if str_cl == 'triple_ramping_cost':
for kk in self.ml.m.vc_ramp:
self.ml.m.vc_ramp[kk].value = self.ml.m.vc_ramp[kk].value * 3
if str_cl == 'fr_nuclear_reduction':
# Power capacity reduced by approximate NTC of not included neighboring countries
mps = maps.Maps(self.ml.io.sc_inp, self.ml.io.db)
dict_cap_pwr_leg = {kk: self.ml.m.cap_pwr_leg[kk].value - 7000 for kk in self.ml.m.cap_pwr_leg.keys()
if kk[0] in mps.dict_pp.keys() and mps.dict_pp[kk[0]] == 'FR_NUC_ELC'}
for kk, vv in dict_cap_pwr_leg.items():
self.ml.m.cap_pwr_leg[kk] = vv
self.ml.dct_vl['swcl_vl'] = str_cl
def set_france_nuclear_vc(self, dict_fr=None):
if dict_fr is None:
dict_fr = {0: '1x',
1: '2x',
2: '3x',
3: '4x',
4: '5x',
5: '6x',
6: '8x',
7: '10x'}
slct_fr = self.ml.dct_step['swfr']
str_fr = dict_fr[slct_fr]
_df = (self.ml.m.parameter_month_dict['vc_fl']
.set_index(['mt_id', 'fl_id', 'nd_id'])['value'])
for kk, vv in _df.to_dict().items():
if kk in [k for k in self.ml.m.vc_fl]:
self.ml.m.vc_fl[kk].value = vv
list_nuclear = self.ml.m.df_def_fuel.loc[self.ml.m.df_def_fuel.fl == 'nuclear_fuel', 'fl_id'].values
list_france = self.ml.m.df_def_node.loc[self.ml.m.df_def_node.nd == 'FR0', 'nd_id'].values
_dfnuc = _df.loc[_df.index.get_level_values('fl_id').isin(list_nuclear)
&_df.index.get_level_values('nd_id').isin(list_france)]
multiplicator = int(str_fr.replace('x', ''))
_dfnuc *= multiplicator
for kk, vv in _dfnuc.to_dict().items():
if kk in [k for k in self.ml.m.vc_fl]:
self.ml.m.vc_fl[kk].value = vv
self.ml.dct_vl['swfr_vl'] = str_fr
def set_chp_on_off(self, dict_chp=None):
if dict_chp is None:
dict_chp = {0: 'on',
1: 'off'}
slct_chp = self.ml.dct_step['swchp']
str_chp = dict_chp[slct_chp]
self.ml.m.chp_prof.activate()
if str_chp == 'off':
self.ml.m.chp_prof.deactivate()
self.ml.dct_vl['swchp_vl'] = str_chp
def set_slope_lin(self, dict_sl=None):
if dict_sl is None:
dict_sl = {0: 'base_value_slope',
1: '2x_value_slope',
2: '4x_value_slope'}
slct_sl = self.ml.dct_step['swsl']
str_sl = dict_sl[slct_sl]
for kk, vv in self.ml.m.df_plant_encar.set_index(['pp_id','ca_id'])['slope_lin_vc_fl'].to_dict().items():
if kk in [k for k in self.ml.m.slope_lin_vc_fl]:
self.ml.m.slope_lin_vc_fl[kk].value = vv
if str_sl == '2x_value_slope':
for kk in self.ml.m.slope_lin_vc_fl:
self.ml.m.slope_lin_vc_fl[kk].value = self.ml.m.slope_lin_vc_fl[kk].value * 2
if str_sl == '4x_value_slope':
for kk in self.ml.m.slope_lin_vc_fl:
self.ml.m.slope_lin_vc_fl[kk].value = self.ml.m.slope_lin_vc_fl[kk].value * 4
self.ml.dct_vl['swsl_vl'] = str_sl
def set_linear_vc(self, dict_ln=None):
if dict_ln is None:
dict_ln = {0: 'quadratic',
1: 'linear'}
slct_ln = self.ml.dct_step['swln']
str_ln = dict_ln[slct_ln]
# fuels/nodes with linear power plants
mask_lin = self.ml.m.df_def_plant.pp.str.contains('LIN')
lin_fuel_node = (self.ml.m.df_def_plant.loc[mask_lin, ['fl_id', 'nd_id']]
.drop_duplicates())
#######################################################################
def switch_linear_quadratic(excl_pp=[]):
slct_col = 'cap_pwr_leg' #+ (str_ln if not reset else '')
dct_capacity = (self.ml.m.df_plant_encar.loc[self.ml.m.df_plant_encar.pp_id.isin(excl_pp)]
.set_index(['pp_id', 'ca_id'])[slct_col]
.to_dict())
for kk, vv in dct_capacity.items():
self.ml.m.cap_pwr_leg[kk] = 0
if str_ln == 'linear':
# inner join to filter (fl, nd) combinations defined above
pp_quad = pd.merge(self.ml.m.df_def_plant,
lin_fuel_node, how='inner')
pp_quad = (pp_quad.loc[pp_quad.pp.str.contains('LIN')]
.set_index('pp')['pp_id'].tolist())
# excl_pp_lin=list(range(31,36))+list(range(46,50))+[55]
switch_linear_quadratic(excl_pp=pp_quad)
self.ml.m.objective_quad.deactivate()
# self.ml.m.objective_lin.activate()
if str_ln == 'quadratic':
# switch_linear_quadratic(reset=True)
pp_lin = pd.merge(self.ml.m.df_def_plant,
lin_fuel_node, how='inner')
pp_lin = (pp_lin.loc[-pp_lin.pp.str.contains('LIN')]
.set_index('pp')['pp_id'].tolist())
# excl_pp_quad=list(range(10,31))+list(range(36,46))+list(range(50,55))
switch_linear_quadratic(excl_pp=pp_lin)
# self.ml.m.objective_lin.deactivate()
self.ml.m.objective_quad.activate()
#
self.ml.dct_vl['swln_vl'] = str_ln
# self.objective.deactivate()
def set_historic_year(self, dict_hy=None):
# e
if dict_hy is None:
dict_hy = {
0: 2015,
1: 2016,
2: 2014,
3: 2013,
4: 2012,
5: 2011,
6: 2010,
7: 2009,
8: 2008,
9: 2007,
10: 2006,
11: 2017
}
slct_hy = self.ml.dct_step['swhy']
str_hy = '_yr' + str(dict_hy[slct_hy]) if slct_hy > 0 else ''
#######################################################################
def set_fuel_prices(str_hy=None, reset=False):
''' Select fuel price values for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'vc_fl' + (str_hy if not reset else '')
msg = ('Resetting vc_fl to base year values'
if reset else
'Setting vc_fl to values' + str_hy.replace('_', ' '))
msg += ' from column {}'.format(slct_col)
if 'vc_fl' in self.ml.m.dict_monthly_factors.keys():
df_mt = self.ml.m.dict_monthly_factors['vc_fl']
df = cols2tuplelist(self.ml.m.df_fuel_node_encar[['fl_id', 'nd_id']],
df_mt['mt_id'], return_df=True)
df = df.join(self.ml.m.df_fuel_node_encar.set_index(['fl_id', 'nd_id'])[slct_col], on=['fl_id', 'nd_id']).fillna(0)
df = df.join(df_mt.set_index(['mt_id','fl_id', 'nd_id'])['mt_fact' + (str_hy if not reset else '')], on=['mt_id', 'fl_id', 'nd_id']).fillna(1)
df['value'] = df[slct_col] * df['mt_fact' + (str_hy if not reset else '')]
dct_prices = df.set_index(['mt_id', 'fl_id', 'nd_id'])['value'].to_dict()
else:
dct_prices = (self.ml.m.df_fuel_node_encar
.set_index(['fl_id', 'nd_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_prices.items():
self.ml.m.vc_fl[kk] = vv
#######################################################################
def set_cap_pwr_leg(str_hy=None, reset=False, excl_pp=[]):
''' Select power plant capacities for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'cap_pwr_leg' + (str_hy if not reset else '')
msg = ('Resetting cap_pwr_leg to base year values.'
if reset else
'Setting cap_pwr_leg to values' + str_hy.replace('_', ' '))
dct_cap = (self.ml.m.df_plant_encar.loc[-self.ml.m.df_plant_encar.pp_id.isin(excl_pp)]
.set_index(['pp_id', 'ca_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_cap.items():
self.ml.m.cap_pwr_leg[kk] = vv
#######################################################################
def set_cf_max(str_hy=None, reset=False, excl_pp=[]):
''' Select maximum capacity factors for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'cf_max' + (str_hy if not reset else '')
msg = ('Resetting cf_max to base year values.'
if reset else
'Setting cf_max to values' + str_hy.replace('_', ' '))
if 'cf_max' in self.ml.m.dict_monthly_factors.keys():
df_mt = self.ml.m.dict_monthly_factors['cf_max']
df = cols2tuplelist(self.ml.m.df_plant_encar[['pp_id', 'ca_id']].loc[self.ml.m.df_plant_encar.pp_id.isin(self.ml.m.setlst['pp'])],
df_mt['mt_id'], return_df=True)
df = df.join(self.ml.m.df_plant_encar.set_index(['pp_id', 'ca_id'])[slct_col], on=['pp_id', 'ca_id']).fillna(0)
df = df.join(df_mt.set_index(['mt_id','ca_id', 'pp_id'])['mt_fact' + (str_hy if not reset else '')], on=['mt_id', 'pp_id', 'ca_id']).fillna(1)
df['value'] = df[slct_col] * df['mt_fact' + (str_hy if not reset else '')]
dct_cf_max = df.set_index(['mt_id', 'pp_id', 'ca_id'])['value'].to_dict()
else:
dct_cf_max = (self.ml.m.df_plant_encar.loc[-self.ml.m.df_plant_encar.pp_id.isin(excl_pp)]
.loc[self.ml.m.df_plant_encar.pp_id
.isin(self.ml.m.setlst['pp'])]
.set_index(['pp_id', 'ca_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_cf_max.items():
self.ml.m.cf_max[kk] = vv
#######################################################################
def set_priceprof(str_hy=None, reset=False):
''' Select price profile for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'value' + (str_hy if not reset else '')
msg = ('Resetting priceprof to base year values.'
if reset else
'Setting priceprof to values' + str_hy.replace('_', ' '))
dct_priceprof = (self.ml.m.df_profprice_soy
.set_index(['sy', 'nd_id', 'fl_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_priceprof.items():
self.ml.m.priceprof[kk] = vv
#######################################################################
def set_dmnd(str_hy=None, reset=False):
''' Select demand profiles for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'value' + (str_hy if not reset else '')
msg = ('Resetting dmnd to base year values.'
if reset else
'Setting dmnd to values' + str_hy.replace('_', ' '))
dct_dmnd = (self.ml.m.df_profdmnd_soy
.set_index(['sy', 'nd_id', 'ca_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_dmnd.items():
self.ml.m.dmnd[kk] = vv
#######################################################################
def set_co2_price(str_hy=None, reset=False):
''' Select CO2 price for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'price_co2' + (str_hy if not reset else '')
msg = ('Resetting price_co2 to base year values.'
if reset else
'Setting price_co2 to values' + str_hy.replace('_', ' '))
if 'price_co2' in self.ml.m.dict_monthly_factors.keys():
df_mt = self.ml.m.dict_monthly_factors['price_co2']
df = cols2tuplelist(self.ml.m.df_def_node[['nd_id']].loc[self.ml.m.df_def_node.nd_id.isin(self.ml.m.slct_node_id)],
df_mt['mt_id'], return_df=True)
df = df.join(self.ml.m.df_def_node.set_index(['nd_id'])[slct_col], on=['nd_id']).fillna(0)
df = df.join(df_mt.set_index(['mt_id','nd_id'])['mt_fact' + (str_hy if not reset else '')], on=['mt_id', 'nd_id']).fillna(1)
df['value'] = df[slct_col] * df['mt_fact' + (str_hy if not reset else '')]
dct_price_co2 = df.set_index(['mt_id', 'nd_id'])['value'].to_dict()
else:
dct_price_co2 = (self.ml.m.df_def_node
.set_index(['nd_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_price_co2.items():
self.ml.m.price_co2[kk] = vv
#######################################################################
def set_erg_inp(str_hy=None, reset=False, excl_fl=[]):
''' Select exogenous energy production for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'erg_inp' + (str_hy if not reset else '')
msg = ('Resetting erg_inp to base year values.'
if reset else
'Setting erg_inp to values' + str_hy.replace('_', ' '))
dct_erg_inp = (self.ml.m.df_fuel_node_encar.loc[-self.ml.m.df_fuel_node_encar.fl_id.isin(excl_fl)]
.set_index(['nd_id', 'ca_id',
'fl_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_erg_inp.items():
self.ml.m.erg_inp[kk] = vv
#######################################################################
def set_erg_chp(str_hy=None, reset=False, excl_fl=[]):
''' Select exogenous chp energy production for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'erg_chp' + (str_hy if not reset else '')
msg = ('Resetting erg_chp to base year values.'
if reset else
'Setting erg_chp to values' + str_hy.replace('_', ' '))
dct_erg_chp = (self.ml.m.df_fuel_node_encar
.loc[-self.ml.m.df_fuel_node_encar.fl_id.isin(excl_fl)]
.set_index(['nd_id', 'ca_id',
'fl_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_erg_chp.items():
self.ml.m.erg_chp[kk] = vv
#######################################################################
def set_cap_trm_leg(str_hy=None, reset=False):
''' Select exogenous energy production for selected year. '''
if str_hy == None:
reset=True
str_hy = ''
slct_col = 'cap_trm_leg' + (str_hy if not reset else '')
msg = ('Resetting cap_trm_leg to base year values.'
if reset else
'Setting cap_trm_leg to values' + str_hy.replace('_', ' '))
dct_cap_trm_leg = (self.ml.m.df_node_connect
.set_index(['mt_id', 'nd_id',
'nd_2_id', 'ca_id'])[slct_col]
.to_dict())
print(msg)
for kk, vv in dct_cap_trm_leg.items():
self.ml.m.cap_trm_leg[kk] = vv
#######################################################################
def set_supprof(str_hy=None, reset=False):
'''
Select supply profiles for selected year.
(Normalized) input profiles are scaled by erg_inp
and then divided by cap_pwr_leg to get the hourly capacity
factor.
'''
if str_hy == None:
reset=True
str_hy = ''
msg = ('Resetting supprof to base year values.'
if reset else
'Setting supprof to values' + str_hy.replace('_', ' '))
df = self.ml.m.df_profsupply_soy
df = df[['sy', 'pp_id', 'ca_id'] + ['value' + str_hy]]
df = df.set_index(['sy', 'pp_id', 'ca_id'])
dct_supprof = df['value' + str_hy].to_dict()
print(msg)
for kk, vv in dct_supprof.items():
self.ml.m.supprof[kk] = vv
#######################################################################
# resetting everything to base year values
# Note: inflow profiles are static and scaled by erg_inp in the constraint
# set_fuel_prices(reset=True)
# set_cap_pwr_leg(reset=True)
# set_cf_max(reset=True)
# set_dmnd(reset=True)
# set_co2_price(reset=True)
# set_erg_inp(reset=True)
# set_erg_chp(reset=True)
# set_cap_trm_leg(reset=True)
# set_supprof(reset=True)
# set_priceprof(reset=True)
# excl_pp = []
# excl_fl = []
# set_fuel_prices(str_hy)
# set_cap_pwr_leg(str_hy, excl_pp=excl_pp)
# set_cf_max(str_hy, excl_pp=excl_pp)
# set_dmnd(str_hy)
# set_co2_price(str_hy)
# set_erg_inp(str_hy, excl_fl=excl_fl)
# set_erg_chp(str_hy, excl_fl=excl_fl)
# set_cap_trm_leg(str_hy)
# set_supprof(str_hy)
# set_priceprof(str_hy)
self.ml.dct_vl['swhy_vl'] = 'yr' + str(dict_hy[slct_hy])