Skip to content

Commit

Permalink
Merge a0abe1c into a2e4bea
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed May 4, 2023
2 parents a2e4bea + a0abe1c commit 0b32a32
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 57 deletions.
38 changes: 4 additions & 34 deletions src/oemof/solph/_energy_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,6 @@
from oemof.tools import debugging


def _add_periods(periods):
"""Returns periods to be added to the energy system
* For a standard model, periods only contain one value {0: 0}
* For a multi-period model, periods are indexed with integer values
starting from zero. The keys are the time indices for the resective
period given by a pd.date_range object. As a default,
each year in the timeindex is mapped to its own period.
Parameters
----------
periods : dict
Periods of a (multi-period) model
Keys are periods as increasing integer values, starting from 0,
values are the periods defined by a pd.date_range object;
For a standard model, only one period is used.
Returns
-------
periods : dict
Periods of the energy system (to ensure it being set)
"""
if periods is not None:
for k in periods.keys():
if not isinstance(k, int):
raise ValueError("Period keys must be of type int.")

return periods


class EnergySystem(es.EnergySystem):
"""A variant of the class EnergySystem from
<oemof.network.network.energy_system.EnergySystem> specially tailored to
Expand Down Expand Up @@ -199,7 +169,7 @@ def __init__(
"please report them."
)
warnings.warn(msg, debugging.SuspiciousUsageWarning)
self.periods = _add_periods(periods)
self.periods = periods
self._extract_periods_years()

def _extract_periods_years(self):
Expand All @@ -211,12 +181,12 @@ def _extract_periods_years(self):
the simulation year of the start of each a period,
relative to the start of the optimization rund and starting with 0
"""
periods_years = {0: 0}
periods_years = [0]
if self.periods is not None:
start_year = self.periods[0].min().year
for k, v in self.periods.items():
for k, v in enumerate(self.periods):
if k >= 1:
periods_years[k] = v.min().year - start_year
periods_years.append(v.min().year - start_year)

self.periods_years = periods_years

Expand Down
4 changes: 2 additions & 2 deletions src/oemof/solph/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def _add_parent_block_sets(self):
self.PERIODS = po.Set(initialize=[0])
else:
nested_list = [
[k] * len(self.es.periods[k]) for k in self.es.periods.keys()
[k] * len(self.es.periods[k]) for k in range(len(self.es.periods))
]
flattened_list = [
item for sublist in nested_list for item in sublist
Expand All @@ -401,7 +401,7 @@ def _add_parent_block_sets(self):
ordered=True,
)
self.PERIODS = po.Set(
initialize=sorted(list(set(self.es.periods.keys())))
initialize=sorted(list(set(range(len(self.es.periods)))))
)

