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

Refactor Dynawo constants #395

Merged
merged 5 commits into from
Nov 8, 2024
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 @@ -9,6 +9,7 @@

import com.powsybl.iidm.serde.IidmVersion;

import java.io.File;
import java.util.List;

/**
Expand All @@ -24,8 +25,6 @@ private DynawoConstants() {
*/
public static final String IIDM_VERSION = IidmVersion.V_1_4.toString(".");

public static final String DYNAWO_TIMELINE_FOLDER = "timeLine";

public static final DynawoVersion VERSION_MIN = new DynawoVersion(1, 5, 0);

public static final List<String> IIDM_EXTENSIONS = List.of(
Expand All @@ -36,5 +35,19 @@ private DynawoConstants() {
"hvdcOperatorActivePowerRange",
"standbyAutomaton");

public static final String NETWORK_FILENAME = "powsybl_dynawo.xiidm";

public static final String OUTPUTS_FOLDER = "outputs";

public static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";

public static final String FINAL_STATE_FOLDER = "finalState";
Lisrte marked this conversation as resolved.
Show resolved Hide resolved

public static final String TIMELINE_FOLDER = "timeLine";

public static final String TIMELINE_FILENAME = "timeline";

public static final String FINAL_STATE_FOLDER_PATH = String.join(File.separator, OUTPUTS_FOLDER, FINAL_STATE_FOLDER);

public static final String OUTPUT_IIDM_FILENAME_PATH = String.join(File.separator, OUTPUTS_FOLDER, FINAL_STATE_FOLDER, OUTPUT_IIDM_FILENAME);
}
27 changes: 27 additions & 0 deletions commons/src/main/java/com/powsybl/dynawo/commons/ExportMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.commons;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public enum ExportMode {
CSV(".csv"),
TXT(".log"),
XML(".xml");

private final String fileExtension;

ExportMode(String fileExtension) {
this.fileExtension = fileExtension;
}

public String getFileExtension() {
return fileExtension;
}
}
2 changes: 1 addition & 1 deletion docs/dynamic_simulation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ The default value is `FALSE`.

