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 14 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,8 @@ interface TemporaryLimit {
* @return false if it is a real limit, false otherwise
*/
boolean isFictitious();

void setValue(double temporaryLimitValue);
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -80,4 +82,13 @@ 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. Throws an exception when the given acceptable duration is not valid, changes the value but
* logs a warning when the new value is not valid.
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
* @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,21 @@
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.LoadingLimits;
import com.powsybl.iidm.network.Validable;
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 {

protected Validable validable;
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 +31,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 @@ -59,9 +63,15 @@ public int getAcceptableDuration() {
public boolean isFictitious() {
return fictitious;
}

@Override
public void setValue(double temporaryLimitValue) {
this.value = temporaryLimitValue;
}
}

AbstractLoadingLimits(OperationalLimitsGroupImpl owner, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
AbstractLoadingLimits(Validable validable, OperationalLimitsGroupImpl owner, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
this.validable = Objects.requireNonNull(validable);
this.group = Objects.requireNonNull(owner);
this.permanentLimit = permanentLimit;
this.temporaryLimits = Objects.requireNonNull(temporaryLimits);
Expand All @@ -85,6 +95,58 @@ 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(validable, "Temporary limit value must be a non-negative double");
}

// Identify the limit that needs to be modified
TemporaryLimit identifiedLimit = getTemporaryLimit(acceptableDuration);
if (identifiedLimit == null) {
throw new ValidationException(validable, "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 {}", oldValue, temporaryLimitValue);
} else {
LOGGER.warn("Temporary limit value changed from {} to {}, but it is not valid", oldValue, temporaryLimitValue);
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
}

identifiedLimit.setValue(temporaryLimitValue);
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) {

if (biggerDurationEntry != null && smallerDurationEntry != null) {
return biggerDurationEntry.getValue().getAcceptableDuration() > acceptableDuration
&& smallerDurationEntry.getValue().getAcceptableDuration() < acceptableDuration
&& biggerDurationEntry.getValue().getValue() < temporaryLimitValue
&& smallerDurationEntry.getValue().getValue() > temporaryLimitValue;
} else if (biggerDurationEntry == null && smallerDurationEntry != null) {
return temporaryLimitValue > this.permanentLimit &&
smallerDurationEntry.getValue().getValue() > temporaryLimitValue
&& smallerDurationEntry.getValue().getAcceptableDuration() < acceptableDuration;
} else if (biggerDurationEntry != null) {
return biggerDurationEntry.getValue().getAcceptableDuration() > acceptableDuration
&& biggerDurationEntry.getValue().getValue() < temporaryLimitValue;
}
return temporaryLimitValue > this.permanentLimit;
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
}

@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 @@ -33,8 +33,9 @@ public ActivePowerLimits add() {
if (group == null) {
throw new PowsyblException(String.format("Error adding ActivePowerLimits on %s: error getting or creating the group", getOwnerId()));
}
ActivePowerLimits limits = new ActivePowerLimitsImpl(group, permanentLimit, temporaryLimits);
ActivePowerLimits limits = new ActivePowerLimitsImpl(validable, group, permanentLimit, temporaryLimits);
group.setActivePowerLimits(limits);
return limits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.ActivePowerLimits;
import com.powsybl.iidm.network.Validable;

import java.util.TreeMap;

Expand All @@ -16,8 +17,8 @@
*/
class ActivePowerLimitsImpl extends AbstractLoadingLimits<ActivePowerLimitsImpl> implements ActivePowerLimits {

ActivePowerLimitsImpl(OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(group, permanentLimit, temporaryLimits);
ActivePowerLimitsImpl(Validable validable, OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(validable, group, permanentLimit, temporaryLimits);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public ApparentPowerLimits add() {
if (group == null) {
throw new PowsyblException(String.format("Error adding ApparentPowerLimits on %s: error getting or creating the group", getOwnerId()));
}
ApparentPowerLimits limits = new ApparentPowerLimitsImpl(group, permanentLimit, temporaryLimits);
ApparentPowerLimits limits = new ApparentPowerLimitsImpl(validable, group, permanentLimit, temporaryLimits);
group.setApparentPowerLimits(limits);
return limits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.ApparentPowerLimits;
import com.powsybl.iidm.network.Validable;

import java.util.TreeMap;

Expand All @@ -16,8 +17,8 @@
*/
class ApparentPowerLimitsImpl extends AbstractLoadingLimits<ApparentPowerLimitsImpl> implements ApparentPowerLimits {

ApparentPowerLimitsImpl(OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(group, permanentLimit, temporaryLimits);
ApparentPowerLimitsImpl(Validable validable, OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(validable, group, permanentLimit, temporaryLimits);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CurrentLimits add() {
if (group == null) {
throw new PowsyblException(String.format("Error adding CurrentLimits on %s: error getting or creating the group", getOwnerId()));
}
CurrentLimitsImpl limits = new CurrentLimitsImpl(group, permanentLimit, temporaryLimits);
CurrentLimitsImpl limits = new CurrentLimitsImpl(validable, group, permanentLimit, temporaryLimits);
group.setCurrentLimits(limits);
return limits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.CurrentLimits;
import com.powsybl.iidm.network.Validable;

import java.util.TreeMap;

Expand All @@ -17,12 +18,13 @@
*/
public class CurrentLimitsImpl extends AbstractLoadingLimits<CurrentLimitsImpl> implements CurrentLimits {

CurrentLimitsImpl(OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(group, permanentLimit, temporaryLimits);
CurrentLimitsImpl(Validable validable, OperationalLimitsGroupImpl group, double permanentLimit, TreeMap<Integer, TemporaryLimit> temporaryLimits) {
super(validable, group, permanentLimit, temporaryLimits);
}

@Override
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 @@ -46,6 +46,10 @@ public boolean isFictitious() {
return fictitious();
}

@Override
public void setValue(double temporaryLimitValue) {
}

public double getOriginalValue() {
return originalValue();
}
Expand Down Expand Up @@ -101,6 +105,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