Skip to content

Commit

Permalink
remove legacy extension, adapt unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Luma <zamarrenolm@aia.es>
  • Loading branch information
zamarrenolm committed Dec 13, 2024
1 parent f2a31ac commit c081ba2
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 743 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1385,29 +1385,29 @@ private static void writeAcdcConverterDCTerminal(String id, String conductingEqu
private static void writeControlAreas(String energyAreaId, Network network, String cimNamespace, String euNamespace, XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException {
for (Area area : network.getAreas()) {
if (area.getAreaType().equals("ControlAreaTypeKind.Interchange")) {
writeControlArea(area, energyAreaId, cimNamespace, euNamespace, writer, context, network);
writeControlArea(area, energyAreaId, cimNamespace, euNamespace, writer, context);
}
}
}

private static void writeControlArea(Area controlArea, String energyAreaId, String cimNamespace, String euNamespace,
XMLStreamWriter writer, CgmesExportContext context, Network network) throws XMLStreamException {
XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException {
// Original control area identifiers may not respect mRID rules, so we pass it through naming strategy
// to obtain always valid mRID identifiers
String controlAreaCgmesId = context.getNamingStrategy().getCgmesId(controlArea.getId());
String energyIdentCodeEic = controlArea.getAliasFromType(CgmesNames.ENERGY_IDENT_CODE_EIC).orElse("");
ControlAreaEq.write(controlAreaCgmesId, controlArea.getNameOrId(), energyIdentCodeEic, energyAreaId, cimNamespace, euNamespace, writer, context);
for (AreaBoundary areaBoundary : controlArea.getAreaBoundaries()) {
TieFlow.from(areaBoundary, context, network).ifPresent(tieFlow ->
TieFlow.from(areaBoundary, context).ifPresent(tieFlow ->
TieFlowEq.write(tieFlow.id(), controlAreaCgmesId, tieFlow.terminalId(), cimNamespace, writer, context)
);
}
}

private record TieFlow(String id, String terminalId) {
static Optional<TieFlow> from(AreaBoundary areaBoundary, CgmesExportContext context, Network network) {
static Optional<TieFlow> from(AreaBoundary areaBoundary, CgmesExportContext context) {
return areaBoundary.getTerminal().map(terminal -> from(terminal, context))
.orElse(areaBoundary.getBoundary().flatMap(boundary -> from(boundary, context, network)));
.orElse(areaBoundary.getBoundary().flatMap(boundary -> from(boundary, context)));
}

static Optional<TieFlow> from(Terminal terminal, CgmesExportContext context) {
Expand All @@ -1416,8 +1416,8 @@ static Optional<TieFlow> from(Terminal terminal, CgmesExportContext context) {
CgmesExportUtil.getTerminalId(terminal, context)));
}

static Optional<TieFlow> from(Boundary boundary, CgmesExportContext context, Network network) {
String terminalId = getTieFlowBoundaryTerminal(boundary, context, network);
static Optional<TieFlow> from(Boundary boundary, CgmesExportContext context) {
String terminalId = getTieFlowBoundaryTerminal(boundary, context);
if (terminalId != null) {
return Optional.of(new TieFlow(
context.getNamingStrategy().getCgmesId(ref(terminalId), TIE_FLOW),
Expand All @@ -1428,34 +1428,14 @@ static Optional<TieFlow> from(Boundary boundary, CgmesExportContext context, Net
}
}

private static String getTieFlowBoundaryTerminal(Boundary boundary, CgmesExportContext context, Network network) {
private static String getTieFlowBoundaryTerminal(Boundary boundary, CgmesExportContext context) {
DanglingLine dl = boundary.getDanglingLine();
if (network.isBoundaryElement(dl)) {
return context.getNamingStrategy().getCgmesIdFromAlias(dl, Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + TERMINAL_BOUNDARY);
} else {
// This means the boundary corresponds to a TieLine.
// Because the network should not be a merging view,
// the only way to have a TieLine in the model is that
// the original data for the network contained both halves of the TieLine.
// That is, the initial CGMES data contains the two ACLSs at each side of one boundary point.

// Currently, we are exporting TieLines in the EQ as a single ACLS,
// We are not exporting the individual halves of the tie line as separate equipment.
// So we do not have terminals for the boundary points.

// This error should be fixed exporting the two halves of the TieLine to the EQ,
// with their corresponding terminals.
// Also, the boundary node should not be exported but referenced,
// as it should be defined in the boundary, not in the instance EQ file.

LOG.error("Unsupported tie flow at TieLine boundary {}", dl.getId());
return null;
}
return context.getNamingStrategy().getCgmesIdFromAlias(dl, Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + TERMINAL_BOUNDARY);
}

private static void writeTerminals(Network network, Map<Terminal, String> mapTerminal2Id, Map<String, String> mapNodeKey2NodeId,
String cimNamespace, XMLStreamWriter writer, CgmesExportContext context) throws XMLStreamException {
for (Connectable<?> c : network.getConnectables()) { // TODO write boundary terminals for tie lines from CGMES
for (Connectable<?> c : network.getConnectables()) {
if (context.isExportedEquipment(c)) {
for (Terminal t : c.getTerminals()) {
writeTerminal(t, mapTerminal2Id, mapNodeKey2NodeId, cimNamespace, writer, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.powsybl.cgmes.conversion.CgmesModelExtension;
import com.powsybl.cgmes.conversion.Conversion;
import com.powsybl.cgmes.conversion.test.ConversionUtil;
import com.powsybl.cgmes.extensions.CgmesControlAreas;
import com.powsybl.cgmes.extensions.CgmesMetadataModels;
import com.powsybl.cgmes.model.*;
import com.powsybl.commons.PowsyblException;
Expand Down Expand Up @@ -862,7 +861,7 @@ void miniNodeBreakerMissingSubstationRegion() {
@Test
void smallBusBranchTieFlowWithoutControlArea() {
Network network = new CgmesImport().importData(CgmesConformity1ModifiedCatalog.smallBusBranchTieFlowsWithoutControlArea().dataSource(), NetworkFactory.findDefault(), importParams);
assertNull(network.getExtension(CgmesControlAreas.class));
assertEquals(0, network.getAreaCount());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,22 @@ void microGridBaseCaseAssembledSwitchAtBoundary() throws XMLStreamException, IOE
ReadOnlyDataSource dataSource = CgmesConformity1ModifiedCatalog.microGridBaseCaseAssembledSwitchAtBoundary().dataSource();
Network network = new CgmesImport().importData(dataSource, NetworkFactory.findDefault(), importParams);

network.newExtension(CgmesControlAreasAdder.class).add();
CgmesControlAreas cgmesControlAreas = network.getExtension(CgmesControlAreas.class);
CgmesControlArea cgmesControlArea = cgmesControlAreas.newCgmesControlArea()
// Define a tie flow at the boundary of a dangling line
TieLine tieLine = network.getTieLine("78736387-5f60-4832-b3fe-d50daf81b0a6 + 7f43f508-2496-4b64-9146-0a40406cbe49");
Area area = network.newArea()
.setId("controlAreaId")
.setName("controlAreaName")
.setEnergyIdentificationCodeEic("energyIdentCodeEic")
.setNetInterchange(Double.NaN)
.setAreaType("ControlAreaTypeKind.Interchange")
.setInterchangeTarget(Double.NaN)
.addAreaBoundary(tieLine.getDanglingLine2().getBoundary(), true)
.add();
TieLine tieLine = network.getTieLine("78736387-5f60-4832-b3fe-d50daf81b0a6 + 7f43f508-2496-4b64-9146-0a40406cbe49");
cgmesControlArea.add(tieLine.getDanglingLine2().getBoundary());
area.addAlias("energyIdentCodeEic", CgmesNames.ENERGY_IDENT_CODE_EIC);

// TODO(Luma) updated expected result after halves of tie lines are exported as equipment
// instead of an error logged and the tie flow ignored,
// the reimported network control area should contain one tie flow
Network actual = exportImportNodeBreaker(network, dataSource);
// The reimported network control area should contain one tie flow
Network actual = exportImportBusBranch(network, dataSource);
Area actualControlArea = actual.getArea("controlAreaId");
boolean tieFlowsAtTieLinesAreSupported = false;
if (tieFlowsAtTieLinesAreSupported) {
assertEquals(1, actualControlArea.getAreaBoundaryStream().count());
assertEquals("7f43f508-2496-4b64-9146-0a40406cbe49", actualControlArea.getAreaBoundaries().iterator().next().getBoundary().get().getDanglingLine().getId());
} else {
assertNull(actualControlArea);
}
assertEquals(1, actualControlArea.getAreaBoundaryStream().count());
assertEquals("7f43f508-2496-4b64-9146-0a40406cbe49", actualControlArea.getAreaBoundaries().iterator().next().getBoundary().get().getDanglingLine().getId());
}

@Test
Expand Down Expand Up @@ -593,7 +586,7 @@ void microGridCgmesExportPreservingOriginalClassesOfLoads() throws IOException,
// Remove the default control area created in expected network during export
expected.getAreaStream().map(Area::getId).toList().forEach(a -> expected.getArea(a).remove());

// Avoid comparing targetP and targetQ (reimport does not consider the SSH file);
// Avoid comparing targetP and targetQ, as reimport does not consider the SSH file
expected.getGenerators().forEach(expectedGenerator -> {
Generator actualGenerator = actual.getGenerator(expectedGenerator.getId());
actualGenerator.setTargetP(expectedGenerator.getTargetP());
Expand Down Expand Up @@ -671,7 +664,7 @@ void miniGridCgmesExportPreservingOriginalClassesOfGenerators() throws IOExcepti
assertEquals(generatorsCreatedFromOriginalClassCount(expected, "ExternalNetworkInjection"), generatorsCreatedFromOriginalClassCount(actual, "ExternalNetworkInjection"));
assertEquals(generatorsCreatedFromOriginalClassCount(expected, "EquivalentInjection"), generatorsCreatedFromOriginalClassCount(actual, "EquivalentInjection"));

// Avoid comparing targetP and targetQ (reimport does not consider the SSH file);
// Avoid comparing targetP and targetQ, as reimport does not consider the SSH file
expected.getGenerators().forEach(expectedGenerator -> {
Generator actualGenerator = actual.getGenerator(expectedGenerator.getId());
actualGenerator.setTargetP(expectedGenerator.getTargetP());
Expand Down Expand Up @@ -1664,6 +1657,7 @@ private Path exportToCgmesEQ(Network network) throws IOException, XMLStreamExcep

private Path exportToCgmesEQ(Network network, boolean transformersWithHighestVoltageAtEnd1) throws IOException, XMLStreamException {
// Export CGMES EQ file
tmpDir = Path.of("/Users/zamarrenolm/Downloads");
Path exportedEq = tmpDir.resolve("exportedEq.xml");
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(exportedEq))) {
XMLStreamWriter writer = XmlUtil.initializeWriter(true, " ", os);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit c081ba2

Please sign in to comment.