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

Create setters for temporary limits #3113

Merged
merged 17 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface TemporaryLimit {
* @return false if it is a real limit, false otherwise
*/
boolean isFictitious();

}

/**
Expand Down Expand Up @@ -80,4 +81,14 @@ interface TemporaryLimit {
* @return the temporary limit value or NaN if there is no temporary limit for this acceptable duration
*/
double getTemporaryLimitValue(int acceptableDuration);

/**
* Set the temporary limit value.
* <p>Throws an exception when no temporary limit of the given acceptable duration is found,
* and changes the value but logs a warning when the new value is not valid.</p>
* @param acceptableDuration the acceptable duration
* @param temporaryLimitValue the temporary limit value
* @return itself for method chaining
*/
LoadingLimits setTemporaryLimitValue(int acceptableDuration, double temporaryLimitValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.LoadingLimits;
import com.powsybl.iidm.network.ValidationException;
import com.powsybl.iidm.network.ValidationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Objects;
import java.util.TreeMap;
import java.util.*;

/**
* @author Miora Ralambotiana {@literal <miora.ralambotiana at rte-france.com>}
*/
abstract class AbstractLoadingLimits<L extends AbstractLoadingLimits<L>> implements LoadingLimits {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLoadingLimits.class);
protected final OperationalLimitsGroupImpl group;
private double permanentLimit;
private final TreeMap<Integer, TemporaryLimit> temporaryLimits;
Expand All @@ -27,7 +29,7 @@ static class TemporaryLimitImpl implements TemporaryLimit {

private final String name;

private final double value;
private double value;

private final int acceptableDuration;

Expand Down Expand Up @@ -85,6 +87,57 @@ public L setPermanentLimit(double permanentLimit) {
return (L) this;
}

@Override
public L setTemporaryLimitValue(int acceptableDuration, double temporaryLimitValue) {
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
if (temporaryLimitValue < 0 || Double.isNaN(temporaryLimitValue)) {
throw new ValidationException(group.getValidable(), "Temporary limit value must be a positive double");
}

// Identify the limit that needs to be modified
TemporaryLimit identifiedLimit = getTemporaryLimit(acceptableDuration);
if (identifiedLimit == null) {
throw new ValidationException(group.getValidable(), "No temporary limit found for the given acceptable duration");
}

TreeMap<Integer, TemporaryLimit> temporaryLimitTreeMap = new TreeMap<>(this.temporaryLimits);
// Creation of index markers
Map.Entry<Integer, TemporaryLimit> biggerDurationEntry = temporaryLimitTreeMap.lowerEntry(acceptableDuration);
Map.Entry<Integer, TemporaryLimit> smallerDurationEntry = temporaryLimitTreeMap.higherEntry(acceptableDuration);

double oldValue = identifiedLimit.getValue();

if (isTemporaryLimitValueValid(biggerDurationEntry, smallerDurationEntry, acceptableDuration, temporaryLimitValue)) {
LOGGER.info("{}Temporary limit value changed from {} to {}", group.getValidable().getMessageHeader(), oldValue, temporaryLimitValue);
} else {
LOGGER.warn("{}Temporary limit value changed from {} to {}, but it is not valid", group.getValidable().getMessageHeader(), oldValue, temporaryLimitValue);
}

this.temporaryLimits.put(acceptableDuration, new TemporaryLimitImpl(identifiedLimit.getName(), temporaryLimitValue,
identifiedLimit.getAcceptableDuration(), identifiedLimit.isFictitious()));

group.notifyTemporaryLimitValueUpdate(getLimitType(), oldValue, temporaryLimitValue, acceptableDuration);

return (L) this;
}

protected boolean isTemporaryLimitValueValid(Map.Entry<Integer, TemporaryLimit> biggerDurationEntry,
Map.Entry<Integer, TemporaryLimit> smallerDurationEntry,
int acceptableDuration,
double temporaryLimitValue) {

boolean checkAgainstBigger = true;
boolean checkAgainstSmaller = true;
if (biggerDurationEntry != null) {
checkAgainstBigger = biggerDurationEntry.getValue().getAcceptableDuration() > acceptableDuration
&& biggerDurationEntry.getValue().getValue() < temporaryLimitValue;
}
if (smallerDurationEntry != null) {
checkAgainstSmaller = smallerDurationEntry.getValue().getAcceptableDuration() < acceptableDuration
&& smallerDurationEntry.getValue().getValue() > temporaryLimitValue;
}
return temporaryLimitValue > this.permanentLimit && checkAgainstBigger && checkAgainstSmaller;
}

@Override
public Collection<TemporaryLimit> getTemporaryLimits() {
return temporaryLimits.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import java.util.*;

import static java.lang.Integer.MAX_VALUE;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ public ActivePowerLimits add() {
group.setActivePowerLimits(limits);
return limits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public ApparentPowerLimits add() {
group.setApparentPowerLimits(limits);
return limits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public class CurrentLimitsImpl extends AbstractLoadingLimits<CurrentLimitsImpl>
public void remove() {
group.removeCurrentLimits();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ private void notifyUpdate(LimitType limitType, OperationalLimits oldValue, Opera
doNotify(attributeName + "_" + limitType, oldOperationalLimitsInfo, newOperationalLimitsInfo);
}

public void notifyTemporaryLimitValueUpdate(LimitType limitType, double oldValue, double newValue, int acceptableDuration) {
TemporaryLimitInfo oldTemporaryLimitInfo = new TemporaryLimitInfo(oldValue, id, id.equals(selectedGroupId), acceptableDuration);
TemporaryLimitInfo newTemporaryLimitInfo = new TemporaryLimitInfo(newValue, id, id.equals(selectedGroupId), acceptableDuration);
doNotify(attributeName + "_" + limitType + ".temporaryLimit.value", oldTemporaryLimitInfo, newTemporaryLimitInfo);
}

private void doNotify(String attribute, Object oldValue, Object newValue) {
if (listeners != null) {
listeners.notifyUpdate(identifiable, attribute, oldValue, newValue);
Expand All @@ -155,4 +161,7 @@ public record PermanentLimitInfo(double value, String groupId, boolean inSelecte

public record OperationalLimitsInfo(OperationalLimits value, String groupId, boolean inSelectedGroup) {
}

public record TemporaryLimitInfo(double value, String groupId, boolean inSelectedGroup, int acceptableDuration) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -697,4 +697,48 @@ public void testAdderWithValueZero() {
assertEquals(0, limits.getPermanentLimit());
assertEquals(0, limits.getTemporaryLimit(Integer.MAX_VALUE).getValue());
}

@Test
public void testSetTemporaryLimitValue() {
colinepiloquet marked this conversation as resolved.
Show resolved Hide resolved
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
Line line = createNetwork().getLine("L");
CurrentLimitsAdder adder = line.newCurrentLimits1()
.setPermanentLimit(1000.)
.beginTemporaryLimit()
.setName("TL1")
.setAcceptableDuration(20 * 60)
.setValue(1200.0)
.endTemporaryLimit()
.beginTemporaryLimit()
.setName("TL2")
.setAcceptableDuration(10 * 60)
.setValue(1400.0)
.endTemporaryLimit()
.beginTemporaryLimit()
.setName("TL3")
.setAcceptableDuration(5 * 60)
.setValue(1600.0)
.endTemporaryLimit();
adder.add();

Optional<CurrentLimits> optionalLimits = line.getCurrentLimits(TwoSides.ONE);
assertTrue(optionalLimits.isPresent());
CurrentLimits limits = optionalLimits.get();

limits.setTemporaryLimitValue(20 * 60, 1050.0);
assertEquals(1050.0, limits.getTemporaryLimit(20 * 60).getValue());

limits.setTemporaryLimitValue(10 * 60, 1450.0);
assertEquals(1450.0, limits.getTemporaryLimit(10 * 60).getValue());

limits.setTemporaryLimitValue(5 * 60, 1750.0);
assertEquals(1750.0, limits.getTemporaryLimit(5 * 60).getValue());

// Tests with invalid values
assertEquals(1750.0, limits.getTemporaryLimit(5 * 60).getValue());
limits.setTemporaryLimitValue(5 * 60, 1250.0);
assertThrows(ValidationException.class, () -> limits.setTemporaryLimitValue(7 * 60, 1550.0));
assertThrows(ValidationException.class, () -> limits.setTemporaryLimitValue(5 * 60, Double.NaN));
assertThrows(ValidationException.class, () -> limits.setTemporaryLimitValue(5 * 60, -6.0));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public LoadingLimits setPermanentLimit(double permanentLimit) {
throw new UnsupportedOperationException("Unsupported operation for reduced loading limits.");
}

@Override
public LoadingLimits setTemporaryLimitValue(int acceptableDuration, double temporaryLimitValue) {
throw new UnsupportedOperationException("Unsupported operation for reduced loading limits.");
}

@Override
public void remove() {
throw new UnsupportedOperationException("Reduced loading limits are not linked to a network element and thus cannot be removed.");
Expand Down