Skip to content

Commit

Permalink
feat(model)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Nov 20, 2023
1 parent f3eabed commit 9c7bff4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,62 @@
from antarest.study.storage.rawstudy.model.filesystem.root.input.bindingconstraints.bindingconstraints_ini import (
BindingConstraintsIni,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series import (
default_binding_constraint_daily,
default_binding_constraint_hourly,
default_binding_constraint_weekly,
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import (
default_binding_constraint_daily as default_bd_87_daily,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import (
default_binding_constraint_hourly as default_bd_87_hourly,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_after_v87 import (
default_binding_constraint_weekly as default_bd_87_weekly,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import (
default_binding_constraint_daily as default_bd_86_daily,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import (
default_binding_constraint_hourly as default_bd_86_hourly,
)
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import (
default_binding_constraint_weekly as default_bd_86_weekly,
)


class BindingConstraints(FolderNode):
def build(self) -> TREE:
default_matrices = {
BindingConstraintFrequency.HOURLY: default_binding_constraint_hourly,
BindingConstraintFrequency.DAILY: default_binding_constraint_daily,
BindingConstraintFrequency.WEEKLY: default_binding_constraint_weekly,
}
children: TREE = {
binding.id: InputSeriesMatrix(
self.context,
self.config.next_file(f"{binding.id}.txt"),
freq=MatrixFrequency(binding.time_step),
nb_columns=3,
default_empty=default_matrices[binding.time_step],
)
for binding in self.config.bindings
}

cfg = self.config
if cfg.version < 870:
default_matrices = {
BindingConstraintFrequency.HOURLY: default_bd_86_hourly,
BindingConstraintFrequency.DAILY: default_bd_86_daily,
BindingConstraintFrequency.WEEKLY: default_bd_86_weekly,
}
children: TREE = {
binding.id: InputSeriesMatrix(
self.context,
self.config.next_file(f"{binding.id}.txt"),
freq=MatrixFrequency(binding.time_step),
nb_columns=3,
default_empty=default_matrices[binding.time_step],
)
for binding in self.config.bindings
}
else:
default_matrices = {
BindingConstraintFrequency.HOURLY: default_bd_87_hourly,
BindingConstraintFrequency.DAILY: default_bd_87_daily,
BindingConstraintFrequency.WEEKLY: default_bd_87_weekly,
}
children = {}
for binding in self.config.bindings:
for term in ["lt", "gt", "eq"]:
matrix_id = f"{binding.id}_{term}"
children[matrix_id] = InputSeriesMatrix(
self.context,
self.config.next_file(f"{matrix_id}.txt"),
freq=MatrixFrequency(binding.time_step),
nb_columns=1 if term in ["lt", "gt"] else None,
default_empty=default_matrices[binding.time_step],
)
children["bindingconstraints"] = BindingConstraintsIni(
self.context, self.config.next_file("bindingconstraints.ini")
)
Expand Down
37 changes: 10 additions & 27 deletions antarest/study/storage/study_upgrader/upgrader_870.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,18 @@ def upgrade_870(study_path: Path) -> None:
for file in binding_constraints_files:
name = Path(file).stem
if os.path.getsize(file) == 0:
low_term = pandas.Series()
great_term = pandas.Series()
equality_term = pandas.Series()
lt, gt, eq = pandas.Series(), pandas.Series(), pandas.Series()
else:
df = pandas.read_csv(file, sep="\t", header=None)
low_term = df.iloc[:, 0]
great_term = df.iloc[:, 1]
equality_term = df.iloc[:, 2]
# noinspection PyTypeChecker
np.savetxt(
binding_constraints_path / f"{name}_lt.txt",
cast(npt.NDArray[np.float64], low_term.values),
delimiter="\t",
fmt="%.6f",
)
# noinspection PyTypeChecker
np.savetxt(
binding_constraints_path / f"{name}_gt.txt",
cast(npt.NDArray[np.float64], great_term.values),
delimiter="\t",
fmt="%.6f",
)
# noinspection PyTypeChecker
np.savetxt(
binding_constraints_path / f"{name}_eq.txt",
cast(npt.NDArray[np.float64], equality_term.values),
delimiter="\t",
fmt="%.6f",
)
lt, gt, eq = df.iloc[:, 0], df.iloc[:, 1], df.iloc[:, 2]
for term in [lt, gt, eq]:
# noinspection PyTypeChecker
np.savetxt(
binding_constraints_path / f"{name}_{str(term)}.txt",
cast(npt.NDArray[np.float64], term.values),
delimiter="\t",
fmt="%.6f",
)
Path(file).unlink()

# Add property group for every section in .ini file
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import series
from . import series_after_v87, series_before_v87
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np

default_binding_constraint_hourly = np.zeros((8760, 1), dtype=np.float64)
default_binding_constraint_hourly.flags.writeable = False

default_binding_constraint_daily = np.zeros((365, 1), dtype=np.float64)
default_binding_constraint_daily.flags.writeable = False

default_binding_constraint_weekly = np.zeros((52, 1), dtype=np.float64)
default_binding_constraint_weekly.flags.writeable = False
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _init(self) -> None:
self.hashes[MISCGEN_TS] = self.matrix_service.create(FIXED_8_COLUMNS)

# Binding constraint matrices
series = matrix_constants.binding_constraint.series
series = matrix_constants.binding_constraint.series_before_v87
self.hashes[BINDING_CONSTRAINT_HOURLY] = self.matrix_service.create(series.default_binding_constraint_hourly)
self.hashes[BINDING_CONSTRAINT_DAILY] = self.matrix_service.create(series.default_binding_constraint_daily)
self.hashes[BINDING_CONSTRAINT_WEEKLY] = self.matrix_service.create(series.default_binding_constraint_weekly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.variantstudy.business.command_extractor import CommandExtractor
from antarest.study.storage.variantstudy.business.command_reverter import CommandReverter
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series import (
from antarest.study.storage.variantstudy.business.matrix_constants.binding_constraint.series_before_v87 import (
default_binding_constraint_daily,
default_binding_constraint_hourly,
default_binding_constraint_weekly,
Expand Down

0 comments on commit 9c7bff4

Please sign in to comment.