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

contrib: Add Links conversion to yaml components #61

Merged
merged 2 commits into from
Jan 16, 2025
Merged
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
63 changes: 63 additions & 0 deletions src/andromede/input_converter/src/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,66 @@ def _convert_thermal_to_component_list(
)
return components, connections

def _convert_link_to_component_list(
self,
) -> tuple[list[InputComponent], list[InputPortConnections]]:
components = []
connections = []
# Add links components for each area
links = self.study.read_links()
for link in links:
capacity_direct_path = (
self.study_path
/ "input"
/ "links"
/ Path(link.area_from_id)
/ "capacities"
/ f"{link.area_to_id}_direct.txt"
)
capacity_indirect_path = (
self.study_path
/ "input"
/ "links"
/ Path(link.area_from_id)
/ "capacities"
/ f"{link.area_to_id}_indirect.txt"
)
components.append(
InputComponent(
id=link.id,
model="link",
parameters=[
InputComponentParameter(
name="capacity_direct",
type="timeseries",
timeseries=str(capacity_direct_path),
),
InputComponentParameter(
name="capacity_indirect",
type="timeseries",
timeseries=str(capacity_indirect_path),
),
],
)
)
connections.append(
InputPortConnections(
component1=link.id,
port_1="in_port",
component2=link.area_from_id,
port_2="balance_port",
)
)
connections.append(
InputPortConnections(
component1=link.id,
port_1="out_port",
component2=link.area_to_id,
port_2="balance_port",
),
)
return components, connections

def _convert_wind_to_component_list(
self, areas: list[Area]
) -> tuple[list[InputComponent], list[InputPortConnections]]:
Expand Down Expand Up @@ -312,6 +372,9 @@ def convert_study_to_input_study(self) -> InputStudy:
list_components: list[InputComponent] = []
list_connections: list[InputPortConnections] = []

