From 8039939634c816c9812e561e4009b0d0361e7fd6 Mon Sep 17 00:00:00 2001 From: lisrte Date: Wed, 22 Feb 2023 14:04:19 +0100 Subject: [PATCH 01/21] Refactor getModelsConnectedTo DynaWaltzContext returns directly the dynamicModel linked to the static id or a default model/exception if not found depending on the method called Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 31 +++++++++++++++---- .../powsybl/dynawaltz/models/OmegaRef.java | 11 ++----- .../automatons/CurrentLimitAutomaton.java | 12 +------ .../dynawaltz/models/buses/StandardBus.java | 14 +++------ .../events/EventQuadripoleDisconnection.java | 7 +---- .../models/events/EventSetPointBoolean.java | 7 +---- .../generators/AbstractGeneratorModel.java | 12 +++---- .../dynawaltz/models/lines/StandardLine.java | 19 +++++------- .../dynawaltz/models/loads/AbstractLoad.java | 8 +---- 9 files changed, 48 insertions(+), 73 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index d3302c725..34992fd7b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -14,7 +14,9 @@ import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; import com.powsybl.dynawaltz.models.utils.ConnectedModelTypes; import com.powsybl.dynawaltz.xml.MacroStaticReference; +import com.powsybl.iidm.network.Branch; import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.Terminal; import java.nio.file.FileSystem; import java.nio.file.Path; @@ -121,8 +123,29 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public BlackBoxModel getStaticIdBlackBoxModel(String staticId) { - return staticIdBlackBoxModelMap.get(staticId); + 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 Model getDynamicModelOrDefaultLine(String staticId, Branch.Side side) { + BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); + if (bbm == null) { + return networkModel.getDefaultLineModel(staticId, side); + } + return bbm; + } + + public Model getDynamicModelOrDefaultBus(Terminal terminal) { + String staticId = terminal.getBusBreakerView().getConnectableBus().getId(); + BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); + if (bbm == null) { + return networkModel.getDefaultBusModel(staticId); + } + return bbm; } private BlackBoxModel mergeDuplicateStaticId(BlackBoxModel bbm1, BlackBoxModel bbm2) { @@ -219,10 +242,6 @@ private static DynaWaltzParametersDatabase loadDatabase(String filename, Platfor return DynaWaltzParametersDatabase.load(fs.getPath(filename)); } - public NetworkModel getNetworkModel() { - return networkModel; - } - public String getParFile() { return Paths.get(getDynaWaltzParameters().getParametersFile()).getFileName().toString(); } 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 56965f080..311c6f7d4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -27,7 +27,7 @@ /** * OmegaRef is a special model: its role is to synchronize the generators' frequency. The corresponding black - * box model XML entry is serialized only once. For each generator synchronised through the OmegaRef model, there will be + * box model XML entry is serialized only once. For each generator synchronized through the OmegaRef model, there will be * one XML entry for the connection with the generator's dynamic model, and one XML entry for the connection with the * NETWORK dynamic model. There are thus two macroConnectors defined for OmegaRef: one to connect it to a generator's * dynamic model and one to connect it to the NETWORK model. @@ -65,7 +65,7 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th writer.writeStartElement(DYN_URI, "set"); writer.writeAttribute("id", getParameterSetId()); - // The dynamic models are declared in the DYD following the order of dynamic models supplier. + // 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()) { GeneratorSynchronousModel generator = e.getKey(); @@ -111,12 +111,7 @@ private BusModel getBusAssociatedTo(GeneratorSynchronousModel generatorModel, Dy if (generator == null) { throw new PowsyblException("Generator " + generatorModel.getLib() + " not found in DynaWaltz context. Id : " + generatorModel.getDynamicModelId()); } - String connectedStaticId = generator.getTerminal().getBusBreakerView().getConnectableBus().getId(); - BusModel busModel = (BusModel) context.getStaticIdBlackBoxModel(connectedStaticId); - if (busModel == null) { - busModel = context.getNetworkModel().getDefaultBusModel(connectedStaticId); - } - return busModel; + return (BusModel) context.getDynamicModelOrDefaultBus(generator.getTerminal()); } @Override 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 76e490ac0..75fc2cf46 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 @@ -9,12 +9,10 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; -import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.iidm.network.Branch; -import com.powsybl.iidm.network.Line; import java.util.Arrays; import java.util.List; @@ -41,15 +39,7 @@ public String getLib() { @Override public List getModelsConnectedTo(DynaWaltzContext context) { - Line line = context.getNetwork().getLine(lineStaticId); - if (line == null) { - throw new PowsyblException("Unknown line static id: " + lineStaticId); - } - BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModel(line.getId()); - if (connectedBbm == null) { - return List.of(context.getNetworkModel().getDefaultLineModel(lineStaticId, side)); - } - return List.of(connectedBbm); + return List.of(context.getDynamicModelOrDefaultLine(lineStaticId, side)); } @Override 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 c39f78a0d..d6d0aabdb 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 @@ -15,7 +15,6 @@ import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.generators.GeneratorModel; import com.powsybl.iidm.network.Bus; -import com.powsybl.iidm.network.Generator; import org.apache.commons.lang3.tuple.Pair; import javax.xml.stream.XMLStreamException; @@ -24,6 +23,7 @@ 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; @@ -75,18 +75,12 @@ public List getVarConnectionsWith(Model connected) { } @Override - public List getModelsConnectedTo(DynaWaltzContext dynaWaltzContext) { - Bus bus = dynaWaltzContext.getNetwork().getBusBreakerView().getBus(getStaticId().orElse(null)); + public List getModelsConnectedTo(DynaWaltzContext context) { + Bus bus = context.getNetwork().getBusBreakerView().getBus(getStaticId().orElse(null)); if (bus == null) { throw new PowsyblException("Bus static id unknown: " + getStaticId()); } - List connectedBbm = new ArrayList<>(); - for (Generator g : dynaWaltzContext.getNetwork().getGenerators()) { - if (g.getTerminal().getBusBreakerView().getConnectableBus().equals(bus)) { - connectedBbm.add(dynaWaltzContext.getStaticIdBlackBoxModel(g.getId())); - } - } - return connectedBbm; + return bus.getGeneratorStream().map(g -> context.getDynamicModelOrThrows(g.getId())).collect(Collectors.toList()); } @Override 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 f2fe96b2e..e94e59c24 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 @@ -8,7 +8,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; @@ -51,11 +50,7 @@ public List getVarConnectionsWith(Model connected) { @Override public List getModelsConnectedTo(DynaWaltzContext context) { - BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModel(getEquipmentStaticId()); - if (connectedBbm == null) { - return List.of(context.getNetworkModel().getDefaultLineModel(getEquipmentStaticId(), Branch.Side.ONE)); - } - return List.of(connectedBbm); + return List.of(context.getDynamicModelOrDefaultLine(getEquipmentStaticId(), Branch.Side.ONE)); } @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 8429f94b9..932b365df 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 @@ -8,7 +8,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.generators.GeneratorModel; @@ -47,11 +46,7 @@ public List getVarConnectionsWith(Model connected) { @Override public List getModelsConnectedTo(DynaWaltzContext context) { - BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModel(getEquipmentStaticId()); - if (connectedBbm == null) { - throw new PowsyblException("Cannot find generator '" + getEquipmentStaticId() + "' among the dynamic models provided"); - } - return List.of(connectedBbm); + return List.of(context.getDynamicModelOrThrows(getEquipmentStaticId())); } @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 945a51db6..26621a6df 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 @@ -8,7 +8,10 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.*; +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; import com.powsybl.iidm.network.Generator; @@ -56,12 +59,7 @@ public List getModelsConnectedTo(DynaWaltzContext context) { if (generator == null) { throw new PowsyblException("Generator static id unknown: " + staticId); } - String connectedStaticId = generator.getTerminal().getBusBreakerView().getConnectableBus().getId(); - BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModel(connectedStaticId); - if (connectedBbm == null) { - return List.of(context.getNetworkModel().getDefaultBusModel(connectedStaticId)); - } - return List.of(connectedBbm); + return List.of(context.getDynamicModelOrDefaultBus(generator.getTerminal())); } @Override 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 7e2e05e48..775afc2c3 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 @@ -8,7 +8,6 @@ import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.iidm.network.Branch; -import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Line; import java.util.ArrayList; @@ -20,8 +19,8 @@ public class StandardLine extends AbstractBlackBoxModel implements LineModel { private final String sidePostfix; - public StandardLine(String dynamicModelId, String statidId, String parameterSetId, Branch.Side side) { - super(dynamicModelId, statidId, parameterSetId); + public StandardLine(String dynamicModelId, String staticId, String parameterSetId, Branch.Side side) { + super(dynamicModelId, staticId, parameterSetId); this.sidePostfix = LineModel.getSuffix(side); } @@ -49,17 +48,13 @@ public List getVarConnectionsWith(Model connected) { } @Override - public List getModelsConnectedTo(DynaWaltzContext dynaWaltzContext) { - Line line = dynaWaltzContext.getNetwork().getLine(getStaticId().orElse(null)); + public List getModelsConnectedTo(DynaWaltzContext context) { + Line line = context.getNetwork().getLine(getStaticId().orElse(null)); if (line == null) { - throw new PowsyblException("Line static id unkwown: " + getStaticId()); - } - List connectedBbm = new ArrayList<>(); - for (Bus b : dynaWaltzContext.getNetwork().getBusBreakerView().getBuses()) { - if (b.getLineStream().anyMatch(l -> l.equals(line))) { - connectedBbm.add(dynaWaltzContext.getStaticIdBlackBoxModel(b.getId())); - } + throw new PowsyblException("Line static id unknown: " + getStaticId()); } + List connectedBbm = new ArrayList<>(2); + line.getTerminals().forEach(t -> connectedBbm.add(context.getDynamicModelOrDefaultBus(t))); return connectedBbm; } 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 d1fbfff74..45d4866b2 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,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.dynawaltz.models.Model; import com.powsybl.iidm.network.Load; @@ -32,11 +31,6 @@ public List getModelsConnectedTo(DynaWaltzContext context) { if (load == null) { throw new PowsyblException("Load static id unknown: " + staticId); } - String connectedStaticId = load.getTerminal().getBusBreakerView().getConnectableBus().getId(); - BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModel(connectedStaticId); - if (connectedBbm == null) { - return List.of(context.getNetworkModel().getDefaultBusModel(connectedStaticId)); - } - return List.of(connectedBbm); + return List.of(context.getDynamicModelOrDefaultBus(load.getTerminal())); } } From e3d0fe79b848e86c7049572b2b3ef142547ba456 Mon Sep 17 00:00:00 2001 From: lisrte Date: Wed, 22 Feb 2023 16:53:39 +0100 Subject: [PATCH 02/21] Add version of getDynamicModelOrDefaultBus Signed-off-by: lisrte --- .../main/java/com/powsybl/dynawaltz/DynaWaltzContext.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 34992fd7b..3311fba06 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -140,7 +140,10 @@ public Model getDynamicModelOrDefaultLine(String staticId, Branch.Side side) { } public Model getDynamicModelOrDefaultBus(Terminal terminal) { - String staticId = terminal.getBusBreakerView().getConnectableBus().getId(); + return getDynamicModelOrDefaultBus(terminal.getBusBreakerView().getConnectableBus().getId()); + } + + public Model getDynamicModelOrDefaultBus(String staticId) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { return networkModel.getDefaultBusModel(staticId); From c149d61f74a6911c29b9b031622b827e70c04602 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 24 Feb 2023 10:04:09 +0100 Subject: [PATCH 03/21] Change getDynamicModelOrDefaultXXX methods return values after review Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 18 +++++++++++++----- .../com/powsybl/dynawaltz/models/OmegaRef.java | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 3311fba06..6c579ac2a 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,7 +11,9 @@ 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.GeneratorSynchronousModel; +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.Branch; @@ -131,24 +133,30 @@ public Model getDynamicModelOrThrows(String staticId) { return bbm; } - public Model getDynamicModelOrDefaultLine(String staticId, Branch.Side side) { + public LineModel getDynamicModelOrDefaultLine(String staticId, Branch.Side side) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { return networkModel.getDefaultLineModel(staticId, side); } - return bbm; + if (bbm instanceof LineModel) { + return (LineModel) bbm; + } + throw new PowsyblException("The model identified by the static id " + staticId + " is not a line model"); } - public Model getDynamicModelOrDefaultBus(Terminal terminal) { + public BusModel getDynamicModelOrDefaultBus(Terminal terminal) { return getDynamicModelOrDefaultBus(terminal.getBusBreakerView().getConnectableBus().getId()); } - public Model getDynamicModelOrDefaultBus(String staticId) { + public BusModel getDynamicModelOrDefaultBus(String staticId) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { return networkModel.getDefaultBusModel(staticId); } - return bbm; + if (bbm instanceof BusModel) { + return (BusModel) bbm; + } + throw new PowsyblException("The model identified by the static id " + staticId + " is not a bus model"); } private BlackBoxModel mergeDuplicateStaticId(BlackBoxModel bbm1, BlackBoxModel bbm2) { 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 311c6f7d4..c1994cb6d 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -111,7 +111,7 @@ private BusModel getBusAssociatedTo(GeneratorSynchronousModel generatorModel, Dy if (generator == null) { throw new PowsyblException("Generator " + generatorModel.getLib() + " not found in DynaWaltz context. Id : " + generatorModel.getDynamicModelId()); } - return (BusModel) context.getDynamicModelOrDefaultBus(generator.getTerminal()); + return context.getDynamicModelOrDefaultBus(generator.getTerminal()); } @Override From 4832d0d309bc0f0ccb1794fe9dad11a1d0708550 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 27 Feb 2023 11:33:10 +0100 Subject: [PATCH 04/21] Add UT Signed-off-by: lisrte --- .../dynawaltz/xml/DynamicModelsXmlTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 0dbae84b5..178cd75c3 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,7 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; +import com.powsybl.iidm.network.Branch; import org.junit.Test; import org.xml.sax.SAXException; @@ -66,4 +67,20 @@ public void duplicateStaticId() { assertEquals("Duplicate staticId: duplicateID", e.getMessage()); } + @Test + public 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()); + + // 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", Branch.Side.ONE)); + assertEquals("The model identified by the static id GEN5 is not a line model", e.getMessage()); + } } From adb3af6c38dc298b813fc3878546f12a13a255f4 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 28 Feb 2023 10:23:51 +0100 Subject: [PATCH 05/21] Add BusUtils for searching connectable bus static id Signed-off-by: lisrte --- .../powsybl/dynawaltz/models/OmegaRef.java | 3 ++- .../generators/AbstractGeneratorModel.java | 3 ++- .../dynawaltz/models/lines/StandardLine.java | 3 ++- .../dynawaltz/models/loads/AbstractLoad.java | 3 ++- .../dynawaltz/models/utils/BusUtils.java | 23 +++++++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java 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 c1994cb6d..5998dbd11 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -11,6 +11,7 @@ import com.powsybl.dynawaltz.DynaWaltzParametersDatabase; import com.powsybl.dynawaltz.models.buses.BusModel; 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; @@ -111,7 +112,7 @@ private BusModel getBusAssociatedTo(GeneratorSynchronousModel generatorModel, Dy if (generator == null) { throw new PowsyblException("Generator " + generatorModel.getLib() + " not found in DynaWaltz context. Id : " + generatorModel.getDynamicModelId()); } - return context.getDynamicModelOrDefaultBus(generator.getTerminal()); + return context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(generator)); } @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 26621a6df..773110ac2 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 @@ -13,6 +13,7 @@ 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.iidm.network.Generator; import java.util.Arrays; @@ -59,7 +60,7 @@ public List getModelsConnectedTo(DynaWaltzContext context) { if (generator == null) { throw new PowsyblException("Generator static id unknown: " + staticId); } - return List.of(context.getDynamicModelOrDefaultBus(generator.getTerminal())); + return List.of(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(generator))); } @Override 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 3a670a3cc..045a74119 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 @@ -7,6 +7,7 @@ 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.iidm.network.Branch; import com.powsybl.iidm.network.Line; @@ -55,7 +56,7 @@ public List getModelsConnectedTo(DynaWaltzContext context) { throw new PowsyblException("Line static id unknown: " + getStaticId()); } List connectedBbm = new ArrayList<>(2); - line.getTerminals().forEach(t -> connectedBbm.add(context.getDynamicModelOrDefaultBus(t))); + line.getTerminals().forEach(t -> connectedBbm.add(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(t)))); return connectedBbm; } 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 45d4866b2..d00b3bf57 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 @@ -10,6 +10,7 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.utils.BusUtils; import com.powsybl.iidm.network.Load; import java.util.List; @@ -31,6 +32,6 @@ public List getModelsConnectedTo(DynaWaltzContext context) { if (load == null) { throw new PowsyblException("Load static id unknown: " + staticId); } - return List.of(context.getDynamicModelOrDefaultBus(load.getTerminal())); + return List.of(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(load))); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java new file mode 100644 index 000000000..a2d5238a4 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java @@ -0,0 +1,23 @@ +package com.powsybl.dynawaltz.models.utils; + +import com.powsybl.iidm.network.Generator; +import com.powsybl.iidm.network.Load; +import com.powsybl.iidm.network.Terminal; + +public final class BusUtils { + + private BusUtils() { + } + + public static String getConnectableBusStaticId(Terminal terminal) { + return terminal.getBusBreakerView().getConnectableBus().getId(); + } + + public static String getConnectableBusStaticId(Generator generator) { + return getConnectableBusStaticId(generator.getTerminal()); + } + + public static String getConnectableBusStaticId(Load load) { + return getConnectableBusStaticId(load.getTerminal()); + } +} From bf84373033a6bb51ec37e50565cfa10619f398c6 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 28 Feb 2023 10:27:52 +0100 Subject: [PATCH 06/21] Fix UT Signed-off-by: lisrte --- .../java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a93eb92f3..b10cac00d 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java @@ -68,7 +68,7 @@ void duplicateStaticId() { } @Test - public void testDynamicModelGetterException() { + void testDynamicModelGetterException() { DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); // dynamic model From e378bf17c854e7b07a2bf46b73b9a9510e5cbd4c Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 28 Feb 2023 14:30:20 +0100 Subject: [PATCH 07/21] Remove unused method Signed-off-by: lisrte --- .../main/java/com/powsybl/dynawaltz/DynaWaltzContext.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 6c579ac2a..d99b936b8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -18,7 +18,6 @@ import com.powsybl.dynawaltz.xml.MacroStaticReference; import com.powsybl.iidm.network.Branch; import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.Terminal; import java.nio.file.FileSystem; import java.nio.file.Path; @@ -144,10 +143,6 @@ public LineModel getDynamicModelOrDefaultLine(String staticId, Branch.Side side) throw new PowsyblException("The model identified by the static id " + staticId + " is not a line model"); } - public BusModel getDynamicModelOrDefaultBus(Terminal terminal) { - return getDynamicModelOrDefaultBus(terminal.getBusBreakerView().getConnectableBus().getId()); - } - public BusModel getDynamicModelOrDefaultBus(String staticId) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { From 76c485bd01e22c6ccec2bd7886c61d7172c89be7 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 28 Feb 2023 14:41:15 +0100 Subject: [PATCH 08/21] Remove side attribute in Line Model Add side suffix value in CurrentLimitAutomaton name Signed-off-by: lisrte --- .../dynawo/commons/loadmerge/LoadsMerger.java | 2 +- .../models/lines/LineGroovyExtension.groovy | 13 +------- .../src/test/resources/dynamicModels.groovy | 2 -- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 10 +++---- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 4 +-- .../powsybl/dynawaltz/DynaWaltzContext.java | 5 ++-- .../dynawaltz/models/NetworkModel.java | 8 ++--- .../automatons/CurrentLimitAutomaton.java | 10 +++++-- .../events/EventQuadripoleDisconnection.java | 3 +- .../models/lines/DefaultLineModel.java | 11 ++++--- .../dynawaltz/models/lines/LineModel.java | 13 +------- .../dynawaltz/models/lines/StandardLine.java | 23 +++++++------- .../dynawaltz/models/utils/LineSideUtils.java | 30 +++++++++++++++++++ .../dynawaltz/xml/DynaWaltzTestUtil.java | 2 +- .../dynawaltz/xml/DynamicModelsXmlTest.java | 3 +- dynawaltz/src/test/resources/dyd.xml | 8 ++--- dynawaltz/src/test/resources/events.xml | 6 ++-- 17 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java diff --git a/commons/src/main/java/com/powsybl/dynawo/commons/loadmerge/LoadsMerger.java b/commons/src/main/java/com/powsybl/dynawo/commons/loadmerge/LoadsMerger.java index bae3a3b01..0f2885b47 100644 --- a/commons/src/main/java/com/powsybl/dynawo/commons/loadmerge/LoadsMerger.java +++ b/commons/src/main/java/com/powsybl/dynawo/commons/loadmerge/LoadsMerger.java @@ -87,7 +87,7 @@ public static List getLoadsToMergeList(Bus bus) { public static Map> getLoadPowersGrouping(Bus bus) { EnumMap> loadsGrouping = new EnumMap<>(LoadPowers.class); for (Load load : bus.getLoads()) { - loadsGrouping.computeIfAbsent(getLoadPowers(load), k -> new ArrayList<>()).add(load); + loadsGrouping.computeIfAbsent(getLoadPowers(load), k -> new ArrayList<>()).add(load); } return loadsGrouping; } diff --git a/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/models/lines/LineGroovyExtension.groovy b/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/models/lines/LineGroovyExtension.groovy index e6cb863f8..e197d0de7 100644 --- a/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/models/lines/LineGroovyExtension.groovy +++ b/dynawaltz-dsl/src/main/groovy/com/powsybl/dynawaltz/dsl/models/lines/LineGroovyExtension.groovy @@ -13,7 +13,6 @@ import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension import com.powsybl.dynawaltz.dsl.AbstractDynamicModelBuilder import com.powsybl.dynawaltz.dsl.AbstractPowsyblDynawoGroovyExtension import com.powsybl.dynawaltz.models.lines.StandardLine -import com.powsybl.iidm.network.Branch /** * An implementation of {@link DynamicModelGroovyExtension} that adds the
Line
keyword to the DSL @@ -34,20 +33,10 @@ class LineGroovyExtension extends AbstractPowsyblDynawoGroovyExtension - + @@ -112,8 +112,8 @@ - - + + @@ -125,8 +125,8 @@ - + - + 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 27188b70f..ac452116d 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 @@ -116,8 +116,8 @@ - + - + diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index d99b936b8..2cb9448ea 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -16,7 +16,6 @@ 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.Branch; import com.powsybl.iidm.network.Network; import java.nio.file.FileSystem; @@ -132,10 +131,10 @@ public Model getDynamicModelOrThrows(String staticId) { return bbm; } - public LineModel getDynamicModelOrDefaultLine(String staticId, Branch.Side side) { + public LineModel getDynamicModelOrDefaultLine(String staticId) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { - return networkModel.getDefaultLineModel(staticId, side); + return networkModel.getDefaultLineModel(staticId); } if (bbm instanceof LineModel) { return (LineModel) bbm; 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 1c3ab00cf..3144bdf00 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/NetworkModel.java @@ -8,11 +8,9 @@ import com.powsybl.dynawaltz.models.buses.DefaultBusModel; import com.powsybl.dynawaltz.models.lines.DefaultLineModel; -import com.powsybl.iidm.network.Branch; import java.util.HashMap; import java.util.Map; -import java.util.function.BiFunction; import java.util.function.Function; /** @@ -23,13 +21,13 @@ public class NetworkModel { private final Map defaultBusModelMap = new HashMap<>(); private final Map defaultLineModelMap = new HashMap<>(); private final Function busModelFactory = DefaultBusModel::new; - private final BiFunction lineModelFactory = DefaultLineModel::new; + private final Function lineModelFactory = DefaultLineModel::new; public DefaultBusModel getDefaultBusModel(String staticId) { return defaultBusModelMap.computeIfAbsent(staticId, key -> busModelFactory.apply(staticId)); } - public DefaultLineModel getDefaultLineModel(String staticId, Branch.Side side) { - return defaultLineModelMap.computeIfAbsent(staticId, key -> lineModelFactory.apply(staticId, side)); + public DefaultLineModel getDefaultLineModel(String staticId) { + return defaultLineModelMap.computeIfAbsent(staticId, key -> lineModelFactory.apply(staticId)); } } 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 75fc2cf46..92b76b9ab 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 @@ -12,6 +12,7 @@ import com.powsybl.dynawaltz.models.Model; 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; @@ -37,9 +38,14 @@ public String getLib() { return "CurrentLimitAutomaton"; } + @Override + public String getName() { + return getLib() + LineSideUtils.getSuffix(side); + } + @Override public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(lineStaticId, side)); + return List.of(context.getDynamicModelOrDefaultLine(lineStaticId)); } @Override @@ -49,7 +55,7 @@ public List getVarConnectionsWith(Model connected) { } LineModel connectedLineModel = (LineModel) connected; return Arrays.asList( - new VarConnection("currentLimitAutomaton_IMonitored", connectedLineModel.getIVarName()), + new VarConnection("currentLimitAutomaton_IMonitored", connectedLineModel.getIVarName(side)), new VarConnection("currentLimitAutomaton_order", connectedLineModel.getStateVarName()), new VarConnection("currentLimitAutomaton_AutomatonExists", connectedLineModel.getDesactivateCurrentLimitsVarName()) ); 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 e94e59c24..8602787d9 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 @@ -12,7 +12,6 @@ import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.xml.ParametersXml; -import com.powsybl.iidm.network.Branch; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -50,7 +49,7 @@ public List getVarConnectionsWith(Model connected) { @Override public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(getEquipmentStaticId(), Branch.Side.ONE)); + return List.of(context.getDynamicModelOrDefaultLine(getEquipmentStaticId())); } @Override 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 32e6a5b8c..0d3eb4a66 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,27 +7,26 @@ 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; /** * @author Florian Dupuy */ public class DefaultLineModel extends AbstractNetworkModel implements LineModel { - private final String sideSuffix; - public DefaultLineModel(String staticId, Branch.Side side) { + public DefaultLineModel(String staticId) { super(staticId); - this.sideSuffix = LineModel.getSuffix(side); } @Override public String getName() { - return "NetworkLine" + sideSuffix; + return "NetworkLine"; } @Override - public String getIVarName() { - return "@NAME@_i" + sideSuffix; + public String getIVarName(Branch.Side side) { + return "@NAME@_i" + LineSideUtils.getSuffix(side); } @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 f8af11f26..b8067c6b4 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 @@ -13,22 +13,11 @@ * @author Florian Dupuy */ public interface LineModel extends Model { - String getIVarName(); + String getIVarName(Branch.Side side); String getStateVarName(); String getDesactivateCurrentLimitsVarName(); String getStateValueVarName(); - - static String getSuffix(Branch.Side side) { - switch (side) { - case ONE: - return "Side1"; - case TWO: - return "Side2"; - default: - throw new AssertionError("Unexpected Side value: " + side); - } - } } 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 045a74119..5a6524de9 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 @@ -8,21 +8,18 @@ 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.iidm.network.Line; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; public class StandardLine extends AbstractBlackBoxModel implements LineModel { - private final String sidePostfix; + private final Map busSideConnection = new HashMap<>(); - public StandardLine(String dynamicModelId, String staticId, String parameterSetId, Branch.Side side) { + public StandardLine(String dynamicModelId, String staticId, String parameterSetId) { super(dynamicModelId, staticId, parameterSetId); - this.sidePostfix = LineModel.getSuffix(side); } @Override @@ -40,7 +37,7 @@ public List getVarConnectionsWith(Model connected) { if (connected instanceof BusModel) { BusModel busModel = (BusModel) connected; return Arrays.asList( - new VarConnection(getIVarName(), busModel.getNumCCVarName()), + new VarConnection(getIVarName(busSideConnection.get(busModel.getStaticId().orElseThrow())), busModel.getNumCCVarName()), new VarConnection(getStateVarName(), busModel.getTerminalVarName()) ); } else { @@ -56,7 +53,11 @@ public List getModelsConnectedTo(DynaWaltzContext context) { throw new PowsyblException("Line static id unknown: " + getStaticId()); } List connectedBbm = new ArrayList<>(2); - line.getTerminals().forEach(t -> connectedBbm.add(context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(t)))); + line.getTerminals().forEach(t -> { + BusModel busModel = context.getDynamicModelOrDefaultBus(BusUtils.getConnectableBusStaticId(t)); + busSideConnection.put(busModel.getStaticId().orElseThrow(), line.getSide(t)); + connectedBbm.add(busModel); + }); return connectedBbm; } @@ -66,8 +67,8 @@ public String getName() { } @Override - public String getIVarName() { - return getDynamicModelId() + sidePostfix; + public String getIVarName(Branch.Side side) { + return getDynamicModelId() + LineSideUtils.getSuffix(side); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.java new file mode 100644 index 000000000..d771d7af4 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LineSideUtils.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.utils; + +import com.powsybl.iidm.network.Branch; + +/** + * @author Laurent Issertial + */ +public final class LineSideUtils { + + private LineSideUtils() { + } + + public static String getSuffix(Branch.Side side) { + switch (side) { + case ONE: + return "Side1"; + case TWO: + return "Side2"; + default: + throw new AssertionError("Unexpected Side value: " + side); + } + } +} 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 e38d05438..cad6d137c 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java @@ -94,7 +94,7 @@ void setup() { }); network.getLineStream().forEach(l -> { if (l.getId().equals("NHV1_NHV2_1")) { - dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL", Branch.Side.ONE)); + dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL")); } }); 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 b10cac00d..60c93d8b2 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java @@ -11,7 +11,6 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; -import com.powsybl.iidm.network.Branch; import org.junit.jupiter.api.Test; import org.xml.sax.SAXException; @@ -80,7 +79,7 @@ void testDynamicModelGetterException() { assertEquals("The model identified by the static id GEN5 is not a bus model", e.getMessage()); //line - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModelOrDefaultLine("GEN5", Branch.Side.ONE)); + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModelOrDefaultLine("GEN5")); assertEquals("The model identified by the static id GEN5 is not a line model", e.getMessage()); } } diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index 332981c43..d703f4524 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -70,12 +70,12 @@ - + - + @@ -152,8 +152,8 @@ - - + + diff --git a/dynawaltz/src/test/resources/events.xml b/dynawaltz/src/test/resources/events.xml index 8840c92dd..171ed4e72 100644 --- a/dynawaltz/src/test/resources/events.xml +++ b/dynawaltz/src/test/resources/events.xml @@ -27,13 +27,13 @@ - + - - + + From 0efd8a4573405208fb6eda83f9319888b5ae1417 Mon Sep 17 00:00:00 2001 From: lisrte Date: Thu, 2 Mar 2023 15:30:19 +0100 Subject: [PATCH 09/21] Refactor MacroConnect and MacroConnector creation Handle MacroConnect index Remove connector commutativity and ConnectedModels Connect StandardBus only with DefaultModel Signed-off-by: lisrte --- ...aWaltzGroovyDynamicModelsSupplierTest.java | 59 +++++----- .../powsybl/dynawaltz/DynaWaltzContext.java | 101 +++++++----------- .../models/AbstractBlackBoxModel.java | 72 +++++++++---- .../AbstractPureDynamicBlackBoxModel.java | 13 +++ .../dynawaltz/models/BlackBoxModel.java | 7 +- .../dynawaltz/models/DefaultModelFactory.java | 30 ++++++ .../dynawaltz/models/MacroConnect.java | 43 ++++++++ .../dynawaltz/models/MacroConnector.java | 29 +++-- .../dynawaltz/models/NetworkModel.java | 24 +++-- .../powsybl/dynawaltz/models/OmegaRef.java | 84 +++++---------- .../automatons/CurrentLimitAutomaton.java | 20 ++-- .../dynawaltz/models/buses/StandardBus.java | 38 +++---- .../models/events/AbstractEventModel.java | 13 +++ .../events/EventQuadripoleDisconnection.java | 15 +-- .../models/events/EventSetPointBoolean.java | 15 +-- .../generators/AbstractGeneratorModel.java | 17 ++- .../dynawaltz/models/lines/StandardLine.java | 48 ++++----- .../dynawaltz/models/loads/AbstractLoad.java | 10 +- .../dynawaltz/models/loads/LoadAlphaBeta.java | 13 +-- .../models/loads/LoadOneTransformer.java | 15 +-- .../models/utils/ConnectedModelTypes.java | 42 -------- .../models/utils/ConnectedModels.java | 56 ---------- .../com/powsybl/dynawaltz/xml/DydXml.java | 23 ++-- .../dynawaltz/models/OmegaRefTest.java | 59 ---------- .../models/buses/StandardBusTest.java | 34 +----- .../models/utils/ConnectedModelTypesTest.java | 36 ------- .../models/utils/ConnectedModelsTest.java | 41 ------- .../dynawaltz/xml/DynamicModelsXmlTest.java | 20 ++-- 28 files changed, 376 insertions(+), 601 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/DefaultModelFactory.java create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java delete mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypes.java delete mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModels.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypesTest.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelsTest.java 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 ebc74b9c3..eb2ad9acd 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 @@ -39,6 +39,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; @@ -56,8 +57,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 @@ -89,7 +90,7 @@ void test() { private static Network createEurostagTutorialExample1WithMoreGens() { Network network = EurostagTutorialExample1Factory.create(NetworkFactory.findDefault()); - VoltageLevel vlgen = network.getVoltageLevel("VLGEN"); + VoltageLevel vlgen = network.getVoltageLevel("VLGEN"); Bus ngen = vlgen.getBusBreakerView().getBus("NGEN"); vlgen.newGenerator() .setId("GEN2") @@ -146,7 +147,7 @@ private static Network createEurostagTutorialExample1WithMoreGens() { .setTargetP(-1.3) .setTargetQ(0.9) .add(); - VoltageLevel vlload = network.getVoltageLevel("VLLOAD"); + VoltageLevel vlload = network.getVoltageLevel("VLLOAD"); Bus nload = vlload.getBusBreakerView().getBus("NLOAD"); vlload.newLoad() .setId("LOAD2") @@ -169,8 +170,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); } @@ -214,25 +214,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/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 2cb9448ea..7788ebbc4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,12 +11,10 @@ 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.GeneratorSynchronousModel; -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; +import org.apache.commons.lang3.tuple.Pair; import java.nio.file.FileSystem; import java.nio.file.Path; @@ -28,6 +26,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class DynaWaltzContext { @@ -41,10 +40,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; @@ -76,26 +75,13 @@ public DynaWaltzContext(Network network, String workingVariantId, List 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 +109,19 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public Model getDynamicModelOrThrows(String staticId) { + public T getDynamicModel(String staticId, Class clazz, boolean defaultIfNotFound) { 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) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - return networkModel.getDefaultLineModel(staticId); - } - if (bbm instanceof LineModel) { - return (LineModel) 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 (defaultIfNotFound) { + return networkModel.getDefaultModel(staticId, clazz); + } else { + throw new PowsyblException("Cannot find the equipment '" + staticId + "' among the dynamic models provided"); + } } - if (bbm instanceof BusModel) { - return (BusModel) bbm; + if (clazz.isInstance(bbm)) { + return clazz.cast(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 +142,40 @@ 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 void addMacroConnector(String macroConId, List varConnections) { + macroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); } - public MacroConnector getEventMacroConnector(BlackBoxModel event, Model model) { - return eventConnectorsMap.get(ConnectedModelTypes.of(event.getName(), model.getName())); + public Collection getMacroConnectors() { + return macroConnectorsMap.values(); + } + + public void addEventMacroConnect(String macroConnectorId, List> attributesFrom, List> attributesTo) { + eventMacroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); } - private MacroConnector createMacroConnector(BlackBoxModel bbm, Model model) { - return new MacroConnector(bbm.getName(), model.getName(), bbm.getVarConnectionsWith(model)); + public List getEventMacroConnectList() { + return eventMacroConnectList; } - public Map> getModelsConnections() { - return modelsConnections; + public void addEventMacroConnector(String macroConId, List varConnections) { + eventMacroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); + } + + public Collection getEventMacroConnectors() { + return eventMacroConnectorsMap.values(); } - public Map> getEventModelsConnections() { - return eventModelsConnections; + public boolean isWithoutBlackBoxDynamicModel(String staticId) { + return getInputBlackBoxDynamicModelStream().noneMatch(d -> staticId.equals(d.getStaticId().orElseThrow())); } 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..20bced5fc 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -12,14 +12,17 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.function.Function; import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; /** * @author Luma Zamarreño + * @author Laurent Issertial */ public abstract class AbstractBlackBoxModel implements BlackBoxModel { @@ -55,13 +58,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(Integer index) { + Pair idAttribute = Pair.of("id1", getDynamicModelId()); + if (index == null) { + return List.of(idAttribute); + } else { + return List.of(idAttribute, Pair.of("index1", String.valueOf(index))); + } } @Override @@ -74,6 +77,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 +91,49 @@ 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 final void createMacroConnectionsWithIndex1(List staticIds, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { + int index = 0; + for (String id : staticIds) { + T connectedModel = context.getDynamicModel(id, modelClass, defaultIfNotFound); + createMacroConnections(connectedModel, varConnectionsSupplier, index, context); + index++; + } + } + + protected final void createMacroConnectionsWithIndex1(List models, Function> varConnectionsSupplier, DynaWaltzContext context) { + int index = 0; + for (T model : models) { + createMacroConnections(model, varConnectionsSupplier, index, context); + index++; + } + } + + protected final void createMacroConnections(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); + createMacroConnections(connectedModel, varConnectionsSupplier, null, context); + } + + protected final void createMacroConnections(T model, Function> varConnectionsSupplier, DynaWaltzContext context) { + createMacroConnections(model, varConnectionsSupplier, null, context); + } + + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); + context.addMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); } } 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..4f1307242 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java @@ -0,0 +1,43 @@ +/** + * 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 org.apache.commons.lang3.tuple.Pair; + +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 (Pair attribute : attributesFrom) { + writer.writeAttribute(attribute.getKey(), attribute.getValue()); + } + for (Pair attribute : attributesTo) { + writer.writeAttribute(attribute.getKey(), attribute.getValue()); + } + } +} 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..1b66df99a 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -6,17 +6,16 @@ */ 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 { @@ -24,21 +23,17 @@ public final class MacroConnector { private final String id; public MacroConnector(String name1, String name2, List varConnections) { - this.id = MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; + this.id = createMacroConnectorId(name1, name2); 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 MacroConnector(String id, List varConnections) { + this.id = id; + this.varConnections = varConnections; + } + + public static String createMacroConnectorId(String name1, String name2) { + return MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; } public void write(XMLStreamWriter writer) throws XMLStreamException { 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 5998dbd11..a7d06cf75 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -14,13 +14,12 @@ 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.Arrays; +import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.DOUBLE; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.INT; @@ -34,20 +33,17 @@ * 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 synchronousGenerators; public OmegaRef(List synchronousGenerators) { super(OMEGA_REF_ID, OMEGA_REF_PARAMETER_SET_ID); - int i = 0; - for (GeneratorSynchronousModel synchronousGenerator : synchronousGenerators) { - this.synchronousGenerators.put(synchronousGenerator, i++); - } + this.synchronousGenerators = synchronousGenerators; } public boolean isEmpty() { @@ -68,12 +64,12 @@ 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()) { - GeneratorSynchronousModel generator = e.getKey(); + int index = 0; + for (GeneratorSynchronousModel generator : synchronousGenerators) { 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())); @@ -81,65 +77,35 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th 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 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 getVarConnectionsWithGeneratorSynchronous(GeneratorSynchronousModel connected) { + return Arrays.asList( + new VarConnection("omega_grp_@INDEX@", connected.getOmegaPuVarName()), + new VarConnection("omegaRef_grp_@INDEX@", connected.getOmegaRefPuVarName()), + new VarConnection("running_grp_@INDEX@", connected.getRunningVarName()) + ); + } + + private List getVarConnectionsWithBus(BusModel connected) { + return List.of(new VarConnection("numcc_node_@INDEX@", connected.getNumCCVarName())); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) throws PowsyblException { - for (GeneratorSynchronousModel g : synchronousGenerators.keySet()) { - BusModel bus = getBusAssociatedTo(g, context); - busGeneratorListMap.computeIfAbsent(bus, k -> new ArrayList<>()).add(g); - } - return Stream.concat(synchronousGenerators.keySet().stream(), busGeneratorListMap.keySet().stream()) - .collect(Collectors.toList()); + public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { + List busStaticIds = synchronousGenerators.stream().map(g -> getBusAssociatedTo(g, context)).collect(Collectors.toList()); + createMacroConnectionsWithIndex1(synchronousGenerators, this::getVarConnectionsWithGeneratorSynchronous, context); + createMacroConnectionsWithIndex1(busStaticIds, BusModel.class, true, this::getVarConnectionsWithBus, context); } - private BusModel getBusAssociatedTo(GeneratorSynchronousModel generatorModel, DynaWaltzContext context) { + private String getBusAssociatedTo(GeneratorSynchronousModel 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 GeneratorSynchronousModel) { - List> attributesConnectFrom = getAttributesConnectFrom((GeneratorSynchronousModel) connected); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, connected.getMacroConnectToAttributes()); - } else if (connected instanceof BusModel) { - BusModel bus = (BusModel) connected; - for (GeneratorSynchronousModel g : busGeneratorListMap.get(bus)) { - List> attributesConnectFrom = getAttributesConnectFrom(g); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, bus.getMacroConnectToAttributes()); - } - } else { - throw new PowsyblException("OmegaRef can only connect to GeneratorSynchronousModel and BusModel"); - } - } - - private List> getAttributesConnectFrom(GeneratorSynchronousModel 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/automatons/CurrentLimitAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java index 92b76b9ab..00523964b 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,10 +6,8 @@ */ 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.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.models.utils.LineSideUtils; @@ -21,6 +19,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class CurrentLimitAutomaton extends AbstractPureDynamicBlackBoxModel { @@ -44,20 +43,15 @@ public String getName() { } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(lineStaticId)); + public void createMacroConnections(DynaWaltzContext context) { + createMacroConnections(lineStaticId, LineModel.class, true, this::getVarConnectionsWithLine, context); } - @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) { 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..dfb7eb355 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,22 @@ 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.iidm.network.Bus; +import com.powsybl.iidm.network.Identifiable; import org.apache.commons.lang3.tuple.Pair; 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; - /** * @author Dimitri Baudrier + * @author Laurent Issertial */ public class StandardBus extends AbstractBlackBoxModel implements BusModel { @@ -42,17 +39,9 @@ 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 @@ -62,26 +51,27 @@ public List> getMacroConnectToAttributes() { 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; + public List getVarConnectionsWithGenerator(GeneratorModel connected) { return Arrays.asList( - new VarConnection(getTerminalVarName(), connectedGeneratorModel.getTerminalVarName()), - new VarConnection(getSwitchOffSignalVarName(), connectedGeneratorModel.getSwitchOffSignalNodeVarName()) + new VarConnection(getTerminalVarName(), connected.getTerminalVarName()), + new VarConnection(getSwitchOffSignalVarName(), connected.getSwitchOffSignalNodeVarName()) ); } + /** + * Creates connections only with generators without a dynamic model + */ @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { 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()); + List staticIds = bus.getGeneratorStream().map(Identifiable::getId).filter(context::isWithoutBlackBoxDynamicModel).collect(Collectors.toList()); + if (!staticIds.isEmpty()) { + createMacroConnectionsWithIndex1(staticIds, GeneratorModel.class, false, this::getVarConnectionsWithGenerator, context); + } } @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..3d2e2d79f 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,22 @@ import com.powsybl.dynamicsimulation.EventModel; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +import com.powsybl.dynawaltz.models.MacroConnector; +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 +62,11 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th } protected abstract void writeEventSpecificParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; + + @Override + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); + context.addEventMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), 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..1ac343ee3 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, true, 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..ed6b7554e 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, false, 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..cd8e81447 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, true, 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/lines/StandardLine.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java index 5a6524de9..ac66f60f7 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,19 +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.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.iidm.network.Line; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +/** + * @author Laurent Issertial + */ public class StandardLine extends AbstractBlackBoxModel implements LineModel { private final Map busSideConnection = new HashMap<>(); @@ -27,38 +38,25 @@ 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) { + return Arrays.asList( + new VarConnection(getIVarName(busSideConnection.get(connected.getStaticId().orElseThrow())), 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: " + 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); + busSideConnection.put(busStaticId, line.getSide(t)); + createMacroConnections(busStaticId, BusModel.class, false, this::getVarConnectionsWithBus, context); }); - return connectedBbm; } @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..ef9d3d44f 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, true, 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/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 b6390f031..000000000 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java +++ /dev/null @@ -1,59 +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 javax.xml.stream.XMLStreamException; -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() throws XMLStreamException { - Model model = mock(Model.class); - PowsyblException e = assertThrows(PowsyblException.class, () -> omegaRef.writeMacroConnect(null, null, null, model)); - assertEquals("OmegaRef can only connect to GeneratorSynchronousModel 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..64fa1e3f7 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 @@ -10,22 +10,19 @@ 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.iidm.network.Network; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -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 +33,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 +42,7 @@ 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()); } } 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/DynamicModelsXmlTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java index 60c93d8b2..676c42d34 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,9 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; +import com.powsybl.dynawaltz.models.generators.GeneratorModel; +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 +26,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ class DynamicModelsXmlTest extends DynaWaltzTestUtil { @@ -70,16 +74,16 @@ 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")); + // throws exception id not found + Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("wrongID", GeneratorModel.class, false)); assertEquals("Cannot find the equipment 'wrongID' among the dynamic models provided", 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()); + // incorrect model + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("GEN5", LineModel.class, true)); + assertEquals("The model identified by the static id GEN5 is not the correct 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, true)); + assertEquals("Default model not implemented for GeneratorSynchronousModel", e.getMessage()); } } From 44610c4daba277338a9e8b6eaf191851b9be3639 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 3 Mar 2023 15:08:42 +0100 Subject: [PATCH 10/21] Check if StandardBus connect with DynamicModel or throw exception Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 2 +- .../dynawaltz/models/buses/StandardBus.java | 33 +++++++++---------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 7788ebbc4..299b23248 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -175,7 +175,7 @@ public Collection getEventMacroConnectors() { } public boolean isWithoutBlackBoxDynamicModel(String staticId) { - return getInputBlackBoxDynamicModelStream().noneMatch(d -> staticId.equals(d.getStaticId().orElseThrow())); + return !staticIdBlackBoxModelMap.containsKey(staticId); } private Stream getInputBlackBoxDynamicModelStream() { 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 dfb7eb355..79e9bdf6c 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,8 +10,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.VarConnection; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Identifiable; import org.apache.commons.lang3.tuple.Pair; @@ -19,9 +17,8 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; +import java.util.stream.Stream; /** * @author Dimitri Baudrier @@ -51,27 +48,27 @@ public List> getMacroConnectToAttributes() { return attributesConnectTo; } - public List getVarConnectionsWithGenerator(GeneratorModel connected) { - return Arrays.asList( - new VarConnection(getTerminalVarName(), connected.getTerminalVarName()), - new VarConnection(getSwitchOffSignalVarName(), connected.getSwitchOffSignalNodeVarName()) - ); - } - - /** - * Creates connections only with generators without a dynamic model - */ @Override 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); } - List staticIds = bus.getGeneratorStream().map(Identifiable::getId).filter(context::isWithoutBlackBoxDynamicModel).collect(Collectors.toList()); - if (!staticIds.isEmpty()) { - createMacroConnectionsWithIndex1(staticIds, GeneratorModel.class, false, this::getVarConnectionsWithGenerator, context); - } + 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 From bdc9e38dc6816bd24e57f2be650d2025a573a3cc Mon Sep 17 00:00:00 2001 From: lisrte Date: Thu, 2 Mar 2023 15:30:19 +0100 Subject: [PATCH 11/21] Refactor MacroConnect and MacroConnector creation Handle MacroConnect index Remove connector commutativity and ConnectedModels Connect StandardBus only with DefaultModel Signed-off-by: lisrte --- ...aWaltzGroovyDynamicModelsSupplierTest.java | 55 ++++++---- .../powsybl/dynawaltz/DynaWaltzContext.java | 101 +++++++----------- .../models/AbstractBlackBoxModel.java | 72 +++++++++---- .../AbstractPureDynamicBlackBoxModel.java | 13 +++ .../dynawaltz/models/BlackBoxModel.java | 7 +- .../dynawaltz/models/DefaultModelFactory.java | 30 ++++++ .../dynawaltz/models/MacroConnect.java | 43 ++++++++ .../dynawaltz/models/MacroConnector.java | 29 +++-- .../dynawaltz/models/NetworkModel.java | 24 +++-- .../powsybl/dynawaltz/models/OmegaRef.java | 84 +++++---------- .../automatons/CurrentLimitAutomaton.java | 20 ++-- .../dynawaltz/models/buses/StandardBus.java | 38 +++---- .../models/events/AbstractEventModel.java | 13 +++ .../events/EventQuadripoleDisconnection.java | 15 +-- .../models/events/EventSetPointBoolean.java | 15 +-- .../generators/AbstractGeneratorModel.java | 17 ++- .../dynawaltz/models/lines/StandardLine.java | 48 ++++----- .../dynawaltz/models/loads/AbstractLoad.java | 10 +- .../dynawaltz/models/loads/LoadAlphaBeta.java | 13 +-- .../models/loads/LoadOneTransformer.java | 15 +-- .../models/utils/ConnectedModelTypes.java | 42 -------- .../models/utils/ConnectedModels.java | 56 ---------- .../com/powsybl/dynawaltz/xml/DydXml.java | 23 ++-- .../dynawaltz/models/OmegaRefTest.java | 59 ---------- .../models/buses/StandardBusTest.java | 34 +----- .../models/utils/ConnectedModelTypesTest.java | 36 ------- .../models/utils/ConnectedModelsTest.java | 41 ------- .../dynawaltz/xml/DynamicModelsXmlTest.java | 20 ++-- 28 files changed, 374 insertions(+), 599 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/DefaultModelFactory.java create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java delete mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypes.java delete mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ConnectedModels.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelTypesTest.java delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/utils/ConnectedModelsTest.java 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 1898e8ad1..eb2ad9acd 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 @@ -39,6 +39,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; @@ -56,8 +57,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 @@ -169,8 +170,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); } @@ -214,25 +214,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/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 2cb9448ea..7788ebbc4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,12 +11,10 @@ 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.GeneratorSynchronousModel; -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; +import org.apache.commons.lang3.tuple.Pair; import java.nio.file.FileSystem; import java.nio.file.Path; @@ -28,6 +26,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class DynaWaltzContext { @@ -41,10 +40,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; @@ -76,26 +75,13 @@ public DynaWaltzContext(Network network, String workingVariantId, List 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 +109,19 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public Model getDynamicModelOrThrows(String staticId) { + public T getDynamicModel(String staticId, Class clazz, boolean defaultIfNotFound) { 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) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - return networkModel.getDefaultLineModel(staticId); - } - if (bbm instanceof LineModel) { - return (LineModel) 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 (defaultIfNotFound) { + return networkModel.getDefaultModel(staticId, clazz); + } else { + throw new PowsyblException("Cannot find the equipment '" + staticId + "' among the dynamic models provided"); + } } - if (bbm instanceof BusModel) { - return (BusModel) bbm; + if (clazz.isInstance(bbm)) { + return clazz.cast(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 +142,40 @@ 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 void addMacroConnector(String macroConId, List varConnections) { + macroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); } - public MacroConnector getEventMacroConnector(BlackBoxModel event, Model model) { - return eventConnectorsMap.get(ConnectedModelTypes.of(event.getName(), model.getName())); + public Collection getMacroConnectors() { + return macroConnectorsMap.values(); + } + + public void addEventMacroConnect(String macroConnectorId, List> attributesFrom, List> attributesTo) { + eventMacroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); } - private MacroConnector createMacroConnector(BlackBoxModel bbm, Model model) { - return new MacroConnector(bbm.getName(), model.getName(), bbm.getVarConnectionsWith(model)); + public List getEventMacroConnectList() { + return eventMacroConnectList; } - public Map> getModelsConnections() { - return modelsConnections; + public void addEventMacroConnector(String macroConId, List varConnections) { + eventMacroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); + } + + public Collection getEventMacroConnectors() { + return eventMacroConnectorsMap.values(); } - public Map> getEventModelsConnections() { - return eventModelsConnections; + public boolean isWithoutBlackBoxDynamicModel(String staticId) { + return getInputBlackBoxDynamicModelStream().noneMatch(d -> staticId.equals(d.getStaticId().orElseThrow())); } 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..20bced5fc 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -12,14 +12,17 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.function.Function; import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; /** * @author Luma Zamarreño + * @author Laurent Issertial */ public abstract class AbstractBlackBoxModel implements BlackBoxModel { @@ -55,13 +58,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(Integer index) { + Pair idAttribute = Pair.of("id1", getDynamicModelId()); + if (index == null) { + return List.of(idAttribute); + } else { + return List.of(idAttribute, Pair.of("index1", String.valueOf(index))); + } } @Override @@ -74,6 +77,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 +91,49 @@ 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 final void createMacroConnectionsWithIndex1(List staticIds, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { + int index = 0; + for (String id : staticIds) { + T connectedModel = context.getDynamicModel(id, modelClass, defaultIfNotFound); + createMacroConnections(connectedModel, varConnectionsSupplier, index, context); + index++; + } + } + + protected final void createMacroConnectionsWithIndex1(List models, Function> varConnectionsSupplier, DynaWaltzContext context) { + int index = 0; + for (T model : models) { + createMacroConnections(model, varConnectionsSupplier, index, context); + index++; + } + } + + protected final void createMacroConnections(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); + createMacroConnections(connectedModel, varConnectionsSupplier, null, context); + } + + protected final void createMacroConnections(T model, Function> varConnectionsSupplier, DynaWaltzContext context) { + createMacroConnections(model, varConnectionsSupplier, null, context); + } + + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); + context.addMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); } } 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..4f1307242 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java @@ -0,0 +1,43 @@ +/** + * 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 org.apache.commons.lang3.tuple.Pair; + +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 (Pair attribute : attributesFrom) { + writer.writeAttribute(attribute.getKey(), attribute.getValue()); + } + for (Pair attribute : attributesTo) { + writer.writeAttribute(attribute.getKey(), attribute.getValue()); + } + } +} 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..1b66df99a 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -6,17 +6,16 @@ */ 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 { @@ -24,21 +23,17 @@ public final class MacroConnector { private final String id; public MacroConnector(String name1, String name2, List varConnections) { - this.id = MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; + this.id = createMacroConnectorId(name1, name2); 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 MacroConnector(String id, List varConnections) { + this.id = id; + this.varConnections = varConnections; + } + + public static String createMacroConnectorId(String name1, String name2) { + return MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; } public void write(XMLStreamWriter writer) throws XMLStreamException { 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 5998dbd11..a7d06cf75 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -14,13 +14,12 @@ 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.Arrays; +import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.DOUBLE; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.INT; @@ -34,20 +33,17 @@ * 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 synchronousGenerators; public OmegaRef(List synchronousGenerators) { super(OMEGA_REF_ID, OMEGA_REF_PARAMETER_SET_ID); - int i = 0; - for (GeneratorSynchronousModel synchronousGenerator : synchronousGenerators) { - this.synchronousGenerators.put(synchronousGenerator, i++); - } + this.synchronousGenerators = synchronousGenerators; } public boolean isEmpty() { @@ -68,12 +64,12 @@ 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()) { - GeneratorSynchronousModel generator = e.getKey(); + int index = 0; + for (GeneratorSynchronousModel generator : synchronousGenerators) { 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())); @@ -81,65 +77,35 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th 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 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 getVarConnectionsWithGeneratorSynchronous(GeneratorSynchronousModel connected) { + return Arrays.asList( + new VarConnection("omega_grp_@INDEX@", connected.getOmegaPuVarName()), + new VarConnection("omegaRef_grp_@INDEX@", connected.getOmegaRefPuVarName()), + new VarConnection("running_grp_@INDEX@", connected.getRunningVarName()) + ); + } + + private List getVarConnectionsWithBus(BusModel connected) { + return List.of(new VarConnection("numcc_node_@INDEX@", connected.getNumCCVarName())); } @Override - public List getModelsConnectedTo(DynaWaltzContext context) throws PowsyblException { - for (GeneratorSynchronousModel g : synchronousGenerators.keySet()) { - BusModel bus = getBusAssociatedTo(g, context); - busGeneratorListMap.computeIfAbsent(bus, k -> new ArrayList<>()).add(g); - } - return Stream.concat(synchronousGenerators.keySet().stream(), busGeneratorListMap.keySet().stream()) - .collect(Collectors.toList()); + public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { + List busStaticIds = synchronousGenerators.stream().map(g -> getBusAssociatedTo(g, context)).collect(Collectors.toList()); + createMacroConnectionsWithIndex1(synchronousGenerators, this::getVarConnectionsWithGeneratorSynchronous, context); + createMacroConnectionsWithIndex1(busStaticIds, BusModel.class, true, this::getVarConnectionsWithBus, context); } - private BusModel getBusAssociatedTo(GeneratorSynchronousModel generatorModel, DynaWaltzContext context) { + private String getBusAssociatedTo(GeneratorSynchronousModel 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 GeneratorSynchronousModel) { - List> attributesConnectFrom = getAttributesConnectFrom((GeneratorSynchronousModel) connected); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, connected.getMacroConnectToAttributes()); - } else if (connected instanceof BusModel) { - BusModel bus = (BusModel) connected; - for (GeneratorSynchronousModel g : busGeneratorListMap.get(bus)) { - List> attributesConnectFrom = getAttributesConnectFrom(g); - macroConnector.writeMacroConnect(writer, attributesConnectFrom, bus.getMacroConnectToAttributes()); - } - } else { - throw new PowsyblException("OmegaRef can only connect to GeneratorSynchronousModel and BusModel"); - } - } - - private List> getAttributesConnectFrom(GeneratorSynchronousModel 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/automatons/CurrentLimitAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitAutomaton.java index 92b76b9ab..00523964b 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,10 +6,8 @@ */ 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.VarConnection; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.models.utils.LineSideUtils; @@ -21,6 +19,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ public class CurrentLimitAutomaton extends AbstractPureDynamicBlackBoxModel { @@ -44,20 +43,15 @@ public String getName() { } @Override - public List getModelsConnectedTo(DynaWaltzContext context) { - return List.of(context.getDynamicModelOrDefaultLine(lineStaticId)); + public void createMacroConnections(DynaWaltzContext context) { + createMacroConnections(lineStaticId, LineModel.class, true, this::getVarConnectionsWithLine, context); } - @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) { 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..dfb7eb355 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,22 @@ 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.iidm.network.Bus; +import com.powsybl.iidm.network.Identifiable; import org.apache.commons.lang3.tuple.Pair; 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; - /** * @author Dimitri Baudrier + * @author Laurent Issertial */ public class StandardBus extends AbstractBlackBoxModel implements BusModel { @@ -42,17 +39,9 @@ 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 @@ -62,26 +51,27 @@ public List> getMacroConnectToAttributes() { 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; + public List getVarConnectionsWithGenerator(GeneratorModel connected) { return Arrays.asList( - new VarConnection(getTerminalVarName(), connectedGeneratorModel.getTerminalVarName()), - new VarConnection(getSwitchOffSignalVarName(), connectedGeneratorModel.getSwitchOffSignalNodeVarName()) + new VarConnection(getTerminalVarName(), connected.getTerminalVarName()), + new VarConnection(getSwitchOffSignalVarName(), connected.getSwitchOffSignalNodeVarName()) ); } + /** + * Creates connections only with generators without a dynamic model + */ @Override - public List getModelsConnectedTo(DynaWaltzContext context) { + public void createMacroConnections(DynaWaltzContext context) { 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()); + List staticIds = bus.getGeneratorStream().map(Identifiable::getId).filter(context::isWithoutBlackBoxDynamicModel).collect(Collectors.toList()); + if (!staticIds.isEmpty()) { + createMacroConnectionsWithIndex1(staticIds, GeneratorModel.class, false, this::getVarConnectionsWithGenerator, context); + } } @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..3d2e2d79f 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,22 @@ import com.powsybl.dynamicsimulation.EventModel; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +import com.powsybl.dynawaltz.models.MacroConnector; +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 +62,11 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th } protected abstract void writeEventSpecificParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; + + @Override + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); + context.addEventMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), 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..1ac343ee3 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, true, 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..ed6b7554e 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, false, 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..cd8e81447 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, true, 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/lines/StandardLine.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/lines/StandardLine.java index 3704b5d5a..815bf0f5b 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,19 +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.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.iidm.network.Line; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +/** + * @author Laurent Issertial + */ public class StandardLine extends AbstractBlackBoxModel implements LineModel { private final Map busSideConnection = new HashMap<>(); @@ -27,38 +38,25 @@ 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) { + return Arrays.asList( + new VarConnection(getIVarName(busSideConnection.get(connected.getStaticId().orElseThrow())), 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); } - 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); + busSideConnection.put(busStaticId, line.getSide(t)); + createMacroConnections(busStaticId, BusModel.class, false, this::getVarConnectionsWithBus, context); }); - return connectedBbm; } @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..ef9d3d44f 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, true, 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/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 b6390f031..000000000 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java +++ /dev/null @@ -1,59 +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 javax.xml.stream.XMLStreamException; -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() throws XMLStreamException { - Model model = mock(Model.class); - PowsyblException e = assertThrows(PowsyblException.class, () -> omegaRef.writeMacroConnect(null, null, null, model)); - assertEquals("OmegaRef can only connect to GeneratorSynchronousModel 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..64fa1e3f7 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 @@ -10,22 +10,19 @@ 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.iidm.network.Network; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -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 +33,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 +42,7 @@ 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()); } } 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/DynamicModelsXmlTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java index 60c93d8b2..676c42d34 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,9 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; +import com.powsybl.dynawaltz.models.generators.GeneratorModel; +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 +26,7 @@ /** * @author Marcos de Miguel + * @author Laurent Issertial */ class DynamicModelsXmlTest extends DynaWaltzTestUtil { @@ -70,16 +74,16 @@ 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")); + // throws exception id not found + Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("wrongID", GeneratorModel.class, false)); assertEquals("Cannot find the equipment 'wrongID' among the dynamic models provided", 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()); + // incorrect model + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("GEN5", LineModel.class, true)); + assertEquals("The model identified by the static id GEN5 is not the correct 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, true)); + assertEquals("Default model not implemented for GeneratorSynchronousModel", e.getMessage()); } } From 642ff0da3123658c7e4ef0c278a5ccd992798ff5 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 3 Mar 2023 15:08:42 +0100 Subject: [PATCH 12/21] Check if StandardBus connect with DynamicModel or throw exception Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 2 +- .../dynawaltz/models/buses/StandardBus.java | 33 +++++++++---------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 7788ebbc4..299b23248 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -175,7 +175,7 @@ public Collection getEventMacroConnectors() { } public boolean isWithoutBlackBoxDynamicModel(String staticId) { - return getInputBlackBoxDynamicModelStream().noneMatch(d -> staticId.equals(d.getStaticId().orElseThrow())); + return !staticIdBlackBoxModelMap.containsKey(staticId); } private Stream getInputBlackBoxDynamicModelStream() { 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 dfb7eb355..79e9bdf6c 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,8 +10,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; -import com.powsybl.dynawaltz.models.VarConnection; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Identifiable; import org.apache.commons.lang3.tuple.Pair; @@ -19,9 +17,8 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; +import java.util.stream.Stream; /** * @author Dimitri Baudrier @@ -51,27 +48,27 @@ public List> getMacroConnectToAttributes() { return attributesConnectTo; } - public List getVarConnectionsWithGenerator(GeneratorModel connected) { - return Arrays.asList( - new VarConnection(getTerminalVarName(), connected.getTerminalVarName()), - new VarConnection(getSwitchOffSignalVarName(), connected.getSwitchOffSignalNodeVarName()) - ); - } - - /** - * Creates connections only with generators without a dynamic model - */ @Override 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); } - List staticIds = bus.getGeneratorStream().map(Identifiable::getId).filter(context::isWithoutBlackBoxDynamicModel).collect(Collectors.toList()); - if (!staticIds.isEmpty()) { - createMacroConnectionsWithIndex1(staticIds, GeneratorModel.class, false, this::getVarConnectionsWithGenerator, context); - } + 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 From f8010481dcfb3d4c85a3d0ea5430525c3d4bb217 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 10 Mar 2023 17:15:49 +0100 Subject: [PATCH 13/21] Fix UT Signed-off-by: lisrte --- .../dynawaltz/xml/DynaWaltzTestUtil.java | 7 ++- dynawaltz/src/test/resources/dyd.xml | 46 +++++++++++-------- 2 files changed, 31 insertions(+), 22 deletions(-) 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 cad6d137c..e9d811054 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java @@ -35,6 +35,7 @@ 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; @@ -93,9 +94,7 @@ void setup() { } }); network.getLineStream().forEach(l -> { - if (l.getId().equals("NHV1_NHV2_1")) { - dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL")); - } + dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL")); }); // Events @@ -118,7 +117,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)); + compareXml(Objects.requireNonNull(getClass().getResourceAsStream("/" + expectedResourceName)), Files.newInputStream(xmlFile)); } private static Network createEurostagTutorialExample1WithMoreLoads() { diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index d703f4524..0813aafca 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -30,6 +30,7 @@ + @@ -75,11 +76,6 @@ - - - - - @@ -143,23 +139,37 @@ - - - - - + + + + + - + + + - - - - - - - + + + + + + + From c5c998d2556e882d513faeb3256640f478a0cf82 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 13 Mar 2023 10:40:44 +0100 Subject: [PATCH 14/21] Fix UT Signed-off-by: lisrte --- .../java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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 cad6d137c..3f084ab2d 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java @@ -92,11 +92,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<>(); From b0269389fbb697cfa858367cdfe2eee0c69e6564 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 13 Mar 2023 16:06:28 +0100 Subject: [PATCH 15/21] Handle Generator PQ Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 11 ++-- .../powsybl/dynawaltz/models/OmegaRef.java | 29 ++++------ .../generators/GeneratorSynchronous.java | 18 +++++- .../generators/GeneratorSynchronousModel.java | 1 - .../models/generators/OmegaRefGenerator.java | 16 +++++- .../generators/OmegaRefGeneratorModel.java | 7 ++- .../dynawaltz/models/OmegaRefTest.java | 57 ------------------- dynawaltz/src/test/resources/dyd.xml | 10 ++-- 8 files changed, 57 insertions(+), 92 deletions(-) delete mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/OmegaRefTest.java diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 299b23248..68ac87627 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,7 +11,7 @@ import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.models.*; -import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; +import com.powsybl.dynawaltz.models.generators.OmegaRefGeneratorModel; import com.powsybl.dynawaltz.xml.MacroStaticReference; import com.powsybl.iidm.network.Network; import org.apache.commons.lang3.tuple.Pair; @@ -69,11 +69,10 @@ public DynaWaltzContext(Network network, String workingVariantId, List synchronousGenerators = dynamicModels.stream() - .filter(GeneratorSynchronousModel.class::isInstance) - .map(GeneratorSynchronousModel.class::cast) - .collect(Collectors.toList()); - this.omegaRef = new OmegaRef(synchronousGenerators); + this.omegaRef = new OmegaRef(dynamicModels.stream() + .filter(OmegaRefGeneratorModel.class::isInstance) + .map(OmegaRefGeneratorModel.class::cast) + .collect(Collectors.toList())); for (BlackBoxModel bbm : getBlackBoxDynamicModelStream().collect(Collectors.toList())) { macroStaticReferences.computeIfAbsent(bbm.getName(), k -> new MacroStaticReference(k, bbm.getVarsMapping())); 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 a7d06cf75..c2cbd408f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -10,14 +10,13 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParametersDatabase; import com.powsybl.dynawaltz.models.buses.BusModel; -import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; +import com.powsybl.dynawaltz.models.generators.OmegaRefGeneratorModel; import com.powsybl.dynawaltz.models.utils.BusUtils; import com.powsybl.dynawaltz.xml.ParametersXml; import com.powsybl.iidm.network.Generator; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -39,15 +38,15 @@ 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 List synchronousGenerators; + private final List omegaRefGenerators; - public OmegaRef(List synchronousGenerators) { + public OmegaRef(List omegaRefGenerators) { super(OMEGA_REF_ID, OMEGA_REF_PARAMETER_SET_ID); - this.synchronousGenerators = synchronousGenerators; + this.omegaRefGenerators = omegaRefGenerators; } public boolean isEmpty() { - return synchronousGenerators.isEmpty(); + return omegaRefGenerators.isEmpty(); } @Override @@ -65,24 +64,20 @@ 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. int index = 0; - for (GeneratorSynchronousModel generator : synchronousGenerators) { + 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_" + 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(); } - private List getVarConnectionsWithGeneratorSynchronous(GeneratorSynchronousModel connected) { - return Arrays.asList( - new VarConnection("omega_grp_@INDEX@", connected.getOmegaPuVarName()), - new VarConnection("omegaRef_grp_@INDEX@", connected.getOmegaRefPuVarName()), - new VarConnection("running_grp_@INDEX@", connected.getRunningVarName()) - ); + private List getVarConnectionsWithOmegaRefGenerator(OmegaRefGeneratorModel connected) { + return connected.getOmegaRefVarConnections(); } private List getVarConnectionsWithBus(BusModel connected) { @@ -91,12 +86,12 @@ private List getVarConnectionsWithBus(BusModel connected) { @Override public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { - List busStaticIds = synchronousGenerators.stream().map(g -> getBusAssociatedTo(g, context)).collect(Collectors.toList()); - createMacroConnectionsWithIndex1(synchronousGenerators, this::getVarConnectionsWithGeneratorSynchronous, context); + List busStaticIds = omegaRefGenerators.stream().map(g -> getBusAssociatedTo(g, context)).collect(Collectors.toList()); + createMacroConnectionsWithIndex1(omegaRefGenerators, this::getVarConnectionsWithOmegaRefGenerator, context); createMacroConnectionsWithIndex1(busStaticIds, BusModel.class, true, this::getVarConnectionsWithBus, context); } - private String getBusAssociatedTo(GeneratorSynchronousModel 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()); 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/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/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index f8374f4e8..e8b22ba27 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -33,6 +33,7 @@ + @@ -82,11 +83,6 @@ - - - - - @@ -169,8 +165,10 @@ + + - + From da3cb50bd429ee8285eb82393efa22e172ffbe74 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 13 Mar 2023 16:49:23 +0100 Subject: [PATCH 16/21] Add UT Signed-off-by: lisrte --- .../dynawaltz/models/MacroConnector.java | 5 ---- .../models/buses/StandardBusTest.java | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) 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 1b66df99a..5e49780fd 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -22,11 +22,6 @@ public final class MacroConnector { private final List varConnections; private final String id; - public MacroConnector(String name1, String name2, List varConnections) { - this.id = createMacroConnectorId(name1, name2); - this.varConnections = varConnections; - } - public MacroConnector(String id, List varConnections) { this.id = id; this.varConnections = varConnections; 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 64fa1e3f7..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,11 +9,21 @@ 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.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.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.anyString; @@ -45,4 +55,22 @@ void getModelsConnectedToException() { 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()); + } } From da019d228552f710d22eb3fa2da792aeefd57e1e Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 14 Mar 2023 15:29:53 +0100 Subject: [PATCH 17/21] Create parametrized MacroConnector Signed-off-by: lisrte --- .../powsybl/dynawaltz/DynaWaltzContext.java | 18 +++- .../models/AbstractBlackBoxModel.java | 10 +- .../dynawaltz/models/MacroConnector.java | 4 + .../models/events/AbstractEventModel.java | 4 +- .../models/lines/DefaultLineModel.java | 6 +- .../dynawaltz/models/lines/LineModel.java | 3 +- .../dynawaltz/models/lines/StandardLine.java | 18 ++-- .../dynawaltz/xml/DynaWaltzTestUtil.java | 7 +- dynawaltz/src/test/resources/curvesInput.xml | 92 +++++++++---------- dynawaltz/src/test/resources/dyd.xml | 71 ++++++++++---- 10 files changed, 136 insertions(+), 97 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 68ac87627..35edbd067 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -149,8 +149,16 @@ public List getMacroConnectList() { return macroConnectList; } - public void addMacroConnector(String macroConId, List varConnections) { - macroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); + 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 String addMacroConnector(String name1, String name2, String parametrizedName, List varConnections) { + String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2, parametrizedName); + macroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + return macroConnectorId; } public Collection getMacroConnectors() { @@ -165,8 +173,10 @@ public List getEventMacroConnectList() { return eventMacroConnectList; } - public void addEventMacroConnector(String macroConId, List varConnections) { - eventMacroConnectorsMap.computeIfAbsent(macroConId, k -> new MacroConnector(macroConId, varConnections)); + 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() { 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 20bced5fc..98a97d9a0 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.function.BiFunction; import java.util.function.Function; import static com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants.DYN_URI; @@ -132,8 +133,13 @@ protected final void createMacroConnections(T model, Function< } protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { - String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); - context.addMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); } + + protected void createMacroConnectionsWithParametrizedConnector(String modelStaticId, Class modelClass, boolean defaultIfNotFound, BiFunction> varConnectionsSupplier, String parametrizedName, DynaWaltzContext context) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), parametrizedName, varConnectionsSupplier.apply(connectedModel, parametrizedName)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(null), connectedModel.getMacroConnectToAttributes()); + } } 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 5e49780fd..be364ac8a 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -31,6 +31,10 @@ public static String createMacroConnectorId(String name1, String name2) { return MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; } + public static String createMacroConnectorId(String name1, String name2, String parametrizedName) { + return MACRO_CONNECTOR_PREFIX + name1 + parametrizedName + "-" + name2; + } + public void write(XMLStreamWriter writer) throws XMLStreamException { writer.writeStartElement(DYN_URI, "macroConnector"); writer.writeAttribute("id", id); 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 3d2e2d79f..0bae786e2 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,7 +10,6 @@ import com.powsybl.dynamicsimulation.EventModel; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; -import com.powsybl.dynawaltz.models.MacroConnector; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.xml.ParametersXml; @@ -65,8 +64,7 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th @Override protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { - String macroConnectorId = MacroConnector.createMacroConnectorId(getName(), connectedModel.getName()); - context.addEventMacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel)); + String macroConnectorId = context.addEventMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); } } 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..78a992c5d 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,6 @@ 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; /** * @author Florian Dupuy @@ -25,8 +23,8 @@ public String getName() { } @Override - public String getIVarName(Branch.Side side) { - return "@NAME@_i" + LineSideUtils.getSuffix(side); + public String getIVarName(String sideSuffix) { + return "@NAME@_i" + sideSuffix; } @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..0032c3cbb 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,12 @@ package com.powsybl.dynawaltz.models.lines; import com.powsybl.dynawaltz.models.Model; -import com.powsybl.iidm.network.Branch; /** * @author Florian Dupuy */ public interface LineModel extends Model { - String getIVarName(Branch.Side side); + String getIVarName(String sideSuffix); 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 ac66f60f7..389a7b699 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 @@ -14,21 +14,16 @@ 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.iidm.network.Line; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @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); } @@ -38,9 +33,9 @@ public String getLib() { return "Line"; } - private List getVarConnectionsWithBus(BusModel connected) { + private List getVarConnectionsWithBus(BusModel connected, String suffix) { return Arrays.asList( - new VarConnection(getIVarName(busSideConnection.get(connected.getStaticId().orElseThrow())), connected.getNumCCVarName()), + new VarConnection(getIVarName(suffix), connected.getNumCCVarName()), new VarConnection(getStateVarName(), connected.getTerminalVarName()) ); } @@ -54,19 +49,18 @@ public void createMacroConnections(DynaWaltzContext context) { } line.getTerminals().forEach(t -> { String busStaticId = BusUtils.getConnectableBusStaticId(t); - busSideConnection.put(busStaticId, line.getSide(t)); - createMacroConnections(busStaticId, BusModel.class, false, this::getVarConnectionsWithBus, context); + createMacroConnectionsWithParametrizedConnector(busStaticId, BusModel.class, true, this::getVarConnectionsWithBus, LineSideUtils.getSuffix(line.getSide(t)), context); }); } @Override public String getName() { - return getLib(); + return getDynamicModelId(); } @Override - public String getIVarName(Branch.Side side) { - return getDynamicModelId() + LineSideUtils.getSuffix(side); + public String getIVarName(String sideSuffix) { + return getDynamicModelId() + sideSuffix; } @Override 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 4ffc66b4b..edc9c8830 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java @@ -14,9 +14,9 @@ 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; @@ -36,8 +36,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 @@ -119,7 +120,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/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 e8b22ba27..7f4a4fe55 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -74,15 +74,32 @@ - + - + + + + + + + + + + + + + + + + + + @@ -155,27 +172,41 @@ - - - - - + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - + From 0ee56ba46a5bc1a2201fa1aa66d1ceba51016276 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 14 Mar 2023 15:33:23 +0100 Subject: [PATCH 18/21] Fix CurrentLimitAutomaton Signed-off-by: lisrte --- .../models/automatons/CurrentLimitAutomaton.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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 00523964b..c7f8bf833 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 @@ -37,19 +37,14 @@ public String getLib() { return "CurrentLimitAutomaton"; } - @Override - public String getName() { - return getLib() + LineSideUtils.getSuffix(side); - } - @Override public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(lineStaticId, LineModel.class, true, this::getVarConnectionsWithLine, context); + createMacroConnectionsWithParametrizedConnector(lineStaticId, LineModel.class, true, this::getVarConnectionsWithLine, LineSideUtils.getSuffix(side), context); } - private List getVarConnectionsWithLine(LineModel connected) { + private List getVarConnectionsWithLine(LineModel connected, String suffix) { return Arrays.asList( - new VarConnection("currentLimitAutomaton_IMonitored", connected.getIVarName(side)), + new VarConnection("currentLimitAutomaton_IMonitored", connected.getIVarName(suffix)), new VarConnection("currentLimitAutomaton_order", connected.getStateVarName()), new VarConnection("currentLimitAutomaton_AutomatonExists", connected.getDesactivateCurrentLimitsVarName()) ); From 7d33ad88f88ba9325056a365b5a7640e80ca9811 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 14 Mar 2023 15:46:03 +0100 Subject: [PATCH 19/21] Fix UT Signed-off-by: lisrte --- dynawaltz/src/test/resources/dyd.xml | 42 ++++++++++------------------ 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index 7f4a4fe55..5ad8d1370 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -172,41 +172,27 @@ - - - - - + + + + + - + - - - - - - - + + + + + + + - + From a6a720c08f0f40bb47991c448a93d555101e7e86 Mon Sep 17 00:00:00 2001 From: lisrte Date: Wed, 15 Mar 2023 17:00:42 +0100 Subject: [PATCH 20/21] Fix OmegaRef et MacroConnect creation Signed-off-by: lisrte --- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 14 ++--- .../dynawaltz-inputs/powsybl_dynawaltz.jobs | 2 +- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 14 ++--- .../dynawaltz-inputs/powsybl_dynawaltz.jobs | 2 +- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 14 ++--- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 12 ++-- .../powsybl/dynawaltz/DynaWaltzContext.java | 5 +- .../models/AbstractBlackBoxModel.java | 57 +++++++------------ .../models/AbstractNetworkModel.java | 7 +-- .../dynawaltz/models/MacroConnect.java | 16 +++--- .../models/MacroConnectAttribute.java | 36 ++++++++++++ .../com/powsybl/dynawaltz/models/Model.java | 3 +- .../powsybl/dynawaltz/models/OmegaRef.java | 10 ++-- .../dynawaltz/models/buses/StandardBus.java | 8 +-- .../models/events/AbstractEventModel.java | 4 +- .../generators/AbstractGeneratorModel.java | 5 ++ dynawaltz/src/test/resources/dyd.xml | 18 +++--- 17 files changed, 124 insertions(+), 103 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java 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 35edbd067..cb3da968f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -14,7 +14,6 @@ import com.powsybl.dynawaltz.models.generators.OmegaRefGeneratorModel; import com.powsybl.dynawaltz.xml.MacroStaticReference; import com.powsybl.iidm.network.Network; -import org.apache.commons.lang3.tuple.Pair; import java.nio.file.FileSystem; import java.nio.file.Path; @@ -141,7 +140,7 @@ private static List checkEventModelIdUniqueness(List> attributesFrom, List> attributesTo) { + public void addMacroConnect(String macroConnectorId, List attributesFrom, List attributesTo) { macroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); } @@ -165,7 +164,7 @@ public Collection getMacroConnectors() { return macroConnectorsMap.values(); } - public void addEventMacroConnect(String macroConnectorId, List> attributesFrom, List> attributesTo) { + public void addEventMacroConnect(String macroConnectorId, List attributesFrom, List attributesTo) { eventMacroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); } 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 98a97d9a0..c8cfe5380 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -8,16 +8,14 @@ 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.Collections; -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; @@ -59,18 +57,13 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th // method empty by default to be redefined by specific models } - protected List> getMacroConnectFromAttributes(Integer index) { - Pair idAttribute = Pair.of("id1", getDynamicModelId()); - if (index == null) { - return List.of(idAttribute); - } else { - return List.of(idAttribute, Pair.of("index1", String.valueOf(index))); - } + 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 @@ -106,40 +99,30 @@ public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLSt } } - protected final void createMacroConnectionsWithIndex1(List staticIds, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { - int index = 0; - for (String id : staticIds) { - T connectedModel = context.getDynamicModel(id, modelClass, defaultIfNotFound); - createMacroConnections(connectedModel, varConnectionsSupplier, index, context); - index++; - } - } - - protected final void createMacroConnectionsWithIndex1(List models, Function> varConnectionsSupplier, DynaWaltzContext context) { - int index = 0; - for (T model : models) { - createMacroConnections(model, varConnectionsSupplier, index, context); - index++; - } - } - protected final void createMacroConnections(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); - createMacroConnections(connectedModel, varConnectionsSupplier, null, context); + createMacroConnections(connectedModel, varConnectionsSupplier, context); } - protected final void createMacroConnections(T model, Function> varConnectionsSupplier, DynaWaltzContext context) { - createMacroConnections(model, varConnectionsSupplier, null, context); + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext context) { + String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); + context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); } - protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + protected final void createMacroConnectionsWithParametrizedConnect(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); + List fromAttributes = Stream.concat(getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); + context.addMacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); + } + + protected final void createMacroConnectionsWithParametrizedConnect(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { + T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); + createMacroConnectionsWithParametrizedConnect(connectedModel, varConnectionsSupplier, context, connectFromAttributes); } protected void createMacroConnectionsWithParametrizedConnector(String modelStaticId, Class modelClass, boolean defaultIfNotFound, BiFunction> varConnectionsSupplier, String parametrizedName, DynaWaltzContext context) { T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), parametrizedName, varConnectionsSupplier.apply(connectedModel, parametrizedName)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(null), connectedModel.getMacroConnectToAttributes()); + 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/MacroConnect.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java index 4f1307242..0945294ed 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnect.java @@ -7,8 +7,6 @@ */ package com.powsybl.dynawaltz.models; -import org.apache.commons.lang3.tuple.Pair; - import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.List; @@ -21,10 +19,10 @@ public final class MacroConnect { private final String id; - private final List> attributesFrom; - private final List> attributesTo; + private final List attributesFrom; + private final List attributesTo; - public MacroConnect(String id, List> attributesFrom, List> attributesTo) { + public MacroConnect(String id, List attributesFrom, List attributesTo) { this.id = id; this.attributesFrom = attributesFrom; this.attributesTo = attributesTo; @@ -33,11 +31,11 @@ public MacroConnect(String id, List> attributesFrom, List

attribute : attributesFrom) { - writer.writeAttribute(attribute.getKey(), attribute.getValue()); + for (MacroConnectAttribute attribute : attributesFrom) { + writer.writeAttribute(attribute.getName(), attribute.getValue()); } - for (Pair attribute : attributesTo) { - writer.writeAttribute(attribute.getKey(), 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..0377f60aa --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java @@ -0,0 +1,36 @@ +/** + * 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/. + */ +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/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/OmegaRef.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java index c2cbd408f..6e9d57df8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -18,7 +18,6 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.util.List; -import java.util.stream.Collectors; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.DOUBLE; import static com.powsybl.dynawaltz.DynaWaltzParametersDatabase.ParameterType.INT; @@ -86,9 +85,12 @@ private List getVarConnectionsWithBus(BusModel connected) { @Override public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { - List busStaticIds = omegaRefGenerators.stream().map(g -> getBusAssociatedTo(g, context)).collect(Collectors.toList()); - createMacroConnectionsWithIndex1(omegaRefGenerators, this::getVarConnectionsWithOmegaRefGenerator, context); - createMacroConnectionsWithIndex1(busStaticIds, BusModel.class, true, this::getVarConnectionsWithBus, context); + int index = 0; + for (OmegaRefGeneratorModel gen : omegaRefGenerators) { + createMacroConnectionsWithParametrizedConnect(gen, this::getVarConnectionsWithOmegaRefGenerator, context, MacroConnectAttribute.ofIndex1(index)); + createMacroConnectionsWithParametrizedConnect(getBusAssociatedTo(gen, context), BusModel.class, true, this::getVarConnectionsWithBus, context, MacroConnectAttribute.ofIndex1(index)); + index++; + } } private String getBusAssociatedTo(OmegaRefGeneratorModel generatorModel, DynaWaltzContext context) { 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 79e9bdf6c..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,9 +10,9 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; +import com.powsybl.dynawaltz.models.MacroConnectAttribute; import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Identifiable; -import org.apache.commons.lang3.tuple.Pair; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -42,9 +42,9 @@ protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext c } @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; } 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 0bae786e2..e55179a4c 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 @@ -63,8 +63,8 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th protected abstract void writeEventSpecificParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; @Override - protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, Integer index, DynaWaltzContext context) { + protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext context) { String macroConnectorId = context.addEventMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(index), connectedModel.getMacroConnectToAttributes()); + context.addEventMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); } } 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 cd8e81447..fa6bc34f9 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 @@ -94,4 +94,9 @@ public String getSwitchOffSignalAutomatonVarName() { public String getRunningVarName() { return runningVarName; } + + @Override + public String getQStatorPuVarName() { + return "generator_QStatorPu"; + } } diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index 5ad8d1370..b5b045ca4 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -105,6 +105,9 @@ + + + @@ -124,9 +127,6 @@ - - - @@ -187,17 +187,17 @@ - - - - - - + + + + + + From c7095354d5a13cbb55130ad48d02141def5ddab2 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 17 Mar 2023 10:42:14 +0100 Subject: [PATCH 21/21] Review fixes Signed-off-by: lisrte --- ...urrentLimitAutomatonGroovyExtension.groovy | 6 +++-- .../powsybl/dynawaltz/DynaWaltzContext.java | 12 +++------ .../models/AbstractBlackBoxModel.java | 27 +++++++++---------- .../models/MacroConnectAttribute.java | 3 ++- .../dynawaltz/models/MacroConnector.java | 4 +-- .../powsybl/dynawaltz/models/OmegaRef.java | 4 +-- .../com/powsybl/dynawaltz/models/Side.java | 27 +++++++++++++++++++ .../automatons/CurrentLimitAutomaton.java | 13 +++++---- .../models/events/AbstractEventModel.java | 3 ++- .../events/EventQuadripoleDisconnection.java | 2 +- .../models/events/EventSetPointBoolean.java | 2 +- .../generators/AbstractGeneratorModel.java | 7 +---- .../models/lines/DefaultLineModel.java | 5 ++-- .../dynawaltz/models/lines/LineModel.java | 3 ++- .../dynawaltz/models/lines/StandardLine.java | 13 ++++----- .../dynawaltz/models/loads/AbstractLoad.java | 2 +- ...{LineSideUtils.java => SideConverter.java} | 11 ++++---- .../dynawaltz/xml/DynaWaltzTestUtil.java | 8 ++++-- .../dynawaltz/xml/DynamicModelsXmlTest.java | 9 ++----- 19 files changed, 92 insertions(+), 69 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/Side.java rename dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/{LineSideUtils.java => SideConverter.java} (74%) 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/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index cb3da968f..8a3d7f5df 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -107,14 +107,10 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public T getDynamicModel(String staticId, Class clazz, boolean defaultIfNotFound) { + public T getDynamicModel(String staticId, Class clazz) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); if (bbm == null) { - if (defaultIfNotFound) { - return networkModel.getDefaultModel(staticId, clazz); - } else { - throw new PowsyblException("Cannot find the equipment '" + staticId + "' among the dynamic models provided"); - } + return networkModel.getDefaultModel(staticId, clazz); } if (clazz.isInstance(bbm)) { return clazz.cast(bbm); @@ -154,8 +150,8 @@ public String addMacroConnector(String name1, String name2, List return macroConnectorId; } - public String addMacroConnector(String name1, String name2, String parametrizedName, List varConnections) { - String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2, parametrizedName); + 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; } 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 c8cfe5380..ad82c3dde 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -99,30 +99,29 @@ public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLSt } } - protected final void createMacroConnections(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context) { - T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); - createMacroConnections(connectedModel, varConnectionsSupplier, context); - } - - protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext 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 createMacroConnectionsWithParametrizedConnect(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); + 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 createMacroConnectionsWithParametrizedConnect(String modelStaticId, Class modelClass, boolean defaultIfNotFound, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); - createMacroConnectionsWithParametrizedConnect(connectedModel, varConnectionsSupplier, context, connectFromAttributes); + 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); } - protected void createMacroConnectionsWithParametrizedConnector(String modelStaticId, Class modelClass, boolean defaultIfNotFound, BiFunction> varConnectionsSupplier, String parametrizedName, DynaWaltzContext context) { - T connectedModel = context.getDynamicModel(modelStaticId, modelClass, defaultIfNotFound); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), parametrizedName, varConnectionsSupplier.apply(connectedModel, parametrizedName)); + /** + * 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/MacroConnectAttribute.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java index 0377f60aa..3d636107f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnectAttribute.java @@ -1,8 +1,9 @@ /** - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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; 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 be364ac8a..e20c4b249 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/MacroConnector.java @@ -31,8 +31,8 @@ public static String createMacroConnectorId(String name1, String name2) { return MACRO_CONNECTOR_PREFIX + name1 + "-" + name2; } - public static String createMacroConnectorId(String name1, String name2, String parametrizedName) { - return MACRO_CONNECTOR_PREFIX + name1 + parametrizedName + "-" + 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/OmegaRef.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java index 6e9d57df8..a9f8b0f9e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/OmegaRef.java @@ -87,8 +87,8 @@ private List getVarConnectionsWithBus(BusModel connected) { public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { int index = 0; for (OmegaRefGeneratorModel gen : omegaRefGenerators) { - createMacroConnectionsWithParametrizedConnect(gen, this::getVarConnectionsWithOmegaRefGenerator, context, MacroConnectAttribute.ofIndex1(index)); - createMacroConnectionsWithParametrizedConnect(getBusAssociatedTo(gen, context), BusModel.class, true, this::getVarConnectionsWithBus, context, MacroConnectAttribute.ofIndex1(index)); + createMacroConnections(gen, getVarConnectionsWithOmegaRefGenerator(gen), context, MacroConnectAttribute.ofIndex1(index)); + createMacroConnections(getBusAssociatedTo(gen, context), BusModel.class, this::getVarConnectionsWithBus, context, MacroConnectAttribute.ofIndex1(index)); 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 c7f8bf833..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 @@ -8,10 +8,9 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +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; @@ -23,10 +22,10 @@ */ 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,12 +38,12 @@ public String getLib() { @Override public void createMacroConnections(DynaWaltzContext context) { - createMacroConnectionsWithParametrizedConnector(lineStaticId, LineModel.class, true, this::getVarConnectionsWithLine, LineSideUtils.getSuffix(side), context); + createMacroConnections(lineStaticId, LineModel.class, this::getVarConnectionsWithLine, context, side); } - private List getVarConnectionsWithLine(LineModel connected, String suffix) { + private List getVarConnectionsWithLine(LineModel connected, Side side) { return Arrays.asList( - new VarConnection("currentLimitAutomaton_IMonitored", connected.getIVarName(suffix)), + 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/events/AbstractEventModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEventModel.java index e55179a4c..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 @@ -63,7 +63,8 @@ public void writeParameters(XMLStreamWriter writer, DynaWaltzContext context) th protected abstract void writeEventSpecificParameters(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException; @Override - protected void createMacroConnections(T connectedModel, Function> varConnectionsSupplier, DynaWaltzContext context) { + 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 1ac343ee3..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 @@ -44,7 +44,7 @@ private List getVarConnectionsWithLine(LineModel connected) { @Override public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipmentStaticId(), LineModel.class, true, this::getVarConnectionsWithLine, 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 ed6b7554e..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 @@ -41,7 +41,7 @@ private List getVarConnectionsWithGenerator(GeneratorModel connec @Override public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipmentStaticId(), GeneratorModel.class, false, this::getVarConnectionsWithGenerator, 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 fa6bc34f9..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 @@ -60,7 +60,7 @@ public void createMacroConnections(DynaWaltzContext context) { if (generator == null) { throw new PowsyblException("Generator static id unknown: " + staticId); } - createMacroConnections(BusUtils.getConnectableBusStaticId(generator), BusModel.class, true, this::getVarConnectionsWithBus, context); + createMacroConnections(BusUtils.getConnectableBusStaticId(generator), BusModel.class, this::getVarConnectionsWithBus, context); } private List getVarConnectionsWithBus(BusModel connected) { @@ -94,9 +94,4 @@ public String getSwitchOffSignalAutomatonVarName() { public String getRunningVarName() { return runningVarName; } - - @Override - public String getQStatorPuVarName() { - return "generator_QStatorPu"; - } } 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 78a992c5d..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,6 +7,7 @@ package com.powsybl.dynawaltz.models.lines; import com.powsybl.dynawaltz.models.AbstractNetworkModel; +import com.powsybl.dynawaltz.models.Side; /** * @author Florian Dupuy @@ -23,8 +24,8 @@ public String getName() { } @Override - public String getIVarName(String sideSuffix) { - return "@NAME@_i" + sideSuffix; + 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 0032c3cbb..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,12 +7,13 @@ package com.powsybl.dynawaltz.models.lines; import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.Side; /** * @author Florian Dupuy */ public interface LineModel extends Model { - String getIVarName(String sideSuffix); + 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 389a7b699..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 @@ -10,10 +10,11 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractBlackBoxModel; +import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.BusModel; import com.powsybl.dynawaltz.models.utils.BusUtils; -import com.powsybl.dynawaltz.models.utils.LineSideUtils; +import com.powsybl.dynawaltz.models.utils.SideConverter; import com.powsybl.iidm.network.Line; import java.util.Arrays; @@ -33,9 +34,9 @@ public String getLib() { return "Line"; } - private List getVarConnectionsWithBus(BusModel connected, String suffix) { + private List getVarConnectionsWithBus(BusModel connected, Side side) { return Arrays.asList( - new VarConnection(getIVarName(suffix), connected.getNumCCVarName()), + new VarConnection(getIVarName(side), connected.getNumCCVarName()), new VarConnection(getStateVarName(), connected.getTerminalVarName()) ); } @@ -49,7 +50,7 @@ public void createMacroConnections(DynaWaltzContext context) { } line.getTerminals().forEach(t -> { String busStaticId = BusUtils.getConnectableBusStaticId(t); - createMacroConnectionsWithParametrizedConnector(busStaticId, BusModel.class, true, this::getVarConnectionsWithBus, LineSideUtils.getSuffix(line.getSide(t)), context); + createMacroConnections(busStaticId, BusModel.class, this::getVarConnectionsWithBus, context, SideConverter.convert(line.getSide(t))); }); } @@ -59,8 +60,8 @@ public String getName() { } @Override - public String getIVarName(String sideSuffix) { - return getDynamicModelId() + sideSuffix; + 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 ef9d3d44f..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 @@ -34,7 +34,7 @@ public void createMacroConnections(DynaWaltzContext context) { if (load == null) { throw new PowsyblException("Load static id unknown: " + staticId); } - createMacroConnections(BusUtils.getConnectableBusStaticId(load), BusModel.class, true, this::getVarConnectionsWithBus, context); + 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/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/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynaWaltzTestUtil.java index edc9c8830..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,6 +10,7 @@ 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; @@ -20,7 +21,10 @@ 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; @@ -110,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 { 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 676c42d34..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,7 +11,6 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; import com.powsybl.dynawaltz.models.generators.GeneratorSynchronousModel; import com.powsybl.dynawaltz.models.lines.LineModel; import org.junit.jupiter.api.Test; @@ -74,16 +73,12 @@ void duplicateStaticId() { void testDynamicModelGetterException() { DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); - // throws exception id not found - Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("wrongID", GeneratorModel.class, false)); - assertEquals("Cannot find the equipment 'wrongID' among the dynamic models provided", e.getMessage()); - // incorrect model - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("GEN5", LineModel.class, true)); + 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()); // default model not implemented - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("unknownID", GeneratorSynchronousModel.class, true)); + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("unknownID", GeneratorSynchronousModel.class)); assertEquals("Default model not implemented for GeneratorSynchronousModel", e.getMessage()); } }