Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@rte-france.com>
  • Loading branch information
geofjamg committed Sep 22, 2023
1 parent c91229f commit 8ceaa80
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
13 changes: 13 additions & 0 deletions pypowsybl/voltage_initializer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2023, RTE (http://www.rte-france.com)
# 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

from .impl.voltage_initializer import (
run,
VoltageInitializerParameters,
VoltageInitializerResults,
VoltageInitializerObjective,
VoltageInitializerStatus
)
5 changes: 5 additions & 0 deletions pypowsybl/voltage_initializer/impl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2023, RTE (http://www.rte-france.com)
# 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
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
#
# Copyright (c) 2023, RTE (http://www.rte-france.com)
# 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
#

from typing import Dict, List, Tuple
from pypowsybl import _pypowsybl as _pp
from pypowsybl.network import Network as _Network
from pypowsybl._pypowsybl import (
create_voltage_initializer_params,
voltage_initializer_add_variable_shunt_compensators,
voltage_initializer_add_constant_q_generators,
voltage_initializer_add_variable_two_windings_transformers,
voltage_initializer_add_specific_voltage_limits,
voltage_initializer_add_algorithm_param,
VoltageInitializerObjective,
voltage_initializer_set_objective,
voltage_initializer_set_objective_distance,
VoltageInitializerStatus,
voltage_initializer_get_status,
voltage_initializer_get_indicators,
voltage_initializer_apply_all_modifications,
run_voltage_initializer,
JavaHandle
)
from pypowsybl.network import Network


class VoltageInitializerParameters:
"""
"""

def __init__(self) -> None:
self._handle = _pp.create_voltage_initializer_params()
self._handle = create_voltage_initializer_params()

def add_variable_shunt_compensators(self, shunt_id_list: List[str]) -> None:
'''
Expand All @@ -25,8 +40,7 @@ def add_variable_shunt_compensators(self, shunt_id_list: List[str]) -> None:
shunt_id_list: List of shunt ids.
'''
for id in shunt_id_list:
_pp.voltage_initializer_add_variable_shunt_compensators(
self._handle, id)
voltage_initializer_add_variable_shunt_compensators(self._handle, id)

def add_constant_q_generators(self, generator_id_list: List[str]) -> None:
'''
Expand All @@ -36,7 +50,7 @@ def add_constant_q_generators(self, generator_id_list: List[str]) -> None:
generator_id_list: List of generator ids.
'''
for id in generator_id_list:
_pp.voltage_initializer_add_constant_q_generators(self._handle, id)
voltage_initializer_add_constant_q_generators(self._handle, id)

def add_variable_two_windings_transformers(self, transformer_id_list: List[str]) -> None:
'''
Expand All @@ -46,8 +60,7 @@ def add_variable_two_windings_transformers(self, transformer_id_list: List[str])
transformer_id_list: List of transformer ids.
'''
for id in transformer_id_list:
_pp.voltage_initializer_add_variable_two_windings_transformers(
self._handle, id)
voltage_initializer_add_variable_two_windings_transformers(self._handle, id)

def add_specific_voltage_limits(self, limits: Dict[str, Tuple[float, float]]) -> None:
'''
Expand All @@ -58,8 +71,7 @@ def add_specific_voltage_limits(self, limits: Dict[str, Tuple[float, float]]) ->
limits: A dictionary keys are voltage ids, values are (lower limit, upper limit)
'''
for key in limits:
_pp.voltage_initializer_add_specific_voltage_limits(
key, limits[key][0], self._handle, limits[key][1])
voltage_initializer_add_specific_voltage_limits(key, limits[key][0], self._handle, limits[key][1])

def add_algorithm_param(self, parameters_dict: Dict[str, str]) -> None:
'''
Expand All @@ -69,17 +81,16 @@ def add_algorithm_param(self, parameters_dict: Dict[str, str]) -> None:
parameters_dict: algorithm params are stored as (key, values) like a dict
'''
for key in parameters_dict:
_pp.voltage_initializer_add_algorithm_param(
self._handle, key, parameters_dict[key])
voltage_initializer_add_algorithm_param(self._handle, key, parameters_dict[key])

def set_objective(self, objective: _pp.VoltageInitializerObjective) -> None:
def set_objective(self, objective: VoltageInitializerObjective) -> None:
'''
If you use BETWEEN_HIGH_AND_LOW_VOLTAGE_LIMIT, you also need to call :func:`~VoltageInitializerParameters.set_objective_distance`.
Args:
objective: objective function to set for VoltageInitializer.
'''
_pp.voltage_initializer_set_objective(self._handle, objective)
voltage_initializer_set_objective(self._handle, objective)

def set_objective_distance(self, distance: float) -> None:
'''
Expand All @@ -90,32 +101,29 @@ def set_objective_distance(self, distance: float) -> None:
A 0% objective means the model will target lower voltage limit.
A 100% objective means the model will target upper voltage limit.
'''
_pp.voltage_initializer_set_objective_distance(self._handle, distance)
voltage_initializer_set_objective_distance(self._handle, distance)


class VoltageInitializerResults:
"""
Stores the result of a voltage initializer run.
"""

def __init__(self, result_handle: _pp.JavaHandle) -> None:
def __init__(self, result_handle: JavaHandle) -> None:
self._handle = result_handle
self._status: _pp.VoltageInitializerStatus = _pp.voltage_initializer_get_status(
self._handle)
self._indicators: Dict[str, str] = _pp.voltage_initializer_get_indicators(
self._handle)
self._status: VoltageInitializerStatus = voltage_initializer_get_status(self._handle)
self._indicators: Dict[str, str] = voltage_initializer_get_indicators(self._handle)

def apply_all_modifications(self, network: _Network) -> None:
def apply_all_modifications(self, network: Network) -> None:
'''
Apply all the modifications voltage initializer found to the network.
Args:
network: the network on which the modifications are to be applied.
'''
_pp.voltage_initializer_apply_all_modifications(
self._handle, network._handle)
voltage_initializer_apply_all_modifications(self._handle, network._handle)

def status(self) -> _pp.VoltageInitializerStatus:
def status(self) -> VoltageInitializerStatus:
'''
If the optimisation failed, it can be useful to check the indicators.
Returns:
Expand All @@ -131,7 +139,7 @@ def indicators(self) -> Dict[str, str]:
return self._indicators


def run(network: _Network, params: VoltageInitializerParameters = VoltageInitializerParameters(), debug: bool = False) -> VoltageInitializerResults:
def run(network: Network, params: VoltageInitializerParameters = VoltageInitializerParameters(), debug: bool = False) -> VoltageInitializerResults:
"""
Run voltage initializer on the network with the given params.
Expand All @@ -140,5 +148,5 @@ def run(network: _Network, params: VoltageInitializerParameters = VoltageInitial
params: The parameters use to customize the run
debug: if true, the tmp directory of the VoltageInitializer run will not be erased.
"""
result_handle = _pp.run_voltage_initializer(debug, network._handle, params._handle)
result_handle = run_voltage_initializer(debug, network._handle, params._handle)
return VoltageInitializerResults(result_handle)
16 changes: 7 additions & 9 deletions tests/test_voltage_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import pypowsybl as pp
import pypowsybl.voltage_initializer as voltage_initializer
import pypowsybl.voltage_initializer as va
import pytest
from pypowsybl._pypowsybl import VoltageInitializerObjective

@pytest.fixture(autouse=True)
def set_up():
pp.set_config_read(False)

def test_parameters():
params = voltage_initializer.VoltageInitializerParameters()
params = va.VoltageInitializerParameters()
params.add_variable_shunt_compensators(["shunt1", "shunt2"])
params.add_constant_q_generators(["gen1", "gen2"])
params.add_variable_two_windings_transformers(["twt1", "twt2"])

params.add_algorithm_param({"foo": "bar", "bar": "bar2"})
params.add_specific_voltage_limits({"vl_id": (0.5,1.2)})

params.set_objective(VoltageInitializerObjective.SPECIFIC_VOLTAGE_PROFILE)
params.set_objective(va.VoltageInitializerObjective.SPECIFIC_VOLTAGE_PROFILE)
params.set_objective_distance(1.3)


@pytest.mark.skip(reason="CI doesn't have a Ampl and Knitro runtime.")
def test_runner():
from pypowsybl import network, voltage_initializer
from pypowsybl._pypowsybl import VoltageInitializerObjective
params = voltage_initializer.VoltageInitializerParameters()
from pypowsybl import network, voltage_initializer as va
params = va.VoltageInitializerParameters()
n = network.create_eurostag_tutorial_example1_network()
some_gen_id = n.get_generators().iloc[0].name
params.add_constant_q_generators([some_gen_id])
Expand All @@ -41,9 +39,9 @@ def test_runner():
params.add_variable_two_windings_transformers([some_2wt_id])

params.add_algorithm_param({"foo": "bar", "bar": "bar2"})
params.set_objective(VoltageInitializerObjective.SPECIFIC_VOLTAGE_PROFILE)
params.set_objective(va.VoltageInitializerObjective.SPECIFIC_VOLTAGE_PROFILE)

results = voltage_initializer.run(n, params, True)
results = va.run(n, params, True)
results.apply_all_modifications(n)

print(results.status())
Expand Down

0 comments on commit 8ceaa80

Please sign in to comment.