diff --git a/open-reac/src/main/java/com/powsybl/openreac/OpenReacRunner.java b/open-reac/src/main/java/com/powsybl/openreac/OpenReacRunner.java index 2d757fb1..907bdd8e 100644 --- a/open-reac/src/main/java/com/powsybl/openreac/OpenReacRunner.java +++ b/open-reac/src/main/java/com/powsybl/openreac/OpenReacRunner.java @@ -25,7 +25,6 @@ import com.powsybl.openloadflow.dc.equations.DcApproximationType; import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.network.impl.LfNetworkLoaderImpl; -import com.powsybl.openloadflow.network.util.UniformValueVoltageInitializer; import com.powsybl.openloadflow.network.util.VoltageInitializer; import com.powsybl.openreac.parameters.OpenReacAmplIOFiles; import com.powsybl.openreac.parameters.input.OpenReacParameters; @@ -184,7 +183,7 @@ private static void checkParameters(Network network, String variantId, OpenReacP parameters.checkIntegrity(network, Reports.createParameterIntegrityReporter(reportNode, network.getId())); } - private static void initializeVoltageBeforeOptimization(Network network, OpenReacParameters openReacParameters) { + public static void initializeVoltageBeforeOptimization(Network network, OpenReacParameters openReacParameters) { // gsk to only distribute on generators network.getGeneratorStream() .forEach(generator -> generator.newExtension(ActivePowerControlAdder.class) @@ -198,34 +197,43 @@ private static void initializeVoltageBeforeOptimization(Network network, OpenRea LfNetwork lfNetwork = LfNetwork.load(network, new LfNetworkLoaderImpl(), slackBusSelector).get(0); // get initializer depending on user option - VoltageInitializer initializer; MatrixFactory matrixFactory = new SparseMatrixFactory(); switch (openReacParameters.getVoltageInitialization()) { // uniform voltage initializer, for flat start voltage angles - case UNIFORM_VALUES -> initializer = new UniformValueVoltageInitializer(); + case UNIFORM_VALUES -> { + for (LfBus lfBus : lfNetwork.getBuses()) { + lfBus.setV(1); + lfBus.setAngle(0); + } + } // direct current initializer, to initialize voltage angles - case DC_VALUES -> initializer = new DcValueVoltageInitializer(new LfNetworkParameters().setSlackBusSelector(slackBusSelector), - true, + case DC_VALUES -> { + VoltageInitializer initializer = new DcValueVoltageInitializer(new LfNetworkParameters().setSlackBusSelector(slackBusSelector), + true, + LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_PARTICIPATION_FACTOR, + true, + DcApproximationType.IGNORE_R, + matrixFactory, + 1); + initializer.prepare(lfNetwork); + } + // full voltage initializer, to initialize voltage magnitudes and angles + case FULL_VOLTAGE -> { + VoltageInitializer initializer = new FullVoltageInitializer( + new VoltageMagnitudeInitializer(false, matrixFactory, openReacParameters.getLowImpedanceThreshold()), + new DcValueVoltageInitializer(new LfNetworkParameters().setSlackBusSelector(slackBusSelector), + true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_PARTICIPATION_FACTOR, true, DcApproximationType.IGNORE_R, matrixFactory, - 1); - // full voltage initializer, to initialize voltage magnitudes and angles - case FULL_VOLTAGE -> initializer = new FullVoltageInitializer( - new VoltageMagnitudeInitializer(false, matrixFactory, openReacParameters.getLowImpedanceThreshold()), - new DcValueVoltageInitializer(new LfNetworkParameters().setSlackBusSelector(slackBusSelector), - true, - LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_PARTICIPATION_FACTOR, - true, - DcApproximationType.IGNORE_R, - matrixFactory, - 1)); + 1)); + initializer.prepare(lfNetwork); + } default -> throw new IllegalStateException("Unexpected value: " + openReacParameters.getVoltageInitialization()); } // initialize voltage values - initializer.prepare(lfNetwork); // update the network with initialization LfNetworkStateUpdateParameters updateParameters = new LfNetworkStateUpdateParameters(false, false, true, false, false, diff --git a/open-reac/src/test/java/com/powsybl/openreac/OpenReacParametersTest.java b/open-reac/src/test/java/com/powsybl/openreac/OpenReacParametersTest.java index dd3e89a1..a922b98c 100644 --- a/open-reac/src/test/java/com/powsybl/openreac/OpenReacParametersTest.java +++ b/open-reac/src/test/java/com/powsybl/openreac/OpenReacParametersTest.java @@ -389,6 +389,21 @@ void testBusesWithReactiveSlackConfigIntegrity() { assertEquals("ALL", parameters.getReactiveSlackBusesMode().toParam().getValue()); } + @Test + void testVoltageInitializationIntegrity() { + OpenReacParameters parameters = new OpenReacParameters(); + + assertThrows(NullPointerException.class, () -> parameters.setVoltageInitialization(null)); + parameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.DC_VALUES); + assertEquals(OpenReacParameters.OpenReacVoltageInitialization.DC_VALUES, parameters.getVoltageInitialization()); + parameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.PREVIOUS_VALUES); + assertEquals(OpenReacParameters.OpenReacVoltageInitialization.PREVIOUS_VALUES, parameters.getVoltageInitialization()); + parameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.UNIFORM_VALUES); + assertEquals(OpenReacParameters.OpenReacVoltageInitialization.UNIFORM_VALUES, parameters.getVoltageInitialization()); + parameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.FULL_VOLTAGE); + assertEquals(OpenReacParameters.OpenReacVoltageInitialization.FULL_VOLTAGE, parameters.getVoltageInitialization()); + } + @Test void testDefaultParametersValuesIntegrity() { OpenReacParameters parameters = new OpenReacParameters(); @@ -413,6 +428,7 @@ void testDefaultParametersValuesIntegrity() { assertEquals(1e-1, parameters.getReactiveSlackVariableScalingFactor()); assertEquals(1e-3, parameters.getTwoWindingTransformerRatioVariableScalingFactor()); assertEquals(1e-1, parameters.getShuntVariableScalingFactor()); + assertEquals(OpenReacParameters.OpenReacVoltageInitialization.FULL_VOLTAGE, parameters.getVoltageInitialization()); assertTrue(parameters.checkAlgorithmParametersIntegrity()); } diff --git a/open-reac/src/test/java/com/powsybl/openreac/OpenReacVoltageInitializationTest.java b/open-reac/src/test/java/com/powsybl/openreac/OpenReacVoltageInitializationTest.java new file mode 100644 index 00000000..d9ac1bb9 --- /dev/null +++ b/open-reac/src/test/java/com/powsybl/openreac/OpenReacVoltageInitializationTest.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2024, 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.openreac; + +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Network; +import com.powsybl.openreac.network.VoltageControlNetworkFactory; +import com.powsybl.openreac.parameters.input.OpenReacParameters; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class OpenReacVoltageInitializationTest { + + final double deltaVoltage = 1e-3; + OpenReacParameters openReacParameters; + Network network; + Bus b1; + Bus b2; + Bus b3; + + @BeforeEach + void setUp() { + openReacParameters = new OpenReacParameters(); + network = VoltageControlNetworkFactory.createNetworkWithT2wt(); + b1 = network.getBusBreakerView().getBus("BUS_1"); + b2 = network.getBusBreakerView().getBus("BUS_2"); + b3 = network.getBusBreakerView().getBus("BUS_3"); + } + + @Test + void testDcVoltageInitialization() { + openReacParameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.DC_VALUES); + OpenReacRunner.initializeVoltageBeforeOptimization(network, openReacParameters); + assertEquals(Double.NaN, b1.getV()); + assertEquals(0.822, b1.getAngle(), deltaVoltage); + assertEquals(Double.NaN, b2.getV()); + assertEquals(0, b2.getAngle(), deltaVoltage); + assertEquals(Double.NaN, b3.getV()); + assertEquals(-2.923, b3.getAngle(), deltaVoltage); + } + + @Test + void testUniformVoltageInitialization() { + openReacParameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.UNIFORM_VALUES); + OpenReacRunner.initializeVoltageBeforeOptimization(network, openReacParameters); + assertEquals(b1.getVoltageLevel().getNominalV(), b1.getV()); + assertEquals(0, b1.getAngle()); + assertEquals(b2.getVoltageLevel().getNominalV(), b2.getV()); + assertEquals(0, b2.getAngle()); + assertEquals(b3.getVoltageLevel().getNominalV(), b3.getV()); + assertEquals(0, b3.getAngle()); + } + + @Test + void testFullVoltageInitialization() { + openReacParameters.setVoltageInitialization(OpenReacParameters.OpenReacVoltageInitialization.FULL_VOLTAGE); + OpenReacRunner.initializeVoltageBeforeOptimization(network, openReacParameters); + assertEquals(135, b1.getV(), deltaVoltage); + assertEquals(0.822, b1.getAngle(), deltaVoltage); + assertEquals(135, b2.getV(), deltaVoltage); + assertEquals(0, b2.getAngle(), deltaVoltage); + assertEquals(30.375, b3.getV(), deltaVoltage); + assertEquals(-2.923, b3.getAngle(), deltaVoltage); + } +}