# (Re-)Map timesteps to periods
Expand Down
2 changes: 1 addition & 1 deletion src/oemof/solph/components/_generic_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ def _old_storage_capacity_rule_end(block):
elif lifetime <= m.es.periods_years[p]:
# Obtain commissioning period
comm_p = 0
for k, v in m.es.periods_years.items():
for k, v in enumerate(m.es.periods_years):
if m.es.periods_years[p] - lifetime - v < 0:
# change of sign is detected
comm_p = k - 1
Expand Down
6 changes: 3 additions & 3 deletions src/oemof/solph/components/experimental/_sink_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ def _old_dsm_capacity_rule_end(block):
elif lifetime <= m.es.periods_years[p]:
# Obtain commissioning period
comm_p = 0
for k, v in m.es.periods_years.items():
for k, v in enumerate(m.es.periods_years):
if m.es.periods_years[p] - lifetime - v < 0:
# change of sign is detected
comm_p = k - 1
Expand Down Expand Up @@ -2295,7 +2295,7 @@ def _old_dsm_capacity_rule_end(block):
elif lifetime <= m.es.periods_years[p]:
# Obtain commissioning period
comm_p = 0
for k, v in m.es.periods_years.items():
for k, v in enumerate(m.es.periods_years):
if m.es.periods_years[p] - lifetime - v < 0:
# change of sign is detected
comm_p = k - 1
Expand Down Expand Up @@ -4390,7 +4390,7 @@ def _old_dsm_capacity_rule_end(block):
elif lifetime <= m.es.periods_years[p]:
# Obtain commissioning period
comm_p = 0
for k, v in m.es.periods_years.items():
for k, v in enumerate(m.es.periods_years):
if m.es.periods_years[p] - lifetime - v < 0:
# change of sign is detected
comm_p = k - 1
Expand Down
2 changes: 1 addition & 1 deletion src/oemof/solph/flows/_investment_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def _old_capacity_rule_end(block):
elif lifetime <= m.es.periods_years[p]:
# Obtain commissioning period
comm_p = 0
for k, v in m.es.periods_years.items():
for k, v in enumerate(m.es.periods_years):
if m.es.periods_years[p] - lifetime - v < 0:
# change of sign is detected
comm_p = k - 1
Expand Down
2 changes: 1 addition & 1 deletion src/oemof/solph/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def _extract_multi_period_model_result(
# map periods to their start years for displaying period results
d = {
key: val + model.es.periods[0].min().year
for key, val in model.es.periods_years.items()
for key, val in enumerate(model.es.periods_years)
}
period_scalars = df_dict[k].loc[:, period_cols].dropna()
sequences = df_dict[k].loc[
Expand Down
2 changes: 1 addition & 1 deletion tests/multi_period_constraint_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setup_class(cls):
timeindex2 = pd.date_range("1/1/2013", periods=2, freq="H")
timeindex3 = pd.date_range("1/1/2014", periods=2, freq="H")
cls.date_time_index = timeindex1.append(timeindex2).append(timeindex3)
cls.periods = {0: timeindex1, 1: timeindex2, 2: timeindex3}
cls.periods = [timeindex1, timeindex2, timeindex3]

cls.tmppath = solph.helpers.extend_basic_path("tmp")
logging.info(cls.tmppath)
Expand Down
18 changes: 6 additions & 12 deletions tests/test_energy_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
def test_add_periods():
"""test method _add_periods of energy system"""
timeindex = pd.date_range(start="2012-01-01", periods=10000, freq="H")
periods = {
0: pd.date_range(start="2012-01-01", periods=8784, freq="H"),
1: pd.date_range(start="2013-01-01", periods=1217, freq="H"),
}
periods = [
pd.date_range(start="2012-01-01", periods=8784, freq="H"),
pd.date_range(start="2013-01-01", periods=1217, freq="H"),
]
es = EnergySystem(timeindex=timeindex, periods=periods)
assert len(es.periods) == 2
assert es.periods[0].equals(
Expand All @@ -36,18 +36,12 @@ def test_extract_periods_years():
t_idx_2 = pd.date_range("1/1/2041", periods=3, freq="H").to_series()
t_idx_3 = pd.date_range("1/1/2050", periods=3, freq="H").to_series()
timeindex = pd.concat([t_idx_1, t_idx_2, t_idx_3]).index
periods = {0: t_idx_1, 1: t_idx_2, 2: t_idx_3}
periods = [t_idx_1, t_idx_2, t_idx_3]
es = EnergySystem(
timeindex=timeindex,
timeincrement=[1] * len(timeindex),
infer_last_interval=False,
periods=periods,
)
periods_years = {0: 0, 1: 21, 2: 30}
periods_years = [0, 21, 30]
assert es.periods_years == periods_years


def test_type_error():
"""test type error getting thrown for energy system's periods"""
with pytest.raises(ValueError, match="must be of type int"):
EnergySystem(periods={0.1: "A", 0.2: "B"})
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_multi_period_dispatch_model(solver="cbc"):
timeindex = pd.concat(
[t_idx_1_series, t_idx_2_series, t_idx_3_series]
).index
periods = {0: t_idx_1, 1: t_idx_2, 2: t_idx_3}
periods = [t_idx_1, t_idx_2, t_idx_3]

es = EnergySystem(
timeindex=timeindex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_multi_period_investment_model(solver="cbc"):
timeindex = pd.concat(
[t_idx_1_series, t_idx_2_series, t_idx_3_series]
).index
periods = {0: t_idx_1, 1: t_idx_2, 2: t_idx_3}
periods = [t_idx_1, t_idx_2, t_idx_3]

es = EnergySystem(
timeindex=timeindex,
Expand Down

0 comments on commit 0b32a32

Please sign in to comment.