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

Introduce ScalingParameters for Scalable #2461

Merged
merged 10 commits into from
Mar 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public double minimumValue(Network n, ScalingConvention powerConvention) {
return value;
}

@Override
public double scale(Network n, double asked) {
return scale(n, asked, ScalingConvention.GENERATOR);
annetill marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void filterInjections(Network n, List<Injection> injections, List<String> notFoundInjections) {
for (Scalable scalable : getScalables()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,4 @@ public double minimumValue(Network n) {
return minimumValue(n, ScalingConvention.GENERATOR);
}

@Override
public double scale(Network n, double asked) {
return scale(n, asked, ScalingConvention.GENERATOR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public void filterInjections(Network n, List<Injection> injections, List<String>
* If scalingConvention is GENERATOR, the load active power decreases for positive "asked" and increases inversely
*/
@Override
public double scale(Network n, double asked, Scalable.ScalingConvention scalingConvention) {
public double scale(Network n, double asked, ScalingContext context) {
Copy link
Member

Choose a reason for hiding this comment

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

If we just add read only data in ScalingContext, it is not really a context. It is more a ScalingParameter

Objects.requireNonNull(n);
Objects.requireNonNull(scalingConvention);
Objects.requireNonNull(context);

DanglingLine dl = n.getDanglingLine(id);

Expand All @@ -122,7 +122,7 @@ public double scale(Network n, double asked, Scalable.ScalingConvention scalingC
}

Terminal t = dl.getTerminal();
if (!t.isConnected()) {
if (!t.isConnected() && context.isReconnect()) {
t.connect();
LOGGER.info("Connecting {}", dl.getId());
}
Expand All @@ -138,7 +138,7 @@ public double scale(Network n, double asked, Scalable.ScalingConvention scalingC
double availableDown = oldP0 - minValue;
double availableUp = maxValue - oldP0;

if (scalingConvention == LOAD) {
if (context.getScalingConvention() == LOAD) {
done = asked > 0 ? Math.min(asked, availableUp) : -Math.min(-asked, availableDown);
dl.setP0(oldP0 + done);
} else {
Expand All @@ -161,9 +161,4 @@ public double maximumValue(Network n) {
public double minimumValue(Network n) {
return minimumValue(n, scalingConvention);
}

@Override
public double scale(Network n, double asked) {
return scale(n, asked, scalingConvention);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public void filterInjections(Network n, List<Injection> injections, List<String>
* If scalingConvention is LOAD, the generator active power decreases for positive "asked" and increases inversely
*/
@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
public double scale(Network n, double asked, ScalingContext context) {
Objects.requireNonNull(n);
Objects.requireNonNull(scalingConvention);
Objects.requireNonNull(context);

Generator g = n.getGenerator(id);
double done = 0;
Expand All @@ -118,7 +118,7 @@ public double scale(Network n, double asked, ScalingConvention scalingConvention
}

Terminal t = g.getTerminal();
if (!t.isConnected()) {
if (!t.isConnected() && context.isReconnect()) {
new ConnectGenerator(g.getId()).apply(n);
LOGGER.info("Connecting {}", g.getId());
}
Expand All @@ -136,7 +136,7 @@ public double scale(Network n, double asked, ScalingConvention scalingConvention
double availableUp = maximumTargetP - oldTargetP;
double availableDown = oldTargetP - minimumTargetP;

if (scalingConvention == GENERATOR) {
if (context.getScalingConvention() == GENERATOR) {
done = asked > 0 ? Math.min(asked, availableUp) : -Math.min(-asked, availableDown);
g.setTargetP(oldTargetP + done);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public void filterInjections(Network n, List<Injection> injections, List<String>
}
}

private double scale(Network n, double asked, ScalingConvention scalingConvention, boolean constantPowerFactor) {
@Override
public double scale(Network n, double asked, ScalingContext context) {
Objects.requireNonNull(n);
Objects.requireNonNull(scalingConvention);
Objects.requireNonNull(context);

Load l = n.getLoad(id);

Expand All @@ -108,7 +109,7 @@ private double scale(Network n, double asked, ScalingConvention scalingConventio
}

Terminal t = l.getTerminal();
if (!t.isConnected()) {
if (!t.isConnected() && context.isReconnect()) {
t.connect();
LOGGER.info("Connecting {}", l.getId());
}
Expand All @@ -125,7 +126,7 @@ private double scale(Network n, double asked, ScalingConvention scalingConventio
double availableDown = oldP0 - minValue;
double availableUp = maxValue - oldP0;

if (scalingConvention == LOAD) {
if (context.getScalingConvention() == LOAD) {
done = asked > 0 ? Math.min(asked, availableUp) : -Math.min(-asked, availableDown);
l.setP0(oldP0 + done);
} else {
Expand All @@ -136,7 +137,7 @@ private double scale(Network n, double asked, ScalingConvention scalingConventio
LOGGER.info("Change active power setpoint of {} from {} to {} ",
l.getId(), oldP0, l.getP0());

if (constantPowerFactor) {
if (context.isConstantPowerFactor()) {
l.setQ0(l.getP0() * oldQ0 / oldP0);
LOGGER.info("Change reactive power setpoint of {} from {} to {} ",
l.getId(), oldQ0, l.getQ0());
Expand All @@ -145,24 +146,4 @@ private double scale(Network n, double asked, ScalingConvention scalingConventio
return done;
}

/**
* {@inheritDoc}
*
* If scalingConvention is LOAD, the load active power increases for positive "asked" and decreases inversely
* If scalingConvention is GENERATOR, the load active power decreases for positive "asked" and increases inversely
*/
@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
return scale(n, asked, scalingConvention, false);
}

@Override
public double scaleWithConstantPowerFactor(Network n, double asked, ScalingConvention scalingConvention) {
return scale(n, asked, scalingConvention, true);
}

@Override
public double scaleWithConstantPowerFactor(Network n, double asked) {
return scaleWithConstantPowerFactor(n, asked, ScalingConvention.GENERATOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,24 @@ private void updateIterationPercentages() {
});
}

private double iterativeScale(Network n, double asked, ScalingConvention scalingConvention, boolean constantPowerFactor) {
private double iterativeScale(Network n, double asked, ScalingContext context) {
double done = 0;
while (Math.abs(asked - done) > EPSILON && notSaturated()) {
checkIterationPercentages();
done += scaleIteration(n, asked - done, scalingConvention, constantPowerFactor);
done += scaleIteration(n, asked - done, context);
updateIterationPercentages();
}
return done;
}

private double scaleIteration(Network n, double asked, ScalingConvention scalingConvention, boolean constantPowerFactor) {
private double scaleIteration(Network n, double asked, ScalingContext context) {
double done = 0;
for (ScalablePercentage scalablePercentage : scalablePercentageList) {
Scalable s = scalablePercentage.getScalable();
double iterationPercentage = scalablePercentage.getIterationPercentage();
double askedOnScalable = iterationPercentage / 100 * asked;
double doneOnScalable = 0;
if (constantPowerFactor) {
doneOnScalable = s.scaleWithConstantPowerFactor(n, askedOnScalable, scalingConvention);
} else {
doneOnScalable = s.scale(n, askedOnScalable, scalingConvention);
}
doneOnScalable = s.scale(n, askedOnScalable, context);
if (Math.abs(doneOnScalable - askedOnScalable) > EPSILON) {
scalablePercentage.setIterationPercentage(0);
}
Expand All @@ -153,31 +149,14 @@ private double scaleIteration(Network n, double asked, ScalingConvention scaling
}

@Override
public double scaleWithConstantPowerFactor(Network n, double asked) {
return scaleWithConstantPowerFactor(n, asked, ScalingConvention.GENERATOR);
}

@Override
public double scaleWithConstantPowerFactor(Network n, double asked, ScalingConvention scalingConvention) {
Objects.requireNonNull(n);
Objects.requireNonNull(scalingConvention);
reinitIterationPercentage();
if (iterative) {
return iterativeScale(n, asked, scalingConvention, true);
} else {
return scaleIteration(n, asked, scalingConvention, true);
}
}

@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
public double scale(Network n, double asked, ScalingContext context) {
Objects.requireNonNull(n);
Objects.requireNonNull(scalingConvention);
Objects.requireNonNull(context);
reinitIterationPercentage();
if (iterative) {
return iterativeScale(n, asked, scalingConvention, false);
return iterativeScale(n, asked, context);
} else {
return scaleIteration(n, asked, scalingConvention, false);
return scaleIteration(n, asked, context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,43 +134,10 @@ enum ScalingConvention {
* @param asked value asked to adjust the scalable active power
Copy link
Contributor

Choose a reason for hiding this comment

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

You should add param annotation in the javadoc for the new parameter

* @return the actual value of the scalable active power adjustment
*/
double scale(Network n, double asked);
double scale(Network n, double asked, ScalingContext context);

/**
* Scale the given network.
* The actual scaling value may be different to the one asked, if
* the Scalable limit is reached.
*
* @param n network
* @param asked value asked to adjust the scalable active power
* @param scalingConvention power convention used for scaling
* @return the actual value of the scalable active power adjustment
* @see ScalingConvention
*/
double scale(Network n, double asked, ScalingConvention scalingConvention);

/**
* Scale the given network using Generator convention by default.
* If the object is a load, scaling is done with constant power factor.
* @param n network
* @param asked value asked to adjust the scalable active power and reactive power if load
* @return the actual value of the scalable active power adjustment
*/
default double scaleWithConstantPowerFactor(Network n, double asked) {
return scale(n, asked);
}

/**
* Scale the given network.
* The actual scaling value may be different to the one asked, if
* the Scalable limit is reached. If the Scalable is a load, the power factor is kept constant.
* @param n network
* @param asked value asked to adjust the scalable active power and reactive power if load
* @param scalingConvention power convention used for scaling
* @return the actual value of the scalable active power adjustment
*/
default double scaleWithConstantPowerFactor(Network n, double asked, ScalingConvention scalingConvention) {
return scale(n, asked, scalingConvention);
default double scale(Network n, double asked) {
return scale(n, asked, ScalingContext.getDefault());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void filterInjections(Network network, List<Injection> injections, List<S
}

@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
return getScalable(n).scale(n, asked, scalingConvention);
public double scale(Network n, double asked, ScalingContext context) {
return getScalable(n).scale(n, asked, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.modification.scalable;

/**
* @author Coline Piloquet <coline.piloquet@rte-france.fr>
*/
public class ScalingContext {

private final Scalable.ScalingConvention scalingConvention;

private final boolean reconnect;

private final boolean constantPowerFactor;

public ScalingContext(Scalable.ScalingConvention scalingConvention, boolean reconnect, boolean constantPowerFactor) {
this.scalingConvention = scalingConvention;
this.reconnect = reconnect;
this.constantPowerFactor = constantPowerFactor;
}

public ScalingContext(Scalable.ScalingConvention scalingConvention) {
this(scalingConvention, false, false);
}

public ScalingContext(boolean reconnect, boolean constantPowerFactor) {
this(Scalable.ScalingConvention.GENERATOR, reconnect, constantPowerFactor);
}

/**
* @return the scaling convention for the scaling.
*/
public Scalable.ScalingConvention getScalingConvention() {
return scalingConvention;
}

/**
* @return a boolean indicating if the terminal of the scalable should be reconnected if it is disconnected.
*/
public boolean isReconnect() {
return reconnect;
}

/**
* @return a boolean indicating if the scaling should be done with constant power factor.
*/
public boolean isConstantPowerFactor() {
return constantPowerFactor;
}

/**
* @return default scaling context: generator convention, not reconnecting terminals and not scaling at constant
* power factor.
*/
public static ScalingContext getDefault() {
return new ScalingContext(Scalable.ScalingConvention.GENERATOR, false, false);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if it is the right design but it seems useful to have a default context

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ Collection<Scalable> getScalables() {
}

@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
public double scale(Network n, double asked, ScalingContext context) {
Objects.requireNonNull(n);

double done = 0;
double remaining = asked;
for (Scalable scalable : scalables) {
if (Math.abs(remaining) > EPSILON) {
double v = scalable.scale(n, remaining, scalingConvention);
double v = scalable.scale(n, remaining, context);
done += v;
remaining -= v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void filterInjections(Network network, List<Injection> injections, List<S
}

@Override
public double scale(Network n, double asked, ScalingConvention scalingConvention) {
return asked > 0 ? upScalable.scale(n, asked, scalingConvention) : downScalable.scale(n, asked, scalingConvention);
public double scale(Network n, double asked, ScalingContext context) {
return asked > 0 ? upScalable.scale(n, asked, context) : downScalable.scale(n, asked, context);
}
}
Loading