Skip to content

Commit

Permalink
Add dynawo dynamic security analysis (#277)
Browse files Browse the repository at this point in the history
* Refactor Macro connections creation: add MacroConnectionsAdder, MultipleJobsXml, ContingenciesDydXml && ContingenciesParXml
* Refactor providers
* Add DynaWaltzSecurityAnalysisProvider and Handler
* Use DynamicSecurityAnalysisParameters
* Fix contingencies Dyd parameter file name
* Create dynamic security analysis module
* Refactor provider/handler/config
* Add integration test
* Rename dynawaltz to dynawo for DSA
* Update readthedocs

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte authored Jul 3, 2024
1 parent 6a1e6cf commit 0531353
Show file tree
Hide file tree
Showing 67 changed files with 2,036 additions and 578 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
/**
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
*/
public class DynawoConfig {
public abstract class AbstractDynawoConfig {

private static final boolean DEBUG_DEFAULT = false;

protected static <T extends DynawoConfig> T load(Function<ModuleConfig, T> configFactory, String moduleName) {
protected static <T extends AbstractDynawoConfig> T load(Function<ModuleConfig, T> configFactory, String moduleName) {
return load(configFactory, moduleName, PlatformConfig.defaultConfig());
}

protected static <T extends DynawoConfig> T load(Function<ModuleConfig, T> configFactory, String moduleName, PlatformConfig platformConfig) {
protected static <T extends AbstractDynawoConfig> T load(Function<ModuleConfig, T> configFactory, String moduleName, PlatformConfig platformConfig) {
return platformConfig.getOptionalModuleConfig(moduleName)
.map(configFactory)
.orElseThrow(() -> new PowsyblException("PlatformConfig incomplete: Module " + moduleName + " not found"));
Expand All @@ -36,12 +36,12 @@ protected static <T extends DynawoConfig> T load(Function<ModuleConfig, T> confi
private final Path homeDir;
private final boolean debug;

public DynawoConfig(Path homeDir, boolean debug) {
protected AbstractDynawoConfig(Path homeDir, boolean debug) {
this.homeDir = Objects.requireNonNull(homeDir);
this.debug = debug;
}

public DynawoConfig(ModuleConfig config) {
protected AbstractDynawoConfig(ModuleConfig config) {
this(config.getPathProperty("homeDir"), config.getBooleanProperty("debug", DEBUG_DEFAULT));
}

Expand All @@ -53,9 +53,7 @@ public boolean isDebug() {
return debug;
}

public String getProgram() {
return getProgram(DynawoConstants.DYNAWO_CMD_NAME);
}
public abstract String getProgram();

public String getProgram(String programName) {
String extension = SystemUtils.IS_OS_WINDOWS ? ".cmd" : ".sh";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ private DynawoConstants() {
*/
public static final String IIDM_VERSION = IidmVersion.V_1_4.toString(".");

public static final String DYNAWO_CMD_NAME = "dynawo";

public static final String DYNAWO_TIMELINE_FOLDER = "timeLine";

public static final DynawoVersion VERSION_MIN = new DynawoVersion(1, 5, 0);
Expand All @@ -37,4 +35,6 @@ private DynawoConstants() {
"hvdcAngleDroopActivePowerControl",
"hvdcOperatorActivePowerRange",
"standbyAutomaton");

public static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public final class DynawoUtil {
private DynawoUtil() {
}

public static List<CommandExecution> getCommandExecutions(Command command) {
return Collections.singletonList(new CommandExecution(command, 1, 0));
}

public static void writeIidm(Network network, Path file) {
Objects.requireNonNull(network);
Objects.requireNonNull(file);
Expand Down
4 changes: 4 additions & 0 deletions distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-dynawaltz-dsl</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-dynawo-security-analysis</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-dynawo-integration-tests</artifactId>
Expand Down
30 changes: 30 additions & 0 deletions docs/dynamic_security_analysis/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Configuration

## Dynawo-algorithms properties
The `dynawo-algorithms` module defines the required parameters to run with Dynawo.

**homeDir**
Use the `homeDir` property to defines the installation directory of the dynawo simulator.

**debug**
Use the `debug` property to specify if the temporary folder where the inputs are generated should be kept after the simulation.

### Examples

**YAML configuration:**
```yaml
dynawo-algorithms:
homeDir: /home/user/dynawo-algorithms
debug: false
```
**XML configuration:**
```xml
<dynawo-algorithms>
<homeDir>/home/user/dynawo</homeDir>
<debug>false</debug>
</dynawo-algorithms>
```

## Default parameters
The dynamic security analysis reuse the `dynawo-default-parameters` [module](../dynamic_simulation/configuration.md#default-parameters).
18 changes: 18 additions & 0 deletions docs/dynamic_security_analysis/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Dynamic security analysis

```{toctree}
:hidden:
configuration.md
```

PowSyBl provides an implementation of the [DynamicSecurityAnalysis API from powsybl-core](inv:powsyblcore:*:*#simulation/dynamic_security/index) with [Dynaωo-algorithms](https://dynawo.github.io/about/dynalgo), a wrapper around [Dynaωo](https://dynawo.github.io) providing utility algorithms to calculate complex key values of a power system.

## Installation

Read this [documentation page](https://dynawo.github.io/install/dynalgo) to learn how to install and configure Dynaωo-algorithms.

## Going further

You may find an extensive documentation of the Dynawo project [here](https://github.com/dynawo/dynawo/releases/latest/download/DynawoDocumentation.pdf).
The dynamic security analysis use the same dynamic models supplier as the dynamic simulation implementation, documentation can be found [here](../dynamic_simulation/dynamic-models-dsl.md).

File renamed without changes.
2 changes: 1 addition & 1 deletion docs/dynamic_simulation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```{toctree}
:hidden:
dynawo-configuration.md
configuration.md
dynamic-models-dsl.md
event-models-dsl.md
curves-dsl.md
Expand Down
10 changes: 6 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Powsybl-dynawo documentation
Powsybl-dynawo is an interface between PowSyBl and [Dynaωo](https://dynawo.github.io) open source suites, providing:
- an implementation of [LoadFlow API from powsybl-core](inv:powsyblcore:*:*#simulation/loadflow/index),
- an implementation of [SecurityAnalysis API from powsybl-core](inv:powsyblcore:*:*#simulation/security/index),
- an implementation of [DynamicSimulation API from powsybl-core](inv:powsyblcore:*:*#simulation/dynamic/index).
Powsybl-dynawo is an interface between PowSyBl and [Dynaωo](https://dynawo.github.io) open source suites, providing implementations of:
- [LoadFlow API from powsybl-core](inv:powsyblcore:*:*#simulation/loadflow/index),
- [SecurityAnalysis API from powsybl-core](inv:powsyblcore:*:*#simulation/security/index),
- [DynamicSimulation API from powsybl-core](inv:powsyblcore:*:*#simulation/dynamic/index),
- [DynamicSecurityAnalysis API from powsybl-core](inv:powsyblcore:*:*#simulation/dynamic_security/index).

```{toctree}
:hidden:
load_flow/index.md
dynamic_simulation/index.md
dynamic_security_analysis/index.md
```
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/load_flow/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```{toctree}
:hidden:
dynaflow-configuration.md
configuration.md
```

PowSyBl provides an implementation of the [LoadFlow API from powsybl-core](inv:powsyblcore:*:*#simulation/loadflow/index) with [DynaFlow](https://dynawo.github.io/about/dynaflow).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2023, 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.dynaflow;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.contingency.Contingency;
import com.powsybl.dynaflow.xml.ConstraintsReader;
import com.powsybl.dynawo.commons.CommonReports;
import com.powsybl.dynawo.commons.timeline.TimelineEntry;
import com.powsybl.dynawo.commons.timeline.XmlTimeLineParser;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.security.*;
import com.powsybl.security.results.NetworkResult;
import com.powsybl.security.results.PostContingencyResult;
import com.powsybl.security.results.PreContingencyResult;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

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

private ContingencyResultsUtils() {
}

/**
* Build the pre-contingency results from the input network
*/
public static PreContingencyResult getPreContingencyResult(Network network, LimitViolationFilter violationFilter) {
List<LimitViolation> limitViolations = Security.checkLimits(network);
List<LimitViolation> filteredViolations = violationFilter.apply(limitViolations, network);
NetworkResult networkResult = new NetworkResult(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
return new PreContingencyResult(LoadFlowResult.ComponentResult.Status.CONVERGED, new LimitViolationsResult(filteredViolations), networkResult);
}

/**
* Build the post-contingency results from the constraints files written by dynawo
*/
public static List<PostContingencyResult> getPostContingencyResults(Network network, LimitViolationFilter violationFilter,
Path constraintsDir, List<Contingency> contingencies) {
return contingencies.stream()
.map(c -> getPostContingencyResult(network, violationFilter, constraintsDir, c))
.collect(Collectors.toList());
}

private static PostContingencyResult getPostContingencyResult(Network network, LimitViolationFilter violationFilter,
Path constraintsDir, Contingency contingency) {
Path constraintsFile = constraintsDir.resolve("constraints_" + contingency.getId() + ".xml");
if (Files.exists(constraintsFile)) {
List<LimitViolation> limitViolationsRead = ConstraintsReader.read(network, constraintsFile);
List<LimitViolation> limitViolationsFiltered = violationFilter.apply(limitViolationsRead, network);
return new PostContingencyResult(contingency, PostContingencyComputationStatus.CONVERGED, new LimitViolationsResult(limitViolationsFiltered));
} else {
return new PostContingencyResult(contingency, PostContingencyComputationStatus.FAILED, Collections.emptyList());
}
}

// Report the timeline events from the timeline files written by dynawo
public static void reportContingenciesTimelines(List<Contingency> contingencies, Path timelineDir, ReportNode reportNode) {
contingencies.forEach(c -> {
ReportNode contingencyReporter = DynaflowReports.createContingenciesTimelineReportNode(reportNode, c.getId());
getTimeline(timelineDir, c).forEach(e -> CommonReports.reportTimelineEntry(contingencyReporter, e));
});
}

private static List<TimelineEntry> getTimeline(Path timelineDir, Contingency c) {
Path timelineFile = timelineDir.resolve("timeline_" + c.getId() + ".xml");
return new XmlTimeLineParser().parse(timelineFile);
}
}
10 changes: 8 additions & 2 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.google.common.collect.ImmutableMap;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.dynawo.commons.DynawoConfig;
import com.powsybl.dynawo.commons.AbstractDynawoConfig;

import java.nio.file.Path;
import java.util.Map;
Expand All @@ -18,8 +18,9 @@
*
* @author Guillaume Pernin {@literal <guillaume.pernin at rte-france.com>}
*/
public class DynaFlowConfig extends DynawoConfig {
public class DynaFlowConfig extends AbstractDynawoConfig {

public static final String DYNAFLOW_LAUNCHER_PROGRAM_NAME = "dynaflow-launcher";
private static final String DYNAFLOW_MODULE_NAME = "dynaflow";

public static DynaFlowConfig load() {
Expand All @@ -42,4 +43,9 @@ public Map<String, String> createEnv() {
return ImmutableMap.<String, String>builder()
.build();
}

@Override
public String getProgram() {
return getProgram(DYNAFLOW_LAUNCHER_PROGRAM_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
public final class DynaFlowConstants {

public static final String DYNAFLOW_LAUNCHER_PROGRAM_NAME = "dynaflow-launcher";
public static final String DYNAFLOW_NAME = "DynaFlow";
public static final String CONFIG_FILENAME = "config.json";
public static final String IIDM_FILENAME = "network.xiidm";
Expand Down
108 changes: 108 additions & 0 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) 2023, 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.dynaflow;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.AbstractExecutionHandler;
import com.powsybl.computation.Command;
import com.powsybl.computation.CommandExecution;
import com.powsybl.computation.ExecutionReport;
import com.powsybl.dynaflow.json.DynaFlowConfigSerializer;
import com.powsybl.dynawo.commons.CommonReports;
import com.powsybl.dynawo.commons.DynawoUtil;
import com.powsybl.dynawo.commons.NetworkResultsUpdater;
import com.powsybl.dynawo.commons.loadmerge.LoadsMerger;
import com.powsybl.dynawo.commons.timeline.TimelineEntry;
import com.powsybl.dynawo.commons.timeline.XmlTimeLineParser;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.serde.NetworkSerDe;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.loadflow.LoadFlowResultImpl;
import com.powsybl.loadflow.json.LoadFlowResultDeserializer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynawo.commons.DynawoConstants.DYNAWO_TIMELINE_FOLDER;
import static com.powsybl.dynawo.commons.DynawoUtil.getCommandExecutions;

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public class DynaFlowHandler extends AbstractExecutionHandler<LoadFlowResult> {
private final Network network;
private final Network dynawoInput;
private final String workingStateId;
private final DynaFlowParameters dynaFlowParameters;
private final LoadFlowParameters loadFlowParameters;
private final Command command;
private final ReportNode reportNode;

public DynaFlowHandler(Network network, String workingStateId, DynaFlowParameters dynaFlowParameters, LoadFlowParameters loadFlowParameters, Command command, ReportNode reportNode) {
this.network = network;
this.workingStateId = workingStateId;
this.dynaFlowParameters = dynaFlowParameters;
this.loadFlowParameters = loadFlowParameters;
this.command = command;
this.dynawoInput = this.dynaFlowParameters.isMergeLoads() ? LoadsMerger.mergeLoads(this.network) : this.network;
this.reportNode = reportNode;
}

@Override
public List<CommandExecution> before(Path workingDir) throws IOException {
network.getVariantManager().setWorkingVariant(workingStateId);
DynawoUtil.writeIidm(dynawoInput, workingDir.resolve(IIDM_FILENAME));
DynaFlowConfigSerializer.serialize(loadFlowParameters, dynaFlowParameters, Path.of("."), workingDir.resolve(CONFIG_FILENAME));
return getCommandExecutions(command);
}

@Override
public LoadFlowResult after(Path workingDir, ExecutionReport report) {
reportTimeLine(workingDir);

report.log();
network.getVariantManager().setWorkingVariant(workingStateId);
boolean status = true;
Path outputNetworkFile = workingDir.resolve("outputs").resolve("finalState").resolve(DynaFlowConstants.OUTPUT_IIDM_FILENAME);
if (Files.exists(outputNetworkFile)) {
NetworkResultsUpdater.update(network, NetworkSerDe.read(outputNetworkFile), dynaFlowParameters.isMergeLoads());
} else {
status = false;
}
Path resultsPath = workingDir.resolve(OUTPUT_RESULTS_FILENAME);
if (!Files.exists(resultsPath)) {
Map<String, String> metrics = new HashMap<>();
List<LoadFlowResult.ComponentResult> componentResults = new ArrayList<>(1);
componentResults.add(new LoadFlowResultImpl.ComponentResultImpl(0,
0,
status ? LoadFlowResult.ComponentResult.Status.CONVERGED : LoadFlowResult.ComponentResult.Status.FAILED,
0,
"not-found",
0.,
Double.NaN));
return new LoadFlowResultImpl(status, metrics, null, componentResults);
}
return LoadFlowResultDeserializer.read(resultsPath);
}

private void reportTimeLine(Path workingDir) {
ReportNode dfReporter = DynaflowReports.createDynaFlowReportNode(reportNode, network.getId());
Path timelineFile = workingDir.resolve(DYNAFLOW_OUTPUTS_FOLDER)
.resolve(DYNAWO_TIMELINE_FOLDER)
.resolve(DYNAFLOW_TIMELINE_FILE);
List<TimelineEntry> tl = new XmlTimeLineParser().parse(timelineFile);
tl.forEach(e -> CommonReports.reportTimelineEntry(dfReporter, e));
}
}
Loading

0 comments on commit 0531353

Please sign in to comment.