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

Add scaling parameters to BalanceComputationParameters and adapt to core 5.2.0 #93

Merged
merged 3 commits into from
Mar 31, 2023
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 @@ -81,12 +81,7 @@ public CompletableFuture<BalanceComputationResult> run(Network network, String w
double asked = entry.getValue();

Scalable scalable = area.getScalable();
double done = 0;
if (parameters.isLoadPowerFactorConstant()) {
done = scalable.scaleWithConstantPowerFactor(network, balanceOffsets.get(area));
} else {
done = scalable.scale(network, balanceOffsets.get(area));
}
double done = scalable.scale(network, balanceOffsets.get(area), parameters.getScalingParameters());
LOGGER.info("Scaling for area {}: asked={}, done={}", area.getName(), asked, done);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
*/
package com.powsybl.balances_adjustment.balance_computation;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.extensions.*;
import com.powsybl.commons.extensions.AbstractExtendable;
import com.powsybl.commons.extensions.Extension;
import com.powsybl.commons.extensions.ExtensionConfigLoader;
import com.powsybl.commons.extensions.ExtensionProviders;
import com.powsybl.iidm.modification.scalable.ScalingParameters;
import com.powsybl.loadflow.LoadFlowParameters;

import java.util.Objects;
import java.util.function.Supplier;

/**
* parameters for balance computation.
Expand All @@ -22,9 +26,10 @@
*/
public class BalanceComputationParameters extends AbstractExtendable<BalanceComputationParameters> {

public static final String VERSION = "1.1";

public static final double DEFAULT_THRESHOLD_NET_POSITION = 1;
public static final int DEFAULT_MAX_NUMBER_ITERATIONS = 5;
public static final boolean DEFAULT_LOAD_POWER_FACTOR_CONSTANT = false;

/**
* Threshold for comparing net positions (given in MW).
Expand All @@ -37,13 +42,11 @@ public class BalanceComputationParameters extends AbstractExtendable<BalanceComp
*/
private int maxNumberIterations;

private boolean loadPowerFactorConstant;

/**
* Constructor with default parameters
*/
public BalanceComputationParameters() {
this(DEFAULT_THRESHOLD_NET_POSITION, DEFAULT_MAX_NUMBER_ITERATIONS, DEFAULT_LOAD_POWER_FACTOR_CONSTANT);
this(DEFAULT_THRESHOLD_NET_POSITION, DEFAULT_MAX_NUMBER_ITERATIONS);
}

/**
Expand All @@ -52,35 +55,56 @@ public BalanceComputationParameters() {
* @param maxNumberIterations Maximum iteration number for balances adjustment
*/
public BalanceComputationParameters(double threshold, int maxNumberIterations) {
this(threshold, maxNumberIterations, DEFAULT_LOAD_POWER_FACTOR_CONSTANT);
this.thresholdNetPosition = checkThresholdNetPosition(threshold);
this.maxNumberIterations = checkMaxNumberIterations(maxNumberIterations);
}

/**
* @deprecated Use {@link #BalanceComputationParameters()} or {@link #BalanceComputationParameters(double, int)} instead.
*/
@Deprecated(since = "2.3.0")
public BalanceComputationParameters(double threshold, int maxNumberIterations, boolean loadPowerFactorConstant) {
this.thresholdNetPosition = checkThresholdNetPosition(threshold);
this.maxNumberIterations = checkMaxNumberIterations(maxNumberIterations);
this.loadPowerFactorConstant = loadPowerFactorConstant;
this(threshold, maxNumberIterations);
scalingParameters.setConstantPowerFactor(loadPowerFactorConstant);
}

private static final double checkThresholdNetPosition(double threshold) {
private static double checkThresholdNetPosition(double threshold) {
if (threshold < 0) {
throw new IllegalArgumentException("Threshold must be positive");
}
return threshold;
}

private static final int checkMaxNumberIterations(int maxNumberIterations) {
private static int checkMaxNumberIterations(int maxNumberIterations) {
if (maxNumberIterations < 0) {
throw new IllegalArgumentException("The maximum number of iterations must be positive");
}
return maxNumberIterations;
}

public ScalingParameters getScalingParameters() {
return scalingParameters;
}

public BalanceComputationParameters setScalingParameters(ScalingParameters scalingParameters) {
this.scalingParameters = Objects.requireNonNull(scalingParameters);
return this;
}

/**
* @deprecated Use {@link #getScalingParameters()} and {@link ScalingParameters#isConstantPowerFactor()} instead.
*/
@Deprecated(since = "2.3.0")
public boolean isLoadPowerFactorConstant() {
return loadPowerFactorConstant;
return scalingParameters.isConstantPowerFactor();
}

/**
* @deprecated Use {@link #getScalingParameters()} and {@link ScalingParameters#setConstantPowerFactor(boolean)} instead.
*/
@Deprecated(since = "2.3.0")
public void setLoadPowerFactorConstant(boolean loadPowerFactorConstant) {
this.loadPowerFactorConstant = loadPowerFactorConstant;
this.scalingParameters.setConstantPowerFactor(loadPowerFactorConstant);
}

/**
Expand All @@ -96,6 +120,8 @@ public static interface ConfigLoader<E extends Extension<BalanceComputationParam

private LoadFlowParameters loadFlowParameters = new LoadFlowParameters();

private ScalingParameters scalingParameters = new ScalingParameters();

Comment on lines 121 to +124
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to define these in the middle of the class rather than at the beginning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, none, I just put it below LoadFlowParameters that was already there

/**
* Load parameters from platform default config.
*/
Expand All @@ -110,14 +136,17 @@ public static BalanceComputationParameters load(PlatformConfig platformConfig) {
Objects.requireNonNull(platformConfig);

BalanceComputationParameters parameters = new BalanceComputationParameters();
platformConfig.getOptionalModuleConfig("balance-computation-parameters").ifPresent(config -> parameters.setMaxNumberIterations(config.getIntProperty("maxNumberIterations", DEFAULT_MAX_NUMBER_ITERATIONS))
.setThresholdNetPosition(config.getDoubleProperty("thresholdNetPosition", DEFAULT_THRESHOLD_NET_POSITION)));
parameters.readExtensions(platformConfig);

parameters.setLoadFlowParameters(LoadFlowParameters.load(platformConfig));
parameters.setScalingParameters(ScalingParameters.load(platformConfig));
return parameters;
}

private void readExtensions(PlatformConfig platformConfig) {
for (ExtensionConfigLoader provider : SUPPLIER.get().getProviders()) {
for (ConfigLoader provider : SUPPLIER.get().getProviders()) {
addExtension(provider.getExtensionClass(), provider.load(platformConfig));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.powsybl.balances_adjustment.balance_computation.BalanceComputationParameters;
import com.powsybl.commons.extensions.Extension;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.iidm.modification.scalable.json.JsonScalingParameters;
import com.powsybl.loadflow.json.JsonLoadFlowParameters;

import java.io.IOException;
Expand All @@ -24,6 +25,8 @@
*/
public class BalanceComputationParametersDeserializer extends StdDeserializer<BalanceComputationParameters> {

private static final String CONTEXT_NAME = "BalanceComputationParameters";

BalanceComputationParametersDeserializer() {
super(BalanceComputationParameters.class);
}
Expand All @@ -35,10 +38,15 @@ public BalanceComputationParameters deserialize(JsonParser parser, Deserializati

@Override
public BalanceComputationParameters deserialize(JsonParser parser, DeserializationContext deserializationContext, BalanceComputationParameters parameters) throws IOException {

String version = "1.0"; // when no version specified, considered 1.0 (version was not serialized in 1.0)
List<Extension<BalanceComputationParameters>> extensions = Collections.emptyList();
while (parser.nextToken() != JsonToken.END_OBJECT) {
switch (parser.getCurrentName()) {
case "version":
parser.nextToken();
version = parser.getValueAsString();
break;

case "maxNumberIterations":
parser.nextToken();
parameters.setMaxNumberIterations(parser.readValueAs(int.class));
Expand All @@ -54,6 +62,12 @@ public BalanceComputationParameters deserialize(JsonParser parser, Deserializati
JsonLoadFlowParameters.deserialize(parser, deserializationContext, parameters.getLoadFlowParameters());
break;

case "scaling-parameters":
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, "Tag: scaling-parameters", version, "1.1");
parser.nextToken();
JsonScalingParameters.deserialize(parser, deserializationContext, parameters.getScalingParameters());
break;

case "extensions":
parser.nextToken();
extensions = JsonUtil.readExtensions(parser, deserializationContext, JsonBalanceComputationParameters.getExtensionSerializers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.powsybl.balances_adjustment.balance_computation.BalanceComputationParameters;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.iidm.modification.scalable.json.JsonScalingParameters;
import com.powsybl.loadflow.json.JsonLoadFlowParameters;

import java.io.IOException;
Expand All @@ -28,10 +29,13 @@ public void serialize(BalanceComputationParameters parameters, JsonGenerator jso

jsonGenerator.writeStartObject();

jsonGenerator.writeStringField("version", BalanceComputationParameters.VERSION);
jsonGenerator.writeNumberField("maxNumberIterations", parameters.getMaxNumberIterations());
jsonGenerator.writeNumberField("thresholdNetPosition", parameters.getThresholdNetPosition());
jsonGenerator.writeFieldName("load-flow-parameters");
JsonLoadFlowParameters.serialize(parameters.getLoadFlowParameters(), jsonGenerator, serializerProvider);
jsonGenerator.writeFieldName("scaling-parameters");
JsonScalingParameters.serialize(parameters.getScalingParameters(), jsonGenerator, serializerProvider);

JsonUtil.writeExtensions(parameters, jsonGenerator, serializerProvider, JsonBalanceComputationParameters.getExtensionSerializers());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void testUnBalancedNetwork() {

@Test
void testConstantPowerFactor() {
parameters.setLoadPowerFactorConstant(true);
parameters.getScalingParameters().setConstantPowerFactor(true);
List<BalanceComputationArea> areas = new ArrayList<>();
areas.add(new BalanceComputationArea("FR", countryAreaFR, scalableFR, 1200.));
areas.add(new BalanceComputationArea("BE", countryAreaBE, scalableBE, 1300.));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"version" : "1.1",
"maxNumberIterations" : 11,
"thresholdNetPosition" : 2.0,
"load-flow-parameters" : {
"version" : "1.8",
"version" : "1.9",
"voltageInitMode" : "DC_VALUES",
"transformerVoltageControlOn" : true,
"phaseShifterRegulationOn" : true,
Expand All @@ -17,6 +18,14 @@
"dcUseTransformerRatio" : true,
"countriesToBalance" : [ ],
"connectedComponentMode" : "MAIN",
"hvdcAcEmulation" : true
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
},
"scaling-parameters" : {
"version" : "1.0",
"scalingConvention" : "GENERATOR",
"constantPowerFactor" : false,
"reconnect" : false,
"iterative" : false
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"version" : "1.1",
"maxNumberIterations" : 5,
"thresholdNetPosition" : 1.0,
"load-flow-parameters" : {
"version" : "1.8",
"version" : "1.9",
"voltageInitMode" : "UNIFORM_VALUES",
"transformerVoltageControlOn" : false,
"phaseShifterRegulationOn" : false,
Expand All @@ -17,7 +18,15 @@
"dcUseTransformerRatio" : true,
"countriesToBalance" : [ ],
"connectedComponentMode" : "MAIN",
"hvdcAcEmulation" : true
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
},
"scaling-parameters" : {
"version" : "1.0",
"scalingConvention" : "GENERATOR",
"constantPowerFactor" : false,
"reconnect" : false,
"iterative" : false
},
"extensions" : {
"dummy-extension" : { }
Expand Down
3 changes: 1 addition & 2 deletions emf/src/test/java/com/powsybl/emf/IGMmergeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ void cgmToCgmes() throws IOException {
Set<String> generatorsId = new HashSet<>();
Set<String> voltageLevelIds = new HashSet<>();

//networkBENL.getBranches().forEach(b -> branchIds.add(b.getId()));
networkBENL.getBranches().forEach(b -> branchIds.add(b.getId().replace(" ", "%20"))); // FIXME workaround before fixing CGMES export/import
networkBENL.getBranches().forEach(b -> branchIds.add(b.getId()));
networkBENL.getGenerators().forEach(g -> generatorsId.add(g.getId()));
networkBENL.getVoltageLevels().forEach(v -> voltageLevelIds.add(v.getId()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static Scalable convert(Network network, List<GlskShiftKey> shiftKeys) {
throw new GlskException("In convert glskShiftKey business type not supported");
}
}
return Scalable.proportional(percentages, scalables, true);
return Scalable.proportional(percentages, scalables); // iterative must be set in scalingParameters during scale
}

private static void convertRemainingCapacity(Network network, GlskShiftKey glskShiftKey, List<Float> percentages, List<Scalable> scalables) {
Expand Down Expand Up @@ -97,7 +97,7 @@ private static Scalable createRemainingCapacityScalable(Network network, GlskShi
scalables.add(getGeneratorScalableWithLimits(network, generatorResource));
}
});
return Scalable.proportional(percentages, scalables, true);
return Scalable.proportional(percentages, scalables); // iterative must be set in scalingParameters during scale
}

private static double getRemainingCapacityUp(GlskRegisteredResource resource, Network network) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<properties>
<java.version>11</java.version>
<powsyblcore.version>5.2.0-alpha-1</powsyblcore.version>
<powsyblcore.version>5.2.0</powsyblcore.version>
<powsyblopenloadflow.version>1.0.0</powsyblopenloadflow.version>
<sonar.coverage.jacoco.xmlReportPaths>
../distribution-entsoe/target/site/jacoco-aggregate/jacoco.xml,
Expand Down