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

Refactor grouped queries in CgmesModel #3238

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@
<cim:OperationalLimitType.direction rdf:resource="http://iec.ch/TC57/2013/CIM-schema-cim16#OperationalLimitDirectionKind.absoluteValue"/>
<entsoe:OperationalLimitType.limitType rdf:resource="http://entsoe.eu/CIM/SchemaExtension/3/1#LimitTypeKind.tct"/>
</cim:OperationalLimitType>
<cim:PhaseTapChangerAsymmetrical rdf:ID="_6ebbef67-3061-4236-a6fd-6ccc4595f6c3">
<cim:PhaseTapChangerAsymmetrical rdf:ID="_6ebbef67-3061-4236-a6fd-6ccc4595f6c4">
<cim:IdentifiedObject.name>BE-TR2_1</cim:IdentifiedObject.name>
<entsoe:IdentifiedObject.shortName>BE-T_1</entsoe:IdentifiedObject.shortName>
<cim:TapChanger.neutralU>400.000000</cim:TapChanger.neutralU>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.triplestore.api.PropertyBags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,11 +58,15 @@ public Context(CgmesModel cgmes, Config config, Network network, ReportNode repo
regulatingControlMapping = new RegulatingControlMapping(this);
nodeMapping = new NodeMapping(this);

ratioTapChangerTables = new HashMap<>();
phaseTapChangerTables = new HashMap<>();
reactiveCapabilityCurveData = new HashMap<>();
powerTransformerRatioTapChangers = new HashMap<>();
powerTransformerPhaseTapChangers = new HashMap<>();
cachedGroupedTransformerEnds = new HashMap<>();
cachedGroupedRatioTapChangers = new HashMap<>();
cachedGroupedRatioTapChangerTablePoints = new HashMap<>();
cachedGroupedPhaseTapChangers = new HashMap<>();
cachedGroupedPhaseTapChangerTablePoints = new HashMap<>();
cachedGroupedShuntCompensatorPoints = new HashMap<>();
cachedGroupedReactiveCapabilityCurveData = new HashMap<>();

buildCaches();
}

