diff --git a/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/automatons/CurrentLimitAutomatonGroovyExtension.groovy b/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/automatons/CurrentLimitAutomatonGroovyExtension.groovy index 2cc6c6d2c..329ba7107 100644 --- a/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/automatons/CurrentLimitAutomatonGroovyExtension.groovy +++ b/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/automatons/CurrentLimitAutomatonGroovyExtension.groovy @@ -12,7 +12,9 @@ import com.powsybl.dynamicsimulation.DynamicModel import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension import com.powsybl.dynawaltz.dsl.AbstractDynamicModelBuilder import com.powsybl.dynawaltz.dsl.AbstractPowsyblDynawoGroovyExtension +import com.powsybl.dynawaltz.models.Side import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton +import com.powsybl.dynawaltz.models.utils.SideConverter import com.powsybl.iidm.network.Branch /** @@ -34,10 +36,10 @@ class CurrentLimitAutomatonGroovyExtension extends AbstractPowsyblDynawoGroovyEx static class CurrentLimitAutomatonBuilder extends AbstractDynamicModelBuilder { - Branch.Side side + Side side void side(Branch.Side side) { - this.side = side + this.side = SideConverter.convert(side) } @Override diff --git a/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/DynaWaltzGroovyDynamicModelsSupplierTest.java b/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/DynaWaltzGroovyDynamicModelsSupplierTest.java index 3ea67f158..892622bd0 100644 --- a/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/DynaWaltzGroovyDynamicModelsSupplierTest.java +++ b/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/DynaWaltzGroovyDynamicModelsSupplierTest.java @@ -41,6 +41,7 @@ import java.nio.file.FileSystem; import java.nio.file.Files; import java.util.List; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -58,8 +59,8 @@ void setup() throws IOException { fileSystem = Jimfs.newFileSystem(Configuration.unix()); network = createEurostagTutorialExample1WithMoreGens(); - Files.copy(getClass().getResourceAsStream("/dynamicModels.groovy"), fileSystem.getPath("/dynamicModels.groovy")); - Files.copy(getClass().getResourceAsStream("/models.par"), fileSystem.getPath("/models.par")); + Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/dynamicModels.groovy")), fileSystem.getPath("/dynamicModels.groovy")); + Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/models.par")), fileSystem.getPath("/models.par")); } @AfterEach @@ -182,8 +183,7 @@ private void validateExtension(DynamicModelGroovyExtension extension) { boolean isLineExtension = extension instanceof LineGroovyExtension; boolean isDynamicModelExtension = isLoadExtension || isGeneratorExtension || isBusExtension || isLineExtension; - boolean isCurrentLimitAutomatonExtension = extension instanceof CurrentLimitAutomatonGroovyExtension; - boolean isAutomatonExtension = isCurrentLimitAutomatonExtension; + boolean isAutomatonExtension = extension instanceof CurrentLimitAutomatonGroovyExtension; assertTrue(isDynamicModelExtension || isAutomatonExtension); } @@ -232,25 +232,34 @@ private void validateModel(DynamicModel dynamicModel) { } private void validateGeneratorSynchronous(GeneratorSynchronous generatorSynchronous) { - if (generatorSynchronous.getLib().equals("GeneratorSynchronousThreeWindingsProportionalRegulations")) { - Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); - assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); - assertEquals("GSTWPR", generatorSynchronous.getParameterSetId()); - assertTrue(identifiable instanceof Generator); - } else if (generatorSynchronous.getLib().equals("GeneratorSynchronousFourWindingsProportionalRegulations")) { - Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); - assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); - assertEquals("GSFWPR", generatorSynchronous.getParameterSetId()); - assertTrue(identifiable instanceof Generator); - } else if (generatorSynchronous.getLib().equals("GeneratorSynchronousThreeWindings")) { - Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); - assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); - assertEquals("GSTW", generatorSynchronous.getParameterSetId()); - } else if (generatorSynchronous.getLib().equals("GeneratorSynchronousFourWindings")) { - Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); - assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); - assertEquals("GSFW", generatorSynchronous.getParameterSetId()); - assertTrue(identifiable instanceof Generator); + switch (generatorSynchronous.getLib()) { + case "GeneratorSynchronousThreeWindingsProportionalRegulations": { + Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); + assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); + assertEquals("GSTWPR", generatorSynchronous.getParameterSetId()); + assertTrue(identifiable instanceof Generator); + break; + } + case "GeneratorSynchronousFourWindingsProportionalRegulations": { + Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); + assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); + assertEquals("GSFWPR", generatorSynchronous.getParameterSetId()); + assertTrue(identifiable instanceof Generator); + break; + } + case "GeneratorSynchronousThreeWindings": { + Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); + assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); + assertEquals("GSTW", generatorSynchronous.getParameterSetId()); + break; + } + case "GeneratorSynchronousFourWindings": { + Identifiable identifiable = network.getIdentifiable(generatorSynchronous.getStaticId().orElse(null)); + assertEquals("BBM_" + identifiable.getId(), generatorSynchronous.getDynamicModelId()); + assertEquals("GSFW", generatorSynchronous.getParameterSetId()); + assertTrue(identifiable instanceof Generator); + break; + } } } } diff --git a/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.dyd b/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.dyd index e05a94813..86805c28e 100644 --- a/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.dyd +++ b/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.dyd @@ -73,14 +73,14 @@ + + + - - - @@ -115,14 +115,14 @@ - - - - + + + + diff --git a/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.jobs b/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.jobs index e8c298159..efff61aa6 100644 --- a/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.jobs +++ b/dynawaltz-dsl/src/test/resources/ieee14-currentlimitautomaton/dynawaltz-inputs/powsybl_dynawaltz.jobs @@ -1,4 +1,4 @@ - + diff --git a/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.dyd b/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.dyd index ac452116d..b71ff7022 100644 --- a/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.dyd +++ b/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.dyd @@ -66,14 +66,14 @@ + + + - - - @@ -106,14 +106,14 @@ - - - - + + + + diff --git a/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.jobs b/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.jobs index 5bab39f79..5591a9cb7 100644 --- a/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.jobs +++ b/dynawaltz-dsl/src/test/resources/ieee14-disconnectline/dynawaltz-inputs/powsybl_dynawaltz.jobs @@ -1,4 +1,4 @@ - + diff --git a/dynawaltz-dsl/src/test/resources/ieee14-macroconnects/dynawaltz-inputs/powsybl_dynawaltz.dyd b/dynawaltz-dsl/src/test/resources/ieee14-macroconnects/dynawaltz-inputs/powsybl_dynawaltz.dyd index b2303839d..7f4041782 100644 --- a/dynawaltz-dsl/src/test/resources/ieee14-macroconnects/dynawaltz-inputs/powsybl_dynawaltz.dyd +++ b/dynawaltz-dsl/src/test/resources/ieee14-macroconnects/dynawaltz-inputs/powsybl_dynawaltz.dyd @@ -66,14 +66,14 @@ + + + - - - @@ -106,13 +106,13 @@ - - - - + + + + diff --git a/dynawaltz-dsl/src/test/resources/ieee57-disconnectgenerator/dynawaltz-inputs/powsybl_dynawaltz.dyd b/dynawaltz-dsl/src/test/resources/ieee57-disconnectgenerator/dynawaltz-inputs/powsybl_dynawaltz.dyd index 7459da91b..5150876fc 100644 --- a/dynawaltz-dsl/src/test/resources/ieee57-disconnectgenerator/dynawaltz-inputs/powsybl_dynawaltz.dyd +++ b/dynawaltz-dsl/src/test/resources/ieee57-disconnectgenerator/dynawaltz-inputs/powsybl_dynawaltz.dyd @@ -47,18 +47,18 @@ - - - - - - + + + + + + diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index aa0118343..8a3d7f5df 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,10 +11,7 @@ import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.models.*; -import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.generators.OmegaRefGeneratorModel; -import com.powsybl.dynawaltz.models.lines.LineModel; -import com.powsybl.dynawaltz.models.utils.ConnectedModelTypes; import com.powsybl.dynawaltz.xml.MacroStaticReference; import com.powsybl.iidm.network.Network; @@ -28,6 +25,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class DynaWaltzContext { @@ -41,10 +39,10 @@ public class DynaWaltzContext { private final Map staticIdBlackBoxModelMap; private final List curves; private final Map macroStaticReferences = new LinkedHashMap<>(); - private final Map connectorsMap = new LinkedHashMap<>(); - private final Map eventConnectorsMap = new LinkedHashMap<>(); - private final Map> modelsConnections = new LinkedHashMap<>(); - private final Map> eventModelsConnections = new LinkedHashMap<>(); + private final List macroConnectList = new ArrayList<>(); + private final Map macroConnectorsMap = new LinkedHashMap<>(); + private final List eventMacroConnectList = new ArrayList<>(); + private final Map eventMacroConnectorsMap = new LinkedHashMap<>(); private final NetworkModel networkModel = new NetworkModel(); private final OmegaRef omegaRef; @@ -70,32 +68,18 @@ public DynaWaltzContext(Network network, String workingVariantId, List synchronousGenerators = dynamicModels.stream() + this.omegaRef = new OmegaRef(dynamicModels.stream() .filter(OmegaRefGeneratorModel.class::isInstance) .map(OmegaRefGeneratorModel.class::cast) - .collect(Collectors.toList()); - this.omegaRef = new OmegaRef(synchronousGenerators); + .collect(Collectors.toList())); - for (BlackBoxModel bbm : Stream.concat(dynamicModels.stream(), Stream.of(omegaRef)).collect(Collectors.toList())) { + for (BlackBoxModel bbm : getBlackBoxDynamicModelStream().collect(Collectors.toList())) { macroStaticReferences.computeIfAbsent(bbm.getName(), k -> new MacroStaticReference(k, bbm.getVarsMapping())); - - List modelsConnected = bbm.getModelsConnectedTo(this); - modelsConnections.put(bbm, modelsConnected); - - for (Model connectedBbm : modelsConnected) { - var key = ConnectedModelTypes.of(bbm.getName(), connectedBbm.getName()); - connectorsMap.computeIfAbsent(key, k -> createMacroConnector(bbm, connectedBbm)); - } + bbm.createMacroConnections(this); } for (BlackBoxModel bbem : eventModels) { - List modelsConnected = bbem.getModelsConnectedTo(this); - eventModelsConnections.put(bbem, modelsConnected); - - for (Model connectedBbm : modelsConnected) { - var key = ConnectedModelTypes.of(bbem.getName(), connectedBbm.getName()); - eventConnectorsMap.computeIfAbsent(key, k -> createMacroConnector(bbem, connectedBbm)); - } + bbem.createMacroConnections(this); } } @@ -123,34 +107,15 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public Model getDynamicModelOrThrows(String staticId) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - throw new PowsyblException("Cannot find the equipment '" + staticId + "' among the dynamic models provided"); - } - return bbm; - } - - public LineModel getDynamicModelOrDefaultLine(String staticId) { + public T getDynamicModel(String staticId, Class clazz) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { - return networkModel.getDefaultLineModel(staticId); + return networkModel.getDefaultModel(staticId, clazz); } - if (bbm instanceof LineModel) { - return (LineModel) bbm; + if (clazz.isInstance(bbm)) { + return clazz.cast(bbm); } - throw new PowsyblException("The model identified by the static id " + staticId + " is not a line model"); - } - - public BusModel getDynamicModelOrDefaultBus(String staticId) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - return networkModel.getDefaultBusModel(staticId); - } - if (bbm instanceof BusModel) { - return (BusModel) bbm; - } - throw new PowsyblException("The model identified by the static id " + staticId + " is not a bus model"); + throw new PowsyblException("The model identified by the static id " + staticId + " is not the correct model"); } private BlackBoxModel mergeDuplicateStaticId(BlackBoxModel bbm1, BlackBoxModel bbm2) { @@ -171,32 +136,50 @@ private static List checkEventModelIdUniqueness(List getMacroConnectors() { - return connectorsMap.values(); + public void addMacroConnect(String macroConnectorId, List attributesFrom, List attributesTo) { + macroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); } - public MacroConnector getMacroConnector(Model model1, Model model2) { - return connectorsMap.get(ConnectedModelTypes.of(model1.getName(), model2.getName())); + public List getMacroConnectList() { + return macroConnectList; } - public Collection getEventMacroConnectors() { - return eventConnectorsMap.values(); + public String addMacroConnector(String name1, String name2, List varConnections) { + String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2); + macroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + return macroConnectorId; } - public MacroConnector getEventMacroConnector(BlackBoxModel event, Model model) { - return eventConnectorsMap.get(ConnectedModelTypes.of(event.getName(), model.getName())); + public String addMacroConnector(String name1, String name2, Side side, List varConnections) { + String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2, side); + macroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + return macroConnectorId; } - private MacroConnector createMacroConnector(BlackBoxModel bbm, Model model) { - return new MacroConnector(bbm.getName(), model.getName(), bbm.getVarConnectionsWith(model)); + public Collection getMacroConnectors() { + return macroConnectorsMap.values(); } - public Map> getModelsConnections() { - return modelsConnections; + public void addEventMacroConnect(String macroConnectorId, List attributesFrom, List attributesTo) { + eventMacroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); + } + + public List getEventMacroConnectList() { + return eventMacroConnectList; + } + + public String addEventMacroConnector(String name1, String name2, List varConnections) { + String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2); + eventMacroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + return macroConnectorId; + } + + public Collection getEventMacroConnectors() { + return eventMacroConnectorsMap.values(); } - public Map> getEventModelsConnections() { - return eventModelsConnections; + public boolean isWithoutBlackBoxDynamicModel(String staticId) { + return !staticIdBlackBoxModelMap.containsKey(staticId); } private Stream getInputBlackBoxDynamicModelStream() { 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 584859396..ad82c3dde 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -8,18 +8,20 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.xml.MacroStaticReference; -import org.apache.commons.lang3.tuple.Pair; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; /** * @author Luma ZamarreƱo + * @author Laurent Issertial */ public abstract class AbstractBlackBoxModel implements BlackBoxModel { @@ -55,18 +57,13 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th // method empty by default to be redefined by specific models } - @Override - public void writeMacroConnect(XMLStreamWriter writer, DynaWaltzContext context, MacroConnector macroConnector, Model connected) throws XMLStreamException { - macroConnector.writeMacroConnect(writer, getMacroConnectFromAttributes(), connected.getMacroConnectToAttributes()); - } - - protected List> getMacroConnectFromAttributes() { - return List.of(Pair.of("id1", getDynamicModelId())); + protected List getMacroConnectFromAttributes() { + return List.of(MacroConnectAttribute.of("id1", getDynamicModelId())); } @Override - public List> getMacroConnectToAttributes() { - return List.of(Pair.of("id2", getDynamicModelId())); + public List getMacroConnectToAttributes() { + return List.of(MacroConnectAttribute.of("id2", getDynamicModelId())); } @Override @@ -74,6 +71,11 @@ public String getParFile(DynaWaltzContext context) { return context.getParFile(); } + @Override + public List getVarsMapping() { + return Collections.emptyList(); + } + protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { writer.writeAttribute("id", getDynamicModelId()); writer.writeAttribute("lib", getLib()); @@ -83,25 +85,43 @@ protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext c @Override public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - if (staticId != null) { - writeBlackBoxModel(writer, context); + boolean hasVarMapping = !getVarsMapping().isEmpty(); + if (hasVarMapping) { + writer.writeStartElement(DYN_URI, "blackBoxModel"); } else { - writePureDynamicBlackBoxModel(writer, context); + writer.writeEmptyElement(DYN_URI, "blackBoxModel"); } - } - - protected void writeBlackBoxModel(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - writer.writeStartElement(DYN_URI, "blackBoxModel"); writeDynamicAttributes(writer, context); - writer.writeAttribute("staticId", staticId); - if (!getVarsMapping().isEmpty()) { + writer.writeAttribute("staticId", getStaticId().orElseThrow()); + if (hasVarMapping) { MacroStaticReference.writeMacroStaticRef(writer, getLib()); + writer.writeEndElement(); } - writer.writeEndElement(); } - protected void writePureDynamicBlackBoxModel(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - writer.writeEmptyElement(DYN_URI, "blackBoxModel"); - writeDynamicAttributes(writer, context); + protected void createMacroConnections(String modelStaticId, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass); + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + } + + protected final void createMacroConnections(T connectedModel, List varConnections, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnections); + List fromAttributes = Stream.concat(getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); + context.addMacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); + } + + protected final void createMacroConnections(String modelStaticId, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass); + createMacroConnections(connectedModel, varConnectionsSupplier.apply(connectedModel), context, connectFromAttributes); + } + + /** + * Suffixes MacroConnector id with side name + */ + protected final void createMacroConnections(String modelStaticId, Class modelClass, BiFunction> varConnectionsSupplier, DynaWaltzContext context, Side side) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass); + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), side, varConnectionsSupplier.apply(connectedModel, side)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractNetworkModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractNetworkModel.java index 0da4b5db1..2a9a14b21 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractNetworkModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractNetworkModel.java @@ -1,7 +1,6 @@ package com.powsybl.dynawaltz.models; import com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants; -import org.apache.commons.lang3.tuple.Pair; import java.util.List; import java.util.Objects; @@ -21,10 +20,10 @@ public Optional getStaticId() { } @Override - public List> getMacroConnectToAttributes() { + public List getMacroConnectToAttributes() { return List.of( - Pair.of("id2", DynaWaltzXmlConstants.NETWORK), - Pair.of("name2", staticId) + MacroConnectAttribute.of("id2", DynaWaltzXmlConstants.NETWORK), + MacroConnectAttribute.of("name2", staticId) ); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractPureDynamicBlackBoxModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractPureDynamicBlackBoxModel.java index 6c96a1dff..6f9e02923 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractPureDynamicBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractPureDynamicBlackBoxModel.java @@ -6,11 +6,18 @@ */ package com.powsybl.dynawaltz.models; +import com.powsybl.dynawaltz.DynaWaltzContext; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; import java.util.Collections; import java.util.List; +import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; + /** * @author Florian Dupuy + * @author Laurent Issertial */ public abstract class AbstractPureDynamicBlackBoxModel extends AbstractBlackBoxModel { @@ -23,4 +30,10 @@ public final List getVarsMapping() { // No static-dynamic mapping as purely dynamic return Collections.emptyList(); } + + @Override + public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { + writer.writeEmptyElement(DYN_URI, "blackBoxModel"); + writeDynamicAttributes(writer, context); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java index cd178a548..bbe2ebff5 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java @@ -14,6 +14,7 @@ /** * @author Florian Dupuy + * @author Laurent Issertial */ public interface BlackBoxModel extends Model { String getDynamicModelId(); @@ -24,15 +25,11 @@ public interface BlackBoxModel extends Model { List getVarsMapping(); - List getVarConnectionsWith(Model connected); - - List getModelsConnectedTo(DynaWaltzContext dynaWaltzContext); + void createMacroConnections(DynaWaltzContext dynaWaltzContext); String getParFile(DynaWaltzContext context); void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; - - void writeMacroConnect(XMLStreamWriter writer, DynaWaltzContext context, MacroConnector macroConnector, Model connected) throws XMLStreamException; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/DefaultModelFactory.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/DefaultModelFactory.java new file mode 100644 index 000000000..6b0c81613 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/DefaultModelFactory.java @@ -0,0 +1,30 @@ +/** + * 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 java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +/** + * @author Laurent Issertial + */ +public class DefaultModelFactory { + + private final Map defaultModelMap = new HashMap<>(); + + private final Function modelConstructor; + + DefaultModelFactory(Function modelConstructor) { + this.modelConstructor = modelConstructor; + } + + public T getDefaultModel(String staticId) { + return defaultModelMap.computeIfAbsent(staticId, key -> modelConstructor.apply(staticId)); + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java new file mode 100644 index 000000000..0945294ed --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java @@ -0,0 +1,41 @@ +/** + * 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 javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import java.util.List; + +import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; + +/** + * @author Laurent Issertial + */ +public final class MacroConnect { + + private final String id; + private final List attributesFrom; + private final List attributesTo; + + public MacroConnect(String id, List attributesFrom, List attributesTo) { + this.id = id; + this.attributesFrom = attributesFrom; + this.attributesTo = attributesTo; + } + + public void write(XMLStreamWriter writer) throws XMLStreamException { + writer.writeEmptyElement(DYN_URI, "macroConnect"); + writer.writeAttribute("connector", id); + for (MacroConnectAttribute attribute : attributesFrom) { + writer.writeAttribute(attribute.getName(), attribute.getValue()); + } + for (MacroConnectAttribute attribute : attributesTo) { + writer.writeAttribute(attribute.getName(), attribute.getValue()); + } + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java new file mode 100644 index 000000000..3d636107f --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java @@ -0,0 +1,37 @@ +/** + * 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; + +/** + * @author Laurent Issertial + */ +public class MacroConnectAttribute { + private final String name; + private final String value; + + public MacroConnectAttribute(String name, String value) { + this.name = name; + this.value = value; + } + + public static MacroConnectAttribute of(String name, String value) { + return new MacroConnectAttribute(name, value); + } + + public static MacroConnectAttribute ofIndex1(int index) { + return new MacroConnectAttribute("index1", String.valueOf(index)); + } + + public String getName() { + return name; + } + + public String getValue() { + return value; + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java index f69dc59e2..e20c4b249 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -6,39 +6,33 @@ */ package com.powsybl.dynawaltz.models; -import org.apache.commons.lang3.tuple.Pair; - -import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; -import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.MACRO_CONNECTOR_PREFIX; - import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.List; +import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; +import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.MACRO_CONNECTOR_PREFIX; + /** * @author Marcos de Miguel + * @author Laurent Issertial */ public final class MacroConnector { private final List varConnections; private final String id; - public MacroConnector(String name1, String name2, List varConnections) { - this.id = MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; + public MacroConnector(String id, List varConnections) { + this.id = id; this.varConnections = varConnections; } - public void writeMacroConnect(XMLStreamWriter writer, - List> attributesFrom, - List> attributesTo) throws XMLStreamException { - writer.writeEmptyElement(DYN_URI, "macroConnect"); - writer.writeAttribute("connector", id); - for (Pair attribute : attributesFrom) { - writer.writeAttribute(attribute.getKey(), attribute.getValue()); - } - for (Pair attribute : attributesTo) { - writer.writeAttribute(attribute.getKey(), attribute.getValue()); - } + public static String createMacroConnectorId(String name1, String name2) { + return MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; + } + + public static String createMacroConnectorId(String name1, String name2, Side side) { + return MACRO_CONNECTOR_PREFIX + name1 + side.getSideSuffix() + "-" + name2; } public void write(XMLStreamWriter writer) throws XMLStreamException { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Model.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Model.java index e2ed049b4..e89245a8c 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Model.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Model.java @@ -7,7 +7,6 @@ package com.powsybl.dynawaltz.models; import com.powsybl.dynamicsimulation.DynamicModel; -import org.apache.commons.lang3.tuple.Pair; import java.util.List; import java.util.Optional; @@ -20,5 +19,5 @@ public interface Model extends DynamicModel { String getName(); - List> getMacroConnectToAttributes(); + List getMacroConnectToAttributes(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java index 3144bdf00..76de3cbfe 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java @@ -6,28 +6,32 @@ */ package com.powsybl.dynawaltz.models; +import com.powsybl.commons.PowsyblException; +import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.buses.DefaultBusModel; import com.powsybl.dynawaltz.models.lines.DefaultLineModel; +import com.powsybl.dynawaltz.models.lines.LineModel; -import java.util.HashMap; import java.util.Map; -import java.util.function.Function; /** * @author Florian Dupuy + * @author Laurent Issertial */ public class NetworkModel { - private final Map defaultBusModelMap = new HashMap<>(); - private final Map defaultLineModelMap = new HashMap<>(); - private final Function busModelFactory = DefaultBusModel::new; - private final Function lineModelFactory = DefaultLineModel::new; + private final Map, DefaultModelFactory> factoryMap; - public DefaultBusModel getDefaultBusModel(String staticId) { - return defaultBusModelMap.computeIfAbsent(staticId, key -> busModelFactory.apply(staticId)); + public NetworkModel() { + factoryMap = Map.of(BusModel.class, new DefaultModelFactory(DefaultBusModel::new), + LineModel.class, new DefaultModelFactory(DefaultLineModel::new)); } - public DefaultLineModel getDefaultLineModel(String staticId) { - return defaultLineModelMap.computeIfAbsent(staticId, key -> lineModelFactory.apply(staticId)); + public T getDefaultModel(String staticId, Class clazz) { + DefaultModelFactory dmf = (DefaultModelFactory) factoryMap.get(clazz); + if (dmf != null) { + return dmf.getDefaultModel(staticId); + } + throw new PowsyblException("Default model not implemented for " + clazz.getSimpleName()); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java index b092e0b6c..a9f8b0f9e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -11,17 +11,13 @@ import com.powsybl.dynawaltz.DynaWaltzParametersDatabase; import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.generators.OmegaRefGeneratorModel; -import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; import com.powsybl.dynawaltz.models.utils.BusUtils; import com.powsybl.dynawaltz.xml.ParametersXml; import com.powsybl.iidm.network.Generator; -import org.apache.commons.lang3.tuple.Pair; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import java.util.List; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.DOUBLE; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.INT; @@ -35,24 +31,21 @@ * dynamic model and one to connect it to the NETWORK model. * * @author Marcos de Miguel + * @author Laurent Issertial */ public class OmegaRef extends AbstractPureDynamicBlackBoxModel { public static final String OMEGA_REF_ID = "OMEGA_REF"; private static final String OMEGA_REF_PARAMETER_SET_ID = "OMEGA_REF"; - private final Map synchronousGenerators = new LinkedHashMap<>(); - private final Map> busGeneratorListMap = new LinkedHashMap<>(); + private final List omegaRefGenerators; - public OmegaRef(List synchronousGenerators) { + public OmegaRef(List omegaRefGenerators) { super(OMEGA_REF_ID, OMEGA_REF_PARAMETER_SET_ID); - int i = 0; - for (OmegaRefGeneratorModel synchronousGenerator : synchronousGenerators) { - this.synchronousGenerators.put(synchronousGenerator, i++); - } + this.omegaRefGenerators = omegaRefGenerators; } public boolean isEmpty() { - return synchronousGenerators.isEmpty(); + return omegaRefGenerators.isEmpty(); } @Override @@ -69,84 +62,47 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th // The dynamic models are declared in the DYD following the order of dynamic models' supplier. // The OmegaRef parameters index the weight of each generator according to that declaration order. - for (Map.Entry e : synchronousGenerators.entrySet()) { - OmegaRefGeneratorModel generator = e.getKey(); + int index = 0; + for (OmegaRefGeneratorModel generator : omegaRefGenerators) { double h = parDB.getDouble(generator.getParameterSetId(), "generator_H"); double snom = parDB.getDouble(generator.getParameterSetId(), "generator_SNom"); - - ParametersXml.writeParameter(writer, DOUBLE, "weight_gen_" + e.getValue(), Double.toString(h * snom)); + ParametersXml.writeParameter(writer, DOUBLE, "weight_gen_" + index, Double.toString(h * snom)); + index++; } - ParametersXml.writeParameter(writer, INT, "nbGen", Long.toString(synchronousGenerators.size())); + ParametersXml.writeParameter(writer, INT, "nbGen", Long.toString(omegaRefGenerators.size())); writer.writeEndElement(); } - @Override - public List getVarConnectionsWith(Model connected) { - if (connected instanceof GeneratorSynchronousModel) { - GeneratorSynchronousModel connectedGeneratorModel = (GeneratorSynchronousModel) connected; - return Arrays.asList( - new VarConnection("omega_grp_@INDEX@", connectedGeneratorModel.getOmegaPuVarName()), - new VarConnection("omegaRef_grp_@INDEX@", connectedGeneratorModel.getOmegaRefPuVarName()), - new VarConnection("running_grp_@INDEX@", connectedGeneratorModel.getRunningVarName()) - ); - } else if (connected instanceof OmegaRefGeneratorModel) { - OmegaRefGeneratorModel connectedOmegaRefGeneratorModel = (OmegaRefGeneratorModel) connected; - return Arrays.asList( - new VarConnection("omegaRef_grp_@INDEX@", connectedOmegaRefGeneratorModel.getOmegaRefPuVarName()), - new VarConnection("running_grp_@INDEX@", connectedOmegaRefGeneratorModel.getRunningVarName()) - ); - } else if (connected instanceof BusModel) { - return List.of(new VarConnection("numcc_node_@INDEX@", ((BusModel) connected).getNumCCVarName())); - } else { - throw new PowsyblException("OmegaRef can only connect to GeneratorModel and BusModel"); - } + private List getVarConnectionsWithOmegaRefGenerator(OmegaRefGeneratorModel connected) { + return connected.getOmegaRefVarConnections(); + } + + private List getVarConnectionsWithBus(BusModel connected) { + return List.of(new VarConnection("numcc_node_@INDEX@", connected.getNumCCVarName())); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) throws PowsyblException { - for (OmegaRefGeneratorModel g : synchronousGenerators.keySet()) { - BusModel bus = getBusAssociatedTo(g, context); - busGeneratorListMap.computeIfAbsent(bus, k -> new ArrayList<>()).add(g); + public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { + int index = 0; + for (OmegaRefGeneratorModel gen : omegaRefGenerators) { + createMacroConnections(gen, getVarConnectionsWithOmegaRefGenerator(gen), context, MacroConnectAttribute.ofIndex1(index)); + createMacroConnections(getBusAssociatedTo(gen, context), BusModel.class, this::getVarConnectionsWithBus, context, MacroConnectAttribute.ofIndex1(index)); + index++; } - return Stream.concat(synchronousGenerators.keySet().stream(), busGeneratorListMap.keySet().stream()) - .collect(Collectors.toList()); } - private BusModel getBusAssociatedTo(OmegaRefGeneratorModel generatorModel, DynaWaltzContext context) { + private String getBusAssociatedTo(OmegaRefGeneratorModel generatorModel, DynaWaltzContext context) { Generator generator = generatorModel.getStaticId().map(staticId -> context.getNetwork().getGenerator(staticId)).orElse(null); if (generator == null) { throw new PowsyblException("Generator " + generatorModel.getLib() + " not found in DynaWaltz context. Id : " + generatorModel.getDynamicModelId()); } - return context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(generator)); + return BusUtils.getConnectableBusStaticId(generator); } @Override public String getParFile(DynaWaltzContext context) { return context.getSimulationParFile(); } - - @Override - public void writeMacroConnect(XMLStreamWriter writer, DynaWaltzContext context, MacroConnector macroConnector, Model connected) throws XMLStreamException { - if (connected instanceof OmegaRefGeneratorModel) { - List> attributesConnectFrom = getAttributesConnectFrom((OmegaRefGeneratorModel) connected); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, connected.getMacroConnectToAttributes()); - } else if (connected instanceof BusModel) { - BusModel bus = (BusModel) connected; - for (OmegaRefGeneratorModel g : busGeneratorListMap.get(bus)) { - List> attributesConnectFrom = getAttributesConnectFrom(g); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, bus.getMacroConnectToAttributes()); - } - } else { - throw new PowsyblException("OmegaRef can only connect to OmegaRefGeneratorModel and BusModel"); - } - } - - private List> getAttributesConnectFrom(OmegaRefGeneratorModel generator) { - int index = synchronousGenerators.get(generator); - return List.of( - Pair.of("id1", OMEGA_REF_ID), - Pair.of("index1", Integer.toString(index))); - } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Side.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Side.java new file mode 100644 index 000000000..b51a5baa5 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Side.java @@ -0,0 +1,27 @@ +/** + * 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; + +/** + * @author Laurent Issertial + */ +public enum Side { + + ONE("Side1"), + TWO("Side2"); + + private final String sideSuffix; + + Side(String sideSuffix) { + this.sideSuffix = sideSuffix; + } + + public String getSideSuffix() { + return sideSuffix; + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java index 92b76b9ab..c99d97900 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java @@ -6,14 +6,11 @@ */ package com.powsybl.dynawaltz.models.automatons; -import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; -import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; -import com.powsybl.dynawaltz.models.utils.LineSideUtils; -import com.powsybl.iidm.network.Branch; import java.util.Arrays; import java.util.List; @@ -21,13 +18,14 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class CurrentLimitAutomaton extends AbstractPureDynamicBlackBoxModel { - private final Branch.Side side; + private final Side side; private final String lineStaticId; - public CurrentLimitAutomaton(String dynamicModelId, String staticId, String parameterSetId, Branch.Side side) { + public CurrentLimitAutomaton(String dynamicModelId, String staticId, String parameterSetId, Side side) { super(dynamicModelId, parameterSetId); this.side = Objects.requireNonNull(side); this.lineStaticId = staticId; @@ -39,25 +37,15 @@ public String getLib() { } @Override - public String getName() { - return getLib() + LineSideUtils.getSuffix(side); + public void createMacroConnections(DynaWaltzContext context) { + createMacroConnections(lineStaticId, LineModel.class, this::getVarConnectionsWithLine, context, side); } - @Override - public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(lineStaticId)); - } - - @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof LineModel)) { - throw new PowsyblException("CurrentLimitAutomaton can only connect to LineModel"); - } - LineModel connectedLineModel = (LineModel) connected; + private List getVarConnectionsWithLine(LineModel connected, Side side) { return Arrays.asList( - new VarConnection("currentLimitAutomaton_IMonitored", connectedLineModel.getIVarName(side)), - new VarConnection("currentLimitAutomaton_order", connectedLineModel.getStateVarName()), - new VarConnection("currentLimitAutomaton_AutomatonExists", connectedLineModel.getDesactivateCurrentLimitsVarName()) + new VarConnection("currentLimitAutomaton_IMonitored", connected.getIVarName(side)), + new VarConnection("currentLimitAutomaton_order", connected.getStateVarName()), + new VarConnection("currentLimitAutomaton_AutomatonExists", connected.getDesactivateCurrentLimitsVarName()) ); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/StandardBus.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/StandardBus.java index f2bd056af..716cb207e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/StandardBus.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/StandardBus.java @@ -10,25 +10,19 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.Model; -import com.powsybl.dynawaltz.models.VarConnection; -import com.powsybl.dynawaltz.models.VarMapping; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; +import com.powsybl.dynawaltz.models.MacroConnectAttribute; import com.powsybl.iidm.network.Bus; -import org.apache.commons.lang3.tuple.Pair; +import com.powsybl.iidm.network.Identifiable; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; - -import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; +import java.util.stream.Stream; /** * @author Dimitri Baudrier + * @author Laurent Issertial */ public class StandardBus extends AbstractBlackBoxModel implements BusModel { @@ -42,46 +36,39 @@ public String getLib() { } @Override - public List getVarsMapping() { - return Collections.emptyList(); - } - - @Override - public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - writer.writeStartElement(DYN_URI, "blackBoxModel"); + protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { writer.writeAttribute("id", getDynamicModelId()); writer.writeAttribute("lib", getLib()); - writer.writeAttribute("staticId", getStaticId().orElse(null)); - writer.writeEndElement(); } @Override - public List> getMacroConnectToAttributes() { - List> attributesConnectTo = new ArrayList<>(super.getMacroConnectToAttributes()); - attributesConnectTo.add(Pair.of("name2", getStaticId().orElse(null))); + public List getMacroConnectToAttributes() { + List attributesConnectTo = new ArrayList<>(super.getMacroConnectToAttributes()); + attributesConnectTo.add(MacroConnectAttribute.of("name2", getStaticId().orElse(null))); return attributesConnectTo; } @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof GeneratorModel)) { - throw new PowsyblException("StandardBusModel can only connect to GeneratorModel"); - } - GeneratorModel connectedGeneratorModel = (GeneratorModel) connected; - return Arrays.asList( - new VarConnection(getTerminalVarName(), connectedGeneratorModel.getTerminalVarName()), - new VarConnection(getSwitchOffSignalVarName(), connectedGeneratorModel.getSwitchOffSignalNodeVarName()) - ); - } - - @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { + // Buses with a dynamical model can only connect to equipment with a dynamic model String staticId = getStaticId().orElse(null); Bus bus = context.getNetwork().getBusBreakerView().getBus(staticId); if (bus == null) { throw new PowsyblException("Bus static id unknown: " + staticId); } - return bus.getGeneratorStream().map(g -> context.getDynamicModelOrThrows(g.getId())).collect(Collectors.toList()); + checkLinkedDynamicModels(bus.getGeneratorStream(), "generator", context); + checkLinkedDynamicModels(bus.getLineStream(), "line", context); + checkLinkedDynamicModels(bus.getLoadStream(), "load", context); + } + + private > void checkLinkedDynamicModels(Stream stream, String equipmentName, DynaWaltzContext context) { + stream.map(Identifiable::getId) + .filter(context::isWithoutBlackBoxDynamicModel) + .findAny() + .ifPresent(id -> { + throw new PowsyblException(String.format("The %s %s linked to the standard bus %s does not possess a dynamic model", + equipmentName, id, getStaticId().orElse(null))); + }); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEventModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEventModel.java index fd942beb2..62657a099 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEventModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEventModel.java @@ -10,16 +10,21 @@ import com.powsybl.dynamicsimulation.EventModel; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.xml.ParametersXml; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import java.util.List; +import java.util.function.Function; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.DOUBLE; import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; /** * @author Florian Dupuy + * @author Laurent Issertial */ public abstract class AbstractEventModel extends AbstractPureDynamicBlackBoxModel implements EventModel { @@ -56,4 +61,11 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th } protected abstract void writeEventSpecificParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; + + @Override + protected final void createMacroConnections(String modelStaticId, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass); + String macroConnectorId = context.addEventMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); + context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventQuadripoleDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventQuadripoleDisconnection.java index 8602787d9..e0b710d9f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventQuadripoleDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventQuadripoleDisconnection.java @@ -6,9 +6,7 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.xml.ParametersXml; @@ -21,6 +19,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class EventQuadripoleDisconnection extends AbstractEventModel { @@ -39,17 +38,13 @@ public String getLib() { return "EventQuadripoleDisconnection"; } - @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof LineModel)) { - throw new PowsyblException("EventQuadripoleDisconnection can only connect to LineModel"); - } - return List.of(new VarConnection("event_state1_value", ((LineModel) connected).getStateValueVarName())); + private List getVarConnectionsWithLine(LineModel connected) { + return List.of(new VarConnection("event_state1_value", connected.getStateValueVarName())); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(getEquipmentStaticId())); + public void createMacroConnections(DynaWaltzContext context) { + createMacroConnections(getEquipmentStaticId(), LineModel.class, this::getVarConnectionsWithLine, context); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventSetPointBoolean.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventSetPointBoolean.java index 932b365df..2f09ea2da 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventSetPointBoolean.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventSetPointBoolean.java @@ -6,9 +6,7 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.generators.GeneratorModel; import com.powsybl.dynawaltz.xml.ParametersXml; @@ -21,6 +19,7 @@ /** * @author Mathieu BAGUE {@literal } + * @author Laurent Issertial */ public class EventSetPointBoolean extends AbstractEventModel { @@ -36,17 +35,13 @@ public String getLib() { return "EventSetPointBoolean"; } - @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof GeneratorModel)) { - throw new PowsyblException("EventSetPointBoolean can only connect to GeneratorModel"); - } - return List.of(new VarConnection("event_state1", ((GeneratorModel) connected).getSwitchOffSignalEventVarName())); + private List getVarConnectionsWithGenerator(GeneratorModel connected) { + return List.of(new VarConnection("event_state1", connected.getSwitchOffSignalEventVarName())); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrThrows(getEquipmentStaticId())); + public void createMacroConnections(DynaWaltzContext context) { + createMacroConnections(getEquipmentStaticId(), GeneratorModel.class, this::getVarConnectionsWithGenerator, context); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGeneratorModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGeneratorModel.java index 773110ac2..4c123aa01 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGeneratorModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGeneratorModel.java @@ -9,7 +9,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.BusModel; @@ -22,6 +21,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public abstract class AbstractGeneratorModel extends AbstractBlackBoxModel implements GeneratorModel { @@ -54,24 +54,19 @@ public List getVarsMapping() { } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { String staticId = getStaticId().orElse(null); // cannot be empty as checked in constructor Generator generator = context.getNetwork().getGenerator(staticId); if (generator == null) { throw new PowsyblException("Generator static id unknown: " + staticId); } - return List.of(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(generator))); + createMacroConnections(BusUtils.getConnectableBusStaticId(generator), BusModel.class, this::getVarConnectionsWithBus, context); } - @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof BusModel)) { - throw new PowsyblException("GeneratorModel can only connect to BusModel"); - } - BusModel connectedBusModel = (BusModel) connected; + private List getVarConnectionsWithBus(BusModel connected) { return Arrays.asList( - new VarConnection(getTerminalVarName(), connectedBusModel.getTerminalVarName()), - new VarConnection(getSwitchOffSignalNodeVarName(), connectedBusModel.getSwitchOffSignalVarName()) + new VarConnection(getTerminalVarName(), connected.getTerminalVarName()), + new VarConnection(getSwitchOffSignalNodeVarName(), connected.getSwitchOffSignalVarName()) ); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronous.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronous.java index b223784cc..3624c1250 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronous.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronous.java @@ -5,6 +5,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package com.powsybl.dynawaltz.models.generators; + +import com.powsybl.dynawaltz.models.VarConnection; + +import java.util.Arrays; +import java.util.List; + /** * @author Florian Dupuy */ @@ -15,8 +21,16 @@ public GeneratorSynchronous(String dynamicModelId, String staticId, String param super(dynamicModelId, staticId, parameterSetId, generatorLib); } - @Override - public String getOmegaPuVarName() { + protected String getOmegaPuVarName() { return "generator_omegaPu"; } + + @Override + public List getOmegaRefVarConnections() { + return Arrays.asList( + new VarConnection("omega_grp_@INDEX@", getOmegaPuVarName()), + new VarConnection("omegaRef_grp_@INDEX@", getOmegaRefPuVarName()), + new VarConnection("running_grp_@INDEX@", getRunningVarName()) + ); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronousModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronousModel.java index 45e55e13e..20b6be251 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronousModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GeneratorSynchronousModel.java @@ -10,5 +10,4 @@ * @author Florian Dupuy */ public interface GeneratorSynchronousModel extends OmegaRefGeneratorModel { - String getOmegaPuVarName(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGenerator.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGenerator.java index 345c5000c..da4daca97 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGenerator.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGenerator.java @@ -7,6 +7,11 @@ */ package com.powsybl.dynawaltz.models.generators; +import com.powsybl.dynawaltz.models.VarConnection; + +import java.util.Arrays; +import java.util.List; + /** * @author Dimitri Baudrier */ @@ -24,11 +29,18 @@ public OmegaRefGenerator(String dynamicModelId, String staticId, String paramete this.generatorLib = generatorLib; } - @Override - public String getOmegaRefPuVarName() { + protected String getOmegaRefPuVarName() { return "generator_omegaRefPu"; } + @Override + public List getOmegaRefVarConnections() { + return Arrays.asList( + new VarConnection("omegaRef_grp_@INDEX@", getOmegaRefPuVarName()), + new VarConnection("running_grp_@INDEX@", getRunningVarName()) + ); + } + @Override public String getLib() { return generatorLib; diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGeneratorModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGeneratorModel.java index 6ec48f017..15dd2e80f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGeneratorModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/OmegaRefGeneratorModel.java @@ -7,9 +7,14 @@ */ package com.powsybl.dynawaltz.models.generators; +import com.powsybl.dynawaltz.models.VarConnection; + +import java.util.List; + /** * @author Dimitri Baudrier */ public interface OmegaRefGeneratorModel extends GeneratorModel { - String getOmegaRefPuVarName(); + + List getOmegaRefVarConnections(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/DefaultLineModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/DefaultLineModel.java index 0d3eb4a66..5effda998 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/DefaultLineModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/DefaultLineModel.java @@ -7,8 +7,7 @@ package com.powsybl.dynawaltz.models.lines; import com.powsybl.dynawaltz.models.AbstractNetworkModel; -import com.powsybl.dynawaltz.models.utils.LineSideUtils; -import com.powsybl.iidm.network.Branch; +import com.powsybl.dynawaltz.models.Side; /** * @author Florian Dupuy @@ -25,8 +24,8 @@ public String getName() { } @Override - public String getIVarName(Branch.Side side) { - return "@NAME@_i" + LineSideUtils.getSuffix(side); + public String getIVarName(Side side) { + return "@NAME@_i" + side.getSideSuffix(); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/LineModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/LineModel.java index b8067c6b4..8de57bac7 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/LineModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/LineModel.java @@ -7,13 +7,13 @@ package com.powsybl.dynawaltz.models.lines; import com.powsybl.dynawaltz.models.Model; -import com.powsybl.iidm.network.Branch; +import com.powsybl.dynawaltz.models.Side; /** * @author Florian Dupuy */ public interface LineModel extends Model { - String getIVarName(Branch.Side side); + String getIVarName(Side side); String getStateVarName(); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java index 3704b5d5a..5b31c7738 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java @@ -1,23 +1,30 @@ +/** + * 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.lines; import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; -import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.utils.BusUtils; -import com.powsybl.dynawaltz.models.utils.LineSideUtils; -import com.powsybl.iidm.network.Branch; +import com.powsybl.dynawaltz.models.utils.SideConverter; import com.powsybl.iidm.network.Line; -import java.util.*; +import java.util.Arrays; +import java.util.List; +/** + * @author Laurent Issertial + */ public class StandardLine extends AbstractBlackBoxModel implements LineModel { - private final Map busSideConnection = new HashMap<>(); - public StandardLine(String dynamicModelId, String staticId, String parameterSetId) { super(dynamicModelId, staticId, parameterSetId); } @@ -27,48 +34,34 @@ public String getLib() { return "Line"; } - @Override - public List getVarsMapping() { - return Collections.emptyList(); - } - - @Override - public List getVarConnectionsWith(Model connected) { - if (connected instanceof BusModel) { - BusModel busModel = (BusModel) connected; - return Arrays.asList( - new VarConnection(getIVarName(busSideConnection.get(busModel.getStaticId().orElseThrow())), busModel.getNumCCVarName()), - new VarConnection(getStateVarName(), busModel.getTerminalVarName()) - ); - } else { - throw new PowsyblException("StandardLineModel can only connect to BusModel"); - } + private List getVarConnectionsWithBus(BusModel connected, Side side) { + return Arrays.asList( + new VarConnection(getIVarName(side), connected.getNumCCVarName()), + new VarConnection(getStateVarName(), connected.getTerminalVarName()) + ); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { String staticId = getStaticId().orElse(null); Line line = context.getNetwork().getLine(staticId); if (line == null) { - throw new PowsyblException("Line static id unknown: " + staticId); + throw new PowsyblException("Line static id unknown: " + getStaticId()); } - List connectedBbm = new ArrayList<>(2); line.getTerminals().forEach(t -> { - BusModel busModel = context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(t)); - busSideConnection.put(busModel.getStaticId().orElseThrow(), line.getSide(t)); - connectedBbm.add(busModel); + String busStaticId = BusUtils.getConnectableBusStaticId(t); + createMacroConnections(busStaticId, BusModel.class, this::getVarConnectionsWithBus, context, SideConverter.convert(line.getSide(t))); }); - return connectedBbm; } @Override public String getName() { - return getLib(); + return getDynamicModelId(); } @Override - public String getIVarName(Branch.Side side) { - return getDynamicModelId() + LineSideUtils.getSuffix(side); + public String getIVarName(Side side) { + return getDynamicModelId() + side.getSideSuffix(); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/AbstractLoad.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/AbstractLoad.java index d00b3bf57..b214d9533 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/AbstractLoad.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/AbstractLoad.java @@ -9,7 +9,8 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.utils.BusUtils; import com.powsybl.iidm.network.Load; @@ -18,6 +19,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public abstract class AbstractLoad extends AbstractBlackBoxModel { @@ -26,12 +28,14 @@ protected AbstractLoad(String dynamicModelId, String staticId, String parameterS } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { String staticId = getStaticId().orElse(null); // cannot be empty as checked in constructor Load load = context.getNetwork().getLoad(staticId); if (load == null) { throw new PowsyblException("Load static id unknown: " + staticId); } - return List.of(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(load))); + createMacroConnections(BusUtils.getConnectableBusStaticId(load), BusModel.class, this::getVarConnectionsWithBus, context); } + + abstract List getVarConnectionsWithBus(BusModel connected); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadAlphaBeta.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadAlphaBeta.java index eef1bc5e1..cd137e7f8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadAlphaBeta.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadAlphaBeta.java @@ -6,8 +6,6 @@ */ package com.powsybl.dynawaltz.models.loads; -import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.BusModel; @@ -17,6 +15,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class LoadAlphaBeta extends AbstractLoad { @@ -40,14 +39,10 @@ public List getVarsMapping() { } @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof BusModel)) { - throw new PowsyblException("LoadAlphaBeta can only connect to BusModel"); - } - BusModel connectedBusModel = (BusModel) connected; + protected List getVarConnectionsWithBus(BusModel connected) { return Arrays.asList( - new VarConnection("load_terminal", connectedBusModel.getTerminalVarName()), - new VarConnection("load_switchOffSignal1", connectedBusModel.getSwitchOffSignalVarName()) + new VarConnection("load_terminal", connected.getTerminalVarName()), + new VarConnection("load_switchOffSignal1", connected.getSwitchOffSignalVarName()) ); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadOneTransformer.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadOneTransformer.java index 3a61132c5..901b72479 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadOneTransformer.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/loads/LoadOneTransformer.java @@ -6,8 +6,6 @@ */ package com.powsybl.dynawaltz.models.loads; -import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.BusModel; @@ -17,6 +15,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class LoadOneTransformer extends AbstractLoad { @@ -40,15 +39,11 @@ public List getVarsMapping() { } @Override - public List getVarConnectionsWith(Model connected) { - if (!(connected instanceof BusModel)) { - throw new PowsyblException("LoadOneTransformer can only connect to BusModel"); - } - BusModel connectedBusModel = (BusModel) connected; + protected List getVarConnectionsWithBus(BusModel connected) { return Arrays.asList( - new VarConnection("transformer_terminal", connectedBusModel.getTerminalVarName()), - new VarConnection("transformer_switchOffSignal1", connectedBusModel.getSwitchOffSignalVarName()), - new VarConnection("load_switchOffSignal1", connectedBusModel.getSwitchOffSignalVarName()) + new VarConnection("transformer_terminal", connected.getTerminalVarName()), + new VarConnection("transformer_switchOffSignal1", connected.getSwitchOffSignalVarName()), + new VarConnection("load_switchOffSignal1", connected.getSwitchOffSignalVarName()) ); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypes.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypes.java deleted file mode 100644 index f53394145..000000000 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypes.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * SPDX-License-Identifier: MPL-2.0 - */ -package com.powsybl.dynawaltz.models.utils; - -/** - * @author Florian Dupuy - * @author Dimitri Baudrier - */ -public final class ConnectedModelTypes { - - private final String modelNameA; - private final String modelNameB; - - private ConnectedModelTypes(String modelNameA, String modelNameB) { - this.modelNameA = modelNameA; - this.modelNameB = modelNameB; - } - - public static ConnectedModelTypes of(String a, String b) { - return new ConnectedModelTypes(a, b); - } - - @Override - public int hashCode() { - return modelNameA.hashCode() + modelNameB.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof ConnectedModelTypes) { - ConnectedModelTypes otherCmc = (ConnectedModelTypes) obj; - return otherCmc.modelNameA.equals(modelNameA) && otherCmc.modelNameB.equals(modelNameB) - || otherCmc.modelNameB.equals(modelNameA) && otherCmc.modelNameA.equals(modelNameB); - } - return false; - } -} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModels.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModels.java deleted file mode 100644 index bbd5e3850..000000000 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModels.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * SPDX-License-Identifier: MPL-2.0 - */ -package com.powsybl.dynawaltz.models.utils; - -import com.powsybl.dynawaltz.models.BlackBoxModel; -import com.powsybl.dynawaltz.models.Model; - -/** - * Pair of models connected. One of them is necessarily a BlackBoxModel. - * This class is used to consider only once this connection by overriding equals. - * - * @author Florian Dupuy - * @author Dimitri Baudrier - */ -public final class ConnectedModels { - - private final BlackBoxModel blackBoxModel; - private final Model model; - - private ConnectedModels(BlackBoxModel blackBoxModel, Model model) { - this.blackBoxModel = blackBoxModel; - this.model = model; - } - - public static ConnectedModels of(BlackBoxModel bbm, Model m) { - return new ConnectedModels(bbm, m); - } - - public BlackBoxModel getBlackBoxModel() { - return blackBoxModel; - } - - public Model getModel() { - return model; - } - - @Override - public int hashCode() { - return blackBoxModel.hashCode() + model.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof ConnectedModels) { - ConnectedModels otherCm = (ConnectedModels) obj; - return otherCm.blackBoxModel.equals(blackBoxModel) && otherCm.model.equals(model) - || otherCm.blackBoxModel.equals(model) && otherCm.model.equals(blackBoxModel); - } - return false; - } -} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/SideConverter.java similarity index 74% rename from dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java rename to dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/SideConverter.java index d771d7af4..00483983b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/SideConverter.java @@ -7,22 +7,23 @@ */ package com.powsybl.dynawaltz.models.utils; +import com.powsybl.dynawaltz.models.Side; import com.powsybl.iidm.network.Branch; /** * @author Laurent Issertial */ -public final class LineSideUtils { +public final class SideConverter { - private LineSideUtils() { + private SideConverter() { } - public static String getSuffix(Branch.Side side) { + public static Side convert(Branch.Side side) { switch (side) { case ONE: - return "Side1"; + return Side.ONE; case TWO: - return "Side2"; + return Side.TWO; default: throw new AssertionError("Unexpected Side value: " + side); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/DydXml.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/DydXml.java index 4332176c4..31a91648d 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/DydXml.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/DydXml.java @@ -9,16 +9,14 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.MacroConnect; import com.powsybl.dynawaltz.models.MacroConnector; -import com.powsybl.dynawaltz.models.Model; -import com.powsybl.dynawaltz.models.utils.ConnectedModels; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.io.IOException; import java.nio.file.Path; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Objects; import static com.powsybl.dynawaltz.xml.DynaWaltzConstants.DYD_FILENAME; @@ -53,14 +51,8 @@ private static void writeDynamicModels(XMLStreamWriter writer, DynaWaltzContext for (MacroStaticReference macroStaticReference : context.getMacroStaticReferences()) { macroStaticReference.write(writer); } - Set allConnectedModels = context.getModelsConnections().entrySet().stream() - .flatMap(e -> e.getValue().stream().map(m -> ConnectedModels.of(e.getKey(), m))) - .collect(Collectors.toCollection(LinkedHashSet::new)); - for (ConnectedModels modelsConnected : allConnectedModels) { - BlackBoxModel bbm = modelsConnected.getBlackBoxModel(); - Model connected = modelsConnected.getModel(); - MacroConnector macroConnector = context.getMacroConnector(bbm, connected); - bbm.writeMacroConnect(writer, context, macroConnector, connected); + for (MacroConnect macroConnect : context.getMacroConnectList()) { + macroConnect.write(writer); } } @@ -71,11 +63,8 @@ private static void writeEvents(XMLStreamWriter writer, DynaWaltzContext context for (MacroConnector macroConnector : context.getEventMacroConnectors()) { macroConnector.write(writer); } - for (Map.Entry> bbmMapping : context.getEventModelsConnections().entrySet()) { - BlackBoxModel event = bbmMapping.getKey(); - for (Model connected : bbmMapping.getValue()) { - event.writeMacroConnect(writer, context, context.getEventMacroConnector(event, connected), connected); - } + for (MacroConnect macroConnect : context.getEventMacroConnectList()) { + macroConnect.write(writer); } } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java deleted file mode 100644 index f393b08a2..000000000 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * SPDX-License-Identifier: MPL-2.0 - */ -package com.powsybl.dynawaltz.models; - -import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.models.buses.BusModel; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Collections; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; - -/** - * @author Dimitri Baudrier - */ -class OmegaRefTest { - - private OmegaRef omegaRef; - - @BeforeEach - void setUp() { - omegaRef = new OmegaRef(Collections.emptyList()); - } - - @Test - void getVarConnectionsWithException() { - Model model = mock(Model.class); - PowsyblException e = assertThrows(PowsyblException.class, () -> omegaRef.getVarConnectionsWith(model)); - assertEquals("OmegaRef can only connect to GeneratorModel and BusModel", e.getMessage()); - } - - @Test - void getVarConnectionsWithVarConnectionBus() { - BusModel busModel = mock(BusModel.class); - List varConnectionList = omegaRef.getVarConnectionsWith(busModel); - assertNotNull(varConnectionList); - assertEquals(1, varConnectionList.size()); - VarConnection varConnection = varConnectionList.get(0); - assertEquals("numcc_node_@INDEX@", varConnection.getVar1()); - assertEquals(busModel.getNumCCVarName(), varConnection.getVar2()); - } - - @Test - void writeMacroConnectException() { - Model model = mock(Model.class); - PowsyblException e = assertThrows(PowsyblException.class, () -> omegaRef.writeMacroConnect(null, null, null, model)); - assertEquals("OmegaRef can only connect to OmegaRefGeneratorModel and BusModel", e.getMessage()); - } -} diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/buses/StandardBusTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/buses/StandardBusTest.java index 36eff087b..f1d73045b 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/buses/StandardBusTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/buses/StandardBusTest.java @@ -9,23 +9,30 @@ package com.powsybl.dynawaltz.models.buses; import com.powsybl.commons.PowsyblException; +import com.powsybl.dynamicsimulation.Curve; +import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.Model; -import com.powsybl.dynawaltz.models.VarConnection; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; +import com.powsybl.dynawaltz.DynaWaltzParameters; +import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.NetworkFactory; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author Dimitri Baudrier + * @author Laurent Issertial */ class StandardBusTest { @@ -36,29 +43,6 @@ void setUp() { standardBus = new StandardBus("dynamicModelId", "staticId", "parameterSetId"); } - @Test - void getVarConnectionsWithException() { - Model model = mock(Model.class); - PowsyblException e = assertThrows(PowsyblException.class, () -> standardBus.getVarConnectionsWith(model)); - assertEquals("StandardBusModel can only connect to GeneratorModel", e.getMessage()); - } - - @Test - void getVarConnectionsWithGetValues() { - GeneratorModel generatorModel = mock(GeneratorModel.class); - List varConnectionList = standardBus.getVarConnectionsWith(generatorModel); - assertNotNull(varConnectionList); - assertEquals(2, varConnectionList.size()); - - VarConnection firstVarConnection = varConnectionList.get(0); - assertEquals(firstVarConnection.getVar1(), standardBus.getTerminalVarName()); - assertEquals(firstVarConnection.getVar2(), generatorModel.getTerminalVarName()); - - VarConnection secondVarConnection = varConnectionList.get(1); - assertEquals(secondVarConnection.getVar1(), standardBus.getSwitchOffSignalVarName()); - assertEquals(secondVarConnection.getVar2(), generatorModel.getSwitchOffSignalNodeVarName()); - } - @Test void getModelsConnectedToException() { DynaWaltzContext dynaWaltzContext = mock(DynaWaltzContext.class); @@ -68,7 +52,25 @@ void getModelsConnectedToException() { when(network.getBusBreakerView()).thenReturn(busBreakerView); when(busBreakerView.getBus(anyString())).thenReturn(null); - PowsyblException e = assertThrows(PowsyblException.class, () -> standardBus.getModelsConnectedTo(dynaWaltzContext)); + PowsyblException e = assertThrows(PowsyblException.class, () -> standardBus.createMacroConnections(dynaWaltzContext)); assertEquals("Bus static id unknown: staticId", e.getMessage()); } + + @Test + void connectionToModelWithoutDynamicModelException() { + Network network = EurostagTutorialExample1Factory.create(NetworkFactory.findDefault()); + List dynamicModels = new ArrayList<>(); + network.getBusBreakerView().getBuses().forEach(b -> { + if (b.getId().equals("NHV1")) { + dynamicModels.add(new StandardBus("BBM_" + b.getId(), b.getId(), "SB")); + } + }); + DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); + DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load(); + String workingVariantId = network.getVariantManager().getWorkingVariantId(); + List events = Collections.emptyList(); + List curves = Collections.emptyList(); + PowsyblException e = assertThrows(PowsyblException.class, () -> new DynaWaltzContext(network, workingVariantId, dynamicModels, events, curves, parameters, dynawoParameters)); + assertEquals("The line NHV1_NHV2_1 linked to the standard bus NHV1 does not possess a dynamic model", e.getMessage()); + } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypesTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypesTest.java deleted file mode 100644 index 80cf765f4..000000000 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypesTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * SPDX-License-Identifier: MPL-2.0 - */ - -package com.powsybl.dynawaltz.models.utils; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; - -/** - * @author Dimitri Baudrier - */ -class ConnectedModelTypesTest { - - @Test - void twoConnectedModelTypesEqual() { - ConnectedModelTypes connectedModelTypes1 = ConnectedModelTypes.of("a", "b"); - ConnectedModelTypes connectedModelTypes2 = ConnectedModelTypes.of("b", "a"); - - assertEquals(connectedModelTypes1, connectedModelTypes2); - } - - @Test - void twoConnectedModelTypesNotEqual() { - ConnectedModelTypes connectedModelTypes1 = ConnectedModelTypes.of("a", "b"); - ConnectedModelTypes connectedModelTypes2 = ConnectedModelTypes.of("a", "c"); - - assertNotEquals(connectedModelTypes1, connectedModelTypes2); - } -} diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelsTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelsTest.java deleted file mode 100644 index d5dc00963..000000000 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * SPDX-License-Identifier: MPL-2.0 - */ - -package com.powsybl.dynawaltz.models.utils; - -import com.powsybl.dynawaltz.models.BlackBoxModel; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.mockito.Mockito.mock; - -/** - * @author Dimitri Baudrier - */ -class ConnectedModelsTest { - - @Test - void twoConnectedModelsEqual() { - BlackBoxModel bbm1 = mock(BlackBoxModel.class); - BlackBoxModel bbm2 = mock(BlackBoxModel.class); - - ConnectedModels connectedModels1 = ConnectedModels.of(bbm1, bbm2); - ConnectedModels connectedModels2 = ConnectedModels.of(bbm2, bbm1); - - assertEquals(connectedModels1, connectedModels2); - } - - @Test - void twoConnectedModelsNotEqual() { - ConnectedModels connectedModels1 = ConnectedModels.of(mock(BlackBoxModel.class), mock(BlackBoxModel.class)); - ConnectedModels connectedModels2 = ConnectedModels.of(mock(BlackBoxModel.class), mock(BlackBoxModel.class)); - - assertNotEquals(connectedModels1, connectedModels2); - } -} diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java index ae26b8085..19181ce3b 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java @@ -10,17 +10,21 @@ import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynawaltz.DynaWaltzCurve; import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton; import com.powsybl.dynawaltz.models.buses.StandardBus; import com.powsybl.dynawaltz.models.events.EventQuadripoleDisconnection; import com.powsybl.dynawaltz.models.events.EventSetPointBoolean; -import com.powsybl.dynawaltz.models.generators.OmegaRefGenerator; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; import com.powsybl.dynawaltz.models.generators.GeneratorSynchronous; +import com.powsybl.dynawaltz.models.generators.OmegaRefGenerator; import com.powsybl.dynawaltz.models.lines.StandardLine; import com.powsybl.dynawaltz.models.loads.LoadAlphaBeta; import com.powsybl.dynawaltz.models.loads.LoadOneTransformer; -import com.powsybl.iidm.network.*; +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.NetworkFactory; +import com.powsybl.iidm.network.VoltageLevel; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import org.junit.jupiter.api.BeforeEach; import org.xml.sax.SAXException; @@ -36,8 +40,9 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Objects; -import static com.powsybl.commons.test.ComparisonUtils.compareXml; +import static com.powsybl.commons.test.ComparisonUtils.compareTxt; /** * @author Marcos de Miguel @@ -95,11 +100,9 @@ void setup() { dynamicModels.add(new StandardBus("BBM_" + b.getId(), b.getId(), "SB")); } }); - network.getLineStream().forEach(l -> { - if (l.getId().equals("NHV1_NHV2_1")) { - dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL")); - } - }); + network.getLineStream().forEach(l -> + dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL")) + ); // Events eventModels = new ArrayList<>(); @@ -111,7 +114,7 @@ void setup() { }); // Automatons - network.getLineStream().forEach(l -> dynamicModels.add(new CurrentLimitAutomaton("BBM_" + l.getId(), l.getId(), "CLA", Branch.Side.ONE))); + network.getLineStream().forEach(l -> dynamicModels.add(new CurrentLimitAutomaton("BBM_" + l.getId(), l.getId(), "CLA", Side.ONE))); } public void validate(String schemaDefinition, String expectedResourceName, Path xmlFile) throws SAXException, IOException { @@ -121,7 +124,7 @@ public void validate(String schemaDefinition, String expectedResourceName, Path Schema schema = factory.newSchema(xsd); Validator validator = schema.newValidator(); validator.validate(xml); - compareXml(getClass().getResourceAsStream("/" + expectedResourceName), Files.newInputStream(xmlFile)); + compareTxt(Objects.requireNonNull(getClass().getResourceAsStream("/" + expectedResourceName)), Files.newInputStream(xmlFile)); } private static Network createEurostagTutorialExample1WithMoreLoads() { diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java index 60c93d8b2..c90ec2afa 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java @@ -11,6 +11,8 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; +import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; +import com.powsybl.dynawaltz.models.lines.LineModel; import org.junit.jupiter.api.Test; import org.xml.sax.SAXException; @@ -23,6 +25,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ class DynamicModelsXmlTest extends DynaWaltzTestUtil { @@ -70,16 +73,12 @@ void duplicateStaticId() { void testDynamicModelGetterException() { DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); - // dynamic model - Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModelOrThrows("wrongID")); - assertEquals("Cannot find the equipment 'wrongID' among the dynamic models provided", e.getMessage()); + // incorrect model + Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("GEN5", LineModel.class)); + assertEquals("The model identified by the static id GEN5 is not the correct model", e.getMessage()); - // bus - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModelOrDefaultBus("GEN5")); - assertEquals("The model identified by the static id GEN5 is not a bus model", e.getMessage()); - - //line - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModelOrDefaultLine("GEN5")); - assertEquals("The model identified by the static id GEN5 is not a line model", e.getMessage()); + // default model not implemented + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("unknownID", GeneratorSynchronousModel.class)); + assertEquals("Default model not implemented for GeneratorSynchronousModel", e.getMessage()); } } diff --git a/dynawaltz/src/test/resources/curvesInput.xml b/dynawaltz/src/test/resources/curvesInput.xml index 9ff5b9e7d..2a58084ae 100644 --- a/dynawaltz/src/test/resources/curvesInput.xml +++ b/dynawaltz/src/test/resources/curvesInput.xml @@ -1,49 +1,47 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index f8374f4e8..b5b045ca4 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -33,6 +33,7 @@ + @@ -73,25 +74,40 @@ - + - + + + + + + + + + + + + + - - - - + + + + + + + @@ -111,9 +127,6 @@ - - - @@ -167,22 +180,24 @@ - - - - + + + + + + - - - - - - + + + + + +