From 71691c6f36661931efbe77e51baab6ce0792a7d7 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Wed, 30 Mar 2022 12:34:09 +0200 Subject: [PATCH] New sensitivity function types (#2039) Signed-off-by: Bertrand Rix --- .../SensitivityAnalysisResult.java | 268 ++++++++++++++++-- .../sensitivity/SensitivityFactor.java | 14 +- .../sensitivity/SensitivityFunctionType.java | 14 +- .../sensitivity/SensitivityValueKey.java | 66 +++++ .../SensitivityAnalysisResultTest.java | 125 +++++++- .../sensitivity/SensitivityAnalysisTest.java | 2 +- .../SensitivityAnalysisToolTest.java | 4 +- .../sensitivity/SensitivityFactorTest.java | 10 +- .../src/test/resources/factorRef.json | 2 +- 9 files changed, 462 insertions(+), 43 deletions(-) create mode 100644 sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityValueKey.java diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityAnalysisResult.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityAnalysisResult.java index 9458f1a62ff..cc3a515162f 100644 --- a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityAnalysisResult.java +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityAnalysisResult.java @@ -8,7 +8,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.contingency.Contingency; -import org.jgrapht.alg.util.Pair; import org.jgrapht.alg.util.Triple; import java.util.*; @@ -46,9 +45,9 @@ public class SensitivityAnalysisResult { private final Map> valuesByContingencyId = new HashMap<>(); - private final Map, SensitivityValue> valuesByContingencyIdAndFunctionIdAndVariableId = new HashMap<>(); + private final Map valuesByContingencyIdAndFunctionAndVariableId = new HashMap<>(); - private final Map, Double> functionReferenceByContingencyAndFunctionId = new HashMap<>(); + private final Map, Double> functionReferenceByContingencyAndFunction = new HashMap<>(); /** * Sensitivity analysis result @@ -66,8 +65,8 @@ public SensitivityAnalysisResult(List factors, List new ArrayList<>()) .add(value); - valuesByContingencyIdAndFunctionIdAndVariableId.put(Triple.of(contingencyId, factor.getFunctionId(), factor.getVariableId()), value); - functionReferenceByContingencyAndFunctionId.put(Pair.of(contingencyId, factor.getFunctionId()), value.getFunctionReference()); + valuesByContingencyIdAndFunctionAndVariableId.put(new SensitivityValueKey(contingencyId, factor.getVariableId(), factor.getFunctionId(), factor.getFunctionType()), value); + functionReferenceByContingencyAndFunction.put(Triple.of(factor.getFunctionType(), contingencyId, factor.getFunctionId()), value.getFunctionReference()); } } @@ -118,55 +117,280 @@ public List getPreContingencyValues() { } /** - * Get the sensitivity value associated to a given function and a given variable and for a specific contingency. + * Get the sensitivity value associated to a given function id and type and a given variable and for a specific contingency. * * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. * @param variableId the sensitivity variable id. * @param functionId the sensitivity function id. + * @param functionType the sensitivity function type. * @return the sensitivity value associated with a given function and a given variable for a given contingency. */ - public double getSensitivityValue(String contingencyId, String variableId, String functionId) { - SensitivityValue value = valuesByContingencyIdAndFunctionIdAndVariableId.get(Triple.of(contingencyId, functionId, variableId)); - if (value == null) { - throw new PowsyblException("Sensitivity value not found for contingency '" + contingencyId + "', function '" - + functionId + "', variable '" + variableId + "'"); + public double getSensitivityValue(String contingencyId, String variableId, String functionId, SensitivityFunctionType functionType) { + SensitivityValue value = valuesByContingencyIdAndFunctionAndVariableId.get(new SensitivityValueKey(contingencyId, variableId, functionId, functionType)); + if (value != null) { + return value.getValue(); } - return value.getValue(); + throw new PowsyblException("Sensitivity value not found for contingency '" + contingencyId + "', function '" + + functionId + "', variable '" + variableId + "'" + "', functionType '" + functionType); } /** - * Get the sensitivity value associated to a given function and a given variable in pre-contingency state. + * Get the sensitivity value associated to a given function id and a given variable and for a specific contingency for function type BRANCH_ACTIVE_POWER_1. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable for a given contingency. + */ + public double getBranchFlow1SensitivityValue(String contingencyId, String variableId, String functionId) { + return getSensitivityValue(contingencyId, variableId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_1); + } + + /** + * Get the sensitivity value associated to a given function id and a given variable and for a specific contingency for function type BRANCH_ACTIVE_POWER_2. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable for a given contingency. + */ + public double getBranchFlow2SensitivityValue(String contingencyId, String variableId, String functionId) { + return getSensitivityValue(contingencyId, variableId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_2); + } + + /** + * Get the sensitivity value associated to a given function id and a given variable and for a specific contingency for function type BRANCH_CURRENT_1. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable for a given contingency. + */ + public double getBranchCurrent1SensitivityValue(String contingencyId, String variableId, String functionId) { + return getSensitivityValue(contingencyId, variableId, functionId, SensitivityFunctionType.BRANCH_CURRENT_1); + } + + /** + * Get the sensitivity value associated to a given function id and a given variable and for a specific contingency for function type BRANCH_CURRENT_2. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable for a given contingency. + */ + public double getBranchCurrent2SensitivityValue(String contingencyId, String variableId, String functionId) { + return getSensitivityValue(contingencyId, variableId, functionId, SensitivityFunctionType.BRANCH_CURRENT_2); + } + + /** + * Get the sensitivity value associated to a given function id and a given variable and for a specific contingency for function type BUS_VOLTAGE. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency sensitivity value. + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable for a given contingency. + */ + public double getBusVoltageSensitivityValue(String contingencyId, String variableId, String functionId) { + return getSensitivityValue(contingencyId, variableId, functionId, SensitivityFunctionType.BUS_VOLTAGE); + } + + /** + * Get the sensitivity value associated to a given function id and type and a given variable in pre-contingency state. + * + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @param functionType sensitivity function type + * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. + */ + public double getSensitivityValue(String variableId, String functionId, SensitivityFunctionType functionType) { + return getSensitivityValue(null, variableId, functionId, functionType); + } + + /** + * Get the sensitivity value associated to a given function and a given variable in pre-contingency state for function type BRANCH_ACTIVE_POWER_1. + * + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. + */ + public double getBranchFlow1SensitivityValue(String variableId, String functionId) { + return getSensitivityValue(null, variableId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_1); + } + + /** + * Get the sensitivity value associated to a given function and a given variable in pre-contingency state for function type BRANCH_ACTIVE_POWER_2. + * + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. + */ + public double getBranchFlow2SensitivityValue(String variableId, String functionId) { + return getSensitivityValue(null, variableId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_2); + } + + /** + * Get the sensitivity value associated to a given function and a given variable in pre-contingency state for function type BRANCH_CURRENT_1. + * + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. + */ + public double getBranchCurrent1SensitivityValue(String variableId, String functionId) { + return getSensitivityValue(null, variableId, functionId, SensitivityFunctionType.BRANCH_CURRENT_1); + } + + /** + * Get the sensitivity value associated to a given function and a given variable in pre-contingency state for function type BRANCH_CURRENT_2. * * @param variableId the sensitivity variable id. * @param functionId the sensitivity function id. * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. */ - public double getSensitivityValue(String variableId, String functionId) { - return getSensitivityValue(null, variableId, functionId); + public double getBranchCurrent2SensitivityValue(String variableId, String functionId) { + return getSensitivityValue(null, variableId, functionId, SensitivityFunctionType.BRANCH_CURRENT_2); } /** - * Get the function reference associated to a given contingency Id and a given function. + * Get the sensitivity value associated to a given function and a given variable in pre-contingency state for function type BUS_VOLTAGE. + * + * @param variableId the sensitivity variable id. + * @param functionId the sensitivity function id. + * @return the sensitivity value associated with a given function and a given variable in pre-contingency state. + */ + public double getBusVoltageSensitivityValue(String variableId, String functionId) { + return getSensitivityValue(null, variableId, functionId, SensitivityFunctionType.BUS_VOLTAGE); + } + + /** + * Get the function reference associated to a given contingency Id and a given function id and type. * * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. * @param functionId sensitivity function id. + * @param functionType sensitivity function type * @return the function reference value */ - public double getFunctionReferenceValue(String contingencyId, String functionId) { - Double value = functionReferenceByContingencyAndFunctionId.get(Pair.of(contingencyId, functionId)); + public double getFunctionReferenceValue(String contingencyId, String functionId, SensitivityFunctionType functionType) { + Double value = functionReferenceByContingencyAndFunction.get(Triple.of(functionType, contingencyId, functionId)); if (value == null) { - throw new PowsyblException("Reference flow value not found for contingency '" + contingencyId + "', function '" + functionId + "'"); + throw new PowsyblException("Reference flow value not found for contingency '" + contingencyId + "', function '" + functionId + "'" + + "', functionType '" + functionType); } return value; } /** - * Get the function reference associated to a given function in a pre-contingency state. + * Get the function reference associated to a given contingency Id and a given function id and function type BRANCH_ACTIVE_POWER_1. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. + * @param functionId sensitivity function id. + * @return the function reference value + */ + public double getBranchFlow1FunctionReferenceValue(String contingencyId, String functionId) { + return getFunctionReferenceValue(contingencyId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_1); + } + + /** + * Get the function reference associated to a given contingency Id and a given function id and function type BRANCH_ACTIVE_POWER_2. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. + * @param functionId sensitivity function id. + * @return the function reference value + */ + public double getBranchFlow2FunctionReferenceValue(String contingencyId, String functionId) { + return getFunctionReferenceValue(contingencyId, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_2); + } + + /** + * Get the function reference associated to a given contingency Id and a given function id and function type BRANCH_CURRENT_1. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. + * @param functionId sensitivity function id. + * @return the function reference value + */ + public double getBranchCurrent1FunctionReferenceValue(String contingencyId, String functionId) { + return getFunctionReferenceValue(contingencyId, functionId, SensitivityFunctionType.BRANCH_CURRENT_1); + } + + /** + * Get the function reference associated to a given contingency Id and a given function id and function type BRANCH_CURRENT_2. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. + * @param functionId sensitivity function id. + * @return the function reference value + */ + public double getBranchCurrent2FunctionReferenceValue(String contingencyId, String functionId) { + return getFunctionReferenceValue(contingencyId, functionId, SensitivityFunctionType.BRANCH_CURRENT_2); + } + + /** + * Get the function reference associated to a given contingency Id and a given function id and function type BUS_VOLTAGE. + * + * @param contingencyId the id of the considered contingency. Use null to get a pre-contingency function reference value. + * @param functionId sensitivity function id. + * @return the function reference value + */ + public double getBusVoltageFunctionReferenceValue(String contingencyId, String functionId) { + return getFunctionReferenceValue(contingencyId, functionId, SensitivityFunctionType.BUS_VOLTAGE); + } + + /** + * Get the function reference associated to a given function id and type in a pre-contingency state. + * + * @param functionId sensitivity function id. + * @param functionType sensitivity function type + * @return the function reference value. + */ + public double getFunctionReferenceValue(String functionId, SensitivityFunctionType functionType) { + return getFunctionReferenceValue(null, functionId, functionType); + } + + /** + * Get the function reference associated to a given function and function type BRANCH_ACTIVE_POWER_1 in a pre-contingency state. + * + * @param functionId sensitivity function id. + * @return the function reference value. + */ + public double getBranchFlow1FunctionReferenceValue(String functionId) { + return getFunctionReferenceValue(null, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_1); + } + + /** + * Get the function reference associated to a given function and function type BRANCH_ACTIVE_POWER_2 in a pre-contingency state. + * + * @param functionId sensitivity function id. + * @return the function reference value. + */ + public double getBranchFlow2FunctionReferenceValue(String functionId) { + return getFunctionReferenceValue(null, functionId, SensitivityFunctionType.BRANCH_ACTIVE_POWER_2); + } + + /** + * Get the function reference associated to a given function and function type BRANCH_CURRENT_1 in a pre-contingency state. + * + * @param functionId sensitivity function id. + * @return the function reference value. + */ + public double getBranchCurrent1FunctionReferenceValue(String functionId) { + return getFunctionReferenceValue(null, functionId, SensitivityFunctionType.BRANCH_CURRENT_1); + } + + /** + * Get the function reference associated to a given function and function type BRANCH_CURRENT_2 in a pre-contingency state. + * + * @param functionId sensitivity function id. + * @return the function reference value. + */ + public double getBranchCurrent2FunctionReferenceValue(String functionId) { + return getFunctionReferenceValue(null, functionId, SensitivityFunctionType.BRANCH_CURRENT_2); + } + + /** + * Get the function reference associated to a given function and function type BUS_VOLTAGE in a pre-contingency state. * * @param functionId sensitivity function id. * @return the function reference value. */ - public double getFunctionReferenceValue(String functionId) { - return getFunctionReferenceValue(null, functionId); + public double getBusVoltageFunctionReferenceValue(String functionId) { + return getFunctionReferenceValue(null, functionId, SensitivityFunctionType.BUS_VOLTAGE); } } diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFactor.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFactor.java index 77f34546ea5..bc3ab08942e 100644 --- a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFactor.java +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFactor.java @@ -110,8 +110,13 @@ public static void writeJson(JsonGenerator jsonGenerator, SensitivityFunctionTyp String variableId, boolean variableSet, ContingencyContext contingencyContext) { try { jsonGenerator.writeStartObject(); - - jsonGenerator.writeStringField("functionType", functionType.name()); + if (functionType.equals(SensitivityFunctionType.BRANCH_CURRENT)) { + jsonGenerator.writeStringField("functionType", SensitivityFunctionType.BRANCH_CURRENT_1.name()); + } else if (functionType.equals(SensitivityFunctionType.BRANCH_ACTIVE_POWER)) { + jsonGenerator.writeStringField("functionType", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1.name()); + } else { + jsonGenerator.writeStringField("functionType", functionType.name()); + } jsonGenerator.writeStringField("functionId", functionId); jsonGenerator.writeStringField("variableType", variableType.name()); jsonGenerator.writeStringField("variableId", variableId); @@ -172,6 +177,11 @@ static void parseJson(JsonParser parser, ParsingContext context) throws IOExcept switch (fieldName) { case "functionType": context.functionType = SensitivityFunctionType.valueOf(parser.nextTextValue()); + if (context.functionType.equals(SensitivityFunctionType.BRANCH_ACTIVE_POWER)) { + context.functionType = SensitivityFunctionType.BRANCH_ACTIVE_POWER_1; + } else if (context.functionType.equals(SensitivityFunctionType.BRANCH_CURRENT)) { + context.functionType = SensitivityFunctionType.BRANCH_CURRENT_1; + } break; case "functionId": context.functionId = parser.nextTextValue(); diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFunctionType.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFunctionType.java index 88e8edd1e6c..c4a47a0c13c 100644 --- a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFunctionType.java +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityFunctionType.java @@ -17,7 +17,17 @@ * @author Geoffroy Jamgotchian */ public enum SensitivityFunctionType { - BRANCH_ACTIVE_POWER, // MW - BRANCH_CURRENT, // A + /** + * @deprecated Use {@link #BRANCH_ACTIVE_POWER_1} instead. + */ + @Deprecated BRANCH_ACTIVE_POWER, // MW + /** + * @deprecated Use {@link #BRANCH_CURRENT_1} instead. + */ + @Deprecated BRANCH_CURRENT, // A + BRANCH_ACTIVE_POWER_1, // MW + BRANCH_CURRENT_1, // A + BRANCH_ACTIVE_POWER_2, // MW + BRANCH_CURRENT_2, // A BUS_VOLTAGE // KV } diff --git a/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityValueKey.java b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityValueKey.java new file mode 100644 index 00000000000..fe9ef7cdc85 --- /dev/null +++ b/sensitivity-analysis-api/src/main/java/com/powsybl/sensitivity/SensitivityValueKey.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2022, 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/. + */ +package com.powsybl.sensitivity; + +import java.util.Objects; + +/* + * Key type for sensitivity value when stored by function type, contingency, function and variable id + * @author Bertrand Rix + */ +public class SensitivityValueKey { + + private final SensitivityFunctionType functionType; + private final String contingencyId; + private final String functionId; + private final String variableId; + + public SensitivityValueKey(final String contingencyId, final String variableId, final String functionId, final SensitivityFunctionType functionType) { + this.contingencyId = contingencyId; + this.variableId = variableId; + this.functionId = functionId; + this.functionType = functionType; + } + + public SensitivityFunctionType getFunctionType() { + return functionType; + } + + public String getContingencyId() { + return contingencyId; + } + + public String getFunctionId() { + return functionId; + } + + public String getVariableId() { + return variableId; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + + if (!(o instanceof SensitivityValueKey)) { + return false; + } + + SensitivityValueKey c = (SensitivityValueKey) o; + + return c.getFunctionType().equals(functionType) && c.getFunctionId().equals(functionId) + && (c.getContingencyId() != null ? c.getContingencyId().equals(contingencyId) : contingencyId == null) + && c.getVariableId().equals(variableId); + } + + @Override + public int hashCode() { + return Objects.hash(functionType, functionId, contingencyId, variableId); + } +} diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisResultTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisResultTest.java index fc26025f0a0..e217adfc232 100644 --- a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisResultTest.java +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisResultTest.java @@ -21,12 +21,104 @@ public class SensitivityAnalysisResultTest { @Test public void test() { - SensitivityFactor factor1 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER, "l", + SensitivityFactor factor1 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, "l", SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", false, ContingencyContext.all()); - SensitivityFactor factor2 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER, "l2", + SensitivityFactor factor2 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, "l2", SensitivityVariableType.INJECTION_ACTIVE_POWER, "g2", false, ContingencyContext.none()); + SensitivityFactor factor3 = new SensitivityFactor(SensitivityFunctionType.BRANCH_CURRENT_1, "l", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", + false, ContingencyContext.all()); + SensitivityFactor factor4 = new SensitivityFactor(SensitivityFunctionType.BRANCH_CURRENT_1, "l2", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g2", + false, ContingencyContext.none()); + + List factors = List.of(factor1, factor2, factor3, factor4); + List contingencies = List.of(new Contingency("NHV1_NHV2_2", new BranchContingency("NHV1_NHV2_2"))); + SensitivityValue value1 = new SensitivityValue(0, 0, 1d, 2d); + SensitivityValue value2 = new SensitivityValue(1, -1, 3d, 4d); + SensitivityValue value3 = new SensitivityValue(2, 0, 1d, 2d); + SensitivityValue value4 = new SensitivityValue(3, -1, 3d, 4d); + List values = List.of(value1, value2, value3, value4); + SensitivityAnalysisResult result = new SensitivityAnalysisResult(factors, contingencies, values); + assertEquals(4, result.getValues().size()); + assertEquals(2, result.getValues("NHV1_NHV2_2").size()); + + assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), 0d); + assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), 0d); + assertEquals(1d, result.getBranchFlow1SensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); + assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); + + assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l", SensitivityFunctionType.BRANCH_CURRENT_1), 0d); + assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l", SensitivityFunctionType.BRANCH_CURRENT_1), 0d); + assertEquals(1d, result.getBranchCurrent1SensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); + assertEquals(2d, result.getBranchCurrent1FunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); + + assertThrows(PowsyblException.class, () -> result.getFunctionReferenceValue("NHV1_NHV2_2", "llll", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1)); + assertThrows(PowsyblException.class, () -> result.getSensitivityValue("NHV1_NHV2_2", "g", "l1", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1)); + assertEquals(3d, result.getSensitivityValue(null, "g2", "l2", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), 0d); + assertEquals(3d, result.getSensitivityValue("g2", "l2", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), 0d); + assertEquals(3d, result.getBranchCurrent1SensitivityValue("g2", "l2"), 0d); + assertEquals(4d, result.getBranchFlow1FunctionReferenceValue("l2"), 0d); + assertEquals(4d, result.getBranchCurrent1FunctionReferenceValue("l2"), 0d); + assertEquals(2, result.getPreContingencyValues().size()); + } + + @Test + public void testSide2() { + SensitivityFactor factor1 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_2, "l", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", + false, ContingencyContext.all()); + SensitivityFactor factor2 = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_2, "l2", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g2", + false, ContingencyContext.none()); + SensitivityFactor factor3 = new SensitivityFactor(SensitivityFunctionType.BRANCH_CURRENT_2, "l", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", + false, ContingencyContext.all()); + SensitivityFactor factor4 = new SensitivityFactor(SensitivityFunctionType.BRANCH_CURRENT_2, "l2", + SensitivityVariableType.INJECTION_ACTIVE_POWER, "g2", + false, ContingencyContext.none()); + + List factors = List.of(factor1, factor2, factor3, factor4); + List contingencies = List.of(new Contingency("NHV1_NHV2_2", new BranchContingency("NHV1_NHV2_2"))); + SensitivityValue value1 = new SensitivityValue(0, 0, 1d, 2d); + SensitivityValue value2 = new SensitivityValue(1, -1, 3d, 4d); + SensitivityValue value3 = new SensitivityValue(2, 0, 1d, 2d); + SensitivityValue value4 = new SensitivityValue(3, -1, 3d, 4d); + List values = List.of(value1, value2, value3, value4); + SensitivityAnalysisResult result = new SensitivityAnalysisResult(factors, contingencies, values); + assertEquals(4, result.getValues().size()); + assertEquals(2, result.getValues("NHV1_NHV2_2").size()); + + assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l", SensitivityFunctionType.BRANCH_ACTIVE_POWER_2), 0d); + assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l", SensitivityFunctionType.BRANCH_ACTIVE_POWER_2), 0d); + assertEquals(1d, result.getBranchFlow2SensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); + assertEquals(2d, result.getBranchFlow2FunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); + + assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l", SensitivityFunctionType.BRANCH_CURRENT_2), 0d); + assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l", SensitivityFunctionType.BRANCH_CURRENT_2), 0d); + assertEquals(1d, result.getBranchCurrent2SensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); + assertEquals(2d, result.getBranchCurrent2FunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); + + assertThrows(PowsyblException.class, () -> result.getFunctionReferenceValue("NHV1_NHV2_2", "llll", SensitivityFunctionType.BRANCH_ACTIVE_POWER_2)); + assertThrows(PowsyblException.class, () -> result.getSensitivityValue("NHV1_NHV2_2", "g", "l1", SensitivityFunctionType.BRANCH_ACTIVE_POWER_2)); + assertEquals(3d, result.getSensitivityValue(null, "g2", "l2", SensitivityFunctionType.BRANCH_ACTIVE_POWER_2), 0d); + assertEquals(3d, result.getBranchCurrent2SensitivityValue("g2", "l2"), 0d); + assertEquals(4d, result.getBranchFlow2FunctionReferenceValue("l2"), 0d); + assertEquals(4d, result.getBranchCurrent2FunctionReferenceValue("l2"), 0d); + assertEquals(2, result.getPreContingencyValues().size()); + } + + @Test + public void testBusVoltage() { + SensitivityFactor factor1 = new SensitivityFactor(SensitivityFunctionType.BUS_VOLTAGE, "l", + SensitivityVariableType.BUS_TARGET_VOLTAGE, "g", + false, ContingencyContext.all()); + SensitivityFactor factor2 = new SensitivityFactor(SensitivityFunctionType.BUS_VOLTAGE, "l2", + SensitivityVariableType.BUS_TARGET_VOLTAGE, "g2", + false, ContingencyContext.none()); + List factors = List.of(factor1, factor2); List contingencies = List.of(new Contingency("NHV1_NHV2_2", new BranchContingency("NHV1_NHV2_2"))); SensitivityValue value1 = new SensitivityValue(0, 0, 1d, 2d); @@ -35,11 +127,28 @@ public void test() { SensitivityAnalysisResult result = new SensitivityAnalysisResult(factors, contingencies, values); assertEquals(2, result.getValues().size()); assertEquals(1, result.getValues("NHV1_NHV2_2").size()); - assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); - assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); - assertThrows(PowsyblException.class, () -> result.getFunctionReferenceValue("NHV1_NHV2_2", "llll")); - assertThrows(PowsyblException.class, () -> result.getSensitivityValue("NHV1_NHV2_2", "g", "l1")); - assertEquals(3d, result.getSensitivityValue(null, "g2", "l2"), 0d); - assertEquals(1, result.getPreContingencyValues().size()); + + assertEquals(1d, result.getSensitivityValue("NHV1_NHV2_2", "g", "l", SensitivityFunctionType.BUS_VOLTAGE), 0d); + assertEquals(2d, result.getFunctionReferenceValue("NHV1_NHV2_2", "l", SensitivityFunctionType.BUS_VOLTAGE), 0d); + assertEquals(1d, result.getBusVoltageSensitivityValue("NHV1_NHV2_2", "g", "l"), 0d); + assertEquals(2d, result.getBusVoltageFunctionReferenceValue("NHV1_NHV2_2", "l"), 0d); + } + + @Test + public void testCompareSensivitiyValueKeysEqualsNotEquals() { + SensitivityValueKey key1 = new SensitivityValueKey(null, "l1", "g1", SensitivityFunctionType.BRANCH_CURRENT_1); + SensitivityValueKey key2 = new SensitivityValueKey("c1", "l1", "g1", SensitivityFunctionType.BRANCH_CURRENT_1); + SensitivityValueKey key3 = new SensitivityValueKey(null, "l1", "g1", SensitivityFunctionType.BRANCH_CURRENT_2); + SensitivityValueKey key4 = new SensitivityValueKey(null, "l2", "g1", SensitivityFunctionType.BRANCH_CURRENT_2); + SensitivityValueKey key5 = new SensitivityValueKey(null, "l2", "g3", SensitivityFunctionType.BRANCH_CURRENT_2); + + assertEquals(key1, key1); + assertNotEquals(key1, key2); + assertNotEquals(key2, key3); + assertNotEquals(key3, key4); + assertNotEquals(key4, key5); + + String dummy = "dummy"; + assertNotEquals(key1, dummy); } } diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisTest.java index 50a0fcab3e2..a29b52a9229 100644 --- a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisTest.java +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisTest.java @@ -42,7 +42,7 @@ public class SensitivityAnalysisTest { public void setUp() { network = EurostagTutorialExample1Factory.create(); computationManager = Mockito.mock(ComputationManager.class); - factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER, + factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER, "GEN", diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisToolTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisToolTest.java index 198d6ac54a7..c288a94ddf1 100644 --- a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisToolTest.java +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityAnalysisToolTest.java @@ -60,8 +60,8 @@ public void setUp() throws Exception { .registerModule(new ContingencyJsonModule()); // create factors - List factors = List.of(new SensitivityFactor(BRANCH_ACTIVE_POWER, "NHV1_NHV2_1", INJECTION_ACTIVE_POWER, "GEN", false, ContingencyContext.all()), - new SensitivityFactor(BRANCH_ACTIVE_POWER, "NHV1_NHV2_1", INJECTION_ACTIVE_POWER, "glsk", true, ContingencyContext.specificContingency("NHV1_NHV2_2"))); + List factors = List.of(new SensitivityFactor(BRANCH_ACTIVE_POWER_1, "NHV1_NHV2_1", INJECTION_ACTIVE_POWER, "GEN", false, ContingencyContext.all()), + new SensitivityFactor(BRANCH_ACTIVE_POWER_2, "NHV1_NHV2_1", INJECTION_ACTIVE_POWER, "glsk", true, ContingencyContext.specificContingency("NHV1_NHV2_2"))); try (Writer writer = Files.newBufferedWriter(fileSystem.getPath("factors.json"), StandardCharsets.UTF_8)) { objectMapper.writeValue(writer, factors); } diff --git a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityFactorTest.java b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityFactorTest.java index ed3740503d6..ff1d3d211f6 100644 --- a/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityFactorTest.java +++ b/sensitivity-analysis-api/src/test/java/com/powsybl/sensitivity/SensitivityFactorTest.java @@ -26,22 +26,22 @@ public class SensitivityFactorTest extends AbstractConverterTest { @Test public void test() { - SensitivityFactor factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER, "l", + SensitivityFactor factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, "l", SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", false, ContingencyContext.all()); assertEquals(ContingencyContext.all(), factor.getContingencyContext()); assertEquals("l", factor.getFunctionId()); - assertEquals(SensitivityFunctionType.BRANCH_ACTIVE_POWER, factor.getFunctionType()); + assertEquals(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, factor.getFunctionType()); assertEquals("l", factor.getFunctionId()); assertEquals(SensitivityVariableType.INJECTION_ACTIVE_POWER, factor.getVariableType()); assertEquals("g", factor.getVariableId()); assertFalse(factor.isVariableSet()); - assertEquals("SensitivityFactor(functionType=BRANCH_ACTIVE_POWER, functionId='l', variableType=INJECTION_ACTIVE_POWER, variableId='g', variableSet=false, contingencyContext=ContingencyContext(contingencyId='', contextType=ALL))", factor.toString()); + assertEquals("SensitivityFactor(functionType=BRANCH_ACTIVE_POWER_1, functionId='l', variableType=INJECTION_ACTIVE_POWER, variableId='g', variableSet=false, contingencyContext=ContingencyContext(contingencyId='', contextType=ALL))", factor.toString()); } @Test public void testMatrix() { - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER, List.of("l12", "l13", "l23"), + List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_2, List.of("l12", "l13", "l23"), SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), false, ContingencyContext.all()); assertEquals(3, factors.size()); @@ -49,7 +49,7 @@ public void testMatrix() { @Test public void testJson() throws IOException { - SensitivityFactor factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER, "l", + SensitivityFactor factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_2, "l", SensitivityVariableType.INJECTION_ACTIVE_POWER, "g", false, ContingencyContext.all()); ObjectMapper objectMapper = SensitivityJson.createObjectMapper(); diff --git a/sensitivity-analysis-api/src/test/resources/factorRef.json b/sensitivity-analysis-api/src/test/resources/factorRef.json index b31353d971e..999cd5fc7c5 100644 --- a/sensitivity-analysis-api/src/test/resources/factorRef.json +++ b/sensitivity-analysis-api/src/test/resources/factorRef.json @@ -1,5 +1,5 @@ { - "functionType" : "BRANCH_ACTIVE_POWER", + "functionType" : "BRANCH_ACTIVE_POWER_2", "functionId" : "l", "variableType" : "INJECTION_ACTIVE_POWER", "variableId" : "g",