Skip to content

Commit

Permalink
Use hasImpactOnNetwork from powsybl-core, 'fix' tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pauline Jean-Marie <pauline.jean-marie@artelys.com>
  • Loading branch information
Pauline Jean-Marie committed Dec 9, 2024
1 parent 625d646 commit 5fb8705
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import com.powsybl.action.*;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.iidm.modification.NetworkModificationImpact;
import com.powsybl.iidm.network.*;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkAction;
import com.powsybl.openrao.data.crac.api.networkaction.SwitchPair;
import com.powsybl.openrao.data.crac.api.NetworkElement;
import com.powsybl.openrao.data.crac.api.usagerule.UsageRule;
import org.apache.commons.lang3.NotImplementedException;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -46,41 +46,10 @@ public Set<Action> getElementaryActions() {
@Override
public boolean hasImpactOnNetwork(Network network) {
return elementaryActions.stream().anyMatch(elementaryAction -> {
if (elementaryAction instanceof GeneratorAction generatorAction) {
Generator generator = network.getGenerator(generatorAction.getGeneratorId());
return Math.abs(generator.getTargetP() - generatorAction.getActivePowerValue().getAsDouble()) >= EPSILON;
} else if (elementaryAction instanceof LoadAction loadAction) {
Load load = network.getLoad(loadAction.getLoadId());
return Math.abs(load.getP0() - loadAction.getActivePowerValue().getAsDouble()) >= EPSILON;
} else if (elementaryAction instanceof DanglingLineAction danglingLineAction) {
DanglingLine danglingLine = network.getDanglingLine(danglingLineAction.getDanglingLineId());
return Math.abs(danglingLine.getP0() - danglingLineAction.getActivePowerValue().getAsDouble()) >= EPSILON;
} else if (elementaryAction instanceof ShuntCompensatorPositionAction shuntCompensatorPositionAction) {
ShuntCompensator shuntCompensator = network.getShuntCompensator(shuntCompensatorPositionAction.getShuntCompensatorId());
return Math.abs(shuntCompensator.getSectionCount() - shuntCompensatorPositionAction.getSectionCount()) > 0;
} else if (elementaryAction instanceof PhaseTapChangerTapPositionAction phaseTapChangerTapPositionAction) {
PhaseTapChanger phaseTapChanger = network.getTwoWindingsTransformer(phaseTapChangerTapPositionAction.getTransformerId()).getPhaseTapChanger();
return phaseTapChangerTapPositionAction.getTapPosition() != phaseTapChanger.getTapPosition();
} else if (elementaryAction instanceof SwitchPair switchPair) {
if (elementaryAction instanceof SwitchPair switchPair) {
return !network.getSwitch(switchPair.getSwitchToOpen().getId()).isOpen() || network.getSwitch(switchPair.getSwitchToClose().getId()).isOpen();
} else if (elementaryAction instanceof TerminalsConnectionAction terminalsConnectionAction) {
Identifiable<?> element = network.getIdentifiable(terminalsConnectionAction.getElementId());
if (element instanceof Branch<?> branch) {
if (terminalsConnectionAction.isOpen()) {
// Line is considered closed if both terminal are connected
return branch.getTerminal1().isConnected() && branch.getTerminal2().isConnected();
} else {
// Line is already considered opened if one of the terminals is disconnected
return !branch.getTerminal1().isConnected() || !branch.getTerminal2().isConnected();
}
} else {
throw new NotImplementedException("TerminalsConnectionAction are only on branches for now");
}
} else if (elementaryAction instanceof SwitchAction switchAction) {
Switch aSwitch = network.getSwitch(switchAction.getSwitchId());
return aSwitch.isOpen() != switchAction.isOpen();
} else {
throw new NotImplementedException();
return elementaryAction.toModification().hasImpactOnNetwork(network) == NetworkModificationImpact.HAS_IMPACT_ON_NETWORK;
}
});
}
Expand All @@ -97,25 +66,12 @@ public boolean apply(Network network) {

@Override
public boolean canBeApplied(Network network) {
// TODO: To implement on powsybl-core Action
return elementaryActions.stream().allMatch(elementaryAction -> {
if (elementaryAction instanceof ShuntCompensatorPositionAction shuntCompensatorPositionAction) {
ShuntCompensator shuntCompensator = network.getShuntCompensator(shuntCompensatorPositionAction.getShuntCompensatorId());
return shuntCompensatorPositionAction.getSectionCount() <= shuntCompensator.getMaximumSectionCount();
} else if (elementaryAction instanceof GeneratorAction || elementaryAction instanceof LoadAction || elementaryAction instanceof DanglingLineAction) {
return true;
} else if (elementaryAction instanceof PhaseTapChangerTapPositionAction phaseTapChangerTapPositionAction) {
// hypothesis: transformer is a two windings transformer
PhaseTapChanger phaseTapChanger = network.getTwoWindingsTransformer(phaseTapChangerTapPositionAction.getTransformerId()).getPhaseTapChanger();
int tapPosition = phaseTapChangerTapPositionAction.getTapPosition();
return tapPosition >= phaseTapChanger.getLowTapPosition() && tapPosition <= phaseTapChanger.getHighTapPosition();
} else if (elementaryAction instanceof SwitchPair switchPair) {
if (elementaryAction instanceof SwitchPair switchPair) {
// It is only applicable if, initially, one switch was closed and the other was open.
return network.getSwitch(switchPair.getSwitchToOpen().getId()).isOpen() != network.getSwitch(switchPair.getSwitchToClose().getId()).isOpen();
} else if (elementaryAction instanceof TerminalsConnectionAction || elementaryAction instanceof SwitchAction) {
return true;
} else {
throw new NotImplementedException();
return elementaryAction.toModification().hasImpactOnNetwork(network) != NetworkModificationImpact.CANNOT_BE_APPLIED;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ void hasImpactOnNetworkThrow() {
.withActivePowerValue(100)
.add()
.add();
assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network));
assertFalse(dummy.canBeApplied(network));
assertFalse(dummy.hasImpactOnNetwork(network));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ void hasImpactOnNetworkThrow() {
.withActivePowerValue(100)
.add()
.add();
assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network));
assertFalse(dummy.canBeApplied(network));
assertFalse(dummy.hasImpactOnNetwork(network));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ void hasImpactOnNetworkThrow() {
.withActivePowerValue(100)
.add()
.add();
assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network));
assertFalse(dummy.canBeApplied(network));
assertFalse(dummy.hasImpactOnNetwork(network));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void hasImpactOnNetworkThrow() {
.withSectionCount(3)
.add()
.add();
assertThrows(NullPointerException.class, () -> dummy.hasImpactOnNetwork(network));
assertFalse(dummy.canBeApplied(network));
assertFalse(dummy.hasImpactOnNetwork(network));
}

@Test
Expand Down

0 comments on commit 5fb8705

Please sign in to comment.