Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add length check (seting it) for sequences #1116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/oemof/solph/_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ def sequence(iterable_or_scalar):
return _FakeSequence(value=iterable_or_scalar)


def valid_sequence(sequence, length: int) -> bool:
if sequence[0] is None:
return False

if isinstance(sequence, _FakeSequence):
sequence.size = length
return True
if isinstance(sequence, np.ndarray):
if sequence.size >= length:
return True
else:
raise ValueError(f"Lentgh of {sequence} should be {length}")

return False


class _FakeSequence:
"""Emulates a list whose length is not known in advance.

Expand Down
6 changes: 3 additions & 3 deletions src/oemof/solph/components/_extraction_turbine_chp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from pyomo.environ import BuildAction
from pyomo.environ import Constraint

from oemof.solph._plumbing import sequence as solph_sequence
from oemof.solph.components._converter import Converter
from oemof.solph._plumbing import sequence
from oemof.solph.components import Converter


class ExtractionTurbineCHP(Converter):
Expand Down Expand Up @@ -87,7 +87,7 @@ def __init__(
custom_attributes=custom_attributes,
)
self.conversion_factor_full_condensation = {
k: solph_sequence(v)
k: sequence(v)
for k, v in conversion_factor_full_condensation.items()
}

Expand Down
39 changes: 18 additions & 21 deletions src/oemof/solph/components/_generic_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

from oemof.solph._helpers import check_node_object_for_missing_attribute
from oemof.solph._options import Investment
from oemof.solph._plumbing import sequence as solph_sequence
from oemof.solph._plumbing import sequence
from oemof.solph._plumbing import valid_sequence


class GenericStorage(Node):
Expand Down Expand Up @@ -225,26 +226,22 @@ def __init__(

self.initial_storage_level = initial_storage_level
self.balanced = balanced
self.loss_rate = solph_sequence(loss_rate)
self.fixed_losses_relative = solph_sequence(fixed_losses_relative)
self.fixed_losses_absolute = solph_sequence(fixed_losses_absolute)
self.inflow_conversion_factor = solph_sequence(
inflow_conversion_factor
)
self.outflow_conversion_factor = solph_sequence(
outflow_conversion_factor
)
self.max_storage_level = solph_sequence(max_storage_level)
self.min_storage_level = solph_sequence(min_storage_level)
self.fixed_costs = solph_sequence(fixed_costs)
self.storage_costs = solph_sequence(storage_costs)
self.invest_relation_input_output = solph_sequence(
self.loss_rate = sequence(loss_rate)
self.fixed_losses_relative = sequence(fixed_losses_relative)
self.fixed_losses_absolute = sequence(fixed_losses_absolute)
self.inflow_conversion_factor = sequence(inflow_conversion_factor)
self.outflow_conversion_factor = sequence(outflow_conversion_factor)
self.max_storage_level = sequence(max_storage_level)
self.min_storage_level = sequence(min_storage_level)
self.fixed_costs = sequence(fixed_costs)
self.storage_costs = sequence(storage_costs)
self.invest_relation_input_output = sequence(
invest_relation_input_output
)
self.invest_relation_input_capacity = solph_sequence(
self.invest_relation_input_capacity = sequence(
invest_relation_input_capacity
)
self.invest_relation_output_capacity = solph_sequence(
self.invest_relation_output_capacity = sequence(
invest_relation_output_capacity
)
self.lifetime_inflow = lifetime_inflow
Expand Down Expand Up @@ -607,7 +604,7 @@ def _objective_expression(self):

if m.es.periods is not None:
for n in self.STORAGES:
if n.fixed_costs[0] is not None:
if valid_sequence(n.fixed_costs, len(m.PERIODS)):
fixed_costs += sum(
n.nominal_storage_capacity
* n.fixed_costs[pp]
Expand All @@ -619,7 +616,7 @@ def _objective_expression(self):
storage_costs = 0

for n in self.STORAGES:
if n.storage_costs[0] is not None:
if valid_sequence(n.storage_costs, len(m.TIMESTEPS)):
# We actually want to iterate over all TIMEPOINTS except the
# 0th. As integers are used for the index, this is equicalent
# to iterating over the TIMESTEPS with one offset.
Expand Down Expand Up @@ -1875,7 +1872,7 @@ def _objective_expression(self):
period_investment_costs[p] += investment_costs_increment

for n in self.INVESTSTORAGES:
if n.investment.fixed_costs[0] is not None:
if valid_sequence(n.investment.fixed_costs, len(m.PERIODS)):
lifetime = n.investment.lifetime
for p in m.PERIODS:
range_limit = min(
Expand All @@ -1893,7 +1890,7 @@ def _objective_expression(self):
)

for n in self.EXISTING_INVESTSTORAGES:
if n.investment.fixed_costs[0] is not None:
if valid_sequence(n.investment.fixed_costs, len(m.PERIODS)):
lifetime = n.investment.lifetime
age = n.investment.age
range_limit = min(
Expand Down
19 changes: 10 additions & 9 deletions src/oemof/solph/components/experimental/_sink_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

from oemof.solph._options import Investment
from oemof.solph._plumbing import sequence
from oemof.solph._plumbing import valid_sequence
from oemof.solph.components._sink import Sink


Expand Down Expand Up @@ -703,7 +704,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.fixed_costs[0] is not None:
if valid_sequence(g.fixed_costs, len(m.PERIODS)):
fixed_costs += sum(
max(g.max_capacity_up, g.max_capacity_down)
* g.fixed_costs[pp]
Expand Down Expand Up @@ -1434,7 +1435,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
for p in m.PERIODS:
range_limit = min(
Expand All @@ -1452,7 +1453,7 @@ def _objective_expression(self):
)

for g in self.EXISTING_INVESTDSM:
if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
age = g.investment.age
range_limit = min(
Expand Down Expand Up @@ -2198,7 +2199,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.fixed_costs[0] is not None:
if valid_sequence(g.fixed_costs, len(m.PERIODS)):
fixed_costs += sum(
max(g.max_capacity_up, g.max_capacity_down)
* g.fixed_costs[pp]
Expand Down Expand Up @@ -3290,7 +3291,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
for p in m.PERIODS:
range_limit = min(
Expand All @@ -3308,7 +3309,7 @@ def _objective_expression(self):
)

for g in self.EXISTING_INVESTDSM:
if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
age = g.investment.age
range_limit = min(
Expand Down Expand Up @@ -4391,7 +4392,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.fixed_costs[0] is not None:
if valid_sequence(g.fixed_costs, len(m.PERIODS)):
fixed_costs += sum(
max(g.max_capacity_up, g.max_capacity_down)
* g.fixed_costs[pp]
Expand Down Expand Up @@ -5791,7 +5792,7 @@ def _objective_expression(self):
* (1 + m.discount_rate) ** (-m.es.periods_years[p])
)

if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
for p in m.PERIODS:
range_limit = min(
Expand All @@ -5809,7 +5810,7 @@ def _objective_expression(self):
)

for g in self.EXISTING_INVESTDSM:
if g.investment.fixed_costs[0] is not None:
if valid_sequence(g.investment.fixed_costs, len(m.PERIODS)):
lifetime = g.investment.lifetime
age = g.investment.age
range_limit = min(
Expand Down
10 changes: 8 additions & 2 deletions src/oemof/solph/flows/_investment_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from pyomo.core import Var
from pyomo.core.base.block import ScalarBlock

from oemof.solph._plumbing import valid_sequence


class InvestmentFlowBlock(ScalarBlock):
r"""Block for all flows with :attr:`Investment` being not None.
Expand Down Expand Up @@ -1007,7 +1009,9 @@ def _objective_expression(self):
period_investment_costs[p] += investment_costs_increment