components, connections = self._convert_link_to_component_list()
list_components.extend(components)
list_connections.extend(connections)
conversion_methods = [
self._convert_renewable_to_component_list,
self._convert_thermal_to_component_list,
Expand Down
228 changes: 130 additions & 98 deletions tests/input_converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,16 @@ def _init_area_reading(self, local_study):
areas = converter.study.read_areas()
return areas, converter

def test_convert_study_to_input_study(self, local_study_w_thermal):
converter = AntaresStudyConverter(study_input=local_study_w_thermal)
def test_convert_study_to_input_study(self, local_study_w_areas):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing this test ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was a little redondant, and with the thermal local study i was forced to add tests for links, which is very long tests. I preferred to make specific tests for links ( with specific thermal tests too)

converter = AntaresStudyConverter(study_input=local_study_w_areas)
input_study = converter.convert_study_to_input_study()

p_max_thermal_timeserie = str(
converter.study_path
/ "input"
/ "thermal"
/ "series"
/ "fr"
/ "gaz"
/ "series.txt"
)
expected_input_study = InputStudy(
nodes=[
InputComponent(
id="fr",
model="area",
scenario_group=None,
parameters=[
InputComponentParameter(
name="energy_cost_unsupplied",
Expand All @@ -66,6 +58,7 @@ def test_convert_study_to_input_study(self, local_study_w_thermal):
InputComponent(
id="it",
model="area",
scenario_group=None,
parameters=[
InputComponentParameter(
name="energy_cost_unsupplied",
Expand All @@ -83,101 +76,17 @@ def test_convert_study_to_input_study(self, local_study_w_thermal):
),
],
),
InputComponent(
id="at",
model="area",
scenario_group=None,
parameters=[
InputComponentParameter(
name="energy_cost_unsupplied",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
InputComponentParameter(
name="energy_cost_spilled",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
],
),
],
components=[
InputComponent(
id="gaz",
model="thermal",
scenario_group=None,
parameters=[
InputComponentParameter(
name="unit_count",
type="constant",
scenario_group=None,
value=1.0,
timeseries=None,
),
InputComponentParameter(
name="efficiency",
type="constant",
scenario_group=None,
value=100.0,
timeseries=None,
),
InputComponentParameter(
name="nominal_capacity",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
InputComponentParameter(
name="marginal_cost",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
InputComponentParameter(
name="fixed_cost",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
InputComponentParameter(
name="startup_cost",
type="constant",
scenario_group=None,
value=0.0,
timeseries=None,
),
InputComponentParameter(
name="p_max_cluster",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{p_max_thermal_timeserie}",
),
],
)
],
connections=[
InputPortConnections(
component1="gaz",
port_1="balance_port",
component2="fr",
port_2="balance_port",
)
],
components=[],
connections=[],
)

# To ensure that the comparison between the actual and expected results is not affected by the order of the nodes,
# both the area_components.nodes and expected_area_components.nodes lists are sorted by the id attribute of each node.
# This sorting step ensures that the test checks only the presence and validity of the nodes, not their order.
input_study.nodes.sort(key=lambda x: x.id)
expected_input_study.nodes.sort(key=lambda x: x.id)

assert input_study == expected_input_study

def test_convert_area_to_component(self, local_study_w_areas):
Expand Down Expand Up @@ -582,3 +491,126 @@ def test_convert_wind_to_component_zero_values(self, local_study_w_areas, fr_win
wind_components, _ = converter._convert_wind_to_component_list(areas)

assert wind_components == []

def test_convert_links_to_component(self, local_study_w_links):
_, converter = self._init_area_reading(local_study_w_links)
study_path = converter.study_path
(
links_components,
links_connections,
) = converter._convert_link_to_component_list()

fr_prefix_path = study_path / "input" / "links" / "fr" / "capacities"
at_prefix_path = study_path / "input" / "links" / "at" / "capacities"
fr_it_direct_links_timeseries = str(fr_prefix_path / "it_direct.txt")
fr_it_indirect_links_timeseries = str(fr_prefix_path / "it_indirect.txt")
at_fr_direct_links_timeseries = str(at_prefix_path / "fr_direct.txt")
at_fr_indirect_links_timeseries = str(at_prefix_path / "fr_indirect.txt")
at_it_direct_links_timeseries = str(at_prefix_path / "it_direct.txt")
at_it_indirect_links_timeseries = str(at_prefix_path / "it_indirect.txt")
expected_link_component = [
InputComponent(
id="fr / it",
model="link",
scenario_group=None,
parameters=[
InputComponentParameter(
name="capacity_direct",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{fr_it_direct_links_timeseries}",
),
InputComponentParameter(
name="capacity_indirect",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{fr_it_indirect_links_timeseries}",
),
],
),
InputComponent(
id="at / fr",
model="link",
scenario_group=None,
parameters=[
InputComponentParameter(
name="capacity_direct",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{at_fr_direct_links_timeseries}",
),
InputComponentParameter(
name="capacity_indirect",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{at_fr_indirect_links_timeseries}",
),
],
),
InputComponent(
id="at / it",
model="link",
scenario_group=None,
parameters=[
InputComponentParameter(
name="capacity_direct",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{at_it_direct_links_timeseries}",
),
InputComponentParameter(
name="capacity_indirect",
type="timeseries",
scenario_group=None,
value=None,
timeseries=f"{at_it_indirect_links_timeseries}",
),
],
),
]
expected_link_connections = [
InputPortConnections(
component1="fr / it",
port_1="in_port",
component2="fr",
port_2="balance_port",
),
InputPortConnections(
component1="fr / it",
port_1="out_port",
component2="it",
port_2="balance_port",
),
InputPortConnections(
component1="at / fr",
port_1="in_port",
component2="at",
port_2="balance_port",
),
InputPortConnections(
component1="at / fr",
port_1="out_port",
component2="fr",
port_2="balance_port",
),
InputPortConnections(
component1="at / it",
port_1="in_port",
component2="at",
port_2="balance_port",
),
InputPortConnections(
component1="at / it",
port_1="out_port",
component2="it",
port_2="balance_port",
),
]

assert links_components == expected_link_component
assert links_connections == expected_link_connections
Loading