Skip to content

Commit

Permalink
Adder by copy for lines (#3208)
Browse files Browse the repository at this point in the history
* successful copy of lines element
* add: details on operational limits groups and extensions duplication while copy of line
* update : lines adder impl updated by adding operational limit groups, and tests
* update : added tests
* update : added method for line adder impl in branchutil
* update : moved utility function to fill line adder to lineAdder
* update : moved the fill adder method call to lineAdderImpl constructor
* update : simplified line adder implementation

Signed-off-by: Naledi EL CHEIKH <[email protected]>
  • Loading branch information
nao1345678 authored Jan 16, 2025
1 parent 8aa0fdf commit 1c6d37e
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/
package com.powsybl.ampl.converter.util;

import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.SlackTerminal;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
* </tr>
* </tbody>
* </table>
*
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public interface Branch<I extends Branch<I>> extends Identifiable<I> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
*/
public interface LineAdder extends BranchAdder<Line, LineAdder> {

static LineAdder fillLineAdder(LineAdder adder, Line line) {
return adder.setR(line.getR())
.setX(line.getX())
.setG1(line.getG1())
.setG2(line.getG2())
.setB1(line.getB1())
.setB2(line.getB2())
.setVoltageLevel1(line.getTerminal1().getVoltageLevel().getId())
.setVoltageLevel2(line.getTerminal2().getVoltageLevel().getId());
}

LineAdder setR(double r);

LineAdder setX(double x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@ default VoltageLevelAdder newVoltageLevel() {
*/
LineAdder newLine();

/**
* Get a builder to create a new AC line by copying an existing one.
* @return a builder to create a new line
*/
LineAdder newLine(Line line);

/**
* Get all AC lines.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package com.powsybl.iidm.network;
import java.util.Optional;

import static com.powsybl.iidm.network.util.LoadingLimitsUtil.initializeFromLoadingLimits;

/**
* @author Pauline Jean-Marie {@literal <pauline.jean-marie at artelys.com>}
*/
Expand All @@ -28,6 +30,21 @@ public interface OperationalLimitsGroup {

ApparentPowerLimitsAdder newApparentPowerLimits();

default CurrentLimitsAdder newCurrentLimits(CurrentLimits currentLimits) {
CurrentLimitsAdder currentLimitsAdder = newCurrentLimits();
return initializeFromLoadingLimits(currentLimitsAdder, currentLimits);
}

default ActivePowerLimitsAdder newActivePowerLimits(ActivePowerLimits activePowerLimits) {
ActivePowerLimitsAdder activePowerLimitsAdder = newActivePowerLimits();
return initializeFromLoadingLimits(activePowerLimitsAdder, activePowerLimits);
}

default ApparentPowerLimitsAdder newApparentPowerLimits(ApparentPowerLimits apparentPowerLimits) {
ApparentPowerLimitsAdder apparentPowerLimitsAdder = newApparentPowerLimits();
return initializeFromLoadingLimits(apparentPowerLimitsAdder, apparentPowerLimits);
}

void removeCurrentLimits();

void removeActivePowerLimits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/
package com.powsybl.iidm.network.util;

import com.powsybl.iidm.network.LoadingLimits;
import com.powsybl.iidm.network.LoadingLimitsAdder;
import com.powsybl.iidm.network.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;

Expand All @@ -22,6 +23,8 @@ public final class LoadingLimitsUtil {
private LoadingLimitsUtil() {
}

private static final Logger LOGGER = LoggerFactory.getLogger(LoadingLimitsUtil.class);

/**
* Interface for objects used to report the performed operation on limits when fixed by
* {@link #fixMissingPermanentLimit(LoadingLimitsAdder, double, String, LimitFixLogger)}.
Expand Down Expand Up @@ -96,6 +99,10 @@ public static <L extends LoadingLimits, A extends LoadingLimitsAdder<L, A>> void
* @param limits the limits to copy
*/
public static <L extends LoadingLimits, A extends LoadingLimitsAdder<L, A>> A initializeFromLoadingLimits(A adder, L limits) {
if (limits == null) {
LOGGER.warn("Created adder is empty");
return adder;
}
adder.setPermanentLimit(limits.getPermanentLimit());
limits.getTemporaryLimits().forEach(limit ->
adder.beginTemporaryLimit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.LineAdder;
import com.powsybl.iidm.network.ValidationException;
import com.powsybl.iidm.network.ValidationUtil;
import com.powsybl.iidm.network.*;
import com.powsybl.commons.ref.Ref;

/**
Expand All @@ -20,6 +18,7 @@ class LineAdderImpl extends AbstractBranchAdder<LineAdderImpl> implements LineAd

private final NetworkImpl network;
private final String subnetwork;
private final Line copiedLine;

private double r = Double.NaN;

Expand All @@ -36,6 +35,14 @@ class LineAdderImpl extends AbstractBranchAdder<LineAdderImpl> implements LineAd
LineAdderImpl(NetworkImpl network, String subnetwork) {
this.network = network;
this.subnetwork = subnetwork;
this.copiedLine = null;
}

LineAdderImpl(NetworkImpl network, String subnetwork, Line copiedLine) {
this.network = network;
this.subnetwork = subnetwork;
this.copiedLine = copiedLine;
LineAdder.fillLineAdder(this, copiedLine);
}

@Override
Expand Down Expand Up @@ -110,6 +117,25 @@ public LineImpl add() {
line.addTerminal(terminal1);
line.addTerminal(terminal2);

if (copiedLine != null) {
copiedLine.getOperationalLimitsGroups1().forEach(groupToCopy -> {
OperationalLimitsGroup copy1 = line.newOperationalLimitsGroup1(groupToCopy.getId());
groupToCopy.getCurrentLimits().ifPresent(limit -> copy1.newCurrentLimits(limit).add());
groupToCopy.getActivePowerLimits().ifPresent(limit -> copy1.newActivePowerLimits(limit).add());
groupToCopy.getApparentPowerLimits().ifPresent(limit -> copy1.newApparentPowerLimits(limit).add());
});

copiedLine.getOperationalLimitsGroups2().forEach(groupToCopy -> {
OperationalLimitsGroup copy2 = line.newOperationalLimitsGroup2(groupToCopy.getId());
groupToCopy.getCurrentLimits().ifPresent(limit -> copy2.newCurrentLimits(limit).add());
groupToCopy.getActivePowerLimits().ifPresent(limit -> copy2.newActivePowerLimits(limit).add());
groupToCopy.getApparentPowerLimits().ifPresent(limit -> copy2.newApparentPowerLimits(limit).add());
});

copiedLine.getSelectedOperationalLimitsGroupId1().ifPresent(line::setSelectedOperationalLimitsGroup1);
copiedLine.getSelectedOperationalLimitsGroupId2().ifPresent(line::setSelectedOperationalLimitsGroup2);
}

// check that the line is attachable on both side
voltageLevel1.getTopologyModel().attach(terminal1, true);
voltageLevel2.getTopologyModel().attach(terminal2, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,22 @@ public VoltageLevelExt getVoltageLevel(String id) {

@Override
public LineAdderImpl newLine() {
return newLine(null);
return newLine((String) null);
}

LineAdderImpl newLine(String subnetwork) {
return new LineAdderImpl(this, subnetwork);
}

@Override
public LineAdderImpl newLine(Line copiedLine) {
return newLine(null, copiedLine);
}

LineAdderImpl newLine(String subnetwork, Line copiedLine) {
return new LineAdderImpl(this, subnetwork, copiedLine);
}

@Override
public Iterable<Line> getLines() {
return Collections.unmodifiableCollection(index.getAll(LineImpl.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public LineAdder newLine() {
return getNetwork().newLine(id);
}

@Override
public LineAdder newLine(Line line) {
return getNetwork().newLine(id, line);
}

@Override
public Iterable<Line> getLines() {
return getLineStream().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ public void testAdderByCopy() {
adder.add();

assertTrue(areLimitsIdentical(limits1, limits2));
assertFalse(line.newCurrentLimits1(null).hasTemporaryLimits());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mockito.Mockito;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -41,6 +42,22 @@ public abstract class AbstractLineTest {
private VoltageLevel voltageLevelA;
private VoltageLevel voltageLevelB;

public static boolean areLinesIdentical(Line line1, Line line2) {
boolean areIdentical = false;

if (line1 != null && line2 != null) {
areIdentical = line1.getR() == line2.getR()
&& line1.getX() == line2.getX()
&& line1.getG1() == line2.getG1()
&& line1.getG2() == line2.getG2()
&& line1.getB1() == line2.getB1()
&& line1.getB2() == line2.getB2()
&& Objects.equals(line1.getTerminal1().getVoltageLevel().getId(), line2.getTerminal1().getVoltageLevel().getId())
&& Objects.equals(line1.getTerminal2().getVoltageLevel().getId(), line2.getTerminal2().getVoltageLevel().getId());
}
return areIdentical;
}

@BeforeEach
public void setUp() {
network = NoEquipmentNetworkFactory.create();
Expand Down Expand Up @@ -181,6 +198,81 @@ public void testDefaultLine() {
assertSame(voltageLevelB, acLine.getTerminal2().getVoltageLevel());
}

@Test
public void testLineCopier() {
// First limit normally created
LineAdder acLineAdder1 = network.newLine()
.setId("line1")
.setName(LINE_NAME)
.setR(1.0)
.setX(2.0)
.setG1(3.0)
.setG2(3.5)
.setB1(4.0)
.setB2(4.5)
.setVoltageLevel1("vl1")
.setVoltageLevel2("vl2")
.setBus1("busA")
.setBus2("busB")
.setConnectableBus1("busA")
.setConnectableBus2("busB");
acLineAdder1.add();
Line acLine1 = network.getLine("line1");
// Group and limits creation 1
acLine1.newOperationalLimitsGroup1("group1").newCurrentLimits().setPermanentLimit(220.0).add();
acLine1.setSelectedOperationalLimitsGroup1("group1");
Optional<CurrentLimits> optionalLimits1 = acLine1.getCurrentLimits1();
assertTrue(optionalLimits1.isPresent());
CurrentLimits limits1 = optionalLimits1.get();
assertNotNull(limits1);

acLine1.getOperationalLimitsGroup1("group1").get().newActivePowerLimits().setPermanentLimit(220.0).add();
acLine1.setSelectedOperationalLimitsGroup1("group1");
Optional<ActivePowerLimits> optionalActivePowerLimits1 = acLine1.getActivePowerLimits1();
assertTrue(optionalActivePowerLimits1.isPresent());
ActivePowerLimits activePowerLimits1 = optionalActivePowerLimits1.get();
assertNotNull(activePowerLimits1);

acLine1.getOperationalLimitsGroup1("group1").get().newApparentPowerLimits().setPermanentLimit(220.0).add();
acLine1.setSelectedOperationalLimitsGroup1("group1");
Optional<ApparentPowerLimits> optionalApparentPowerLimits1 = acLine1.getApparentPowerLimits1();
assertTrue(optionalApparentPowerLimits1.isPresent());
ApparentPowerLimits apparentPowerLimits1 = optionalApparentPowerLimits1.get();
assertNotNull(apparentPowerLimits1);

// Group and limit creation 2
acLine1.newOperationalLimitsGroup2("group2").newCurrentLimits().setPermanentLimit(80.0).add();
acLine1.setSelectedOperationalLimitsGroup2("group2");
Optional<CurrentLimits> optionalLimits2 = acLine1.getCurrentLimits2();
assertTrue(optionalLimits2.isPresent());
CurrentLimits limits2 = optionalLimits2.get();
assertNotNull(limits2);

// Second limit created by copy
LineAdder acLineAdder2 = network.newLine(acLine1);
acLineAdder2
.setId("line2")
.setName(LINE_NAME)
.setBus1("busA")
.setBus2("busB")
.setConnectableBus1("busA")
.setConnectableBus2("busB");
acLineAdder2.add();
Line acLine2 = network.getLine("line2");
// Limits check to set up test
Optional<CurrentLimits> optionalLimits3 = acLine2.getCurrentLimits1();
assertTrue(optionalLimits3.isPresent());
CurrentLimits limits3 = optionalLimits3.get();

// Tests
assertNotNull(acLine2);
assertTrue(areLinesIdentical(acLine1, acLine2));
assertEquals(limits1.getPermanentLimit(), limits3.getPermanentLimit());
assertNotNull(acLine2.getOperationalLimitsGroup2("group2"));
assertEquals(acLine1.getSelectedOperationalLimitsGroupId2(), acLine2.getSelectedOperationalLimitsGroupId2());

}

@Test
public void testMove1Bb() {
Line line = createLineBetweenVoltageAB("line", LINE_NAME, 1.0, 2.0, 3.0, 3.5, 4.0, 4.5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.stream.Stream;
import static com.powsybl.iidm.network.tck.AbstractLineTest.areLinesIdentical;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -138,6 +139,17 @@ public void testLineCreation() {
// On root network, voltage levels in root network and subnetwork2
Line l4 = addLine(network, "l4", "vl0_0", "vl2_0");

// New lines creation using the copying adder
Line l5 = subnetwork2.newLine(l2)
.setId("l5")
.setBus1(getBusId("vl2_0"))
.setBus2(getBusId("vl2_1"))
.add();

// Tests if copy proceeds
assertNotNull(l5);
assertTrue(areLinesIdentical(l2, l5));

// Try to detach all. Some elements prevent it.
assertFalse(subnetwork1.isDetachable());
assertFalse(subnetwork2.isDetachable());
Expand All @@ -161,7 +173,7 @@ public void testLineCreation() {
// - Check Lines
assertEquals(1, network.getLineCount());
assertEquals(1, n1.getLineCount());
assertEquals(1, n2.getLineCount());
assertEquals(2, n2.getLineCount());
assertNetworks(network, network, network.getLine("l0"));
assertNetworks(n1, n1, n1.getLine("l1"));
assertNetworks(n2, n2, n2.getLine("l2"));
Expand Down

0 comments on commit 1c6d37e

Please sign in to comment.