diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index db00d3f9e..c931279f3 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -74,19 +74,23 @@ public DynaWaltzContext(Network network, String workingVariantId, List uniqueIdsDynamicModels = Objects.requireNonNull(dynamicModels).stream() + .filter(distinctByDynamicId(contextReporter).and(distinctByStaticId(contextReporter))); + this.dynamicModels = dynaWaltzParameters.isUseModelSimplifiers() + ? simplifyModels(uniqueIdsDynamicModels, contextReporter).toList() + : uniqueIdsDynamicModels.toList(); + + this.eventModels = Objects.requireNonNull(eventModels).stream() + .filter(distinctByDynamicId(contextReporter)) .toList(); this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream() .filter(EquipmentBlackBoxModel.class::isInstance) .map(EquipmentBlackBoxModel.class::cast) .collect(Collectors.toMap(EquipmentBlackBoxModel::getStaticId, Function.identity())); - this.eventModels = Objects.requireNonNull(eventModels).stream() - .filter(distinctByDynamicId(contextReporter)) - .toList(); - // Late init on ContextDependentEvents this.eventModels.stream() .filter(ContextDependentEvent.class::isInstance) @@ -94,8 +98,6 @@ public DynaWaltzContext(Network network, String workingVariantId, List e.setEquipmentHasDynamicModel(this)); this.curves = Objects.requireNonNull(curves); - this.parameters = Objects.requireNonNull(parameters); - this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters); this.frequencySynchronizer = setupFrequencySynchronizer(dynamicModels.stream().anyMatch(AbstractBus.class::isInstance) ? SetPoint::new : OmegaRef::new); this.macroConnectionsAdder = new MacroConnectionsAdder(this::getDynamicModel, this::getPureDynamicModel, @@ -117,6 +119,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) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index 82bf200f9..faecedf39 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -39,6 +39,7 @@ public class DynaWaltzParameters extends AbstractExtension c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE); + 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); String exportDumpFileFolder = config.flatMap(c -> c.getOptionalStringProperty("dump.exportFolder")).orElse(DumpFileParameters.DEFAULT_DUMP_FOLDER); @@ -130,6 +134,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem .setSolverType(solverType) .setMergeLoads(mergeLoads) .setWriteFinalState(writeFinalState) + .setUseModelSimplifiers(useModelSimplifiers) .setDumpFileParameters(dumpFileParameters); } @@ -138,6 +143,10 @@ public String getName() { return "DynaWaltzParameters"; } + public void addModelParameters(ParametersSet parameterSet) { + modelsParameters.put(parameterSet.getId(), parameterSet); + } + public ParametersSet getModelParameters(String parameterSetId) { ParametersSet parametersSet = modelsParameters.get(parameterSetId); if (parametersSet == null) { @@ -203,6 +212,15 @@ public boolean isWriteFinalState() { return writeFinalState; } + public boolean isUseModelSimplifiers() { + return useModelSimplifiers; + } + + public DynaWaltzParameters setUseModelSimplifiers(boolean useModelSimplifiers) { + this.useModelSimplifiers = useModelSimplifiers; + return this; + } + public DumpFileParameters getDumpFileParameters() { return dumpFileParameters; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java new file mode 100644 index 000000000..d84dbea13 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/ModelsSimplifier.java @@ -0,0 +1,21 @@ +/** + * 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 ModelsSimplifier { + + Stream simplifyModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter); +} 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 6366486f2..c4b793cd4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -25,7 +25,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); @@ -45,6 +45,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 a9085ac8c..c2eaedd05 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.buses.EquipmentConnectionPoint; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.utils.SideUtils; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.TwoSides; @@ -62,4 +63,8 @@ protected List getVarConnectionsWith(EquipmentConnectionPoint con protected final VarConnection getSimpleVarConnectionWithBus(EquipmentConnectionPoint connected, TwoSides side) { return new VarConnection(TERMINAL_PREFIX + side.getNum(), 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 2d02c8385..163a191bd 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 @@ -9,9 +9,12 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.TwoSides; +import java.util.List; + /** * @author Laurent Issertial {@literal } */ @@ -39,4 +42,9 @@ public String getSwitchOffSignalEventVarName(TwoSides side) { } return super.getSwitchOffSignalEventVarName(side); } + + @Override + public List> getConnectedStations() { + return List.of(danglingSide.isDangling(TwoSides.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 4fa9d33be..2aec9bbd3 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 @@ -8,10 +8,13 @@ package com.powsybl.dynawaltz.models.hvdc; import com.powsybl.commons.PowsyblException; +import com.powsybl.iidm.network.HvdcConverterStation; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.TwoSides; +import java.util.List; + /** * @author Laurent Issertial {@literal } */ @@ -39,4 +42,9 @@ public String getSwitchOffSignalEventVarName(TwoSides side) { } return super.getSwitchOffSignalEventVarName(side); } + + @Override + public List> getConnectedStations() { + return List.of(danglingSide.isDangling(TwoSides.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..39c968bf4 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; } @@ -70,16 +86,15 @@ public String getString(String parameterName) { return parameter.value(); } - public Parameter getParameter(String parameterName) { + public boolean hasParameter(String parameterName) { + return parameters.containsKey(parameterName); + } + + 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); } - return parameter; - } - - private Parameter getParameter(String parameterName, ParameterType type) { - Parameter parameter = getParameter(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/DynaWaltzParametersDatabaseTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersDatabaseTest.java index 9f7b9b24d..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; @@ -57,6 +58,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")); } @@ -74,4 +77,31 @@ 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")); + } + + @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")); + } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index 3a27d257a..3f3b26525 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.*; @@ -57,36 +57,38 @@ void checkParameters() throws IOException { SolverType solverType = SolverType.IDA; String solverParametersId = "solverParametersId"; boolean mergeLoads = true; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads); + boolean useModelSimplifiers = true; + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers); DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem); checkModelParameters(parameters); assertEquals(networkParametersId, parameters.getNetworkParameters().getId()); - ParametersSet networkParameters = parameters.getNetworkParameters(); - Parameter loadTp = networkParameters.getParameter("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.getParameter("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.getParameter("order"); + Parameter order = solverParameters.get("order"); assertEquals("1", order.value()); assertEquals("order", order.name()); assertEquals(ParameterType.INT, order.type()); - Parameter absAccuracy = solverParameters.getParameter("absAccuracy"); + Parameter absAccuracy = solverParameters.get("absAccuracy"); assertEquals("1e-4", absAccuracy.value()); assertEquals("absAccuracy", absAccuracy.name()); assertEquals(ParameterType.DOUBLE, absAccuracy.type()); assertEquals(mergeLoads, parameters.isMergeLoads()); + assertEquals(useModelSimplifiers, parameters.isUseModelSimplifiers()); } @Test @@ -109,7 +111,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) @@ -120,7 +122,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 useModelSimplifiers) throws IOException { String parametersFile = USER_HOME + "parametersFile"; String networkParametersFile = USER_HOME + "networkParametersFile"; String solverParametersFile = USER_HOME + "solverParametersFile"; @@ -133,6 +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("useModelSimplifiers", String.valueOf(useModelSimplifiers)); Files.createDirectories(fileSystem.getPath(USER_HOME)); copyFile("/parametersSet/models.par", parametersFile); @@ -179,6 +182,7 @@ void checkDefaultParameters() throws IOException { assertEquals("1", parameters.getSolverParameters().getId()); assertEquals(DynaWaltzParameters.DEFAULT_MERGE_LOADS, parameters.isMergeLoads()); + assertEquals(DynaWaltzParameters.USE_MODEL_SIMPLIFIERS, parameters.isUseModelSimplifiers()); } @Test @@ -231,11 +235,11 @@ void dumpFileNotFound() throws IOException { } private static void checkModelParameters(DynaWaltzParameters dynaWaltzParameters) { - Parameter booleanParameter = dynaWaltzParameters.getModelParameters("test").getParameter("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").getParameter("string"); + Parameter stringParameter = dynaWaltzParameters.getModelParameters("test").getParameters().get("string"); assertEquals("aString", stringParameter.value()); assertEquals("string", stringParameter.name()); assertEquals(ParameterType.STRING, stringParameter.type()); diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.java new file mode 100644 index 000000000..f59b72482 --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/ModelsSimplifierTest.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 ModelsSimplifierTest { + + @Test + void loadSimplifiers() { + List simplifiers = Lists.newArrayList(ServiceLoader.load(ModelsSimplifier.class)); + assertEquals(2, simplifiers.size()); + } + + @Test + void simplifyModels() { + Network network = EurostagTutorialExample1Factory.create(); + DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); + 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"), + 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(ModelsSimplifier.class) + public static class ModelsSimplifierFilter implements ModelsSimplifier { + @Override + public Stream simplifyModels(Stream models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { + return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD")); + } + } + + @AutoService(ModelsSimplifier.class) + public static class ModelsSimplifierSubstitution implements ModelsSimplifier { + @Override + 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"); + } + 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..83d9ac20e --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java @@ -0,0 +1,46 @@ +/** + * 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.TwoSides; +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", TwoSides.ONE); + assertEquals(1, hvdc.getConnectedStations().size()); + assertEquals(line.getConverterStation2(), hvdc.getConnectedStations().get(0)); + + HvdcVscDangling hvdc2 = new HvdcVscDangling("hvdc", line, "HVDC", "HvdcVSCDanglingP", TwoSides.TWO); + assertEquals(1, hvdc.getConnectedStations().size()); + assertEquals(line.getConverterStation1(), hvdc2.getConnectedStations().get(0)); + } +} diff --git a/dynawaltz/src/test/resources/DynaWaltzParameters.json b/dynawaltz/src/test/resources/DynaWaltzParameters.json index 583dac4d4..8544c81e2 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, + "useModelSimplifiers" : false, "dumpFileParameters" : { "exportDumpFile" : false, "useDumpFile" : false,