Skip to content

Commit

Permalink
Add version 1.2 of AMPL exporter (#3220)
Browse files Browse the repository at this point in the history
* Add condenser column in generator file of extended ampl exporter
* Add target Q for LCC converter stations
* Add new version of extended ampl exporter
* Remove public attribut to AbstractAmplExporterTest
* Add columns to export active set points (targetP/ac emulation) of hvdc converter stations
* Add columns for AC emulation and targetP in VSC table
* Move AC emulation parameters from VSC table to HVDC lines table
* Fix Q0 unit in batteries/lcc columns

Signed-off-by: p-arvy <[email protected]>
  • Loading branch information
p-arvy authored Dec 3, 2024
1 parent 0974696 commit 218eb50
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private AmplConstants() {
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 Q0 = "q0 (MVar)";
public static final String ID = "id";
// End column headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
public enum AmplExportVersion {

V1_0("1.0", BasicAmplExporter::new),
V1_1("1.1", ExtendedAmplExporter::new);
V1_1("1.1", ExtendedAmplExporter::new),
V1_2("1.2", ExtendedAmplExporterV2::new);

public interface Factory {
AmplColumnsExporter create(AmplExportConfig config, Network network, StringToIntMapper<AmplSubset> mapper,
Expand Down Expand Up @@ -64,6 +65,6 @@ public static AmplExportVersion fromExporterId(String exporterId) {
}

public static AmplExportVersion defaultVersion() {
return V1_1;
return V1_2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public List<Column> getBatteriesColumns() {
new Column(CON_BUS),
new Column(SUBSTATION),
new Column(P0),
new Column(Q0),
new Column("q0 (MW)"), // Wrong unit, fixed in v1.2
new Column(MINP),
new Column(MAXP),
new Column(MIN_Q_MAX_P),
Expand Down Expand Up @@ -446,20 +446,32 @@ public void writeHvdcToFormatter(TableFormatter formatter, HvdcLine hvdcLine) th
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());
TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter);
formatterHelper.addCell(variantIndex)
.addCell(num)
.addCell(type.equals(HvdcConverterStation.HvdcType.VSC) ? 1 : 2)
.addCell(mapper.getInt(subset, hvdcLine.getConverterStation1().getId()))
.addCell(mapper.getInt(subset, hvdcLine.getConverterStation2().getId()))
.addCell(hvdcLine.getR())
.addCell(hvdcLine.getNominalV())
.addCell(hvdcLine.getConvertersMode().name())
.addCell(hvdcLine.getActivePowerSetpoint())
.addCell(hvdcLine.getMaxP())
.addCell(faultNum)
.addCell(actionNum)
.addCell(id)
.addCell(hvdcLine.getNameOrId());

// Add cells if necessary
addAdditionalCellsHvdcLine(formatterHelper, hvdcLine);

// Write the cells
formatterHelper.write();
}

public void addAdditionalCellsHvdcLine(TableFormatterHelper formatterHelper,
HvdcLine hvdcLine) {
// Nothing to do here
}

@Override
Expand All @@ -473,19 +485,31 @@ public void writeLccConverterStationToFormatter(TableFormatter formatter,

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());
TableFormatterHelper formatterHelper = new TableFormatterHelper(formatter);
formatterHelper.addCell(variantIndex)
.addCell(num)
.addCell(busNum)
.addCell(conBusNum != -1 ? conBusNum : busNum)
.addCell(vlNum)
.addCell(lccStation.getLossFactor())
.addCell(lccStation.getPowerFactor())
.addCell(faultNum)
.addCell(actionNum)
.addCell(lccStation.getId())
.addCell(lccStation.getNameOrId())
.addCell(t.getP())
.addCell(t.getQ());

// Add cells if necessary
addAdditionalCellsLccConverterStation(formatterHelper, lccStation);

// Write the cells
formatterHelper.write();
}

public void addAdditionalCellsLccConverterStation(TableFormatterHelper formatterHelper,
LccConverterStation lccStation) {
// Nothing to do here
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* 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.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.TableFormatterHelper;
import com.powsybl.commons.util.StringToIntMapper;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControl;
import com.powsybl.iidm.network.util.HvdcUtils;

import java.util.ArrayList;
import java.util.List;

import static com.powsybl.ampl.converter.AmplConstants.*;

/**
* @author Pierre ARVY {@literal <pierre.arvy at artelys.com>}
*/
public class ExtendedAmplExporterV2 extends ExtendedAmplExporter {

private static final int BATTERY_Q0_COLUMN_INDEX = 6;
private static final int GENERATOR_IS_CONDENSER_COLUMN_INDEX = 16;
private static final int LCC_TARGET_Q_COLUMN_INDEX = 5;
private static final int HVDC_AC_EMULATION_COLUMN_INDEX = 8;
private static final int HVDC_P_OFFSET_COLUMN_INDEX = 10;
private static final int HVDC_K_COLUMN_INDEX = 11;

public ExtendedAmplExporterV2(AmplExportConfig config,
Network network,
StringToIntMapper<AmplSubset> mapper,
int variantIndex, int faultNum, int actionNum) {
super(config, network, mapper, variantIndex, faultNum, actionNum);
}

@Override
public List<Column> getBatteriesColumns() {
List<Column> batteriesColumns = new ArrayList<>(super.getBatteriesColumns());
// fix unit of q0 column
batteriesColumns.set(BATTERY_Q0_COLUMN_INDEX, new Column(Q0));
return batteriesColumns;
}

@Override
public List<Column> getGeneratorsColumns() {
List<Column> generatorsColumns = new ArrayList<>(super.getGeneratorsColumns());
// add column to indicate if generator is a condenser
generatorsColumns.add(GENERATOR_IS_CONDENSER_COLUMN_INDEX, new Column("condenser"));
return generatorsColumns;
}

@Override
public List<Column> getLccConverterStationsColumns() {
List<Column> lccColumns = new ArrayList<>(super.getLccConverterStationsColumns());
// add columns for load target Q of converter station
lccColumns.add(LCC_TARGET_Q_COLUMN_INDEX, new Column(Q0));
return lccColumns;
}

@Override
public List<Column> getHvdcLinesColumns() {
List<Column> hvdcColumns = new ArrayList<>(super.getHvdcLinesColumns());
// add columns for AC emulation
hvdcColumns.add(HVDC_AC_EMULATION_COLUMN_INDEX, new Column("ac emul."));
hvdcColumns.add(HVDC_P_OFFSET_COLUMN_INDEX, new Column("P offset (MW)"));
hvdcColumns.add(HVDC_K_COLUMN_INDEX, new Column("k (MW/rad)"));
return hvdcColumns;
}

@Override
public void addAdditionalCellsGenerator(TableFormatterHelper formatterHelper, Generator gen) {
super.addAdditionalCellsGenerator(formatterHelper, gen);
formatterHelper.addCell(gen.isCondenser(), GENERATOR_IS_CONDENSER_COLUMN_INDEX);
}

@Override
public void addAdditionalCellsLccConverterStation(TableFormatterHelper formatterHelper,
LccConverterStation lccStation) {
double loadTargetQ = HvdcUtils.getLccConverterStationLoadTargetQ(lccStation);
formatterHelper.addCell(loadTargetQ, LCC_TARGET_Q_COLUMN_INDEX);
}

@Override
public void addAdditionalCellsHvdcLine(TableFormatterHelper formatterHelper,
HvdcLine hvdcLine) {
boolean isEnabled = false;
double p0 = Double.NaN;
double k = Double.NaN;
HvdcAngleDroopActivePowerControl droopControl = hvdcLine.getExtension(HvdcAngleDroopActivePowerControl.class);
if (droopControl != null) {
isEnabled = droopControl.isEnabled();
p0 = droopControl.getP0();
k = droopControl.getDroop() * 180 / Math.PI; // export MW/rad as voltage angles are exported in rad
}
formatterHelper.addCell(isEnabled, HVDC_AC_EMULATION_COLUMN_INDEX);
formatterHelper.addCell(p0, HVDC_P_OFFSET_COLUMN_INDEX);
formatterHelper.addCell(k, HVDC_K_COLUMN_INDEX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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.ampl.converter;

import com.powsybl.commons.datasource.MemDataSource;
import com.powsybl.commons.test.AbstractSerDeTest;
import org.junit.jupiter.api.BeforeEach;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
* @author Pierre ARVY {@literal <pierre.arvy at artelys.com>}
*/
abstract class AbstractAmplExporterTest extends AbstractSerDeTest {

MemDataSource dataSource;
AmplExporter exporter;
Properties properties;

protected 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();
dataSource = new MemDataSource();
exporter = new AmplExporter();
properties = new Properties();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,27 @@
*/
package com.powsybl.ampl.converter;

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;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
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);
}
}
class AmplNetworkWriterTest extends AbstractAmplExporterTest {

@Override
@BeforeEach
public void setUp() throws IOException {
super.setUp();
properties = new Properties();
properties.put("iidm.export.ampl.export-version", "1.0");
}

Expand Down Expand Up @@ -274,7 +261,7 @@ void writeHeadersWithUnknownVersion() {

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, 1.1]"));
assertTrue(e.getMessage().contains("Value V1_0 of parameter iidm.export.ampl.export-version is not contained in possible values [1.0, 1.1, 1.2]"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,25 @@
*/
package com.powsybl.ampl.converter;

import com.powsybl.commons.datasource.MemDataSource;
import com.powsybl.commons.test.AbstractSerDeTest;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.SlackTerminalAdder;
import com.powsybl.iidm.network.test.*;
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;

/**
* @author Nicolas PIERRE {@literal <nicolas.pierre at artelys.com>}
* @author Pierre ARVY {@literal <pierre.arvy at artelys.com>}
*/
class ExtendedAmplExporterTest extends AbstractSerDeTest {

MemDataSource dataSource;
AmplExporter exporter;
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);
}
}
class ExtendedAmplExporterTest extends AbstractAmplExporterTest {

@Override
@BeforeEach
public void setUp() throws IOException {
super.setUp();
dataSource = new MemDataSource();
exporter = new AmplExporter();
properties = new Properties();
properties.put("iidm.export.ampl.export-version", "1.1");
}

@Test
Expand Down
Loading

0 comments on commit 218eb50

Please sign in to comment.