From 24c0bfc3f964eac12b09db45b96e81df15da43f6 Mon Sep 17 00:00:00 2001 From: ChouaneLouis Date: Mon, 6 May 2024 17:19:23 +0200 Subject: [PATCH] add executable main file to test see tests/unittests/data/components_for_thermal_cluster.yml --- src/andromede/main/main.py | 87 ++++++++++++++ .../data/components_for_thermal_cluster.yml | 81 +++++++++++++ tests/unittests/data/demand-ts_2.txt | 3 + tests/unittests/data/lib_2.yml | 109 ++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 src/andromede/main/main.py create mode 100644 tests/unittests/data/components_for_thermal_cluster.yml create mode 100644 tests/unittests/data/demand-ts_2.txt create mode 100644 tests/unittests/data/lib_2.yml diff --git a/src/andromede/main/main.py b/src/andromede/main/main.py new file mode 100644 index 00000000..c04aa002 --- /dev/null +++ b/src/andromede/main/main.py @@ -0,0 +1,87 @@ +# Copyright (c) 2024, RTE (https://www.rte-france.com) +# +# See AUTHORS.txt +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 +# +# This file is part of the Antares project. +import argparse +from pathlib import Path +from typing import Optional + +from andromede.model.library import Library +from andromede.model.parsing import parse_yaml_library +from andromede.model.resolve_library import resolve_library +from andromede.simulation import OutputValues, TimeBlock, build_problem +from andromede.study import DataBase +from andromede.study.parsing import parse_yaml_components +from andromede.study.resolve_components import ( + NetworkComponents, + build_data_base, + build_network, + consistency_check, + resolve_components_and_cnx, +) + + +class AntaresTimeSeriesImportError(Exception): + pass + + +def input_models(model_path: Path) -> Library: + with model_path.open() as lib: + return resolve_library(parse_yaml_library(lib)) + + +def input_database(study_path: Path, timeseries_path: Optional[Path]) -> DataBase: + with study_path.open() as comp: + return build_data_base(parse_yaml_components(comp), timeseries_path) + + +def input_components(study_path: Path, model: Library) -> NetworkComponents: + with study_path.open() as comp: + return resolve_components_and_cnx(parse_yaml_components(comp), model) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", type=Path, help="path to the model file, *.yml", required=True + ) + parser.add_argument( + "--study", type=Path, help="path to the study file, *.yml", required=True + ) + parser.add_argument( + "--timeseries", type=Path, help="path to the timeseries repertory" + ) + parser.add_argument( + "--duration", type=int, help="duration of the simulation", default=1 + ) + + args = parser.parse_args() + + models = input_models(args.model) + components = input_components(args.study, models) + consistency_check(components.components, models.models) + + try: + database = input_database(args.study, args.timeseries) + except UnboundLocalError: + raise AntaresTimeSeriesImportError( + "An error occurred while importing time series. Did you correctly use the '--timeseries' parameter ?" + ) + network = build_network(components) + + scenarios = 1 + timeblock = TimeBlock(1, list(range(args.duration))) + problem = build_problem(network, database, timeblock, scenarios) + + status = problem.solver.Solve() + print(status) + print(problem.solver.Objective().Value()) + + output = OutputValues(problem) diff --git a/tests/unittests/data/components_for_thermal_cluster.yml b/tests/unittests/data/components_for_thermal_cluster.yml new file mode 100644 index 00000000..c477f351 --- /dev/null +++ b/tests/unittests/data/components_for_thermal_cluster.yml @@ -0,0 +1,81 @@ +# Copyright (c) 2024, RTE (https://www.rte-france.com) +# +# See AUTHORS.txt +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 +# +# This file is part of the Antares project. + + +#test file for command line execution +#run : +#$ python src/andromede/main/main.py --model tests/unittests/data/lib_2.yml --study tests/unittests/data/components_for_thermal_cluster.yml --duration 3 --timeseries tests/unittests/data/ +#expected value : +#> 0 +#> 72000 + +study: + nodes: + - id: N + model: node + + components: + - id: G + model: thermal-cluster-dhd + parameters: + - name: cost + type: constant + value: 100 + - name: p_min + type: constant + value: 100 + - name: p_max + type: constant + value: 500 + - name: d_min_up + type: constant + value: 3 + - name: d_min_down + type: constant + value: 3 + - name: nb_units_max + type: constant + value: 1 + - name: nb_failures + type: constant + value: 0 + - id: D + model: demand + parameters: + - name: demand + type: timeseries + timeseries: demand-ts_2 + - id: S + model: spillage + parameters: + - name: cost + type: constant + value: 10 + + connections: + - component1: N + port_1: injection_port + component2: D + port_2: injection_port + + - component1: N + port_1: injection_port + component2: G + port_2: injection_port + + - component1: N + port_1: injection_port + component2: S + port_2: injection_port + + + diff --git a/tests/unittests/data/demand-ts_2.txt b/tests/unittests/data/demand-ts_2.txt new file mode 100644 index 00000000..ff60702d --- /dev/null +++ b/tests/unittests/data/demand-ts_2.txt @@ -0,0 +1,3 @@ +500 +0 +0 \ No newline at end of file diff --git a/tests/unittests/data/lib_2.yml b/tests/unittests/data/lib_2.yml new file mode 100644 index 00000000..cf398567 --- /dev/null +++ b/tests/unittests/data/lib_2.yml @@ -0,0 +1,109 @@ +# Copyright (c) 2024, RTE (https://www.rte-france.com) +# +# See AUTHORS.txt +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 +# +# This file is part of the Antares project. +library: + id: basic + description: Basic library + + port-types: + - id: flow + description: A port which transfers power flow + fields: + - name: flow + + models: + + + - id: node + description: A basic balancing node model + ports: + - name: injection_port + type: flow + binding-constraints: + - name: balance + expression: sum_connections(injection_port.flow) = 0 + + - id: spillage + description: A basic spillage model + parameters: + - name: cost + time-dependent: false + scenario-dependent: false + variables: + - name: spillage + lower-bound: 0 + ports: + - name: injection_port + type: flow + port-field-definitions: + - port: injection_port + field: flow + definition: -spillage + objective: expec(sum(cost * spillage)) + + - id: demand + description: A basic fixed demand model + parameters: + - name: demand + time-dependent: true + scenario-dependent: true + ports: + - name: injection_port + type: flow + port-field-definitions: + - port: injection_port + field: flow + definition: -demand + + - id: thermal-cluster-dhd + parameters: + - name: p_max + - name: p_min + - name: cost + - name: d_min_up + - name: d_min_down + - name: nb_units_max + - name: nb_failures + variables: + - name: nb_units_on + lower-bound: 0 + upper-bound: nb_units_max + variable-type: integer + - name: nb_starting + lower-bound: 0 + upper-bound: nb_units_max + variable-type: integer + - name: nb_stoping + lower-bound: 0 + upper-bound: nb_units_max + variable-type: integer + - name: production + lower-bound: 0 + upper-bound: nb_units_max * p_max + ports: + - name: injection_port + type: flow + port-field-definitions: + - port: injection_port + field: flow + definition: production + constraints: + - name: max production + expression: production <= nb_units_on * p_max + - name: min production + expression: production >= nb_units_on * p_min + - name: on units variation + expression: nb_units_on = nb_units_on[-1] + nb_starting - nb_stoping + - name: starting time + expression: sum(nb_starting[-d_min_up + 1 .. 0]) <= nb_units_on + - name: stoping time + expression: sum(nb_stoping[-d_min_down + 1 .. 0]) <= nb_units_max - nb_units_on + objective: expec(sum(cost * production)) \ No newline at end of file