Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adder by copy for lines #3208

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
* </tr>
* </tbody>
* </table>
*

olperr1 marked this conversation as resolved.
Show resolved Hide resolved
* @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 @@ -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,8 @@
*/
package com.powsybl.iidm.network.util;

import com.powsybl.iidm.network.LoadingLimits;
import com.powsybl.iidm.network.LoadingLimitsAdder;
import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;

import java.util.Comparator;

Expand Down Expand Up @@ -96,6 +96,9 @@ 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) {
throw new PowsyblException("Cannot initialize new limits from null limits");
}
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
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,13 @@ 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;
}

@Override
Expand Down Expand Up @@ -110,6 +116,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.attach(terminal1, true);
voltageLevel2.attach(terminal2, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,29 @@ 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 line) {
return newLine(null, line).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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setters should be called in the adder's constructor (or in a utility method, but called in the constructor).
In the current state, these variables are not set when newLine(Line) is called from a subnetwork.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BranchUtil is in the iidm-impl module here. I think it would be better to have this method in the iidm-api module, so it could be reused by custom IIDM implementations (such as powsybl-network-store).

I think you can define it as a static method directly in LineAdder.

Another think. It would be better to call it in LineAdderImpl(NetworkImpl network, String subnetwork, Line copiedLine). It would be counterintuitive that this method initializes the limits, but not the line attributes.

}

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

@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(line.getId());
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Iterable<Line> getLines() {
return getLineStream().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.iidm.network.tck;

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -738,6 +739,7 @@ public void testAdderByCopy() {
adder.add();

assertTrue(areLimitsIdentical(limits1, limits2));
assertThrows(PowsyblException.class, () -> line.newCurrentLimits1(null));
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.powsybl.iidm.network.tck;

import com.powsybl.iidm.network.Line;

import java.util.Objects;

public abstract class AbstractIdenticalLinesTest {
public 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;
}
}
olperr1 marked this conversation as resolved.
Show resolved Hide resolved


Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public abstract class AbstractLineTest {
public abstract class AbstractLineTest extends AbstractIdenticalLinesTest {

private static final String INVALID = "invalid";

Expand Down Expand Up @@ -181,6 +181,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