Skip to content

Commit

Permalink
Decomposed Flows Observer before Rescaling (#177)
Browse files Browse the repository at this point in the history
* Decomposed Flows Observer before Rescaling

Signed-off-by: Sharyal Zafar <sharyal.zafar@artelys.com>
  • Loading branch information
sharyalZ authored Feb 14, 2025
1 parent d376571 commit 55ad9ca
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ Observers are notified of the following events:
* when the PSDF matrix is computed (for base case or contingency)
* when the AC loadflow is computed (for base case or contingency)
* when the DC loadflow is computed (for base case or contingency)
* when the flow decomposition results before rescaling are being built (for base case or contingency)
* when the computation is done

After AC and DC loadflows the observer has access to the network at that stage, as well as the loadflow result.
In this manner, it is possible to retrieve any information of the network after loadflow calculations.
In this manner, it is possible to retrieve any information of the network after loadflow
calculations. Additionally, the observer has access to the decomposed flows prior to the
rescaling step, enabling more effective testing and analysis of the available rescaling algorithms.

Note that these observers are meant to be used for testing purposes only.
Using observers impacts calculation performance and therefore are not suitable in production environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ private void decomposeFlowForState(Network network,
computeAllocatedAndLoopFlows(flowDecompositionResultsBuilder, nodalInjectionsMatrix, ptdfMatrix);
computePstFlows(network, flowDecompositionResultsBuilder, networkMatrixIndexes, psdfMatrix);

// Add the observes to keep the decomposed flows before rescaling
flowDecompositionResultsBuilder.addObserversList(observers);
flowDecompositionResultsBuilder.build(decomposedFlowRescaler, network);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public interface FlowDecompositionObserver {
*/
void computedAcLoadFlowResults(Network network, LoadFlowResult loadFlowResult, boolean fallbackHasBeenActivated);

/**
* Called before the scaling is performed
*
* @param decomposedFlow the decomposed flow on which the scaling is performed
*/
void computedPreRescalingDecomposedFlows(DecomposedFlow decomposedFlow);

/**
* Called after a DC loadflow has been computed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Guillaume Verger {@literal <guillaume.verger at artelys.com>}
* @author Caio Luke {@literal <caio.luke at artelys.com>}
*/
public class FlowDecompositionObserverList {
class FlowDecompositionObserverList {

private final List<FlowDecompositionObserver> observers;

Expand All @@ -34,6 +34,16 @@ public void removeObserver(FlowDecompositionObserver o) {
this.observers.remove(o);
}

public void addObserversFrom(FlowDecompositionObserverList otherList) {
for (FlowDecompositionObserver o : otherList.getObservers()) {
this.addObserver(o);
}
}

public List<FlowDecompositionObserver> getObservers() {
return this.observers;
}

public void runStart() {
for (FlowDecompositionObserver o : observers) {
o.runStart();
Expand Down Expand Up @@ -94,6 +104,12 @@ public void computedDcLoadFlowResults(Network network, LoadFlowRunningService.Re
}
}

public void computedPreRescalingDecomposedFlows(DecomposedFlow decomposedFlow) {
for (FlowDecompositionObserver o : observers) {
o.computedPreRescalingDecomposedFlows(decomposedFlow);
}
}

private void sendMatrix(MatrixNotification notification, SparseMatrixWithIndexesTriplet matrix) {
if (observers.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class PerStateBuilder {
private Map<String, Double> acCurrentTerminal1;
private Map<String, Double> acCurrentTerminal2;
private Map<String, Double> dcReferenceFlow;
private final FlowDecompositionObserverList observers = new FlowDecompositionObserverList();

PerStateBuilder(String contingencyId, Set<Branch> xnecList) {
this.xnecMap = xnecList.stream().collect(Collectors.toMap(Identifiable::getId, Function.identity()));
Expand Down Expand Up @@ -81,6 +82,10 @@ void saveDcReferenceFlow(Map<String, Double> dcReferenceFlow) {
this.dcReferenceFlow = dcReferenceFlow;
}

void addObserversList(FlowDecompositionObserverList observers) {
this.observers.addObserversFrom(observers);
}

void build(DecomposedFlowRescaler decomposedFlowRescaler, Network network) {
allocatedAndLoopFlowsMatrix.toMap()
.forEach((branchId, decomposedFlow) -> {
Expand Down Expand Up @@ -115,6 +120,7 @@ private DecomposedFlow createDecomposedFlow(String branchId, Map<String, Double>
.withInternalFlow(internalFlow)
.withLoopFlowsMap(loopFlowsMap)
.build();
observers.computedPreRescalingDecomposedFlows(decomposedFlow);
return decomposedFlowRescaler.rescale(decomposedFlow, network);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private enum Event {
COMPUTED_DC_NODAL_INJECTIONS,
COMPUTED_AC_FLOWS,
COMPUTED_DC_FLOWS,
COMPUTED_AC_CURRENTS
COMPUTED_AC_CURRENTS,
COMPUTED_PRE_RESCALING_DECOMPOSED_FLOWS
}

private static final String BASE_CASE = "base-case";
Expand Down Expand Up @@ -72,6 +73,7 @@ private static final class ObserverReport implements FlowDecompositionObserver {
private final ContingencyValue<Map<String, Double>> dcFlows = new ContingencyValue<>();
private final ContingencyValue<Map<String, Double>> acCurrentsTerminal1 = new ContingencyValue<>();
private final ContingencyValue<Map<String, Double>> acCurrentsTerminal2 = new ContingencyValue<>();
private final ContingencyValue<Map<String, DecomposedFlow>> preRescalingDecomposedFlows = new ContingencyValue<>();

public List<Event> allEvents() {
return events;
Expand Down Expand Up @@ -197,6 +199,12 @@ private void computedAcCurrentsTerminal2(Network network, boolean fallbackHasBee
this.acCurrentsTerminal2.put(currentContingency, currents);
}

@Override
public void computedPreRescalingDecomposedFlows(DecomposedFlow decomposedFlow) {
addEvent(Event.COMPUTED_PRE_RESCALING_DECOMPOSED_FLOWS);
this.preRescalingDecomposedFlows.put(currentContingency, Map.of(decomposedFlow.getBranchId(), decomposedFlow));
}

private void addEvent(Event e) {
if (currentContingency != null) {
eventsPerContingency.putIfAbsent(currentContingency, new LinkedList<>());
Expand Down Expand Up @@ -289,6 +297,9 @@ void testNStateN1AndN2PostContingencyState() {
"XDF00011 DF000011 1 + XDF00011 FD000011 1");

validateObserverReportFlows(report, List.of(BASE_CASE, contingencyId1, contingencyId2), allBranches);

var decomposedFlowBranches = Set.of("DB000011 DF000011 1");
validateObserverReportDecomposedFlows(report, List.of(contingencyId1), decomposedFlowBranches);
}

@Test
Expand Down Expand Up @@ -363,6 +374,9 @@ void testNStateN1LoadContingencyState() {
"XDF00011 DF000011 1 + XDF00011 FD000011 1");

validateObserverReportFlows(report, List.of(contingencyId1), allBranches);

var decomposedFlowBranches = Set.of("DB000011 DF000011 1");
validateObserverReportDecomposedFlows(report, List.of(contingencyId1), decomposedFlowBranches);
}

private static void validateObserverReportLoadFlowResult(ObserverReport report, List<String> contingencyIds) {
Expand All @@ -387,7 +401,8 @@ private static void validateObserverReportEvents(ObserverReport report, List<Str
Event.COMPUTED_DC_NODAL_INJECTIONS,
Event.COMPUTED_NODAL_INJECTIONS_MATRIX,
Event.COMPUTED_PTDF_MATRIX,
Event.COMPUTED_PSDF_MATRIX);
Event.COMPUTED_PSDF_MATRIX,
Event.COMPUTED_PRE_RESCALING_DECOMPOSED_FLOWS);
}

for (var contingencyId : contingencyIds) {
Expand All @@ -398,7 +413,8 @@ private static void validateObserverReportEvents(ObserverReport report, List<Str
Event.COMPUTED_DC_NODAL_INJECTIONS,
Event.COMPUTED_NODAL_INJECTIONS_MATRIX,
Event.COMPUTED_PTDF_MATRIX,
Event.COMPUTED_PSDF_MATRIX);
Event.COMPUTED_PSDF_MATRIX,
Event.COMPUTED_PRE_RESCALING_DECOMPOSED_FLOWS);
}
}

Expand Down Expand Up @@ -444,6 +460,12 @@ private static void validateObserverReportFlows(ObserverReport report, List<Stri
}
}

private static void validateObserverReportDecomposedFlows(ObserverReport report, List<String> caseIds, Set<String> allBranches) {
for (var contingencyId : caseIds) {
assertEquals(allBranches, report.preRescalingDecomposedFlows.forContingency(contingencyId).keySet());
}
}

@Test
void testRemoveObserver() {
String networkFileName = "19700101_0000_FO4_UX1.uct";
Expand Down

0 comments on commit 55ad9ca

Please sign in to comment.