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

Report timeline in dynaflow loadflow #298

Merged
merged 2 commits into from
Oct 10, 2023
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 @@ -15,14 +15,14 @@
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 DYNAWO_TIMELINE_FOLDER = "timeLine";
static final String DYNAFLOW_TIMELINE_FILE = "timeline.xml";


public enum OutputTypes {
STEADYSTATE,
Expand Down
23 changes: 20 additions & 3 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import com.powsybl.commons.extensions.Extension;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
import com.powsybl.commons.parameters.Parameter;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.*;
import com.powsybl.dynaflow.json.DynaFlowConfigSerializer;
import com.powsybl.dynaflow.json.JsonDynaFlowParametersSerializer;
import com.powsybl.dynawo.commons.DynawoUtil;
import com.powsybl.dynawo.commons.NetworkResultsUpdater;
import com.powsybl.dynawo.commons.PowsyblDynawoVersion;
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.xml.NetworkXml;
import com.powsybl.loadflow.LoadFlowParameters;
Expand Down Expand Up @@ -103,7 +106,8 @@ public String getVersion() {
}

@Override
public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager computationManager, String workingStateId, LoadFlowParameters loadFlowParameters) {
public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager computationManager, String workingStateId,
LoadFlowParameters loadFlowParameters, Reporter reporter) {
Objects.requireNonNull(network);
Objects.requireNonNull(computationManager);
Objects.requireNonNull(workingStateId);
Expand All @@ -113,7 +117,7 @@ public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager
ExecutionEnvironment env = new ExecutionEnvironment(config.createEnv(), WORKING_DIR_PREFIX, config.isDebug());
Command versionCmd = getVersionCommand(config);
DynawoUtil.requireDynawoMinVersion(env, computationManager, versionCmd, true);
return computationManager.execute(env, new DynaFlowHandler(network, workingStateId, dynaFlowParameters, loadFlowParameters, config));
return computationManager.execute(env, new DynaFlowHandler(network, workingStateId, dynaFlowParameters, loadFlowParameters, config, reporter));
}

@Override
Expand Down Expand Up @@ -149,14 +153,16 @@ private static class DynaFlowHandler extends AbstractExecutionHandler<LoadFlowRe
private final DynaFlowParameters dynaFlowParameters;
private final LoadFlowParameters loadFlowParameters;
private final DynaFlowConfig config;
private final Reporter reporter;

