Skip to content

Commit

Permalink
CGMES post-processor to remove grounds (#2877)
Browse files Browse the repository at this point in the history
Signed-off-by: Luma <zamarrenolm@aia.es>
  • Loading branch information
zamarrenolm authored Jan 29, 2024
1 parent ee2d2a8 commit 7beecd6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cgmes/cgmes-conversion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>powsybl-iidm-extensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-iidm-modification</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.gdata</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.cgmes.conversion;

import com.google.auto.service.AutoService;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.iidm.modification.topology.RemoveFeederBay;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Network;
import com.powsybl.triplestore.api.TripleStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

/**
* @author Luma Zamarreño {@literal <zamarrenolm@aia.es>}
*/
@AutoService(CgmesImportPostProcessor.class)
public class RemoveGroundsPostProcessor implements CgmesImportPostProcessor {

public static final String NAME = "RemoveGrounds";
private static final Logger LOG = LoggerFactory.getLogger(RemoveGroundsPostProcessor.class);

public RemoveGroundsPostProcessor() {
this(PlatformConfig.defaultConfig());
}

public RemoveGroundsPostProcessor(PlatformConfig platformConfig) {
Objects.requireNonNull(platformConfig);
}

@Override
public String getName() {
return NAME;
}

@Override
public void process(Network network, TripleStore tripleStore) {
Objects.requireNonNull(network);
LOG.info("Execute {} post processor on network {}", getName(), network.getId());
List<String> grounds = network.getGroundStream().map(Identifiable::getId).toList();
grounds.forEach(g -> new RemoveFeederBay(g).apply(network));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@

package com.powsybl.cgmes.conversion.test;

import com.powsybl.cgmes.conversion.CgmesImport;
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.commons.test.AbstractSerDeTest;
import com.powsybl.iidm.network.Ground;
import com.powsybl.iidm.network.Network;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author Sophie Frasnedo {@literal <sophie.frasnedo at rte-france.com>}
*/

class GroundConversionTest {
class GroundConversionTest extends AbstractSerDeTest {

@Test
void groundConversionTest() {
Expand All @@ -40,4 +49,56 @@ void groundConversionTest() {
assertTrue(groundCV.getTerminal().isConnected());
assertEquals("S", groundCV.getTerminal().getVoltageLevel().getId());
}

@Test
void groundConversionRemoveTest() {
Properties importParams = new Properties();
importParams.put(CgmesImport.POST_PROCESSORS, "RemoveGrounds");
Network network = Network.read(
new ResourceDataSource("groundTest.xml", new ResourceSet("/", "groundTest.xml")),
importParams);

assertEquals(0, network.getGroundCount());

// Check also the exported GraphViz
// Some edges have been removed, ensure it is exported properly
String actual = graphVizClean(graphViz(network, "S"));
String expected = graphVizClean("""
digraph G {
\tnode [shape=box];
\tcompound=true;
\tn0 [label="0",shape="ellipse",style="filled",fillcolor="#8F7AF3"];
\tn2 [label="5\\lBUSBAR_SECTION\\lAX\\lEF",shape="ellipse",style="filled",fillcolor="#8F7AF3"];
\tn3 [label="8\\lGENERATOR\\lZX\\lZY",shape="ellipse",style="filled",fillcolor="#8F7AF3"];
\tn2 -> n0 [];
\tn3 -> n0 [];
\tsubgraph cluster_c1 {
\t\t// scope=1392570698
\t\tcluster_c1 [label="",shape=point,style=invis];
\t\tpencolor="transparent";
\t\tn0;
\t\tn2;
\t\tn3;
\t}
}
""");
assertEquals(expected, actual);
}

private String graphViz(Network network, String voltageLevelId) {
try {
Path gv = tmpDir.resolve(voltageLevelId + ".gv");
network.getVoltageLevel(voltageLevelId).exportTopology(gv);
return Files.readString(gv);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private String graphVizClean(String gv) {
// Remove all colors (they are assigned randomly each time a graphviz is exported)
String r = gv.replaceAll("color=\"#[^\"]+\"", "color=\"---\"");
// Remove all comments
return r.replaceAll("([\\n\\r])(\\s+)//.*([\\n\\r])", "$1$2//$3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,8 @@ private void exportNodes(Random random, GraphVizGraph gvGraph, GraphVizScope sco
}

private void exportEdges(GraphVizGraph gvGraph, GraphVizScope scope) {
for (int e = 0; e < graph.getEdgeCount(); e++) {
// Iterate over non-removed edges
for (int e : graph.getEdges()) {
GraphVizEdge edge = gvGraph.edge(scope, graph.getEdgeVertex1(e), graph.getEdgeVertex2(e));
SwitchImpl aSwitch = graph.getEdgeObject(e);
if (aSwitch != null) {
Expand Down

0 comments on commit 7beecd6

Please sign in to comment.