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 sensitivity threshold parameters #2499

Merged
merged 6 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -6,6 +6,7 @@
*/
package com.powsybl.sensitivity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.extensions.AbstractExtendable;
import com.powsybl.commons.util.ServiceLoaderCache;
Expand All @@ -21,7 +22,24 @@
*/
public class SensitivityAnalysisParameters extends AbstractExtendable<SensitivityAnalysisParameters> {

public static final String VERSION = "1.0";
public static final String VERSION = "1.1";

static final double FLOW_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE = 0.0;
static final double VOLTAGE_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE = 0.0;
static final double FLOW_VOLTAGE_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE = 0.0;
static final double ANGLE_FLOW_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE = 0.0;

@JsonProperty("flow-sensitivity-value-threshold")
obrix marked this conversation as resolved.
Show resolved Hide resolved
private double flowSensitivityValueThreshold = FLOW_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE;

@JsonProperty("voltage-sensitivity-value-threshold")
private double voltageSensitivityValueThreshold = VOLTAGE_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE;

@JsonProperty("flow-voltage-sensitivity-value-threshold")
private double flowVoltageSensitivityValueThreshold = FLOW_VOLTAGE_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE;

@JsonProperty("angle-flow-sensitivity-value-threshold")
private double angleFlowSensitivityValueThreshold = ANGLE_FLOW_SENSITIVITY_VALUE_THRESHOLD_DEFAULT_VALUE;

private LoadFlowParameters loadFlowParameters = new LoadFlowParameters();

Expand All @@ -40,7 +58,6 @@ public static SensitivityAnalysisParameters load(PlatformConfig platformConfig)

SensitivityAnalysisParameters parameters = new SensitivityAnalysisParameters();
parameters.readExtensions(platformConfig);

parameters.setLoadFlowParameters(LoadFlowParameters.load(platformConfig));

return parameters;
Expand All @@ -61,4 +78,40 @@ public SensitivityAnalysisParameters setLoadFlowParameters(LoadFlowParameters lo
this.loadFlowParameters = Objects.requireNonNull(loadFlowParameters);
return this;
}

public SensitivityAnalysisParameters setFlowSensitivityValueThreshold(double threshold) {
flowSensitivityValueThreshold = threshold;
return this;
}

public double getFlowSensitivityValueThreshold() {
return flowSensitivityValueThreshold;
}

public SensitivityAnalysisParameters setVoltageSensitivityValueThreshold(double threshold) {
voltageSensitivityValueThreshold = threshold;
return this;
}

public double getVoltageSensitivityValueThreshold() {
return voltageSensitivityValueThreshold;
}

public SensitivityAnalysisParameters setFlowVoltageSensitivityValueThreshold(double threshold) {
flowVoltageSensitivityValueThreshold = threshold;
return this;
}

public double getFlowVoltageSensitivityValueThreshold() {
return flowVoltageSensitivityValueThreshold;
}

public SensitivityAnalysisParameters setAngleFlowSensitivityValueThreshold(double threshold) {
angleFlowSensitivityValueThreshold = threshold;
return this;
}

public double getAngleFlowSensitivityValueThreshold() {
return angleFlowSensitivityValueThreshold;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
import com.powsybl.commons.extensions.ExtensionProvider;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.sensitivity.SensitivityAnalysisParameters;
import com.powsybl.sensitivity.SensitivityAnalysisProvider;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -33,6 +40,41 @@ public static Map<String, ExtensionJsonSerializer> getExtensionSerializers() {
private JsonSensitivityAnalysisParameters() {
}

public static SensitivityAnalysisParameters read(Path jsonFile) {
return update(new SensitivityAnalysisParameters(), jsonFile);
}

/**
* Reads parameters from a JSON file (will NOT rely on platform config).
*/
public static SensitivityAnalysisParameters read(InputStream jsonStream) {
return update(new SensitivityAnalysisParameters(), jsonStream);
}

/**
* Updates parameters by reading the content of a JSON file.
*/
public static SensitivityAnalysisParameters update(SensitivityAnalysisParameters parameters, Path jsonFile) {
Objects.requireNonNull(jsonFile);
try (InputStream is = Files.newInputStream(jsonFile)) {
return update(parameters, is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Updates parameters by reading the content of a JSON stream.
*/
public static SensitivityAnalysisParameters update(SensitivityAnalysisParameters parameters, InputStream jsonStream) {
try {
ObjectMapper objectMapper = createObjectMapper();
return objectMapper.readerForUpdating(parameters).readValue(jsonStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static ObjectMapper createObjectMapper() {
return JsonUtil.createObjectMapper()
.registerModule(new SensitivityJsonModule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class SensitivityAnalysisParametersDeserializer extends StdDeserializer<SensitivityAnalysisParameters> {

private static final String CONTEXT_NAME = "SensitivityAnalysisParameters";

SensitivityAnalysisParametersDeserializer() {
super(SensitivityAnalysisParameters.class);
}
Expand All @@ -39,18 +41,47 @@ public SensitivityAnalysisParameters deserialize(JsonParser parser, Deserializat
public SensitivityAnalysisParameters deserialize(JsonParser parser, DeserializationContext deserializationContext, SensitivityAnalysisParameters parameters) throws IOException {

List<Extension<SensitivityAnalysisParameters>> extensions = Collections.emptyList();
String version = null;
while (parser.nextToken() != JsonToken.END_OBJECT) {
switch (parser.getCurrentName()) {

case "version":
parser.nextToken();
version = parser.getValueAsString();
break;

case "load-flow-parameters":
parser.nextToken();
JsonLoadFlowParameters.deserialize(parser, deserializationContext, parameters.getLoadFlowParameters());
break;

case "flow-sensitivity-value-threshold":
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, parser.getCurrentName(), version, "1.1");
parser.nextToken();
parameters.setFlowSensitivityValueThreshold(parser.readValueAs(Double.class));
break;

case "voltage-sensitivity-value-threshold":
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, parser.getCurrentName(), version, "1.1");

parser.nextToken();
parameters.setVoltageSensitivityValueThreshold(parser.readValueAs(Double.class));
break;

case "flow-voltage-sensitivity-value-threshold":
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, parser.getCurrentName(), version, "1.1");

parser.nextToken();
parameters.setFlowVoltageSensitivityValueThreshold(parser.readValueAs(Double.class));
break;

case "angle-flow-sensitivity-value-threshold":
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, parser.getCurrentName(), version, "1.1");

parser.nextToken();
parameters.setAngleFlowSensitivityValueThreshold(parser.readValueAs(Double.class));
break;

case "extensions":
parser.nextToken();
extensions = JsonUtil.updateExtensions(parser, deserializationContext, JsonSensitivityAnalysisParameters.getExtensionSerializers()::get, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void serialize(SensitivityAnalysisParameters parameters, JsonGenerator js
jsonGenerator.writeFieldName("load-flow-parameters");
JsonLoadFlowParameters.serialize(parameters.getLoadFlowParameters(), jsonGenerator, serializerProvider);

jsonGenerator.writeNumberField("flow-sensitivity-value-threshold", parameters.getFlowSensitivityValueThreshold());
jsonGenerator.writeNumberField("voltage-sensitivity-value-threshold", parameters.getVoltageSensitivityValueThreshold());
jsonGenerator.writeNumberField("flow-voltage-sensitivity-value-threshold", parameters.getFlowVoltageSensitivityValueThreshold());
jsonGenerator.writeNumberField("angle-flow-sensitivity-value-threshold", parameters.getAngleFlowSensitivityValueThreshold());

JsonUtil.writeExtensions(parameters, jsonGenerator, serializerProvider, JsonSensitivityAnalysisParameters.getExtensionSerializers()::get);

jsonGenerator.writeEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
import com.powsybl.commons.json.JsonUtil;
Expand Down Expand Up @@ -225,4 +226,38 @@ void testSensitivityAnalysisResultContingencyStatusSerializer() throws IOExcepti
roundTripTest(value, (value2, jsonFile) -> JsonUtil.writeJson(jsonFile, value, objectMapper),
jsonFile -> JsonUtil.readJson(jsonFile, SensitivityAnalysisResult.SensitivityContingencyStatus.class, objectMapper), "/contingencyStatusRef.json");
}

@Test
void updateThresholdParameters() {
SensitivityAnalysisParameters parameters = new SensitivityAnalysisParameters();

assertEquals(0.0, parameters.getFlowSensitivityValueThreshold(), 1e-3);
assertEquals(0.0, parameters.getAngleFlowSensitivityValueThreshold(), 1e-3);
assertEquals(0.0, parameters.getFlowVoltageSensitivityValueThreshold(), 1e-3);
assertEquals(0.0, parameters.getVoltageSensitivityValueThreshold(), 1e-3);

parameters.setFlowSensitivityValueThreshold(0.1)
.setAngleFlowSensitivityValueThreshold(0.2)
.setFlowVoltageSensitivityValueThreshold(0.3)
.setVoltageSensitivityValueThreshold(0.4);

assertEquals(0.1, parameters.getFlowSensitivityValueThreshold(), 1e-3);
assertEquals(0.2, parameters.getAngleFlowSensitivityValueThreshold(), 1e-3);
assertEquals(0.3, parameters.getFlowVoltageSensitivityValueThreshold(), 1e-3);
assertEquals(0.4, parameters.getVoltageSensitivityValueThreshold(), 1e-3);
}

@Test
void readJsonVersion10() {
SensitivityAnalysisParameters parameters = JsonSensitivityAnalysisParameters
.read(getClass().getResourceAsStream("/SensitivityAnalysisParametersV1.0.json"));
assertEquals(0.0, parameters.getFlowSensitivityValueThreshold(), 0.0001);
}

@Test
void readJsonVersion10Invalid() {
assertThrows(PowsyblException.class, () -> JsonSensitivityAnalysisParameters
.read(getClass().getResourceAsStream("/SensitivityAnalysisParametersV1.0Invalid.json")),
"SensitivityAnalysisParameters. flow-sensitivity-value-threshold is not valid for version 1.0. Version should be >= 1.1");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "1.0",
"version" : "1.1",
"load-flow-parameters" : {
"version" : "1.9",
"voltageInitMode" : "UNIFORM_VALUES",
Expand All @@ -18,5 +18,9 @@
"connectedComponentMode" : "MAIN",
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
}
},
"flow-sensitivity-value-threshold" : 0.0,
"voltage-sensitivity-value-threshold" : 0.0,
"flow-voltage-sensitivity-value-threshold" : 0.0,
"angle-flow-sensitivity-value-threshold" : 0.0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version" : "1.0",
"load-flow-parameters" : {
"version" : "1.9",
"voltageInitMode" : "UNIFORM_VALUES",
"transformerVoltageControlOn" : false,
"phaseShifterRegulationOn" : false,
"useReactiveLimits" : true,
"twtSplitShuntAdmittance" : false,
"shuntCompensatorVoltageControlOn" : false,
"readSlackBus" : true,
"writeSlackBus" : false,
"dc" : false,
"distributedSlack" : true,
"balanceType" : "PROPORTIONAL_TO_GENERATION_P_MAX",
"dcUseTransformerRatio" : true,
"countriesToBalance" : [ ],
"connectedComponentMode" : "MAIN",
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version" : "1.0",
"load-flow-parameters" : {
"version" : "1.9",
"voltageInitMode" : "UNIFORM_VALUES",
"transformerVoltageControlOn" : false,
"phaseShifterRegulationOn" : false,
"useReactiveLimits" : true,
"twtSplitShuntAdmittance" : false,
"shuntCompensatorVoltageControlOn" : false,
"readSlackBus" : true,
"writeSlackBus" : false,
"dc" : false,
"distributedSlack" : true,
"balanceType" : "PROPORTIONAL_TO_GENERATION_P_MAX",
"dcUseTransformerRatio" : true,
"countriesToBalance" : [ ],
"connectedComponentMode" : "MAIN",
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
},
"flow-sensitivity-value-threshold" : 0.0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "1.0",
"version" : "1.1",
"load-flow-parameters" : {
"version" : "1.9",
"voltageInitMode" : "UNIFORM_VALUES",
Expand All @@ -19,6 +19,10 @@
"hvdcAcEmulation" : true,
"dcPowerFactor" : 1.0
},
"flow-sensitivity-value-threshold" : 0.0,
"voltage-sensitivity-value-threshold" : 0.0,
"flow-voltage-sensitivity-value-threshold" : 0.0,
"angle-flow-sensitivity-value-threshold" : 0.0,
"extensions" : {
"dummy-extension" : { }
}
Expand Down