Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete CGMES update to only use CGMES full export #1949

Merged
merged 7 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ private static Network microBE(String modelId) {
.setId("_37e14a0f-5e34-4647-a062-8bfd9305fa9d")
.setName("PP_Brussels")
.setCountry(Country.BE)
.setGeographicalTags("_c1d5bfc88f8011e08e4d00247eb1f55e") // ELIA-Brussels
.setGeographicalTags("ELIA-Brussels") // _c1d5bfc88f8011e08e4d00247eb1f55e
.add();
sBrussels.setProperty("CGMES.regionId", "_c1d5bfc88f8011e08e4d00247eb1f55e");
Substation sAnvers = network.newSubstation()
.setId("_87f7002b-056f-4a6a-a872-1744eea757e3")
.setName("Anvers")
.setCountry(Country.BE)
.setGeographicalTags("_c1d5c0378f8011e08e4d00247eb1f55e") // ELIA-Anvers
.setGeographicalTags("ELIA-Anvers") // _c1d5c0378f8011e08e4d00247eb1f55e
.add();
sAnvers.setProperty("CGMES.regionId", "_c1d5c0378f8011e08e4d00247eb1f55e");
VoltageLevel vlBrussels21 = sBrussels.newVoltageLevel()
.setId("_929ba893-c9dc-44d7-b1fd-30834bd3ab85")
.setName("21.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

import com.google.auto.service.AutoService;
import com.powsybl.cgmes.conversion.export.*;
import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.cgmes.model.CgmesModelException;
import com.powsybl.cgmes.model.CgmesModelFactory;
import com.powsybl.commons.datasource.DataSource;
import com.powsybl.commons.exceptions.UncheckedXmlStreamException;
import com.powsybl.commons.xml.XmlUtil;
Expand All @@ -38,6 +34,8 @@
@AutoService(Exporter.class)
public class CgmesExport implements Exporter {

private static final String INDENT = " ";

@Override
public List<Parameter> getParameters() {
return STATIC_PARAMETERS;
Expand All @@ -46,59 +44,50 @@ public List<Parameter> getParameters() {
@Override
public void export(Network network, Properties params, DataSource ds) {
Objects.requireNonNull(network);
if (ConversionParameters.readBooleanParameter(getFormat(), params, USING_ONLY_NETWORK_PARAMETER)) {
exportUsingOnlyNetwork(network, params, ds);
} else {
CgmesModelExtension ext = network.getExtension(CgmesModelExtension.class);
if (ext == null) {
throw new CgmesModelException("CGMES model is required and not found in Network extension");
}
exportUsingOriginalCgmesModel(network, ds, ext);
}
}

private String baseName(Network network, Properties params) {
String baseName = ConversionParameters.readStringParameter(getFormat(), params, BASE_NAME_PARAMETER);
return baseName != null ? baseName : network.getNameOrId();
}

private void exportUsingOnlyNetwork(Network network, Properties params, DataSource ds) {
// At this point only SSH, SV can be exported when relying only in Network data
// (minimum amount of CGMES references are expected as aliases/properties/extensions)
String baseName = baseName(network, params);
String filenameEq = baseName + "_EQ.xml";
String filenameSv = baseName + "_SV.xml";
String filenameTp = baseName + "_TP.xml";
String filenameSsh = baseName + "_SSH.xml";
String filenameSv = baseName + "_SV.xml";
CgmesExportContext context = new CgmesExportContext(network)
.setExportBoundaryPowerFlows(ConversionParameters.readBooleanParameter(getFormat(), params, EXPORT_BOUNDARY_POWER_FLOWS_PARAMETER))
.setExportFlowsForSwitches(ConversionParameters.readBooleanParameter(getFormat(), params, EXPORT_POWER_FLOWS_FOR_SWITCHES_PARAMETER));
try (OutputStream oeq = new BufferedOutputStream(ds.newOutputStream(filenameEq, false));
OutputStream osv = new BufferedOutputStream(ds.newOutputStream(filenameSv, false));
OutputStream ossh = new BufferedOutputStream(ds.newOutputStream(filenameSsh, false))) {
XMLStreamWriter writer;
writer = XmlUtil.initializeWriter(true, " ", oeq);
EquipmentExport.write(network, writer, context);
writer = XmlUtil.initializeWriter(true, " ", osv);
StateVariablesExport.write(network, writer, context);
writer = XmlUtil.initializeWriter(true, " ", ossh);
SteadyStateHypothesisExport.write(network, writer, context);
try {
List<String> profiles = ConversionParameters.readStringListParameter(getFormat(), params, PROFILES_PARAMETER);
if (profiles.contains("EQ")) {
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(filenameEq, false))) {
XMLStreamWriter writer = XmlUtil.initializeWriter(true, INDENT, out);
EquipmentExport.write(network, writer, context);
}
}
if (profiles.contains("TP")) {
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(filenameTp, false))) {
XMLStreamWriter writer = XmlUtil.initializeWriter(true, INDENT, out);
TopologyExport.write(network, writer, context);
}
}
if (profiles.contains("SSH")) {
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(filenameSsh, false))) {
XMLStreamWriter writer = XmlUtil.initializeWriter(true, INDENT, out);
SteadyStateHypothesisExport.write(network, writer, context);
}
}
if (profiles.contains("SV")) {
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(filenameSv, false))) {
XMLStreamWriter writer = XmlUtil.initializeWriter(true, INDENT, out);
StateVariablesExport.write(network, writer, context);
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (XMLStreamException e) {
throw new UncheckedXmlStreamException(e);
}
}

private static void exportUsingOriginalCgmesModel(Network network, DataSource ds, CgmesModelExtension ext) {
CgmesUpdate cgmesUpdate = ext.getCgmesUpdate();
CgmesModel cgmesSource = ext.getCgmesModel();
CgmesModel cgmes = CgmesModelFactory.copy(cgmesSource);
String variantId = network.getVariantManager().getWorkingVariantId();
cgmesUpdate.update(cgmes, variantId);
// Fill the State Variables data with the Network current state values
StateVariablesAdder adder = new StateVariablesAdder(cgmes, network);
adder.addStateVariablesToCgmes();
cgmes.write(ds);
private String baseName(Network network, Properties params) {
String baseName = ConversionParameters.readStringParameter(getFormat(), params, BASE_NAME_PARAMETER);
return baseName != null ? baseName : network.getNameOrId();
}

@Override
Expand All @@ -111,16 +100,11 @@ public String getFormat() {
return "CGMES";
}

public static final String USING_ONLY_NETWORK = "iidm.export.cgmes.using-only-network";
public static final String BASE_NAME = "iidm.export.cgmes.base-name";
public static final String EXPORT_BOUNDARY_POWER_FLOWS = "iidm.export.cgmes.export-boundary-power-flows";
public static final String EXPORT_POWER_FLOWS_FOR_SWITCHES = "iidm.export.cgmes.export-power-flows-for-switches";
public static final String PROFILES = "iidm.export.cgmes.profiles";

private static final Parameter USING_ONLY_NETWORK_PARAMETER = new Parameter(
USING_ONLY_NETWORK,
ParameterType.BOOLEAN,
"Export to CGMES using only information present in IIDM Network (including extensions and aliases)",
Boolean.FALSE);
private static final Parameter BASE_NAME_PARAMETER = new Parameter(
BASE_NAME,
ParameterType.STRING,
Expand All @@ -136,10 +120,15 @@ public String getFormat() {
ParameterType.BOOLEAN,
"Export power flows for switches",
Boolean.FALSE);
private static final Parameter PROFILES_PARAMETER = new Parameter(
PROFILES,
ParameterType.STRING_LIST,
"Profiles to export",
List.of("EQ", "TP", "SSH", "SV"));

private static final List<Parameter> STATIC_PARAMETERS = List.of(
USING_ONLY_NETWORK_PARAMETER,
BASE_NAME_PARAMETER,
EXPORT_BOUNDARY_POWER_FLOWS_PARAMETER,
EXPORT_POWER_FLOWS_FOR_SWITCHES_PARAMETER);
EXPORT_POWER_FLOWS_FOR_SWITCHES_PARAMETER,
PROFILES_PARAMETER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.cgmes.conversion;

import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.commons.extensions.Extension;
import com.powsybl.iidm.network.Network;
Expand All @@ -20,8 +19,6 @@ public interface CgmesModelExtension extends Extension<Network> {

CgmesModel getCgmesModel();

CgmesUpdate getCgmesUpdate();

@Override
default String getName() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.cgmes.conversion;

import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.commons.extensions.ExtensionAdder;
import com.powsybl.iidm.network.Network;
Expand All @@ -22,6 +21,4 @@ default Class<CgmesModelExtension> getExtensionClass() {
}

CgmesModelExtensionAdder withModel(CgmesModel cgmes);

CgmesModelExtensionAdder withUpdate(CgmesUpdate cgmesUpdate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.cgmes.conversion;

import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.commons.extensions.AbstractExtensionAdder;
import com.powsybl.iidm.network.Network;
Expand All @@ -18,26 +17,19 @@ public class CgmesModelExtensionAdderImpl extends AbstractExtensionAdder<Network
implements CgmesModelExtensionAdder {

private CgmesModel cgmes;
private CgmesUpdate cgmesUpdate;

protected CgmesModelExtensionAdderImpl(Network extendable) {
super(extendable);
}

@Override
protected CgmesModelExtension createExtension(Network extendable) {
return new CgmesModelExtensionImpl(cgmes, cgmesUpdate);
return new CgmesModelExtensionImpl(cgmes);
}

@Override
public CgmesModelExtensionAdder withModel(CgmesModel cgmes) {
this.cgmes = cgmes;
return this;
}

@Override
public CgmesModelExtensionAdder withUpdate(CgmesUpdate cgmesUpdate) {
this.cgmesUpdate = cgmesUpdate;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.cgmes.conversion;

import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.iidm.network.Network;
Expand All @@ -19,20 +18,13 @@
public class CgmesModelExtensionImpl extends AbstractExtension<Network> implements CgmesModelExtension {

private final CgmesModel cgmes;
private final CgmesUpdate cgmesUpdate;

public CgmesModelExtensionImpl(CgmesModel cgmes, CgmesUpdate cgmesUpdate) {
public CgmesModelExtensionImpl(CgmesModel cgmes) {
this.cgmes = Objects.requireNonNull(cgmes);
this.cgmesUpdate = Objects.requireNonNull(cgmesUpdate);
}

@Override
public CgmesModel getCgmesModel() {
return cgmes;
}

@Override
public CgmesUpdate getCgmesUpdate() {
return cgmesUpdate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.cgmes.conversion.elements.hvdc.CgmesDcConversion;
import com.powsybl.cgmes.conversion.elements.transformers.ThreeWindingsTransformerConversion;
import com.powsybl.cgmes.conversion.elements.transformers.TwoWindingsTransformerConversion;
import com.powsybl.cgmes.conversion.update.CgmesUpdate;
import com.powsybl.cgmes.extensions.*;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.cgmes.model.CgmesModelException;
Expand Down Expand Up @@ -225,9 +224,7 @@ public Network convert() {

if (config.storeCgmesModelAsNetworkExtension()) {
// Store a reference to the original CGMES model inside the IIDM network
// CgmesUpdate will add a listener to Network changes
CgmesUpdate cgmesUpdater = new CgmesUpdate(network);
network.newExtension(CgmesModelExtensionAdder.class).withModel(cgmes).withUpdate(cgmesUpdater).add();
network.newExtension(CgmesModelExtensionAdder.class).withModel(cgmes).add();
}

// apply post-processors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@
import java.util.Objects;
import java.util.function.Supplier;

import com.powsybl.iidm.network.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.powsybl.cgmes.conversion.Context;
import com.powsybl.cgmes.conversion.CountryConversion;
import com.powsybl.cgmes.model.CgmesNames;
import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.BusbarSection;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.iidm.network.TopologyKind;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.util.Networks;
import com.powsybl.triplestore.api.PropertyBag;

Expand Down Expand Up @@ -68,13 +63,15 @@ private VoltageLevel newBoundarySubstationVoltageLevel() {
String vlId = Context.boundaryVoltageLevelId(this.id);
String substationName = "boundary";
String vlName = "boundary";
return context.network()
SubstationAdder adder = context.network()
.newSubstation()
.setId(context.namingStrategy().getId("Substation", substationId))
.setName(substationName)
.setCountry(boundaryCountryCode())
.add()
.newVoltageLevel()
.setCountry(boundaryCountryCode());
if (boundaryCountryCode() != null) {
adder.setGeographicalTags(boundaryCountryCode().toString());
}
return adder.add().newVoltageLevel()
.setId(context.namingStrategy().getId("VoltageLevel", vlId))
.setName(vlName)
.setNominalV(nominalVoltage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
package com.powsybl.cgmes.conversion.elements;

import com.powsybl.cgmes.conversion.Context;
import com.powsybl.cgmes.conversion.Conversion;
import com.powsybl.cgmes.conversion.CountryConversion;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Substation;
import com.powsybl.iidm.network.SubstationAdder;
import com.powsybl.triplestore.api.PropertyBag;

/**
Expand All @@ -30,39 +32,40 @@ public boolean valid() {

@Override
public void convert() {
String subRegion = p.getId("SubRegion");
String subRegionName = p.get("subRegionName");
String regionName = p.get("regionName");

Country country = CountryConversion.fromRegionName(regionName)
.orElseGet(() -> CountryConversion.fromSubregionName(subRegionName)
.orElse(null));
String geo = subRegion;

// TODO add naminStrategy (for regions and substations)
// After applying naming strategy it is possible that two CGMES substations are mapped
// to the same Network substation, so we should check if corresponding substation has
// already been created
String geoTag = context.namingStrategy().getGeographicalTag(geo);
String geoTag = context.namingStrategy().getGeographicalTag(subRegionName);

String iidmSubstationId = context.substationIdMapping().substationIidm(id);
Substation substation = context.network().getSubstation(iidmSubstationId);
assert substation == null;
Substation s = context.network().newSubstation()
SubstationAdder adder = context.network().newSubstation()
.setId(iidmSubstationId)
.setName(iidmName())
.setEnsureIdUnicity(context.config().isEnsureIdAliasUnicity())
.setCountry(country)
.setGeographicalTags(geoTag)
.add();
addAliases(s);
.setCountry(country);
if (geoTag != null) {
adder.setGeographicalTags(geoTag);
}
Substation s = adder.add();
addAliasesAndProperties(s, p.getId("SubRegion"));
}

private void addAliases(Substation s) {
private void addAliasesAndProperties(Substation s, String regionId) {
int index = 0;
for (String mergedSub : context.substationIdMapping().mergedSubstations(s.getId())) {
index++;
s.addAlias(mergedSub, "MergedSubstation" + index, context.config().isEnsureIdAliasUnicity());
}
s.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + "regionId", regionId);
}
}
Loading