Skip to content

Commit

Permalink
Handle sided contingencies in DynaFlow and Dynawo SA (#412)
Browse files Browse the repository at this point in the history
* Handle sided contingencies in DynaFlow and Dynawo SA
* Add ContingencyEventModelsTest

Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte authored Dec 13, 2024
1 parent e226a1d commit c16c095
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import com.powsybl.computation.CommandExecution;
import com.powsybl.computation.ExecutionReport;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.SidedContingencyElement;
import com.powsybl.contingency.contingency.list.ContingencyList;
import com.powsybl.contingency.contingency.list.DefaultContingencyList;
import com.powsybl.contingency.json.ContingencyJsonModule;
import com.powsybl.dynaflow.json.DynaFlowConfigSerializer;
import com.powsybl.dynaflow.results.ContingencyResultsUtils;
Expand All @@ -33,6 +35,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;

import static com.powsybl.dynaflow.DynaFlowConstants.CONFIG_FILENAME;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONTINGENCIES_FILENAME;
Expand Down Expand Up @@ -87,16 +90,30 @@ public SecurityAnalysisReport after(Path workingDir, ExecutionReport report) thr
return new SecurityAnalysisReport(createSecurityAnalysisResult(network, violationFilter, workingDir, contingencies));
}

private static void writeContingencies(List<Contingency> contingencies, Path workingDir) throws IOException {
private void writeContingencies(List<Contingency> contingencies, Path workingDir) throws IOException {
try (OutputStream os = Files.newOutputStream(workingDir.resolve(CONTINGENCIES_FILENAME))) {
ObjectMapper mapper = JsonUtil.createObjectMapper();
ContingencyJsonModule module = new ContingencyJsonModule();
mapper.registerModule(module);
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValue(os, ContingencyList.of(contingencies.toArray(Contingency[]::new)));
writer.writeValue(os, buildContingencyList(contingencies));
}
}

private ContingencyList buildContingencyList(List<Contingency> contingencies) {
return new DefaultContingencyList("", contingencies.stream().filter(nonSidedContingency()).toList());
}

private Predicate<Contingency> nonSidedContingency() {
return c -> {
if (c instanceof SidedContingencyElement sidedC && sidedC.getVoltageLevelId() != null) {
DynaflowReports.createSidedContingencyReportNode(reportNode, c.getId());
return false;
}
return true;
};
}

private static void writeParameters(SecurityAnalysisParameters securityAnalysisParameters, Path workingDir) throws IOException {
// TODO(Luma) Take into account also Security Analysis parameters
LoadFlowParameters loadFlowParameters = securityAnalysisParameters.getLoadFlowParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public static ReportNode createContingenciesTimelineReportNode(ReportNode report
.withUntypedValue("contingencyId", contingencyId)
.add();
}

public static void createSidedContingencyReportNode(ReportNode reportNode, String contingencyId) {
reportNode.newReportNode()
.withMessageTemplate("saContingency", "Contingency '${contingencyId}' has a voltageId information and cannot be handle by DynaFlow, the contingency will be skipped")
.withUntypedValue("contingencyId", contingencyId)
.add();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ void testIeee14DSA(String criteriaPath, List<Contingency> contingencies, String
private static Stream<Arguments> provideSimulationParameter() {
return Stream.of(
Arguments.of("/ieee14/dynamic-security-analysis/convergence/criteria.crt",
List.of(Contingency.line("_BUS____1-BUS____5-1_AC"),
List.of(Contingency.line("_BUS____1-BUS____5-1_AC", "_BUS____5_VL"),
Contingency.generator("_GEN____2_SM")),
"/ieee14/dynamic-security-analysis/convergence/results.json"),
Arguments.of("/ieee14/dynamic-security-analysis/failed-criteria/criteria.crt",
List.of(Contingency.line("_BUS____1-BUS____5-1_AC")),
List.of(Contingency.line("_BUS____1-BUS____5-1_AC", "_BUS____5_VL")),
"/ieee14/dynamic-security-analysis/failed-criteria/results.json"),
Arguments.of("/ieee14/dynamic-security-analysis/divergence/criteria.crt",
List.of(Contingency.builder("Disconnect")
.addLine("_BUS____1-BUS____5-1_AC")
.addLine("_BUS____1-BUS____5-1_AC", "_BUS____5_VL")
.addGenerator("_GEN____2_SM")
.addBus("_BUS____1_TN")
.build()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"id" : "_BUS____1-BUS____5-1_AC",
"elements" : [ {
"id" : "_BUS____1-BUS____5-1_AC",
"type" : "LINE"
"type" : "LINE",
"voltageLevelId" : "_BUS____5_VL"
} ]
},
"status" : "CONVERGED",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"id" : "Disconnect",
"elements" : [ {
"id" : "_BUS____1-BUS____5-1_AC",
"type" : "LINE"
"type" : "LINE",
"voltageLevelId" : "_BUS____5_VL"
}, {
"id" : "_GEN____2_SM",
"type" : "GENERATOR"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"id" : "_BUS____1-BUS____5-1_AC",
"elements" : [ {
"id" : "_BUS____1-BUS____5-1_AC",
"type" : "LINE"
"type" : "LINE",
"voltageLevelId" : "_BUS____5_VL"
} ]
},
"status" : "FAILED",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.security;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyElement;
import com.powsybl.contingency.SidedContingencyElement;
import com.powsybl.dynawo.DynawoSimulationContext;
import com.powsybl.dynawo.models.BlackBoxModel;
import com.powsybl.dynawo.models.events.ContextDependentEvent;
import com.powsybl.dynawo.models.events.EventDisconnectionBuilder;
import com.powsybl.dynawo.models.macroconnections.MacroConnect;
import com.powsybl.dynawo.models.macroconnections.MacroConnectionsAdder;
import com.powsybl.dynawo.models.macroconnections.MacroConnector;
import com.powsybl.dynawo.parameters.ParametersSet;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.TwoSides;

import java.util.*;

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public final class ContingencyEventModelsFactory {

public static List<ContingencyEventModels> createFrom(List<Contingency> contingencies, DynawoSimulationContext context,
MacroConnectionsAdder macroConnectionsAdder,
double contingenciesStartTime,
ReportNode reportNode) {
return contingencies.stream()
.map(c -> createFrom(c, context, macroConnectionsAdder, contingenciesStartTime, reportNode))
.filter(Objects::nonNull)
.toList();
}

public static ContingencyEventModels createFrom(Contingency contingency, DynawoSimulationContext context,
MacroConnectionsAdder macroConnectionsAdder,
double contingenciesStartTime,
ReportNode reportNode) {
List<BlackBoxModel> eventModels = createContingencyEventModelList(contingency, context, contingenciesStartTime, reportNode);
if (eventModels.isEmpty()) {
return null;
}
Map<String, MacroConnector> macroConnectorsMap = new HashMap<>();
List<MacroConnect> macroConnectList = new ArrayList<>();
List<ParametersSet> eventParameters = new ArrayList<>(eventModels.size());
// Set Contingencies connections and parameters
macroConnectionsAdder.setMacroConnectorAdder(macroConnectorsMap::computeIfAbsent);
macroConnectionsAdder.setMacroConnectAdder(macroConnectList::add);
eventModels.forEach(em -> {
em.createMacroConnections(macroConnectionsAdder);
em.createDynamicModelParameters(context, eventParameters::add);
});
return new ContingencyEventModels(contingency, eventModels, macroConnectorsMap, macroConnectList, eventParameters);
}

private static List<BlackBoxModel> createContingencyEventModelList(Contingency contingency,
DynawoSimulationContext context,
double contingenciesStartTime,
ReportNode reportNode) {
return contingency.getElements().stream()
.map(ce -> createContingencyEventModel(ce, context, contingenciesStartTime, reportNode))
.filter(Objects::nonNull)
.toList();
}

private static BlackBoxModel createContingencyEventModel(ContingencyElement element,
DynawoSimulationContext context,
double contingenciesStartTime,
ReportNode reportNode) {
Network network = context.getNetwork();
EventDisconnectionBuilder builder = EventDisconnectionBuilder.of(network)
.staticId(element.getId())
.startTime(contingenciesStartTime);
if (element instanceof SidedContingencyElement sidedElement && sidedElement.getVoltageLevelId() != null) {
TwoSides side = SidedContingencyElement.getContingencySide(network, sidedElement);
if (side != null) {
builder.disconnectOnly(side);
} else {
DynamicSecurityAnalysisReports.createContingencyVoltageIdNotFoundReportNode(reportNode,
sidedElement.getId(), sidedElement.getVoltageLevelId());
return null;
}
}
BlackBoxModel bbm = builder.build();
if (bbm == null) {
throw new PowsyblException("Contingency element " + element.getType() + " not supported");
}
if (bbm instanceof ContextDependentEvent cde) {
cde.setEquipmentHasDynamicModel(context);
}
return bbm;
}

private ContingencyEventModelsFactory() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public static ReportNode createDynamicSecurityAnalysisReportNode(ReportNode repo
.withUntypedValue("networkId", networkId)
.add();
}

public static ReportNode createContingencyVoltageIdNotFoundReportNode(ReportNode reportNode, String contingencyId, String voltageLevelId) {
return reportNode.newReportNode()
.withMessageTemplate("contingencyVlIdNotFound", "Voltage id '${voltageLevelId}' of contingency '${contingencyId}' not found, contingency will be skipped")
.withUntypedValue("voltageLevelId", voltageLevelId)
.withUntypedValue("contingencyId", contingencyId)
.add();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@
*/
package com.powsybl.dynawo.security;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyElement;
import com.powsybl.dynawo.DynawoSimulationContext;
import com.powsybl.dynawo.DynawoSimulationParameters;
import com.powsybl.dynawo.commons.DynawoConstants;
import com.powsybl.dynawo.commons.DynawoVersion;
import com.powsybl.dynawo.models.BlackBoxModel;
import com.powsybl.dynawo.models.events.ContextDependentEvent;
import com.powsybl.dynawo.models.events.EventDisconnectionBuilder;
import com.powsybl.dynawo.models.macroconnections.MacroConnect;
import com.powsybl.dynawo.models.macroconnections.MacroConnector;
import com.powsybl.dynawo.parameters.ParametersSet;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.*;
import com.powsybl.security.dynamic.DynamicSecurityAnalysisParameters;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
Expand Down Expand Up @@ -53,40 +45,7 @@ public SecurityAnalysisContext(Network network, String workingVariantId,
parameters.getDynamicSimulationParameters(), dynawoSimulationParameters, currentVersion, ReportNode.NO_OP);
double contingenciesStartTime = parameters.getDynamicContingenciesParameters().getContingenciesStartTime();
this.contingencies = contingencies;
this.contingencyEventModels = contingencies.stream()
.map(c -> {
List<BlackBoxModel> contEventModels = c.getElements().stream()
.map(ce -> {
BlackBoxModel bbm = this.createContingencyEventModel(ce, contingenciesStartTime);
if (bbm instanceof ContextDependentEvent cde) {
cde.setEquipmentHasDynamicModel(this);
}
return bbm;
})
.collect(Collectors.toList());
Map<String, MacroConnector> macroConnectorsMap = new HashMap<>();
List<MacroConnect> macroConnects = new ArrayList<>();
List<ParametersSet> parametersSets = new ArrayList<>(contEventModels.size());
macroConnectionsAdder.setMacroConnectorAdder(macroConnectorsMap::computeIfAbsent);
macroConnectionsAdder.setMacroConnectAdder(macroConnects::add);
for (BlackBoxModel bbm : contEventModels) {
bbm.createMacroConnections(macroConnectionsAdder);
bbm.createDynamicModelParameters(this, parametersSets::add);
}
return new ContingencyEventModels(c, contEventModels, macroConnectorsMap, macroConnects, parametersSets);
})
.collect(Collectors.toList());
}

private BlackBoxModel createContingencyEventModel(ContingencyElement element, double contingenciesStartTime) {
BlackBoxModel bbm = EventDisconnectionBuilder.of(network)
.staticId(element.getId())
.startTime(contingenciesStartTime)
.build();
if (bbm == null) {
throw new PowsyblException("Contingency element " + element.getType() + " not supported");
}
return bbm;
this.contingencyEventModels = ContingencyEventModelsFactory.createFrom(contingencies, this, macroConnectionsAdder, contingenciesStartTime, ReportNode.NO_OP);
}

public List<Contingency> getContingencies() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.security;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.contingency.Contingency;
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
import com.powsybl.dynawo.DynawoSimulationContext;
import com.powsybl.dynawo.DynawoSimulationParameters;
import com.powsybl.dynawo.models.BlackBoxModel;
import com.powsybl.dynawo.models.generators.BaseGeneratorBuilder;
import com.powsybl.dynawo.models.macroconnections.MacroConnectionsAdder;
import com.powsybl.dynawo.models.macroconnections.MacroConnector;
import com.powsybl.dynawo.parameters.ParametersSet;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import org.junit.jupiter.api.Test;

import java.util.*;

import static com.powsybl.iidm.network.test.EurostagTutorialExample1Factory.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
class ContingencyEventModelsTest {

@Test
void test() {
Network network = EurostagTutorialExample1Factory.create();
List<BlackBoxModel> dynamicModels = List.of(
BaseGeneratorBuilder.of(network)
.staticId("GEN")
.parameterSetId("gen")
.build());
DynawoSimulationContext context = setupDynawoContext(network, dynamicModels);
MacroConnectionsAdder macroConnectionsAdder = new MacroConnectionsAdder(context::getDynamicModel,
context::getPureDynamicModel,
new ArrayList<>()::add,
new HashMap<String, MacroConnector>()::computeIfAbsent,
ReportNode.NO_OP);
List<Contingency> contingencies = List.of(
Contingency.load("LOAD"),
Contingency.generator("GEN"),
Contingency.line(NHV1_NHV2_1, VLHV1),
Contingency.branch(NHV1_NHV2_2, "WRONG_ID"));

List<ContingencyEventModels> contingencyEvents = ContingencyEventModelsFactory.createFrom(contingencies, context,
macroConnectionsAdder, 2, ReportNode.NO_OP);
assertThat(contingencyEvents).hasSize(3);
assertThat(contingencyEvents.get(0).eventModels())
.hasSize(1)
.map(BlackBoxModel::getLib).containsExactly("EventConnectedStatus");
assertThat(contingencyEvents.get(1).eventModels())
.hasSize(1)
.map(BlackBoxModel::getLib).containsExactly("EventSetPointBoolean");
assertThat(contingencyEvents.get(2).eventModels())
.hasSize(1)
.map(BlackBoxModel::getLib).containsExactly("EventQuadripoleDisconnection");
ParametersSet parametersSet = contingencyEvents.get(2).eventParameters().get(0);
assertTrue(parametersSet.getBool("event_disconnectOrigin"));
assertFalse(parametersSet.getBool("event_disconnectExtremity"));
}

private DynawoSimulationContext setupDynawoContext(Network network, List<BlackBoxModel> dynamicModels) {
DynamicSimulationParameters parameters = DynamicSimulationParameters.load();
DynawoSimulationParameters dynawoParameters = DynawoSimulationParameters.load();
return new DynawoSimulationContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels,
Collections.emptyList(), Collections.emptyList(), parameters, dynawoParameters);
}
}

0 comments on commit c16c095

Please sign in to comment.