-
Notifications
You must be signed in to change notification settings - Fork 19
/
RunLoadFlowTool.java
146 lines (128 loc) · 5.81 KB
/
RunLoadFlowTool.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
/**
* Copyright (c) 2016, 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 eu.itesla_project.online.tools;
import com.google.auto.service.AutoService;
import eu.itesla_project.commons.config.ComponentDefaultConfig;
import eu.itesla_project.commons.io.table.Column;
import eu.itesla_project.commons.io.table.TableFormatter;
import eu.itesla_project.commons.io.table.TableFormatterConfig;
import eu.itesla_project.commons.tools.Command;
import eu.itesla_project.commons.tools.Tool;
import eu.itesla_project.computation.ComputationManager;
import eu.itesla_project.computation.local.LocalComputationManager;
import eu.itesla_project.iidm.import_.ImportConfig;
import eu.itesla_project.iidm.import_.Importers;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.loadflow.api.LoadFlow;
import eu.itesla_project.loadflow.api.LoadFlowFactory;
import eu.itesla_project.loadflow.api.LoadFlowResult;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
@AutoService(Tool.class)
public class RunLoadFlowTool implements Tool {
private static final String TABLE_TITLE = "loadflow";
private static Command COMMAND = new Command() {
@Override
public String getName() {
return "run-loadflow";
}
@Override
public String getTheme() {
return "Computation";
}
@Override
public String getDescription() {
return "Run loadflow";
}
@Override
public Options getOptions() {
Options options = new Options();
options.addOption(Option.builder().longOpt("case-file")
.desc("the case path")
.hasArg()
.argName("FILE")
.required()
.build());
options.addOption(Option.builder().longOpt("skip-postproc")
.desc("skip network importer post processors (when configured)")
.build());
options.addOption(Option.builder().longOpt("output-file")
.desc("export to a file")
.hasArg()
.argName("FILE")
.build());
options.addOption(Option.builder().longOpt("output-format")
.desc("output formats available: " + PrintOnlineWorkflowUtils.availableTableFormatterFormats() + " (default is ascii)")
.hasArg()
.argName("FORMAT")
.build());
return options;
}
@Override
public String getUsageFooter() {
return null;
}
};
@Override
public Command getCommand() {
return COMMAND;
}
private TableFormatter getFormatter(String outputFormat, Path outputFile) throws IOException {
TableFormatterConfig tableFormatterConfig = TableFormatterConfig.load();
Column[] tableColumns = {
new Column("Network"),
new Column("Result"),
new Column("Metrics"),
};
return PrintOnlineWorkflowUtils.createFormatter(tableFormatterConfig, outputFormat, outputFile, TABLE_TITLE, tableColumns);
}
private void printTableEntry(TableFormatter formatter, Network network, LoadFlowResult result) {
try {
formatter.writeCell(network.getId());
formatter.writeCell(result.isOk() ? "ok" : "nok");
formatter.writeCell(result.getMetrics().toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void run(CommandLine line) throws Exception {
Path caseFile = Paths.get(line.getOptionValue("case-file"));
Path outputFile = (line.hasOption("output-file")) ? Paths.get(line.getOptionValue("output-file")) : null;
String outputFormat = (line.hasOption("output-format")) ? line.getOptionValue("output-format") : "ascii";
boolean skipPostProc = line.hasOption("skip-postproc");
ComponentDefaultConfig defaultConfig = ComponentDefaultConfig.load();
try (ComputationManager computationManager = new LocalComputationManager(); TableFormatter formatter = getFormatter(outputFormat, outputFile)) {
ImportConfig importConfig = (skipPostProc == false) ? ImportConfig.load() : new ImportConfig();
if (Files.isRegularFile(caseFile)) {
Network network = Importers.loadNetwork(caseFile, computationManager, importConfig, null);
if (network == null) {
throw new RuntimeException("Case '" + caseFile + "' not found");
}
LoadFlow loadFlow = defaultConfig.newFactoryImpl(LoadFlowFactory.class).create(network, computationManager, 0);
printTableEntry(formatter, network, loadFlow.run());
} else if (Files.isDirectory(caseFile)) {
Importers.loadNetworks(caseFile, false, computationManager, importConfig, network -> {
try {
LoadFlow loadFlow = defaultConfig.newFactoryImpl(LoadFlowFactory.class).create(network, computationManager, 0);
printTableEntry(formatter, network, loadFlow.run());
} catch (Exception e) {
e.printStackTrace();
}
}, dataSource -> System.out.println("loading case " + dataSource.getBaseName()));
}
}
}
}