From 23ee8800b426bb6dbd036356cbeaadc08bb7dc8a Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 19 Dec 2024 14:10:53 +0100 Subject: [PATCH] Add fix to Matpower importer to import some pglib files Signed-off-by: Geoffroy Jamgotchian --- .../matpower/model/MatpowerReader.java | 52 ++++++++++++------- .../matpower/model/MatpowerWriter.java | 2 +- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerReader.java b/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerReader.java index e7aef01bb9f..a8c44513f0b 100644 --- a/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerReader.java +++ b/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerReader.java @@ -8,6 +8,8 @@ package com.powsybl.matpower.model; import com.google.common.collect.Sets; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import us.hebi.matlab.mat.format.Mat5; import us.hebi.matlab.mat.types.*; @@ -24,10 +26,13 @@ */ public final class MatpowerReader { + private static final Logger LOGGER = LoggerFactory.getLogger(MatpowerReader.class); + public static final String MATPOWER_STRUCT_NAME = "mpc"; public static final String MATPOWER_SUPPORTED_VERSION = "2"; public static final int MATPOWER_BUSES_COLUMNS = 13; - public static final int MATPOWER_GENERATORS_COLUMNS = 21; + public static final int MATPOWER_V1_GENERATORS_COLUMNS = 10; + public static final int MATPOWER_V2_GENERATORS_COLUMNS = 21; public static final int MATPOWER_BRANCHES_COLUMNS = 13; public static final int MATPOWER_DCLINES_COLUMNS = 17; @@ -88,20 +93,27 @@ public static MatpowerModel read(InputStream iStream, String caseName) throws IO } private static void checkNumberOfColumns(Matrix buses, Matrix generators, Matrix branches, Matrix dcLines) { - if (buses.getDimensions()[1] < MATPOWER_BUSES_COLUMNS - || generators.getDimensions()[1] < MATPOWER_GENERATORS_COLUMNS - || branches.getDimensions()[1] < MATPOWER_BRANCHES_COLUMNS + int busColumns = buses.getDimensions()[1]; + int generatorColumns = generators.getDimensions()[1]; + int branchColumns = branches.getDimensions()[1]; + if (generatorColumns == MATPOWER_V1_GENERATORS_COLUMNS) { + LOGGER.warn("It is not expected in Matpower v2 format to have {} columns for generators. {} columns are only expected for the v1 format and v2 requires {} columns", + MATPOWER_V1_GENERATORS_COLUMNS, MATPOWER_V1_GENERATORS_COLUMNS, MATPOWER_V2_GENERATORS_COLUMNS); + } + if (busColumns < MATPOWER_BUSES_COLUMNS + || generatorColumns < MATPOWER_V1_GENERATORS_COLUMNS + || branchColumns < MATPOWER_BRANCHES_COLUMNS || dcLines != null && dcLines.getDimensions()[1] < MATPOWER_DCLINES_COLUMNS) { String exceptionMessage; if (dcLines == null) { exceptionMessage = String.format("Unexpected number of columns. Expected: Buses %d Generators %d Branches %d Received: Buses %d Generators %d Branches %d", - MATPOWER_BUSES_COLUMNS, MATPOWER_GENERATORS_COLUMNS, MATPOWER_BRANCHES_COLUMNS, - buses.getDimensions()[1], generators.getDimensions()[1], branches.getDimensions()[1]); + MATPOWER_BUSES_COLUMNS, MATPOWER_V1_GENERATORS_COLUMNS, MATPOWER_BRANCHES_COLUMNS, + busColumns, generatorColumns, branchColumns); } else { exceptionMessage = String.format("Unexpected number of columns. Expected: Buses %d Generators %d Branches %d DcLines %d Received: Buses %d Generators %d Branches %d DcLines %d", - MATPOWER_BUSES_COLUMNS, MATPOWER_GENERATORS_COLUMNS, MATPOWER_BRANCHES_COLUMNS, MATPOWER_DCLINES_COLUMNS, - buses.getDimensions()[1], generators.getDimensions()[1], branches.getDimensions()[1], dcLines.getDimensions()[1]); + MATPOWER_BUSES_COLUMNS, MATPOWER_V1_GENERATORS_COLUMNS, MATPOWER_BRANCHES_COLUMNS, MATPOWER_DCLINES_COLUMNS, + busColumns, generatorColumns, branchColumns, dcLines.getDimensions()[1]); } throw new IllegalStateException(exceptionMessage); } @@ -145,17 +157,19 @@ private static void readGenerators(Matrix generators, MatpowerModel model) { gen.setStatus(generators.getInt(row, 7)); gen.setMaximumRealPowerOutput(generators.getDouble(row, 8)); gen.setMinimumRealPowerOutput(generators.getDouble(row, 9)); - gen.setPc1(generators.getDouble(row, 10)); - gen.setPc2(generators.getDouble(row, 11)); - gen.setQc1Min(generators.getDouble(row, 12)); - gen.setQc1Max(generators.getDouble(row, 13)); - gen.setQc2Min(generators.getDouble(row, 14)); - gen.setQc2Max(generators.getDouble(row, 15)); - gen.setRampAgc(generators.getDouble(row, 16)); - gen.setRampTenMinutes(generators.getDouble(row, 17)); - gen.setRampThirtyMinutes(generators.getDouble(row, 18)); - gen.setRampQ(generators.getDouble(row, 19)); - gen.setApf(generators.getDouble(row, 20)); + if (generators.getDimensions()[1] > MATPOWER_V1_GENERATORS_COLUMNS) { + gen.setPc1(generators.getDouble(row, 10)); + gen.setPc2(generators.getDouble(row, 11)); + gen.setQc1Min(generators.getDouble(row, 12)); + gen.setQc1Max(generators.getDouble(row, 13)); + gen.setQc2Min(generators.getDouble(row, 14)); + gen.setQc2Max(generators.getDouble(row, 15)); + gen.setRampAgc(generators.getDouble(row, 16)); + gen.setRampTenMinutes(generators.getDouble(row, 17)); + gen.setRampThirtyMinutes(generators.getDouble(row, 18)); + gen.setRampQ(generators.getDouble(row, 19)); + gen.setApf(generators.getDouble(row, 20)); + } model.addGenerator(gen); } diff --git a/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerWriter.java b/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerWriter.java index e50d5d7bd61..6480dd2be16 100644 --- a/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerWriter.java +++ b/matpower/matpower-model/src/main/java/com/powsybl/matpower/model/MatpowerWriter.java @@ -96,7 +96,7 @@ private static Cell fillBusesNames(List buses, boolean withBusNames) { } private static Matrix fillGeneratorsMatrix(List gens) { - Matrix gensM = Mat5.newMatrix(gens.size(), MatpowerReader.MATPOWER_GENERATORS_COLUMNS); + Matrix gensM = Mat5.newMatrix(gens.size(), MatpowerReader.MATPOWER_V2_GENERATORS_COLUMNS); for (int row = 0; row < gens.size(); row++) { gensM.setInt(row, 0, gens.get(row).getNumber());