Skip to content

Commit 8e85f3e

Browse files
author
Denis Bonnand
committed
Clean up code, fix not working tests
Signed-off-by: Denis Bonnand <[email protected]>
1 parent d3760d3 commit 8e85f3e

14 files changed

+121
-85
lines changed

java/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@
8181
<type>pom</type>
8282
<scope>import</scope>
8383
</dependency>
84-
<dependency>
85-
<groupId>com.powsybl</groupId>
86-
<artifactId>powsybl-open-loadflow</artifactId>
87-
<version>2.0.0-CUSTOM</version>
88-
</dependency>
8984
</dependencies>
9085
</dependencyManagement>
9186

pypowsybl/network/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
create_dc_detailed_lcc_bipole_metallic_return_network,
4949
create_dc_detailed_vsc_symmetrical_monopole_network,
5050
create_dc_detailed_vsc_asymmetrical_monopole_network,
51+
create_ac_dc_monopolar_network,
5152
create_ac_dc_bipolar_network,
5253
create_ac_dc_bipolar_network_with_metallic_return
5354
)

pypowsybl/network/impl/network_creation_util.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,43 @@ def create_dc_detailed_vsc_asymmetrical_monopole_network(allow_variant_multi_thr
256256
"""
257257
return _create_network('dc_detailed_vsc_asymmetrical_monopole', '', allow_variant_multi_thread_access)
258258

259+
def create_ac_dc_monopolar_network(allow_variant_multi_thread_access: bool = False) -> Network:
260+
"""
261+
Create an instance of an AC DC monopolar network case, for AC DC loadflow
262+
263+
Returns:
264+
a new instance of an AC DC monopolar network case
265+
"""
266+
n = pp.network.create_empty()
267+
n.create_dc_nodes(id='dn3p', nominal_v=400)
268+
n.create_dc_nodes(id='dn3n', nominal_v=400)
269+
n.create_dc_nodes(id='dn4p', nominal_v=400)
270+
n.create_dc_nodes(id='dn4n', nominal_v=400)
271+
n.create_dc_grounds(id='dg3', r=0.0, dc_node_id='dn3n')
272+
n.create_dc_grounds(id='dg4', r=0.0, dc_node_id='dn4n')
273+
n.create_voltage_levels(id='vl1', topology_kind='BUS_BREAKER', nominal_v=400)
274+
n.create_buses(id='b1', voltage_level_id='vl1')
275+
n.create_generators(id='g1', voltage_level_id='vl1', bus_id='b1', target_p=102.56, min_p=0, max_p=500,
276+
target_v=390,
277+
voltage_regulator_on=True)
278+
n.create_voltage_levels(id='vl2', topology_kind='BUS_BREAKER', nominal_v=400)
279+
n.create_buses(id='b2', voltage_level_id='vl2')
280+
n.create_loads(id='ld2', voltage_level_id='vl2', bus_id='b2', p0=20, q0=10)
281+
n.create_voltage_levels(id='vl5', topology_kind='BUS_BREAKER', nominal_v=400)
282+
n.create_buses(id='b5', voltage_level_id='vl5')
283+
n.create_loads(id='ld5', voltage_level_id='vl5', bus_id='b5', p0=50, q0=10)
284+
n.create_dc_lines(id='dl34p', dc_node1_id='dn3p', dc_node2_id='dn4p', r=0.1)
285+
n.create_lines(id='l12', voltage_level1_id='vl1', bus1_id='b1', voltage_level2_id='vl2', bus2_id='b2', r=1, x=3)
286+
n.create_lines(id='l25', voltage_level1_id='vl2', bus1_id='b2', voltage_level2_id='vl5', bus2_id='b5', r=1, x=3)
287+
n.create_voltage_source_converters(id='conv23', voltage_level_id='vl2', dc_node1_id='dn3p', dc_node2_id='dn3n',
288+
bus1_id='b2', voltage_regulator_on=0, control_mode='P_PCC', target_p=-50.0,
289+
target_q=0.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
290+
dc_connected1=1, dc_connected2=1)
291+
n.create_voltage_source_converters(id='conv45', voltage_level_id='vl5', dc_node1_id='dn4p', dc_node2_id='dn4n',
292+
bus1_id='b5', voltage_regulator_on=0, control_mode='V_DC', target_v_dc=400.0,
293+
target_q=0.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
294+
dc_connected1=1, dc_connected2=1)
295+
return n
259296

260297
def create_ac_dc_bipolar_network(allow_variant_multi_thread_access: bool = False) -> Network:
261298
"""
@@ -294,11 +331,11 @@ def create_ac_dc_bipolar_network(allow_variant_multi_thread_access: bool = False
294331
n.create_lines(id='l25', voltage_level1_id='vl2', bus1_id='b2', voltage_level2_id='vl5', bus2_id='b5', r=1, x=3)
295332
n.create_voltage_source_converters(id='conv23', voltage_level_id='vl2', dc_node1_id='dn3p', dc_node2_id='dn3n',
296333
bus1_id='b2', voltage_regulator_on=0, control_mode='P_PCC', target_p=-50.0,
297-
target_q=0.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
298-
dc_connected1=1, dc_connected2=1)
334+
target_q=0.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
335+
dc_connected1=1, dc_connected2=1)
299336
n.create_voltage_source_converters(id='conv45', voltage_level_id='vl5', dc_node1_id='dn4p', dc_node2_id='dn4n',
300337
bus1_id='b5', voltage_regulator_on=0, control_mode='V_DC', target_q=0.0,
301-
target_v_dc=400.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
338+
target_v_dc=400.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
302339
dc_connected1=1, dc_connected2=1)
303340
return n
304341

@@ -337,19 +374,19 @@ def create_ac_dc_bipolar_network_with_metallic_return(allow_variant_multi_thread
337374
n.create_lines(id='l25', voltage_level1_id='vl2', bus1_id='b2', voltage_level2_id='vl5', bus2_id='b5', r=1, x=3)
338375
n.create_voltage_source_converters(id='conv23p', voltage_level_id='vl2', dc_node1_id='dn3p', dc_node2_id='dn3r',
339376
bus1_id='b2', voltage_regulator_on=0, control_mode='P_PCC', target_p=-25.0,
340-
target_q=0.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
377+
target_q=0.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
341378
dc_connected1=1, dc_connected2=1)
342379
n.create_voltage_source_converters(id='conv23n', voltage_level_id='vl2', dc_node1_id='dn3n', dc_node2_id='dn3r',
343380
bus1_id='b2', voltage_regulator_on=0, control_mode='P_PCC', target_p=-25.0,
344-
target_q=0.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
381+
target_q=0.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
345382
dc_connected1=1, dc_connected2=1)
346383
n.create_voltage_source_converters(id='conv45p', voltage_level_id='vl5', dc_node1_id='dn4p', dc_node2_id='dn4r',
347384
bus1_id='b5', voltage_regulator_on=0, control_mode='V_DC', target_q=0.0,
348-
target_v_dc=200.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
385+
target_v_dc=200.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
349386
dc_connected1=1, dc_connected2=1)
350387
n.create_voltage_source_converters(id='conv45n', voltage_level_id='vl5', dc_node1_id='dn4n', dc_node2_id='dn4r',
351388
bus1_id='b5', voltage_regulator_on=0, control_mode='V_DC', target_q=0.0,
352-
target_v_dc=-200.0, idle_loss=0.0, switching_loss=0.0, resistive_loss=0.0,
389+
target_v_dc=-200.0, idle_loss=0.5, switching_loss=1.0, resistive_loss=0.2,
353390
dc_connected1=1, dc_connected2=1)
354391

355392
return n

pypowsybl/opf/impl/bounds/dc_line_current_bounds.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
17
import logging
28

39
from pyoptinterface import ipopt
@@ -15,7 +21,8 @@ class DcLineCurrentBounds(VariableBounds):
1521
def add(self, parameters: ModelParameters, network_cache: NetworkCache,
1622
variable_context: VariableContext, model: ipopt.Model):
1723
for dc_line_num, row in enumerate(network_cache.dc_lines.itertuples()):
18-
i_bounds = Bounds(-10, 10)
24+
#TODO add current limits in DC line core modelization
25+
i_bounds = Bounds(-2, 2)
1926
logger.log(TRACE_LEVEL, f"Add current bounds {i_bounds} to dc line '{row.Index}' (num={dc_line_num})'")
2027
dc_line_index = variable_context.dc_line_num_2_index[dc_line_num]
2128
model.set_variable_bounds(variable_context.closed_dc_line_i1_vars[dc_line_index],

pypowsybl/opf/impl/bounds/dc_node_voltage_bounds.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
17
import logging
28

39
from pyoptinterface import ipopt
@@ -15,9 +21,9 @@ class DcNodeVoltageBounds(VariableBounds):
1521
def add(self, parameters: ModelParameters, network_cache: NetworkCache,
1622
variable_context: VariableContext, model: ipopt.Model):
1723
for dc_node_num, row in enumerate(network_cache.dc_nodes.itertuples()):
18-
#TODO add voltage limits in DC NODE core modelization
19-
low_voltage_limit = -1
20-
high_voltage_limit = 1
24+
#TODO add voltage limits in DC node core modelization
25+
low_voltage_limit = -2.0
26+
high_voltage_limit = 2.0
2127
v_bounds = Bounds(low_voltage_limit, high_voltage_limit)
2228
logger.log(TRACE_LEVEL, f"Add voltage magnitude bounds {v_bounds} to dc_node '{row.Index}' (num={dc_node_num})'")
2329
model.set_variable_bounds(variable_context.v_dc_vars[dc_node_num],

pypowsybl/opf/impl/bounds/voltage_source_converter_power_bounds.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
17
import logging
28

39
from pyoptinterface import ipopt

pypowsybl/opf/impl/constraints/dc_current_balance_constraints.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
17
import pyoptinterface as poi
28
from pyoptinterface import ipopt
39

@@ -14,7 +20,6 @@ def add(self, parameters: ModelParameters, network_cache: NetworkCache,
1420
for dc_node_expr in self.create_dc_node_expr_list(network_cache, variable_context):
1521
model.add_linear_constraint(dc_node_expr, poi.Eq, 0.0)
1622

17-
1823
class DcNodesBalance:
1924
def __init__(self, dc_node_count: int):
2025
self.i_in = [[] for _ in range(dc_node_count)]
@@ -28,7 +33,6 @@ def _to_expr(dc_node_i_in: list, dc_node_i_out: list) -> list[poi.ExprBuilder]:
2833
def to_expr(self) -> list[poi.ExprBuilder]:
2934
return self._to_expr(self.i_in, self.i_out)
3035

31-
3236
@classmethod
3337
def create_dc_node_expr_list(cls, network_cache, variable_context):
3438
dc_node_grounded_ids = [row.dc_node_id for row in network_cache.dc_grounds.itertuples(index=False)]
@@ -46,15 +50,13 @@ def create_dc_node_expr_list(cls, network_cache, variable_context):
4650

4751
# voltage source converters
4852
for conv_num, row in enumerate(network_cache.voltage_source_converters.itertuples(index=False)):
49-
dc_node1_id = row.dc_node1_id
50-
dc_node2_id = row.dc_node2_id
51-
if row.dc_connected1:
53+
if row.dc_node1_id not in dc_node_grounded_ids:
5254
conv_index = variable_context.conv_num_2_index[conv_num]
53-
dc_node1_num = network_cache.dc_nodes.index.get_loc(dc_node1_id)
55+
dc_node1_num = network_cache.dc_nodes.index.get_loc(row.dc_node1_id)
5456
dc_nodes_balance.i_in[dc_node1_num].append(variable_context.conv_i_vars[conv_index])
55-
if row.dc_connected2:
57+
if row.dc_node2_id not in dc_node_grounded_ids:
5658
conv_index = variable_context.conv_num_2_index[conv_num]
57-
dc_node2_num = network_cache.dc_nodes.index.get_loc(dc_node2_id)
59+
dc_node2_num = network_cache.dc_nodes.index.get_loc(row.dc_node2_id)
5860
dc_nodes_balance.i_in[dc_node2_num].append(-variable_context.conv_i_vars[conv_index])
5961

60-
return dc_nodes_balance.to_expr()
62+
return dc_nodes_balance.to_expr()

pypowsybl/opf/impl/constraints/dc_ground_constraints.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from math import hypot, atan2
2-
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
37
from pyoptinterface import ipopt, nl
48

59
from pypowsybl.opf.impl.model.constraints import Constraints

pypowsybl/opf/impl/constraints/dc_line_constraints.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from math import hypot, atan2
2-
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
37
from pyoptinterface import ipopt, nl
48

59
from pypowsybl.opf.impl.model.constraints import Constraints

pypowsybl/opf/impl/constraints/voltage_source_converter_constraints.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# Copyright (c) 2025, SuperGrid Institute (http://www.supergrid-institute.com)
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
# SPDX-License-Identifier: MPL-2.0
6+
#
17
from pyoptinterface import ipopt, nl
28

39
from pypowsybl.opf.impl.model.constraints import Constraints
410
from pypowsybl.opf.impl.model.model_parameters import ModelParameters
511
from pypowsybl.opf.impl.model.network_cache import NetworkCache
612
from pypowsybl.opf.impl.model.variable_context import VariableContext
7-
import math
813

914
class VoltageSourceConverterConstraints(Constraints):
1015
def add(self, parameters: ModelParameters, network_cache: NetworkCache, variable_context: VariableContext,
@@ -17,7 +22,6 @@ def add(self, parameters: ModelParameters, network_cache: NetworkCache, variable
1722
converter_row.target_v_dc, converter_row.target_v_ac)
1823
idle_loss, switching_loss, resistive_loss = (converter_row.idle_loss, converter_row.switching_loss,
1924
converter_row.resistive_loss)
20-
dc_connected1, dc_connected2 = converter_row.dc_connected1, converter_row.dc_connected2
2125

2226
dc_node1_num = network_cache.dc_nodes.index.get_loc(dc_node1_id)
2327
dc_node2_num = network_cache.dc_nodes.index.get_loc(dc_node2_id)
@@ -35,9 +39,6 @@ def add(self, parameters: ModelParameters, network_cache: NetworkCache, variable
3539
model.add_nl_constraint(p_ac_eq == 0.0)
3640

3741
# if control_mode == "V_DC", we let the opf determine the value of U
38-
# if control_mode == "V_DC":
39-
# v_dc_eq = v1_var - v2_var - target_v_dc
40-
# model.add_nl_constraint(v_dc_eq == 0.0)
4142

4243
if voltage_regulator_on:
4344
bus1_v_eq = bus1_v_var - target_v_ac
@@ -50,13 +51,9 @@ def add(self, parameters: ModelParameters, network_cache: NetworkCache, variable
5051
i_ac_var = nl.sqrt(nl.pow(conv_p_var,2) + nl.pow(conv_q_var,2))/1000.0
5152
p_loss = idle_loss + switching_loss*i_ac_var + resistive_loss*nl.pow(i_ac_var,2)
5253

53-
u_var = 0
54-
if dc_connected1:
55-
u_var+= v1_var
56-
if dc_connected2:
57-
u_var-= v2_var
5854
# P_dc = -P_ac - P_loss because we consider that the power P_dc injected in DC is positive,
5955
# and the power P_ac flowing out of AC is negative
60-
conv_p_dc_eq = (-conv_p_var - p_loss) - conv_i_var * (u_var + 0.001)
56+
# FIXME : I needed to add 0.01 because at the initialization v1_var = v2_var and the opf never converge
57+
conv_p_dc_eq = (-conv_p_var - p_loss) - conv_i_var * (v1_var - v2_var + 0.01)
6158

6259
model.add_nl_constraint(conv_p_dc_eq == 0.0)

0 commit comments

Comments
 (0)