Skip to content

Commit

Permalink
Fix LoadsMerger thanks to new unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup committed Feb 17, 2023
1 parent 9878784 commit 2eb4f44
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
44 changes: 30 additions & 14 deletions commons/src/main/java/com/powsybl/dynawo/commons/LoadsMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.xml.NetworkXml;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Dimitri Baudrier <dimitri.baudrier at rte-france.com>
*/
Expand All @@ -16,27 +19,31 @@ private LoadsMerger() {

public static Network mergeLoads(Network network) throws PowsyblException {
Network mergedLoadsNetwork = NetworkXml.copy(network);
for (Bus bus : mergedLoadsNetwork.getBusBreakerView().getBuses()) {
if (bus.getLoadStream().count() > 1) {
mergeLoads(bus);
}

List<LoadsMerging> loadsMerging = mergedLoadsNetwork.getBusBreakerView().getBusStream()
.filter(bus -> bus.getLoadStream().count() > 1)
.map(LoadsMerger::mergeLoads)
.collect(Collectors.toList());

for (LoadsMerging merging : loadsMerging) {
merging.loadsToMerge.forEach(Connectable::remove);
merging.loadAdder.setP0(merging.busState.p0);
merging.loadAdder.setQ0(merging.busState.q0);
Load load = merging.loadAdder.add();
load.getTerminal().setP(merging.busState.p);
load.getTerminal().setQ(merging.busState.q);
}
return mergedLoadsNetwork;
}

private static void mergeLoads(Bus bus) {
private static LoadsMerging mergeLoads(Bus bus) {
TopologyKind topologyKind = bus.getVoltageLevel().getTopologyKind();

LoadAdder loadAdder = bus.getVoltageLevel().newLoad();
loadAdder.setId(MERGE_LOAD_PREFIX_ID + bus.getId());

loadAdder.setLoadType(LoadType.UNDEFINED);

BusState busState = getBusState(bus);

loadAdder.setP0(busState.p0);
loadAdder.setQ0(busState.q0);

if (TopologyKind.BUS_BREAKER.equals(topologyKind)) {
loadAdder.setBus(bus.getId());
loadAdder.setConnectableBus(bus.getId());
Expand All @@ -49,10 +56,7 @@ private static void mergeLoads(Bus bus) {
loadAdder.setNode(node);
}

loadsToMerge.forEach(Connectable::remove);
Load load = loadAdder.add();
load.getTerminal().setP(busState.p);
load.getTerminal().setQ(busState.q);
return new LoadsMerging(loadAdder, loadsToMerge, getBusState(bus));
}

static final class BusState {
Expand Down Expand Up @@ -90,4 +94,16 @@ static BusState getBusState(Bus bus) {
}
return new BusState(p, q, p0, q0);
}

private static class LoadsMerging {
private final BusState busState;
private final LoadAdder loadAdder;
private final Iterable<Load> loadsToMerge;

public LoadsMerging(LoadAdder loadAdder, Iterable<Load> loadsToMerge, BusState busState) {
this.loadAdder = loadAdder;
this.loadsToMerge = loadsToMerge;
this.busState = busState;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.powsybl.dynawo.commons;

import com.powsybl.commons.test.AbstractConverterTest;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.xml.NetworkXml;
import com.powsybl.sld.SingleLineDiagram;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static com.powsybl.commons.test.ComparisonUtils.compareTxt;
import static org.junit.Assert.assertNotNull;

public class LoadsMergerTest extends AbstractConverterTest {

@Test
public void mergeLoads() throws IOException {
Network network = createTestNetwork();
Network expectedIidm = Network.read("mergedLoads.xiidm", getClass().getResourceAsStream("/mergedLoads.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
}

private static Network createTestNetwork() {
Network network = Network.create("test", "test");
Substation s = network.newSubstation().setId("substation").add();

VoltageLevel vl1 = s.newVoltageLevel().setId("vl1").setNominalV(250).setTopologyKind(TopologyKind.NODE_BREAKER).add();
vl1.getNodeBreakerView().newBusbarSection().setId("Busbar1").setNode(0).add();
vl1.getNodeBreakerView().newBusbarSection().setId("Busbar2").setNode(4).add();
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(1).setId("d1").add();
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(2).setId("d2").add();
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(3).setId("d3").add();
vl1.getNodeBreakerView().newBreaker().setNode1(0).setNode2(4).setId("coupler").setOpen(true).add();
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(5).setId("d4").add();
vl1.getNodeBreakerView().newDisconnector().setNode1(4).setNode2(6).setId("d5").add();
vl1.newLoad().setId("load1").setP0(10.0).setQ0(5.0).setNode(1).add();
vl1.newLoad().setId("load2").setP0(12.0).setQ0(1.0).setNode(2).add();
vl1.newLoad().setId("load3").setP0(22.0).setQ0(3.0).setNode(3).add();
vl1.newLoad().setId("load4").setP0(-2.0).setQ0(-1.0).setNode(6).add();

VoltageLevel vl2 = s.newVoltageLevel().setId("vl2").setNominalV(250).setTopologyKind(TopologyKind.BUS_BREAKER).add();
Bus b1 = vl2.getBusBreakerView().newBus().setId("b1").add();
Bus b2 = vl2.getBusBreakerView().newBus().setId("b2").add();
Bus b3 = vl2.getBusBreakerView().newBus().setId("b3").add();
vl2.getBusBreakerView().newSwitch().setId("c1").setBus1(b1.getId()).setBus2(b2.getId()).add();
vl2.getBusBreakerView().newSwitch().setId("c2").setBus1(b2.getId()).setBus2(b3.getId()).add();
vl2.newGenerator().setId("g1").setBus(b1.getId()).setTargetP(101).setTargetV(390).setMinP(0).setMaxP(150).setVoltageRegulatorOn(true).add();
vl2.newLoad().setId("load5").setP0(37.0).setQ0(1.0).setBus(b2.getId()).add();
vl2.newLoad().setId("load6").setP0(13.0).setQ0(6.0).setBus(b2.getId()).add();
vl2.newLoad().setId("load7").setP0(7.0).setQ0(-4.0).setBus(b3.getId()).add();

network.newLine().setId("l1").setVoltageLevel1(vl1.getId()).setNode1(5).setVoltageLevel2(vl2.getId()).setBus2(b1.getId())
.setR(1).setX(3).setG1(0).setG2(0).setB1(0).setB2(0).add();
return network;
}

private void compare(Network expected, Network actual) throws IOException {
Path pexpected = tmpDir.resolve("expected.xiidm");
assertNotNull(pexpected);
Path pactual = tmpDir.resolve("actual.xiidm");
assertNotNull(pactual);
NetworkXml.write(expected, pexpected);
actual.setCaseDate(expected.getCaseDate());
NetworkXml.write(actual, pactual);
compareTxt(Files.newInputStream(pexpected), Files.newInputStream(pactual));
}
}
34 changes: 34 additions & 0 deletions commons/src/test/resources/mergedLoads.xiidm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_9" id="test" caseDate="2023-02-17T05:41:11.194+01:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
<iidm:substation id="substation">
<iidm:voltageLevel id="vl1" nominalV="250.0" topologyKind="NODE_BREAKER">
<iidm:nodeBreakerTopology>
<iidm:busbarSection id="Busbar1" node="0"/>
<iidm:busbarSection id="Busbar2" node="4"/>
<iidm:switch id="d1" kind="DISCONNECTOR" retained="false" open="false" node1="0" node2="1"/>
<iidm:switch id="d2" kind="DISCONNECTOR" retained="false" open="false" node1="0" node2="2"/>
<iidm:switch id="d3" kind="DISCONNECTOR" retained="false" open="false" node1="0" node2="3"/>
<iidm:switch id="coupler" kind="BREAKER" retained="false" open="true" node1="0" node2="4"/>
<iidm:switch id="d4" kind="DISCONNECTOR" retained="false" open="false" node1="0" node2="5"/>
<iidm:switch id="d5" kind="DISCONNECTOR" retained="false" open="false" node1="4" node2="6"/>
</iidm:nodeBreakerTopology>
<iidm:load id="merged_load_.vl1_0" loadType="UNDEFINED" p0="44.0" q0="9.0" node="1"/>
<iidm:load id="load4" loadType="UNDEFINED" p0="-2.0" q0="-1.0" node="6"/>
</iidm:voltageLevel>
<iidm:voltageLevel id="vl2" nominalV="250.0" topologyKind="BUS_BREAKER">
<iidm:busBreakerTopology>
<iidm:bus id="b1"/>
<iidm:bus id="b2"/>
<iidm:bus id="b3"/>
<iidm:switch id="c1" kind="BREAKER" retained="true" open="false" bus1="b1" bus2="b2"/>
<iidm:switch id="c2" kind="BREAKER" retained="true" open="false" bus1="b2" bus2="b3"/>
</iidm:busBreakerTopology>
<iidm:generator id="g1" energySource="OTHER" minP="0.0" maxP="150.0" voltageRegulatorOn="true" targetP="101.0" targetV="390.0" bus="b1" connectableBus="b1">
<iidm:minMaxReactiveLimits minQ="-1.7976931348623157E308" maxQ="1.7976931348623157E308"/>
</iidm:generator>
<iidm:load id="merged_load_.b2" loadType="UNDEFINED" p0="50.0" q0="7.0" bus="b2" connectableBus="b2"/>
<iidm:load id="load7" loadType="UNDEFINED" p0="7.0" q0="-4.0" bus="b3" connectableBus="b3"/>
</iidm:voltageLevel>
</iidm:substation>
<iidm:line id="l1" r="1.0" x="3.0" g1="0.0" b1="0.0" g2="0.0" b2="0.0" node1="5" voltageLevelId1="vl1" bus2="b1" connectableBus2="b1" voltageLevelId2="vl2"/>
</iidm:network>

0 comments on commit 2eb4f44

Please sign in to comment.