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 3be0ad4
Showing 1 changed file with 30 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;
}
}
}

0 comments on commit 3be0ad4

Please sign in to comment.