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

Refactoring to add a Flow Decomposition Result builder #64

Merged
merged 9 commits into from
Nov 7, 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 @@ -13,7 +13,6 @@
* @author Hugo Schindler {@literal <hugo.schindler at rte-france.com>}
*/
abstract class AbstractSparseMatrixWithIndexes {
private static final boolean DO_NOT_FILL_ZEROS = false;
protected final Map<String, Integer> rowIndex;
protected final Map<String, Integer> colIndex;

Expand All @@ -22,9 +21,5 @@ protected AbstractSparseMatrixWithIndexes(Map<String, Integer> rowIndex, Map<Str
this.colIndex = colIndex;
}

abstract Map<String, Map<String, Double>> toMap(boolean fillZeros);

Map<String, Map<String, Double>> toMap() {
return toMap(DO_NOT_FILL_ZEROS);
}
abstract Map<String, Map<String, Double>> toMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,25 @@
import com.powsybl.iidm.network.Country;

import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
* @author Sebastien Murgey {@literal <sebastien.murgey at rte-france.com>}
* @author Hugo Schindler {@literal <hugo.schindler at rte-france.com>}
*/
class DecomposedFlowsRescaler {
final class DecomposedFlowsRescaler {
private DecomposedFlowsRescaler() {
}

private double reLU(double value) {
private static double reLU(double value) {
return value > 0 ? value : 0.;
}

private double rescaleValue(double initialValue, double delta, double sumOfReLUFlows) {
private static double rescaleValue(double initialValue, double delta, double sumOfReLUFlows) {
return initialValue + delta * reLU(initialValue) / sumOfReLUFlows;
}

Map<String, DecomposedFlow> rescale(Map<String, DecomposedFlow> decomposedFlowMap) {
Map<String, DecomposedFlow> rescaledDecomposedFlowMap = new TreeMap<>();
decomposedFlowMap.forEach((s, decomposedFlow) -> rescaledDecomposedFlowMap.put(s, rescale(decomposedFlow)));
return rescaledDecomposedFlowMap;
}

DecomposedFlow rescale(DecomposedFlow decomposedFlow) {
static DecomposedFlow rescale(DecomposedFlow decomposedFlow) {
double allocatedFlow = decomposedFlow.getAllocatedFlow();
double pstFlow = decomposedFlow.getPstFlow();
Map<String, Double> loopFlows = decomposedFlow.getLoopFlows();
Expand All @@ -45,9 +40,9 @@ DecomposedFlow rescale(DecomposedFlow decomposedFlow) {
return decomposedFlow;
}
double deltaToRescale = acReferenceFlow * Math.signum(acReferenceFlow) - decomposedFlow.getTotalFlow();
double sumOfReLUFlows = reLU(allocatedFlow) + reLU(pstFlow) + loopFlows.values().stream().mapToDouble(this::reLU).sum() + reLU(internalFlow);
double sumOfReLUFlows = reLU(allocatedFlow) + reLU(pstFlow) + loopFlows.values().stream().mapToDouble(DecomposedFlowsRescaler::reLU).sum() + reLU(internalFlow);
Map<String, Double> rescaledLoopFlows = loopFlows.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> rescaleValue(entry.getValue(), deltaToRescale, sumOfReLUFlows)));
.collect(Collectors.toMap(Map.Entry::getKey, entry -> rescaleValue(entry.getValue(), deltaToRescale, sumOfReLUFlows)));
double rescaledAllocatedFlow = rescaleValue(allocatedFlow, deltaToRescale, sumOfReLUFlows);
double rescaledPstFlow = rescaleValue(pstFlow, deltaToRescale, sumOfReLUFlows);
double rescaleInternalFlow = rescaleValue(internalFlow, deltaToRescale, sumOfReLUFlows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public FlowDecompositionComputer(FlowDecompositionParameters flowDecompositionPa
}

public FlowDecompositionResults run(Network network) {
FlowDecompositionResults flowDecompositionResults = new FlowDecompositionResults(network, parameters);
FlowDecompositionResultsBuilder flowDecompositionResultsBuilder = new FlowDecompositionResultsBuilder(network);

LoadFlowRunningService.Result loadFlowServiceAcResult = runAcLoadFlow(network);

Map<Country, Map<String, Double>> glsks = getGlsks(network, flowDecompositionResults);
List<Branch> xnecList = getXnecList(network, glsks, flowDecompositionResults);
Map<Country, Double> netPositions = getZonesNetPosition(network, flowDecompositionResults);
saveAcReferenceFlow(flowDecompositionResults, xnecList, loadFlowServiceAcResult);
Map<Country, Map<String, Double>> glsks = getGlsks(network);
List<Branch> xnecList = getXnecList(network, glsks, flowDecompositionResultsBuilder);
Map<Country, Double> netPositions = getZonesNetPosition(network);
saveAcReferenceFlow(flowDecompositionResultsBuilder, xnecList, loadFlowServiceAcResult);
compensateLosses(network);

// None
Expand All @@ -67,82 +67,69 @@ public FlowDecompositionResults run(Network network) {
runDcLoadFlow(network);

SparseMatrixWithIndexesTriplet nodalInjectionsMatrix = getNodalInjectionsMatrix(network,
flowDecompositionResults, netPositions, networkMatrixIndexes, glsks);
saveDcReferenceFlow(flowDecompositionResults, xnecList);
netPositions, networkMatrixIndexes, glsks);
saveDcReferenceFlow(flowDecompositionResultsBuilder, xnecList);

// DC Sensi
SensitivityAnalyser sensitivityAnalyser = getSensitivityAnalyser(network, networkMatrixIndexes);
SparseMatrixWithIndexesTriplet ptdfMatrix = getPtdfMatrix(flowDecompositionResults,
networkMatrixIndexes, sensitivityAnalyser);
SparseMatrixWithIndexesTriplet psdfMatrix = getPsdfMatrix(flowDecompositionResults,
networkMatrixIndexes, sensitivityAnalyser);
SparseMatrixWithIndexesTriplet ptdfMatrix = getPtdfMatrix(networkMatrixIndexes, sensitivityAnalyser);
SparseMatrixWithIndexesTriplet psdfMatrix = getPsdfMatrix(networkMatrixIndexes, sensitivityAnalyser);

// None
computeAllocatedAndLoopFlows(flowDecompositionResults, nodalInjectionsMatrix, ptdfMatrix);
computePstFlows(network, flowDecompositionResults, networkMatrixIndexes, psdfMatrix);
computeAllocatedAndLoopFlows(flowDecompositionResultsBuilder, nodalInjectionsMatrix, ptdfMatrix);
computePstFlows(network, flowDecompositionResultsBuilder, networkMatrixIndexes, psdfMatrix);

rescale(flowDecompositionResults);

return flowDecompositionResults;
return flowDecompositionResultsBuilder.build(parameters.isRescaleEnabled());
}

private LoadFlowRunningService.Result runAcLoadFlow(Network network) {
return loadFlowRunningService.runAcLoadflow(network, loadFlowParameters, parameters.isDcFallbackEnabledAfterAcDivergence());
}

private List<Branch> getXnecList(Network network, Map<Country, Map<String, Double>> glsks, FlowDecompositionResults flowDecompositionResults) {
private List<Branch> getXnecList(Network network, Map<Country, Map<String, Double>> glsks, FlowDecompositionResultsBuilder flowDecompositionResultsBuilder) {
XnecSelector xnecSelector;
switch (parameters.getXnecSelectionStrategy()) {
case ONLY_INTERCONNECTIONS:
xnecSelector = new XnecSelectorInterconnection();
break;
case INTERCONNECTION_OR_ZONE_TO_ZONE_PTDF_GT_5PC:
Map<String, Map<Country, Double>> zonalPtdf = getZonalPtdf(network, glsks, flowDecompositionResults);
Map<String, Map<Country, Double>> zonalPtdf = getZonalPtdf(network, glsks);
xnecSelector = new XnecSelector5percPtdf(zonalPtdf);
break;
default:
throw new PowsyblException(String.format("XnecSelectionStrategy %s is not valid",
parameters.getXnecSelectionStrategy()));
}
List<Branch> xnecList = xnecSelector.run(network);
flowDecompositionResults.saveXnec(xnecList);
flowDecompositionResultsBuilder.saveXnec(xnecList);
return xnecList;
}

private void saveAcReferenceFlow(FlowDecompositionResults flowDecompositionResults, List<Branch> xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) {
private void saveAcReferenceFlow(FlowDecompositionResultsBuilder flowDecompositionResultsBuilder, List<Branch> xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) {
Map<String, Double> acReferenceFlows;
if (loadFlowServiceAcResult.fallbackHasBeenActivated()) {
acReferenceFlows = xnecList.stream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN));
} else {
acReferenceFlows = getXnecReferenceFlows(xnecList);
}
flowDecompositionResults.saveAcReferenceFlow(acReferenceFlows);
flowDecompositionResultsBuilder.saveAcReferenceFlow(acReferenceFlows);
}

private Map<Country, Map<String, Double>> getGlsks(Network network,
FlowDecompositionResults flowDecompositionResults) {
private Map<Country, Map<String, Double>> getGlsks(Network network) {
GlskComputer glskComputer = new GlskComputer();
Map<Country, Map<String, Double>> glsks = glskComputer.run(network);
flowDecompositionResults.saveGlsks(glsks);
return glsks;
return glskComputer.run(network);
}

private Map<String, Map<Country, Double>> getZonalPtdf(Network network,
Map<Country, Map<String, Double>> glsks,
FlowDecompositionResults flowDecompositionResults) {
Map<Country, Map<String, Double>> glsks) {
ZonalSensitivityAnalyser zonalSensitivityAnalyser = new ZonalSensitivityAnalyser(loadFlowParameters, sensitivityAnalysisRunner);
Map<String, Map<Country, Double>> zonalPtdf = zonalSensitivityAnalyser.run(network,
return zonalSensitivityAnalyser.run(network,
glsks, SensitivityVariableType.INJECTION_ACTIVE_POWER);
flowDecompositionResults.saveZonalPtdf(zonalPtdf);
return zonalPtdf;
}

private Map<Country, Double> getZonesNetPosition(Network network,
FlowDecompositionResults flowDecompositionResults) {
private Map<Country, Double> getZonesNetPosition(Network network) {
NetPositionComputer netPositionComputer = new NetPositionComputer();
Map<Country, Double> netPosition = netPositionComputer.run(network);
flowDecompositionResults.saveACNetPosition(netPosition);
return netPosition;
return netPositionComputer.run(network);
}

private Map<String, Double> getXnecReferenceFlows(List<Branch> xnecList) {
Expand All @@ -162,94 +149,64 @@ private LoadFlowRunningService.Result runDcLoadFlow(Network network) {
}

private SparseMatrixWithIndexesTriplet getNodalInjectionsMatrix(Network network,
FlowDecompositionResults flowDecompositionResults,
Map<Country, Double> netPositions,
NetworkMatrixIndexes networkMatrixIndexes,
Map<Country, Map<String, Double>> glsks) {
NodalInjectionComputer nodalInjectionComputer = new NodalInjectionComputer(networkMatrixIndexes);
Map<String, Double> dcNodalInjection = getDcNodalInjection(flowDecompositionResults, networkMatrixIndexes);
Map<String, Double> dcNodalInjection = getDcNodalInjection(networkMatrixIndexes);

return getNodalInjectionsMatrix(network, flowDecompositionResults, netPositions, glsks,
return getNodalInjectionsMatrix(network, netPositions, glsks,
nodalInjectionComputer, dcNodalInjection);
}

private Map<String, Double> getDcNodalInjection(FlowDecompositionResults flowDecompositionResults,
NetworkMatrixIndexes networkMatrixIndexes) {
private Map<String, Double> getDcNodalInjection(NetworkMatrixIndexes networkMatrixIndexes) {
ReferenceNodalInjectionComputer referenceNodalInjectionComputer = new ReferenceNodalInjectionComputer(networkMatrixIndexes);
Map<String, Double> dcNodalInjection = referenceNodalInjectionComputer.run();
flowDecompositionResults.saveDcNodalInjections(dcNodalInjection);
return dcNodalInjection;
return referenceNodalInjectionComputer.run();
}

private SparseMatrixWithIndexesTriplet getNodalInjectionsMatrix(Network network,
FlowDecompositionResults flowDecompositionResults,
Map<Country, Double> netPositions,
Map<Country, Map<String, Double>> glsks,
NodalInjectionComputer nodalInjectionComputer,
Map<String, Double> dcNodalInjection) {
SparseMatrixWithIndexesTriplet nodalInjectionsMatrix =
nodalInjectionComputer.run(network,
glsks, netPositions, dcNodalInjection);
flowDecompositionResults.saveNodalInjectionsMatrix(nodalInjectionsMatrix);
return nodalInjectionsMatrix;
return nodalInjectionComputer.run(network, glsks, netPositions, dcNodalInjection);
}

private void saveDcReferenceFlow(FlowDecompositionResults flowDecompositionResults, List<Branch> xnecList) {
flowDecompositionResults.saveDcReferenceFlow(getXnecReferenceFlows(xnecList));
private void saveDcReferenceFlow(FlowDecompositionResultsBuilder flowDecompositionResultsBuilder, List<Branch> xnecList) {
flowDecompositionResultsBuilder.saveDcReferenceFlow(getXnecReferenceFlows(xnecList));
}

private SensitivityAnalyser getSensitivityAnalyser(Network network, NetworkMatrixIndexes networkMatrixIndexes) {
return new SensitivityAnalyser(loadFlowParameters, parameters, sensitivityAnalysisRunner, network, networkMatrixIndexes);
}

private SparseMatrixWithIndexesTriplet getPtdfMatrix(FlowDecompositionResults flowDecompositionResults,
NetworkMatrixIndexes networkMatrixIndexes,
private SparseMatrixWithIndexesTriplet getPtdfMatrix(NetworkMatrixIndexes networkMatrixIndexes,
SensitivityAnalyser sensitivityAnalyser) {
SparseMatrixWithIndexesTriplet ptdfMatrix =
sensitivityAnalyser.run(networkMatrixIndexes.getNodeIdList(),
networkMatrixIndexes.getNodeIndex(),
SensitivityVariableType.INJECTION_ACTIVE_POWER);
flowDecompositionResults.savePtdfMatrix(ptdfMatrix);
return ptdfMatrix;
return sensitivityAnalyser.run(networkMatrixIndexes.getNodeIdList(),
networkMatrixIndexes.getNodeIndex(),
SensitivityVariableType.INJECTION_ACTIVE_POWER);
}

private void computeAllocatedAndLoopFlows(FlowDecompositionResults flowDecompositionResults,
private void computeAllocatedAndLoopFlows(FlowDecompositionResultsBuilder flowDecompositionResultsBuilder,
SparseMatrixWithIndexesTriplet nodalInjectionsMatrix,
SparseMatrixWithIndexesTriplet ptdfMatrix) {
SparseMatrixWithIndexesCSC allocatedLoopFlowsMatrix =
SparseMatrixWithIndexesCSC.mult(ptdfMatrix.toCSCMatrix(), nodalInjectionsMatrix.toCSCMatrix());
flowDecompositionResults.saveAllocatedAndLoopFlowsMatrix(allocatedLoopFlowsMatrix);
flowDecompositionResultsBuilder.saveAllocatedAndLoopFlowsMatrix(allocatedLoopFlowsMatrix);
}

private SparseMatrixWithIndexesTriplet getPsdfMatrix(FlowDecompositionResults flowDecompositionResults,
NetworkMatrixIndexes networkMatrixIndexes,
private SparseMatrixWithIndexesTriplet getPsdfMatrix(NetworkMatrixIndexes networkMatrixIndexes,
SensitivityAnalyser sensitivityAnalyser) {
SparseMatrixWithIndexesTriplet psdfMatrix =
sensitivityAnalyser.run(networkMatrixIndexes.getPstList(),
networkMatrixIndexes.getPstIndex(), SensitivityVariableType.TRANSFORMER_PHASE);
flowDecompositionResults.savePsdfMatrix(psdfMatrix);
return psdfMatrix;
return sensitivityAnalyser.run(networkMatrixIndexes.getPstList(),
networkMatrixIndexes.getPstIndex(), SensitivityVariableType.TRANSFORMER_PHASE);
}

private void computePstFlows(Network network,
FlowDecompositionResults flowDecompositionResults,
FlowDecompositionResultsBuilder flowDecompositionResultsBuilder,
NetworkMatrixIndexes networkMatrixIndexes,
SparseMatrixWithIndexesTriplet psdfMatrix) {
PstFlowComputer pstFlowComputer = new PstFlowComputer();
SparseMatrixWithIndexesCSC pstFlowMatrix = pstFlowComputer.run(network, networkMatrixIndexes, psdfMatrix);
flowDecompositionResults.savePstFlowMatrix(pstFlowMatrix);
}

private void rescale(FlowDecompositionResults flowDecompositionResults) {
flowDecompositionResults.saveRescaledDecomposedFlowMap(getRescaledDecomposedFlowMap(flowDecompositionResults));
}

private Map<String, DecomposedFlow> getRescaledDecomposedFlowMap(FlowDecompositionResults flowDecompositionResults) {
Map<String, DecomposedFlow> decomposedFlowMap = flowDecompositionResults.getDecomposedFlowMapBeforeRescaling();
if (parameters.isRescaleEnabled()) {
DecomposedFlowsRescaler decomposedFlowsRescaler = new DecomposedFlowsRescaler();
return decomposedFlowsRescaler.rescale(decomposedFlowMap);
}
return decomposedFlowMap;
flowDecompositionResultsBuilder.savePstFlowMatrix(pstFlowMatrix);
}
}
Loading