Skip to content

Commit

Permalink
aligns to powsybl-core v2.1.0 apis
Browse files Browse the repository at this point in the history
  • Loading branch information
CBiasuzzi authored and sylvlecl committed Nov 2, 2018
1 parent 1a96050 commit dc53ec2
Show file tree
Hide file tree
Showing 45 changed files with 313 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,9 @@ public String getVersion() {
return "1.0.0";
}

@Override
public LoadFlowResult run(LoadFlowParameters parameters) throws Exception {
Boolean wres = runAmplTask(network.getStateManager().getWorkingStateId(), parameters).join();
return new LoadFlowResultImpl(wres, new HashMap<>(), null);
}

@Override
public CompletableFuture<LoadFlowResult> runAsync(String workingStateId, LoadFlowParameters parameters) {
public CompletableFuture<LoadFlowResult> run(String workingStateId, LoadFlowParameters parameters) {
return runAmplTask(workingStateId, parameters)
.thenApply(x -> new LoadFlowResultImpl(x, new HashMap<>(), null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import com.google.auto.service.AutoService;
import com.powsybl.commons.config.ComponentDefaultConfig;
import com.powsybl.iidm.network.StateManagerConstants;
import com.powsybl.tools.Command;
import com.powsybl.tools.Tool;
import com.powsybl.tools.ToolRunningContext;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.import_.Importers;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.StateManager;
import com.powsybl.loadflow.LoadFlowFactory;
import com.powsybl.simulation.SimulatorFactory;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -88,6 +88,6 @@ public void run(CommandLine line, ToolRunningContext context) throws Exception {
caseProjectorConfig = new CaseProjectorConfig(caseProjectorConfig.getAmplHomeDir(), Paths.get(line.getOptionValue("generators-domains-file")), caseProjectorConfig.isDebug());
}
new CaseProjector(network, LocalComputationManager.getDefault(), loadFlowFactory, simulatorFactory, caseProjectorConfig)
.project(StateManager.INITIAL_STATE_ID).join();
.project(StateManagerConstants.INITIAL_STATE_ID).join();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import com.powsybl.commons.Version;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.computation.*;
import com.powsybl.contingency.*;
import eu.itesla_project.dymola.contingency.*;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyElement;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.util.Networks;
import com.powsybl.loadflow.LoadFlowFactory;
import eu.itesla_project.modelica_events_adder.events.ModEventsExport;
import eu.itesla_project.modelica_export.ModelicaMainExporter;
import com.powsybl.simulation.*;
import com.powsybl.simulation.securityindexes.SecurityIndex;
import com.powsybl.simulation.securityindexes.SecurityIndexParser;
import com.powsybl.simulation.*;
import eu.itesla_project.dymola.contingency.*;
import eu.itesla_project.modelica_events_adder.events.ModEventsExport;
import eu.itesla_project.modelica_export.ModelicaMainExporter;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SubnodeConfiguration;
Expand All @@ -33,26 +35,28 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

import static com.powsybl.computation.FilePreProcessor.ARCHIVE_UNZIP;

/**
*
* @author Quinary <itesla@quinary.com>
*/
public class DymolaImpactAnalysis implements ImpactAnalysis {
private static final Logger LOGGER = LoggerFactory.getLogger(DymolaImpactAnalysis.class);

private static final String WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME = "_wp43_smallsignal_security_indexes.xml";
private static final String WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME = "_wp43_smallsignal_security_indexes.xml";
private static final String WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME = "_wp43_transient_security_indexes.xml";
private static final String WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME = "_wp43_overload_security_indexes.xml";
private static final String WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME = "_wp43_underovervoltage_security_indexes.xml";

private static final String WP43_SMALLSIGNAL_SECURITY_INPUT_FILE_NAME = "_wp43_smallsignal.mat";
private static final String WP43_SMALLSIGNAL_SECURITY_INPUT_FILE_NAME = "_wp43_smallsignal.mat";
private static final String WP43_TRANSIENT_SECURITY_INPUT_FILE_NAME = "_wp43_transient.mat";
private static final String WP43_OVERLOAD_SECURITY_INPUT_FILE_NAME = "_wp43_overload.mat";
private static final String WP43_UNDEROVERVOLTAGE_SECURITY_INPUT_FILE_NAME = "_wp43_underovervoltage.mat";
Expand Down Expand Up @@ -165,7 +169,7 @@ private void readSecurityIndexes(List<Contingency> contingencies, Path workingDi
prefixFile + WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME,
prefixFile + WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME,
prefixFile + WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME)) {
Path file = workingDir.resolve(securityIndexFileName.replace(Command.EXECUTION_NUMBER_PATTERN, Integer.toString(i)));
Path file = workingDir.resolve(securityIndexFileName.replace(CommandConstants.EXECUTION_NUMBER_PATTERN, Integer.toString(i)));
LOGGER.info("reading indexes output from file {} ", file);
if (Files.exists(file)) {
try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
Expand Down Expand Up @@ -208,7 +212,7 @@ public ImpactAnalysisResult run(SimulationState state) throws Exception {
return run(state, null);
}

private Command before(SimulationState state, Set<String> contingencyIds, Path workingDir, List<Contingency> contingencies) throws IOException {
private Command createCommand(SimulationState state, Set<String> contingencyIds, Path workingDir, List<Contingency> contingencies) throws IOException {
// dump state info for debugging
if (config.isDebug()) {
Networks.dumpStateId(workingDir, state.getName());
Expand Down Expand Up @@ -246,7 +250,7 @@ private Command before(SimulationState state, Set<String> contingencyIds, Path w
return cmd;
}

private ImpactAnalysisResult after(Path workingDir, List<Contingency> contingencies, ExecutionReport report) throws IOException {
private ImpactAnalysisResult getResult(Path workingDir, List<Contingency> contingencies, ExecutionReport report) throws IOException {
report.log();

// read security indexes files generated by impact analysis
Expand All @@ -260,19 +264,7 @@ private ImpactAnalysisResult after(Path workingDir, List<Contingency> contingenc

@Override
public ImpactAnalysisResult run(SimulationState state, Set<String> contingencyIds) throws Exception {
checkState(state);

try (CommandExecutor executor = computationManager.newCommandExecutor(DymolaUtil.createEnv(config), WORKING_DIR_PREFIX, config.isDebug())) {
Path workingDir = executor.getWorkingDir();

List<Contingency> contingencies = new ArrayList<>();
Command cmd = before(state, contingencyIds, workingDir, contingencies);

// start execution
ExecutionReport report = executor.start(new CommandExecution(cmd, contingencies.size(), priority, ImmutableMap.of("state", state.getName())));

return after(workingDir, contingencies, report);
}
return runAsync(state, contingencyIds, null).join();
}

@Override
Expand All @@ -286,20 +278,13 @@ public CompletableFuture<ImpactAnalysisResult> runAsync(SimulationState state, S

@Override
public List<CommandExecution> before(Path workingDir) throws IOException {
Command cmd = DymolaImpactAnalysis.this.before(state, contingencyIds, workingDir, contingencies);
Command cmd = DymolaImpactAnalysis.this.createCommand(state, contingencyIds, workingDir, contingencies);
return Arrays.asList(new CommandExecution(cmd, contingencies.size(), priority, ImmutableMap.of("state", state.getName())));
}

@Override
public void onProgress(CommandExecution execution, int executionIndex) {
if (listener != null) {
listener.onProgress(executionIndex);
}
}

@Override
public ImpactAnalysisResult after(Path workingDir, ExecutionReport report) throws IOException {
return DymolaImpactAnalysis.this.after(workingDir, contingencies, report);
return DymolaImpactAnalysis.this.getResult(workingDir, contingencies, report);
}
});
}
Expand All @@ -326,37 +311,37 @@ private Command createCommand(String inputScenarioZipPrefixFileName, String matF

return new GroupCommandBuilder()
.id("dym_imp")
.inputFiles(new InputFile(inputScenarioZipPrefixFileName + "_" + Command.EXECUTION_NUMBER_PATTERN + ".zip"),
new InputFile(inputScenarioZipPrefixFileName + "_" + Command.EXECUTION_NUMBER_PATTERN + "_pars.zip", ARCHIVE_UNZIP))
.inputFiles(new InputFile(inputScenarioZipPrefixFileName + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".zip"),
new InputFile(inputScenarioZipPrefixFileName + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + "_pars.zip", ARCHIVE_UNZIP))
.subCommand()
.program(REMOTE_DYMOLA_SCRIPT)
.args(modelFileName, modelNamePrefix + Command.EXECUTION_NUMBER_PATTERN, "./", Command.EXECUTION_NUMBER_PATTERN, config.getDymolaSeviceWSDL(),
.args(modelFileName, modelNamePrefix + CommandConstants.EXECUTION_NUMBER_PATTERN, "./", CommandConstants.EXECUTION_NUMBER_PATTERN, config.getDymolaSeviceWSDL(),
"" + startSimulationTime, "" + endSimulationTime, "" + simConfig.getNumberOfIntervals(),
"" + simConfig.getOutputInterval(), "" + simConfig.getMethod(), "" + simConfig.getTolerance(),
"" + simConfig.getOutputFixedstepSize(),
"" + DymolaUtil.DYMOLAINPUTZIPFILENAMEPREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + ".zip",
"" + DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + ".zip",
"" + DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN,
"" + DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + ".txt",
"" + DymolaUtil.DYMOLAINPUTZIPFILENAMEPREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".zip",
"" + DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".zip",
"" + DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN,
"" + DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".txt",
"" + config.isFakeDymolaExecution())
.timeout(config.getSimTimeout())
.add()
.subCommand()
.program(WP43_SCRIPT)
.args("./", DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN, String.join(",", config.getIndexesNames()))
.args("./", DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN, String.join(",", config.getIndexesNames()))
.timeout(config.getIdxTimeout())
.add()
.outputFiles(
new OutputFile(DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + ".zip"),
new OutputFile(DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + ".txt"),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_SMALLSIGNAL_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_TRANSIENT_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_OVERLOAD_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + Command.EXECUTION_NUMBER_PATTERN + WP43_UNDEROVERVOLTAGE_SECURITY_INPUT_FILE_NAME)
new OutputFile(DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".zip"),
new OutputFile(DymolaUtil.DYMOLAOUTPUTZIPFILENAMEPREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".txt"),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_SMALLSIGNAL_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_TRANSIENT_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_OVERLOAD_SECURITY_INPUT_FILE_NAME),
new OutputFile(DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + CommandConstants.EXECUTION_NUMBER_PATTERN + WP43_UNDEROVERVOLTAGE_SECURITY_INPUT_FILE_NAME)
)
.build();
}
Expand All @@ -370,7 +355,7 @@ private void writeModelicaExporterContingenciesFile(Path eventsPath, List<Contin
if (contingency.getElements().isEmpty()) {
throw new AssertionError("Empty contingency " + contingency.getId());
}
for (ContingencyElement element: contingency.getElements()) {
for (ContingencyElement element : contingency.getElements()) {
csvWriter.write(element.toString());
csvWriter.newLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<Contingency> getContingencies(Network network) {
if (contingency == null) {
List<ContingencyElement> elements = new ArrayList<>();
elements.add(newElement);
contingency = new ContingencyImpl(contingencyId, elements);
contingency = new Contingency(contingencyId, elements);
contingenciesMap.put(contingencyId, contingency);
} else {
contingency.getElements().add(newElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static Bus selectSlackbusCriteria1(Network network, EurostagEchExportCon
return StreamSupport.stream(EchUtil.getBuses(network, config).spliterator(), false)
.sorted((b1, b2) -> b1.getId().compareTo(b2.getId()))
.filter(b -> !busesToAvoid.contains(b.getId())
&& b.getConnectedComponent() != null && b.getConnectedComponent().getNum() == Component.MAIN_NUM)
&& b.getConnectedComponent() != null && b.getConnectedComponent().getNum() == ComponentConstants.MAIN_NUM)
.map(b -> decorate(b))
.filter(db -> db.regulatingGenerator > 0 && db.maxP > 100) // only keep bus with a regulating generator and a pmax > 100 MW
.sorted((db1, db2) -> Float.compare((db1.maxP - db1.minP) / 2 - db1.targetP, (db2.maxP - db2.minP) / 2 - db2.targetP)) // select first bus with a high margin
Expand All @@ -173,16 +173,16 @@ private static Bus selectSlackbusCriteria1(Network network, EurostagEchExportCon
}

public static boolean isInMainCc(Bus bus) {
return ConnectedComponents.getCcNum(bus) == Component.MAIN_NUM;
return ConnectedComponents.getCcNum(bus) == ComponentConstants.MAIN_NUM;
}

public static boolean isInMainCc(Injection injection, boolean noswitch) {
return ConnectedComponents.getCcNum(EchUtil.getBus(injection.getTerminal(), noswitch)) == Component.MAIN_NUM;
return ConnectedComponents.getCcNum(EchUtil.getBus(injection.getTerminal(), noswitch)) == ComponentConstants.MAIN_NUM;
}

public static boolean isInMainCc(Branch branch, boolean noswitch) {
return (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal1(), noswitch)) == Component.MAIN_NUM)
&& (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal2(), noswitch)) == Component.MAIN_NUM);
return (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal1(), noswitch)) == ComponentConstants.MAIN_NUM)
&& (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal2(), noswitch)) == ComponentConstants.MAIN_NUM);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/
package eu.itesla_project.eurostag;

import com.powsybl.computation.Command;
import com.powsybl.computation.CommandConstants;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public interface EurostagConstants {
Expand All @@ -21,7 +20,7 @@ public interface EurostagConstants {
String PRE_FAULT_SAC_FILE_NAME = PRE_FAULT_SEQ_FILE_NAME.replace(".seq", ".sac");
String PRE_FAULT_SAC_GZ_FILE_NAME = PRE_FAULT_SAC_FILE_NAME + ".gz";

String FAULT_SEQ_FILE_NAME = "sim_fault_" + Command.EXECUTION_NUMBER_PATTERN + ".seq";
String FAULT_SEQ_FILE_NAME = "sim_fault_" + CommandConstants.EXECUTION_NUMBER_PATTERN + ".seq";

String ALL_SCENARIOS_ZIP_FILE_NAME = "eurostag-all-scenarios.zip";

Expand Down
Loading

0 comments on commit dc53ec2

Please sign in to comment.