From 32f95352d779ac71fa73d9a5331e1eef4238cca5 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 24 Oct 2023 16:49:02 +0200 Subject: [PATCH 01/13] Refactor default model factory Use the default model factory for EquipmentConnectionPoint Use MacroConnectionsAdder instead of AbstractBlackBoxModel methods for macro connections creation Signed-off-by: lisrte --- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 12 +- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 12 +- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 12 +- .../dynawaltz-inputs/powsybl_dynawaltz.dyd | 16 +- .../powsybl/dynawaltz/DynaWaltzContext.java | 67 +---- .../models/AbstractBlackBoxModel.java | 115 +-------- .../dynawaltz/models/BlackBoxModel.java | 6 +- .../automatons/CurrentLimitAutomaton.java | 8 +- .../CurrentLimitTwoLevelsAutomaton.java | 10 +- .../automatons/TapChangerAutomaton.java | 9 +- .../TapChangerBlockingAutomaton.java | 21 +- .../automatons/UnderVoltageAutomaton.java | 6 +- .../AbstractPhaseShifterAutomaton.java | 6 +- .../dynawaltz/models/buses/AbstractBus.java | 16 +- .../DefaultBusOfFrequencySynchronized.java | 19 +- .../DefaultEquipmentConnectionPoint.java | 2 +- .../DefaultModelConfiguration.java | 82 +++++++ .../defaultmodels/DefaultModelFactory.java | 2 +- .../DefaultModelFactoryInterface.java | 10 + .../defaultmodels/DefaultModelsHandler.java | 99 +++----- .../events/EventActivePowerVariation.java | 9 +- .../models/events/EventHvdcDisconnection.java | 10 +- .../events/EventInjectionDisconnection.java | 6 +- .../events/EventQuadripoleDisconnection.java | 5 +- .../models/events/NodeFaultEvent.java | 5 +- .../FrequencySynchronizedModel.java | 3 + .../frequencysynchronizers/OmegaRef.java | 13 +- .../frequencysynchronizers/SetPoint.java | 5 +- .../models/generators/AbstractGenerator.java | 6 +- .../generators/GridFormingConverter.java | 13 +- .../generators/SynchronizedGenerator.java | 7 + .../generators/SynchronizedWeccGen.java | 7 + .../dynawaltz/models/generators/WeccGen.java | 6 +- .../dynawaltz/models/hvdc/AbstractHvdc.java | 8 +- .../dynawaltz/models/hvdc/HvdcPDangling.java | 6 +- .../models/hvdc/HvdcVscDangling.java | 6 +- .../dynawaltz/models/lines/StandardLine.java | 6 +- .../dynawaltz/models/loads/AbstractLoad.java | 6 +- .../MacroConnectionsAdder.java | 229 ++++++++++++++++++ .../models/svarcs/StaticVarCompensator.java | 6 +- .../transformers/TransformerFixedRatio.java | 6 +- .../dynawaltz/models/utils/BusUtils.java | 8 +- .../dynawaltz/xml/DynamicModelsXmlTest.java | 20 +- dynawaltz/src/test/resources/apv_dyd.xml | 6 +- dynawaltz/src/test/resources/dyd.xml | 16 +- dynawaltz/src/test/resources/events.xml | 4 +- .../resources/grid_forming_converter_dyd.xml | 4 +- .../src/test/resources/omega_ref_dyd.xml | 8 +- dynawaltz/src/test/resources/wecc_pv_dyd.xml | 4 +- .../test/resources/wecc_wt_synchro_dyd.xml | 4 +- 50 files changed, 560 insertions(+), 412 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.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 31f7aa04c..f778cd73f 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 @@ -76,7 +76,7 @@ - + @@ -123,14 +123,14 @@ - + - + - + - + - + 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 5ec4dc0b3..0fdd5547b 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 @@ -67,7 +67,7 @@ - + @@ -110,14 +110,14 @@ - + - + - + - + - + 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 c7d4d570f..7f3824975 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,7 +66,7 @@ - + @@ -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 e8dcc5bb7..2ac622b35 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 @@ -32,7 +32,7 @@ - + @@ -51,18 +51,18 @@ - + - + - + - + - + - + - + diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 608d940ee..d4f5c1063 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -11,15 +11,13 @@ import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.models.*; import com.powsybl.dynawaltz.models.buses.AbstractBus; -import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; -import com.powsybl.dynawaltz.models.buses.DefaultEquipmentConnectionPoint; import com.powsybl.dynawaltz.models.defaultmodels.DefaultModelsHandler; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizerModel; import com.powsybl.dynawaltz.models.frequencysynchronizers.OmegaRef; import com.powsybl.dynawaltz.models.frequencysynchronizers.SetPoint; import com.powsybl.dynawaltz.models.macroconnections.MacroConnect; -import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.macroconnections.MacroConnector; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.dynawaltz.xml.MacroStaticReference; @@ -58,6 +56,7 @@ public class DynaWaltzContext { private final DefaultModelsHandler defaultModelsHandler = new DefaultModelsHandler(); private final FrequencySynchronizerModel frequencySynchronizer; private final List dynamicModelsParameters = new ArrayList<>(); + private final MacroConnectionsAdder macroConnectionsAdder; public DynaWaltzContext(Network network, String workingVariantId, List dynamicModels, List eventModels, List curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters) { @@ -77,16 +76,21 @@ public DynaWaltzContext(Network network, String workingVariantId, List new MacroStaticReference(k, bbm.getVarsMapping())); - bbm.createMacroConnections(this); + bbm.createMacroConnections(macroConnectionsAdder); bbm.createDynamicModelParameters(this, dynamicModelsParameters::add); } ParametersSet networkParameters = getDynaWaltzParameters().getNetworkParameters(); for (BlackBoxModel bbem : eventModels) { - bbem.createMacroConnections(this); + bbem.createMacroConnections(macroConnectionsAdder); bbem.createDynamicModelParameters(this, dynamicModelsParameters::add); bbem.createNetworkParameter(networkParameters); } @@ -119,21 +123,6 @@ public Collection getMacroStaticReferences() { return macroStaticReferences.values(); } - public T getDynamicModel(String staticId, Class clazz) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - return defaultModelsHandler.getDefaultModel(staticId, clazz); - } - if (clazz.isInstance(bbm)) { - return clazz.cast(bbm); - } - throw new PowsyblException(String.format(MODEL_ID_EXCEPTION, staticId, clazz.getSimpleName())); - } - - public T getDynamicModel(Identifiable equipment, Class connectableClass) { - return getDynamicModel(equipment, connectableClass, true); - } - public T getDynamicModel(Identifiable equipment, Class connectableClass, boolean throwException) { BlackBoxModel bbm = staticIdBlackBoxModelMap.get(equipment.getId()); if (bbm == null) { @@ -153,6 +142,7 @@ public T getDynamicModel(Identifiable equipment, Class c public T getPureDynamicModel(String dynamicId, Class connectableClass, boolean throwException) { BlackBoxModel bbm = dynamicModels.stream() .filter(dm -> dynamicId.equals(dm.getDynamicModelId())) + .filter(AbstractPureDynamicBlackBoxModel.class::isInstance) .findFirst().orElse(null); if (bbm == null) { if (throwException) { @@ -173,17 +163,6 @@ public T getPureDynamicModel(String dynamicId, Class connec } } - public EquipmentConnectionPoint getConnectionPointDynamicModel(String staticId) { - BlackBoxModel bbm = staticIdBlackBoxModelMap.get(staticId); - if (bbm == null) { - return DefaultEquipmentConnectionPoint.getInstance(); - } - if (bbm instanceof EquipmentConnectionPoint cp) { - return cp; - } - throw new PowsyblException(String.format(MODEL_ID_EXCEPTION, staticId, "ConnectionPoint")); - } - protected static Predicate distinctByStaticId() { Set seen = new HashSet<>(); return bbm -> { @@ -206,40 +185,14 @@ protected static Predicate distinctByDynamicId() { }; } - public void addMacroConnect(String macroConnectorId, List attributesFrom, List attributesTo) { - macroConnectList.add(new MacroConnect(macroConnectorId, attributesFrom, attributesTo)); - } - public List getMacroConnectList() { return macroConnectList; } - 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, Side side, List varConnections) { - String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2, side); - macroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); - return macroConnectorId; - } - - public String addMacroConnector(String name1, String name2, String name1Suffix, List varConnections) { - String macroConnectorId = MacroConnector.createMacroConnectorId(name1, name2, name1Suffix); - macroConnectorsMap.computeIfAbsent(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); - return macroConnectorId; - } - public Collection getMacroConnectors() { return macroConnectorsMap.values(); } - public boolean isWithoutBlackBoxDynamicModel(String staticId) { - return !staticIdBlackBoxModelMap.containsKey(staticId); - } - public boolean isWithoutBlackBoxDynamicModel(Identifiable equipment) { return !staticIdBlackBoxModelMap.containsKey(equipment.getId()); } 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 c5a62731c..055f15feb 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/AbstractBlackBoxModel.java @@ -9,25 +9,14 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; -import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionSuffix; import com.powsybl.dynawaltz.parameters.ParametersSet; -import com.powsybl.iidm.network.Identifiable; -import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; -import com.powsybl.dynawaltz.models.utils.BusUtils; -import com.powsybl.dynawaltz.models.utils.SideConverter; -import com.powsybl.iidm.network.*; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.function.BiFunction; import java.util.function.Consumer; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; /** * @author Luma ZamarreƱo @@ -66,7 +55,8 @@ public void createNetworkParameter(ParametersSet networkParameters) { // method empty by default to be redefined by specific models } - protected List getMacroConnectFromAttributes() { + @Override + public List getMacroConnectFromAttributes() { return List.of(MacroConnectAttribute.of("id1", getDynamicModelId())); } @@ -91,105 +81,4 @@ protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext c writer.writeAttribute("parFile", getParFile(context)); writer.writeAttribute("parId", getParameterSetId()); } - - protected void createMacroConnections(String modelStaticId, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context) { - T connectedModel = context.getDynamicModel(modelStaticId, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected final void createMacroConnections(T connectedModel, List varConnections, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnections); - List fromAttributes = Stream.concat(getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); - context.addMacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); - } - - protected final void createMacroConnections(String modelStaticId, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - T connectedModel = context.getDynamicModel(modelStaticId, modelClass); - createMacroConnections(connectedModel, varConnectionsSupplier.apply(connectedModel), context, connectFromAttributes); - } - - protected final void createMacroConnections(Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - createMacroConnections(connectedModel, varConnectionsSupplier.apply(connectedModel), context, connectFromAttributes); - } - - protected final boolean createMacroConnectionsOrSkip(Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context, MacroConnectAttribute... connectFromAttributes) { - T connectedModel = context.getDynamicModel(equipment, modelClass, false); - if (connectedModel != null) { - createMacroConnections(connectedModel, varConnectionsSupplier.apply(connectedModel), context, connectFromAttributes); - return false; - } - return true; - } - - protected void createMacroConnections(Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected boolean createMacroConnectionsOrSkip(Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context) { - T connectedModel = context.getDynamicModel(equipment, modelClass, false); - if (connectedModel != null) { - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - return false; - } - return true; - } - - /** - * 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()); - } - - protected final void createMacroConnections(Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, DynaWaltzContext context, Side side) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), side, varConnectionsSupplier.apply(connectedModel, side)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - /** - * Suffixes MacroConnector id with string - */ - protected void createMacroConnections(Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, DynaWaltzContext context, String parametrizedName) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), parametrizedName, varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected void createMacroConnections(Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, DynaWaltzContext context, MacroConnectionSuffix suffix) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), suffix.getIdSuffix(), varConnectionsSupplier.apply(connectedModel, suffix.getConnectionSuffix())); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected final void createMacroConnections(Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, DynaWaltzContext context, String parametrizedName, MacroConnectAttribute... connectFromAttributes) { - T connectedModel = context.getDynamicModel(equipment, modelClass); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), parametrizedName, varConnectionsSupplier.apply(connectedModel, parametrizedName)); - List fromAttributes = Stream.concat(getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); - context.addMacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); - } - - protected void createTerminalMacroConnections(Injection equipment, Function> varConnectionsSupplier, DynaWaltzContext context) { - EquipmentConnectionPoint connectedModel = context.getConnectionPointDynamicModel(BusUtils.getConnectableBusStaticId(equipment)); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), varConnectionsSupplier.apply(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected final void createTerminalMacroConnections(Terminal terminal, BiFunction> varConnectionsSupplier, DynaWaltzContext context, Side side) { - EquipmentConnectionPoint connectedModel = context.getConnectionPointDynamicModel(BusUtils.getConnectableBusStaticId(terminal)); - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), side, varConnectionsSupplier.apply(connectedModel, side)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - } - - protected final void createTerminalMacroConnections(HvdcLine hvdc, BiFunction> varConnectionsSupplier, DynaWaltzContext context, Side side) { - HvdcConverterStation station = hvdc.getConverterStation(SideConverter.convert(side)); - createTerminalMacroConnections(station.getTerminal(), varConnectionsSupplier, context, side); - } } 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 edc05fc0a..bca97511f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/BlackBoxModel.java @@ -7,6 +7,8 @@ package com.powsybl.dynawaltz.models; import com.powsybl.dynawaltz.DynaWaltzContext; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import javax.xml.stream.XMLStreamException; @@ -27,7 +29,9 @@ public interface BlackBoxModel extends Model { List getVarsMapping(); - void createMacroConnections(DynaWaltzContext context); + void createMacroConnections(MacroConnectionsAdder adder); + + List getMacroConnectFromAttributes(); String getParFile(DynaWaltzContext context); 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 ea01b015c..d34f4b020 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,10 @@ */ package com.powsybl.dynawaltz.models.automatons; -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.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Branch; import java.util.Arrays; @@ -48,9 +48,9 @@ public String getLib() { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithMeasuredQuadripole, context, MEASURE_SUFFIX + measuredSide.getSideSuffix()); - createMacroConnections(controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, context, CONTROL_SUFFIX); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithMeasuredQuadripole, MEASURE_SUFFIX + measuredSide.getSideSuffix()); + adder.createMacroConnections(this, controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, CONTROL_SUFFIX); } protected List getVarConnectionsWithMeasuredQuadripole(QuadripoleModel connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java index e0a16edd7..dd15be108 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java @@ -7,9 +7,9 @@ */ package com.powsybl.dynawaltz.models.automatons; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Branch; import java.util.Arrays; @@ -34,10 +34,10 @@ public CurrentLimitTwoLevelsAutomaton(String dynamicModelId, String parameterSet } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithMeasuredQuadripole, context, FIRST_MEASURE_SUFFIX + measuredSide.getSideSuffix()); - createMacroConnections(secondMeasuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithSecondMeasuredQuadripole, context, SECOND_MEASURE_SUFFIX + secondMeasuredSide.getSideSuffix()); - createMacroConnections(controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, context, CONTROL_SUFFIX); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithMeasuredQuadripole, FIRST_MEASURE_SUFFIX + measuredSide.getSideSuffix()); + adder.createMacroConnections(this, secondMeasuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithSecondMeasuredQuadripole, SECOND_MEASURE_SUFFIX + secondMeasuredSide.getSideSuffix()); + adder.createMacroConnections(this, controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, CONTROL_SUFFIX); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java index ee986c2bc..992153c07 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java @@ -12,6 +12,7 @@ import com.powsybl.dynawaltz.models.TransformerSide; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.loads.LoadWithTransformers; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.transformers.TapChangerModel; import com.powsybl.iidm.network.Load; import org.slf4j.Logger; @@ -61,9 +62,9 @@ public String getLib() { } @Override - public void createMacroConnections(DynaWaltzContext context) { + public void createMacroConnections(MacroConnectionsAdder adder) { if (ConnectionState.NOT_SET == connection) { - boolean isSkipped = createMacroConnectionsOrSkip(load, LoadWithTransformers.class, this::getVarConnectionsWith, context); + boolean isSkipped = adder.createMacroConnectionsOrSkip(this, load, LoadWithTransformers.class, this::getVarConnectionsWith); if (isSkipped) { connection = ConnectionState.NOT_CONNECTED; LOGGER.warn("TapChangerAutomaton {} load does not possess a transformer, the automaton will be skipped", getDynamicModelId()); @@ -89,9 +90,9 @@ public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLSt } } - public boolean isConnected(DynaWaltzContext context) { + public boolean isConnected(MacroConnectionsAdder adder) { if (ConnectionState.NOT_SET == connection) { - createMacroConnections(context); + createMacroConnections(adder); } return ConnectionState.CONNECTED == connection; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java index ee1a9a9e0..dfd442547 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java @@ -13,6 +13,7 @@ import com.powsybl.dynawaltz.models.MeasurementPointSuffix; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.ActionConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.transformers.TapChangerModel; import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.IdentifiableType; @@ -80,26 +81,26 @@ public String getLib() { } @Override - public void createMacroConnections(DynaWaltzContext context) { + public void createMacroConnections(MacroConnectionsAdder adder) { for (TwoWindingsTransformer transformer : transformers) { - createMacroConnections(transformer, TapChangerModel.class, this::getVarConnectionsWith, context); + adder.createMacroConnections(this, transformer, TapChangerModel.class, this::getVarConnectionsWith); } int skippedTapChangers = 0; for (Load load : loadsWithTransformer) { - boolean isSkipped = createMacroConnectionsOrSkip(load, TapChangerModel.class, this::getVarConnectionsWith, context); + boolean isSkipped = adder.createMacroConnectionsOrSkip(this, load, TapChangerModel.class, this::getVarConnectionsWith); if (isSkipped) { skippedTapChangers++; } } for (String id : tapChangerAutomatonIds) { - if (createTcaMacroConnectionsOrSkip(id, context)) { + if (adder.createTcaMacroConnectionsOrSkip(this, id, this::getVarConnectionsWith)) { skippedTapChangers++; } } if (!transformers.isEmpty() || skippedTapChangers < (loadsWithTransformer.size() + tapChangerAutomatonIds.size())) { int i = 1; for (Bus bus : uMeasurements) { - createMacroConnections(bus, ActionConnectionPoint.class, this::getVarConnectionsWith, context, MeasurementPointSuffix.of(i)); + adder.createMacroConnections(this, bus, ActionConnectionPoint.class, this::getVarConnectionsWith, MeasurementPointSuffix.of(i)); i++; } } else { @@ -118,16 +119,6 @@ private List getVarConnectionsWith(ActionConnectionPoint connecte .orElse(Collections.emptyList()); } - private boolean createTcaMacroConnectionsOrSkip(String dynamicId, DynaWaltzContext context) { - TapChangerAutomaton connectedModel = context.getPureDynamicModel(dynamicId, TapChangerAutomaton.class, false); - if (connectedModel != null && connectedModel.isConnected(context)) { - String macroConnectorId = context.addMacroConnector(getName(), connectedModel.getName(), getVarConnectionsWith(connectedModel)); - context.addMacroConnect(macroConnectorId, getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); - return false; - } - return true; - } - @Override public void write(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { if (isConnected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/UnderVoltageAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/UnderVoltageAutomaton.java index a25ee77b4..ad999aded 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/UnderVoltageAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/UnderVoltageAutomaton.java @@ -7,10 +7,10 @@ */ package com.powsybl.dynawaltz.models.automatons; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.generators.GeneratorModel; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Generator; import java.util.Arrays; @@ -35,8 +35,8 @@ public String getLib() { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(generator, GeneratorModel.class, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, generator, GeneratorModel.class, this::getVarConnectionsWith); } protected List getVarConnectionsWith(GeneratorModel connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/phaseshifters/AbstractPhaseShifterAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/phaseshifters/AbstractPhaseShifterAutomaton.java index c0f6f2da6..8fe57ea80 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/phaseshifters/AbstractPhaseShifterAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/phaseshifters/AbstractPhaseShifterAutomaton.java @@ -7,9 +7,9 @@ */ package com.powsybl.dynawaltz.models.automatons.phaseshifters; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.transformers.TransformerModel; import com.powsybl.iidm.network.TwoWindingsTransformer; @@ -29,8 +29,8 @@ protected AbstractPhaseShifterAutomaton(String dynamicModelId, TwoWindingsTransf } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(transformer, TransformerModel.class, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, transformer, TransformerModel.class, this::getVarConnectionsWith); } protected abstract List getVarConnectionsWith(TransformerModel connected); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java index 1f56d21d6..53f670670 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java @@ -8,10 +8,11 @@ package com.powsybl.dynawaltz.models.buses; import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Terminal; import java.util.ArrayList; import java.util.List; @@ -33,15 +34,14 @@ public List getMacroConnectToAttributes() { return attributesConnectTo; } - @Override - public void createMacroConnections(DynaWaltzContext context) { - checkLinkedDynamicModels(equipment, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + checkLinkedDynamicModels(adder); } - private void checkLinkedDynamicModels(Bus bus, DynaWaltzContext context) { - bus.getConnectedTerminalStream() - .map(t -> t.getConnectable().getId()) - .filter(context::isWithoutBlackBoxDynamicModel) + private void checkLinkedDynamicModels(MacroConnectionsAdder adder) { + equipment.getConnectedTerminalStream() + .map(Terminal::getConnectable) + .filter(adder::isWithoutBlackBoxDynamicModel) .findAny() .ifPresent(id -> { throw new PowsyblException(String.format("The equipment %s linked to the %s %s does not possess a dynamic model", diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultBusOfFrequencySynchronized.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultBusOfFrequencySynchronized.java index ed59655c8..0961168b5 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultBusOfFrequencySynchronized.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultBusOfFrequencySynchronized.java @@ -1,19 +1,34 @@ package com.powsybl.dynawaltz.models.buses; import com.powsybl.dynawaltz.models.defaultmodels.AbstractDefaultModel; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; +import com.powsybl.dynawaltz.xml.DynaWaltzXmlConstants; + +import java.util.List; public class DefaultBusOfFrequencySynchronized extends AbstractDefaultModel implements BusOfFrequencySynchronizedModel { - public DefaultBusOfFrequencySynchronized(String staticId) { + private final String frequencySynchronizedStaticId; + + public DefaultBusOfFrequencySynchronized(String staticId, String frequencySynchronizedStaticId) { super(staticId); + this.frequencySynchronizedStaticId = frequencySynchronizedStaticId; } @Override public String getName() { - return "DefaultBusOfEquipment"; + return "DefaultBusOfFrequencySynchronized"; } public String getNumCCVarName() { return "@@NAME@@@NODE@_numcc"; } + + @Override + public List getMacroConnectToAttributes() { + return List.of( + MacroConnectAttribute.of("id2", DynaWaltzXmlConstants.NETWORK), + MacroConnectAttribute.of("name2", frequencySynchronizedStaticId) + ); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java index 1da6f4c9c..67f25404f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java @@ -27,7 +27,7 @@ public final class DefaultEquipmentConnectionPoint implements EquipmentConnectio private DefaultEquipmentConnectionPoint() { } - public static DefaultEquipmentConnectionPoint getInstance() { + public static DefaultEquipmentConnectionPoint getDefaultModel(String staticId) { return INSTANCE; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java new file mode 100644 index 000000000..a30f70188 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java @@ -0,0 +1,82 @@ +/** + * 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.defaultmodels; + +import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.buses.*; +import com.powsybl.dynawaltz.models.generators.DefaultGenerator; +import com.powsybl.dynawaltz.models.generators.GeneratorModel; +import com.powsybl.dynawaltz.models.hvdc.DefaultHvdc; +import com.powsybl.dynawaltz.models.hvdc.HvdcModel; +import com.powsybl.dynawaltz.models.lines.DefaultLine; +import com.powsybl.dynawaltz.models.lines.LineModel; +import com.powsybl.dynawaltz.models.loads.DefaultLoad; +import com.powsybl.dynawaltz.models.loads.LoadModel; +import com.powsybl.dynawaltz.models.shunts.DefaultShunt; +import com.powsybl.dynawaltz.models.shunts.ShuntModel; +import com.powsybl.dynawaltz.models.svarcs.DefaultStaticVarCompensator; +import com.powsybl.dynawaltz.models.svarcs.StaticVarCompensatorModel; +import com.powsybl.dynawaltz.models.transformers.DefaultTransformer; +import com.powsybl.dynawaltz.models.transformers.TransformerModel; +import com.powsybl.iidm.network.IdentifiableType; + +/** + * @author Laurent Issertial + */ +public enum DefaultModelConfiguration { + + ACTION_CONNECTION_POINT(IdentifiableType.BUS, + ActionConnectionPoint.class, + new DefaultModelFactory(DefaultActionConnectionPoint::new)), + EQUIPMENT_CONNECTION_POINT(IdentifiableType.BUS, + EquipmentConnectionPoint.class, + DefaultEquipmentConnectionPoint::getDefaultModel), + GENERATOR(IdentifiableType.GENERATOR, + GeneratorModel.class, + new DefaultModelFactory(DefaultGenerator::new)), + HVDC(IdentifiableType.HVDC_LINE, + HvdcModel.class, + new DefaultModelFactory(DefaultHvdc::new)), + LINE(IdentifiableType.LINE, + LineModel.class, + new DefaultModelFactory(DefaultLine::new)), + LOAD(IdentifiableType.LOAD, + LoadModel.class, + new DefaultModelFactory(DefaultLoad::new)), + SHUNT_COMPENSATOR(IdentifiableType.SHUNT_COMPENSATOR, + ShuntModel.class, + new DefaultModelFactory(DefaultShunt::new)), + STATIC_VAR_COMPENSATOR(IdentifiableType.STATIC_VAR_COMPENSATOR, + StaticVarCompensatorModel.class, + new DefaultModelFactory(DefaultStaticVarCompensator::new)), + TWO_WINDINGS_TRANSFORMER(IdentifiableType.TWO_WINDINGS_TRANSFORMER, + TransformerModel.class, + new DefaultModelFactory(DefaultTransformer::new)); + + private final IdentifiableType identifiableType; + private final Class equipmentClass; + private final DefaultModelFactoryInterface factory; + + DefaultModelConfiguration(IdentifiableType identifiableType, Class equipmentClass, DefaultModelFactoryInterface factory) { + this.identifiableType = identifiableType; + this.equipmentClass = equipmentClass; + this.factory = factory; + } + + public IdentifiableType getIdentifiableType() { + return identifiableType; + } + + public Class getEquipmentClass() { + return equipmentClass; + } + + public Model getDefaultModel(String staticId) { + return factory.getDefaultModel(staticId); + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactory.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactory.java index 584f7f14e..e9692e170 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactory.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactory.java @@ -14,7 +14,7 @@ /** * @author Laurent Issertial */ -public class DefaultModelFactory { +public class DefaultModelFactory implements DefaultModelFactoryInterface { private final Map defaultModelMap = new HashMap<>(); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java new file mode 100644 index 000000000..740914011 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java @@ -0,0 +1,10 @@ +package com.powsybl.dynawaltz.models.defaultmodels; + +/** + * @author Laurent Issertial + */ +public interface DefaultModelFactoryInterface { + + T getDefaultModel(String staticId); + +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java index 442178885..123deee51 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java @@ -8,29 +8,14 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.models.Model; -import com.powsybl.dynawaltz.models.buses.DefaultActionConnectionPoint; -import com.powsybl.dynawaltz.models.buses.ActionConnectionPoint; -import com.powsybl.dynawaltz.models.generators.GeneratorModel; -import com.powsybl.dynawaltz.models.generators.DefaultGenerator; -import com.powsybl.dynawaltz.models.hvdc.DefaultHvdc; -import com.powsybl.dynawaltz.models.hvdc.HvdcModel; -import com.powsybl.dynawaltz.models.lines.DefaultLine; -import com.powsybl.dynawaltz.models.lines.LineModel; -import com.powsybl.dynawaltz.models.loads.DefaultLoad; -import com.powsybl.dynawaltz.models.loads.LoadModel; -import com.powsybl.dynawaltz.models.shunts.DefaultShunt; -import com.powsybl.dynawaltz.models.shunts.ShuntModel; -import com.powsybl.dynawaltz.models.svarcs.DefaultStaticVarCompensator; -import com.powsybl.dynawaltz.models.svarcs.StaticVarCompensatorModel; -import com.powsybl.dynawaltz.models.transformers.DefaultTransformer; -import com.powsybl.dynawaltz.models.transformers.TransformerModel; import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.IdentifiableType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.EnumMap; -import java.util.Map; +import java.util.*; + +import static java.util.stream.Collectors.groupingBy; /** * @author Florian Dupuy @@ -40,67 +25,41 @@ public class DefaultModelsHandler { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultModelsHandler.class); - private final Map> powSyBlTypeToModel = new EnumMap<>(IdentifiableType.class); - private final Map, DefaultModelFactory> factoryMap; + private final Map> configurations = EnumSet.allOf(DefaultModelConfiguration.class) + .stream() + .collect(groupingBy(DefaultModelConfiguration::getIdentifiableType)); public DefaultModelsHandler() { - factoryMap = Map.of(ActionConnectionPoint.class, new DefaultModelFactory(DefaultActionConnectionPoint::new), - GeneratorModel.class, new DefaultModelFactory(DefaultGenerator::new), - HvdcModel.class, new DefaultModelFactory(DefaultHvdc::new), - LineModel.class, new DefaultModelFactory(DefaultLine::new), - LoadModel.class, new DefaultModelFactory(DefaultLoad::new), - ShuntModel.class, new DefaultModelFactory(DefaultShunt::new), - StaticVarCompensatorModel.class, new DefaultModelFactory(DefaultStaticVarCompensator::new), - TransformerModel.class, new DefaultModelFactory(DefaultTransformer::new)); - - // When using the bus equipment DefaultModelsHandler will always instantiate the DefaultActionConnectionPoint default model, - // for EquipmentConnectionPoint the DefaultBus singleton will be accessed via DynaWaltzContext::getConnectionPointDynamicModel - powSyBlTypeToModel.put(IdentifiableType.BUS, ActionConnectionPoint.class); - powSyBlTypeToModel.put(IdentifiableType.GENERATOR, GeneratorModel.class); - powSyBlTypeToModel.put(IdentifiableType.HVDC_LINE, HvdcModel.class); - powSyBlTypeToModel.put(IdentifiableType.LINE, LineModel.class); - powSyBlTypeToModel.put(IdentifiableType.LOAD, LoadModel.class); - powSyBlTypeToModel.put(IdentifiableType.SHUNT_COMPENSATOR, ShuntModel.class); - powSyBlTypeToModel.put(IdentifiableType.STATIC_VAR_COMPENSATOR, StaticVarCompensatorModel.class); - powSyBlTypeToModel.put(IdentifiableType.TWO_WINDINGS_TRANSFORMER, TransformerModel.class); - } - - 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()); - } - - public T getDefaultModel(Identifiable equipment, Class connectableClass) { - return getDefaultModel(equipment, connectableClass, true); } public T getDefaultModel(Identifiable equipment, Class connectableClass, boolean throwException) { - - Class equipmentClass = powSyBlTypeToModel.get(equipment.getType()); - if (equipmentClass == null) { - throw new PowsyblException("No dynamic model associated with " + equipment.getType()); - } - DefaultModelFactory dmf = factoryMap.get(equipmentClass); - if (dmf != null) { - Model defaultModel = dmf.getDefaultModel(equipment.getId()); - if (connectableClass.isInstance(defaultModel)) { - return connectableClass.cast(defaultModel); - } - if (throwException) { - throw new PowsyblException("Default model " + defaultModel.getClass().getSimpleName() + " does not implement " + connectableClass.getSimpleName() + " interface"); - } else { - LOGGER.warn("Default model {} does not implement {} interface", defaultModel.getClass().getSimpleName(), connectableClass.getSimpleName()); - return null; - } + DefaultModelConfiguration conf = findConfiguration(equipment.getType(), connectableClass); + Model defaultModel = conf.getDefaultModel(equipment.getId()); + if (connectableClass.isInstance(defaultModel)) { + return connectableClass.cast(defaultModel); } if (throwException) { - throw new PowsyblException("Default model not implemented for " + equipmentClass.getSimpleName()); + throw new PowsyblException("Default model " + defaultModel.getClass().getSimpleName() + " does not implement " + connectableClass.getSimpleName() + " interface"); } else { - LOGGER.warn("Default model not implemented for {}", equipmentClass.getSimpleName()); + LOGGER.warn("Default model {} does not implement {} interface", defaultModel.getClass().getSimpleName(), connectableClass.getSimpleName()); return null; } } + + private DefaultModelConfiguration findConfiguration(IdentifiableType type, Class connectableClass) { + List configurationList = configurations.get(type); + if (configurationList == null) { + throw new PowsyblException("No dynamic model associated with " + type); + } + if (configurationList.size() == 1) { + return configurationList.get(0); + } else { + for (DefaultModelConfiguration configuration : configurationList) { + if (connectableClass.isAssignableFrom(configuration.getEquipmentClass())) { + return configuration; + } + } + } + throw new PowsyblException("No default model configuration for " + type + " - " + connectableClass.getSimpleName()); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java index 265795ee8..79594d5b7 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java @@ -10,6 +10,7 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.IdentifiableType; @@ -68,11 +69,11 @@ private List getVarConnectionsWith(ControllableEquipment connecte } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipment(), + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, + getEquipment(), ControllableEquipment.class, - context.isWithoutBlackBoxDynamicModel(getEquipment()) ? this::getVarConnectionsWithDefault : this::getVarConnectionsWith, - context); + adder.isWithoutBlackBoxDynamicModel(getEquipment()) ? this::getVarConnectionsWithDefault : this::getVarConnectionsWith); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventHvdcDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventHvdcDisconnection.java index 5c7b94d86..3bbeed725 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventHvdcDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventHvdcDisconnection.java @@ -7,10 +7,10 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.hvdc.HvdcModel; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.HvdcLine; import java.util.List; @@ -43,13 +43,13 @@ private List getVarConnectionsWithHvdcModelSide(HvdcModel connect } @Override - public void createMacroConnections(DynaWaltzContext context) { + public void createMacroConnections(MacroConnectionsAdder adder) { if (disconnectOrigin && disconnectExtremity) { - createMacroConnections(getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModel, context); + adder.createMacroConnections(this, getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModel); } else if (disconnectOrigin) { - createMacroConnections(getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModelSide, context, Side.ONE); + adder.createMacroConnections(this, getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModelSide, Side.ONE); } else if (disconnectExtremity) { - createMacroConnections(getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModelSide, context, Side.TWO); + adder.createMacroConnections(this, getEquipment(), HvdcModel.class, this::getVarConnectionsWithHvdcModelSide, Side.TWO); } } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventInjectionDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventInjectionDisconnection.java index e2cebbe70..9e163941a 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventInjectionDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventInjectionDisconnection.java @@ -6,9 +6,9 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.InjectionModel; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.Load; import com.powsybl.iidm.network.ShuntCompensator; @@ -59,7 +59,7 @@ private List getVarConnectionsWith(InjectionModel connected) { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipment(), InjectionModel.class, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, getEquipment(), InjectionModel.class, this::getVarConnectionsWith); } } 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 f2f59fdc8..2056e663a 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 @@ -9,6 +9,7 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.automatons.QuadripoleModel; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Branch; @@ -47,8 +48,8 @@ private List getVarConnectionsWith(QuadripoleModel connected) { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipment(), QuadripoleModel.class, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, getEquipment(), QuadripoleModel.class, this::getVarConnectionsWith); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java index 8f4226486..ed076a653 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java @@ -10,6 +10,7 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.ActionConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Bus; @@ -42,8 +43,8 @@ public String getLib() { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createMacroConnections(getEquipment(), ActionConnectionPoint.class, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createMacroConnections(this, getEquipment(), ActionConnectionPoint.class, this::getVarConnectionsWith); } private List getVarConnectionsWith(ActionConnectionPoint connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java index d9711a178..b29450aae 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java @@ -10,6 +10,7 @@ import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.iidm.network.Bus; import java.util.List; @@ -40,4 +41,6 @@ default double getWeightGen(DynaWaltzParameters dynaWaltzParameters) { String getRunningVarName(); String getStaticId(); + + Bus getConnectesBus(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java index 8eb2d0b57..1fd6824c5 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java @@ -9,10 +9,11 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; -import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.BusOfFrequencySynchronizedModel; import com.powsybl.dynawaltz.models.buses.DefaultBusOfFrequencySynchronized; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import java.util.List; @@ -61,18 +62,18 @@ private List getVarConnectionsWith(FrequencySynchronizedModel con return connected.getOmegaRefVarConnections(); } - private List getVarConnectionsWith(BusOfFrequencySynchronizedModel connected) { + private List getVarConnectionsWithBus(BusOfFrequencySynchronizedModel connected) { return List.of(new VarConnection("numcc_node_@INDEX@", connected.getNumCCVarName())); } @Override - public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { + public void createMacroConnections(MacroConnectionsAdder adder) throws PowsyblException { int index = 0; for (FrequencySynchronizedModel eq : synchronizedEquipments) { - createMacroConnections(eq, getVarConnectionsWith(eq), context, MacroConnectAttribute.ofIndex1(index)); + adder.createMacroConnections(this, eq, getVarConnectionsWith(eq), MacroConnectAttribute.ofIndex1(index)); // If a bus with a dynamic model is found SetPoint is used in place of OmegaRef, thus at this point we don't have to handle dynamic model buses - BusOfFrequencySynchronizedModel busOf = new DefaultBusOfFrequencySynchronized(eq.getStaticId()); - createMacroConnections(busOf, getVarConnectionsWith(busOf), context, MacroConnectAttribute.ofIndex1(index)); + BusOfFrequencySynchronizedModel busOf = new DefaultBusOfFrequencySynchronized(eq.getConnectesBus().getId(), eq.getStaticId()); + adder.createMacroConnections(this, busOf, getVarConnectionsWithBus(busOf), MacroConnectAttribute.ofIndex1(index)); index++; } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/SetPoint.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/SetPoint.java index ca624dbff..d8ba12948 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/SetPoint.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/SetPoint.java @@ -10,6 +10,7 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.parameters.ParametersSet; import java.util.List; @@ -45,9 +46,9 @@ private List getVarConnectionsWith(FrequencySynchronizedModel con } @Override - public void createMacroConnections(DynaWaltzContext context) throws PowsyblException { + public void createMacroConnections(MacroConnectionsAdder adder) throws PowsyblException { for (FrequencySynchronizedModel eq : synchronizedEquipments) { - createMacroConnections(eq, getVarConnectionsWith(eq), context); + adder.createMacroConnections(this, eq, getVarConnectionsWith(eq)); } } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGenerator.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGenerator.java index 52e4205ef..6b8397b7e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGenerator.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/AbstractGenerator.java @@ -6,11 +6,11 @@ */ package com.powsybl.dynawaltz.models.generators; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Generator; import java.util.ArrayList; @@ -38,8 +38,8 @@ public List getVarsMapping() { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment.getTerminal(), this::getVarConnectionsWith); } private List getVarConnectionsWith(EquipmentConnectionPoint connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java index 833007b69..1cbfe8281 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java @@ -7,13 +7,15 @@ */ package com.powsybl.dynawaltz.models.generators; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; +import com.powsybl.dynawaltz.models.utils.BusUtils; +import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Generator; import java.util.Arrays; @@ -34,8 +36,8 @@ public GridFormingConverter(String dynamicModelId, Generator generator, String p } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment.getTerminal(), this::getVarConnectionsWith); } private List getVarConnectionsWith(EquipmentConnectionPoint connected) { @@ -74,4 +76,9 @@ public double getWeightGen(DynaWaltzParameters dynaWaltzParameters) { public List getVarsMapping() { return VAR_MAPPING; } + + @Override + public Bus getConnectesBus() { + return BusUtils.getConnectableBus(equipment); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java index 0a3ec5245..adc8bfc3e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java @@ -9,6 +9,8 @@ import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; +import com.powsybl.dynawaltz.models.utils.BusUtils; +import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Generator; import java.util.List; @@ -42,4 +44,9 @@ public List getSetPointVarConnections() { public String getOmegaRefPuVarName() { return "generator_omegaRefPu"; } + + @Override + public Bus getConnectesBus() { + return BusUtils.getConnectableBus(equipment); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java index f19c93a21..a1333fc51 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java @@ -8,6 +8,8 @@ package com.powsybl.dynawaltz.models.generators; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; +import com.powsybl.dynawaltz.models.utils.BusUtils; +import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Generator; /** @@ -28,4 +30,9 @@ public String getOmegaRefPuVarName() { public String getRunningVarName() { return weccPrefix + "_injector_running"; } + + @Override + public Bus getConnectesBus() { + return BusUtils.getConnectableBus(equipment); + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/WeccGen.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/WeccGen.java index 07365edba..12ccc22d4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/WeccGen.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/WeccGen.java @@ -7,11 +7,11 @@ */ package com.powsybl.dynawaltz.models.generators; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Generator; import java.util.List; @@ -35,8 +35,8 @@ public WeccGen(String dynamicModelId, Generator generator, String parameterSetId } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment.getTerminal(), this::getVarConnectionsWith); } private List getVarConnectionsWith(EquipmentConnectionPoint connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java index 8f83acc66..07b7ecec0 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/AbstractHvdc.java @@ -7,12 +7,12 @@ */ package com.powsybl.dynawaltz.models.hvdc; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.HvdcLine; import java.util.ArrayList; @@ -39,9 +39,9 @@ protected AbstractHvdc(String dynamicModelId, HvdcLine hvdc, String parameterSet } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context, Side.ONE); - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context, Side.TWO); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment, this::getVarConnectionsWith, Side.ONE); + adder.createTerminalMacroConnections(this, equipment, this::getVarConnectionsWith, Side.TWO); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java index e54b3648c..4dc8ab24e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcPDangling.java @@ -8,8 +8,8 @@ package com.powsybl.dynawaltz.models.hvdc; import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.utils.SideConverter; import com.powsybl.iidm.network.HvdcLine; @@ -26,10 +26,10 @@ public HvdcPDangling(String dynamicModelId, HvdcLine hvdc, String parameterSetId } @Override - public void createMacroConnections(DynaWaltzContext context) { + public void createMacroConnections(MacroConnectionsAdder adder) { danglingSide.createMacroConnections( this::getVarConnectionsWith, - (varCoSupplier, side) -> createTerminalMacroConnections(equipment.getConverterStation(SideConverter.convert(side)).getTerminal(), varCoSupplier, context, side) + (varCoSupplier, side) -> adder.createTerminalMacroConnections(this, equipment.getConverterStation(SideConverter.convert(side)).getTerminal(), varCoSupplier, side) ); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java index f5afd7099..cbca2cb75 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/hvdc/HvdcVscDangling.java @@ -8,8 +8,8 @@ package com.powsybl.dynawaltz.models.hvdc; import com.powsybl.commons.PowsyblException; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.Side; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.HvdcLine; /** @@ -25,10 +25,10 @@ public HvdcVscDangling(String dynamicModelId, HvdcLine hvdc, String parameterSet } @Override - public void createMacroConnections(DynaWaltzContext context) { + public void createMacroConnections(MacroConnectionsAdder adder) { danglingSide.createMacroConnections( this::getVarConnectionsWith, - (varCoSupplier, side) -> createTerminalMacroConnections(equipment, varCoSupplier, context, side) + (varCoSupplier, side) -> adder.createTerminalMacroConnections(this, equipment, varCoSupplier, 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 47d6d09de..70dc5c79e 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,11 +7,11 @@ */ package com.powsybl.dynawaltz.models.lines; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.utils.SideConverter; import com.powsybl.iidm.network.Line; @@ -35,8 +35,8 @@ private String getTerminalVarName(Side side) { } @Override - public void createMacroConnections(DynaWaltzContext context) { - equipment.getTerminals().forEach(t -> createTerminalMacroConnections(t, this::getVarConnectionsWith, context, SideConverter.convert(equipment.getSide(t)))); + public void createMacroConnections(MacroConnectionsAdder adder) { + equipment.getTerminals().forEach(t -> adder.createTerminalMacroConnections(this, t, this::getVarConnectionsWith, SideConverter.convert(equipment.getSide(t)))); } @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 68b157657..50f8fa82d 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 @@ -6,10 +6,10 @@ */ package com.powsybl.dynawaltz.models.loads; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Load; import java.util.List; @@ -32,8 +32,8 @@ protected String getTerminalVarName() { } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment.getTerminal(), this::getVarConnectionsWith); } abstract List getVarConnectionsWith(EquipmentConnectionPoint connected); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java new file mode 100644 index 000000000..e82dfd3a1 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java @@ -0,0 +1,229 @@ +/** + * 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.macroconnections; + +import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.Side; +import com.powsybl.dynawaltz.models.VarConnection; +import com.powsybl.dynawaltz.models.automatons.TapChangerAutomaton; +import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.utils.BusUtils; +import com.powsybl.dynawaltz.models.utils.SideConverter; +import com.powsybl.iidm.network.HvdcConverterStation; +import com.powsybl.iidm.network.HvdcLine; +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.Terminal; + +import java.util.Arrays; +import java.util.List; +import java.util.function.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * @author Laurent Issertial + */ +public final class MacroConnectionsAdder { + + @FunctionalInterface + public interface DynamicModelGetter { + T getDynamicModel(Identifiable equipment, Class connectableClass, boolean throwException); + } + + @FunctionalInterface + public interface PureDynamicModelGetter { + T getPureDynamicModel(String dynamicId, Class connectableClass, boolean throwException); + } + + private final DynamicModelGetter dynamicModelGetter; + private final PureDynamicModelGetter pureDynamicModelGetter; + private final Predicate> dynamicModelPredicate; + private Consumer macroConnectAdder; + private BiConsumer> macroConnectorAdder; + + public MacroConnectionsAdder(DynamicModelGetter dynamicModelGetter, PureDynamicModelGetter pureDynamicModelGetter, + Predicate> dynamicModelPredicate, + Consumer macroConnectAdder, + BiConsumer> macroConnectorAdder) { + this.dynamicModelGetter = dynamicModelGetter; + this.pureDynamicModelGetter = pureDynamicModelGetter; + this.dynamicModelPredicate = dynamicModelPredicate; + this.macroConnectAdder = macroConnectAdder; + this.macroConnectorAdder = macroConnectorAdder; + } + + /** + * Creates macro connection from equipment and model class + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, Function> varConnectionsSupplier) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName()); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + addMacroConnections(connectedModel, macroConnectorId, mc, varConnectionsSupplier); + } + + /** + * Creates macro connection from equipment and model class + * Skip the instantiation is the equipment does not correspond to the model + */ + public boolean createMacroConnectionsOrSkip(BlackBoxModel originModel, Identifiable equipment, Class modelClass, Function> varConnectionsSupplier) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, false); + if (connectedModel != null) { + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName()); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + addMacroConnections(connectedModel, macroConnectorId, mc, varConnectionsSupplier); + return false; + } + return true; + } + + /** + * Creates macro connection from model classes + */ + public void createMacroConnections(BlackBoxModel originModel, T connectedModel, List varConnections, MacroConnectAttribute... connectFromAttributes) { + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName()); + List fromAttributes = Stream.concat(originModel.getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); + MacroConnect mc = new MacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); + macroConnectAdder.accept(mc); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnections)); + } + + /** + * Creates macro connection from equipment and model class + * Add MacroConnectAttribute "from" attributes + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, MacroConnectAttribute... connectFromAttributes) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + createMacroConnections(originModel, connectedModel, varConnectionsSupplier.apply(connectedModel), connectFromAttributes); + } + + /** + * Creates macro connection from equipment and model class + * Skip the instantiation is the equipment does not correspond to the model + * Add MacroConnectAttribute "from" attributes + */ + public boolean createMacroConnectionsOrSkip(BlackBoxModel originModel, Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, MacroConnectAttribute... connectFromAttributes) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, false); + if (connectedModel != null) { + createMacroConnections(originModel, connectedModel, varConnectionsSupplier.apply(connectedModel), connectFromAttributes); + return false; + } + return true; + } + + /** + * Creates macro connection from equipment and model class + * Suffixes MacroConnector id with side name + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, Side side) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName(), side); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + macroConnectAdder.accept(mc); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel, side))); + } + + /** + * Creates macro connection from equipment and model class + * Suffixes MacroConnector id with string + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, Function> varConnectionsSupplier, String parametrizedName) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName(), parametrizedName); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + addMacroConnections(connectedModel, macroConnectorId, mc, varConnectionsSupplier); + } + + /** + * Creates macro connection from equipment and model class + * Suffixes MacroConnector id and connection with MacroConnectionSuffix (different id and connection suffix) + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, MacroConnectionSuffix suffix) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName(), suffix.getIdSuffix()); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + addMacroConnections(connectedModel, macroConnectorId, mc, varConnectionsSupplier, suffix.getConnectionSuffix()); + } + + /** + * Creates macro connection from equipment and model class + * Add MacroConnectAttribute "from" attributes + * Use a parametrized macro connector name + */ + public void createMacroConnections(BlackBoxModel originModel, Identifiable equipment, Class modelClass, BiFunction> varConnectionsSupplier, String parametrizedName, MacroConnectAttribute... connectFromAttributes) { + T connectedModel = dynamicModelGetter.getDynamicModel(equipment, modelClass, true); + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName(), parametrizedName); + List fromAttributes = Stream.concat(originModel.getMacroConnectFromAttributes().stream(), Arrays.stream(connectFromAttributes)).collect(Collectors.toList()); + MacroConnect mc = new MacroConnect(macroConnectorId, fromAttributes, connectedModel.getMacroConnectToAttributes()); + addMacroConnections(connectedModel, macroConnectorId, mc, varConnectionsSupplier, parametrizedName); + } + + /** + * Creates macro connection with a bus from a terminal + */ + public void createTerminalMacroConnections(BlackBoxModel originModel, Terminal terminal, Function> varConnectionsSupplier) { + createMacroConnections(originModel, BusUtils.getConnectableBus(terminal), EquipmentConnectionPoint.class, varConnectionsSupplier); + } + + /** + * Creates macro connection with a bus from a terminal + * Suffixes MacroConnector id with side name + */ + public void createTerminalMacroConnections(BlackBoxModel originModel, Terminal terminal, BiFunction> varConnectionsSupplier, Side side) { + createMacroConnections(originModel, BusUtils.getConnectableBus(terminal), EquipmentConnectionPoint.class, varConnectionsSupplier, side); + } + + /** + * Creates macro connection with a bus from an HVDC + * Suffixes MacroConnector id with side name + */ + public void createTerminalMacroConnections(BlackBoxModel originModel, HvdcLine hvdc, BiFunction> varConnectionsSupplier, Side side) { + HvdcConverterStation station = hvdc.getConverterStation(SideConverter.convert(side)); + createTerminalMacroConnections(originModel, station.getTerminal(), varConnectionsSupplier, side); + } + + /** + * Creates macro connection with TapChangerAutomaton from dynamic id + */ + public boolean createTcaMacroConnectionsOrSkip(BlackBoxModel originModel, String tapChangerId, Function> varConnectionsSupplier) { + TapChangerAutomaton connectedModel = pureDynamicModelGetter.getPureDynamicModel(tapChangerId, TapChangerAutomaton.class, false); + if (connectedModel != null && connectedModel.isConnected(this)) { + String macroConnectorId = MacroConnector.createMacroConnectorId(originModel.getName(), connectedModel.getName()); + MacroConnect mc = new MacroConnect(macroConnectorId, originModel.getMacroConnectFromAttributes(), connectedModel.getMacroConnectToAttributes()); + macroConnectAdder.accept(mc); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel))); + return false; + } + return true; + } + + private void addMacroConnections(T connectedModel, String macroConnectorId, MacroConnect mc, Function> varConnectionsSupplier) { + macroConnectAdder.accept(mc); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel))); + } + + private void addMacroConnections(T connectedModel, String macroConnectorId, MacroConnect mc, BiFunction> varConnectionsSupplier, String parametrizedName) { + macroConnectAdder.accept(mc); + macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel, parametrizedName))); + } + + //TODO use context method instead ? + public boolean isWithoutBlackBoxDynamicModel(Identifiable equipment) { + return dynamicModelPredicate.test(equipment); + } + + public void setMacroConnectAdder(Consumer macroConnectAdder) { + this.macroConnectAdder = macroConnectAdder; + } + + public void setMacroConnectorAdder(BiConsumer> macroConnectorAdder) { + this.macroConnectorAdder = macroConnectorAdder; + } +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/svarcs/StaticVarCompensator.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/svarcs/StaticVarCompensator.java index ecfcebe86..3184e1608 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/svarcs/StaticVarCompensator.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/svarcs/StaticVarCompensator.java @@ -7,11 +7,11 @@ */ package com.powsybl.dynawaltz.models.svarcs; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.VarMapping; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import java.util.Arrays; import java.util.List; @@ -32,8 +32,8 @@ public StaticVarCompensator(String dynamicModelId, com.powsybl.iidm.network.Stat } @Override - public void createMacroConnections(DynaWaltzContext context) { - createTerminalMacroConnections(equipment, this::getVarConnectionsWith, context); + public void createMacroConnections(MacroConnectionsAdder adder) { + adder.createTerminalMacroConnections(this, equipment.getTerminal(), this::getVarConnectionsWith); } private List getVarConnectionsWith(EquipmentConnectionPoint connected) { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/transformers/TransformerFixedRatio.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/transformers/TransformerFixedRatio.java index d2c2d44b9..b987a3b05 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/transformers/TransformerFixedRatio.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/transformers/TransformerFixedRatio.java @@ -7,11 +7,11 @@ */ package com.powsybl.dynawaltz.models.transformers; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.Side; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.dynawaltz.models.utils.SideConverter; import com.powsybl.iidm.network.TwoWindingsTransformer; @@ -37,8 +37,8 @@ private String getTerminalVarName(Side side) { } @Override - public void createMacroConnections(DynaWaltzContext context) { - equipment.getTerminals().forEach(t -> createTerminalMacroConnections(t, this::getVarConnectionsWith, context, SideConverter.convert(equipment.getSide(t)))); + public void createMacroConnections(MacroConnectionsAdder adder) { + equipment.getTerminals().forEach(t -> adder.createTerminalMacroConnections(this, t, this::getVarConnectionsWith, SideConverter.convert(equipment.getSide(t)))); } @Override 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 index fb3b6ee58..059a76086 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/BusUtils.java @@ -17,11 +17,11 @@ public final class BusUtils { private BusUtils() { } - public static String getConnectableBusStaticId(Terminal terminal) { - return terminal.getBusBreakerView().getConnectableBus().getId(); + public static Bus getConnectableBus(Terminal terminal) { + return terminal.getBusBreakerView().getConnectableBus(); } - public static String getConnectableBusStaticId(Injection injection) { - return getConnectableBusStaticId(injection.getTerminal()); + public static Bus getConnectableBus(Injection injection) { + return getConnectableBus(injection.getTerminal()); } } 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 ebcb229a5..f19d54367 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java @@ -14,7 +14,6 @@ import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.models.loads.BaseLoad; -import com.powsybl.dynawaltz.models.transformers.TapChangerModel; import com.powsybl.iidm.network.Identifiable; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -84,36 +83,23 @@ void duplicateDynamicId() { Assertions.assertThat(context.getBlackBoxDynamicModels()).containsExactly(load1); } - @Test - void testDynamicModelGetterException() { - DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); - - // incorrect model - Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("GEN5", LineModel.class)); - assertEquals("The model identified by the static id GEN5 does not match the expected model (LineModel)", e.getMessage()); - - // default model not implemented - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel("unknownID", TapChangerModel.class)); - assertEquals("Default model not implemented for TapChangerModel", e.getMessage()); - } - @Test void testDynamicModelGetterFromIdentifiableTypeException() { DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); // incorrect model Identifiable gen = network.getIdentifiable("GEN5"); - Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(gen, LineModel.class)); + Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(gen, LineModel.class, true)); assertEquals("The model identified by the static id GEN5 does not match the expected model (LineModel)", e.getMessage()); // dynamic model not found Identifiable substation = network.getIdentifiable("P1"); - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(substation, InjectionModel.class)); + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(substation, InjectionModel.class, true)); assertEquals("No dynamic model associated with SUBSTATION", e.getMessage()); // requested interface not implemented Identifiable transformer = network.getIdentifiable("NGEN_NHV1"); - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(transformer, InjectionModel.class)); + e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(transformer, InjectionModel.class, true)); assertEquals("Default model DefaultTransformer does not implement InjectionModel interface", e.getMessage()); } } diff --git a/dynawaltz/src/test/resources/apv_dyd.xml b/dynawaltz/src/test/resources/apv_dyd.xml index 5604eff39..4aa2f4552 100644 --- a/dynawaltz/src/test/resources/apv_dyd.xml +++ b/dynawaltz/src/test/resources/apv_dyd.xml @@ -31,7 +31,7 @@ - + @@ -73,9 +73,9 @@ - + - + diff --git a/dynawaltz/src/test/resources/dyd.xml b/dynawaltz/src/test/resources/dyd.xml index 7ba5ddea3..aea2c7496 100644 --- a/dynawaltz/src/test/resources/dyd.xml +++ b/dynawaltz/src/test/resources/dyd.xml @@ -84,7 +84,7 @@ - + @@ -161,17 +161,17 @@ - + - + - + - + - + - + - + diff --git a/dynawaltz/src/test/resources/events.xml b/dynawaltz/src/test/resources/events.xml index 4dacda7de..d8b10d589 100644 --- a/dynawaltz/src/test/resources/events.xml +++ b/dynawaltz/src/test/resources/events.xml @@ -16,7 +16,7 @@ - + @@ -32,7 +32,7 @@ - + diff --git a/dynawaltz/src/test/resources/grid_forming_converter_dyd.xml b/dynawaltz/src/test/resources/grid_forming_converter_dyd.xml index 730a02372..37c6d3c88 100644 --- a/dynawaltz/src/test/resources/grid_forming_converter_dyd.xml +++ b/dynawaltz/src/test/resources/grid_forming_converter_dyd.xml @@ -12,7 +12,7 @@ - + @@ -22,5 +22,5 @@ - + diff --git a/dynawaltz/src/test/resources/omega_ref_dyd.xml b/dynawaltz/src/test/resources/omega_ref_dyd.xml index d1344a8a0..69aa3343f 100644 --- a/dynawaltz/src/test/resources/omega_ref_dyd.xml +++ b/dynawaltz/src/test/resources/omega_ref_dyd.xml @@ -27,7 +27,7 @@ - + @@ -58,9 +58,9 @@ - + - + - + diff --git a/dynawaltz/src/test/resources/wecc_pv_dyd.xml b/dynawaltz/src/test/resources/wecc_pv_dyd.xml index 9b696f255..4b9351383 100644 --- a/dynawaltz/src/test/resources/wecc_pv_dyd.xml +++ b/dynawaltz/src/test/resources/wecc_pv_dyd.xml @@ -11,7 +11,7 @@ - + @@ -21,5 +21,5 @@ - + diff --git a/dynawaltz/src/test/resources/wecc_wt_synchro_dyd.xml b/dynawaltz/src/test/resources/wecc_wt_synchro_dyd.xml index 014354498..2c4837b1d 100644 --- a/dynawaltz/src/test/resources/wecc_wt_synchro_dyd.xml +++ b/dynawaltz/src/test/resources/wecc_wt_synchro_dyd.xml @@ -11,7 +11,7 @@ - + @@ -21,5 +21,5 @@ - + From bc5683ea312df5a3889d07019e5c62a037462d60 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 21 Nov 2023 12:57:26 +0100 Subject: [PATCH 02/13] Add default model tests Signed-off-by: lisrte --- .../DefaultModelConfiguration.java | 16 ++-- .../defaultmodels/DefaultModelsHandler.java | 9 +-- .../DefaultModelHandlerTest.java | 73 +++++++++++++++++++ .../dynawaltz/xml/DynamicModelsXmlTest.java | 15 +--- 4 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java index a30f70188..ae4b1803d 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java @@ -32,31 +32,31 @@ public enum DefaultModelConfiguration { ACTION_CONNECTION_POINT(IdentifiableType.BUS, ActionConnectionPoint.class, - new DefaultModelFactory(DefaultActionConnectionPoint::new)), + new DefaultModelFactory<>(DefaultActionConnectionPoint::new)), EQUIPMENT_CONNECTION_POINT(IdentifiableType.BUS, EquipmentConnectionPoint.class, DefaultEquipmentConnectionPoint::getDefaultModel), GENERATOR(IdentifiableType.GENERATOR, GeneratorModel.class, - new DefaultModelFactory(DefaultGenerator::new)), + new DefaultModelFactory<>(DefaultGenerator::new)), HVDC(IdentifiableType.HVDC_LINE, HvdcModel.class, - new DefaultModelFactory(DefaultHvdc::new)), + new DefaultModelFactory<>(DefaultHvdc::new)), LINE(IdentifiableType.LINE, LineModel.class, - new DefaultModelFactory(DefaultLine::new)), + new DefaultModelFactory<>(DefaultLine::new)), LOAD(IdentifiableType.LOAD, LoadModel.class, - new DefaultModelFactory(DefaultLoad::new)), + new DefaultModelFactory<>(DefaultLoad::new)), SHUNT_COMPENSATOR(IdentifiableType.SHUNT_COMPENSATOR, ShuntModel.class, - new DefaultModelFactory(DefaultShunt::new)), + new DefaultModelFactory<>(DefaultShunt::new)), STATIC_VAR_COMPENSATOR(IdentifiableType.STATIC_VAR_COMPENSATOR, StaticVarCompensatorModel.class, - new DefaultModelFactory(DefaultStaticVarCompensator::new)), + new DefaultModelFactory<>(DefaultStaticVarCompensator::new)), TWO_WINDINGS_TRANSFORMER(IdentifiableType.TWO_WINDINGS_TRANSFORMER, TransformerModel.class, - new DefaultModelFactory(DefaultTransformer::new)); + new DefaultModelFactory<>(DefaultTransformer::new)); private final IdentifiableType identifiableType; private final Class equipmentClass; diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java index 41346ad54..bf06f38ff 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelsHandler.java @@ -21,7 +21,7 @@ * @author Florian Dupuy {@literal } * @author Laurent Issertial {@literal } */ -public class DefaultModelsHandler { +public final class DefaultModelsHandler { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultModelsHandler.class); @@ -29,9 +29,6 @@ public class DefaultModelsHandler { .stream() .collect(groupingBy(DefaultModelConfiguration::getIdentifiableType)); - public DefaultModelsHandler() { - } - public T getDefaultModel(Identifiable equipment, Class connectableClass, boolean throwException) { DefaultModelConfiguration conf = findConfiguration(equipment.getType(), connectableClass); Model defaultModel = conf.getDefaultModel(equipment.getId()); @@ -49,7 +46,7 @@ public T getDefaultModel(Identifiable equipment, Class c private DefaultModelConfiguration findConfiguration(IdentifiableType type, Class connectableClass) { List configurationList = configurations.get(type); if (configurationList == null) { - throw new PowsyblException("No dynamic model associated with " + type); + throw new PowsyblException("No default model configuration for " + type); } if (configurationList.size() == 1) { return configurationList.get(0); @@ -59,7 +56,7 @@ private DefaultModelConfiguration findConfiguration(Identifiab return configuration; } } + throw new PowsyblException("No default model configuration for " + type + " - " + connectableClass.getSimpleName()); } - throw new PowsyblException("No default model configuration for " + type + " - " + connectableClass.getSimpleName()); } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java new file mode 100644 index 000000000..87dad1a0c --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java @@ -0,0 +1,73 @@ +/** + * 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.defaultmodels; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.dynawaltz.models.Model; +import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint; +import com.powsybl.dynawaltz.models.generators.GeneratorModel; +import com.powsybl.dynawaltz.models.loads.LoadModel; +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Generator; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Laurent Issertial + */ +public class DefaultModelHandlerTest { + + protected Network network = EurostagTutorialExample1Factory.create(); + protected DefaultModelsHandler defaultModelHandler = new DefaultModelsHandler(); + + @Test + void getSimpleDefaultModel() { + Model model = defaultModelHandler.getDefaultModel(network.getGenerator("GEN"), GeneratorModel.class, false); + assertNotNull(model); + assertEquals("DefaultGenerator", model.getName()); + } + + @Test + void getMultipleConfigDefaultModel() { + Model model = defaultModelHandler.getDefaultModel(network.getBusBreakerView().getBus("NGEN"), EquipmentConnectionPoint.class, false); + assertNotNull(model); + assertEquals("DefaultEquipmentConnectionPoint", model.getName()); + } + + @Test + void noInterfaceImplementationLog() { + Generator gen = network.getGenerator("GEN"); + Model model = defaultModelHandler.getDefaultModel(gen, LoadModel.class, false); + assertNull(model); + } + + @Test + void noInterfaceImplementationException() { + Generator gen = network.getGenerator("GEN"); + PowsyblException pe = assertThrows(PowsyblException.class, () -> defaultModelHandler.getDefaultModel(gen, LoadModel.class, true)); + assertEquals("Default model DefaultGenerator does not implement LoadModel interface", pe.getMessage()); + } + + @Test + void noDynamicModelException() { + VoltageLevel vl = network.getVoltageLevel("VLGEN"); + PowsyblException pe = assertThrows(PowsyblException.class, () -> defaultModelHandler.getDefaultModel(vl, LoadModel.class, true)); + assertEquals("No default model configuration for VOLTAGE_LEVEL", pe.getMessage()); + } + + @Test + void noMultipleDynamicModelException() { + Bus bus = network.getBusBreakerView().getBus("NGEN"); + PowsyblException pe = assertThrows(PowsyblException.class, () -> defaultModelHandler.getDefaultModel(bus, LoadModel.class, true)); + assertEquals("No default model configuration for BUS - LoadModel", pe.getMessage()); + } +} 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 d00b3cfd7..46303ac58 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/DynamicModelsXmlTest.java @@ -10,7 +10,6 @@ import com.powsybl.dynamicsimulation.DynamicSimulationParameters; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.DynaWaltzParameters; -import com.powsybl.dynawaltz.models.InjectionModel; import com.powsybl.dynawaltz.models.generators.GeneratorFictitious; import com.powsybl.dynawaltz.models.lines.LineModel; import com.powsybl.dynawaltz.models.loads.BaseLoad; @@ -84,22 +83,10 @@ void duplicateDynamicId() { } @Test - void testDynamicModelGetterFromIdentifiableTypeException() { + void testIncorrectModelException() { DynaWaltzContext dc = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load()); - - // incorrect model Identifiable gen = network.getIdentifiable("GEN5"); Exception e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(gen, LineModel.class, true)); assertEquals("The model identified by the static id GEN5 does not match the expected model (LineModel)", e.getMessage()); - - // dynamic model not found - Identifiable substation = network.getIdentifiable("P1"); - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(substation, InjectionModel.class, true)); - assertEquals("No dynamic model associated with SUBSTATION", e.getMessage()); - - // requested interface not implemented - Identifiable transformer = network.getIdentifiable("NGEN_NHV1"); - e = assertThrows(PowsyblException.class, () -> dc.getDynamicModel(transformer, InjectionModel.class, true)); - assertEquals("Default model DefaultTransformer does not implement InjectionModel interface", e.getMessage()); } } From eeb805f3aadb8c5027285368e3676de8d3e5827e Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 21 Nov 2023 13:01:45 +0100 Subject: [PATCH 03/13] Fix author tag Signed-off-by: lisrte --- .../models/defaultmodels/DefaultModelConfiguration.java | 2 +- .../models/defaultmodels/DefaultModelFactoryInterface.java | 2 +- .../models/macroconnections/MacroConnectionsAdder.java | 2 +- .../dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java index ae4b1803d..114a7743f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java @@ -26,7 +26,7 @@ import com.powsybl.iidm.network.IdentifiableType; /** - * @author Laurent Issertial + * @author Laurent Issertial {@literal } */ public enum DefaultModelConfiguration { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java index 740914011..7ded6de0c 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java @@ -1,7 +1,7 @@ package com.powsybl.dynawaltz.models.defaultmodels; /** - * @author Laurent Issertial + * @author Laurent Issertial {@literal } */ public interface DefaultModelFactoryInterface { diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java index e82dfd3a1..a82a68fc4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java @@ -27,7 +27,7 @@ import java.util.stream.Stream; /** - * @author Laurent Issertial + * @author Laurent Issertial {@literal } */ public final class MacroConnectionsAdder { diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java index 87dad1a0c..c82ba5978 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java @@ -22,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.*; /** - * @author Laurent Issertial + * @author Laurent Issertial {@literal } */ public class DefaultModelHandlerTest { From dc5d79aa8e23d37e81d47985457e52f14c415133 Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 21 Nov 2023 13:07:31 +0100 Subject: [PATCH 04/13] Fix checkstyle Signed-off-by: lisrte --- .../dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java index c82ba5978..adfcbe65e 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java @@ -46,7 +46,7 @@ void getMultipleConfigDefaultModel() { @Test void noInterfaceImplementationLog() { Generator gen = network.getGenerator("GEN"); - Model model = defaultModelHandler.getDefaultModel(gen, LoadModel.class, false); + Model model = defaultModelHandler.getDefaultModel(gen, LoadModel.class, false); assertNull(model); } From 2d603adb078dce72d29026c55c86d0bb67561a6e Mon Sep 17 00:00:00 2001 From: lisrte Date: Tue, 21 Nov 2023 13:44:45 +0100 Subject: [PATCH 05/13] Fix checkstyle Signed-off-by: lisrte --- .../dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java index adfcbe65e..d0d8fe2c9 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelHandlerTest.java @@ -24,7 +24,7 @@ /** * @author Laurent Issertial {@literal } */ -public class DefaultModelHandlerTest { +class DefaultModelHandlerTest { protected Network network = EurostagTutorialExample1Factory.create(); protected DefaultModelsHandler defaultModelHandler = new DefaultModelsHandler(); From 13f6ab0751a41f5dd43427e4dd47bb91e6b650a2 Mon Sep 17 00:00:00 2001 From: lisrte Date: Thu, 23 Nov 2023 13:46:58 +0100 Subject: [PATCH 06/13] Use adder in Bus connection verification Signed-off-by: lisrte --- .../dynawaltz/models/buses/AbstractBus.java | 20 +++++++------------ .../MacroConnectionsAdder.java | 7 +++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java index af78cb300..2a7ded14c 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/AbstractBus.java @@ -9,10 +9,10 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel; +import com.powsybl.dynawaltz.models.EquipmentBlackBoxModel; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectAttribute; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; import com.powsybl.iidm.network.Bus; -import com.powsybl.iidm.network.Terminal; import java.util.ArrayList; import java.util.List; @@ -35,18 +35,12 @@ public List getMacroConnectToAttributes() { } public void createMacroConnections(MacroConnectionsAdder adder) { - checkLinkedDynamicModels(adder); - } - - private void checkLinkedDynamicModels(MacroConnectionsAdder adder) { - equipment.getConnectedTerminalStream() - .map(Terminal::getConnectable) - .filter(adder::isWithoutBlackBoxDynamicModel) - .findAny() - .ifPresent(id -> { - throw new PowsyblException(String.format("The equipment %s linked to the %s %s does not possess a dynamic model", - id, this.getClass().getSimpleName(), getStaticId())); - }); + equipment.getConnectedTerminalStream().forEach(t -> { + if (!adder.checkMacroConnections(t.getConnectable(), EquipmentBlackBoxModel.class)) { + throw new PowsyblException(String.format("The equipment %s linked to the %s %s does not possess a dynamic model", + t.getConnectable().getId(), this.getClass().getSimpleName(), getStaticId())); + } + }); } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java index a82a68fc4..b6d013ed2 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java @@ -204,6 +204,13 @@ public boolean createTcaMacroConnectionsOrSkip(BlackBoxModel originModel, String return true; } + /** + * Verifies if a connection can be created with the specified equipment/model combination without creating macro connections + */ + public boolean checkMacroConnections(Identifiable equipment, Class modelClass) { + return dynamicModelGetter.getDynamicModel(equipment, modelClass, false) != null; + } + private void addMacroConnections(T connectedModel, String macroConnectorId, MacroConnect mc, Function> varConnectionsSupplier) { macroConnectAdder.accept(mc); macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel))); From 1722afa1a4b7abaf61cd66b52e10c00e69773e6f Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 24 Nov 2023 09:44:53 +0100 Subject: [PATCH 07/13] Replace call to context with late init field in event model Signed-off-by: lisrte --- .../dsl/EventModelsSupplierTest.java | 2 +- .../powsybl/dynawaltz/DynaWaltzContext.java | 34 +++++++++++---- .../AbstractDynamicLibEventDisconnection.java | 23 ++++------- .../models/events/AbstractEvent.java | 4 +- .../models/events/ContextDependentEvent.java | 25 +++++++++++ .../events/EventActivePowerVariation.java | 41 ++++++++----------- .../events/EventQuadripoleDisconnection.java | 3 +- .../models/events/NodeFaultEvent.java | 3 +- .../MacroConnectionsAdder.java | 11 +---- .../dynawaltz/models/utils/LateInitField.java | 31 ++++++++++++++ 10 files changed, 113 insertions(+), 64 deletions(-) create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/ContextDependentEvent.java create mode 100644 dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java diff --git a/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/EventModelsSupplierTest.java b/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/EventModelsSupplierTest.java index 27702044d..54d4786d3 100644 --- a/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/EventModelsSupplierTest.java +++ b/dynawaltz-dsl/src/test/java/com/powsybl/dynawaltz/dsl/EventModelsSupplierTest.java @@ -60,7 +60,7 @@ void assertEventModel(AbstractEvent em, String dynamicId, String equipmentStatic assertEquals(lib, em.getLib()); } else { Exception e = assertThrows(PowsyblException.class, em::getLib); - assertEquals("The associated library depends on context", e.getMessage()); + assertEquals("Field has not been initialized", e.getMessage()); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 46a8251b6..07fd5d160 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -9,9 +9,13 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynamicsimulation.DynamicSimulationParameters; -import com.powsybl.dynawaltz.models.*; +import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.EquipmentBlackBoxModel; +import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.buses.AbstractBus; import com.powsybl.dynawaltz.models.defaultmodels.DefaultModelsHandler; +import com.powsybl.dynawaltz.models.events.ContextDependentEvent; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizedModel; import com.powsybl.dynawaltz.models.frequencysynchronizers.FrequencySynchronizerModel; import com.powsybl.dynawaltz.models.frequencysynchronizers.OmegaRef; @@ -62,23 +66,26 @@ public DynaWaltzContext(Network network, String workingVariantId, List curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters) { this.network = Objects.requireNonNull(network); this.workingVariantId = Objects.requireNonNull(workingVariantId); + this.dynamicModels = Objects.requireNonNull(dynamicModels).stream() .filter(distinctByDynamicId().and(distinctByStaticId())) .toList(); - this.eventModels = Objects.requireNonNull(eventModels).stream() - .filter(distinctByDynamicId()) - .toList(); this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream() .filter(EquipmentBlackBoxModel.class::isInstance) .map(EquipmentBlackBoxModel.class::cast) .collect(Collectors.toMap(EquipmentBlackBoxModel::getStaticId, Function.identity())); + + this.eventModels = Objects.requireNonNull(eventModels).stream() + .filter(distinctByDynamicId()) + .map(setLateInitEventField()) + .toList(); + this.curves = Objects.requireNonNull(curves); this.parameters = Objects.requireNonNull(parameters); this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters); this.frequencySynchronizer = setupFrequencySynchronizer(dynamicModels.stream().anyMatch(AbstractBus.class::isInstance) ? SetPoint::new : OmegaRef::new); this.macroConnectionsAdder = new MacroConnectionsAdder(this::getDynamicModel, this::getPureDynamicModel, - this::isWithoutBlackBoxDynamicModel, macroConnectList::add, macroConnectorsMap::computeIfAbsent); @@ -185,6 +192,19 @@ protected static Predicate distinctByDynamicId() { }; } + protected Function setLateInitEventField() { + return blackBoxModel -> { + if (blackBoxModel instanceof ContextDependentEvent event) { + event.setEquipmentHasDynamicModel(this); + } + return blackBoxModel; + }; + } + + public boolean hasDynamicModel(Identifiable equipment) { + return staticIdBlackBoxModelMap.containsKey(equipment.getId()); + } + public List getMacroConnectList() { return macroConnectList; } @@ -193,10 +213,6 @@ public Collection getMacroConnectors() { return macroConnectorsMap.values(); } - public boolean isWithoutBlackBoxDynamicModel(Identifiable equipment) { - return !staticIdBlackBoxModelMap.containsKey(equipment.getId()); - } - private Stream getInputBlackBoxDynamicModelStream() { // Doesn't include the OmegaRef, it only concerns the DynamicModels provided by the user return dynamicModels.stream(); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java index 71b0bf936..c8493ef60 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java @@ -7,21 +7,18 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; +import com.powsybl.dynawaltz.models.utils.LateInitField; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Identifiable; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - import static com.powsybl.dynawaltz.parameters.ParameterType.BOOL; import static com.powsybl.dynawaltz.parameters.ParameterType.DOUBLE; /** * @author Laurent Issertial {@literal } */ -public abstract class AbstractDynamicLibEventDisconnection extends AbstractEvent { +public abstract class AbstractDynamicLibEventDisconnection extends AbstractEvent implements ContextDependentEvent { private static final String EVENT_PREFIX = "Disconnect_"; private static final String DYNAMIC_MODEL_LIB = "EventSetPointBoolean"; @@ -29,6 +26,7 @@ public abstract class AbstractDynamicLibEventDisconnection extends AbstractEvent protected static final String DISCONNECTION_VAR_CONNECT = "event_state1"; private final boolean disconnect; + private final LateInitField equipmentHasDynamicModel = new LateInitField<>(); protected AbstractDynamicLibEventDisconnection(Identifiable equipment, double startTime, boolean disconnect) { super(equipment, startTime, EVENT_PREFIX); @@ -37,7 +35,7 @@ protected AbstractDynamicLibEventDisconnection(Identifiable equipment, double @Override public String getLib() { - throw new PowsyblException("The associated library depends on context"); + return equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; } @Override @@ -46,16 +44,13 @@ public String getName() { } @Override - protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - writer.writeAttribute("id", getDynamicModelId()); - writer.writeAttribute("lib", context.isWithoutBlackBoxDynamicModel(getEquipment()) ? DEFAULT_MODEL_LIB : DYNAMIC_MODEL_LIB); - writer.writeAttribute("parFile", getParFile(context)); - writer.writeAttribute("parId", getParameterSetId()); + protected void createEventSpecificParameters(ParametersSet paramSet) { + paramSet.addParameter("event_tEvent", DOUBLE, Double.toString(getStartTime())); + paramSet.addParameter("event_stateEvent1", BOOL, Boolean.toString(disconnect)); } @Override - protected void createEventSpecificParameters(ParametersSet paramSet, DynaWaltzContext context) { - paramSet.addParameter("event_tEvent", DOUBLE, Double.toString(getStartTime())); - paramSet.addParameter("event_stateEvent1", BOOL, Boolean.toString(disconnect)); + public final void setEquipmentHasDynamicModel(DynaWaltzContext context) { + this.equipmentHasDynamicModel.setValue(hasDynamicModel(context)); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEvent.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEvent.java index 1891db848..9db60ae54 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEvent.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractEvent.java @@ -51,9 +51,9 @@ public String getParFile(DynaWaltzContext context) { @Override public void createDynamicModelParameters(DynaWaltzContext context, Consumer parametersAdder) { ParametersSet paramSet = new ParametersSet(getParameterSetId()); - createEventSpecificParameters(paramSet, context); + createEventSpecificParameters(paramSet); parametersAdder.accept(paramSet); } - protected abstract void createEventSpecificParameters(ParametersSet paramSet, DynaWaltzContext context); + protected abstract void createEventSpecificParameters(ParametersSet paramSet); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/ContextDependentEvent.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/ContextDependentEvent.java new file mode 100644 index 000000000..61c14dd86 --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/ContextDependentEvent.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com/) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.dynawaltz.models.events; + +import com.powsybl.dynawaltz.DynaWaltzContext; +import com.powsybl.iidm.network.Identifiable; + +/** + * @author Laurent Issertial {@literal } + */ +public interface ContextDependentEvent { + + Identifiable getEquipment(); + + default boolean hasDynamicModel(DynaWaltzContext context) { + return context.hasDynamicModel(getEquipment()); + } + + void setEquipmentHasDynamicModel(DynaWaltzContext context); +} diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java index b8c184485..53eaece1f 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java @@ -7,17 +7,15 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.commons.PowsyblException; import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; +import com.powsybl.dynawaltz.models.utils.LateInitField; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.IdentifiableType; import com.powsybl.iidm.network.Load; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; import java.util.EnumSet; import java.util.List; @@ -27,7 +25,7 @@ /** * @author Laurent Issertial {@literal } */ -public class EventActivePowerVariation extends AbstractEvent { +public class EventActivePowerVariation extends AbstractEvent implements ContextDependentEvent { private static final EnumSet CONNECTABLE_EQUIPMENTS = EnumSet.of(IdentifiableType.GENERATOR, IdentifiableType.LOAD); private static final String EVENT_PREFIX = "Step_"; @@ -35,6 +33,7 @@ public class EventActivePowerVariation extends AbstractEvent { private static final String DEFAULT_MODEL_LIB = "EventSetPointReal"; private final double deltaP; + private final LateInitField equipmentHasDynamicModel = new LateInitField<>(); public EventActivePowerVariation(Load equipment, double startTime, double deltaP) { super(equipment, startTime, EVENT_PREFIX); @@ -52,7 +51,7 @@ public static boolean isConnectable(IdentifiableType type) { @Override public String getLib() { - throw new PowsyblException("The associated library depends on context"); + return equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; } @Override @@ -60,12 +59,9 @@ public String getName() { return EventActivePowerVariation.class.getSimpleName(); } - private List getVarConnectionsWithDefault(ControllableEquipment connected) { - return List.of(new VarConnection("event_state1", connected.getDeltaPVarName())); - } - private List getVarConnectionsWith(ControllableEquipment connected) { - return List.of(new VarConnection("step_step_value", connected.getDeltaPVarName())); + return List.of(equipmentHasDynamicModel.getValue() ? new VarConnection("step_step_value", connected.getDeltaPVarName()) + : new VarConnection("event_state1", connected.getDeltaPVarName())); } @Override @@ -73,33 +69,30 @@ public void createMacroConnections(MacroConnectionsAdder adder) { adder.createMacroConnections(this, getEquipment(), ControllableEquipment.class, - adder.isWithoutBlackBoxDynamicModel(getEquipment()) ? this::getVarConnectionsWithDefault : this::getVarConnectionsWith); + this::getVarConnectionsWith); } @Override - protected void createEventSpecificParameters(ParametersSet paramSet, DynaWaltzContext context) { - if (context.isWithoutBlackBoxDynamicModel(getEquipment())) { - paramSet.addParameter("event_tEvent", DOUBLE, Double.toString(getStartTime())); - paramSet.addParameter("event_stateEvent1", DOUBLE, Double.toString(deltaP)); - } else { + protected void createEventSpecificParameters(ParametersSet paramSet) { + if (equipmentHasDynamicModel.getValue()) { paramSet.addParameter("step_Value0", DOUBLE, Double.toString(0)); paramSet.addParameter("step_tStep", DOUBLE, Double.toString(getStartTime())); paramSet.addParameter("step_Height", DOUBLE, Double.toString(deltaP)); + } else { + paramSet.addParameter("event_tEvent", DOUBLE, Double.toString(getStartTime())); + paramSet.addParameter("event_stateEvent1", DOUBLE, Double.toString(deltaP)); } } - @Override - protected void writeDynamicAttributes(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException { - writer.writeAttribute("id", getDynamicModelId()); - writer.writeAttribute("lib", context.isWithoutBlackBoxDynamicModel(getEquipment()) ? DEFAULT_MODEL_LIB : DYNAMIC_MODEL_LIB); - writer.writeAttribute("parFile", getParFile(context)); - writer.writeAttribute("parId", getParameterSetId()); - } - @Override public void createNetworkParameter(ParametersSet networkParameters) { if (getEquipment().getType() == IdentifiableType.LOAD) { networkParameters.addParameter(getEquipment().getId() + "_isControllable", BOOL, Boolean.toString(true)); } } + + @Override + public final void setEquipmentHasDynamicModel(DynaWaltzContext context) { + this.equipmentHasDynamicModel.setValue(hasDynamicModel(context)); + } } 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 3e12cb62d..138335099 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,7 +6,6 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.automatons.QuadripoleModel; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; @@ -53,7 +52,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) { } @Override - protected void createEventSpecificParameters(ParametersSet paramSet, DynaWaltzContext context) { + protected void createEventSpecificParameters(ParametersSet paramSet) { paramSet.addParameter("event_tEvent", DOUBLE, Double.toString(getStartTime())); paramSet.addParameter("event_disconnectOrigin", BOOL, Boolean.toString(disconnectOrigin)); paramSet.addParameter("event_disconnectExtremity", BOOL, Boolean.toString(disconnectExtremity)); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java index 58d68e306..eafc80f15 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/NodeFaultEvent.java @@ -7,7 +7,6 @@ */ package com.powsybl.dynawaltz.models.events; -import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.buses.ActionConnectionPoint; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; @@ -52,7 +51,7 @@ private List getVarConnectionsWith(ActionConnectionPoint connecte } @Override - protected void createEventSpecificParameters(ParametersSet paramSet, DynaWaltzContext context) { + protected void createEventSpecificParameters(ParametersSet paramSet) { paramSet.addParameter("fault_RPu", DOUBLE, Double.toString(rPu)); paramSet.addParameter("fault_XPu", DOUBLE, Double.toString(xPu)); paramSet.addParameter("fault_tBegin", DOUBLE, Double.toString(getStartTime())); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java index b6d013ed2..53dceb1cb 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java @@ -43,17 +43,13 @@ public interface PureDynamicModelGetter { private final DynamicModelGetter dynamicModelGetter; private final PureDynamicModelGetter pureDynamicModelGetter; - private final Predicate> dynamicModelPredicate; private Consumer macroConnectAdder; private BiConsumer> macroConnectorAdder; public MacroConnectionsAdder(DynamicModelGetter dynamicModelGetter, PureDynamicModelGetter pureDynamicModelGetter, - Predicate> dynamicModelPredicate, - Consumer macroConnectAdder, - BiConsumer> macroConnectorAdder) { + Consumer macroConnectAdder, BiConsumer> macroConnectorAdder) { this.dynamicModelGetter = dynamicModelGetter; this.pureDynamicModelGetter = pureDynamicModelGetter; - this.dynamicModelPredicate = dynamicModelPredicate; this.macroConnectAdder = macroConnectAdder; this.macroConnectorAdder = macroConnectorAdder; } @@ -221,11 +217,6 @@ private void addMacroConnections(T connectedModel, String macr macroConnectorAdder.accept(macroConnectorId, k -> new MacroConnector(macroConnectorId, varConnectionsSupplier.apply(connectedModel, parametrizedName))); } - //TODO use context method instead ? - public boolean isWithoutBlackBoxDynamicModel(Identifiable equipment) { - return dynamicModelPredicate.test(equipment); - } - public void setMacroConnectAdder(Consumer macroConnectAdder) { this.macroConnectAdder = macroConnectAdder; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java new file mode 100644 index 000000000..59f8a16ba --- /dev/null +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java @@ -0,0 +1,31 @@ +/** + * 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.commons.PowsyblException; + +import java.util.Objects; + +/** + * @author Laurent Issertial {@literal } + */ +public class LateInitField { + + private T value = null; + + public final T getValue() { + if (value == null) { + throw new PowsyblException("Field has not been initialized"); + } + return value; + } + + public final void setValue(T value) { + this.value = Objects.requireNonNull(value); + } +} From f35843beef3999b4efb93bd7bf0e719a1b4827f3 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 24 Nov 2023 11:31:54 +0100 Subject: [PATCH 08/13] Add reporter to MacroConnectionsAdder Signed-off-by: lisrte --- .../com/powsybl/dynawaltz/DynaWaltzContext.java | 14 +++++--------- .../models/automatons/TapChangerAutomaton.java | 2 +- .../automatons/TapChangerBlockingAutomaton.java | 2 +- .../macroconnections/MacroConnectionsAdder.java | 16 +++++++++++++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 47b6f6393..867207a66 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -47,7 +47,6 @@ public class DynaWaltzContext { private static final String MODEL_ID_EXCEPTION = "The model identified by the static id %s does not match the expected model (%s)"; private static final String MODEL_ID_LOG = "The model identified by the static id {} does not match the expected model ({})"; - private final Reporter reporter; private final Network network; private final String workingVariantId; private final DynamicSimulationParameters parameters; @@ -72,12 +71,12 @@ public DynaWaltzContext(Network network, String workingVariantId, List dynamicModels, List eventModels, List curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) { - this.reporter = DynawaltzReports.createDynaWaltzContextReporter(reporter); + Reporter contextReporter = DynawaltzReports.createDynaWaltzContextReporter(reporter); this.network = Objects.requireNonNull(network); this.workingVariantId = Objects.requireNonNull(workingVariantId); this.dynamicModels = Objects.requireNonNull(dynamicModels).stream() - .filter(distinctByDynamicId(reporter).and(distinctByStaticId(reporter))) + .filter(distinctByDynamicId(contextReporter).and(distinctByStaticId(contextReporter))) .toList(); this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream() .filter(EquipmentBlackBoxModel.class::isInstance) @@ -85,7 +84,7 @@ public DynaWaltzContext(Network network, String workingVariantId, List new MacroStaticReference(k, bbm.getVarsMapping())); @@ -261,8 +261,4 @@ public List getDynamicModelsParameters() { public String getSimulationParFile() { return getNetwork().getId() + ".par"; } - - public Reporter getReporter() { - return reporter; - } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java index d1980267f..90bd12b64 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerAutomaton.java @@ -64,7 +64,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) { boolean isSkipped = adder.createMacroConnectionsOrSkip(this, load, LoadWithTransformers.class, this::getVarConnectionsWith); if (isSkipped) { connection = ConnectionState.NOT_CONNECTED; - DynawaltzReports.reportEmptyTapChanger(context.getReporter(), getDynamicModelId()); + DynawaltzReports.reportEmptyTapChanger(adder.getReporter(), getDynamicModelId()); } else { connection = ConnectionState.CONNECTED; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java index cee1a31f9..8475d3a85 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/TapChangerBlockingAutomaton.java @@ -103,7 +103,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) { } } else { isConnected = false; - DynawaltzReports.reportEmptyTapChangerBlockingAutomaton(context.getReporter(), getDynamicModelId()); + DynawaltzReports.reportEmptyTapChangerBlockingAutomaton(adder.getReporter(), getDynamicModelId()); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java index 53dceb1cb..51f1f9487 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/macroconnections/MacroConnectionsAdder.java @@ -7,6 +7,7 @@ */ package com.powsybl.dynawaltz.models.macroconnections; +import com.powsybl.commons.reporter.Reporter; import com.powsybl.dynawaltz.models.BlackBoxModel; import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.Side; @@ -22,7 +23,10 @@ import java.util.Arrays; import java.util.List; -import java.util.function.*; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,17 +45,19 @@ public interface PureDynamicModelGetter { T getPureDynamicModel(String dynamicId, Class connectableClass, boolean throwException); } + private final Reporter reporter; private final DynamicModelGetter dynamicModelGetter; private final PureDynamicModelGetter pureDynamicModelGetter; private Consumer macroConnectAdder; private BiConsumer> macroConnectorAdder; - public MacroConnectionsAdder(DynamicModelGetter dynamicModelGetter, PureDynamicModelGetter pureDynamicModelGetter, - Consumer macroConnectAdder, BiConsumer> macroConnectorAdder) { + public MacroConnectionsAdder(DynamicModelGetter dynamicModelGetter, PureDynamicModelGetter pureDynamicModelGetter, Consumer macroConnectAdder, + BiConsumer> macroConnectorAdder, Reporter reporter) { this.dynamicModelGetter = dynamicModelGetter; this.pureDynamicModelGetter = pureDynamicModelGetter; this.macroConnectAdder = macroConnectAdder; this.macroConnectorAdder = macroConnectorAdder; + this.reporter = reporter; } /** @@ -224,4 +230,8 @@ public void setMacroConnectAdder(Consumer macroConnectAdder) { public void setMacroConnectorAdder(BiConsumer> macroConnectorAdder) { this.macroConnectorAdder = macroConnectorAdder; } + + public Reporter getReporter() { + return reporter; + } } From a263d0f27aaef8eb0f288462dff71720be825b96 Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 24 Nov 2023 14:20:39 +0100 Subject: [PATCH 09/13] Fix code smells Signed-off-by: lisrte --- .../events/AbstractDynamicLibEventDisconnection.java | 3 ++- .../dynawaltz/models/events/EventActivePowerVariation.java | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java index c8493ef60..8f62d3b83 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java @@ -14,6 +14,7 @@ import static com.powsybl.dynawaltz.parameters.ParameterType.BOOL; import static com.powsybl.dynawaltz.parameters.ParameterType.DOUBLE; +import static java.lang.Boolean.TRUE; /** * @author Laurent Issertial {@literal } @@ -35,7 +36,7 @@ protected AbstractDynamicLibEventDisconnection(Identifiable equipment, double @Override public String getLib() { - return equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; + return TRUE == equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; } @Override diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java index 53eaece1f..5c3594b25 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java @@ -21,6 +21,7 @@ import static com.powsybl.dynawaltz.parameters.ParameterType.BOOL; import static com.powsybl.dynawaltz.parameters.ParameterType.DOUBLE; +import static java.lang.Boolean.TRUE; /** * @author Laurent Issertial {@literal } @@ -51,7 +52,7 @@ public static boolean isConnectable(IdentifiableType type) { @Override public String getLib() { - return equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; + return TRUE == equipmentHasDynamicModel.getValue() ? DYNAMIC_MODEL_LIB : DEFAULT_MODEL_LIB; } @Override @@ -60,7 +61,7 @@ public String getName() { } private List getVarConnectionsWith(ControllableEquipment connected) { - return List.of(equipmentHasDynamicModel.getValue() ? new VarConnection("step_step_value", connected.getDeltaPVarName()) + return List.of(TRUE == equipmentHasDynamicModel.getValue() ? new VarConnection("step_step_value", connected.getDeltaPVarName()) : new VarConnection("event_state1", connected.getDeltaPVarName())); } @@ -74,7 +75,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) { @Override protected void createEventSpecificParameters(ParametersSet paramSet) { - if (equipmentHasDynamicModel.getValue()) { + if (TRUE == equipmentHasDynamicModel.getValue()) { paramSet.addParameter("step_Value0", DOUBLE, Double.toString(0)); paramSet.addParameter("step_tStep", DOUBLE, Double.toString(getStartTime())); paramSet.addParameter("step_Height", DOUBLE, Double.toString(deltaP)); From 96c0f0f9b9bedd66eb85d176be343283d2be5c21 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 11:44:59 +0100 Subject: [PATCH 10/13] Fix checkstyle Signed-off-by: Florian Dupuy --- .../main/java/com/powsybl/dynawaltz/DynaWaltzContext.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index f2583b278..867207a66 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -10,7 +10,10 @@ import com.powsybl.commons.reporter.Reporter; import com.powsybl.dynamicsimulation.Curve; import com.powsybl.dynamicsimulation.DynamicSimulationParameters; -import com.powsybl.dynawaltz.models.*; +import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel; +import com.powsybl.dynawaltz.models.BlackBoxModel; +import com.powsybl.dynawaltz.models.EquipmentBlackBoxModel; +import com.powsybl.dynawaltz.models.Model; import com.powsybl.dynawaltz.models.buses.AbstractBus; import com.powsybl.dynawaltz.models.defaultmodels.DefaultModelsHandler; import com.powsybl.dynawaltz.models.events.ContextDependentEvent; @@ -25,7 +28,6 @@ import com.powsybl.dynawaltz.xml.MacroStaticReference; import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.TwoSides; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 6d9975b0b35d40741b89a42b2d7c8baab5ba898b Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 14:54:51 +0100 Subject: [PATCH 11/13] Review fixes Signed-off-by: Florian Dupuy --- .../com/powsybl/dynawaltz/DynaWaltzContext.java | 16 ++++++---------- .../buses/DefaultEquipmentConnectionPoint.java | 2 +- .../defaultmodels/DefaultModelConfiguration.java | 2 +- .../DefaultModelFactoryInterface.java | 7 +++++++ .../FrequencySynchronizedModel.java | 2 +- .../models/frequencysynchronizers/OmegaRef.java | 2 +- .../models/generators/GridFormingConverter.java | 2 +- .../models/generators/SynchronizedGenerator.java | 2 +- .../models/generators/SynchronizedWeccGen.java | 2 +- .../dynawaltz/models/utils/LateInitField.java | 3 +++ 10 files changed, 23 insertions(+), 17 deletions(-) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java index 867207a66..db00d3f9e 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java @@ -85,9 +85,14 @@ public DynaWaltzContext(Network network, String workingVariantId, List e.setEquipmentHasDynamicModel(this)); + this.curves = Objects.requireNonNull(curves); this.parameters = Objects.requireNonNull(parameters); this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters); @@ -201,15 +206,6 @@ protected static Predicate distinctByDynamicId(Reporter reporter) }; } - protected Function setLateInitEventField() { - return blackBoxModel -> { - if (blackBoxModel instanceof ContextDependentEvent event) { - event.setEquipmentHasDynamicModel(this); - } - return blackBoxModel; - }; - } - public boolean hasDynamicModel(Identifiable equipment) { return staticIdBlackBoxModelMap.containsKey(equipment.getId()); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java index 660bc16b0..b484e431b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/buses/DefaultEquipmentConnectionPoint.java @@ -27,7 +27,7 @@ public final class DefaultEquipmentConnectionPoint implements EquipmentConnectio private DefaultEquipmentConnectionPoint() { } - public static DefaultEquipmentConnectionPoint getDefaultModel(String staticId) { + public static DefaultEquipmentConnectionPoint getDefaultModel() { return INSTANCE; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java index 114a7743f..40e61b648 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelConfiguration.java @@ -35,7 +35,7 @@ public enum DefaultModelConfiguration { new DefaultModelFactory<>(DefaultActionConnectionPoint::new)), EQUIPMENT_CONNECTION_POINT(IdentifiableType.BUS, EquipmentConnectionPoint.class, - DefaultEquipmentConnectionPoint::getDefaultModel), + staticId -> DefaultEquipmentConnectionPoint.getDefaultModel()), GENERATOR(IdentifiableType.GENERATOR, GeneratorModel.class, new DefaultModelFactory<>(DefaultGenerator::new)), diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java index 7ded6de0c..3e2b88ea8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/defaultmodels/DefaultModelFactoryInterface.java @@ -1,3 +1,10 @@ +/* + * 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.defaultmodels; /** diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java index 6efdfd8ed..666da6bdf 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/FrequencySynchronizedModel.java @@ -42,5 +42,5 @@ default double getWeightGen(DynaWaltzParameters dynaWaltzParameters) { String getStaticId(); - Bus getConnectesBus(); + Bus getConnectableBus(); } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java index 867cbef2f..8ecc36db3 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/frequencysynchronizers/OmegaRef.java @@ -72,7 +72,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) throws PowsyblEx for (FrequencySynchronizedModel eq : synchronizedEquipments) { adder.createMacroConnections(this, eq, getVarConnectionsWith(eq), MacroConnectAttribute.ofIndex1(index)); // If a bus with a dynamic model is found SetPoint is used in place of OmegaRef, thus at this point we don't have to handle dynamic model buses - BusOfFrequencySynchronizedModel busOf = new DefaultBusOfFrequencySynchronized(eq.getConnectesBus().getId(), eq.getStaticId()); + BusOfFrequencySynchronizedModel busOf = new DefaultBusOfFrequencySynchronized(eq.getConnectableBus().getId(), eq.getStaticId()); adder.createMacroConnections(this, busOf, getVarConnectionsWithBus(busOf), MacroConnectAttribute.ofIndex1(index)); index++; } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java index 206bddd23..7918cc32b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/GridFormingConverter.java @@ -78,7 +78,7 @@ public List getVarsMapping() { } @Override - public Bus getConnectesBus() { + public Bus getConnectableBus() { return BusUtils.getConnectableBus(equipment); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java index 3038e19dc..02f968ace 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedGenerator.java @@ -46,7 +46,7 @@ public String getOmegaRefPuVarName() { } @Override - public Bus getConnectesBus() { + public Bus getConnectableBus() { return BusUtils.getConnectableBus(equipment); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java index 6b8c6133d..f1515d199 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/generators/SynchronizedWeccGen.java @@ -32,7 +32,7 @@ public String getRunningVarName() { } @Override - public Bus getConnectesBus() { + public Bus getConnectableBus() { return BusUtils.getConnectableBus(equipment); } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java index 59f8a16ba..8572c1cc8 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java @@ -26,6 +26,9 @@ public final T getValue() { } public final void setValue(T value) { + if (this.value != null) { + throw new PowsyblException("Field has already been initialized"); + } this.value = Objects.requireNonNull(value); } } From b33df9f93f4a813ffb49f6f180a8b1a56ee511ae Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 14:55:16 +0100 Subject: [PATCH 12/13] Fix code smell Signed-off-by: Florian Dupuy --- .../dynawaltz/models/automatons/CurrentLimitAutomaton.java | 2 +- .../models/automatons/CurrentLimitTwoLevelsAutomaton.java | 4 ++-- 2 files changed, 3 insertions(+), 3 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 41bb6883c..d2744e94d 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 @@ -54,7 +54,7 @@ public void createMacroConnections(MacroConnectionsAdder adder) { adder.createMacroConnections(this, controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, CONTROL_SUFFIX); } - protected List getVarConnectionsWithMeasuredQuadripole(QuadripoleModel connected) { + private List getVarConnectionsWithMeasuredQuadripole(QuadripoleModel connected) { return Arrays.asList( new VarConnection("currentLimitAutomaton_IMonitored", connected.getIVarName(measuredSide)), new VarConnection("currentLimitAutomaton_AutomatonExists", connected.getDeactivateCurrentLimitsVarName()) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java index f47206800..46f9c66a5 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/automatons/CurrentLimitTwoLevelsAutomaton.java @@ -36,12 +36,12 @@ public CurrentLimitTwoLevelsAutomaton(String dynamicModelId, String parameterSet @Override public void createMacroConnections(MacroConnectionsAdder adder) { - adder.createMacroConnections(this, measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithMeasuredQuadripole, FIRST_MEASURE_SUFFIX + SideUtils.getSideSuffix(measuredSide)); + adder.createMacroConnections(this, measuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithFirstMeasuredQuadripole, FIRST_MEASURE_SUFFIX + SideUtils.getSideSuffix(measuredSide)); adder.createMacroConnections(this, secondMeasuredQuadripole, QuadripoleModel.class, this::getVarConnectionsWithSecondMeasuredQuadripole, SECOND_MEASURE_SUFFIX + SideUtils.getSideSuffix(secondMeasuredSide)); adder.createMacroConnections(this, controlledQuadripole, QuadripoleModel.class, this::getVarConnectionsWithControlledQuadripole, CONTROL_SUFFIX); } - protected List getVarConnectionsWithMeasuredQuadripole(QuadripoleModel connected) { + private List getVarConnectionsWithFirstMeasuredQuadripole(QuadripoleModel connected) { return Arrays.asList( new VarConnection("currentLimitAutomaton_IMonitored1", connected.getIVarName(measuredSide)), new VarConnection("currentLimitAutomaton_AutomatonExists", connected.getDeactivateCurrentLimitsVarName()) From 51dda1778654ebdc96f7000aa27bd27af5b56279 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Wed, 13 Dec 2023 15:13:39 +0100 Subject: [PATCH 13/13] Rename LateInitField -> ImmutableLateInit Signed-off-by: Florian Dupuy --- .../models/events/AbstractDynamicLibEventDisconnection.java | 4 ++-- .../dynawaltz/models/events/EventActivePowerVariation.java | 4 ++-- .../utils/{LateInitField.java => ImmutableLateInit.java} | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/{LateInitField.java => ImmutableLateInit.java} (96%) diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java index 8f62d3b83..042a4b67b 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/AbstractDynamicLibEventDisconnection.java @@ -8,7 +8,7 @@ package com.powsybl.dynawaltz.models.events; import com.powsybl.dynawaltz.DynaWaltzContext; -import com.powsybl.dynawaltz.models.utils.LateInitField; +import com.powsybl.dynawaltz.models.utils.ImmutableLateInit; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Identifiable; @@ -27,7 +27,7 @@ public abstract class AbstractDynamicLibEventDisconnection extends AbstractEvent protected static final String DISCONNECTION_VAR_CONNECT = "event_state1"; private final boolean disconnect; - private final LateInitField equipmentHasDynamicModel = new LateInitField<>(); + private final ImmutableLateInit equipmentHasDynamicModel = new ImmutableLateInit<>(); protected AbstractDynamicLibEventDisconnection(Identifiable equipment, double startTime, boolean disconnect) { super(equipment, startTime, EVENT_PREFIX); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java index 5c3594b25..ed25420e3 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/events/EventActivePowerVariation.java @@ -10,7 +10,7 @@ import com.powsybl.dynawaltz.DynaWaltzContext; import com.powsybl.dynawaltz.models.VarConnection; import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder; -import com.powsybl.dynawaltz.models.utils.LateInitField; +import com.powsybl.dynawaltz.models.utils.ImmutableLateInit; import com.powsybl.dynawaltz.parameters.ParametersSet; import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.IdentifiableType; @@ -34,7 +34,7 @@ public class EventActivePowerVariation extends AbstractEvent implements ContextD private static final String DEFAULT_MODEL_LIB = "EventSetPointReal"; private final double deltaP; - private final LateInitField equipmentHasDynamicModel = new LateInitField<>(); + private final ImmutableLateInit equipmentHasDynamicModel = new ImmutableLateInit<>(); public EventActivePowerVariation(Load equipment, double startTime, double deltaP) { super(equipment, startTime, EVENT_PREFIX); diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ImmutableLateInit.java similarity index 96% rename from dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java rename to dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ImmutableLateInit.java index 8572c1cc8..3b29b2f05 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/LateInitField.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/models/utils/ImmutableLateInit.java @@ -14,7 +14,7 @@ /** * @author Laurent Issertial {@literal } */ -public class LateInitField { +public class ImmutableLateInit { private T value = null;