-
Notifications
You must be signed in to change notification settings - Fork 4
/
VoltageMonitoring.java
365 lines (338 loc) · 18.6 KB
/
VoltageMonitoring.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (c) 2022, 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/.
*/
package com.powsybl.openrao.monitoring.voltagemonitoring;
import com.powsybl.computation.ComputationManager;
import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.*;
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
import com.powsybl.openrao.data.cracapi.networkaction.NetworkAction;
import com.powsybl.openrao.data.cracapi.usagerule.OnConstraint;
import com.powsybl.openrao.data.raoresultapi.RaoResult;
import com.powsybl.openrao.util.AbstractNetworkPool;
import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.BusbarSection;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import static com.powsybl.openrao.commons.logs.OpenRaoLoggerProvider.*;
/**
* Monitors voltage of VoltageCnecs
*
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
*/
public class VoltageMonitoring {
public static final String CONTINGENCY_ERROR = "At least one contingency could not be monitored. This should not happen.";
public static final String VOLTAGE_MONITORING_END = "----- Voltage monitoring [end]";
private final Crac crac;
private final Network network;
private final RaoResult raoResult;
private List<VoltageMonitoringResult> stateSpecificResults;
public VoltageMonitoring(Crac crac, Network network, RaoResult raoResult) {
this.crac = crac;
this.network = network;
this.raoResult = raoResult;
}
/**
* Main function : runs VoltageMonitoring computation on all VoltageCnecs defined in the CRAC.
* Returns an RaoResult enhanced with VoltageMonitoringResult
*/
public RaoResult runAndUpdateRaoResult(String loadFlowProvider, LoadFlowParameters loadFlowParameters, int numberOfLoadFlowsInParallel) {
return new RaoResultWithVoltageMonitoring(raoResult, run(loadFlowProvider, loadFlowParameters, numberOfLoadFlowsInParallel));
}
/**
* Main function : runs VoltageMonitoring computation on all VoltageCnecs defined in the CRAC.
* Returns an VoltageMonitoringResult
*/
@Deprecated
public VoltageMonitoringResult run(String loadFlowProvider, LoadFlowParameters loadFlowParameters, int numberOfLoadFlowsInParallel) {
BUSINESS_LOGS.info("----- Voltage monitoring [start]");
stateSpecificResults = new ArrayList<>();
if (crac.getVoltageCnecs().isEmpty()) {
BUSINESS_WARNS.warn("No VoltageCnecs defined.");
stateSpecificResults.add(new VoltageMonitoringResult(Collections.emptyMap(), Collections.emptyMap(), VoltageMonitoringResult.Status.SECURE));
return assembleVoltageMonitoringResults();
}
// I) Preventive state
if (Objects.nonNull(crac.getPreventiveState())) {
applyOptimalRemedialActions(crac.getPreventiveState(), network);
stateSpecificResults.add(monitorVoltageCnecsAndLog(loadFlowProvider, loadFlowParameters, crac.getPreventiveState(), network));
}
// II) Curative states
Set<State> contingencyStates = crac.getVoltageCnecs().stream().map(Cnec::getState).filter(state -> !state.isPreventive()).collect(Collectors.toSet());
if (contingencyStates.isEmpty()) {
BUSINESS_LOGS.info(VOLTAGE_MONITORING_END);
return assembleVoltageMonitoringResults();
}
try {
try (AbstractNetworkPool networkPool = AbstractNetworkPool.create(network, network.getVariantManager().getWorkingVariantId(), Math.min(numberOfLoadFlowsInParallel, contingencyStates.size()), true)) {
List<ForkJoinTask<Object>> tasks = contingencyStates.stream().map(state ->
networkPool.submit(() -> {
Network networkClone = networkPool.getAvailableNetwork();
try {
Contingency contingency = state.getContingency().orElseThrow();
if (!contingency.isValid(networkClone)) {
throw new OpenRaoException("Unable to apply contingency " + contingency.getId());
}
contingency.toModification().apply(networkClone, (ComputationManager) null);
applyOptimalRemedialActionsOnContingencyState(state, networkClone);
stateSpecificResults.add(monitorVoltageCnecsAndLog(loadFlowProvider, loadFlowParameters, state, networkClone));
} catch (Exception e) {
Thread.currentThread().interrupt();
throw new OpenRaoException(CONTINGENCY_ERROR, e);
}
networkPool.releaseUsedNetwork(networkClone);
return null;
})).toList();
for (ForkJoinTask<Object> task : tasks) {
try {
task.get();
} catch (ExecutionException e) {
throw new OpenRaoException(e);
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return assembleVoltageMonitoringResults();
}
/**
* Gathers optimal remedial actions retrieved from raoResult for a given state on network.
* For curative states, consider auto (when they exist) and curative states.
*/
private void applyOptimalRemedialActionsOnContingencyState(State state, Network networkClone) {
if (state.getInstant().isCurative()) {
Optional<Contingency> contingency = state.getContingency();
if (contingency.isPresent()) {
crac.getStates(contingency.get()).forEach(contingencyState ->
applyOptimalRemedialActions(state, networkClone));
} else {
throw new OpenRaoException(String.format("Curative state %s was defined without a contingency", state.getId()));
}
} else {
applyOptimalRemedialActions(state, networkClone);
}
}
/**
* Applies optimal remedial actions retrieved from raoResult for a given state on network.
*/
private void applyOptimalRemedialActions(State state, Network network) {
raoResult.getActivatedNetworkActionsDuringState(state)
.forEach(na -> na.apply(network));
raoResult.getActivatedRangeActionsDuringState(state)
.forEach(ra -> ra.apply(network, raoResult.getOptimizedSetPointOnState(state, ra)));
}
private VoltageMonitoringResult monitorVoltageCnecsAndLog(String loadFlowProvider, LoadFlowParameters loadFlowParameters, State state, Network networkClone) {
BUSINESS_LOGS.info("-- Monitoring voltages at state \"{}\" [start]", state);
VoltageMonitoringResult result = monitorVoltageCnecs(loadFlowProvider, loadFlowParameters, state, networkClone);
result.printConstraints().forEach(BUSINESS_LOGS::info);
BUSINESS_LOGS.info("-- Monitoring voltages at state \"{}\" [end]", state);
return result;
}
/**
* VoltageMonitoring computation on all VoltageCnecs in the CRAC for a given state.
* Returns an VoltageMonitoringResult.
*/
private VoltageMonitoringResult monitorVoltageCnecs(String loadFlowProvider, LoadFlowParameters loadFlowParameters, State state, Network networkClone) {
//First load flow with only preventive action, it is supposed to converge
if (!computeLoadFlow(loadFlowProvider, loadFlowParameters, networkClone)) {
return catchVoltageMonitoringResult(state, VoltageMonitoringResult.Status.UNKNOWN);
}
// Check for threshold overshoot for the voltages of each cnec
Set<RemedialAction<?>> appliedNetworkActions = new TreeSet<>(Comparator.comparing(RemedialAction::getId));
Map<VoltageCnec, ExtremeVoltageValues> voltageValues = computeVoltages(crac.getVoltageCnecs(state), networkClone);
for (Map.Entry<VoltageCnec, ExtremeVoltageValues> voltages : voltageValues.entrySet()) {
VoltageCnec voltageCnec = voltages.getKey();
//If there is a threshold overshoot, apply topological network action
if (thresholdOvershoot(voltageCnec, voltages.getValue())) {
Set<NetworkAction> availableNetworkActions = getVoltageCnecNetworkActions(state, voltageCnec);
appliedNetworkActions.addAll(applyTopologicalNetworkActions(networkClone, availableNetworkActions));
}
}
// If some action were applied, recompute a loadflow. If the loadflow doesn't converge, it is unsecure
if (!appliedNetworkActions.isEmpty() && !computeLoadFlow(loadFlowProvider, loadFlowParameters, networkClone)) {
BUSINESS_WARNS.warn("Load-flow computation failed at state {} after applying RAs. Skipping this state.", state);
return new VoltageMonitoringResult(voltageValues, new HashMap<>(), VoltageMonitoringResult.getUnsecureStatus(voltageValues));
}
Map<State, Set<RemedialAction<?>>> appliedRa = new HashMap<>();
if (!appliedNetworkActions.isEmpty()) {
appliedRa.put(state, appliedNetworkActions);
}
VoltageMonitoringResult.Status status = VoltageMonitoringResult.Status.SECURE;
//Check that with the curative action, the new voltage don't overshoot the threshold, else it is unsecure
Map<VoltageCnec, ExtremeVoltageValues> newVoltageValues = computeVoltages(crac.getVoltageCnecs(state), networkClone);
if (newVoltageValues.entrySet().stream().anyMatch(entrySet -> thresholdOvershoot(entrySet.getKey(), entrySet.getValue()))) {
status = VoltageMonitoringResult.getUnsecureStatus(newVoltageValues);
}
return new VoltageMonitoringResult(newVoltageValues, appliedRa, status);
}
private Map<VoltageCnec, ExtremeVoltageValues> computeVoltages(Set<VoltageCnec> voltageCnecs, Network networkClone) {
Map<VoltageCnec, ExtremeVoltageValues> voltagePerCnec = new HashMap<>();
voltageCnecs.forEach(vc -> {
VoltageLevel voltageLevel = networkClone.getVoltageLevel(vc.getNetworkElement().getId());
Set<Double> voltages = null;
if (voltageLevel != null) {
voltages = voltageLevel.getBusView().getBusStream().map(Bus::getV).collect(Collectors.toSet());
}
BusbarSection busbarSection = networkClone.getBusbarSection(vc.getNetworkElement().getId());
if (busbarSection != null) {
Double busBarVoltages = busbarSection.getV();
voltages = new HashSet<>();
voltages.add(busBarVoltages);
}
if (voltageLevel != null) {
voltagePerCnec.put(vc, new ExtremeVoltageValues(voltages));
}
});
return voltagePerCnec;
}
/**
* Compares an voltageCnec's thresholds to a voltage (parameter).
* Returns true if a threshold is breached.
*/
private static boolean thresholdOvershoot(VoltageCnec voltageCnec, ExtremeVoltageValues voltages) {
return voltageCnec.getThresholds().stream()
.anyMatch(threshold -> threshold.limitsByMax() && voltages != null && voltages.getMax() > threshold.max().orElseThrow())
|| voltageCnec.getThresholds().stream()
.anyMatch(threshold -> threshold.limitsByMin() && voltages != null && voltages.getMin() < threshold.min().orElseThrow());
}
/**
* Retrieves the network actions that were defined for a VoltageCnec (parameter) in a given state (parameter).
* Preventive network actions are filtered.
*/
private Set<NetworkAction> getVoltageCnecNetworkActions(State state, VoltageCnec voltageCnec) {
Set<RemedialAction<?>> availableRemedialActions =
crac.getRemedialActions().stream()
.filter(remedialAction ->
remedialAction.getUsageRules().stream().filter(OnConstraint.class::isInstance)
.map(OnConstraint.class::cast)
.anyMatch(onVoltageConstraint -> onVoltageConstraint.getCnec().equals(voltageCnec)))
.collect(Collectors.toSet());
if (availableRemedialActions.isEmpty()) {
BUSINESS_WARNS.warn("VoltageCnec {} in state {} has no associated RA. Voltage constraint cannot be secured.", voltageCnec.getId(), state.getId());
return Collections.emptySet();
}
if (state.isPreventive()) {
BUSINESS_WARNS.warn("VoltageCnec {} is constrained in preventive state, it cannot be secured.", voltageCnec.getId());
return Collections.emptySet();
}
// Convert remedial actions to network actions
Set<NetworkAction> networkActions = availableRemedialActions.stream().filter(remedialAction -> {
if (remedialAction instanceof NetworkAction) {
return true;
} else {
BUSINESS_WARNS.warn("Remedial action {} of VoltageCnec {} in state {} is ignored : it's not a network action.", remedialAction.getId(), voltageCnec.getId(), state.getId());
return false;
}
}).map(NetworkAction.class::cast).collect(Collectors.toSet());
BUSINESS_LOGS.info("Applying the following remedial action(s) in order to reduce constraints on CNEC \"{}\": {}", voltageCnec.getId(), networkActions.stream().map(com.powsybl.openrao.data.cracapi.Identifiable::getId).collect(Collectors.joining(", ")));
return networkActions;
}
/**
* Apply any topological network action
*
* @param networkClone
* @param availableNetworkActions
* @return the set of applied network action
*/
private Set<NetworkAction> applyTopologicalNetworkActions(Network networkClone, Set<NetworkAction> availableNetworkActions) {
Set<NetworkAction> topologicalNetworkActionsAdded = new HashSet<>();
for (NetworkAction na : availableNetworkActions) {
na.apply(networkClone);
topologicalNetworkActionsAdded.add(na);
}
return topologicalNetworkActionsAdded;
}
/**
* Runs a LoadFlow computation
* Returns false if loadFlow has not converged.
*/
private boolean computeLoadFlow(String loadFlowProvider, LoadFlowParameters loadFlowParameters, Network networkClone) {
TECHNICAL_LOGS.info("Load-flow computation [start]");
LoadFlowResult loadFlowResult = LoadFlow.find(loadFlowProvider)
.run(networkClone, loadFlowParameters);
if (!loadFlowResult.isOk()) {
BUSINESS_WARNS.warn("LoadFlow error.");
}
TECHNICAL_LOGS.info("Load-flow computation [end]");
return loadFlowResult.isOk();
}
/**
* Assembles all VoltageMonitoringResults computed.
* Individual VoltageResults and appliedCras maps are concatenated.
* Global status :
* - SECURE if all VoltageMonitoringResults are SECURE.
* - HIGH_AND_LOW_VOLTAGE_CONSTRAINT if any AngleMonitoringResult is HIGH_AND_LOW_VOLTAGE_CONSTRAINT
* or if an AngleMonitoringResult is LOW_VOLTAGE_CONSTRAINT and another is HIGH_VOLTAGE_CONSTRAINT.
* - HIGH/LOW_VOLTAGE_CONSTRAINT if any AngleMonitoringResult is HIGH/LOW_VOLTAGE_CONSTRAINT.
* - UNKNOWN if any AngleMonitoringResult is UNKNOWN and no AngleMonitoringResult is UNSECURE.
*/
private VoltageMonitoringResult assembleVoltageMonitoringResults() {
Map<VoltageCnec, ExtremeVoltageValues> extremeVoltageValuesMap = new HashMap<>();
Map<State, Set<RemedialAction<?>>> appliedRas = new HashMap<>();
stateSpecificResults.forEach(s -> {
extremeVoltageValuesMap.putAll(s.getExtremeVoltageValues());
appliedRas.putAll(s.getAppliedRas());
});
VoltageMonitoringResult.Status securityStatus = concatenateSpecificResults();
VoltageMonitoringResult result = new VoltageMonitoringResult(extremeVoltageValuesMap, appliedRas, securityStatus);
result.printConstraints().forEach(BUSINESS_LOGS::info);
BUSINESS_LOGS.info(VOLTAGE_MONITORING_END);
return result;
}
private VoltageMonitoringResult.Status concatenateSpecificResults() {
if (stateSpecificResults.isEmpty()) {
return VoltageMonitoringResult.Status.UNKNOWN;
}
AtomicBoolean atLeastOneHigh = new AtomicBoolean(false);
AtomicBoolean atLeastOneLow = new AtomicBoolean(false);
AtomicBoolean atLeastOneUnknown = new AtomicBoolean(false);
stateSpecificResults.forEach(result -> {
switch (result.getStatus()) {
case HIGH_VOLTAGE_CONSTRAINT -> atLeastOneHigh.set(true);
case LOW_VOLTAGE_CONSTRAINT -> atLeastOneLow.set(true);
case HIGH_AND_LOW_VOLTAGE_CONSTRAINTS -> {
atLeastOneHigh.set(true);
atLeastOneLow.set(true);
}
case UNKNOWN -> atLeastOneUnknown.set(true);
case SECURE -> { }
}
}
);
if (atLeastOneHigh.get() && atLeastOneLow.get()) {
return VoltageMonitoringResult.Status.HIGH_AND_LOW_VOLTAGE_CONSTRAINTS;
}
if (atLeastOneHigh.get()) {
return VoltageMonitoringResult.Status.HIGH_VOLTAGE_CONSTRAINT;
}
if (atLeastOneLow.get()) {
return VoltageMonitoringResult.Status.LOW_VOLTAGE_CONSTRAINT;
}
if (atLeastOneUnknown.get()) {
return VoltageMonitoringResult.Status.UNKNOWN;
}
return VoltageMonitoringResult.Status.SECURE;
}
private VoltageMonitoringResult catchVoltageMonitoringResult(State state, VoltageMonitoringResult.Status securityStatus) {
BUSINESS_WARNS.warn("Load-flow computation failed at state {}. Skipping this state.", state);
Map<VoltageCnec, ExtremeVoltageValues> voltagePerCnec = new HashMap<>();
crac.getVoltageCnecs(state).forEach(vc ->
voltagePerCnec.put(vc, new ExtremeVoltageValues(new HashSet<>(Arrays.asList(Double.NaN))))
);
return new VoltageMonitoringResult(voltagePerCnec, new HashMap<>(), securityStatus);
}
}