Skip to content

Commit

Permalink
implement binding constraint with write_timeseries for homogeneous re…
Browse files Browse the repository at this point in the history
…asons
  • Loading branch information
killian-scalian committed Feb 25, 2025
1 parent 4d961b8 commit b7034a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from antares.craft.service.local_services.models.binding_constraint import BindingConstraintPropertiesLocal
from antares.craft.tools.contents_tool import transform_name_to_id
from antares.craft.tools.ini_tool import IniFile, InitializationFilesTypes
from antares.craft.tools.matrix_tool import df_read, df_save
from antares.craft.tools.matrix_tool import df_read, write_timeseries
from antares.craft.tools.time_series_tool import TimeSeriesFileType
from typing_extensions import override

Expand Down Expand Up @@ -78,23 +78,15 @@ def _store_time_series(
equal_term_matrix: Optional[pd.DataFrame],
greater_term_matrix: Optional[pd.DataFrame],
) -> None:
time_series = []
file_types = []
# Lesser or greater can happen together when operator is both
if constraint.properties.operator in (BindingConstraintOperator.LESS, BindingConstraintOperator.BOTH):
time_series += [self._check_if_empty_ts(constraint.properties.time_step, less_term_matrix)]
file_types += [TimeSeriesFileType.BINDING_CONSTRAINT_LESS]
write_timeseries(self.config.study_path, self._check_if_empty_ts(constraint.properties.time_step, less_term_matrix), TimeSeriesFileType.BINDING_CONSTRAINT_LESS, constraint_id=constraint.id)
if constraint.properties.operator in (BindingConstraintOperator.GREATER, BindingConstraintOperator.BOTH):
time_series += [self._check_if_empty_ts(constraint.properties.time_step, greater_term_matrix)]
file_types += [TimeSeriesFileType.BINDING_CONSTRAINT_GREATER]
write_timeseries(self.config.study_path, self._check_if_empty_ts(constraint.properties.time_step, greater_term_matrix), TimeSeriesFileType.BINDING_CONSTRAINT_GREATER, constraint_id=constraint.id)
# Equal is always exclusive
if constraint.properties.operator == BindingConstraintOperator.EQUAL:
time_series = [self._check_if_empty_ts(constraint.properties.time_step, equal_term_matrix)]
file_types = [TimeSeriesFileType.BINDING_CONSTRAINT_EQUAL]
write_timeseries(self.config.study_path, self._check_if_empty_ts(constraint.properties.time_step, equal_term_matrix), TimeSeriesFileType.BINDING_CONSTRAINT_EQUAL, constraint_id=constraint.id)

for ts, file_type in zip(time_series, file_types):
matrix_path = self.config.study_path.joinpath(file_type.value.format(constraint_id=constraint.id))
df_save(ts, matrix_path)

@staticmethod
def _check_if_empty_ts(time_step: BindingConstraintFrequency, time_series: Optional[pd.DataFrame]) -> pd.DataFrame:
Expand Down
10 changes: 5 additions & 5 deletions src/antares/craft/tools/matrix_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ def write_timeseries(
study_path: Path,
series: pd.DataFrame,
ts_file_type: TimeSeriesFileType,
area_id: str,
area_id: Optional[str] = None,
cluster_id: Optional[str] = None,
second_area_id: Optional[str] = None,
constraint_id: Optional[str] = None,
) -> None:
# This method does not concern binding constraints timeseries, which is handled differently

format_kwargs = {"area_id": area_id}
format_kwargs = {}
if area_id:
format_kwargs["area_id"] = area_id
if cluster_id:
format_kwargs["cluster_id"] = cluster_id
if second_area_id:
format_kwargs["second_area_id"] = second_area_id
if constraint_id:
format_kwargs["constraint_id"] = constraint_id

file_path = study_path / ts_file_type.value.format(**format_kwargs)

file_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
4 changes: 4 additions & 0 deletions tests/antares/tools/test_contents_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ def test_write_timeseries(tmpdir):
link_capacity_direct_path = file_path / "input/links/fr/capacities/es_direct.txt"
assert link_capacity_direct_path.exists()
assert link_capacity_direct_path.is_file()
write_timeseries(file_path, df, TimeSeriesFileType.BINDING_CONSTRAINT_EQUAL, constraint_id="constraint_1")
thermal_modulation_path = file_path / "input/bindingconstraints/constraint_1_eq.txt"
assert thermal_modulation_path.exists()
assert thermal_modulation_path.is_file()

0 comments on commit b7034a0

Please sign in to comment.