diff --git a/src/test/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysisTest.java deleted file mode 100644 index a8e4326ffd..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysisTest.java +++ /dev/null @@ -1,457 +0,0 @@ -/** - * Copyright (c) 2020, 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.openloadflow.sensi; - -import com.powsybl.commons.AbstractConverterTest; -import com.powsybl.commons.PowsyblException; -import com.powsybl.commons.reporter.Reporter; -import com.powsybl.computation.local.LocalComputationManager; -import com.powsybl.contingency.BranchContingency; -import com.powsybl.contingency.Contingency; -import com.powsybl.contingency.ContingencyContext; -import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.loadflow.LoadFlowResult; -import com.powsybl.math.matrix.DenseMatrixFactory; -import com.powsybl.openloadflow.OpenLoadFlowParameters; -import com.powsybl.openloadflow.OpenLoadFlowProvider; -import com.powsybl.openloadflow.network.HvdcNetworkFactory; -import com.powsybl.openloadflow.network.SlackBusSelectionMode; -import com.powsybl.openloadflow.util.LoadFlowAssert; -import com.powsybl.sensitivity.*; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.concurrent.CompletionException; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Geoffroy Jamgotchian - */ -public abstract class AbstractSensitivityAnalysisTest extends AbstractConverterTest { - - protected static final double SENSI_CHANGE = 10e-4; - - protected final DenseMatrixFactory matrixFactory = new DenseMatrixFactory(); - - protected final OpenSensitivityAnalysisProvider sensiProvider = new OpenSensitivityAnalysisProvider(matrixFactory); - - protected final SensitivityAnalysis.Runner sensiRunner = new SensitivityAnalysis.Runner(sensiProvider); - - protected static SensitivityAnalysisParameters createParameters(boolean dc, String slackBusId, boolean distributedSlack) { - return createParameters(dc, List.of(slackBusId), distributedSlack); - } - - protected static SensitivityAnalysisParameters createParameters(boolean dc, List slackBusesIds, boolean distributedSlack) { - SensitivityAnalysisParameters sensiParameters = new SensitivityAnalysisParameters(); - LoadFlowParameters lfParameters = sensiParameters.getLoadFlowParameters(); - lfParameters.setDc(dc) - .setDistributedSlack(distributedSlack); - OpenLoadFlowParameters.create(lfParameters) - .setSlackBusSelectionMode(SlackBusSelectionMode.NAME) - .setSlackBusesIds(slackBusesIds); - return sensiParameters; - } - - protected static SensitivityAnalysisParameters createParameters(boolean dc, String slackBusId) { - return createParameters(dc, slackBusId, false); - } - - protected static SensitivityAnalysisParameters createParameters(boolean dc) { - SensitivityAnalysisParameters sensiParameters = new SensitivityAnalysisParameters(); - LoadFlowParameters lfParameters = sensiParameters.getLoadFlowParameters(); - lfParameters.setDc(dc) - .setDistributedSlack(true); - OpenLoadFlowParameters.create(lfParameters) - .setSlackBusSelectionMode(SlackBusSelectionMode.MOST_MESHED); - return sensiParameters; - } - - protected static > List createFactorMatrix(List injections, List branches, String contingencyId, Branch.Side side) { - Objects.requireNonNull(injections); - Objects.requireNonNull(branches); - return injections.stream().flatMap(injection -> branches.stream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), injection.getId(), contingencyId, side))).collect(Collectors.toList()); - } - - protected static > List createFactorMatrix(List injections, List branches, Branch.Side side) { - return createFactorMatrix(injections, branches, null, side); - } - - protected static > List createFactorMatrix(List injections, List branches) { - return createFactorMatrix(injections, branches, null, Branch.Side.ONE); - } - - protected static > List createFactorMatrix(List injections, List branches, String contingencyId) { - return createFactorMatrix(injections, branches, contingencyId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerInjectionIncrease(String functionId, String variableId, String contingencyId) { - return createBranchFlowPerInjectionIncrease(functionId, variableId, contingencyId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerInjectionIncrease(String functionId, String variableId, String contingencyId, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_1 : SensitivityFunctionType.BRANCH_ACTIVE_POWER_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId, false, Objects.isNull(contingencyId) ? ContingencyContext.all() : ContingencyContext.specificContingency(contingencyId)); - } - - protected static SensitivityFactor createBranchFlowPerInjectionIncrease(String functionId, String variableId, Branch.Side side) { - return createBranchFlowPerInjectionIncrease(functionId, variableId, null, side); - } - - protected static SensitivityFactor createBranchFlowPerInjectionIncrease(String functionId, String variableId) { - return createBranchFlowPerInjectionIncrease(functionId, variableId, null, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId, String contingencyId, Branch.Side side) { - return createBranchFlowPerLinearGlsk(functionId, variableId, Objects.isNull(contingencyId) ? ContingencyContext.all() : ContingencyContext.specificContingency(contingencyId), side); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId, ContingencyContext contingencyContext) { - return createBranchFlowPerLinearGlsk(functionId, variableId, contingencyContext, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId, String contingencyId) { - return createBranchFlowPerLinearGlsk(functionId, variableId, contingencyId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId, ContingencyContext contingencyContext, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_1 : SensitivityFunctionType.BRANCH_ACTIVE_POWER_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId, true, contingencyContext); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId, Branch.Side side) { - return createBranchFlowPerLinearGlsk(functionId, variableId, (String) null, side); - } - - protected static SensitivityFactor createBranchFlowPerLinearGlsk(String functionId, String variableId) { - return createBranchFlowPerLinearGlsk(functionId, variableId, (String) null, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerPSTAngle(String functionId, String variableId, String contingencyId) { - return createBranchFlowPerPSTAngle(functionId, variableId, contingencyId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchFlowPerPSTAngle(String functionId, String variableId, String contingencyId, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_1 : SensitivityFunctionType.BRANCH_ACTIVE_POWER_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.TRANSFORMER_PHASE, variableId, false, Objects.isNull(contingencyId) ? ContingencyContext.all() : ContingencyContext.specificContingency(contingencyId)); - } - - protected static SensitivityFactor createBranchFlowPerPSTAngle(String functionId, String variableId, Branch.Side side) { - return createBranchFlowPerPSTAngle(functionId, variableId, null, side); - } - - protected static SensitivityFactor createBranchFlowPerTransformerLegPSTAngle(String functionId, String variableId, ThreeWindingsTransformer.Side side) { - SensitivityVariableType fVariable = side.equals(ThreeWindingsTransformer.Side.ONE) ? SensitivityVariableType.TRANSFORMER_PHASE_1 - : side.equals(ThreeWindingsTransformer.Side.TWO) ? SensitivityVariableType.TRANSFORMER_PHASE_2 - : SensitivityVariableType.TRANSFORMER_PHASE_3; - return new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, functionId, fVariable, variableId, false, ContingencyContext.all()); - } - - protected static SensitivityFactor createTransformerLegFlowPerInjectionIncrease(String functionId, String variableId, ThreeWindingsTransformer.Side side) { - SensitivityFunctionType ftype = side.equals(ThreeWindingsTransformer.Side.ONE) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_1 - : side.equals(ThreeWindingsTransformer.Side.TWO) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_2 - : SensitivityFunctionType.BRANCH_ACTIVE_POWER_3; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId, false, ContingencyContext.all()); - } - - protected static SensitivityFactor createBranchFlowPerPSTAngle(String functionId, String variableId) { - return createBranchFlowPerPSTAngle(functionId, variableId, null, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchIntensityPerInjectionIncrease(String functionId, String variableId, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_CURRENT_1 : SensitivityFunctionType.BRANCH_CURRENT_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId, false, ContingencyContext.all()); - } - - protected static SensitivityFactor createTransformerLegIntensityPerInjectionIncrease(String functionId, String variableId, ThreeWindingsTransformer.Side side) { - SensitivityFunctionType ftype = side.equals(ThreeWindingsTransformer.Side.ONE) ? SensitivityFunctionType.BRANCH_CURRENT_1 - : side.equals(ThreeWindingsTransformer.Side.TWO) ? SensitivityFunctionType.BRANCH_CURRENT_2 - : SensitivityFunctionType.BRANCH_CURRENT_3; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId, false, ContingencyContext.all()); - } - - protected static SensitivityFactor createBranchIntensityPerInjectionIncrease(String functionId, String variableId) { - return createBranchIntensityPerInjectionIncrease(functionId, variableId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBranchIntensityPerPSTAngle(String functionId, String variableId, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_CURRENT_1 : SensitivityFunctionType.BRANCH_CURRENT_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.TRANSFORMER_PHASE, variableId, false, ContingencyContext.all()); - } - - protected static SensitivityFactor createBranchIntensityPerPSTAngle(String functionId, String variableId) { - return createBranchIntensityPerPSTAngle(functionId, variableId, Branch.Side.ONE); - } - - protected static SensitivityFactor createBusVoltagePerTargetV(String functionId, String variableId, String contingencyId) { - return new SensitivityFactor(SensitivityFunctionType.BUS_VOLTAGE, functionId, SensitivityVariableType.BUS_TARGET_VOLTAGE, variableId, false, Objects.isNull(contingencyId) ? ContingencyContext.all() : ContingencyContext.specificContingency(contingencyId)); - } - - protected static SensitivityFactor createBusVoltagePerTargetV(String functionId, String variableId) { - return createBusVoltagePerTargetV(functionId, variableId, null); - } - - protected static SensitivityFactor createHvdcInjection(String functionId, String variableId, Branch.Side side) { - SensitivityFunctionType ftype = side.equals(Branch.Side.ONE) ? SensitivityFunctionType.BRANCH_ACTIVE_POWER_1 : SensitivityFunctionType.BRANCH_ACTIVE_POWER_2; - return new SensitivityFactor(ftype, functionId, SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, variableId, false, ContingencyContext.all()); - } - - protected void runAcLf(Network network) { - runAcLf(network, Reporter.NO_OP); - } - - protected void runAcLf(Network network, Reporter reporter) { - LoadFlowResult result = new OpenLoadFlowProvider(matrixFactory) - .run(network, LocalComputationManager.getDefault(), VariantManagerConstants.INITIAL_VARIANT_ID, new LoadFlowParameters(), reporter) - .join(); - if (!result.isOk()) { - throw new PowsyblException("AC LF diverged"); - } - } - - protected void runDcLf(Network network) { - runDcLf(network, Reporter.NO_OP); - } - - protected void runDcLf(Network network, Reporter reporter) { - LoadFlowParameters parameters = new LoadFlowParameters().setDc(true); - LoadFlowResult result = new OpenLoadFlowProvider(matrixFactory) - .run(network, LocalComputationManager.getDefault(), VariantManagerConstants.INITIAL_VARIANT_ID, parameters, reporter) - .join(); - if (!result.isOk()) { - throw new PowsyblException("DC LF failed"); - } - } - - protected void runLf(Network network, LoadFlowParameters loadFlowParameters) { - runLf(network, loadFlowParameters, Reporter.NO_OP); - } - - protected void runLf(Network network, LoadFlowParameters loadFlowParameters, Reporter reporter) { - LoadFlowResult result = new OpenLoadFlowProvider(matrixFactory) - .run(network, LocalComputationManager.getDefault(), VariantManagerConstants.INITIAL_VARIANT_ID, loadFlowParameters, reporter) - .join(); - if (!result.isOk()) { - throw new PowsyblException("LF failed"); - } - } - - protected void testInjectionNotFound(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = Collections.singletonList(createBranchFlowPerInjectionIncrease("NHV1_NHV2_1", "a")); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Injection 'a' not found", e.getCause().getMessage()); - } - - protected void testInjectionNotFoundAdditionalFactor(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = Collections.singletonList(createBranchFlowPerInjectionIncrease("NHV1_NHV2_1", "a")); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Injection 'a' not found", e.getCause().getMessage()); - } - - protected void testInjectionNotFoundAdditionalFactorContingency(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("NHV1_NHV2_1", "a")); - - List contingencies = List.of(new Contingency("a", new BranchContingency("NHV1_NHV2_2"))); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Injection 'a' not found", e.getCause().getMessage()); - } - - protected void testGlskInjectionNotFound(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = Collections.singletonList(createBranchFlowPerLinearGlsk("NHV1_NHV2_1", "glsk")); - - List contingencies = Collections.emptyList(); - - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("a", 10f)))); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Injection 'a' not found", e.getCause().getMessage()); - } - - protected void testHvdcInjectionNotFound(boolean dc) { - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "b1_vl_0", true); - - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcWithGenerators(); - - List factors = List.of(createHvdcInjection("l12", "nop", Branch.Side.ONE)); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("HVDC line 'nop' cannot be found in the network.", e.getCause().getMessage()); - } - - protected void testBranchNotFound(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("b", "GEN")); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Branch 'b' not found", e.getCause().getMessage()); - } - - protected void testEmptyFactors(boolean dc) { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "VLLOAD_0"); - - List factors = Collections.emptyList(); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertTrue(result.getValues().isEmpty()); - } - - protected void testBranchFunctionOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l56", "g1")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0d, result.getValues().iterator().next().getValue()); - } - - protected void testInjectionOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l12", "g3")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0f, result.getBranchFlow1SensitivityValue("g3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - protected void testPhaseShifterOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerPSTAngle("l12", "l45")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l45", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - if (dc) { - assertEquals(100.00, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - } else { - assertEquals(100.08, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - } - } - - protected void testGlskOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l12", "glsk")); - - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - if (dc) { - assertEquals(100.000, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - } else { - assertEquals(100.080, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - } - } - - protected void testGlskAndLineOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l56", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("glsk", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l56"), LoadFlowAssert.DELTA_POWER); - } - - protected void testGlskPartiallyOutsideMainComponent(boolean dc) { - Network network = HvdcNetworkFactory.createLccWithBiggerComponents(); - - SensitivityAnalysisParameters sensiParameters = createParameters(dc, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l12", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("ld2", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(1, result.getValues().size()); - - List factorsInjection = List.of(createBranchFlowPerInjectionIncrease("l12", "ld2")); - - SensitivityAnalysisResult resultInjection = sensiRunner.run(network, factorsInjection, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(resultInjection.getValues().iterator().next().getValue(), result.getValues().iterator().next().getValue(), LoadFlowAssert.DELTA_POWER); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisParametersTest.java b/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisParametersTest.java deleted file mode 100644 index 01e4911660..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisParametersTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * 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.openloadflow.sensi; - -import com.google.common.jimfs.Configuration; -import com.google.common.jimfs.Jimfs; -import com.powsybl.commons.config.InMemoryPlatformConfig; -import com.powsybl.commons.config.MapModuleConfig; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.nio.file.FileSystem; - -/** - * @author Etienne Lesot - */ -class OpenSensitivityAnalysisParametersTest { - - private InMemoryPlatformConfig platformConfig; - - private FileSystem fileSystem; - - @BeforeEach - public void setUp() { - fileSystem = Jimfs.newFileSystem(Configuration.unix()); - platformConfig = new InMemoryPlatformConfig(fileSystem); - - MapModuleConfig lfModuleConfig = platformConfig.createModuleConfig("open-sensitivityanalysis-default-parameters"); - lfModuleConfig.setStringProperty("debugDir", "/debugDir"); - } - - @AfterEach - void tearDown() throws IOException { - fileSystem.close(); - } - - @Test - void test() { - OpenSensitivityAnalysisParameters parameters = OpenSensitivityAnalysisParameters.load(platformConfig); - Assertions.assertEquals("/debugDir", parameters.getDebugDir()); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProviderTest.java b/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProviderTest.java deleted file mode 100644 index 766bf90486..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProviderTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (c) 2020, 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.openloadflow.sensi; - -import com.powsybl.math.matrix.DenseMatrixFactory; -import com.powsybl.openloadflow.util.ProviderConstants; -import com.powsybl.sensitivity.SensitivityAnalysisParameters; -import com.powsybl.tools.PowsyblCoreVersion; -import org.junit.jupiter.api.Test; - -import java.util.Collections; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -/** - * @author Geoffroy Jamgotchian - */ -class OpenSensitivityAnalysisProviderTest extends AbstractSensitivityAnalysisTest { - - @Test - void testGeneralInfos() { - OpenSensitivityAnalysisProvider provider = new OpenSensitivityAnalysisProvider(new DenseMatrixFactory()); - assertEquals(ProviderConstants.NAME, provider.getName()); - assertEquals(new PowsyblCoreVersion().getMavenProjectVersion(), provider.getVersion()); - assertEquals(ProviderConstants.NAME, provider.getLoadFlowProviderName().orElseThrow()); - } - - @Test - void specificParametersTest() { - var provider = new OpenSensitivityAnalysisProvider(); - assertEquals(1, provider.getSpecificParametersNames().size()); - SensitivityAnalysisParameters parameters = new SensitivityAnalysisParameters(); - - provider.loadSpecificParameters(Collections.emptyMap()) - .ifPresent(parametersExt -> parameters.addExtension((Class) parametersExt.getClass(), parametersExt)); - assertNull(parameters.getExtension(OpenSensitivityAnalysisParameters.class).getDebugDir()); - - provider.loadSpecificParameters(Map.of(OpenSensitivityAnalysisParameters.DEBUG_DIR_PARAM_NAME, "")) - .ifPresent(parametersExt -> parameters.addExtension((Class) parametersExt.getClass(), parametersExt)); - assertEquals("", parameters.getExtension(OpenSensitivityAnalysisParameters.class).getDebugDir()); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java b/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java deleted file mode 100644 index 656eb9d276..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java +++ /dev/null @@ -1,1184 +0,0 @@ -/** - * Copyright (c) 2020, 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.openloadflow.sensi.ac; - -import com.powsybl.commons.PowsyblException; -import com.powsybl.commons.reporter.Reporter; -import com.powsybl.contingency.*; -import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControlAdder; -import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.OpenLoadFlowParameters; -import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.util.LoadFlowAssert; -import com.powsybl.sensitivity.*; -import org.junit.jupiter.api.Test; - -import java.util.*; -import java.util.concurrent.CompletionException; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Geoffroy Jamgotchian - */ -class AcSensitivityAnalysisContingenciesTest extends AbstractSensitivityAnalysisTest { - - @Test - void test4BusesSensi() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l23"); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(-0.5409d, result.getBranchFlow1SensitivityValue("l23", "g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.4000d, result.getBranchFlow1SensitivityValue("l23", "g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.4000d, result.getBranchFlow1SensitivityValue("l23", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.6000d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4BusesSensiAdditionalFactor() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues("l23").size()); - - assertEquals(-0.5409d, result.getBranchFlow1SensitivityValue("l23", "g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.4000d, result.getBranchFlow1SensitivityValue("l23", "g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.4000d, result.getBranchFlow1SensitivityValue("l23", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.6000d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4BusesFunctionReference() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = Collections.singletonList(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0.6761d, result.getBranchFlow1FunctionReferenceValue("l23", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.0d, result.getBranchFlow1FunctionReferenceValue("l23", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l23", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.676d, result.getBranchFlow1FunctionReferenceValue("l23", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(2.324d, result.getBranchFlow1FunctionReferenceValue("l23", "l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesLoosingATransfo() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l23"); - - List contingencies = Collections.singletonList(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(-0.5409d, result.getBranchFlow1SensitivityValue("l23", "g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.400d, result.getBranchFlow1SensitivityValue("l23", "g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2591d, result.getBranchFlow1SensitivityValue("l23", "g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.4000d, result.getBranchFlow1SensitivityValue("l23", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.600d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesLoosingATransfoFunctionRef() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l23"); - - List contingencies = Collections.singletonList(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(0.6761d, result.getBranchFlow1FunctionReferenceValue("l23", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.0d, result.getBranchFlow1FunctionReferenceValue("l23", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l23", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.676d, result.getBranchFlow1FunctionReferenceValue("l23", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(2.324d, result.getBranchFlow1FunctionReferenceValue("l23", "l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesPhaseShift() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23", "l34", Branch.Side.ONE)).collect(Collectors.toList()); - - List contingencies = Collections.singletonList(new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0564d, result.getBranchFlow1SensitivityValue("l34", "l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.0564d, result.getBranchFlow1SensitivityValue("l34", "l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0564d, result.getBranchFlow1SensitivityValue("l34", "l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesFunctionReferenceWithTransformer() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23", "l34", Branch.Side.ONE)).collect(Collectors.toList()); - - List contingencies = Collections.singletonList(new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(-1.0d, result.getBranchFlow1FunctionReferenceValue("l34", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.7319d, result.getBranchFlow1FunctionReferenceValue("l34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l34", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(2.268d, result.getBranchFlow1FunctionReferenceValue("l34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.7319d, result.getBranchFlow1FunctionReferenceValue("l34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesFunctionReferenceWithTransformerInAdditionalFactors() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - List contingencies = Collections.singletonList(new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues("l34").size()); - assertEquals(-1.0d, result.getBranchFlow1FunctionReferenceValue("l34", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.7319d, result.getBranchFlow1FunctionReferenceValue("l34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l34", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(2.268d, result.getBranchFlow1FunctionReferenceValue("l34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.7319d, result.getBranchFlow1FunctionReferenceValue("l34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testMultipleContingencies() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23")), - new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.600d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0648d, result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("l34", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.4056d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1944d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1944d, result.getBranchFlow1SensitivityValue("l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testMultipleContingenciesAdditionalFactors() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l14", "g2"), - createBranchFlowPerInjectionIncrease("l23", "g2", "l23"), - createBranchFlowPerInjectionIncrease("l12", "g2", "l23"), - createBranchFlowPerInjectionIncrease("l23", "g2", "l34"), - createBranchFlowPerInjectionIncrease("l34", "g2", "l34"), - createBranchFlowPerInjectionIncrease("l12", "g2", "l34")); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23")), - new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getValues("l34").size()); - assertEquals(3, result.getValues("l23").size()); - - assertEquals(0.1352d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.600d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("l34", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.4056d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1944d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyWithMultipleBranches() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("l23+l34", new BranchContingency("l23"), new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.600d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConnectivityLossOnSingleLine() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(14, result.getValues("l34").size()); - assertEquals(-0.1324d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2676d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1324d, result.getBranchFlow1SensitivityValue("l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1986d, result.getBranchFlow1SensitivityValue("l34", "g3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.4013d, result.getBranchFlow1SensitivityValue("l34", "g3", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1986d, result.getBranchFlow1SensitivityValue("l34", "g3", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g3", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g3", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g3", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g3", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-1.662, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.066, result.getBranchFlow1FunctionReferenceValue("l34", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyMultipleLinesBreaksOneContingency() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLinesWithAdditionnalGens(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l24+l35", new BranchContingency("l24"), new BranchContingency("l35"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l24+l35"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(24, result.getValues("l24+l35").size()); - assertEquals(-0.1331d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2669d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1331d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1997d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.4003d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1997d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g3", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskRescale() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List variables = List.of(new WeightedSensitivityVariable("g2", 0.4f), - new WeightedSensitivityVariable("g6", 0.6f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream() - .map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk", "l34", Branch.Side.ONE)) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskRescaleAdditionalFactor() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List variables = List.of(new WeightedSensitivityVariable("g2", 0.4f), - new WeightedSensitivityVariable("g6", 0.6f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream() - .map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk", "l34", Branch.Side.ONE)) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testInjectionNotFoundAdditionalFactorContingency() { - testInjectionNotFoundAdditionalFactorContingency(false); - } - - @Test - void testBusVoltagePerTargetV() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = Collections.singletonList(new Contingency("l45", new BranchContingency("l45"))); - - List busIds = new ArrayList<>(10); - for (int i = 1; i <= 10; i++) { - busIds.add("b" + i); - } - List factors = busIds.stream() - .map(bus -> createBusVoltagePerTargetV(bus, "g2")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0.916d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(0), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // 0 on the slack - assertEquals(1d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(1), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // 1 on itself - assertEquals(0.8133d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(2), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.512d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(3), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(4), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(0d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(5), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(0d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(6), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(0.209d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(7), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.1062d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(8), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result.getBusVoltageSensitivityValue("l45", "g2", busIds.get(9), SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // no impact on a pv - } - - @Test - void testBusVoltagePerTargetVFunctionRef() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = Collections.singletonList(new Contingency("l45", new BranchContingency("l45"))); - - List busIds = new ArrayList<>(10); - for (int i = 1; i <= 10; i++) { - busIds.add("b" + i); - } - List factors = busIds.stream() - .map(bus -> createBusVoltagePerTargetV(bus, "g2")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0.993d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(0)), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(1)), LoadFlowAssert.DELTA_V); - assertEquals(0.992d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(2)), LoadFlowAssert.DELTA_V); - assertEquals(0.988d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(3)), LoadFlowAssert.DELTA_V); - assertEquals(Double.NaN, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(4)), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(Double.NaN, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(5)), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(Double.NaN, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(6)), LoadFlowAssert.DELTA_V); // disconnected - assertEquals(0.987d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(7)), LoadFlowAssert.DELTA_V); - assertEquals(0.989d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(8)), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageFunctionReferenceValue("l45", busIds.get(9)), LoadFlowAssert.DELTA_V); - } - - @Test - void testHvdcSensiRescale() { - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusSelectionMode(SlackBusSelectionMode.MOST_MESHED); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators(); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network1.getLine("l25").getTerminal1().disconnect(); - network1.getLine("l25").getTerminal2().disconnect(); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Network network2 = HvdcNetworkFactory.createNetworkWithGenerators(); - network2.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - network2.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network2.getLine("l25").getTerminal1().disconnect(); - network2.getLine("l25").getTerminal2().disconnect(); - runLf(network2, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(line -> line.getId()) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network2.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class) - .setSlackBusSelectionMode(SlackBusSelectionMode.NAME) - .setSlackBusId("b1_vl_0"); // the most meshed bus selected in the loadflow - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.singletonList(new Contingency("l25", new BranchContingency("l25"))), Collections.emptyList(), sensiParameters); - - // FIXME -// assertEquals(loadFlowDiff.get("l12"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l12"), "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); -// assertEquals(loadFlowDiff.get("l13"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l13"), "l25"), LoadFlowAssert.DELTA_POWER); -// assertEquals(loadFlowDiff.get("l23"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l23"), "l25"), LoadFlowAssert.DELTA_POWER); -// assertEquals(0d, hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l25"), "l25"), LoadFlowAssert.DELTA_POWER); -// assertEquals(loadFlowDiff.get("l45"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l45"), "l25"), LoadFlowAssert.DELTA_POWER); -// assertEquals(loadFlowDiff.get("l46"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l46"), "l25"), LoadFlowAssert.DELTA_POWER); -// assertEquals(loadFlowDiff.get("l56"), hvdcWriter.getBranchFlow1SensitivityValue(Pair.of("hvdc34", "l56"), "l25"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyPropagationLfSwitch() { - SensitivityAnalysisParameters sensiParameters = createParameters(false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = NodeBreakerNetworkFactory.create3Bars(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("L2", new BranchContingency("L2"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(200.794, result.getBranchFlow1FunctionReferenceValue("L1"), LoadFlowAssert.DELTA_POWER); - assertEquals(200.794, result.getBranchFlow1FunctionReferenceValue("L2"), LoadFlowAssert.DELTA_POWER); - assertEquals(200.794, result.getBranchFlow1FunctionReferenceValue("L3"), LoadFlowAssert.DELTA_POWER); - - // Propagating contingency on L2 encounters a coupler, which is not (yet) supported in sensitivity analysis - assertEquals(301.864, result.getBranchFlow1FunctionReferenceValue("L2", "L1"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("L2", "L2"), LoadFlowAssert.DELTA_POWER); - assertEquals(301.864, result.getBranchFlow1FunctionReferenceValue("L2", "L3"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testDanglingLineContingencyDistributedSlackOnLoads() { - Network network = BoundaryFactory.createWithLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl3_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "g1")); - - List contingencies = List.of(new Contingency("dl1", new DanglingLineContingency("dl1"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(0.3697, result.getBranchFlow1SensitivityValue("g1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - assertEquals(75.272, result.getBranchFlow1FunctionReferenceValue("l1"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3695, result.getBranchFlow1SensitivityValue("dl1", "g1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - assertEquals(36.794, result.getBranchFlow1FunctionReferenceValue("dl1", "l1"), LoadFlowAssert.DELTA_POWER); - - network.getDanglingLine("dl1").getTerminal().disconnect(); - - LoadFlowParameters parameters = sensiParameters.getLoadFlowParameters(); - parameters.getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - runLf(network, parameters, Reporter.NO_OP); - - Line l1 = network.getLine("l1"); - double initialP = l1.getTerminal1().getP(); - assertEquals(36.795, initialP, LoadFlowAssert.DELTA_POWER); - - network.getGenerator("g1").setTargetP(network.getGenerator("g1").getTargetP() + 1); - - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - - double finalP = l1.getTerminal1().getP(); - assertEquals(37.164, finalP, LoadFlowAssert.DELTA_POWER); - assertEquals(0.3695, finalP - initialP, LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - } - - @Test - void testDanglingLineContingencyDistributedSlackOnGenerators() { - Network network = BoundaryFactory.createWithLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "load3")); - - List contingencies = List.of(new Contingency("dl1", new DanglingLineContingency("dl1"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(-0.3704, result.getBranchFlow1SensitivityValue("load3", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - assertEquals(75.336, result.getBranchFlow1FunctionReferenceValue("l1"), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.3704, result.getBranchFlow1SensitivityValue("dl1", "load3", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - assertEquals(3.0071, result.getBranchFlow1FunctionReferenceValue("dl1", "l1"), LoadFlowAssert.DELTA_POWER); - - network.getDanglingLine("dl1").getTerminal().disconnect(); - - LoadFlowParameters parameters = sensiParameters.getLoadFlowParameters(); - parameters.getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - runLf(network, parameters, Reporter.NO_OP); - - Line l1 = network.getLine("l1"); - double initialP = l1.getTerminal1().getP(); - assertEquals(3.0071, initialP, LoadFlowAssert.DELTA_POWER); - - network.getLoad("load3").setP0(network.getLoad("load3").getP0() + 1); - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - - double finalP = l1.getTerminal1().getP(); - assertEquals(3.3775, finalP, LoadFlowAssert.DELTA_POWER); - assertEquals(-0.3704, initialP - finalP, LoadFlowAssert.DELTA_SENSITIVITY_VALUE); - } - - @Test - void testEurostagFactory() { - Network network = EurostagFactory.fix(EurostagTutorialExample1Factory.create()); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLGEN_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "NHV2_NLOAD")) - .collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("NHV1_NHV2_2", new BranchContingency("NHV1_NHV2_2"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getPreContingencyValues().size()); - assertEquals(0d, result.getBusVoltageSensitivityValue("NHV2_NLOAD", "NGEN", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.035205d, result.getBusVoltageSensitivityValue("NHV2_NLOAD", "NHV1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.077194d, result.getBusVoltageSensitivityValue("NHV2_NLOAD", "NHV2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1.0d, result.getBusVoltageSensitivityValue("NHV2_NLOAD", "NLOAD", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - - assertEquals(0d, result.getBusVoltageSensitivityValue("NHV1_NHV2_2", "NHV2_NLOAD", "NGEN", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.026329d, result.getBusVoltageSensitivityValue("NHV1_NHV2_2", "NHV2_NLOAD", "NHV1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.103981d, result.getBusVoltageSensitivityValue("NHV1_NHV2_2", "NHV2_NLOAD", "NHV2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1.0d, result.getBusVoltageSensitivityValue("NHV1_NHV2_2", "NHV2_NLOAD", "NLOAD", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testGlskOutsideMainComponentWithContingency() { - Network network = HvdcNetworkFactory.createLccWithBiggerComponentsAndAdditionalLine(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l12", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - List contingencies = List.of(new Contingency("additionnalline_0", new BranchContingency("additionnalline_0"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(100.080, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(100.080, result.getBranchFlow1FunctionReferenceValue("additionnalline_0", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesPhaseShiftIntensityFunctionReference() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBranchStream().map(branch -> createBranchIntensityPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(Double.NaN, result.getFunctionReferenceValue("l23", "l23", SensitivityFunctionType.BRANCH_CURRENT_1), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0, result.getBranchCurrent1SensitivityValue("l23", "l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLosingALineButBothEndsInMainComponent() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b3_vl_0", false); - - List contingencies = List.of(new Contingency("l34+l12", new BranchContingency("l34"), new BranchContingency("l12"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g3")), - List.of(network.getLine("l12")), - "l34+l12"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues("l34+l12").size()); - - assertEquals(0.0, result.getBranchFlow1SensitivityValue("l34+l12", "g3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l34+l12", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testTrivialContingencyOnGenerator() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1", true); - sensiParameters.setLoadFlowParameters(new LoadFlowParameters() - .setDc(false) - .setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX)); - - List factors = createFactorMatrix(Stream.of("g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("g6", new GeneratorContingency("g6"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(3, result.getValues("g6").size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-1.3365d, result.getBranchFlow1FunctionReferenceValue("g6", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3364d, result.getBranchFlow1FunctionReferenceValue("g6", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.6664, result.getBranchFlow1FunctionReferenceValue("g6", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnLoad() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1", false); - sensiParameters.setLoadFlowParameters(new LoadFlowParameters() - .setDc(false) - .setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); - - List factors = createFactorMatrix(Stream.of("g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("d5", new LoadContingency("d5"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - network.getLoad("d5").getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(3, result.getValues("d5").size()); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d5", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d5", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d5", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("d5", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("d5", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("d5", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGLSK() { - Network network = FourBusNetworkFactory.create(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("g1", new GeneratorContingency("g1"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - network.getGenerator("g1").getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l14"), result.getBranchFlow1FunctionReferenceValue("g1", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("g1", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("g1", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("g1", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("g1", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("g1", "l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnBranch() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1", true); - sensiParameters.setLoadFlowParameters(new LoadFlowParameters() - .setDc(false) - .setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); - - List factors = createFactorMatrix(Stream.of("g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(SensitivityAnalysisResult.Status.SUCCESS, result.getContingencyStatus("l23")); - - network.getLine("l23").getTerminal1().disconnect(); - network.getLine("l23").getTerminal2().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("l23", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("l23", "l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLcc() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(3 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect(); - network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - List contingencyResult = result.getValues("hvdc34"); - assertEquals(6, contingencyResult.size()); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcVsc() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect(); - network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - List contingencyResult = result.getValues("hvdc34"); - assertEquals(6, contingencyResult.size()); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcVsc2() { - Network network = HvdcNetworkFactory.createWithHvdcInAcEmulation(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network.getHvdcLine("hvdc34").newExtension(HvdcAngleDroopActivePowerControlAdder.class) - .withDroop(180) - .withP0(0.f) - .withEnabled(true) - .add(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - - List factors = createFactorMatrix(Stream.of("g1", "g5").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l25", "l56").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect(); - network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - List contingencyResult = result.getValues("hvdc34"); - assertEquals(6, contingencyResult.size()); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l25"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l25"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l56"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testVoltageSensitivityConnectivityLoss() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34")), new Contingency("l13+l23", new BranchContingency("l13"), new BranchContingency("l23"))); - - List factors = List.of(createBusVoltagePerTargetV("b4", "g3", "l34"), createBusVoltagePerTargetV("b1", "g3", "l13+l23"), createBusVoltagePerTargetV("b4", "g3", "l13+l23")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(3, result.getValues().size()); - - assertEquals(0.0, result.getBusVoltageSensitivityValue("l34", "g3", "b4", SensitivityVariableType.BUS_TARGET_VOLTAGE)); - assertEquals(Double.NaN, result.getBusVoltageFunctionReferenceValue("l34", "b4")); - assertEquals(0.0, result.getBusVoltageSensitivityValue("l13+l23", "g3", "b1", SensitivityVariableType.BUS_TARGET_VOLTAGE)); - assertEquals(0.9798, result.getBusVoltageFunctionReferenceValue("l13+l23", "b1"), LoadFlowAssert.DELTA_V); - assertEquals(Double.NaN, result.getBusVoltageSensitivityValue("l13+l23", "g3", "b4", SensitivityVariableType.BUS_TARGET_VOLTAGE)); - assertEquals(Double.NaN, result.getBusVoltageFunctionReferenceValue("l13+l23", "b4")); - } - - @Test - void testContingencyWithDisconnectedBranch() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l45", new BranchContingency("l45"))); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l46", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - // different sensitivity for (g2, l46) on base case and after contingency l45 - assertEquals(0.0667d, result.getBranchFlow1SensitivityValue("g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1452d, result.getBranchFlow1SensitivityValue("l45", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - // we open l45 at both sides - Line l45 = network.getLine("l45"); - l45.getTerminal1().disconnect(); - l45.getTerminal2().disconnect(); - - result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - // we now have as expected the sensitivity for (g2, l46) on base case and after contingency l45 - // because l45 is already open on base case - assertEquals(0.1452d, result.getBranchFlow1SensitivityValue("g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1452d, result.getBranchFlow1SensitivityValue("l45", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(SensitivityAnalysisResult.Status.NO_IMPACT, result.getContingencyStatus("l45")); - } - - @Test - void testSwitchContingency() { - Network network = NodeBreakerNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VL1_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("L1", "LD")); - - List contingencies = List.of(new Contingency("C", new SwitchContingency("C"))); - - List variableSets = Collections.emptyList(); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - assertEquals(-0.506, result.getBranchFlow1SensitivityValue("LD", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.000, result.getBranchFlow1SensitivityValue("C", "LD", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(301.884, result.getBranchFlow1FunctionReferenceValue("L1"), LoadFlowAssert.DELTA_POWER); - assertEquals(3.912, result.getBranchFlow1FunctionReferenceValue("C", "L1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testSwitchContingency2() { - Network network = NodeBreakerNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VL1_0"); - - List factors = List.of(createBusVoltagePerTargetV("BBS3", "G")); - - List contingencies = List.of(new Contingency("C", new SwitchContingency("C"))); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Switch contingency is not yet supported with sensitivity function of type BUS_VOLTAGE", e.getCause().getMessage()); - } - - @Test - void testNoImpactContingencyAfterNormalContingency() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - // we open l45 at both sides - Line l13 = network.getLine("l13"); - l13.getTerminal1().disconnect(); - l13.getTerminal2().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("lines", List.of(new BranchContingency("l46"), new BranchContingency("l56"))), - new Contingency("l13", new BranchContingency("l13"))); - - ContingencyContext contingencyContext = new ContingencyContext("l13", ContingencyContextType.SPECIFIC); - SensitivityFactor factor = new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, "l46", - SensitivityVariableType.INJECTION_ACTIVE_POWER, - "d1", false, - contingencyContext); - List factors = List.of(factor); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0.0735, result.getBranchFlow1SensitivityValue("l13", "d1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(SensitivityAnalysisResult.Status.NO_IMPACT, result.getContingencyStatus("l13")); - } - - @Test - void testMaxIterationReachedAfterContingency() { - Network network = EurostagTutorialExample1Factory.create(); - network.getLine("NHV1_NHV2_1").setX(1000); - List contingencies = List.of(new Contingency("NHV1_NHV2_2", List.of(new BranchContingency("NHV1_NHV2_2")))); - List factors = List.of(createBranchFlowPerInjectionIncrease("NHV1_NHV2_1", "LOAD")); - SensitivityAnalysisParameters parameters = new SensitivityAnalysisParameters(); - parameters.getLoadFlowParameters().setDistributedSlack(false); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), parameters); - assertEquals(SensitivityAnalysisResult.Status.FAILURE, result.getContingencyStatus("NHV1_NHV2_2")); - } - - @Test - void testPredefinedResults() { - // Load and generator in contingency - Network network = FourBusNetworkFactory.create(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - List factors = List.of(createBranchFlowPerInjectionIncrease("l14", "g1"), - createBranchFlowPerInjectionIncrease("l14", "d2")); - List contingencies = List.of(new Contingency("g1", new GeneratorContingency("g1")), new Contingency("d2", new LoadContingency("d2"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBranchFlow1SensitivityValue("g1", "g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("d2", "d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testPredefinedResults2() { - // LCC line in contingency - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(3 * gen.getMaxP())); - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - // VSC line in contingency - Network network2 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - SensitivityAnalysisResult result2 = sensiRunner.run(network2, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result2.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testPredefinedResults3() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - List contingencies = List.of(new Contingency("g2", new GeneratorContingency("g2"))); - List factors = List.of(createBusVoltagePerTargetV("b4", "g2", "g2")); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBusVoltageSensitivityValue("g2", "g2", "b4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testPredefinedResults4() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - List factors = List.of(createBranchFlowPerPSTAngle("l12", "l23", "l23"), - createBranchFlowPerPSTAngle("l23", "l23", "l23")); - List contingencies = Collections.singletonList(new Contingency("l23", new BranchContingency("l23"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBranchFlow1SensitivityValue("l23", "l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l23", "l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java b/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java deleted file mode 100644 index a060f3dd3b..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright (c) 2021, 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.openloadflow.sensi.ac; - -import com.google.common.io.ByteStreams; -import com.powsybl.commons.reporter.ReporterModel; -import com.powsybl.computation.local.LocalComputationManager; -import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.VariantManagerConstants; -import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.network.EurostagFactory; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.sensitivity.SensitivityAnalysisParameters; -import com.powsybl.sensitivity.SensitivityFactor; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author Florian Dupuy - */ -class AcSensitivityAnalysisReportTest extends AbstractSensitivityAnalysisTest { - - protected static String normalizeLineSeparator(String str) { - return str.replace("\r\n", "\n").replace("\r", "\n"); - } - - @Test - void testEsgTuto() throws IOException { - Network network = EurostagFactory.fix(EurostagTutorialExample1Factory.create()); - ReporterModel reporter = new ReporterModel("testEsgTutoReport", "Test ESG tutorial report"); - runAcLf(network, reporter); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLLOAD_0"); - sensiParameters.getLoadFlowParameters().setVoltageInitMode(LoadFlowParameters.VoltageInitMode.PREVIOUS_VALUES); - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getLineStream().collect(Collectors.toList())); - sensiRunner.run(network, VariantManagerConstants.INITIAL_VARIANT_ID, factors, Collections.emptyList(), Collections.emptyList(), - sensiParameters, LocalComputationManager.getDefault(), reporter); - - StringWriter sw = new StringWriter(); - reporter.export(sw); - - InputStream refStream = getClass().getResourceAsStream("/esgTutoReport.txt"); - String refLogExport = normalizeLineSeparator(new String(ByteStreams.toByteArray(refStream), StandardCharsets.UTF_8)); - String logExport = normalizeLineSeparator(sw.toString()); - assertEquals(refLogExport, logExport); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java deleted file mode 100644 index 1c69ea332e..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java +++ /dev/null @@ -1,1215 +0,0 @@ -/** - * Copyright (c) 2020, 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.openloadflow.sensi.ac; - -import com.powsybl.commons.PowsyblException; -import com.powsybl.contingency.Contingency; -import com.powsybl.contingency.ContingencyContext; -import com.powsybl.contingency.DanglingLineContingency; -import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControlAdder; -import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; -import com.powsybl.loadflow.LoadFlow; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.OpenLoadFlowParameters; -import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.util.LoadFlowAssert; -import com.powsybl.sensitivity.*; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletionException; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Geoffroy Jamgotchian - */ -class AcSensitivityAnalysisTest extends AbstractSensitivityAnalysisTest { - - @Test - void testEsgTuto() { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLLOAD_0"); - sensiParameters.getLoadFlowParameters().setVoltageInitMode(LoadFlowParameters.VoltageInitMode.PREVIOUS_VALUES); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getLineStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0.498d, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.498d, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4buses() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - // this network has no G or B, so we should be very close to DC results - Network network = FourBusNetworkFactory.createBaseNetwork(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(List.of(network.getGenerator("g4")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - - assertEquals(-0.632d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.122d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.122d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.368d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.245d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesDistributed() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - // this network has no G or B, so we should be very close to DC results - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - assertEquals(-0.453d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.152d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.248d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.347d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.099d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.175d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.276, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.123d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.024d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.147d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.051d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.352d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.247d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.149d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.099d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesDistributedSide2() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - // this network has no G or B, so we should be very close to DC results - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), null, Branch.Side.TWO); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - assertEquals(0.45328d, result.getBranchFlow2SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1518d, result.getBranchFlow2SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.24819d, result.getBranchFlow2SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3467d, result.getBranchFlow2SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0986d, result.getBranchFlow2SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.1757d, result.getBranchFlow2SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2765, result.getBranchFlow2SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1235d, result.getBranchFlow2SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.02434d, result.getBranchFlow2SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1478d, result.getBranchFlow2SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.25116d, result.getBranchFlow2FunctionReferenceValue("l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25116d, result.getBranchFlow2FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.2510d, result.getBranchFlow2FunctionReferenceValue("l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.2510d, result.getBranchFlow2FunctionReferenceValue("l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.4975d, result.getBranchFlow2FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesGlsk() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - // this network has no G or B, so we should be very close to DC results - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List variables = List.of(new WeightedSensitivityVariable("g1", 0.25f), - new WeightedSensitivityVariable("g4", 0.25f), - new WeightedSensitivityVariable("d2", 0.5f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream() - .map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(5, result.getValues().size()); - - assertEquals(-0.044d, result.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.069d, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.031d, result.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.006d, result.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.037d, result.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesWithTransfoInjection() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - assertEquals(-0.453d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.151d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.248d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.346d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.098d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.175d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.276d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.123d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.024d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.147d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.051d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.352d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.247d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.149d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.098d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesPhaseShift() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - - assertEquals(-0.0217d, result.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0647d, result.getBranchFlow1SensitivityValue("l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0217d, result.getBranchFlow1SensitivityValue("l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.0429d, result.getBranchFlow1SensitivityValue("l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0647d, result.getBranchFlow1SensitivityValue("l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesOpenPhaseShifterOnPower() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - network.getBranch("l23").getTerminal1().disconnect(); - - List factors = List.of(createBranchFlowPerPSTAngle("l14", "l23")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_ANGLE); - - network.getBranch("l23").getTerminal1().connect(); - network.getBranch("l23").getTerminal2().disconnect(); - - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result2.getValues().size()); - assertEquals(0, result2.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_ANGLE); - } - - @Test - void test4busesOpenPhaseShifterOnCurrent() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - network.getBranch("l23").getTerminal1().disconnect(); - - List factors = List.of(createBranchFlowPerPSTAngle("l14", "l23")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_ANGLE); - - network.getBranch("l23").getTerminal1().connect(); - network.getBranch("l23").getTerminal2().disconnect(); - - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result2.getValues().size()); - assertEquals(0, result2.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_ANGLE); - } - - @Test - void test4busesFunctionReference() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.create(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - assertEquals(0.2512d, result.getBranchFlow1FunctionReferenceValue("l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2512d, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.2512d, result.getBranchFlow1FunctionReferenceValue("l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.2512d, result.getBranchFlow1FunctionReferenceValue("l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.4976d, result.getBranchFlow1FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesFunctionReferenceWithTransformer() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - sensiParameters.getLoadFlowParameters().setVoltageInitMode(LoadFlowParameters.VoltageInitMode.DC_VALUES); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - - assertEquals(0.2296d, result.getBranchFlow1FunctionReferenceValue("l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3154d, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.2296d, result.getBranchFlow1FunctionReferenceValue("l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.4549d, result.getBranchFlow1FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.3154d, result.getBranchFlow1FunctionReferenceValue("l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesPhaseShiftIntensity() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factorsSide1 = network.getBranchStream().map(branch -> createBranchIntensityPerPSTAngle(branch.getId(), "l23", Branch.Side.ONE)).collect(Collectors.toList()); - List factorsSide2 = network.getBranchStream().map(branch -> createBranchIntensityPerPSTAngle(branch.getId(), "l23", Branch.Side.TWO)).collect(Collectors.toList()); - - List factors = new ArrayList<>(); - factors.addAll(factorsSide1); - factors.addAll(factorsSide2); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(10, result.getValues().size()); - - //Check values for side 1 using generic and function type specific api - assertEquals(37.6799d, result.getSensitivityValue("l23", "l23", SensitivityFunctionType.BRANCH_CURRENT_1, SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-12.5507d, result.getBranchCurrent1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(37.3710d, result.getBranchCurrent1SensitivityValue("l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-12.6565d, result.getBranchCurrent1SensitivityValue("l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-25.0905d, result.getBranchCurrent1SensitivityValue("l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - - //Check values for side 2 using generic and function type specific api - assertEquals(-161.8233d, result.getSensitivityValue("l23", "l23", SensitivityFunctionType.BRANCH_CURRENT_2, SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-12.5509d, result.getBranchCurrent2SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(37.3727d, result.getBranchCurrent2SensitivityValue("l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-12.6567d, result.getBranchCurrent2SensitivityValue("l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - assertEquals(-25.0917d, result.getBranchCurrent2SensitivityValue("l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_I); - } - - @Test - void test4busesPhaseShiftIntensityFunctionReference() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBranchStream().map(branch -> createBranchIntensityPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(766.4654d, result.getBranchCurrent1FunctionReferenceValue("l23"), LoadFlowAssert.DELTA_I); - assertEquals(132.5631d, result.getBranchCurrent1FunctionReferenceValue("l14"), LoadFlowAssert.DELTA_I); - assertEquals(182.1272d, result.getBranchCurrent1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_I); - assertEquals(716.5036d, result.getBranchCurrent1FunctionReferenceValue("l34"), LoadFlowAssert.DELTA_I); - assertEquals(847.8542d, result.getBranchCurrent1FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_I); - } - - @Test - void testBusVoltagePerTargetVRemoteControl() { - Network network = VoltageControlNetworkFactory.createWithGeneratorRemoteControl(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "g1")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getValues().size()); - assertEquals(0.04997d, result.getBusVoltageSensitivityValue("g1", "b1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.0507d, result.getBusVoltageSensitivityValue("g1", "b2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.0525d, result.getBusVoltageSensitivityValue("g1", "b3", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageSensitivityValue("g1", "b4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testBusVoltagePerTargetV() { - Network network = FourBusNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "g2")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getValues().size()); - assertEquals(0d, result.getBusVoltageSensitivityValue("g2", "b1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // no impact on a pv - assertEquals(1d, result.getBusVoltageSensitivityValue("g2", "b2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // 1 on itself - assertEquals(0.3423d, result.getBusVoltageSensitivityValue("g2", "b3", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); // value obtained by running two loadflow with a very small difference on targetV for bus2 - assertEquals(0d, result.getBusVoltageSensitivityValue("g2", "b4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testBusVoltagePerTargetVTwt() { - Network network = VoltageControlNetworkFactory.createNetworkWithT2wt(); - - Substation substation4 = network.newSubstation() - .setId("SUBSTATION4") - .setCountry(Country.FR) - .add(); - VoltageLevel vl4 = substation4.newVoltageLevel() - .setId("VL_4") - .setNominalV(33.0) - .setLowVoltageLimit(0.0) - .setHighVoltageLimit(100.0) - .setTopologyKind(TopologyKind.BUS_BREAKER) - .add(); - vl4.getBusBreakerView().newBus() - .setId("BUS_4") - .add(); - vl4.newLoad() - .setId("LOAD_4") - .setBus("BUS_4") - .setQ0(0) - .setP0(10) - .add(); - network.newLine() - .setId("LINE_34") - .setVoltageLevel1("VL_3") - .setVoltageLevel2("VL_4") - .setBus1("BUS_3") - .setBus2("BUS_4") - .setR(1.05) - .setX(10.0) - .setG1(0.0000005) - .setG2(0.) - .setB1(0.) - .setB2(0.) - .add(); - - TwoWindingsTransformer t2wt = network.getTwoWindingsTransformer("T2wT"); - t2wt.getRatioTapChanger() - .setTargetDeadband(0) - .setRegulating(true) - .setTapPosition(0) - .setRegulationTerminal(t2wt.getTerminal2()) - .setTargetV(34.0); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VL_1_0", true); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "T2wT")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getValues().size()); - assertEquals(0d, result.getBusVoltageSensitivityValue("T2wT", "BUS_1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0.035205d, result.getBusVoltageSensitivityValue("T2wT", "BUS_2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageSensitivityValue("T2wT", "BUS_3", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1.055117d, result.getBusVoltageSensitivityValue("T2wT", "BUS_4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - - t2wt.getRatioTapChanger() - .setTargetDeadband(0) - .setRegulating(true) - .setTapPosition(3) - .setRegulationTerminal(t2wt.getTerminal1()) // control will be disabled. - .setTargetV(135.0); - - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(4, result2.getValues().size()); - assertEquals(0d, result2.getBusVoltageSensitivityValue("T2wT", "BUS_1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result2.getBusVoltageSensitivityValue("T2wT", "BUS_2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result2.getBusVoltageSensitivityValue("T2wT", "BUS_3", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result2.getBusVoltageSensitivityValue("T2wT", "BUS_4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - - assertEquals(135.0, result2.getBusVoltageFunctionReferenceValue("BUS_1"), LoadFlowAssert.DELTA_V); - assertEquals(133.77, result2.getBusVoltageFunctionReferenceValue("BUS_2"), LoadFlowAssert.DELTA_V); - assertEquals(25.88, result2.getBusVoltageFunctionReferenceValue("BUS_3"), LoadFlowAssert.DELTA_V); - assertEquals(25.16, result2.getBusVoltageFunctionReferenceValue("BUS_4"), LoadFlowAssert.DELTA_V); - } - - @Test - void testBusVoltagePerTargetVVsc() { - Network network = HvdcNetworkFactory.createVsc(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "cs2")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(0d, result.getBusVoltageSensitivityValue("cs2", "b1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageSensitivityValue("cs2", "b2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testBusVoltagePerTarget3wt() { - Network network = VoltageControlNetworkFactory.createNetworkWithT3wt(); - ThreeWindingsTransformer t3wt = network.getThreeWindingsTransformer("T3wT"); - t3wt.getLeg2().getRatioTapChanger() - .setTargetDeadband(0) - .setRegulating(true) - .setTapPosition(0) - .setRegulationTerminal(t3wt.getLeg2().getTerminal()) - .setTargetV(28.); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VL_1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "T3wT")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getValues().size()); - assertEquals(0d, result.getBusVoltageSensitivityValue("T3wT", "BUS_1", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result.getBusVoltageSensitivityValue("T3wT", "BUS_2", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(1d, result.getBusVoltageSensitivityValue("T3wT", "BUS_3", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - assertEquals(0d, result.getBusVoltageSensitivityValue("T3wT", "BUS_4", SensitivityVariableType.BUS_TARGET_VOLTAGE), LoadFlowAssert.DELTA_V); - } - - @Test - void testBusVoltagePerTargetVFunctionRef() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLGEN_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = EurostagFactory.fix(EurostagTutorialExample1Factory.create()); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "GEN")) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - Function getV = busId -> network.getBusView().getBus(busId).getV(); - assertEquals(getV.apply("VLGEN_0"), result.getBusVoltageFunctionReferenceValue("NGEN"), LoadFlowAssert.DELTA_V); - assertEquals(getV.apply("VLHV1_0"), result.getBusVoltageFunctionReferenceValue("NHV1"), LoadFlowAssert.DELTA_V); - assertEquals(getV.apply("VLHV2_0"), result.getBusVoltageFunctionReferenceValue("NHV2"), LoadFlowAssert.DELTA_V); - assertEquals(getV.apply("VLLOAD_0"), result.getBusVoltageFunctionReferenceValue("NLOAD"), LoadFlowAssert.DELTA_V); - } - - @Test - void testAdditionnalFactors() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - - assertEquals(0.2296d, result.getBranchFlow1FunctionReferenceValue("l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3154d, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.2296d, result.getBranchFlow1FunctionReferenceValue("l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.4549d, result.getBranchFlow1FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.3154d, result.getBranchFlow1FunctionReferenceValue("l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testInjectionNotFoundAdditionalFactor() { - testInjectionNotFoundAdditionalFactor(false); - } - - @Test - void testTargetVOnPqNode() { - // asking a target v on a load should crash - Network network = FourBusNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "d3")) - .collect(Collectors.toList()); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Regulating terminal for 'd3' not found", e.getCause().getMessage()); - } - - @Test - void testTargetVOnAbsentTerminal() { - Network network = FourBusNetworkFactory.create(); - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "a")) - .collect(Collectors.toList()); - - List contingencies = Collections.emptyList(); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Regulating terminal for 'a' not found", e.getCause().getMessage()); - } - - @Test - void testTargetVOnNotRegulatingTwt() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBusBreakerView().getBusStream() - .map(bus -> createBusVoltagePerTargetV(bus.getId(), "l23")) - .collect(Collectors.toList()); - - List contingencies = Collections.emptyList(); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Regulating terminal for 'l23' not found", e.getCause().getMessage()); - } - - @Test - void testBusVoltageOnAbsentBus() { - Network network = FourBusNetworkFactory.create(); - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = Collections.singletonList(createBusVoltagePerTargetV("id", "g2")); - - List contingencies = Collections.emptyList(); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - - assertTrue(e.getCause() instanceof PowsyblException); - - assertEquals("The bus ref for 'id' cannot be resolved.", e.getCause().getMessage()); - } - - @Test - void testHvdcSensi() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusSelectionMode(SlackBusSelectionMode.MOST_MESHED); - - // test active power setpoint increase on an HVDC line - Network network = HvdcNetworkFactory.createNetworkWithGenerators2(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithLCCs() { - // test active power setpoint increase on a HVDC line - // FIXME - // Note that in case of LCC converter stations, in AC, an increase of the setpoint of the HDVC line is not equivalent to - // running two LFs and comparing the differences as we don't change Q at LCCs when we change P. - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusSelectionMode(SlackBusSelectionMode.MOST_MESHED); - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(0.36218, loadFlowDiff.get("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35967, loadFlowDiff.get("l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.61191, loadFlowDiff.get("l23"), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.341889, result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.341889, result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.63611, result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithBothSides() { - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", false); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l25"), result.getBranchFlow1SensitivityValue("hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l45"), result.getBranchFlow1SensitivityValue("hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l46"), result.getBranchFlow1SensitivityValue("hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l56"), result.getBranchFlow1SensitivityValue("hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithBothSidesDistributed() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.01); - - Network network = HvdcNetworkFactory.createNetworkWithGenerators2(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l25"), result.getBranchFlow1SensitivityValue("hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l45"), result.getBranchFlow1SensitivityValue("hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l46"), result.getBranchFlow1SensitivityValue("hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l56"), result.getBranchFlow1SensitivityValue("hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithBothSidesDistributed2() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.01); - - Network network = HvdcNetworkFactory.createNetworkWithGenerators2(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network1.getGenerator("g1").setTargetP(network1.getGenerator("g1").getTargetP() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.INJECTION_ACTIVE_POWER, List.of("g1"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l25"), result.getBranchFlow1SensitivityValue("g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l45"), result.getBranchFlow1SensitivityValue("g1", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l46"), result.getBranchFlow1SensitivityValue("g1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l56"), result.getBranchFlow1SensitivityValue("g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcInjectionNotFound() { - testHvdcInjectionNotFound(false); - } - - @Test - void disconnectedGeneratorShouldBeSkipped() { - Network network = FourBusNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - //Disconnect g4 generator - network.getGenerator("g4").getTerminal().disconnect(); - - // FIXME: using getBusBreakerView in AbstractSensitivityAnalysis.checkBus -> make this test passed - - List factors = Collections.singletonList(createBusVoltagePerTargetV("b1", "g4")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0d, result.getBusVoltageSensitivityValue("g4", "b1", SensitivityVariableType.BUS_TARGET_VOLTAGE)); - } - - @Test - void testBranchFunctionOutsideMainComponent() { - testBranchFunctionOutsideMainComponent(false); - } - - @Test - void testInjectionOutsideMainComponent() { - testInjectionOutsideMainComponent(false); - } - - @Test - void testPhaseShifterOutsideMainComponent() { - testPhaseShifterOutsideMainComponent(false); - } - - @Test - void testGlskOutsideMainComponent() { - testGlskOutsideMainComponent(false); - } - - @Test - void testGlskAndLineOutsideMainComponent() { - testGlskAndLineOutsideMainComponent(false); - } - - @Test - void testGlskPartiallyOutsideMainComponent() { - testGlskPartiallyOutsideMainComponent(false); - } - - @Test - void testInjectionNotFound() { - testInjectionNotFound(false); - } - - @Test - void testBranchNotFound() { - testBranchNotFound(false); - } - - @Test - void testEmptyFactors() { - testEmptyFactors(false); - } - - @Test - void testDanglingLineSensi() { - Network network = BoundaryFactory.createWithLoad(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "vl1_0"); - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "dl1"), - createBranchFlowPerInjectionIncrease("dl1", "dl1"), - createBranchIntensityPerInjectionIncrease("dl1", "load3")); - - // dangling line is connected - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(-0.903d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(91.293, result.getBranchFlow1FunctionReferenceValue("dl1"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.001d, result.getBranchFlow1SensitivityValue("dl1", "dl1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(260.51, result.getBranchCurrent1FunctionReferenceValue("dl1"), LoadFlowAssert.DELTA_I); - - // dangling line is connected on base case but will be disconnected by a contingency => 0 - List contingencies = List.of(new Contingency("c", new DanglingLineContingency("dl1"))); - result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(-0.903d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("c", "dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - // dangling line is disconnected on base case => 0 - network.getDanglingLine("dl1").getTerminal().disconnect(); - result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(0d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testWithHvdcAcEmulation() { - Network network = HvdcNetworkFactory.createWithHvdcInAcEmulation(); - network.getHvdcLine("hvdc34").newExtension(HvdcAngleDroopActivePowerControlAdder.class) - .withDroop(180) - .withP0(0.f) - .withEnabled(true) - .add(); - - LoadFlowParameters parameters = new LoadFlowParameters(); - parameters.setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - parameters.setHvdcAcEmulation(true); - OpenLoadFlowParameters.create(parameters) - .setSlackBusSelectionMode(SlackBusSelectionMode.MOST_MESHED); - - SensitivityAnalysisParameters sensiParameters = createParameters(false); - sensiParameters.setLoadFlowParameters(parameters); - List factors = List.of(createBranchFlowPerInjectionIncrease("l25", "d2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(0.442, result.getBranchFlow1SensitivityValue("d2", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testWithPhaseControlOn() { - Network network = PhaseControlFactory.createNetworkWithT2wt(); - TwoWindingsTransformer t2wt = network.getTwoWindingsTransformer("PS1"); - t2wt.getPhaseTapChanger().setRegulationMode(PhaseTapChanger.RegulationMode.ACTIVE_POWER_CONTROL) - .setTargetDeadband(1) - .setRegulating(true) - .setTapPosition(1) - .setRegulationTerminal(t2wt.getTerminal1()) - .setRegulationValue(83); - SensitivityAnalysisParameters sensiParameters = createParameters(false); - sensiParameters.getLoadFlowParameters().setPhaseShifterRegulationOn(true); - LoadFlow.run(network, sensiParameters.getLoadFlowParameters()); - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, - List.of("L1", "L2", "PS1"), SensitivityVariableType.INJECTION_ACTIVE_POWER, List.of("G1"), false, ContingencyContext.none()); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(network.getTwoWindingsTransformer("PS1").getTerminal1().getP(), result.getBranchFlow1FunctionReferenceValue(null, "PS1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testInjectionIncreaseWithPhaseControlOnInTheNetwork() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VL2_0", false); - sensiParameters.getLoadFlowParameters().setPhaseShifterRegulationOn(true); - - Network network = PhaseControlFactory.createNetworkWithT2wt(); - TwoWindingsTransformer t2wt = network.getTwoWindingsTransformer("PS1"); - t2wt.getPhaseTapChanger().setRegulationMode(PhaseTapChanger.RegulationMode.ACTIVE_POWER_CONTROL) - .setTargetDeadband(1) - .setRegulating(true) - .setTapPosition(1) - .setRegulationTerminal(t2wt.getTerminal1()) - .setRegulationValue(83); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = PhaseControlFactory.createNetworkWithT2wt(); - TwoWindingsTransformer t2wt1 = network1.getTwoWindingsTransformer("PS1"); - t2wt1.getPhaseTapChanger().setRegulationMode(PhaseTapChanger.RegulationMode.ACTIVE_POWER_CONTROL) - .setTargetDeadband(1) - .setRegulating(true) - .setTapPosition(1) - .setRegulationTerminal(t2wt1.getTerminal1()) - .setRegulationValue(83); - network1.getGenerator("G1").setTargetP(network1.getGenerator("G1").getTargetP() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("L1", "L2"), - SensitivityVariableType.INJECTION_ACTIVE_POWER, List.of("G1"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("L1"), result.getBranchFlow1SensitivityValue("G1", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("L2"), result.getBranchFlow1SensitivityValue("G1", "L2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void lineWithDifferentNominalVoltageTest() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLLOAD_0", false); - Network network = EurostagTutorialExample1Factory.create(); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("NHV1_NHV2_1", "NHV1_NHV2_2"), - SensitivityVariableType.INJECTION_ACTIVE_POWER, List.of("GEN"), - false, ContingencyContext.all()); - - runLf(network, sensiParameters.getLoadFlowParameters()); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(0.499, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.499, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(303.166, result.getFunctionReferenceValue("NHV1_NHV2_1", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), LoadFlowAssert.DELTA_POWER); - assertEquals(303.166, result.getFunctionReferenceValue("NHV1_NHV2_2", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), LoadFlowAssert.DELTA_POWER); - assertEquals(303.166, network.getLine("NHV1_NHV2_1").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(303.166, network.getLine("NHV1_NHV2_2").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(602.290, network.getTwoWindingsTransformer("NHV2_NLOAD").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(-601.419, network.getTwoWindingsTransformer("NHV2_NLOAD").getTerminal2().getP(), LoadFlowAssert.DELTA_POWER); - - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setAddRatioToLinesWithDifferentNominalVoltageAtBothEnds(true); - network.getVoltageLevel("VLHV2").setNominalV(360); - runLf(network, sensiParameters.getLoadFlowParameters()); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(0.499, result2.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.499, result2.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(303.170, result2.getFunctionReferenceValue("NHV1_NHV2_1", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), LoadFlowAssert.DELTA_POWER); - assertEquals(303.170, result2.getFunctionReferenceValue("NHV1_NHV2_2", SensitivityFunctionType.BRANCH_ACTIVE_POWER_1), LoadFlowAssert.DELTA_POWER); - assertEquals(303.170, network.getLine("NHV1_NHV2_1").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(303.170, network.getLine("NHV1_NHV2_2").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(602.302, network.getTwoWindingsTransformer("NHV2_NLOAD").getTerminal1().getP(), LoadFlowAssert.DELTA_POWER); - assertEquals(-601.430, network.getTwoWindingsTransformer("NHV2_NLOAD").getTerminal2().getP(), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testWithPvPqSwitch() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - // this network has no G or B, so we should be very close to DC results - Network network = FourBusNetworkFactory.createBaseNetwork(); - network.getLoad("d2").setQ0(0.4); - network.getLoad("d3").setQ0(1.6); - network.getGenerator("g4").newMinMaxReactiveLimits().setMinQ(-0.5).setMaxQ(0.5).add(); - - runLf(network, sensiParameters.getLoadFlowParameters()); - - List factors = List.of(createBusVoltagePerTargetV("b3", "g4")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - - assertEquals(0.0, result.getBusVoltageSensitivityValue("g4", "b3", SensitivityVariableType.BUS_TARGET_VOLTAGE)); - } - - @Test - void testNullBusInjection() { - Network network = EurostagTutorialExample1Factory.create(); - network.getLoad("LOAD").getTerminal().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(false, "VLGEN_0"); - - List factors = List.of(createBranchIntensityPerInjectionIncrease("NHV1_NHV2_1", "LOAD")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(92.0836, result.getBranchCurrent1FunctionReferenceValue("NHV1_NHV2_1"), LoadFlowAssert.DELTA_I); - } - - @Test - void testThreeWindingsTransformerAsFunction() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - Network network = VoltageControlNetworkFactory.createNetworkWithT3wt(); - - SensitivityFactor factorActivePower1Twt = createTransformerLegFlowPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.ONE); - SensitivityFactor factorActivePower2Twt = createTransformerLegFlowPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.TWO); - SensitivityFactor factorActivePower3Twt = createTransformerLegFlowPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.THREE); - - SensitivityFactor factorCurrent1 = createTransformerLegIntensityPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.ONE); - SensitivityFactor factorCurrent2 = createTransformerLegIntensityPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.TWO); - SensitivityFactor factorCurrent3 = createTransformerLegIntensityPerInjectionIncrease("T3wT", "LOAD_3", ThreeWindingsTransformer.Side.THREE); - - List factors = List.of(factorActivePower1Twt, factorActivePower2Twt, factorActivePower3Twt, - factorCurrent1, factorCurrent2, factorCurrent3); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(6, result.getValues().size()); - - assertEquals(43.03, result.getBranchCurrent1FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_I); - assertEquals(83.91, result.getBranchCurrent2FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_I); - assertEquals(279.70, result.getBranchCurrent3FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_I); - assertEquals(10.007, result.getBranchFlow1FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_POWER); - assertEquals(-4.999, result.getBranchFlow2FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_POWER); - assertEquals(-4.999, result.getBranchFlow3FunctionReferenceValue("T3wT"), LoadFlowAssert.DELTA_POWER); - - assertEquals(-4.309, result.getBranchCurrent1SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_I); - assertEquals(-16.796, result.getBranchCurrent2SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_I); - assertEquals(-0.033, result.getBranchCurrent3SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_I); - assertEquals(-1.001, result.getBranchFlow1SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1.000, result.getBranchFlow2SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.000, result.getBranchFlow3SensitivityValue("LOAD_3", "T3wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testThreeWindingsTransformerAsVariable() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - Network network = PhaseControlFactory.createNetworkWithT3wt(); - - //Add phase tap changer to leg1 and leg3 of the twt for testing purpose - ThreeWindingsTransformer twt = network.getThreeWindingsTransformer("PS1"); - twt.getLeg1().newPhaseTapChanger() - .setTapPosition(1) - .setRegulationTerminal(twt.getLeg1().getTerminal()) - .setRegulationMode(PhaseTapChanger.RegulationMode.FIXED_TAP) - .setRegulationValue(200) - .beginStep() - .setAlpha(-5.0) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .beginStep() - .setAlpha(0.0) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .beginStep() - .setAlpha(5) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .add(); - twt.getLeg3().newPhaseTapChanger() - .setTapPosition(1) - .setRegulationTerminal(twt.getLeg3().getTerminal()) - .setRegulationMode(PhaseTapChanger.RegulationMode.FIXED_TAP) - .setRegulationValue(200) - .beginStep() - .setAlpha(-5.0) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .beginStep() - .setAlpha(0.0) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .beginStep() - .setAlpha(5) - .setRho(1.0) - .setR(0.0) - .setX(0.0) - .setG(0.0) - .setB(0.0) - .endStep() - .add(); - - SensitivityFactor factorPhase1 = createBranchFlowPerTransformerLegPSTAngle("L1", "PS1", ThreeWindingsTransformer.Side.ONE); - SensitivityFactor factorPhase2 = createBranchFlowPerTransformerLegPSTAngle("L1", "PS1", ThreeWindingsTransformer.Side.TWO); - SensitivityFactor factorPhase3 = createBranchFlowPerTransformerLegPSTAngle("L1", "PS1", ThreeWindingsTransformer.Side.THREE); - List factors = List.of(factorPhase1, factorPhase2, factorPhase3); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(3, result.getValues().size()); - // FIXME. - assertEquals(-5.421, result.getBranchFlow1SensitivityValue("PS1", "L1", SensitivityVariableType.TRANSFORMER_PHASE_1), LoadFlowAssert.DELTA_POWER); - assertEquals(5.421, result.getBranchFlow1SensitivityValue("PS1", "L1", SensitivityVariableType.TRANSFORMER_PHASE_2), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0, result.getBranchFlow1SensitivityValue("PS1", "L1", SensitivityVariableType.TRANSFORMER_PHASE_3), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testThreeWindingsTransformerNoPhaseShifter() { - SensitivityAnalysisParameters sensiParameters = createParameters(false, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - Network network = VoltageControlNetworkFactory.createNetworkWithT3wt(); - SensitivityFactor factorPhase1 = createBranchFlowPerTransformerLegPSTAngle("LINE_12", "T3wT", ThreeWindingsTransformer.Side.ONE); - assertThrows(CompletionException.class, () -> - sensiRunner.run(network, List.of(factorPhase1), Collections.emptyList(), Collections.emptyList(), sensiParameters), - "Three windings transformer 'T3wT' leg on side 'TRANSFORMER_PHASE_1' is not a phase shifter"); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java b/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java deleted file mode 100644 index 1bb2149836..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java +++ /dev/null @@ -1,2252 +0,0 @@ -/** - * Copyright (c) 2021, 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.openloadflow.sensi.dc; - -import com.powsybl.commons.ComparisonUtils; -import com.powsybl.commons.PowsyblException; -import com.powsybl.commons.config.PlatformConfig; -import com.powsybl.commons.reporter.Reporter; -import com.powsybl.contingency.*; -import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory; -import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControlAdder; -import com.powsybl.iidm.network.test.PhaseShifterTestCaseFactory; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.OpenLoadFlowParameters; -import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.sensi.OpenSensitivityAnalysisParameters; -import com.powsybl.openloadflow.sensi.OpenSensitivityAnalysisProvider; -import com.powsybl.openloadflow.util.LoadFlowAssert; -import com.powsybl.sensitivity.*; -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileSystem; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.PathMatcher; -import java.util.*; -import java.util.concurrent.CompletionException; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Gael Macherel - */ -class DcSensitivityAnalysisContingenciesTest extends AbstractSensitivityAnalysisTest { - - @Test - void testContingencyWithOneElementAwayFromSlack() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getPreContingencyValues().size()); - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(5, result.getValues("l23").size()); - assertEquals(2d / 15d, result.getBranchFlow1SensitivityValue("l23", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.6d, result.getBranchFlow1SensitivityValue("l23", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 15d, result.getBranchFlow1SensitivityValue("l23", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 15d, result.getBranchFlow1SensitivityValue("l23", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyWithOneElementAwayOnSlack() { - //remove a branch connected to slack - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l12", new BranchContingency("l12"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getPreContingencyValues().size()); - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(5, result.getValues("l12").size()); - assertEquals(-1d / 15d, result.getBranchFlow1SensitivityValue("l12", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l12", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.6d, result.getBranchFlow1SensitivityValue("l12", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(4d / 15d, result.getBranchFlow1SensitivityValue("l12", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l12", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyWithOneElementAwayOnSlackWithAdditionalFactors() { - //remove a branch connected to slack - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l12", new BranchContingency("l12"))); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l13", "g2"), - createBranchFlowPerInjectionIncrease("l14", "g2"), - createBranchFlowPerInjectionIncrease("l34", "g2"), - createBranchFlowPerInjectionIncrease("l12", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(4, result.getPreContingencyValues().size()); - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(4, result.getValues("l12").size()); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l12", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(4d / 15d, result.getBranchFlow1SensitivityValue("l12", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l12", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.0666, result.getBranchFlow1SensitivityValue("l12", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionRefOnOneElement() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l23", new BranchContingency("l23"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - Network networkDisconnected = FourBusNetworkFactory.create(); - networkDisconnected.getLine("l23").getTerminal1().disconnect(); - networkDisconnected.getLine("l23").getTerminal2().disconnect(); - runDcLf(networkDisconnected); - - for (Line line : networkDisconnected.getLineStream().filter(line -> !Double.isNaN(line.getTerminal1().getP())).collect(Collectors.toList())) { - assertEquals(line.getTerminal1().getP(), result.getBranchFlow1FunctionReferenceValue("l23", line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testFunctionRefOnTwoElement() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l23+l34", new BranchContingency("l23"), new BranchContingency("l34"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - Network networkDisconnected = FourBusNetworkFactory.create(); - networkDisconnected.getLine("l23").getTerminal1().disconnect(); - networkDisconnected.getLine("l23").getTerminal2().disconnect(); - networkDisconnected.getLine("l34").getTerminal1().disconnect(); - networkDisconnected.getLine("l34").getTerminal2().disconnect(); - - runDcLf(networkDisconnected); - - for (Line line : networkDisconnected.getLineStream().filter(line -> !Double.isNaN(line.getTerminal1().getP())).collect(Collectors.toList())) { - assertEquals(line.getTerminal1().getP(), result.getBranchFlow1FunctionReferenceValue("l23+l34", line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testContingencyWithTwoElementsAwayFromSlack() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l23+l34", new BranchContingency("l23"), new BranchContingency("l34"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g2")), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getPreContingencyValues().size()); - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(5, result.getValues("l23+l34").size()); - assertEquals(0.2, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.6d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConnectivityLossOnSingleLine() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(14, result.getValues("l34").size()); - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-4d / 3d, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-5d / 3d, result.getBranchFlow1FunctionReferenceValue("l34", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConnectivityLossOnSingleLineWithDistributedSlack() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(14, result.getValues("l34").size()); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void slackRedistributionInAdditionalFactors() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - Branch l12 = network.getBranch("l12"); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), Collections.singletonList(l12)); - - factors.addAll(createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().filter(b -> !b.equals(l12)).collect(Collectors.toList()))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(14, result.getValues("l34").size()); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("l34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("l34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConnectivityLossOnTwoComponentAtATime() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34+l48", new BranchContingency("l34"), new BranchContingency("l48"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l34+l48"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(36, result.getValues("l34+l48").size()); - - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l48", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l810", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g2", "l910", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l48", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l810", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g6", "l910", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - // FIXME: Next line is not working with EvenShiloach, it feels like the connectivity check is wrong (in the predefinedResults definition) - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l48", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l810", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l48", "g10", "l910", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLosingTheSameConnectivityTwice() { - Network network = ConnectedComponentNetworkFactory.createTwoConnectedComponentsLinkedByASerieOfTwoBranches(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34+l45", new BranchContingency("l34"), new BranchContingency("l45"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l34+l45"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(16, result.getValues("l34+l45").size()); - - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g2", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l45", "g6", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLosingConnectivityOnTwoBranches() { - Network network = ConnectedComponentNetworkFactory.createThreeCc(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34+l47", new BranchContingency("l34"), new BranchContingency("l47"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l34+l47"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(33, result.getValues("l34+l47").size()); - - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g2", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g6", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l47", "g9", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLosingAllPossibleCompensations() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "d1", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "d1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "d1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d5", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d5", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - assertTrue(Double.isNaN(result.getBranchFlow1SensitivityValue("l34", "d6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER))); - } - - @Test - void testPhaseShifterUnrelatedContingency() { - Network network = FourBusNetworkFactory.createWithPhaseTapChanger(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0"); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l14", new BranchContingency("l14"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getPreContingencyValues().size()); - assertEquals(15d / 4d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(-10d / 4d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(-5d / 4d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(15d / 4d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(5d / 4d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - - assertEquals(5, result.getValues("l14").size()); - - assertEquals(10d / 3d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l14", "l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(-10d / 3d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l14", "l23", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l14", "l23", "l14", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(10d / 3d * Math.PI / 180d, result.getBranchFlow1SensitivityValue("l14", "l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l14", "l23", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testPhaseShifterConnectivityLoss() { - Network network = ConnectedComponentNetworkFactory.createTwoCcWithATransformerLinkedByASingleLine(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0"); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l56", "l34")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "l56", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "l56", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "l56", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "l56", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "l56", "l45", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "l56", "l46", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34", "l56", "l56", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnTransformer() { - Network network = ConnectedComponentNetworkFactory.createTwoCcWithATransformerLinkedByASingleLine(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0"); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerPSTAngle(branch.getId(), "l56", "l56")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l56", new BranchContingency("l56"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("l56").size()); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l13", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l34", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l45", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l56", "l56", "l46", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l56", "l56", "l56", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLcc() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - List contingencyResult = result.getValues("hvdc34"); - assertEquals(6, contingencyResult.size()); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcVsc() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcVscDistributedOnLoad() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - assertEquals(2d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(2d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLccWithoutLosingConnectivity() { - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23", "l25", "l45", "l46", "l56").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("hvdc34").size()); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 12d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 12d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(2d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(-2d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l25"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l46"), LoadFlowAssert.DELTA_POWER); - assertEquals(-2d / 3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLccAndLosingConnectivity() { - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23", "l25", "l45", "l46", "l56").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"), new BranchContingency("l25"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("hvdc34").size()); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 6d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l25"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l46"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLccAndTransformerWithoutLosingConnectivity() { - Network network = HvdcNetworkFactory.createNetworkWithTransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"), new BranchContingency("l23"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnHvdcLccAndTransformerAndLosingConnectivity() { - Network network = HvdcNetworkFactory.createLinkedNetworkWithTransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23", "l25", "l45", "l46", "l56").map(network::getBranch).collect(Collectors.toList()), - "hvdc34"); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"), new BranchContingency("l23"), new BranchContingency("l25"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - List contingencyResult = result.getValues("hvdc34"); - assertEquals(7, contingencyResult.size()); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(3d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l25"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l46"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("hvdc34", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testTrivialContingencyOnGenerator() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1", true); - sensiParameters.setLoadFlowParameters(new LoadFlowParameters() - .setDc(true) - .setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX)); - - List factors = createFactorMatrix(Stream.of("g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("g6", new GeneratorContingency("g6"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(3, result.getValues("g6").size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("g6", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-1.3333d, result.getBranchFlow1FunctionReferenceValue("g6", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.3333d, result.getBranchFlow1FunctionReferenceValue("g6", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.6666d, result.getBranchFlow1FunctionReferenceValue("g6", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnLoad() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1", true); - sensiParameters.setLoadFlowParameters(new LoadFlowParameters() - .setDc(true) - .setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); - - List factors = createFactorMatrix(Stream.of("g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("d5", new LoadContingency("d5"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(3, result.getValues("d5").size()); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("d5", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("d5", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("d5", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-14d / 10d, result.getBranchFlow1FunctionReferenceValue("d5", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-8d / 30d, result.getBranchFlow1FunctionReferenceValue("d5", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(34d / 30d, result.getBranchFlow1FunctionReferenceValue("d5", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyMultipleLinesBreaksOneContingency() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l24+l35", new BranchContingency("l24"), new BranchContingency("l35"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l24+l35"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(16, result.getValues("l24+l35").size()); - assertEquals(-0.5d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l24+l35", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testCircularLossOfConnectivity() { - Network network = ConnectedComponentNetworkFactory.createThreeCircularCc(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l34+l27+l58", new BranchContingency("l34"), new BranchContingency("l27"), new BranchContingency("l58"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), - "l34+l27+l58"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(36, result.getValues("l34+l27+l58").size()); - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER)); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g2", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER)); - - // Components that are not linked to slack should be NaN - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER)); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l34+l27+l58", "g9", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER)); - } - - @Test - void testAsymetricLossOnMultipleComponents() { - Network network = ConnectedComponentNetworkFactory.createAsymetricNetwork(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l27+l18+l39+l14", new BranchContingency("l27"), - new BranchContingency("l18"), - new BranchContingency("l39"), - new BranchContingency("l14"))); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), "l27+l18+l39+l14"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(39, result.getValues("l27+l18+l39+l14").size()); - List contingencyValues = result.getValues("l27+l18+l39+l14"); - assertEquals(-2d / 3d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l18", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l27", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l39", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g2", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l18", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l27", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l39", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g6", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l18", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l27", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l39", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l79", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l27+l18+l39+l14", "g9", "l89", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testRemainingGlskFactors() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List variables = List.of(new WeightedSensitivityVariable("g2", 25f), - new WeightedSensitivityVariable("g6", 40f), - new WeightedSensitivityVariable("d3", 35f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk", "l34")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - assertEquals(-17d / 36d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 18d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-19d / 36d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskFactorBug() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBusWithTransformer(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List variables = List.of(new WeightedSensitivityVariable("g2", 25f), - new WeightedSensitivityVariable("g6", 40f), - new WeightedSensitivityVariable("g10", 35f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l45", new BranchContingency("l45"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - List contingencies2 = List.of(new Contingency("l34", new BranchContingency("l34")), - new Contingency("l45", new BranchContingency("l45"))); - - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, contingencies2, variableSets, sensiParameters); - - // result for l45 contingency should exactly be the same as l34 contingency for simulation 2 should not impact - // l45 contingency. - // an issue has been identified that is responsible in case of 2 consecutive GLSK sensitivity loosing connectivity - // of bad reset of state - for (Branch branch : network.getBranches()) { - assertEquals(result.getBranchFlow1SensitivityValue("l45", "glsk", branch.getId(), SensitivityVariableType.INJECTION_ACTIVE_POWER), - result2.getBranchFlow1SensitivityValue("l45", "glsk", branch.getId(), SensitivityVariableType.INJECTION_ACTIVE_POWER), - 0d); - } - } - - @Test - void testRemainingGlskFactorsAdditionalFactors() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List variables = List.of(new WeightedSensitivityVariable("g2", 25f), - new WeightedSensitivityVariable("g6", 40f), - new WeightedSensitivityVariable("d3", 35f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = contingencies.stream() - .flatMap(c -> network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk", c.getId()))) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, - variableSets, sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - assertEquals(-17d / 36d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 18d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-19d / 36d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l34", "glsk", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiRescale() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, List.of("b1_vl_0", "b4_vl_0"), true); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators(); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network1.getLine("l25").getTerminal1().disconnect(); - network1.getLine("l25").getTerminal2().disconnect(); - runLf(network1, sensiParameters.getLoadFlowParameters()); - - Network network2 = HvdcNetworkFactory.createNetworkWithGenerators(); - network2.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - network2.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network2.getLine("l25").getTerminal1().disconnect(); - network2.getLine("l25").getTerminal2().disconnect(); - runLf(network2, sensiParameters.getLoadFlowParameters()); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - Map loadFlowDiff = network.getLineStream() - .map(Identifiable::getId) - .collect(Collectors.toMap(Function.identity(), line -> (network2.getLine(line).getTerminal1().getP() - network1.getLine(line).getTerminal1().getP()) / SENSI_CHANGE)); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - List contingencies = Collections.singletonList(new Contingency("l25", new BranchContingency("l25"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l25", "hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testNullValue() { - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testReconnectingMultipleLinesToRestoreConnectivity() { - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "d5", "l23+l24+l36+l35+l46")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l23+l24+l36+l35+l46", - new BranchContingency("l23"), - new BranchContingency("l24"), - new BranchContingency("l36"), - new BranchContingency("l35"), - new BranchContingency("l46"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(11, result.getValues("l23+l24+l36+l35+l46").size()); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l36", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 4d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 4d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 4d, result.getBranchFlow1SensitivityValue("l23+l24+l36+l35+l46", "d5", "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testBreakingConnectivityOutsideMainComponent() { - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - - // Create contingencies containing a branch contingency on l12 which breaks the connectivity outside the main - // component and which leaves a connected component with a single bus - // Connected components after these contingencies are: {b1}, {b2, b3, b4}, {b5, b6, b7, b8} - // The two contingencies should be grouped together for computing the sensitivities (shared ConnectivityAnalysisResult) - List> contingencyBranchesIds = List.of( - List.of("l12", "l36", "l35", "l46"), - List.of("l12", "l36", "l35", "l34", "l46")); - List contingencies = contingencyBranchesIds.stream() - .map(branchesIds -> new Contingency(String.join("+", branchesIds), - branchesIds.stream().map(BranchContingency::new).collect(Collectors.toList()))) - .collect(Collectors.toList()); - String contingency1Id = contingencies.get(0).getId(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - - String variableId = "d6"; - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), variableId)).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(11, result.getValues(contingency1Id).size()); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l24", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l35", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l36", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1 / 3., result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1 / 3., result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l57", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(2 / 3., result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l67", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, "l78", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyAllLines() { - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - - List branchesId = network.getLineStream().map(Line::getId).collect(Collectors.toList()); - List contingencies = List.of(new Contingency( - String.join("+", branchesId), - branchesId.stream().map(BranchContingency::new).collect(Collectors.toList()))); - String contingency1Id = contingencies.get(0).getId(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true); - - String variableId = "d4"; - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), variableId)).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(11, result.getValues(contingency1Id).size()); - for (Line line : network.getLines()) { - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue(contingency1Id, variableId, line.getId(), SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testFunctionRefWithMultipleReconnections() { - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "d5")).collect(Collectors.toList()); - - List contingencyBranchesId = List.of("l23", "l24", "l36", "l35", "l46"); - String contingencyId = String.join("+", contingencyBranchesId); - List contingencies = List.of(new Contingency( - contingencyId, - contingencyBranchesId.stream().map(BranchContingency::new).collect(Collectors.toList()) - )); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - Network networkDisconnected = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - contingencyBranchesId.forEach(id -> { - networkDisconnected.getLine(id).getTerminal1().disconnect(); - networkDisconnected.getLine(id).getTerminal2().disconnect(); - }); - - runLf(networkDisconnected, sensiParameters.getLoadFlowParameters()); - List nonDisconnectedLines = networkDisconnected.getLineStream().filter(line -> !Double.isNaN(line.getTerminal1().getP())).collect(Collectors.toList()); - assertNotEquals(0, nonDisconnectedLines.size()); - for (Line line : nonDisconnectedLines) { - assertEquals(line.getTerminal1().getP(), result.getBranchFlow1FunctionReferenceValue(contingencyId, line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testFunctionRefWithOneReconnection() { - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedSingleComponent(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "g2")).collect(Collectors.toList()); - - List contingencyBranchesId = List.of("l24", "l35"); - String contingencyId = String.join("+", contingencyBranchesId); - List contingencies = List.of(new Contingency( - contingencyId, - contingencyBranchesId.stream().map(BranchContingency::new).collect(Collectors.toList()) - )); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - Network networkDisconnected = ConnectedComponentNetworkFactory.createHighlyConnectedSingleComponent(); - contingencyBranchesId.forEach(id -> { - networkDisconnected.getLine(id).getTerminal1().disconnect(); - networkDisconnected.getLine(id).getTerminal2().disconnect(); - }); - - runLf(networkDisconnected, sensiParameters.getLoadFlowParameters()); - List nonDisconnectedLines = networkDisconnected.getLineStream().filter(line -> !Double.isNaN(line.getTerminal1().getP())).collect(Collectors.toList()); - assertNotEquals(0, nonDisconnectedLines.size()); - for (Line line : nonDisconnectedLines) { - assertEquals(line.getTerminal1().getP(), result.getBranchFlow1FunctionReferenceValue(contingencyId, line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testFunctionRefWithSequenceOfConnectivty() { - // Test that the result if you compute sensitivity manually one by one, - // or all at once does not change result on function reference - // (especially if you loose compensation) - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - String branchId = "l36"; - String injectionId = "g3"; - Branch branch = network.getBranch(branchId); - List factors = List.of(createBranchFlowPerInjectionIncrease(branch.getId(), injectionId)); - - Contingency contingency78 = new Contingency("l78", new BranchContingency("l78")); - Contingency contingency12 = new Contingency("l12", new BranchContingency("l12")); - Contingency contingency35and56and57 = new Contingency("l35+l56+l57", new BranchContingency("l35"), new BranchContingency("l56"), new BranchContingency("l57")); - - Function, SensitivityAnalysisResult> resultProvider = contingencies -> sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - SensitivityAnalysisResult result78 = resultProvider.apply(List.of(contingency78)); - SensitivityAnalysisResult result12 = resultProvider.apply(List.of(contingency12)); - SensitivityAnalysisResult result35and56and57 = resultProvider.apply(List.of(contingency35and56and57)); - SensitivityAnalysisResult globalResult = resultProvider.apply(List.of(contingency12, contingency78, contingency35and56and57)); - - assertEquals(result78.getBranchFlow1FunctionReferenceValue("l78", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l78", branchId), LoadFlowAssert.DELTA_POWER); - assertEquals(result12.getBranchFlow1FunctionReferenceValue("l12", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l12", branchId), LoadFlowAssert.DELTA_POWER); - assertEquals(result35and56and57.getBranchFlow1FunctionReferenceValue("l35+l56+l57", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l35+l56+l57", branchId), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionRefWithAdditionalFactors() { - // Test that the result if you compute sensitivity manually one by one, - // or all at once does not change result on function reference - // (especially if you loose compensation) - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - String branchId = "l36"; - String injectionId = "g3"; - Branch branch = network.getBranch(branchId); - List factors = List.of(createBranchFlowPerInjectionIncrease(branch.getId(), injectionId)); - - Contingency contingency78 = new Contingency("l78", new BranchContingency("l78")); - Contingency contingency12 = new Contingency("l12", new BranchContingency("l12")); - Contingency contingency35and56and57 = new Contingency("l35+l56+l57", new BranchContingency("l35"), new BranchContingency("l56"), new BranchContingency("l57")); - - Function, SensitivityAnalysisResult> resultProvider = contingencies -> sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - SensitivityAnalysisResult result78 = resultProvider.apply(List.of(contingency78)); - SensitivityAnalysisResult result12 = resultProvider.apply(List.of(contingency12)); - SensitivityAnalysisResult result35and56and57 = resultProvider.apply(List.of(contingency35and56and57)); - SensitivityAnalysisResult globalResult = resultProvider.apply(List.of(contingency12, contingency78, contingency35and56and57)); - - assertEquals(result78.getBranchFlow1FunctionReferenceValue("l78", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l78", branchId), LoadFlowAssert.DELTA_POWER); - assertEquals(result12.getBranchFlow1FunctionReferenceValue("l12", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l12", branchId), LoadFlowAssert.DELTA_POWER); - assertEquals(result35and56and57.getBranchFlow1FunctionReferenceValue("l35+l56+l57", branchId), globalResult.getBranchFlow1FunctionReferenceValue("l35+l56+l57", branchId), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testChangingCompensationThenNot() { - // Multiple contingencies: one that lose connectivity and change the slack distribution, and others that lose connectivity without changing distribution - // This allows us to check that changing the compensation has no side effect on the next contingencies - // The contingency changing distribution must be done before at least one of the other. - - Network network = ConnectedComponentNetworkFactory.createHighlyConnectedNetwork(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b6_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - String branchId = "l36"; - String injectionId = "g3"; - Branch branch = network.getBranch(branchId); - List factors = List.of(createBranchFlowPerInjectionIncrease(branch.getId(), injectionId)); - - Contingency contingency78 = new Contingency("l78", new BranchContingency("l78")); - Contingency contingency12 = new Contingency("l12", new BranchContingency("l12")); // change distribution - Contingency contingency35and56and57 = new Contingency("l35+l56+l57", new BranchContingency("l35"), new BranchContingency("l56"), new BranchContingency("l57")); - - Function, SensitivityAnalysisResult> resultProvider = contingencies -> sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - SensitivityAnalysisResult result78 = resultProvider.apply(List.of(contingency78)); - SensitivityAnalysisResult result12 = resultProvider.apply(List.of(contingency12)); - SensitivityAnalysisResult result35and56and57 = resultProvider.apply(List.of(contingency35and56and57)); - SensitivityAnalysisResult globalResult = resultProvider.apply(List.of(contingency12, contingency78, contingency35and56and57)); - - assertEquals(result78.getBranchFlow1SensitivityValue("l78", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), globalResult.getBranchFlow1SensitivityValue("l78", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result12.getBranchFlow1SensitivityValue("l12", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), globalResult.getBranchFlow1SensitivityValue("l12", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result35and56and57.getBranchFlow1SensitivityValue("l35+l56+l57", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), globalResult.getBranchFlow1SensitivityValue("l35+l56+l57", injectionId, branchId, SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionReferenceWhenLosingATransformer() { - Network network = ConnectedComponentNetworkFactory.createTwoCcWithATransformerLinkedByASingleLine(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "g2", "l56")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l56", new BranchContingency("l56"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("l56").size()); - - assertEquals(-4d / 3d, result.getBranchFlow1FunctionReferenceValue("l56", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("l56", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(5d / 3d, result.getBranchFlow1FunctionReferenceValue("l56", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("l56", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("l56", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l56", "l56"), LoadFlowAssert.DELTA_POWER); - assertEquals(-2d, result.getBranchFlow1FunctionReferenceValue("l56", "l46"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionReferenceWhenLosingATransformerAndAContingency() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBusWithTransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "g2", "l48+l67")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l48+l67", new BranchContingency("l48"), new BranchContingency("l67"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(-4d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(5d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(-4d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l56"), LoadFlowAssert.DELTA_POWER); - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l57"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l89"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l67"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionReferenceWhenLosingATransformerAndAContingencyInAdditionalFactors() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBusWithTransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = Stream.concat(Stream.of(createBranchFlowPerInjectionIncrease("l12", "g2", "l48+l67")), - network.getBranchStream().filter(branch -> !branch.getId().equals("l12")).map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "g2", "l48+l67"))) - .collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l48+l67", new BranchContingency("l48"), new BranchContingency("l67"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(-4d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(5d / 3d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(1d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l45"), LoadFlowAssert.DELTA_POWER); - assertEquals(-4d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l56"), LoadFlowAssert.DELTA_POWER); - assertEquals(2d, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l57"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l89"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l48+l67", "l67"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionReferenceWhenLosingConnectivityOnATransformer() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByATransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0"); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), "g2", "l34")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(7, result.getValues("l34").size()); - - assertEquals(-5d / 3d, result.getBranchFlow1FunctionReferenceValue("l34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 3d, result.getBranchFlow1FunctionReferenceValue("l34", "l13"), LoadFlowAssert.DELTA_POWER); - assertEquals(4d / 3d, result.getBranchFlow1FunctionReferenceValue("l34", "l23"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testInjectionNotFoundAdditionalFactorContingency() { - testInjectionNotFoundAdditionalFactorContingency(true); - } - - @Test - void testFunctionReferenceWhenLosingATransformerThenLosingAConnectivityBreakingContingency() { - Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBusWithTransformer(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l56", "g2")); - - Contingency transformerContingency = new Contingency("l67", new BranchContingency("l67")); // losing a transformer - Contingency connectivityLosingContingency = new Contingency("l48", new BranchContingency("l48")); // losing connectivty - - SensitivityAnalysisResult resultBoth = sensiRunner.run(network, factors, List.of(transformerContingency, connectivityLosingContingency), Collections.emptyList(), sensiParameters); - - SensitivityAnalysisResult resultLosingConnectivityAlone = sensiRunner.run(network, factors, List.of(connectivityLosingContingency), Collections.emptyList(), sensiParameters); - - SensitivityAnalysisResult resultLosingTransformerAlone = sensiRunner.run(network, factors, List.of(transformerContingency), Collections.emptyList(), sensiParameters); - - assertEquals(resultLosingConnectivityAlone.getBranchFlow1FunctionReferenceValue("l48", "l56"), resultBoth.getBranchFlow1FunctionReferenceValue("l48", "l56"), LoadFlowAssert.DELTA_POWER); - assertEquals(resultLosingTransformerAlone.getBranchFlow1FunctionReferenceValue("l67", "l56"), resultBoth.getBranchFlow1FunctionReferenceValue("l67", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnAWrongHvdc() { - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List factors = createFactorMatrix(Stream.of("g1", "g2").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l13", "l23").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("wrong"))); - - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("HVDC line 'wrong' not found in the network", e.getCause().getMessage()); - } - - @Test - void testContingencyWithDisconnectedBranch() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List contingencies = List.of(new Contingency("l45", new BranchContingency("l45"))); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l46", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - // different sensitivity for (g2, l46) on base case and after contingency l45 - assertEquals(0.0667d, result.getBranchFlow1SensitivityValue("g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1429d, result.getBranchFlow1SensitivityValue("l45", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - // we open l45 at both sides - Line l45 = network.getLine("l45"); - l45.getTerminal1().disconnect(); - l45.getTerminal2().disconnect(); - runDcLf(network); - - result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - // we now have as expected the sensitivity for (g2, l46) on base case and after contingency l45 - // because l45 is already open on base case - assertEquals(0.1429d, result.getBranchFlow1SensitivityValue("g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1429d, result.getBranchFlow1SensitivityValue("l45", "g2", "l46", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionDisconnectedBranchSide1() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - Line l45 = network.getLine("l45"); - l45.getTerminal1().disconnect(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l45", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - // sensitivity on an open branch is zero - assertEquals(0, result.getBranchFlow1SensitivityValue("g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionDisconnectedBranchSide2() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - Line l45 = network.getLine("l45"); - l45.getTerminal2().disconnect(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l45", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - // sensitivity on an open branch is zero - assertEquals(0, result.getBranchFlow1SensitivityValue("g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionDisconnectedBranchBothSides() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - Line l45 = network.getLine("l45"); - l45.getTerminal1().disconnect(); - l45.getTerminal2().disconnect(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l45", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - // sensitivity on an open branch is zero - assertEquals(0, result.getBranchFlow1SensitivityValue("g2", "l45", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testDebug() throws IOException { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); - network.setCaseDate(DateTime.parse("2021-04-25T13:47:34.697+02:00")); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - String debugDir = "/work"; - OpenSensitivityAnalysisParameters sensiParametersExt = new OpenSensitivityAnalysisParameters() - .setDebugDir(debugDir); - sensiParameters.addExtension(OpenSensitivityAnalysisParameters.class, sensiParametersExt); - - List contingencies = List.of(new Contingency("l34", new BranchContingency("l34"))); - - List variableSets = List.of(new SensitivityVariableSet("glsk", - List.of(new WeightedSensitivityVariable("g2", 25f), - new WeightedSensitivityVariable("g6", 40f), - new WeightedSensitivityVariable("d3", 35f)))); - - List factors = List.of(createBranchFlowPerLinearGlsk("l45", "glsk"), - createBranchFlowPerInjectionIncrease("l12", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - Path contingenciesFile = null; - Path factorsFile = null; - Path networkFile = null; - Path parametersFile = null; - Path variableSetsFile = null; - FileSystem fileSystem = PlatformConfig.defaultConfig().getConfigDir().map(Path::getFileSystem).orElseThrow(PowsyblException::new); - PathMatcher contingenciesMatcher = fileSystem.getPathMatcher("glob:contingencies-*.json"); - PathMatcher factorsMatcher = fileSystem.getPathMatcher("glob:factors-*.json"); - PathMatcher networkMatcher = fileSystem.getPathMatcher("glob:network-*.xiidm"); - PathMatcher parametersMatcher = fileSystem.getPathMatcher("glob:parameters-*.json"); - PathMatcher variableSetsMatcher = fileSystem.getPathMatcher("glob:variable-sets-*.json"); - for (Path path : Files.list(fileSystem.getPath(debugDir)).collect(Collectors.toList())) { - if (contingenciesMatcher.matches(path.getFileName())) { - contingenciesFile = path; - } - if (factorsMatcher.matches(path.getFileName())) { - factorsFile = path; - } - if (networkMatcher.matches(path.getFileName())) { - networkFile = path; - } - if (parametersMatcher.matches(path.getFileName())) { - parametersFile = path; - } - if (variableSetsMatcher.matches(path.getFileName())) { - variableSetsFile = path; - } - } - assertNotNull(contingenciesFile); - assertNotNull(factorsFile); - assertNotNull(networkFile); - assertNotNull(parametersFile); - assertNotNull(variableSetsFile); - try (InputStream is = Files.newInputStream(contingenciesFile)) { - ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/debug-contingencies.json")), is); - } - try (InputStream is = Files.newInputStream(factorsFile)) { - ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/debug-factors.json")), is); - } - try (InputStream is = Files.newInputStream(networkFile)) { - ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/debug-network.xiidm")), is); - } - try (InputStream is = Files.newInputStream(parametersFile)) { - ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/debug-parameters.json")), is); - } - try (InputStream is = Files.newInputStream(variableSetsFile)) { - ComparisonUtils.compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/debug-variable-sets.json")), is); - } - - String fileName = contingenciesFile.getFileName().toString(); - String dateStr = fileName.substring(14, fileName.length() - 5); - DateTime date = DateTime.parse(dateStr, DateTimeFormat.forPattern(OpenSensitivityAnalysisProvider.DATE_TIME_FORMAT)); - - List values2 = sensiProvider.replay(date, fileSystem.getPath(debugDir)); - - // assert we have exactly the same result with replay - assertEquals(result.getValues().size(), values2.size()); - Iterator itExpected = values2.iterator(); - - for (SensitivityValue actual : result.getValues()) { - SensitivityValue expected = itExpected.next(); - assertEquals(actual.getContingencyIndex(), expected.getContingencyIndex()); - assertEquals(actual.getValue(), expected.getValue(), LoadFlowAssert.DELTA_POWER); - assertEquals(actual.getFunctionReference(), expected.getFunctionReference(), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testDanglingLineContingencyDistributedSlackOnLoads() { - Network network = BoundaryFactory.createWithLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl3_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "g1")); - - List contingencies = List.of(new Contingency("dl1", new DanglingLineContingency("dl1"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(0.1875, result.getBranchFlow1SensitivityValue("g1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(75.881, result.getBranchFlow1FunctionReferenceValue("l1"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.1875, result.getBranchFlow1SensitivityValue("dl1", "g1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(19.006, result.getBranchFlow1FunctionReferenceValue("dl1", "l1"), LoadFlowAssert.DELTA_POWER); - - network.getDanglingLine("dl1").getTerminal().disconnect(); - Line l1 = network.getLine("l1"); - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - double initialP = l1.getTerminal1().getP(); - assertEquals(19.006, initialP, LoadFlowAssert.DELTA_POWER); - network.getGenerator("g1").setTargetP(network.getGenerator("g1").getTargetP() + 1); - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - double finalP = l1.getTerminal1().getP(); - assertEquals(19.194, finalP, LoadFlowAssert.DELTA_POWER); - assertEquals(0.1875, finalP - initialP, LoadFlowAssert.DELTA_POWER); - } - - @Test - void testDanglingLineContingencyDistributedSlackOnGenerators() { - Network network = BoundaryFactory.createWithLoad(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl3_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "load3")); - - List contingencies = List.of(new Contingency("dl1", new DanglingLineContingency("dl1"))); - - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result2.getPreContingencyValues().size()); - assertEquals(-0.1874, result2.getBranchFlow1SensitivityValue("load3", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(75.813, result2.getBranchFlow1FunctionReferenceValue("l1"), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1875, result2.getBranchFlow1SensitivityValue("dl1", "load3", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1.875, result2.getBranchFlow1FunctionReferenceValue("dl1", "l1"), LoadFlowAssert.DELTA_POWER); - - network.getDanglingLine("dl1").getTerminal().disconnect(); - Line l1 = network.getLine("l1"); - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - double initialP = l1.getTerminal1().getP(); - assertEquals(1.875, initialP, LoadFlowAssert.DELTA_POWER); - network.getLoad("load3").setP0(network.getLoad("load3").getP0() + 1); - runLf(network, sensiParameters.getLoadFlowParameters(), Reporter.NO_OP); - double finalP = l1.getTerminal1().getP(); - assertEquals(2.0624, finalP, LoadFlowAssert.DELTA_POWER); - assertEquals(0.1875, finalP - initialP, LoadFlowAssert.DELTA_POWER); - } - - @Test - void contingencyOnPhaseTapChangerTest() { - Network network = PhaseShifterTestCaseFactory.create(); - - SensitivityAnalysisParameters parameters = createParameters(true, "VL1_0", true); - parameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - TwoWindingsTransformer ps1 = network.getTwoWindingsTransformer("PS1"); - List factors = List.of(createBranchFlowPerPSTAngle("L1", ps1.getId())); - - List contingencies = List.of(new Contingency("PS1", new BranchContingency("PS1"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), parameters); - - assertEquals(100.0, result.getBranchFlow1FunctionReferenceValue("PS1", "L1"), LoadFlowAssert.DELTA_POWER); - assertEquals(0.0, result.getBranchFlow1SensitivityValue("PS1", "PS1", "L1", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskOutsideMainComponentWithContingency() { - Network network = HvdcNetworkFactory.createLccWithBiggerComponentsAndAdditionalLine(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l12", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - List contingencies = List.of(new Contingency("additionnalline_0", new BranchContingency("additionnalline_0"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(100.000, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(100.000, result.getBranchFlow1FunctionReferenceValue("additionnalline_0", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskOutsideMainComponentWithContingencyOnWatchedLine() { - Network network = HvdcNetworkFactory.createLccWithBiggerComponentsAndAdditionalLine(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("l12", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - List contingencies = List.of(new Contingency("l12", new BranchContingency("l12"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(100.0, result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l12", "l12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskOutsideMainComponentWithContingencyOnWatchedLine2() { - Network network = HvdcNetworkFactory.createLccWithBiggerComponentsAndAdditionalLine(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("additionnalline_0", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - List contingencies = List.of(new Contingency("additionnalline_0", new BranchContingency("additionnalline_0"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "additionnalline_0", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0, result.getBranchFlow1FunctionReferenceValue("additionnalline_0"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("additionnalline_0", "additionnalline_0"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskOutsideMainComponentWithContingencyBreakingConnectivity() { - Network network = HvdcNetworkFactory.createLccWithBiggerComponentsAndAdditionalLine2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0"); - - List factors = List.of(createBranchFlowPerLinearGlsk("additionnalline_10", "glsk")); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("g6", 1f), - new WeightedSensitivityVariable("g3", 2f)))); - - List contingencies = List.of(new Contingency("additionnalline_0", new BranchContingency("additionnalline_0"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("glsk", "additionnalline_10", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0, result.getBranchFlow1FunctionReferenceValue("additionnalline_10"), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("additionnalline_0", "additionnalline_10"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadContingencyNotInMainComponent() { - Network network = VoltageControlNetworkFactory.createNetworkWithT2wt(); - network.getTwoWindingsTransformer("T2wT").getTerminal1().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL_1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("LINE_12", "GEN_1")); - - List contingencies = List.of(new Contingency("LOAD_3", new LoadContingency("LOAD_3"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(1.0, result.getBranchFlow1SensitivityValue("GEN_1", "LINE_12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(25.0, result.getBranchFlow1FunctionReferenceValue("LINE_12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.0, result.getBranchFlow1SensitivityValue("LOAD_3", "GEN_1", "LINE_12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(25.0, result.getBranchFlow1FunctionReferenceValue("LOAD_3", "LINE_12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGeneratorContingencyNotInMainComponent() { - Network network = VoltageControlNetworkFactory.createNetworkWithT2wt(); - network.getVoltageLevel("VL_3").newGenerator() - .setId("GEN_3") - .setBus("BUS_3") - .setMinP(0.0) - .setMaxP(10) - .setTargetP(5) - .setTargetV(30) - .setVoltageRegulatorOn(true) - .add(); - network.getTwoWindingsTransformer("T2wT").getTerminal1().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL_1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("LINE_12", "GEN_1")); - - List contingencies = List.of(new Contingency("GEN_3", new GeneratorContingency("GEN_3"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(1.0, result.getBranchFlow1SensitivityValue("GEN_1", "LINE_12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(25.0, result.getBranchFlow1FunctionReferenceValue("LINE_12"), LoadFlowAssert.DELTA_POWER); - assertEquals(1.0, result.getBranchFlow1SensitivityValue("GEN_3", "GEN_1", "LINE_12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(25.0, result.getBranchFlow1FunctionReferenceValue("GEN_3", "LINE_12"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGeneratorContingencyNotInMainComponentAndMonitoredBranchNotInMainComponent() { - Network network = VoltageControlNetworkFactory.createNetworkWithT2wt(); - network.getVoltageLevel("VL_3").newGenerator() - .setId("GEN_3") - .setBus("BUS_3") - .setMinP(0.0) - .setMaxP(10) - .setTargetP(5) - .setTargetV(30) - .setVoltageRegulatorOn(true) - .add(); - network.getTwoWindingsTransformer("T2wT").getTerminal1().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL_1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("T2wT", "GEN_1")); - - List contingencies = List.of(new Contingency("GEN_3", new GeneratorContingency("GEN_3"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(0.0, result.getBranchFlow1SensitivityValue("GEN_1", "T2wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("T2wT"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testVariableNotInMainComponentAndMonitoredBranchNotInMainComponent() { - Network network = VoltageControlNetworkFactory.createNetworkWithT2wt(); - network.getVoltageLevel("VL_3").newGenerator() - .setId("GEN_3") - .setBus("BUS_3") - .setMinP(0.0) - .setMaxP(10) - .setTargetP(5) - .setTargetV(30) - .setVoltageRegulatorOn(true) - .add(); - network.getTwoWindingsTransformer("T2wT").getTerminal1().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL_1_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("T2wT", "LOAD_3")); - - List contingencies = List.of(new Contingency("GEN_3", new GeneratorContingency("GEN_3"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getPreContingencyValues().size()); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("LOAD_3", "T2wT", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("T2wT"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGLSK() { - Network network = FourBusNetworkFactory.create(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("g1", new GeneratorContingency("g1")), new Contingency("g4", new GeneratorContingency("g4"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - network.getGenerator("g1").getTerminal().disconnect(); - List variables2 = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f)); - List variableSets2 = Collections.singletonList(new SensitivityVariableSet("glsk", variables2)); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), variableSets2, sensiParameters); - - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g1", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l14"), result.getBranchFlow1FunctionReferenceValue("g1", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("g1", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("g1", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("g1", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("g1", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("g1", "l13"), LoadFlowAssert.DELTA_POWER); - - network.getGenerator("g1").getTerminal().connect(); - network.getGenerator("g4").getTerminal().disconnect(); - SensitivityAnalysisResult result3 = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(result3.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("g4", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGLSK2() { - Network network = FourBusNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("d3", new LoadContingency("d3"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - network.getLoad("d3").getTerminal().disconnect(); - List variables2 = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets2 = Collections.singletonList(new SensitivityVariableSet("glsk", variables2)); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), variableSets2, sensiParameters); - - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d3", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l14"), result.getBranchFlow1FunctionReferenceValue("d3", "l14"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("d3", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l23"), result.getBranchFlow1FunctionReferenceValue("d3", "l23"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("d3", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l34"), result.getBranchFlow1FunctionReferenceValue("d3", "l34"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l13"), result.getBranchFlow1FunctionReferenceValue("d3", "l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGLSK3() { - // FIXME - // In case of a contingency that involves a generator or a load. The GLSK should contains - // this contingency element and an other one connected to the same bus. - Network network = FourBusNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - List contingencies = List.of(new Contingency("d2", new LoadContingency("d2"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - - network.getLoad("d2").getTerminal().disconnect(); - List variables2 = List.of(new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets2 = Collections.singletonList(new SensitivityVariableSet("glsk", variables2)); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), variableSets2, sensiParameters); - - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertNotEquals(result2.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("d2", "glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLosingALineButBothEndsInMainComponent() { - Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorOnOneSide(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", false); - - List contingencies = List.of(new Contingency("l34+l12", new BranchContingency("l34"), new BranchContingency("l12"))); - - List factors = createFactorMatrix(List.of(network.getGenerator("g3")), - List.of(network.getLine("l12")), - "l34+l12"); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues("l34+l12").size()); - - assertEquals(0.0, result.getBranchFlow1SensitivityValue("l34+l12", "g3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("l34+l12", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(SensitivityAnalysisResult.Status.SUCCESS, result.getContingencyStatus("l34+l12")); - } - - @Test - void testContingencyOnHvdcInAcEmulation() { - Network network = HvdcNetworkFactory.createWithHvdcInAcEmulation(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - network.getHvdcLine("hvdc34").newExtension(HvdcAngleDroopActivePowerControlAdder.class) - .withDroop(180) - .withP0(0.f) - .withEnabled(true) - .add(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - - List factors = createFactorMatrix(Stream.of("g1", "g5").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("l12", "l25", "l56").map(network::getBranch).collect(Collectors.toList())); - - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(SensitivityAnalysisResult.Status.SUCCESS, result.getContingencyStatus("hvdc34")); - - network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect(); - network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect(); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(6, result.getValues("hvdc34").size()); - List contingencyResult = result.getValues("hvdc34"); - assertEquals(6, contingencyResult.size()); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g1", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l25", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1SensitivityValue("g5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), result.getBranchFlow1SensitivityValue("hvdc34", "g5", "l56", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l12"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l25"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l25"), LoadFlowAssert.DELTA_POWER); - assertEquals(result2.getBranchFlow1FunctionReferenceValue(null, "l56"), result.getBranchFlow1FunctionReferenceValue("hvdc34", "l56"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testIeeeCdf14() { - Network network = IeeeCdfNetworkFactory.create14(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(5000)); - SensitivityAnalysisParameters sensiParameters = createParameters(true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setAddRatioToLinesWithDifferentNominalVoltageAtBothEnds(false); - List factors = createFactorMatrix(Stream.of("B1-G", "B2-G", "B3-G").map(network::getGenerator).collect(Collectors.toList()), - Stream.of("L1-5-1", "L2-3-1").map(network::getBranch).collect(Collectors.toList())); - List contingencies = List.of(new Contingency("L1-2-1", new BranchContingency("L1-2-1"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(72.247, result.getBranchFlow1FunctionReferenceValue(null, "L1-5-1"), LoadFlowAssert.DELTA_POWER); - assertEquals(69.831, result.getBranchFlow1FunctionReferenceValue(null, "L2-3-1"), LoadFlowAssert.DELTA_POWER); - - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setAddRatioToLinesWithDifferentNominalVoltageAtBothEnds(true); - SensitivityAnalysisResult result2 = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - - assertEquals(72.224, result2.getBranchFlow1FunctionReferenceValue(null, "L1-5-1"), LoadFlowAssert.DELTA_POWER); - assertEquals(69.850, result2.getBranchFlow1FunctionReferenceValue(null, "L2-3-1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testSwitchContingency() { - Network network = NodeBreakerNetworkFactory.create(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL1_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("L1", "LD")); - - List contingencies = List.of(new Contingency("C", new SwitchContingency("C"))); - - List variableSets = Collections.emptyList(); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters); - assertEquals(-0.500, result.getBranchFlow1SensitivityValue("LD", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1.000, result.getBranchFlow1SensitivityValue("C", "LD", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(300.0, result.getBranchFlow1FunctionReferenceValue("L1"), LoadFlowAssert.DELTA_POWER); - assertEquals(-3.770, result.getBranchFlow1FunctionReferenceValue("C", "L1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testFunctionDisconnectedBranchBothSidesWithContingency() { - Network network = ConnectedComponentNetworkFactory.createTwoCcLinkedByTwoLines(); - Line l45 = network.getLine("l45"); - l45.getTerminal1().disconnect(); - l45.getTerminal2().disconnect(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l45", "g2")); - List contingencies = List.of(new Contingency("C", List.of(new BranchContingency("l24"), new BranchContingency("l35")))); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(SensitivityAnalysisResult.Status.SUCCESS, result.getContingencyStatus("C")); - } - - @Test - void testPredefinedResults() { - // Load and generator in contingency - Network network = FourBusNetworkFactory.create(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - List factors = List.of(createBranchFlowPerInjectionIncrease("l14", "g1"), - createBranchFlowPerInjectionIncrease("l14", "d2")); - List contingencies = List.of(new Contingency("g1", new GeneratorContingency("g1")), new Contingency("d2", new LoadContingency("d2"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBranchFlow1SensitivityValue("g1", "g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("d2", "d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testPredefinedResults2() { - // LCC line in contingency - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(3 * gen.getMaxP())); - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().getExtension(OpenLoadFlowParameters.class).setSlackBusPMaxMismatch(0.001); - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l25"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - List contingencies = List.of(new Contingency("hvdc34", new HvdcLineContingency("hvdc34"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0.9889, result.getBranchFlow1SensitivityValue("hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0, result.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - // VSC line in contingency - Network network2 = HvdcNetworkFactory.createNetworkWithGenerators2(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - SensitivityAnalysisResult result2 = sensiRunner.run(network2, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result2.getBranchFlow1SensitivityValue("hvdc34", "hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testPredefinedResults3() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - List factors = List.of(createBranchFlowPerPSTAngle("l12", "l23", "l23"), - createBranchFlowPerPSTAngle("l23", "l23", "l23")); - List contingencies = Collections.singletonList(new Contingency("l23", new BranchContingency("l23"))); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(0, result.getBranchFlow1SensitivityValue("l23", "l23", "l12", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1SensitivityValue("l23", "l23", "l23", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } -} diff --git a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java deleted file mode 100644 index c7d9e2b1db..0000000000 --- a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java +++ /dev/null @@ -1,986 +0,0 @@ -/** - * Copyright (c) 2020, 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.openloadflow.sensi.dc; - -import com.powsybl.commons.PowsyblException; -import com.powsybl.contingency.BranchContingency; -import com.powsybl.contingency.Contingency; -import com.powsybl.contingency.ContingencyContext; -import com.powsybl.contingency.DanglingLineContingency; -import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; -import com.powsybl.iidm.network.test.PhaseShifterTestCaseFactory; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.network.BoundaryFactory; -import com.powsybl.openloadflow.network.FourBusNetworkFactory; -import com.powsybl.openloadflow.network.HvdcNetworkFactory; -import com.powsybl.openloadflow.network.NodeBreakerNetworkFactory; -import com.powsybl.openloadflow.network.impl.PropagatedContingency; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.util.LoadFlowAssert; -import com.powsybl.sensitivity.*; -import org.junit.jupiter.api.Test; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletionException; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Geoffroy Jamgotchian - */ -class DcSensitivityAnalysisTest extends AbstractSensitivityAnalysisTest { - - @Test - void testEsgTuto() { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VLLOAD_0"); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getLineStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4buses() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - Map functionReferenceByLine = new HashMap<>(); - for (Line line : network.getLines()) { - functionReferenceByLine.put(line.getId(), line.getTerminal1().getP()); - } - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0"); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), Branch.Side.ONE); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - //Check sensitivity values for side one - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.125d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.375d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.625d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.375d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.125d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.125d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.625d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - for (Line line : network.getLines()) { - assertEquals(functionReferenceByLine.get(line.getId()), result.getBranchFlow1FunctionReferenceValue(line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void test4busesSide2() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - Map functionReferenceByLine = new HashMap<>(); - for (Line line : network.getLines()) { - functionReferenceByLine.put(line.getId(), line.getTerminal2().getP()); - } - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0"); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList()), Branch.Side.TWO); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - - //Check sensitivity values for side two - assertEquals(-0.25d, result.getBranchFlow2SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow2SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow2SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow2SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.5d, result.getBranchFlow2SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.125d, result.getBranchFlow2SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.375d, result.getBranchFlow2SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.625d, result.getBranchFlow2SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.125d, result.getBranchFlow2SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow2SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.375d, result.getBranchFlow2SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow2SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow2SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.625d, result.getBranchFlow2SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow2SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - for (Line line : network.getLines()) { - assertEquals(functionReferenceByLine.get(line.getId()), result.getBranchFlow2FunctionReferenceValue(line.getId()), LoadFlowAssert.DELTA_POWER); - } - } - - @Test - void testGeneratorInjection4busesDistributed() { - // The factors are generators injections - Network network = FourBusNetworkFactory.create(); - for (Generator generator : network.getGenerators()) { - generator.setMaxP(generator.getTargetP() + 0.5); - } - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(0.192d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.269d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.115d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.038d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.154d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.067d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.356d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.260d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.163d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.096d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.433d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.144d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.24d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.337d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.096d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGeneratorInjection4busesDistributed2() { - // The factors are generators injections - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(0.175d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.275d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.025d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.450d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadInjection4busesDistributed() { - // test injection increase on loads - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(10, result.getValues().size()); - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("d2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("d2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("d2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("d2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.075d, result.getBranchFlow1SensitivityValue("d3", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.025d, result.getBranchFlow1SensitivityValue("d3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.375d, result.getBranchFlow1SensitivityValue("d3", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.275d, result.getBranchFlow1SensitivityValue("d3", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("d3", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testSeveralGeneratorsConnectedToTheSameBus() { - // test injection increase on loads - Network network = FourBusNetworkFactory.createWithTwoGeneratorsAtBus2(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(10, result.getValues().size()); - assertEquals(0.045d, result.getBranchFlow1SensitivityValue("d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.318d, result.getBranchFlow1SensitivityValue("d2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.227d, result.getBranchFlow1SensitivityValue("d2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.136d, result.getBranchFlow1SensitivityValue("d2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.09d, result.getBranchFlow1SensitivityValue("d2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGeneratorInjection4busesDistributedOnLoad() { - // test injection increase on loads - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(0.225d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.325d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.125d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.225d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.45d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.1d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.3d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.4d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.6d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadInjection4busesDistributedOnLoad() { - // test injection increase on loads - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(10, result.getValues().size()); - assertEquals(0.1d, result.getBranchFlow1SensitivityValue("d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.3d, result.getBranchFlow1SensitivityValue("d2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.5d, result.getBranchFlow1SensitivityValue("d2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("d2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("d2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.025d, result.getBranchFlow1SensitivityValue("d3", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.075d, result.getBranchFlow1SensitivityValue("d3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow1SensitivityValue("d3", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.025d, result.getBranchFlow1SensitivityValue("d3", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.05d, result.getBranchFlow1SensitivityValue("d3", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void test4busesDistributedPartialFactors() { - // test that the sensitivity computation does not make assumption about the presence of all factors - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getGeneratorStream().filter(gen -> gen.getId().equals("g1")).collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(0.175d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.275d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.025d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadInjectionWithoutGenerator() { - // test injection increase on loads - Network network = FourBusNetworkFactory.createBaseNetwork(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(10, result.getValues().size()); - assertEquals(0.083d, result.getBranchFlow1SensitivityValue("d2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.583d, result.getBranchFlow1SensitivityValue("d2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.417d, result.getBranchFlow1SensitivityValue("d2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("d2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.167d, result.getBranchFlow1SensitivityValue("d2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.0416d, result.getBranchFlow1SensitivityValue("d3", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2083d, result.getBranchFlow1SensitivityValue("d3", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.2083d, result.getBranchFlow1SensitivityValue("d3", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.375d, result.getBranchFlow1SensitivityValue("d3", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.416d, result.getBranchFlow1SensitivityValue("d3", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadInjectionOnSlackBusDistributed() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = createFactorMatrix(network.getGeneratorStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(15, result.getValues().size()); - assertEquals(0.175d, result.getBranchFlow1SensitivityValue("g1", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.275d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.125d, result.getBranchFlow1SensitivityValue("g1", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.025d, result.getBranchFlow1SensitivityValue("g1", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g1", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(0.05d, result.getBranchFlow1SensitivityValue("g2", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.25d, result.getBranchFlow1SensitivityValue("g2", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g2", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(-0.450d, result.getBranchFlow1SensitivityValue("g4", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.15d, result.getBranchFlow1SensitivityValue("g4", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.25d, result.getBranchFlow1SensitivityValue("g4", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.35d, result.getBranchFlow1SensitivityValue("g4", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-0.1d, result.getBranchFlow1SensitivityValue("g4", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGLSK() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(-7d / 40d, result.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-3d / 8d, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 40d, result.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(7d / 40d, result.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-7d / 20d, result.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testGlskOnSlackBusDistributed() { - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List variables = List.of(new WeightedSensitivityVariable("d2", 30f), - new WeightedSensitivityVariable("g2", 10f), - new WeightedSensitivityVariable("d3", 50f), - new WeightedSensitivityVariable("g1", 10f)); - List variableSets = Collections.singletonList(new SensitivityVariableSet("glsk", variables)); - - List factors = network.getBranchStream().map(branch -> createBranchFlowPerLinearGlsk(branch.getId(), "glsk")).collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(5, result.getValues().size()); - assertEquals(0d, result.getBranchFlow1SensitivityValue("glsk", "l14", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 10d, result.getBranchFlow1SensitivityValue("glsk", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 10d, result.getBranchFlow1SensitivityValue("glsk", "l23", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(1d / 5d, result.getBranchFlow1SensitivityValue("glsk", "l34", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(-1d / 5d, result.getBranchFlow1SensitivityValue("glsk", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLoadInjectionOnSlackBus() { - // test injection increase on loads - Network network = FourBusNetworkFactory.createBaseNetwork(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b2_vl_0", false); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(0.0d, result.getBranchFlow1SensitivityValue("d2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testVscInjection() { - // test injection increase on loads - Network network = HvdcNetworkFactory.createVsc(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0", false); - - List factors = network.getBranchStream() - .map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), network.getVscConverterStation("cs2").getId())) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(-1d, result.getBranchFlow1SensitivityValue("cs2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testLccInjection() { - // test injection increase on loads - Network network = HvdcNetworkFactory.createLcc(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0", false); - - List factors = network.getBranchStream() - .map(branch -> createBranchFlowPerInjectionIncrease(branch.getId(), network.getLccConverterStation("cs2").getId())) - .collect(Collectors.toList()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(-1d, result.getBranchFlow1SensitivityValue("cs2", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensi() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, List.of("b1_vl_0", "b4_vl_0"), false); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcWithGenerators(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createTwoCcLinkedByAHvdcWithGenerators(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithBothSides() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", false); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l25"), result.getBranchFlow1SensitivityValue("hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l45"), result.getBranchFlow1SensitivityValue("hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l46"), result.getBranchFlow1SensitivityValue("hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l56"), result.getBranchFlow1SensitivityValue("hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiWithBothSidesDistributed() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createNetworkWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createNetworkWithGenerators(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23", "l25", "l45", "l46", "l56"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l25"), result.getBranchFlow1SensitivityValue("hvdc34", "l25", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l45"), result.getBranchFlow1SensitivityValue("hvdc34", "l45", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l46"), result.getBranchFlow1SensitivityValue("hvdc34", "l46", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l56"), result.getBranchFlow1SensitivityValue("hvdc34", "l56", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiVsc() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, List.of("b1_vl_0", "b4_vl_0"), true); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(2 * gen.getMaxP())); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcSensiVsc2() { - SensitivityAnalysisParameters sensiParameters = createParameters(true, List.of("b1_vl_0", "b4_vl_0"), true); - - // test injection increase on loads - Network network = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - network.getHvdcLine("hvdc34").setConvertersMode(HvdcLine.ConvertersMode.SIDE_1_RECTIFIER_SIDE_2_INVERTER); - network.getGeneratorStream().forEach(gen -> gen.setMaxP(5 * gen.getMaxP())); - runLf(network, sensiParameters.getLoadFlowParameters()); - - Network network1 = HvdcNetworkFactory.createTwoCcLinkedByAHvdcVscWithGenerators(); - network1.getHvdcLine("hvdc34").setConvertersMode(HvdcLine.ConvertersMode.SIDE_1_RECTIFIER_SIDE_2_INVERTER); - network1.getHvdcLine("hvdc34").setActivePowerSetpoint(network1.getHvdcLine("hvdc34").getActivePowerSetpoint() + SENSI_CHANGE); - network1.getGeneratorStream().forEach(gen -> gen.setMaxP(5 * gen.getMaxP())); - runLf(network1, sensiParameters.getLoadFlowParameters()); - Map loadFlowDiff = network.getLineStream().map(Identifiable::getId) - .collect(Collectors.toMap( - lineId -> lineId, - line -> (network1.getLine(line).getTerminal1().getP() - network.getLine(line).getTerminal1().getP()) / SENSI_CHANGE - )); - - List factors = SensitivityFactor.createMatrix(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, List.of("l12", "l13", "l23"), - SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, List.of("hvdc34"), - false, ContingencyContext.all()); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(loadFlowDiff.get("l12"), result.getBranchFlow1SensitivityValue("hvdc34", "l12", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l13"), result.getBranchFlow1SensitivityValue("hvdc34", "l13", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(loadFlowDiff.get("l23"), result.getBranchFlow1SensitivityValue("hvdc34", "l23", SensitivityVariableType.HVDC_LINE_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testHvdcInjectionNotFound() { - testHvdcInjectionNotFound(true); - } - - @Test - void testBalanceTypeNotSupported() { - // test injection increase on loads - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD); - - List factors = createFactorMatrix(network.getLoadStream().collect(Collectors.toList()), - network.getBranchStream().collect(Collectors.toList())); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException exception = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(exception.getCause() instanceof UnsupportedOperationException); - assertEquals("Unsupported balance type mode: PROPORTIONAL_TO_CONFORM_LOAD", exception.getCause().getMessage()); - } - - @Test - void testPhaseShifter() { - Network network = PhaseShifterTestCaseFactory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL2_0"); - - List factors = List.of(createBranchFlowPerPSTAngle("L1", "PS1")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(-6.3d, result.getBranchFlow1SensitivityValue("PS1", "L1", SensitivityVariableType.TRANSFORMER_PHASE), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testAdditionalFactors() { - // test injection increase on loads - Network network = FourBusNetworkFactory.create(); - runDcLf(network); - - Map functionReferenceByLine = new HashMap<>(); - for (Line line : network.getLines()) { - functionReferenceByLine.put(line.getId(), line.getTerminal1().getP()); - } - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b3_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l12", "g1"), - createBranchFlowPerInjectionIncrease("l13", "g2")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(0.325d, result.getBranchFlow1SensitivityValue("g1", "l12", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0.2d, result.getBranchFlow1SensitivityValue("g2", "l13", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - assertEquals(functionReferenceByLine.get("l12"), result.getBranchFlow1FunctionReferenceValue("l12"), LoadFlowAssert.DELTA_POWER); - assertEquals(functionReferenceByLine.get("l13"), result.getBranchFlow1FunctionReferenceValue("l13"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testInjectionNotFoundAdditionalFactor() { - testInjectionNotFoundAdditionalFactor(true); - } - - @Test - void testIntensityCrash() { - Network network = FourBusNetworkFactory.createWithPhaseTapChangerAndGeneratorAtBus2(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "b1_vl_0", true); - sensiParameters.getLoadFlowParameters().setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX); - - List factors = network.getBranchStream().map(branch -> createBranchIntensityPerPSTAngle(branch.getId(), "l23")).collect(Collectors.toList()); - - List contingencies = Collections.emptyList(); - List variableSets = Collections.emptyList(); - - CompletionException e = assertThrows(CompletionException.class, () -> sensiRunner.run(network, factors, contingencies, variableSets, sensiParameters)); - assertTrue(e.getCause() instanceof PowsyblException); - assertEquals("Only variables of type TRANSFORMER_PHASE, INJECTION_ACTIVE_POWER and HVDC_LINE_ACTIVE_POWER, and functions of type BRANCH_ACTIVE_POWER_1 and BRANCH_ACTIVE_POWER_2 are yet supported in DC", e.getCause().getMessage()); - } - - @Test - void testBranchFunctionOutsideMainComponent() { - testBranchFunctionOutsideMainComponent(true); - } - - @Test - void testInjectionOutsideMainComponent() { - testInjectionOutsideMainComponent(true); - } - - @Test - void testPhaseShifterOutsideMainComponent() { - testPhaseShifterOutsideMainComponent(true); - } - - @Test - void testGlskOutsideMainComponent() { - testGlskOutsideMainComponent(true); - } - - @Test - void testGlskAndLineOutsideMainComponent() { - testGlskAndLineOutsideMainComponent(true); - } - - @Test - void testGlskPartiallyOutsideMainComponent() { - testGlskPartiallyOutsideMainComponent(true); - } - - @Test - void testInjectionNotFound() { - testInjectionNotFound(true); - } - - @Test - void testBranchNotFound() { - testBranchNotFound(true); - } - - @Test - void testEmptyFactors() { - testEmptyFactors(true); - } - - @Test - void testGlskNotFound() { - testGlskInjectionNotFound(true); - } - - @Test - void testDanglingLineSensi() { - Network network = BoundaryFactory.createWithLoad(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "vl1_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("l1", "dl1"), - createBranchFlowPerInjectionIncrease("dl1", "load3")); - - // dangling line is connected - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(-0.812d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(91.0, result.getBranchFlow1FunctionReferenceValue("dl1"), LoadFlowAssert.DELTA_POWER); - - // dangling line is connected on base case but will be disconnected by a contingency => 0 - List contingencies = List.of(new Contingency("c", new DanglingLineContingency("dl1"))); - result = sensiRunner.run(network, factors, contingencies, Collections.emptyList(), sensiParameters); - assertEquals(-0.812d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(0d, result.getBranchFlow1SensitivityValue("c", "dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - - // dangling line is disconnected on base case => 0 - network.getDanglingLine("dl1").getTerminal().disconnect(); - result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - assertEquals(0d, result.getBranchFlow1SensitivityValue("dl1", "l1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testContingencyOnOpenLine() { - Network network = NodeBreakerNetworkFactory.create(); - List contingencies = List.of(new Contingency("c1", new BranchContingency("L1"))); - - List propagatedContingencies = PropagatedContingency.createList(network, contingencies, - Collections.emptySet(), false, false, false, false); - assertEquals(1, propagatedContingencies.size()); - } - - @Test - void testOpenMonitoredBranch() { - Network network = EurostagTutorialExample1Factory.create(); - runDcLf(network); - network.getLine("NHV1_NHV2_1").getTerminal2().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VLLOAD_0"); - - List variables = List.of(new WeightedSensitivityVariable("LOAD", 10f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = List.of(createBranchFlowPerLinearGlsk("NHV1_NHV2_1", "glsk")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(0., result.getBranchFlow1SensitivityValue("glsk", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("NHV1_NHV2_1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testOpenMonitoredBranch2() { - Network network = EurostagTutorialExample1Factory.create(); - runDcLf(network); - network.getLine("NHV1_NHV2_1").getTerminal1().disconnect(); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VLLOAD_0"); - - List variables = List.of(new WeightedSensitivityVariable("LOAD", 10f)); - List variableSets = List.of(new SensitivityVariableSet("glsk", variables)); - - List factors = List.of(createBranchFlowPerLinearGlsk("NHV1_NHV2_1", "glsk")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(0., result.getBranchFlow1SensitivityValue("glsk", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(Double.NaN, result.getBranchFlow1FunctionReferenceValue("NHV1_NHV2_1"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void nonImpedantBranchTest() { - Network network = PhaseShifterTestCaseFactory.create(); - network.getLine("L2").setX(0).setR(0); - - SensitivityAnalysisParameters sensiParameters = createParameters(true); - - List variableSets = List.of(new SensitivityVariableSet("glsk", List.of(new WeightedSensitivityVariable("LD2", 10f)))); - - List factors = List.of(createBranchFlowPerLinearGlsk("L2", "glsk")); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), variableSets, sensiParameters); - - assertEquals(-0.6666666, result.getBranchFlow1SensitivityValue("glsk", "L2", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - assertEquals(66.6666, result.getBranchFlow1FunctionReferenceValue("L2"), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConfiguredBusFactor() { - Network network = EurostagTutorialExample1Factory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VLLOAD_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("NHV1_NHV2_1", "GEN"), - new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, - "NHV1_NHV2_1", - SensitivityVariableType.INJECTION_ACTIVE_POWER, - "NGEN", - false, - ContingencyContext.all())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(result.getBranchFlow1SensitivityValue("GEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), - result.getBranchFlow1SensitivityValue("NGEN", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), - LoadFlowAssert.DELTA_POWER); - } - - @Test - void testConfiguredBusInvalidFactor() { - Network network = EurostagTutorialExample1Factory.create(); - network.getVoltageLevel("VLGEN").getBusBreakerView().newBus() - .setId("X") - .add(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VLLOAD_0"); - - List factors = List.of(new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, - "NHV1_NHV2_1", - SensitivityVariableType.INJECTION_ACTIVE_POWER, - "X", - false, - ContingencyContext.all())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("X", "NHV1_NHV2_1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } - - @Test - void testBusbarSectionFactor() { - Network network = NodeBreakerNetworkFactory.create(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL2_0"); - - List factors = List.of(createBranchFlowPerInjectionIncrease("L1", "G"), - new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, - "L1", - SensitivityVariableType.INJECTION_ACTIVE_POWER, - "BBS2", - false, - ContingencyContext.all())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(2, result.getValues().size()); - assertEquals(result.getBranchFlow1SensitivityValue("G", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), - result.getBranchFlow1SensitivityValue("BBS2", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), - LoadFlowAssert.DELTA_POWER); - } - - @Test - void testBusbarSectionInvalidFactor() { - Network network = NodeBreakerNetworkFactory.create(); - network.getVoltageLevel("VL1").getNodeBreakerView().newBusbarSection() - .setId("X") - .setNode(100) - .add(); - runAcLf(network); - - SensitivityAnalysisParameters sensiParameters = createParameters(true, "VL2_0"); - - List factors = List.of(new SensitivityFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, - "L1", - SensitivityVariableType.INJECTION_ACTIVE_POWER, - "X", - false, - ContingencyContext.all())); - - SensitivityAnalysisResult result = sensiRunner.run(network, factors, Collections.emptyList(), Collections.emptyList(), sensiParameters); - - assertEquals(1, result.getValues().size()); - assertEquals(0, result.getBranchFlow1SensitivityValue("X", "L1", SensitivityVariableType.INJECTION_ACTIVE_POWER), LoadFlowAssert.DELTA_POWER); - } -}