public DynaFlowHandler(Network network, String workingStateId, DynaFlowParameters dynaFlowParameters, LoadFlowParameters loadFlowParameters, DynaFlowConfig config) {
public DynaFlowHandler(Network network, String workingStateId, DynaFlowParameters dynaFlowParameters, LoadFlowParameters loadFlowParameters, DynaFlowConfig config, Reporter reporter) {
this.network = network;
this.workingStateId = workingStateId;
this.dynaFlowParameters = dynaFlowParameters;
this.loadFlowParameters = loadFlowParameters;
this.config = config;
this.dynawoInput = this.dynaFlowParameters.isMergeLoads() ? LoadsMerger.mergeLoads(this.network) : this.network;
this.reporter = reporter;
}

@Override
Expand All @@ -174,6 +180,8 @@ private static CommandExecution createCommandExecution(DynaFlowConfig config) {

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

report.log();
network.getVariantManager().setWorkingVariant(workingStateId);
boolean status = true;
Expand All @@ -198,5 +206,14 @@ public LoadFlowResult after(Path workingDir, ExecutionReport report) {
}
return LoadFlowResultDeserializer.read(resultsPath);
}

private void reportTimeLine(Path workingDir) {
Reporter dfReporter = Reports.createDynaFlowReporter(reporter, 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 -> Reports.reportTimelineEvent(dfReporter, e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class DynaFlowSecurityAnalysis {
private static final String DYNAFLOW_LAUNCHER_PROGRAM_NAME = "dynaflow-launcher.sh";
private static final String CONTINGENCIES_FILENAME = "contingencies.json";
private static final String DYNAWO_CONSTRAINTS_FOLDER = "constraints";
private static final String DYNAWO_TIMELINE_FOLDER = "timeLine";

private final Supplier<DynaFlowConfig> configSupplier;

Expand Down Expand Up @@ -172,7 +171,7 @@ public SecurityAnalysisReport after(Path workingDir, ExecutionReport report) thr
.toList();

// Report the timeline events from the timeline files written by dynawo
Path timelineDir = workingDir.resolve(DYNAWO_TIMELINE_FOLDER);
Path timelineDir = workingDir.resolve(DynaFlowConstants.DYNAWO_TIMELINE_FOLDER);
contingencies.forEach(c -> {
Reporter contingencyReporter = Reports.createDynaFlowTimelineReporter(reporter, c.getId());
getTimeline(timelineDir, c).forEach(e -> Reports.reportTimelineEvent(contingencyReporter, e));
Expand Down Expand Up @@ -204,8 +203,8 @@ private static PostContingencyResult getPostContingencyResult(Network network, L
}
}

private List<TimelineEntry> getTimeline(Path constraintsDir, Contingency c) {
Path timelineFile = constraintsDir.resolve("timeline_" + c.getId() + ".xml");
private List<TimelineEntry> getTimeline(Path timelineDir, Contingency c) {
Path timelineFile = timelineDir.resolve("timeline_" + c.getId() + ".xml");
return new XmlTimeLineParser().parse(timelineFile);
}
}
6 changes: 6 additions & 0 deletions dynaflow/src/main/java/com/powsybl/dynaflow/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public final class Reports {
private Reports() {
}

public static Reporter createDynaFlowReporter(Reporter reporter, String networkId) {
return reporter.createSubReporter("dynaflow",
"Dynaflow loadflow on network '${networkId}'",
"networkId", networkId);
}

public static Reporter createDynaFlowSecurityAnalysisReporter(Reporter reporter, String networkId) {
return reporter.createSubReporter("dynaflowSa",
"Dynaflow security analysis on network '${networkId}'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,31 @@ void setUp() throws Exception {
}

@Test
void testLf() {
void testLf() throws IOException {
Network network = IeeeCdfNetworkFactory.create14Solved();
LoadFlowResult result = loadFlowProvider.run(network, computationManager, VariantManagerConstants.INITIAL_VARIANT_ID, loadFlowParameters)
network.getLine("L6-13-1").newCurrentLimits1()
.beginTemporaryLimit().setName("1").setAcceptableDuration(60).setValue(100).endTemporaryLimit()
.beginTemporaryLimit().setName("2").setAcceptableDuration(120).setValue(110).endTemporaryLimit()
.setPermanentLimit(200)
.add();

ReporterModel reporter = new ReporterModel("root", "testLf root reporter");
LoadFlowResult result = loadFlowProvider.run(network, computationManager, VariantManagerConstants.INITIAL_VARIANT_ID, loadFlowParameters, reporter)
.join();

assertTrue(result.isOk());
assertEquals(1, result.getComponentResults().size());
LoadFlowResult.ComponentResult componentResult = result.getComponentResults().get(0);
assertEquals(LoadFlowResult.ComponentResult.Status.CONVERGED, componentResult.getStatus());
assertEquals("B4", componentResult.getSlackBusId());

StringWriter sw = new StringWriter();
reporter.export(sw);
System.out.println(sw);
InputStream refStream = Objects.requireNonNull(getClass().getResourceAsStream("/loadflow_timeline_report.txt"));
String refLogExport = TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(refStream), StandardCharsets.UTF_8));
String logExport = TestUtil.normalizeLineSeparator(sw.toString());
assertEquals(refLogExport, logExport);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
+ testLf root reporter
+ Dynaflow loadflow on network 'ieee14cdf-solved'
[t=90.0] Overload 60 : opening line on equipment 'L6-13-1'
[t=90.0] LINE : opening both sides on equipment 'L6-13-1'
[t=90.0] Overload 60 : opening line on equipment 'L6-13-1'
Loading