-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariables.py
258 lines (215 loc) · 12.4 KB
/
variables.py
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
from linopy import Model
import xarray as xr
from numpy import inf
def add_demand_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add demand variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
RTiFY = [ds.coords['REGION'], ds.coords['TIMESLICE'], ds.coords['FUEL'], ds.coords['YEAR']]
# mask = ds['SpecifiedAnnualDemand'].expand_dims('TIMESLICE').notnull()
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='RateOfDemand', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='Demand', integer=False)
return m
def add_storage_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add storage variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
# Create the required index
RSSDDY = [ds.coords['REGION'],
ds.coords['STORAGE'],
ds.coords['SEASON'],
ds.coords['DAYTYPE'],
ds.coords['DAILYTIMEBRACKET'], ds.coords['YEAR']]
RSY = [ds.coords['REGION'],
ds.coords['STORAGE'],
ds.coords['YEAR']]
m.add_variables(lower=-inf, upper=inf, coords=RSSDDY, name='RateOfStorageCharge', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RSSDDY, name='RateOfStorageDischarge', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RSSDDY, name='NetChargeWithinYear', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RSSDDY, name='NetChargeWithinDay', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='StorageLevelYearStart', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='StorageLevelYearFinish', integer=False)
coords = [ds.coords['REGION'], ds.coords['STORAGE'], ds.coords['SEASON'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='StorageLevelSeasonStart', integer=False)
coords = [ds.coords['REGION'], ds.coords['STORAGE'], ds.coords['SEASON'], ds.coords['DAYTYPE'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='StorageLevelDayTypeStart', integer=False)
coords = [ds.coords['REGION'], ds.coords['STORAGE'], ds.coords['SEASON'], ds.coords['DAYTYPE'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='StorageLevelDayTypeFinish', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='StorageLowerLimit', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='StorageUpperLimit', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='AccumulatedNewStorageCapacity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='NewStorageCapacity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='CapitalInvestmentStorage', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='DiscountedCapitalInvestmentStorage', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='SalvageValueStorage', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='DiscountedSalvageValueStorage', integer=False)
m.add_variables(lower=0, upper=inf, coords=RSY, name='TotalDiscountedStorageCost', integer=False)
return m
def add_capacity_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add capacity variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
# Create the required index
RTeY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['YEAR']]
# masks
mask = ds['CapacityOfOneTechnologyUnit'].notnull()
m.add_variables(lower=0, upper=inf, coords=RTeY, name='NumberOfNewTechnologyUnits', integer=True, mask=mask)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='NewCapacity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='AccumulatedNewCapacity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='TotalCapacityAnnual', integer=False)
return m
def add_activity_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add activity variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
# Add indices
RRTiFY = [ds.coords['REGION'], ds.coords['_REGION'], ds.coords['TIMESLICE'], ds.coords['FUEL'], ds.coords['YEAR']]
RRFY = [ds.coords['REGION'], ds.coords['_REGION'], ds.coords['FUEL'], ds.coords['YEAR']]
RFY = [ds.coords['REGION'], ds.coords['FUEL'], ds.coords['YEAR']]
RTiFY = [ds.coords['REGION'], ds.coords['TIMESLICE'], ds.coords['FUEL'], ds.coords['YEAR']]
RTiTeFY = [ds.coords['REGION'], ds.coords['TIMESLICE'], ds.coords['TECHNOLOGY'], ds.coords['FUEL'], ds.coords['YEAR']]
RTiTeMY = [ds.coords['REGION'], ds.coords['TIMESLICE'],
ds.coords['TECHNOLOGY'], ds.coords['MODE_OF_OPERATION'],
ds.coords['YEAR']]
RTiTeMFY = [ds.coords['REGION'], ds.coords['TIMESLICE'],
ds.coords['TECHNOLOGY'], ds.coords['MODE_OF_OPERATION'],
ds.coords['FUEL'], ds.coords['YEAR']]
RTeY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['YEAR']]
# Add masks
iac_mask = ds['InputActivityRatio'].notnull()
iac_mask_m = ds['InputActivityRatio'].sum('MODE_OF_OPERATION') != 0
oac_mask = ds['OutputActivityRatio'].notnull()
oac_mask_m = ds['OutputActivityRatio'].sum('MODE_OF_OPERATION') != 0
m.add_variables(lower=0, upper=inf, coords=RTiTeMY, name='RateOfActivity', integer=False)
coords = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['TIMESLICE'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='RateOfTotalActivity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='TotalTechnologyAnnualActivity', integer=False)
coords = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['MODE_OF_OPERATION'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='TotalAnnualTechnologyActivityByMode', integer=False)
coords = [ds.coords['REGION'], ds.coords['TECHNOLOGY']]
m.add_variables(lower=-inf, upper=inf, coords=coords, name='TotalTechnologyModelPeriodActivity', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTiTeMFY, name='RateOfProductionByTechnologyByMode', integer=False, mask=oac_mask)
m.add_variables(lower=0, upper=inf, coords=RTiTeFY, name='RateOfProductionByTechnology', integer=False, mask=oac_mask_m)
m.add_variables(lower=0, upper=inf, coords=RTiTeFY, name='ProductionByTechnology', integer=False, mask=oac_mask_m)
coords = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['FUEL'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='ProductionByTechnologyAnnual', integer=False, mask=oac_mask_m)
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='RateOfProduction', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='Production', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTiTeMFY, name='RateOfUseByTechnologyByMode', integer=False, mask=iac_mask)
m.add_variables(lower=0, upper=inf, coords=RTiTeFY, name='RateOfUseByTechnology', integer=False, mask=iac_mask_m)
coords = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['FUEL'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='UseByTechnologyAnnual', integer=False, mask=iac_mask_m)
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='RateOfUse', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTiTeFY, name='UseByTechnology', integer=False, mask=iac_mask_m)
m.add_variables(lower=0, upper=inf, coords=RTiFY, name='Use', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RRTiFY, name='Trade', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RRFY, name='TradeAnnual', integer=False)
m.add_variables(lower=0, upper=inf, coords=RFY, name='ProductionAnnual', integer=False)
m.add_variables(lower=0, upper=inf, coords=RFY, name='UseAnnual', integer=False)
return m
def add_emission_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add emisison variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
# Create the required indexes
RTeY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['YEAR']]
RE = [ds.coords['REGION'], ds.coords['EMISSION']]
REY = [ds.coords['REGION'], ds.coords['EMISSION'], ds.coords['YEAR']]
RTeEY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['EMISSION'], ds.coords['YEAR']]
RTeEMY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['EMISSION'], ds.coords['MODE_OF_OPERATION'], ds.coords['YEAR']]
# Create the masks
ear_mask = ds['EmissionActivityRatio'].notnull()
ear_mask_m = ds['EmissionActivityRatio'].sum('MODE_OF_OPERATION') != 0
ep_mask = ds['EmissionsPenalty'].notnull()
# Add the variables
m.add_variables(lower=0, upper=inf, coords=RTeEMY, name='AnnualTechnologyEmissionByMode', integer=False,
mask=ear_mask)
m.add_variables(lower=0, upper=inf, coords=RTeEY, name='AnnualTechnologyEmission', integer=False,
mask=ear_mask_m)
m.add_variables(lower=0, upper=inf, coords=RTeEY, name='AnnualTechnologyEmissionPenaltyByEmission', integer=False,
mask=ep_mask)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='AnnualTechnologyEmissionsPenalty', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='DiscountedTechnologyEmissionsPenalty', integer=False)
m.add_variables(lower=0, upper=inf, coords=REY, name='AnnualEmissions', integer=False)
m.add_variables(lower=0, upper=inf, coords=RE, name='ModelPeriodEmissions', integer=False)
return m
def add_variables(ds: xr.Dataset, m: Model) -> Model:
"""Add all variables to the model
Arguments
---------
ds: xarray.Dataset
The parameters dataset
m: linopy.Model
A linopy model
Returns
-------
linopy.Model
"""
RTeY = [ds.coords['REGION'], ds.coords['TECHNOLOGY'], ds.coords['YEAR']]
RY = [ds.coords['REGION'], ds.coords['YEAR']]
m = add_demand_variables(ds, m)
# m = add_storage_variables(ds, m)
m = add_capacity_variables(ds, m)
m = add_activity_variables(ds, m)
# Costing Variables
m.add_variables(lower=0, upper=inf, coords=RTeY, name='CapitalInvestment', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='DiscountedCapitalInvestment', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='SalvageValue', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='DiscountedSalvageValue', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='OperatingCost', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='DiscountedOperatingCost', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='AnnualVariableOperatingCost', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='AnnualFixedOperatingCost', integer=False)
m.add_variables(lower=0, upper=inf, coords=RTeY, name='TotalDiscountedCostByTechnology', integer=False)
m.add_variables(lower=0, upper=inf, coords=RY, name='TotalDiscountedCost', integer=False)
coords = [ds.coords['REGION']]
m.add_variables(lower=0, upper=inf, coords=coords, name='ModelPeriodCostByRegion', integer=False)
# Reserve Margin
m.add_variables(lower=0, upper=inf, coords=RY, name='TotalCapacityInReserveMargin', integer=False)
coords = [ds.coords['REGION'], ds.coords['TIMESLICE'], ds.coords['YEAR']]
m.add_variables(lower=0, upper=inf, coords=coords, name='DemandNeedingReserveMargin', integer=False)
# RE Gen Target
m.add_variables(lower=-inf, upper=inf, coords=RY, name='TotalREProductionAnnual', integer=False)
m.add_variables(lower=-inf, upper=inf, coords=RY, name='RETotalProductionOfTargetFuelAnnual', integer=False)
m = add_emission_variables(ds, m)
return m