public CgmesModel cgmes() {
Expand Down Expand Up @@ -138,71 +141,49 @@ public static String boundarySubstationId(String nodeId) {
return nodeId + "_S";
}

public void loadReactiveCapabilityCurveData() {
PropertyBags rccdata = cgmes.reactiveCapabilityCurveData();
if (rccdata == null) {
return;
}
rccdata.forEach(p -> {
String curveId = p.getId("ReactiveCapabilityCurve");
reactiveCapabilityCurveData.computeIfAbsent(curveId, cid -> new PropertyBags()).add(p);
});
}

public PropertyBags reactiveCapabilityCurveData(String curveId) {
return reactiveCapabilityCurveData.get(curveId);
private void buildCaches() {
buildCache(cachedGroupedTransformerEnds, cgmes().transformerEnds(), CgmesNames.POWER_TRANSFORMER);
buildCache(cachedGroupedRatioTapChangers, cgmes().ratioTapChangers(), CgmesNames.POWER_TRANSFORMER);
buildCache(cachedGroupedRatioTapChangerTablePoints, cgmes().ratioTapChangerTablePoints(), CgmesNames.RATIO_TAP_CHANGER_TABLE);
buildCache(cachedGroupedPhaseTapChangers, cgmes().phaseTapChangers(), CgmesNames.POWER_TRANSFORMER);
buildCache(cachedGroupedPhaseTapChangerTablePoints, cgmes().phaseTapChangerTablePoints(), CgmesNames.PHASE_TAP_CHANGER_TABLE);
buildCache(cachedGroupedShuntCompensatorPoints, cgmes().nonlinearShuntCompensatorPoints(), "Shunt");
buildCache(cachedGroupedReactiveCapabilityCurveData, cgmes().reactiveCapabilityCurveData(), "ReactiveCapabilityCurve");
}

public void loadRatioTapChangers() {
cgmes.ratioTapChangers().forEach(ratio -> {
String id = ratio.getId(CgmesNames.RATIO_TAP_CHANGER);
powerTransformerRatioTapChangers.put(id, ratio);
private void buildCache(Map<String, PropertyBags> cache, PropertyBags ps, String groupName) {
ps.forEach(p -> {
String groupId = p.getId(groupName);
cache.computeIfAbsent(groupId, b -> new PropertyBags()).add(p);
});
}

public PropertyBag ratioTapChanger(String id) {
return powerTransformerRatioTapChangers.get(id);
public PropertyBags transformerEnds(String transformerId) {
return cachedGroupedTransformerEnds.getOrDefault(transformerId, new PropertyBags());
}

public void loadPhaseTapChangers() {
cgmes.phaseTapChangers().forEach(phase -> {
String id = phase.getId(CgmesNames.PHASE_TAP_CHANGER);
powerTransformerPhaseTapChangers.put(id, phase);
});
public PropertyBags ratioTapChangers(String transformerId) {
return cachedGroupedRatioTapChangers.getOrDefault(transformerId, new PropertyBags());
}

public PropertyBag phaseTapChanger(String id) {
return powerTransformerPhaseTapChangers.get(id);
public PropertyBags ratioTapChangerTablePoints(String tableId) {
return cachedGroupedRatioTapChangerTablePoints.getOrDefault(tableId, new PropertyBags());
}

public void loadRatioTapChangerTables() {
PropertyBags rtcpoints = cgmes.ratioTapChangerTablesPoints();
if (rtcpoints == null) {
return;
}
rtcpoints.forEach(p -> {
String tableId = p.getId("RatioTapChangerTable");
ratioTapChangerTables.computeIfAbsent(tableId, tid -> new PropertyBags()).add(p);
});
public PropertyBags phaseTapChangers(String transformerId) {
return cachedGroupedPhaseTapChangers.getOrDefault(transformerId, new PropertyBags());
}

public void loadPhaseTapChangerTables() {
PropertyBags ptcpoints = cgmes.phaseTapChangerTablesPoints();
if (ptcpoints == null) {
return;
}
ptcpoints.forEach(p -> {
String tableId = p.getId("PhaseTapChangerTable");
phaseTapChangerTables.computeIfAbsent(tableId, tid -> new PropertyBags()).add(p);
});
public PropertyBags phaseTapChangerTablePoints(String tableId) {
return cachedGroupedPhaseTapChangerTablePoints.getOrDefault(tableId, new PropertyBags());
}

public PropertyBags ratioTapChangerTable(String tableId) {
return ratioTapChangerTables.get(tableId);
public PropertyBags nonlinearShuntCompensatorPoints(String shuntId) {
return cachedGroupedShuntCompensatorPoints.getOrDefault(shuntId, new PropertyBags());
}

public PropertyBags phaseTapChangerTable(String tableId) {
return phaseTapChangerTables.get(tableId);
public PropertyBags reactiveCapabilityCurveData(String curveId) {
return cachedGroupedReactiveCapabilityCurveData.getOrDefault(curveId, new PropertyBags());
}

// Handling issues found during conversion
Expand Down Expand Up @@ -313,11 +294,13 @@ private static void logIssue(ConversionIssueCategory category, String what, Supp
private final LoadingLimitsMapping loadingLimitsMapping;
private final RegulatingControlMapping regulatingControlMapping;

private final Map<String, PropertyBags> ratioTapChangerTables;
private final Map<String, PropertyBags> phaseTapChangerTables;
private final Map<String, PropertyBags> reactiveCapabilityCurveData;
private final Map<String, PropertyBag> powerTransformerRatioTapChangers;
private final Map<String, PropertyBag> powerTransformerPhaseTapChangers;
private final Map<String, PropertyBags> cachedGroupedTransformerEnds;
private final Map<String, PropertyBags> cachedGroupedRatioTapChangers;
private final Map<String, PropertyBags> cachedGroupedRatioTapChangerTablePoints;
private final Map<String, PropertyBags> cachedGroupedPhaseTapChangers;
private final Map<String, PropertyBags> cachedGroupedPhaseTapChangerTablePoints;
private final Map<String, PropertyBags> cachedGroupedShuntCompensatorPoints;
private final Map<String, PropertyBags> cachedGroupedReactiveCapabilityCurveData;

private static final Logger LOG = LoggerFactory.getLogger(Context.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,6 @@ private Network createNetwork() {
private Context createContext(Network network, ReportNode reportNode) {
Context context = new Context(cgmes, config, network, reportNode);
context.dc().initialize();
context.loadRatioTapChangers();
context.loadPhaseTapChangers();
context.loadRatioTapChangerTables();
context.loadPhaseTapChangerTables();
context.loadReactiveCapabilityCurveData();
return context;
}

Expand Down Expand Up @@ -688,21 +683,24 @@ private void convertEquivalentBranchesToLines(Context context, Set<String> delay

private void convertTransformers(Context context, Set<String> delayedBoundaryNodes) {
context.pushReportNode(CgmesReports.convertingElementTypeReport(context.getReportNode(), CgmesNames.POWER_TRANSFORMER));
cgmes.groupedTransformerEnds().forEach((t, ends) -> {
if (LOG.isTraceEnabled()) {
LOG.trace("Transformer {}, {}-winding", t, ends.size());
ends.forEach(e -> LOG.trace(e.tabulateLocals("TransformerEnd")));
}
if (ends.size() == 2) {
convertTwoWindingsTransformers(context, ends, delayedBoundaryNodes);
} else if (ends.size() == 3) {
convertThreeWindingsTransformers(context, ends);
} else {
String what = "PowerTransformer " + t;
Supplier<String> reason = () -> String.format("Has %d ends. Only 2 or 3 ends are supported", ends.size());
context.invalid(what, reason);
}
});
cgmes.transformers().stream()
.map(t -> context.transformerEnds(t.getId("PowerTransformer")))
.forEach(ends -> {
String transformerId = ends.get(0).getId("PowerTransformer");
if (LOG.isTraceEnabled()) {
LOG.trace("Transformer {}, {}-winding", transformerId, ends.size());
ends.forEach(e -> LOG.trace(e.tabulateLocals("TransformerEnd")));
}
if (ends.size() == 2) {
convertTwoWindingsTransformers(context, ends, delayedBoundaryNodes);
} else if (ends.size() == 3) {
convertThreeWindingsTransformers(context, ends);
} else {
String what = "PowerTransformer " + transformerId;
Supplier<String> reason = () -> String.format("Has %d ends. Only 2 or 3 ends are supported", ends.size());
context.invalid(what, reason);
}
});
context.popReportNode();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ private void buildAdjacency(Map<String, Set<String>> voltageLevelAdjacency, Map<
boolean fictitiousVoltageLevelForEveryNode = context.config().getCreateFictitiousVoltageLevelsForEveryNode();

context.cgmes().switches().forEach(sw -> addAdjacencyThroughSwitch(voltageLevelAdjacency, substationAdjacency, sw, fictitiousVoltageLevelForEveryNode));
context.cgmes().groupedTransformerEnds().forEach((t, tends) -> addAdjacencyThroughTransformerEnds(substationAdjacency, tends));
context.cgmes().transformers().stream()
.map(t -> context.transformerEnds(t.getId("PowerTransformer")))
.forEach(tends -> addAdjacencyThroughTransformerEnds(substationAdjacency, tends));

context.cgmes().acLineSegments().forEach(ac -> addAdjacencyThroughBranch(fictitiousVoltageLevelAdjacency, ac, fictitiousVoltageLevelForEveryNode));
context.cgmes().seriesCompensators().forEach(sc -> addAdjacencyThroughBranch(fictitiousVoltageLevelAdjacency, sc, fictitiousVoltageLevelForEveryNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import com.powsybl.triplestore.api.PropertyBags;
import com.powsybl.triplestore.api.TripleStore;

import java.util.HashMap;
import java.util.Map;

/**
* @author Luma Zamarreño {@literal <zamarrenolm at aia.es>}
* @author José Antonio Marqués {@literal <marquesja at aia.es>}
Expand All @@ -48,16 +51,21 @@ public void process(Network network, TripleStore tripleStore) {
LOG.warn("PhaseAngleClock-PostProcessor: Unexpected null cgmesModel pointer");
return;
}

cgmes.groupedTransformerEnds().forEach((t, ends) -> {
if (ends.size() == 2) {
phaseAngleClockTwoWindingTransformer(ends, network);
} else if (ends.size() == 3) {
phaseAngleClockThreeWindingTransformer(ends, network);
} else {
throw new PowsyblException(String.format("Unexpected TransformerEnds: ends %d", ends.size()));
}
});
Map<String, PropertyBags> groupedTransformerEnds = new HashMap<>();
cgmes.transformerEnds()
.forEach(p -> groupedTransformerEnds
.computeIfAbsent(p.getId("PowerTransformer"), b -> new PropertyBags())
.add(p));
groupedTransformerEnds.values()
.forEach(ends -> {
if (ends.size() == 2) {
phaseAngleClockTwoWindingTransformer(ends, network);
} else if (ends.size() == 3) {
phaseAngleClockThreeWindingTransformer(ends, network);
} else {
throw new PowsyblException(String.format("Unexpected TransformerEnds: ends %d", ends.size()));
}
});
}

private void phaseAngleClockTwoWindingTransformer(PropertyBags ends, Network network) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void convert() {
.add();
} else if ("NonlinearShuntCompensator".equals(shuntType)) {
ShuntCompensatorNonLinearModelAdder modelAdder = adder.newNonLinearModel();
PropertyBags ss = context.cgmes().nonlinearShuntCompensatorPoints(id);
PropertyBags ss = context.nonlinearShuntCompensatorPoints(id);
ss.stream()
.filter(s -> s.asInt(SECTION_NUMBER) > 0)
.sorted(Comparator.comparing(s -> s.asInt(SECTION_NUMBER)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.Objects;

import com.powsybl.cgmes.conversion.Context;
import com.powsybl.cgmes.model.CgmesDcTerminal;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.cgmes.model.CgmesNames;
Expand All @@ -36,23 +37,25 @@ enum AdjacentType {

private final Map<String, List<Adjacent>> adjacency;

Adjacency(CgmesModel cgmesModel, AcDcConverterNodes acDcConverterNodes) {
Adjacency(CgmesModel cgmesModel, Context context, AcDcConverterNodes acDcConverterNodes) {
adjacency = new HashMap<>();
cgmesModel.dcLineSegments().forEach(dcls -> computeDcLineSegmentAdjacency(cgmesModel, dcls));

acDcConverterNodes.getConverterNodes()
.forEach((key, value) -> computeAcDcConverterAdjacency(value.acNode,
value.dcNode));

cgmesModel.groupedTransformerEnds().forEach((t, ends) -> {
if (ends.size() == 2) {
computeTwoWindingsTransformerAdjacency(cgmesModel, ends);
} else if (ends.size() == 3) {
computeThreeWindingsTransformerAdjacency(cgmesModel, ends);
} else {
throw new PowsyblException(String.format("Unexpected TransformerEnds: ends %d", ends.size()));
}
});
cgmesModel.transformers().stream()
.map(t -> context.transformerEnds(t.getId("PowerTransformer")))
olperr1 marked this conversation as resolved.
Show resolved Hide resolved
.forEach(ends -> {
if (ends.size() == 2) {
computeTwoWindingsTransformerAdjacency(cgmesModel, ends);
} else if (ends.size() == 3) {
computeThreeWindingsTransformerAdjacency(cgmesModel, ends);
} else {
throw new PowsyblException(String.format("Unexpected TransformerEnds: ends %d", ends.size()));
}
});
}

private void computeDcLineSegmentAdjacency(CgmesModel cgmesModel, PropertyBag equipment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public void convert() {
// Get hvdc configurations
AcDcConverterNodes acDcConverterNodes = new AcDcConverterNodes(cgmesModel);

Adjacency adjacency = new Adjacency(cgmesModel, acDcConverterNodes);
Adjacency adjacency = new Adjacency(cgmesModel, context, acDcConverterNodes);
if (adjacency.isEmpty()) {
return;
}
NodeEquipment nodeEquipment = new NodeEquipment(cgmesModel, acDcConverterNodes, adjacency);
NodeEquipment nodeEquipment = new NodeEquipment(cgmesModel, context, acDcConverterNodes, adjacency);
Islands islands = new Islands(adjacency);

IslandsEnds islandsEnds = new IslandsEnds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.*;
import java.util.stream.Collectors;

import com.powsybl.cgmes.conversion.Context;
import com.powsybl.cgmes.model.CgmesDcTerminal;
import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.cgmes.model.CgmesNames;
Expand All @@ -31,7 +32,7 @@ enum EquipmentType {

private final Map<String, List<EquipmentReference>> nodeEquipment;

NodeEquipment(CgmesModel cgmesModel, AcDcConverterNodes acDcConverterNodes, Adjacency adjacency) {
NodeEquipment(CgmesModel cgmesModel, Context context, AcDcConverterNodes acDcConverterNodes, Adjacency adjacency) {
nodeEquipment = new HashMap<>();

cgmesModel.dcLineSegments().forEach(dcls -> computeDcLineSegment(cgmesModel, adjacency, dcls));
Expand All @@ -40,13 +41,15 @@ enum EquipmentType {
.forEach(value -> addEquipment(adjacency, value.id, value.acNode,
value.dcNode, EquipmentType.AC_DC_CONVERTER));

cgmesModel.groupedTransformerEnds().forEach((t, ends) -> {
if (ends.size() == 2) {
computeTwoWindingsTransformer(cgmesModel, adjacency, ends);
} else if (ends.size() == 3) {
computeThreeWindingsTransformer(cgmesModel, adjacency, ends);
}
});
cgmesModel.transformers().stream()
.map(t -> context.transformerEnds(t.getId("PowerTransformer")))
.forEach(ends -> {
if (ends.size() == 2) {
computeTwoWindingsTransformer(cgmesModel, adjacency, ends);
} else if (ends.size() == 3) {
computeThreeWindingsTransformer(cgmesModel, adjacency, ends);
}
});
}

private void computeDcLineSegment(CgmesModel cgmesModel, Adjacency adjacency, PropertyBag equipment) {
Expand Down
Loading
Loading