Skip to content

Commit 6466809

Browse files
Add debugDir parameter to dynamic-simulation, dynamic-security-analysis, shortcircuit and ampl-executor (#3455)
* Add debugDir parameter to dynmaic-simulation run() method * Move debugDir to DynamicSimulationParameters * copy debug files to debugDir/workingDir + use getFileSystem() * fix unit test dynamic simulation parameter * ShortcircuitParameters 1.4 * add debugDir for DynamicSecurityAnalysisParameters * Manage version for DynamicSimulationParameters * increase version for DynamicSecurityAnalysisParameters * manage retro compatibility in DynamicSimulationParametersDeserializer * Add debugDir to AmplParameters * fix DynamicSimulationParameters constr * Rename debugDir to dumpDir in ExecutionEnvironment * Add documentation * do not serialize debugDir if null Signed-off-by: Etienne Homer <[email protected]> Co-authored-by: Thang PHAM <[email protected]>
1 parent 102859a commit 6466809

File tree

37 files changed

+297
-47
lines changed

37 files changed

+297
-47
lines changed

ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplModelRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static AmplResults run(Network network, String variantId, AmplModel model
2929

3030
public static CompletableFuture<AmplResults> runAsync(Network network, String variantId, AmplModel model, ComputationManager manager,
3131
AmplParameters parameters) {
32-
ExecutionEnvironment env = new ExecutionEnvironment(Collections.emptyMap(), "ampl_", parameters.isDebug());
32+
ExecutionEnvironment env = new ExecutionEnvironment(Collections.emptyMap(), "ampl_", parameters.isDebug(), parameters.getDebugDir());
3333
AmplModelExecutionHandler handler = new AmplModelExecutionHandler(model, network, variantId, AmplConfig.load(),
3434
parameters);
3535
return manager.execute(env, handler);

ampl-executor/src/main/java/com/powsybl/ampl/executor/AmplParameters.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public interface AmplParameters {
3636
*/
3737
boolean isDebug();
3838

39+
/**
40+
* Get the directory where execution files will be dumped
41+
*
42+
* @return the debug directory
43+
*/
44+
String getDebugDir();
45+
3946
/**
4047
* Configuration for AmplExporter
4148
*/

ampl-executor/src/main/java/com/powsybl/ampl/executor/EmptyAmplParameters.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public boolean isDebug() {
3232
return false;
3333
}
3434

35+
@Override
36+
public String getDebugDir() {
37+
return null;
38+
}
39+
3540
@Override
3641
public AmplExportConfig getAmplExportConfig() {
3742
return null;

ampl-executor/src/test/java/com/powsybl/ampl/executor/SimpleAmplParameters.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public boolean isDebug() {
7373
return true;
7474
}
7575

76+
@Override
77+
public String getDebugDir() {
78+
return null;
79+
}
80+
7681
@Override
7782
public AmplExportConfig getAmplExportConfig() {
7883
return new AmplExportConfig(AmplExportConfig.ExportScope.ALL, false, AmplExportConfig.ExportActionType.CURATIVE, false, false, AmplExportVersion.defaultVersion(), false);

computation-local/src/main/java/com/powsybl/computation/local/LocalComputationManager.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.common.io.ByteStreams;
1313
import com.powsybl.commons.PowsyblException;
1414
import com.powsybl.commons.config.PlatformConfig;
15+
import com.powsybl.commons.io.FileUtil;
1516
import com.powsybl.commons.io.WorkingDirectory;
1617
import com.powsybl.computation.*;
1718
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -20,10 +21,7 @@
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

23-
import java.io.IOException;
24-
import java.io.InputStream;
25-
import java.io.OutputStream;
26-
import java.io.UncheckedIOException;
24+
import java.io.*;
2725
import java.nio.file.Files;
2826
import java.nio.file.Path;
2927
import java.util.*;
@@ -146,7 +144,7 @@ private interface ExecutionMonitor {
146144

147145
}
148146

149-
private ExecutionReport execute(Path workingDir, List<CommandExecution> commandExecutionList, Map<String, String> variables, ComputationParameters computationParameters, ExecutionMonitor monitor)
147+
private ExecutionReport execute(Path workingDir, Path dumpDir, List<CommandExecution> commandExecutionList, Map<String, String> variables, ComputationParameters computationParameters, ExecutionMonitor monitor)
150148
throws InterruptedException {
151149
// TODO concurrent
152150
List<ExecutionError> errors = new ArrayList<>();
@@ -155,7 +153,7 @@ private ExecutionReport execute(Path workingDir, List<CommandExecution> commandE
155153
for (CommandExecution commandExecution : commandExecutionList) {
156154
Command command = commandExecution.getCommand();
157155
CountDownLatch latch = new CountDownLatch(commandExecution.getExecutionCount());
158-
ExecutionParameters executionParameters = new ExecutionParameters(workingDir, commandExecution, variables, computationParameters, executionSubmitter,
156+
ExecutionParameters executionParameters = new ExecutionParameters(workingDir, dumpDir, commandExecution, variables, computationParameters, executionSubmitter,
159157
command, latch, errors, monitor);
160158
IntStream.range(0, commandExecution.getExecutionCount()).forEach(idx -> performSingleExecution(executionParameters, idx));
161159
latch.await();
@@ -173,7 +171,7 @@ private ExecutionReport execute(Path workingDir, List<CommandExecution> commandE
173171
return new DefaultExecutionReport(workingDir, errors);
174172
}
175173

176-
private record ExecutionParameters(Path workingDir, CommandExecution commandExecution,
174+
private record ExecutionParameters(Path workingDir, Path dumpDir, CommandExecution commandExecution,
177175
Map<String, String> variables, ComputationParameters computationParameters,
178176
ExecutorService executionSubmitter, Command command, CountDownLatch latch,
179177
List<ExecutionError> errors, ExecutionMonitor monitor) {
@@ -207,6 +205,17 @@ private void performSingleExecution(ExecutionParameters executionParameters, int
207205
} catch (Exception e) {
208206
LOGGER.warn(e.getMessage(), e);
209207
} finally {
208+
if (executionParameters.dumpDir != null) {
209+
try {
210+
Path sourcePath = executionParameters.workingDir;
211+
Path destinationPath = executionParameters.dumpDir.resolve(executionParameters.workingDir.getFileName());
212+
FileUtil.createDirectory(destinationPath);
213+
FileUtil.copyDir(sourcePath, destinationPath);
214+
215+
} catch (IOException e) {
216+
LOGGER.warn(e.getMessage(), e);
217+
}
218+
}
210219
executionParameters.latch.countDown();
211220
exit();
212221
}
@@ -363,8 +372,14 @@ private <R> R doExecute(ExecutionEnvironment environment, ExecutionHandler<R> ha
363372
List<CommandExecution> commandExecutionList = handler.before(workingDir.toPath());
364373

365374
ExecutionReport report;
375+
366376
try {
367-
report = execute(workingDir.toPath(), commandExecutionList, environment.getVariables(), parameters, handler::onExecutionCompletion);
377+
report = execute(workingDir.toPath(),
378+
environment.getDumpDir() != null ? config.getLocalDir().getFileSystem().getPath(environment.getDumpDir()) : null,
379+
commandExecutionList,
380+
environment.getVariables(),
381+
parameters,
382+
handler::onExecutionCompletion);
368383
} catch (InterruptedException exc) {
369384
localCommandExecutor.stop(workingDir.toPath());
370385
throw exc;

computation-local/src/test/java/com/powsybl/computation/local/LocalComputationManagerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class LocalComputationManagerTest {
4646

4747
private Path localDir;
4848

49+
private static final String DEBUG_DIR = "/tmp/debugDir";
50+
4951
private LocalComputationConfig config;
5052

5153
@BeforeEach
@@ -98,7 +100,7 @@ public int execute(String program, List<String> args, Path outFile, Path errFile
98100
}
99101
};
100102
try (ComputationManager computationManager = new LocalComputationManager(config, localCommandExecutor, ForkJoinPool.commonPool())) {
101-
computationManager.execute(new ExecutionEnvironment(ImmutableMap.of("var1", "val1"), PREFIX, false),
103+
computationManager.execute(new ExecutionEnvironment(ImmutableMap.of("var1", "val1"), PREFIX, false, DEBUG_DIR),
102104
new AbstractExecutionHandler<Object>() {
103105
@Override
104106
public List<CommandExecution> before(Path workingDir) throws IOException {
@@ -131,7 +133,7 @@ public Object after(Path workingDir, ExecutionReport report) throws IOException
131133
assertEquals("prog1_cmd", report.getErrors().get(0).getCommand().getId());
132134
assertEquals(0, report.getErrors().get(0).getIndex());
133135
assertEquals(1, report.getErrors().get(0).getExitCode());
134-
136+
assertTrue(Files.exists(fileSystem.getPath(DEBUG_DIR).resolve(workingDir)));
135137
return null;
136138
}
137139
}).join();

computation/src/main/java/com/powsybl/computation/ExecutionEnvironment.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* <li>a map of environment variables</li>
1919
* <li>a prefix for the execution working directory</li>
2020
* <li>a debug indicator</li>
21+
<li>the directory where execution files will be dumped</li>
2122
* </ul>
2223
*
2324
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
@@ -32,12 +33,28 @@ public static ExecutionEnvironment createDefault() {
3233

3334
private String workingDirPrefix;
3435

36+
/**
37+
* If debug=true, execution files generated for and by the binary model will not be removed after the computation. There are available in the workingDir.
38+
* If debug=false, the workingDir which contains execution files is removed after computation.
39+
*/
3540
private boolean debug;
3641

37-
public ExecutionEnvironment(Map<String, String> variables, String workingDirPrefix, boolean debug) {
42+
/**
43+
* If dumpDir is provided, in/out execution files are dumped in dumpDir.
44+
* If dumpDir=null, nothing is dumped.
45+
* debug and dumpDir have fully independent behaviors.
46+
*/
47+
private String dumpDir;
48+
49+
public ExecutionEnvironment(Map<String, String> variables, String workingDirPrefix, boolean debug, String dumpDir) {
3850
this.variables = Objects.requireNonNull(variables);
3951
this.workingDirPrefix = Objects.requireNonNull(workingDirPrefix);
4052
this.debug = debug;
53+
this.dumpDir = dumpDir;
54+
}
55+
56+
public ExecutionEnvironment(Map<String, String> variables, String workingDirPrefix, boolean debug) {
57+
this(variables, workingDirPrefix, debug, null);
4158
}
4259

4360
public Map<String, String> getVariables() {
@@ -66,4 +83,8 @@ public ExecutionEnvironment setDebug(boolean debug) {
6683
this.debug = debug;
6784
return this;
6885
}
86+
87+
public String getDumpDir() {
88+
return dumpDir;
89+
}
6990
}

docs/simulation/dynamic/configuration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ You may configure some generic parameters for all implementations:
2626
dynamic-simulation-default-parameters:
2727
startTime: 0
2828
stopTime: 1
29+
debugDir: /tmp/debugDir
2930
```
3031
3132
The parameters may also be overridden with a JSON file, in which case the configuration will look like:
@@ -34,6 +35,7 @@ The parameters may also be overridden with a JSON file, in which case the config
3435
"version" : "1.0",
3536
"startTime" : 0,
3637
"stopTime" : 1,
38+
"debugDir": "/tmp/debugDir",
3739
"extensions" : {
3840
...
3941
}
@@ -48,6 +50,9 @@ The parameters may also be overridden with a JSON file, in which case the config
4850
**stopTime**
4951
`stopTime` defines when the simulation stops, in seconds. The default value of this property is `1`.
5052

53+
**debugDir**
54+
This property specifies the directory path where debug files will be dumped. If `null`, no file will be dumped.
55+
5156
### Specific parameters
5257
Some implementations use specific parameters that can be defined in the configuration file or in the JSON parameters file:
5358
- [Dynawo](inv:powsybldynawo:*:*#dynamic_simulation/configuration)
@@ -59,12 +64,14 @@ Some implementations use specific parameters that can be defined in the configur
5964
dynamic-simulation-default-parameters:
6065
startTime: 0
6166
stopTime: 3600
67+
debugDir: /tmp/debugDir
6268
```
6369
6470
**XML configuration:**
6571
```xml
6672
<dynamic-simulation-default-parameters>
6773
<startTime>0</startTime>
6874
<stopTime>3600</stopTime>
75+
<debugDir>/tmp/debugDir</debugDir>
6976
</dynamic-simulation-default-parameters>
7077
```

docs/simulation/dynamic_security/configuration.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dynamic-simulation-default-parameters:
3030

3131
dynamic-security-analysis-default-parameters:
3232
contingencies-start-time: 10
33+
debugDir: /tmp/debugDir
3334
```
3435
3536
The parameters may also be overridden with a JSON file, in which case the configuration will look like:
@@ -43,7 +44,8 @@ The parameters may also be overridden with a JSON file, in which case the config
4344
},
4445
"contingencies-parameters" : {
4546
"contingencies-start-time" : 5.5
46-
}
47+
},
48+
"debugDir": "/tmp/debugDir"
4749
}
4850
```
4951

@@ -52,17 +54,22 @@ The parameters may also be overridden with a JSON file, in which case the config
5254
**contingencies-start-time**
5355
`contingencies-start-time` defines when the contingencies start, in seconds. The default value of this property is `5`.
5456

57+
**debugDir**
58+
This property specifies the directory path where debug files will be dumped. If `null`, no file will be dumped.
59+
5560
### Examples
5661

5762
**YAML configuration:**
5863
```yaml
5964
dynamic-security-analysis-default-parameters:
6065
contingencies-start-time: 10
66+
debugDir: /tmp/debugDir
6167
```
6268
6369
**XML configuration:**
6470
```xml
6571
<dynamic-security-analysis-default-parameters>
6672
<contingencies-start-time>10</contingencies-start-time>
73+
<debugDir>/tmp/debugDir</debugDir>
6774
</dynamic-security-analysis-default-parameters>
6875
```

docs/simulation/shortcircuit/parameters.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ short-circuit-parameters:
1717
with-neutral-position: true
1818
initial-voltage-profile-mode: CONFIGURED
1919
voltage-ranges: /path/to/voltage/ranges/file
20+
detailedReport: true
21+
debugDir: null
2022
```
2123
2224
Available parameters in the short-circuit API are stored in `com.powsybl.shortcircuit.ShortCircuitParameters`. They are all optional.
@@ -136,6 +138,13 @@ Here is an example of this JSON file:
136138
]
137139
````
138140

141+
**detailedReport**
142+
This property indicates whether the computation should produce detailed reporting. If `true`, detailed reports are returned.
143+
If `false`, summarized reports are returned.
144+
145+
**debugDir**
146+
This property specifies the directory path where debug files will be dumped. If `null`, no file will be dumped.
147+
139148
(shortcircuit-fault-parameters)=
140149
## FaultParameters
141150

dynamic-security-analysis/src/main/java/com/powsybl/security/dynamic/DynamicSecurityAnalysisParameters.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
*/
2727
public class DynamicSecurityAnalysisParameters extends AbstractExtendable<DynamicSecurityAnalysisParameters> {
2828

29-
public static final String VERSION = "1.0";
29+
// VERSION = 1.0 dynamicSimulationParameters, contingenciesParameters
30+
// VERSION = 1.1 debugDir
31+
public static final String VERSION = "1.1";
3032

3133
private DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters();
3234
private ContingenciesParameters contingenciesParameters = new ContingenciesParameters();
35+
private String debugDir;
3336

3437
public static class ContingenciesParameters {
3538

@@ -112,6 +115,15 @@ public DynamicSecurityAnalysisParameters setDynamicContingenciesParameters(Conti
112115
return this;
113116
}
114117

118+
public String getDebugDir() {
119+
return debugDir;
120+
}
121+
122+
public DynamicSecurityAnalysisParameters setDebugDir(String debugDir) {
123+
this.debugDir = debugDir;
124+
return this;
125+
}
126+
115127
public void write(Path parametersPath) {
116128
JsonDynamicSecurityAnalysisParameters.write(this, parametersPath);
117129
}

dynamic-security-analysis/src/main/java/com/powsybl/security/dynamic/json/DynamicSecurityAnalysisParametersDeserializer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
*/
2828
public class DynamicSecurityAnalysisParametersDeserializer extends StdDeserializer<DynamicSecurityAnalysisParameters> {
2929

30+
private static final String CONTEXT_NAME = "DynamicSecurityAnalysisParameters";
31+
private static final String TAG = "Tag: ";
32+
3033
DynamicSecurityAnalysisParametersDeserializer() {
3134
super(DynamicSecurityAnalysisParameters.class);
3235
}
@@ -56,6 +59,11 @@ public DynamicSecurityAnalysisParameters deserialize(JsonParser parser, Deserial
5659
parser,
5760
DynamicSecurityAnalysisParameters.ContingenciesParameters.class));
5861
break;
62+
case "debugDir":
63+
JsonUtil.assertGreaterOrEqualThanReferenceVersion(CONTEXT_NAME, TAG + parser.currentName(), version, "1.1");
64+
parser.nextToken();
65+
parameters.setDebugDir(parser.readValueAs(String.class));
66+
break;
5967
case "extensions":
6068
parser.nextToken();
6169
extensions = JsonUtil.updateExtensions(parser, deserializationContext, getExtensionSerializers()::get, parameters);
@@ -64,10 +72,7 @@ public DynamicSecurityAnalysisParameters deserialize(JsonParser parser, Deserial
6472
throw new IllegalStateException("Unexpected field: " + parser.currentName());
6573
}
6674
}
67-
if (version == null || !version.equals("1.0")) {
68-
//Only 1.0 version is supported for now
69-
throw new IllegalStateException("Version different than 1.0 not supported.");
70-
}
75+
7176
extensions.forEach(extension -> parameters.addExtension((Class) extension.getClass(), extension));
7277
return parameters;
7378
}

dynamic-security-analysis/src/main/java/com/powsybl/security/dynamic/json/DynamicSecurityAnalysisParametersSerializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void serialize(DynamicSecurityAnalysisParameters parameters, JsonGenerato
3232
jsonGenerator.writeFieldName("dynamic-simulation-parameters");
3333
JsonDynamicSimulationParameters.serialize(parameters.getDynamicSimulationParameters(), jsonGenerator, serializerProvider);
3434
serializerProvider.defaultSerializeField("contingencies-parameters", parameters.getDynamicContingenciesParameters(), jsonGenerator);
35+
JsonUtil.writeOptionalStringField(jsonGenerator, "debugDir", parameters.getDebugDir());
3536
JsonUtil.writeExtensions(parameters, jsonGenerator, serializerProvider, JsonDynamicSecurityAnalysisParameters.getExtensionSerializers()::get);
3637
jsonGenerator.writeEndObject();
3738
}

0 commit comments

Comments
 (0)