Select Git revision
gitlab-simgrid.yml
adapter.py 6.89 KiB
"""
MIT License
Copyright (c) 2023 RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from Model_Library.component.core import (
AbstractComponent,
ComponentCapacity,
ComponentCommodity,
ComponentKind,
ComponentLink,
ComponentPart,
)
from Model_Library.optimization_model import VariableKind
import pyomo.environ as pyo
class AssetLink(ComponentLink):
def __init__(self, asset, asset_var_name, start, var_name, end):
self.asset = asset
self.asset_var_name = asset_var_name
self.start = start
self.var_name = var_name
self.end = end
class MemberLink(ComponentLink):
def __init__(self, member, member_res_name, local_res_name, end):
self.member = member
self.member_res_name = member_res_name
self.local_res_name = local_res_name
self.end = end
class AssetAdapter(AbstractComponent):
def __init__(self, name, configuration, assets):
self.asset = assets[configuration["asset"]]
self.grid = self.asset._components[configuration["grid"]]
super().__init__(
name=name,
commodity_1=self.grid.commodity,
commodity_2=None,
commodity_3=self.grid.commodity,
commodity_4=None,
configuration=configuration,
capacity=ComponentCapacity.NONE,
)
self.commodity = self.grid.commodity
def match(self, kind=ComponentKind.ALL, commodity=ComponentCommodity.ALL):
match_kind = kind == ComponentKind.ALL or kind == ComponentKind.ASSET_ADAPTER
match_commodity = (
commodity == ComponentCommodity.ALL
or commodity == self.commodity
or (isinstance(commodity, list) and self.commodity in commodity)
)
return match_kind and match_commodity
def iter_component_parts(self):
yield ComponentPart.NONE_STATE
def iter_links(self):
yield AssetLink(
self.asset,
self.grid.name + ".output_1",
(self.grid.name, ComponentPart.NONE_STATE),
self.name + ".into_asset",
(self.name, ComponentPart.NONE_STATE),
)
yield AssetLink(
self.asset,
self.grid.name + ".input_1",
(self.grid.name, ComponentPart.NONE_STATE),
self.name + ".from_asset",
(self.name, ComponentPart.NONE_STATE),
)
def non_state_base_variable_names(self):
return [
(self.name + ".input_1", VariableKind.INDEXED),
(self.name + ".output_1", VariableKind.INDEXED),
]
def add_non_state_variables(self, o_block):
input = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".input_1", input)
output = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".output_1", output)
def add_non_state_model(self, d_block, o_block):
input = o_block.component_dict[self.name + ".input_1"]
output = o_block.component_dict[self.name + ".output_1"]
into_asset = o_block.component_dict[self.name + ".into_asset"]
from_asset = o_block.component_dict[self.name + ".from_asset"]
def rule(m, t):
return input[t] == into_asset[t]
o_block.add(self.name + ".input_cons", pyo.Constraint(o_block.T, rule=rule))
def rule(m, t):
return output[t] == from_asset[t]
o_block.add(self.name + ".output_cons", pyo.Constraint(o_block.T, rule=rule))
class MemberAdapter(AbstractComponent):
def __init__(self, name, configuration, members):
self.member = members[configuration["member"]]
self.grid = self.member._components[configuration["grid"]]
super().__init__(
name=name,
commodity_1=self.grid.commodity,
commodity_2=None,
commodity_3=self.grid.commodity,
commodity_4=None,
configuration=configuration,
capacity=ComponentCapacity.NONE,
)
self.commodity = self.grid.commodity
def match(self, kind=ComponentKind.ALL, commodity=ComponentCommodity.ALL):
match_kind = kind == ComponentKind.ALL or kind == ComponentKind.ASSET_ADAPTER
match_commodity = (
commodity == ComponentCommodity.ALL
or commodity == self.commodity
or (isinstance(commodity, list) and self.commodity in commodity)
)
return match_kind and match_commodity
def iter_component_parts(self):
yield ComponentPart.NONE_STATE
def iter_links(self):
yield MemberLink(
self.member,
self.grid.name + ".output_1",
"into_member",
(self.name, ComponentPart.NONE_STATE),
)
yield MemberLink(
self.member,
self.grid.name + ".input_1",
"from_member",
(self.name, ComponentPart.NONE_STATE),
)
def non_state_base_variable_names(self):
return [
(self.name + ".input_1", VariableKind.INDEXED),
(self.name + ".output_1", VariableKind.INDEXED),
]
def add_non_state_variables(self, o_block):
input = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".input_1", input)
output = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".output_1", output)
def add_non_state_model(self, d_block, o_block):
input = o_block.component_dict[self.name + ".input_1"]
output = o_block.component_dict[self.name + ".output_1"]
into_member = self.into_member.resample(o_block.dynamic).values
from_member = self.from_member.resample(o_block.dynamic).values
def rule(m, t):
return input[t] == into_member[t]
o_block.add(self.name + ".input_cons", pyo.Constraint(o_block.T, rule=rule))
def rule(m, t):
return output[t] == from_member[t]
o_block.add(self.name + ".output_cons", pyo.Constraint(o_block.T, rule=rule))