**timeline.exportMode**
`timeline.exportMode` defines the file extension of the timeline export.
The available `com.powsybl.dynawo.DynawoSimulationParameters.ExportMode` values are:
The available `com.powsybl.dynawo.commons.ExportMode` values are:
- `CSV`
- `TXT`: same format as `CSV` but with `|` separator
- `XML`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public final class DynaFlowConstants {

public static final String DYNAFLOW_NAME = "DynaFlow";
public static final String CONFIG_FILENAME = "config.json";
public static final String IIDM_FILENAME = "network.xiidm";
public static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";
public static final String OUTPUT_RESULTS_FILENAME = "results.json";
static final String DYNAFLOW_OUTPUTS_FOLDER = "outputs";
static final String DYNAFLOW_TIMELINE_FILE = "timeline.xml";


public enum OutputTypes {
STEADYSTATE,
Expand Down
14 changes: 8 additions & 6 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.dynaflow.json.DynaFlowConfigSerializer;
import com.powsybl.dynawo.commons.CommonReports;
import com.powsybl.dynawo.commons.DynawoUtil;
import com.powsybl.dynawo.commons.ExportMode;
import com.powsybl.dynawo.commons.NetworkResultsUpdater;
import com.powsybl.dynawo.commons.loadmerge.LoadsMerger;
import com.powsybl.dynawo.commons.timeline.TimelineEntry;
Expand All @@ -35,7 +36,7 @@
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.DynawoConstants.*;
import static com.powsybl.dynawo.commons.DynawoUtil.getCommandExecutions;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public DynaFlowHandler(Network network, String workingStateId, DynaFlowParameter
@Override
public List<CommandExecution> before(Path workingDir) throws IOException {
network.getVariantManager().setWorkingVariant(workingStateId);
DynawoUtil.writeIidm(dynawoInput, workingDir.resolve(IIDM_FILENAME));
DynawoUtil.writeIidm(dynawoInput, workingDir.resolve(NETWORK_FILENAME));
DynaFlowConfigSerializer.serialize(loadFlowParameters, dynaFlowParameters, Path.of("."), workingDir.resolve(CONFIG_FILENAME));
return getCommandExecutions(command);
}
Expand All @@ -75,7 +76,8 @@ public LoadFlowResult after(Path workingDir, ExecutionReport report) {
report.log();
network.getVariantManager().setWorkingVariant(workingStateId);
boolean status = true;
Path outputNetworkFile = workingDir.resolve("outputs").resolve("finalState").resolve(DynaFlowConstants.OUTPUT_IIDM_FILENAME);
Path outputNetworkFile = workingDir.resolve(OUTPUT_IIDM_FILENAME_PATH);

if (Files.exists(outputNetworkFile)) {
NetworkResultsUpdater.update(network, NetworkSerDe.read(outputNetworkFile), dynaFlowParameters.isMergeLoads());
} else {
Expand All @@ -99,9 +101,9 @@ public LoadFlowResult after(Path workingDir, ExecutionReport report) {

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);
Path timelineFile = workingDir.resolve(OUTPUTS_FOLDER)
.resolve(TIMELINE_FOLDER)
.resolve(TIMELINE_FILENAME + ExportMode.XML.getFileExtension());
List<TimelineEntry> tl = new XmlTimeLineParser().parse(timelineFile);
tl.forEach(e -> CommonReports.reportTimelineEntry(dfReporter, e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.CompletableFuture;

import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;
import static com.powsybl.dynawo.commons.DynawoConstants.OUTPUT_IIDM_FILENAME_PATH;

/**
*
Expand All @@ -52,16 +54,14 @@ public DynaFlowProvider(Supplier<DynaFlowConfig> configSupplier) {
}

public static Command getCommand(DynaFlowConfig config) {
List<String> args = Arrays.asList("--network", IIDM_FILENAME, "--config", CONFIG_FILENAME);

return new SimpleCommandBuilder()
.id("dynaflow_lf")
.program(config.getProgram())
.args(args)
.inputFiles(new InputFile(IIDM_FILENAME),
.args("--network", NETWORK_FILENAME, "--config", CONFIG_FILENAME)
.inputFiles(new InputFile(NETWORK_FILENAME),
new InputFile(CONFIG_FILENAME))
.outputFiles(new OutputFile(OUTPUT_RESULTS_FILENAME),
new OutputFile("outputs/finalState/" + OUTPUT_IIDM_FILENAME))
new OutputFile(OUTPUT_IIDM_FILENAME_PATH))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import java.util.List;

import static com.powsybl.dynaflow.DynaFlowConstants.CONFIG_FILENAME;
import static com.powsybl.dynaflow.DynaFlowConstants.IIDM_FILENAME;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONTINGENCIES_FILENAME;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.DYNAWO_CONSTRAINTS_FOLDER;
import static com.powsybl.dynawo.commons.DynawoConstants.DYNAWO_TIMELINE_FOLDER;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONSTRAINTS_FOLDER;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;
import static com.powsybl.dynawo.commons.DynawoConstants.TIMELINE_FOLDER;
import static com.powsybl.dynawo.commons.DynawoUtil.getCommandExecutions;

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ public DynaFlowSecurityAnalysisHandler(Network network, String workingVariantId,
public List<CommandExecution> before(Path workingDir) throws IOException {
network.getVariantManager().setWorkingVariant(workingVariantId);

DynawoUtil.writeIidm(network, workingDir.resolve(IIDM_FILENAME));
DynawoUtil.writeIidm(network, workingDir.resolve(NETWORK_FILENAME));
writeParameters(securityAnalysisParameters, workingDir);
writeContingencies(contingencies, workingDir);
return getCommandExecutions(command);
Expand All @@ -84,11 +84,11 @@ public List<CommandExecution> before(Path workingDir) throws IOException {
public SecurityAnalysisReport after(Path workingDir, ExecutionReport report) throws IOException {
super.after(workingDir, report);
network.getVariantManager().setWorkingVariant(workingVariantId);
ContingencyResultsUtils.reportContingenciesTimelines(contingencies, workingDir.resolve(DYNAWO_TIMELINE_FOLDER), reportNode);
ContingencyResultsUtils.reportContingenciesTimelines(contingencies, workingDir.resolve(TIMELINE_FOLDER), reportNode);
return new SecurityAnalysisReport(
new SecurityAnalysisResult(
ContingencyResultsUtils.getPreContingencyResult(network, violationFilter),
ContingencyResultsUtils.getPostContingencyResults(network, violationFilter, workingDir.resolve(DYNAWO_CONSTRAINTS_FOLDER), contingencies),
ContingencyResultsUtils.getPostContingencyResults(network, violationFilter, workingDir.resolve(CONSTRAINTS_FOLDER), contingencies),
Collections.emptyList())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONTINGENCIES_FILENAME;
import static com.powsybl.dynaflow.DynaFlowConstants.DYNAFLOW_NAME;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;

/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
Expand Down Expand Up @@ -89,7 +90,7 @@ public String getVersion() {
}

public static Command getCommand(DynaFlowConfig config) {
List<String> args = Arrays.asList("--network", IIDM_FILENAME,
List<String> args = Arrays.asList("--network", NETWORK_FILENAME,
"--config", CONFIG_FILENAME,
"--contingencies", CONTINGENCIES_FILENAME);
return new SimpleCommandBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public final class SecurityAnalysisConstants {

public static final String DYNAWO_CONSTRAINTS_FOLDER = "constraints";
public static final String CONSTRAINTS_FOLDER = "constraints";

public static final String CONTINGENCIES_FILENAME = "contingencies.json";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import static com.powsybl.commons.test.ComparisonUtils.assertXmlEquals;
import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynawo.commons.DynawoConstants.OUTPUT_IIDM_FILENAME;
import static com.powsybl.dynawo.commons.DynawoConstants.*;
import static com.powsybl.loadflow.LoadFlowResult.Status.FAILED;
import static com.powsybl.loadflow.LoadFlowResult.Status.FULLY_CONVERGED;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -68,7 +68,7 @@ void checkVersionCommand() {
@Test
void checkExecutionCommand() {
String executionCommand = DynaFlowProvider.getCommand(config).toString(0);
String expectedExecutionCommand = "[" + getProgram(homeDir) + ", --network, " + IIDM_FILENAME + ", --config, " + CONFIG_FILENAME + "]";
String expectedExecutionCommand = "[" + getProgram(homeDir) + ", --network, " + NETWORK_FILENAME + ", --config, " + CONFIG_FILENAME + "]";
assertEquals(expectedExecutionCommand, executionCommand);
}

Expand All @@ -93,8 +93,8 @@ public int execute(String program, List<String> args, Path outFile, Path errFile
try {
copyFile(stdOutFileRef, errFile);
copyFile(outputResults, workingDir.resolve(OUTPUT_RESULTS_FILENAME));
Files.createDirectories(workingDir.resolve("outputs").resolve("finalState"));
copyFile(outputIidm, workingDir.resolve("outputs").resolve("finalState").resolve(OUTPUT_IIDM_FILENAME));
Path finalState = Files.createDirectories(workingDir.resolve(FINAL_STATE_FOLDER_PATH));
copyFile(outputIidm, finalState.resolve(OUTPUT_IIDM_FILENAME));

return 0;
} catch (IOException e) {
Expand All @@ -115,7 +115,7 @@ public EmptyLocalCommandExecutorMock(String stdoutFileRef) {
public int execute(String program, List<String> args, Path outFile, Path errFile, Path workingDir, Map<String, String> env) {
try {
copyFile(stdOutFileRef, errFile);
Files.createDirectories(workingDir.resolve("outputs").resolve("finalState"));
Files.createDirectories(workingDir.resolve(FINAL_STATE_FOLDER_PATH));
return 0;
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals;
import static com.powsybl.commons.test.ComparisonUtils.assertXmlEquals;
import static com.powsybl.dynaflow.DynaFlowConstants.DYNAFLOW_NAME;
import static com.powsybl.dynaflow.DynaFlowConstants.IIDM_FILENAME;
import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONTINGENCIES_FILENAME;
import static com.powsybl.dynaflow.SecurityAnalysisConstants.CONSTRAINTS_FOLDER;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -69,7 +71,8 @@ public int execute(String program, List<String> args, Path outFile, Path errFile
if (args.get(0).equals("--version")) {
copyFile(stdOutFileRef, errFile);
} else {
assertEquals("--network network.xiidm --config config.json --contingencies contingencies.json", String.join(" ", args));
assertEquals("--network %s --config %s --contingencies %s".formatted(NETWORK_FILENAME, CONFIG_FILENAME, CONTINGENCIES_FILENAME),
String.join(" ", args));
validateInputs(workingDir);
copyOutputs(workingDir);
}
Expand All @@ -81,16 +84,16 @@ public int execute(String program, List<String> args, Path outFile, Path errFile

private void validateInputs(Path workingDir) throws IOException {
if (inputFile != null) {
assertXmlEquals(getClass().getResourceAsStream(inputFile), Files.newInputStream(workingDir.resolve(IIDM_FILENAME)));
assertXmlEquals(getClass().getResourceAsStream(inputFile), Files.newInputStream(workingDir.resolve(NETWORK_FILENAME)));
}
if (contingencyFile != null) {
InputStream contingencyIs = Objects.requireNonNull(getClass().getResourceAsStream(contingencyFile));
assertTxtEquals(contingencyIs, Files.newInputStream(workingDir.resolve("contingencies.json")));
assertTxtEquals(contingencyIs, Files.newInputStream(workingDir.resolve(CONTINGENCIES_FILENAME)));
}
}

private void copyOutputs(Path workingDir) throws IOException {
Path constraintsFolder = Files.createDirectories(workingDir.resolve("constraints"));
Path constraintsFolder = Files.createDirectories(workingDir.resolve(CONSTRAINTS_FOLDER));
for (int i = 0; i < contingencyIds.size(); i++) {
copyFile(constraints.get(i), constraintsFolder.resolve("constraints_" + contingencyIds.get(i) + ".xml"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.Map;
import java.util.Objects;

import static com.powsybl.dynawo.xml.DynawoSimulationConstants.*;
import static com.powsybl.dynawo.DynawoSimulationConstants.*;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;
import static com.powsybl.dynawo.commons.DynawoConstants.OUTPUTS_FOLDER;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
Expand Down Expand Up @@ -61,8 +63,8 @@ protected void validateInputs(Path workingDir) throws IOException {
}

protected void copyOutputs(Path workingDir) throws IOException {
Path output = Files.createDirectories(workingDir.resolve("outputs/outputVariables").toAbsolutePath());
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/" + baseDirName + "/dynawo-outputs/outputVariables.csv")), output.resolve("curves.csv"));
Path output = Files.createDirectories(workingDir.resolve(OUTPUTS_FOLDER).resolve(CURVES_OUTPUT_PATH).toAbsolutePath());
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/" + baseDirName + "/dynawo-outputs/curves.csv")), output.resolve(CURVES_FILENAME));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.*;
import com.powsybl.dynamicsimulation.groovy.*;
import com.powsybl.dynawo.DumpFileParameters;
import com.powsybl.dynawo.DynawoSimulationConfig;
import com.powsybl.dynawo.DynawoSimulationParameters;
import com.powsybl.dynawo.DynawoSimulationProvider;
import com.powsybl.dynawo.*;
import com.powsybl.dynawo.commons.ExportMode;
import com.powsybl.dynawo.parameters.ParametersSet;
import com.powsybl.dynawo.suppliers.dynamicmodels.DynawoModelsSupplier;
import com.powsybl.dynawo.suppliers.events.DynawoEventModelsSupplier;
Expand All @@ -37,6 +35,7 @@

import static com.powsybl.commons.report.ReportNode.NO_OP;
import static com.powsybl.commons.report.ReportNode.newRootReportNode;
import static com.powsybl.dynawo.commons.DynawoConstants.NETWORK_FILENAME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -216,7 +215,7 @@ void testSmib() {

@Test
void testSimulationError() {
Network network = Network.read(new ResourceDataSource("powsybl_dynawo", new ResourceSet("/error", "powsybl_dynawo.xiidm")));
Network network = Network.read(new ResourceDataSource("powsybl_dynawo", new ResourceSet("/error", NETWORK_FILENAME)));

GroovyDynamicModelsSupplier dynamicModelsSupplier = new GroovyDynamicModelsSupplier(
getResourceAsStream("/error/models.groovy"),
Expand Down Expand Up @@ -256,7 +255,7 @@ void testIeee14DynawoSuppliers() {
.setNetworkParameters(networkParameters)
.setSolverParameters(solverParameters)
.setSolverType(DynawoSimulationParameters.SolverType.IDA)
.setTimelineExportMode(DynawoSimulationParameters.ExportMode.XML);
.setTimelineExportMode(ExportMode.XML);

DynamicSimulationResult result = provider.run(network, dynamicModelsSupplier, eventModelsSupplier, OutputVariablesSupplier.empty(),
VariantManagerConstants.INITIAL_VARIANT_ID, computationManager, parameters, NO_OP)
Expand Down Expand Up @@ -292,7 +291,7 @@ void testIEEE14SignalN() {
.setSolverParameters(ParametersXml.load(getResourceAsStream("/ieee14/signal_n/IEEE14.par"), "SimplifiedSolver"))
.setLogLevelFilter(DynawoSimulationParameters.LogLevel.DEBUG)
.setSolverType(DynawoSimulationParameters.SolverType.SIM)
.setTimelineExportMode(DynawoSimulationParameters.ExportMode.XML);
.setTimelineExportMode(ExportMode.XML);

DynamicSimulationResult result = provider.run(network, dynamicModelsSupplier, eventModelsSupplier, OutputVariablesSupplier.empty(),
VariantManagerConstants.INITIAL_VARIANT_ID, computationManager, parameters, NO_OP)
Expand Down Expand Up @@ -332,7 +331,7 @@ private Supplier<DynamicSimulationResult> setupIEEE14Simulation(ReportNode repor
.setNetworkParameters(networkParameters)
.setSolverParameters(solverParameters)
.setSolverType(DynawoSimulationParameters.SolverType.IDA)
.setTimelineExportMode(DynawoSimulationParameters.ExportMode.XML);
.setTimelineExportMode(ExportMode.XML);

return () -> provider.run(network, dynamicModelsSupplier, eventModelsSupplier, outputVariablesSupplier,
VariantManagerConstants.INITIAL_VARIANT_ID, computationManager, parameters, reportNode)
Expand Down
Loading
Loading