for i, o in self.INVESTFLOWS:
if m.flows[i, o].investment.fixed_costs[0] is not None:
if valid_sequence(
m.flows[i, o].investment.fixed_costs, len(m.PERIODS)
):
lifetime = m.flows[i, o].investment.lifetime
for p in m.PERIODS:
range_limit = min(
Expand All @@ -1022,7 +1026,9 @@ def _objective_expression(self):
)

for i, o in self.EXISTING_INVESTFLOWS:
if m.flows[i, o].investment.fixed_costs[0] is not None:
if valid_sequence(
m.flows[i, o].investment.fixed_costs, len(m.PERIODS)
):
lifetime = m.flows[i, o].investment.lifetime
age = m.flows[i, o].investment.age
range_limit = min(
Expand Down
18 changes: 14 additions & 4 deletions src/oemof/solph/flows/_non_convex_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from pyomo.core import Var
from pyomo.core.base.block import ScalarBlock

from oemof.solph._plumbing import valid_sequence


class NonConvexFlowBlock(ScalarBlock):
r"""
Expand Down Expand Up @@ -326,7 +328,9 @@ def _startup_costs(self):
m = self.parent_block()

for i, o in self.STARTUPFLOWS:
if m.flows[i, o].nonconvex.startup_costs[0] is not None:
if valid_sequence(
m.flows[i, o].nonconvex.startup_costs, len(m.TIMESTEPS)
):
startup_costs += sum(
self.startup[i, o, t]
* m.flows[i, o].nonconvex.startup_costs[t]
Expand All @@ -349,7 +353,9 @@ def _shutdown_costs(self):
m = self.parent_block()

for i, o in self.SHUTDOWNFLOWS:
if m.flows[i, o].nonconvex.shutdown_costs[0] is not None:
if valid_sequence(
m.flows[i, o].nonconvex.shutdown_costs, len(m.TIMESTEPS)
):
shutdown_costs += sum(
self.shutdown[i, o, t]
* m.flows[i, o].nonconvex.shutdown_costs[t]
Expand All @@ -372,7 +378,9 @@ def _activity_costs(self):
m = self.parent_block()

for i, o in self.ACTIVITYCOSTFLOWS:
if m.flows[i, o].nonconvex.activity_costs[0] is not None:
if valid_sequence(
m.flows[i, o].nonconvex.activity_costs, len(m.TIMESTEPS)
):
activity_costs += sum(
self.status[i, o, t]
* m.flows[i, o].nonconvex.activity_costs[t]
Expand All @@ -395,7 +403,9 @@ def _inactivity_costs(self):
m = self.parent_block()

for i, o in self.INACTIVITYCOSTFLOWS:
if m.flows[i, o].nonconvex.inactivity_costs[0] is not None:
if valid_sequence(
m.flows[i, o].nonconvex.inactivity_costs, len(m.TIMESTEPS)
):
inactivity_costs += sum(
(1 - self.status[i, o, t])
* m.flows[i, o].nonconvex.inactivity_costs[t]
Expand Down
22 changes: 16 additions & 6 deletions src/oemof/solph/flows/_simple_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from pyomo.core import Var
from pyomo.core.base.block import ScalarBlock

from oemof.solph._plumbing import valid_sequence


class SimpleFlowBlock(ScalarBlock):
r"""Flow block with definitions for standard flows.
Expand Down Expand Up @@ -172,12 +174,16 @@ def _create_variables(self, group):
)
# set upper bound of gradient variable
for i, o, f in group:
if m.flows[i, o].positive_gradient_limit[0] is not None:
if valid_sequence(
m.flows[i, o].positive_gradient_limit, len(m.TIMESTEPS)
):
for t in m.TIMESTEPS:
self.positive_gradient[i, o, t].setub(
f.positive_gradient_limit[t] * f.nominal_value
)
if m.flows[i, o].negative_gradient_limit[0] is not None:
if valid_sequence(
m.flows[i, o].negative_gradient_limit, len(m.TIMESTEPS)
):
for t in m.TIMESTEPS:
self.negative_gradient[i, o, t].setub(
f.negative_gradient_limit[t] * f.nominal_value
Expand Down Expand Up @@ -429,7 +435,9 @@ def _objective_expression(self):

if m.es.periods is None:
for i, o in m.FLOWS:
if m.flows[i, o].variable_costs[0] is not None:
if valid_sequence(
m.flows[i, o].variable_costs, len(m.TIMESTEPS)
):
for t in m.TIMESTEPS:
variable_costs += (
m.flow[i, o, t]
Expand All @@ -439,7 +447,9 @@ def _objective_expression(self):

else:
for i, o in m.FLOWS:
if m.flows[i, o].variable_costs[0] is not None:
if valid_sequence(
m.flows[i, o].variable_costs, len(m.TIMESTEPS)
):
for p, t in m.TIMEINDEX:
variable_costs += (
m.flow[i, o, t]
Expand All @@ -464,7 +474,7 @@ def _objective_expression(self):

# Fixed costs for units with limited lifetime
for i, o in self.LIFETIME_FLOWS:
if m.flows[i, o].fixed_costs[0] is not None:
if valid_sequence(m.flows[i, o].fixed_costs, len(m.TIMESTEPS)):
range_limit = min(
m.es.end_year_of_optimization,
m.flows[i, o].lifetime,
Expand All @@ -477,7 +487,7 @@ def _objective_expression(self):
)

for i, o in self.LIFETIME_AGE_FLOWS:
if m.flows[i, o].fixed_costs[0] is not None:
if valid_sequence(m.flows[i, o].fixed_costs, len(m.TIMESTEPS)):
range_limit = min(
m.es.end_year_of_optimization,
m.flows[i, o].lifetime - m.flows[i, o].age,
Expand Down
4 changes: 2 additions & 2 deletions src/oemof/solph/flows/experimental/_electrical_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pyomo.environ import Set
from pyomo.environ import Var

from oemof.solph._plumbing import sequence as solph_sequence
from oemof.solph._plumbing import sequence
from oemof.solph.buses.experimental._electrical_bus import ElectricalBus
from oemof.solph.flows._flow import Flow

Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, **kwargs):
nonconvex=kwargs.get("nonconvex"),
custom_attributes=kwargs.get("costom_attributes"),
)
self.reactance = solph_sequence(kwargs.get("reactance", 0.00001))
self.reactance = sequence(kwargs.get("reactance", 0.00001))

self.input = kwargs.get("input")
self.output = kwargs.get("output")
Expand Down
Loading