From 9675420c19f9b6797e21bd9694605f6cc767e70f Mon Sep 17 00:00:00 2001 From: lisrte Date: Thu, 2 Nov 2023 15:15:58 +0100 Subject: [PATCH 01/12] Handle model optimizer with dynawaltz parameter Signed-off-by: lisrte --- .../com/powsybl/dynawaltz/DataOptimizer.java | 25 ++++++++++++++++ .../powsybl/dynawaltz/DynaWaltzContext.java | 21 +++++++++---- .../dynawaltz/DynaWaltzParameters.java | 20 ++++++++++++- .../models/AbstractBlackBoxModel.java | 6 +++- .../AbstractEquipmentBlackBoxModel.java | 5 ++++ .../models/EquipmentBlackBoxModel.java | 4 +++ .../dynawaltz/models/hvdc/AbstractHvdc.java | 5 ++++ .../dynawaltz/models/hvdc/HvdcPDangling.java | 8 +++++ .../models/hvdc/HvdcVscDangling.java | 8 +++++ .../dynawaltz/parameters/ParametersSet.java | 30 ++++++++++++++----- .../test/resources/DynaWaltzParameters.json | 1 + pom.xml | 2 +- 12 files changed, 120 insertions(+), 15 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/DataOptimizer.java diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DataOptimizer.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DataOptimizer.java new file mode 100644 index 000000000..2decd2ea5 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DataOptimizer.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2023, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.dynawaltz; + +import com.powsybl.commons.reporter.Reporter; +import com.powsybl.dynawaltz.models.BlackBoxModel; + +import java.util.stream.Stream; + +/** + * @author Laurent Issertial + */ +public interface DataOptimizer { + + Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter); + + default Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters) { + return optimizeModels(models, dynaWaltzParameters, Reporter.NO_OP); + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 8f214d55d..3ea5af76e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -7,12 +7,13 @@ package com.powsybl.dynawaltz; import com.powsybl.commons.PowsyblException; +import com.powsybl.commons.reporter.Reporter; import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.models.*; import com.powsybl.dynawaltz.models.buses.AbstractBus; -import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; import com.powsybl.dynawaltz.models.buses.DefaultEquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; import com.powsybl.dynawaltz.models.defaultmodels.DefaultModelsHandler; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizerModel; @@ -63,9 +64,17 @@ public DynaWaltzContext(Network network, String workingVariantId, List curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters) { this.network = Objects.requireNonNull(network); this.workingVariantId = Objects.requireNonNull(workingVariantId); - this.dynamicModels = Objects.requireNonNull(dynamicModels).stream() - .filter(distinctByDynamicId().and(distinctByStaticId())) + this.parameters = Objects.requireNonNull(parameters); + this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters); + + Iterator dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(DataOptimizer.class).iterator() : Collections.emptyIterator(); + this.dynamicModels = runOptimizers(dataOptimizers, + Objects.requireNonNull(dynamicModels) + .stream() + .filter(distinctByDynamicId().and(distinctByStaticId())), + Reporter.NO_OP) .toList(); + this.eventModels = Objects.requireNonNull(eventModels).stream() .filter(distinctByDynamicId()) .toList(); @@ -74,8 +83,6 @@ public DynaWaltzContext(Network network, String workingVariantId, List runOptimizers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { + return dataOptimizers.hasNext() ? runOptimizers(dataOptimizers, dataOptimizers.next().optimizeModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; + } + public Network getNetwork() { return network; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index d9d252142..1a692d0d6 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -37,6 +37,7 @@ public class DynaWaltzParameters extends AbstractExtension c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE); + boolean useModelOptimizers = config.flatMap(c -> c.getOptionalBooleanProperty("useOptimizers")).orElse(DEFAULT_USE_MODEL_OPTIMIZERS); + // Load xml files List modelsParameters = ParametersXml.load(parametersPath); ParametersSet networkParameters = ParametersXml.load(networkParametersPath, networkParametersId); @@ -110,7 +114,8 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem .setSolverParameters(solverParameters) .setSolverType(solverType) .setMergeLoads(mergeLoads) - .setWriteFinalState(writeFinalState); + .setWriteFinalState(writeFinalState) + .setUseModelOptimizers(useModelOptimizers); } @Override @@ -118,6 +123,10 @@ public String getName() { return "DynaWaltzParameters"; } + public void addModelParameters(ParametersSet parameterSet) { + modelsParameters.put(parameterSet.getId(), parameterSet); + } + public ParametersSet getModelParameters(String parameterSetId) { return modelsParameters.get(parameterSetId); } @@ -178,4 +187,13 @@ public DynaWaltzParameters setWriteFinalState(boolean writeFinalState) { public boolean isWriteFinalState() { return writeFinalState; } + + public boolean isUseModelOptimizers() { + return useModelOptimizers; + } + + public DynaWaltzParameters setUseModelOptimizers(boolean useModelOptimizers) { + this.useModelOptimizers = useModelOptimizers; + return this; + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java index 2e4079c41..326aae7cb 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -36,7 +36,7 @@ public abstract class AbstractBlackBoxModel implements BlackBoxModel { private final String dynamicModelId; - private final String parameterSetId; + private String parameterSetId; protected AbstractBlackBoxModel(String dynamicModelId, String parameterSetId) { this.dynamicModelId = Objects.requireNonNull(dynamicModelId); @@ -56,6 +56,10 @@ public String getParameterSetId() { return parameterSetId; } + public void setParameterSetId(String parameterSetId) { + this.parameterSetId = parameterSetId; + } + @Override public void createDynamicModelParameters(DynaWaltzContext context, Consumer parametersAdder) { // method empty by default to be redefined by specific models diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractEquipmentBlackBoxModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractEquipmentBlackBoxModel.java index f2937372b..f97fdef4f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractEquipmentBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractEquipmentBlackBoxModel.java @@ -37,6 +37,11 @@ public String getStaticId() { return equipment.getId(); } + @Override + public T getEquipment() { + return equipment; + } + @Override public String getLib() { return lib; diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/EquipmentBlackBoxModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/EquipmentBlackBoxModel.java index d54080ac2..0e38d9f62 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/EquipmentBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/EquipmentBlackBoxModel.java @@ -7,9 +7,13 @@ */ package com.powsybl.dynawaltz.models; +import com.powsybl.iidm.network.Identifiable; + /** * @author Laurent Issertial {@literal } */ public interface EquipmentBlackBoxModel extends BlackBoxModel { String getStaticId(); + + Identifiable getEquipment(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java index 2c79c41e6..db28b7cff 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java @@ -13,6 +13,7 @@ import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.iidm.network.HvdcLine; import java.util.ArrayList; @@ -61,4 +62,8 @@ protected List getVarConnectionsWith(EquipmentConnectionPoint con protected final VarConnection getSimpleVarConnectionWithBus(EquipmentConnectionPoint connected, Side side) { return new VarConnection(TERMINAL_PREFIX + side.getSideNumber(), connected.getTerminalVarName(side)); } + + public List> getConnectedStations() { + return List.of(equipment.getConverterStation1(), equipment.getConverterStation2()); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java index 39dea79a3..ab434e138 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java @@ -11,8 +11,11 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.utils.SideConverter; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.iidm.network.HvdcLine; +import java.util.List; + /** * @author Laurent Issertial {@literal } */ @@ -40,4 +43,9 @@ public String getSwitchOffSignalEventVarName(Side side) { } return super.getSwitchOffSignalEventVarName(side); } + + @Override + public List> getConnectedStations() { + return List.of(danglingSide.isDangling(Side.ONE) ? equipment.getConverterStation2() : equipment.getConverterStation1()); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java index 2eb39236e..19d49b88b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java @@ -10,8 +10,11 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.iidm.network.HvdcLine; +import java.util.List; + /** * @author Laurent Issertial {@literal } */ @@ -39,4 +42,9 @@ public String getSwitchOffSignalEventVarName(Side side) { } return super.getSwitchOffSignalEventVarName(side); } + + @Override + public List> getConnectedStations() { + return List.of(danglingSide.isDangling(Side.ONE) ? equipment.getConverterStation2() : equipment.getConverterStation1()); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java index 8c1eeb3a5..ef491d6fc 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java @@ -10,10 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.powsybl.commons.PowsyblException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author Marcos de Miguel {@literal } @@ -21,23 +18,42 @@ */ public class ParametersSet { - private final Map parameters = new LinkedHashMap<>(); - - private final List references = new ArrayList<>(); + private final Map parameters; + private final List references; private final String id; public ParametersSet(@JsonProperty("id") String id) { this.id = id; + this.parameters = new LinkedHashMap<>(); + this.references = new ArrayList<>(); + } + + public ParametersSet(String id, ParametersSet parametersSet) { + this.id = id; + this.parameters = new LinkedHashMap<>(parametersSet.parameters); + this.references = new ArrayList<>(parametersSet.references); } public void addParameter(String name, ParameterType type, String value) { parameters.put(name, new Parameter(name, type, value)); } + public void addParameter(Parameter parameter) { + parameters.put(parameter.name(), parameter); + } + + public void replaceParameter(String parameterName, ParameterType type, String value) { + parameters.replace(parameterName, new Parameter(parameterName, type, value)); + } + public void addReference(String name, ParameterType type, String origData, String origName, String componentId) { references.add(new Reference(name, type, origData, origName, componentId)); } + public void addReference(String name, ParameterType type, String origData, String origName) { + references.add(new Reference(name, type, origData, origName, null)); + } + public String getId() { return id; } diff --git a/dynawaltz/src/test/resources/DynaWaltzParameters.json b/dynawaltz/src/test/resources/DynaWaltzParameters.json index 9317fd014..fedb6d956 100644 --- a/dynawaltz/src/test/resources/DynaWaltzParameters.json +++ b/dynawaltz/src/test/resources/DynaWaltzParameters.json @@ -39,6 +39,7 @@ "solverType" : "IDA", "mergeLoads" : false, "writeFinalState" : true, + "useModelOptimizers" : false, "modelsParameters" : [ { "id" : "test", "parameters" : { diff --git a/pom.xml b/pom.xml index 5581048d2..934cc5fe3 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ 17 - 6.0.0 + 6.0.1 4.0.14 ../distribution/target/site/jacoco-aggregate/jacoco.xml, From 06812f59fb5f72d5c79d52fc68b23a8fa55343f4 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 3 Nov 2023 15:10:22 +0100 Subject: [PATCH 02/12] Fix useModelOptimizers parameter Add getParameter to ParametersSet Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzParameters.java | 2 +- .../dynawaltz/parameters/ParametersSet.java | 16 ++++++++++------ .../dynawaltz/DynaWaltzParametersTest.java | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index 1a692d0d6..0434589cc 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -101,7 +101,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem // Writes final state IIDM boolean writeFinalState = config.flatMap(c -> c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE); - boolean useModelOptimizers = config.flatMap(c -> c.getOptionalBooleanProperty("useOptimizers")).orElse(DEFAULT_USE_MODEL_OPTIMIZERS); + boolean useModelOptimizers = config.flatMap(c -> c.getOptionalBooleanProperty("useModelOptimizers")).orElse(DEFAULT_USE_MODEL_OPTIMIZERS); // Load xml files List modelsParameters = ParametersXml.load(parametersPath); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java index ef491d6fc..79c76fc1c 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java @@ -67,26 +67,30 @@ public List getReferences() { } public boolean getBool(String parameterName) { - Parameter parameter = getParameter(parameterName, ParameterType.BOOL); + Parameter parameter = getParameterOrThrows(parameterName, ParameterType.BOOL); return Boolean.parseBoolean(parameter.value()); } public double getDouble(String parameterName) { - Parameter parameter = getParameter(parameterName, ParameterType.DOUBLE); + Parameter parameter = getParameterOrThrows(parameterName, ParameterType.DOUBLE); return Double.parseDouble(parameter.value()); } public int getInt(String parameterName) { - Parameter parameter = getParameter(parameterName, ParameterType.INT); + Parameter parameter = getParameterOrThrows(parameterName, ParameterType.INT); return Integer.parseInt(parameter.value()); } public String getString(String parameterName) { - Parameter parameter = getParameter(parameterName, ParameterType.STRING); + Parameter parameter = getParameterOrThrows(parameterName, ParameterType.STRING); return parameter.value(); } public Parameter getParameter(String parameterName) { + return parameters.get(parameterName); + } + + public Parameter getParameterOrThrows(String parameterName) { Parameter parameter = parameters.get(parameterName); if (parameter == null) { throw new IllegalArgumentException("Parameter " + parameterName + " not found in set " + id); @@ -94,8 +98,8 @@ public Parameter getParameter(String parameterName) { return parameter; } - private Parameter getParameter(String parameterName, ParameterType type) { - Parameter parameter = getParameter(parameterName); + private Parameter getParameterOrThrows(String parameterName, ParameterType type) { + Parameter parameter = getParameterOrThrows(parameterName); if (parameter.type() != type) { throw new PowsyblException("Invalid parameter type: " + parameter.type() + " (" + type + " expected)"); } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index e3c3cda60..0f086045f 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java @@ -65,11 +65,11 @@ void checkParameters() throws IOException { assertEquals(networkParametersId, parameters.getNetworkParameters().getId()); ParametersSet networkParameters = parameters.getNetworkParameters(); - Parameter loadTp = networkParameters.getParameter("load_Tp"); + Parameter loadTp = networkParameters.getParameterOrThrows("load_Tp"); assertEquals("90", loadTp.value()); assertEquals("load_Tp", loadTp.name()); assertEquals(ParameterType.DOUBLE, loadTp.type()); - Parameter loadControllable = networkParameters.getParameter("load_isControllable"); + Parameter loadControllable = networkParameters.getParameterOrThrows("load_isControllable"); assertEquals("false", loadControllable.value()); assertEquals("load_isControllable", loadControllable.name()); assertEquals(ParameterType.BOOL, loadControllable.type()); @@ -77,11 +77,11 @@ void checkParameters() throws IOException { ParametersSet solverParameters = parameters.getSolverParameters(); assertEquals(solverParametersId, solverParameters.getId()); assertEquals(solverType, parameters.getSolverType()); - Parameter order = solverParameters.getParameter("order"); + Parameter order = solverParameters.getParameterOrThrows("order"); assertEquals("1", order.value()); assertEquals("order", order.name()); assertEquals(ParameterType.INT, order.type()); - Parameter absAccuracy = solverParameters.getParameter("absAccuracy"); + Parameter absAccuracy = solverParameters.getParameterOrThrows("absAccuracy"); assertEquals("1e-4", absAccuracy.value()); assertEquals("absAccuracy", absAccuracy.name()); assertEquals(ParameterType.DOUBLE, absAccuracy.type()); @@ -164,11 +164,11 @@ void checkException() throws IOException { } private static void checkModelParameters(DynaWaltzParameters dynaWaltzParameters) { - Parameter booleanParameter = dynaWaltzParameters.getModelParameters("test").getParameter("boolean"); + Parameter booleanParameter = dynaWaltzParameters.getModelParameters("test").getParameterOrThrows("boolean"); assertEquals("true", booleanParameter.value()); assertEquals("boolean", booleanParameter.name()); assertEquals(ParameterType.BOOL, booleanParameter.type()); - Parameter stringParameter = dynaWaltzParameters.getModelParameters("test").getParameter("string"); + Parameter stringParameter = dynaWaltzParameters.getModelParameters("test").getParameterOrThrows("string"); assertEquals("aString", stringParameter.value()); assertEquals("string", stringParameter.name()); assertEquals(ParameterType.STRING, stringParameter.type()); From b200dbb770b7328c7b224416e0b2bf4c09bf662d Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 3 Nov 2023 17:31:40 +0100 Subject: [PATCH 03/12] Add tests Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 4 +- .../dynawaltz/DynaWaltzParameters.java | 2 +- ...DataOptimizer.java => ModelOptimizer.java} | 6 +- .../DynaWaltzParametersDatabaseTest.java | 9 +++ .../dynawaltz/DynaWaltzParametersTest.java | 10 ++- .../powsybl/dynawaltz/ModelOptimizerTest.java | 76 +++++++++++++++++++ .../powsybl/dynawaltz/models/HvdcTest.java | 45 +++++++++++ 7 files changed, 141 insertions(+), 11 deletions(-) rename dynawaltz/src/main/java/com/powsybl/dynawaltz/{DataOptimizer.java => ModelOptimizer.java} (73%) create mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java create mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 3ea5af76e..b80179a54 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -67,7 +67,7 @@ public DynaWaltzContext(Network network, String workingVariantId, List dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(DataOptimizer.class).iterator() : Collections.emptyIterator(); + Iterator dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(ModelOptimizer.class).iterator() : Collections.emptyIterator(); this.dynamicModels = runOptimizers(dataOptimizers, Objects.requireNonNull(dynamicModels) .stream() @@ -106,7 +106,7 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function runOptimizers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { + private Stream runOptimizers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { return dataOptimizers.hasNext() ? runOptimizers(dataOptimizers, dataOptimizers.next().optimizeModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index 0434589cc..ee180eafd 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -37,7 +37,7 @@ public class DynaWaltzParameters extends AbstractExtension */ -public interface DataOptimizer { +public interface ModelOptimizer { Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter); - - default Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters) { - return optimizeModels(models, dynaWaltzParameters, Reporter.NO_OP); - } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java index 9f7b9b24d..47d9958e8 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java @@ -74,4 +74,13 @@ void checkParametersNotFound() { UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> ParametersXml.load(path)); assertEquals("java.nio.file.NoSuchFileException: /file.par", e.getMessage()); } + + @Test + void addParametersSet() { + DynaWaltzParameters dParameters = new DynaWaltzParameters(); + ParametersSet set = new ParametersSet("test"); + dParameters.addModelParameters(set); + assertEquals(1, dParameters.getModelParameters().size()); + assertEquals(set, dParameters.getModelParameters("test")); + } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index 0f086045f..209208d0b 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java @@ -57,7 +57,8 @@ void checkParameters() throws IOException { SolverType solverType = SolverType.IDA; String solverParametersId = "solverParametersId"; boolean mergeLoads = true; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads); + boolean useModelOptimizers = true; + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelOptimizers); DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem); @@ -87,6 +88,7 @@ void checkParameters() throws IOException { assertEquals(ParameterType.DOUBLE, absAccuracy.type()); assertEquals(mergeLoads, parameters.isMergeLoads()); + assertEquals(useModelOptimizers, parameters.isUseModelOptimizers()); } @Test @@ -95,7 +97,7 @@ void roundTripParametersSerializing() throws IOException { SolverType solverType = SolverType.IDA; String solverParametersId = "solverParametersId"; boolean mergeLoads = false; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads); + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false); DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters() .setStartTime(0) @@ -106,7 +108,7 @@ void roundTripParametersSerializing() throws IOException { JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json"); } - private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads) throws IOException { + private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelOptimizers) throws IOException { String parametersFile = USER_HOME + "parametersFile"; String networkParametersFile = USER_HOME + "networkParametersFile"; String solverParametersFile = USER_HOME + "solverParametersFile"; @@ -119,6 +121,7 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp moduleConfig.setStringProperty("network.parametersId", networkParametersId); moduleConfig.setStringProperty("solver.type", solverType.toString()); moduleConfig.setStringProperty("solver.parametersId", solverParametersId); + moduleConfig.setStringProperty("useModelOptimizers", String.valueOf(useModelOptimizers)); Files.createDirectories(fileSystem.getPath(USER_HOME)); copyFile("/parametersSet/models.par", parametersFile); @@ -145,6 +148,7 @@ void checkDefaultParameters() throws IOException { assertEquals("1", parameters.getSolverParameters().getId()); assertEquals(DynaWaltzParameters.DEFAULT_MERGE_LOADS, parameters.isMergeLoads()); + assertEquals(DynaWaltzParameters.DEFAULT_USE_MODEL_OPTIMIZERS, parameters.isUseModelOptimizers()); } @Test diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java new file mode 100644 index 000000000..58c9f8611 --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2023, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.dynawaltz; + +import com.google.auto.service.AutoService; +import com.google.common.collect.Lists; +import com.powsybl.commons.reporter.Reporter; +import com.powsybl.dynamicsimulation.DynamicSimulationParameters; +import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.generators.AbstractGenerator; +import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; +import com.powsybl.dynawaltz.models.loads.BaseLoad; +import com.powsybl.dynawaltz.models.transformers.TransformerFixedRatio; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; +import java.util.ServiceLoader; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Laurent Issertial {@literal } + */ +class ModelOptimizerTest { + + @Test + void loadOptimizer() { + List optimizers = Lists.newArrayList(ServiceLoader.load(ModelOptimizer.class)); + assertEquals(2, optimizers.size()); + } + + @Test + void optimizeModels() { + Network network = EurostagTutorialExample1Factory.create(); + DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); + DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load().setUseModelOptimizers(true); + List dynamicModels = List.of( + new GeneratorFictitious("BBM_GEN", network.getGenerator("GEN"), "GPV"), + new BaseLoad("BBM_LOAD", network.getLoad("LOAD"), "LOAD", "LoadAlphaBeta"), + new TransformerFixedRatio("BBM_TRA", network.getTwoWindingsTransformer("NGEN_NHV1"), "TR", "TransformerFixedRatio")); + DynaWaltzContext context = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, Collections.emptyList(), Collections.emptyList(), parameters, dynawoParameters); + assertEquals(2, context.getBlackBoxDynamicModels().size()); + assertFalse(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("BBM_LOAD"))); + assertTrue(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("newModel"))); + } + + @AutoService(ModelOptimizer.class) + public static class ModelOptimizerFilter implements ModelOptimizer { + @Override + public Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { + return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD")); + } + } + + @AutoService(ModelOptimizer.class) + public static class ModelOptimizerSubstitution implements ModelOptimizer { + @Override + public Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { + return models.map(m -> { + if ("BBM_GEN".equalsIgnoreCase(m.getDynamicModelId()) && m instanceof AbstractGenerator gen) { + return new GeneratorFictitious("newModel", gen.getEquipment(), "G"); + } + return m; + }); + } + } +} diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java new file mode 100644 index 000000000..b7c138a88 --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2023, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.dynawaltz.models; + +import com.powsybl.dynawaltz.models.hvdc.HvdcP; +import com.powsybl.dynawaltz.models.hvdc.HvdcPDangling; +import com.powsybl.dynawaltz.models.hvdc.HvdcVscDangling; +import com.powsybl.iidm.network.HvdcLine; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.test.HvdcTestNetwork; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Laurent Issertial {@literal } + */ +class HvdcTest { + + @Test + void testConnectedStation() { + Network network = HvdcTestNetwork.createVsc(); + HvdcP hvdc = new HvdcP("hvdc", network.getHvdcLine("L"), "HVDC", "HvdcPV"); + assertEquals(2, hvdc.getConnectedStations().size()); + } + + @Test + void testDanglingConnectedStation() { + Network network = HvdcTestNetwork.createVsc(); + HvdcLine line = network.getHvdcLine("L"); + + HvdcPDangling hvdc = new HvdcPDangling("hvdc", line, "HVDC", "HvdcPVDangling", Side.ONE); + assertEquals(1, hvdc.getConnectedStations().size()); + assertEquals(line.getConverterStation2(), hvdc.getConnectedStations().get(0)); + + HvdcVscDangling hvdc2 = new HvdcVscDangling("hvdc", line, "HVDC", "HvdcVSCDanglingP", Side.TWO); + assertEquals(1, hvdc.getConnectedStations().size()); + assertEquals(line.getConverterStation1(), hvdc2.getConnectedStations().get(0)); + } +} From 7a901a82979511e68483a0cdaf297cc02e05d2a3 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 6 Nov 2023 13:27:21 +0100 Subject: [PATCH 04/12] Refactor parameter set Signed-off-by: lisrte --- .../dynawaltz/parameters/ParametersSet.java | 11 +++------- .../dynawaltz/DynaWaltzParametersTest.java | 20 +++++++++---------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java index 79c76fc1c..daf60b1dd 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java @@ -86,20 +86,15 @@ public String getString(String parameterName) { return parameter.value(); } - public Parameter getParameter(String parameterName) { - return parameters.get(parameterName); + public boolean hasParameter(String parameterName) { + return parameters.containsKey(parameterName); } - public Parameter getParameterOrThrows(String parameterName) { + private Parameter getParameterOrThrows(String parameterName, ParameterType type) { Parameter parameter = parameters.get(parameterName); if (parameter == null) { throw new IllegalArgumentException("Parameter " + parameterName + " not found in set " + id); } - return parameter; - } - - private Parameter getParameterOrThrows(String parameterName, ParameterType type) { - Parameter parameter = getParameterOrThrows(parameterName); if (parameter.type() != type) { throw new PowsyblException("Invalid parameter type: " + parameter.type() + " (" + type + " expected)"); } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index 209208d0b..897396538 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java @@ -15,7 +15,6 @@ import com.powsybl.dynawaltz.DynaWaltzParameters.SolverType; import com.powsybl.dynawaltz.parameters.Parameter; import com.powsybl.dynawaltz.parameters.ParameterType; -import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.dynawaltz.xml.ParametersXml; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -24,6 +23,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; @@ -65,24 +65,24 @@ void checkParameters() throws IOException { checkModelParameters(parameters); assertEquals(networkParametersId, parameters.getNetworkParameters().getId()); - ParametersSet networkParameters = parameters.getNetworkParameters(); - Parameter loadTp = networkParameters.getParameterOrThrows("load_Tp"); + Map networkParameters = parameters.getNetworkParameters().getParameters(); + Parameter loadTp = networkParameters.get("load_Tp"); assertEquals("90", loadTp.value()); assertEquals("load_Tp", loadTp.name()); assertEquals(ParameterType.DOUBLE, loadTp.type()); - Parameter loadControllable = networkParameters.getParameterOrThrows("load_isControllable"); + Parameter loadControllable = networkParameters.get("load_isControllable"); assertEquals("false", loadControllable.value()); assertEquals("load_isControllable", loadControllable.name()); assertEquals(ParameterType.BOOL, loadControllable.type()); - ParametersSet solverParameters = parameters.getSolverParameters(); - assertEquals(solverParametersId, solverParameters.getId()); + Map solverParameters = parameters.getSolverParameters().getParameters(); + assertEquals(solverParametersId, parameters.getSolverParameters().getId()); assertEquals(solverType, parameters.getSolverType()); - Parameter order = solverParameters.getParameterOrThrows("order"); + Parameter order = solverParameters.get("order"); assertEquals("1", order.value()); assertEquals("order", order.name()); assertEquals(ParameterType.INT, order.type()); - Parameter absAccuracy = solverParameters.getParameterOrThrows("absAccuracy"); + Parameter absAccuracy = solverParameters.get("absAccuracy"); assertEquals("1e-4", absAccuracy.value()); assertEquals("absAccuracy", absAccuracy.name()); assertEquals(ParameterType.DOUBLE, absAccuracy.type()); @@ -168,11 +168,11 @@ void checkException() throws IOException { } private static void checkModelParameters(DynaWaltzParameters dynaWaltzParameters) { - Parameter booleanParameter = dynaWaltzParameters.getModelParameters("test").getParameterOrThrows("boolean"); + Parameter booleanParameter = dynaWaltzParameters.getModelParameters("test").getParameters().get("boolean"); assertEquals("true", booleanParameter.value()); assertEquals("boolean", booleanParameter.name()); assertEquals(ParameterType.BOOL, booleanParameter.type()); - Parameter stringParameter = dynaWaltzParameters.getModelParameters("test").getParameterOrThrows("string"); + Parameter stringParameter = dynaWaltzParameters.getModelParameters("test").getParameters().get("string"); assertEquals("aString", stringParameter.value()); assertEquals("string", stringParameter.name()); assertEquals(ParameterType.STRING, stringParameter.type()); From 8f8281839ad436484586a94bffe5f2a8d18ce262 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 28 Nov 2023 12:35:08 +0100 Subject: [PATCH 05/12] Fix branch update Signed-off-by: lisrte --- .../main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index 2da285b78..d38584bba 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -134,7 +134,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem .setSolverType(solverType) .setMergeLoads(mergeLoads) .setWriteFinalState(writeFinalState) - .setUseModelOptimizers(useModelOptimizers); + .setUseModelOptimizers(useModelOptimizers) .setDumpFileParameters(dumpFileParameters); } From 2ddc03b8e294a9978601bb89280562d49717375c Mon Sep 17 00:00:00 2001 From: lisrte Date: Wed, 13 Dec 2023 12:58:00 +0100 Subject: [PATCH 06/12] Rename optimizer to simplifier Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 8 +++---- ...elOptimizer.java => ModelsSimplifier.java} | 4 ++-- ...zerTest.java => ModelsSimplifierTest.java} | 22 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) rename dynawaltz/src/main/java/com/powsybl/dynawaltz/{ModelOptimizer.java => ModelsSimplifier.java} (85%) rename dynawaltz/src/test/java/com/powsybl/dynawaltz/{ModelOptimizerTest.java => ModelsSimplifierTest.java} (83%) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 391a0fc5a..9f42e19c5 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -75,8 +75,8 @@ public DynaWaltzContext(Network network, String workingVariantId, List dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(ModelOptimizer.class).iterator() : Collections.emptyIterator(); - this.dynamicModels = runOptimizers(dataOptimizers, + Iterator dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(ModelsSimplifier.class).iterator() : Collections.emptyIterator(); + this.dynamicModels = runSimplifiers(dataOptimizers, Objects.requireNonNull(dynamicModels) .stream() .filter(distinctByDynamicId(reporter).and(distinctByStaticId(reporter))), @@ -114,8 +114,8 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function runOptimizers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { - return dataOptimizers.hasNext() ? runOptimizers(dataOptimizers, dataOptimizers.next().optimizeModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; + private Stream runSimplifiers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { + return dataOptimizers.hasNext() ? runSimplifiers(dataOptimizers, dataOptimizers.next().simplifyModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; } public Network getNetwork() { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelOptimizer.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java similarity index 85% rename from dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelOptimizer.java rename to dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java index 8f8860bae..d84dbea13 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelOptimizer.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java @@ -15,7 +15,7 @@ /** * @author Laurent Issertial */ -public interface ModelOptimizer { +public interface ModelsSimplifier { - Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter); + Stream simplifyModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter); } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java similarity index 83% rename from dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java rename to dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java index 58c9f8611..52b6caa3c 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelOptimizerTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java @@ -30,16 +30,16 @@ /** * @author Laurent Issertial {@literal } */ -class ModelOptimizerTest { +class ModelsSimplifierTest { @Test - void loadOptimizer() { - List optimizers = Lists.newArrayList(ServiceLoader.load(ModelOptimizer.class)); - assertEquals(2, optimizers.size()); + void loadSimplifiers() { + List simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSimplifier.class)); + assertEquals(2, simplifiers.size()); } @Test - void optimizeModels() { + void simplifyModels() { Network network = EurostagTutorialExample1Factory.create(); DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load().setUseModelOptimizers(true); @@ -53,18 +53,18 @@ void optimizeModels() { assertTrue(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("newModel"))); } - @AutoService(ModelOptimizer.class) - public static class ModelOptimizerFilter implements ModelOptimizer { + @AutoService(ModelsSimplifier.class) + public static class ModelsSimplifierFilter implements ModelsSimplifier { @Override - public Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { + public Stream simplifyModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD")); } } - @AutoService(ModelOptimizer.class) - public static class ModelOptimizerSubstitution implements ModelOptimizer { + @AutoService(ModelsSimplifier.class) + public static class ModelsSimplifierSubstitution implements ModelsSimplifier { @Override - public Stream optimizeModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { + public Stream simplifyModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { return models.map(m -> { if ("BBM_GEN".equalsIgnoreCase(m.getDynamicModelId()) && m instanceof AbstractGenerator gen) { return new GeneratorFictitious("newModel", gen.getEquipment(), "G"); From 9f7abd99644e5861b600aa570ae38cb4c520bbd8 Mon Sep 17 00:00:00 2001 From: Lisrte Date: Wed, 13 Dec 2023 13:19:57 +0100 Subject: [PATCH 07/12] Rename optimizer to simplifier 2 Signed-off-by: lisrte --- .../com/powsybl/dynawaltz/DynaWaltzContext.java | 8 ++++---- .../powsybl/dynawaltz/DynaWaltzParameters.java | 16 ++++++++-------- .../dynawaltz/DynaWaltzParametersTest.java | 12 ++++++------ .../powsybl/dynawaltz/ModelsSimplifierTest.java | 2 +- .../src/test/resources/DynaWaltzParameters.json | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 9f42e19c5..bff2d47e8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -75,8 +75,8 @@ public DynaWaltzContext(Network network, String workingVariantId, List dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(ModelsSimplifier.class).iterator() : Collections.emptyIterator(); - this.dynamicModels = runSimplifiers(dataOptimizers, + Iterator dataSimplifiers = dynaWaltzParameters.isUseModelSimplifiers() ? ServiceLoader.load(ModelsSimplifier.class).iterator() : Collections.emptyIterator(); + this.dynamicModels = runSimplifiers(dataSimplifiers, Objects.requireNonNull(dynamicModels) .stream() .filter(distinctByDynamicId(reporter).and(distinctByStaticId(reporter))), @@ -114,8 +114,8 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function runSimplifiers(Iterator dataOptimizers, Stream inputData, Reporter reporter) { - return dataOptimizers.hasNext() ? runSimplifiers(dataOptimizers, dataOptimizers.next().simplifyModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; + private Stream runSimplifiers(Iterator dataSimplifiers, Stream inputData, Reporter reporter) { + return dataSimplifiers.hasNext() ? runSimplifiers(dataSimplifiers, dataSimplifiers.next().simplifyModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; } public Network getNetwork() { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index d38584bba..268b66e1c 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -39,7 +39,7 @@ public class DynaWaltzParameters extends AbstractExtension c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE); - boolean useModelOptimizers = config.flatMap(c -> c.getOptionalBooleanProperty("useModelOptimizers")).orElse(DEFAULT_USE_MODEL_OPTIMIZERS); + boolean useModelSimplifiers = config.flatMap(c -> c.getOptionalBooleanProperty("useModelSimplifiers")).orElse(USE_MODEL_SIMPLIFIERS); // Dump file config boolean exportDumpFile = config.flatMap(c -> c.getOptionalBooleanProperty("dump.export")).orElse(DumpFileParameters.DEFAULT_EXPORT_DUMP); @@ -134,7 +134,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem .setSolverType(solverType) .setMergeLoads(mergeLoads) .setWriteFinalState(writeFinalState) - .setUseModelOptimizers(useModelOptimizers) + .setUseModelSimplifiers(useModelSimplifiers) .setDumpFileParameters(dumpFileParameters); } @@ -208,12 +208,12 @@ public boolean isWriteFinalState() { return writeFinalState; } - public boolean isUseModelOptimizers() { - return useModelOptimizers; + public boolean isUseModelSimplifiers() { + return useModelSimplifiers; } - public DynaWaltzParameters setUseModelOptimizers(boolean useModelOptimizers) { - this.useModelOptimizers = useModelOptimizers; + public DynaWaltzParameters setUseModelSimplifiers(boolean useModelSimplifiers) { + this.useModelSimplifiers = useModelSimplifiers; return this; } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index 9a439a6bc..3f3b26525 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java @@ -57,8 +57,8 @@ void checkParameters() throws IOException { SolverType solverType = SolverType.IDA; String solverParametersId = "solverParametersId"; boolean mergeLoads = true; - boolean useModelOptimizers = true; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelOptimizers); + boolean useModelSimplifiers = true; + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers); DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem); @@ -88,7 +88,7 @@ void checkParameters() throws IOException { assertEquals(ParameterType.DOUBLE, absAccuracy.type()); assertEquals(mergeLoads, parameters.isMergeLoads()); - assertEquals(useModelOptimizers, parameters.isUseModelOptimizers()); + assertEquals(useModelSimplifiers, parameters.isUseModelSimplifiers()); } @Test @@ -122,7 +122,7 @@ void roundTripParametersSerializing() throws IOException { JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json"); } - private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelOptimizers) throws IOException { + private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelSimplifiers) throws IOException { String parametersFile = USER_HOME + "parametersFile"; String networkParametersFile = USER_HOME + "networkParametersFile"; String solverParametersFile = USER_HOME + "solverParametersFile"; @@ -135,7 +135,7 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp moduleConfig.setStringProperty("network.parametersId", networkParametersId); moduleConfig.setStringProperty("solver.type", solverType.toString()); moduleConfig.setStringProperty("solver.parametersId", solverParametersId); - moduleConfig.setStringProperty("useModelOptimizers", String.valueOf(useModelOptimizers)); + moduleConfig.setStringProperty("useModelSimplifiers", String.valueOf(useModelSimplifiers)); Files.createDirectories(fileSystem.getPath(USER_HOME)); copyFile("/parametersSet/models.par", parametersFile); @@ -182,7 +182,7 @@ void checkDefaultParameters() throws IOException { assertEquals("1", parameters.getSolverParameters().getId()); assertEquals(DynaWaltzParameters.DEFAULT_MERGE_LOADS, parameters.isMergeLoads()); - assertEquals(DynaWaltzParameters.DEFAULT_USE_MODEL_OPTIMIZERS, parameters.isUseModelOptimizers()); + assertEquals(DynaWaltzParameters.USE_MODEL_SIMPLIFIERS, parameters.isUseModelSimplifiers()); } @Test diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java index 52b6caa3c..f59b72482 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java @@ -42,7 +42,7 @@ void loadSimplifiers() { void simplifyModels() { Network network = EurostagTutorialExample1Factory.create(); DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); - DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load().setUseModelOptimizers(true); + DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load().setUseModelSimplifiers(true); List dynamicModels = List.of( new GeneratorFictitious("BBM_GEN", network.getGenerator("GEN"), "GPV"), new BaseLoad("BBM_LOAD", network.getLoad("LOAD"), "LOAD", "LoadAlphaBeta"), diff --git a/dynawaltz/src/test/resources/DynaWaltzParameters.json b/dynawaltz/src/test/resources/DynaWaltzParameters.json index 55802855d..8544c81e2 100644 --- a/dynawaltz/src/test/resources/DynaWaltzParameters.json +++ b/dynawaltz/src/test/resources/DynaWaltzParameters.json @@ -39,7 +39,7 @@ "solverType" : "IDA", "mergeLoads" : false, "writeFinalState" : true, - "useModelOptimizers" : false, + "useModelSimplifiers" : false, "dumpFileParameters" : { "exportDumpFile" : false, "useDumpFile" : false, From 554ae934689f5f2f51d9fb5972f463e0e4b6fb58 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 16:19:57 +0100 Subject: [PATCH 08/12] Simplify call to simplifiers Signed-off-by: Florian Dupuy --- .../powsybl/dynawaltz/DynaWaltzContext.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index bff2d47e8..2e1cc5b0f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -75,13 +75,11 @@ public DynaWaltzContext(Network network, String workingVariantId, List dataSimplifiers = dynaWaltzParameters.isUseModelSimplifiers() ? ServiceLoader.load(ModelsSimplifier.class).iterator() : Collections.emptyIterator(); - this.dynamicModels = runSimplifiers(dataSimplifiers, - Objects.requireNonNull(dynamicModels) - .stream() - .filter(distinctByDynamicId(reporter).and(distinctByStaticId(reporter))), - reporter) - .toList(); + Stream uniqueIdsDynamicModels = Objects.requireNonNull(dynamicModels).stream() + .filter(distinctByDynamicId(reporter).and(distinctByStaticId(reporter))); + this.dynamicModels = dynaWaltzParameters.isUseModelSimplifiers() + ? uniqueIdsDynamicModels.toList() + : simplifyModels(uniqueIdsDynamicModels, reporter).toList(); this.eventModels = Objects.requireNonNull(eventModels).stream() .filter(distinctByDynamicId(reporter)) @@ -107,6 +105,14 @@ public DynaWaltzContext(Network network, String workingVariantId, List simplifyModels(Stream inputBbm, Reporter reporter) { + Stream outputBbm = inputBbm; + for (ModelsSimplifier modelsSimplifier : ServiceLoader.load(ModelsSimplifier.class)) { + outputBbm = modelsSimplifier.simplifyModels(outputBbm, dynaWaltzParameters, reporter); + } + return outputBbm; + } + private FrequencySynchronizerModel setupFrequencySynchronizer(Function, FrequencySynchronizerModel> fsConstructor) { return fsConstructor.apply(dynamicModels.stream() .filter(FrequencySynchronizedModel.class::isInstance) @@ -114,10 +120,6 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function runSimplifiers(Iterator dataSimplifiers, Stream inputData, Reporter reporter) { - return dataSimplifiers.hasNext() ? runSimplifiers(dataSimplifiers, dataSimplifiers.next().simplifyModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData; - } - public Network getNetwork() { return network; } From 7daea1254812aca97c216402d26a12ef24a11e10 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 16:22:08 +0100 Subject: [PATCH 09/12] Revert orThrows renaming Signed-off-by: Florian Dupuy --- .../powsybl/dynawaltz/parameters/ParametersSet.java | 10 +++++----- .../dynawaltz/DynaWaltzParametersDatabaseTest.java | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java index daf60b1dd..39c968bf4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/parameters/ParametersSet.java @@ -67,22 +67,22 @@ public List getReferences() { } public boolean getBool(String parameterName) { - Parameter parameter = getParameterOrThrows(parameterName, ParameterType.BOOL); + Parameter parameter = getParameter(parameterName, ParameterType.BOOL); return Boolean.parseBoolean(parameter.value()); } public double getDouble(String parameterName) { - Parameter parameter = getParameterOrThrows(parameterName, ParameterType.DOUBLE); + Parameter parameter = getParameter(parameterName, ParameterType.DOUBLE); return Double.parseDouble(parameter.value()); } public int getInt(String parameterName) { - Parameter parameter = getParameterOrThrows(parameterName, ParameterType.INT); + Parameter parameter = getParameter(parameterName, ParameterType.INT); return Integer.parseInt(parameter.value()); } public String getString(String parameterName) { - Parameter parameter = getParameterOrThrows(parameterName, ParameterType.STRING); + Parameter parameter = getParameter(parameterName, ParameterType.STRING); return parameter.value(); } @@ -90,7 +90,7 @@ public boolean hasParameter(String parameterName) { return parameters.containsKey(parameterName); } - private Parameter getParameterOrThrows(String parameterName, ParameterType type) { + private Parameter getParameter(String parameterName, ParameterType type) { Parameter parameter = parameters.get(parameterName); if (parameter == null) { throw new IllegalArgumentException("Parameter " + parameterName + " not found in set " + id); diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java index 47d9958e8..a479a7fa6 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java @@ -57,6 +57,8 @@ void checkParameters() { assertEquals(1, set2.getInt("generator_ExcitationPu")); ParametersSet set3 = dParameters.getModelParameters("test"); + assertFalse(set3.hasParameter("unknown")); + assertTrue(set3.hasParameter("boolean")); assertTrue(set3.getBool("boolean")); assertEquals("aString", set3.getString("string")); } From 7aab0059be61ff02eae86efe638779a1b5c66604 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 16:38:08 +0100 Subject: [PATCH 10/12] Fix Side -> TwoSides Signed-off-by: Florian Dupuy --- .../src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java index b7c138a88..83d9ac20e 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java @@ -12,6 +12,7 @@ import com.powsybl.dynawaltz.models.hvdc.HvdcVscDangling; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.TwoSides; import com.powsybl.iidm.network.test.HvdcTestNetwork; import org.junit.jupiter.api.Test; @@ -34,11 +35,11 @@ void testDanglingConnectedStation() { Network network = HvdcTestNetwork.createVsc(); HvdcLine line = network.getHvdcLine("L"); - HvdcPDangling hvdc = new HvdcPDangling("hvdc", line, "HVDC", "HvdcPVDangling", Side.ONE); + HvdcPDangling hvdc = new HvdcPDangling("hvdc", line, "HVDC", "HvdcPVDangling", TwoSides.ONE); assertEquals(1, hvdc.getConnectedStations().size()); assertEquals(line.getConverterStation2(), hvdc.getConnectedStations().get(0)); - HvdcVscDangling hvdc2 = new HvdcVscDangling("hvdc", line, "HVDC", "HvdcVSCDanglingP", Side.TWO); + HvdcVscDangling hvdc2 = new HvdcVscDangling("hvdc", line, "HVDC", "HvdcVSCDanglingP", TwoSides.TWO); assertEquals(1, hvdc.getConnectedStations().size()); assertEquals(line.getConverterStation1(), hvdc2.getConnectedStations().get(0)); } From e123756ee93d727842eac6f17e149908bd6f9655 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 16:42:50 +0100 Subject: [PATCH 11/12] Fix Signed-off-by: Florian Dupuy --- .../src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index bc3936d35..c931279f3 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -80,8 +80,8 @@ public DynaWaltzContext(Network network, String workingVariantId, List uniqueIdsDynamicModels = Objects.requireNonNull(dynamicModels).stream() .filter(distinctByDynamicId(contextReporter).and(distinctByStaticId(contextReporter))); this.dynamicModels = dynaWaltzParameters.isUseModelSimplifiers() - ? uniqueIdsDynamicModels.toList() - : simplifyModels(uniqueIdsDynamicModels, contextReporter).toList(); + ? simplifyModels(uniqueIdsDynamicModels, contextReporter).toList() + : uniqueIdsDynamicModels.toList(); this.eventModels = Objects.requireNonNull(eventModels).stream() .filter(distinctByDynamicId(contextReporter)) From c010b674f74101af9efa8ba5bdb9fc9d78276942 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 16:54:59 +0100 Subject: [PATCH 12/12] Add unit tests Signed-off-by: Florian Dupuy --- .../DynaWaltzParametersDatabaseTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java index a479a7fa6..58ac7497f 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java @@ -9,6 +9,7 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; import com.powsybl.commons.PowsyblException; +import com.powsybl.dynawaltz.parameters.ParameterType; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.dynawaltz.xml.ParametersXml; import org.junit.jupiter.api.AfterEach; @@ -85,4 +86,22 @@ void addParametersSet() { assertEquals(1, dParameters.getModelParameters().size()); assertEquals(set, dParameters.getModelParameters("test")); } + + @Test + void replaceParameter() { + ParametersSet set = new ParametersSet("test"); + String param = "modifiedParam"; + set.addParameter(param, ParameterType.DOUBLE, "2.2"); + set.replaceParameter(param, ParameterType.INT, "3"); + assertEquals(3, set.getInt(param)); + assertThrows(PowsyblException.class, () -> set.getDouble(param)); + } + + @Test + void copyParametersSet() { + ParametersSet set0 = new ParametersSet("test"); + set0.addParameter("param", ParameterType.INT, "2"); + ParametersSet set1 = new ParametersSet("copy", set0); + assertEquals(2, set1.getInt("param")); + } }