From 9379befc7d72b87ec2c7ef6e3437e2ea37f9819c Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:19:30 +0200 Subject: [PATCH 01/65] add constructor with list instead of varargs Signed-off-by: Nicolas Pierre --- .../converter/DefaultAmplNetworkUpdaterFactory.java | 9 +++++++++ .../ampl/converter/util/AmplDatTableFormatter.java | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java index 897c57431a2..49fc6c67a7c 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java @@ -1,8 +1,17 @@ +/** + * 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/. + */ package com.powsybl.ampl.converter; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.Network; +/** + * @author Nicolas Pierre + */ public class DefaultAmplNetworkUpdaterFactory implements AmplNetworkUpdaterFactory { @Override public AmplNetworkUpdater create(StringToIntMapper mapper, Network network) { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/AmplDatTableFormatter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/AmplDatTableFormatter.java index 17116ae6a25..e36ceea91df 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/AmplDatTableFormatter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/AmplDatTableFormatter.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.io.Writer; +import java.util.List; import java.util.Locale; /** @@ -25,14 +26,21 @@ */ public class AmplDatTableFormatter extends CsvTableFormatter { - public AmplDatTableFormatter(Writer writer, String title, float invalidFloatValue, boolean writeHeader, Locale locale, Column... columns) { + public AmplDatTableFormatter(Writer writer, String title, float invalidFloatValue, boolean writeHeader, + Locale locale, Column... columns) { super(writer, title, ' ', Float.toString(invalidFloatValue), writeHeader, locale, columns); } + public AmplDatTableFormatter(Writer writer, String title, float invalidFloatValue, boolean writeHeader, + Locale locale, List columns) { + super(writer, title, ' ', Float.toString(invalidFloatValue), writeHeader, locale, + columns.toArray(new Column[0])); + } + @Override protected void writeHeader() throws IOException { writer.append("#").append(title).append(System.lineSeparator()) - .append("#"); + .append("#"); for (int i = 0; i < columns.length; i++) { writer.append("\"").append(columns[i].getName()).append("\""); if (i < columns.length - 1) { From dbf18e0e228a1aeb255dede50756b8169e61905a Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:33:21 +0200 Subject: [PATCH 02/65] WIP add ampl export version Signed-off-by: Nicolas Pierre --- .../ampl/converter/AmplExportConfig.java | 25 +++- .../version/AmplColumnsExporter.java | 124 ++++++++++++++++++ .../converter/version/AmplExportVersion.java | 27 ++++ 3 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index c88e5b19b26..e7c195be581 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -7,6 +7,8 @@ */ package com.powsybl.ampl.converter; +import com.powsybl.ampl.converter.version.AmplExportVersion; + import java.util.Objects; /** @@ -38,6 +40,8 @@ public String getLabel() { private ExportScope exportScope; + private AmplExportVersion version; + private boolean exportXNodes; private ExportActionType actionType; @@ -47,15 +51,24 @@ public String getLabel() { private boolean twtSplitShuntAdmittance; public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { - this(exportScope, exportXNodes, actionType, false, false); + this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1); + } + + public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, + boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { + this(exportScope, exportXNodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, + AmplExportVersion.V1); } - public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { + public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, + boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance, + AmplExportVersion version) { this.exportScope = Objects.requireNonNull(exportScope); this.exportXNodes = exportXNodes; this.actionType = Objects.requireNonNull(actionType); this.exportRatioTapChangerVoltageTarget = exportRatioTapChangerVoltageTarget; this.twtSplitShuntAdmittance = twtSplitShuntAdmittance; + this.version = Objects.requireNonNull(version); } public ExportScope getExportScope() { @@ -103,4 +116,12 @@ public AmplExportConfig setTwtSplitShuntAdmittance(boolean twtSplitShuntAdmittan return this; } + public AmplExportVersion getVersion() { + return version; + } + + public void setVersion(AmplExportVersion version) { + this.version = Objects.requireNonNull(version); + } + } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java new file mode 100644 index 00000000000..47d023533ec --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -0,0 +1,124 @@ +/** + * 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/. + */ +package com.powsybl.ampl.converter.version; + +import com.powsybl.ampl.converter.AmplExportConfig; +import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.commons.io.table.Column; +import com.powsybl.commons.io.table.TableFormatter; +import com.powsybl.commons.util.StringToIntMapper; +import com.powsybl.iidm.network.*; + +import java.io.IOException; +import java.util.List; + +/** + * @author Nicolas Pierre + */ +public interface AmplColumnsExporter { + + interface Factory { + AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum); + } + + List getRtcColumns(); + + List getPtcColumns(); + + List getCurrentLimitsColumns(); + + List getHvdcLinesColumns(); + + List getLccConverterStationsColumns(); + + List getVscConverterStationsColumns(); + + List getSubstationsColumns(); + + List getLoadsColumns(); + + List getShuntsColumns(); + + List getStaticVarCompensatorColumns(); + + List getGeneratorsColumns(); + + List getBatteriesColumns(); + + List getBusesColumns(); + + List getBranchesColumns(); + + List getTapChangerTableColumns(); + + void writeRtcToFormatter(TableFormatter formatter) throws IOException; + + void writePtcToFormatter(TableFormatter formatter); + + void writeTwoWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + TwoWindingsTransformer twt) throws IOException; + + void writeThreeWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt) throws IOException; + + void writeCurrentLimits(TableFormatter formatter) throws IOException; + + void writeHvdcToFormatter(TableFormatter formatter, HvdcLine hvdcLine) throws IOException; + + void writeLccConverterStationToFormatter(TableFormatter formatter, + LccConverterStation lccStation) throws IOException; + + void writeVscConverterStationToFormatter(TableFormatter formatter, + VscConverterStation vscStation) throws IOException; + + void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws IOException; + + void writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt, + int middleCcNum) throws IOException; + + void writeLinesToFormatter(TableFormatter formatter, Line l) throws IOException; + + void writeDanglingLineMiddleBusesToFormatter(TableFormatter formatter, DanglingLine dl, + int middleCcNum) throws IOException; + + void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine tieLine, + int xNodeCcNum) throws IOException; + + void writeTieLineToFormatter(TableFormatter formatter, TieLine tieLine) throws IOException; + + void writeDanglingLineToFormatter(TableFormatter formatter, DanglingLine dl) throws IOException; + + void writeTwoWindingsTranformerToFormatter(TableFormatter formatter, TwoWindingsTransformer twt) throws IOException; + + void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter, ThreeWindingsTransformer twt, + int middleBusNum, int middleVlNum, + ThreeWindingsTransformer.Side side) throws IOException; + + void writeTieLineVoltageLevelToFormatter(TableFormatter formatter, TieLine tieLine) throws IOException; + + void writeDanglingLineVoltageLevelToFormatter(TableFormatter formatter, DanglingLine dl) throws IOException; + + void writeThreeWindingsTransformerVoltageLevelToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt) throws IOException; + + void writeVoltageLevelToFormatter(TableFormatter formatter, VoltageLevel vl) throws IOException; + + void writeDanglingLineLoadToFormatter(TableFormatter formatter, DanglingLine dl) throws IOException; + + void writeLoadtoFormatter(TableFormatter formatter, Load l) throws IOException; + + void writeShuntCompensatorToFormatter(TableFormatter formatter, ShuntCompensator sc) throws IOException; + + void writeBatteryToFormatter(TableFormatter formatter, Battery battery) throws IOException; + + void writeStaticVarCompensatorToFormatter(TableFormatter formatter, StaticVarCompensator svc) throws IOException; + + void writeGeneratorToFormatter(TableFormatter formatter, Generator g) throws IOException; + +} diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java new file mode 100644 index 00000000000..ac0e8dedf55 --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.ampl.converter.version; + +/** + * @author Nicolas Pierre + */ +public enum AmplExportVersion { + /** + * Legacy export + */ + V1(BasicAmplExporter.getFactory()); + + private final AmplColumnsExporter.Factory factory; + + AmplExportVersion(AmplColumnsExporter.Factory factory) { + this.factory = factory; + } + + public AmplColumnsExporter.Factory getColumnsExporter() { + return factory; + } +} From 4143d1a0771d56bf9d5e7b9c18afb4e62a629ed2 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:35:35 +0200 Subject: [PATCH 03/65] refactor all writing to BasicAmplExporter Signed-off-by: Nicolas Pierre --- .../powsybl/ampl/converter/AmplConstants.java | 27 +- .../ampl/converter/AmplNetworkWriter.java | 1562 ++--------------- .../com/powsybl/ampl/converter/AmplUtil.java | 36 +- .../converter/version/BasicAmplExporter.java | 1448 +++++++++++++++ 4 files changed, 1697 insertions(+), 1376 deletions(-) create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java index be760dc378e..14a0377f562 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java @@ -9,7 +9,6 @@ import java.util.Locale; /** - * * @author Geoffroy Jamgotchian */ public final class AmplConstants { @@ -17,6 +16,32 @@ public final class AmplConstants { private AmplConstants() { } + public static final String XNODE_COUNTRY_NAME = "XNODE"; + public static final String FAULT = "fault"; + public static final String DESCRIPTION = "description"; + public static final String SUBSTATION = "substation"; + public static final String TARGET_V = "targetV (pu)"; + public static final String TARGET_Q = "targetQ (MVar)"; + public static final String CON_BUS = "con. bus"; + public static final String MINP = "minP (MW)"; + public static final String MAXP = "maxP (MW)"; + public static final String V_REGUL = "v regul."; + public static final String ACTIVE_POWER = "P (MW)"; + public static final String REACTIVE_POWER = "Q (MVar)"; + public static final String MIN_Q_MAX_P = "minQmaxP (MVar)"; + public static final String MIN_Q0 = "minQ0 (MVar)"; + public static final String MIN_Q_MIN_P = "minQminP (MVar)"; + public static final String MAX_Q_MAX_P = "maxQmaxP (MVar)"; + public static final String MAX_Q0 = "maxQ0 (MVar)"; + public static final String MAX_Q_MIN_P = "maxQminP (MVar)"; + public static final String NUM = "num"; + public static final String BUS = "bus"; + public static final String P0 = "p0 (MW)"; + public static final String Q0 = "q0 (MW)"; + public static final String ID = "id"; + public static final String RATIO_TABLE = "_ratio_table"; + public static final String PHASE_TABLE = "_phase_table"; + public static final float INVALID_FLOAT_VALUE = -99999f; public static final Locale LOCALE = Locale.US; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java index 9156332d941..fea575dc811 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java @@ -8,31 +8,26 @@ package com.powsybl.ampl.converter; import com.powsybl.ampl.converter.util.AmplDatTableFormatter; +import com.powsybl.ampl.converter.version.AmplColumnsExporter; import com.powsybl.commons.datasource.DataSource; import com.powsybl.commons.extensions.Extendable; import com.powsybl.commons.extensions.Extension; -import com.powsybl.commons.io.table.Column; import com.powsybl.commons.io.table.TableFormatter; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.LoadingLimits.TemporaryLimit; import com.powsybl.iidm.network.HvdcConverterStation.HvdcType; -import com.powsybl.iidm.network.StaticVarCompensator.RegulationMode; import com.powsybl.iidm.network.util.ConnectedComponents; -import com.powsybl.iidm.network.util.SV; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.OutputStreamWriter; -import java.io.UncheckedIOException; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.Map.Entry; import static com.powsybl.ampl.converter.AmplConstants.DEFAULT_VARIANT_INDEX; -import static com.powsybl.ampl.converter.AmplConstants.VARIANT; /** * @author Geoffroy Jamgotchian @@ -41,38 +36,10 @@ public class AmplNetworkWriter { private static final Logger LOGGER = LoggerFactory.getLogger(AmplNetworkWriter.class); - private static final String XNODE_COUNTRY_NAME = "XNODE"; - private static final String FAULT = "fault"; - private static final String DESCRIPTION = "description"; - private static final String SUBSTATION = "substation"; - private static final String TARGET_V = "targetV (pu)"; - private static final String TARGET_Q = "targetQ (MVar)"; - private static final String CON_BUS = "con. bus"; - private static final String MINP = "minP (MW)"; - private static final String MAXP = "maxP (MW)"; - private static final String V_REGUL = "v regul."; - private static final String ACTIVE_POWER = "P (MW)"; - private static final String REACTIVE_POWER = "Q (MVar)"; - private static final String MIN_Q_MAX_P = "minQmaxP (MVar)"; - private static final String MIN_Q0 = "minQ0 (MVar)"; - private static final String MIN_Q_MIN_P = "minQminP (MVar)"; - private static final String MAX_Q_MAX_P = "maxQmaxP (MVar)"; - private static final String MAX_Q0 = "maxQ0 (MVar)"; - private static final String MAX_Q_MIN_P = "maxQminP (MVar)"; - private static final String NUM = "num"; - private static final String BUS = "bus"; - private static final String P0 = "p0 (MW)"; - private static final String Q0 = "q0 (MW)"; - private static final String ID = "id"; - private final Network network; private final int variantIndex; - private final int faultNum; - - private final int actionNum; - private final DataSource dataSource; private final boolean append; @@ -83,6 +50,8 @@ public class AmplNetworkWriter { private final AmplExportConfig config; + private final AmplColumnsExporter columnsExporter; + private static class AmplExportContext { private int otherCcNum = Integer.MAX_VALUE; @@ -103,13 +72,14 @@ public AmplNetworkWriter(Network network, int variantIndex, DataSource dataSourc boolean append, StringToIntMapper mapper, AmplExportConfig config) { this.network = Objects.requireNonNull(network); this.variantIndex = variantIndex; - this.faultNum = faultNum; - this.actionNum = actionNum; this.dataSource = Objects.requireNonNull(dataSource); this.append = append; this.mapper = Objects.requireNonNull(mapper); this.config = Objects.requireNonNull(config); extensionMap = new HashMap<>(); + this.columnsExporter = config.getVersion() + .getColumnsExporter() + .create(config, network, mapper, variantIndex, faultNum, actionNum); } public AmplNetworkWriter(Network network, DataSource dataSource, int faultNum, int actionNum, @@ -130,14 +100,6 @@ public static String getTableTitle(Network network, String tableName) { return tableName + " (" + network.getId() + "/" + network.getVariantManager().getWorkingVariantId() + ")"; } - private static String getThreeWindingsTransformerMiddleBusId(ThreeWindingsTransformer twt) { - return twt.getId(); // same id as the transformer - } - - private static String getThreeWindingsTransformerMiddleVoltageLevelId(ThreeWindingsTransformer twt) { - return twt.getId(); // same id as the transformer - } - private static int getThreeWindingsTransformerMiddleBusComponentNum(AmplExportContext context, ThreeWindingsTransformer twt) { Terminal t1 = twt.getLeg1().getTerminal(); Terminal t2 = twt.getLeg2().getTerminal(); @@ -159,14 +121,6 @@ private static int getThreeWindingsTransformerMiddleBusComponentNum(AmplExportCo return middleCcNum; } - private static String getDanglingLineMiddleBusId(DanglingLine dl) { - return dl.getId(); // same id as the dangling line - } - - private static String getDanglingLineMiddleVoltageLevelId(DanglingLine dl) { - return dl.getId(); // same id as the dangling line - } - private static int getDanglingLineMiddleBusComponentNum(AmplExportContext context, DanglingLine dl) { Bus b = AmplUtil.getBus(dl.getTerminal()); int middleCcNum; @@ -198,13 +152,6 @@ private static int getTieLineMiddleBusComponentNum(AmplExportContext context, Ti return xNodeCcNum; } - private static double getPermanentLimit(CurrentLimits limits) { - if (limits != null) { - return limits.getPermanentLimit(); - } - return Double.NaN; - } - private static boolean isBusExported(AmplExportContext context, String busId) { return busId != null && context.busIdsToExport.contains(busId); } @@ -214,102 +161,35 @@ private String getTableTitle(String tableName) { } private void writeSubstations() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_substations", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_substations", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Substations"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("unused1"), - new Column("unused2"), - new Column("nomV (KV)"), - new Column("minV (pu)"), - new Column("maxV (pu)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column("country"), - new Column(ID), - new Column(DESCRIPTION))) { + getTableTitle("Substations"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getSubstationsColumns())) { for (VoltageLevel vl : network.getVoltageLevels()) { - int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); - double nomV = vl.getNominalV(); - double minV = vl.getLowVoltageLimit() / nomV; - double maxV = vl.getHighVoltageLimit() / nomV; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(nomV) - .writeCell(minV) - .writeCell(maxV) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) - .writeCell(vl.getId()) - .writeCell(vl.getNameOrId()); - addExtensions(num, vl); + columnsExporter.writeVoltageLevelToFormatter(formatter, vl); + addExtensions(mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()), vl); } // voltage level associated to 3 windings transformers middle bus for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { - String vlId = getThreeWindingsTransformerMiddleVoltageLevelId(twt); - int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); - Terminal t1 = twt.getLeg1().getTerminal(); - VoltageLevel vl1 = t1.getVoltageLevel(); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(vl1.getNominalV()) - .writeCell(Float.NaN) - .writeCell(Float.NaN) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(vl1.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) - .writeCell(vlId) - .writeCell(""); - addExtensions(num, twt); + columnsExporter.writeThreeWindingsTransformerVoltageLevelToFormatter(formatter, twt); + addExtensions(mapper.getInt(AmplSubset.VOLTAGE_LEVEL, + AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt)), twt); } // voltage level associated to dangling lines middle bus for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { - String vlId = getDanglingLineMiddleVoltageLevelId(dl); - int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); - VoltageLevel vl = dl.getTerminal().getVoltageLevel(); - double nomV = vl.getNominalV(); - double minV = vl.getLowVoltageLimit() / nomV; - double maxV = vl.getHighVoltageLimit() / nomV; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(nomV) - .writeCell(minV) - .writeCell(maxV) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) - .writeCell(dl.getId() + "_voltageLevel") - .writeCell(""); - addExtensions(num, dl); + columnsExporter.writeDanglingLineVoltageLevelToFormatter(formatter, dl); + addExtensions(mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getDanglingLineMiddleVoltageLevelId(dl)), + dl); } if (config.isExportXNodes()) { for (TieLine tieLine : network.getTieLines()) { - String vlId = AmplUtil.getXnodeVoltageLevelId(tieLine); - int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(tieLine.getDanglingLine1().getTerminal().getVoltageLevel().getNominalV()) - .writeCell(Float.NaN) - .writeCell(Float.NaN) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(XNODE_COUNTRY_NAME) - .writeCell(AmplUtil.getXnodeBusId(tieLine) + "_voltageLevel") - .writeCell(""); - addExtensions(num, tieLine); + columnsExporter.writeTieLineVoltageLevelToFormatter(formatter, tieLine); + addExtensions(mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)), + tieLine); } } } @@ -335,23 +215,14 @@ private boolean connectedComponentToExport(int numCC) { } private void writeBuses(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_buses", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_buses", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Buses"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(SUBSTATION), - new Column("cc"), - new Column("v (pu)"), - new Column("theta (rad)"), - new Column("p (MW)"), - new Column("q (MVar)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID))) { + getTableTitle("Buses"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getBusesColumns())) { writeBuses(context, formatter); @@ -369,26 +240,9 @@ private void writeBuses(AmplExportContext context, TableFormatter formatter) thr for (Bus b : AmplUtil.getBuses(network)) { int ccNum = ConnectedComponents.getCcNum(b); if (connectedComponentToExport(ccNum)) { - String id = b.getId(); - VoltageLevel vl = b.getVoltageLevel(); - context.busIdsToExport.add(id); - int num = mapper.getInt(AmplSubset.BUS, id); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); - double nomV = vl.getNominalV(); - double v = b.getV() / nomV; - double theta = Math.toRadians(b.getAngle()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(vlNum) - .writeCell(ccNum) - .writeCell(v) - .writeCell(theta) - .writeCell(b.getP()) - .writeCell(b.getQ()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id); - addExtensions(num, b); + context.busIdsToExport.add(b.getId()); + columnsExporter.writeBusesColumnsToFormatter(formatter, b); + addExtensions(mapper.getInt(AmplSubset.BUS, b.getId()), b); } } } @@ -421,53 +275,19 @@ private void writeThreeWindingsTransformerMiddleBuses(AmplExportContext context, int middleCcNum = getThreeWindingsTransformerMiddleBusComponentNum(context, twt); if (connectedComponentToExport(middleCcNum)) { - String middleBusId = getThreeWindingsTransformerMiddleBusId(twt); - String middleVlId = getThreeWindingsTransformerMiddleVoltageLevelId(twt); + String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); context.busIdsToExport.add(middleBusId); - int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); - int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - formatter.writeCell(variantIndex) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(Float.NaN) - .writeCell(Double.NaN) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(middleBusId); + columnsExporter.writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(formatter, twt, middleCcNum); } } } private void writeDanglingLineMiddleBuses(AmplExportContext context, TableFormatter formatter) throws IOException { for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { - Terminal t = dl.getTerminal(); - Bus b = AmplUtil.getBus(dl.getTerminal()); - int middleCcNum = getDanglingLineMiddleBusComponentNum(context, dl); if (connectedComponentToExport(middleCcNum)) { - String middleBusId = getDanglingLineMiddleBusId(dl); - String middleVlId = getDanglingLineMiddleVoltageLevelId(dl); - context.busIdsToExport.add(middleBusId); - int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); - int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - SV sv = new SV(t.getP(), t.getQ(), b != null ? b.getV() : Double.NaN, b != null ? b.getAngle() : Double.NaN, Branch.Side.ONE).otherSide(dl, true); - double nomV = t.getVoltageLevel().getNominalV(); - double v = sv.getU() / nomV; - double theta = Math.toRadians(sv.getA()); - formatter.writeCell(variantIndex) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(v) - .writeCell(theta) - .writeCell(0.0) // 0 MW injected at dangling line internal bus - .writeCell(0.0) // 0 MVar injected at dangling line internal bus - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(middleBusId); + context.busIdsToExport.add(AmplUtil.getDanglingLineMiddleBusId(dl)); + columnsExporter.writeDanglingLineMiddleBusesToFormatter(formatter, dl, middleCcNum); } } } @@ -477,58 +297,21 @@ private void writeTieLineMiddleBuses(AmplExportContext context, TableFormatter f int xNodeCcNum = getTieLineMiddleBusComponentNum(context, tieLine); if (connectedComponentToExport(xNodeCcNum)) { - String xNodeBusId = AmplUtil.getXnodeBusId(tieLine); - int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId); - int xNodeVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)); - formatter.writeCell(variantIndex) - .writeCell(xNodeBusNum) - .writeCell(xNodeVlNum) - .writeCell(xNodeCcNum) - .writeCell(Float.NaN) - .writeCell(Double.NaN) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(xNodeBusId); + columnsExporter.writeTieLineMiddleBusesToFormatter(formatter, tieLine, xNodeCcNum); } } } private void writeBranches(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_branches", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_branches", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Branches"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("bus1"), - new Column("bus2"), - new Column("3wt num"), - new Column("sub.1"), - new Column("sub.2"), - new Column("r (pu)"), - new Column("x (pu)"), - new Column("g1 (pu)"), - new Column("g2 (pu)"), - new Column("b1 (pu)"), - new Column("b2 (pu)"), - new Column("cst ratio (pu)"), - new Column("ratio tc"), - new Column("phase tc"), - new Column("p1 (MW)"), - new Column("p2 (MW)"), - new Column("q1 (MVar)"), - new Column("q2 (MVar)"), - new Column("patl1 (A)"), - new Column("patl2 (A)"), - new Column("merged"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION))) { + getTableTitle("Branches"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getBranchesColumns() + )) { writeLines(context, formatter); @@ -553,52 +336,14 @@ private void writeLines(AmplExportContext context, TableFormatter formatter) thr continue; } String bus1Id = getBusId(bus1); - int bus1Num = getBusNum(bus1); String bus2Id = getBusId(bus2); - int bus2Num = getBusNum(bus2); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) { continue; } - VoltageLevel vl1 = t1.getVoltageLevel(); - VoltageLevel vl2 = t2.getVoltageLevel(); - context.voltageLevelIdsToExport.add(vl1.getId()); - context.voltageLevelIdsToExport.add(vl2.getId()); - String id = l.getId(); - int num = mapper.getInt(AmplSubset.BRANCH, id); - int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); - int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); - - double vb = vl1.getNominalV(); - double zb = vb * vb / AmplConstants.SB; - - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(bus1Num) - .writeCell(bus2Num) - .writeCell(-1) - .writeCell(vl1Num) - .writeCell(vl2Num) - .writeCell(l.getR() / zb) - .writeCell(l.getX() / zb) - .writeCell(l.getG1() * zb) - .writeCell(l.getG2() * zb) - .writeCell(l.getB1() * zb) - .writeCell(l.getB2() * zb) - .writeCell(1f) // constant ratio - .writeCell(-1) // no ratio tap changer - .writeCell(-1) // no phase tap changer - .writeCell(t1.getP()) - .writeCell(t2.getP()) - .writeCell(t1.getQ()) - .writeCell(t2.getQ()) - .writeCell(getPermanentLimit(l.getCurrentLimits1().orElse(null))) - .writeCell(getPermanentLimit(l.getCurrentLimits2().orElse(null))) - .writeCell(false) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(l.getNameOrId()); - addExtensions(num, l); + context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId()); + context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId()); + columnsExporter.writeLinesToFormatter(formatter, l); + addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l); } } @@ -613,119 +358,14 @@ private void writeTieLines(AmplExportContext context, TableFormatter formatter) continue; } String bus1Id = getBusId(bus1); - int bus1Num = getBusNum(bus1); String bus2Id = getBusId(bus2); - int bus2Num = getBusNum(bus2); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) { continue; } - VoltageLevel vl1 = t1.getVoltageLevel(); - VoltageLevel vl2 = t2.getVoltageLevel(); - context.voltageLevelIdsToExport.add(vl1.getId()); - context.voltageLevelIdsToExport.add(vl2.getId()); - String id = l.getId(); - int num = mapper.getInt(AmplSubset.BRANCH, id); - int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); - int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); - - double vb = vl1.getNominalV(); - double zb = vb * vb / AmplConstants.SB; - - boolean merged = !config.isExportXNodes(); - if (config.isExportXNodes()) { - String dl1Id = l.getDanglingLine1().getId(); - String dl2Id = l.getDanglingLine2().getId(); - int dl1Num = mapper.getInt(AmplSubset.BRANCH, dl1Id); - int dl2Num = mapper.getInt(AmplSubset.BRANCH, dl2Id); - String xNodeBusId = AmplUtil.getXnodeBusId(l); - String xnodeVoltageLevelId = AmplUtil.getXnodeVoltageLevelId(l); - int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId); - int xNodeVoltageLevelNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, xnodeVoltageLevelId); - - formatter.writeCell(variantIndex) - .writeCell(dl1Num) - .writeCell(bus1Num) - .writeCell(xNodeBusNum) - .writeCell(-1) - .writeCell(vl1Num) - .writeCell(xNodeVoltageLevelNum) - .writeCell(l.getDanglingLine1().getR() / zb) - .writeCell(l.getDanglingLine1().getX() / zb) - .writeCell(l.getDanglingLine1().getG() * zb / 2) - .writeCell(l.getDanglingLine1().getG() * zb / 2) - .writeCell(l.getDanglingLine1().getB() * zb / 2) - .writeCell(l.getDanglingLine1().getB() * zb / 2) - .writeCell(1f) // constant ratio - .writeCell(-1) // no ratio tap changer - .writeCell(-1) // no phase tap changer - .writeCell(t1.getP()) - .writeCell(l.getDanglingLine1().getBoundary().getP()) // xnode node flow side 1 - .writeCell(t1.getQ()) - .writeCell(l.getDanglingLine1().getBoundary().getQ()) // xnode node flow side 1 - .writeCell(getPermanentLimit(l.getDanglingLine1().getCurrentLimits().orElse(null))) - .writeCell(Float.NaN) - .writeCell(merged) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(dl1Id) - .writeCell(l.getDanglingLine1().getNameOrId()); - formatter.writeCell(variantIndex) - .writeCell(dl2Num) - .writeCell(xNodeBusNum) - .writeCell(bus2Num) - .writeCell(-1) - .writeCell(xNodeVoltageLevelNum) - .writeCell(vl2Num) - .writeCell(l.getDanglingLine2().getR() / zb) - .writeCell(l.getDanglingLine2().getX() / zb) - .writeCell(l.getDanglingLine1().getG() * zb / 2) - .writeCell(l.getDanglingLine1().getG() * zb / 2) - .writeCell(l.getDanglingLine1().getB() * zb / 2) - .writeCell(l.getDanglingLine1().getB() * zb / 2) - .writeCell(1f) // constant ratio - .writeCell(-1) // no ratio tap changer - .writeCell(-1) // no phase tap changer - .writeCell(l.getDanglingLine2().getBoundary().getP()) // xnode node flow side 2 - .writeCell(t2.getP()) - .writeCell(l.getDanglingLine2().getBoundary().getQ()) // xnode node flow side 2 - .writeCell(t2.getQ()) - .writeCell(Float.NaN) - .writeCell(getPermanentLimit(l.getDanglingLine2().getCurrentLimits().orElse(null))) - .writeCell(merged) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(dl2Id) - .writeCell(l.getDanglingLine2().getNameOrId()); - } else { - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(bus1Num) - .writeCell(bus2Num) - .writeCell(-1) - .writeCell(vl1Num) - .writeCell(vl2Num) - .writeCell(l.getR() / zb) - .writeCell(l.getX() / zb) - .writeCell(l.getG1() * zb) - .writeCell(l.getG2() * zb) - .writeCell(l.getB1() * zb) - .writeCell(l.getB2() * zb) - .writeCell(1f) // constant ratio - .writeCell(-1) // no ratio tap changer - .writeCell(-1) // no phase tap changer - .writeCell(t1.getP()) - .writeCell(t2.getP()) - .writeCell(t1.getQ()) - .writeCell(t2.getQ()) - .writeCell(getPermanentLimit(l.getDanglingLine1().getCurrentLimits().orElse(null))) - .writeCell(getPermanentLimit(l.getDanglingLine2().getCurrentLimits().orElse(null))) - .writeCell(merged) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(l.getNameOrId()); - } - addExtensions(num, l); + context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId()); + context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId()); + columnsExporter.writeTieLineToFormatter(formatter, l); + addExtensions(mapper.getInt(AmplSubset.BRANCH, l.getId()), l); } } @@ -735,81 +375,17 @@ private void writeTwoWindingsTransformers(AmplExportContext context, TableFormat Terminal t2 = twt.getTerminal2(); Bus bus1 = AmplUtil.getBus(t1); Bus bus2 = AmplUtil.getBus(t2); - if (bus1 != null && bus2 != null && bus1 == bus2) { + if (bus1 != null && bus1 == bus2) { LOGGER.warn("Skipping transformer '{}' connected to the same bus at both sides", twt.getId()); continue; } - String bus1Id = getBusId(bus1); - int bus1Num = getBusNum(bus1); - String bus2Id = getBusId(bus2); - int bus2Num = getBusNum(bus2); - if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) { + if (isOnlyMainCc() && !(isBusExported(context, getBusId(bus1)) || isBusExported(context, getBusId(bus2)))) { continue; } - VoltageLevel vl1 = t1.getVoltageLevel(); - VoltageLevel vl2 = t2.getVoltageLevel(); - context.voltageLevelIdsToExport.add(vl1.getId()); - context.voltageLevelIdsToExport.add(vl2.getId()); - String id = twt.getId(); - int num = mapper.getInt(AmplSubset.BRANCH, id); - int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); - int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); - double vb1 = vl1.getNominalV(); - double vb2 = vl2.getNominalV(); - double zb2 = vb2 * vb2 / AmplConstants.SB; - double r = twt.getR() / zb2; - double x = twt.getX() / zb2; - double g1; - double g2; - double b1; - double b2; - if (config.isTwtSplitShuntAdmittance()) { - g1 = twt.getG() * zb2 / 2; - g2 = g1; - b1 = twt.getB() * zb2 / 2; - b2 = b1; - } else { - g1 = twt.getG() * zb2; - g2 = 0; - b1 = twt.getB() * zb2; - b2 = 0; - } - - double ratedU1 = twt.getRatedU1(); - double ratedU2 = twt.getRatedU2(); - double ratio = ratedU2 / vb2 / (ratedU1 / vb1); - RatioTapChanger rtc = twt.getRatioTapChanger(); - PhaseTapChanger ptc = twt.getPhaseTapChanger(); - int rtcNum = rtc != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, twt.getId()) : -1; - int ptcNum = ptc != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, twt.getId()) : -1; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(bus1Num) - .writeCell(bus2Num) - .writeCell(-1) - .writeCell(vl1Num) - .writeCell(vl2Num) - .writeCell(r) - .writeCell(x) - .writeCell(g1) - .writeCell(g2) - .writeCell(b1) - .writeCell(b2) - .writeCell(ratio) - .writeCell(rtcNum) - .writeCell(ptcNum) - .writeCell(t1.getP()) - .writeCell(t2.getP()) - .writeCell(t1.getQ()) - .writeCell(t2.getQ()) - .writeCell(getPermanentLimit(twt.getCurrentLimits1().orElse(null))) - .writeCell(getPermanentLimit(twt.getCurrentLimits2().orElse(null))) - .writeCell(false) // TODO to update - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(twt.getNameOrId()); - addExtensions(num, twt); + context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId()); + context.voltageLevelIdsToExport.add(t2.getVoltageLevel().getId()); + columnsExporter.writeTwoWindingsTranformerToFormatter(formatter, twt); + addExtensions(mapper.getInt(AmplSubset.BRANCH, twt.getId()), twt); } } @@ -831,152 +407,31 @@ private void writeThreeWindingsTransformers(AmplExportContext context, TableForm String id1 = twt.getId() + AmplConstants.LEG1_SUFFIX; String id2 = twt.getId() + AmplConstants.LEG2_SUFFIX; String id3 = twt.getId() + AmplConstants.LEG3_SUFFIX; - int num3wt = mapper.getInt(AmplSubset.THREE_WINDINGS_TRANSFO, twt.getId()); int num1 = mapper.getInt(AmplSubset.BRANCH, id1); int num2 = mapper.getInt(AmplSubset.BRANCH, id2); int num3 = mapper.getInt(AmplSubset.BRANCH, id3); - int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); - int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); - int vl3Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl3.getId()); String bus1Id = getBusId(bus1); - int bus1Num = getBusNum(bus1); String bus2Id = getBusId(bus2); - int bus2Num = getBusNum(bus2); String bus3Id = getBusId(bus3); - int bus3Num = getBusNum(bus3); - double vb1 = vl1.getNominalV(); - double vb2 = vl2.getNominalV(); - double vb3 = vl3.getNominalV(); - double zb1 = vb1 * vb1 / AmplConstants.SB; - double zb2 = vb2 * vb2 / AmplConstants.SB; - double zb3 = vb3 * vb3 / AmplConstants.SB; - double r1 = twt.getLeg1().getR() / zb1; - double x1 = twt.getLeg1().getX() / zb1; - double g1 = twt.getLeg1().getG() * zb1; - double b1 = twt.getLeg1().getB() * zb1; - - double r2 = twt.getLeg2().getR() / zb2; - double x2 = twt.getLeg2().getX() / zb2; - double g2 = twt.getLeg2().getG() * zb2; - double b2 = twt.getLeg2().getB() * zb2; - - double r3 = twt.getLeg3().getR() / zb3; - double x3 = twt.getLeg3().getX() / zb3; - double g3 = twt.getLeg3().getG() * zb3; - double b3 = twt.getLeg3().getB() * zb3; - - double ratedU1 = twt.getLeg1().getRatedU(); - double ratedU2 = twt.getLeg2().getRatedU(); - double ratedU3 = twt.getLeg3().getRatedU(); - double ratedU0 = twt.getRatedU0(); - double ratio1 = ratedU0 / ratedU1; - double ratio2 = ratedU0 / ratedU2; - double ratio3 = ratedU0 / ratedU3; - RatioTapChanger rtc1 = twt.getLeg1().getRatioTapChanger(); - RatioTapChanger rtc2 = twt.getLeg2().getRatioTapChanger(); - RatioTapChanger rtc3 = twt.getLeg3().getRatioTapChanger(); - PhaseTapChanger ptc1 = twt.getLeg1().getPhaseTapChanger(); - PhaseTapChanger ptc2 = twt.getLeg2().getPhaseTapChanger(); - PhaseTapChanger ptc3 = twt.getLeg3().getPhaseTapChanger(); - int rtc1Num = rtc1 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id1) : -1; - int rtc2Num = rtc2 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id2) : -1; - int rtc3Num = rtc3 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id3) : -1; - int ptc1Num = ptc1 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id1) : -1; - int ptc2Num = ptc2 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id2) : -1; - int ptc3Num = ptc3 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id3) : -1; - - int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, getThreeWindingsTransformerMiddleVoltageLevelId(twt)); - String middleBusId = getThreeWindingsTransformerMiddleBusId(twt); + + int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, + AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt)); + String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus1Id)) { - formatter.writeCell(variantIndex) - .writeCell(num1) - .writeCell(middleBusNum) - .writeCell(bus1Num) - .writeCell(num3wt) - .writeCell(middleVlNum) - .writeCell(vl1Num) - .writeCell(r1) - .writeCell(x1) - .writeCell(g1) - .writeCell(0.0) - .writeCell(b1) - .writeCell(0.0) - .writeCell(ratio1) - .writeCell(rtc1Num) - .writeCell(ptc1Num) - .writeCell(Double.NaN) - .writeCell(t1.getP()) - .writeCell(Double.NaN) - .writeCell(t1.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg1().getCurrentLimits().orElse(null))) - .writeCell(false) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id1) - .writeCell(""); + columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum, + ThreeWindingsTransformer.Side.ONE); addExtensions(num1, twt); } if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus2Id)) { - formatter.writeCell(variantIndex) - .writeCell(num2) - .writeCell(bus2Num) - .writeCell(middleBusNum) - .writeCell(num3wt) - .writeCell(vl2Num) - .writeCell(middleVlNum) - .writeCell(r2) - .writeCell(x2) - .writeCell(g2) - .writeCell(0.0) - .writeCell(b2) - .writeCell(0.0) - .writeCell(ratio2) - .writeCell(rtc2Num) - .writeCell(ptc2Num) - .writeCell(t2.getP()) - .writeCell(Double.NaN) - .writeCell(t2.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg2().getCurrentLimits().orElse(null))) - .writeCell(Double.NaN) - .writeCell(false) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id2) - .writeCell(""); + columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum, + ThreeWindingsTransformer.Side.TWO); addExtensions(num2, twt); } if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus3Id)) { - formatter.writeCell(variantIndex) - .writeCell(num3) - .writeCell(bus3Num) - .writeCell(middleBusNum) - .writeCell(num3wt) - .writeCell(vl3Num) - .writeCell(middleVlNum) - .writeCell(r3) - .writeCell(x3) - .writeCell(g3) - .writeCell(0.0) - .writeCell(b3) - .writeCell(0.0) - .writeCell(ratio3) - .writeCell(rtc3Num) - .writeCell(ptc3Num) - .writeCell(t3.getP()) - .writeCell(Double.NaN) - .writeCell(t3.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg3().getCurrentLimits().orElse(null))) - .writeCell(Double.NaN) - .writeCell(false) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id3) - .writeCell(""); + columnsExporter.writeThreeWindingsTransformerLegToFormatter(formatter, twt, middleBusNum, middleVlNum, + ThreeWindingsTransformer.Side.THREE); addExtensions(num3, twt); } } @@ -987,56 +442,16 @@ private void writeDanglingLines(AmplExportContext context, TableFormatter format Terminal t = dl.getTerminal(); Bus bus1 = AmplUtil.getBus(t); String bus1Id = getBusId(bus1); - int bus1Num = getBusNum(bus1); - String middleBusId = getDanglingLineMiddleBusId(dl); - int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, middleBusId))) { continue; } VoltageLevel vl = t.getVoltageLevel(); - String middleVlId = getDanglingLineMiddleVoltageLevelId(dl); + String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); context.voltageLevelIdsToExport.add(vl.getId()); context.voltageLevelIdsToExport.add(middleVlId); - String id = dl.getId(); - int num = mapper.getInt(AmplSubset.BRANCH, id); - int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); - int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - double vb = vl.getNominalV(); - double zb = vb * vb / AmplConstants.SB; - double p1 = t.getP(); - double q1 = t.getQ(); - SV sv = new SV(p1, q1, bus1 != null ? bus1.getV() : Double.NaN, bus1 != null ? bus1.getAngle() : Double.NaN, Branch.Side.ONE).otherSide(dl, true); - double p2 = sv.getP(); - double q2 = sv.getQ(); - double patl = getPermanentLimit(dl.getCurrentLimits().orElse(null)); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(bus1Num) - .writeCell(middleBusNum) - .writeCell(-1) - .writeCell(vl1Num) - .writeCell(middleVlNum) - .writeCell(dl.getR() / zb) - .writeCell(dl.getX() / zb) - .writeCell(dl.getG() / 2 * zb) - .writeCell(dl.getG() / 2 * zb) - .writeCell(dl.getB() / 2 * zb) - .writeCell(dl.getB() / 2 * zb) - .writeCell(1f) // constant ratio - .writeCell(-1) // no ratio tap changer - .writeCell(-1) // no phase tap changer - .writeCell(p1) - .writeCell(p2) - .writeCell(q1) - .writeCell(q2) - .writeCell(patl) - .writeCell(patl) - .writeCell(false) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(dl.getNameOrId()); - addExtensions(num, dl); + columnsExporter.writeDanglingLineToFormatter(formatter, dl); + addExtensions(mapper.getInt(AmplSubset.BRANCH, dl.getId()), dl); } } @@ -1049,20 +464,14 @@ private int getBusNum(Bus bus) { } private void writeTapChangerTable() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_tct", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_tct", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Tap changer table"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("tap"), - new Column("var ratio"), - new Column("x (pu)"), - new Column("angle (rad)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()))) { + getTableTitle("Tap changer table"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getTapChangerTableColumns())) { writeTwoWindingsTransformerTapChangerTable(formatter); @@ -1072,173 +481,39 @@ private void writeTapChangerTable() throws IOException { private void writeTwoWindingsTransformerTapChangerTable(TableFormatter formatter) throws IOException { for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { - Terminal t2 = twt.getTerminal2(); - double vb2 = t2.getVoltageLevel().getNominalV(); - double zb2 = vb2 * vb2 / AmplConstants.SB; - - RatioTapChanger rtc = twt.getRatioTapChanger(); - if (rtc != null) { - String id = twt.getId() + "_ratio_table"; - writeRatioTapChanger(formatter, id, zb2, twt.getX(), rtc); - } - - PhaseTapChanger ptc = twt.getPhaseTapChanger(); - if (ptc != null) { - String id = twt.getId() + "_phase_table"; - writePhaseTapChanger(formatter, id, zb2, twt.getX(), ptc); - } + columnsExporter.writeTwoWindingsTransformerTapChangerTableToFormatter(formatter, twt); } } private void writeThreeWindingsTransformerTapChangerTable(TableFormatter formatter) throws IOException { for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { - int legNumber = 0; - for (ThreeWindingsTransformer.Leg leg : twt.getLegs()) { - legNumber++; - RatioTapChanger rtc = leg.getRatioTapChanger(); - Terminal t = leg.getTerminal(); - double vb = t.getVoltageLevel().getNominalV(); - double zb = vb * vb / AmplConstants.SB; - if (rtc != null) { - String id = twt.getId() + "_leg" + legNumber + "_ratio_table"; - writeRatioTapChanger(formatter, id, zb, leg.getX(), rtc); - } - PhaseTapChanger ptc = leg.getPhaseTapChanger(); - if (ptc != null) { - String id = twt.getId() + "_leg" + legNumber + "_phase_table"; - writePhaseTapChanger(formatter, id, zb, leg.getX(), ptc); - } - } - } - } - - private void writeRatioTapChanger(TableFormatter formatter, String id, double zb2, double reactance, RatioTapChanger rtc) throws IOException { - int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id); - - for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) { - RatioTapChangerStep step = rtc.getStep(position); - double x = reactance * (1 + step.getX() / 100) / zb2; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(position - rtc.getLowTapPosition() + 1) - .writeCell(step.getRho()) - .writeCell(x) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum); - } - } - - private void writePhaseTapChanger(TableFormatter formatter, String id, double zb2, double reactance, PhaseTapChanger ptc) throws IOException { - int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id); - - for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { - PhaseTapChangerStep step = ptc.getStep(position); - double x = reactance * (1 + step.getX() / 100) / zb2; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(position - ptc.getLowTapPosition() + 1) - .writeCell(step.getRho()) - .writeCell(x) - .writeCell(Math.toRadians(step.getAlpha())) - .writeCell(faultNum) - .writeCell(actionNum); - } - } - - private void writeRatioTapChanger(TableFormatter formatter, Identifiable twt, RatioTapChanger rtc, String leg) { - try { - String rtcId = twt.getId() + leg; - String tcsId = twt.getId() + leg + "_ratio_table"; - int rtcNum = mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, rtcId); - int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); - formatter.writeCell(variantIndex) - .writeCell(rtcNum) - .writeCell(rtc.getTapPosition() - rtc.getLowTapPosition() + 1) - .writeCell(tcsNum) - .writeCell(rtc.hasLoadTapChangingCapabilities() && rtc.isRegulating()); - if (config.isExportRatioTapChangerVoltageTarget()) { - formatter.writeCell(rtc.getTargetV()); - } - formatter.writeCell(faultNum) - .writeCell(actionNum) - .writeCell(rtcId); - } catch (IOException e) { - throw new UncheckedIOException(e); + columnsExporter.writeThreeWindingsTransformerTapChangerTableToFormatter(formatter, twt); } } private void writeRatioTapChangers() throws IOException { - List columns = new ArrayList<>(8); - columns.add(new Column(VARIANT)); - columns.add(new Column(NUM)); - columns.add(new Column("tap")); - columns.add(new Column("table")); - columns.add(new Column("onLoad")); - if (config.isExportRatioTapChangerVoltageTarget()) { - columns.add(new Column("targetV")); - } - columns.add(new Column(FAULT)); - columns.add(new Column(config.getActionType().getLabel())); - columns.add(new Column(ID)); - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_rtc", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_rtc", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Ratio tap changers"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - columns.toArray(new Column[columns.size()]))) { - for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { - twt.getOptionalRatioTapChanger().ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, "")); - } - for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { - twt.getLeg1().getOptionalRatioTapChanger().ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG1_SUFFIX)); - twt.getLeg2().getOptionalRatioTapChanger().ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG2_SUFFIX)); - twt.getLeg3().getOptionalRatioTapChanger().ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG3_SUFFIX)); - } - } - } - - private void writePhaseTapChanger(TableFormatter formatter, Identifiable twt, PhaseTapChanger ptc, String leg) { - try { - String ptcId = twt.getId() + leg; - String tcsId = twt.getId() + leg + "_phase_table"; - int rtcNum = mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, ptcId); - int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); - formatter.writeCell(variantIndex) - .writeCell(rtcNum) - .writeCell(ptc.getTapPosition() - ptc.getLowTapPosition() + 1) - .writeCell(tcsNum) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(ptcId); - } catch (IOException e) { - throw new UncheckedIOException(e); + getTableTitle("Ratio tap changers"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getRtcColumns())) { + columnsExporter.writeRtcToFormatter(formatter); } } private void writePhaseTapChangers() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_ptc", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_ptc", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Phase tap changers"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("tap"), - new Column("table"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID))) { - for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { - twt.getOptionalPhaseTapChanger().ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, "")); - } - for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { - twt.getLeg1().getOptionalPhaseTapChanger().ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG1_SUFFIX)); - twt.getLeg2().getOptionalPhaseTapChanger().ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG2_SUFFIX)); - twt.getLeg3().getOptionalPhaseTapChanger().ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG3_SUFFIX)); - } + getTableTitle("Phase tap changers"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getPtcColumns())) { + columnsExporter.writePtcToFormatter(formatter); } } @@ -1256,79 +531,38 @@ private boolean exportLoad(AmplExportContext context, String busId) { } private void writeLoads(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_loads", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_loads", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Loads"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(SUBSTATION), - new Column(P0), - new Column("q0 (MVar)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column("p (MW)"), - new Column("q (MVar)"))) { + getTableTitle("Loads"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getLoadsColumns())) { List skipped = new ArrayList<>(); for (Load l : network.getLoads()) { Terminal t = l.getTerminal(); Bus bus = AmplUtil.getBus(t); String busId = null; - int busNum = -1; if (bus != null) { busId = bus.getId(); - busNum = mapper.getInt(AmplSubset.BUS, bus.getId()); } if (!exportLoad(context, busId)) { skipped.add(l.getId()); continue; } - String id = l.getId(); - context.loadsToExport.add(id); - int num = mapper.getInt(AmplSubset.LOAD, id); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(vlNum) - .writeCell(l.getP0()) - .writeCell(l.getQ0()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(l.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, l); + context.loadsToExport.add(l.getId()); + + columnsExporter.writeLoadtoFormatter(formatter, l); + addExtensions(mapper.getInt(AmplSubset.LOAD, l.getId()), l); } for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { - String middleBusId = getDanglingLineMiddleBusId(dl); - String id = dl.getId(); - int num = mapper.getInt(AmplSubset.LOAD, id); - int busNum = mapper.getInt(AmplSubset.BUS, middleBusId); + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); if (!exportLoad(context, middleBusId)) { skipped.add(dl.getId()); continue; } - String middleVlId = getDanglingLineMiddleVoltageLevelId(dl); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(vlNum) - .writeCell(dl.getP0()) - .writeCell(dl.getQ0()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(dl.getId() + "_load") - .writeCell("") - .writeCell(dl.getTerminal().getP()) - .writeCell(dl.getTerminal().getQ()); + columnsExporter.writeDanglingLineLoadToFormatter(formatter, dl); } if (!skipped.isEmpty()) { LOGGER.trace("Skip loads {} because not connected and not connectable", skipped); @@ -1351,90 +585,34 @@ private boolean exportGeneratorOrShunt(AmplExportContext context, String busId, } private void writeShunts(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_shunts", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_shunts", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Shunts"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column("minB (pu)"), - new Column("maxB (pu)"), - new Column("inter. points"), - new Column("b (pu)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER), - new Column("sections count"))) { + getTableTitle("Shunts"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getShuntsColumns())) { List skipped = new ArrayList<>(); for (ShuntCompensator sc : network.getShuntCompensators()) { Terminal t = sc.getTerminal(); Bus bus = AmplUtil.getBus(t); String busId = null; - int busNum = -1; if (bus != null) { busId = bus.getId(); - busNum = mapper.getInt(AmplSubset.BUS, bus.getId()); } - int conBusNum = -1; // take connectable bus if exists String conBusId = null; Bus conBus = AmplUtil.getConnectableBus(t); if (conBus != null) { conBusId = conBus.getId(); - conBusNum = mapper.getInt(AmplSubset.BUS, conBus.getId()); } if (!exportGeneratorOrShunt(context, busId, conBusId)) { skipped.add(sc.getId()); - continue; - } - String id = sc.getId(); - int num = mapper.getInt(AmplSubset.SHUNT, id); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - double vb = t.getVoltageLevel().getNominalV(); - double zb = vb * vb / AmplConstants.SB; - double b1 = 0; - double b2; - int points = 0; - int sectionCount = 1; - if (sc.getModelType() == ShuntCompensatorModelType.NON_LINEAR) { - // TODO non linear shunt has to be converted as multiple sections shunt. - if (sc.getSectionCount() > 1) { - b1 = sc.getB(sc.getSectionCount() - 1) * zb; - } - b2 = sc.getB() * zb; } else { - b2 = sc.getModel(ShuntCompensatorLinearModel.class).getBPerSection() * sc.getMaximumSectionCount() * zb; - points = sc.getMaximumSectionCount() < 1 ? 0 : sc.getMaximumSectionCount() - 1; - sectionCount = sc.getSectionCount(); + columnsExporter.writeShuntCompensatorToFormatter(formatter, sc); + addExtensions(mapper.getInt(AmplSubset.SHUNT, sc.getId()), sc); } - double b = sc.getB() * zb; - double minB = Math.min(b1, b2); - double maxB = Math.max(b1, b2); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(minB) - .writeCell(maxB) - .writeCell(points) - .writeCell(b) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(sc.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()) - .writeCell(sectionCount); - addExtensions(num, sc); } if (!skipped.isEmpty()) { LOGGER.trace("Skip shunts {} because not connected and not connectable", skipped); @@ -1443,62 +621,18 @@ private void writeShunts(AmplExportContext context) throws IOException { } private void writeStaticVarCompensators() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_static_var_compensators", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter( + dataSource.newOutputStream("_network_static_var_compensators", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Static VAR compensators"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column("minB (pu)"), - new Column("maxB (pu)"), - new Column(V_REGUL), - new Column(TARGET_V), - new Column(TARGET_Q), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER))) { + getTableTitle("Static VAR compensators"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getStaticVarCompensatorColumns())) { List skipped = new ArrayList<>(); for (StaticVarCompensator svc : network.getStaticVarCompensators()) { - - String id = svc.getId(); - int num = mapper.getInt(AmplSubset.STATIC_VAR_COMPENSATOR, id); - - Terminal t = svc.getTerminal(); - - int busNum = AmplUtil.getBusNum(mapper, t); - - int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); - - double vlSet = svc.getVoltageSetpoint(); - double vb = t.getVoltageLevel().getNominalV(); - double zb = vb * vb / AmplConstants.SB; // Base impedance - - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum) - .writeCell(vlNum) - .writeCell(svc.getBmin() * zb) - .writeCell(svc.getBmax() * zb) - .writeCell(svc.getRegulationMode().equals(RegulationMode.VOLTAGE)) - .writeCell(vlSet / vb) - .writeCell(svc.getReactivePowerSetpoint()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(svc.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, svc); + columnsExporter.writeStaticVarCompensatorToFormatter(formatter, svc); + addExtensions(mapper.getInt(AmplSubset.STATIC_VAR_COMPENSATOR, svc.getId()), svc); } if (!skipped.isEmpty()) { LOGGER.trace("Skip static VAR compensators {} because not connected and not connectable", skipped); @@ -1507,89 +641,35 @@ private void writeStaticVarCompensators() throws IOException { } private void writeGenerators(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_generators", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_generators", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Generators"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column(MINP), - new Column(MAXP), - new Column(MIN_Q_MAX_P), - new Column(MIN_Q0), - new Column(MIN_Q_MIN_P), - new Column(MAX_Q_MAX_P), - new Column(MAX_Q0), - new Column(MAX_Q_MIN_P), - new Column(V_REGUL), - new Column(TARGET_V), - new Column("targetP (MW)"), - new Column(TARGET_Q), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER))) { + getTableTitle("Generators"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getGeneratorsColumns())) { List skipped = new ArrayList<>(); for (Generator g : network.getGenerators()) { Terminal t = g.getTerminal(); Bus bus = AmplUtil.getBus(t); String busId = null; - int busNum = -1; if (bus != null) { busId = bus.getId(); - busNum = mapper.getInt(AmplSubset.BUS, bus.getId()); } String conBusId = null; - int conBusNum = -1; // take connectable bus if exists Bus conBus = AmplUtil.getConnectableBus(t); if (conBus != null) { conBusId = conBus.getId(); - conBusNum = mapper.getInt(AmplSubset.BUS, conBus.getId()); } if (!exportGeneratorOrShunt(context, busId, conBusId)) { skipped.add(g.getId()); - continue; + } else { + context.generatorIdsToExport.add(g.getId()); + columnsExporter.writeGeneratorToFormatter(formatter, g); + addExtensions(mapper.getInt(AmplSubset.GENERATOR, g.getId()), g); } - String id = g.getId(); - context.generatorIdsToExport.add(id); - int num = mapper.getInt(AmplSubset.GENERATOR, id); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - double minP = g.getMinP(); - double maxP = g.getMaxP(); - double vb = t.getVoltageLevel().getNominalV(); - - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(minP) - .writeCell(maxP) - .writeCell(g.getReactiveLimits().getMinQ(maxP)) - .writeCell(g.getReactiveLimits().getMinQ(0)) - .writeCell(g.getReactiveLimits().getMinQ(minP)) - .writeCell(g.getReactiveLimits().getMaxQ(maxP)) - .writeCell(g.getReactiveLimits().getMaxQ(0)) - .writeCell(g.getReactiveLimits().getMaxQ(minP)) - .writeCell(g.isVoltageRegulatorOn()) - .writeCell(g.getTargetV() / vb) - .writeCell(g.getTargetP()) - .writeCell(g.getTargetQ()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(g.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, g); } if (!skipped.isEmpty()) { LOGGER.trace("Skip generators {} because not connected and not connectable", skipped); @@ -1598,33 +678,14 @@ private void writeGenerators(AmplExportContext context) throws IOException { } private void writeBatteries(AmplExportContext context) throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_batteries", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_batteries", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Batteries"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column(P0), - new Column(Q0), - new Column(MINP), - new Column(MAXP), - new Column(MIN_Q_MAX_P), - new Column(MIN_Q0), - new Column(MIN_Q_MIN_P), - new Column(MAX_Q_MAX_P), - new Column(MAX_Q0), - new Column(MAX_Q_MIN_P), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER))) { + getTableTitle("Batteries"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getBatteriesColumns())) { List skipped = new ArrayList<>(); for (Battery b : network.getBatteries()) { Terminal t = b.getTerminal(); @@ -1643,40 +704,11 @@ private void writeBatteries(AmplExportContext context) throws IOException { } if (!isBusExported(context, busId)) { skipped.add(b.getId()); - continue; + } else { + context.batteryIdsToExport.add(b.getId()); + columnsExporter.writeBatteryToFormatter(formatter, b); + addExtensions(mapper.getInt(AmplSubset.BATTERY, b.getId()), b); } - - String id = b.getId(); - context.batteryIdsToExport.add(id); - int num = mapper.getInt(AmplSubset.BATTERY, id); - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - double targetP = b.getTargetP(); - double targetQ = b.getTargetQ(); - double minP = b.getMinP(); - double maxP = b.getMaxP(); - - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(targetP) - .writeCell(targetQ) - .writeCell(minP) - .writeCell(maxP) - .writeCell(b.getReactiveLimits().getMinQ(maxP)) - .writeCell(b.getReactiveLimits().getMinQ(0)) - .writeCell(b.getReactiveLimits().getMinQ(minP)) - .writeCell(b.getReactiveLimits().getMaxQ(maxP)) - .writeCell(b.getReactiveLimits().getMaxQ(0)) - .writeCell(b.getReactiveLimits().getMaxQ(minP)) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(b.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, b); } if (!skipped.isEmpty()) { LOGGER.trace("Skip batteries {} because not connected and not connectable", skipped); @@ -1684,288 +716,72 @@ private void writeBatteries(AmplExportContext context) throws IOException { } } - private void writeTemporaryCurrentLimits(CurrentLimits limits, TableFormatter formatter, String branchId, boolean side1, String sideId) throws IOException { - int branchNum = mapper.getInt(AmplSubset.BRANCH, branchId); - for (TemporaryLimit tl : limits.getTemporaryLimits()) { - String limitId = branchId + "_" + sideId + "_" + tl.getAcceptableDuration(); - int limitNum = mapper.getInt(AmplSubset.TEMPORARY_CURRENT_LIMIT, limitId); - formatter.writeCell(variantIndex) - .writeCell(limitNum) - .writeCell(branchNum) - .writeCell(side1 ? 1 : 2) - .writeCell(tl.getValue()) - .writeCell(tl.getAcceptableDuration()) - .writeCell(faultNum) - .writeCell(actionNum); - } - } - private void writeCurrentLimits() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_limits", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_limits", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("Temporary current limits"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("branch"), - new Column("side"), - new Column("limit (A)"), - new Column("accept. duration (s)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()))) { - - writeBranchCurrentLimits(formatter); - - writeTieLineCurrentLimits(formatter); - - writeThreeWindingsTransformerCurrentLimits(formatter); - - writeDanglingLineCurrentLimits(formatter); - } - } - - private void writeBranchCurrentLimits(TableFormatter formatter) throws IOException { - for (Branch branch : network.getBranches()) { - String branchId = branch.getId(); - Optional currentLimits1 = branch.getCurrentLimits1(); - if (currentLimits1.isPresent()) { - writeTemporaryCurrentLimits(currentLimits1.get(), formatter, branchId, true, "_1_"); - } - Optional currentLimits2 = branch.getCurrentLimits2(); - if (currentLimits2.isPresent()) { - writeTemporaryCurrentLimits(currentLimits2.get(), formatter, branchId, false, "_2_"); - } - } - } - - private void writeTieLineCurrentLimits(TableFormatter formatter) throws IOException { - for (TieLine line : network.getTieLines()) { - String lineId = line.getId(); - Optional currentLimits1 = line.getDanglingLine1().getCurrentLimits(); - if (currentLimits1.isPresent()) { - writeTemporaryCurrentLimits(currentLimits1.get(), formatter, lineId, true, "_1_"); - } - Optional currentLimits2 = line.getDanglingLine2().getCurrentLimits(); - if (currentLimits2.isPresent()) { - writeTemporaryCurrentLimits(currentLimits2.get(), formatter, lineId, false, "_2_"); - } - } - } - - private void writeThreeWindingsTransformerCurrentLimits(TableFormatter formatter) throws IOException { - for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { - Optional currentLimits1 = twt.getLeg1().getCurrentLimits(); - if (currentLimits1.isPresent()) { - String branchId = twt.getId() + AmplConstants.LEG1_SUFFIX; - writeTemporaryCurrentLimits(currentLimits1.get(), formatter, branchId, true, ""); - } - Optional currentLimits2 = twt.getLeg2().getCurrentLimits(); - if (currentLimits2.isPresent()) { - String branchId = twt.getId() + AmplConstants.LEG2_SUFFIX; - writeTemporaryCurrentLimits(currentLimits2.get(), formatter, branchId, true, ""); - } - Optional currentLimits3 = twt.getLeg3().getCurrentLimits(); - if (currentLimits3.isPresent()) { - String branchId = twt.getId() + AmplConstants.LEG3_SUFFIX; - writeTemporaryCurrentLimits(currentLimits3.get(), formatter, branchId, true, ""); - } - } - } + getTableTitle("Temporary current limits"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getCurrentLimitsColumns())) { - private void writeDanglingLineCurrentLimits(TableFormatter formatter) throws IOException { - for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { - String branchId = dl.getId(); - Optional currentLimits = dl.getCurrentLimits(); - if (currentLimits.isPresent()) { - writeTemporaryCurrentLimits(currentLimits.get(), formatter, branchId, true, ""); - } + columnsExporter.writeCurrentLimits(formatter); } } private void writeHvdcLines() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_hvdc", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_hvdc", "txt", append), + StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("HVDC lines"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column("type"), - new Column("converterStation1"), - new Column("converterStation2"), - new Column("r (ohm)"), - new Column("nomV (KV)"), - new Column("convertersMode"), - new Column("targetP (MW)"), - new Column(MAXP), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION))) { + getTableTitle("HVDC lines"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getHvdcLinesColumns())) { for (HvdcLine hvdcLine : network.getHvdcLines()) { - String id = hvdcLine.getId(); - int num = mapper.getInt(AmplSubset.HVDC_LINE, id); - HvdcType type = hvdcLine.getConverterStation1().getHvdcType(); - AmplSubset subset = type.equals(HvdcType.VSC) ? AmplSubset.VSC_CONVERTER_STATION : AmplSubset.LCC_CONVERTER_STATION; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(type.equals(HvdcType.VSC) ? 1 : 2) - .writeCell(mapper.getInt(subset, hvdcLine.getConverterStation1().getId())) - .writeCell(mapper.getInt(subset, hvdcLine.getConverterStation2().getId())) - .writeCell(hvdcLine.getR()) - .writeCell(hvdcLine.getNominalV()) - .writeCell(hvdcLine.getConvertersMode().name()) - .writeCell(hvdcLine.getActivePowerSetpoint()) - .writeCell(hvdcLine.getMaxP()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(hvdcLine.getNameOrId()); - addExtensions(num, hvdcLine); + columnsExporter.writeHvdcToFormatter(formatter, hvdcLine); + addExtensions(mapper.getInt(AmplSubset.HVDC_LINE, hvdcLine.getId()), hvdcLine); } } } - private HashMap getHvdcLinesMap() { - HashMap lineMap = new HashMap<>(); - network.getHvdcLines().forEach(line -> { - if (line.getConverterStation1() != null) { - lineMap.put(line.getConverterStation1().getId(), line); - } - if (line.getConverterStation2() != null) { - lineMap.put(line.getConverterStation2().getId(), line); - } - }); - return lineMap; - } - private void writeLccConverterStations() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_lcc_converter_stations", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter( + dataSource.newOutputStream("_network_lcc_converter_stations", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("LCC Converter Stations"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column("lossFactor (%PDC)"), - new Column("powerFactor"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER))) { + getTableTitle("LCC Converter Stations"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getLccConverterStationsColumns())) { for (HvdcConverterStation hvdcStation : network.getHvdcConverterStations()) { if (hvdcStation.getHvdcType().equals(HvdcType.LCC)) { - Terminal t = hvdcStation.getTerminal(); - int busNum = AmplUtil.getBusNum(mapper, t); - int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); - LccConverterStation lccStation = (LccConverterStation) hvdcStation; - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - - int num = mapper.getInt(AmplSubset.LCC_CONVERTER_STATION, lccStation.getId()); - - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(lccStation.getLossFactor()) - .writeCell(lccStation.getPowerFactor()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(lccStation.getId()) - .writeCell(lccStation.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, lccStation); + columnsExporter.writeLccConverterStationToFormatter(formatter, lccStation); + addExtensions(mapper.getInt(AmplSubset.LCC_CONVERTER_STATION, lccStation.getId()), lccStation); } } } } private void writeVscConverterStations() throws IOException { - try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_vsc_converter_stations", "txt", append), StandardCharsets.UTF_8); + try (Writer writer = new OutputStreamWriter( + dataSource.newOutputStream("_network_vsc_converter_stations", "txt", append), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter(writer, - getTableTitle("VSC Converter Stations"), - AmplConstants.INVALID_FLOAT_VALUE, - !append, - AmplConstants.LOCALE, - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column(MINP), - new Column(MAXP), - new Column(MIN_Q_MAX_P), - new Column(MIN_Q0), - new Column(MIN_Q_MIN_P), - new Column(MAX_Q_MAX_P), - new Column(MAX_Q0), - new Column(MAX_Q_MIN_P), - new Column(V_REGUL), - new Column(TARGET_V), - new Column(TARGET_Q), - new Column("lossFactor (%PDC)"), - new Column(FAULT), - new Column(config.getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER))) { - - HashMap lineMap = getHvdcLinesMap(); + getTableTitle("VSC Converter Stations"), + AmplConstants.INVALID_FLOAT_VALUE, + !append, + AmplConstants.LOCALE, + columnsExporter.getVscConverterStationsColumns())) { for (HvdcConverterStation hvdcStation : network.getHvdcConverterStations()) { if (hvdcStation.getHvdcType().equals(HvdcType.VSC)) { - String id = hvdcStation.getId(); - Terminal t = hvdcStation.getTerminal(); - int busNum = AmplUtil.getBusNum(mapper, t); - int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); - - double maxP = lineMap.get(id) != null ? lineMap.get(id).getMaxP() : Double.NaN; - VscConverterStation vscStation = (VscConverterStation) hvdcStation; - int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - double vlSet = vscStation.getVoltageSetpoint(); - double vb = t.getVoltageLevel().getNominalV(); - double minP = -maxP; - - int num = mapper.getInt(AmplSubset.VSC_CONVERTER_STATION, vscStation.getId()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(minP) - .writeCell(maxP) - .writeCell(vscStation.getReactiveLimits().getMinQ(maxP)) - .writeCell(vscStation.getReactiveLimits().getMinQ(0)) - .writeCell(vscStation.getReactiveLimits().getMinQ(minP)) - .writeCell(vscStation.getReactiveLimits().getMaxQ(maxP)) - .writeCell(vscStation.getReactiveLimits().getMaxQ(0)) - .writeCell(vscStation.getReactiveLimits().getMaxQ(minP)) - .writeCell(vscStation.isVoltageRegulatorOn()) - .writeCell(vlSet / vb) - .writeCell(vscStation.getReactivePowerSetpoint()) - .writeCell(vscStation.getLossFactor()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(vscStation.getId()) - .writeCell(vscStation.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); - addExtensions(num, vscStation); + columnsExporter.writeVscConverterStationToFormatter(formatter, vscStation); + addExtensions(mapper.getInt(AmplSubset.VSC_CONVERTER_STATION, vscStation.getId()), vscStation); + } } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java index 5a4246c1ac8..e45c6a0d625 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java @@ -7,10 +7,13 @@ */ package com.powsybl.ampl.converter; +import com.powsybl.commons.PowsyblException; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.HvdcConverterStation.HvdcType; +import java.util.Objects; + /** * @author Geoffroy Jamgotchian */ @@ -54,14 +57,30 @@ public static StringToIntMapper createMapper(Network network) { return mapper; } - static String getXnodeBusId(TieLine tieLine) { + public static String getXnodeBusId(TieLine tieLine) { return tieLine.getUcteXnodeCode(); } - static String getXnodeVoltageLevelId(TieLine tieLine) { + public static String getXnodeVoltageLevelId(TieLine tieLine) { return tieLine.getUcteXnodeCode(); } + public static String getThreeWindingsTransformerMiddleBusId(ThreeWindingsTransformer twt) { + return twt.getId(); // same id as the transformer + } + + public static String getThreeWindingsTransformerMiddleVoltageLevelId(ThreeWindingsTransformer twt) { + return twt.getId(); // same id as the transformer + } + + public static String getDanglingLineMiddleBusId(DanglingLine dl) { + return dl.getId(); // same id as the dangling line + } + + public static String getDanglingLineMiddleVoltageLevelId(DanglingLine dl) { + return dl.getId(); // same id as the dangling line + } + public static void fillMapper(StringToIntMapper mapper, Network network) { // Network mapper.newInt(AmplSubset.NETWORK, network.getId()); @@ -226,4 +245,17 @@ public static void resetNetworkMapping(StringToIntMapper mapper) { mapper.reset(AmplSubset.HVDC_LINE); } + public static String getLegSuffix(ThreeWindingsTransformer.Side leg) { + Objects.requireNonNull(leg); + switch (leg) { + case ONE: + return AmplConstants.LEG1_SUFFIX; + case TWO: + return AmplConstants.LEG2_SUFFIX; + case THREE: + return AmplConstants.LEG3_SUFFIX; + } + throw new PowsyblException("Unsupported three windings leg."); + } + } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java new file mode 100644 index 00000000000..31365a63d24 --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -0,0 +1,1448 @@ +/** + * 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/. + */ +package com.powsybl.ampl.converter.version; + +import com.powsybl.ampl.converter.AmplConstants; +import com.powsybl.ampl.converter.AmplExportConfig; +import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.ampl.converter.AmplUtil; +import com.powsybl.commons.io.table.Column; +import com.powsybl.commons.io.table.TableFormatter; +import com.powsybl.commons.util.StringToIntMapper; +import com.powsybl.iidm.network.*; +import com.powsybl.iidm.network.util.ConnectedComponents; +import com.powsybl.iidm.network.util.SV; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.*; + +import static com.powsybl.ampl.converter.AmplConstants.*; + +/** + * @author Nicolas Pierre + * @author Geoffroy Jamgotchian + */ +public class BasicAmplExporter implements AmplColumnsExporter { + + private final AmplExportConfig config; + private final Network network; + private final StringToIntMapper mapper; + private final int variantIndex; + private final int faultNum; + private final int actionNum; + private HashMap hvdcLinesMap; + + public static AmplColumnsExporter.Factory getFactory() { + return BasicAmplExporter::new; + } + + public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum) { + + this.config = Objects.requireNonNull(config); + this.network = Objects.requireNonNull(network); + this.mapper = Objects.requireNonNull(mapper); + this.variantIndex = variantIndex; + this.faultNum = faultNum; + this.actionNum = actionNum; + } + + @Override + public List getRtcColumns() { + List columns = new ArrayList<>(8); + columns.add(new Column(VARIANT)); + columns.add(new Column(NUM)); + columns.add(new Column("tap")); + columns.add(new Column("table")); + columns.add(new Column("onLoad")); + if (config.isExportRatioTapChangerVoltageTarget()) { + columns.add(new Column("targetV")); + } + columns.add(new Column(FAULT)); + columns.add(new Column(config.getActionType().getLabel())); + columns.add(new Column(ID)); + return columns; + } + + @Override + public List getPtcColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column("tap"), + new Column("table"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID)); + } + + @Override + public List getCurrentLimitsColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column("branch"), + new Column("side"), + new Column("limit (A)"), + new Column("accept. duration (s)"), + new Column(FAULT), + new Column(config.getActionType().getLabel())); + } + + @Override + public List getHvdcLinesColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column("type"), + new Column("converterStation1"), + new Column("converterStation2"), + new Column("r (ohm)"), + new Column("nomV (KV)"), + new Column("convertersMode"), + new Column("targetP (MW)"), + new Column(MAXP), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION) + ); + } + + @Override + public List getLccConverterStationsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column("lossFactor (%PDC)"), + new Column("powerFactor"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getVscConverterStationsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column(MINP), + new Column(MAXP), + new Column(MIN_Q_MAX_P), + new Column(MIN_Q0), + new Column(MIN_Q_MIN_P), + new Column(MAX_Q_MAX_P), + new Column(MAX_Q0), + new Column(MAX_Q_MIN_P), + new Column(V_REGUL), + new Column(TARGET_V), + new Column(TARGET_Q), + new Column("lossFactor (%PDC)"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getSubstationsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column("unused1"), + new Column("unused2"), + new Column("nomV (KV)"), + new Column("minV (pu)"), + new Column("maxV (pu)"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column("country"), + new Column(ID), + new Column(DESCRIPTION) + ); + } + + @Override + public List getLoadsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(SUBSTATION), + new Column(P0), + new Column("q0 (MVar)"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column("p (MW)"), + new Column("q (MVar)") + ); + } + + @Override + public List getShuntsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column("minB (pu)"), + new Column("maxB (pu)"), + new Column("inter. points"), + new Column("b (pu)"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER), + new Column("sections count") + ); + } + + @Override + public List getStaticVarCompensatorColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column("minB (pu)"), + new Column("maxB (pu)"), + new Column(V_REGUL), + new Column(TARGET_V), + new Column(TARGET_Q), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getGeneratorsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column(MINP), + new Column(MAXP), + new Column(MIN_Q_MAX_P), + new Column(MIN_Q0), + new Column(MIN_Q_MIN_P), + new Column(MAX_Q_MAX_P), + new Column(MAX_Q0), + new Column(MAX_Q_MIN_P), + new Column(V_REGUL), + new Column(TARGET_V), + new Column("targetP (MW)"), + new Column(TARGET_Q), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getBatteriesColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column(P0), + new Column(Q0), + new Column(MINP), + new Column(MAXP), + new Column(MIN_Q_MAX_P), + new Column(MIN_Q0), + new Column(MIN_Q_MIN_P), + new Column(MAX_Q_MAX_P), + new Column(MAX_Q0), + new Column(MAX_Q_MIN_P), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getBranchesColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column("bus1"), + new Column("bus2"), + new Column("3wt num"), + new Column("sub.1"), + new Column("sub.2"), + new Column("r (pu)"), + new Column("x (pu)"), + new Column("g1 (pu)"), + new Column("g2 (pu)"), + new Column("b1 (pu)"), + new Column("b2 (pu)"), + new Column("cst ratio (pu)"), + new Column("ratio tc"), + new Column("phase tc"), + new Column("p1 (MW)"), + new Column("p2 (MW)"), + new Column("q1 (MVar)"), + new Column("q2 (MVar)"), + new Column("patl1 (A)"), + new Column("patl2 (A)"), + new Column("merged"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION)); + } + + @Override + public List getTapChangerTableColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column("tap"), + new Column("var ratio"), + new Column("x (pu)"), + new Column("angle (rad)"), + new Column(FAULT), + new Column(config.getActionType().getLabel())); + } + + @Override + public List getBusesColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column(SUBSTATION), + new Column("cc"), + new Column("v (pu)"), + new Column("theta (rad)"), + new Column("p (MW)"), + new Column("q (MVar)"), + new Column(FAULT), + new Column(config.getActionType().getLabel()), + new Column(ID)); + } + + @Override + public void writeTwoWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + TwoWindingsTransformer twt) throws IOException { + Terminal t2 = twt.getTerminal2(); + double vb2 = t2.getVoltageLevel().getNominalV(); + double zb2 = vb2 * vb2 / AmplConstants.SB; + + RatioTapChanger rtc = twt.getRatioTapChanger(); + if (rtc != null) { + String id = twt.getId() + RATIO_TABLE; + writeRatioTapChanger(formatter, id, zb2, twt.getX(), rtc); + } + + PhaseTapChanger ptc = twt.getPhaseTapChanger(); + if (ptc != null) { + String id = twt.getId() + PHASE_TABLE; + writePhaseTapChanger(formatter, id, zb2, twt.getX(), ptc); + } + } + + @Override + public void writeThreeWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt) throws IOException { + int legNumber = 0; + for (ThreeWindingsTransformer.Leg leg : twt.getLegs()) { + legNumber++; + RatioTapChanger rtc = leg.getRatioTapChanger(); + Terminal t = leg.getTerminal(); + double vb = t.getVoltageLevel().getNominalV(); + double zb = vb * vb / AmplConstants.SB; + if (rtc != null) { + String id = twt.getId() + "_leg" + legNumber + RATIO_TABLE; + writeRatioTapChanger(formatter, id, zb, leg.getX(), rtc); + } + PhaseTapChanger ptc = leg.getPhaseTapChanger(); + if (ptc != null) { + String id = twt.getId() + "_leg" + legNumber + PHASE_TABLE; + writePhaseTapChanger(formatter, id, zb, leg.getX(), ptc); + } + } + } + + @Override + public void writeRtcToFormatter(TableFormatter formatter) { + for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { + twt.getOptionalRatioTapChanger().ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, "")); + } + for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { + twt.getLeg1() + .getOptionalRatioTapChanger() + .ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG1_SUFFIX)); + twt.getLeg2() + .getOptionalRatioTapChanger() + .ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG2_SUFFIX)); + twt.getLeg3() + .getOptionalRatioTapChanger() + .ifPresent(rtc -> writeRatioTapChanger(formatter, twt, rtc, AmplConstants.LEG3_SUFFIX)); + } + } + + @Override + public void writePtcToFormatter(TableFormatter formatter) { + for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { + twt.getOptionalPhaseTapChanger().ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, "")); + } + for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { + twt.getLeg1() + .getOptionalPhaseTapChanger() + .ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG1_SUFFIX)); + twt.getLeg2() + .getOptionalPhaseTapChanger() + .ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG2_SUFFIX)); + twt.getLeg3() + .getOptionalPhaseTapChanger() + .ifPresent(ptc -> writePhaseTapChanger(formatter, twt, ptc, AmplConstants.LEG3_SUFFIX)); + } + } + + @Override + public void writeCurrentLimits(TableFormatter formatter) throws IOException { + writeBranchCurrentLimits(formatter); + writeTieLineCurrentLimits(formatter); + writeThreeWindingsTransformerCurrentLimits(formatter); + writeDanglingLineCurrentLimits(formatter); + } + + @Override + public void writeHvdcToFormatter(TableFormatter formatter, HvdcLine hvdcLine) throws IOException { + String id = hvdcLine.getId(); + int num = mapper.getInt(AmplSubset.HVDC_LINE, id); + HvdcConverterStation.HvdcType type = hvdcLine.getConverterStation1().getHvdcType(); + AmplSubset subset = type.equals( + HvdcConverterStation.HvdcType.VSC) ? AmplSubset.VSC_CONVERTER_STATION : AmplSubset.LCC_CONVERTER_STATION; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(type.equals(HvdcConverterStation.HvdcType.VSC) ? 1 : 2) + .writeCell(mapper.getInt(subset, hvdcLine.getConverterStation1().getId())) + .writeCell(mapper.getInt(subset, hvdcLine.getConverterStation2().getId())) + .writeCell(hvdcLine.getR()) + .writeCell(hvdcLine.getNominalV()) + .writeCell(hvdcLine.getConvertersMode().name()) + .writeCell(hvdcLine.getActivePowerSetpoint()) + .writeCell(hvdcLine.getMaxP()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(hvdcLine.getNameOrId()); + } + + @Override + public void writeLccConverterStationToFormatter(TableFormatter formatter, + LccConverterStation lccStation) throws IOException { + Terminal t = lccStation.getTerminal(); + int busNum = AmplUtil.getBusNum(mapper, t); + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + + int num = mapper.getInt(AmplSubset.LCC_CONVERTER_STATION, lccStation.getId()); + + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(lccStation.getLossFactor()) + .writeCell(lccStation.getPowerFactor()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(lccStation.getId()) + .writeCell(lccStation.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + @Override + public void writeVscConverterStationToFormatter(TableFormatter formatter, + VscConverterStation vscStation) throws IOException { + String id = vscStation.getId(); + Terminal t = vscStation.getTerminal(); + int busNum = AmplUtil.getBusNum(mapper, t); + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + Map lineMap = getHvdcLinesMap(); + double maxP = lineMap.get(id) != null ? lineMap.get(id).getMaxP() : Double.NaN; + + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + double vlSet = vscStation.getVoltageSetpoint(); + double vb = t.getVoltageLevel().getNominalV(); + double minP = -maxP; + + int num = mapper.getInt(AmplSubset.VSC_CONVERTER_STATION, vscStation.getId()); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(minP) + .writeCell(maxP) + .writeCell(vscStation.getReactiveLimits().getMinQ(maxP)) + .writeCell(vscStation.getReactiveLimits().getMinQ(0)) + .writeCell(vscStation.getReactiveLimits().getMinQ(minP)) + .writeCell(vscStation.getReactiveLimits().getMaxQ(maxP)) + .writeCell(vscStation.getReactiveLimits().getMaxQ(0)) + .writeCell(vscStation.getReactiveLimits().getMaxQ(minP)) + .writeCell(vscStation.isVoltageRegulatorOn()) + .writeCell(vlSet / vb) + .writeCell(vscStation.getReactivePowerSetpoint()) + .writeCell(vscStation.getLossFactor()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(vscStation.getId()) + .writeCell(vscStation.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + private void writeBranchCurrentLimits(TableFormatter formatter) throws IOException { + for (Branch branch : network.getBranches()) { + String branchId = branch.getId(); + Optional currentLimits1 = branch.getCurrentLimits1(); + if (currentLimits1.isPresent()) { + writeTemporaryCurrentLimits(currentLimits1.get(), formatter, branchId, true, "_1_"); + } + Optional currentLimits2 = branch.getCurrentLimits2(); + if (currentLimits2.isPresent()) { + writeTemporaryCurrentLimits(currentLimits2.get(), formatter, branchId, false, "_2_"); + } + } + } + + private void writeTieLineCurrentLimits(TableFormatter formatter) throws IOException { + for (TieLine line : network.getTieLines()) { + String lineId = line.getId(); + Optional currentLimits1 = line.getDanglingLine1().getCurrentLimits(); + if (currentLimits1.isPresent()) { + writeTemporaryCurrentLimits(currentLimits1.get(), formatter, lineId, true, "_1_"); + } + Optional currentLimits2 = line.getDanglingLine2().getCurrentLimits(); + if (currentLimits2.isPresent()) { + writeTemporaryCurrentLimits(currentLimits2.get(), formatter, lineId, false, "_2_"); + } + } + } + + private void writeThreeWindingsTransformerCurrentLimits(TableFormatter formatter) throws IOException { + for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { + for (ThreeWindingsTransformer.Side side : ThreeWindingsTransformer.Side.values()) { + Optional currentLimits = twt.getLeg(side).getCurrentLimits(); + if (currentLimits.isPresent()) { + String branchId = twt.getId() + AmplUtil.getLegSuffix(side); + writeTemporaryCurrentLimits(currentLimits.get(), formatter, branchId, true, ""); + } + } + } + } + + private void writeDanglingLineCurrentLimits(TableFormatter formatter) throws IOException { + for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { + String branchId = dl.getId(); + Optional currentLimits = dl.getCurrentLimits(); + if (currentLimits.isPresent()) { + writeTemporaryCurrentLimits(currentLimits.get(), formatter, branchId, true, ""); + } + } + } + + private void writePhaseTapChanger(TableFormatter formatter, Identifiable twt, PhaseTapChanger ptc, String leg) { + try { + String ptcId = twt.getId() + leg; + String tcsId = twt.getId() + leg + PHASE_TABLE; + int rtcNum = mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, ptcId); + int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); + formatter.writeCell(variantIndex) + .writeCell(rtcNum) + .writeCell(ptc.getTapPosition() - ptc.getLowTapPosition() + 1) + .writeCell(tcsNum) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(ptcId); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private void writeRatioTapChanger(TableFormatter formatter, Identifiable twt, RatioTapChanger rtc, String leg) { + try { + String rtcId = twt.getId() + leg; + String tcsId = twt.getId() + leg + RATIO_TABLE; + int rtcNum = mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, rtcId); + int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); + formatter.writeCell(variantIndex) + .writeCell(rtcNum) + .writeCell(rtc.getTapPosition() - rtc.getLowTapPosition() + 1) + .writeCell(tcsNum) + .writeCell(rtc.hasLoadTapChangingCapabilities() && rtc.isRegulating()); + if (config.isExportRatioTapChangerVoltageTarget()) { + formatter.writeCell(rtc.getTargetV()); + } + formatter.writeCell(faultNum) + .writeCell(actionNum) + .writeCell(rtcId); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws IOException { + int ccNum = ConnectedComponents.getCcNum(b); + String id = b.getId(); + VoltageLevel vl = b.getVoltageLevel(); + int num = mapper.getInt(AmplSubset.BUS, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); + double nomV = vl.getNominalV(); + double v = b.getV() / nomV; + double theta = Math.toRadians(b.getAngle()); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(vlNum) + .writeCell(ccNum) + .writeCell(v) + .writeCell(theta) + .writeCell(b.getP()) + .writeCell(b.getQ()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id); + } + + @Override + public void writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt, + int middleCcNum) throws IOException { + String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); + String middleVlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); + int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); + int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + formatter.writeCell(variantIndex) + .writeCell(middleBusNum) + .writeCell(middleVlNum) + .writeCell(middleCcNum) + .writeCell(Float.NaN) + .writeCell(Double.NaN) + .writeCell(0.0) + .writeCell(0.0) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(middleBusId); + } + + @Override + public void writeDanglingLineMiddleBusesToFormatter(TableFormatter formatter, DanglingLine dl, + int middleCcNum) throws IOException { + Terminal t = dl.getTerminal(); + Bus b = AmplUtil.getBus(dl.getTerminal()); + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); + String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); + int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); + int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + SV sv = new SV(t.getP(), t.getQ(), b != null ? b.getV() : Double.NaN, b != null ? b.getAngle() : Double.NaN, + Branch.Side.ONE).otherSide( + dl, true); + double nomV = t.getVoltageLevel().getNominalV(); + double v = sv.getU() / nomV; + double theta = Math.toRadians(sv.getA()); + formatter.writeCell(variantIndex) + .writeCell(middleBusNum) + .writeCell(middleVlNum) + .writeCell(middleCcNum) + .writeCell(v) + .writeCell(theta) + .writeCell(0.0) // 0 MW injected at dangling line internal bus + .writeCell(0.0) // 0 MVar injected at dangling line internal bus + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(middleBusId); + } + + @Override + public void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine tieLine, + int xNodeCcNum) throws IOException { + String xNodeBusId = AmplUtil.getXnodeBusId(tieLine); + int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId); + int xNodeVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)); + formatter.writeCell(variantIndex) + .writeCell(xNodeBusNum) + .writeCell(xNodeVlNum) + .writeCell(xNodeCcNum) + .writeCell(Float.NaN) + .writeCell(Double.NaN) + .writeCell(0.0) + .writeCell(0.0) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(xNodeBusId); + } + + @Override + public void writeLinesToFormatter(TableFormatter formatter, Line l) throws IOException { + String id = l.getId(); + Terminal t1 = l.getTerminal1(); + Terminal t2 = l.getTerminal2(); + Bus bus1 = AmplUtil.getBus(t1); + Bus bus2 = AmplUtil.getBus(t2); + int bus1Num = getBusNum(bus1); + int bus2Num = getBusNum(bus2); + VoltageLevel vl1 = t1.getVoltageLevel(); + VoltageLevel vl2 = t2.getVoltageLevel(); + int num = mapper.getInt(AmplSubset.BRANCH, id); + int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); + int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); + double vb = vl1.getNominalV(); + double zb = vb * vb / AmplConstants.SB; + + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(bus1Num) + .writeCell(bus2Num) + .writeCell(-1) + .writeCell(vl1Num) + .writeCell(vl2Num) + .writeCell(l.getR() / zb) + .writeCell(l.getX() / zb) + .writeCell(l.getG1() * zb) + .writeCell(l.getG2() * zb) + .writeCell(l.getB1() * zb) + .writeCell(l.getB2() * zb) + .writeCell(1f) // constant ratio + .writeCell(-1) // no ratio tap changer + .writeCell(-1) // no phase tap changer + .writeCell(t1.getP()) + .writeCell(t2.getP()) + .writeCell(t1.getQ()) + .writeCell(t2.getQ()) + .writeCell(getPermanentLimit(l.getCurrentLimits1().orElse(null))) + .writeCell(getPermanentLimit(l.getCurrentLimits2().orElse(null))) + .writeCell(false) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(l.getNameOrId()); + } + + @Override + public void writeTieLineToFormatter(TableFormatter formatter, TieLine l) throws IOException { + Terminal t1 = l.getDanglingLine1().getTerminal(); + Terminal t2 = l.getDanglingLine2().getTerminal(); + Bus bus1 = AmplUtil.getBus(t1); + Bus bus2 = AmplUtil.getBus(t2); + int bus1Num = getBusNum(bus1); + int bus2Num = getBusNum(bus2); + VoltageLevel vl1 = t1.getVoltageLevel(); + VoltageLevel vl2 = t2.getVoltageLevel(); + String id = l.getId(); + int num = mapper.getInt(AmplSubset.BRANCH, id); + int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); + int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); + + double vb = vl1.getNominalV(); + double zb = vb * vb / AmplConstants.SB; + + boolean merged = !config.isExportXNodes(); + if (config.isExportXNodes()) { + String dl1Id = l.getDanglingLine1().getId(); + String dl2Id = l.getDanglingLine2().getId(); + int dl1Num = mapper.getInt(AmplSubset.BRANCH, dl1Id); + int dl2Num = mapper.getInt(AmplSubset.BRANCH, dl2Id); + String xNodeBusId = AmplUtil.getXnodeBusId(l); + String xnodeVoltageLevelId = AmplUtil.getXnodeVoltageLevelId(l); + int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId); + int xNodeVoltageLevelNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, xnodeVoltageLevelId); + + formatter.writeCell(variantIndex) + .writeCell(dl1Num) + .writeCell(bus1Num) + .writeCell(xNodeBusNum) + .writeCell(-1) + .writeCell(vl1Num) + .writeCell(xNodeVoltageLevelNum) + .writeCell(l.getDanglingLine1().getR() / zb) + .writeCell(l.getDanglingLine1().getX() / zb) + .writeCell(l.getDanglingLine1().getG() * zb / 2) + .writeCell(l.getDanglingLine1().getG() * zb / 2) + .writeCell(l.getDanglingLine1().getB() * zb / 2) + .writeCell(l.getDanglingLine1().getB() * zb / 2) + .writeCell(1f) // constant ratio + .writeCell(-1) // no ratio tap changer + .writeCell(-1) // no phase tap changer + .writeCell(t1.getP()) + .writeCell(l.getDanglingLine1().getBoundary().getP()) // xnode node flow side 1 + .writeCell(t1.getQ()) + .writeCell(l.getDanglingLine1().getBoundary().getQ()) // xnode node flow side 1 + .writeCell(getPermanentLimit(l.getDanglingLine1().getCurrentLimits().orElse(null))) + .writeCell(Float.NaN) + .writeCell(merged) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(dl1Id) + .writeCell(l.getDanglingLine1().getNameOrId()); + formatter.writeCell(variantIndex) + .writeCell(dl2Num) + .writeCell(xNodeBusNum) + .writeCell(bus2Num) + .writeCell(-1) + .writeCell(xNodeVoltageLevelNum) + .writeCell(vl2Num) + .writeCell(l.getDanglingLine2().getR() / zb) + .writeCell(l.getDanglingLine2().getX() / zb) + .writeCell(l.getDanglingLine1().getG() * zb / 2) + .writeCell(l.getDanglingLine1().getG() * zb / 2) + .writeCell(l.getDanglingLine1().getB() * zb / 2) + .writeCell(l.getDanglingLine1().getB() * zb / 2) + .writeCell(1f) // constant ratio + .writeCell(-1) // no ratio tap changer + .writeCell(-1) // no phase tap changer + .writeCell(l.getDanglingLine2().getBoundary().getP()) // xnode node flow side 2 + .writeCell(t2.getP()) + .writeCell(l.getDanglingLine2().getBoundary().getQ()) // xnode node flow side 2 + .writeCell(t2.getQ()) + .writeCell(Float.NaN) + .writeCell(getPermanentLimit(l.getDanglingLine2().getCurrentLimits().orElse(null))) + .writeCell(merged) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(dl2Id) + .writeCell(l.getDanglingLine2().getNameOrId()); + } else { + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(bus1Num) + .writeCell(bus2Num) + .writeCell(-1) + .writeCell(vl1Num) + .writeCell(vl2Num) + .writeCell(l.getR() / zb) + .writeCell(l.getX() / zb) + .writeCell(l.getG1() * zb) + .writeCell(l.getG2() * zb) + .writeCell(l.getB1() * zb) + .writeCell(l.getB2() * zb) + .writeCell(1f) // constant ratio + .writeCell(-1) // no ratio tap changer + .writeCell(-1) // no phase tap changer + .writeCell(t1.getP()) + .writeCell(t2.getP()) + .writeCell(t1.getQ()) + .writeCell(t2.getQ()) + .writeCell(getPermanentLimit(l.getDanglingLine1().getCurrentLimits().orElse(null))) + .writeCell(getPermanentLimit(l.getDanglingLine2().getCurrentLimits().orElse(null))) + .writeCell(merged) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(l.getNameOrId()); + } + } + + @Override + public void writeDanglingLineToFormatter(TableFormatter formatter, DanglingLine dl) throws IOException { + String id = dl.getId(); + Terminal t = dl.getTerminal(); + VoltageLevel vl = t.getVoltageLevel(); + Bus bus = AmplUtil.getBus(t); + int busNum = getBusNum(bus); + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); + String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); + int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); + int num = mapper.getInt(AmplSubset.BRANCH, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); + int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + double vb = vl.getNominalV(); + double zb = vb * vb / AmplConstants.SB; + double p1 = t.getP(); + double q1 = t.getQ(); + SV sv = new SV(p1, q1, bus != null ? bus.getV() : Double.NaN, bus != null ? bus.getAngle() : Double.NaN, + Branch.Side.ONE).otherSide( + dl, true); + double p2 = sv.getP(); + double q2 = sv.getQ(); + double patl = getPermanentLimit(dl.getCurrentLimits().orElse(null)); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(middleBusNum) + .writeCell(-1) + .writeCell(vlNum) + .writeCell(middleVlNum) + .writeCell(dl.getR() / zb) + .writeCell(dl.getX() / zb) + .writeCell(dl.getG() / 2 * zb) + .writeCell(dl.getG() / 2 * zb) + .writeCell(dl.getB() / 2 * zb) + .writeCell(dl.getB() / 2 * zb) + .writeCell(1f) // constant ratio + .writeCell(-1) // no ratio tap changer + .writeCell(-1) // no phase tap changer + .writeCell(p1) + .writeCell(p2) + .writeCell(q1) + .writeCell(q2) + .writeCell(patl) + .writeCell(patl) + .writeCell(false) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(dl.getNameOrId()); + } + + @Override + public void writeTwoWindingsTranformerToFormatter(TableFormatter formatter, + TwoWindingsTransformer twt) throws IOException { + String id = twt.getId(); + Terminal t1 = twt.getTerminal1(); + Terminal t2 = twt.getTerminal2(); + Bus bus1 = AmplUtil.getBus(t1); + Bus bus2 = AmplUtil.getBus(t2); + VoltageLevel vl1 = t1.getVoltageLevel(); + VoltageLevel vl2 = t2.getVoltageLevel(); + int bus1Num = getBusNum(bus1); + int bus2Num = getBusNum(bus2); + int num = mapper.getInt(AmplSubset.BRANCH, id); + int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId()); + int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId()); + double vb1 = vl1.getNominalV(); + double vb2 = vl2.getNominalV(); + double zb2 = vb2 * vb2 / AmplConstants.SB; + double r = twt.getR() / zb2; + double x = twt.getX() / zb2; + double g1; + double g2; + double b1; + double b2; + if (config.isTwtSplitShuntAdmittance()) { + g1 = twt.getG() * zb2 / 2; + g2 = g1; + b1 = twt.getB() * zb2 / 2; + b2 = b1; + } else { + g1 = twt.getG() * zb2; + g2 = 0; + b1 = twt.getB() * zb2; + b2 = 0; + } + + double ratedU1 = twt.getRatedU1(); + double ratedU2 = twt.getRatedU2(); + double ratio = ratedU2 / vb2 / (ratedU1 / vb1); + RatioTapChanger rtc = twt.getRatioTapChanger(); + PhaseTapChanger ptc = twt.getPhaseTapChanger(); + int rtcNum = rtc != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, twt.getId()) : -1; + int ptcNum = ptc != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, twt.getId()) : -1; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(bus1Num) + .writeCell(bus2Num) + .writeCell(-1) + .writeCell(vl1Num) + .writeCell(vl2Num) + .writeCell(r) + .writeCell(x) + .writeCell(g1) + .writeCell(g2) + .writeCell(b1) + .writeCell(b2) + .writeCell(ratio) + .writeCell(rtcNum) + .writeCell(ptcNum) + .writeCell(t1.getP()) + .writeCell(t2.getP()) + .writeCell(t1.getQ()) + .writeCell(t2.getQ()) + .writeCell(getPermanentLimit(twt.getCurrentLimits1().orElse(null))) + .writeCell(getPermanentLimit(twt.getCurrentLimits2().orElse(null))) + .writeCell(false) // TODO to update + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(twt.getNameOrId()); + } + + @Override + public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter, ThreeWindingsTransformer twt, + int middleBusNum, int middleVlNum, + ThreeWindingsTransformer.Side legSide) throws IOException { + Terminal terminal = twt.getLeg(legSide).getTerminal(); + Bus bus = AmplUtil.getBus(terminal); + VoltageLevel vl = terminal.getVoltageLevel(); + String id = twt.getId() + AmplUtil.getLegSuffix(legSide); + int num3wt = mapper.getInt(AmplSubset.THREE_WINDINGS_TRANSFO, twt.getId()); + int num = mapper.getInt(AmplSubset.BRANCH, id); + int legVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); + int legBusNum = getBusNum(bus); + double vb = vl.getNominalV(); + double zb = vb * vb / AmplConstants.SB; + double r = twt.getLeg(legSide).getR() / zb; + double x = twt.getLeg(legSide).getX() / zb; + double g = twt.getLeg(legSide).getG() * zb; + double b = twt.getLeg(legSide).getB() * zb; + double ratedU = twt.getLeg(legSide).getRatedU(); + double ratedU0 = twt.getRatedU0(); + double ratio = ratedU0 / ratedU; + RatioTapChanger rtc1 = twt.getLeg(legSide).getRatioTapChanger(); + PhaseTapChanger ptc1 = twt.getLeg(legSide).getPhaseTapChanger(); + int rtc1Num = rtc1 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id) : -1; + int ptc1Num = ptc1 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id) : -1; + + formatter.writeCell(variantIndex) + .writeCell(num); + if (ThreeWindingsTransformer.Side.ONE.equals(legSide)) { + formatter.writeCell(middleBusNum) + .writeCell(legBusNum) + .writeCell(num3wt) + .writeCell(middleVlNum) + .writeCell(legVlNum); + } else { + formatter.writeCell(legBusNum) + .writeCell(middleBusNum) + .writeCell(num3wt) + .writeCell(legVlNum) + .writeCell(middleVlNum); + } + formatter.writeCell(r) + .writeCell(x) + .writeCell(g) + .writeCell(0.0) + .writeCell(b) + .writeCell(0.0) + .writeCell(ratio) + .writeCell(rtc1Num) + .writeCell(ptc1Num); + switch (legSide) { + case ONE: + formatter.writeCell(Double.NaN) + .writeCell(terminal.getP()) + .writeCell(Double.NaN) + .writeCell(terminal.getQ()) + .writeCell(Double.NaN) + .writeCell(getPermanentLimit(twt.getLeg1().getCurrentLimits().orElse(null))); + break; + case TWO: + formatter.writeCell(terminal.getP()) + .writeCell(Double.NaN) + .writeCell(terminal.getQ()) + .writeCell(Double.NaN) + .writeCell(getPermanentLimit(twt.getLeg2().getCurrentLimits().orElse(null))) + .writeCell(Double.NaN); + break; + case THREE: + formatter.writeCell(terminal.getP()) + .writeCell(Double.NaN) + .writeCell(terminal.getQ()) + .writeCell(Double.NaN) + .writeCell(getPermanentLimit(twt.getLeg3().getCurrentLimits().orElse(null))) + .writeCell(Double.NaN); + break; + } + formatter.writeCell(false) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(""); + } + + @Override + public void writeTieLineVoltageLevelToFormatter(TableFormatter formatter, TieLine tieLine) throws IOException { + String vlId = AmplUtil.getXnodeVoltageLevelId(tieLine); + int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell("") + .writeCell(0) + .writeCell(tieLine.getDanglingLine1().getTerminal().getVoltageLevel().getNominalV()) + .writeCell(Float.NaN) + .writeCell(Float.NaN) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(XNODE_COUNTRY_NAME) + .writeCell(AmplUtil.getXnodeBusId(tieLine) + "_voltageLevel") + .writeCell(""); + } + + @Override + public void writeVoltageLevelToFormatter(TableFormatter formatter, VoltageLevel vl) throws IOException { + int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); + double nomV = vl.getNominalV(); + double minV = vl.getLowVoltageLimit() / nomV; + double maxV = vl.getHighVoltageLimit() / nomV; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell("") + .writeCell(0) + .writeCell(nomV) + .writeCell(minV) + .writeCell(maxV) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) + .writeCell(vl.getId()) + .writeCell(vl.getNameOrId()); + } + + @Override + public void writeDanglingLineLoadToFormatter(TableFormatter formatter, DanglingLine dl) throws IOException { + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); + String id = dl.getId(); + int num = mapper.getInt(AmplSubset.LOAD, id); + int busNum = mapper.getInt(AmplSubset.BUS, middleBusId); + String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(vlNum) + .writeCell(dl.getP0()) + .writeCell(dl.getQ0()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(dl.getId() + "_load") + .writeCell("") + .writeCell(dl.getTerminal().getP()) + .writeCell(dl.getTerminal().getQ()); + } + + @Override + public void writeLoadtoFormatter(TableFormatter formatter, Load l) throws IOException { + String id = l.getId(); + Terminal t = l.getTerminal(); + Bus bus = AmplUtil.getBus(t); + int busNum = bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); + int num = mapper.getInt(AmplSubset.LOAD, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(vlNum) + .writeCell(l.getP0()) + .writeCell(l.getQ0()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(l.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + @Override + public void writeShuntCompensatorToFormatter(TableFormatter formatter, ShuntCompensator sc) throws IOException { + + String id = sc.getId(); + Terminal t = sc.getTerminal(); + int num = mapper.getInt(AmplSubset.SHUNT, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + int busNum = AmplUtil.getBusNum(mapper, t); + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + + double vb = t.getVoltageLevel().getNominalV(); + double zb = vb * vb / AmplConstants.SB; + double b1 = 0; + double b2; + int points = 0; + int sectionCount = 1; + if (sc.getModelType() == ShuntCompensatorModelType.NON_LINEAR) { + // TODO non linear shunt has to be converted as multiple sections shunt. + if (sc.getSectionCount() > 1) { + b1 = sc.getB(sc.getSectionCount() - 1) * zb; + } + b2 = sc.getB() * zb; + } else { + b2 = sc.getModel(ShuntCompensatorLinearModel.class).getBPerSection() * sc.getMaximumSectionCount() * zb; + points = sc.getMaximumSectionCount() < 1 ? 0 : sc.getMaximumSectionCount() - 1; + sectionCount = sc.getSectionCount(); + } + double b = sc.getB() * zb; + double minB = Math.min(b1, b2); + double maxB = Math.max(b1, b2); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(minB) + .writeCell(maxB) + .writeCell(points) + .writeCell(b) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(sc.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()) + .writeCell(sectionCount); + } + + @Override + public void writeGeneratorToFormatter(TableFormatter formatter, Generator gen) throws IOException { + String id = gen.getId(); + Terminal t = gen.getTerminal(); + int num = mapper.getInt(AmplSubset.GENERATOR, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + int busNum = AmplUtil.getBusNum(mapper, t); + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + double minP = gen.getMinP(); + double maxP = gen.getMaxP(); + double vb = t.getVoltageLevel().getNominalV(); + + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(minP) + .writeCell(maxP) + .writeCell(gen.getReactiveLimits().getMinQ(maxP)) + .writeCell(gen.getReactiveLimits().getMinQ(0)) + .writeCell(gen.getReactiveLimits().getMinQ(minP)) + .writeCell(gen.getReactiveLimits().getMaxQ(maxP)) + .writeCell(gen.getReactiveLimits().getMaxQ(0)) + .writeCell(gen.getReactiveLimits().getMaxQ(minP)) + .writeCell(gen.isVoltageRegulatorOn()) + .writeCell(gen.getTargetV() / vb) + .writeCell(gen.getTargetP()) + .writeCell(gen.getTargetQ()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(gen.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + @Override + public void writeBatteryToFormatter(TableFormatter formatter, Battery battery) throws IOException { + String id = battery.getId(); + Terminal t = battery.getTerminal(); + int num = mapper.getInt(AmplSubset.BATTERY, id); + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + int busNum = AmplUtil.getBusNum(mapper, t); + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + double minP = battery.getMinP(); + double maxP = battery.getMaxP(); + + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(battery.getTargetP()) + .writeCell(battery.getTargetQ()) + .writeCell(minP) + .writeCell(maxP) + .writeCell(battery.getReactiveLimits().getMinQ(maxP)) + .writeCell(battery.getReactiveLimits().getMinQ(0)) + .writeCell(battery.getReactiveLimits().getMinQ(minP)) + .writeCell(battery.getReactiveLimits().getMaxQ(maxP)) + .writeCell(battery.getReactiveLimits().getMaxQ(0)) + .writeCell(battery.getReactiveLimits().getMaxQ(minP)) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(battery.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + @Override + public void writeStaticVarCompensatorToFormatter(TableFormatter formatter, + StaticVarCompensator svc) throws IOException { + String id = svc.getId(); + int num = mapper.getInt(AmplSubset.STATIC_VAR_COMPENSATOR, id); + + Terminal t = svc.getTerminal(); + + int busNum = AmplUtil.getBusNum(mapper, t); + + int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); + + double vlSet = svc.getVoltageSetpoint(); + double vb = t.getVoltageLevel().getNominalV(); + double zb = vb * vb / AmplConstants.SB; // Base impedance + + int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum) + .writeCell(vlNum) + .writeCell(svc.getBmin() * zb) + .writeCell(svc.getBmax() * zb) + .writeCell(svc.getRegulationMode().equals(StaticVarCompensator.RegulationMode.VOLTAGE)) + .writeCell(vlSet / vb) + .writeCell(svc.getReactivePowerSetpoint()) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(id) + .writeCell(svc.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + + } + + @Override + public void writeDanglingLineVoltageLevelToFormatter(TableFormatter formatter, + DanglingLine dl) throws IOException { + String vlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); + int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); + VoltageLevel vl = dl.getTerminal().getVoltageLevel(); + double nomV = vl.getNominalV(); + double minV = vl.getLowVoltageLimit() / nomV; + double maxV = vl.getHighVoltageLimit() / nomV; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell("") + .writeCell(0) + .writeCell(nomV) + .writeCell(minV) + .writeCell(maxV) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) + .writeCell(dl.getId() + "_voltageLevel") + .writeCell(""); + } + + @Override + public void writeThreeWindingsTransformerVoltageLevelToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt) throws IOException { + String vlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); + int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); + Terminal t1 = twt.getLeg1().getTerminal(); + VoltageLevel vl1 = t1.getVoltageLevel(); + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell("") + .writeCell(0) + .writeCell(vl1.getNominalV()) + .writeCell(Float.NaN) + .writeCell(Float.NaN) + .writeCell(faultNum) + .writeCell(actionNum) + .writeCell(vl1.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) + .writeCell(vlId) + .writeCell(""); + } + + private void writeRatioTapChanger(TableFormatter formatter, String id, double zb2, double reactance, + RatioTapChanger rtc) throws IOException { + int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id); + + for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) { + RatioTapChangerStep step = rtc.getStep(position); + double x = reactance * (1 + step.getX() / 100) / zb2; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(position - rtc.getLowTapPosition() + 1) + .writeCell(step.getRho()) + .writeCell(x) + .writeCell(0.0) + .writeCell(faultNum) + .writeCell(actionNum); + } + } + + private void writePhaseTapChanger(TableFormatter formatter, String id, double zb2, double reactance, + PhaseTapChanger ptc) throws IOException { + int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id); + + for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { + PhaseTapChangerStep step = ptc.getStep(position); + double x = reactance * (1 + step.getX() / 100) / zb2; + formatter.writeCell(variantIndex) + .writeCell(num) + .writeCell(position - ptc.getLowTapPosition() + 1) + .writeCell(step.getRho()) + .writeCell(x) + .writeCell(Math.toRadians(step.getAlpha())) + .writeCell(faultNum) + .writeCell(actionNum); + } + } + + private void writeTemporaryCurrentLimits(CurrentLimits limits, TableFormatter formatter, String branchId, + boolean side1, String sideId) throws IOException { + int branchNum = mapper.getInt(AmplSubset.BRANCH, branchId); + for (LoadingLimits.TemporaryLimit tl : limits.getTemporaryLimits()) { + String limitId = branchId + "_" + sideId + "_" + tl.getAcceptableDuration(); + int limitNum = mapper.getInt(AmplSubset.TEMPORARY_CURRENT_LIMIT, limitId); + formatter.writeCell(variantIndex) + .writeCell(limitNum) + .writeCell(branchNum) + .writeCell(side1 ? 1 : 2) + .writeCell(tl.getValue()) + .writeCell(tl.getAcceptableDuration()) + .writeCell(faultNum) + .writeCell(actionNum); + } + } + + /** + * Cache the computation of the map. + */ + private HashMap getHvdcLinesMap() { + if (hvdcLinesMap == null) { + hvdcLinesMap = computeHvdcLinesMap(); + } + return hvdcLinesMap; + } + + private HashMap computeHvdcLinesMap() { + HashMap lineMap = new HashMap<>(); + network.getHvdcLines().forEach(line -> { + if (line.getConverterStation1() != null) { + lineMap.put(line.getConverterStation1().getId(), line); + } + if (line.getConverterStation2() != null) { + lineMap.put(line.getConverterStation2().getId(), line); + } + }); + return lineMap; + } + + private int getBusNum(Bus bus) { + return bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); + } + + private static double getPermanentLimit(CurrentLimits limits) { + if (limits != null) { + return limits.getPermanentLimit(); + } + return Double.NaN; + } +} From 84987ead5c98feb31de7b7623e89dc6b50902143 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:38:56 +0200 Subject: [PATCH 04/65] match setVersion to return this Signed-off-by: Nicolas Pierre --- .../main/java/com/powsybl/ampl/converter/AmplExportConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index e7c195be581..7f0fd843ccb 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -120,8 +120,9 @@ public AmplExportVersion getVersion() { return version; } - public void setVersion(AmplExportVersion version) { + public AmplExportConfig setVersion(AmplExportVersion version) { this.version = Objects.requireNonNull(version); + return this; } } From 54fa975c95f148f781c2e590735c1dbd159ed3f9 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:57:44 +0200 Subject: [PATCH 05/65] AmplExportVersion is now an interface Signed-off-by: Nicolas Pierre --- .../ampl/converter/AmplExportConfig.java | 7 +++-- .../version/AmplColumnsExporter.java | 8 ----- .../converter/version/AmplExportVersion.java | 30 ++++++------------- .../version/AmplExportVersionImpl.java | 27 +++++++++++++++++ .../converter/version/BasicAmplExporter.java | 9 ++++-- 5 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index 7f0fd843ccb..77041a54f5d 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -8,6 +8,7 @@ package com.powsybl.ampl.converter; import com.powsybl.ampl.converter.version.AmplExportVersion; +import com.powsybl.ampl.converter.version.AmplExportVersionImpl; import java.util.Objects; @@ -51,18 +52,18 @@ public String getLabel() { private boolean twtSplitShuntAdmittance; public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { - this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1); + this(exportScope, exportXNodes, actionType, false, false, AmplExportVersionImpl.V1_LEGACY); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { this(exportScope, exportXNodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, - AmplExportVersion.V1); + AmplExportVersionImpl.V1_LEGACY); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance, - AmplExportVersion version) { + AmplExportVersionImpl version) { this.exportScope = Objects.requireNonNull(exportScope); this.exportXNodes = exportXNodes; this.actionType = Objects.requireNonNull(actionType); diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 47d023533ec..de976a73054 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -6,11 +6,8 @@ */ package com.powsybl.ampl.converter.version; -import com.powsybl.ampl.converter.AmplExportConfig; -import com.powsybl.ampl.converter.AmplSubset; import com.powsybl.commons.io.table.Column; import com.powsybl.commons.io.table.TableFormatter; -import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; import java.io.IOException; @@ -21,11 +18,6 @@ */ public interface AmplColumnsExporter { - interface Factory { - AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, - int variantIndex, int faultNum, int actionNum); - } - List getRtcColumns(); List getPtcColumns(); diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index ac0e8dedf55..624e84f9471 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -1,27 +1,15 @@ -/** - * 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/. - */ package com.powsybl.ampl.converter.version; -/** - * @author Nicolas Pierre - */ -public enum AmplExportVersion { - /** - * Legacy export - */ - V1(BasicAmplExporter.getFactory()); +import com.powsybl.ampl.converter.AmplExportConfig; +import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.commons.util.StringToIntMapper; +import com.powsybl.iidm.network.Network; - private final AmplColumnsExporter.Factory factory; - - AmplExportVersion(AmplColumnsExporter.Factory factory) { - this.factory = factory; +public interface AmplExportVersion { + interface Factory { + AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum); } - public AmplColumnsExporter.Factory getColumnsExporter() { - return factory; - } + Factory getColumnsExporter(); } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java new file mode 100644 index 00000000000..0e458ca79bd --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.ampl.converter.version; + +/** + * @author Nicolas Pierre + */ +public enum AmplExportVersionImpl implements AmplExportVersion { + /** + * Legacy export + */ + V1_LEGACY(BasicAmplExporter.getFactory()); + + private final AmplExportVersion.Factory factory; + + AmplExportVersionImpl(AmplExportVersion.Factory factory) { + this.factory = factory; + } + + public AmplExportVersion.Factory getColumnsExporter() { + return factory; + } +} diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 31365a63d24..d31878522dd 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -24,8 +24,11 @@ import static com.powsybl.ampl.converter.AmplConstants.*; /** - * @author Nicolas Pierre - * @author Geoffroy Jamgotchian + * Legacy exporter that must be retrocompatible, + * and only fixes should be made on this class. + * + * @author Nicolas Pierre for the refactor + * @author Geoffroy Jamgotchian for the original code */ public class BasicAmplExporter implements AmplColumnsExporter { @@ -37,7 +40,7 @@ public class BasicAmplExporter implements AmplColumnsExporter { private final int actionNum; private HashMap hvdcLinesMap; - public static AmplColumnsExporter.Factory getFactory() { + public static AmplExportVersion.Factory getFactory() { return BasicAmplExporter::new; } From 8be12fe5d9c5613b024a167813f334a38474995a Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 2 Jun 2023 15:59:06 +0200 Subject: [PATCH 06/65] missing author and licence Signed-off-by: Nicolas Pierre --- .../ampl/converter/version/AmplExportVersion.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 624e84f9471..7fd56fbded9 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -1,3 +1,9 @@ +/** + * 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/. + */ package com.powsybl.ampl.converter.version; import com.powsybl.ampl.converter.AmplExportConfig; @@ -5,6 +11,9 @@ import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.Network; +/** + * @author Nicolas Pierre + */ public interface AmplExportVersion { interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, From 595b20b50cda8788def10bede095aa658cfe7d41 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Mon, 5 Jun 2023 13:48:02 +0200 Subject: [PATCH 07/65] typo in writeThreeWindingsTranformersMiddleBusesColumnsToFormatter Signed-off-by: Nicolas Pierre --- .../java/com/powsybl/ampl/converter/AmplNetworkWriter.java | 2 +- .../powsybl/ampl/converter/version/AmplColumnsExporter.java | 6 +++--- .../powsybl/ampl/converter/version/BasicAmplExporter.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java index fea575dc811..ee49bda491c 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java @@ -277,7 +277,7 @@ private void writeThreeWindingsTransformerMiddleBuses(AmplExportContext context, if (connectedComponentToExport(middleCcNum)) { String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); context.busIdsToExport.add(middleBusId); - columnsExporter.writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(formatter, twt, middleCcNum); + columnsExporter.writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(formatter, twt, middleCcNum); } } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index de976a73054..748fbea1e79 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -70,9 +70,9 @@ void writeVscConverterStationToFormatter(TableFormatter formatter, void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws IOException; - void writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(TableFormatter formatter, - ThreeWindingsTransformer twt, - int middleCcNum) throws IOException; + void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt, + int middleCcNum) throws IOException; void writeLinesToFormatter(TableFormatter formatter, Line l) throws IOException; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index d31878522dd..caa377b396e 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -647,9 +647,9 @@ public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws } @Override - public void writeThreeWindingsTranformsMiddleBusesColumnsToFormatter(TableFormatter formatter, - ThreeWindingsTransformer twt, - int middleCcNum) throws IOException { + public void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt, + int middleCcNum) throws IOException { String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); String middleVlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); From 57cd65993f9d8c4282e3e1d0566cc4c10016834d Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Mon, 5 Jun 2023 13:52:39 +0200 Subject: [PATCH 08/65] integrating #2596 into this PR Signed-off-by: Nicolas Pierre --- .../converter/version/BasicAmplExporter.java | 105 ++++++++---------- .../three-windings-transformers-branches.txt | 6 +- .../three-windings-transformers-tct.txt | 12 +- 3 files changed, 53 insertions(+), 70 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index caa377b396e..9e411523392 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -384,8 +384,7 @@ public void writeThreeWindingsTransformerTapChangerTableToFormatter(TableFormatt for (ThreeWindingsTransformer.Leg leg : twt.getLegs()) { legNumber++; RatioTapChanger rtc = leg.getRatioTapChanger(); - Terminal t = leg.getTerminal(); - double vb = t.getVoltageLevel().getNominalV(); + double vb = twt.getRatedU0(); double zb = vb * vb / AmplConstants.SB; if (rtc != null) { String id = twt.getId() + "_leg" + legNumber + RATIO_TABLE; @@ -587,10 +586,10 @@ private void writePhaseTapChanger(TableFormatter formatter, Identifiable twt, try { String ptcId = twt.getId() + leg; String tcsId = twt.getId() + leg + PHASE_TABLE; - int rtcNum = mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, ptcId); + int ptcNum = mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, ptcId); int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); formatter.writeCell(variantIndex) - .writeCell(rtcNum) + .writeCell(ptcNum) .writeCell(ptc.getTapPosition() - ptc.getLowTapPosition() + 1) .writeCell(tcsNum) .writeCell(faultNum) @@ -654,12 +653,18 @@ public void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableForm String middleVlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId); int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + + double v = twt.getProperty("v") == null ? Double.NaN : + Double.parseDouble(twt.getProperty("v")) / twt.getRatedU0(); + double angle = twt.getProperty("angle") == null ? Double.NaN : + Math.toRadians(Double.parseDouble(twt.getProperty("angle"))); + formatter.writeCell(variantIndex) .writeCell(middleBusNum) .writeCell(middleVlNum) .writeCell(middleCcNum) - .writeCell(Float.NaN) - .writeCell(Double.NaN) + .writeCell(v) + .writeCell(angle) .writeCell(0.0) .writeCell(0.0) .writeCell(faultNum) @@ -1011,35 +1016,29 @@ public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter int legVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); int legBusNum = getBusNum(bus); double vb = vl.getNominalV(); - double zb = vb * vb / AmplConstants.SB; - double r = twt.getLeg(legSide).getR() / zb; - double x = twt.getLeg(legSide).getX() / zb; - double g = twt.getLeg(legSide).getG() * zb; - double b = twt.getLeg(legSide).getB() * zb; - double ratedU = twt.getLeg(legSide).getRatedU(); double ratedU0 = twt.getRatedU0(); - double ratio = ratedU0 / ratedU; + double ratedU = twt.getLeg(legSide).getRatedU(); + + double zb0 = ratedU0 * ratedU0 / AmplConstants.SB; + double r = twt.getLeg(legSide).getR() / zb0; + double x = twt.getLeg(legSide).getX() / zb0; + double g = twt.getLeg(legSide).getG() * zb0; + double b = twt.getLeg(legSide).getB() * zb0; + double ratio = vb / ratedU; + RatioTapChanger rtc1 = twt.getLeg(legSide).getRatioTapChanger(); PhaseTapChanger ptc1 = twt.getLeg(legSide).getPhaseTapChanger(); int rtc1Num = rtc1 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id) : -1; int ptc1Num = ptc1 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id) : -1; formatter.writeCell(variantIndex) - .writeCell(num); - if (ThreeWindingsTransformer.Side.ONE.equals(legSide)) { - formatter.writeCell(middleBusNum) - .writeCell(legBusNum) - .writeCell(num3wt) - .writeCell(middleVlNum) - .writeCell(legVlNum); - } else { - formatter.writeCell(legBusNum) - .writeCell(middleBusNum) - .writeCell(num3wt) - .writeCell(legVlNum) - .writeCell(middleVlNum); - } - formatter.writeCell(r) + .writeCell(num) + .writeCell(legBusNum) + .writeCell(middleBusNum) + .writeCell(num3wt) + .writeCell(legVlNum) + .writeCell(middleVlNum) + .writeCell(r) .writeCell(x) .writeCell(g) .writeCell(0.0) @@ -1047,34 +1046,14 @@ public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter .writeCell(0.0) .writeCell(ratio) .writeCell(rtc1Num) - .writeCell(ptc1Num); - switch (legSide) { - case ONE: - formatter.writeCell(Double.NaN) - .writeCell(terminal.getP()) - .writeCell(Double.NaN) - .writeCell(terminal.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg1().getCurrentLimits().orElse(null))); - break; - case TWO: - formatter.writeCell(terminal.getP()) - .writeCell(Double.NaN) - .writeCell(terminal.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg2().getCurrentLimits().orElse(null))) - .writeCell(Double.NaN); - break; - case THREE: - formatter.writeCell(terminal.getP()) - .writeCell(Double.NaN) - .writeCell(terminal.getQ()) - .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg3().getCurrentLimits().orElse(null))) - .writeCell(Double.NaN); - break; - } - formatter.writeCell(false) + .writeCell(ptc1Num) + .writeCell(terminal.getP()) + .writeCell(Double.NaN) + .writeCell(terminal.getQ()) + .writeCell(Double.NaN) + .writeCell(getPermanentLimit(twt.getLeg(legSide).getCurrentLimits().orElse(null))) + .writeCell(Double.NaN) + .writeCell(false) .writeCell(faultNum) .writeCell(actionNum) .writeCell(id) @@ -1221,7 +1200,7 @@ public void writeGeneratorToFormatter(TableFormatter formatter, Generator gen) t int conBusNum = AmplUtil.getConnectableBusNum(mapper, t); double minP = gen.getMinP(); double maxP = gen.getMaxP(); - double vb = t.getVoltageLevel().getNominalV(); + double vb = gen.getRegulatingTerminal().getVoltageLevel().getNominalV(); formatter.writeCell(variantIndex) .writeCell(num) @@ -1346,18 +1325,22 @@ public void writeThreeWindingsTransformerVoltageLevelToFormatter(TableFormatter ThreeWindingsTransformer twt) throws IOException { String vlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); - Terminal t1 = twt.getLeg1().getTerminal(); - VoltageLevel vl1 = t1.getVoltageLevel(); formatter.writeCell(variantIndex) .writeCell(num) .writeCell("") .writeCell(0) - .writeCell(vl1.getNominalV()) + .writeCell(twt.getRatedU0()) .writeCell(Float.NaN) .writeCell(Float.NaN) .writeCell(faultNum) .writeCell(actionNum) - .writeCell(vl1.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) + .writeCell(twt.getLeg1() + .getTerminal() + .getVoltageLevel() + .getSubstation() + .flatMap(Substation::getCountry) + .map(Enum::toString) + .orElse("")) .writeCell(vlId) .writeCell(""); } diff --git a/ampl-converter/src/test/resources/inputs/three-windings-transformers-branches.txt b/ampl-converter/src/test/resources/inputs/three-windings-transformers-branches.txt index 41161bd0d17..2d16057a6e8 100644 --- a/ampl-converter/src/test/resources/inputs/three-windings-transformers-branches.txt +++ b/ampl-converter/src/test/resources/inputs/three-windings-transformers-branches.txt @@ -1,5 +1,5 @@ #Branches (three-windings-transformer/InitialState) #"variant" "num" "bus1" "bus2" "3wt num" "sub.1" "sub.2" "r (pu)" "x (pu)" "g1 (pu)" "g2 (pu)" "b1 (pu)" "b2 (pu)" "cst ratio (pu)" "ratio tc" "phase tc" "p1 (MW)" "p2 (MW)" "q1 (MVar)" "q2 (MVar)" "patl1 (A)" "patl2 (A)" "merged" "fault" "curative" "id" "description" -1 1 4 1 1 4 1 0.100000 0.0100000 1.00000 0.00000 0.100000 0.00000 1.00000 -1 1 -99999.0 -99999.0 -99999.0 -99999.0 -99999.0 1000.00 false 0 0 "3WT_leg1" "" -1 2 2 4 1 2 4 0.100000 0.0100000 0.00000 0.00000 0.00000 0.00000 4.00000 1 -1 -99999.0 -99999.0 -99999.0 -99999.0 100.000 -99999.0 false 0 0 "3WT_leg2" "" -1 3 3 4 1 3 4 0.100000 0.0100000 0.00000 0.00000 0.00000 0.00000 12.0000 2 -1 -99999.0 -99999.0 -99999.0 -99999.0 10.0000 -99999.0 false 0 0 "3WT_leg3" "" +1 1 1 4 1 1 4 0.100000 0.0100000 1.00000 0.00000 0.100000 0.00000 1.00000 -1 1 -99999.0 -99999.0 -99999.0 -99999.0 1000.00 -99999.0 false 0 0 "3WT_leg1" "" +1 2 2 4 1 2 4 0.00625000 0.000625000 0.00000 0.00000 0.00000 0.00000 1.00000 1 -1 -99999.0 -99999.0 -99999.0 -99999.0 100.000 -99999.0 false 0 0 "3WT_leg2" "" +1 3 3 4 1 3 4 0.000694444 6.94444e-05 0.00000 0.00000 0.00000 0.00000 1.00000 2 -1 -99999.0 -99999.0 -99999.0 -99999.0 10.0000 -99999.0 false 0 0 "3WT_leg3" "" diff --git a/ampl-converter/src/test/resources/inputs/three-windings-transformers-tct.txt b/ampl-converter/src/test/resources/inputs/three-windings-transformers-tct.txt index 89d3cefc5b5..69c87b78445 100644 --- a/ampl-converter/src/test/resources/inputs/three-windings-transformers-tct.txt +++ b/ampl-converter/src/test/resources/inputs/three-windings-transformers-tct.txt @@ -1,9 +1,9 @@ #Tap changer table (three-windings-transformer/InitialState) #"variant" "num" "tap" "var ratio" "x (pu)" "angle (rad)" "fault" "curative" 1 3 1 1.00000 0.0101000 0.00000 0 0 -1 1 1 0.900000 0.0100098 0.00000 0 0 -1 1 2 1.00000 0.0100109 0.00000 0 0 -1 1 3 1.10000 0.0100120 0.00000 0 0 -1 2 1 0.900000 0.0100011 0.00000 0 0 -1 2 2 1.00000 0.0100012 0.00000 0 0 -1 2 3 1.10000 0.0100013 0.00000 0 0 +1 1 1 0.900000 0.000625613 0.00000 0 0 +1 1 2 1.00000 0.000625681 0.00000 0 0 +1 1 3 1.10000 0.000625749 0.00000 0 0 +1 2 1 0.900000 6.94520e-05 0.00000 0 0 +1 2 2 1.00000 6.94528e-05 0.00000 0 0 +1 2 3 1.10000 6.94537e-05 0.00000 0 0 From 73884d91fc603a6a3166265c5768d750406bf12a Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 17:10:43 +0200 Subject: [PATCH 09/65] fix function accepting only implementation and not interface Signed-off-by: Nicolas Pierre --- .../main/java/com/powsybl/ampl/converter/AmplExportConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index 77041a54f5d..0ed97bca8f0 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -63,7 +63,7 @@ public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportAct public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance, - AmplExportVersionImpl version) { + AmplExportVersion version) { this.exportScope = Objects.requireNonNull(exportScope); this.exportXNodes = exportXNodes; this.actionType = Objects.requireNonNull(actionType); From 8c67231811ccd235cea291c05337a516d040afbc Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 17:17:41 +0200 Subject: [PATCH 10/65] Accept version in parameters of AmplExport service Signed-off-by: Nicolas Pierre --- .../powsybl/ampl/converter/AmplExporter.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 1cdb702d925..d2ea3baf3cd 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -8,13 +8,15 @@ package com.powsybl.ampl.converter; import com.google.auto.service.AutoService; +import com.powsybl.ampl.converter.version.AmplExportVersion; +import com.powsybl.ampl.converter.version.AmplExportVersionImpl; import com.powsybl.commons.config.PlatformConfig; import com.powsybl.commons.datasource.DataSource; -import com.powsybl.iidm.network.Exporter; -import com.powsybl.iidm.network.Network; import com.powsybl.commons.parameters.Parameter; import com.powsybl.commons.parameters.ParameterDefaultValueConfig; import com.powsybl.commons.parameters.ParameterType; +import com.powsybl.iidm.network.Exporter; +import com.powsybl.iidm.network.Network; import java.io.IOException; import java.io.UncheckedIOException; @@ -36,22 +38,33 @@ public class AmplExporter implements Exporter { public static final String EXPORT_ACTION_TYPE = "iidm.export.ampl.action-type"; public static final String EXPORT_RATIOTAPCHANGER_VT = "iidm.export.ampl.export-ratio-tap-changer-voltage-target"; public static final String TWT_SPLIT_SHUNT_ADMITTANCE = "iidm.export.ampl.twt-split-shunt-admittance"; + public static final String EXPORT_VERSION = "iidm.export.ampl.export-version"; private static final Parameter EXPORT_SCOPE_PARAMETER = new Parameter(EXPORT_SCOPE, ParameterType.STRING, "Export scope", AmplExportConfig.ExportScope.ALL.name(), - Arrays.stream(AmplExportConfig.ExportScope.values()).map(Enum::name).collect(Collectors.toList())); - private static final Parameter EXPORT_XNODES_PARAMETER = new Parameter(EXPORT_XNODES, ParameterType.BOOLEAN, "Export Xnodes of tie-lines", Boolean.FALSE); - private static final Parameter EXPORT_ACTION_TYPE_PARAMETER = new Parameter(EXPORT_ACTION_TYPE, ParameterType.STRING, "Type of the remedial actions (preventive or curative)", - AmplExportConfig.ExportActionType.CURATIVE.name(), - Arrays.stream(AmplExportConfig.ExportActionType.values()).map(Enum::name).collect(Collectors.toList())); - private static final Parameter EXPORT_RATIOTAPCHANGER_VT_PARAMETER = new Parameter(EXPORT_RATIOTAPCHANGER_VT, ParameterType.BOOLEAN, "Export ratio tap changer voltage target", Boolean.FALSE) - .addAdditionalNames("iidm.export.ampl.exportRatioTapChangerVoltageTarget"); - private static final Parameter TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER = new Parameter(TWT_SPLIT_SHUNT_ADMITTANCE, ParameterType.BOOLEAN, "Export twt split shunt admittance", Boolean.FALSE) + Arrays.stream(AmplExportConfig.ExportScope.values()).map(Enum::name).collect(Collectors.toList())); + private static final Parameter EXPORT_XNODES_PARAMETER = new Parameter(EXPORT_XNODES, ParameterType.BOOLEAN, + "Export Xnodes of tie-lines", Boolean.FALSE); + private static final Parameter EXPORT_ACTION_TYPE_PARAMETER = new Parameter(EXPORT_ACTION_TYPE, + ParameterType.STRING, "Type of the remedial actions (preventive or curative)", + AmplExportConfig.ExportActionType.CURATIVE.name(), + Arrays.stream(AmplExportConfig.ExportActionType.values()).map(Enum::name).collect(Collectors.toList())); + private static final Parameter EXPORT_RATIOTAPCHANGER_VT_PARAMETER = new Parameter(EXPORT_RATIOTAPCHANGER_VT, + ParameterType.BOOLEAN, "Export ratio tap changer voltage target", Boolean.FALSE) + .addAdditionalNames("iidm.export.ampl.exportRatioTapChangerVoltageTarget"); + private static final Parameter TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER = new Parameter(TWT_SPLIT_SHUNT_ADMITTANCE, + ParameterType.BOOLEAN, "Export twt split shunt admittance", Boolean.FALSE) .addAdditionalNames("iidm.export.ampl.specific-compatibility") .addAdditionalNames("iidm.export.ampl.specificCompatibility"); - private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, EXPORT_ACTION_TYPE_PARAMETER, - EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER); + private static final Parameter EXPORT_VERSION_PARAMETER = new Parameter(EXPORT_VERSION, ParameterType.STRING, + "The version of the export.", + AmplExportVersionImpl.V1_LEGACY.name(), + Arrays.stream(AmplExportVersionImpl.values()).map(Enum::name).collect(Collectors.toList())); + + private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, + EXPORT_ACTION_TYPE_PARAMETER, + EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, EXPORT_VERSION_PARAMETER); private final ParameterDefaultValueConfig defaultValueConfig; @@ -82,9 +95,13 @@ public void export(Network network, Properties parameters, DataSource dataSource boolean exportXnodes = Parameter.readBoolean(getFormat(), parameters, EXPORT_XNODES_PARAMETER, defaultValueConfig); AmplExportConfig.ExportActionType actionType = AmplExportConfig.ExportActionType.valueOf(Parameter.readString(getFormat(), parameters, EXPORT_ACTION_TYPE_PARAMETER, defaultValueConfig)); boolean exportRatioTapChangerVoltageTarget = Parameter.readBoolean(getFormat(), parameters, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, defaultValueConfig); - boolean twtSplitShuntAdmittance = Parameter.readBoolean(getFormat(), parameters, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, defaultValueConfig); + boolean twtSplitShuntAdmittance = Parameter.readBoolean(getFormat(), parameters, + TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, defaultValueConfig); + AmplExportVersion exportVersion = AmplExportVersionImpl.valueOf( + Parameter.readString(getFormat(), parameters, EXPORT_VERSION_PARAMETER, defaultValueConfig)); - AmplExportConfig config = new AmplExportConfig(scope, exportXnodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance); + AmplExportConfig config = new AmplExportConfig(scope, exportXnodes, actionType, + exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, exportVersion); new AmplNetworkWriter(network, dataSource, config).write(); } catch (IOException e) { From fc3adf0ef54394086b5b221050c1c9ac798bead2 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Mon, 12 Jun 2023 11:12:23 +0200 Subject: [PATCH 11/65] add header file indicating version Signed-off-by: Nicolas Pierre --- .../powsybl/ampl/converter/AmplNetworkWriter.java | 9 +++++++++ .../ampl/converter/version/AmplColumnsExporter.java | 9 +++++++++ .../ampl/converter/version/BasicAmplExporter.java | 6 ++++++ .../ampl/converter/AmplNetworkWriterTest.java | 12 ++++++++++-- ampl-converter/src/test/resources/inputs/headers.txt | 1 + 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 ampl-converter/src/test/resources/inputs/headers.txt diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java index ee49bda491c..9a3919848b4 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java @@ -808,8 +808,17 @@ public void write(AmplExportContext context) throws IOException { writeVscConverterStations(); writeLccConverterStations(); writeHvdcLines(); + writeHeaders(); addNetworkExtensions(); exportExtensions(); } + + private void writeHeaders() throws IOException { + try (Writer writer = new OutputStreamWriter( + dataSource.newOutputStream("_headers", "txt", append), StandardCharsets.UTF_8) + ) { + columnsExporter.writeHeaderFile(writer); + } + } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 748fbea1e79..5dbb311a5cf 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -11,6 +11,7 @@ import com.powsybl.iidm.network.*; import java.io.IOException; +import java.io.Writer; import java.util.List; /** @@ -18,6 +19,14 @@ */ public interface AmplColumnsExporter { + String VERSION_HEADER_NAME = "version"; + + String getExporterId(); + + default void writeHeaderFile(Writer headerFileWriter) throws IOException { + headerFileWriter.write(VERSION_HEADER_NAME + " " + getExporterId() + "\n"); + } + List getRtcColumns(); List getPtcColumns(); diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 9e411523392..f4939fce03d 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -32,6 +32,7 @@ */ public class BasicAmplExporter implements AmplColumnsExporter { + private static final String EXPORTER_ID = "V1_LEGACY"; private final AmplExportConfig config; private final Network network; private final StringToIntMapper mapper; @@ -55,6 +56,11 @@ public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMa this.actionNum = actionNum; } + @Override + public String getExporterId() { + return EXPORTER_ID; + } + @Override public List getRtcColumns() { List columns = new ArrayList<>(8); diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index abaacee0336..bb864226d0d 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -6,15 +6,14 @@ */ package com.powsybl.ampl.converter; -import com.powsybl.commons.test.AbstractConverterTest; import com.powsybl.commons.datasource.DataSource; import com.powsybl.commons.datasource.MemDataSource; +import com.powsybl.commons.test.AbstractConverterTest; import com.powsybl.iidm.network.DanglingLine; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.ShuntCompensator; import com.powsybl.iidm.network.test.*; -import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; @@ -23,6 +22,7 @@ import java.util.Properties; import static com.powsybl.commons.test.ComparisonUtils.compareTxt; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Geoffroy Jamgotchian @@ -242,4 +242,12 @@ private void export(Network network, Properties properties, DataSource dataSourc AmplExporter exporter = new AmplExporter(); exporter.export(network, properties, dataSource); } + + @Test + void writeHeaders() throws IOException { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + export(network, new Properties(), dataSource); + assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); + } } diff --git a/ampl-converter/src/test/resources/inputs/headers.txt b/ampl-converter/src/test/resources/inputs/headers.txt new file mode 100644 index 00000000000..99c74f7383f --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/headers.txt @@ -0,0 +1 @@ +version V1_LEGACY From 38eba0a433fdd134be444e32fcadbb5ced12415c Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Mon, 12 Jun 2023 11:17:20 +0200 Subject: [PATCH 12/65] fix test because of new version parameter Signed-off-by: Nicolas Pierre --- .../java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index bb864226d0d..c4aa45b0258 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -40,7 +40,7 @@ void test() { AmplExporter exporter = new AmplExporter(); assertEquals("AMPL", exporter.getFormat()); assertEquals("IIDM to AMPL converter", exporter.getComment()); - assertEquals(5, exporter.getParameters().size()); + assertEquals(6, exporter.getParameters().size()); } @Test From a0160f30d9fee0c26f439bfc9efe45784f42e6bd Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Tue, 13 Jun 2023 11:04:35 +0200 Subject: [PATCH 13/65] fix BasicAmplExporter according to b7f9c3df Signed-off-by: Nicolas Pierre --- .../ampl/converter/version/BasicAmplExporter.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index f4939fce03d..c78ac1ccd4a 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -443,7 +443,6 @@ public void writePtcToFormatter(TableFormatter formatter) { @Override public void writeCurrentLimits(TableFormatter formatter) throws IOException { writeBranchCurrentLimits(formatter); - writeTieLineCurrentLimits(formatter); writeThreeWindingsTransformerCurrentLimits(formatter); writeDanglingLineCurrentLimits(formatter); } @@ -552,20 +551,6 @@ private void writeBranchCurrentLimits(TableFormatter formatter) throws IOExcepti } } - private void writeTieLineCurrentLimits(TableFormatter formatter) throws IOException { - for (TieLine line : network.getTieLines()) { - String lineId = line.getId(); - Optional currentLimits1 = line.getDanglingLine1().getCurrentLimits(); - if (currentLimits1.isPresent()) { - writeTemporaryCurrentLimits(currentLimits1.get(), formatter, lineId, true, "_1_"); - } - Optional currentLimits2 = line.getDanglingLine2().getCurrentLimits(); - if (currentLimits2.isPresent()) { - writeTemporaryCurrentLimits(currentLimits2.get(), formatter, lineId, false, "_2_"); - } - } - } - private void writeThreeWindingsTransformerCurrentLimits(TableFormatter formatter) throws IOException { for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) { for (ThreeWindingsTransformer.Side side : ThreeWindingsTransformer.Side.values()) { From 261c0e06ffaad69a06d76ccbd61b3c452f7d6ad4 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Tue, 13 Jun 2023 12:04:11 +0200 Subject: [PATCH 14/65] clean Signed-off-by: Nicolas Pierre --- .../ampl/converter/AmplNetworkWriter.java | 34 ++++++------------- .../com/powsybl/ampl/converter/AmplUtil.java | 4 +++ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java index 9a3919848b4..1018f5d8742 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java @@ -335,8 +335,8 @@ private void writeLines(AmplExportContext context, TableFormatter formatter) thr LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", l.getId()); continue; } - String bus1Id = getBusId(bus1); - String bus2Id = getBusId(bus2); + String bus1Id = AmplUtil.getBusId(bus1); + String bus2Id = AmplUtil.getBusId(bus2); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) { continue; } @@ -357,8 +357,8 @@ private void writeTieLines(AmplExportContext context, TableFormatter formatter) LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", l.getId()); continue; } - String bus1Id = getBusId(bus1); - String bus2Id = getBusId(bus2); + String bus1Id = AmplUtil.getBusId(bus1); + String bus2Id = AmplUtil.getBusId(bus2); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) { continue; } @@ -379,7 +379,8 @@ private void writeTwoWindingsTransformers(AmplExportContext context, TableFormat LOGGER.warn("Skipping transformer '{}' connected to the same bus at both sides", twt.getId()); continue; } - if (isOnlyMainCc() && !(isBusExported(context, getBusId(bus1)) || isBusExported(context, getBusId(bus2)))) { + if (isOnlyMainCc() && !(isBusExported(context, AmplUtil.getBusId(bus1)) || isBusExported(context, + AmplUtil.getBusId(bus2)))) { continue; } context.voltageLevelIdsToExport.add(t1.getVoltageLevel().getId()); @@ -410,9 +411,9 @@ private void writeThreeWindingsTransformers(AmplExportContext context, TableForm int num1 = mapper.getInt(AmplSubset.BRANCH, id1); int num2 = mapper.getInt(AmplSubset.BRANCH, id2); int num3 = mapper.getInt(AmplSubset.BRANCH, id3); - String bus1Id = getBusId(bus1); - String bus2Id = getBusId(bus2); - String bus3Id = getBusId(bus3); + String bus1Id = AmplUtil.getBusId(bus1); + String bus2Id = AmplUtil.getBusId(bus2); + String bus3Id = AmplUtil.getBusId(bus3); int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt)); @@ -441,7 +442,7 @@ private void writeDanglingLines(AmplExportContext context, TableFormatter format for (DanglingLine dl : network.getDanglingLines(DanglingLineFilter.UNPAIRED)) { Terminal t = dl.getTerminal(); Bus bus1 = AmplUtil.getBus(t); - String bus1Id = getBusId(bus1); + String bus1Id = AmplUtil.getBusId(bus1); String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, middleBusId))) { continue; @@ -455,14 +456,6 @@ private void writeDanglingLines(AmplExportContext context, TableFormatter format } } - private String getBusId(Bus bus) { - return bus == null ? null : bus.getId(); - } - - private int getBusNum(Bus bus) { - return bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); - } - private void writeTapChangerTable() throws IOException { try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_tct", "txt", append), StandardCharsets.UTF_8); @@ -691,17 +684,10 @@ private void writeBatteries(AmplExportContext context) throws IOException { Terminal t = b.getTerminal(); Bus bus = AmplUtil.getBus(t); String busId = null; - int busNum = -1; if (bus != null) { busId = bus.getId(); - busNum = mapper.getInt(AmplSubset.BUS, bus.getId()); } - int conBusNum = -1; // take connectable bus if exists - Bus conBus = AmplUtil.getConnectableBus(t); - if (conBus != null) { - conBusNum = mapper.getInt(AmplSubset.BUS, conBus.getId()); - } if (!isBusExported(context, busId)) { skipped.add(b.getId()); } else { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java index e45c6a0d625..c9a2a659b87 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java @@ -26,6 +26,10 @@ public static Iterable getBuses(Network n) { return n.getBusView().getBuses(); } + public static String getBusId(Bus bus) { + return bus == null ? null : bus.getId(); + } + public static Bus getBus(Terminal t) { return t.getBusView().getBus(); } From 0770a2cb2ea7e22c45e45fb4a770cd1bf7d2d683 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 10:55:08 +0200 Subject: [PATCH 15/65] add networkUtil Signed-off-by: Nicolas Pierre --- ampl-converter/pom.xml | 4 +++ .../ampl/converter/util/NetworkUtil.java | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java diff --git a/ampl-converter/pom.xml b/ampl-converter/pom.xml index ef84a26c1e8..8a3118520ff 100644 --- a/ampl-converter/pom.xml +++ b/ampl-converter/pom.xml @@ -89,5 +89,9 @@ ${project.version} test + + com.powsybl + powsybl-iidm-extensions + diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java new file mode 100644 index 00000000000..a0b989ee350 --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.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/. + */ +package com.powsybl.ampl.converter.util; + +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Terminal; +import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.iidm.network.extensions.SlackTerminal; + +/** + * @author Nicolas PIERRE + */ +public final class NetworkUtil { + + public static boolean isSlackBus(Bus bus) { + VoltageLevel vl = bus.getVoltageLevel(); + SlackTerminal slackTerminal = vl.getExtension(SlackTerminal.class); + if (slackTerminal != null) { + Terminal terminal = slackTerminal.getTerminal(); + return terminal.getBusBreakerView().getBus() == bus; + } + return false; + } + + private NetworkUtil() { + } +} From 54dd6c184a65a7d96b31705970a4bbb1828ae039 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 11:00:57 +0200 Subject: [PATCH 16/65] add getters for inheritance Signed-off-by: Nicolas Pierre --- .../converter/version/BasicAmplExporter.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index c78ac1ccd4a..e8a41b13876 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -1422,4 +1422,28 @@ private static double getPermanentLimit(CurrentLimits limits) { } return Double.NaN; } + + public AmplExportConfig getConfig() { + return config; + } + + public Network getNetwork() { + return network; + } + + public StringToIntMapper getMapper() { + return mapper; + } + + public int getVariantIndex() { + return variantIndex; + } + + public int getFaultNum() { + return faultNum; + } + + public int getActionNum() { + return actionNum; + } } From 7e559a950374c586d7dee8492b33fbbb34ab4b9f Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 11:01:59 +0200 Subject: [PATCH 17/65] added ExtendedAmplExporter remove static getFactory Signed-off-by: Nicolas Pierre --- .../version/AmplExportVersionImpl.java | 3 ++- .../converter/version/BasicAmplExporter.java | 4 --- .../version/ExtendedAmplExporter.java | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java index 0e458ca79bd..c6dd852fb69 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java @@ -13,7 +13,8 @@ public enum AmplExportVersionImpl implements AmplExportVersion { /** * Legacy export */ - V1_LEGACY(BasicAmplExporter.getFactory()); + V1_LEGACY(BasicAmplExporter::new), + V2(ExtendedAmplExporter::new); private final AmplExportVersion.Factory factory; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index e8a41b13876..85133ee01b7 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -41,10 +41,6 @@ public class BasicAmplExporter implements AmplColumnsExporter { private final int actionNum; private HashMap hvdcLinesMap; - public static AmplExportVersion.Factory getFactory() { - return BasicAmplExporter::new; - } - public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMapper mapper, int variantIndex, int faultNum, int actionNum) { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java new file mode 100644 index 00000000000..653be38f87c --- /dev/null +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java @@ -0,0 +1,26 @@ +/** + * 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/. + */ +package com.powsybl.ampl.converter.version; + +import com.powsybl.ampl.converter.AmplExportConfig; +import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.commons.util.StringToIntMapper; +import com.powsybl.iidm.network.Network; + +/** + * @author Nicolas PIERRE + */ +public class ExtendedAmplExporter extends BasicAmplExporter { + + public ExtendedAmplExporter(AmplExportConfig config, + Network network, + StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum) { + super(config, network, mapper, variantIndex, faultNum, actionNum); + } + +} From 353c1f197efae414b75080544cae53961a164260 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 11:04:19 +0200 Subject: [PATCH 18/65] added new columns : Slack bus boolean in Bus r g and b in TapSteps regulating bus id in Generators and Svc Signed-off-by: Nicolas Pierre --- .../powsybl/ampl/converter/AmplConstants.java | 1 + .../version/ExtendedAmplExporter.java | 308 +++++++++++++++++- 2 files changed, 308 insertions(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java index 14a0377f562..795a279a549 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java @@ -26,6 +26,7 @@ private AmplConstants() { public static final String MINP = "minP (MW)"; public static final String MAXP = "maxP (MW)"; public static final String V_REGUL = "v regul."; + public static final String V_REGUL_BUS = "v regul. bus"; public static final String ACTIVE_POWER = "P (MW)"; public static final String REACTIVE_POWER = "Q (MVar)"; public static final String MIN_Q_MAX_P = "minQmaxP (MVar)"; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java index 653be38f87c..f922bba0082 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java @@ -6,10 +6,21 @@ */ package com.powsybl.ampl.converter.version; +import com.powsybl.ampl.converter.AmplConstants; import com.powsybl.ampl.converter.AmplExportConfig; import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.ampl.converter.AmplUtil; +import com.powsybl.ampl.converter.util.NetworkUtil; +import com.powsybl.commons.io.table.Column; +import com.powsybl.commons.io.table.TableFormatter; import com.powsybl.commons.util.StringToIntMapper; -import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.*; +import com.powsybl.iidm.network.util.ConnectedComponents; + +import java.io.IOException; +import java.util.List; + +import static com.powsybl.ampl.converter.AmplConstants.*; /** * @author Nicolas PIERRE @@ -23,4 +34,299 @@ public ExtendedAmplExporter(AmplExportConfig config, super(config, network, mapper, variantIndex, faultNum, actionNum); } + private static class ImpedanceAndAdmittance { + double r; + double x; + double g; + double b; + + public ImpedanceAndAdmittance(double r, double x, double g, double b) { + this.r = r; + this.x = x; + this.g = g; + this.b = b; + } + } + + @Override + public List getBusesColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column(SUBSTATION), + new Column("cc"), + new Column("v (pu)"), + new Column("theta (rad)"), + new Column("p (MW)"), + new Column("q (MVar)"), + new Column("slack bus"), + new Column(FAULT), + new Column(getConfig().getActionType().getLabel()), + new Column(ID)); + } + + @Override + public List getTapChangerTableColumns() { + return List.of(new Column(VARIANT), + new Column(NUM), + new Column("tap"), + new Column("var ratio"), + new Column("r (pu)"), + new Column("x (pu)"), + new Column("g (pu)"), + new Column("b (pu)"), + new Column("angle (rad)"), + new Column(FAULT), + new Column(getConfig().getActionType().getLabel())); + } + + @Override + public List getGeneratorsColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column(MINP), + new Column(MAXP), + new Column(MIN_Q_MAX_P), + new Column(MIN_Q0), + new Column(MIN_Q_MIN_P), + new Column(MAX_Q_MAX_P), + new Column(MAX_Q0), + new Column(MAX_Q_MIN_P), + new Column(V_REGUL), + new Column(V_REGUL_BUS), + new Column(TARGET_V), + new Column("targetP (MW)"), + new Column(TARGET_Q), + new Column(FAULT), + new Column(getConfig().getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public List getStaticVarCompensatorColumns() { + return List.of( + new Column(VARIANT), + new Column(NUM), + new Column(BUS), + new Column(CON_BUS), + new Column(SUBSTATION), + new Column("minB (pu)"), + new Column("maxB (pu)"), + new Column(V_REGUL), + new Column(V_REGUL_BUS), + new Column(TARGET_V), + new Column(TARGET_Q), + new Column(FAULT), + new Column(getConfig().getActionType().getLabel()), + new Column(ID), + new Column(DESCRIPTION), + new Column(ACTIVE_POWER), + new Column(REACTIVE_POWER) + ); + } + + @Override + public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws IOException { + int ccNum = ConnectedComponents.getCcNum(b); + String id = b.getId(); + VoltageLevel vl = b.getVoltageLevel(); + int num = getMapper().getInt(AmplSubset.BUS, id); + int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); + double nomV = vl.getNominalV(); + double v = b.getV() / nomV; + double theta = Math.toRadians(b.getAngle()); + formatter.writeCell(getVariantIndex()) + .writeCell(num) + .writeCell(vlNum) + .writeCell(ccNum) + .writeCell(v) + .writeCell(theta) + .writeCell(b.getP()) + .writeCell(b.getQ()) + .writeCell(NetworkUtil.isSlackBus(b)) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(id); + } + + @Override + public void writeTwoWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + TwoWindingsTransformer twt) throws IOException { + Terminal t2 = twt.getTerminal2(); + double vb2 = t2.getVoltageLevel().getNominalV(); + double zb2 = vb2 * vb2 / AmplConstants.SB; + ImpedanceAndAdmittance transformer = new ImpedanceAndAdmittance(twt.getR(), twt.getX(), + twt.getG(), twt.getB()); + RatioTapChanger rtc = twt.getRatioTapChanger(); + if (rtc != null) { + String id = twt.getId() + RATIO_TABLE; + writeRatioTapChanger(formatter, id, zb2, transformer, rtc); + } + + PhaseTapChanger ptc = twt.getPhaseTapChanger(); + if (ptc != null) { + String id = twt.getId() + PHASE_TABLE; + writePhaseTapChanger(formatter, id, zb2, transformer, ptc); + } + } + + @Override + public void writeThreeWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt) throws IOException { + int legNumber = 0; + for (ThreeWindingsTransformer.Leg leg : twt.getLegs()) { + legNumber++; + RatioTapChanger rtc = leg.getRatioTapChanger(); + double vb = twt.getRatedU0(); + double zb = vb * vb / AmplConstants.SB; + ImpedanceAndAdmittance transformer = new ImpedanceAndAdmittance(leg.getR(), leg.getX(), + leg.getG(), leg.getB()); + if (rtc != null) { + String id = twt.getId() + "_leg" + legNumber + RATIO_TABLE; + writeRatioTapChanger(formatter, id, zb, transformer, rtc); + } + PhaseTapChanger ptc = leg.getPhaseTapChanger(); + if (ptc != null) { + String id = twt.getId() + "_leg" + legNumber + PHASE_TABLE; + writePhaseTapChanger(formatter, id, zb, transformer, ptc); + } + } + } + + private void writeRatioTapChanger(TableFormatter formatter, String id, double zb2, + ImpedanceAndAdmittance transformerZandY, + RatioTapChanger rtc) throws IOException { + int num = getMapper().getInt(AmplSubset.TAP_CHANGER_TABLE, id); + + int lowTapPosition = rtc.getLowTapPosition(); + for (int position = lowTapPosition; position <= rtc.getHighTapPosition(); position++) { + RatioTapChangerStep step = rtc.getStep(position); + writeTapChanger(formatter, zb2, + transformerZandY, num, rtc.getLowTapPosition(), + new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()), position, step.getRho(), + 0); + } + } + + private void writePhaseTapChanger(TableFormatter formatter, String id, double zb2, + ImpedanceAndAdmittance transformerZandY, + PhaseTapChanger ptc) throws IOException { + int num = getMapper().getInt(AmplSubset.TAP_CHANGER_TABLE, id); + + for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { + PhaseTapChangerStep step = ptc.getStep(position); + writeTapChanger(formatter, zb2, + transformerZandY, num, ptc.getLowTapPosition(), + new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()), position, step.getRho(), + Math.toRadians(step.getAlpha())); + } + } + + private void writeTapChanger(TableFormatter formatter, double zb2, ImpedanceAndAdmittance transformer, int num, + int lowTapPosition, ImpedanceAndAdmittance step, int position, double rho, + double alpha) throws IOException { + double rNorm = transformer.r * (1 + step.r / 100) / zb2; + double xNorm = transformer.x * (1 + step.x / 100) / zb2; + double gNorm = transformer.g * (1 + step.g / 100) / zb2; + double bNorm = transformer.b * (1 + step.b / 100) / zb2; + formatter.writeCell(getVariantIndex()) + .writeCell(num) + .writeCell(position - lowTapPosition + 1) + .writeCell(rho) + .writeCell(rNorm) + .writeCell(xNorm) + .writeCell(gNorm) + .writeCell(bNorm) + .writeCell(alpha) + .writeCell(getFaultNum()) + .writeCell(getActionNum()); + } + + @Override + public void writeGeneratorToFormatter(TableFormatter formatter, Generator gen) throws IOException { + String id = gen.getId(); + Terminal t = gen.getTerminal(); + int num = getMapper().getInt(AmplSubset.GENERATOR, id); + int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + int busNum = AmplUtil.getBusNum(getMapper(), t); + int conBusNum = AmplUtil.getConnectableBusNum(getMapper(), t); + double minP = gen.getMinP(); + double maxP = gen.getMaxP(); + double vb = gen.getRegulatingTerminal().getVoltageLevel().getNominalV(); + int regulatingBusNum = gen.isVoltageRegulatorOn() ? + getMapper().getInt(AmplSubset.BUS, gen.getRegulatingTerminal().getBusView().getBus().getId()) : -1; + + formatter.writeCell(getVariantIndex()) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum != -1 ? conBusNum : busNum) + .writeCell(vlNum) + .writeCell(minP) + .writeCell(maxP) + .writeCell(gen.getReactiveLimits().getMinQ(maxP)) + .writeCell(gen.getReactiveLimits().getMinQ(0)) + .writeCell(gen.getReactiveLimits().getMinQ(minP)) + .writeCell(gen.getReactiveLimits().getMaxQ(maxP)) + .writeCell(gen.getReactiveLimits().getMaxQ(0)) + .writeCell(gen.getReactiveLimits().getMaxQ(minP)) + .writeCell(gen.isVoltageRegulatorOn()) + .writeCell(regulatingBusNum) + .writeCell(gen.getTargetV() / vb) + .writeCell(gen.getTargetP()) + .writeCell(gen.getTargetQ()) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(id) + .writeCell(gen.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + } + + @Override + public void writeStaticVarCompensatorToFormatter(TableFormatter formatter, + StaticVarCompensator svc) throws IOException { + String id = svc.getId(); + int num = getMapper().getInt(AmplSubset.STATIC_VAR_COMPENSATOR, id); + + Terminal t = svc.getTerminal(); + + int busNum = AmplUtil.getBusNum(getMapper(), t); + int conBusNum = AmplUtil.getConnectableBusNum(getMapper(), t); + + boolean voltageRegulation = svc.getRegulationMode().equals(StaticVarCompensator.RegulationMode.VOLTAGE); + int regulatingBusNum = voltageRegulation ? + getMapper().getInt(AmplSubset.BUS, svc.getRegulatingTerminal().getBusView().getBus().getId()) : -1; + + double vlSet = svc.getVoltageSetpoint(); + double vb = t.getVoltageLevel().getNominalV(); + double zb = vb * vb / AmplConstants.SB; // Base impedance + + int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); + formatter.writeCell(getVariantIndex()) + .writeCell(num) + .writeCell(busNum) + .writeCell(conBusNum) + .writeCell(vlNum) + .writeCell(svc.getBmin() * zb) + .writeCell(svc.getBmax() * zb) + .writeCell(voltageRegulation) + .writeCell(regulatingBusNum) + .writeCell(vlSet / vb) + .writeCell(svc.getReactivePowerSetpoint()) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(id) + .writeCell(svc.getNameOrId()) + .writeCell(t.getP()) + .writeCell(t.getQ()); + + } + } From bde0378d144e49b28c920074892b5f7988f45387 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 17:08:09 +0200 Subject: [PATCH 19/65] wip tests Signed-off-by: Nicolas Pierre --- .../converter/ExtendedAmplExporterTest.java | 90 +++++++++++++++++++ .../eurostag-tutorial-example1-buses.txt | 6 ++ ...ial-example1-generators-regulating-bus.txt | 4 + .../svc-test-case-regulating-bus.txt | 4 + 4 files changed, 104 insertions(+) create mode 100644 ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses.txt create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java new file mode 100644 index 00000000000..ed3ee965f1b --- /dev/null +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -0,0 +1,90 @@ +/** + * 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/. + */ +package com.powsybl.ampl.converter; + +import com.powsybl.ampl.converter.version.AmplExportVersionImpl; +import com.powsybl.commons.datasource.MemDataSource; +import com.powsybl.commons.test.AbstractConverterTest; +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.StaticVarCompensator; +import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.iidm.network.extensions.SlackTerminalAdder; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.iidm.network.test.SvcTestCaseFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static com.powsybl.commons.test.ComparisonUtils.compareTxt; + +/** + * @author Nicolas PIERRE + */ +public class ExtendedAmplExporterTest extends AbstractConverterTest { + + private AmplExportConfig v2Config; + + private void assertEqualsToRef(MemDataSource dataSource, String suffix, String refFileName) throws IOException { + try (InputStream actual = new ByteArrayInputStream(dataSource.getData(suffix, "txt"))) { + compareTxt(getClass().getResourceAsStream("/inputs/extended_exporter/" + refFileName), actual); + } + } + + @BeforeEach + public void setUp() throws IOException { + super.setUp(); + v2Config = new AmplExportConfig(AmplExportConfig.ExportScope.ALL, false, + AmplExportConfig.ExportActionType.CURATIVE, false, false, + AmplExportVersionImpl.V2); + } + + @Test + public void testSlackBusExport() throws IOException { + Network network = EurostagTutorialExample1Factory.create(); + VoltageLevel vlGen = network.getVoltageLevel("VLLOAD"); + Bus bus = vlGen.getBusBreakerView().getBus("NLOAD"); + SlackTerminalAdder adder = vlGen.newExtension(SlackTerminalAdder.class); + adder.withTerminal(bus.getConnectedTerminals().iterator().next()).add(); + + MemDataSource dataSource = new MemDataSource(); + new AmplNetworkWriter(network, dataSource, v2Config).write(); + + assertEqualsToRef(dataSource, "_network_buses", + "eurostag-tutorial-example1-buses.txt"); + } + + @Test + public void testNewTapExport() throws IOException { + + } + + @Test + public void testRegulatingBusIdExportGenerators() throws IOException { + Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); + network.getGenerator("GEN").setVoltageRegulatorOn(false); + + MemDataSource dataSource = new MemDataSource(); + new AmplNetworkWriter(network, dataSource, v2Config).write(); + + assertEqualsToRef(dataSource, "_network_generators", + "eurostag-tutorial-example1-generators-regulating-bus.txt"); + } + + @Test + public void testRegulatingBusIdExportSvc() throws IOException { + Network network = SvcTestCaseFactory.createWithMoreSVCs(); + network.getStaticVarCompensator("SVC2").setRegulationMode(StaticVarCompensator.RegulationMode.OFF); + MemDataSource dataSource = new MemDataSource(); + new AmplNetworkWriter(network, dataSource, v2Config).write(); + + assertEqualsToRef(dataSource, "_network_static_var_compensators", "svc-test-case-regulating-bus.txt"); + } +} diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses.txt new file mode 100644 index 00000000000..4d907ee5d04 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses.txt @@ -0,0 +1,6 @@ +#Buses (sim1/InitialState) +#"variant" "num" "substation" "cc" "v (pu)" "theta (rad)" "p (MW)" "q (MVar)" "slack bus" "fault" "curative" "id" +1 1 1 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "VLGEN_0" +1 2 2 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "VLHV1_0" +1 3 3 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "VLHV2_0" +1 4 4 0 -99999.0 -99999.0 0.00000 0.00000 true 0 0 "VLLOAD_0" diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt new file mode 100644 index 00000000000..0ee5057c9df --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt @@ -0,0 +1,4 @@ +#Generators (sim1/InitialState) +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 false -1 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 +1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt new file mode 100644 index 00000000000..0f867134bb0 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt @@ -0,0 +1,4 @@ +#Static VAR compensators (svcTestCase/InitialState) +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +1 1 2 2 2 0.288800 1.15520 false -1 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 +1 2 2 2 2 0.288800 1.15520 true 2 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 From 6e5cb2d22b748c2d2578a8e4657da96724173ab0 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 7 Jun 2023 17:19:35 +0200 Subject: [PATCH 20/65] export is in busView Signed-off-by: Nicolas Pierre --- .../main/java/com/powsybl/ampl/converter/util/NetworkUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java index a0b989ee350..586f7777081 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java @@ -21,7 +21,7 @@ public static boolean isSlackBus(Bus bus) { SlackTerminal slackTerminal = vl.getExtension(SlackTerminal.class); if (slackTerminal != null) { Terminal terminal = slackTerminal.getTerminal(); - return terminal.getBusBreakerView().getBus() == bus; + return terminal.getBusView().getBus() == bus; } return false; } From 334e7e0a0d554be8ed48bf4a2ed5ca1bea049b43 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Fri, 9 Jun 2023 15:35:16 +0200 Subject: [PATCH 21/65] WIP tap tests Signed-off-by: Nicolas Pierre --- .../ampl/converter/ExtendedAmplExporterTest.java | 14 ++++++++++---- .../eurostag-tutorial-example1-tct.txt | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-tct.txt diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java index ed3ee965f1b..5a94ef05648 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -9,10 +9,7 @@ import com.powsybl.ampl.converter.version.AmplExportVersionImpl; import com.powsybl.commons.datasource.MemDataSource; import com.powsybl.commons.test.AbstractConverterTest; -import com.powsybl.iidm.network.Bus; -import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.StaticVarCompensator; -import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.extensions.SlackTerminalAdder; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.iidm.network.test.SvcTestCaseFactory; @@ -63,6 +60,15 @@ public void testSlackBusExport() throws IOException { @Test public void testNewTapExport() throws IOException { + Network network = EurostagTutorialExample1Factory.create(); + + TwoWindingsTransformer transformer = network.getTwoWindingsTransformers().iterator().next(); + transformer.newRatioTapChanger(); + transformer.newPhaseTapChanger(); + MemDataSource dataSource = new MemDataSource(); + new AmplNetworkWriter(network, dataSource, v2Config).write(); + assertEqualsToRef(dataSource, "_network_tct", + "eurostag-tutorial-example1-tct.txt"); } diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-tct.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-tct.txt new file mode 100644 index 00000000000..e6bc54e3d8f --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-tct.txt @@ -0,0 +1,5 @@ +#Tap changer table (sim1/InitialState) +#"variant" "num" "tap" "var ratio" "r (pu)" "x (pu)" "g (pu)" "b (pu)" "angle (rad)" "fault" "curative" +1 1 1 0.850567 0.000210000 0.0179988 0.00000 0.00000 0.00000 0 0 +1 1 2 1.00067 0.000210000 0.0179988 0.00000 0.00000 0.00000 0 0 +1 1 3 1.15077 0.000210000 0.0179988 0.00000 0.00000 0.00000 0 0 From 3e11f7b034bb1d50ed2fe152d66d5437f809427d Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Tue, 13 Jun 2023 15:25:05 +0200 Subject: [PATCH 22/65] clean public for tests Signed-off-by: Nicolas Pierre --- .../ampl/converter/ExtendedAmplExporterTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java index 5a94ef05648..35d0784c0ec 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -25,7 +25,7 @@ /** * @author Nicolas PIERRE */ -public class ExtendedAmplExporterTest extends AbstractConverterTest { +class ExtendedAmplExporterTest extends AbstractConverterTest { private AmplExportConfig v2Config; @@ -44,7 +44,7 @@ public void setUp() throws IOException { } @Test - public void testSlackBusExport() throws IOException { + void testSlackBusExport() throws IOException { Network network = EurostagTutorialExample1Factory.create(); VoltageLevel vlGen = network.getVoltageLevel("VLLOAD"); Bus bus = vlGen.getBusBreakerView().getBus("NLOAD"); @@ -59,7 +59,7 @@ public void testSlackBusExport() throws IOException { } @Test - public void testNewTapExport() throws IOException { + void testNewTapExport() throws IOException { Network network = EurostagTutorialExample1Factory.create(); TwoWindingsTransformer transformer = network.getTwoWindingsTransformers().iterator().next(); @@ -73,7 +73,7 @@ public void testNewTapExport() throws IOException { } @Test - public void testRegulatingBusIdExportGenerators() throws IOException { + void testRegulatingBusIdExportGenerators() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); network.getGenerator("GEN").setVoltageRegulatorOn(false); @@ -85,7 +85,7 @@ public void testRegulatingBusIdExportGenerators() throws IOException { } @Test - public void testRegulatingBusIdExportSvc() throws IOException { + void testRegulatingBusIdExportSvc() throws IOException { Network network = SvcTestCaseFactory.createWithMoreSVCs(); network.getStaticVarCompensator("SVC2").setRegulationMode(StaticVarCompensator.RegulationMode.OFF); MemDataSource dataSource = new MemDataSource(); From 39060bfaa0250e9b64d83756acbb2035f572bf67 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Mon, 7 Aug 2023 16:01:53 +0200 Subject: [PATCH 23/65] fix constants typo Signed-off-by: Nicolas Pierre --- .../powsybl/ampl/converter/AmplConstants.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java index 795a279a549..5dc3ce0a7c4 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java @@ -21,20 +21,20 @@ private AmplConstants() { public static final String DESCRIPTION = "description"; public static final String SUBSTATION = "substation"; public static final String TARGET_V = "targetV (pu)"; - public static final String TARGET_Q = "targetQ (MVar)"; + public static final String TARGET_Q = "targetQ (MVAr)"; public static final String CON_BUS = "con. bus"; public static final String MINP = "minP (MW)"; public static final String MAXP = "maxP (MW)"; public static final String V_REGUL = "v regul."; public static final String V_REGUL_BUS = "v regul. bus"; public static final String ACTIVE_POWER = "P (MW)"; - public static final String REACTIVE_POWER = "Q (MVar)"; - public static final String MIN_Q_MAX_P = "minQmaxP (MVar)"; - public static final String MIN_Q0 = "minQ0 (MVar)"; - public static final String MIN_Q_MIN_P = "minQminP (MVar)"; - public static final String MAX_Q_MAX_P = "maxQmaxP (MVar)"; - public static final String MAX_Q0 = "maxQ0 (MVar)"; - public static final String MAX_Q_MIN_P = "maxQminP (MVar)"; + public static final String REACTIVE_POWER = "Q (MVAr)"; + public static final String MIN_Q_MAX_P = "minQmaxP (MVAr)"; + public static final String MIN_Q0 = "minQ0 (MVAr)"; + public static final String MIN_Q_MIN_P = "minQminP (MVAr)"; + public static final String MAX_Q_MAX_P = "maxQmaxP (MVAr)"; + public static final String MAX_Q0 = "maxQ0 (MVAr)"; + public static final String MAX_Q_MIN_P = "maxQminP (MVAr)"; public static final String NUM = "num"; public static final String BUS = "bus"; public static final String P0 = "p0 (MW)"; From 4e1c69430371286e2887f241b1f199c27e59abe3 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 9 Aug 2023 16:25:55 +0200 Subject: [PATCH 24/65] fix MVar to MVAr Signed-off-by: Nicolas Pierre --- .../src/test/resources/inputs/battery-test-batteries.txt | 2 +- .../resources/inputs/eurostag-tutorial-example1-generators.txt | 2 +- .../test/resources/inputs/eurostag-tutorial-example1-shunts.txt | 2 +- ampl-converter/src/test/resources/inputs/lcc-test-case.txt | 2 +- .../src/test/resources/inputs/shunt-test-case-shunts.txt | 2 +- ampl-converter/src/test/resources/inputs/svc-test-case.txt | 2 +- ampl-converter/src/test/resources/inputs/vsc-test-case.txt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt b/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt index acf6218c0f1..baa15672ba4 100644 --- a/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt +++ b/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt @@ -1,4 +1,4 @@ #Batteries (fictitious/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "p0 (MW)" "q0 (MW)" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "p0 (MW)" "q0 (MW)" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 2 2 2 9999.99 9999.99 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 0 0 "BAT" "BAT" -605.000 -225.000 1 2 2 2 2 100.000 200.000 -200.000 200.000 -54.5500 -59.3000 -59.3000 46.2500 60.0000 60.0000 0 0 "BAT2" "BAT2" -605.000 -225.000 diff --git a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt index 05c0fa04868..06cca302550 100644 --- a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt +++ b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt @@ -1,4 +1,4 @@ #Generators (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "targetV (pu)" "targetP (MW)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 true 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt index e0f5959dee6..008cd2ab353 100644 --- a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt +++ b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt @@ -1,3 +1,3 @@ #Shunts (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" "sections count" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" "sections count" 1 1 -1 -1 5 0.00000 5.76000e-05 0 5.76000e-05 0 0 "SHUNT" "SHUNT" -99999.0 -99999.0 1 diff --git a/ampl-converter/src/test/resources/inputs/lcc-test-case.txt b/ampl-converter/src/test/resources/inputs/lcc-test-case.txt index fa6d31515f8..8ab3efa609a 100644 --- a/ampl-converter/src/test/resources/inputs/lcc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/lcc-test-case.txt @@ -1,4 +1,4 @@ #LCC Converter Stations (hvdctest/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "lossFactor (%PDC)" "powerFactor" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "lossFactor (%PDC)" "powerFactor" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 1 1 1 1.10000 0.500000 0 0 "C1" "Converter1" 100.000 50.0000 1 2 2 2 2 1.10000 0.600000 0 0 "C2" "Converter2" 75.0000 25.0000 diff --git a/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt b/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt index 89287149ef4..9fa7f904dc1 100644 --- a/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt +++ b/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt @@ -1,3 +1,3 @@ #Shunts (shuntTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" "sections count" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" "sections count" 1 1 1 1 1 0.0144400 28.8800 0 28.8800 0 0 "SHUNT" "SHUNT" -99999.0 -99999.0 1 diff --git a/ampl-converter/src/test/resources/inputs/svc-test-case.txt b/ampl-converter/src/test/resources/inputs/svc-test-case.txt index e18c7e48319..2617f1848e6 100644 --- a/ampl-converter/src/test/resources/inputs/svc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/svc-test-case.txt @@ -1,4 +1,4 @@ #Static VAR compensators (svcTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "targetV (pu)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 2 2 2 0.288800 1.15520 true 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 1 2 2 2 2 0.288800 1.15520 true 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/vsc-test-case.txt b/ampl-converter/src/test/resources/inputs/vsc-test-case.txt index 5852cf7396d..7b4de2bbf3f 100644 --- a/ampl-converter/src/test/resources/inputs/vsc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/vsc-test-case.txt @@ -1,4 +1,4 @@ #VSC Converter Stations (hvdctest/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "targetV (pu)" "targetQ (MVar)" "lossFactor (%PDC)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "targetV (pu)" "targetQ (MVAr)" "lossFactor (%PDC)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 1 1 1 -300.000 300.000 0.00000 0.00000 0.00000 10.0000 10.0000 10.0000 true 1.01250 -99999.0 1.10000 0 0 "C1" "Converter1" 100.000 50.0000 1 2 2 2 2 -300.000 300.000 0.00000 0.00000 0.00000 10.0000 10.0000 10.0000 false -99999.0 123.000 1.10000 0 0 "C2" "Converter2" -99999.0 -99999.0 From 96d8e905b740d4f1520924f557a9ff23713d4c77 Mon Sep 17 00:00:00 2001 From: Nicolas Pierre Date: Wed, 9 Aug 2023 16:40:51 +0200 Subject: [PATCH 25/65] fix MVar to MVAr Signed-off-by: Nicolas Pierre --- .../eurostag-tutorial-example1-generators-regulating-bus.txt | 2 +- .../inputs/extended_exporter/svc-test-case-regulating-bus.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt index 0ee5057c9df..94c51673c42 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt @@ -1,4 +1,4 @@ #Generators (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 false -1 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt index 0f867134bb0..762a77c7553 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt @@ -1,4 +1,4 @@ #Static VAR compensators (svcTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" 1 1 2 2 2 0.288800 1.15520 false -1 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 1 2 2 2 2 0.288800 1.15520 true 2 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 From 43dabbbe35843fe1d61a786ab257f7b7106bbce2 Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:54:39 +0100 Subject: [PATCH 26/65] formatting Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../main/java/com/powsybl/ampl/converter/AmplExporter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 8a4f72dc518..67af4f7eb5b 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -63,8 +63,8 @@ public class AmplExporter implements Exporter { Arrays.stream(AmplExportVersionImpl.values()).map(Enum::name).collect(Collectors.toList())); private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, - EXPORT_ACTION_TYPE_PARAMETER, - EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, EXPORT_VERSION_PARAMETER); + EXPORT_ACTION_TYPE_PARAMETER, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, + EXPORT_VERSION_PARAMETER); private final ParameterDefaultValueConfig defaultValueConfig; From 51a9598ead740dded6b217cd4874e84ad8de45d4 Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:55:25 +0100 Subject: [PATCH 27/65] SPDX clause missing Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java | 1 + .../com/powsybl/ampl/converter/version/AmplColumnsExporter.java | 1 + .../com/powsybl/ampl/converter/version/AmplExportVersion.java | 1 + .../powsybl/ampl/converter/version/AmplExportVersionImpl.java | 1 + .../com/powsybl/ampl/converter/version/BasicAmplExporter.java | 1 + 5 files changed, 5 insertions(+) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java index 49fc6c67a7c..28ce8715caa 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java @@ -3,6 +3,7 @@ * 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.ampl.converter; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 5dbb311a5cf..9411c623ae4 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -3,6 +3,7 @@ * 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.ampl.converter.version; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 7fd56fbded9..b05c19e53f7 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -3,6 +3,7 @@ * 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.ampl.converter.version; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java index 0e458ca79bd..969d125135c 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java @@ -3,6 +3,7 @@ * 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.ampl.converter.version; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index c78ac1ccd4a..4eeecb7aaec 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -3,6 +3,7 @@ * 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.ampl.converter.version; From 20bc489347b6786df6568aae73b2dfa2e204e111 Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:56:11 +0100 Subject: [PATCH 28/65] escape email to avoid javadoc warnings Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../ampl/converter/DefaultAmplNetworkUpdaterFactory.java | 2 +- .../powsybl/ampl/converter/version/AmplColumnsExporter.java | 2 +- .../com/powsybl/ampl/converter/version/AmplExportVersion.java | 2 +- .../powsybl/ampl/converter/version/AmplExportVersionImpl.java | 2 +- .../com/powsybl/ampl/converter/version/BasicAmplExporter.java | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java index 28ce8715caa..47c73cbf2fb 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/DefaultAmplNetworkUpdaterFactory.java @@ -11,7 +11,7 @@ import com.powsybl.iidm.network.Network; /** - * @author Nicolas Pierre + * @author Nicolas Pierre {@literal } */ public class DefaultAmplNetworkUpdaterFactory implements AmplNetworkUpdaterFactory { @Override diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 9411c623ae4..25c500cc5f2 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -16,7 +16,7 @@ import java.util.List; /** - * @author Nicolas Pierre + * @author Nicolas Pierre {@literal } */ public interface AmplColumnsExporter { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index b05c19e53f7..98e4631b224 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -13,7 +13,7 @@ import com.powsybl.iidm.network.Network; /** - * @author Nicolas Pierre + * @author Nicolas Pierre {@literal } */ public interface AmplExportVersion { interface Factory { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java index 969d125135c..0d5a7874b4d 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java @@ -8,7 +8,7 @@ package com.powsybl.ampl.converter.version; /** - * @author Nicolas Pierre + * @author Nicolas Pierre {@literal } */ public enum AmplExportVersionImpl implements AmplExportVersion { /** diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 4eeecb7aaec..f878e7b7caa 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -28,8 +28,8 @@ * Legacy exporter that must be retrocompatible, * and only fixes should be made on this class. * - * @author Nicolas Pierre for the refactor - * @author Geoffroy Jamgotchian for the original code + * @author Nicolas Pierre {@literal } for the refactor + * @author Geoffroy Jamgotchian {@literal } for the original code */ public class BasicAmplExporter implements AmplColumnsExporter { From 54cdc68d6aabab00b15a36629ef89eafc1fd3d2a Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:57:32 +0100 Subject: [PATCH 29/65] Add missing Override Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../powsybl/ampl/converter/version/AmplExportVersionImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java index 0d5a7874b4d..75ba1c23ca0 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java @@ -22,6 +22,7 @@ public enum AmplExportVersionImpl implements AmplExportVersion { this.factory = factory; } + @Override public AmplExportVersion.Factory getColumnsExporter() { return factory; } From 4db1b94ff9b3a0b538b80496bab394bc084e8421 Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:58:26 +0100 Subject: [PATCH 30/65] Improve switch case Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../java/com/powsybl/ampl/converter/AmplUtil.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java index 6270b0f1850..9f21dd194ec 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplUtil.java @@ -7,7 +7,6 @@ */ package com.powsybl.ampl.converter; -import com.powsybl.commons.PowsyblException; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.HvdcConverterStation.HvdcType; @@ -251,15 +250,11 @@ public static void resetNetworkMapping(StringToIntMapper mapper) { public static String getLegSuffix(ThreeWindingsTransformer.Side leg) { Objects.requireNonNull(leg); - switch (leg) { - case ONE: - return AmplConstants.LEG1_SUFFIX; - case TWO: - return AmplConstants.LEG2_SUFFIX; - case THREE: - return AmplConstants.LEG3_SUFFIX; - } - throw new PowsyblException("Unsupported three windings leg."); + return switch (leg) { + case ONE -> AmplConstants.LEG1_SUFFIX; + case TWO -> AmplConstants.LEG2_SUFFIX; + case THREE -> AmplConstants.LEG3_SUFFIX; + }; } } From fa3aa31259b86901bd0f5a6aad4ff6594caad9a1 Mon Sep 17 00:00:00 2001 From: pjeanmarie <72982611+pjeanmarie@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:07:37 +0100 Subject: [PATCH 31/65] To be consistent Co-authored-by: Olivier Perrin Signed-off-by: Pauline Jean-Marie --- .../com/powsybl/ampl/converter/version/AmplColumnsExporter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 25c500cc5f2..1afc0d75cc1 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -60,7 +60,7 @@ default void writeHeaderFile(Writer headerFileWriter) throws IOException { void writeRtcToFormatter(TableFormatter formatter) throws IOException; - void writePtcToFormatter(TableFormatter formatter); + void writePtcToFormatter(TableFormatter formatter) throws IOException; void writeTwoWindingsTransformerTapChangerTableToFormatter(TableFormatter formatter, TwoWindingsTransformer twt) throws IOException; From 9a084210285a6a5e5aebad84bf2a08dcf7146e7f Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Tue, 7 Nov 2023 17:38:48 +0100 Subject: [PATCH 32/65] Distinct columns headers from suffix Signed-off-by: Pauline Jean-Marie --- .../com/powsybl/ampl/converter/AmplConstants.java | 7 +++++-- .../ampl/converter/version/BasicAmplExporter.java | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java index 3b98401954c..6278907fc77 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java @@ -17,6 +17,8 @@ private AmplConstants() { } public static final String XNODE_COUNTRY_NAME = "XNODE"; + + // Column headers: public static final String FAULT = "fault"; public static final String DESCRIPTION = "description"; public static final String SUBSTATION = "substation"; @@ -39,8 +41,7 @@ private AmplConstants() { public static final String P0 = "p0 (MW)"; public static final String Q0 = "q0 (MW)"; public static final String ID = "id"; - public static final String RATIO_TABLE = "_ratio_table"; - public static final String PHASE_TABLE = "_phase_table"; + // End column headers public static final float INVALID_FLOAT_VALUE = -99999f; @@ -51,6 +52,8 @@ private AmplConstants() { */ public static final float SB = 100f; + public static final String RATIO_TABLE_SUFFIX = "_ratio_table"; + public static final String PHASE_TABLE_SUFFIX = "_phase_table"; public static final String LEG1_SUFFIX = "_leg1"; public static final String LEG2_SUFFIX = "_leg2"; public static final String LEG3_SUFFIX = "_leg3"; diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index f878e7b7caa..63f55a07810 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -373,13 +373,13 @@ public void writeTwoWindingsTransformerTapChangerTableToFormatter(TableFormatter RatioTapChanger rtc = twt.getRatioTapChanger(); if (rtc != null) { - String id = twt.getId() + RATIO_TABLE; + String id = twt.getId() + RATIO_TABLE_SUFFIX; writeRatioTapChanger(formatter, id, zb2, twt.getX(), rtc); } PhaseTapChanger ptc = twt.getPhaseTapChanger(); if (ptc != null) { - String id = twt.getId() + PHASE_TABLE; + String id = twt.getId() + PHASE_TABLE_SUFFIX; writePhaseTapChanger(formatter, id, zb2, twt.getX(), ptc); } } @@ -394,12 +394,12 @@ public void writeThreeWindingsTransformerTapChangerTableToFormatter(TableFormatt double vb = twt.getRatedU0(); double zb = vb * vb / AmplConstants.SB; if (rtc != null) { - String id = twt.getId() + "_leg" + legNumber + RATIO_TABLE; + String id = twt.getId() + "_leg" + legNumber + RATIO_TABLE_SUFFIX; writeRatioTapChanger(formatter, id, zb, leg.getX(), rtc); } PhaseTapChanger ptc = leg.getPhaseTapChanger(); if (ptc != null) { - String id = twt.getId() + "_leg" + legNumber + PHASE_TABLE; + String id = twt.getId() + "_leg" + legNumber + PHASE_TABLE_SUFFIX; writePhaseTapChanger(formatter, id, zb, leg.getX(), ptc); } } @@ -577,7 +577,7 @@ private void writeDanglingLineCurrentLimits(TableFormatter formatter) throws IOE private void writePhaseTapChanger(TableFormatter formatter, Identifiable twt, PhaseTapChanger ptc, String leg) { try { String ptcId = twt.getId() + leg; - String tcsId = twt.getId() + leg + PHASE_TABLE; + String tcsId = twt.getId() + leg + PHASE_TABLE_SUFFIX; int ptcNum = mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, ptcId); int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); formatter.writeCell(variantIndex) @@ -595,7 +595,7 @@ private void writePhaseTapChanger(TableFormatter formatter, Identifiable twt, private void writeRatioTapChanger(TableFormatter formatter, Identifiable twt, RatioTapChanger rtc, String leg) { try { String rtcId = twt.getId() + leg; - String tcsId = twt.getId() + leg + RATIO_TABLE; + String tcsId = twt.getId() + leg + RATIO_TABLE_SUFFIX; int rtcNum = mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, rtcId); int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId); formatter.writeCell(variantIndex) From 759507d925fa1e99bbcfac47d0767f8b91a38ac9 Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Tue, 7 Nov 2023 18:36:17 +0100 Subject: [PATCH 33/65] If we want to use valueOf which exist on enum, than there is not usage of interface AmplExportVersion without using also its enum implementation, so we merge the interface and its implementation. No new implementation are planned, only additional enum values. Signed-off-by: Pauline Jean-Marie --- .../ampl/converter/AmplExportConfig.java | 5 ++-- .../powsybl/ampl/converter/AmplExporter.java | 7 ++--- .../converter/version/AmplExportVersion.java | 19 ++++++++++-- .../version/AmplExportVersionImpl.java | 29 ------------------- 4 files changed, 21 insertions(+), 39 deletions(-) delete mode 100644 ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index a55982fb08f..f6d936e84b3 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -8,7 +8,6 @@ package com.powsybl.ampl.converter; import com.powsybl.ampl.converter.version.AmplExportVersion; -import com.powsybl.ampl.converter.version.AmplExportVersionImpl; import java.util.Objects; @@ -52,13 +51,13 @@ public String getLabel() { private boolean twtSplitShuntAdmittance; public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { - this(exportScope, exportXNodes, actionType, false, false, AmplExportVersionImpl.V1_LEGACY); + this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1_LEGACY); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { this(exportScope, exportXNodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, - AmplExportVersionImpl.V1_LEGACY); + AmplExportVersion.V1_LEGACY); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 67af4f7eb5b..25f1807041c 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -9,7 +9,6 @@ import com.google.auto.service.AutoService; import com.powsybl.ampl.converter.version.AmplExportVersion; -import com.powsybl.ampl.converter.version.AmplExportVersionImpl; import com.powsybl.commons.config.PlatformConfig; import com.powsybl.commons.datasource.DataSource; import com.powsybl.commons.parameters.Parameter; @@ -59,8 +58,8 @@ public class AmplExporter implements Exporter { private static final Parameter EXPORT_VERSION_PARAMETER = new Parameter(EXPORT_VERSION, ParameterType.STRING, "The version of the export.", - AmplExportVersionImpl.V1_LEGACY.name(), - Arrays.stream(AmplExportVersionImpl.values()).map(Enum::name).collect(Collectors.toList())); + AmplExportVersion.V1_LEGACY.name(), + Arrays.stream(AmplExportVersion.values()).map(Enum::name).collect(Collectors.toList())); private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, EXPORT_ACTION_TYPE_PARAMETER, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, @@ -97,7 +96,7 @@ public void export(Network network, Properties parameters, DataSource dataSource boolean exportRatioTapChangerVoltageTarget = Parameter.readBoolean(getFormat(), parameters, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, defaultValueConfig); boolean twtSplitShuntAdmittance = Parameter.readBoolean(getFormat(), parameters, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, defaultValueConfig); - AmplExportVersion exportVersion = AmplExportVersionImpl.valueOf( + AmplExportVersion exportVersion = AmplExportVersion.valueOf( Parameter.readString(getFormat(), parameters, EXPORT_VERSION_PARAMETER, defaultValueConfig)); AmplExportConfig config = new AmplExportConfig(scope, exportXnodes, actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 98e4631b224..fc69d8450bb 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -15,11 +15,24 @@ /** * @author Nicolas Pierre {@literal } */ -public interface AmplExportVersion { - interface Factory { +public enum AmplExportVersion { + /** + * Legacy export + */ + V1_LEGACY(BasicAmplExporter.getFactory()); + + public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, int variantIndex, int faultNum, int actionNum); } - Factory getColumnsExporter(); + private final Factory factory; + + AmplExportVersion(Factory factory) { + this.factory = factory; + } + + public Factory getColumnsExporter() { + return factory; + } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java deleted file mode 100644 index 75ba1c23ca0..00000000000 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersionImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 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.ampl.converter.version; - -/** - * @author Nicolas Pierre {@literal } - */ -public enum AmplExportVersionImpl implements AmplExportVersion { - /** - * Legacy export - */ - V1_LEGACY(BasicAmplExporter.getFactory()); - - private final AmplExportVersion.Factory factory; - - AmplExportVersionImpl(AmplExportVersion.Factory factory) { - this.factory = factory; - } - - @Override - public AmplExportVersion.Factory getColumnsExporter() { - return factory; - } -} From 2331afe2b977a28a8b196f590df5d1a9fb12239a Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Fri, 10 Nov 2023 10:30:09 +0100 Subject: [PATCH 34/65] Fix comment Signed-off-by: Pauline Jean-Marie --- .../src/main/java/com/powsybl/iidm/network/ThreeSides.java | 4 ++-- .../src/test/java/com/powsybl/iidm/xml/TopologyLevelTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/ThreeSides.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/ThreeSides.java index 14ce31cb56e..bdad9b5c500 100644 --- a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/ThreeSides.java +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/ThreeSides.java @@ -9,8 +9,8 @@ import com.powsybl.commons.PowsyblException; -/* - @author Bertrand Rix +/** + * @author Bertrand Rix {@literal } */ public enum ThreeSides { ONE(1, ThreeWindingsTransformer.Side.ONE, Branch.Side.ONE), diff --git a/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/TopologyLevelTest.java b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/TopologyLevelTest.java index 47d53c55118..6e858cc8860 100644 --- a/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/TopologyLevelTest.java +++ b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/TopologyLevelTest.java @@ -19,7 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** - * @author Teofil Calin Banc + * @author Teofil Calin Banc {@literal } */ class TopologyLevelTest extends AbstractXmlConverterTest { From 5454e505d8cf29b7c5c23ef32b65b895b1e88013 Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Fri, 10 Nov 2023 10:44:23 +0100 Subject: [PATCH 35/65] Light refacto Signed-off-by: Pauline Jean-Marie --- .../converter/version/BasicAmplExporter.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 63f55a07810..74bdd95ff92 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -999,7 +999,8 @@ public void writeTwoWindingsTranformerToFormatter(TableFormatter formatter, public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter, ThreeWindingsTransformer twt, int middleBusNum, int middleVlNum, ThreeWindingsTransformer.Side legSide) throws IOException { - Terminal terminal = twt.getLeg(legSide).getTerminal(); + ThreeWindingsTransformer.Leg twtLeg = twt.getLeg(legSide); + Terminal terminal = twtLeg.getTerminal(); Bus bus = AmplUtil.getBus(terminal); VoltageLevel vl = terminal.getVoltageLevel(); String id = twt.getId() + AmplUtil.getLegSuffix(legSide); @@ -1009,17 +1010,17 @@ public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter int legBusNum = getBusNum(bus); double vb = vl.getNominalV(); double ratedU0 = twt.getRatedU0(); - double ratedU = twt.getLeg(legSide).getRatedU(); + double ratedU = twtLeg.getRatedU(); double zb0 = ratedU0 * ratedU0 / AmplConstants.SB; - double r = twt.getLeg(legSide).getR() / zb0; - double x = twt.getLeg(legSide).getX() / zb0; - double g = twt.getLeg(legSide).getG() * zb0; - double b = twt.getLeg(legSide).getB() * zb0; + double r = twtLeg.getR() / zb0; + double x = twtLeg.getX() / zb0; + double g = twtLeg.getG() * zb0; + double b = twtLeg.getB() * zb0; double ratio = vb / ratedU; - RatioTapChanger rtc1 = twt.getLeg(legSide).getRatioTapChanger(); - PhaseTapChanger ptc1 = twt.getLeg(legSide).getPhaseTapChanger(); + RatioTapChanger rtc1 = twtLeg.getRatioTapChanger(); + PhaseTapChanger ptc1 = twtLeg.getPhaseTapChanger(); int rtc1Num = rtc1 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id) : -1; int ptc1Num = ptc1 != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, id) : -1; @@ -1043,7 +1044,7 @@ public void writeThreeWindingsTransformerLegToFormatter(TableFormatter formatter .writeCell(Double.NaN) .writeCell(terminal.getQ()) .writeCell(Double.NaN) - .writeCell(getPermanentLimit(twt.getLeg(legSide).getCurrentLimits().orElse(null))) + .writeCell(getPermanentLimit(twtLeg.getCurrentLimits().orElse(null))) .writeCell(Double.NaN) .writeCell(false) .writeCell(faultNum) From 8f60ac3af6498bb449ef751b73e88d0d69effa1b Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Fri, 10 Nov 2023 11:59:30 +0100 Subject: [PATCH 36/65] Actual version of Ampl is 1.0 so use that instead of V1_LEGACY NB. Which string is used? Enum.name is "V1_0" and seems to be used in AmplExporter but there is another string AmplColumnsExporter.EXPORTER_ID which is "1.0" Signed-off-by: Pauline Jean-Marie --- .../java/com/powsybl/ampl/converter/AmplExportConfig.java | 4 ++-- .../main/java/com/powsybl/ampl/converter/AmplExporter.java | 2 +- .../com/powsybl/ampl/converter/version/AmplExportVersion.java | 2 +- .../com/powsybl/ampl/converter/version/BasicAmplExporter.java | 2 +- ampl-converter/src/test/resources/inputs/headers.txt | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index f6d936e84b3..b2c51072154 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -51,13 +51,13 @@ public String getLabel() { private boolean twtSplitShuntAdmittance; public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { - this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1_LEGACY); + this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1_0); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { this(exportScope, exportXNodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, - AmplExportVersion.V1_LEGACY); + AmplExportVersion.V1_0); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 25f1807041c..2ca2e836657 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -58,7 +58,7 @@ public class AmplExporter implements Exporter { private static final Parameter EXPORT_VERSION_PARAMETER = new Parameter(EXPORT_VERSION, ParameterType.STRING, "The version of the export.", - AmplExportVersion.V1_LEGACY.name(), + AmplExportVersion.V1_0.name(), Arrays.stream(AmplExportVersion.values()).map(Enum::name).collect(Collectors.toList())); private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index fc69d8450bb..182050ee823 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -19,7 +19,7 @@ public enum AmplExportVersion { /** * Legacy export */ - V1_LEGACY(BasicAmplExporter.getFactory()); + V1_0(BasicAmplExporter.getFactory()); public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 74bdd95ff92..b3df04bbb14 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -33,7 +33,7 @@ */ public class BasicAmplExporter implements AmplColumnsExporter { - private static final String EXPORTER_ID = "V1_LEGACY"; + private static final String EXPORTER_ID = "1.0"; private final AmplExportConfig config; private final Network network; private final StringToIntMapper mapper; diff --git a/ampl-converter/src/test/resources/inputs/headers.txt b/ampl-converter/src/test/resources/inputs/headers.txt index 99c74f7383f..7c8de0324fd 100644 --- a/ampl-converter/src/test/resources/inputs/headers.txt +++ b/ampl-converter/src/test/resources/inputs/headers.txt @@ -1 +1 @@ -version V1_LEGACY +version 1.0 From 6d321055946f08960c0e802e3a2947c5ce6fd97f Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Tue, 14 Nov 2023 16:38:51 +0100 Subject: [PATCH 37/65] Use exporter id instead of AmplExportVersion enum name in properties for AmplExporter Signed-off-by: Pauline Jean-Marie --- .../ampl/converter/AmplExportConfig.java | 4 +- .../powsybl/ampl/converter/AmplExporter.java | 11 ++--- .../converter/version/AmplExportVersion.java | 42 ++++++++++++++++--- .../converter/version/BasicAmplExporter.java | 3 +- .../ampl/converter/AmplNetworkWriterTest.java | 27 +++++++++++- 5 files changed, 69 insertions(+), 18 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java index b2c51072154..191ee33c2c1 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExportConfig.java @@ -51,13 +51,13 @@ public String getLabel() { private boolean twtSplitShuntAdmittance; public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType) { - this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.V1_0); + this(exportScope, exportXNodes, actionType, false, false, AmplExportVersion.defaultVersion()); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, boolean exportRatioTapChangerVoltageTarget, boolean twtSplitShuntAdmittance) { this(exportScope, exportXNodes, actionType, exportRatioTapChangerVoltageTarget, twtSplitShuntAdmittance, - AmplExportVersion.V1_0); + AmplExportVersion.defaultVersion()); } public AmplExportConfig(ExportScope exportScope, boolean exportXNodes, ExportActionType actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java index 2ca2e836657..5a9d0178350 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplExporter.java @@ -19,10 +19,7 @@ import java.io.IOException; import java.io.UncheckedIOException; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.Properties; +import java.util.*; import java.util.stream.Collectors; /** @@ -58,8 +55,8 @@ public class AmplExporter implements Exporter { private static final Parameter EXPORT_VERSION_PARAMETER = new Parameter(EXPORT_VERSION, ParameterType.STRING, "The version of the export.", - AmplExportVersion.V1_0.name(), - Arrays.stream(AmplExportVersion.values()).map(Enum::name).collect(Collectors.toList())); + AmplExportVersion.defaultVersion().getExporterId(), + new ArrayList<>(AmplExportVersion.exporterIdValues())); private static final List STATIC_PARAMETERS = List.of(EXPORT_SCOPE_PARAMETER, EXPORT_XNODES_PARAMETER, EXPORT_ACTION_TYPE_PARAMETER, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, @@ -96,7 +93,7 @@ public void export(Network network, Properties parameters, DataSource dataSource boolean exportRatioTapChangerVoltageTarget = Parameter.readBoolean(getFormat(), parameters, EXPORT_RATIOTAPCHANGER_VT_PARAMETER, defaultValueConfig); boolean twtSplitShuntAdmittance = Parameter.readBoolean(getFormat(), parameters, TWT_SPLIT_SHUNT_ADMITTANCE_PARAMETER, defaultValueConfig); - AmplExportVersion exportVersion = AmplExportVersion.valueOf( + AmplExportVersion exportVersion = AmplExportVersion.fromExporterId( Parameter.readString(getFormat(), parameters, EXPORT_VERSION_PARAMETER, defaultValueConfig)); AmplExportConfig config = new AmplExportConfig(scope, exportXnodes, actionType, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 182050ee823..1b218d252f8 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -9,30 +9,60 @@ import com.powsybl.ampl.converter.AmplExportConfig; import com.powsybl.ampl.converter.AmplSubset; +import com.powsybl.commons.PowsyblException; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.Network; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + /** * @author Nicolas Pierre {@literal } */ public enum AmplExportVersion { - /** - * Legacy export - */ - V1_0(BasicAmplExporter.getFactory()); + + V1_0("1.0", BasicAmplExporter.getFactory()); public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, int variantIndex, int faultNum, int actionNum); } + private static final Map VERSION_BY_EXPORTER_ID = Arrays.stream(values()) + .collect(Collectors.toMap(AmplExportVersion::getExporterId, Function.identity())); + + private final String exporterId; private final Factory factory; - AmplExportVersion(Factory factory) { + AmplExportVersion(String exporterId, Factory factory) { + this.exporterId = exporterId; this.factory = factory; } + public String getExporterId() { + return this.exporterId; + } + public Factory getColumnsExporter() { - return factory; + return this.factory; + } + + public static Set exporterIdValues() { + return VERSION_BY_EXPORTER_ID.keySet(); + } + + public static AmplExportVersion fromExporterId(String exporterId) { + AmplExportVersion version = VERSION_BY_EXPORTER_ID.get(exporterId); + if (version == null) { + throw new PowsyblException("exporterId " + exporterId + " is not in the exporterId possible values: " + exporterIdValues()); + } + return version; + } + + public static AmplExportVersion defaultVersion() { + return V1_0; } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index b3df04bbb14..b2ddcdddb1e 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -33,7 +33,6 @@ */ public class BasicAmplExporter implements AmplColumnsExporter { - private static final String EXPORTER_ID = "1.0"; private final AmplExportConfig config; private final Network network; private final StringToIntMapper mapper; @@ -59,7 +58,7 @@ public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMa @Override public String getExporterId() { - return EXPORTER_ID; + return AmplExportVersion.V1_0.getExporterId(); } @Override diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index 40aa529aa09..284e27ae317 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -22,7 +22,7 @@ import java.util.Properties; import static com.powsybl.commons.test.ComparisonUtils.compareTxt; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * @author Geoffroy Jamgotchian {@literal } @@ -250,4 +250,29 @@ void writeHeaders() throws IOException { export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); } + + @Test + void writeHeadersWithVersion() throws IOException { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + + Properties properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "1.0"); + + export(network, properties, dataSource); + assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); + } + + @Test + void writeHeadersWithUnknownVersion() { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + + Properties properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "V1_0"); + + Exception e = assertThrows(IllegalArgumentException.class, () -> export(network, properties, dataSource)); + assertTrue(e.getMessage().contains("Value V1_0 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0]")); + + } } From 39c09d2db72cc61a86e51a6efc2a23324b399bc1 Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Thu, 16 Nov 2023 11:09:03 +0100 Subject: [PATCH 38/65] Remove duplicate version instantiation of columns exporter, now it is the instantiation in AmplExportConfig which is taken Signed-off-by: Pauline Jean-Marie --- .../powsybl/ampl/converter/AmplNetworkWriter.java | 2 +- .../converter/version/AmplColumnsExporter.java | 9 --------- .../ampl/converter/version/BasicAmplExporter.java | 5 ----- .../ampl/converter/AmplNetworkWriterTest.java | 14 +++++++++++++- .../ampl/converter/ExtendedAmplExporterTest.java | 9 +++++++++ .../resources/inputs/extended_exporter/headers.txt | 1 + 6 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/headers.txt diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java index c40aedd0de4..d30a788bfea 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkWriter.java @@ -804,7 +804,7 @@ private void writeHeaders() throws IOException { try (Writer writer = new OutputStreamWriter( dataSource.newOutputStream("_headers", "txt", append), StandardCharsets.UTF_8) ) { - columnsExporter.writeHeaderFile(writer); + writer.write("version " + config.getVersion().getExporterId() + "\n"); } } } diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java index 1afc0d75cc1..203f75b89a2 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplColumnsExporter.java @@ -12,7 +12,6 @@ import com.powsybl.iidm.network.*; import java.io.IOException; -import java.io.Writer; import java.util.List; /** @@ -20,14 +19,6 @@ */ public interface AmplColumnsExporter { - String VERSION_HEADER_NAME = "version"; - - String getExporterId(); - - default void writeHeaderFile(Writer headerFileWriter) throws IOException { - headerFileWriter.write(VERSION_HEADER_NAME + " " + getExporterId() + "\n"); - } - List getRtcColumns(); List getPtcColumns(); diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index 2c50a32d95d..6873dabfd40 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -52,11 +52,6 @@ public BasicAmplExporter(AmplExportConfig config, Network network, StringToIntMa this.actionNum = actionNum; } - @Override - public String getExporterId() { - return AmplExportVersion.V1_0.getExporterId(); - } - @Override public List getRtcColumns() { List columns = new ArrayList<>(8); diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index 2c216dfd447..063c172608f 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -252,7 +252,7 @@ void writeHeaders() throws IOException { } @Test - void writeHeadersWithVersion() throws IOException { + void writeHeadersWithVersion10() throws IOException { Network network = Network.create("dummy_network", "test"); MemDataSource dataSource = new MemDataSource(); @@ -275,4 +275,16 @@ void writeHeadersWithUnknownVersion() { assertTrue(e.getMessage().contains("Value V1_0 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0")); } + + @Test + void writeHeadersWithVersion11() throws IOException { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + + Properties properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "1.1"); + + export(network, properties, dataSource); + assertEqualsToRef(dataSource, "_headers", "inputs//extended_exporter/headers.txt"); + } } diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java index fe0b40cfb3f..ab138ed0a0d 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -93,4 +93,13 @@ void testRegulatingBusIdExportSvc() throws IOException { assertEqualsToRef(dataSource, "_network_static_var_compensators", "svc-test-case-regulating-bus.txt"); } + + @Test + void testVersion() throws IOException { + Network network = Network.create("dummy_network", "test"); + MemDataSource dataSource = new MemDataSource(); + new AmplNetworkWriter(network, dataSource, v2Config).write(); + + assertEqualsToRef(dataSource, "_headers", "headers.txt"); + } } diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/headers.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/headers.txt new file mode 100644 index 00000000000..3987f9c4e85 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/headers.txt @@ -0,0 +1 @@ +version 1.1 From 52591172ab7a0c9780434f06693c7790043dcd71 Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Thu, 16 Nov 2023 11:36:56 +0100 Subject: [PATCH 39/65] Update comments for javadoc Signed-off-by: Pauline Jean-Marie --- .../main/java/com/powsybl/ampl/converter/util/NetworkUtil.java | 3 ++- .../powsybl/ampl/converter/version/ExtendedAmplExporter.java | 3 ++- .../com/powsybl/ampl/converter/ExtendedAmplExporterTest.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java index 586f7777081..d1081fbed80 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/util/NetworkUtil.java @@ -3,6 +3,7 @@ * 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.ampl.converter.util; @@ -12,7 +13,7 @@ import com.powsybl.iidm.network.extensions.SlackTerminal; /** - * @author Nicolas PIERRE + * @author Nicolas PIERRE {@literal } */ public final class NetworkUtil { diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java index a091f3a3306..17f29a95920 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java @@ -3,6 +3,7 @@ * 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.ampl.converter.version; @@ -23,7 +24,7 @@ import static com.powsybl.ampl.converter.AmplConstants.*; /** - * @author Nicolas PIERRE + * @author Nicolas PIERRE {@literal } */ public class ExtendedAmplExporter extends BasicAmplExporter { diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java index ab138ed0a0d..6a852fe50eb 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -3,6 +3,7 @@ * 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.ampl.converter; @@ -23,7 +24,7 @@ import static com.powsybl.commons.test.ComparisonUtils.compareTxt; /** - * @author Nicolas PIERRE + * @author Nicolas PIERRE {@literal } */ class ExtendedAmplExporterTest extends AbstractConverterTest { From 8a64e54ddf4e60e15cfee90d3a31d5a116607165 Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Thu, 16 Nov 2023 16:38:59 +0100 Subject: [PATCH 40/65] Give a more specific name to the first columns extension Signed-off-by: Pauline Jean-Marie --- .../ampl/converter/version/AmplExportVersion.java | 2 +- ...AmplExporter.java => ExtendedAmplExporterV1.java} | 12 +++++++----- ...rterTest.java => ExtendedAmplExporterV1Test.java} | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) rename ampl-converter/src/main/java/com/powsybl/ampl/converter/version/{ExtendedAmplExporter.java => ExtendedAmplExporterV1.java} (96%) rename ampl-converter/src/test/java/com/powsybl/ampl/converter/{ExtendedAmplExporterTest.java => ExtendedAmplExporterV1Test.java} (98%) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 645077225cb..a208f5221e9 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -25,7 +25,7 @@ public enum AmplExportVersion { V1_0("1.0", BasicAmplExporter::new), - V1_1("1.1", ExtendedAmplExporter::new); + V1_1("1.1", ExtendedAmplExporterV1::new); public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java similarity index 96% rename from ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java rename to ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 17f29a95920..e65dc3cb6b3 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -24,14 +24,16 @@ import static com.powsybl.ampl.converter.AmplConstants.*; /** + * 1st (V1) extension of BasicAmplExporter, associated with AMPL version 1.1 (exporter id) + * * @author Nicolas PIERRE {@literal } */ -public class ExtendedAmplExporter extends BasicAmplExporter { +public class ExtendedAmplExporterV1 extends BasicAmplExporter { - public ExtendedAmplExporter(AmplExportConfig config, - Network network, - StringToIntMapper mapper, - int variantIndex, int faultNum, int actionNum) { + public ExtendedAmplExporterV1(AmplExportConfig config, + Network network, + StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum) { super(config, network, mapper, variantIndex, faultNum, actionNum); } diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java similarity index 98% rename from ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java rename to ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 6a852fe50eb..662969293c0 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -26,7 +26,7 @@ /** * @author Nicolas PIERRE {@literal } */ -class ExtendedAmplExporterTest extends AbstractConverterTest { +class ExtendedAmplExporterV1Test extends AbstractConverterTest { private AmplExportConfig v2Config; From cfeaa232369643d1dc47cc723d4f88ee3476418b Mon Sep 17 00:00:00 2001 From: Pauline Jean-Marie Date: Tue, 21 Nov 2023 15:55:30 +0100 Subject: [PATCH 41/65] Add ampl exporter version in ampl executor via AmplModelExecutionHandler.parameters Signed-off-by: Pauline Jean-Marie --- .../executor/AmplModelExecutionHandler.java | 9 +++++++- .../powsybl/ampl/executor/AmplParameters.java | 4 ++++ .../AmplModelExecutionHandlerTest.java | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplModelExecutionHandler.java b/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplModelExecutionHandler.java index 8be1058e257..1387438727f 100644 --- a/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplModelExecutionHandler.java +++ b/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplModelExecutionHandler.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; /** * This executionHandler will run an ampl model on a network. @@ -96,7 +97,13 @@ private void exportAmplParameters(Path workingDir) throws IOException { private void exportNetworkAsAmpl(Path workingDir) { DataSource networkExportDataSource = new FileDataSource(workingDir, this.model.getNetworkDataPrefix()); - new AmplExporter().export(network, null, networkExportDataSource); + Properties prop = null; + String version = parameters.getAmplExportVersionId(); + if (version != null) { + prop = new Properties(); + prop.put(AmplExporter.EXPORT_VERSION, version); + } + new AmplExporter().export(network, prop, networkExportDataSource); } /** diff --git a/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplParameters.java b/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplParameters.java index d056b4b3bcb..63e2d367744 100644 --- a/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplParameters.java +++ b/ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplParameters.java @@ -32,4 +32,8 @@ public interface AmplParameters { * @return true if debug mode is on, else otherwise */ boolean isDebug(); + + default String getAmplExportVersionId() { + return null; + } } diff --git a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java index cb8f486dc35..bf70e64d6a3 100644 --- a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java +++ b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java @@ -293,4 +293,26 @@ public boolean checkModelConvergence(Map metrics) { } } + @Test + void testAmplParametersVersion() throws Exception { + try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { + Files.createDirectory(fs.getPath("/workingDir")); + // Test data + Network network = EurostagTutorialExample1Factory.create(); + DummyAmplModel model = new DummyAmplModel(); + AmplConfig cfg = getAmplConfig(); + // Test config + String variantId = network.getVariantManager().getWorkingVariantId(); + AmplParameters parameters = new EmptyAmplParameters() { + @Override + public String getAmplExportVersionId() { + return "1.2"; + } + }; + AmplModelExecutionHandler handler = new AmplModelExecutionHandler(model, network, variantId, cfg, parameters); + Exception e = assertThrows(IllegalArgumentException.class, () -> handler.before(fs.getPath("/workingDir"))); + assertTrue(e.getMessage().contains("Value 1.2 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0")); + } + } + } From d6055070d4e4cae30646d68999870a5bc96e37d1 Mon Sep 17 00:00:00 2001 From: Olivier Perrin Date: Fri, 24 Nov 2023 09:35:38 +0100 Subject: [PATCH 42/65] Revert "fix MVar to MVAr" This reverts commit 96d8e905b740d4f1520924f557a9ff23713d4c77. Signed-off-by: Olivier Perrin --- .../eurostag-tutorial-example1-generators-regulating-bus.txt | 2 +- .../inputs/extended_exporter/svc-test-case-regulating-bus.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt index 94c51673c42..0ee5057c9df 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt @@ -1,4 +1,4 @@ #Generators (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 false -1 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt index 762a77c7553..0f867134bb0 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt @@ -1,4 +1,4 @@ #Static VAR compensators (svcTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 2 2 2 0.288800 1.15520 false -1 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 1 2 2 2 2 0.288800 1.15520 true 2 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 From 26fb1caae827f57bf2808e26a24d996faddc9e8f Mon Sep 17 00:00:00 2001 From: Olivier Perrin Date: Fri, 24 Nov 2023 09:35:50 +0100 Subject: [PATCH 43/65] Revert "fix MVar to MVAr" This reverts commit 4e1c69430371286e2887f241b1f199c27e59abe3. Signed-off-by: Olivier Perrin --- .../src/test/resources/inputs/battery-test-batteries.txt | 2 +- .../resources/inputs/eurostag-tutorial-example1-generators.txt | 2 +- .../test/resources/inputs/eurostag-tutorial-example1-shunts.txt | 2 +- ampl-converter/src/test/resources/inputs/lcc-test-case.txt | 2 +- .../src/test/resources/inputs/shunt-test-case-shunts.txt | 2 +- ampl-converter/src/test/resources/inputs/svc-test-case.txt | 2 +- ampl-converter/src/test/resources/inputs/vsc-test-case.txt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt b/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt index baa15672ba4..acf6218c0f1 100644 --- a/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt +++ b/ampl-converter/src/test/resources/inputs/battery-test-batteries.txt @@ -1,4 +1,4 @@ #Batteries (fictitious/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "p0 (MW)" "q0 (MW)" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "p0 (MW)" "q0 (MW)" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 2 2 2 9999.99 9999.99 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 0 0 "BAT" "BAT" -605.000 -225.000 1 2 2 2 2 100.000 200.000 -200.000 200.000 -54.5500 -59.3000 -59.3000 46.2500 60.0000 60.0000 0 0 "BAT2" "BAT2" -605.000 -225.000 diff --git a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt index 06cca302550..05c0fa04868 100644 --- a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt +++ b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-generators.txt @@ -1,4 +1,4 @@ #Generators (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "targetV (pu)" "targetP (MW)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 true 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt index 008cd2ab353..e0f5959dee6 100644 --- a/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt +++ b/ampl-converter/src/test/resources/inputs/eurostag-tutorial-example1-shunts.txt @@ -1,3 +1,3 @@ #Shunts (sim1/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" "sections count" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" "sections count" 1 1 -1 -1 5 0.00000 5.76000e-05 0 5.76000e-05 0 0 "SHUNT" "SHUNT" -99999.0 -99999.0 1 diff --git a/ampl-converter/src/test/resources/inputs/lcc-test-case.txt b/ampl-converter/src/test/resources/inputs/lcc-test-case.txt index 8ab3efa609a..fa6d31515f8 100644 --- a/ampl-converter/src/test/resources/inputs/lcc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/lcc-test-case.txt @@ -1,4 +1,4 @@ #LCC Converter Stations (hvdctest/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "lossFactor (%PDC)" "powerFactor" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "lossFactor (%PDC)" "powerFactor" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 1 1 1 1.10000 0.500000 0 0 "C1" "Converter1" 100.000 50.0000 1 2 2 2 2 1.10000 0.600000 0 0 "C2" "Converter2" 75.0000 25.0000 diff --git a/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt b/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt index 9fa7f904dc1..89287149ef4 100644 --- a/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt +++ b/ampl-converter/src/test/resources/inputs/shunt-test-case-shunts.txt @@ -1,3 +1,3 @@ #Shunts (shuntTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" "sections count" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "inter. points" "b (pu)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" "sections count" 1 1 1 1 1 0.0144400 28.8800 0 28.8800 0 0 "SHUNT" "SHUNT" -99999.0 -99999.0 1 diff --git a/ampl-converter/src/test/resources/inputs/svc-test-case.txt b/ampl-converter/src/test/resources/inputs/svc-test-case.txt index 2617f1848e6..e18c7e48319 100644 --- a/ampl-converter/src/test/resources/inputs/svc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/svc-test-case.txt @@ -1,4 +1,4 @@ #Static VAR compensators (svcTestCase/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "targetV (pu)" "targetQ (MVAr)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 2 2 2 0.288800 1.15520 true 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 1 2 2 2 2 0.288800 1.15520 true 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/vsc-test-case.txt b/ampl-converter/src/test/resources/inputs/vsc-test-case.txt index 7b4de2bbf3f..5852cf7396d 100644 --- a/ampl-converter/src/test/resources/inputs/vsc-test-case.txt +++ b/ampl-converter/src/test/resources/inputs/vsc-test-case.txt @@ -1,4 +1,4 @@ #VSC Converter Stations (hvdctest/InitialState) -#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVAr)" "minQ0 (MVAr)" "minQminP (MVAr)" "maxQmaxP (MVAr)" "maxQ0 (MVAr)" "maxQminP (MVAr)" "v regul." "targetV (pu)" "targetQ (MVAr)" "lossFactor (%PDC)" "fault" "curative" "id" "description" "P (MW)" "Q (MVAr)" +#"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "targetV (pu)" "targetQ (MVar)" "lossFactor (%PDC)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 1 1 1 -300.000 300.000 0.00000 0.00000 0.00000 10.0000 10.0000 10.0000 true 1.01250 -99999.0 1.10000 0 0 "C1" "Converter1" 100.000 50.0000 1 2 2 2 2 -300.000 300.000 0.00000 0.00000 0.00000 10.0000 10.0000 10.0000 false -99999.0 123.000 1.10000 0 0 "C2" "Converter2" -99999.0 -99999.0 From 2dcdbf373746226515086290c537e8a5716c58ec Mon Sep 17 00:00:00 2001 From: Olivier Perrin Date: Fri, 24 Nov 2023 09:36:04 +0100 Subject: [PATCH 44/65] Revert "fix constants typo" This reverts commit 39060bfaa0250e9b64d83756acbb2035f572bf67. Signed-off-by: Olivier Perrin --- .../powsybl/ampl/converter/AmplConstants.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java index c70f1d6b3d4..9440707b373 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplConstants.java @@ -23,20 +23,20 @@ private AmplConstants() { public static final String DESCRIPTION = "description"; public static final String SUBSTATION = "substation"; public static final String TARGET_V = "targetV (pu)"; - public static final String TARGET_Q = "targetQ (MVAr)"; + public static final String TARGET_Q = "targetQ (MVar)"; public static final String CON_BUS = "con. bus"; public static final String MINP = "minP (MW)"; public static final String MAXP = "maxP (MW)"; public static final String V_REGUL = "v regul."; public static final String V_REGUL_BUS = "v regul. bus"; public static final String ACTIVE_POWER = "P (MW)"; - public static final String REACTIVE_POWER = "Q (MVAr)"; - public static final String MIN_Q_MAX_P = "minQmaxP (MVAr)"; - public static final String MIN_Q0 = "minQ0 (MVAr)"; - public static final String MIN_Q_MIN_P = "minQminP (MVAr)"; - public static final String MAX_Q_MAX_P = "maxQmaxP (MVAr)"; - public static final String MAX_Q0 = "maxQ0 (MVAr)"; - public static final String MAX_Q_MIN_P = "maxQminP (MVAr)"; + public static final String REACTIVE_POWER = "Q (MVar)"; + public static final String MIN_Q_MAX_P = "minQmaxP (MVar)"; + public static final String MIN_Q0 = "minQ0 (MVar)"; + public static final String MIN_Q_MIN_P = "minQminP (MVar)"; + public static final String MAX_Q_MAX_P = "maxQmaxP (MVar)"; + public static final String MAX_Q0 = "maxQ0 (MVar)"; + public static final String MAX_Q_MIN_P = "maxQminP (MVar)"; public static final String NUM = "num"; public static final String BUS = "bus"; public static final String P0 = "p0 (MW)"; From ab37a56a14feaee6fbc7d7359f4417f523a99ad8 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 10:34:13 +0200 Subject: [PATCH 45/65] Refactor TUs Signed-off-by: p-arvy --- .../com/powsybl/ampl/converter/AmplNetworkWriterTest.java | 2 +- .../powsybl/ampl/converter/ExtendedAmplExporterV1Test.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index 57322536b6f..6cb015e04a1 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -285,7 +285,7 @@ void writeHeadersWithVersion11() throws IOException { properties.put("iidm.export.ampl.export-version", "1.1"); export(network, properties, dataSource); - assertEqualsToRef(dataSource, "_headers", "inputs//extended_exporter/headers.txt"); + assertEqualsToRef(dataSource, "_headers", "inputs/extended_exporter/headers.txt"); } @Test diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 0d5c15c3160..58ddfb593af 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -21,7 +21,7 @@ import java.io.IOException; import java.io.InputStream; -import static com.powsybl.commons.test.ComparisonUtils.compareTxt; +import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals; /** * @author Nicolas PIERRE {@literal } @@ -32,7 +32,7 @@ class ExtendedAmplExporterV1Test extends AbstractSerDeTest { private void assertEqualsToRef(MemDataSource dataSource, String suffix, String refFileName) throws IOException { try (InputStream actual = new ByteArrayInputStream(dataSource.getData(suffix, "txt"))) { - compareTxt(getClass().getResourceAsStream("/inputs/extended_exporter/" + refFileName), actual); + assertTxtEquals(getClass().getResourceAsStream("/" + refFileName), actual); } } From 23141d9c189696264772c0389d0ff88326845244 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 11:07:28 +0200 Subject: [PATCH 46/65] Small refactor Signed-off-by: p-arvy --- .../version/ExtendedAmplExporterV1.java | 16 ++----- .../converter/ExtendedAmplExporterV1Test.java | 11 ++--- .../AmplModelExecutionHandlerTest.java | 42 +++++++++---------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index e65dc3cb6b3..2b1cff2e550 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -37,22 +37,12 @@ public ExtendedAmplExporterV1(AmplExportConfig config, super(config, network, mapper, variantIndex, faultNum, actionNum); } - private static class ImpedanceAndAdmittance { - double r; - double x; - double g; - double b; - - public ImpedanceAndAdmittance(double r, double x, double g, double b) { - this.r = r; - this.x = x; - this.g = g; - this.b = b; - } - } + private record ImpedanceAndAdmittance(double r, double x, double g, double b) { } @Override public List getBusesColumns() { + super.getBusesColumns(); + return List.of(new Column(VARIANT), new Column(NUM), new Column(SUBSTATION), diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 58ddfb593af..9260f7b749c 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -56,7 +56,7 @@ void testSlackBusExport() throws IOException { new AmplNetworkWriter(network, dataSource, v2Config).write(); assertEqualsToRef(dataSource, "_network_buses", - "eurostag-tutorial-example1-buses.txt"); + "inputs/extended_exporter/eurostag-tutorial-example1-buses.txt"); } @Test @@ -69,7 +69,7 @@ void testNewTapExport() throws IOException { MemDataSource dataSource = new MemDataSource(); new AmplNetworkWriter(network, dataSource, v2Config).write(); assertEqualsToRef(dataSource, "_network_tct", - "eurostag-tutorial-example1-tct.txt"); + "inputs/extended_exporter/eurostag-tutorial-example1-tct.txt"); } @@ -82,7 +82,7 @@ void testRegulatingBusIdExportGenerators() throws IOException { new AmplNetworkWriter(network, dataSource, v2Config).write(); assertEqualsToRef(dataSource, "_network_generators", - "eurostag-tutorial-example1-generators-regulating-bus.txt"); + "inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt"); } @Test @@ -92,7 +92,8 @@ void testRegulatingBusIdExportSvc() throws IOException { MemDataSource dataSource = new MemDataSource(); new AmplNetworkWriter(network, dataSource, v2Config).write(); - assertEqualsToRef(dataSource, "_network_static_var_compensators", "svc-test-case-regulating-bus.txt"); + assertEqualsToRef(dataSource, "_network_static_var_compensators", + "inputs/extended_exporter/svc-test-case-regulating-bus.txt"); } @Test @@ -101,6 +102,6 @@ void testVersion() throws IOException { MemDataSource dataSource = new MemDataSource(); new AmplNetworkWriter(network, dataSource, v2Config).write(); - assertEqualsToRef(dataSource, "_headers", "headers.txt"); + assertEqualsToRef(dataSource, "_headers", "inputs/extended_exporter/headers.txt"); } } diff --git a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java index 797e8f24569..a3b9e645d65 100644 --- a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java +++ b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java @@ -294,26 +294,26 @@ public boolean checkModelConvergence(Map metrics) { } } - @Test - void testAmplParametersVersion() throws Exception { - try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { - Files.createDirectory(fs.getPath("/workingDir")); - // Test data - Network network = EurostagTutorialExample1Factory.create(); - DummyAmplModel model = new DummyAmplModel(); - AmplConfig cfg = getAmplConfig(); - // Test config - String variantId = network.getVariantManager().getWorkingVariantId(); - AmplParameters parameters = new EmptyAmplParameters() { - @Override - public String getAmplExportVersionId() { - return "1.2"; - } - }; - AmplModelExecutionHandler handler = new AmplModelExecutionHandler(model, network, variantId, cfg, parameters); - Exception e = assertThrows(IllegalArgumentException.class, () -> handler.before(fs.getPath("/workingDir"))); - assertTrue(e.getMessage().contains("Value 1.2 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0")); - } - } +// @Test +// void testAmplParametersVersion() throws Exception { +// try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { +// Files.createDirectory(fs.getPath("/workingDir")); +// // Test data +// Network network = EurostagTutorialExample1Factory.create(); +// DummyAmplModel model = new DummyAmplModel(); +// AmplConfig cfg = getAmplConfig(); +// // Test config +// String variantId = network.getVariantManager().getWorkingVariantId(); +// AmplParameters parameters = new EmptyAmplParameters() { +// @Override +// public String getAmplExportVersionId() { +// return "1.2"; +// } +// }; +// AmplModelExecutionHandler handler = new AmplModelExecutionHandler(model, network, variantId, cfg, parameters); +// Exception e = assertThrows(IllegalArgumentException.class, () -> handler.before(fs.getPath("/workingDir"))); +// assertTrue(e.getMessage().contains("Value 1.2 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0")); +// } +// } } From 626d1a2a115c191e6731ef7388b781238cc06709 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 11:28:23 +0200 Subject: [PATCH 47/65] Refactor table columns in extended ampl exporter Signed-off-by: p-arvy --- .../version/ExtendedAmplExporterV1.java | 96 +++++-------------- 1 file changed, 26 insertions(+), 70 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 2b1cff2e550..005cb45abc8 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -19,6 +19,7 @@ import com.powsybl.iidm.network.util.ConnectedComponents; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import static com.powsybl.ampl.converter.AmplConstants.*; @@ -30,6 +31,13 @@ */ public class ExtendedAmplExporterV1 extends BasicAmplExporter { + private static final int SLACK_BUS_COLUMN_INDEX = 8; + private static final int TAP_CHANGER_R_COLUMN_INDEX = 4; + private static final int TAP_CHANGER_G_COLUMN_INDEX = 6; + private static final int TAP_CHANGER_B_COLUMN_INDEX = 7; + private static final int GENERATOR_V_REGUL_BUS_COLUMN_INDEX = 14; + private static final int STATIC_VAR_COMPENSATOR_V_REGUL_BUS_COLUMN_INDEX = 8; + public ExtendedAmplExporterV1(AmplExportConfig config, Network network, StringToIntMapper mapper, @@ -41,88 +49,36 @@ private record ImpedanceAndAdmittance(double r, double x, double g, double b) { @Override public List getBusesColumns() { - super.getBusesColumns(); - - return List.of(new Column(VARIANT), - new Column(NUM), - new Column(SUBSTATION), - new Column("cc"), - new Column("v (pu)"), - new Column("theta (rad)"), - new Column("p (MW)"), - new Column("q (MVar)"), - new Column("slack bus"), - new Column(FAULT), - new Column(getConfig().getActionType().getLabel()), - new Column(ID)); + List busesColumns = new ArrayList<>(super.getBusesColumns()); + // add slack bus column + busesColumns.add(SLACK_BUS_COLUMN_INDEX, new Column("slack bus")); + return busesColumns; } @Override public List getTapChangerTableColumns() { - return List.of(new Column(VARIANT), - new Column(NUM), - new Column("tap"), - new Column("var ratio"), - new Column("r (pu)"), - new Column("x (pu)"), - new Column("g (pu)"), - new Column("b (pu)"), - new Column("angle (rad)"), - new Column(FAULT), - new Column(getConfig().getActionType().getLabel())); + List tapChangerTableColumns = new ArrayList<>(super.getTapChangerTableColumns()); + // add r, g and b columns + tapChangerTableColumns.add(TAP_CHANGER_R_COLUMN_INDEX, new Column("r (pu)")); + tapChangerTableColumns.add(TAP_CHANGER_G_COLUMN_INDEX, new Column("g (pu)")); + tapChangerTableColumns.add(TAP_CHANGER_B_COLUMN_INDEX, new Column("b (pu)")); + return tapChangerTableColumns; } @Override public List getGeneratorsColumns() { - return List.of( - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column(MINP), - new Column(MAXP), - new Column(MIN_Q_MAX_P), - new Column(MIN_Q0), - new Column(MIN_Q_MIN_P), - new Column(MAX_Q_MAX_P), - new Column(MAX_Q0), - new Column(MAX_Q_MIN_P), - new Column(V_REGUL), - new Column(V_REGUL_BUS), - new Column(TARGET_V), - new Column("targetP (MW)"), - new Column(TARGET_Q), - new Column(FAULT), - new Column(getConfig().getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER) - ); + List generatorsColumns = new ArrayList<>(super.getGeneratorsColumns()); + // add column for voltage regulated bus + generatorsColumns.add(GENERATOR_V_REGUL_BUS_COLUMN_INDEX, new Column(V_REGUL_BUS)); + return generatorsColumns; } @Override public List getStaticVarCompensatorColumns() { - return List.of( - new Column(VARIANT), - new Column(NUM), - new Column(BUS), - new Column(CON_BUS), - new Column(SUBSTATION), - new Column("minB (pu)"), - new Column("maxB (pu)"), - new Column(V_REGUL), - new Column(V_REGUL_BUS), - new Column(TARGET_V), - new Column(TARGET_Q), - new Column(FAULT), - new Column(getConfig().getActionType().getLabel()), - new Column(ID), - new Column(DESCRIPTION), - new Column(ACTIVE_POWER), - new Column(REACTIVE_POWER) - ); + List svcColumns = new ArrayList<>(super.getStaticVarCompensatorColumns()); + // add column for voltage regulated bus + svcColumns.add(STATIC_VAR_COMPENSATOR_V_REGUL_BUS_COLUMN_INDEX, new Column(V_REGUL_BUS)); + return svcColumns; } @Override From 906cd02a956ec0d4548f1bea5f5a7300e412a62f Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 11:44:42 +0200 Subject: [PATCH 48/65] Add slack bus export for 3wt, dangling lines and tie lines buses in extended ampl exporter Signed-off-by: p-arvy --- .../version/ExtendedAmplExporterV1.java | 99 ++++++++++++++++--- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 005cb45abc8..e04d40efebb 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -17,6 +17,7 @@ import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.util.ConnectedComponents; +import com.powsybl.iidm.network.util.SV; import java.io.IOException; import java.util.ArrayList; @@ -92,17 +93,93 @@ public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws double v = b.getV() / nomV; double theta = Math.toRadians(b.getAngle()); formatter.writeCell(getVariantIndex()) - .writeCell(num) - .writeCell(vlNum) - .writeCell(ccNum) - .writeCell(v) - .writeCell(theta) - .writeCell(b.getP()) - .writeCell(b.getQ()) - .writeCell(NetworkUtil.isSlackBus(b)) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(id); + .writeCell(num) + .writeCell(vlNum) + .writeCell(ccNum) + .writeCell(v) + .writeCell(theta) + .writeCell(b.getP()) + .writeCell(b.getQ()) + .writeCell(NetworkUtil.isSlackBus(b)) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(id); + } + + @Override + public void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableFormatter formatter, + ThreeWindingsTransformer twt, + int middleCcNum) throws IOException { + String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); + String middleVlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); + int middleBusNum = getMapper().getInt(AmplSubset.BUS, middleBusId); + int middleVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + + double v = twt.getProperty("v") == null ? Double.NaN : + Double.parseDouble(twt.getProperty("v")) / twt.getRatedU0(); + double angle = twt.getProperty("angle") == null ? Double.NaN : + Math.toRadians(Double.parseDouble(twt.getProperty("angle"))); + + formatter.writeCell(getVariantIndex()) + .writeCell(middleBusNum) + .writeCell(middleVlNum) + .writeCell(middleCcNum) + .writeCell(v) + .writeCell(angle) + .writeCell(0.0) + .writeCell(0.0) + .writeCell(false) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(middleBusId); + } + + @Override + public void writeDanglingLineMiddleBusesToFormatter(TableFormatter formatter, DanglingLine dl, + int middleCcNum) throws IOException { + Terminal t = dl.getTerminal(); + Bus b = AmplUtil.getBus(dl.getTerminal()); + String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); + String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); + int middleBusNum = getMapper().getInt(AmplSubset.BUS, middleBusId); + int middleVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); + SV sv = new SV(t.getP(), t.getQ(), b != null ? b.getV() : Double.NaN, b != null ? b.getAngle() : Double.NaN, + TwoSides.ONE).otherSide( + dl, true); + double nomV = t.getVoltageLevel().getNominalV(); + double v = sv.getU() / nomV; + double theta = Math.toRadians(sv.getA()); + formatter.writeCell(getVariantIndex()) + .writeCell(middleBusNum) + .writeCell(middleVlNum) + .writeCell(middleCcNum) + .writeCell(v) + .writeCell(theta) + .writeCell(0.0) // 0 MW injected at dangling line internal bus + .writeCell(0.0) // 0 MVar injected at dangling line internal bus + .writeCell(false) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(middleBusId); + } + + @Override + public void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine tieLine, + int xNodeCcNum) throws IOException { + String xNodeBusId = AmplUtil.getXnodeBusId(tieLine); + int xNodeBusNum = getMapper().getInt(AmplSubset.BUS, xNodeBusId); + int xNodeVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)); + formatter.writeCell(getVariantIndex()) + .writeCell(xNodeBusNum) + .writeCell(xNodeVlNum) + .writeCell(xNodeCcNum) + .writeCell(Float.NaN) + .writeCell(Double.NaN) + .writeCell(0.0) + .writeCell(0.0) + .writeCell(getFaultNum()) + .writeCell(getActionNum()) + .writeCell(xNodeBusId); } @Override From a2fe2c3b96609cb0dba2d38dac2588ffd5fe73d3 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 12:14:46 +0200 Subject: [PATCH 49/65] Add TUs for slack bus values in new extended ampl export Signed-off-by: p-arvy --- .../version/ExtendedAmplExporterV1.java | 1 + .../converter/ExtendedAmplExporterV1Test.java | 90 +++++++++++++++---- .../extended_exporter/dangling-line-buses.txt | 4 + .../eurostag-tutorial-example1-buses-tl.txt | 8 ++ .../three-windings-transformers-buses.txt | 6 ++ .../three-windings-transformers-tct.txt | 9 ++ 6 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/dangling-line-buses.txt create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-buses.txt create mode 100644 ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index e04d40efebb..0d7b361aad9 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -177,6 +177,7 @@ public void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine .writeCell(Double.NaN) .writeCell(0.0) .writeCell(0.0) + .writeCell(false) .writeCell(getFaultNum()) .writeCell(getActionNum()) .writeCell(xNodeBusId); diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 9260f7b749c..2f732f0d97d 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -12,14 +12,17 @@ import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.extensions.SlackTerminalAdder; +import com.powsybl.iidm.network.test.DanglingLineNetworkFactory; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.iidm.network.test.SvcTestCaseFactory; +import com.powsybl.iidm.network.test.ThreeWindingsTransformerNetworkFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Properties; import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals; @@ -28,7 +31,9 @@ */ class ExtendedAmplExporterV1Test extends AbstractSerDeTest { - private AmplExportConfig v2Config; + Properties properties; + MemDataSource dataSource; + AmplExporter exporter; private void assertEqualsToRef(MemDataSource dataSource, String suffix, String refFileName) throws IOException { try (InputStream actual = new ByteArrayInputStream(dataSource.getData(suffix, "txt"))) { @@ -39,9 +44,10 @@ private void assertEqualsToRef(MemDataSource dataSource, String suffix, String r @BeforeEach public void setUp() throws IOException { super.setUp(); - v2Config = new AmplExportConfig(AmplExportConfig.ExportScope.ALL, false, - AmplExportConfig.ExportActionType.CURATIVE, false, false, - AmplExportVersion.V1_1); + properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "1.1"); + dataSource = new MemDataSource(); + exporter = new AmplExporter(); } @Test @@ -52,25 +58,82 @@ void testSlackBusExport() throws IOException { SlackTerminalAdder adder = vlGen.newExtension(SlackTerminalAdder.class); adder.withTerminal(bus.getConnectedTerminals().iterator().next()).add(); - MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, v2Config).write(); + exporter.export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/eurostag-tutorial-example1-buses.txt"); } @Test - void testNewTapExport() throws IOException { + void testSlackBusValue3wtMiddleBusExport() throws IOException { + Network network = ThreeWindingsTransformerNetworkFactory.createWithCurrentLimits(); + + exporter.export(network, properties, dataSource); + + // verify slack bus has been added to buses file + assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/three-windings-transformers-buses.txt"); + } + + @Test + void testSlackBusValueDanglingLineMiddleBusExport() throws IOException { + Network network = DanglingLineNetworkFactory.create(); + + exporter.export(network, properties, dataSource); + + // verify slack bus has been added to buses file + assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/dangling-line-buses.txt"); + } + + @Test + void testSlackBusValueTieLineMiddleBusExport() throws IOException { + Network network = EurostagTutorialExample1Factory.createWithTieLine(); + + properties.put("iidm.export.ampl.with-xnodes", "true"); + exporter.export(network, properties, dataSource); + + // verify slack bus has been added to buses file + assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt"); + } + + @Test + void testNewTapTwoWindingsTransformerExport() throws IOException { Network network = EurostagTutorialExample1Factory.create(); TwoWindingsTransformer transformer = network.getTwoWindingsTransformers().iterator().next(); transformer.newRatioTapChanger(); transformer.newPhaseTapChanger(); - MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, v2Config).write(); + exporter.export(network, properties, dataSource); + // verify r, g and b values have been added to tap changer file assertEqualsToRef(dataSource, "_network_tct", "inputs/extended_exporter/eurostag-tutorial-example1-tct.txt"); + } + @Test + void testNewTapThreeWindingsTransformerExport() throws IOException { + Network network = ThreeWindingsTransformerNetworkFactory.createWithCurrentLimits(); + network.getThreeWindingsTransformer("3WT").getLeg1() + .newPhaseTapChanger() + .beginStep() + .setRho(1) + .setR(0.1) + .setX(1.) + .setB(0.) + .setG(0.) + .setAlpha(0) + .endStep() + .setTapPosition(0) + .setLowTapPosition(0) + .add(); + + exporter.export(network, properties, dataSource); + + // verify export is the same as for basic ampl exporter + assertEqualsToRef(dataSource, "_network_branches", "inputs/three-windings-transformers-branches.txt"); + assertEqualsToRef(dataSource, "_network_rtc", "inputs/three-windings-transformers-rtc.txt"); + assertEqualsToRef(dataSource, "_network_limits", "inputs/three-windings-transformers-limits.txt"); + + // verify r, g and b values have been added to tap changer file + assertEqualsToRef(dataSource, "_network_tct", "inputs/extended_exporter/three-windings-transformers-tct.txt"); } @Test @@ -78,8 +141,7 @@ void testRegulatingBusIdExportGenerators() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); network.getGenerator("GEN").setVoltageRegulatorOn(false); - MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, v2Config).write(); + exporter.export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_generators", "inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt"); @@ -89,8 +151,7 @@ void testRegulatingBusIdExportGenerators() throws IOException { void testRegulatingBusIdExportSvc() throws IOException { Network network = SvcTestCaseFactory.createWithMoreSVCs(); network.getStaticVarCompensator("SVC2").setRegulationMode(StaticVarCompensator.RegulationMode.OFF); - MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, v2Config).write(); + exporter.export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_static_var_compensators", "inputs/extended_exporter/svc-test-case-regulating-bus.txt"); @@ -99,8 +160,7 @@ void testRegulatingBusIdExportSvc() throws IOException { @Test void testVersion() throws IOException { Network network = Network.create("dummy_network", "test"); - MemDataSource dataSource = new MemDataSource(); - new AmplNetworkWriter(network, dataSource, v2Config).write(); + exporter.export(network, properties, dataSource); assertEqualsToRef(dataSource, "_headers", "inputs/extended_exporter/headers.txt"); } diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/dangling-line-buses.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/dangling-line-buses.txt new file mode 100644 index 00000000000..6ee9de1a5d2 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/dangling-line-buses.txt @@ -0,0 +1,4 @@ +#Buses (dangling-line/InitialState) +#"variant" "num" "substation" "cc" "v (pu)" "theta (rad)" "p (MW)" "q (MVar)" "slack bus" "fault" "curative" "id" +1 1 1 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "VL_0" +1 2 2 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "DL" diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt new file mode 100644 index 00000000000..831f0411822 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt @@ -0,0 +1,8 @@ +#Buses (sim1/InitialState) +#"variant" "num" "substation" "cc" "v (pu)" "theta (rad)" "p (MW)" "q (MVar)" "slack bus" "fault" "curative" "id" +1 1 1 0 1.02083 0.0405959 -605.558 -225.283 false 0 0 "VLGEN_0" +1 2 2 0 1.05827 0.00000 0.00000 0.00000 false 0 0 "VLHV1_0" +1 3 3 0 1.02619 -0.0611975 0.00000 0.00000 false 0 0 "VLHV2_0" +1 4 4 0 0.983857 -0.167804 600.000 200.000 false 0 0 "VLLOAD_0" +1 5 5 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "XNODE1" +1 6 6 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "XNODE2" diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-buses.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-buses.txt new file mode 100644 index 00000000000..40da6768d11 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-buses.txt @@ -0,0 +1,6 @@ +#Buses (three-windings-transformer/InitialState) +#"variant" "num" "substation" "cc" "v (pu)" "theta (rad)" "p (MW)" "q (MVar)" "slack bus" "fault" "curative" "id" +1 1 1 0 1.01200 -0.167901 0.00000 0.00000 false 0 0 "VL_132_0" +1 2 2 0 1.05700 -0.265988 11.2000 7.50000 false 0 0 "VL_33_0" +1 3 3 0 1.07100 -0.265988 0.00000 -10.6000 false 0 0 "VL_11_0" +1 4 4 0 -99999.0 -99999.0 0.00000 0.00000 false 0 0 "3WT" diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt new file mode 100644 index 00000000000..ca307fb8538 --- /dev/null +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt @@ -0,0 +1,9 @@ +#Tap changer table (three-windings-transformer/InitialState) +#"variant" "num" "tap" "var ratio" "r (pu)" "x (pu)" "g (pu)" "b (pu)" "angle (rad)" "fault" "curative" +1 3 1 1.00000 0.100100 0.0101000 3.29385e-05 3.29385e-06 0.00000 0 0 +1 1 1 0.900000 0.00631126 0.000625613 0.00000 0.00000 0.00000 0 0 +1 1 2 1.00000 0.00631806 0.000625681 0.00000 0.00000 0.00000 0 0 +1 1 3 1.10000 0.00632487 0.000625749 0.00000 0.00000 0.00000 0 0 +1 2 1 0.900000 0.000695201 6.94520e-05 0.00000 0.00000 0.00000 0 0 +1 2 2 1.00000 0.000695285 6.94528e-05 0.00000 0.00000 0.00000 0 0 +1 2 3 1.10000 0.000695369 6.94537e-05 0.00000 0.00000 0.00000 0 0 From c9c95c86c0d3fd16d0758b474ee539a7b90375a4 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 12:15:42 +0200 Subject: [PATCH 50/65] Small clean Signed-off-by: p-arvy --- .../com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 2f732f0d97d..8137122eddd 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -7,7 +7,6 @@ */ package com.powsybl.ampl.converter; -import com.powsybl.ampl.converter.version.AmplExportVersion; import com.powsybl.commons.datasource.MemDataSource; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.iidm.network.*; From 2222f614559c817b33c37ba13d370d780dca65d1 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 12:46:14 +0200 Subject: [PATCH 51/65] Fix G/B per unitage in new ampl extended export for tap changer tables Signed-off-by: p-arvy --- .../version/ExtendedAmplExporterV1.java | 27 +++++++------------ .../three-windings-transformers-tct.txt | 2 +- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 0d7b361aad9..bacc2050c8d 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -232,13 +232,10 @@ private void writeRatioTapChanger(TableFormatter formatter, String id, double zb RatioTapChanger rtc) throws IOException { int num = getMapper().getInt(AmplSubset.TAP_CHANGER_TABLE, id); - int lowTapPosition = rtc.getLowTapPosition(); - for (int position = lowTapPosition; position <= rtc.getHighTapPosition(); position++) { + for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) { RatioTapChangerStep step = rtc.getStep(position); - writeTapChanger(formatter, zb2, - transformerZandY, num, rtc.getLowTapPosition(), - new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()), position, step.getRho(), - 0); + ImpedanceAndAdmittance stepCharacteristics = new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()); + writeTapChanger(formatter, num, position, rtc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), 0); } } @@ -249,23 +246,20 @@ private void writePhaseTapChanger(TableFormatter formatter, String id, double zb for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { PhaseTapChangerStep step = ptc.getStep(position); - writeTapChanger(formatter, zb2, - transformerZandY, num, ptc.getLowTapPosition(), - new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()), position, step.getRho(), - Math.toRadians(step.getAlpha())); + ImpedanceAndAdmittance stepCharacteristics = new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()); + writeTapChanger(formatter, num, position, ptc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), Math.toRadians(step.getAlpha())); } } - private void writeTapChanger(TableFormatter formatter, double zb2, ImpedanceAndAdmittance transformer, int num, - int lowTapPosition, ImpedanceAndAdmittance step, int position, double rho, - double alpha) throws IOException { + private void writeTapChanger(TableFormatter formatter, int num, int stepPosition, int lowTapPosition, double zb2, + ImpedanceAndAdmittance transformer, ImpedanceAndAdmittance step, double rho, double alpha) throws IOException { double rNorm = transformer.r * (1 + step.r / 100) / zb2; double xNorm = transformer.x * (1 + step.x / 100) / zb2; - double gNorm = transformer.g * (1 + step.g / 100) / zb2; - double bNorm = transformer.b * (1 + step.b / 100) / zb2; + double gNorm = transformer.g * (1 + step.g / 100) * zb2; + double bNorm = transformer.b * (1 + step.b / 100) * zb2; formatter.writeCell(getVariantIndex()) .writeCell(num) - .writeCell(position - lowTapPosition + 1) + .writeCell(stepPosition - lowTapPosition + 1) .writeCell(rho) .writeCell(rNorm) .writeCell(xNorm) @@ -353,7 +347,6 @@ public void writeStaticVarCompensatorToFormatter(TableFormatter formatter, .writeCell(svc.getNameOrId()) .writeCell(t.getP()) .writeCell(t.getQ()); - } } diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt index ca307fb8538..cb190a811e2 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/three-windings-transformers-tct.txt @@ -1,6 +1,6 @@ #Tap changer table (three-windings-transformer/InitialState) #"variant" "num" "tap" "var ratio" "r (pu)" "x (pu)" "g (pu)" "b (pu)" "angle (rad)" "fault" "curative" -1 3 1 1.00000 0.100100 0.0101000 3.29385e-05 3.29385e-06 0.00000 0 0 +1 3 1 1.00000 0.100100 0.0101000 1.00000 0.100000 0.00000 0 0 1 1 1 0.900000 0.00631126 0.000625613 0.00000 0.00000 0.00000 0 0 1 1 2 1.00000 0.00631806 0.000625681 0.00000 0.00000 0.00000 0 0 1 1 3 1.10000 0.00632487 0.000625749 0.00000 0.00000 0.00000 0 0 From 5c5e901c8f7fbfdf23feb962e14930d92b0a3251 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 14:18:05 +0200 Subject: [PATCH 52/65] Improve TUs on remote regulating terminal Signed-off-by: p-arvy --- .../converter/ExtendedAmplExporterV1Test.java | 25 +++++++++++++++++++ ...ial-example1-generators-regulating-bus.txt | 1 + .../svc-test-case-regulating-bus.txt | 1 + 3 files changed, 27 insertions(+) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 8137122eddd..d7314b72f04 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -27,6 +27,7 @@ /** * @author Nicolas PIERRE {@literal } + * @author Pierre ARVY {@literal } */ class ExtendedAmplExporterV1Test extends AbstractSerDeTest { @@ -40,6 +41,7 @@ private void assertEqualsToRef(MemDataSource dataSource, String suffix, String r } } + @Override @BeforeEach public void setUp() throws IOException { super.setUp(); @@ -139,6 +141,18 @@ void testNewTapThreeWindingsTransformerExport() throws IOException { void testRegulatingBusIdExportGenerators() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); network.getGenerator("GEN").setVoltageRegulatorOn(false); + network.getVoltageLevel("VLGEN").newGenerator() + .setId("GEN3") + .setBus("NGEN") + .setConnectableBus("NGEN") + .setMinP(-9999.99) + .setMaxP(9999.99) + .setVoltageRegulatorOn(true) + .setRegulatingTerminal(network.getLoad("LOAD").getTerminal()) + .setTargetV(152.5) + .setTargetP(607.0) + .setTargetQ(301.0) + .add(); exporter.export(network, properties, dataSource); @@ -150,6 +164,17 @@ void testRegulatingBusIdExportGenerators() throws IOException { void testRegulatingBusIdExportSvc() throws IOException { Network network = SvcTestCaseFactory.createWithMoreSVCs(); network.getStaticVarCompensator("SVC2").setRegulationMode(StaticVarCompensator.RegulationMode.OFF); + network.getVoltageLevel("VL1").newStaticVarCompensator() + .setId("SVC1") + .setConnectableBus("B1") + .setBus("B1") + .setBmin(0.0002) + .setBmax(0.0008) + .setRegulationMode(StaticVarCompensator.RegulationMode.VOLTAGE) + .setVoltageSetpoint(390) + .setRegulatingTerminal(network.getLoad("L2").getTerminal()) + .add(); + exporter.export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_static_var_compensators", diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt index 0ee5057c9df..e39619353d9 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt @@ -2,3 +2,4 @@ #"variant" "num" "bus" "con. bus" "substation" "minP (MW)" "maxP (MW)" "minQmaxP (MVar)" "minQ0 (MVar)" "minQminP (MVar)" "maxQmaxP (MVar)" "maxQ0 (MVar)" "maxQminP (MVar)" "v regul." "v regul. bus" "targetV (pu)" "targetP (MW)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 1 1 1 -9999.99 9999.99 -9999.99 -9999.99 -9999.99 9999.99 9999.99 9999.99 false -1 1.02083 607.000 301.000 0 0 "GEN" "GEN" -99999.0 -99999.0 1 2 1 1 1 -9999.99 9999.99 4.00000 6.00000 6.00000 5.00000 7.00000 7.00000 true 1 1.02083 607.000 301.000 0 0 "GEN2" "GEN2" -99999.0 -99999.0 +1 3 1 1 1 -9999.99 9999.99 -1.79769e+308 -1.79769e+308 -1.79769e+308 1.79769e+308 1.79769e+308 1.79769e+308 true 4 1.01667 607.000 301.000 0 0 "GEN3" "GEN3" -99999.0 -99999.0 diff --git a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt index 0f867134bb0..6c474e1d57f 100644 --- a/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt +++ b/ampl-converter/src/test/resources/inputs/extended_exporter/svc-test-case-regulating-bus.txt @@ -2,3 +2,4 @@ #"variant" "num" "bus" "con. bus" "substation" "minB (pu)" "maxB (pu)" "v regul." "v regul. bus" "targetV (pu)" "targetQ (MVar)" "fault" "curative" "id" "description" "P (MW)" "Q (MVar)" 1 1 2 2 2 0.288800 1.15520 false -1 1.02632 -99999.0 0 0 "SVC2" "SVC2" -99999.0 -99999.0 1 2 2 2 2 0.288800 1.15520 true 2 1.02632 350.000 0 0 "SVC3" "SVC3" -99999.0 -99999.0 +1 3 1 1 1 0.288800 1.15520 true 2 1.02632 -99999.0 0 0 "SVC1" "SVC1" -99999.0 -99999.0 From dfc89a44c878bd7ff3ae235cb4a851621863d5b6 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 14:24:33 +0200 Subject: [PATCH 53/65] Add comment Signed-off-by: p-arvy --- .../ampl/converter/version/ExtendedAmplExporterV1.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index bacc2050c8d..6189dfd459e 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -26,9 +26,14 @@ import static com.powsybl.ampl.converter.AmplConstants.*; /** - * 1st (V1) extension of BasicAmplExporter, associated with AMPL version 1.1 (exporter id) + * 1st (V1) extension of BasicAmplExporter, associated with AMPL version 1.1 (exporter id). + * The extension adds: + * - A slack bus boolean in the bus table. + * - R, G and B characteristics in tap tables. + * - The regulated bus in generator and static var compensator tables. * * @author Nicolas PIERRE {@literal } + * @author Pierre ARVY {@literal } */ public class ExtendedAmplExporterV1 extends BasicAmplExporter { From 87d575ee19ca8fa0fae28bc5bb7324e8828062ce Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 14:37:56 +0200 Subject: [PATCH 54/65] Remove block of comments Signed-off-by: p-arvy --- .../AmplModelExecutionHandlerTest.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java index a3b9e645d65..bc7c1090be9 100644 --- a/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java +++ b/ampl-executor/src/test/java/com/powsybl/ampl/executor/AmplModelExecutionHandlerTest.java @@ -294,26 +294,4 @@ public boolean checkModelConvergence(Map metrics) { } } -// @Test -// void testAmplParametersVersion() throws Exception { -// try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { -// Files.createDirectory(fs.getPath("/workingDir")); -// // Test data -// Network network = EurostagTutorialExample1Factory.create(); -// DummyAmplModel model = new DummyAmplModel(); -// AmplConfig cfg = getAmplConfig(); -// // Test config -// String variantId = network.getVariantManager().getWorkingVariantId(); -// AmplParameters parameters = new EmptyAmplParameters() { -// @Override -// public String getAmplExportVersionId() { -// return "1.2"; -// } -// }; -// AmplModelExecutionHandler handler = new AmplModelExecutionHandler(model, network, variantId, cfg, parameters); -// Exception e = assertThrows(IllegalArgumentException.class, () -> handler.before(fs.getPath("/workingDir"))); -// assertTrue(e.getMessage().contains("Value 1.2 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0")); -// } -// } - } From f1dd78e4fabc7d5923530d95635ddd8304b9c352 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 14:50:13 +0200 Subject: [PATCH 55/65] Add TU Signed-off-by: p-arvy --- .../converter/ExtendedAmplExporterV1Test.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index d7314b72f04..3aa74344396 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -7,6 +7,7 @@ */ package com.powsybl.ampl.converter; +import com.powsybl.ampl.converter.version.AmplExportVersion; import com.powsybl.commons.datasource.MemDataSource; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.iidm.network.*; @@ -51,6 +52,19 @@ public void setUp() throws IOException { exporter = new AmplExporter(); } + @Test + void testNoModifiedExports() throws IOException { + Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); + + exporter.export(network, properties, dataSource); + + assertEqualsToRef(dataSource, "_network_substations", "inputs/eurostag-tutorial-example1-substations.txt"); + assertEqualsToRef(dataSource, "_network_rtc", "inputs/eurostag-tutorial-example1-rtc.txt"); + assertEqualsToRef(dataSource, "_network_ptc", "inputs/eurostag-tutorial-example1-ptc.txt"); + assertEqualsToRef(dataSource, "_network_loads", "inputs/eurostag-tutorial-example1-loads.txt"); + assertEqualsToRef(dataSource, "_network_limits", "inputs/eurostag-tutorial-example1-limits.txt"); + } + @Test void testSlackBusExport() throws IOException { Network network = EurostagTutorialExample1Factory.create(); @@ -72,7 +86,8 @@ void testSlackBusValue3wtMiddleBusExport() throws IOException { exporter.export(network, properties, dataSource); // verify slack bus has been added to buses file - assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/three-windings-transformers-buses.txt"); + assertEqualsToRef(dataSource, "_network_buses", + "inputs/extended_exporter/three-windings-transformers-buses.txt"); } @Test @@ -82,7 +97,8 @@ void testSlackBusValueDanglingLineMiddleBusExport() throws IOException { exporter.export(network, properties, dataSource); // verify slack bus has been added to buses file - assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/dangling-line-buses.txt"); + assertEqualsToRef(dataSource, "_network_buses", + "inputs/extended_exporter/dangling-line-buses.txt"); } @Test @@ -93,7 +109,8 @@ void testSlackBusValueTieLineMiddleBusExport() throws IOException { exporter.export(network, properties, dataSource); // verify slack bus has been added to buses file - assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt"); + assertEqualsToRef(dataSource, "_network_buses", + "inputs/extended_exporter/eurostag-tutorial-example1-buses-tl.txt"); } @Test @@ -128,13 +145,9 @@ void testNewTapThreeWindingsTransformerExport() throws IOException { exporter.export(network, properties, dataSource); - // verify export is the same as for basic ampl exporter - assertEqualsToRef(dataSource, "_network_branches", "inputs/three-windings-transformers-branches.txt"); - assertEqualsToRef(dataSource, "_network_rtc", "inputs/three-windings-transformers-rtc.txt"); - assertEqualsToRef(dataSource, "_network_limits", "inputs/three-windings-transformers-limits.txt"); - // verify r, g and b values have been added to tap changer file - assertEqualsToRef(dataSource, "_network_tct", "inputs/extended_exporter/three-windings-transformers-tct.txt"); + assertEqualsToRef(dataSource, "_network_tct", + "inputs/extended_exporter/three-windings-transformers-tct.txt"); } @Test From 5619c2da2bd83038aa0eac09c81a600a3e03ad97 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 15:09:30 +0200 Subject: [PATCH 56/65] Fix unused import Signed-off-by: p-arvy --- .../com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index 3aa74344396..c254495c36b 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -7,7 +7,6 @@ */ package com.powsybl.ampl.converter; -import com.powsybl.ampl.converter.version.AmplExportVersion; import com.powsybl.commons.datasource.MemDataSource; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.iidm.network.*; From ba29e11dec127c1ad9f721551a8f7dd30a7ba675 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 2 Sep 2024 15:36:29 +0200 Subject: [PATCH 57/65] Put extended version of the exporter as default version Signed-off-by: p-arvy --- .../converter/version/AmplExportVersion.java | 2 +- .../ampl/converter/AmplNetworkWriterTest.java | 51 ++++++++++--------- .../converter/ExtendedAmplExporterV1Test.java | 22 ++++---- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index a208f5221e9..8e8522729e4 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -64,6 +64,6 @@ public static AmplExportVersion fromExporterId(String exporterId) { } public static AmplExportVersion defaultVersion() { - return V1_0; + return V1_1; } } diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index 6cb015e04a1..fa7113f4dbc 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -7,12 +7,12 @@ */ package com.powsybl.ampl.converter; -import com.powsybl.ampl.converter.version.AmplExportVersion; import com.powsybl.commons.test.AbstractSerDeTest; import com.powsybl.commons.datasource.DataSource; import com.powsybl.commons.datasource.MemDataSource; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.test.*; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; @@ -28,12 +28,22 @@ */ class AmplNetworkWriterTest extends AbstractSerDeTest { + Properties properties; + private void assertEqualsToRef(MemDataSource dataSource, String suffix, String refFileName) throws IOException { try (InputStream actual = new ByteArrayInputStream(dataSource.getData(suffix, "txt"))) { assertTxtEquals(getClass().getResourceAsStream("/" + refFileName), actual); } } + @Override + @BeforeEach + public void setUp() throws IOException { + super.setUp(); + properties = new Properties(); + properties.put("iidm.export.ampl.export-version", "1.0"); + } + @Test void test() { AmplExporter exporter = new AmplExporter(); @@ -47,9 +57,7 @@ void writeEurostag() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); MemDataSource dataSource = new MemDataSource(); - AmplExporter exporter = new AmplExporter(); - AmplExportConfig amplExportConfig = new AmplExportConfig(AmplExportConfig.ExportScope.ALL, false, AmplExportConfig.ExportActionType.CURATIVE, false, false, AmplExportVersion.defaultVersion(), true); - exporter.export(network, amplExportConfig, dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_substations", "inputs/eurostag-tutorial-example1-substations.txt"); assertEqualsToRef(dataSource, "_network_buses", "inputs/eurostag-tutorial-example1-buses.txt"); @@ -64,12 +72,10 @@ void writeEurostag() throws IOException { @Test void writeNetworkWithExtension() throws IOException { Network network = Network.create("sim1", "test"); - network.addExtension(FooNetworkExtension.class, new FooNetworkExtension()); MemDataSource dataSource = new MemDataSource(); - AmplExporter exporter = new AmplExporter(); - exporter.export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "foo-network-extension", "inputs/foo-network-extension.txt"); } @@ -79,8 +85,7 @@ void writeShunt() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMultipleConnectedComponents(); MemDataSource dataSource = new MemDataSource(); - AmplExporter exporter = new AmplExporter(); - exporter.export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_shunts", "inputs/eurostag-tutorial-example1-shunts.txt"); } @@ -92,8 +97,7 @@ void writeShunt2() throws IOException { sc.setSectionCount(2); MemDataSource dataSource = new MemDataSource(); - AmplExporter exporter = new AmplExporter(); - exporter.export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_shunts", "inputs/shunt-test-case-shunts.txt"); } @@ -103,7 +107,7 @@ void writeLcc() throws IOException { Network network = HvdcTestNetwork.createLcc(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_hvdc", "inputs/hvdc-lcc-test-case.txt"); assertEqualsToRef(dataSource, "_network_lcc_converter_stations", "inputs/lcc-test-case.txt"); @@ -115,7 +119,7 @@ void writePhaseTapChanger() throws IOException { Network network = PhaseShifterTestCaseFactory.create(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_ptc", "inputs/ptc-test-case.txt"); } @@ -125,7 +129,7 @@ void writeSVC() throws IOException { Network network = SvcTestCaseFactory.createWithMoreSVCs(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_static_var_compensators", "inputs/svc-test-case.txt"); } @@ -135,7 +139,7 @@ void writeBattery() throws IOException { Network network = BatteryNetworkFactory.create(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_batteries", "inputs/battery-test-batteries.txt"); } @@ -158,7 +162,7 @@ void writeThreeWindingsTransformer() throws IOException { .add(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_branches", "inputs/three-windings-transformers-branches.txt"); assertEqualsToRef(dataSource, "_network_buses", "inputs/three-windings-transformers-buses.txt"); @@ -173,7 +177,7 @@ void writeVsc() throws IOException { Network network = HvdcTestNetwork.createVsc(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_hvdc", "inputs/hvdc-vsc-test-case.txt"); assertEqualsToRef(dataSource, "_network_vsc_converter_stations", "inputs/vsc-test-case.txt"); @@ -185,7 +189,7 @@ void writeCurrentLimits() throws IOException { Network network = EurostagTutorialExample1Factory.createWithCurrentLimits(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_limits", "inputs/current-limits-test-case.txt"); } @@ -201,7 +205,6 @@ void writeTieLine() throws IOException { .add(); } - Properties properties = new Properties(); properties.put("iidm.export.ampl.with-xnodes", "true"); MemDataSource dataSource = new MemDataSource(); @@ -218,7 +221,7 @@ void writeDanglingLines() throws IOException { Network network = DanglingLineNetworkFactory.create(); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_branches", "inputs/dangling-line-branches.txt"); assertEqualsToRef(dataSource, "_network_buses", "inputs/dangling-line-buses.txt"); @@ -233,7 +236,7 @@ void writeExtensions() throws IOException { HvdcLine l = network.getHvdcLine("L"); l.addExtension(FooExtension.class, new FooExtension()); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "foo-extension", "inputs/foo-extension.txt"); } @@ -247,7 +250,7 @@ private void export(Network network, Properties properties, DataSource dataSourc void writeHeaders() throws IOException { Network network = Network.create("dummy_network", "test"); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_headers", "inputs/headers.txt"); } @@ -294,7 +297,7 @@ void writeLineWithDifferentNominalVoltageAtEnds() throws IOException { network.getVoltageLevel("VL2").setNominalV(400); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_branches", "inputs/line-with-different-nominal-voltage-at-ends-test-case.txt"); } @@ -307,7 +310,7 @@ void writeZeroImpedanceLineWithDifferentNominalVoltageAtEnds() throws IOExceptio .setX(0); MemDataSource dataSource = new MemDataSource(); - export(network, new Properties(), dataSource); + export(network, properties, dataSource); assertEqualsToRef(dataSource, "_network_branches", "inputs/zero-impedance-line-with-different-nominal-voltage-at-ends-test-case.txt"); } diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java index c254495c36b..a62436938df 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java @@ -31,7 +31,6 @@ */ class ExtendedAmplExporterV1Test extends AbstractSerDeTest { - Properties properties; MemDataSource dataSource; AmplExporter exporter; @@ -45,8 +44,6 @@ private void assertEqualsToRef(MemDataSource dataSource, String suffix, String r @BeforeEach public void setUp() throws IOException { super.setUp(); - properties = new Properties(); - properties.put("iidm.export.ampl.export-version", "1.1"); dataSource = new MemDataSource(); exporter = new AmplExporter(); } @@ -55,7 +52,7 @@ public void setUp() throws IOException { void testNoModifiedExports() throws IOException { Network network = EurostagTutorialExample1Factory.createWithMoreGenerators(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_network_substations", "inputs/eurostag-tutorial-example1-substations.txt"); assertEqualsToRef(dataSource, "_network_rtc", "inputs/eurostag-tutorial-example1-rtc.txt"); @@ -72,7 +69,7 @@ void testSlackBusExport() throws IOException { SlackTerminalAdder adder = vlGen.newExtension(SlackTerminalAdder.class); adder.withTerminal(bus.getConnectedTerminals().iterator().next()).add(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_network_buses", "inputs/extended_exporter/eurostag-tutorial-example1-buses.txt"); @@ -82,7 +79,7 @@ void testSlackBusExport() throws IOException { void testSlackBusValue3wtMiddleBusExport() throws IOException { Network network = ThreeWindingsTransformerNetworkFactory.createWithCurrentLimits(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); // verify slack bus has been added to buses file assertEqualsToRef(dataSource, "_network_buses", @@ -93,7 +90,7 @@ void testSlackBusValue3wtMiddleBusExport() throws IOException { void testSlackBusValueDanglingLineMiddleBusExport() throws IOException { Network network = DanglingLineNetworkFactory.create(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); // verify slack bus has been added to buses file assertEqualsToRef(dataSource, "_network_buses", @@ -104,6 +101,7 @@ void testSlackBusValueDanglingLineMiddleBusExport() throws IOException { void testSlackBusValueTieLineMiddleBusExport() throws IOException { Network network = EurostagTutorialExample1Factory.createWithTieLine(); + Properties properties = new Properties(); properties.put("iidm.export.ampl.with-xnodes", "true"); exporter.export(network, properties, dataSource); @@ -119,7 +117,7 @@ void testNewTapTwoWindingsTransformerExport() throws IOException { TwoWindingsTransformer transformer = network.getTwoWindingsTransformers().iterator().next(); transformer.newRatioTapChanger(); transformer.newPhaseTapChanger(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); // verify r, g and b values have been added to tap changer file assertEqualsToRef(dataSource, "_network_tct", "inputs/extended_exporter/eurostag-tutorial-example1-tct.txt"); @@ -142,7 +140,7 @@ void testNewTapThreeWindingsTransformerExport() throws IOException { .setLowTapPosition(0) .add(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); // verify r, g and b values have been added to tap changer file assertEqualsToRef(dataSource, "_network_tct", @@ -166,7 +164,7 @@ void testRegulatingBusIdExportGenerators() throws IOException { .setTargetQ(301.0) .add(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_network_generators", "inputs/extended_exporter/eurostag-tutorial-example1-generators-regulating-bus.txt"); @@ -187,7 +185,7 @@ void testRegulatingBusIdExportSvc() throws IOException { .setRegulatingTerminal(network.getLoad("L2").getTerminal()) .add(); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_network_static_var_compensators", "inputs/extended_exporter/svc-test-case-regulating-bus.txt"); @@ -196,7 +194,7 @@ void testRegulatingBusIdExportSvc() throws IOException { @Test void testVersion() throws IOException { Network network = Network.create("dummy_network", "test"); - exporter.export(network, properties, dataSource); + exporter.export(network, new Properties(), dataSource); assertEqualsToRef(dataSource, "_headers", "inputs/extended_exporter/headers.txt"); } From c6fc222d06622a3d016c0773b05984c59b098696 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Wed, 11 Sep 2024 14:42:27 +0200 Subject: [PATCH 58/65] Update version of iidm extensions Signed-off-by: p-arvy --- ampl-converter/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ampl-converter/pom.xml b/ampl-converter/pom.xml index 678e4bb0afc..bc6d7f7c3a5 100644 --- a/ampl-converter/pom.xml +++ b/ampl-converter/pom.xml @@ -90,8 +90,9 @@ test - com.powsybl + ${project.groupId} powsybl-iidm-extensions + ${project.version} From 1e643c1859d35f67766fbe47c62974c17d578646 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Wed, 11 Sep 2024 14:45:41 +0200 Subject: [PATCH 59/65] Remove properties override in TUs Signed-off-by: p-arvy --- .../java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java index fa7113f4dbc..5ed81140981 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/AmplNetworkWriterTest.java @@ -259,7 +259,6 @@ void writeHeadersWithVersion10() throws IOException { Network network = Network.create("dummy_network", "test"); MemDataSource dataSource = new MemDataSource(); - Properties properties = new Properties(); properties.put("iidm.export.ampl.export-version", "1.0"); export(network, properties, dataSource); @@ -271,7 +270,6 @@ void writeHeadersWithUnknownVersion() { Network network = Network.create("dummy_network", "test"); MemDataSource dataSource = new MemDataSource(); - Properties properties = new Properties(); properties.put("iidm.export.ampl.export-version", "V1_0"); Exception e = assertThrows(IllegalArgumentException.class, () -> export(network, properties, dataSource)); @@ -284,7 +282,6 @@ void writeHeadersWithVersion11() throws IOException { Network network = Network.create("dummy_network", "test"); MemDataSource dataSource = new MemDataSource(); - Properties properties = new Properties(); properties.put("iidm.export.ampl.export-version", "1.1"); export(network, properties, dataSource); From 661e9c1c476774243b8e309e03fdc9027f40195b Mon Sep 17 00:00:00 2001 From: Nicolas Rol Date: Fri, 13 Sep 2024 17:28:56 +0200 Subject: [PATCH 60/65] initialize TableFormatterHelper Signed-off-by: Nicolas Rol --- .../io/table/TableFormatterHelper.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java diff --git a/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java b/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java new file mode 100644 index 00000000000..53f43d4f96a --- /dev/null +++ b/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024, 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.commons.io.table; + +import com.powsybl.commons.PowsyblException; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Nicolas Rol {@literal } + */ +public class TableFormatterHelper { + + private final TableFormatter tableFormatter; + private final List objectsToWrite = new LinkedList<>(); + + public TableFormatterHelper(TableFormatter tableFormatter) { + this.tableFormatter = tableFormatter; + } + + public TableFormatterHelper add(Object object) { + objectsToWrite.add(object); + return this; + } + + public TableFormatterHelper add(Object object, int position) { + objectsToWrite.add(position, object); + return this; + } + + public TableFormatterHelper addEmptyCell() { + objectsToWrite.add(new EmptyCells(1)); + return this; + } + + public TableFormatterHelper addEmptyCell(int position) { + objectsToWrite.add(position, new EmptyCells(1)); + return this; + } + + public TableFormatterHelper addEmptyCells(int count) { + objectsToWrite.add(new EmptyCells(count)); + return this; + } + + public TableFormatterHelper addEmptyCells(int count, int position) { + objectsToWrite.add(position, new EmptyCells(count)); + return this; + } + + public TableFormatterHelper addEmptyLine() { + objectsToWrite.add(new EmptyLines(1)); + return this; + } + + public TableFormatterHelper addEmptyLine(int position) { + objectsToWrite.add(position, new EmptyLines(1)); + return this; + } + + public TableFormatterHelper addEmptyLines(int count) { + objectsToWrite.add(new EmptyLines(count)); + return this; + } + + public TableFormatterHelper addEmptyLines(int count, int position) { + objectsToWrite.add(position, new EmptyLines(count)); + return this; + } + + public TableFormatterHelper addComment(String comment) { + objectsToWrite.add(new Comment(comment)); + return this; + } + + public TableFormatterHelper addComment(String comment, int position) { + objectsToWrite.add(position, new Comment(comment)); + return this; + } + + public TableFormatter write() { + objectsToWrite.forEach(object -> { + try { + if (object instanceof String s) { + tableFormatter.writeCell(s); + } else if (object instanceof Character c) { + tableFormatter.writeCell(c); + } else if (object instanceof Integer i) { + tableFormatter.writeCell(i); + } else if (object instanceof Float f) { + tableFormatter.writeCell(f); + } else if (object instanceof Double d) { + tableFormatter.writeCell(d); + } else if (object instanceof Boolean b) { + tableFormatter.writeCell(b); + } else if (object instanceof Comment comment) { + tableFormatter.writeComment(comment.commentToWrite); + } else if (object instanceof EmptyCells emptyCells) { + tableFormatter.writeEmptyCells(emptyCells.numberOfCells); + } else if (object instanceof EmptyLines emptyLines) { + tableFormatter.writeEmptyLines(emptyLines.numberOfLines); + } + } catch (IOException e) { + throw new PowsyblException(e); + } + }); + return tableFormatter; + } + + private static class Comment { + String commentToWrite; + Comment(String commentToWrite) { + this.commentToWrite = commentToWrite; + } + } + + private static class EmptyCells { + int numberOfCells; + EmptyCells(int numberOfCells) { + this.numberOfCells = numberOfCells; + } + } + + private static class EmptyLines { + int numberOfLines; + EmptyLines(int numberOfLines) { + this.numberOfLines = numberOfLines; + } + } +} From 6e12d01f58e281f42a686eb87bf1d464719fd8de Mon Sep 17 00:00:00 2001 From: Nicolas Rol Date: Mon, 16 Sep 2024 09:05:53 +0200 Subject: [PATCH 61/65] finish TableFormatterHelper Signed-off-by: Nicolas Rol --- .../converter/version/BasicAmplExporter.java | 372 ++++++++++++------ .../version/ExtendedAmplExporterV1.java | 178 +-------- .../io/table/TableFormatterHelper.java | 19 +- 3 files changed, 276 insertions(+), 293 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java index b30adea441f..06267f37b52 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/BasicAmplExporter.java @@ -13,6 +13,7 @@ import com.powsybl.ampl.converter.AmplUtil; import com.powsybl.commons.io.table.Column; import com.powsybl.commons.io.table.TableFormatter; +import com.powsybl.commons.io.table.TableFormatterHelper; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.util.ConnectedComponents; @@ -614,17 +615,28 @@ public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws double nomV = vl.getNominalV(); double v = b.getV() / nomV; double theta = Math.toRadians(b.getAngle()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(vlNum) - .writeCell(ccNum) - .writeCell(v) - .writeCell(theta) - .writeCell(b.getP()) - .writeCell(b.getQ()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell(vlNum) + .addCell(ccNum) + .addCell(v) + .addCell(theta) + .addCell(b.getP()) + .addCell(b.getQ()) + .addCell(faultNum) + .addCell(actionNum) + .addCell(id); + + // Add cells if necessary + addAdditionalCellsBusesColumns(formatterHelper, b); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsBusesColumns(TableFormatterHelper formatterHelper, Bus b) { + // Nothing to do here } @Override @@ -641,17 +653,30 @@ public void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableForm double angle = twt.getProperty("angle") == null ? Double.NaN : Math.toRadians(Double.parseDouble(twt.getProperty("angle"))); - formatter.writeCell(variantIndex) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(v) - .writeCell(angle) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(middleBusId); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(middleBusNum) + .addCell(middleVlNum) + .addCell(middleCcNum) + .addCell(v) + .addCell(angle) + .addCell(0.0) + .addCell(0.0) + .addCell(faultNum) + .addCell(actionNum) + .addCell(middleBusId); + + // Add cells if necessary + addAdditionalCellsThreeWindingsTranformersMiddleBusesColumns(formatterHelper, twt, middleCcNum); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsThreeWindingsTranformersMiddleBusesColumns(TableFormatterHelper formatterHelper, + ThreeWindingsTransformer twt, + int middleCcNum) { + // Nothing to do here } @Override @@ -669,17 +694,30 @@ public void writeDanglingLineMiddleBusesToFormatter(TableFormatter formatter, Da double nomV = t.getVoltageLevel().getNominalV(); double v = sv.getU() / nomV; double theta = Math.toRadians(sv.getA()); - formatter.writeCell(variantIndex) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(v) - .writeCell(theta) - .writeCell(0.0) // 0 MW injected at dangling line internal bus - .writeCell(0.0) // 0 MVar injected at dangling line internal bus - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(middleBusId); + + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(middleBusNum) + .addCell(middleVlNum) + .addCell(middleCcNum) + .addCell(v) + .addCell(theta) + .addCell(0.0) // 0 MW injected at dangling line internal bus + .addCell(0.0) // 0 MVar injected at dangling line internal bus + .addCell(faultNum) + .addCell(actionNum) + .addCell(middleBusId); + + // Add cells if necessary + addAdditionalCellsDanglingLineMiddleBuses(formatterHelper, dl, middleCcNum); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsDanglingLineMiddleBuses(TableFormatterHelper formatterHelper, DanglingLine dl, + int middleCcNum) { + // Nothing to do here } @Override @@ -688,17 +726,30 @@ public void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine String xNodeBusId = AmplUtil.getXnodeBusId(tieLine); int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId); int xNodeVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)); - formatter.writeCell(variantIndex) - .writeCell(xNodeBusNum) - .writeCell(xNodeVlNum) - .writeCell(xNodeCcNum) - .writeCell(Float.NaN) - .writeCell(Double.NaN) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(xNodeBusId); + + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(xNodeBusNum) + .addCell(xNodeVlNum) + .addCell(xNodeCcNum) + .addCell(Float.NaN) + .addCell(Double.NaN) + .addCell(0.0) + .addCell(0.0) + .addCell(faultNum) + .addCell(actionNum) + .addCell(xNodeBusId); + + // Add cells if necessary + addAdditionalCellsTieLineMiddleBuses(formatterHelper, tieLine, xNodeCcNum); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsTieLineMiddleBuses(TableFormatterHelper formatterHelper, TieLine tieLine, + int xNodeCcNum) { + // Nothing to do here } @Override @@ -1210,29 +1261,40 @@ public void writeGeneratorToFormatter(TableFormatter formatter, Generator gen) t double maxP = gen.getMaxP(); double vb = gen.getRegulatingTerminal().getVoltageLevel().getNominalV(); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(minP) - .writeCell(maxP) - .writeCell(gen.getReactiveLimits().getMinQ(maxP)) - .writeCell(gen.getReactiveLimits().getMinQ(0)) - .writeCell(gen.getReactiveLimits().getMinQ(minP)) - .writeCell(gen.getReactiveLimits().getMaxQ(maxP)) - .writeCell(gen.getReactiveLimits().getMaxQ(0)) - .writeCell(gen.getReactiveLimits().getMaxQ(minP)) - .writeCell(gen.isVoltageRegulatorOn()) - .writeCell(gen.getTargetV() / vb) - .writeCell(gen.getTargetP()) - .writeCell(gen.getTargetQ()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(gen.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell(busNum) + .addCell(conBusNum != -1 ? conBusNum : busNum) + .addCell(vlNum) + .addCell(minP) + .addCell(maxP) + .addCell(gen.getReactiveLimits().getMinQ(maxP)) + .addCell(gen.getReactiveLimits().getMinQ(0)) + .addCell(gen.getReactiveLimits().getMinQ(minP)) + .addCell(gen.getReactiveLimits().getMaxQ(maxP)) + .addCell(gen.getReactiveLimits().getMaxQ(0)) + .addCell(gen.getReactiveLimits().getMaxQ(minP)) + .addCell(gen.isVoltageRegulatorOn()) + .addCell(gen.getTargetV() / vb) + .addCell(gen.getTargetP()) + .addCell(gen.getTargetQ()) + .addCell(faultNum) + .addCell(actionNum) + .addCell(id) + .addCell(gen.getNameOrId()) + .addCell(t.getP()) + .addCell(t.getQ()); + + // Add cells if necessary + addAdditionalCellsGenerator(formatterHelper, gen); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsGenerator(TableFormatterHelper formatterHelper, Generator gen) { + // Nothing to do here } @Override @@ -1286,23 +1348,35 @@ public void writeStaticVarCompensatorToFormatter(TableFormatter formatter, double zb = vb * vb / AmplConstants.SB; // Base impedance int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum) - .writeCell(vlNum) - .writeCell(svc.getBmin() * zb) - .writeCell(svc.getBmax() * zb) - .writeCell(svc.getRegulationMode().equals(StaticVarCompensator.RegulationMode.VOLTAGE)) - .writeCell(vlSet / vb) - .writeCell(svc.getReactivePowerSetpoint()) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(id) - .writeCell(svc.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell(busNum) + .addCell(conBusNum) + .addCell(vlNum) + .addCell(svc.getBmin() * zb) + .addCell(svc.getBmax() * zb) + .addCell(svc.getRegulationMode().equals(StaticVarCompensator.RegulationMode.VOLTAGE)) + .addCell(vlSet / vb) + .addCell(svc.getReactivePowerSetpoint()) + .addCell(faultNum) + .addCell(actionNum) + .addCell(id) + .addCell(svc.getNameOrId()) + .addCell(t.getP()) + .addCell(t.getQ()); + + // Add cells if necessary + addAdditionalCellsStaticVarCompensator(formatterHelper, svc); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsStaticVarCompensator(TableFormatterHelper formatterHelper, + StaticVarCompensator svc) { + // Nothing to do here } @Override @@ -1314,18 +1388,31 @@ public void writeDanglingLineVoltageLevelToFormatter(TableFormatter formatter, double nomV = vl.getNominalV(); double minV = vl.getLowVoltageLimit() / nomV; double maxV = vl.getHighVoltageLimit() / nomV; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(nomV) - .writeCell(minV) - .writeCell(maxV) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) - .writeCell(dl.getId() + "_voltageLevel") - .writeCell(""); + + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell("") + .addCell(0) + .addCell(nomV) + .addCell(minV) + .addCell(maxV) + .addCell(faultNum) + .addCell(actionNum) + .addCell(vl.getSubstation().flatMap(Substation::getCountry).map(Enum::toString).orElse("")) + .addCell(dl.getId() + "_voltageLevel") + .addCell(""); + + // Add cells if necessary + addAdditionalCellsDanglingLineVoltageLevel(formatterHelper, dl); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsDanglingLineVoltageLevel(TableFormatterHelper formatterHelper, + DanglingLine dl) { + // Nothing to do here } @Override @@ -1333,24 +1420,37 @@ public void writeThreeWindingsTransformerVoltageLevelToFormatter(TableFormatter ThreeWindingsTransformer twt) throws IOException { String vlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId); - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell("") - .writeCell(0) - .writeCell(twt.getRatedU0()) - .writeCell(Float.NaN) - .writeCell(Float.NaN) - .writeCell(faultNum) - .writeCell(actionNum) - .writeCell(twt.getLeg1() + + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell("") + .addCell(0) + .addCell(twt.getRatedU0()) + .addCell(Float.NaN) + .addCell(Float.NaN) + .addCell(faultNum) + .addCell(actionNum) + .addCell(twt.getLeg1() .getTerminal() .getVoltageLevel() .getSubstation() .flatMap(Substation::getCountry) .map(Enum::toString) .orElse("")) - .writeCell(vlId) - .writeCell(""); + .addCell(vlId) + .addCell(""); + + // Add cells if necessary + addAdditionalCellsThreeWindingsTransformerVoltageLevel(formatterHelper, twt); + + // Write the cells + formatterHelper.write(); + } + + public void addAdditionalCellsThreeWindingsTransformerVoltageLevel(TableFormatterHelper formatterHelper, + ThreeWindingsTransformer twt) { + // Nothing to do here } private void writeRatioTapChanger(TableFormatter formatter, String id, double zb2, double reactance, @@ -1360,17 +1460,30 @@ private void writeRatioTapChanger(TableFormatter formatter, String id, double zb for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) { RatioTapChangerStep step = rtc.getStep(position); double x = reactance * (1 + step.getX() / 100) / zb2; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(position - rtc.getLowTapPosition() + 1) - .writeCell(step.getRho()) - .writeCell(x) - .writeCell(0.0) - .writeCell(faultNum) - .writeCell(actionNum); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell(position - rtc.getLowTapPosition() + 1) + .addCell(step.getRho()) + .addCell(x) + .addCell(0.0) + .addCell(faultNum) + .addCell(actionNum); + + // Add cells if necessary + addAdditionalCellsRatioTapChangerStep(formatterHelper, id, zb2, reactance, rtc); + + // Write the cells + formatterHelper.write(); } } + public void addAdditionalCellsRatioTapChangerStep(TableFormatterHelper formatterHelper, + String id, double zb2, double reactance, + RatioTapChanger rtc) { + // Nothing to do here + } + private void writePhaseTapChanger(TableFormatter formatter, String id, double zb2, double reactance, PhaseTapChanger ptc) throws IOException { int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id); @@ -1378,17 +1491,30 @@ private void writePhaseTapChanger(TableFormatter formatter, String id, double zb for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { PhaseTapChangerStep step = ptc.getStep(position); double x = reactance * (1 + step.getX() / 100) / zb2; - formatter.writeCell(variantIndex) - .writeCell(num) - .writeCell(position - ptc.getLowTapPosition() + 1) - .writeCell(step.getRho()) - .writeCell(x) - .writeCell(Math.toRadians(step.getAlpha())) - .writeCell(faultNum) - .writeCell(actionNum); + TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter); + formatterHelper.addCell(variantIndex) + .addCell(num) + .addCell(position - ptc.getLowTapPosition() + 1) + .addCell(step.getRho()) + .addCell(x) + .addCell(Math.toRadians(step.getAlpha())) + .addCell(faultNum) + .addCell(actionNum); + + // Add cells if necessary + addAdditionalCellsPhaseTapChangerStep(formatterHelper, id, zb2, reactance, ptc); + + // Write the cells + formatterHelper.write(); } } + public void addAdditionalCellsPhaseTapChangerStep(TableFormatterHelper formatterHelper, + String id, double zb2, double reactance, + PhaseTapChanger ptc) { + // Nothing to do here + } + private void writeTemporaryCurrentLimits(CurrentLimits limits, TableFormatter formatter, String branchId, boolean side1, String sideId) throws IOException { int branchNum = mapper.getInt(AmplSubset.BRANCH, branchId); diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 6189dfd459e..146c0c9c6dc 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -10,14 +10,12 @@ import com.powsybl.ampl.converter.AmplConstants; import com.powsybl.ampl.converter.AmplExportConfig; import com.powsybl.ampl.converter.AmplSubset; -import com.powsybl.ampl.converter.AmplUtil; import com.powsybl.ampl.converter.util.NetworkUtil; import com.powsybl.commons.io.table.Column; import com.powsybl.commons.io.table.TableFormatter; +import com.powsybl.commons.io.table.TableFormatterHelper; import com.powsybl.commons.util.StringToIntMapper; import com.powsybl.iidm.network.*; -import com.powsybl.iidm.network.util.ConnectedComponents; -import com.powsybl.iidm.network.util.SV; import java.io.IOException; import java.util.ArrayList; @@ -88,104 +86,27 @@ public List getStaticVarCompensatorColumns() { } @Override - public void writeBusesColumnsToFormatter(TableFormatter formatter, Bus b) throws IOException { - int ccNum = ConnectedComponents.getCcNum(b); - String id = b.getId(); - VoltageLevel vl = b.getVoltageLevel(); - int num = getMapper().getInt(AmplSubset.BUS, id); - int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId()); - double nomV = vl.getNominalV(); - double v = b.getV() / nomV; - double theta = Math.toRadians(b.getAngle()); - formatter.writeCell(getVariantIndex()) - .writeCell(num) - .writeCell(vlNum) - .writeCell(ccNum) - .writeCell(v) - .writeCell(theta) - .writeCell(b.getP()) - .writeCell(b.getQ()) - .writeCell(NetworkUtil.isSlackBus(b)) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(id); + public void addAdditionalCellsBusesColumns(TableFormatterHelper formatterHelper, Bus b) { + formatterHelper.addCell(NetworkUtil.isSlackBus(b), 8); } @Override - public void writeThreeWindingsTranformersMiddleBusesColumnsToFormatter(TableFormatter formatter, - ThreeWindingsTransformer twt, - int middleCcNum) throws IOException { - String middleBusId = AmplUtil.getThreeWindingsTransformerMiddleBusId(twt); - String middleVlId = AmplUtil.getThreeWindingsTransformerMiddleVoltageLevelId(twt); - int middleBusNum = getMapper().getInt(AmplSubset.BUS, middleBusId); - int middleVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - - double v = twt.getProperty("v") == null ? Double.NaN : - Double.parseDouble(twt.getProperty("v")) / twt.getRatedU0(); - double angle = twt.getProperty("angle") == null ? Double.NaN : - Math.toRadians(Double.parseDouble(twt.getProperty("angle"))); - - formatter.writeCell(getVariantIndex()) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(v) - .writeCell(angle) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(false) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(middleBusId); + public void addAdditionalCellsThreeWindingsTranformersMiddleBusesColumns(TableFormatterHelper formatterHelper, + ThreeWindingsTransformer twt, + int middleCcNum) { + formatterHelper.addCell(false, 8); } @Override - public void writeDanglingLineMiddleBusesToFormatter(TableFormatter formatter, DanglingLine dl, - int middleCcNum) throws IOException { - Terminal t = dl.getTerminal(); - Bus b = AmplUtil.getBus(dl.getTerminal()); - String middleBusId = AmplUtil.getDanglingLineMiddleBusId(dl); - String middleVlId = AmplUtil.getDanglingLineMiddleVoltageLevelId(dl); - int middleBusNum = getMapper().getInt(AmplSubset.BUS, middleBusId); - int middleVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId); - SV sv = new SV(t.getP(), t.getQ(), b != null ? b.getV() : Double.NaN, b != null ? b.getAngle() : Double.NaN, - TwoSides.ONE).otherSide( - dl, true); - double nomV = t.getVoltageLevel().getNominalV(); - double v = sv.getU() / nomV; - double theta = Math.toRadians(sv.getA()); - formatter.writeCell(getVariantIndex()) - .writeCell(middleBusNum) - .writeCell(middleVlNum) - .writeCell(middleCcNum) - .writeCell(v) - .writeCell(theta) - .writeCell(0.0) // 0 MW injected at dangling line internal bus - .writeCell(0.0) // 0 MVar injected at dangling line internal bus - .writeCell(false) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(middleBusId); + public void addAdditionalCellsDanglingLineMiddleBuses(TableFormatterHelper formatterHelper, DanglingLine dl, + int middleCcNum) { + formatterHelper.addCell(false, 8); } @Override - public void writeTieLineMiddleBusesToFormatter(TableFormatter formatter, TieLine tieLine, - int xNodeCcNum) throws IOException { - String xNodeBusId = AmplUtil.getXnodeBusId(tieLine); - int xNodeBusNum = getMapper().getInt(AmplSubset.BUS, xNodeBusId); - int xNodeVlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine)); - formatter.writeCell(getVariantIndex()) - .writeCell(xNodeBusNum) - .writeCell(xNodeVlNum) - .writeCell(xNodeCcNum) - .writeCell(Float.NaN) - .writeCell(Double.NaN) - .writeCell(0.0) - .writeCell(0.0) - .writeCell(false) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(xNodeBusId); + public void addAdditionalCellsTieLineMiddleBuses(TableFormatterHelper formatterHelper, TieLine tieLine, + int xNodeCcNum) { + formatterHelper.addCell(false, 8); } @Override @@ -276,82 +197,21 @@ private void writeTapChanger(TableFormatter formatter, int num, int stepPosition } @Override - public void writeGeneratorToFormatter(TableFormatter formatter, Generator gen) throws IOException { - String id = gen.getId(); - Terminal t = gen.getTerminal(); - int num = getMapper().getInt(AmplSubset.GENERATOR, id); - int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - int busNum = AmplUtil.getBusNum(getMapper(), t); - int conBusNum = AmplUtil.getConnectableBusNum(getMapper(), t); - double minP = gen.getMinP(); - double maxP = gen.getMaxP(); - double vb = gen.getRegulatingTerminal().getVoltageLevel().getNominalV(); + public void addAdditionalCellsGenerator(TableFormatterHelper formatterHelper, Generator gen) { int regulatingBusNum = gen.isVoltageRegulatorOn() ? getMapper().getInt(AmplSubset.BUS, gen.getRegulatingTerminal().getBusView().getBus().getId()) : -1; - - formatter.writeCell(getVariantIndex()) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum != -1 ? conBusNum : busNum) - .writeCell(vlNum) - .writeCell(minP) - .writeCell(maxP) - .writeCell(gen.getReactiveLimits().getMinQ(maxP)) - .writeCell(gen.getReactiveLimits().getMinQ(0)) - .writeCell(gen.getReactiveLimits().getMinQ(minP)) - .writeCell(gen.getReactiveLimits().getMaxQ(maxP)) - .writeCell(gen.getReactiveLimits().getMaxQ(0)) - .writeCell(gen.getReactiveLimits().getMaxQ(minP)) - .writeCell(gen.isVoltageRegulatorOn()) - .writeCell(regulatingBusNum) - .writeCell(gen.getTargetV() / vb) - .writeCell(gen.getTargetP()) - .writeCell(gen.getTargetQ()) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(id) - .writeCell(gen.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); + formatterHelper.addCell(regulatingBusNum, 14); } @Override - public void writeStaticVarCompensatorToFormatter(TableFormatter formatter, - StaticVarCompensator svc) throws IOException { - String id = svc.getId(); - int num = getMapper().getInt(AmplSubset.STATIC_VAR_COMPENSATOR, id); - - Terminal t = svc.getTerminal(); - - int busNum = AmplUtil.getBusNum(getMapper(), t); - int conBusNum = AmplUtil.getConnectableBusNum(getMapper(), t); - + public void addAdditionalCellsStaticVarCompensator(TableFormatterHelper formatterHelper, + StaticVarCompensator svc) { boolean voltageRegulation = svc.getRegulationMode().equals(StaticVarCompensator.RegulationMode.VOLTAGE); int regulatingBusNum = voltageRegulation ? getMapper().getInt(AmplSubset.BUS, svc.getRegulatingTerminal().getBusView().getBus().getId()) : -1; - double vlSet = svc.getVoltageSetpoint(); - double vb = t.getVoltageLevel().getNominalV(); - double zb = vb * vb / AmplConstants.SB; // Base impedance - - int vlNum = getMapper().getInt(AmplSubset.VOLTAGE_LEVEL, t.getVoltageLevel().getId()); - formatter.writeCell(getVariantIndex()) - .writeCell(num) - .writeCell(busNum) - .writeCell(conBusNum) - .writeCell(vlNum) - .writeCell(svc.getBmin() * zb) - .writeCell(svc.getBmax() * zb) - .writeCell(voltageRegulation) - .writeCell(regulatingBusNum) - .writeCell(vlSet / vb) - .writeCell(svc.getReactivePowerSetpoint()) - .writeCell(getFaultNum()) - .writeCell(getActionNum()) - .writeCell(id) - .writeCell(svc.getNameOrId()) - .writeCell(t.getP()) - .writeCell(t.getQ()); + // Cell to add + formatterHelper.addCell(regulatingBusNum, 8); } } diff --git a/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java b/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java index 53f43d4f96a..c3cda7bd5f5 100644 --- a/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java +++ b/commons/src/main/java/com/powsybl/commons/io/table/TableFormatterHelper.java @@ -7,8 +7,6 @@ */ package com.powsybl.commons.io.table; -import com.powsybl.commons.PowsyblException; - import java.io.IOException; import java.util.LinkedList; import java.util.List; @@ -25,12 +23,12 @@ public TableFormatterHelper(TableFormatter tableFormatter) { this.tableFormatter = tableFormatter; } - public TableFormatterHelper add(Object object) { + public TableFormatterHelper addCell(Object object) { objectsToWrite.add(object); return this; } - public TableFormatterHelper add(Object object, int position) { + public TableFormatterHelper addCell(Object object, int position) { objectsToWrite.add(position, object); return this; } @@ -85,9 +83,8 @@ public TableFormatterHelper addComment(String comment, int position) { return this; } - public TableFormatter write() { - objectsToWrite.forEach(object -> { - try { + public TableFormatter write() throws IOException { + for (Object object : objectsToWrite) { if (object instanceof String s) { tableFormatter.writeCell(s); } else if (object instanceof Character c) { @@ -107,15 +104,13 @@ public TableFormatter write() { } else if (object instanceof EmptyLines emptyLines) { tableFormatter.writeEmptyLines(emptyLines.numberOfLines); } - } catch (IOException e) { - throw new PowsyblException(e); - } - }); + } return tableFormatter; } private static class Comment { String commentToWrite; + Comment(String commentToWrite) { this.commentToWrite = commentToWrite; } @@ -123,6 +118,7 @@ private static class Comment { private static class EmptyCells { int numberOfCells; + EmptyCells(int numberOfCells) { this.numberOfCells = numberOfCells; } @@ -130,6 +126,7 @@ private static class EmptyCells { private static class EmptyLines { int numberOfLines; + EmptyLines(int numberOfLines) { this.numberOfLines = numberOfLines; } From aafe76aa066070958bd7d6697a079c37507fd202 Mon Sep 17 00:00:00 2001 From: Nicolas Rol Date: Mon, 16 Sep 2024 09:08:39 +0200 Subject: [PATCH 62/65] use record to lower number of method parameters Signed-off-by: Nicolas Rol --- .../version/ExtendedAmplExporterV1.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index 146c0c9c6dc..acadc27a4b6 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -161,7 +161,7 @@ private void writeRatioTapChanger(TableFormatter formatter, String id, double zb for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) { RatioTapChangerStep step = rtc.getStep(position); ImpedanceAndAdmittance stepCharacteristics = new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()); - writeTapChanger(formatter, num, position, rtc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), 0); + writeTapChanger(formatter, new TapChangerParametersForWriter(num, position, rtc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), 0)); } } @@ -173,25 +173,27 @@ private void writePhaseTapChanger(TableFormatter formatter, String id, double zb for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) { PhaseTapChangerStep step = ptc.getStep(position); ImpedanceAndAdmittance stepCharacteristics = new ImpedanceAndAdmittance(step.getR(), step.getX(), step.getG(), step.getB()); - writeTapChanger(formatter, num, position, ptc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), Math.toRadians(step.getAlpha())); + writeTapChanger(formatter, new TapChangerParametersForWriter(num, position, ptc.getLowTapPosition(), zb2, transformerZandY, stepCharacteristics, step.getRho(), Math.toRadians(step.getAlpha()))); } } - private void writeTapChanger(TableFormatter formatter, int num, int stepPosition, int lowTapPosition, double zb2, - ImpedanceAndAdmittance transformer, ImpedanceAndAdmittance step, double rho, double alpha) throws IOException { - double rNorm = transformer.r * (1 + step.r / 100) / zb2; - double xNorm = transformer.x * (1 + step.x / 100) / zb2; - double gNorm = transformer.g * (1 + step.g / 100) * zb2; - double bNorm = transformer.b * (1 + step.b / 100) * zb2; + private record TapChangerParametersForWriter(int num, int stepPosition, int lowTapPosition, double zb2, + ImpedanceAndAdmittance transformer, ImpedanceAndAdmittance step, double rho, double alpha) {} + + private void writeTapChanger(TableFormatter formatter, TapChangerParametersForWriter parametersForWriter) throws IOException { + double rNorm = parametersForWriter.transformer.r * (1 + parametersForWriter.step.r / 100) / parametersForWriter.zb2; + double xNorm = parametersForWriter.transformer.x * (1 + parametersForWriter.step.x / 100) / parametersForWriter.zb2; + double gNorm = parametersForWriter.transformer.g * (1 + parametersForWriter.step.g / 100) * parametersForWriter.zb2; + double bNorm = parametersForWriter.transformer.b * (1 + parametersForWriter.step.b / 100) * parametersForWriter.zb2; formatter.writeCell(getVariantIndex()) - .writeCell(num) - .writeCell(stepPosition - lowTapPosition + 1) - .writeCell(rho) + .writeCell(parametersForWriter.num) + .writeCell(parametersForWriter.stepPosition - parametersForWriter.lowTapPosition + 1) + .writeCell(parametersForWriter.rho) .writeCell(rNorm) .writeCell(xNorm) .writeCell(gNorm) .writeCell(bNorm) - .writeCell(alpha) + .writeCell(parametersForWriter.alpha) .writeCell(getFaultNum()) .writeCell(getActionNum()); } From 015b9ca1bdcae74739d1cc51873de62d2abaee0a Mon Sep 17 00:00:00 2001 From: Olivier Perrin Date: Thu, 19 Sep 2024 17:37:33 +0200 Subject: [PATCH 63/65] Use index constants instead of their numeric values Signed-off-by: Olivier Perrin --- .../converter/version/ExtendedAmplExporterV1.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java index acadc27a4b6..4a3759758b2 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java @@ -87,26 +87,26 @@ public List getStaticVarCompensatorColumns() { @Override public void addAdditionalCellsBusesColumns(TableFormatterHelper formatterHelper, Bus b) { - formatterHelper.addCell(NetworkUtil.isSlackBus(b), 8); + formatterHelper.addCell(NetworkUtil.isSlackBus(b), SLACK_BUS_COLUMN_INDEX); } @Override public void addAdditionalCellsThreeWindingsTranformersMiddleBusesColumns(TableFormatterHelper formatterHelper, ThreeWindingsTransformer twt, int middleCcNum) { - formatterHelper.addCell(false, 8); + formatterHelper.addCell(false, SLACK_BUS_COLUMN_INDEX); } @Override public void addAdditionalCellsDanglingLineMiddleBuses(TableFormatterHelper formatterHelper, DanglingLine dl, int middleCcNum) { - formatterHelper.addCell(false, 8); + formatterHelper.addCell(false, SLACK_BUS_COLUMN_INDEX); } @Override public void addAdditionalCellsTieLineMiddleBuses(TableFormatterHelper formatterHelper, TieLine tieLine, int xNodeCcNum) { - formatterHelper.addCell(false, 8); + formatterHelper.addCell(false, SLACK_BUS_COLUMN_INDEX); } @Override @@ -178,7 +178,7 @@ private void writePhaseTapChanger(TableFormatter formatter, String id, double zb } private record TapChangerParametersForWriter(int num, int stepPosition, int lowTapPosition, double zb2, - ImpedanceAndAdmittance transformer, ImpedanceAndAdmittance step, double rho, double alpha) {} + ImpedanceAndAdmittance transformer, ImpedanceAndAdmittance step, double rho, double alpha) { } private void writeTapChanger(TableFormatter formatter, TapChangerParametersForWriter parametersForWriter) throws IOException { double rNorm = parametersForWriter.transformer.r * (1 + parametersForWriter.step.r / 100) / parametersForWriter.zb2; @@ -202,7 +202,7 @@ private void writeTapChanger(TableFormatter formatter, TapChangerParametersForWr public void addAdditionalCellsGenerator(TableFormatterHelper formatterHelper, Generator gen) { int regulatingBusNum = gen.isVoltageRegulatorOn() ? getMapper().getInt(AmplSubset.BUS, gen.getRegulatingTerminal().getBusView().getBus().getId()) : -1; - formatterHelper.addCell(regulatingBusNum, 14); + formatterHelper.addCell(regulatingBusNum, GENERATOR_V_REGUL_BUS_COLUMN_INDEX); } @Override @@ -213,7 +213,7 @@ public void addAdditionalCellsStaticVarCompensator(TableFormatterHelper formatte getMapper().getInt(AmplSubset.BUS, svc.getRegulatingTerminal().getBusView().getBus().getId()) : -1; // Cell to add - formatterHelper.addCell(regulatingBusNum, 8); + formatterHelper.addCell(regulatingBusNum, STATIC_VAR_COMPENSATOR_V_REGUL_BUS_COLUMN_INDEX); } } From 7d60ce76c0991dcde58ae9a6f9ce902280eda204 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Fri, 20 Sep 2024 17:34:22 +0200 Subject: [PATCH 64/65] Rename ExtendedAmplExporter Signed-off-by: p-arvy --- .../ampl/converter/version/AmplExportVersion.java | 2 +- ...edAmplExporterV1.java => ExtendedAmplExporter.java} | 10 +++++----- ...porterV1Test.java => ExtendedAmplExporterTest.java} | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) rename ampl-converter/src/main/java/com/powsybl/ampl/converter/version/{ExtendedAmplExporterV1.java => ExtendedAmplExporter.java} (96%) rename ampl-converter/src/test/java/com/powsybl/ampl/converter/{ExtendedAmplExporterV1Test.java => ExtendedAmplExporterTest.java} (99%) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java index 8e8522729e4..17b385463eb 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/AmplExportVersion.java @@ -25,7 +25,7 @@ public enum AmplExportVersion { V1_0("1.0", BasicAmplExporter::new), - V1_1("1.1", ExtendedAmplExporterV1::new); + V1_1("1.1", ExtendedAmplExporter::new); public interface Factory { AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper mapper, diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java similarity index 96% rename from ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java rename to ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java index 4a3759758b2..6d1996a01f6 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporterV1.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java @@ -33,7 +33,7 @@ * @author Nicolas PIERRE {@literal } * @author Pierre ARVY {@literal } */ -public class ExtendedAmplExporterV1 extends BasicAmplExporter { +public class ExtendedAmplExporter extends BasicAmplExporter { private static final int SLACK_BUS_COLUMN_INDEX = 8; private static final int TAP_CHANGER_R_COLUMN_INDEX = 4; @@ -42,10 +42,10 @@ public class ExtendedAmplExporterV1 extends BasicAmplExporter { private static final int GENERATOR_V_REGUL_BUS_COLUMN_INDEX = 14; private static final int STATIC_VAR_COMPENSATOR_V_REGUL_BUS_COLUMN_INDEX = 8; - public ExtendedAmplExporterV1(AmplExportConfig config, - Network network, - StringToIntMapper mapper, - int variantIndex, int faultNum, int actionNum) { + public ExtendedAmplExporter(AmplExportConfig config, + Network network, + StringToIntMapper mapper, + int variantIndex, int faultNum, int actionNum) { super(config, network, mapper, variantIndex, faultNum, actionNum); } diff --git a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java similarity index 99% rename from ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java rename to ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java index a62436938df..505dbe22017 100644 --- a/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterV1Test.java +++ b/ampl-converter/src/test/java/com/powsybl/ampl/converter/ExtendedAmplExporterTest.java @@ -29,7 +29,7 @@ * @author Nicolas PIERRE {@literal } * @author Pierre ARVY {@literal } */ -class ExtendedAmplExporterV1Test extends AbstractSerDeTest { +class ExtendedAmplExporterTest extends AbstractSerDeTest { MemDataSource dataSource; AmplExporter exporter; From 6378899e088e55bc39b8688d983c0795136dbbad Mon Sep 17 00:00:00 2001 From: p-arvy Date: Fri, 20 Sep 2024 17:47:19 +0200 Subject: [PATCH 65/65] Modify extended ampl exporter javadoc Signed-off-by: p-arvy --- .../powsybl/ampl/converter/version/ExtendedAmplExporter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java index 6d1996a01f6..52ed1042170 100644 --- a/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java +++ b/ampl-converter/src/main/java/com/powsybl/ampl/converter/version/ExtendedAmplExporter.java @@ -24,7 +24,7 @@ import static com.powsybl.ampl.converter.AmplConstants.*; /** - * 1st (V1) extension of BasicAmplExporter, associated with AMPL version 1.1 (exporter id). + * 1st extension of BasicAmplExporter, associated with AMPL version 1.1 (exporter id). * The extension adds: * - A slack bus boolean in the bus table. * - R, G and B characteristics in tap tables.