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,10 +7,11 @@
*/
package com.powsybl.iidm.network.util;

import com.powsybl.iidm.network.LoadingLimits;
import com.powsybl.iidm.network.LoadingLimitsAdder;
import com.powsybl.iidm.network.*;
import com.sun.source.tree.UsesTree;

import java.util.Comparator;
import java.util.Optional;

import static java.lang.Integer.MAX_VALUE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/
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.commons.extensions.Extension;
import com.powsybl.iidm.network.*;
import com.powsybl.commons.ref.Ref;

import java.util.Collection;

/**
*
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
Expand All @@ -20,6 +21,8 @@ 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,8 +39,17 @@ 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
protected NetworkImpl getNetwork() {
return network;
Expand Down Expand Up @@ -110,6 +122,26 @@ public LineImpl add() {
line.addTerminal(terminal1);
line.addTerminal(terminal2);

if (copiedLine != null) {
Collection<Extension<Line>> extensions = copiedLine.getExtensions();
extensions.forEach(lineExtension ->
line.addExtension((Class<? super Extension<Line>>) lineExtension.getClass(), lineExtension));
HugoKulesza marked this conversation as resolved.
Show resolved Hide resolved

copiedLine.getOperationalLimitsGroups1().forEach(operationalLimitsGroup -> {
OperationalLimitsGroup copiedGroup = line.newOperationalLimitsGroup1(operationalLimitsGroup.getId());
copiedGroup.newCurrentLimits(line.getCurrentLimits1().orElse(null)).add();
copiedGroup.newActivePowerLimits(line.getActivePowerLimits1().orElse(null)).add();
copiedGroup.newApparentPowerLimits(line.getApparentPowerLimits1().orElse(null)).add();
});

copiedLine.getOperationalLimitsGroups2().forEach(operationalLimitsGroup -> {
OperationalLimitsGroup copiedGroup = line.newOperationalLimitsGroup1(operationalLimitsGroup.getId());
copiedGroup.newCurrentLimits(line.getCurrentLimits2().orElse(null)).add();
copiedGroup.newActivePowerLimits(line.getActivePowerLimits2().orElse(null)).add();
copiedGroup.newApparentPowerLimits(line.getApparentPowerLimits2().orElse(null)).add();
});
}

// 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,27 @@ 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().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());
}

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
@@ -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,44 @@ 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");

// 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");

assertNotNull(acLine2);
assertTrue(areLinesIdentical(acLine1, acLine2));
}

@Test
public void testMove1Bb() {
Line line = createLineBetweenVoltageAB("line", LINE_NAME, 1.0, 2.0, 3.0, 3.5, 4.0, 4.5);
Expand Down
Loading