-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_constraints.py
106 lines (77 loc) · 2.97 KB
/
test_constraints.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
import pytest
from variables import add_demand_variables, add_activity_variables
import xarray as xr
import numpy as np
from linopy import Model, Variable
from constraints import add_demand_constraints
@pytest.fixture()
def specified_annual_demand():
data = np.empty((1, 2, 1))
data.fill(np.nan)
coords = {
'REGION': ['SIMPLICITY'],
'FUEL': ['ELC', 'HEAT'],
'YEAR': [2010]
}
dims = ['REGION', 'FUEL', 'YEAR']
return xr.DataArray(data, coords, dims, 'SpecifiedAnnualDemand')
@pytest.fixture()
def specified_demand_profile():
data = np.empty((1, 2, 1, 1))
data.fill(1)
coords = {
'REGION': ['SIMPLICITY'],
'FUEL': ['ELC', 'HEAT'],
'TIMESLICE': ['1'],
'YEAR': [2010]
}
dims = ['REGION', 'FUEL', 'TIMESLICE', 'YEAR']
return xr.DataArray(data, coords, dims, 'SpecifiedDemandProfile')
@pytest.fixture()
def year_split():
data = np.empty((1, 1))
data[0, 0] = 1
coords = {
'TIMESLICE': ['1'],
'YEAR': [2010]
}
dims = ['TIMESLICE', 'YEAR']
return xr.DataArray(data, coords, dims, 'SpecifiedDemandProfile')
@pytest.fixture()
def coords():
return {
'_REGION': ['SIMPLICITY'],
'REGION': ['SIMPLICITY'],
'TECHNOLOGY': ['HEATER'],
'TIMESLICE': ['1'],
'MODE_OF_OPERATION': [1],
'FUEL': ['ELC', 'HEAT'],
'YEAR': [2010]
}
@pytest.fixture()
def dataset(coords, specified_annual_demand, specified_demand_profile, year_split):
data_vars = {
'SpecifiedAnnualDemand': specified_annual_demand,
'SpecifiedDemandProfile': specified_demand_profile,
'YearSplit': year_split
}
# Dataset containing all the parameters read in from an OSeMOSYS file
ds = xr.Dataset(data_vars=data_vars, coords=coords)
return ds
def test_add_demand_constraints(dataset):
model = Model(force_dim_names=True)
RTiFY = [dataset.coords['REGION'], dataset.coords['TIMESLICE'], dataset.coords['FUEL'], dataset.coords['YEAR']]
model.add_variables(coords=RTiFY, name='RateOfDemand')
actual = add_demand_constraints(dataset, model).constraints
assert actual.labels.EQ_SpecifiedDemand.shape == (1, 1, 2, 1)
assert (actual.labels.EQ_SpecifiedDemand[0, 0, :, 0] == -1).all()
def test_add_demand_constraints_no_mask(dataset):
model = Model(force_dim_names=True)
RTiFY = [dataset.coords['REGION'], dataset.coords['TIMESLICE'], dataset.coords['FUEL'], dataset.coords['YEAR']]
model.add_variables(coords=RTiFY, name='RateOfDemand')
# Fill non nan values into demand so the constraint is built for all demands
dataset['SpecifiedAnnualDemand'] = dataset['SpecifiedAnnualDemand'].fillna(1)
actual = add_demand_constraints(dataset, model).constraints
assert actual.labels.EQ_SpecifiedDemand.shape == (1, 1, 2, 1)
assert (actual.labels.EQ_SpecifiedDemand[0, 0, :, 0] != -1).all()
assert(actual['EQ_SpecifiedDemand'].coeffs.values == np.array([[[[1], [1]]]])).all()