diff --git a/glsk/glsk-document-api/src/main/java/com/powsybl/glsk/api/util/converters/GlskPointScalableConverter.java b/glsk/glsk-document-api/src/main/java/com/powsybl/glsk/api/util/converters/GlskPointScalableConverter.java index 727261c8..9ec80eb3 100644 --- a/glsk/glsk-document-api/src/main/java/com/powsybl/glsk/api/util/converters/GlskPointScalableConverter.java +++ b/glsk/glsk-document-api/src/main/java/com/powsybl/glsk/api/util/converters/GlskPointScalableConverter.java @@ -12,15 +12,26 @@ import com.powsybl.glsk.commons.CountryEICode; import com.powsybl.glsk.commons.GlskException; import com.powsybl.iidm.modification.scalable.Scalable; -import com.powsybl.iidm.network.*; +import com.powsybl.iidm.network.Country; +import com.powsybl.iidm.network.DanglingLine; +import com.powsybl.iidm.network.Generator; +import com.powsybl.iidm.network.Load; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.Substation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.function.BiFunction; +import java.util.function.Function; /** * Convert a single GlskPoint to Scalable + * * @author Pengbo Wang {@literal } */ public final class GlskPointScalableConverter { @@ -31,7 +42,7 @@ private GlskPointScalableConverter() { } /** - * @param network IIDM network + * @param network IIDM network * @param glskPoint GLSK Point * @return powsybl-core Scalable */ @@ -135,21 +146,28 @@ private static Scalable convertMeritOrder(Network network, GlskPoint glskPoint) Scalable upScalable = Scalable.stack(glskPoint.getGlskShiftKeys().stream() .filter(glskShiftKey -> glskShiftKey.getMeritOrderPosition() > 0) .sorted(Comparator.comparingInt(GlskShiftKey::getMeritOrderPosition)) - .map(glskShiftKey -> { - GlskRegisteredResource resource = Objects.requireNonNull(glskShiftKey.getRegisteredResourceArrayList()).get(0); - return isGenerator(network, resource) ? getGeneratorScalableWithLimits(network, resource) : getDanglingLineScalableWithLimits(network, resource); - }).toArray(Scalable[]::new)); + .map(getGlskShiftKeyScalableFunction(network)).toArray(Scalable[]::new)); Scalable downScalable = Scalable.stack(glskPoint.getGlskShiftKeys().stream() .filter(glskShiftKey -> glskShiftKey.getMeritOrderPosition() < 0) .sorted(Comparator.comparingInt(GlskShiftKey::getMeritOrderPosition).reversed()) - .map(glskShiftKey -> { - GlskRegisteredResource resource = Objects.requireNonNull(glskShiftKey.getRegisteredResourceArrayList()).get(0); - return isGenerator(network, resource) ? getGeneratorScalableWithLimits(network, resource) : getDanglingLineScalableWithLimits(network, resource); - }).toArray(Scalable[]::new)); + .map(getGlskShiftKeyScalableFunction(network)).toArray(Scalable[]::new)); return Scalable.upDown(upScalable, downScalable); } + private static Function getGlskShiftKeyScalableFunction(Network network) { + return glskShiftKey -> { + GlskRegisteredResource resource = Objects.requireNonNull(glskShiftKey.getRegisteredResourceArrayList()).get(0); + if (isGenerator(network, resource)) { + return getGeneratorScalableWithLimits(network, resource); + } else if (isLoad(network, resource)) { + return getLoadScalableWithLimits(network, resource); + } else { + return getDanglingLineScalableWithLimits(network, resource); + } + }; + } + /** * convert country proportional glsk point to scalable * @param network iidm network @@ -306,6 +324,10 @@ private static boolean isGenerator(Network network, GlskRegisteredResource glskR return network.getGenerator(glskRegisteredResource.getGeneratorId()) != null; } + private static boolean isLoad(Network network, GlskRegisteredResource glskRegisteredResource) { + return network.getLoad(glskRegisteredResource.getLoadId()) != null; + } + private static Scalable getGeneratorScalableWithLimits(Network network, GlskRegisteredResource generatorRegisteredResource) { String generatorId = generatorRegisteredResource.getGeneratorId(); double incomingMaxP = generatorRegisteredResource.getMaximumCapacity().orElse(Double.MAX_VALUE); diff --git a/glsk/glsk-document-cse/src/test/java/com/powsybl/glsk/cse/CseGlskDocumentImporterTest.java b/glsk/glsk-document-cse/src/test/java/com/powsybl/glsk/cse/CseGlskDocumentImporterTest.java index d4ccffdb..5a44bb34 100644 --- a/glsk/glsk-document-cse/src/test/java/com/powsybl/glsk/cse/CseGlskDocumentImporterTest.java +++ b/glsk/glsk-document-cse/src/test/java/com/powsybl/glsk/cse/CseGlskDocumentImporterTest.java @@ -580,7 +580,19 @@ void checkCseGlskDocumentImporterCorrectlyConvertMeritOrderGskBlocksDownWithDang assertEquals(2000., network.getDanglingLine("BBE2AA1 XNODE_1B 1").getP0(), EPSILON); // -1000 assertEquals(2000., network.getDanglingLine("DDE3AA1 XNODE_1A 1").getP0(), EPSILON); // -3000 assertEquals(1500., network.getGenerator("FFR1AA1 _generator").getTargetP(), EPSILON); // -500 + } + + @Test + void checkCseGlskDocumentImporterCorrectlyConvertMeritOrderGskBlocksWithLoads() { + Network network = Network.read("testCaseWithLoadss.xiidm", getClass().getResourceAsStream("/testCaseWithLoads.xiidm")); + GlskDocument glskDocument = GlskDocumentImporters.importGlsk(getClass().getResourceAsStream("/testGlskWithLoads.xml")); + Scalable meritOrderGskScalable = glskDocument.getZonalScalable(network).getData("FR_MERITORDER"); + + assertNotNull(meritOrderGskScalable); + assertEquals(-100., network.getLoad("IMESM121_load").getP0(), EPSILON); + meritOrderGskScalable.scale(network, 100.); + assertEquals(-200., network.getLoad("IMESM121_load").getP0(), EPSILON); } @Test diff --git a/glsk/glsk-document-cse/src/test/resources/testCaseWithLoads.xiidm b/glsk/glsk-document-cse/src/test/resources/testCaseWithLoads.xiidm new file mode 100644 index 00000000..ba2ea8be --- /dev/null +++ b/glsk/glsk-document-cse/src/test/resources/testCaseWithLoads.xiidm @@ -0,0 +1,283 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/glsk/glsk-document-cse/src/test/resources/testGlskWithLoads.xml b/glsk/glsk-document-cse/src/test/resources/testGlskWithLoads.xml new file mode 100644 index 00000000..d750ec11 --- /dev/null +++ b/glsk/glsk-document-cse/src/test/resources/testGlskWithLoads.xml @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +